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