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 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Defaults to a float with no precision specifier |
| 39 | */ |
| 40 | GrShaderVar() |
| 41 | : fType(kFloat_GrSLType) |
| 42 | , fTypeModifier(kNone_TypeModifier) |
| 43 | , fCount(kNonArray) |
bsalomon | c0bd648 | 2014-12-09 10:04:14 -0800 | [diff] [blame] | 44 | , fPrecision(kDefault_GrSLPrecision) { |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 45 | } |
| 46 | |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 47 | GrShaderVar(const SkString& name, GrSLType type, int arrayCount = kNonArray, |
bsalomon | c0bd648 | 2014-12-09 10:04:14 -0800 | [diff] [blame] | 48 | GrSLPrecision precision = kDefault_GrSLPrecision) |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 49 | : fType(type) |
| 50 | , fTypeModifier(kNone_TypeModifier) |
| 51 | , fName(name) |
| 52 | , fCount(arrayCount) |
| 53 | , fPrecision(precision) { |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 54 | SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type)); |
joshualitt | 23e280d | 2014-09-18 12:26:38 -0700 | [diff] [blame] | 55 | SkASSERT(kVoid_GrSLType != type); |
| 56 | } |
| 57 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 58 | GrShaderVar(const char* name, GrSLType type, int arrayCount = kNonArray, |
bsalomon | c0bd648 | 2014-12-09 10:04:14 -0800 | [diff] [blame] | 59 | GrSLPrecision precision = kDefault_GrSLPrecision) |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 60 | : fType(type) |
| 61 | , fTypeModifier(kNone_TypeModifier) |
| 62 | , fName(name) |
| 63 | , fCount(arrayCount) |
| 64 | , fPrecision(precision) { |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 65 | SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type)); |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 66 | SkASSERT(kVoid_GrSLType != type); |
| 67 | } |
| 68 | |
| 69 | GrShaderVar(const char* name, GrSLType type, TypeModifier typeModifier, |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame^] | 70 | int arrayCount = kNonArray, GrSLPrecision precision = kDefault_GrSLPrecision) |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 71 | : fType(type) |
| 72 | , fTypeModifier(typeModifier) |
| 73 | , fName(name) |
| 74 | , fCount(arrayCount) |
| 75 | , fPrecision(precision) { |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 76 | SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type)); |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 77 | SkASSERT(kVoid_GrSLType != type); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Values for array count that have special meaning. We allow 1-sized arrays. |
| 82 | */ |
| 83 | enum { |
| 84 | kNonArray = 0, // not an array |
| 85 | kUnsizedArray = -1, // an unsized array (declared with []) |
| 86 | }; |
| 87 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 88 | void set(GrSLType type, |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 89 | const SkString& name, |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame^] | 90 | TypeModifier typeModifier = kNone_TypeModifier, |
| 91 | GrSLPrecision precision = kDefault_GrSLPrecision, |
| 92 | int count = kNonArray) { |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 93 | SkASSERT(kVoid_GrSLType != type); |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 94 | SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type)); |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 95 | fType = type; |
| 96 | fTypeModifier = typeModifier; |
| 97 | fName = name; |
| 98 | fCount = count; |
| 99 | fPrecision = precision; |
| 100 | } |
| 101 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 102 | void set(GrSLType type, |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 103 | const char* name, |
robertphillips | 46d36f0 | 2015-01-18 08:14:14 -0800 | [diff] [blame^] | 104 | TypeModifier typeModifier = kNone_TypeModifier, |
| 105 | GrSLPrecision precision = kDefault_GrSLPrecision, |
| 106 | int count = kNonArray) { |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 107 | SkASSERT(kVoid_GrSLType != type); |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 108 | SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type)); |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 109 | fType = type; |
| 110 | fTypeModifier = typeModifier; |
| 111 | fName = name; |
| 112 | fCount = count; |
| 113 | fPrecision = precision; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Is the var an array. |
| 118 | */ |
| 119 | bool isArray() const { return kNonArray != fCount; } |
| 120 | /** |
| 121 | * Is this an unsized array, (i.e. declared with []). |
| 122 | */ |
| 123 | bool isUnsizedArray() const { return kUnsizedArray == fCount; } |
| 124 | /** |
| 125 | * Get the array length of the var. |
| 126 | */ |
| 127 | int getArrayCount() const { return fCount; } |
| 128 | /** |
| 129 | * Set the array length of the var |
| 130 | */ |
| 131 | void setArrayCount(int count) { fCount = count; } |
| 132 | /** |
| 133 | * Set to be a non-array. |
| 134 | */ |
| 135 | void setNonArray() { fCount = kNonArray; } |
| 136 | /** |
| 137 | * Set to be an unsized array. |
| 138 | */ |
| 139 | void setUnsizedArray() { fCount = kUnsizedArray; } |
| 140 | |
| 141 | /** |
| 142 | * Access the var name as a writable string |
| 143 | */ |
| 144 | SkString* accessName() { return &fName; } |
| 145 | /** |
| 146 | * Set the var name |
| 147 | */ |
| 148 | void setName(const SkString& n) { fName = n; } |
| 149 | void setName(const char* n) { fName = n; } |
| 150 | |
| 151 | /** |
| 152 | * Get the var name. |
| 153 | */ |
| 154 | const SkString& getName() const { return fName; } |
| 155 | |
| 156 | /** |
| 157 | * Shortcut for this->getName().c_str(); |
| 158 | */ |
| 159 | const char* c_str() const { return this->getName().c_str(); } |
| 160 | |
| 161 | /** |
| 162 | * Get the type of the var |
| 163 | */ |
| 164 | GrSLType getType() const { return fType; } |
| 165 | /** |
| 166 | * Set the type of the var |
| 167 | */ |
| 168 | void setType(GrSLType type) { fType = type; } |
| 169 | |
| 170 | TypeModifier getTypeModifier() const { return fTypeModifier; } |
| 171 | void setTypeModifier(TypeModifier type) { fTypeModifier = type; } |
| 172 | |
| 173 | /** |
| 174 | * Get the precision of the var |
| 175 | */ |
bsalomon | c0bd648 | 2014-12-09 10:04:14 -0800 | [diff] [blame] | 176 | GrSLPrecision getPrecision() const { return fPrecision; } |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 177 | |
| 178 | /** |
| 179 | * Set the precision of the var |
| 180 | */ |
bsalomon | c0bd648 | 2014-12-09 10:04:14 -0800 | [diff] [blame] | 181 | void setPrecision(GrSLPrecision p) { fPrecision = p; } |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 182 | |
| 183 | protected: |
| 184 | GrSLType fType; |
| 185 | TypeModifier fTypeModifier; |
| 186 | SkString fName; |
| 187 | int fCount; |
bsalomon | c0bd648 | 2014-12-09 10:04:14 -0800 | [diff] [blame] | 188 | GrSLPrecision fPrecision; |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | #endif |