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 { |
| 38 | kLow_Precision, // lowp |
| 39 | kMedium_Precision, // mediump |
| 40 | kHigh_Precision, // highp |
| 41 | kDefault_Precision, // Default for the current context. We make |
| 42 | // fragment shaders default to mediump on ES2 |
| 43 | // because highp support is not guaranteed (and |
| 44 | // we haven't been motivated to test for it). |
| 45 | // Otherwise, highp. |
| 46 | }; |
| 47 | |
| 48 | /** |
| 49 | * Defaults to a float with no precision specifier |
| 50 | */ |
| 51 | GrShaderVar() |
| 52 | : fType(kFloat_GrSLType) |
| 53 | , fTypeModifier(kNone_TypeModifier) |
| 54 | , fCount(kNonArray) |
| 55 | , fPrecision(kDefault_Precision) { |
| 56 | } |
| 57 | |
| 58 | GrShaderVar(const char* name, GrSLType type, int arrayCount = kNonArray, |
| 59 | Precision precision = kDefault_Precision) |
| 60 | : fType(type) |
| 61 | , fTypeModifier(kNone_TypeModifier) |
| 62 | , fName(name) |
| 63 | , fCount(arrayCount) |
| 64 | , fPrecision(precision) { |
| 65 | SkASSERT(kVoid_GrSLType != type); |
| 66 | } |
| 67 | |
| 68 | GrShaderVar(const char* name, GrSLType type, TypeModifier typeModifier, |
| 69 | int arrayCount = kNonArray, Precision precision = kDefault_Precision) |
| 70 | : fType(type) |
| 71 | , fTypeModifier(typeModifier) |
| 72 | , fName(name) |
| 73 | , fCount(arrayCount) |
| 74 | , fPrecision(precision) { |
| 75 | SkASSERT(kVoid_GrSLType != type); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Values for array count that have special meaning. We allow 1-sized arrays. |
| 80 | */ |
| 81 | enum { |
| 82 | kNonArray = 0, // not an array |
| 83 | kUnsizedArray = -1, // an unsized array (declared with []) |
| 84 | }; |
| 85 | |
| 86 | /** |
| 87 | * Sets as a non-array. |
| 88 | */ |
| 89 | void set(GrSLType type, |
| 90 | TypeModifier typeModifier, |
| 91 | const SkString& name, |
| 92 | Precision precision = kDefault_Precision) { |
| 93 | SkASSERT(kVoid_GrSLType != type); |
| 94 | fType = type; |
| 95 | fTypeModifier = typeModifier; |
| 96 | fName = name; |
| 97 | fCount = kNonArray; |
| 98 | fPrecision = precision; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Sets as a non-array. |
| 103 | */ |
| 104 | void set(GrSLType type, |
| 105 | TypeModifier typeModifier, |
| 106 | const char* name, |
| 107 | Precision precision = kDefault_Precision) { |
| 108 | SkASSERT(kVoid_GrSLType != type); |
| 109 | fType = type; |
| 110 | fTypeModifier = typeModifier; |
| 111 | fName = name; |
| 112 | fCount = kNonArray; |
| 113 | fPrecision = precision; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Set all var options |
| 118 | */ |
| 119 | void set(GrSLType type, |
| 120 | TypeModifier typeModifier, |
| 121 | const SkString& name, |
| 122 | int count, |
| 123 | Precision precision = kDefault_Precision) { |
| 124 | SkASSERT(kVoid_GrSLType != type); |
| 125 | fType = type; |
| 126 | fTypeModifier = typeModifier; |
| 127 | fName = name; |
| 128 | fCount = count; |
| 129 | fPrecision = precision; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Set all var options |
| 134 | */ |
| 135 | void set(GrSLType type, |
| 136 | TypeModifier typeModifier, |
| 137 | const char* name, |
| 138 | int count, |
| 139 | Precision precision = kDefault_Precision) { |
| 140 | SkASSERT(kVoid_GrSLType != type); |
| 141 | fType = type; |
| 142 | fTypeModifier = typeModifier; |
| 143 | fName = name; |
| 144 | fCount = count; |
| 145 | fPrecision = precision; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Is the var an array. |
| 150 | */ |
| 151 | bool isArray() const { return kNonArray != fCount; } |
| 152 | /** |
| 153 | * Is this an unsized array, (i.e. declared with []). |
| 154 | */ |
| 155 | bool isUnsizedArray() const { return kUnsizedArray == fCount; } |
| 156 | /** |
| 157 | * Get the array length of the var. |
| 158 | */ |
| 159 | int getArrayCount() const { return fCount; } |
| 160 | /** |
| 161 | * Set the array length of the var |
| 162 | */ |
| 163 | void setArrayCount(int count) { fCount = count; } |
| 164 | /** |
| 165 | * Set to be a non-array. |
| 166 | */ |
| 167 | void setNonArray() { fCount = kNonArray; } |
| 168 | /** |
| 169 | * Set to be an unsized array. |
| 170 | */ |
| 171 | void setUnsizedArray() { fCount = kUnsizedArray; } |
| 172 | |
| 173 | /** |
| 174 | * Access the var name as a writable string |
| 175 | */ |
| 176 | SkString* accessName() { return &fName; } |
| 177 | /** |
| 178 | * Set the var name |
| 179 | */ |
| 180 | void setName(const SkString& n) { fName = n; } |
| 181 | void setName(const char* n) { fName = n; } |
| 182 | |
| 183 | /** |
| 184 | * Get the var name. |
| 185 | */ |
| 186 | const SkString& getName() const { return fName; } |
| 187 | |
| 188 | /** |
| 189 | * Shortcut for this->getName().c_str(); |
| 190 | */ |
| 191 | const char* c_str() const { return this->getName().c_str(); } |
| 192 | |
| 193 | /** |
| 194 | * Get the type of the var |
| 195 | */ |
| 196 | GrSLType getType() const { return fType; } |
| 197 | /** |
| 198 | * Set the type of the var |
| 199 | */ |
| 200 | void setType(GrSLType type) { fType = type; } |
| 201 | |
| 202 | TypeModifier getTypeModifier() const { return fTypeModifier; } |
| 203 | void setTypeModifier(TypeModifier type) { fTypeModifier = type; } |
| 204 | |
| 205 | /** |
| 206 | * Get the precision of the var |
| 207 | */ |
| 208 | Precision getPrecision() const { return fPrecision; } |
| 209 | |
| 210 | /** |
| 211 | * Set the precision of the var |
| 212 | */ |
| 213 | void setPrecision(Precision p) { fPrecision = p; } |
| 214 | |
| 215 | protected: |
| 216 | GrSLType fType; |
| 217 | TypeModifier fTypeModifier; |
| 218 | SkString fName; |
| 219 | int fCount; |
| 220 | Precision fPrecision; |
| 221 | }; |
| 222 | |
| 223 | #endif |