blob: 68ce34b8ef8d0520dee17d577711597eb5d740c4 [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,
robertphillips46d36f02015-01-18 08:14: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
joshualitt249af152014-09-15 11:41:13 -070088 void set(GrSLType type,
joshualitt249af152014-09-15 11:41:13 -070089 const SkString& name,
robertphillips46d36f02015-01-18 08:14:14 -080090 TypeModifier typeModifier = kNone_TypeModifier,
91 GrSLPrecision precision = kDefault_GrSLPrecision,
92 int count = kNonArray) {
joshualitt249af152014-09-15 11:41:13 -070093 SkASSERT(kVoid_GrSLType != type);
bsalomon422f56f2014-12-09 10:18:12 -080094 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type));
joshualitt249af152014-09-15 11:41:13 -070095 fType = type;
96 fTypeModifier = typeModifier;
97 fName = name;
98 fCount = count;
99 fPrecision = precision;
100 }
101
joshualitt249af152014-09-15 11:41:13 -0700102 void set(GrSLType type,
joshualitt249af152014-09-15 11:41:13 -0700103 const char* name,
robertphillips46d36f02015-01-18 08:14:14 -0800104 TypeModifier typeModifier = kNone_TypeModifier,
105 GrSLPrecision precision = kDefault_GrSLPrecision,
106 int count = kNonArray) {
joshualitt249af152014-09-15 11:41:13 -0700107 SkASSERT(kVoid_GrSLType != type);
bsalomon422f56f2014-12-09 10:18:12 -0800108 SkASSERT(kDefault_GrSLPrecision == precision || GrSLTypeIsFloatType(type));
joshualitt249af152014-09-15 11:41:13 -0700109 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 */
bsalomonc0bd6482014-12-09 10:04:14 -0800176 GrSLPrecision getPrecision() const { return fPrecision; }
joshualitt249af152014-09-15 11:41:13 -0700177
178 /**
179 * Set the precision of the var
180 */
bsalomonc0bd6482014-12-09 10:04:14 -0800181 void setPrecision(GrSLPrecision p) { fPrecision = p; }
joshualitt249af152014-09-15 11:41:13 -0700182
183protected:
184 GrSLType fType;
185 TypeModifier fTypeModifier;
186 SkString fName;
187 int fCount;
bsalomonc0bd6482014-12-09 10:04:14 -0800188 GrSLPrecision fPrecision;
joshualitt249af152014-09-15 11:41:13 -0700189};
190
191#endif