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