blob: 53d0c0f2016dcb937d4d4d951c0e9a7e17c9803e [file] [log] [blame]
joshualitt249af152014-09-15 11:41:13 -07001/*
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
14class GrShaderVar {
15public:
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
joshualitt249af152014-09-15 11:41:13 -070037 /**
38 * Defaults to a float with no precision specifier
39 */
40 GrShaderVar()
41 : fType(kFloat_GrSLType)
42 , fTypeModifier(kNone_TypeModifier)
43 , fCount(kNonArray)
bsalomonc0bd6482014-12-09 10:04:14 -080044 , fPrecision(kDefault_GrSLPrecision) {
joshualitt249af152014-09-15 11:41:13 -070045 }
46
joshualitt23e280d2014-09-18 12:26:38 -070047 GrShaderVar(const SkString& name, GrSLType type, int arrayCount = kNonArray,
bsalomonc0bd6482014-12-09 10:04:14 -080048 GrSLPrecision precision = kDefault_GrSLPrecision)
joshualitt23e280d2014-09-18 12:26:38 -070049 : fType(type)
50 , fTypeModifier(kNone_TypeModifier)
51 , fName(name)
52 , fCount(arrayCount)
53 , fPrecision(precision) {
bsalomon422f56f2014-12-09 10:18:12 -080054 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type));
joshualitt23e280d2014-09-18 12:26:38 -070055 SkASSERT(kVoid_GrSLType != type);
56 }
57
joshualitt249af152014-09-15 11:41:13 -070058 GrShaderVar(const char* name, GrSLType type, int arrayCount = kNonArray,
bsalomonc0bd6482014-12-09 10:04:14 -080059 GrSLPrecision precision = kDefault_GrSLPrecision)
joshualitt249af152014-09-15 11:41:13 -070060 : fType(type)
61 , fTypeModifier(kNone_TypeModifier)
62 , fName(name)
63 , fCount(arrayCount)
64 , fPrecision(precision) {
bsalomon422f56f2014-12-09 10:18:12 -080065 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type));
joshualitt249af152014-09-15 11:41:13 -070066 SkASSERT(kVoid_GrSLType != type);
67 }
68
69 GrShaderVar(const char* name, GrSLType type, TypeModifier typeModifier,
bsalomonc0bd6482014-12-09 10:04:14 -080070 int arrayCount = kNonArray, GrSLPrecision precision = kDefault_GrSLPrecision)
joshualitt249af152014-09-15 11:41:13 -070071 : fType(type)
72 , fTypeModifier(typeModifier)
73 , fName(name)
74 , fCount(arrayCount)
75 , fPrecision(precision) {
bsalomon422f56f2014-12-09 10:18:12 -080076 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type));
joshualitt249af152014-09-15 11:41:13 -070077 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 /**
89 * Sets as a non-array.
90 */
91 void set(GrSLType type,
92 TypeModifier typeModifier,
93 const SkString& name,
bsalomonc0bd6482014-12-09 10:04:14 -080094 GrSLPrecision precision = kDefault_GrSLPrecision) {
joshualitt249af152014-09-15 11:41:13 -070095 SkASSERT(kVoid_GrSLType != type);
bsalomon422f56f2014-12-09 10:18:12 -080096 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type));
joshualitt249af152014-09-15 11:41:13 -070097 fType = type;
98 fTypeModifier = typeModifier;
99 fName = name;
100 fCount = kNonArray;
101 fPrecision = precision;
102 }
103
104 /**
105 * Sets as a non-array.
106 */
107 void set(GrSLType type,
108 TypeModifier typeModifier,
109 const char* name,
bsalomonc0bd6482014-12-09 10:04:14 -0800110 GrSLPrecision precision = kDefault_GrSLPrecision) {
joshualitt249af152014-09-15 11:41:13 -0700111 SkASSERT(kVoid_GrSLType != type);
bsalomon422f56f2014-12-09 10:18:12 -0800112 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type));
joshualitt249af152014-09-15 11:41:13 -0700113 fType = type;
114 fTypeModifier = typeModifier;
115 fName = name;
116 fCount = kNonArray;
117 fPrecision = precision;
118 }
119
120 /**
121 * Set all var options
122 */
123 void set(GrSLType type,
124 TypeModifier typeModifier,
125 const SkString& name,
126 int count,
bsalomonc0bd6482014-12-09 10:04:14 -0800127 GrSLPrecision precision = kDefault_GrSLPrecision) {
joshualitt249af152014-09-15 11:41:13 -0700128 SkASSERT(kVoid_GrSLType != type);
bsalomon422f56f2014-12-09 10:18:12 -0800129 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type));
joshualitt249af152014-09-15 11:41:13 -0700130 fType = type;
131 fTypeModifier = typeModifier;
132 fName = name;
133 fCount = count;
134 fPrecision = precision;
135 }
136
137 /**
138 * Set all var options
139 */
140 void set(GrSLType type,
141 TypeModifier typeModifier,
142 const char* name,
143 int count,
bsalomonc0bd6482014-12-09 10:04:14 -0800144 GrSLPrecision precision = kDefault_GrSLPrecision) {
joshualitt249af152014-09-15 11:41:13 -0700145 SkASSERT(kVoid_GrSLType != type);
bsalomon422f56f2014-12-09 10:18:12 -0800146 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type));
joshualitt249af152014-09-15 11:41:13 -0700147 fType = type;
148 fTypeModifier = typeModifier;
149 fName = name;
150 fCount = count;
151 fPrecision = precision;
152 }
153
154 /**
155 * Is the var an array.
156 */
157 bool isArray() const { return kNonArray != fCount; }
158 /**
159 * Is this an unsized array, (i.e. declared with []).
160 */
161 bool isUnsizedArray() const { return kUnsizedArray == fCount; }
162 /**
163 * Get the array length of the var.
164 */
165 int getArrayCount() const { return fCount; }
166 /**
167 * Set the array length of the var
168 */
169 void setArrayCount(int count) { fCount = count; }
170 /**
171 * Set to be a non-array.
172 */
173 void setNonArray() { fCount = kNonArray; }
174 /**
175 * Set to be an unsized array.
176 */
177 void setUnsizedArray() { fCount = kUnsizedArray; }
178
179 /**
180 * Access the var name as a writable string
181 */
182 SkString* accessName() { return &fName; }
183 /**
184 * Set the var name
185 */
186 void setName(const SkString& n) { fName = n; }
187 void setName(const char* n) { fName = n; }
188
189 /**
190 * Get the var name.
191 */
192 const SkString& getName() const { return fName; }
193
194 /**
195 * Shortcut for this->getName().c_str();
196 */
197 const char* c_str() const { return this->getName().c_str(); }
198
199 /**
200 * Get the type of the var
201 */
202 GrSLType getType() const { return fType; }
203 /**
204 * Set the type of the var
205 */
206 void setType(GrSLType type) { fType = type; }
207
208 TypeModifier getTypeModifier() const { return fTypeModifier; }
209 void setTypeModifier(TypeModifier type) { fTypeModifier = type; }
210
211 /**
212 * Get the precision of the var
213 */
bsalomonc0bd6482014-12-09 10:04:14 -0800214 GrSLPrecision getPrecision() const { return fPrecision; }
joshualitt249af152014-09-15 11:41:13 -0700215
216 /**
217 * Set the precision of the var
218 */
bsalomonc0bd6482014-12-09 10:04:14 -0800219 void setPrecision(GrSLPrecision p) { fPrecision = p; }
joshualitt249af152014-09-15 11:41:13 -0700220
221protected:
222 GrSLType fType;
223 TypeModifier fTypeModifier;
224 SkString fName;
225 int fCount;
bsalomonc0bd6482014-12-09 10:04:14 -0800226 GrSLPrecision fPrecision;
joshualitt249af152014-09-15 11:41:13 -0700227};
228
229#endif