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. */
12static 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 }
78 SK_ABORT("Unexpected type");
79}
80
81static void finalize_helper(GrSPIRVVaryingHandler::VarArray& vars) {
82 int locationIndex = 0;
83 for (GrShaderVar& var : vars.items()) {
84 SkString location;
85 location.appendf("location = %d", locationIndex);
86 var.addLayoutQualifier(location.c_str());
87
88 int elementSize = grsltype_to_location_size(var.getType());
89 SkASSERT(elementSize > 0);
90 int numElements = 1;
91 if (var.isArray() && !var.isUnsizedArray()) {
92 numElements = var.getArrayCount();
93 }
94 SkASSERT(numElements > 0);
95 locationIndex += elementSize * numElements;
96 }
97 // TODO: determine the layout limits for SPIR-V, and enforce them via asserts here.
98}
99
100void GrSPIRVVaryingHandler::onFinalize() {
101 finalize_helper(fVertexInputs);
102 finalize_helper(fVertexOutputs);
103 finalize_helper(fGeomInputs);
104 finalize_helper(fGeomOutputs);
105 finalize_helper(fFragInputs);
106 finalize_helper(fFragOutputs);
107}
108