GrShaderVar.h revision c0bd6484f621e4a1033be318b1947a5d32157c13
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
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
37    /**
38     * Defaults to a float with no precision specifier
39     */
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)
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)
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)
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    /**
86     * Sets as a non-array.
87     */
88    void set(GrSLType type,
89             TypeModifier typeModifier,
90             const SkString& name,
91             GrSLPrecision precision = kDefault_GrSLPrecision) {
92        SkASSERT(kVoid_GrSLType != type);
93        fType = type;
94        fTypeModifier = typeModifier;
95        fName = name;
96        fCount = kNonArray;
97        fPrecision = precision;
98    }
99
100    /**
101     * Sets as a non-array.
102     */
103    void set(GrSLType type,
104             TypeModifier typeModifier,
105             const char* name,
106             GrSLPrecision precision = kDefault_GrSLPrecision) {
107        SkASSERT(kVoid_GrSLType != type);
108        fType = type;
109        fTypeModifier = typeModifier;
110        fName = name;
111        fCount = kNonArray;
112        fPrecision = precision;
113    }
114
115    /**
116     * Set all var options
117     */
118    void set(GrSLType type,
119             TypeModifier typeModifier,
120             const SkString& name,
121             int count,
122             GrSLPrecision precision = kDefault_GrSLPrecision) {
123        SkASSERT(kVoid_GrSLType != type);
124        fType = type;
125        fTypeModifier = typeModifier;
126        fName = name;
127        fCount = count;
128        fPrecision = precision;
129    }
130
131    /**
132     * Set all var options
133     */
134    void set(GrSLType type,
135             TypeModifier typeModifier,
136             const char* name,
137             int count,
138             GrSLPrecision precision = kDefault_GrSLPrecision) {
139        SkASSERT(kVoid_GrSLType != type);
140        fType = type;
141        fTypeModifier = typeModifier;
142        fName = name;
143        fCount = count;
144        fPrecision = precision;
145    }
146
147    /**
148     * Is the var an array.
149     */
150    bool isArray() const { return kNonArray != fCount; }
151    /**
152     * Is this an unsized array, (i.e. declared with []).
153     */
154    bool isUnsizedArray() const { return kUnsizedArray == fCount; }
155    /**
156     * Get the array length of the var.
157     */
158    int getArrayCount() const { return fCount; }
159    /**
160     * Set the array length of the var
161     */
162    void setArrayCount(int count) { fCount = count; }
163    /**
164     * Set to be a non-array.
165     */
166    void setNonArray() { fCount = kNonArray; }
167    /**
168     * Set to be an unsized array.
169     */
170    void setUnsizedArray() { fCount = kUnsizedArray; }
171
172    /**
173     * Access the var name as a writable string
174     */
175    SkString* accessName() { return &fName; }
176    /**
177     * Set the var name
178     */
179    void setName(const SkString& n) { fName = n; }
180    void setName(const char* n) { fName = n; }
181
182    /**
183     * Get the var name.
184     */
185    const SkString& getName() const { return fName; }
186
187    /**
188     * Shortcut for this->getName().c_str();
189     */
190    const char* c_str() const { return this->getName().c_str(); }
191
192    /**
193     * Get the type of the var
194     */
195    GrSLType getType() const { return fType; }
196    /**
197     * Set the type of the var
198     */
199    void setType(GrSLType type) { fType = type; }
200
201    TypeModifier getTypeModifier() const { return fTypeModifier; }
202    void setTypeModifier(TypeModifier type) { fTypeModifier = type; }
203
204    /**
205     * Get the precision of the var
206     */
207    GrSLPrecision getPrecision() const { return fPrecision; }
208
209    /**
210     * Set the precision of the var
211     */
212    void setPrecision(GrSLPrecision p) { fPrecision = p; }
213
214protected:
215    GrSLType        fType;
216    TypeModifier    fTypeModifier;
217    SkString        fName;
218    int             fCount;
219    GrSLPrecision   fPrecision;
220};
221
222#endif
223