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 | kUnsizedArray = -1, // an unsized array (declared with []) |
33 | }; |
34 | |
35 | /** Defaults to a void with no type modifier or layout qualifier. */ |
36 | GrShaderVar() |
37 | : fType(kVoid_GrSLType) |
38 | , fTypeModifier(TypeModifier::None) |
39 | , fCount(kNonArray) {} |
40 | |
41 | GrShaderVar(SkString name, GrSLType type, int arrayCount = kNonArray) |
42 | : fType(type) |
43 | , fTypeModifier(TypeModifier::None) |
44 | , fCount(arrayCount) |
45 | , fName(std::move(name)) {} |
46 | GrShaderVar(const char* name, GrSLType type, int arrayCount = kNonArray) |
47 | : GrShaderVar(SkString(name), type, arrayCount) {} |
48 | |
49 | GrShaderVar(SkString name, GrSLType type, TypeModifier typeModifier) |
50 | : fType(type) |
51 | , fTypeModifier(typeModifier) |
52 | , fCount(kNonArray) |
53 | , fName(std::move(name)) {} |
54 | GrShaderVar(const char* name, GrSLType type, TypeModifier typeModifier) |
55 | : GrShaderVar(SkString(name), type, typeModifier) {} |
56 | |
57 | GrShaderVar(SkString name, GrSLType type, TypeModifier typeModifier, int arrayCount) |
58 | : fType(type) |
59 | , fTypeModifier(typeModifier) |
60 | , fCount(arrayCount) |
61 | , fName(std::move(name)) {} |
62 | |
63 | GrShaderVar(SkString name, GrSLType type, TypeModifier typeModifier, int arrayCount, |
64 | SkString layoutQualifier, SkString ) |
65 | : fType(type) |
66 | , fTypeModifier(typeModifier) |
67 | , fCount(arrayCount) |
68 | , fName(std::move(name)) |
69 | , fLayoutQualifier(std::move(layoutQualifier)) |
70 | , fExtraModifiers(std::move(extraModifier)) {} |
71 | |
72 | GrShaderVar(const GrShaderVar&) = default; |
73 | GrShaderVar& operator=(const GrShaderVar&) = default; |
74 | GrShaderVar(GrShaderVar&&) = default; |
75 | GrShaderVar& operator=(GrShaderVar&&) = default; |
76 | |
77 | /** Sets as a non-array. */ |
78 | void set(GrSLType type, |
79 | const char* name) { |
80 | SkASSERT(kVoid_GrSLType != type); |
81 | fType = type; |
82 | fName = name; |
83 | } |
84 | |
85 | /** Is the var an array. */ |
86 | bool isArray() const { return kNonArray != fCount; } |
87 | |
88 | /** Is this an unsized array, (i.e. declared with []). */ |
89 | bool isUnsizedArray() const { return kUnsizedArray == fCount; } |
90 | |
91 | /** Get the array length. */ |
92 | int getArrayCount() const { return fCount; } |
93 | |
94 | /** Get the name. */ |
95 | const SkString& getName() const { return fName; } |
96 | |
97 | /** Shortcut for this->getName().c_str(); */ |
98 | const char* c_str() const { return this->getName().c_str(); } |
99 | |
100 | /** Get the type. */ |
101 | GrSLType getType() const { return fType; } |
102 | |
103 | TypeModifier getTypeModifier() const { return fTypeModifier; } |
104 | void setTypeModifier(TypeModifier type) { fTypeModifier = type; } |
105 | |
106 | /** Appends to the layout qualifier. */ |
107 | void addLayoutQualifier(const char* layoutQualifier) { |
108 | if (!layoutQualifier || !strlen(layoutQualifier)) { |
109 | return; |
110 | } |
111 | if (fLayoutQualifier.isEmpty()) { |
112 | fLayoutQualifier = layoutQualifier; |
113 | } else { |
114 | fLayoutQualifier.appendf(", %s" , layoutQualifier); |
115 | } |
116 | } |
117 | |
118 | /** Appends to the modifiers. */ |
119 | void addModifier(const char* modifier) { |
120 | if (!modifier || !strlen(modifier)) { |
121 | return; |
122 | } |
123 | if (fExtraModifiers.isEmpty()) { |
124 | fExtraModifiers = modifier; |
125 | } else { |
126 | fExtraModifiers.appendf(" %s" , modifier); |
127 | } |
128 | } |
129 | |
130 | /** Write a declaration of this variable to out. */ |
131 | void appendDecl(const GrShaderCaps*, SkString* out) const; |
132 | |
133 | void appendArrayAccess(const char* indexName, SkString* out) const { |
134 | out->appendf("%s[%s]" , this->getName().c_str(), indexName); |
135 | } |
136 | |
137 | private: |
138 | GrSLType fType; |
139 | TypeModifier fTypeModifier; |
140 | int fCount; |
141 | |
142 | SkString fName; |
143 | SkString fLayoutQualifier; |
144 | SkString ; |
145 | }; |
146 | |
147 | #endif |
148 | |