joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 GrShaderVar_DEFINED |
| 9 | #define GrShaderVar_DEFINED |
| 10 | |
| 11 | #include "GrTypesPriv.h" |
| 12 | #include "SkString.h" |
| 13 | |
| 14 | class GrShaderVar { |
| 15 | public: |
| 16 | /** |
| 17 | * Early versions of GLSL have Varying and Attribute; those are later |
| 18 | * deprecated, but we still need to know whether a Varying variable |
| 19 | * should be treated as In or Out. |
| 20 | * |
| 21 | * TODO This really shouldn't live here, but until we have c++11, there is really no good way |
| 22 | * to write extensible enums. In reality, only none, out, in, inout, and uniform really |
| 23 | * make sense on this base class |
| 24 | */ |
| 25 | enum TypeModifier { |
| 26 | kNone_TypeModifier, |
| 27 | kOut_TypeModifier, |
| 28 | kIn_TypeModifier, |
| 29 | kInOut_TypeModifier, |
| 30 | kUniform_TypeModifier, |
| 31 | // GL Specific types below |
| 32 | kAttribute_TypeModifier, |
| 33 | kVaryingIn_TypeModifier, |
| 34 | kVaryingOut_TypeModifier |
| 35 | }; |
| 36 | |
| 37 | enum Precision { |
bsalomon | c92aadc | 2014-12-04 13:02:47 -0800 | [diff] [blame] | 38 | kLow_Precision, |
| 39 | kMedium_Precision, |
| 40 | kHigh_Precision, |
| 41 | |
| 42 | // Default precision is medium. This is because on OpenGL ES 2 highp support is not |
| 43 | // guaranteed. On (non-ES) OpenGL the specifiers have no effect on precision. |
| 44 | kDefault_Precision = kMedium_Precision, |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame^] | 45 | |
| 46 | kLast_Precision = kHigh_Precision |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 47 | }; |
bsalomon | 17168df | 2014-12-09 09:00:49 -0800 | [diff] [blame^] | 48 | static const int kPrecisionCount = kLast_Precision + 1; |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Defaults to a float with no precision specifier |
| 52 | */ |
| 53 | GrShaderVar() |
| 54 | : fType(kFloat_GrSLType) |
| 55 | , fTypeModifier(kNone_TypeModifier) |
| 56 | , fCount(kNonArray) |
| 57 | , fPrecision(kDefault_Precision) { |
| 58 | } |
| 59 | |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 60 | GrShaderVar(const SkString& name, GrSLType type, int arrayCount = kNonArray, |
| 61 | Precision precision = kDefault_Precision) |
| 62 | : fType(type) |
| 63 | , fTypeModifier(kNone_TypeModifier) |
| 64 | , fName(name) |
| 65 | , fCount(arrayCount) |
| 66 | , fPrecision(precision) { |
| 67 | SkASSERT(kVoid_GrSLType != type); |
| 68 | } |
| 69 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 70 | GrShaderVar(const char* name, GrSLType type, int arrayCount = kNonArray, |
| 71 | Precision precision = kDefault_Precision) |
| 72 | : fType(type) |
| 73 | , fTypeModifier(kNone_TypeModifier) |
| 74 | , fName(name) |
| 75 | , fCount(arrayCount) |
| 76 | , fPrecision(precision) { |
| 77 | SkASSERT(kVoid_GrSLType != type); |
| 78 | } |
| 79 | |
| 80 | GrShaderVar(const char* name, GrSLType type, TypeModifier typeModifier, |
| 81 | int arrayCount = kNonArray, Precision precision = kDefault_Precision) |
| 82 | : fType(type) |
| 83 | , fTypeModifier(typeModifier) |
| 84 | , fName(name) |
| 85 | , fCount(arrayCount) |
| 86 | , fPrecision(precision) { |
| 87 | SkASSERT(kVoid_GrSLType != type); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Values for array count that have special meaning. We allow 1-sized arrays. |
| 92 | */ |
| 93 | enum { |
| 94 | kNonArray = 0, // not an array |
| 95 | kUnsizedArray = -1, // an unsized array (declared with []) |
| 96 | }; |
| 97 | |
| 98 | /** |
| 99 | * Sets as a non-array. |
| 100 | */ |
| 101 | void set(GrSLType type, |
| 102 | TypeModifier typeModifier, |
| 103 | const SkString& name, |
| 104 | Precision precision = kDefault_Precision) { |
| 105 | SkASSERT(kVoid_GrSLType != type); |
| 106 | fType = type; |
| 107 | fTypeModifier = typeModifier; |
| 108 | fName = name; |
| 109 | fCount = kNonArray; |
| 110 | fPrecision = precision; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Sets as a non-array. |
| 115 | */ |
| 116 | void set(GrSLType type, |
| 117 | TypeModifier typeModifier, |
| 118 | const char* name, |
| 119 | Precision precision = kDefault_Precision) { |
| 120 | SkASSERT(kVoid_GrSLType != type); |
| 121 | fType = type; |
| 122 | fTypeModifier = typeModifier; |
| 123 | fName = name; |
| 124 | fCount = kNonArray; |
| 125 | fPrecision = precision; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Set all var options |
| 130 | */ |
| 131 | void set(GrSLType type, |
| 132 | TypeModifier typeModifier, |
| 133 | const SkString& name, |
| 134 | int count, |
| 135 | Precision precision = kDefault_Precision) { |
| 136 | SkASSERT(kVoid_GrSLType != type); |
| 137 | fType = type; |
| 138 | fTypeModifier = typeModifier; |
| 139 | fName = name; |
| 140 | fCount = count; |
| 141 | fPrecision = precision; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Set all var options |
| 146 | */ |
| 147 | void set(GrSLType type, |
| 148 | TypeModifier typeModifier, |
| 149 | const char* name, |
| 150 | int count, |
| 151 | Precision precision = kDefault_Precision) { |
| 152 | SkASSERT(kVoid_GrSLType != type); |
| 153 | fType = type; |
| 154 | fTypeModifier = typeModifier; |
| 155 | fName = name; |
| 156 | fCount = count; |
| 157 | fPrecision = precision; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Is the var an array. |
| 162 | */ |
| 163 | bool isArray() const { return kNonArray != fCount; } |
| 164 | /** |
| 165 | * Is this an unsized array, (i.e. declared with []). |
| 166 | */ |
| 167 | bool isUnsizedArray() const { return kUnsizedArray == fCount; } |
| 168 | /** |
| 169 | * Get the array length of the var. |
| 170 | */ |
| 171 | int getArrayCount() const { return fCount; } |
| 172 | /** |
| 173 | * Set the array length of the var |
| 174 | */ |
| 175 | void setArrayCount(int count) { fCount = count; } |
| 176 | /** |
| 177 | * Set to be a non-array. |
| 178 | */ |
| 179 | void setNonArray() { fCount = kNonArray; } |
| 180 | /** |
| 181 | * Set to be an unsized array. |
| 182 | */ |
| 183 | void setUnsizedArray() { fCount = kUnsizedArray; } |
| 184 | |
| 185 | /** |
| 186 | * Access the var name as a writable string |
| 187 | */ |
| 188 | SkString* accessName() { return &fName; } |
| 189 | /** |
| 190 | * Set the var name |
| 191 | */ |
| 192 | void setName(const SkString& n) { fName = n; } |
| 193 | void setName(const char* n) { fName = n; } |
| 194 | |
| 195 | /** |
| 196 | * Get the var name. |
| 197 | */ |
| 198 | const SkString& getName() const { return fName; } |
| 199 | |
| 200 | /** |
| 201 | * Shortcut for this->getName().c_str(); |
| 202 | */ |
| 203 | const char* c_str() const { return this->getName().c_str(); } |
| 204 | |
| 205 | /** |
| 206 | * Get the type of the var |
| 207 | */ |
| 208 | GrSLType getType() const { return fType; } |
| 209 | /** |
| 210 | * Set the type of the var |
| 211 | */ |
| 212 | void setType(GrSLType type) { fType = type; } |
| 213 | |
| 214 | TypeModifier getTypeModifier() const { return fTypeModifier; } |
| 215 | void setTypeModifier(TypeModifier type) { fTypeModifier = type; } |
| 216 | |
| 217 | /** |
| 218 | * Get the precision of the var |
| 219 | */ |
| 220 | Precision getPrecision() const { return fPrecision; } |
| 221 | |
| 222 | /** |
| 223 | * Set the precision of the var |
| 224 | */ |
| 225 | void setPrecision(Precision p) { fPrecision = p; } |
| 226 | |
| 227 | protected: |
| 228 | GrSLType fType; |
| 229 | TypeModifier fTypeModifier; |
| 230 | SkString fName; |
| 231 | int fCount; |
| 232 | Precision fPrecision; |
| 233 | }; |
| 234 | |
| 235 | #endif |