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