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/GrGLCoordTransform.h" | 16 #include "gl/GrGLEffectArray.h" |
17 #include "gl/GrGLSL.h" | 17 #include "gl/GrGLSL.h" |
18 #include "gl/GrGLUniformManager.h" | 18 #include "gl/GrGLUniformManager.h" |
19 | 19 |
20 #include <stdarg.h> | 20 #include <stdarg.h> |
21 | 21 |
22 class GrGLContextInfo; | 22 class GrGLContextInfo; |
23 class GrEffectStage; | 23 class GrEffectStage; |
24 class GrGLProgramDesc; | 24 class GrGLProgramDesc; |
25 | 25 |
26 /** | 26 /** |
27 Contains all the incremental state of a shader as it is being built,as well as helpers to | 27 Contains all the incremental state of a shader as it is being built,as well as helpers to |
28 manipulate that state. | 28 manipulate that state. |
29 */ | 29 */ |
30 class GrGLShaderBuilder { | 30 class GrGLShaderBuilder { |
31 public: | 31 public: |
32 /** | |
33 * Passed to GrGLEffects to add texture reads to their shader code. | |
34 */ | |
35 class TextureSampler { | |
36 public: | |
37 TextureSampler() | |
38 : fConfigComponentMask(0) { | |
39 // we will memcpy the first 4 bytes from passed in swizzle. This ens ures the string is | |
40 // terminated. | |
41 fSwizzle[4] = '\0'; | |
42 } | |
43 | |
44 TextureSampler(const TextureSampler& other) { *this = other; } | |
45 | |
46 TextureSampler& operator= (const TextureSampler& other) { | |
47 SkASSERT(0 == fConfigComponentMask); | |
48 SkASSERT(!fSamplerUniform.isValid()); | |
49 | |
50 fConfigComponentMask = other.fConfigComponentMask; | |
51 fSamplerUniform = other.fSamplerUniform; | |
52 return *this; | |
53 } | |
54 | |
55 // bitfield of GrColorComponentFlags present in the texture's config. | |
56 uint32_t configComponentMask() const { return fConfigComponentMask; } | |
57 | |
58 const char* swizzle() const { return fSwizzle; } | |
59 | |
60 bool isInitialized() const { return 0 != fConfigComponentMask; } | |
61 | |
62 private: | |
63 // The idx param is used to ensure multiple samplers within a single eff ect have unique | |
64 // uniform names. swizzle is a four char max string made up of chars 'r' , 'g', 'b', and 'a'. | |
65 void init(GrGLShaderBuilder* builder, | |
66 uint32_t configComponentMask, | |
67 const char* swizzle, | |
68 int idx) { | |
69 SkASSERT(!this->isInitialized()); | |
70 SkASSERT(0 != configComponentMask); | |
71 SkASSERT(!fSamplerUniform.isValid()); | |
72 | |
73 SkASSERT(NULL != builder); | |
74 SkString name; | |
75 name.printf("Sampler%d", idx); | |
76 fSamplerUniform = builder->addUniform(GrGLShaderBuilder::kFragment_V isibility, | |
77 kSampler2D_GrSLType, | |
78 name.c_str()); | |
79 SkASSERT(fSamplerUniform.isValid()); | |
80 | |
81 fConfigComponentMask = configComponentMask; | |
82 memcpy(fSwizzle, swizzle, 4); | |
83 } | |
84 | |
85 void init(GrGLShaderBuilder* builder, const GrTextureAccess* access, int idx) { | |
86 SkASSERT(NULL != access); | |
87 this->init(builder, | |
88 GrPixelConfigComponentMask(access->getTexture()->config() ), | |
89 access->getSwizzle(), | |
90 idx); | |
91 } | |
92 | |
93 uint32_t fConfigComponentMask; | |
94 char fSwizzle[5]; | |
95 GrGLUniformManager::UniformHandle fSamplerUniform; | |
96 | |
97 friend class GrGLShaderBuilder; // to call init(). | |
98 }; | |
99 | |
100 typedef SkTArray<GrGLCoordTransform::TransformedCoords> TransformedCoordsArr ay; | |
101 typedef SkTArray<TextureSampler> TextureSamplerArray; | |
102 typedef GrTAllocator<GrGLShaderVar> VarArray; | 32 typedef GrTAllocator<GrGLShaderVar> VarArray; |
103 typedef GrBackendEffectFactory::EffectKey EffectKey; | 33 typedef GrBackendEffectFactory::EffectKey EffectKey; |
34 typedef GrGLEffectArray::TextureSampler TextureSampler; | |
35 typedef GrGLEffectArray::TransformedCoordsArray TransformedCoordsArray; | |
104 | 36 |
105 enum ShaderVisibility { | 37 enum ShaderVisibility { |
106 kVertex_Visibility = 0x1, | 38 kVertex_Visibility = 0x1, |
107 kGeometry_Visibility = 0x2, | 39 kGeometry_Visibility = 0x2, |
108 kFragment_Visibility = 0x4, | 40 kFragment_Visibility = 0x4, |
109 }; | 41 }; |
110 | 42 |
111 GrGLShaderBuilder(GrGpuGL*, | 43 GrGLShaderBuilder(GrGpuGL*, |
112 GrGLUniformManager&, | 44 GrGLUniformManager&, |
113 const GrGLProgramDesc&, | 45 const GrGLProgramDesc&, |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
168 void fsEmitFunction(GrSLType returnType, | 100 void fsEmitFunction(GrSLType returnType, |
169 const char* name, | 101 const char* name, |
170 int argCnt, | 102 int argCnt, |
171 const GrGLShaderVar* args, | 103 const GrGLShaderVar* args, |
172 const char* body, | 104 const char* body, |
173 SkString* outName); | 105 SkString* outName); |
174 | 106 |
175 /** Add input/output variable declarations (i.e. 'varying') to the fragment shader. */ | 107 /** Add input/output variable declarations (i.e. 'varying') to the fragment shader. */ |
176 GrGLShaderVar& fsInputAppend() { return fFSInputs.push_back(); } | 108 GrGLShaderVar& fsInputAppend() { return fFSInputs.push_back(); } |
177 | 109 |
178 /** Generates a EffectKey for the shader code based on the texture access pa rameters and the | |
179 capabilities of the GL context. This is useful for keying the shader pr ograms that may | |
180 have multiple representations, based on the type/format of textures used . */ | |
181 static EffectKey KeyForTextureAccess(const GrTextureAccess&, const GrGLCaps& ); | |
182 | |
183 typedef uint8_t DstReadKey; | 110 typedef uint8_t DstReadKey; |
184 typedef uint8_t FragPosKey; | 111 typedef uint8_t FragPosKey; |
185 | 112 |
186 /** Returns a key for adding code to read the copy-of-dst color in service of effects that | 113 /** Returns a key for adding code to read the copy-of-dst color in service of effects that |
187 require reading the dst. It must not return 0 because 0 indicates that there is no dst | 114 require reading the dst. It must not return 0 because 0 indicates that there is no dst |
188 copy read at all (in which case this function should not be called). */ | 115 copy read at all (in which case this function should not be called). */ |
189 static DstReadKey KeyForDstRead(const GrTexture* dstCopy, const GrGLCaps&); | 116 static DstReadKey KeyForDstRead(const GrTexture* dstCopy, const GrGLCaps&); |
190 | 117 |
191 /** Returns a key for reading the fragment location. This should only be cal led if there is an | 118 /** Returns a key for reading the fragment location. This should only be cal led if there is an |
192 effect that will requires the fragment position. If the fragment positio n is not required, | 119 effect that will requires the fragment position. If the fragment positio n is not required, |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
256 * Adds code for effects. effectStages contains the effects to add. effectKe ys[i] is the key | 183 * Adds code for effects. effectStages contains the effects to add. effectKe ys[i] is the key |
257 * generated from effectStages[i]. An entry in effectStages can be NULL, in which case it is | 184 * generated from effectStages[i]. An entry in effectStages can be NULL, in which case it is |
258 * skipped. Moreover, if the corresponding key is GrGLEffect::NoEffectKey th en it is skipped. | 185 * skipped. Moreover, if the corresponding key is GrGLEffect::NoEffectKey th en it is skipped. |
259 * inOutFSColor specifies the input color to the first stage and is updated to be the | 186 * inOutFSColor specifies the input color to the first stage and is updated to be the |
260 * output color of the last stage. fsInOutColorKnownValue specifies whether the input color | 187 * output color of the last stage. fsInOutColorKnownValue specifies whether the input color |
261 * has a known constant value and is updated to refer to the status of the o utput color. | 188 * has a known constant value and is updated to refer to the status of the o utput color. |
262 * The handles to texture samplers for effectStage[i] are added to effectSam plerHandles[i]. The | 189 * The handles to texture samplers for effectStage[i] are added to effectSam plerHandles[i]. The |
263 * glEffects array is updated to contain the GrGLEffect generated for each e ntry in | 190 * glEffects array is updated to contain the GrGLEffect generated for each e ntry in |
264 * effectStages. | 191 * effectStages. |
265 */ | 192 */ |
266 void emitEffects(const GrEffectStage* effectStages[], | 193 GrGLEffectArray* emitEffects(const GrEffectStage* effectStages[], |
bsalomon
2013/10/02 20:17:06
emitEffectsAndCreateFoo()?
Can we update the comm
Chris Dalton
2013/10/02 21:25:43
Done.
| |
267 const EffectKey effectKeys[], | 194 const EffectKey effectKeys[], |
268 int effectCnt, | 195 int effectCnt, |
269 SkString* inOutFSColor, | 196 SkString* inOutFSColor, |
270 GrSLConstantVec* fsInOutColorKnownValue, | 197 GrSLConstantVec* fsInOutColorKnownValue); |
271 SkTArray<GrGLCoordTransform, false>* effectCoordTransformAr rays[], | |
272 SkTArray<GrGLUniformManager::UniformHandle, true>* effectSa mplerHandles[], | |
273 GrGLEffect* glEffects[]); | |
274 | 198 |
275 const char* getColorOutputName() const; | 199 const char* getColorOutputName() const; |
276 const char* enableSecondaryOutput(); | 200 const char* enableSecondaryOutput(); |
277 | 201 |
278 GrGLUniformManager::UniformHandle getRTHeightUniform() const { return fRTHei ghtUniform; } | 202 GrGLUniformManager::UniformHandle getRTHeightUniform() const { return fRTHei ghtUniform; } |
279 GrGLUniformManager::UniformHandle getDstCopyTopLeftUniform() const { | 203 GrGLUniformManager::UniformHandle getDstCopyTopLeftUniform() const { |
280 return fDstCopyTopLeftUniform; | 204 return fDstCopyTopLeftUniform; |
281 } | 205 } |
282 GrGLUniformManager::UniformHandle getDstCopyScaleUniform() const { | 206 GrGLUniformManager::UniformHandle getDstCopyScaleUniform() const { |
283 return fDstCopyScaleUniform; | 207 return fDstCopyScaleUniform; |
284 } | 208 } |
285 GrGLUniformManager::UniformHandle getColorUniform() const { return fColorUni form; } | 209 GrGLUniformManager::UniformHandle getColorUniform() const { return fColorUni form; } |
286 GrGLUniformManager::UniformHandle getCoverageUniform() const { return fCover ageUniform; } | 210 GrGLUniformManager::UniformHandle getCoverageUniform() const { return fCover ageUniform; } |
287 GrGLUniformManager::UniformHandle getDstCopySamplerUniform() const { | 211 GrGLUniformManager::UniformHandle getDstCopySamplerUniform() const { |
288 return fDstCopySampler.fSamplerUniform; | 212 return fDstCopySamplerUniform; |
289 } | 213 } |
290 | 214 |
291 /** Helper class used to build the vertex and geometry shaders. This functio nality | 215 /** Helper class used to build the vertex and geometry shaders. This functio nality |
292 is kept separate from the rest of GrGLShaderBuilder to allow for shaders programs | 216 is kept separate from the rest of GrGLShaderBuilder to allow for shaders programs |
293 that only use the fragment shader. */ | 217 that only use the fragment shader. */ |
294 class VertexBuilder { | 218 class VertexBuilder { |
295 public: | 219 public: |
296 VertexBuilder(GrGLShaderBuilder* parent, GrGpuGL* gpu, const GrGLProgram Desc&); | 220 VertexBuilder(GrGLShaderBuilder* parent, GrGpuGL* gpu, const GrGLProgram Desc&); |
297 | 221 |
298 /** | 222 /** |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
477 GrGLUniformManager& fUniformManager; | 401 GrGLUniformManager& fUniformManager; |
478 uint32_t fFSFeaturesAddedMask; | 402 uint32_t fFSFeaturesAddedMask; |
479 SkString fFSFunctions; | 403 SkString fFSFunctions; |
480 SkString fFSExtensions; | 404 SkString fFSExtensions; |
481 VarArray fFSInputs; | 405 VarArray fFSInputs; |
482 VarArray fFSOutputs; | 406 VarArray fFSOutputs; |
483 | 407 |
484 SkString fFSCode; | 408 SkString fFSCode; |
485 | 409 |
486 bool fSetupFragPosition; | 410 bool fSetupFragPosition; |
487 TextureSampler fDstCopySampler; | 411 GrGLUniformManager::UniformHandle fDstCopySamplerUniform; |
488 | 412 |
489 SkString fInputColor; | 413 SkString fInputColor; |
490 GrSLConstantVec fKnownColorValue; | 414 GrSLConstantVec fKnownColorValue; |
491 SkString fInputCoverage; | 415 SkString fInputCoverage; |
492 GrSLConstantVec fKnownCoverageValue; | 416 GrSLConstantVec fKnownCoverageValue; |
493 | 417 |
494 bool fHasCustomColorOutput; | 418 bool fHasCustomColorOutput; |
495 bool fHasSecondaryOutput; | 419 bool fHasSecondaryOutput; |
496 | 420 |
497 GrGLUniformManager::UniformHandle fRTHeightUniform; | 421 GrGLUniformManager::UniformHandle fRTHeightUniform; |
498 GrGLUniformManager::UniformHandle fDstCopyTopLeftUniform; | 422 GrGLUniformManager::UniformHandle fDstCopyTopLeftUniform; |
499 GrGLUniformManager::UniformHandle fDstCopyScaleUniform; | 423 GrGLUniformManager::UniformHandle fDstCopyScaleUniform; |
500 GrGLUniformManager::UniformHandle fColorUniform; | 424 GrGLUniformManager::UniformHandle fColorUniform; |
501 GrGLUniformManager::UniformHandle fCoverageUniform; | 425 GrGLUniformManager::UniformHandle fCoverageUniform; |
502 | 426 |
503 bool fTopLeftFragPosRead; | 427 bool fTopLeftFragPosRead; |
504 | 428 |
505 SkAutoTDelete<VertexBuilder> fVertexBuilder; | 429 SkAutoTDelete<VertexBuilder> fVertexBuilder; |
506 }; | 430 }; |
507 | 431 |
508 #endif | 432 #endif |
OLD | NEW |