bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #ifndef GrGLShaderVar_DEFINED |
| 10 | #define GrGLShaderVar_DEFINED |
| 11 | |
| 12 | #include "GrGLInterface.h" |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 13 | #include "GrGLSL.h" |
bsalomon@google.com | ffa11bb | 2011-10-20 13:43:13 +0000 | [diff] [blame] | 14 | #include "GrStringBuilder.h" |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 15 | |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 16 | #define USE_UNIFORM_FLOAT_ARRAYS true |
| 17 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 18 | /** |
| 19 | * Represents a variable in a shader |
| 20 | */ |
| 21 | class GrGLShaderVar { |
| 22 | public: |
| 23 | |
| 24 | enum Type { |
| 25 | kFloat_Type, |
| 26 | kVec2f_Type, |
| 27 | kVec3f_Type, |
| 28 | kVec4f_Type, |
| 29 | kMat33f_Type, |
| 30 | kSampler2D_Type, |
| 31 | }; |
| 32 | |
| 33 | /** |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 34 | * Early versions of GLSL have Varying and Attribute; those are later |
| 35 | * deprecated, but we still need to know whether a Varying variable |
| 36 | * should be treated as In or Out. |
| 37 | */ |
| 38 | enum TypeModifier { |
| 39 | kNone_TypeModifier, |
| 40 | kOut_TypeModifier, |
| 41 | kIn_TypeModifier, |
| 42 | kUniform_TypeModifier, |
| 43 | kAttribute_TypeModifier |
| 44 | }; |
| 45 | |
| 46 | /** |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 47 | * Defaults to a float with no precision specifier |
| 48 | */ |
| 49 | GrGLShaderVar() { |
| 50 | fType = kFloat_Type; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 51 | fTypeModifier = kNone_TypeModifier; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 52 | fCount = kNonArray; |
| 53 | fEmitPrecision = false; |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 54 | fUseUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | GrGLShaderVar(const GrGLShaderVar& var) |
| 58 | : fType(var.fType) |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 59 | , fTypeModifier(var.fTypeModifier) |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 60 | , fName(var.fName) |
| 61 | , fCount(var.fCount) |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 62 | , fEmitPrecision(var.fEmitPrecision) |
| 63 | , fUseUniformFloatArrays(var.fUseUniformFloatArrays) {} |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Values for array count that have special meaning. We allow 1-sized arrays. |
| 67 | */ |
| 68 | enum { |
| 69 | kNonArray = 0, // not an array |
| 70 | kUnsizedArray = -1, // an unsized array (declared with []) |
| 71 | }; |
| 72 | |
| 73 | /** |
| 74 | * Sets as a non-array. |
| 75 | */ |
| 76 | void set(Type type, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 77 | TypeModifier typeModifier, |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 78 | const GrStringBuilder& name, |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 79 | bool emitPrecision = false, |
| 80 | bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 81 | fType = type; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 82 | fTypeModifier = typeModifier; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 83 | fName = name; |
| 84 | fCount = kNonArray; |
| 85 | fEmitPrecision = emitPrecision; |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 86 | fUseUniformFloatArrays = useUniformFloatArrays; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Sets as a non-array. |
| 91 | */ |
| 92 | void set(Type type, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 93 | TypeModifier typeModifier, |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 94 | const char* name, |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 95 | bool specifyPrecision = false, |
| 96 | bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 97 | fType = type; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 98 | fTypeModifier = typeModifier; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 99 | fName = name; |
| 100 | fCount = kNonArray; |
| 101 | fEmitPrecision = specifyPrecision; |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 102 | fUseUniformFloatArrays = useUniformFloatArrays; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Set all var options |
| 107 | */ |
| 108 | void set(Type type, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 109 | TypeModifier typeModifier, |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 110 | const GrStringBuilder& name, |
| 111 | int count, |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 112 | bool specifyPrecision = false, |
| 113 | bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 114 | fType = type; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 115 | fTypeModifier = typeModifier; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 116 | fName = name; |
| 117 | fCount = count; |
| 118 | fEmitPrecision = specifyPrecision; |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 119 | fUseUniformFloatArrays = useUniformFloatArrays; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Set all var options |
| 124 | */ |
| 125 | void set(Type type, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 126 | TypeModifier typeModifier, |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 127 | const char* name, |
| 128 | int count, |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 129 | bool specifyPrecision = false, |
| 130 | bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 131 | fType = type; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 132 | fTypeModifier = typeModifier; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 133 | fName = name; |
| 134 | fCount = count; |
| 135 | fEmitPrecision = specifyPrecision; |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 136 | fUseUniformFloatArrays = useUniformFloatArrays; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Is the var an array. |
| 141 | */ |
| 142 | bool isArray() const { return kNonArray != fCount; } |
| 143 | /** |
| 144 | * Is this an unsized array, (i.e. declared with []). |
| 145 | */ |
| 146 | bool isUnsizedArray() const { return kUnsizedArray == fCount; } |
| 147 | /** |
| 148 | * Get the array length of the var. |
| 149 | */ |
| 150 | int getArrayCount() const { return fCount; } |
| 151 | /** |
| 152 | * Set the array length of the var |
| 153 | */ |
| 154 | void setArrayCount(int count) { fCount = count; } |
| 155 | /** |
| 156 | * Set to be a non-array. |
| 157 | */ |
| 158 | void setNonArray() { fCount = kNonArray; } |
| 159 | /** |
| 160 | * Set to be an unsized array. |
| 161 | */ |
| 162 | void setUnsizedArray() { fCount = kUnsizedArray; } |
| 163 | |
| 164 | /** |
| 165 | * Access the var name as a writable string |
| 166 | */ |
| 167 | GrStringBuilder* accessName() { return &fName; } |
| 168 | /** |
| 169 | * Set the var name |
| 170 | */ |
| 171 | void setName(const GrStringBuilder& n) { fName = n; } |
| 172 | void setName(const char* n) { fName = n; } |
| 173 | /** |
| 174 | * Get the var name. |
| 175 | */ |
| 176 | const GrStringBuilder& getName() const { return fName; } |
| 177 | |
| 178 | /** |
| 179 | * Get the type of the var |
| 180 | */ |
| 181 | Type getType() const { return fType; } |
| 182 | /** |
| 183 | * Set the type of the var |
| 184 | */ |
| 185 | void setType(Type type) { fType = type; } |
| 186 | |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 187 | TypeModifier getTypeModifier() const { return fTypeModifier; } |
| 188 | void setTypeModifier(TypeModifier type) { fTypeModifier = type; } |
| 189 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 190 | /** |
| 191 | * Must the variable declaration emit a precision specifier |
| 192 | */ |
| 193 | bool emitsPrecision() const { return fEmitPrecision; } |
| 194 | /** |
| 195 | * Specify whether the declaration should specify precision |
| 196 | */ |
| 197 | void setEmitPrecision(bool p) { fEmitPrecision = p; } |
| 198 | |
| 199 | /** |
| 200 | * Write a declaration of this variable to out. |
| 201 | */ |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 202 | void appendDecl(const GrGLInterface* gl, GrStringBuilder* out, |
| 203 | GrGLSLGeneration gen) const { |
| 204 | if (this->getTypeModifier() != kNone_TypeModifier) { |
| 205 | out->append(TypeModifierString(this->getTypeModifier(), gen)); |
| 206 | out->append(" "); |
| 207 | } |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 208 | if (this->emitsPrecision()) { |
| 209 | out->append(PrecisionString(gl)); |
| 210 | out->append(" "); |
| 211 | } |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 212 | Type effectiveType = this->getType(); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 213 | if (this->isArray()) { |
| 214 | if (this->isUnsizedArray()) { |
| 215 | out->appendf("%s %s[]", |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 216 | TypeString(effectiveType), |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 217 | this->getName().c_str()); |
| 218 | } else { |
| 219 | GrAssert(this->getArrayCount() > 0); |
| 220 | out->appendf("%s %s[%d]", |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 221 | TypeString(effectiveType), |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 222 | this->getName().c_str(), |
| 223 | this->getArrayCount()); |
| 224 | } |
| 225 | } else { |
| 226 | out->appendf("%s %s", |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 227 | TypeString(effectiveType), |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 228 | this->getName().c_str()); |
| 229 | } |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 230 | out->append(";\n"); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static const char* TypeString(Type t) { |
| 234 | switch (t) { |
| 235 | case kFloat_Type: |
| 236 | return "float"; |
| 237 | case kVec2f_Type: |
| 238 | return "vec2"; |
| 239 | case kVec3f_Type: |
| 240 | return "vec3"; |
| 241 | case kVec4f_Type: |
| 242 | return "vec4"; |
| 243 | case kMat33f_Type: |
| 244 | return "mat3"; |
| 245 | case kSampler2D_Type: |
| 246 | return "sampler2D"; |
| 247 | default: |
| 248 | GrCrash("Unknown shader var type."); |
| 249 | return ""; // suppress warning |
| 250 | } |
| 251 | } |
| 252 | |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 253 | void appendArrayAccess(int index, GrStringBuilder* out) { |
| 254 | out->appendf("%s[%d]%s", |
| 255 | this->getName().c_str(), |
| 256 | index, |
| 257 | fUseUniformFloatArrays ? "" : ".x"); |
| 258 | } |
| 259 | |
| 260 | void appendArrayAccess(const char* indexName, GrStringBuilder* out) { |
| 261 | out->appendf("%s[%s]%s", |
| 262 | this->getName().c_str(), |
| 263 | indexName, |
| 264 | fUseUniformFloatArrays ? "" : ".x"); |
| 265 | } |
| 266 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 267 | private: |
| 268 | static const char* PrecisionString(const GrGLInterface* gl) { |
| 269 | return gl->supportsDesktop() ? "" : "mediump"; |
| 270 | } |
| 271 | |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 272 | static const char* TypeModifierString(TypeModifier t, |
| 273 | GrGLSLGeneration gen) { |
| 274 | switch (t) { |
| 275 | case kNone_TypeModifier: |
| 276 | return ""; |
| 277 | case kOut_TypeModifier: |
| 278 | return k110_GLSLGeneration == gen ? "varying" : "out"; |
| 279 | case kIn_TypeModifier: |
| 280 | return k110_GLSLGeneration == gen ? "varying" : "in"; |
| 281 | case kUniform_TypeModifier: |
| 282 | return "uniform"; |
| 283 | case kAttribute_TypeModifier: |
| 284 | return k110_GLSLGeneration == gen ? "attribute" : "in"; |
| 285 | default: |
| 286 | GrCrash("Unknown shader variable type modifier."); |
| 287 | return ""; // suppress warning |
| 288 | } |
| 289 | } |
| 290 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 291 | Type fType; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 292 | TypeModifier fTypeModifier; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 293 | GrStringBuilder fName; |
| 294 | int fCount; |
| 295 | bool fEmitPrecision; |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 296 | /// Work around driver bugs on some hardware that don't correctly |
| 297 | /// support uniform float [] |
| 298 | bool fUseUniformFloatArrays; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 299 | }; |
| 300 | |
| 301 | #endif |