• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 == 49);
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         case kByte2_GrSLType:
36         case kUByte2_GrSLType:
37             return 1;
38         case kInt3_GrSLType:
39         case kUint3_GrSLType:
40         case kShort3_GrSLType:
41         case kUShort3_GrSLType:
42         case kByte3_GrSLType:
43         case kUByte3_GrSLType:
44             return 1;
45         case kInt4_GrSLType:
46         case kUint4_GrSLType:
47         case kShort4_GrSLType:
48         case kUShort4_GrSLType:
49         case kByte4_GrSLType:
50         case kUByte4_GrSLType:
51             return 1;
52         case kFloat2x2_GrSLType:
53         case kHalf2x2_GrSLType:
54             return 2;
55         case kFloat3x3_GrSLType:
56         case kHalf3x3_GrSLType:
57             return 3;
58         case kFloat4x4_GrSLType:
59         case kHalf4x4_GrSLType:
60             return 4;
61         case kTexture2DSampler_GrSLType:
62             return 0;
63         case kTextureExternalSampler_GrSLType:
64              return 0;
65         case kTexture2DRectSampler_GrSLType:
66              return 0;
67         case kBool_GrSLType:
68         case kBool2_GrSLType:
69         case kBool3_GrSLType:
70         case kBool4_GrSLType:
71              return 1;
72         case kInt_GrSLType: // fall through
73         case kShort_GrSLType:
74         case kByte_GrSLType:
75              return 1;
76         case kUint_GrSLType: // fall through
77         case kUShort_GrSLType:
78         case kUByte_GrSLType:
79              return 1;
80         case kTexture2D_GrSLType:
81              return 0;
82         case kSampler_GrSLType:
83              return 0;
84         case kInput_GrSLType:
85             return 0;
86     }
87     SK_ABORT("Unexpected type");
88 }
89 
finalize_helper(GrSPIRVVaryingHandler::VarArray & vars)90 static void finalize_helper(GrSPIRVVaryingHandler::VarArray& vars) {
91     int locationIndex = 0;
92     for (GrShaderVar& var : vars.items()) {
93         SkString location;
94         location.appendf("location = %d", locationIndex);
95         var.addLayoutQualifier(location.c_str());
96 
97         int elementSize = grsltype_to_location_size(var.getType());
98         SkASSERT(elementSize > 0);
99         int numElements = 1;
100         if (var.isArray() && !var.isUnsizedArray()) {
101             numElements = var.getArrayCount();
102         }
103         SkASSERT(numElements > 0);
104         locationIndex += elementSize * numElements;
105     }
106     // TODO: determine the layout limits for SPIR-V, and enforce them via asserts here.
107 }
108 
onFinalize()109 void GrSPIRVVaryingHandler::onFinalize() {
110     finalize_helper(fVertexInputs);
111     finalize_helper(fVertexOutputs);
112     finalize_helper(fGeomInputs);
113     finalize_helper(fGeomOutputs);
114     finalize_helper(fFragInputs);
115     finalize_helper(fFragOutputs);
116 }
117