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(kVoid_GrSLType != type);
55     }
56 
57     GrShaderVar(const char* name, GrSLType type, int arrayCount = kNonArray,
58                 GrSLPrecision precision = kDefault_GrSLPrecision)
fType(type)59         : fType(type)
60         , fTypeModifier(kNone_TypeModifier)
61         , fName(name)
62         , fCount(arrayCount)
63         , fPrecision(precision) {
64         SkASSERT(kVoid_GrSLType != type);
65     }
66 
67     GrShaderVar(const char* name, GrSLType type, TypeModifier typeModifier,
68                 int arrayCount = kNonArray, GrSLPrecision precision = kDefault_GrSLPrecision)
fType(type)69         : fType(type)
70         , fTypeModifier(typeModifier)
71         , fName(name)
72         , fCount(arrayCount)
73         , fPrecision(precision) {
74         SkASSERT(kVoid_GrSLType != type);
75     }
76 
77     /**
78      * Values for array count that have special meaning. We allow 1-sized arrays.
79      */
80     enum {
81         kNonArray     =  0, // not an array
82         kUnsizedArray = -1, // an unsized array (declared with [])
83     };
84 
85     void set(GrSLType type,
86              const SkString& name,
87              TypeModifier typeModifier = kNone_TypeModifier,
88              GrSLPrecision precision = kDefault_GrSLPrecision,
89              int count = kNonArray) {
90         SkASSERT(kVoid_GrSLType != type);
91         fType = type;
92         fTypeModifier = typeModifier;
93         fName = name;
94         fCount = count;
95         fPrecision = precision;
96     }
97 
98     void set(GrSLType type,
99              const char* name,
100              TypeModifier typeModifier = kNone_TypeModifier,
101              GrSLPrecision precision = kDefault_GrSLPrecision,
102              int count = kNonArray) {
103         SkASSERT(kVoid_GrSLType != type);
104         fType = type;
105         fTypeModifier = typeModifier;
106         fName = name;
107         fCount = count;
108         fPrecision = precision;
109     }
110 
111     /**
112      * Is the var an array.
113      */
isArray()114     bool isArray() const { return kNonArray != fCount; }
115     /**
116      * Is this an unsized array, (i.e. declared with []).
117      */
isUnsizedArray()118     bool isUnsizedArray() const { return kUnsizedArray == fCount; }
119     /**
120      * Get the array length of the var.
121      */
getArrayCount()122     int getArrayCount() const { return fCount; }
123     /**
124      * Set the array length of the var
125      */
setArrayCount(int count)126     void setArrayCount(int count) { fCount = count; }
127     /**
128      * Set to be a non-array.
129      */
setNonArray()130     void setNonArray() { fCount = kNonArray; }
131     /**
132      * Set to be an unsized array.
133      */
setUnsizedArray()134     void setUnsizedArray() { fCount = kUnsizedArray; }
135 
136     /**
137      * Access the var name as a writable string
138      */
accessName()139     SkString* accessName() { return &fName; }
140     /**
141      * Set the var name
142      */
setName(const SkString & n)143     void setName(const SkString& n) { fName = n; }
setName(const char * n)144     void setName(const char* n) { fName = n; }
145 
146     /**
147      * Get the var name.
148      */
getName()149     const SkString& getName() const { return fName; }
150 
151     /**
152      * Shortcut for this->getName().c_str();
153      */
c_str()154     const char* c_str() const { return this->getName().c_str(); }
155 
156     /**
157      * Get the type of the var
158      */
getType()159     GrSLType getType() const { return fType; }
160     /**
161      * Set the type of the var
162      */
setType(GrSLType type)163     void setType(GrSLType type) { fType = type; }
164 
getTypeModifier()165     TypeModifier getTypeModifier() const { return fTypeModifier; }
setTypeModifier(TypeModifier type)166     void setTypeModifier(TypeModifier type) { fTypeModifier = type; }
167 
168     /**
169      * Get the precision of the var
170      */
getPrecision()171     GrSLPrecision getPrecision() const { return fPrecision; }
172 
173     /**
174      * Set the precision of the var
175      */
setPrecision(GrSLPrecision p)176     void setPrecision(GrSLPrecision p) { fPrecision = p; }
177 
178 protected:
179     GrSLType        fType;
180     TypeModifier    fTypeModifier;
181     SkString        fName;
182     int             fCount;
183     GrSLPrecision   fPrecision;
184 };
185 
186 #endif
187