blob: 791d3594aea3dcd67d958940b5fab2a4a1888990 [file] [log] [blame]
joshualitt249af152014-09-15 11:41:13 -07001/*
Brian Salomon99938a82016-11-21 13:41:08 -05002 * Copyright 2016 Google Inc.
joshualitt249af152014-09-15 11:41:13 -07003 *
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkString.h"
12#include "include/private/GrTypesPriv.h"
joshualitt249af152014-09-15 11:41:13 -070013
Brian Salomon94efbf52016-11-29 13:43:05 -050014class GrShaderCaps;
Brian Salomon99938a82016-11-21 13:41:08 -050015
16#define USE_UNIFORM_FLOAT_ARRAYS true
17
18/**
19 * Represents a variable in a shader
20 */
joshualitt249af152014-09-15 11:41:13 -070021class GrShaderVar {
22public:
joshualitt249af152014-09-15 11:41:13 -070023 enum TypeModifier {
24 kNone_TypeModifier,
25 kOut_TypeModifier,
26 kIn_TypeModifier,
27 kInOut_TypeModifier,
28 kUniform_TypeModifier,
joshualitt249af152014-09-15 11:41:13 -070029 };
30
joshualitt249af152014-09-15 11:41:13 -070031 /**
Ben Wagner63fd7602017-10-09 15:45:33 -040032 * Values for array count that have special meaning. We allow 1-sized arrays.git
joshualitt249af152014-09-15 11:41:13 -070033 */
34 enum {
35 kNonArray = 0, // not an array
36 kUnsizedArray = -1, // an unsized array (declared with [])
37 };
38
Brian Salomon99938a82016-11-21 13:41:08 -050039 /**
Ethan Nicholasf7b88202017-09-18 14:10:39 -040040 * Defaults to a non-arry half with no type modifier or layout qualifier.
Brian Salomon99938a82016-11-21 13:41:08 -050041 */
42 GrShaderVar()
Ethan Nicholasf7b88202017-09-18 14:10:39 -040043 : fType(kHalf_GrSLType)
Brian Salomon99938a82016-11-21 13:41:08 -050044 , fTypeModifier(kNone_TypeModifier)
45 , fCount(kNonArray)
Brian Salomon99938a82016-11-21 13:41:08 -050046 , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS) {
47 }
48
Ethan Nicholas858fecc2019-03-07 13:19:18 -050049 GrShaderVar(const SkString& name, GrSLType type, int arrayCount = kNonArray)
Brian Salomon99938a82016-11-21 13:41:08 -050050 : fType(type)
51 , fTypeModifier(kNone_TypeModifier)
52 , fCount(arrayCount)
Brian Salomon99938a82016-11-21 13:41:08 -050053 , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS)
54 , fName(name) {
55 SkASSERT(kVoid_GrSLType != type);
56 fUseUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS;
57 }
58
Ethan Nicholas858fecc2019-03-07 13:19:18 -050059 GrShaderVar(const char* name, GrSLType type, int arrayCount = kNonArray)
Brian Salomon99938a82016-11-21 13:41:08 -050060 : fType(type)
61 , fTypeModifier(kNone_TypeModifier)
62 , fCount(arrayCount)
Brian Salomon99938a82016-11-21 13:41:08 -050063 , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS)
64 , fName(name) {
65 SkASSERT(kVoid_GrSLType != type);
66 fUseUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS;
67 }
68
Ethan Nicholas858fecc2019-03-07 13:19:18 -050069 GrShaderVar(const char* name, GrSLType type, TypeModifier typeModifier)
Brian Salomon99938a82016-11-21 13:41:08 -050070 : fType(type)
71 , fTypeModifier(typeModifier)
72 , fCount(kNonArray)
Brian Salomon99938a82016-11-21 13:41:08 -050073 , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS)
74 , fName(name) {
75 SkASSERT(kVoid_GrSLType != type);
76 }
77
Ethan Nicholas858fecc2019-03-07 13:19:18 -050078 GrShaderVar(const char* name, GrSLType type, TypeModifier typeModifier, int arrayCount)
Brian Salomon99938a82016-11-21 13:41:08 -050079 : fType(type)
80 , fTypeModifier(typeModifier)
81 , fCount(arrayCount)
Brian Salomon99938a82016-11-21 13:41:08 -050082 , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS)
83 , fName(name) {
84 SkASSERT(kVoid_GrSLType != type);
85 }
86
87 GrShaderVar(const GrShaderVar& that)
88 : fType(that.fType)
89 , fTypeModifier(that.fTypeModifier)
90 , fCount(that.fCount)
Brian Salomon99938a82016-11-21 13:41:08 -050091 , fUseUniformFloatArrays(USE_UNIFORM_FLOAT_ARRAYS)
92 , fName(that.fName)
93 , fLayoutQualifier(that.fLayoutQualifier)
94 , fExtraModifiers(that.fExtraModifiers) {
95 SkASSERT(kVoid_GrSLType != that.getType());
96 }
97
98 /**
99 * Sets as a non-array.
100 */
joshualitt249af152014-09-15 11:41:13 -0700101 void set(GrSLType type,
joshualitt249af152014-09-15 11:41:13 -0700102 const SkString& name,
robertphillips46d36f02015-01-18 08:14:14 -0800103 TypeModifier typeModifier = kNone_TypeModifier,
Brian Salomon99938a82016-11-21 13:41:08 -0500104 const char* layoutQualifier = nullptr,
105 const char* extraModifiers = nullptr,
106 bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) {
joshualitt249af152014-09-15 11:41:13 -0700107 SkASSERT(kVoid_GrSLType != type);
108 fType = type;
109 fTypeModifier = typeModifier;
110 fName = name;
Brian Salomon99938a82016-11-21 13:41:08 -0500111 fCount = kNonArray;
Brian Salomon99938a82016-11-21 13:41:08 -0500112 fLayoutQualifier = layoutQualifier;
113 if (extraModifiers) {
114 fExtraModifiers.printf("%s ", extraModifiers);
115 }
116 fUseUniformFloatArrays = useUniformFloatArrays;
joshualitt249af152014-09-15 11:41:13 -0700117 }
118
Brian Salomon99938a82016-11-21 13:41:08 -0500119 /**
120 * Sets as a non-array.
121 */
joshualitt249af152014-09-15 11:41:13 -0700122 void set(GrSLType type,
joshualitt249af152014-09-15 11:41:13 -0700123 const char* name,
robertphillips46d36f02015-01-18 08:14:14 -0800124 TypeModifier typeModifier = kNone_TypeModifier,
Brian Salomon99938a82016-11-21 13:41:08 -0500125 const char* layoutQualifier = nullptr,
126 const char* extraModifiers = nullptr,
127 bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) {
joshualitt249af152014-09-15 11:41:13 -0700128 SkASSERT(kVoid_GrSLType != type);
Brian Salomon99938a82016-11-21 13:41:08 -0500129 fType = type;
130 fTypeModifier = typeModifier;
131 fName = name;
132 fCount = kNonArray;
Brian Salomon99938a82016-11-21 13:41:08 -0500133 fLayoutQualifier = layoutQualifier;
134 if (extraModifiers) {
135 fExtraModifiers.printf("%s ", extraModifiers);
136 }
137 fUseUniformFloatArrays = useUniformFloatArrays;
138 }
139
140 /**
141 * Set all var options
142 */
143 void set(GrSLType type,
144 const SkString& name,
145 int count,
146 TypeModifier typeModifier,
Brian Salomon99938a82016-11-21 13:41:08 -0500147 const char* layoutQualifier = nullptr,
148 const char* extraModifiers = nullptr,
149 bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) {
150 SkASSERT(kVoid_GrSLType != type);
joshualitt249af152014-09-15 11:41:13 -0700151 fType = type;
152 fTypeModifier = typeModifier;
153 fName = name;
154 fCount = count;
Brian Salomon99938a82016-11-21 13:41:08 -0500155 fLayoutQualifier = layoutQualifier;
156 if (extraModifiers) {
157 fExtraModifiers.printf("%s ", extraModifiers);
158 }
159 fUseUniformFloatArrays = useUniformFloatArrays;
160 }
161
162 /**
163 * Set all var options
164 */
165 void set(GrSLType type,
166 const char* name,
167 int count,
168 TypeModifier typeModifier,
Brian Salomon99938a82016-11-21 13:41:08 -0500169 const char* layoutQualifier = nullptr,
170 const char* extraModifiers = nullptr,
171 bool useUniformFloatArrays = USE_UNIFORM_FLOAT_ARRAYS) {
172 SkASSERT(kVoid_GrSLType != type);
Brian Salomon99938a82016-11-21 13:41:08 -0500173 fType = type;
174 fTypeModifier = typeModifier;
175 fName = name;
176 fCount = count;
Brian Salomon99938a82016-11-21 13:41:08 -0500177 fLayoutQualifier = layoutQualifier;
178 if (extraModifiers) {
179 fExtraModifiers.printf("%s ", extraModifiers);
180 }
181 fUseUniformFloatArrays = useUniformFloatArrays;
joshualitt249af152014-09-15 11:41:13 -0700182 }
183
184 /**
185 * Is the var an array.
186 */
187 bool isArray() const { return kNonArray != fCount; }
188 /**
189 * Is this an unsized array, (i.e. declared with []).
190 */
191 bool isUnsizedArray() const { return kUnsizedArray == fCount; }
192 /**
193 * Get the array length of the var.
194 */
195 int getArrayCount() const { return fCount; }
196 /**
197 * Set the array length of the var
198 */
199 void setArrayCount(int count) { fCount = count; }
200 /**
201 * Set to be a non-array.
202 */
203 void setNonArray() { fCount = kNonArray; }
204 /**
205 * Set to be an unsized array.
206 */
207 void setUnsizedArray() { fCount = kUnsizedArray; }
208
209 /**
210 * Access the var name as a writable string
211 */
212 SkString* accessName() { return &fName; }
213 /**
214 * Set the var name
215 */
216 void setName(const SkString& n) { fName = n; }
217 void setName(const char* n) { fName = n; }
218
219 /**
220 * Get the var name.
221 */
222 const SkString& getName() const { return fName; }
223
224 /**
225 * Shortcut for this->getName().c_str();
226 */
227 const char* c_str() const { return this->getName().c_str(); }
228
229 /**
230 * Get the type of the var
231 */
232 GrSLType getType() const { return fType; }
233 /**
234 * Set the type of the var
235 */
236 void setType(GrSLType type) { fType = type; }
237
238 TypeModifier getTypeModifier() const { return fTypeModifier; }
239 void setTypeModifier(TypeModifier type) { fTypeModifier = type; }
240
241 /**
Brian Salomon60397682016-11-22 15:06:46 -0500242 * Appends to the layout qualifier
Brian Salomon99938a82016-11-21 13:41:08 -0500243 */
Brian Salomon60397682016-11-22 15:06:46 -0500244 void addLayoutQualifier(const char* layoutQualifier) {
245 if (!layoutQualifier || !strlen(layoutQualifier)) {
246 return;
247 }
248 if (fLayoutQualifier.isEmpty()) {
249 fLayoutQualifier = layoutQualifier;
250 } else {
251 fLayoutQualifier.appendf(", %s", layoutQualifier);
252 }
Brian Salomon99938a82016-11-21 13:41:08 -0500253 }
254
Brian Salomonf9f45122016-11-29 11:59:17 -0500255 void setIOType(GrIOType);
256
Brian Salomon99938a82016-11-21 13:41:08 -0500257 void addModifier(const char* modifier) {
258 if (modifier) {
259 fExtraModifiers.appendf("%s ", modifier);
260 }
261 }
262
263 /**
264 * Write a declaration of this variable to out.
265 */
Brian Salomon94efbf52016-11-29 13:43:05 -0500266 void appendDecl(const GrShaderCaps*, SkString* out) const;
Brian Salomon99938a82016-11-21 13:41:08 -0500267
268 void appendArrayAccess(int index, SkString* out) const {
269 out->appendf("%s[%d]%s",
270 this->getName().c_str(),
271 index,
272 fUseUniformFloatArrays ? "" : ".x");
273 }
274
275 void appendArrayAccess(const char* indexName, SkString* out) const {
276 out->appendf("%s[%s]%s",
277 this->getName().c_str(),
278 indexName,
279 fUseUniformFloatArrays ? "" : ".x");
280 }
281
282private:
joshualitt249af152014-09-15 11:41:13 -0700283 GrSLType fType;
284 TypeModifier fTypeModifier;
joshualitt249af152014-09-15 11:41:13 -0700285 int fCount;
Brian Salomon99938a82016-11-21 13:41:08 -0500286 /// Work around driver bugs on some hardware that don't correctly
287 /// support uniform float []
288 bool fUseUniformFloatArrays;
289
290 SkString fName;
291 SkString fLayoutQualifier;
292 SkString fExtraModifiers;
joshualitt249af152014-09-15 11:41:13 -0700293};
294
Brian Salomon94efbf52016-11-29 13:43:05 -0500295#endif