• 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/ganesh/GrSPIRVVaryingHandler.h"
9 
10 /** Returns the number of locations take up by a given SkSLType. We assume that all
11     scalar values are 32 bits. */
sksltype_to_location_size(SkSLType type)12 static inline int sksltype_to_location_size(SkSLType type) {
13     // If a new GrSL type is added, this function will need to be updated.
14     static_assert(kSkSLTypeCount == 41);
15 
16     switch(type) {
17         case SkSLType::kVoid:
18             return 0;
19         case SkSLType::kFloat: // fall through
20         case SkSLType::kHalf:
21             return 1;
22         case SkSLType::kFloat2: // fall through
23         case SkSLType::kHalf2:
24             return 1;
25         case SkSLType::kFloat3:
26         case SkSLType::kHalf3:
27             return 1;
28         case SkSLType::kFloat4:
29         case SkSLType::kHalf4:
30             return 1;
31         case SkSLType::kInt2:
32         case SkSLType::kUInt2:
33         case SkSLType::kShort2:
34         case SkSLType::kUShort2:
35             return 1;
36         case SkSLType::kInt3:
37         case SkSLType::kUInt3:
38         case SkSLType::kShort3:
39         case SkSLType::kUShort3:
40             return 1;
41         case SkSLType::kInt4:
42         case SkSLType::kUInt4:
43         case SkSLType::kShort4:
44         case SkSLType::kUShort4:
45             return 1;
46         case SkSLType::kFloat2x2:
47         case SkSLType::kHalf2x2:
48             return 2;
49         case SkSLType::kFloat3x3:
50         case SkSLType::kHalf3x3:
51             return 3;
52         case SkSLType::kFloat4x4:
53         case SkSLType::kHalf4x4:
54             return 4;
55         case SkSLType::kTexture2DSampler:
56             return 0;
57         case SkSLType::kTextureExternalSampler:
58              return 0;
59         case SkSLType::kTexture2DRectSampler:
60              return 0;
61         case SkSLType::kBool:
62         case SkSLType::kBool2:
63         case SkSLType::kBool3:
64         case SkSLType::kBool4:
65              return 1;
66         case SkSLType::kInt: // fall through
67         case SkSLType::kShort:
68              return 1;
69         case SkSLType::kUInt: // fall through
70         case SkSLType::kUShort:
71              return 1;
72         case SkSLType::kTexture2D:
73              return 0;
74         case SkSLType::kSampler:
75              return 0;
76         case SkSLType::kInput:
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 = sksltype_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