OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef GrGLSL_DEFINED | 8 #ifndef GrGLSL_DEFINED |
9 #define GrGLSL_DEFINED | 9 #define GrGLSL_DEFINED |
10 | 10 |
11 #include "gl/GrGLInterface.h" | 11 #include "gl/GrGLInterface.h" |
| 12 #include "GrColor.h" |
12 #include "GrTypesPriv.h" | 13 #include "GrTypesPriv.h" |
13 | 14 |
14 class GrGLShaderVar; | 15 class GrGLShaderVar; |
15 class SkString; | 16 class SkString; |
16 | 17 |
17 // Limited set of GLSL versions we build shaders for. Caller should round | 18 // Limited set of GLSL versions we build shaders for. Caller should round |
18 // down the GLSL version to one of these enums. | 19 // down the GLSL version to one of these enums. |
19 enum GrGLSLGeneration { | 20 enum GrGLSLGeneration { |
20 /** | 21 /** |
21 * Desktop GLSL 1.10 and ES2 shading language (based on desktop GLSL 1.20) | 22 * Desktop GLSL 1.10 and ES2 shading language (based on desktop GLSL 1.20) |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 * If the function returns false: | 97 * If the function returns false: |
97 * * Parameter var's name will be set to the GLSL built-in color output name. | 98 * * Parameter var's name will be set to the GLSL built-in color output name. |
98 * * Do not declare the variable in the shader. | 99 * * Do not declare the variable in the shader. |
99 * * Do not use glBindFragDataLocation to bind the variable | 100 * * Do not use glBindFragDataLocation to bind the variable |
100 * In either case var is initialized to represent the color output in the | 101 * In either case var is initialized to represent the color output in the |
101 * shader. | 102 * shader. |
102 */ | 103 */ |
103 bool GrGLSLSetupFSColorOuput(GrGLSLGeneration gen, | 104 bool GrGLSLSetupFSColorOuput(GrGLSLGeneration gen, |
104 const char* nameIfDeclared, | 105 const char* nameIfDeclared, |
105 GrGLShaderVar* var); | 106 GrGLShaderVar* var); |
| 107 /** |
| 108 * Converts a GrSLType to a string containing the name of the equivalent GLSL ty
pe. |
| 109 */ |
| 110 static const char* GrGLSLTypeString(GrSLType t) { |
| 111 switch (t) { |
| 112 case kVoid_GrSLType: |
| 113 return "void"; |
| 114 case kFloat_GrSLType: |
| 115 return "float"; |
| 116 case kVec2f_GrSLType: |
| 117 return "vec2"; |
| 118 case kVec3f_GrSLType: |
| 119 return "vec3"; |
| 120 case kVec4f_GrSLType: |
| 121 return "vec4"; |
| 122 case kMat33f_GrSLType: |
| 123 return "mat3"; |
| 124 case kMat44f_GrSLType: |
| 125 return "mat4"; |
| 126 case kSampler2D_GrSLType: |
| 127 return "sampler2D"; |
| 128 default: |
| 129 GrCrash("Unknown shader var type."); |
| 130 return ""; // suppress warning |
| 131 } |
| 132 } |
106 | 133 |
107 /** Convert a count of 1..n floats into the corresponding type enum, | 134 /** Return the type enum for a vector of floats of length n (1..4), |
108 e.g. 1 -> kFloat_GrSLType, 2 -> kVec2_GrSLType, ... */ | 135 e.g. 1 -> "float", 2 -> "vec2", ... */ |
109 GrSLType GrSLFloatVectorType(int count); | 136 static inline const char* GrGLSLFloatVectorTypeString(int n) { |
| 137 return GrGLSLTypeString(GrSLFloatVectorType(n)); |
| 138 } |
110 | 139 |
111 /** Return the GLSL swizzle operator for a homogenous component of a vector | 140 /** Return the GLSL swizzle operator for a homogenous component of a vector |
112 with the given number of coordinates, e.g. 2 -> ".y", 3 -> ".z" */ | 141 with the given number of coordinates, e.g. 2 -> ".y", 3 -> ".z" */ |
113 const char* GrGLSLVectorHomogCoord(int count); | 142 const char* GrGLSLVectorHomogCoord(int count); |
114 const char* GrGLSLVectorHomogCoord(GrSLType type); | 143 const char* GrGLSLVectorHomogCoord(GrSLType type); |
115 | 144 |
116 /** Return the GLSL swizzle operator for a nonhomogenous components of a vector | 145 /** Return the GLSL swizzle operator for a nonhomogenous components of a vector |
117 with the given number of coordinates, e.g. 2 -> ".x", 3 -> ".xy" */ | 146 with the given number of coordinates, e.g. 2 -> ".x", 3 -> ".xy" */ |
118 const char* GrGLSLVectorNonhomogCoords(int count); | 147 const char* GrGLSLVectorNonhomogCoords(int count); |
119 const char* GrGLSLVectorNonhomogCoords(GrSLType type); | 148 const char* GrGLSLVectorNonhomogCoords(GrSLType type); |
120 | 149 |
121 /** | 150 /** |
122 * Produces a string that is the result of modulating two inputs. The inputs mu
st be vec4 or | 151 * Produces a string that is the result of modulating two inputs. The inputs mu
st be vecN or |
123 * float. The result is always a vec4. The inputs may be expressions, not just
identifier names. | 152 * float. The result is always a vecN. The inputs may be expressions, not just
identifier names. |
124 * Either can be NULL or "" in which case the default params control whether ve
c4(1,1,1,1) or | 153 * Either can be NULL or "" in which case the default params control whether a
vector of ones or |
125 * vec4(0,0,0,0) is assumed. It is an error to pass kNone for default<i> if in<
i> is NULL or "". | 154 * zeros. It is an error to pass kNone for default<i> if in<i> is NULL or "". N
ote that when if |
126 * Note that when if function determines that the result is a zeros or ones vec
then any expression | 155 * function determines that the result is a zeros or ones vec then any expressi
on represented by |
127 * represented by in0 or in1 will not be emitted. The return value indicates wh
ether a zeros, ones | 156 * or in1 will not be emitted (side effects won't occur). The return value indi
cates whether a |
128 * or neither was appended. | 157 * known zeros or ones vector resulted. The output can be supressed when known
vector is produced |
| 158 * by passing true for omitIfConstVec. |
129 */ | 159 */ |
130 GrSLConstantVec GrGLSLModulate4f(SkString* outAppend, | 160 template <int N> |
131 const char* in0, | 161 GrSLConstantVec GrGLSLModulatef(SkString* outAppend, |
132 const char* in1, | 162 const char* in0, |
133 GrSLConstantVec default0 = kOnes_GrSLConstantVe
c, | 163 const char* in1, |
134 GrSLConstantVec default1 = kOnes_GrSLConstantVe
c); | 164 GrSLConstantVec default0 = kOnes_GrSLConstantVec
, |
| 165 GrSLConstantVec default1 = kOnes_GrSLConstantVec
, |
| 166 bool omitIfConstVec = false); |
| 167 |
| 168 /** |
| 169 * Produces a string that is the result of adding two inputs. The inputs must be
vecN or |
| 170 * float. The result is always a vecN. The inputs may be expressions, not just i
dentifier names. |
| 171 * Either can be NULL or "" in which case the default params control whether a v
ector of ones or |
| 172 * zeros. It is an error to pass kNone for default<i> if in<i> is NULL or "". No
te that when if |
| 173 * function determines that the result is a zeros or ones vec then any expressio
n represented by |
| 174 * or in1 will not be emitted (side effects won't occur). The return value indic
ates whether a |
| 175 * known zeros or ones vector resulted. The output can be supressed when known v
ector is produced |
| 176 * by passing true for omitIfConstVec. |
| 177 */ |
| 178 template <int N> |
| 179 GrSLConstantVec GrGLSLAddf(SkString* outAppend, |
| 180 const char* in0, |
| 181 const char* in1, |
| 182 GrSLConstantVec default0 = kZeros_GrSLConstantVec, |
| 183 GrSLConstantVec default1 = kZeros_GrSLConstantVec, |
| 184 bool omitIfConstVec = false); |
| 185 |
| 186 /** |
| 187 * Produces a string that is the result of adding two inputs. The inputs must be
vecN or |
| 188 * float. The result is always a vecN. The inputs may be expressions, not just i
dentifier names. |
| 189 * Either can be NULL or "" in which case the default params control whether a v
ector of ones or |
| 190 * zeros. It is an error to pass kNone for default<i> if in<i> is NULL or "". No
te that when if |
| 191 * function determines that the result is a zeros or ones vec then any expressio
n represented by |
| 192 * or in1 will not be emitted (side effects won't occur). The return value indic
ates whether a |
| 193 * known zeros or ones vector resulted. The output can be supressed when known v
ector is produced |
| 194 * by passing true for omitIfConstVec. |
| 195 */ |
| 196 template <int N> |
| 197 GrSLConstantVec GrGLSLSubtractf(SkString* outAppend, |
| 198 const char* in0, |
| 199 const char* in1, |
| 200 GrSLConstantVec default0 = kZeros_GrSLConstantVe
c, |
| 201 GrSLConstantVec default1 = kZeros_GrSLConstantVe
c, |
| 202 bool omitIfConstVec = false); |
135 | 203 |
136 /** | 204 /** |
137 * Does an inplace mul, *=, of vec4VarName by mulFactor. If mulFactorDefault is
not kNone then | 205 * Does an inplace mul, *=, of vec4VarName by mulFactor. If mulFactorDefault is
not kNone then |
138 * mulFactor may be either "" or NULL. In this case either nothing will be appen
ded (kOnes) or an | 206 * mulFactor may be either "" or NULL. In this case either nothing will be appen
ded (kOnes) or an |
139 * assignment of vec(0,0,0,0) will be appended (kZeros). The assignment is prepe
nded by tabCnt tabs. | 207 * assignment of vec(0,0,0,0) will be appended (kZeros). The assignment is prepe
nded by tabCnt tabs. |
140 * A semicolon and newline are added after the assignment. (TODO: Remove tabCnt
when we auto-insert | 208 * A semicolon and newline are added after the assignment. (TODO: Remove tabCnt
when we auto-insert |
141 * tabs to GrGLEffect-generated lines.) If a zeros vec is assigned then the retu
rn value is | 209 * tabs to GrGLEffect-generated lines.) If a zeros vec is assigned then the retu
rn value is |
142 * kZeros, otherwise kNone. | 210 * kZeros, otherwise kNone. |
143 */ | 211 */ |
144 GrSLConstantVec GrGLSLMulVarBy4f(SkString* outAppend, | 212 GrSLConstantVec GrGLSLMulVarBy4f(SkString* outAppend, |
145 int tabCnt, | 213 int tabCnt, |
146 const char* vec4VarName, | 214 const char* vec4VarName, |
147 const char* mulFactor, | 215 const char* mulFactor, |
148 GrSLConstantVec mulFactorDefault = kOnes_GrSLCo
nstantVec); | 216 GrSLConstantVec mulFactorDefault = kOnes_GrSLCo
nstantVec); |
149 | 217 |
150 /** | 218 /** |
151 * Produces a string that is the result of adding two inputs. The inputs must b
e vec4 or float. | 219 * Given an expression that evaluates to a GLSL vec4, extract a component. If |
152 * The result is always a vec4. The inputs may be expressions, not just identif
ier names. Either | 220 */ |
153 * can be NULL or "" in which case if the default is kZeros then vec4(0,0,0,0)
is assumed. It is an | 221 GrSLConstantVec GrGLSLGetComponent4f(SkString* outAppend, |
154 * error to pass kOnes for either default or to pass kNone for default<i> if in
<i> is NULL or "". | 222 const char* expr, |
155 * Note that if the function determines that the result is a zeros vec any expr
ession represented | 223 GrColorComponentFlags component, |
156 * by in0 or in1 will not be emitted. The return value indicates whether a zero
s vec was appended | 224 GrSLConstantVec defaultExpr = kNone_GrSLCon
stantVec, |
157 * or not. | 225 bool omitIfConst = false); |
158 */ | 226 |
159 GrSLConstantVec GrGLSLAdd4f(SkString* outAppend, | 227 #include "GrGLSL_impl.h" |
160 const char* in0, | |
161 const char* in1, | |
162 GrSLConstantVec default0 = kZeros_GrSLConstantVec, | |
163 GrSLConstantVec default1 = kZeros_GrSLConstantVec); | |
164 | 228 |
165 #endif | 229 #endif |
OLD | NEW |