OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 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 GrGLShaderBuilder_DEFINED | 8 #ifndef GrGLShaderBuilder_DEFINED |
9 #define GrGLShaderBuilder_DEFINED | 9 #define GrGLShaderBuilder_DEFINED |
10 | 10 |
11 #include "GrAllocator.h" | 11 #include "GrAllocator.h" |
12 #include "GrBackendEffectFactory.h" | 12 #include "GrBackendEffectFactory.h" |
13 #include "GrEffect.h" | 13 #include "GrEffect.h" |
14 #include "gl/GrGLSL.h" | 14 #include "gl/GrGLSL.h" |
15 #include "gl/GrGLUniformManager.h" | 15 #include "gl/GrGLUniformManager.h" |
16 | 16 |
| 17 #include <stdarg.h> |
| 18 |
17 class GrGLContextInfo; | 19 class GrGLContextInfo; |
18 | 20 |
19 /** | 21 /** |
20 Contains all the incremental state of a shader as it is being built,as well as
helpers to | 22 Contains all the incremental state of a shader as it is being built,as well as
helpers to |
21 manipulate that state. | 23 manipulate that state. |
22 */ | 24 */ |
23 class GrGLShaderBuilder { | 25 class GrGLShaderBuilder { |
24 public: | 26 public: |
25 /** | 27 /** |
26 * Passed to GrGLEffects to add texture reads to their shader code. | 28 * Passed to GrGLEffects to add texture reads to their shader code. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 typedef SkTArray<TextureSampler> TextureSamplerArray; | 75 typedef SkTArray<TextureSampler> TextureSamplerArray; |
74 | 76 |
75 enum ShaderType { | 77 enum ShaderType { |
76 kVertex_ShaderType = 0x1, | 78 kVertex_ShaderType = 0x1, |
77 kGeometry_ShaderType = 0x2, | 79 kGeometry_ShaderType = 0x2, |
78 kFragment_ShaderType = 0x4, | 80 kFragment_ShaderType = 0x4, |
79 }; | 81 }; |
80 | 82 |
81 GrGLShaderBuilder(const GrGLContextInfo&, GrGLUniformManager&); | 83 GrGLShaderBuilder(const GrGLContextInfo&, GrGLUniformManager&); |
82 | 84 |
| 85 /** |
| 86 * Called by GrGLEffects to add code to one of the shaders. |
| 87 */ |
| 88 void vsCodeAppendf(const char format[], ...) SK_PRINTF_LIKE(2, 3) { |
| 89 va_list args; |
| 90 va_start(args, format); |
| 91 this->codeAppendf(kVertex_ShaderType, format, args); |
| 92 va_end(args); |
| 93 } |
| 94 |
| 95 void gsCodeAppendf(const char format[], ...) SK_PRINTF_LIKE(2, 3) { |
| 96 va_list args; |
| 97 va_start(args, format); |
| 98 this->codeAppendf(kGeometry_ShaderType, format, args); |
| 99 va_end(args); |
| 100 } |
| 101 |
| 102 void fsCodeAppendf(const char format[], ...) SK_PRINTF_LIKE(2, 3) { |
| 103 va_list args; |
| 104 va_start(args, format); |
| 105 this->codeAppendf(kFragment_ShaderType, format, args); |
| 106 va_end(args); |
| 107 } |
| 108 |
| 109 void vsCodeAppend(const char* str) { this->codeAppend(kVertex_ShaderType, st
r); } |
| 110 void gsCodeAppend(const char* str) { this->codeAppend(kGeometry_ShaderType,
str); } |
| 111 void fsCodeAppend(const char* str) { this->codeAppend(kFragment_ShaderType,
str); } |
| 112 |
83 /** Appends a 2D texture sample with projection if necessary. coordType must
either be Vec2f or | 113 /** Appends a 2D texture sample with projection if necessary. coordType must
either be Vec2f or |
84 Vec3f. The latter is interpreted as projective texture coords. The vec l
ength and swizzle | 114 Vec3f. The latter is interpreted as projective texture coords. The vec l
ength and swizzle |
85 order of the result depends on the GrTextureAccess associated with the T
extureSampler. */ | 115 order of the result depends on the GrTextureAccess associated with the T
extureSampler. */ |
86 void appendTextureLookup(SkString* out, | 116 void appendTextureLookup(SkString* out, |
87 const TextureSampler&, | 117 const TextureSampler&, |
88 const char* coordName, | 118 const char* coordName, |
89 GrSLType coordType = kVec2f_GrSLType) const; | 119 GrSLType coordType = kVec2f_GrSLType) const; |
90 | 120 |
| 121 /** Version of above that appends the result to the shader code rather than
an SkString. |
| 122 Currently the shader type must be kFragment */ |
| 123 void appendTextureLookup(ShaderType, |
| 124 const TextureSampler&, |
| 125 const char* coordName, |
| 126 GrSLType coordType = kVec2f_GrSLType); |
| 127 |
| 128 |
91 /** Does the work of appendTextureLookup and modulates the result by modulat
ion. The result is | 129 /** Does the work of appendTextureLookup and modulates the result by modulat
ion. The result is |
92 always a vec4. modulation and the swizzle specified by TextureSampler mu
st both be vec4 or | 130 always a vec4. modulation and the swizzle specified by TextureSampler mu
st both be vec4 or |
93 float. If modulation is "" or NULL it this function acts as though appen
dTextureLookup were | 131 float. If modulation is "" or NULL it this function acts as though appen
dTextureLookup were |
94 called. */ | 132 called. */ |
95 void appendTextureLookupAndModulate(SkString* out, | 133 void appendTextureLookupAndModulate(ShaderType, |
96 const char* modulation, | 134 const char* modulation, |
97 const TextureSampler&, | 135 const TextureSampler&, |
98 const char* coordName, | 136 const char* coordName, |
99 GrSLType coordType = kVec2f_GrSLType) co
nst; | 137 GrSLType coordType = kVec2f_GrSLType); |
100 | 138 |
101 /** Emits a helper function outside of main(). Currently ShaderType must be | 139 /** Emits a helper function outside of main(). Currently ShaderType must be |
102 kFragment_ShaderType. */ | 140 kFragment_ShaderType. */ |
103 void emitFunction(ShaderType shader, | 141 void emitFunction(ShaderType shader, |
104 GrSLType returnType, | 142 GrSLType returnType, |
105 const char* name, | 143 const char* name, |
106 int argCnt, | 144 int argCnt, |
107 const GrGLShaderVar* args, | 145 const GrGLShaderVar* args, |
108 const char* body, | 146 const char* body, |
109 SkString* outName); | 147 SkString* outName); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 const char* fsOutColor, | 221 const char* fsOutColor, |
184 const char* vsInCoord, | 222 const char* vsInCoord, |
185 SkTArray<GrGLUniformManager::UniformHandle, true
>* samplerHandles); | 223 SkTArray<GrGLUniformManager::UniformHandle, true
>* samplerHandles); |
186 GrGLUniformManager::UniformHandle getRTHeightUniform() const { return fRTHei
ghtUniform; } | 224 GrGLUniformManager::UniformHandle getRTHeightUniform() const { return fRTHei
ghtUniform; } |
187 // TODO: Make this do all the compiling, linking, etc. | 225 // TODO: Make this do all the compiling, linking, etc. |
188 void finished(GrGLuint programID); | 226 void finished(GrGLuint programID); |
189 | 227 |
190 const GrGLContextInfo& ctxInfo() const { return fCtxInfo; } | 228 const GrGLContextInfo& ctxInfo() const { return fCtxInfo; } |
191 | 229 |
192 private: | 230 private: |
| 231 void codeAppendf(ShaderType type, const char format[], va_list args); |
| 232 void codeAppend(ShaderType type, const char* str); |
| 233 |
193 typedef GrTAllocator<GrGLShaderVar> VarArray; | 234 typedef GrTAllocator<GrGLShaderVar> VarArray; |
194 | 235 |
195 void appendDecls(const VarArray&, SkString*) const; | 236 void appendDecls(const VarArray&, SkString*) const; |
196 void appendUniformDecls(ShaderType, SkString*) const; | 237 void appendUniformDecls(ShaderType, SkString*) const; |
197 | 238 |
198 typedef GrGLUniformManager::BuilderUniform BuilderUniform; | 239 typedef GrGLUniformManager::BuilderUniform BuilderUniform; |
199 GrGLUniformManager::BuilderUniformArray fUniforms; | 240 GrGLUniformManager::BuilderUniformArray fUniforms; |
200 | 241 |
201 // TODO: Everything below here private. | 242 // TODO: Everything below here private. |
202 public: | 243 public: |
203 | 244 |
204 SkString fHeader; // VS+FS, GLSL version, etc | 245 SkString fHeader; // VS+FS, GLSL version, etc |
205 VarArray fVSAttrs; | 246 VarArray fVSAttrs; |
206 VarArray fVSOutputs; | 247 VarArray fVSOutputs; |
207 VarArray fGSInputs; | 248 VarArray fGSInputs; |
208 VarArray fGSOutputs; | 249 VarArray fGSOutputs; |
209 VarArray fFSInputs; | 250 VarArray fFSInputs; |
210 SkString fGSHeader; // layout qualifiers specific to GS | 251 SkString fGSHeader; // layout qualifiers specific to GS |
211 VarArray fFSOutputs; | 252 VarArray fFSOutputs; |
212 SkString fVSCode; | |
213 SkString fGSCode; | |
214 SkString fFSCode; | |
215 bool fUsesGS; | 253 bool fUsesGS; |
216 | 254 |
217 private: | 255 private: |
218 enum { | 256 enum { |
219 kNonStageIdx = -1, | 257 kNonStageIdx = -1, |
220 }; | 258 }; |
221 | 259 |
222 const GrGLContextInfo& fCtxInfo; | 260 const GrGLContextInfo& fCtxInfo; |
223 GrGLUniformManager& fUniformManager; | 261 GrGLUniformManager& fUniformManager; |
224 int fCurrentStageIdx; | 262 int fCurrentStageIdx; |
225 SkString fFSFunctions; | 263 SkString fFSFunctions; |
226 SkString fFSHeader; | 264 SkString fFSHeader; |
227 | 265 |
| 266 SkString fFSCode; |
| 267 SkString fVSCode; |
| 268 SkString fGSCode; |
| 269 |
228 bool fSetupFragPosition; | 270 bool fSetupFragPosition; |
229 GrGLUniformManager::UniformHandle fRTHeightUniform; | 271 GrGLUniformManager::UniformHandle fRTHeightUniform; |
230 | 272 |
231 GrGLShaderVar* fPositionVar; | 273 GrGLShaderVar* fPositionVar; |
232 }; | 274 }; |
233 | 275 |
234 #endif | 276 #endif |
OLD | NEW |