Line data Source code
1 : /*
2 : * Copyright 2016 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 : #ifndef GrShaderVar_DEFINED
9 : #define GrShaderVar_DEFINED
10 :
11 : #include "SkString.h"
12 : #include "GrTypesPriv.h"
13 :
14 : class GrShaderCaps;
15 :
16 : #define USE_UNIFORM_FLOAT_ARRAYS true
17 :
18 : /**
19 : * Represents a variable in a shader
20 : */
21 0 : class GrShaderVar {
22 : public:
23 : enum TypeModifier {
24 : kNone_TypeModifier,
25 : kOut_TypeModifier,
26 : kIn_TypeModifier,
27 : kInOut_TypeModifier,
28 : kUniform_TypeModifier,
29 : };
30 :
31 : /**
32 : * Values for array count that have special meaning. We allow 1-sized arrays.git
33 : */
34 : enum {
35 : kNonArray = 0, // not an array
36 : kUnsizedArray = -1, // an unsized array (declared with [])
37 : };
38 :
39 : /**
40 : * Defaults to a non-arry float with no precision specifier, type modifier, or layout qualifier.
41 : */
42 0 : GrShaderVar()
43 0 : : fType(kFloat_GrSLType)
44 : , fTypeModifier(kNone_TypeModifier)
45 : , fCount(kNonArray)
46 : , fPrecision(kDefault_GrSLPrecision)
47 0 : , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS) {
48 0 : }
49 :
50 0 : GrShaderVar(const SkString& name, GrSLType type, int arrayCount = kNonArray,
51 : GrSLPrecision precision = kDefault_GrSLPrecision)
52 0 : : fType(type)
53 : , fTypeModifier(kNone_TypeModifier)
54 : , fCount(arrayCount)
55 : , fPrecision(precision)
56 : , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS)
57 0 : , fName(name) {
58 0 : SkASSERT(kVoid_GrSLType != type);
59 0 : fUseUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS;
60 0 : }
61 :
62 0 : GrShaderVar(const char* name, GrSLType type, int arrayCount = kNonArray,
63 : GrSLPrecision precision = kDefault_GrSLPrecision)
64 0 : : fType(type)
65 : , fTypeModifier(kNone_TypeModifier)
66 : , fCount(arrayCount)
67 : , fPrecision(precision)
68 : , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS)
69 0 : , fName(name) {
70 0 : SkASSERT(kVoid_GrSLType != type);
71 0 : fUseUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS;
72 0 : }
73 :
74 0 : GrShaderVar(const char* name, GrSLType type, TypeModifier typeModifier,
75 : GrSLPrecision precision = kDefault_GrSLPrecision)
76 0 : : fType(type)
77 : , fTypeModifier(typeModifier)
78 : , fCount(kNonArray)
79 : , fPrecision(precision)
80 : , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS)
81 0 : , fName(name) {
82 0 : SkASSERT(kVoid_GrSLType != type);
83 0 : }
84 :
85 0 : GrShaderVar(const char* name, GrSLType type, TypeModifier typeModifier,
86 : int arrayCount, GrSLPrecision precision = kDefault_GrSLPrecision)
87 0 : : fType(type)
88 : , fTypeModifier(typeModifier)
89 : , fCount(arrayCount)
90 : , fPrecision(precision)
91 : , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS)
92 0 : , fName(name) {
93 0 : SkASSERT(kVoid_GrSLType != type);
94 0 : }
95 :
96 0 : GrShaderVar(const GrShaderVar& that)
97 0 : : fType(that.fType)
98 0 : , fTypeModifier(that.fTypeModifier)
99 0 : , fCount(that.fCount)
100 0 : , fPrecision(that.fPrecision)
101 : , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS)
102 : , fName(that.fName)
103 : , fLayoutQualifier(that.fLayoutQualifier)
104 0 : , fExtraModifiers(that.fExtraModifiers) {
105 0 : SkASSERT(kVoid_GrSLType != that.getType());
106 0 : }
107 :
108 : /**
109 : * Sets as a non-array.
110 : */
111 0 : void set(GrSLType type,
112 : const SkString& name,
113 : TypeModifier typeModifier = kNone_TypeModifier,
114 : GrSLPrecision precision = kDefault_GrSLPrecision,
115 : const char* layoutQualifier = nullptr,
116 : const char* extraModifiers = nullptr,
117 : bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) {
118 0 : SkASSERT(kVoid_GrSLType != type);
119 0 : SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeAcceptsPrecision(type));
120 0 : fType = type;
121 0 : fTypeModifier = typeModifier;
122 0 : fName = name;
123 0 : fCount = kNonArray;
124 0 : fPrecision = precision;
125 0 : fLayoutQualifier = layoutQualifier;
126 0 : if (extraModifiers) {
127 0 : fExtraModifiers.printf("%s ", extraModifiers);
128 : }
129 0 : fUseUniformFloatArrays = useUniformFloatArrays;
130 0 : }
131 :
132 : /**
133 : * Sets as a non-array.
134 : */
135 0 : void set(GrSLType type,
136 : const char* name,
137 : TypeModifier typeModifier = kNone_TypeModifier,
138 : GrSLPrecision precision = kDefault_GrSLPrecision,
139 : const char* layoutQualifier = nullptr,
140 : const char* extraModifiers = nullptr,
141 : bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) {
142 0 : SkASSERT(kVoid_GrSLType != type);
143 0 : SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeAcceptsPrecision(type));
144 0 : fType = type;
145 0 : fTypeModifier = typeModifier;
146 0 : fName = name;
147 0 : fCount = kNonArray;
148 0 : fPrecision = precision;
149 0 : fLayoutQualifier = layoutQualifier;
150 0 : if (extraModifiers) {
151 0 : fExtraModifiers.printf("%s ", extraModifiers);
152 : }
153 0 : fUseUniformFloatArrays = useUniformFloatArrays;
154 0 : }
155 :
156 : /**
157 : * Set all var options
158 : */
159 0 : void set(GrSLType type,
160 : const SkString& name,
161 : int count,
162 : TypeModifier typeModifier,
163 : GrSLPrecision precision = kDefault_GrSLPrecision,
164 : const char* layoutQualifier = nullptr,
165 : const char* extraModifiers = nullptr,
166 : bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) {
167 0 : SkASSERT(kVoid_GrSLType != type);
168 0 : SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeAcceptsPrecision(type));
169 0 : fType = type;
170 0 : fTypeModifier = typeModifier;
171 0 : fName = name;
172 0 : fCount = count;
173 0 : fPrecision = precision;
174 0 : fLayoutQualifier = layoutQualifier;
175 0 : if (extraModifiers) {
176 0 : fExtraModifiers.printf("%s ", extraModifiers);
177 : }
178 0 : fUseUniformFloatArrays = useUniformFloatArrays;
179 0 : }
180 :
181 : /**
182 : * Set all var options
183 : */
184 0 : void set(GrSLType type,
185 : const char* name,
186 : int count,
187 : TypeModifier typeModifier,
188 : GrSLPrecision precision = kDefault_GrSLPrecision,
189 : const char* layoutQualifier = nullptr,
190 : const char* extraModifiers = nullptr,
191 : bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) {
192 0 : SkASSERT(kVoid_GrSLType != type);
193 0 : SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeAcceptsPrecision(type));
194 0 : fType = type;
195 0 : fTypeModifier = typeModifier;
196 0 : fName = name;
197 0 : fCount = count;
198 0 : fPrecision = precision;
199 0 : fLayoutQualifier = layoutQualifier;
200 0 : if (extraModifiers) {
201 0 : fExtraModifiers.printf("%s ", extraModifiers);
202 : }
203 0 : fUseUniformFloatArrays = useUniformFloatArrays;
204 0 : }
205 :
206 : /**
207 : * Is the var an array.
208 : */
209 0 : bool isArray() const { return kNonArray != fCount; }
210 : /**
211 : * Is this an unsized array, (i.e. declared with []).
212 : */
213 0 : bool isUnsizedArray() const { return kUnsizedArray == fCount; }
214 : /**
215 : * Get the array length of the var.
216 : */
217 0 : int getArrayCount() const { return fCount; }
218 : /**
219 : * Set the array length of the var
220 : */
221 0 : void setArrayCount(int count) { fCount = count; }
222 : /**
223 : * Set to be a non-array.
224 : */
225 : void setNonArray() { fCount = kNonArray; }
226 : /**
227 : * Set to be an unsized array.
228 : */
229 : void setUnsizedArray() { fCount = kUnsizedArray; }
230 :
231 : /**
232 : * Access the var name as a writable string
233 : */
234 0 : SkString* accessName() { return &fName; }
235 : /**
236 : * Set the var name
237 : */
238 0 : void setName(const SkString& n) { fName = n; }
239 : void setName(const char* n) { fName = n; }
240 :
241 : /**
242 : * Get the var name.
243 : */
244 0 : const SkString& getName() const { return fName; }
245 :
246 : /**
247 : * Shortcut for this->getName().c_str();
248 : */
249 0 : const char* c_str() const { return this->getName().c_str(); }
250 :
251 : /**
252 : * Get the type of the var
253 : */
254 0 : GrSLType getType() const { return fType; }
255 : /**
256 : * Set the type of the var
257 : */
258 0 : void setType(GrSLType type) { fType = type; }
259 :
260 0 : TypeModifier getTypeModifier() const { return fTypeModifier; }
261 0 : void setTypeModifier(TypeModifier type) { fTypeModifier = type; }
262 :
263 : /**
264 : * Get the precision of the var
265 : */
266 : GrSLPrecision getPrecision() const { return fPrecision; }
267 :
268 : /**
269 : * Set the precision of the var
270 : */
271 0 : void setPrecision(GrSLPrecision p) { fPrecision = p; }
272 :
273 : /**
274 : * Appends to the layout qualifier
275 : */
276 0 : void addLayoutQualifier(const char* layoutQualifier) {
277 0 : if (!layoutQualifier || !strlen(layoutQualifier)) {
278 0 : return;
279 : }
280 0 : if (fLayoutQualifier.isEmpty()) {
281 0 : fLayoutQualifier = layoutQualifier;
282 : } else {
283 0 : fLayoutQualifier.appendf(", %s", layoutQualifier);
284 : }
285 : }
286 :
287 : void setImageStorageFormat(GrImageStorageFormat format);
288 :
289 : void setMemoryModel(GrSLMemoryModel);
290 :
291 : void setRestrict(GrSLRestrict);
292 :
293 : void setIOType(GrIOType);
294 :
295 0 : void addModifier(const char* modifier) {
296 0 : if (modifier) {
297 0 : fExtraModifiers.appendf("%s ", modifier);
298 : }
299 0 : }
300 :
301 : /**
302 : * Write a declaration of this variable to out.
303 : */
304 : void appendDecl(const GrShaderCaps*, SkString* out) const;
305 :
306 : void appendArrayAccess(int index, SkString* out) const {
307 : out->appendf("%s[%d]%s",
308 : this->getName().c_str(),
309 : index,
310 : fUseUniformFloatArrays ? "" : ".x");
311 : }
312 :
313 0 : void appendArrayAccess(const char* indexName, SkString* out) const {
314 0 : out->appendf("%s[%s]%s",
315 0 : this->getName().c_str(),
316 : indexName,
317 0 : fUseUniformFloatArrays ? "" : ".x");
318 0 : }
319 :
320 : private:
321 : GrSLType fType;
322 : TypeModifier fTypeModifier;
323 : int fCount;
324 : GrSLPrecision fPrecision;
325 : /// Work around driver bugs on some hardware that don't correctly
326 : /// support uniform float []
327 : bool fUseUniformFloatArrays;
328 :
329 : SkString fName;
330 : SkString fLayoutQualifier;
331 : SkString fExtraModifiers;
332 : };
333 :
334 : #endif
|