• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /** Returns the SkSL typename for this type. */
64 const char* SkSLTypeString(SkSLType t);
65 
66 /** Is the shading language type float (including vectors/matrices)? */
SkSLTypeIsFloatType(SkSLType type)67 static constexpr bool SkSLTypeIsFloatType(SkSLType type) {
68     switch (type) {
69         case SkSLType::kFloat:
70         case SkSLType::kFloat2:
71         case SkSLType::kFloat3:
72         case SkSLType::kFloat4:
73         case SkSLType::kFloat2x2:
74         case SkSLType::kFloat3x3:
75         case SkSLType::kFloat4x4:
76         case SkSLType::kHalf:
77         case SkSLType::kHalf2:
78         case SkSLType::kHalf3:
79         case SkSLType::kHalf4:
80         case SkSLType::kHalf2x2:
81         case SkSLType::kHalf3x3:
82         case SkSLType::kHalf4x4:
83             return true;
84 
85         case SkSLType::kVoid:
86         case SkSLType::kTexture2DSampler:
87         case SkSLType::kTextureExternalSampler:
88         case SkSLType::kTexture2DRectSampler:
89         case SkSLType::kBool:
90         case SkSLType::kBool2:
91         case SkSLType::kBool3:
92         case SkSLType::kBool4:
93         case SkSLType::kShort:
94         case SkSLType::kShort2:
95         case SkSLType::kShort3:
96         case SkSLType::kShort4:
97         case SkSLType::kUShort:
98         case SkSLType::kUShort2:
99         case SkSLType::kUShort3:
100         case SkSLType::kUShort4:
101         case SkSLType::kInt:
102         case SkSLType::kInt2:
103         case SkSLType::kInt3:
104         case SkSLType::kInt4:
105         case SkSLType::kUInt:
106         case SkSLType::kUInt2:
107         case SkSLType::kUInt3:
108         case SkSLType::kUInt4:
109         case SkSLType::kTexture2D:
110         case SkSLType::kSampler:
111         case SkSLType::kInput:
112             return false;
113     }
114     SkUNREACHABLE;
115 }
116 
117 /** Is the shading language type integral (including vectors)? */
SkSLTypeIsIntegralType(SkSLType type)118 static constexpr bool SkSLTypeIsIntegralType(SkSLType type) {
119     switch (type) {
120         case SkSLType::kShort:
121         case SkSLType::kShort2:
122         case SkSLType::kShort3:
123         case SkSLType::kShort4:
124         case SkSLType::kUShort:
125         case SkSLType::kUShort2:
126         case SkSLType::kUShort3:
127         case SkSLType::kUShort4:
128         case SkSLType::kInt:
129         case SkSLType::kInt2:
130         case SkSLType::kInt3:
131         case SkSLType::kInt4:
132         case SkSLType::kUInt:
133         case SkSLType::kUInt2:
134         case SkSLType::kUInt3:
135         case SkSLType::kUInt4:
136             return true;
137 
138         case SkSLType::kFloat:
139         case SkSLType::kFloat2:
140         case SkSLType::kFloat3:
141         case SkSLType::kFloat4:
142         case SkSLType::kFloat2x2:
143         case SkSLType::kFloat3x3:
144         case SkSLType::kFloat4x4:
145         case SkSLType::kHalf:
146         case SkSLType::kHalf2:
147         case SkSLType::kHalf3:
148         case SkSLType::kHalf4:
149         case SkSLType::kHalf2x2:
150         case SkSLType::kHalf3x3:
151         case SkSLType::kHalf4x4:
152         case SkSLType::kVoid:
153         case SkSLType::kTexture2DSampler:
154         case SkSLType::kTextureExternalSampler:
155         case SkSLType::kTexture2DRectSampler:
156         case SkSLType::kBool:
157         case SkSLType::kBool2:
158         case SkSLType::kBool3:
159         case SkSLType::kBool4:
160         case SkSLType::kTexture2D:
161         case SkSLType::kSampler:
162         case SkSLType::kInput:
163             return false;
164     }
165     SkUNREACHABLE;
166 }
167 
168 /** If the type represents a single value or vector return the vector length; otherwise, -1. */
SkSLTypeVecLength(SkSLType type)169 static constexpr int SkSLTypeVecLength(SkSLType type) {
170     switch (type) {
171         case SkSLType::kFloat:
172         case SkSLType::kHalf:
173         case SkSLType::kBool:
174         case SkSLType::kShort:
175         case SkSLType::kUShort:
176         case SkSLType::kInt:
177         case SkSLType::kUInt:
178             return 1;
179 
180         case SkSLType::kFloat2:
181         case SkSLType::kHalf2:
182         case SkSLType::kBool2:
183         case SkSLType::kShort2:
184         case SkSLType::kUShort2:
185         case SkSLType::kInt2:
186         case SkSLType::kUInt2:
187             return 2;
188 
189         case SkSLType::kFloat3:
190         case SkSLType::kHalf3:
191         case SkSLType::kBool3:
192         case SkSLType::kShort3:
193         case SkSLType::kUShort3:
194         case SkSLType::kInt3:
195         case SkSLType::kUInt3:
196             return 3;
197 
198         case SkSLType::kFloat4:
199         case SkSLType::kHalf4:
200         case SkSLType::kBool4:
201         case SkSLType::kShort4:
202         case SkSLType::kUShort4:
203         case SkSLType::kInt4:
204         case SkSLType::kUInt4:
205             return 4;
206 
207         case SkSLType::kFloat2x2:
208         case SkSLType::kFloat3x3:
209         case SkSLType::kFloat4x4:
210         case SkSLType::kHalf2x2:
211         case SkSLType::kHalf3x3:
212         case SkSLType::kHalf4x4:
213         case SkSLType::kVoid:
214         case SkSLType::kTexture2DSampler:
215         case SkSLType::kTextureExternalSampler:
216         case SkSLType::kTexture2DRectSampler:
217         case SkSLType::kTexture2D:
218         case SkSLType::kSampler:
219         case SkSLType::kInput:
220             return -1;
221     }
222     SkUNREACHABLE;
223 }
224 
225 /**
226  * Is the shading language type supported as a uniform (ie, does it have a corresponding set
227  * function on GrGLSLProgramDataManager)?
228  */
SkSLTypeCanBeUniformValue(SkSLType type)229 static constexpr bool SkSLTypeCanBeUniformValue(SkSLType type) {
230     return SkSLTypeIsFloatType(type) || SkSLTypeIsIntegralType(type);
231 }
232 
233 /** Is the shading language type full precision? */
234 bool SkSLTypeIsFullPrecisionNumericType(SkSLType type);
235 
236 /** If the type represents a square matrix, return its size; otherwise, -1. */
237 int SkSLTypeMatrixSize(SkSLType type);
238 
239 /** If the type represents a square matrix, return its size; otherwise, -1. */
240 bool SkSLTypeIsCombinedSamplerType(SkSLType type);
241 
242 #endif // SkSLTypeShared_DEFINED
243