OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef GrGLShaderVar_DEFINED | 8 #ifndef GrGLShaderVar_DEFINED |
9 #define GrGLShaderVar_DEFINED | 9 #define GrGLShaderVar_DEFINED |
10 | 10 |
11 #include "GrGLContext.h" | 11 #include "GrGLContext.h" |
12 #include "GrGLSL.h" | 12 #include "GrGLSL.h" |
13 #include "SkString.h" | 13 #include "GrShaderVar.h" |
14 | 14 |
15 #define USE_UNIFORM_FLOAT_ARRAYS true | 15 #define USE_UNIFORM_FLOAT_ARRAYS true |
16 | 16 |
17 /** | 17 /** |
18 * Represents a variable in a shader | 18 * Represents a variable in a shader |
19 */ | 19 */ |
20 class GrGLShaderVar { | 20 class GrGLShaderVar : public GrShaderVar { |
21 public: | 21 public: |
22 | |
23 /** | |
24 * Early versions of GLSL have Varying and Attribute; those are later | |
25 * deprecated, but we still need to know whether a Varying variable | |
26 * should be treated as In or Out. | |
27 */ | |
28 enum TypeModifier { | |
29 kNone_TypeModifier, | |
30 kOut_TypeModifier, | |
31 kIn_TypeModifier, | |
32 kInOut_TypeModifier, | |
33 kUniform_TypeModifier, | |
34 kAttribute_TypeModifier, | |
35 kVaryingIn_TypeModifier, | |
36 kVaryingOut_TypeModifier | |
37 }; | |
38 | |
39 enum Precision { | |
40 kLow_Precision, // lowp | |
41 kMedium_Precision, // mediump | |
42 kHigh_Precision, // highp | |
43 kDefault_Precision, // Default for the current context. We make | |
44 // fragment shaders default to mediump on ES2 | |
45 // because highp support is not guaranteed (and | |
46 // we haven't been motivated to test for it). | |
47 // Otherwise, highp. | |
48 }; | |
49 | |
50 /** | 22 /** |
51 * See GL_ARB_fragment_coord_conventions. | 23 * See GL_ARB_fragment_coord_conventions. |
52 */ | 24 */ |
53 enum Origin { | 25 enum Origin { |
54 kDefault_Origin, // when set to kDefault the origin field is igno
red. | 26 kDefault_Origin, // when set to kDefault the origin field is igno
red. |
55 kUpperLeft_Origin, // only used to declare vec4 in gl_FragCoord. | 27 kUpperLeft_Origin, // only used to declare vec4 in gl_FragCoord. |
56 }; | 28 }; |
57 | 29 |
58 /** | 30 /** |
59 * Defaults to a float with no precision specifier | 31 * Defaults to a float with no precision specifier |
60 */ | 32 */ |
61 GrGLShaderVar() { | 33 GrGLShaderVar() |
62 fType = kFloat_GrSLType; | 34 : GrShaderVar() |
63 fTypeModifier = kNone_TypeModifier; | 35 , fOrigin(kDefault_Origin) |
64 fCount = kNonArray; | 36 , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS) { |
65 fPrecision = kDefault_Precision; | 37 } |
| 38 |
| 39 GrGLShaderVar(const char* name, GrSLType type, int arrayCount = kNonArray, |
| 40 Precision precision = kDefault_Precision) |
| 41 : GrShaderVar(name, type, arrayCount, precision) |
| 42 , fOrigin(kDefault_Origin) |
| 43 , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS) { |
| 44 SkASSERT(kVoid_GrSLType != type); |
66 fOrigin = kDefault_Origin; | 45 fOrigin = kDefault_Origin; |
67 fUseUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS; | 46 fUseUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS; |
68 } | 47 } |
69 | 48 |
70 GrGLShaderVar(const char* name, GrSLType type, int arrayCount = kNonArray, | 49 GrGLShaderVar(const char* name, GrSLType type, TypeModifier typeModifier, |
71 Precision precision = kDefault_Precision) { | 50 int arrayCount = kNonArray, Precision precision = kDefault_Pre
cision) |
| 51 : GrShaderVar(name, type, typeModifier, arrayCount, precision) |
| 52 , fOrigin(kDefault_Origin) |
| 53 , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS) { |
72 SkASSERT(kVoid_GrSLType != type); | 54 SkASSERT(kVoid_GrSLType != type); |
73 fType = type; | 55 } |
74 fTypeModifier = kNone_TypeModifier; | 56 |
75 fCount = arrayCount; | 57 GrGLShaderVar(const GrShaderVar& var) |
76 fPrecision = precision; | 58 : GrShaderVar(var) |
77 fOrigin = kDefault_Origin; | 59 , fOrigin(kDefault_Origin) |
78 fUseUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS; | 60 , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS) { |
79 fName = name; | 61 SkASSERT(kVoid_GrSLType != var.getType()); |
80 } | 62 } |
81 | 63 |
82 GrGLShaderVar(const GrGLShaderVar& var) | 64 GrGLShaderVar(const GrGLShaderVar& var) |
83 : fType(var.fType) | 65 : GrShaderVar(var.c_str(), var.getType(), var.getTypeModifier(), |
84 , fTypeModifier(var.fTypeModifier) | 66 var.getArrayCount(), var.getPrecision()) |
85 , fName(var.fName) | |
86 , fCount(var.fCount) | |
87 , fPrecision(var.fPrecision) | |
88 , fOrigin(var.fOrigin) | 67 , fOrigin(var.fOrigin) |
89 , fUseUniformFloatArrays(var.fUseUniformFloatArrays) { | 68 , fUseUniformFloatArrays(var.fUseUniformFloatArrays) { |
90 SkASSERT(kVoid_GrSLType != var.fType); | 69 SkASSERT(kVoid_GrSLType != var.getType()); |
91 } | 70 } |
92 | 71 |
93 /** | 72 /** |
94 * Values for array count that have special meaning. We allow 1-sized arrays
. | 73 * Values for array count that have special meaning. We allow 1-sized arrays
. |
95 */ | 74 */ |
96 enum { | 75 enum { |
97 kNonArray = 0, // not an array | 76 kNonArray = 0, // not an array |
98 kUnsizedArray = -1, // an unsized array (declared with []) | 77 kUnsizedArray = -1, // an unsized array (declared with []) |
99 }; | 78 }; |
100 | 79 |
101 /** | 80 /** |
102 * Sets as a non-array. | 81 * Sets as a non-array. |
103 */ | 82 */ |
104 void set(GrSLType type, | 83 void set(GrSLType type, |
105 TypeModifier typeModifier, | 84 TypeModifier typeModifier, |
106 const SkString& name, | 85 const SkString& name, |
107 Precision precision = kDefault_Precision, | 86 Precision precision = kDefault_Precision, |
108 Origin origin = kDefault_Origin, | 87 Origin origin = kDefault_Origin, |
109 bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { | 88 bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { |
110 SkASSERT(kVoid_GrSLType != type); | 89 SkASSERT(kVoid_GrSLType != type); |
111 fType = type; | 90 INHERITED::set(type, typeModifier, name, precision); |
112 fTypeModifier = typeModifier; | |
113 fName = name; | |
114 fCount = kNonArray; | |
115 fPrecision = precision; | |
116 fOrigin = origin; | 91 fOrigin = origin; |
117 fUseUniformFloatArrays = useUniformFloatArrays; | 92 fUseUniformFloatArrays = useUniformFloatArrays; |
118 } | 93 } |
119 | 94 |
120 /** | 95 /** |
121 * Sets as a non-array. | 96 * Sets as a non-array. |
122 */ | 97 */ |
123 void set(GrSLType type, | 98 void set(GrSLType type, |
124 TypeModifier typeModifier, | 99 TypeModifier typeModifier, |
125 const char* name, | 100 const char* name, |
126 Precision precision = kDefault_Precision, | 101 Precision precision = kDefault_Precision, |
127 Origin origin = kDefault_Origin, | 102 Origin origin = kDefault_Origin, |
128 bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { | 103 bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { |
129 SkASSERT(kVoid_GrSLType != type); | 104 SkASSERT(kVoid_GrSLType != type); |
130 fType = type; | 105 INHERITED::set(type, typeModifier, name, precision); |
131 fTypeModifier = typeModifier; | |
132 fName = name; | |
133 fCount = kNonArray; | |
134 fPrecision = precision; | |
135 fOrigin = origin; | 106 fOrigin = origin; |
136 fUseUniformFloatArrays = useUniformFloatArrays; | 107 fUseUniformFloatArrays = useUniformFloatArrays; |
137 } | 108 } |
138 | 109 |
139 /** | 110 /** |
140 * Set all var options | 111 * Set all var options |
141 */ | 112 */ |
142 void set(GrSLType type, | 113 void set(GrSLType type, |
143 TypeModifier typeModifier, | 114 TypeModifier typeModifier, |
144 const SkString& name, | 115 const SkString& name, |
145 int count, | 116 int count, |
146 Precision precision = kDefault_Precision, | 117 Precision precision = kDefault_Precision, |
147 Origin origin = kDefault_Origin, | 118 Origin origin = kDefault_Origin, |
148 bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { | 119 bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { |
149 SkASSERT(kVoid_GrSLType != type); | 120 SkASSERT(kVoid_GrSLType != type); |
150 fType = type; | 121 INHERITED::set(type, typeModifier, name, count, precision); |
151 fTypeModifier = typeModifier; | |
152 fName = name; | |
153 fCount = count; | |
154 fPrecision = precision; | |
155 fOrigin = origin; | 122 fOrigin = origin; |
156 fUseUniformFloatArrays = useUniformFloatArrays; | 123 fUseUniformFloatArrays = useUniformFloatArrays; |
157 } | 124 } |
158 | 125 |
159 /** | 126 /** |
160 * Set all var options | 127 * Set all var options |
161 */ | 128 */ |
162 void set(GrSLType type, | 129 void set(GrSLType type, |
163 TypeModifier typeModifier, | 130 TypeModifier typeModifier, |
164 const char* name, | 131 const char* name, |
165 int count, | 132 int count, |
166 Precision precision = kDefault_Precision, | 133 Precision precision = kDefault_Precision, |
167 Origin origin = kDefault_Origin, | 134 Origin origin = kDefault_Origin, |
168 bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { | 135 bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { |
169 SkASSERT(kVoid_GrSLType != type); | 136 SkASSERT(kVoid_GrSLType != type); |
170 fType = type; | 137 INHERITED::set(type, typeModifier, name, count, precision); |
171 fTypeModifier = typeModifier; | |
172 fName = name; | |
173 fCount = count; | |
174 fPrecision = precision; | |
175 fOrigin = origin; | 138 fOrigin = origin; |
176 fUseUniformFloatArrays = useUniformFloatArrays; | 139 fUseUniformFloatArrays = useUniformFloatArrays; |
177 } | 140 } |
178 | 141 |
179 /** | 142 /** |
180 * Is the var an array. | |
181 */ | |
182 bool isArray() const { return kNonArray != fCount; } | |
183 /** | |
184 * Is this an unsized array, (i.e. declared with []). | |
185 */ | |
186 bool isUnsizedArray() const { return kUnsizedArray == fCount; } | |
187 /** | |
188 * Get the array length of the var. | |
189 */ | |
190 int getArrayCount() const { return fCount; } | |
191 /** | |
192 * Set the array length of the var | |
193 */ | |
194 void setArrayCount(int count) { fCount = count; } | |
195 /** | |
196 * Set to be a non-array. | |
197 */ | |
198 void setNonArray() { fCount = kNonArray; } | |
199 /** | |
200 * Set to be an unsized array. | |
201 */ | |
202 void setUnsizedArray() { fCount = kUnsizedArray; } | |
203 | |
204 /** | |
205 * Access the var name as a writable string | |
206 */ | |
207 SkString* accessName() { return &fName; } | |
208 /** | |
209 * Set the var name | |
210 */ | |
211 void setName(const SkString& n) { fName = n; } | |
212 void setName(const char* n) { fName = n; } | |
213 | |
214 /** | |
215 * Get the var name. | |
216 */ | |
217 const SkString& getName() const { return fName; } | |
218 | |
219 /** | |
220 * Shortcut for this->getName().c_str(); | |
221 */ | |
222 const char* c_str() const { return this->getName().c_str(); } | |
223 | |
224 /** | |
225 * Get the type of the var | |
226 */ | |
227 GrSLType getType() const { return fType; } | |
228 /** | |
229 * Set the type of the var | |
230 */ | |
231 void setType(GrSLType type) { fType = type; } | |
232 | |
233 TypeModifier getTypeModifier() const { return fTypeModifier; } | |
234 void setTypeModifier(TypeModifier type) { fTypeModifier = type; } | |
235 | |
236 /** | |
237 * Get the precision of the var | |
238 */ | |
239 Precision getPrecision() const { return fPrecision; } | |
240 | |
241 /** | |
242 * Set the precision of the var | |
243 */ | |
244 void setPrecision(Precision p) { fPrecision = p; } | |
245 | |
246 /** | |
247 * Get the origin of the var | 143 * Get the origin of the var |
248 */ | 144 */ |
249 Origin getOrigin() const { return fOrigin; } | 145 Origin getOrigin() const { return fOrigin; } |
250 | 146 |
251 /** | 147 /** |
252 * Set the origin of the var | 148 * Set the origin of the var |
253 */ | 149 */ |
254 void setOrigin(Origin origin) { fOrigin = origin; } | 150 void setOrigin(Origin origin) { fOrigin = origin; } |
255 | 151 |
256 /** | 152 /** |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 case kVaryingIn_TypeModifier: | 235 case kVaryingIn_TypeModifier: |
340 return k110_GrGLSLGeneration == gen ? "varying" : "in"; | 236 return k110_GrGLSLGeneration == gen ? "varying" : "in"; |
341 case kVaryingOut_TypeModifier: | 237 case kVaryingOut_TypeModifier: |
342 return k110_GrGLSLGeneration == gen ? "varying" : "out"; | 238 return k110_GrGLSLGeneration == gen ? "varying" : "out"; |
343 default: | 239 default: |
344 SkFAIL("Unknown shader variable type modifier."); | 240 SkFAIL("Unknown shader variable type modifier."); |
345 return ""; // suppress warning | 241 return ""; // suppress warning |
346 } | 242 } |
347 } | 243 } |
348 | 244 |
349 GrSLType fType; | |
350 TypeModifier fTypeModifier; | |
351 SkString fName; | |
352 int fCount; | |
353 Precision fPrecision; | |
354 Origin fOrigin; | 245 Origin fOrigin; |
355 /// Work around driver bugs on some hardware that don't correctly | 246 /// Work around driver bugs on some hardware that don't correctly |
356 /// support uniform float [] | 247 /// support uniform float [] |
357 bool fUseUniformFloatArrays; | 248 bool fUseUniformFloatArrays; |
| 249 |
| 250 typedef GrShaderVar INHERITED; |
358 }; | 251 }; |
359 | 252 |
360 #endif | 253 #endif |
OLD | NEW |