• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 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 "include/core/SkString.h"
12 #include "include/private/GrTypesPriv.h"
13 
14 class GrShaderCaps;
15 
16 /**
17  * Represents a variable in a shader
18  */
19 class GrShaderVar {
20 public:
21     enum class TypeModifier {
22         None,
23         Out,
24         In,
25         InOut,
26         Uniform,
27     };
28 
29     /** Values for array count that have special meaning. We allow 1-sized arrays. */
30     enum {
31         kNonArray     =  0, // not an array
32     };
33 
34     /** Defaults to a void with no type modifier or layout qualifier. */
GrShaderVar()35     GrShaderVar()
36             : fType(kVoid_GrSLType)
37             , fTypeModifier(TypeModifier::None)
38             , fCount(kNonArray) {}
39 
40     GrShaderVar(SkString name, GrSLType type, int arrayCount = kNonArray)
fType(type)41             : fType(type)
42             , fTypeModifier(TypeModifier::None)
43             , fCount(arrayCount)
44             , fName(std::move(name)) {}
45     GrShaderVar(const char* name, GrSLType type, int arrayCount = kNonArray)
GrShaderVar(SkString (name),type,arrayCount)46         : GrShaderVar(SkString(name), type, arrayCount) {}
47 
GrShaderVar(SkString name,GrSLType type,TypeModifier typeModifier)48     GrShaderVar(SkString name, GrSLType type, TypeModifier typeModifier)
49             : fType(type)
50             , fTypeModifier(typeModifier)
51             , fCount(kNonArray)
52             , fName(std::move(name)) {}
GrShaderVar(const char * name,GrSLType type,TypeModifier typeModifier)53     GrShaderVar(const char* name, GrSLType type, TypeModifier typeModifier)
54         : GrShaderVar(SkString(name), type, typeModifier) {}
55 
GrShaderVar(SkString name,GrSLType type,TypeModifier typeModifier,int arrayCount)56     GrShaderVar(SkString name, GrSLType type, TypeModifier typeModifier, int arrayCount)
57             : fType(type)
58             , fTypeModifier(typeModifier)
59             , fCount(arrayCount)
60             , fName(std::move(name)) {}
61 
GrShaderVar(SkString name,GrSLType type,TypeModifier typeModifier,int arrayCount,SkString layoutQualifier,SkString extraModifier)62     GrShaderVar(SkString name, GrSLType type, TypeModifier typeModifier, int arrayCount,
63                 SkString layoutQualifier, SkString extraModifier)
64             : fType(type)
65             , fTypeModifier(typeModifier)
66             , fCount(arrayCount)
67             , fName(std::move(name))
68             , fLayoutQualifier(std::move(layoutQualifier))
69             , fExtraModifiers(std::move(extraModifier)) {}
70 
71     GrShaderVar(const GrShaderVar&) = default;
72     GrShaderVar& operator=(const GrShaderVar&) = default;
73     GrShaderVar(GrShaderVar&&) = default;
74     GrShaderVar& operator=(GrShaderVar&&) = default;
75 
76     /** Sets as a non-array. */
set(GrSLType type,const char * name)77     void set(GrSLType type,
78              const char* name) {
79         SkASSERT(kVoid_GrSLType != type);
80         fType = type;
81         fName = name;
82     }
83 
84     /** Is the var an array. */
isArray()85     bool isArray() const { return kNonArray != fCount; }
86 
87     /** Get the array length. */
getArrayCount()88     int getArrayCount() const { return fCount; }
89 
90     /** Get the name. */
getName()91     const SkString& getName() const { return fName; }
92 
93     /** Shortcut for this->getName().c_str(); */
c_str()94     const char* c_str() const { return this->getName().c_str(); }
95 
96     /** Get the type. */
getType()97     GrSLType getType() const { return fType; }
98 
getTypeModifier()99     TypeModifier getTypeModifier() const { return fTypeModifier; }
setTypeModifier(TypeModifier type)100     void setTypeModifier(TypeModifier type) { fTypeModifier = type; }
101 
102     /** Appends to the layout qualifier. */
addLayoutQualifier(const char * layoutQualifier)103     void addLayoutQualifier(const char* layoutQualifier) {
104         if (!layoutQualifier || !strlen(layoutQualifier)) {
105             return;
106         }
107         if (fLayoutQualifier.isEmpty()) {
108             fLayoutQualifier = layoutQualifier;
109         } else {
110             fLayoutQualifier.appendf(", %s", layoutQualifier);
111         }
112     }
113 
114     /** Appends to the modifiers. */
addModifier(const char * modifier)115     void addModifier(const char* modifier) {
116         if (!modifier || !strlen(modifier)) {
117             return;
118         }
119         if (fExtraModifiers.isEmpty()) {
120             fExtraModifiers = modifier;
121         } else {
122             fExtraModifiers.appendf(" %s", modifier);
123         }
124     }
125 
126     /** Write a declaration of this variable to out. */
127     void appendDecl(const GrShaderCaps*, SkString* out) const;
128 
129 private:
130     GrSLType        fType;
131     TypeModifier    fTypeModifier;
132     int             fCount;
133 
134     SkString        fName;
135     SkString        fLayoutQualifier;
136     SkString        fExtraModifiers;
137 };
138 
139 #endif
140