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     switch(type) {
14         case kVoid_GrSLType:
15             return 0;
16         case kFloat_GrSLType: // fall through
17         case kHalf_GrSLType:
18             return 1;
19         case kFloat2_GrSLType: // fall through
20         case kHalf2_GrSLType:
21             return 1;
22         case kFloat3_GrSLType:
23         case kHalf3_GrSLType:
24             return 1;
25         case kFloat4_GrSLType:
26         case kHalf4_GrSLType:
27             return 1;
28         case kUint2_GrSLType:
29             return 1;
30         case kInt2_GrSLType:
31         case kShort2_GrSLType:
32         case kUShort2_GrSLType:
33         case kByte2_GrSLType:
34         case kUByte2_GrSLType:
35             return 1;
36         case kInt3_GrSLType:
37         case kShort3_GrSLType:
38         case kUShort3_GrSLType:
39         case kByte3_GrSLType:
40         case kUByte3_GrSLType:
41             return 1;
42         case kInt4_GrSLType:
43         case kShort4_GrSLType:
44         case kUShort4_GrSLType:
45         case kByte4_GrSLType:
46         case kUByte4_GrSLType:
47             return 1;
48         case kFloat2x2_GrSLType:
49         case kHalf2x2_GrSLType:
50             return 2;
51         case kFloat3x3_GrSLType:
52         case kHalf3x3_GrSLType:
53             return 3;
54         case kFloat4x4_GrSLType:
55         case kHalf4x4_GrSLType:
56             return 4;
57         case kTexture2DSampler_GrSLType:
58             return 0;
59         case kTextureExternalSampler_GrSLType:
60              return 0;
61         case kTexture2DRectSampler_GrSLType:
62              return 0;
63         case kBool_GrSLType:
64              return 1;
65         case kInt_GrSLType: // fall through
66         case kShort_GrSLType:
67         case kByte_GrSLType:
68              return 1;
69         case kUint_GrSLType: // fall through
70         case kUShort_GrSLType:
71         case kUByte_GrSLType:
72              return 1;
73         case kTexture2D_GrSLType:
74              return 0;
75         case kSampler_GrSLType:
76              return 0;
77         case kInput_GrSLType:
78             return 0;
79     }
80     SK_ABORT("Unexpected type");
81 }
82 
finalize_helper(GrSPIRVVaryingHandler::VarArray & vars)83 static void finalize_helper(GrSPIRVVaryingHandler::VarArray& vars) {
84     int locationIndex = 0;
85     for (GrShaderVar& var : vars.items()) {
86         SkString location;
87         location.appendf("location = %d", locationIndex);
88         var.addLayoutQualifier(location.c_str());
89 
90         int elementSize = grsltype_to_location_size(var.getType());
91         SkASSERT(elementSize > 0);
92         int numElements = 1;
93         if (var.isArray() && !var.isUnsizedArray()) {
94             numElements = var.getArrayCount();
95         }
96         SkASSERT(numElements > 0);
97         locationIndex += elementSize * numElements;
98     }
99     // TODO: determine the layout limits for SPIR-V, and enforce them via asserts here.
100 }
101 
onFinalize()102 void GrSPIRVVaryingHandler::onFinalize() {
103     finalize_helper(fVertexInputs);
104     finalize_helper(fVertexOutputs);
105     finalize_helper(fGeomInputs);
106     finalize_helper(fGeomOutputs);
107     finalize_helper(fFragInputs);
108     finalize_helper(fFragOutputs);
109 }
110