2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef GrGLShaderVar_DEFINED
9 #define GrGLShaderVar_DEFINED
11 #include "GrGLContext.h"
15 #define USE_UNIFORM_FLOAT_ARRAYS true
18 * Represents a variable in a shader
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.
33 kUniform_TypeModifier,
34 kAttribute_TypeModifier,
35 kVaryingIn_TypeModifier,
36 kVaryingOut_TypeModifier
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).
51 * See GL_ARB_fragment_coord_conventions.
54 kDefault_Origin, // when set to kDefault the origin field is ignored.
55 kUpperLeft_Origin, // only used to declare vec4 in gl_FragCoord.
59 * Defaults to a float with no precision specifier
62 fType = kFloat_GrSLType;
63 fTypeModifier = kNone_TypeModifier;
65 fPrecision = kDefault_Precision;
66 fOrigin = kDefault_Origin;
67 fUseUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS;
70 GrGLShaderVar(const char* name, GrSLType type, int arrayCount = kNonArray,
71 Precision precision = kDefault_Precision) {
72 SkASSERT(kVoid_GrSLType != type);
74 fTypeModifier = kNone_TypeModifier;
76 fPrecision = precision;
77 fOrigin = kDefault_Origin;
78 fUseUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS;
82 GrGLShaderVar(const GrGLShaderVar& var)
84 , fTypeModifier(var.fTypeModifier)
87 , fPrecision(var.fPrecision)
88 , fOrigin(var.fOrigin)
89 , fUseUniformFloatArrays(var.fUseUniformFloatArrays) {
90 SkASSERT(kVoid_GrSLType != var.fType);
94 * Values for array count that have special meaning. We allow 1-sized arrays.
97 kNonArray = 0, // not an array
98 kUnsizedArray = -1, // an unsized array (declared with [])
102 * Sets as a non-array.
104 void set(GrSLType type,
105 TypeModifier typeModifier,
106 const SkString& name,
107 Precision precision = kDefault_Precision,
108 Origin origin = kDefault_Origin,
109 bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) {
110 SkASSERT(kVoid_GrSLType != type);
112 fTypeModifier = typeModifier;
115 fPrecision = precision;
117 fUseUniformFloatArrays = useUniformFloatArrays;
121 * Sets as a non-array.
123 void set(GrSLType type,
124 TypeModifier typeModifier,
126 Precision precision = kDefault_Precision,
127 Origin origin = kDefault_Origin,
128 bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) {
129 SkASSERT(kVoid_GrSLType != type);
131 fTypeModifier = typeModifier;
134 fPrecision = precision;
136 fUseUniformFloatArrays = useUniformFloatArrays;
140 * Set all var options
142 void set(GrSLType type,
143 TypeModifier typeModifier,
144 const SkString& name,
146 Precision precision = kDefault_Precision,
147 Origin origin = kDefault_Origin,
148 bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) {
149 SkASSERT(kVoid_GrSLType != type);
151 fTypeModifier = typeModifier;
154 fPrecision = precision;
156 fUseUniformFloatArrays = useUniformFloatArrays;
160 * Set all var options
162 void set(GrSLType type,
163 TypeModifier typeModifier,
166 Precision precision = kDefault_Precision,
167 Origin origin = kDefault_Origin,
168 bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) {
169 SkASSERT(kVoid_GrSLType != type);
171 fTypeModifier = typeModifier;
174 fPrecision = precision;
176 fUseUniformFloatArrays = useUniformFloatArrays;
180 * Is the var an array.
182 bool isArray() const { return kNonArray != fCount; }
184 * Is this an unsized array, (i.e. declared with []).
186 bool isUnsizedArray() const { return kUnsizedArray == fCount; }
188 * Get the array length of the var.
190 int getArrayCount() const { return fCount; }
192 * Set the array length of the var
194 void setArrayCount(int count) { fCount = count; }
196 * Set to be a non-array.
198 void setNonArray() { fCount = kNonArray; }
200 * Set to be an unsized array.
202 void setUnsizedArray() { fCount = kUnsizedArray; }
205 * Access the var name as a writable string
207 SkString* accessName() { return &fName; }
211 void setName(const SkString& n) { fName = n; }
212 void setName(const char* n) { fName = n; }
217 const SkString& getName() const { return fName; }
220 * Shortcut for this->getName().c_str();
222 const char* c_str() const { return this->getName().c_str(); }
225 * Get the type of the var
227 GrSLType getType() const { return fType; }
229 * Set the type of the var
231 void setType(GrSLType type) { fType = type; }
233 TypeModifier getTypeModifier() const { return fTypeModifier; }
234 void setTypeModifier(TypeModifier type) { fTypeModifier = type; }
237 * Get the precision of the var
239 Precision getPrecision() const { return fPrecision; }
242 * Set the precision of the var
244 void setPrecision(Precision p) { fPrecision = p; }
247 * Get the origin of the var
249 Origin getOrigin() const { return fOrigin; }
252 * Set the origin of the var
254 void setOrigin(Origin origin) { fOrigin = origin; }
257 * Write a declaration of this variable to out.
259 void appendDecl(const GrGLContextInfo& ctxInfo, SkString* out) const {
260 if (kUpperLeft_Origin == fOrigin) {
261 // this is the only place where we specify a layout modifier. If we use other layout
262 // modifiers in the future then they should be placed in a list.
263 out->append("layout(origin_upper_left) ");
265 if (this->getTypeModifier() != kNone_TypeModifier) {
266 out->append(TypeModifierString(this->getTypeModifier(),
267 ctxInfo.glslGeneration()));
270 out->append(PrecisionString(fPrecision, ctxInfo.standard()));
271 GrSLType effectiveType = this->getType();
272 if (this->isArray()) {
273 if (this->isUnsizedArray()) {
274 out->appendf("%s %s[]",
275 GrGLSLTypeString(effectiveType),
276 this->getName().c_str());
278 SkASSERT(this->getArrayCount() > 0);
279 out->appendf("%s %s[%d]",
280 GrGLSLTypeString(effectiveType),
281 this->getName().c_str(),
282 this->getArrayCount());
285 out->appendf("%s %s",
286 GrGLSLTypeString(effectiveType),
287 this->getName().c_str());
291 void appendArrayAccess(int index, SkString* out) const {
292 out->appendf("%s[%d]%s",
293 this->getName().c_str(),
295 fUseUniformFloatArrays ? "" : ".x");
298 void appendArrayAccess(const char* indexName, SkString* out) const {
299 out->appendf("%s[%s]%s",
300 this->getName().c_str(),
302 fUseUniformFloatArrays ? "" : ".x");
305 static const char* PrecisionString(Precision p, GrGLStandard standard) {
306 // Desktop GLSL has added precision qualifiers but they don't do anything.
307 if (kGLES_GrGLStandard == standard) {
311 case kMedium_Precision:
313 case kHigh_Precision:
315 case kDefault_Precision:
318 SkFAIL("Unexpected precision type.");
325 static const char* TypeModifierString(TypeModifier t, GrGLSLGeneration gen) {
327 case kNone_TypeModifier:
329 case kIn_TypeModifier:
331 case kInOut_TypeModifier:
333 case kOut_TypeModifier:
335 case kUniform_TypeModifier:
337 case kAttribute_TypeModifier:
338 return k110_GrGLSLGeneration == gen ? "attribute" : "in";
339 case kVaryingIn_TypeModifier:
340 return k110_GrGLSLGeneration == gen ? "varying" : "in";
341 case kVaryingOut_TypeModifier:
342 return k110_GrGLSLGeneration == gen ? "varying" : "out";
344 SkFAIL("Unknown shader variable type modifier.");
345 return ""; // suppress warning
350 TypeModifier fTypeModifier;
353 Precision fPrecision;
355 /// Work around driver bugs on some hardware that don't correctly
356 /// support uniform float []
357 bool fUseUniformFloatArrays;