bsalomon@google.com | 4fa6694 | 2011-09-20 19:06:12 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #ifndef GrGLShaderVar_DEFINED |
| 10 | #define GrGLShaderVar_DEFINED |
| 11 | |
| 12 | #include "GrGLInterface.h" |
| 13 | |
| 14 | /** |
| 15 | * Represents a variable in a shader |
| 16 | */ |
| 17 | class GrGLShaderVar { |
| 18 | public: |
| 19 | |
| 20 | enum Type { |
| 21 | kFloat_Type, |
| 22 | kVec2f_Type, |
| 23 | kVec3f_Type, |
| 24 | kVec4f_Type, |
| 25 | kMat33f_Type, |
| 26 | kSampler2D_Type, |
| 27 | }; |
| 28 | |
| 29 | /** |
| 30 | * Defaults to a float with no precision specifier |
| 31 | */ |
| 32 | GrGLShaderVar() { |
| 33 | fType = kFloat_Type; |
| 34 | fCount = kNonArray; |
| 35 | fEmitPrecision = false; |
| 36 | } |
| 37 | |
| 38 | GrGLShaderVar(const GrGLShaderVar& var) |
| 39 | : fType(var.fType) |
| 40 | , fName(var.fName) |
| 41 | , fCount(var.fCount) |
| 42 | , fEmitPrecision(var.fEmitPrecision) {} |
| 43 | |
| 44 | /** |
| 45 | * Values for array count that have special meaning. We allow 1-sized arrays. |
| 46 | */ |
| 47 | enum { |
| 48 | kNonArray = 0, // not an array |
| 49 | kUnsizedArray = -1, // an unsized array (declared with []) |
| 50 | }; |
| 51 | |
| 52 | /** |
| 53 | * Sets as a non-array. |
| 54 | */ |
| 55 | void set(Type type, |
| 56 | const GrStringBuilder& name, |
| 57 | bool emitPrecision = false) { |
| 58 | fType = type; |
| 59 | fName = name; |
| 60 | fCount = kNonArray; |
| 61 | fEmitPrecision = emitPrecision; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Sets as a non-array. |
| 66 | */ |
| 67 | void set(Type type, |
| 68 | const char* name, |
| 69 | bool specifyPrecision = false) { |
| 70 | fType = type; |
| 71 | fName = name; |
| 72 | fCount = kNonArray; |
| 73 | fEmitPrecision = specifyPrecision; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Set all var options |
| 78 | */ |
| 79 | void set(Type type, |
| 80 | const GrStringBuilder& name, |
| 81 | int count, |
| 82 | bool specifyPrecision = false) { |
| 83 | fType = type; |
| 84 | fName = name; |
| 85 | fCount = count; |
| 86 | fEmitPrecision = specifyPrecision; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Set all var options |
| 91 | */ |
| 92 | void set(Type type, |
| 93 | const char* name, |
| 94 | int count, |
| 95 | bool specifyPrecision = false) { |
| 96 | fType = type; |
| 97 | fName = name; |
| 98 | fCount = count; |
| 99 | fEmitPrecision = specifyPrecision; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Is the var an array. |
| 104 | */ |
| 105 | bool isArray() const { return kNonArray != fCount; } |
| 106 | /** |
| 107 | * Is this an unsized array, (i.e. declared with []). |
| 108 | */ |
| 109 | bool isUnsizedArray() const { return kUnsizedArray == fCount; } |
| 110 | /** |
| 111 | * Get the array length of the var. |
| 112 | */ |
| 113 | int getArrayCount() const { return fCount; } |
| 114 | /** |
| 115 | * Set the array length of the var |
| 116 | */ |
| 117 | void setArrayCount(int count) { fCount = count; } |
| 118 | /** |
| 119 | * Set to be a non-array. |
| 120 | */ |
| 121 | void setNonArray() { fCount = kNonArray; } |
| 122 | /** |
| 123 | * Set to be an unsized array. |
| 124 | */ |
| 125 | void setUnsizedArray() { fCount = kUnsizedArray; } |
| 126 | |
| 127 | /** |
| 128 | * Access the var name as a writable string |
| 129 | */ |
| 130 | GrStringBuilder* accessName() { return &fName; } |
| 131 | /** |
| 132 | * Set the var name |
| 133 | */ |
| 134 | void setName(const GrStringBuilder& n) { fName = n; } |
| 135 | void setName(const char* n) { fName = n; } |
| 136 | /** |
| 137 | * Get the var name. |
| 138 | */ |
| 139 | const GrStringBuilder& getName() const { return fName; } |
| 140 | |
| 141 | /** |
| 142 | * Get the type of the var |
| 143 | */ |
| 144 | Type getType() const { return fType; } |
| 145 | /** |
| 146 | * Set the type of the var |
| 147 | */ |
| 148 | void setType(Type type) { fType = type; } |
| 149 | |
| 150 | /** |
| 151 | * Must the variable declaration emit a precision specifier |
| 152 | */ |
| 153 | bool emitsPrecision() const { return fEmitPrecision; } |
| 154 | /** |
| 155 | * Specify whether the declaration should specify precision |
| 156 | */ |
| 157 | void setEmitPrecision(bool p) { fEmitPrecision = p; } |
| 158 | |
| 159 | /** |
| 160 | * Write a declaration of this variable to out. |
| 161 | */ |
| 162 | void appendDecl(const GrGLInterface* gl, GrStringBuilder* out) const { |
| 163 | if (this->emitsPrecision()) { |
| 164 | out->append(PrecisionString(gl)); |
| 165 | out->append(" "); |
| 166 | } |
| 167 | if (this->isArray()) { |
| 168 | if (this->isUnsizedArray()) { |
| 169 | out->appendf("%s %s[]", |
| 170 | TypeString(this->getType()), |
| 171 | this->getName().c_str()); |
| 172 | } else { |
| 173 | GrAssert(this->getArrayCount() > 0); |
| 174 | out->appendf("%s %s[%d]", |
| 175 | TypeString(this->getType()), |
| 176 | this->getName().c_str(), |
| 177 | this->getArrayCount()); |
| 178 | } |
| 179 | } else { |
| 180 | out->appendf("%s %s", |
| 181 | TypeString(this->getType()), |
| 182 | this->getName().c_str()); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | static const char* TypeString(Type t) { |
| 187 | switch (t) { |
| 188 | case kFloat_Type: |
| 189 | return "float"; |
| 190 | case kVec2f_Type: |
| 191 | return "vec2"; |
| 192 | case kVec3f_Type: |
| 193 | return "vec3"; |
| 194 | case kVec4f_Type: |
| 195 | return "vec4"; |
| 196 | case kMat33f_Type: |
| 197 | return "mat3"; |
| 198 | case kSampler2D_Type: |
| 199 | return "sampler2D"; |
| 200 | default: |
| 201 | GrCrash("Unknown shader var type."); |
| 202 | return ""; // suppress warning |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | private: |
| 207 | static const char* PrecisionString(const GrGLInterface* gl) { |
| 208 | return gl->supportsDesktop() ? "" : "mediump"; |
| 209 | } |
| 210 | |
| 211 | Type fType; |
| 212 | GrStringBuilder fName; |
| 213 | int fCount; |
| 214 | bool fEmitPrecision; |
| 215 | }; |
| 216 | |
| 217 | #endif |