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 "GrColor.h" | 13 #include "GrColor.h" |
14 #include "GrEffect.h" | 14 #include "GrEffect.h" |
15 #include "SkTypes.h" | 15 #include "SkTypes.h" |
16 #include "gl/GrGLProgramDesc.h" | 16 #include "gl/GrGLProgramDesc.h" |
17 #include "gl/GrGLProgramEffects.h" | 17 #include "gl/GrGLProgramEffects.h" |
18 #include "gl/GrGLSL.h" | 18 #include "gl/GrGLSL.h" |
19 #include "gl/GrGLUniformManager.h" | 19 #include "gl/GrGLProgramDataManager.h" |
20 | 20 |
21 #include <stdarg.h> | 21 #include <stdarg.h> |
22 | 22 |
23 class GrGLContextInfo; | 23 class GrGLContextInfo; |
24 class GrEffectStage; | 24 class GrEffectStage; |
25 class GrGLProgramDesc; | 25 class GrGLProgramDesc; |
26 | 26 |
27 /** | 27 /** |
28 Contains all the incremental state of a shader as it is being built,as well as
helpers to | 28 Contains all the incremental state of a shader as it is being built,as well as
helpers to |
29 manipulate that state. | 29 manipulate that state. |
30 */ | 30 */ |
31 class GrGLShaderBuilder { | 31 class GrGLShaderBuilder { |
32 public: | 32 public: |
33 typedef GrTAllocator<GrGLShaderVar> VarArray; | 33 typedef GrTAllocator<GrGLShaderVar> VarArray; |
34 typedef GrGLProgramEffects::TextureSampler TextureSampler; | 34 typedef GrGLProgramEffects::TextureSampler TextureSampler; |
35 typedef GrGLProgramEffects::TransformedCoordsArray TransformedCoordsArray; | 35 typedef GrGLProgramEffects::TransformedCoordsArray TransformedCoordsArray; |
36 typedef GrGLUniformManager::BuilderUniform BuilderUniform; | 36 typedef GrGLProgramDataManager::BuilderUniform BuilderUniform; |
37 | 37 |
38 enum ShaderVisibility { | 38 enum ShaderVisibility { |
39 kVertex_Visibility = 0x1, | 39 kVertex_Visibility = 0x1, |
40 kGeometry_Visibility = 0x2, | 40 kGeometry_Visibility = 0x2, |
41 kFragment_Visibility = 0x4, | 41 kFragment_Visibility = 0x4, |
42 }; | 42 }; |
43 | 43 |
44 typedef GrGLUniformManager::UniformHandle UniformHandle; | 44 typedef GrGLProgramDataManager::UniformHandle UniformHandle; |
45 | 45 |
46 // Handles for program uniforms (other than per-effect uniforms) | 46 // Handles for program uniforms (other than per-effect uniforms) |
47 struct UniformHandles { | 47 struct UniformHandles { |
48 UniformHandle fViewMatrixUni; | 48 UniformHandle fViewMatrixUni; |
49 UniformHandle fRTAdjustmentUni; | 49 UniformHandle fRTAdjustmentUni; |
50 UniformHandle fColorUni; | 50 UniformHandle fColorUni; |
51 UniformHandle fCoverageUni; | 51 UniformHandle fCoverageUni; |
52 | 52 |
53 // We use the render target height to provide a y-down frag coord when s
pecifying | 53 // We use the render target height to provide a y-down frag coord when s
pecifying |
54 // origin_upper_left is not supported. | 54 // origin_upper_left is not supported. |
(...skipping 29 matching lines...) Expand all Loading... |
84 | 84 |
85 SkAutoTUnref<GrGLProgramEffects> fColorEffects; | 85 SkAutoTUnref<GrGLProgramEffects> fColorEffects; |
86 SkAutoTUnref<GrGLProgramEffects> fCoverageEffects; | 86 SkAutoTUnref<GrGLProgramEffects> fCoverageEffects; |
87 UniformHandles fUniformHandles; | 87 UniformHandles fUniformHandles; |
88 bool fHasVertexShader; | 88 bool fHasVertexShader; |
89 int fTexCoordSetCnt; | 89 int fTexCoordSetCnt; |
90 GrGLuint fProgramID; | 90 GrGLuint fProgramID; |
91 }; | 91 }; |
92 | 92 |
93 static bool GenProgram(GrGpuGL* gpu, | 93 static bool GenProgram(GrGpuGL* gpu, |
94 GrGLUniformManager* uman, | 94 GrGLProgramDataManager* pdman, |
95 const GrGLProgramDesc& desc, | 95 const GrGLProgramDesc& desc, |
96 const GrEffectStage* inColorStages[], | 96 const GrEffectStage* inColorStages[], |
97 const GrEffectStage* inCoverageStages[], | 97 const GrEffectStage* inCoverageStages[], |
98 GenProgramOutput* output); | 98 GenProgramOutput* output); |
99 | 99 |
100 virtual ~GrGLShaderBuilder() {} | 100 virtual ~GrGLShaderBuilder() {} |
101 | 101 |
102 /** | 102 /** |
103 * Use of these features may require a GLSL extension to be enabled. Shaders
may not compile | 103 * Use of these features may require a GLSL extension to be enabled. Shaders
may not compile |
104 * if code is added that uses one of these features without calling enableFe
ature() | 104 * if code is added that uses one of these features without calling enableFe
ature() |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 /** If texture swizzling is available using tex parameters then it is prefer
red over mangling | 174 /** If texture swizzling is available using tex parameters then it is prefer
red over mangling |
175 the generated shader code. This potentially allows greater reuse of cach
ed shaders. */ | 175 the generated shader code. This potentially allows greater reuse of cach
ed shaders. */ |
176 static const GrGLenum* GetTexParamSwizzle(GrPixelConfig config, const GrGLCa
ps& caps); | 176 static const GrGLenum* GetTexParamSwizzle(GrPixelConfig config, const GrGLCa
ps& caps); |
177 | 177 |
178 /** Add a uniform variable to the current program, that has visibility in on
e or more shaders. | 178 /** Add a uniform variable to the current program, that has visibility in on
e or more shaders. |
179 visibility is a bitfield of ShaderVisibility values indicating from whic
h shaders the | 179 visibility is a bitfield of ShaderVisibility values indicating from whic
h shaders the |
180 uniform should be accessible. At least one bit must be set. Geometry sha
der uniforms are not | 180 uniform should be accessible. At least one bit must be set. Geometry sha
der uniforms are not |
181 supported at this time. The actual uniform name will be mangled. If outN
ame is not NULL then | 181 supported at this time. The actual uniform name will be mangled. If outN
ame is not NULL then |
182 it will refer to the final uniform name after return. Use the addUniform
Array variant to add | 182 it will refer to the final uniform name after return. Use the addUniform
Array variant to add |
183 an array of uniforms. */ | 183 an array of uniforms. */ |
184 GrGLUniformManager::UniformHandle addUniform(uint32_t visibility, | 184 GrGLProgramDataManager::UniformHandle addUniform(uint32_t visibility, |
185 GrSLType type, | 185 GrSLType type, |
186 const char* name, | 186 const char* name, |
187 const char** outName = NULL) { | 187 const char** outName = NULL
) { |
188 return this->addUniformArray(visibility, type, name, GrGLShaderVar::kNon
Array, outName); | 188 return this->addUniformArray(visibility, type, name, GrGLShaderVar::kNon
Array, outName); |
189 } | 189 } |
190 GrGLUniformManager::UniformHandle addUniformArray(uint32_t visibility, | 190 GrGLProgramDataManager::UniformHandle addUniformArray(uint32_t visibility, |
191 GrSLType type, | 191 GrSLType type, |
192 const char* name, | 192 const char* name, |
193 int arrayCount, | 193 int arrayCount, |
194 const char** outName = NUL
L); | 194 const char** outName =
NULL); |
195 | 195 |
196 const GrGLShaderVar& getUniformVariable(GrGLUniformManager::UniformHandle u)
const { | 196 const GrGLShaderVar& getUniformVariable(GrGLProgramDataManager::UniformHandl
e u) const { |
197 return fUniformManager->getBuilderUniform(fUniforms, u).fVariable; | 197 return fProgramDataManager->getBuilderUniform(fUniforms, u).fVariable; |
198 } | 198 } |
199 | 199 |
200 /** | 200 /** |
201 * Shortcut for getUniformVariable(u).c_str() | 201 * Shortcut for getUniformVariable(u).c_str() |
202 */ | 202 */ |
203 const char* getUniformCStr(GrGLUniformManager::UniformHandle u) const { | 203 const char* getUniformCStr(GrGLProgramDataManager::UniformHandle u) const { |
204 return this->getUniformVariable(u).c_str(); | 204 return this->getUniformVariable(u).c_str(); |
205 } | 205 } |
206 | 206 |
207 /** | 207 /** |
208 * This returns a variable name to access the 2D, perspective correct versio
n of the coords in | 208 * This returns a variable name to access the 2D, perspective correct versio
n of the coords in |
209 * the fragment shader. If the coordinates at index are 3-dimensional, it im
mediately emits a | 209 * the fragment shader. If the coordinates at index are 3-dimensional, it im
mediately emits a |
210 * perspective divide into the fragment shader (xy / z) to convert them to 2
D. | 210 * perspective divide into the fragment shader (xy / z) to convert them to 2
D. |
211 */ | 211 */ |
212 SkString ensureFSCoords2D(const TransformedCoordsArray&, int index); | 212 SkString ensureFSCoords2D(const TransformedCoordsArray&, int index); |
213 | 213 |
(...skipping 20 matching lines...) Expand all Loading... |
234 } | 234 } |
235 | 235 |
236 ~FSBlock() { | 236 ~FSBlock() { |
237 fBuilder->fsCodeAppend("\t}\n"); | 237 fBuilder->fsCodeAppend("\t}\n"); |
238 } | 238 } |
239 private: | 239 private: |
240 GrGLShaderBuilder* fBuilder; | 240 GrGLShaderBuilder* fBuilder; |
241 }; | 241 }; |
242 | 242 |
243 protected: | 243 protected: |
244 GrGLShaderBuilder(GrGpuGL*, GrGLUniformManager*, const GrGLProgramDesc&); | 244 GrGLShaderBuilder(GrGpuGL*, GrGLProgramDataManager*, const GrGLProgramDesc&)
; |
245 | 245 |
246 GrGpuGL* gpu() const { return fGpu; } | 246 GrGpuGL* gpu() const { return fGpu; } |
247 | 247 |
248 const GrGLProgramDesc& desc() const { return fDesc; } | 248 const GrGLProgramDesc& desc() const { return fDesc; } |
249 | 249 |
250 /** Add input/output variable declarations (i.e. 'varying') to the fragment
shader. */ | 250 /** Add input/output variable declarations (i.e. 'varying') to the fragment
shader. */ |
251 GrGLShaderVar& fsInputAppend() { return fFSInputs.push_back(); } | 251 GrGLShaderVar& fsInputAppend() { return fFSInputs.push_back(); } |
252 | 252 |
253 // Helper for emitEffects(). | 253 // Helper for emitEffects(). |
254 void createAndEmitEffects(GrGLProgramEffectsBuilder*, | 254 void createAndEmitEffects(GrGLProgramEffectsBuilder*, |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 }; | 389 }; |
390 | 390 |
391 enum { | 391 enum { |
392 kNoFragPosRead_FragPosKey = 0, // The fragment positition wil
l not be needed. | 392 kNoFragPosRead_FragPosKey = 0, // The fragment positition wil
l not be needed. |
393 kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to t
op-left. | 393 kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to t
op-left. |
394 kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to b
ottom-left. | 394 kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to b
ottom-left. |
395 }; | 395 }; |
396 | 396 |
397 const GrGLProgramDesc& fDesc; | 397 const GrGLProgramDesc& fDesc; |
398 GrGpuGL* fGpu; | 398 GrGpuGL* fGpu; |
399 SkAutoTUnref<GrGLUniformManager> fUniformManager; | 399 SkAutoTUnref<GrGLProgramDataManager> fProgramDataManager; |
400 uint32_t fFSFeaturesAddedMask; | 400 uint32_t fFSFeaturesAddedMask; |
401 SkString fFSFunctions; | 401 SkString fFSFunctions; |
402 SkString fFSExtensions; | 402 SkString fFSExtensions; |
403 VarArray fFSInputs; | 403 VarArray fFSInputs; |
404 VarArray fFSOutputs; | 404 VarArray fFSOutputs; |
405 GrGLUniformManager::BuilderUniformArray fUniforms; | 405 GrGLProgramDataManager::BuilderUniformArray fUniforms; |
406 | 406 |
407 SkString fFSCode; | 407 SkString fFSCode; |
408 | 408 |
409 bool fSetupFragPosition; | 409 bool fSetupFragPosition; |
410 bool fTopLeftFragPosRead; | 410 bool fTopLeftFragPosRead; |
411 | 411 |
412 bool fHasCustomColorOutput; | 412 bool fHasCustomColorOutput; |
413 bool fHasSecondaryOutput; | 413 bool fHasSecondaryOutput; |
414 }; | 414 }; |
415 | 415 |
416 //////////////////////////////////////////////////////////////////////////////// | 416 //////////////////////////////////////////////////////////////////////////////// |
417 | 417 |
418 class GrGLFullShaderBuilder : public GrGLShaderBuilder { | 418 class GrGLFullShaderBuilder : public GrGLShaderBuilder { |
419 public: | 419 public: |
420 GrGLFullShaderBuilder(GrGpuGL*, GrGLUniformManager*, const GrGLProgramDesc&)
; | 420 GrGLFullShaderBuilder(GrGpuGL*, GrGLProgramDataManager*, const GrGLProgramDe
sc&); |
421 | 421 |
422 /** | 422 /** |
423 * Called by GrGLEffects to add code to one of the shaders. | 423 * Called by GrGLEffects to add code to one of the shaders. |
424 */ | 424 */ |
425 void vsCodeAppendf(const char format[], ...) SK_PRINTF_LIKE(2, 3) { | 425 void vsCodeAppendf(const char format[], ...) SK_PRINTF_LIKE(2, 3) { |
426 va_list args; | 426 va_list args; |
427 va_start(args, format); | 427 va_start(args, format); |
428 fVSCode.appendVAList(format, args); | 428 fVSCode.appendVAList(format, args); |
429 va_end(args); | 429 va_end(args); |
430 } | 430 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
495 GrGLShaderVar* fPositionVar; | 495 GrGLShaderVar* fPositionVar; |
496 GrGLShaderVar* fLocalCoordsVar; | 496 GrGLShaderVar* fLocalCoordsVar; |
497 | 497 |
498 typedef GrGLShaderBuilder INHERITED; | 498 typedef GrGLShaderBuilder INHERITED; |
499 }; | 499 }; |
500 | 500 |
501 //////////////////////////////////////////////////////////////////////////////// | 501 //////////////////////////////////////////////////////////////////////////////// |
502 | 502 |
503 class GrGLFragmentOnlyShaderBuilder : public GrGLShaderBuilder { | 503 class GrGLFragmentOnlyShaderBuilder : public GrGLShaderBuilder { |
504 public: | 504 public: |
505 GrGLFragmentOnlyShaderBuilder(GrGpuGL*, GrGLUniformManager*, const GrGLProgr
amDesc&); | 505 GrGLFragmentOnlyShaderBuilder(GrGpuGL*, GrGLProgramDataManager*, const GrGLP
rogramDesc&); |
506 | 506 |
507 int addTexCoordSets(int count); | 507 int addTexCoordSets(int count); |
508 | 508 |
509 private: | 509 private: |
510 virtual void emitCodeBeforeEffects(GrGLSLExpr4* color, GrGLSLExpr4* coverage
) SK_OVERRIDE {} | 510 virtual void emitCodeBeforeEffects(GrGLSLExpr4* color, GrGLSLExpr4* coverage
) SK_OVERRIDE {} |
511 | 511 |
512 virtual GrGLProgramEffects* createAndEmitEffects(const GrEffectStage* effect
Stages[], | 512 virtual GrGLProgramEffects* createAndEmitEffects(const GrEffectStage* effect
Stages[], |
513 int effectCnt, | 513 int effectCnt, |
514 const GrGLProgramDesc::Effe
ctKeyProvider&, | 514 const GrGLProgramDesc::Effe
ctKeyProvider&, |
515 GrGLSLExpr4* inOutFSColor)
SK_OVERRIDE; | 515 GrGLSLExpr4* inOutFSColor)
SK_OVERRIDE; |
516 | 516 |
517 virtual void emitCodeAfterEffects() SK_OVERRIDE {} | 517 virtual void emitCodeAfterEffects() SK_OVERRIDE {} |
518 | 518 |
519 typedef GrGLShaderBuilder INHERITED; | 519 typedef GrGLShaderBuilder INHERITED; |
520 }; | 520 }; |
521 | 521 |
522 #endif | 522 #endif |
OLD | NEW |