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 | |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame^] | 11 | #include "GrGLContext.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 | 5fa2107 | 2012-10-25 14:57:46 +0000 | [diff] [blame] | 48 | * See GL_ARB_fragment_coord_conventions. |
| 49 | */ |
| 50 | enum Origin { |
| 51 | kDefault_Origin, // when set to kDefault the origin field is ignored. |
| 52 | kUpperLeft_Origin, // only used to declare vec4 in gl_FragCoord. |
| 53 | }; |
| 54 | |
| 55 | /** |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 56 | * Defaults to a float with no precision specifier |
| 57 | */ |
| 58 | GrGLShaderVar() { |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 59 | fType = kFloat_GrSLType; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 60 | fTypeModifier = kNone_TypeModifier; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 61 | fCount = kNonArray; |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 62 | fPrecision = kDefault_Precision; |
bsalomon@google.com | 5fa2107 | 2012-10-25 14:57:46 +0000 | [diff] [blame] | 63 | fOrigin = kDefault_Origin; |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 64 | fUseUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 65 | } |
| 66 | |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 67 | GrGLShaderVar(const char* name, GrSLType type, int arrayCount = kNonArray) { |
| 68 | GrAssert(kVoid_GrSLType != type); |
| 69 | fType = type; |
| 70 | fTypeModifier = kNone_TypeModifier; |
| 71 | fCount = arrayCount; |
| 72 | fPrecision = kDefault_Precision; |
bsalomon@google.com | 5fa2107 | 2012-10-25 14:57:46 +0000 | [diff] [blame] | 73 | fOrigin = kDefault_Origin; |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 74 | fUseUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS; |
| 75 | fName = name; |
| 76 | } |
| 77 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 78 | GrGLShaderVar(const GrGLShaderVar& var) |
| 79 | : fType(var.fType) |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 80 | , fTypeModifier(var.fTypeModifier) |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 81 | , fName(var.fName) |
| 82 | , fCount(var.fCount) |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 83 | , fPrecision(var.fPrecision) |
bsalomon@google.com | b7f9a86 | 2012-10-25 19:35:05 +0000 | [diff] [blame] | 84 | , fOrigin(var.fOrigin) |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 85 | , fUseUniformFloatArrays(var.fUseUniformFloatArrays) { |
| 86 | GrAssert(kVoid_GrSLType != var.fType); |
| 87 | } |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * Values for array count that have special meaning. We allow 1-sized arrays. |
| 91 | */ |
| 92 | enum { |
| 93 | kNonArray = 0, // not an array |
| 94 | kUnsizedArray = -1, // an unsized array (declared with []) |
| 95 | }; |
| 96 | |
| 97 | /** |
| 98 | * Sets as a non-array. |
| 99 | */ |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 100 | void set(GrSLType type, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 101 | TypeModifier typeModifier, |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 102 | const SkString& name, |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 103 | Precision precision = kDefault_Precision, |
bsalomon@google.com | 5fa2107 | 2012-10-25 14:57:46 +0000 | [diff] [blame] | 104 | Origin origin = kDefault_Origin, |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 105 | bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 106 | GrAssert(kVoid_GrSLType != type); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 107 | fType = type; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 108 | fTypeModifier = typeModifier; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 109 | fName = name; |
| 110 | fCount = kNonArray; |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 111 | fPrecision = precision; |
bsalomon@google.com | 5fa2107 | 2012-10-25 14:57:46 +0000 | [diff] [blame] | 112 | fOrigin = origin; |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 113 | fUseUniformFloatArrays = useUniformFloatArrays; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Sets as a non-array. |
| 118 | */ |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 119 | void set(GrSLType type, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 120 | TypeModifier typeModifier, |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 121 | const char* name, |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 122 | Precision precision = kDefault_Precision, |
bsalomon@google.com | 5fa2107 | 2012-10-25 14:57:46 +0000 | [diff] [blame] | 123 | Origin origin = kDefault_Origin, |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 124 | bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 125 | GrAssert(kVoid_GrSLType != type); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 126 | fType = type; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 127 | fTypeModifier = typeModifier; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 128 | fName = name; |
| 129 | fCount = kNonArray; |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 130 | fPrecision = precision; |
bsalomon@google.com | 5fa2107 | 2012-10-25 14:57:46 +0000 | [diff] [blame] | 131 | fOrigin = origin; |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 132 | fUseUniformFloatArrays = useUniformFloatArrays; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Set all var options |
| 137 | */ |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 138 | void set(GrSLType type, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 139 | TypeModifier typeModifier, |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 140 | const SkString& name, |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 141 | int count, |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 142 | Precision precision = kDefault_Precision, |
bsalomon@google.com | 5fa2107 | 2012-10-25 14:57:46 +0000 | [diff] [blame] | 143 | Origin origin = kDefault_Origin, |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 144 | bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 145 | GrAssert(kVoid_GrSLType != type); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 146 | fType = type; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 147 | fTypeModifier = typeModifier; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 148 | fName = name; |
| 149 | fCount = count; |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 150 | fPrecision = precision; |
bsalomon@google.com | 5fa2107 | 2012-10-25 14:57:46 +0000 | [diff] [blame] | 151 | fOrigin = origin; |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 152 | fUseUniformFloatArrays = useUniformFloatArrays; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Set all var options |
| 157 | */ |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 158 | void set(GrSLType type, |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 159 | TypeModifier typeModifier, |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 160 | const char* name, |
| 161 | int count, |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 162 | Precision precision = kDefault_Precision, |
bsalomon@google.com | 5fa2107 | 2012-10-25 14:57:46 +0000 | [diff] [blame] | 163 | Origin origin = kDefault_Origin, |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 164 | bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 165 | GrAssert(kVoid_GrSLType != type); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 166 | fType = type; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 167 | fTypeModifier = typeModifier; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 168 | fName = name; |
| 169 | fCount = count; |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 170 | fPrecision = precision; |
bsalomon@google.com | 5fa2107 | 2012-10-25 14:57:46 +0000 | [diff] [blame] | 171 | fOrigin = origin; |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 172 | fUseUniformFloatArrays = useUniformFloatArrays; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Is the var an array. |
| 177 | */ |
| 178 | bool isArray() const { return kNonArray != fCount; } |
| 179 | /** |
| 180 | * Is this an unsized array, (i.e. declared with []). |
| 181 | */ |
| 182 | bool isUnsizedArray() const { return kUnsizedArray == fCount; } |
| 183 | /** |
| 184 | * Get the array length of the var. |
| 185 | */ |
| 186 | int getArrayCount() const { return fCount; } |
| 187 | /** |
| 188 | * Set the array length of the var |
| 189 | */ |
| 190 | void setArrayCount(int count) { fCount = count; } |
| 191 | /** |
| 192 | * Set to be a non-array. |
| 193 | */ |
| 194 | void setNonArray() { fCount = kNonArray; } |
| 195 | /** |
| 196 | * Set to be an unsized array. |
| 197 | */ |
| 198 | void setUnsizedArray() { fCount = kUnsizedArray; } |
| 199 | |
| 200 | /** |
| 201 | * Access the var name as a writable string |
| 202 | */ |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 203 | SkString* accessName() { return &fName; } |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 204 | /** |
| 205 | * Set the var name |
| 206 | */ |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 207 | void setName(const SkString& n) { fName = n; } |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 208 | void setName(const char* n) { fName = n; } |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 209 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 210 | /** |
| 211 | * Get the var name. |
| 212 | */ |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 213 | const SkString& getName() const { return fName; } |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 214 | |
| 215 | /** |
bsalomon@google.com | 032b221 | 2012-07-16 13:36:18 +0000 | [diff] [blame] | 216 | * Shortcut for this->getName().c_str(); |
| 217 | */ |
| 218 | const char* c_str() const { return this->getName().c_str(); } |
| 219 | |
| 220 | /** |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 221 | * Get the type of the var |
| 222 | */ |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 223 | GrSLType getType() const { return fType; } |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 224 | /** |
| 225 | * Set the type of the var |
| 226 | */ |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 227 | void setType(GrSLType type) { fType = type; } |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 228 | |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 229 | TypeModifier getTypeModifier() const { return fTypeModifier; } |
| 230 | void setTypeModifier(TypeModifier type) { fTypeModifier = type; } |
| 231 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 232 | /** |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 233 | * Get the precision of the var |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 234 | */ |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 235 | Precision getPrecision() const { return fPrecision; } |
| 236 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 237 | /** |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 238 | * Set the precision of the var |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 239 | */ |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 240 | void setPrecision(Precision p) { fPrecision = p; } |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 241 | |
| 242 | /** |
bsalomon@google.com | 5fa2107 | 2012-10-25 14:57:46 +0000 | [diff] [blame] | 243 | * Get the origin of the var |
| 244 | */ |
| 245 | Origin getOrigin() const { return fOrigin; } |
| 246 | |
| 247 | /** |
| 248 | * Set the origin of the var |
| 249 | */ |
| 250 | void setOrigin(Origin origin) { fOrigin = origin; } |
| 251 | |
| 252 | /** |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 253 | * Write a declaration of this variable to out. |
| 254 | */ |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame^] | 255 | void appendDecl(const GrGLContextInfo& ctxInfo, SkString* out) const { |
bsalomon@google.com | 5fa2107 | 2012-10-25 14:57:46 +0000 | [diff] [blame] | 256 | if (kUpperLeft_Origin == fOrigin) { |
| 257 | // this is the only place where we specify a layout modifier. If we use other layout |
| 258 | // modifiers in the future then they should be placed in a list. |
| 259 | out->append("layout(origin_upper_left) "); |
| 260 | } |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 261 | if (this->getTypeModifier() != kNone_TypeModifier) { |
bsalomon@google.com | 9639994 | 2012-02-13 14:39:16 +0000 | [diff] [blame] | 262 | out->append(TypeModifierString(this->getTypeModifier(), |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame^] | 263 | ctxInfo.glslGeneration())); |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 264 | out->append(" "); |
| 265 | } |
robertphillips@google.com | 6177e69 | 2013-02-28 20:16:25 +0000 | [diff] [blame^] | 266 | out->append(PrecisionString(fPrecision, ctxInfo.binding())); |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 267 | GrSLType effectiveType = this->getType(); |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 268 | if (this->isArray()) { |
| 269 | if (this->isUnsizedArray()) { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 270 | out->appendf("%s %s[]", |
| 271 | TypeString(effectiveType), |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 272 | this->getName().c_str()); |
| 273 | } else { |
| 274 | GrAssert(this->getArrayCount() > 0); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 275 | out->appendf("%s %s[%d]", |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 276 | TypeString(effectiveType), |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 277 | this->getName().c_str(), |
| 278 | this->getArrayCount()); |
| 279 | } |
| 280 | } else { |
| 281 | out->appendf("%s %s", |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 282 | TypeString(effectiveType), |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 283 | this->getName().c_str()); |
| 284 | } |
| 285 | } |
| 286 | |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 287 | static const char* TypeString(GrSLType t) { |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 288 | switch (t) { |
bsalomon@google.com | a1bf0ff | 2012-08-07 17:36:29 +0000 | [diff] [blame] | 289 | case kVoid_GrSLType: |
| 290 | return "void"; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 291 | case kFloat_GrSLType: |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 292 | return "float"; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 293 | case kVec2f_GrSLType: |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 294 | return "vec2"; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 295 | case kVec3f_GrSLType: |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 296 | return "vec3"; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 297 | case kVec4f_GrSLType: |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 298 | return "vec4"; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 299 | case kMat33f_GrSLType: |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 300 | return "mat3"; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 301 | case kMat44f_GrSLType: |
senorblanco@chromium.org | 50bdad8 | 2012-01-03 20:51:57 +0000 | [diff] [blame] | 302 | return "mat4"; |
tomhudson@google.com | 168e634 | 2012-04-18 17:49:20 +0000 | [diff] [blame] | 303 | case kSampler2D_GrSLType: |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 304 | return "sampler2D"; |
| 305 | default: |
| 306 | GrCrash("Unknown shader var type."); |
| 307 | return ""; // suppress warning |
| 308 | } |
| 309 | } |
| 310 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 311 | void appendArrayAccess(int index, SkString* out) const { |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 312 | out->appendf("%s[%d]%s", |
| 313 | this->getName().c_str(), |
| 314 | index, |
| 315 | fUseUniformFloatArrays ? "" : ".x"); |
| 316 | } |
| 317 | |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 318 | void appendArrayAccess(const char* indexName, SkString* out) const { |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 319 | out->appendf("%s[%s]%s", |
| 320 | this->getName().c_str(), |
| 321 | indexName, |
| 322 | fUseUniformFloatArrays ? "" : ".x"); |
| 323 | } |
| 324 | |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 325 | private: |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 326 | static const char* TypeModifierString(TypeModifier t, GrGLSLGeneration gen) { |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 327 | switch (t) { |
| 328 | case kNone_TypeModifier: |
| 329 | return ""; |
| 330 | case kOut_TypeModifier: |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 331 | return k110_GrGLSLGeneration == gen ? "varying" : "out"; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 332 | case kIn_TypeModifier: |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 333 | return k110_GrGLSLGeneration == gen ? "varying" : "in"; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 334 | case kUniform_TypeModifier: |
| 335 | return "uniform"; |
| 336 | case kAttribute_TypeModifier: |
bsalomon@google.com | e55fd0f | 2012-02-10 15:56:06 +0000 | [diff] [blame] | 337 | return k110_GrGLSLGeneration == gen ? "attribute" : "in"; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 338 | default: |
| 339 | GrCrash("Unknown shader variable type modifier."); |
| 340 | return ""; // suppress warning |
| 341 | } |
| 342 | } |
| 343 | |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 344 | static const char* PrecisionString(Precision p, GrGLBinding binding) { |
| 345 | // Desktop GLSL has added precision qualifiers but they don't do anything. |
| 346 | if (kES2_GrGLBinding == binding) { |
| 347 | switch (p) { |
| 348 | case kLow_Precision: |
| 349 | return "lowp "; |
| 350 | case kMedium_Precision: |
| 351 | return "mediump "; |
| 352 | case kHigh_Precision: |
| 353 | return "highp "; |
| 354 | case kDefault_Precision: |
| 355 | return ""; |
| 356 | default: |
| 357 | GrCrash("Unexpected precision type."); |
| 358 | } |
| 359 | } |
| 360 | return ""; |
| 361 | } |
| 362 | |
| 363 | GrSLType fType; |
tomhudson@google.com | 086e535 | 2011-12-08 14:44:10 +0000 | [diff] [blame] | 364 | TypeModifier fTypeModifier; |
bsalomon@google.com | f0a104e | 2012-07-10 17:51:07 +0000 | [diff] [blame] | 365 | SkString fName; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 366 | int fCount; |
bsalomon@google.com | d7727ce | 2012-07-12 16:40:03 +0000 | [diff] [blame] | 367 | Precision fPrecision; |
bsalomon@google.com | 5fa2107 | 2012-10-25 14:57:46 +0000 | [diff] [blame] | 368 | Origin fOrigin; |
tomhudson@google.com | da66898 | 2011-12-07 15:06:29 +0000 | [diff] [blame] | 369 | /// Work around driver bugs on some hardware that don't correctly |
| 370 | /// support uniform float [] |
| 371 | bool fUseUniformFloatArrays; |
bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 372 | }; |
| 373 | |
| 374 | #endif |