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