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 /** 38 * Defaults to a float with no precision specifier 39 */ GrShaderVar()40 GrShaderVar() 41 : fType(kFloat_GrSLType) 42 , fTypeModifier(kNone_TypeModifier) 43 , fCount(kNonArray) 44 , fPrecision(kDefault_GrSLPrecision) { 45 } 46 47 GrShaderVar(const SkString& name, GrSLType type, int arrayCount = kNonArray, 48 GrSLPrecision precision = kDefault_GrSLPrecision) fType(type)49 : fType(type) 50 , fTypeModifier(kNone_TypeModifier) 51 , fName(name) 52 , fCount(arrayCount) 53 , fPrecision(precision) { 54 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type)); 55 SkASSERT(kVoid_GrSLType != type); 56 } 57 58 GrShaderVar(const char* name, GrSLType type, int arrayCount = kNonArray, 59 GrSLPrecision precision = kDefault_GrSLPrecision) fType(type)60 : fType(type) 61 , fTypeModifier(kNone_TypeModifier) 62 , fName(name) 63 , fCount(arrayCount) 64 , fPrecision(precision) { 65 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type)); 66 SkASSERT(kVoid_GrSLType != type); 67 } 68 69 GrShaderVar(const char* name, GrSLType type, TypeModifier typeModifier, 70 int arrayCount = kNonArray, GrSLPrecision precision = kDefault_GrSLPrecision) fType(type)71 : fType(type) 72 , fTypeModifier(typeModifier) 73 , fName(name) 74 , fCount(arrayCount) 75 , fPrecision(precision) { 76 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type)); 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 88 void set(GrSLType type, 89 const SkString& name, 90 TypeModifier typeModifier = kNone_TypeModifier, 91 GrSLPrecision precision = kDefault_GrSLPrecision, 92 int count = kNonArray) { 93 SkASSERT(kVoid_GrSLType != type); 94 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type)); 95 fType = type; 96 fTypeModifier = typeModifier; 97 fName = name; 98 fCount = count; 99 fPrecision = precision; 100 } 101 102 void set(GrSLType type, 103 const char* name, 104 TypeModifier typeModifier = kNone_TypeModifier, 105 GrSLPrecision precision = kDefault_GrSLPrecision, 106 int count = kNonArray) { 107 SkASSERT(kVoid_GrSLType != type); 108 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type)); 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 */ isArray()119 bool isArray() const { return kNonArray != fCount; } 120 /** 121 * Is this an unsized array, (i.e. declared with []). 122 */ isUnsizedArray()123 bool isUnsizedArray() const { return kUnsizedArray == fCount; } 124 /** 125 * Get the array length of the var. 126 */ getArrayCount()127 int getArrayCount() const { return fCount; } 128 /** 129 * Set the array length of the var 130 */ setArrayCount(int count)131 void setArrayCount(int count) { fCount = count; } 132 /** 133 * Set to be a non-array. 134 */ setNonArray()135 void setNonArray() { fCount = kNonArray; } 136 /** 137 * Set to be an unsized array. 138 */ setUnsizedArray()139 void setUnsizedArray() { fCount = kUnsizedArray; } 140 141 /** 142 * Access the var name as a writable string 143 */ accessName()144 SkString* accessName() { return &fName; } 145 /** 146 * Set the var name 147 */ setName(const SkString & n)148 void setName(const SkString& n) { fName = n; } setName(const char * n)149 void setName(const char* n) { fName = n; } 150 151 /** 152 * Get the var name. 153 */ getName()154 const SkString& getName() const { return fName; } 155 156 /** 157 * Shortcut for this->getName().c_str(); 158 */ c_str()159 const char* c_str() const { return this->getName().c_str(); } 160 161 /** 162 * Get the type of the var 163 */ getType()164 GrSLType getType() const { return fType; } 165 /** 166 * Set the type of the var 167 */ setType(GrSLType type)168 void setType(GrSLType type) { fType = type; } 169 getTypeModifier()170 TypeModifier getTypeModifier() const { return fTypeModifier; } setTypeModifier(TypeModifier type)171 void setTypeModifier(TypeModifier type) { fTypeModifier = type; } 172 173 /** 174 * Get the precision of the var 175 */ getPrecision()176 GrSLPrecision getPrecision() const { return fPrecision; } 177 178 /** 179 * Set the precision of the var 180 */ setPrecision(GrSLPrecision p)181 void setPrecision(GrSLPrecision p) { fPrecision = p; } 182 183 protected: 184 GrSLType fType; 185 TypeModifier fTypeModifier; 186 SkString fName; 187 int fCount; 188 GrSLPrecision fPrecision; 189 }; 190 191 #endif 192