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 GrGLSLShaderVar_DEFINED | 8 #ifndef GrGLSLShaderVar_DEFINED |
9 #define GrGLSLShaderVar_DEFINED | 9 #define GrGLSLShaderVar_DEFINED |
10 | 10 |
11 #include "GrShaderVar.h" | 11 #include "GrShaderVar.h" |
12 #include "../glsl/GrGLSL.h" | 12 #include "../glsl/GrGLSL.h" |
13 #include "../glsl/GrGLSLCaps.h" | 13 #include "../glsl/GrGLSLCaps.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 GrGLSLShaderVar : public GrShaderVar { | 20 class GrGLSLShaderVar : public GrShaderVar { |
21 public: | 21 public: |
22 /** | 22 /** |
23 * See GL_ARB_fragment_coord_conventions. | |
24 */ | |
25 enum Origin { | |
26 kDefault_Origin, // when set to kDefault the origin field is igno
red. | |
27 kUpperLeft_Origin, // only used to declare vec4 in gl_FragCoord. | |
28 }; | |
29 | |
30 /** | |
31 * Defaults to a float with no precision specifier | 23 * Defaults to a float with no precision specifier |
32 */ | 24 */ |
33 GrGLSLShaderVar() | 25 GrGLSLShaderVar() |
34 : GrShaderVar() | 26 : GrShaderVar() |
35 , fOrigin(kDefault_Origin) | |
36 , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS) { | 27 , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS) { |
37 } | 28 } |
38 | 29 |
39 GrGLSLShaderVar(const char* name, GrSLType type, int arrayCount = kNonArray, | 30 GrGLSLShaderVar(const char* name, GrSLType type, int arrayCount = kNonArray, |
40 GrSLPrecision precision = kDefault_GrSLPrecision) | 31 GrSLPrecision precision = kDefault_GrSLPrecision) |
41 : GrShaderVar(name, type, arrayCount, precision) | 32 : GrShaderVar(name, type, arrayCount, precision) |
42 , fOrigin(kDefault_Origin) | |
43 , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS) { | 33 , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS) { |
44 SkASSERT(kVoid_GrSLType != type); | 34 SkASSERT(kVoid_GrSLType != type); |
45 fOrigin = kDefault_Origin; | |
46 fUseUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS; | 35 fUseUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS; |
47 } | 36 } |
48 | 37 |
49 GrGLSLShaderVar(const char* name, GrSLType type, TypeModifier typeModifier, | 38 GrGLSLShaderVar(const char* name, GrSLType type, TypeModifier typeModifier, |
50 int arrayCount = kNonArray, GrSLPrecision precision = kDefau
lt_GrSLPrecision) | 39 int arrayCount = kNonArray, GrSLPrecision precision = kDefau
lt_GrSLPrecision) |
51 : GrShaderVar(name, type, typeModifier, arrayCount, precision) | 40 : GrShaderVar(name, type, typeModifier, arrayCount, precision) |
52 , fOrigin(kDefault_Origin) | |
53 , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS) { | 41 , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS) { |
54 SkASSERT(kVoid_GrSLType != type); | 42 SkASSERT(kVoid_GrSLType != type); |
55 } | 43 } |
56 | 44 |
57 GrGLSLShaderVar(const GrShaderVar& var) | 45 GrGLSLShaderVar(const GrShaderVar& var) |
58 : GrShaderVar(var) | 46 : GrShaderVar(var) |
59 , fOrigin(kDefault_Origin) | |
60 , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS) { | 47 , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS) { |
61 SkASSERT(kVoid_GrSLType != var.getType()); | 48 SkASSERT(kVoid_GrSLType != var.getType()); |
62 } | 49 } |
63 | 50 |
64 GrGLSLShaderVar(const GrGLSLShaderVar& var) | 51 GrGLSLShaderVar(const GrGLSLShaderVar& var) |
65 : GrShaderVar(var.c_str(), var.getType(), var.getTypeModifier(), | 52 : GrShaderVar(var.c_str(), var.getType(), var.getTypeModifier(), |
66 var.getArrayCount(), var.getPrecision()) | 53 var.getArrayCount(), var.getPrecision()) |
67 , fOrigin(var.fOrigin) | |
68 , fUseUniformFloatArrays(var.fUseUniformFloatArrays) { | 54 , fUseUniformFloatArrays(var.fUseUniformFloatArrays) { |
69 SkASSERT(kVoid_GrSLType != var.getType()); | 55 SkASSERT(kVoid_GrSLType != var.getType()); |
70 } | 56 } |
71 | 57 |
72 /** | 58 /** |
73 * Values for array count that have special meaning. We allow 1-sized arrays
. | 59 * Values for array count that have special meaning. We allow 1-sized arrays
. |
74 */ | 60 */ |
75 enum { | 61 enum { |
76 kNonArray = 0, // not an array | 62 kNonArray = 0, // not an array |
77 kUnsizedArray = -1, // an unsized array (declared with []) | 63 kUnsizedArray = -1, // an unsized array (declared with []) |
78 }; | 64 }; |
79 | 65 |
80 /** | 66 /** |
81 * Sets as a non-array. | 67 * Sets as a non-array. |
82 */ | 68 */ |
83 void set(GrSLType type, | 69 void set(GrSLType type, |
84 TypeModifier typeModifier, | 70 TypeModifier typeModifier, |
85 const SkString& name, | 71 const SkString& name, |
86 GrSLPrecision precision = kDefault_GrSLPrecision, | 72 GrSLPrecision precision = kDefault_GrSLPrecision, |
87 Origin origin = kDefault_Origin, | 73 const char* layoutQualifier = nullptr, |
88 bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { | 74 bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { |
89 SkASSERT(kVoid_GrSLType != type); | 75 SkASSERT(kVoid_GrSLType != type); |
90 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type
)); | 76 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type
)); |
91 INHERITED::set(type, name, typeModifier, precision); | 77 INHERITED::set(type, name, typeModifier, precision); |
92 fOrigin = origin; | 78 fLayoutQualifier = layoutQualifier; |
93 fUseUniformFloatArrays = useUniformFloatArrays; | 79 fUseUniformFloatArrays = useUniformFloatArrays; |
94 } | 80 } |
95 | 81 |
96 /** | 82 /** |
97 * Sets as a non-array. | 83 * Sets as a non-array. |
98 */ | 84 */ |
99 void set(GrSLType type, | 85 void set(GrSLType type, |
100 TypeModifier typeModifier, | 86 TypeModifier typeModifier, |
101 const char* name, | 87 const char* name, |
102 GrSLPrecision precision = kDefault_GrSLPrecision, | 88 GrSLPrecision precision = kDefault_GrSLPrecision, |
103 Origin origin = kDefault_Origin, | 89 const char* layoutQualifier = nullptr, |
104 bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { | 90 bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { |
105 SkASSERT(kVoid_GrSLType != type); | 91 SkASSERT(kVoid_GrSLType != type); |
106 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type
)); | 92 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type
)); |
107 INHERITED::set(type, name, typeModifier, precision); | 93 INHERITED::set(type, name, typeModifier, precision); |
108 fOrigin = origin; | 94 fLayoutQualifier = layoutQualifier; |
109 fUseUniformFloatArrays = useUniformFloatArrays; | 95 fUseUniformFloatArrays = useUniformFloatArrays; |
110 } | 96 } |
111 | 97 |
112 /** | 98 /** |
113 * Set all var options | 99 * Set all var options |
114 */ | 100 */ |
115 void set(GrSLType type, | 101 void set(GrSLType type, |
116 TypeModifier typeModifier, | 102 TypeModifier typeModifier, |
117 const SkString& name, | 103 const SkString& name, |
118 int count, | 104 int count, |
119 GrSLPrecision precision = kDefault_GrSLPrecision, | 105 GrSLPrecision precision = kDefault_GrSLPrecision, |
120 Origin origin = kDefault_Origin, | 106 const char* layoutQualifier = nullptr, |
121 bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { | 107 bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { |
122 SkASSERT(kVoid_GrSLType != type); | 108 SkASSERT(kVoid_GrSLType != type); |
123 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type
)); | 109 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type
)); |
124 INHERITED::set(type, name, typeModifier, precision, count); | 110 INHERITED::set(type, name, typeModifier, precision, count); |
125 fOrigin = origin; | 111 fLayoutQualifier = layoutQualifier; |
126 fUseUniformFloatArrays = useUniformFloatArrays; | 112 fUseUniformFloatArrays = useUniformFloatArrays; |
127 } | 113 } |
128 | 114 |
129 /** | 115 /** |
130 * Set all var options | 116 * Set all var options |
131 */ | 117 */ |
132 void set(GrSLType type, | 118 void set(GrSLType type, |
133 TypeModifier typeModifier, | 119 TypeModifier typeModifier, |
134 const char* name, | 120 const char* name, |
135 int count, | 121 int count, |
136 GrSLPrecision precision = kDefault_GrSLPrecision, | 122 GrSLPrecision precision = kDefault_GrSLPrecision, |
137 Origin origin = kDefault_Origin, | 123 const char* layoutQualifier = nullptr, |
138 bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { | 124 bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { |
139 SkASSERT(kVoid_GrSLType != type); | 125 SkASSERT(kVoid_GrSLType != type); |
140 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type
)); | 126 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type
)); |
141 INHERITED::set(type, name, typeModifier, precision, count); | 127 INHERITED::set(type, name, typeModifier, precision, count); |
142 fOrigin = origin; | 128 fLayoutQualifier = layoutQualifier; |
143 fUseUniformFloatArrays = useUniformFloatArrays; | 129 fUseUniformFloatArrays = useUniformFloatArrays; |
144 } | 130 } |
145 | 131 |
146 /** | 132 /** |
147 * Get the origin of the var | |
148 */ | |
149 Origin getOrigin() const { return fOrigin; } | |
150 | |
151 /** | |
152 * Set the origin of the var | |
153 */ | |
154 void setOrigin(Origin origin) { fOrigin = origin; } | |
155 | |
156 /** | |
157 * Write a declaration of this variable to out. | 133 * Write a declaration of this variable to out. |
158 */ | 134 */ |
159 void appendDecl(const GrGLSLCaps* glslCaps, SkString* out) const { | 135 void appendDecl(const GrGLSLCaps* glslCaps, SkString* out) const { |
160 SkASSERT(kDefault_GrSLPrecision == fPrecision || GrSLTypeIsFloatType(fTy
pe)); | 136 SkASSERT(kDefault_GrSLPrecision == fPrecision || GrSLTypeIsFloatType(fTy
pe)); |
161 if (kUpperLeft_Origin == fOrigin) { | 137 if (!fLayoutQualifier.isEmpty()) { |
162 // this is the only place where we specify a layout modifier. If we
use other layout | 138 out->appendf("layout(%s) ", fLayoutQualifier.c_str()); |
163 // modifiers in the future then they should be placed in a list. | |
164 out->append("layout(origin_upper_left) "); | |
165 } | 139 } |
166 if (this->getTypeModifier() != kNone_TypeModifier) { | 140 if (this->getTypeModifier() != kNone_TypeModifier) { |
167 out->append(TypeModifierString(glslCaps, this->getTypeModifier())); | 141 out->append(TypeModifierString(glslCaps, this->getTypeModifier())); |
168 out->append(" "); | 142 out->append(" "); |
169 } | 143 } |
170 out->append(PrecisionString(glslCaps, fPrecision)); | 144 out->append(PrecisionString(glslCaps, fPrecision)); |
171 GrSLType effectiveType = this->getType(); | 145 GrSLType effectiveType = this->getType(); |
172 if (this->isArray()) { | 146 if (this->isArray()) { |
173 if (this->isUnsizedArray()) { | 147 if (this->isUnsizedArray()) { |
174 out->appendf("%s %s[]", | 148 out->appendf("%s %s[]", |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 case kVaryingIn_TypeModifier: | 212 case kVaryingIn_TypeModifier: |
239 return k110_GrGLSLGeneration == gen ? "varying" : "in"; | 213 return k110_GrGLSLGeneration == gen ? "varying" : "in"; |
240 case kVaryingOut_TypeModifier: | 214 case kVaryingOut_TypeModifier: |
241 return k110_GrGLSLGeneration == gen ? "varying" : "out"; | 215 return k110_GrGLSLGeneration == gen ? "varying" : "out"; |
242 default: | 216 default: |
243 SkFAIL("Unknown shader variable type modifier."); | 217 SkFAIL("Unknown shader variable type modifier."); |
244 return ""; // suppress warning | 218 return ""; // suppress warning |
245 } | 219 } |
246 } | 220 } |
247 | 221 |
248 Origin fOrigin; | |
249 /// Work around driver bugs on some hardware that don't correctly | 222 /// Work around driver bugs on some hardware that don't correctly |
250 /// support uniform float [] | 223 /// support uniform float [] |
251 bool fUseUniformFloatArrays; | 224 bool fUseUniformFloatArrays; |
252 | 225 |
| 226 SkString fLayoutQualifier; |
| 227 |
253 typedef GrShaderVar INHERITED; | 228 typedef GrShaderVar INHERITED; |
254 }; | 229 }; |
255 | 230 |
256 #endif | 231 #endif |
OLD | NEW |