| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef GrGLShaderVar_DEFINED |
| 9 | #define GrGLShaderVar_DEFINED |
| 10 | |
| bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 11 | #include "GrGLContextInfo.h" |
| tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 12 | #include "GrGLSL.h" |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 13 | #include "SkString.h" |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 14 | |
| tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 15 | #define USE_UNIFORM_FLOAT_ARRAYS true |
| 16 | |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 17 | /** |
| 18 | * Represents a variable in a shader |
| 19 | */ |
| 20 | class GrGLShaderVar { |
| 21 | public: |
| 22 | |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 23 | /** |
| tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 24 | * Early versions of GLSL have Varying and Attribute; those are later |
| 25 | * deprecated, but we still need to know whether a Varying variable |
| 26 | * should be treated as In or Out. |
| 27 | */ |
| 28 | enum TypeModifier { |
| 29 | kNone_TypeModifier, |
| 30 | kOut_TypeModifier, |
| 31 | kIn_TypeModifier, |
| 32 | kUniform_TypeModifier, |
| 33 | kAttribute_TypeModifier |
| 34 | }; |
| 35 | |
| bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 36 | enum Precision { |
| 37 | kLow_Precision, // lowp |
| 38 | kMedium_Precision, // mediump |
| 39 | kHigh_Precision, // highp |
| 40 | kDefault_Precision, // Default for the current context. We make |
| 41 | // fragment shaders default to mediump on ES2 |
| 42 | // because highp support is not guaranteed (and |
| 43 | // we haven't been motivated to test for it). |
| 44 | // Otherwise, highp. |
| 45 | }; |
| 46 | |
| tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 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() { |
| tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 51 | fType = kFloat_GrSLType; |
| 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; |
| bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 54 | fPrecision = kDefault_Precision; |
| 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 | |
| bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 58 | GrGLShaderVar(const char* name, GrSLType type, int arrayCount = kNonArray) { |
| 59 | GrAssert(kVoid_GrSLType != type); |
| 60 | fType = type; |
| 61 | fTypeModifier = kNone_TypeModifier; |
| 62 | fCount = arrayCount; |
| 63 | fPrecision = kDefault_Precision; |
| 64 | fUseUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS; |
| 65 | fName = name; |
| 66 | } |
| 67 | |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 68 | GrGLShaderVar(const GrGLShaderVar& var) |
| 69 | : fType(var.fType) |
| tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 70 | , fTypeModifier(var.fTypeModifier) |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 71 | , fName(var.fName) |
| 72 | , fCount(var.fCount) |
| bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 73 | , fPrecision(var.fPrecision) |
| bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 74 | , fUseUniformFloatArrays(var.fUseUniformFloatArrays) { |
| 75 | GrAssert(kVoid_GrSLType != var.fType); |
| 76 | } |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * Values for array count that have special meaning. We allow 1-sized arrays. |
| 80 | */ |
| 81 | enum { |
| 82 | kNonArray = 0, // not an array |
| 83 | kUnsizedArray = -1, // an unsized array (declared with []) |
| 84 | }; |
| 85 | |
| 86 | /** |
| 87 | * Sets as a non-array. |
| 88 | */ |
| tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 89 | void set(GrSLType type, |
| tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 90 | TypeModifier typeModifier, |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 91 | const SkString& name, |
| bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 92 | Precision precision = kDefault_Precision, |
| tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 93 | bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { |
| bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 94 | GrAssert(kVoid_GrSLType != type); |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 95 | fType = type; |
| tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 96 | fTypeModifier = typeModifier; |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 97 | fName = name; |
| 98 | fCount = kNonArray; |
| bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 99 | fPrecision = precision; |
| tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 100 | fUseUniformFloatArrays = useUniformFloatArrays; |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Sets as a non-array. |
| 105 | */ |
| tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 106 | void set(GrSLType type, |
| tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 107 | TypeModifier typeModifier, |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 108 | const char* name, |
| bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 109 | Precision precision = kDefault_Precision, |
| tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 110 | bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { |
| bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 111 | GrAssert(kVoid_GrSLType != type); |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 112 | fType = type; |
| tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 113 | fTypeModifier = typeModifier; |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 114 | fName = name; |
| 115 | fCount = kNonArray; |
| bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 116 | fPrecision = precision; |
| tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 117 | fUseUniformFloatArrays = useUniformFloatArrays; |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Set all var options |
| 122 | */ |
| tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 123 | void set(GrSLType type, |
| tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 124 | TypeModifier typeModifier, |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 125 | const SkString& name, |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 126 | int count, |
| bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 127 | Precision precision = kDefault_Precision, |
| tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 128 | bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { |
| bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 129 | GrAssert(kVoid_GrSLType != type); |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 130 | fType = type; |
| tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 131 | fTypeModifier = typeModifier; |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 132 | fName = name; |
| 133 | fCount = count; |
| bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 134 | fPrecision = precision; |
| tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 135 | fUseUniformFloatArrays = useUniformFloatArrays; |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Set all var options |
| 140 | */ |
| tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 141 | void set(GrSLType type, |
| tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 142 | TypeModifier typeModifier, |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 143 | const char* name, |
| 144 | int count, |
| bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 145 | Precision precision = kDefault_Precision, |
| tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 146 | bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { |
| bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 147 | GrAssert(kVoid_GrSLType != type); |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 148 | fType = type; |
| tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 149 | fTypeModifier = typeModifier; |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 150 | fName = name; |
| 151 | fCount = count; |
| bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 152 | fPrecision = precision; |
| tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 153 | fUseUniformFloatArrays = useUniformFloatArrays; |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Is the var an array. |
| 158 | */ |
| 159 | bool isArray() const { return kNonArray != fCount; } |
| 160 | /** |
| 161 | * Is this an unsized array, (i.e. declared with []). |
| 162 | */ |
| 163 | bool isUnsizedArray() const { return kUnsizedArray == fCount; } |
| 164 | /** |
| 165 | * Get the array length of the var. |
| 166 | */ |
| 167 | int getArrayCount() const { return fCount; } |
| 168 | /** |
| 169 | * Set the array length of the var |
| 170 | */ |
| 171 | void setArrayCount(int count) { fCount = count; } |
| 172 | /** |
| 173 | * Set to be a non-array. |
| 174 | */ |
| 175 | void setNonArray() { fCount = kNonArray; } |
| 176 | /** |
| 177 | * Set to be an unsized array. |
| 178 | */ |
| 179 | void setUnsizedArray() { fCount = kUnsizedArray; } |
| 180 | |
| 181 | /** |
| 182 | * Access the var name as a writable string |
| 183 | */ |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 184 | SkString* accessName() { return &fName; } |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 185 | /** |
| 186 | * Set the var name |
| 187 | */ |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 188 | void setName(const SkString& n) { fName = n; } |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 189 | void setName(const char* n) { fName = n; } |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 190 | |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 191 | /** |
| 192 | * Get the var name. |
| 193 | */ |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 194 | const SkString& getName() const { return fName; } |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 195 | |
| 196 | /** |
| bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 197 | * Shortcut for this->getName().c_str(); |
| 198 | */ |
| 199 | const char* c_str() const { return this->getName().c_str(); } |
| 200 | |
| 201 | /** |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 202 | * Get the type of the var |
| 203 | */ |
| tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 204 | GrSLType getType() const { return fType; } |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 205 | /** |
| 206 | * Set the type of the var |
| 207 | */ |
| tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 208 | void setType(GrSLType type) { fType = type; } |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 209 | |
| tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 210 | TypeModifier getTypeModifier() const { return fTypeModifier; } |
| 211 | void setTypeModifier(TypeModifier type) { fTypeModifier = type; } |
| 212 | |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 213 | /** |
| bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 214 | * Get the precision of the var |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 215 | */ |
| bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 216 | Precision getPrecision() const { return fPrecision; } |
| 217 | |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 218 | /** |
| bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 219 | * Set the precision of the var |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 220 | */ |
| bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 221 | void setPrecision(Precision p) { fPrecision = p; } |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 222 | |
| 223 | /** |
| 224 | * Write a declaration of this variable to out. |
| 225 | */ |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 226 | void appendDecl(const GrGLContextInfo& gl, SkString* out) const { |
| tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 227 | if (this->getTypeModifier() != kNone_TypeModifier) { |
| bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 228 | out->append(TypeModifierString(this->getTypeModifier(), |
| 229 | gl.glslGeneration())); |
| tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 230 | out->append(" "); |
| 231 | } |
| bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 232 | out->append(PrecisionString(fPrecision, gl.binding())); |
| tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 233 | GrSLType effectiveType = this->getType(); |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 234 | if (this->isArray()) { |
| 235 | if (this->isUnsizedArray()) { |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame^] | 236 | out->appendf("%s %s[]", |
| 237 | TypeString(effectiveType), |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 238 | this->getName().c_str()); |
| 239 | } else { |
| 240 | GrAssert(this->getArrayCount() > 0); |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame^] | 241 | out->appendf("%s %s[%d]", |
| tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 242 | TypeString(effectiveType), |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 243 | this->getName().c_str(), |
| 244 | this->getArrayCount()); |
| 245 | } |
| 246 | } else { |
| 247 | out->appendf("%s %s", |
| tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 248 | TypeString(effectiveType), |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 249 | this->getName().c_str()); |
| 250 | } |
| 251 | } |
| 252 | |
| tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 253 | static const char* TypeString(GrSLType t) { |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 254 | switch (t) { |
| bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 255 | case kVoid_GrSLType: |
| 256 | return "void"; |
| tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 257 | case kFloat_GrSLType: |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 258 | return "float"; |
| tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 259 | case kVec2f_GrSLType: |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 260 | return "vec2"; |
| tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 261 | case kVec3f_GrSLType: |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 262 | return "vec3"; |
| tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 263 | case kVec4f_GrSLType: |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 264 | return "vec4"; |
| tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 265 | case kMat33f_GrSLType: |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 266 | return "mat3"; |
| tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 267 | case kMat44f_GrSLType: |
| senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 268 | return "mat4"; |
| tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 269 | case kSampler2D_GrSLType: |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 270 | return "sampler2D"; |
| 271 | default: |
| 272 | GrCrash("Unknown shader var type."); |
| 273 | return ""; // suppress warning |
| 274 | } |
| 275 | } |
| 276 | |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 277 | void appendArrayAccess(int index, SkString* out) const { |
| tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 278 | out->appendf("%s[%d]%s", |
| 279 | this->getName().c_str(), |
| 280 | index, |
| 281 | fUseUniformFloatArrays ? "" : ".x"); |
| 282 | } |
| 283 | |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 284 | void appendArrayAccess(const char* indexName, SkString* out) const { |
| tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 285 | out->appendf("%s[%s]%s", |
| 286 | this->getName().c_str(), |
| 287 | indexName, |
| 288 | fUseUniformFloatArrays ? "" : ".x"); |
| 289 | } |
| 290 | |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 291 | private: |
| bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 292 | static const char* TypeModifierString(TypeModifier t, GrGLSLGeneration gen) { |
| tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 293 | switch (t) { |
| 294 | case kNone_TypeModifier: |
| 295 | return ""; |
| 296 | case kOut_TypeModifier: |
| bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 297 | return k110_GrGLSLGeneration == gen ? "varying" : "out"; |
| tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 298 | case kIn_TypeModifier: |
| bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 299 | return k110_GrGLSLGeneration == gen ? "varying" : "in"; |
| tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 300 | case kUniform_TypeModifier: |
| 301 | return "uniform"; |
| 302 | case kAttribute_TypeModifier: |
| bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 303 | return k110_GrGLSLGeneration == gen ? "attribute" : "in"; |
| tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 304 | default: |
| 305 | GrCrash("Unknown shader variable type modifier."); |
| 306 | return ""; // suppress warning |
| 307 | } |
| 308 | } |
| 309 | |
| bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 310 | static const char* PrecisionString(Precision p, GrGLBinding binding) { |
| 311 | // Desktop GLSL has added precision qualifiers but they don't do anything. |
| 312 | if (kES2_GrGLBinding == binding) { |
| 313 | switch (p) { |
| 314 | case kLow_Precision: |
| 315 | return "lowp "; |
| 316 | case kMedium_Precision: |
| 317 | return "mediump "; |
| 318 | case kHigh_Precision: |
| 319 | return "highp "; |
| 320 | case kDefault_Precision: |
| 321 | return ""; |
| 322 | default: |
| 323 | GrCrash("Unexpected precision type."); |
| 324 | } |
| 325 | } |
| 326 | return ""; |
| 327 | } |
| 328 | |
| 329 | GrSLType fType; |
| tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 330 | TypeModifier fTypeModifier; |
| bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 331 | SkString fName; |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 332 | int fCount; |
| bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 333 | Precision fPrecision; |
| tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 334 | /// Work around driver bugs on some hardware that don't correctly |
| 335 | /// support uniform float [] |
| 336 | bool fUseUniformFloatArrays; |
| bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 337 | }; |
| 338 | |
| 339 | #endif |