xref: /aosp_15_r20/external/skia/src/gpu/ganesh/vk/GrVkVaryingHandler.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2016 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "src/gpu/ganesh/vk/GrVkVaryingHandler.h"
9 
10 #include "include/core/SkString.h"
11 #include "include/private/base/SkAssert.h"
12 #include "src/core/SkSLTypeShared.h"
13 #include "src/gpu/ganesh/GrShaderVar.h"
14 
15 /** Returns the number of locations take up by a given SkSLType. We assume that all
16     scalar values are 32 bits. */
sksltype_to_location_size(SkSLType type)17 static inline int sksltype_to_location_size(SkSLType type) {
18     switch(type) {
19         case SkSLType::kVoid:
20             return 0;
21         case SkSLType::kFloat: // fall through
22         case SkSLType::kHalf:
23             return 1;
24         case SkSLType::kFloat2: // fall through
25         case SkSLType::kHalf2:
26             return 1;
27         case SkSLType::kFloat3:
28         case SkSLType::kHalf3:
29             return 1;
30         case SkSLType::kFloat4:
31         case SkSLType::kHalf4:
32             return 1;
33         case SkSLType::kInt2:
34         case SkSLType::kUInt2:
35         case SkSLType::kShort2:
36         case SkSLType::kUShort2:
37             return 1;
38         case SkSLType::kInt3:
39         case SkSLType::kUInt3:
40         case SkSLType::kShort3:
41         case SkSLType::kUShort3:
42             return 1;
43         case SkSLType::kInt4:
44         case SkSLType::kUInt4:
45         case SkSLType::kShort4:
46         case SkSLType::kUShort4:
47             return 1;
48         case SkSLType::kFloat2x2:
49         case SkSLType::kHalf2x2:
50             return 2;
51         case SkSLType::kFloat3x3:
52         case SkSLType::kHalf3x3:
53             return 3;
54         case SkSLType::kFloat4x4:
55         case SkSLType::kHalf4x4:
56             return 4;
57         case SkSLType::kTexture2DSampler:
58         case SkSLType::kSampler:
59         case SkSLType::kTexture2D:
60         case SkSLType::kInput:
61             return 0;
62         case SkSLType::kTextureExternalSampler:
63              return 0;
64         case SkSLType::kTexture2DRectSampler:
65              return 0;
66         case SkSLType::kBool:
67         case SkSLType::kBool2:
68         case SkSLType::kBool3:
69         case SkSLType::kBool4:
70              return 1;
71         case SkSLType::kInt: // fall through
72         case SkSLType::kShort:
73              return 1;
74         case SkSLType::kUInt: // fall through
75         case SkSLType::kUShort:
76              return 1;
77     }
78     SK_ABORT("Unexpected type");
79 }
80 
finalize_helper(GrVkVaryingHandler::VarArray & vars)81 static void finalize_helper(GrVkVaryingHandler::VarArray& vars) {
82     int locationIndex = 0;
83     for (GrShaderVar& var : vars.items()) {
84         SkString location;
85         location.appendf("location = %d", locationIndex);
86         var.addLayoutQualifier(location.c_str());
87 
88         int elementSize = sksltype_to_location_size(var.getType());
89         SkASSERT(elementSize > 0);
90         int numElements = var.isArray() ? var.getArrayCount() : 1;
91         SkASSERT(numElements > 0);
92         locationIndex += elementSize * numElements;
93     }
94     // Vulkan requires at least 64 locations to be supported for both vertex output and fragment
95     // input. If we ever hit this assert, then we'll need to add a cap to actually check the
96     // supported input and output values and adjust our supported shaders based on those values.
97     SkASSERT(locationIndex <= 64);
98 }
99 
onFinalize()100 void GrVkVaryingHandler::onFinalize() {
101     finalize_helper(fVertexInputs);
102     finalize_helper(fVertexOutputs);
103     finalize_helper(fFragInputs);
104     finalize_helper(fFragOutputs);
105 }
106