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/GrSPIRVVaryingHandler.h"
9 
10 /** Returns the number of locations take up by a given GrSLType. We assume that all
11     scalar values are 32 bits. */
grsltype_to_location_size(GrSLType type)12 static inline int grsltype_to_location_size(GrSLType type) {
13     // If a new GrSL type is added, this function will need to be updated.
14     static_assert(kGrSLTypeCount == 41);
15 
16     switch(type) {
17         case kVoid_GrSLType:
18             return 0;
19         case kFloat_GrSLType: // fall through
20         case kHalf_GrSLType:
21             return 1;
22         case kFloat2_GrSLType: // fall through
23         case kHalf2_GrSLType:
24             return 1;
25         case kFloat3_GrSLType:
26         case kHalf3_GrSLType:
27             return 1;
28         case kFloat4_GrSLType:
29         case kHalf4_GrSLType:
30             return 1;
31         case kInt2_GrSLType:
32         case kUInt2_GrSLType:
33         case kShort2_GrSLType:
34         case kUShort2_GrSLType:
35             return 1;
36         case kInt3_GrSLType:
37         case kUInt3_GrSLType:
38         case kShort3_GrSLType:
39         case kUShort3_GrSLType:
40             return 1;
41         case kInt4_GrSLType:
42         case kUInt4_GrSLType:
43         case kShort4_GrSLType:
44         case kUShort4_GrSLType:
45             return 1;
46         case kFloat2x2_GrSLType:
47         case kHalf2x2_GrSLType:
48             return 2;
49         case kFloat3x3_GrSLType:
50         case kHalf3x3_GrSLType:
51             return 3;
52         case kFloat4x4_GrSLType:
53         case kHalf4x4_GrSLType:
54             return 4;
55         case kTexture2DSampler_GrSLType:
56             return 0;
57         case kTextureExternalSampler_GrSLType:
58              return 0;
59         case kTexture2DRectSampler_GrSLType:
60              return 0;
61         case kBool_GrSLType:
62         case kBool2_GrSLType:
63         case kBool3_GrSLType:
64         case kBool4_GrSLType:
65              return 1;
66         case kInt_GrSLType: // fall through
67         case kShort_GrSLType:
68              return 1;
69         case kUInt_GrSLType: // fall through
70         case kUShort_GrSLType:
71              return 1;
72         case kTexture2D_GrSLType:
73              return 0;
74         case kSampler_GrSLType:
75              return 0;
76         case kInput_GrSLType:
77             return 0;
78     }
79     SK_ABORT("Unexpected type");
80 }
81 
finalize_helper(GrSPIRVVaryingHandler::VarArray& vars)82 static void finalize_helper(GrSPIRVVaryingHandler::VarArray& vars) {
83     int locationIndex = 0;
84     for (GrShaderVar& var : vars.items()) {
85         SkString location;
86         location.appendf("location = %d", locationIndex);
87         var.addLayoutQualifier(location.c_str());
88 
89         int elementSize = grsltype_to_location_size(var.getType());
90         SkASSERT(elementSize > 0);
91         int numElements = var.isArray() ? var.getArrayCount() : 1;
92         SkASSERT(numElements > 0);
93         locationIndex += elementSize * numElements;
94     }
95     // TODO: determine the layout limits for SPIR-V, and enforce them via asserts here.
96 }
97 
onFinalize()98 void GrSPIRVVaryingHandler::onFinalize() {
99     finalize_helper(fVertexInputs);
100     finalize_helper(fVertexOutputs);
101     finalize_helper(fFragInputs);
102     finalize_helper(fFragOutputs);
103 }
104