xref: /aosp_15_r20/external/skia/src/gpu/ganesh/GrSPIRVVaryingHandler.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2019 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/GrSPIRVVaryingHandler.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     // If a new GrSL type is added, this function will need to be updated.
19     static_assert(kSkSLTypeCount == 41);
20 
21     switch(type) {
22         case SkSLType::kVoid:
23             return 0;
24         case SkSLType::kFloat: // fall through
25         case SkSLType::kHalf:
26             return 1;
27         case SkSLType::kFloat2: // fall through
28         case SkSLType::kHalf2:
29             return 1;
30         case SkSLType::kFloat3:
31         case SkSLType::kHalf3:
32             return 1;
33         case SkSLType::kFloat4:
34         case SkSLType::kHalf4:
35             return 1;
36         case SkSLType::kInt2:
37         case SkSLType::kUInt2:
38         case SkSLType::kShort2:
39         case SkSLType::kUShort2:
40             return 1;
41         case SkSLType::kInt3:
42         case SkSLType::kUInt3:
43         case SkSLType::kShort3:
44         case SkSLType::kUShort3:
45             return 1;
46         case SkSLType::kInt4:
47         case SkSLType::kUInt4:
48         case SkSLType::kShort4:
49         case SkSLType::kUShort4:
50             return 1;
51         case SkSLType::kFloat2x2:
52         case SkSLType::kHalf2x2:
53             return 2;
54         case SkSLType::kFloat3x3:
55         case SkSLType::kHalf3x3:
56             return 3;
57         case SkSLType::kFloat4x4:
58         case SkSLType::kHalf4x4:
59             return 4;
60         case SkSLType::kTexture2DSampler:
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         case SkSLType::kTexture2D:
78              return 0;
79         case SkSLType::kSampler:
80              return 0;
81         case SkSLType::kInput:
82             return 0;
83     }
84     SK_ABORT("Unexpected type");
85 }
86 
finalize_helper(GrSPIRVVaryingHandler::VarArray & vars)87 static void finalize_helper(GrSPIRVVaryingHandler::VarArray& vars) {
88     int locationIndex = 0;
89     for (GrShaderVar& var : vars.items()) {
90         SkString location;
91         location.appendf("location = %d", locationIndex);
92         var.addLayoutQualifier(location.c_str());
93 
94         int elementSize = sksltype_to_location_size(var.getType());
95         SkASSERT(elementSize > 0);
96         int numElements = var.isArray() ? var.getArrayCount() : 1;
97         SkASSERT(numElements > 0);
98         locationIndex += elementSize * numElements;
99     }
100     // TODO: determine the layout limits for SPIR-V, and enforce them via asserts here.
101 }
102 
onFinalize()103 void GrSPIRVVaryingHandler::onFinalize() {
104     finalize_helper(fVertexInputs);
105     finalize_helper(fVertexOutputs);
106     finalize_helper(fFragInputs);
107     finalize_helper(fFragOutputs);
108 }
109