1 /*
2 * Copyright 2022 Google LLC
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 #ifndef SkSLTypeShared_DEFINED
9 #define SkSLTypeShared_DEFINED
10
11 #include "include/core/SkTypes.h"
12
13 /**
14 * Types of shader-language-specific boxed variables we can create.
15 */
16 enum class SkSLType : char {
17 kVoid,
18 kBool,
19 kBool2,
20 kBool3,
21 kBool4,
22 kShort,
23 kShort2,
24 kShort3,
25 kShort4,
26 kUShort,
27 kUShort2,
28 kUShort3,
29 kUShort4,
30 kFloat,
31 kFloat2,
32 kFloat3,
33 kFloat4,
34 kFloat2x2,
35 kFloat3x3,
36 kFloat4x4,
37 kHalf,
38 kHalf2,
39 kHalf3,
40 kHalf4,
41 kHalf2x2,
42 kHalf3x3,
43 kHalf4x4,
44 kInt,
45 kInt2,
46 kInt3,
47 kInt4,
48 kUInt,
49 kUInt2,
50 kUInt3,
51 kUInt4,
52 kTexture2DSampler,
53 kTextureExternalSampler,
54 kTexture2DRectSampler,
55 kTexture2D,
56 kSampler,
57 kInput,
58
59 kLast = kInput
60 };
61 static const int kSkSLTypeCount = static_cast<int>(SkSLType::kLast) + 1;
62
63 /** Is the shading language type float (including vectors/matrices)? */
SkSLTypeIsFloatType(SkSLType type)64 static constexpr bool SkSLTypeIsFloatType(SkSLType type) {
65 switch (type) {
66 case SkSLType::kFloat:
67 case SkSLType::kFloat2:
68 case SkSLType::kFloat3:
69 case SkSLType::kFloat4:
70 case SkSLType::kFloat2x2:
71 case SkSLType::kFloat3x3:
72 case SkSLType::kFloat4x4:
73 case SkSLType::kHalf:
74 case SkSLType::kHalf2:
75 case SkSLType::kHalf3:
76 case SkSLType::kHalf4:
77 case SkSLType::kHalf2x2:
78 case SkSLType::kHalf3x3:
79 case SkSLType::kHalf4x4:
80 return true;
81
82 case SkSLType::kVoid:
83 case SkSLType::kTexture2DSampler:
84 case SkSLType::kTextureExternalSampler:
85 case SkSLType::kTexture2DRectSampler:
86 case SkSLType::kBool:
87 case SkSLType::kBool2:
88 case SkSLType::kBool3:
89 case SkSLType::kBool4:
90 case SkSLType::kShort:
91 case SkSLType::kShort2:
92 case SkSLType::kShort3:
93 case SkSLType::kShort4:
94 case SkSLType::kUShort:
95 case SkSLType::kUShort2:
96 case SkSLType::kUShort3:
97 case SkSLType::kUShort4:
98 case SkSLType::kInt:
99 case SkSLType::kInt2:
100 case SkSLType::kInt3:
101 case SkSLType::kInt4:
102 case SkSLType::kUInt:
103 case SkSLType::kUInt2:
104 case SkSLType::kUInt3:
105 case SkSLType::kUInt4:
106 case SkSLType::kTexture2D:
107 case SkSLType::kSampler:
108 case SkSLType::kInput:
109 return false;
110 }
111 SkUNREACHABLE;
112 }
113
114 /** Is the shading language type integral (including vectors)? */
SkSLTypeIsIntegralType(SkSLType type)115 static constexpr bool SkSLTypeIsIntegralType(SkSLType type) {
116 switch (type) {
117 case SkSLType::kShort:
118 case SkSLType::kShort2:
119 case SkSLType::kShort3:
120 case SkSLType::kShort4:
121 case SkSLType::kUShort:
122 case SkSLType::kUShort2:
123 case SkSLType::kUShort3:
124 case SkSLType::kUShort4:
125 case SkSLType::kInt:
126 case SkSLType::kInt2:
127 case SkSLType::kInt3:
128 case SkSLType::kInt4:
129 case SkSLType::kUInt:
130 case SkSLType::kUInt2:
131 case SkSLType::kUInt3:
132 case SkSLType::kUInt4:
133 return true;
134
135 case SkSLType::kFloat:
136 case SkSLType::kFloat2:
137 case SkSLType::kFloat3:
138 case SkSLType::kFloat4:
139 case SkSLType::kFloat2x2:
140 case SkSLType::kFloat3x3:
141 case SkSLType::kFloat4x4:
142 case SkSLType::kHalf:
143 case SkSLType::kHalf2:
144 case SkSLType::kHalf3:
145 case SkSLType::kHalf4:
146 case SkSLType::kHalf2x2:
147 case SkSLType::kHalf3x3:
148 case SkSLType::kHalf4x4:
149 case SkSLType::kVoid:
150 case SkSLType::kTexture2DSampler:
151 case SkSLType::kTextureExternalSampler:
152 case SkSLType::kTexture2DRectSampler:
153 case SkSLType::kBool:
154 case SkSLType::kBool2:
155 case SkSLType::kBool3:
156 case SkSLType::kBool4:
157 case SkSLType::kTexture2D:
158 case SkSLType::kSampler:
159 case SkSLType::kInput:
160 return false;
161 }
162 SkUNREACHABLE;
163 }
164
165 /**
166 * Is the shading language type supported as a uniform (ie, does it have a corresponding set
167 * function on GrGLSLProgramDataManager)?
168 */
SkSLTypeCanBeUniformValue(SkSLType type)169 static constexpr bool SkSLTypeCanBeUniformValue(SkSLType type) {
170 return SkSLTypeIsFloatType(type) || SkSLTypeIsIntegralType(type);
171 }
172
173 /** If the type represents a single value or vector return the vector length, else -1. */
SkSLTypeVecLength(SkSLType type)174 static constexpr int SkSLTypeVecLength(SkSLType type) {
175 switch (type) {
176 case SkSLType::kFloat:
177 case SkSLType::kHalf:
178 case SkSLType::kBool:
179 case SkSLType::kShort:
180 case SkSLType::kUShort:
181 case SkSLType::kInt:
182 case SkSLType::kUInt:
183 return 1;
184
185 case SkSLType::kFloat2:
186 case SkSLType::kHalf2:
187 case SkSLType::kBool2:
188 case SkSLType::kShort2:
189 case SkSLType::kUShort2:
190 case SkSLType::kInt2:
191 case SkSLType::kUInt2:
192 return 2;
193
194 case SkSLType::kFloat3:
195 case SkSLType::kHalf3:
196 case SkSLType::kBool3:
197 case SkSLType::kShort3:
198 case SkSLType::kUShort3:
199 case SkSLType::kInt3:
200 case SkSLType::kUInt3:
201 return 3;
202
203 case SkSLType::kFloat4:
204 case SkSLType::kHalf4:
205 case SkSLType::kBool4:
206 case SkSLType::kShort4:
207 case SkSLType::kUShort4:
208 case SkSLType::kInt4:
209 case SkSLType::kUInt4:
210 return 4;
211
212 case SkSLType::kFloat2x2:
213 case SkSLType::kFloat3x3:
214 case SkSLType::kFloat4x4:
215 case SkSLType::kHalf2x2:
216 case SkSLType::kHalf3x3:
217 case SkSLType::kHalf4x4:
218 case SkSLType::kVoid:
219 case SkSLType::kTexture2DSampler:
220 case SkSLType::kTextureExternalSampler:
221 case SkSLType::kTexture2DRectSampler:
222 case SkSLType::kTexture2D:
223 case SkSLType::kSampler:
224 case SkSLType::kInput:
225 return -1;
226 }
227 SkUNREACHABLE;
228 }
229
SkSLTypeIsCombinedSamplerType(SkSLType type)230 static constexpr bool SkSLTypeIsCombinedSamplerType(SkSLType type) {
231 switch (type) {
232 case SkSLType::kTexture2DSampler:
233 case SkSLType::kTextureExternalSampler:
234 case SkSLType::kTexture2DRectSampler:
235 return true;
236
237 case SkSLType::kVoid:
238 case SkSLType::kFloat:
239 case SkSLType::kFloat2:
240 case SkSLType::kFloat3:
241 case SkSLType::kFloat4:
242 case SkSLType::kFloat2x2:
243 case SkSLType::kFloat3x3:
244 case SkSLType::kFloat4x4:
245 case SkSLType::kHalf:
246 case SkSLType::kHalf2:
247 case SkSLType::kHalf3:
248 case SkSLType::kHalf4:
249 case SkSLType::kHalf2x2:
250 case SkSLType::kHalf3x3:
251 case SkSLType::kHalf4x4:
252 case SkSLType::kInt:
253 case SkSLType::kInt2:
254 case SkSLType::kInt3:
255 case SkSLType::kInt4:
256 case SkSLType::kUInt:
257 case SkSLType::kUInt2:
258 case SkSLType::kUInt3:
259 case SkSLType::kUInt4:
260 case SkSLType::kBool:
261 case SkSLType::kBool2:
262 case SkSLType::kBool3:
263 case SkSLType::kBool4:
264 case SkSLType::kShort:
265 case SkSLType::kShort2:
266 case SkSLType::kShort3:
267 case SkSLType::kShort4:
268 case SkSLType::kUShort:
269 case SkSLType::kUShort2:
270 case SkSLType::kUShort3:
271 case SkSLType::kUShort4:
272 case SkSLType::kTexture2D:
273 case SkSLType::kSampler:
274 case SkSLType::kInput:
275 return false;
276 }
277 SkUNREACHABLE;
278 }
279
280 #endif // SkSLTypeShared_DEFINED
281