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