blob: 8b9045ad0f9bef492e0ce2fa299552421674d87f [file] [log] [blame]
Stephen White83d8a942019-07-31 15:03:34 -04001/*
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
Greg Danield59a91d2020-04-23 13:22:47 -04008#include "src/gpu/GrSPIRVVaryingHandler.h"
Stephen White83d8a942019-07-31 15:03:34 -04009
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) {
John Stiles9fc9b872021-02-01 15:50:32 -050013 // If a new GrSL type is added, this function will need to be updated.
14 static_assert(kGrSLTypeCount == 49);
15
Stephen White83d8a942019-07-31 15:03:34 -040016 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;
Stephen White83d8a942019-07-31 15:03:34 -040031 case kInt2_GrSLType:
John Stiles148224e2021-01-19 17:35:33 -050032 case kUint2_GrSLType:
Stephen White83d8a942019-07-31 15:03:34 -040033 case kShort2_GrSLType:
34 case kUShort2_GrSLType:
35 case kByte2_GrSLType:
36 case kUByte2_GrSLType:
37 return 1;
38 case kInt3_GrSLType:
John Stiles148224e2021-01-19 17:35:33 -050039 case kUint3_GrSLType:
Stephen White83d8a942019-07-31 15:03:34 -040040 case kShort3_GrSLType:
41 case kUShort3_GrSLType:
42 case kByte3_GrSLType:
43 case kUByte3_GrSLType:
44 return 1;
45 case kInt4_GrSLType:
John Stiles148224e2021-01-19 17:35:33 -050046 case kUint4_GrSLType:
Stephen White83d8a942019-07-31 15:03:34 -040047 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:
John Stiles9fc9b872021-02-01 15:50:32 -050068 case kBool2_GrSLType:
69 case kBool3_GrSLType:
70 case kBool4_GrSLType:
Stephen White83d8a942019-07-31 15:03:34 -040071 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;
Greg Daniel37fd6582020-09-14 12:36:09 -040084 case kInput_GrSLType:
85 return 0;
Stephen White83d8a942019-07-31 15:03:34 -040086 }
87 SK_ABORT("Unexpected type");
Stephen White83d8a942019-07-31 15:03:34 -040088}
89
Greg Danield59a91d2020-04-23 13:22:47 -040090static void finalize_helper(GrSPIRVVaryingHandler::VarArray& vars) {
Stephen White83d8a942019-07-31 15:03:34 -040091 int locationIndex = 0;
Michael Ludwig45191342020-03-24 12:29:39 -040092 for (GrShaderVar& var : vars.items()) {
Stephen White83d8a942019-07-31 15:03:34 -040093 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 }
Greg Danield59a91d2020-04-23 13:22:47 -0400106 // TODO: determine the layout limits for SPIR-V, and enforce them via asserts here.
Stephen White83d8a942019-07-31 15:03:34 -0400107}
108
Greg Danield59a91d2020-04-23 13:22:47 -0400109void GrSPIRVVaryingHandler::onFinalize() {
Stephen White83d8a942019-07-31 15:03:34 -0400110 finalize_helper(fVertexInputs);
111 finalize_helper(fVertexOutputs);
112 finalize_helper(fGeomInputs);
113 finalize_helper(fGeomOutputs);
114 finalize_helper(fFragInputs);
115 finalize_helper(fFragOutputs);
116}