joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 1 | /* |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 2 | * Copyright 2016 Google Inc. |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 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 GrShaderVar_DEFINED |
| 9 | #define GrShaderVar_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkString.h" |
| 12 | #include "include/private/GrTypesPriv.h" |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 13 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 14 | class GrShaderCaps; |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 15 | |
| 16 | #define USE_UNIFORM_FLOAT_ARRAYS true |
| 17 | |
| 18 | /** |
| 19 | * Represents a variable in a shader |
| 20 | */ |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 21 | class GrShaderVar { |
| 22 | public: |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 23 | enum TypeModifier { |
| 24 | kNone_TypeModifier, |
| 25 | kOut_TypeModifier, |
| 26 | kIn_TypeModifier, |
| 27 | kInOut_TypeModifier, |
| 28 | kUniform_TypeModifier, |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 29 | }; |
| 30 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 31 | /** |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 32 | * Values for array count that have special meaning. We allow 1-sized arrays.git |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 33 | */ |
| 34 | enum { |
| 35 | kNonArray = 0, // not an array |
| 36 | kUnsizedArray = -1, // an unsized array (declared with []) |
| 37 | }; |
| 38 | |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 39 | /** |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 40 | * Defaults to a non-arry half with no type modifier or layout qualifier. |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 41 | */ |
| 42 | GrShaderVar() |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 43 | : fType(kHalf_GrSLType) |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 44 | , fTypeModifier(kNone_TypeModifier) |
| 45 | , fCount(kNonArray) |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 46 | , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS) { |
| 47 | } |
| 48 | |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 49 | GrShaderVar(const SkString& name, GrSLType type, int arrayCount = kNonArray) |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 50 | : fType(type) |
| 51 | , fTypeModifier(kNone_TypeModifier) |
| 52 | , fCount(arrayCount) |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 53 | , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS) |
| 54 | , fName(name) { |
| 55 | SkASSERT(kVoid_GrSLType != type); |
| 56 | fUseUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS; |
| 57 | } |
| 58 | |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 59 | GrShaderVar(const char* name, GrSLType type, int arrayCount = kNonArray) |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 60 | : fType(type) |
| 61 | , fTypeModifier(kNone_TypeModifier) |
| 62 | , fCount(arrayCount) |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 63 | , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS) |
| 64 | , fName(name) { |
| 65 | SkASSERT(kVoid_GrSLType != type); |
| 66 | fUseUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS; |
| 67 | } |
| 68 | |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 69 | GrShaderVar(const char* name, GrSLType type, TypeModifier typeModifier) |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 70 | : fType(type) |
| 71 | , fTypeModifier(typeModifier) |
| 72 | , fCount(kNonArray) |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 73 | , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS) |
| 74 | , fName(name) { |
| 75 | SkASSERT(kVoid_GrSLType != type); |
| 76 | } |
| 77 | |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 78 | GrShaderVar(const char* name, GrSLType type, TypeModifier typeModifier, int arrayCount) |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 79 | : fType(type) |
| 80 | , fTypeModifier(typeModifier) |
| 81 | , fCount(arrayCount) |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 82 | , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS) |
| 83 | , fName(name) { |
| 84 | SkASSERT(kVoid_GrSLType != type); |
| 85 | } |
| 86 | |
| 87 | GrShaderVar(const GrShaderVar& that) |
| 88 | : fType(that.fType) |
| 89 | , fTypeModifier(that.fTypeModifier) |
| 90 | , fCount(that.fCount) |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 91 | , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS) |
| 92 | , fName(that.fName) |
| 93 | , fLayoutQualifier(that.fLayoutQualifier) |
| 94 | , fExtraModifiers(that.fExtraModifiers) { |
| 95 | SkASSERT(kVoid_GrSLType != that.getType()); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Sets as a non-array. |
| 100 | */ |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 101 | void set(GrSLType type, |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 102 | const SkString& name, |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 103 | TypeModifier typeModifier = kNone_TypeModifier, |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 104 | const char* layoutQualifier = nullptr, |
| 105 | const char* extraModifiers = nullptr, |
| 106 | bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 107 | SkASSERT(kVoid_GrSLType != type); |
| 108 | fType = type; |
| 109 | fTypeModifier = typeModifier; |
| 110 | fName = name; |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 111 | fCount = kNonArray; |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 112 | fLayoutQualifier = layoutQualifier; |
| 113 | if (extraModifiers) { |
| 114 | fExtraModifiers.printf("%s ", extraModifiers); |
| 115 | } |
| 116 | fUseUniformFloatArrays = useUniformFloatArrays; |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 119 | /** |
| 120 | * Sets as a non-array. |
| 121 | */ |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 122 | void set(GrSLType type, |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 123 | const char* name, |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame] | 124 | TypeModifier typeModifier = kNone_TypeModifier, |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 125 | const char* layoutQualifier = nullptr, |
| 126 | const char* extraModifiers = nullptr, |
| 127 | bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 128 | SkASSERT(kVoid_GrSLType != type); |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 129 | fType = type; |
| 130 | fTypeModifier = typeModifier; |
| 131 | fName = name; |
| 132 | fCount = kNonArray; |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 133 | fLayoutQualifier = layoutQualifier; |
| 134 | if (extraModifiers) { |
| 135 | fExtraModifiers.printf("%s ", extraModifiers); |
| 136 | } |
| 137 | fUseUniformFloatArrays = useUniformFloatArrays; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Set all var options |
| 142 | */ |
| 143 | void set(GrSLType type, |
| 144 | const SkString& name, |
| 145 | int count, |
| 146 | TypeModifier typeModifier, |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 147 | const char* layoutQualifier = nullptr, |
| 148 | const char* extraModifiers = nullptr, |
| 149 | bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { |
| 150 | SkASSERT(kVoid_GrSLType != type); |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 151 | fType = type; |
| 152 | fTypeModifier = typeModifier; |
| 153 | fName = name; |
| 154 | fCount = count; |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 155 | fLayoutQualifier = layoutQualifier; |
| 156 | if (extraModifiers) { |
| 157 | fExtraModifiers.printf("%s ", extraModifiers); |
| 158 | } |
| 159 | fUseUniformFloatArrays = useUniformFloatArrays; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Set all var options |
| 164 | */ |
| 165 | void set(GrSLType type, |
| 166 | const char* name, |
| 167 | int count, |
| 168 | TypeModifier typeModifier, |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 169 | const char* layoutQualifier = nullptr, |
| 170 | const char* extraModifiers = nullptr, |
| 171 | bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) { |
| 172 | SkASSERT(kVoid_GrSLType != type); |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 173 | fType = type; |
| 174 | fTypeModifier = typeModifier; |
| 175 | fName = name; |
| 176 | fCount = count; |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 177 | fLayoutQualifier = layoutQualifier; |
| 178 | if (extraModifiers) { |
| 179 | fExtraModifiers.printf("%s ", extraModifiers); |
| 180 | } |
| 181 | fUseUniformFloatArrays = useUniformFloatArrays; |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Is the var an array. |
| 186 | */ |
| 187 | bool isArray() const { return kNonArray != fCount; } |
| 188 | /** |
| 189 | * Is this an unsized array, (i.e. declared with []). |
| 190 | */ |
| 191 | bool isUnsizedArray() const { return kUnsizedArray == fCount; } |
| 192 | /** |
| 193 | * Get the array length of the var. |
| 194 | */ |
| 195 | int getArrayCount() const { return fCount; } |
| 196 | /** |
| 197 | * Set the array length of the var |
| 198 | */ |
| 199 | void setArrayCount(int count) { fCount = count; } |
| 200 | /** |
| 201 | * Set to be a non-array. |
| 202 | */ |
| 203 | void setNonArray() { fCount = kNonArray; } |
| 204 | /** |
| 205 | * Set to be an unsized array. |
| 206 | */ |
| 207 | void setUnsizedArray() { fCount = kUnsizedArray; } |
| 208 | |
| 209 | /** |
| 210 | * Access the var name as a writable string |
| 211 | */ |
| 212 | SkString* accessName() { return &fName; } |
| 213 | /** |
| 214 | * Set the var name |
| 215 | */ |
| 216 | void setName(const SkString& n) { fName = n; } |
| 217 | void setName(const char* n) { fName = n; } |
| 218 | |
| 219 | /** |
| 220 | * Get the var name. |
| 221 | */ |
| 222 | const SkString& getName() const { return fName; } |
| 223 | |
| 224 | /** |
| 225 | * Shortcut for this->getName().c_str(); |
| 226 | */ |
| 227 | const char* c_str() const { return this->getName().c_str(); } |
| 228 | |
| 229 | /** |
| 230 | * Get the type of the var |
| 231 | */ |
| 232 | GrSLType getType() const { return fType; } |
| 233 | /** |
| 234 | * Set the type of the var |
| 235 | */ |
| 236 | void setType(GrSLType type) { fType = type; } |
| 237 | |
| 238 | TypeModifier getTypeModifier() const { return fTypeModifier; } |
| 239 | void setTypeModifier(TypeModifier type) { fTypeModifier = type; } |
| 240 | |
| 241 | /** |
Brian Salomon | 6039768 | 2016-11-22 15:06:46 -0500 | [diff] [blame] | 242 | * Appends to the layout qualifier |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 243 | */ |
Brian Salomon | 6039768 | 2016-11-22 15:06:46 -0500 | [diff] [blame] | 244 | void addLayoutQualifier(const char* layoutQualifier) { |
| 245 | if (!layoutQualifier || !strlen(layoutQualifier)) { |
| 246 | return; |
| 247 | } |
| 248 | if (fLayoutQualifier.isEmpty()) { |
| 249 | fLayoutQualifier = layoutQualifier; |
| 250 | } else { |
| 251 | fLayoutQualifier.appendf(", %s", layoutQualifier); |
| 252 | } |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 253 | } |
| 254 | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 255 | void setIOType(GrIOType); |
| 256 | |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 257 | void addModifier(const char* modifier) { |
| 258 | if (modifier) { |
| 259 | fExtraModifiers.appendf("%s ", modifier); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Write a declaration of this variable to out. |
| 265 | */ |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 266 | void appendDecl(const GrShaderCaps*, SkString* out) const; |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 267 | |
| 268 | void appendArrayAccess(int index, SkString* out) const { |
| 269 | out->appendf("%s[%d]%s", |
| 270 | this->getName().c_str(), |
| 271 | index, |
| 272 | fUseUniformFloatArrays ? "" : ".x"); |
| 273 | } |
| 274 | |
| 275 | void appendArrayAccess(const char* indexName, SkString* out) const { |
| 276 | out->appendf("%s[%s]%s", |
| 277 | this->getName().c_str(), |
| 278 | indexName, |
| 279 | fUseUniformFloatArrays ? "" : ".x"); |
| 280 | } |
| 281 | |
| 282 | private: |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 283 | GrSLType fType; |
| 284 | TypeModifier fTypeModifier; |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 285 | int fCount; |
Brian Salomon | 99938a8 | 2016-11-21 13:41:08 -0500 | [diff] [blame] | 286 | /// Work around driver bugs on some hardware that don't correctly |
| 287 | /// support uniform float [] |
| 288 | bool fUseUniformFloatArrays; |
| 289 | |
| 290 | SkString fName; |
| 291 | SkString fLayoutQualifier; |
| 292 | SkString fExtraModifiers; |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 293 | }; |
| 294 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 295 | #endif |