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 "gl/GrGLSL.h" | 15 #include "gl/GrGLSL.h" |
16 #include "gl/GrGLUniformManager.h" | 16 #include "gl/GrGLUniformManager.h" |
17 | 17 |
18 #include <stdarg.h> | 18 #include <stdarg.h> |
19 | 19 |
20 class GrGLContextInfo; | 20 class GrGLContextInfo; |
21 class GrEffectStage; | 21 class GrEffectStage; |
| 22 class GrGLProgramDesc; |
22 | 23 |
23 /** | 24 /** |
24 Contains all the incremental state of a shader as it is being built,as well as
helpers to | 25 Contains all the incremental state of a shader as it is being built,as well as
helpers to |
25 manipulate that state. | 26 manipulate that state. |
26 */ | 27 */ |
27 class GrGLShaderBuilder { | 28 class GrGLShaderBuilder { |
28 public: | 29 public: |
29 /** | 30 /** |
30 * Passed to GrGLEffects to add texture reads to their shader code. | 31 * Passed to GrGLEffects to add texture reads to their shader code. |
31 */ | 32 */ |
(...skipping 16 matching lines...) Expand all Loading... |
48 fConfigComponentMask = other.fConfigComponentMask; | 49 fConfigComponentMask = other.fConfigComponentMask; |
49 fSamplerUniform = other.fSamplerUniform; | 50 fSamplerUniform = other.fSamplerUniform; |
50 return *this; | 51 return *this; |
51 } | 52 } |
52 | 53 |
53 // bitfield of GrColorComponentFlags present in the texture's config. | 54 // bitfield of GrColorComponentFlags present in the texture's config. |
54 uint32_t configComponentMask() const { return fConfigComponentMask; } | 55 uint32_t configComponentMask() const { return fConfigComponentMask; } |
55 | 56 |
56 const char* swizzle() const { return fSwizzle; } | 57 const char* swizzle() const { return fSwizzle; } |
57 | 58 |
| 59 bool isInitialized() const { return 0 != fConfigComponentMask; } |
| 60 |
58 private: | 61 private: |
59 // The idx param is used to ensure multiple samplers within a single eff
ect have unique | 62 // The idx param is used to ensure multiple samplers within a single eff
ect have unique |
60 // uniform names. swizzle is a four char max string made up of chars 'r'
, 'g', 'b', and 'a'. | 63 // uniform names. swizzle is a four char max string made up of chars 'r'
, 'g', 'b', and 'a'. |
61 void init(GrGLShaderBuilder* builder, | 64 void init(GrGLShaderBuilder* builder, |
62 uint32_t configComponentMask, | 65 uint32_t configComponentMask, |
63 const char* swizzle, | 66 const char* swizzle, |
64 int idx) { | 67 int idx) { |
65 GrAssert(0 == fConfigComponentMask); | 68 GrAssert(!this->isInitialized()); |
| 69 GrAssert(0 != configComponentMask); |
66 GrAssert(GrGLUniformManager::kInvalidUniformHandle == fSamplerUnifor
m); | 70 GrAssert(GrGLUniformManager::kInvalidUniformHandle == fSamplerUnifor
m); |
67 | 71 |
68 GrAssert(NULL != builder); | 72 GrAssert(NULL != builder); |
69 SkString name; | 73 SkString name; |
70 name.printf("Sampler%d_", idx); | 74 name.printf("Sampler%d_", idx); |
71 fSamplerUniform = builder->addUniform(GrGLShaderBuilder::kFragment_S
haderType, | 75 fSamplerUniform = builder->addUniform(GrGLShaderBuilder::kFragment_S
haderType, |
72 kSampler2D_GrSLType, | 76 kSampler2D_GrSLType, |
73 name.c_str()); | 77 name.c_str()); |
74 GrAssert(GrGLUniformManager::kInvalidUniformHandle != fSamplerUnifor
m); | 78 GrAssert(GrGLUniformManager::kInvalidUniformHandle != fSamplerUnifor
m); |
75 | 79 |
(...skipping 17 matching lines...) Expand all Loading... |
93 }; | 97 }; |
94 | 98 |
95 typedef SkTArray<TextureSampler> TextureSamplerArray; | 99 typedef SkTArray<TextureSampler> TextureSamplerArray; |
96 | 100 |
97 enum ShaderType { | 101 enum ShaderType { |
98 kVertex_ShaderType = 0x1, | 102 kVertex_ShaderType = 0x1, |
99 kGeometry_ShaderType = 0x2, | 103 kGeometry_ShaderType = 0x2, |
100 kFragment_ShaderType = 0x4, | 104 kFragment_ShaderType = 0x4, |
101 }; | 105 }; |
102 | 106 |
103 GrGLShaderBuilder(const GrGLContextInfo&, GrGLUniformManager&, bool explicit
LocalCoords); | 107 GrGLShaderBuilder(const GrGLContextInfo&, GrGLUniformManager&, const GrGLPro
gramDesc&); |
104 | 108 |
105 /** | 109 /** |
106 * Called by GrGLEffects to add code to one of the shaders. | 110 * Called by GrGLEffects to add code to one of the shaders. |
107 */ | 111 */ |
108 void vsCodeAppendf(const char format[], ...) SK_PRINTF_LIKE(2, 3) { | 112 void vsCodeAppendf(const char format[], ...) SK_PRINTF_LIKE(2, 3) { |
109 va_list args; | 113 va_list args; |
110 va_start(args, format); | 114 va_start(args, format); |
111 this->codeAppendf(kVertex_ShaderType, format, args); | 115 this->codeAppendf(kVertex_ShaderType, format, args); |
112 va_end(args); | 116 va_end(args); |
113 } | 117 } |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 const GrGLShaderVar* args, | 169 const GrGLShaderVar* args, |
166 const char* body, | 170 const char* body, |
167 SkString* outName); | 171 SkString* outName); |
168 | 172 |
169 /** Generates a EffectKey for the shader code based on the texture access pa
rameters and the | 173 /** Generates a EffectKey for the shader code based on the texture access pa
rameters and the |
170 capabilities of the GL context. This is useful for keying the shader pr
ograms that may | 174 capabilities of the GL context. This is useful for keying the shader pr
ograms that may |
171 have multiple representations, based on the type/format of textures used
. */ | 175 have multiple representations, based on the type/format of textures used
. */ |
172 static GrBackendEffectFactory::EffectKey KeyForTextureAccess(const GrTexture
Access&, | 176 static GrBackendEffectFactory::EffectKey KeyForTextureAccess(const GrTexture
Access&, |
173 const GrGLCaps&
); | 177 const GrGLCaps&
); |
174 | 178 |
| 179 typedef uint8_t DstReadKey; |
| 180 |
| 181 /** Returns a key for adding code to read the copy-of-dst color in service
of effects that |
| 182 require reading the dst. It must not return 0 because 0 indicates that
there is no dst |
| 183 copy read at all. */ |
| 184 static DstReadKey KeyForDstRead(const GrTexture* dstCopy, const GrGLCaps&); |
| 185 |
175 /** If texture swizzling is available using tex parameters then it is prefer
red over mangling | 186 /** If texture swizzling is available using tex parameters then it is prefer
red over mangling |
176 the generated shader code. This potentially allows greater reuse of cach
ed shaders. */ | 187 the generated shader code. This potentially allows greater reuse of cach
ed shaders. */ |
177 static const GrGLenum* GetTexParamSwizzle(GrPixelConfig config, const GrGLCa
ps& caps); | 188 static const GrGLenum* GetTexParamSwizzle(GrPixelConfig config, const GrGLCa
ps& caps); |
178 | 189 |
179 /** Add a uniform variable to the current program, that has visibility in on
e or more shaders. | 190 /** Add a uniform variable to the current program, that has visibility in on
e or more shaders. |
180 visibility is a bitfield of ShaderType values indicating from which shad
ers the uniform | 191 visibility is a bitfield of ShaderType values indicating from which shad
ers the uniform |
181 should be accessible. At least one bit must be set. Geometry shader unif
orms are not | 192 should be accessible. At least one bit must be set. Geometry shader unif
orms are not |
182 supported at this time. The actual uniform name will be mangled. If outN
ame is not NULL then | 193 supported at this time. The actual uniform name will be mangled. If outN
ame is not NULL then |
183 it will refer to the final uniform name after return. Use the addUniform
Array variant to add | 194 it will refer to the final uniform name after return. Use the addUniform
Array variant to add |
184 an array of uniforms. | 195 an array of uniforms. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 /** Returns a vertex attribute that represents the vertex position in the VS
. This is the | 234 /** Returns a vertex attribute that represents the vertex position in the VS
. This is the |
224 pre-matrix position and is commonly used by effects to compute texture c
oords via a matrix. | 235 pre-matrix position and is commonly used by effects to compute texture c
oords via a matrix. |
225 */ | 236 */ |
226 const GrGLShaderVar& positionAttribute() const { return *fPositionVar; } | 237 const GrGLShaderVar& positionAttribute() const { return *fPositionVar; } |
227 | 238 |
228 /** Returns a vertex attribute that represents the local coords in the VS. T
his may be the same | 239 /** Returns a vertex attribute that represents the local coords in the VS. T
his may be the same |
229 as positionAttribute() or it may not be. It depends upon whether the ren
dering code | 240 as positionAttribute() or it may not be. It depends upon whether the ren
dering code |
230 specified explicit local coords or not in the GrDrawState. */ | 241 specified explicit local coords or not in the GrDrawState. */ |
231 const GrGLShaderVar& localCoordsAttribute() const { return *fLocalCoordsVar;
} | 242 const GrGLShaderVar& localCoordsAttribute() const { return *fLocalCoordsVar;
} |
232 | 243 |
| 244 /** Returns the color of the destination pixel. This may be NULL if no effec
t advertised |
| 245 that it will read the destination. */ |
| 246 const char* dstColor() const; |
| 247 |
233 /** | 248 /** |
234 * Are explicit local coordinates provided as input to the vertex shader. | 249 * Are explicit local coordinates provided as input to the vertex shader. |
235 */ | 250 */ |
236 bool hasExplicitLocalCoords() const { return (fLocalCoordsVar != fPositionVa
r); } | 251 bool hasExplicitLocalCoords() const { return (fLocalCoordsVar != fPositionVa
r); } |
237 | 252 |
238 /** | 253 /** |
239 * Interfaces used by GrGLProgram. | 254 * Interfaces used by GrGLProgram. |
240 * TODO: Hide these from the GrEffects using friend or splitting this into t
wo related classes. | 255 * TODO: Hide these from the GrEffects using friend or splitting this into t
wo related classes. |
241 * Also, GrGLProgram's shader string construction should be moved to this cl
ass. | 256 * Also, GrGLProgram's shader string construction should be moved to this cl
ass. |
242 */ | 257 */ |
243 | 258 |
244 /** Called after building is complete to get the final shader string. */ | 259 /** Called after building is complete to get the final shader string. */ |
245 void getShader(ShaderType, SkString*) const; | 260 void getShader(ShaderType, SkString*) const; |
246 | 261 |
247 void setCurrentStage(int stageIdx) { fCurrentStageIdx = stageIdx; } | 262 void setCurrentStage(int stageIdx) { fCurrentStageIdx = stageIdx; } |
248 void setNonStage() { fCurrentStageIdx = kNonStageIdx; } | 263 void setNonStage() { fCurrentStageIdx = kNonStageIdx; } |
249 // TODO: move remainder of shader code generation to this class and call thi
s privately | 264 // TODO: move remainder of shader code generation to this class and call thi
s privately |
250 // Handles of sampler uniforms generated for the effect are appended to samp
lerHandles. | 265 // Handles of sampler uniforms generated for the effect are appended to samp
lerHandles. |
251 GrGLEffect* createAndEmitGLEffect( | 266 GrGLEffect* createAndEmitGLEffect( |
252 const GrEffectStage& stage, | 267 const GrEffectStage& stage, |
253 GrBackendEffectFactory::EffectKey key, | 268 GrBackendEffectFactory::EffectKey key, |
254 const char* fsInColor, // NULL means no incoming
color | 269 const char* fsInColor, // NULL means no incoming
color |
255 const char* fsOutColor, | 270 const char* fsOutColor, |
256 SkTArray<GrGLUniformManager::UniformHandle, true
>* samplerHandles); | 271 SkTArray<GrGLUniformManager::UniformHandle, true
>* samplerHandles); |
| 272 |
257 GrGLUniformManager::UniformHandle getRTHeightUniform() const { return fRTHei
ghtUniform; } | 273 GrGLUniformManager::UniformHandle getRTHeightUniform() const { return fRTHei
ghtUniform; } |
| 274 GrGLUniformManager::UniformHandle getDstCopyTopLeftUniform() const { |
| 275 return fDstCopyTopLeftUniform; |
| 276 } |
| 277 GrGLUniformManager::UniformHandle getDstCopyScaleUniform() const { |
| 278 return fDstCopyScaleUniform; |
| 279 } |
| 280 GrGLUniformManager::UniformHandle getDstCopySamplerUniform() const { |
| 281 return fDstCopySampler.fSamplerUniform; |
| 282 } |
258 | 283 |
259 struct AttributePair { | 284 struct AttributePair { |
260 void set(int index, const SkString& name) { | 285 void set(int index, const SkString& name) { |
261 fIndex = index; fName = name; | 286 fIndex = index; fName = name; |
262 } | 287 } |
263 int fIndex; | 288 int fIndex; |
264 SkString fName; | 289 SkString fName; |
265 }; | 290 }; |
266 const SkSTArray<10, AttributePair, true>& getEffectAttributes() const { | 291 const SkTArray<AttributePair, true>& getEffectAttributes() const { |
267 return fEffectAttributes; | 292 return fEffectAttributes; |
268 } | 293 } |
269 const SkString* getEffectAttributeName(int attributeIndex) const; | 294 const SkString* getEffectAttributeName(int attributeIndex) const; |
270 | 295 |
271 // TODO: Make this do all the compiling, linking, etc. | 296 // TODO: Make this do all the compiling, linking, etc. |
272 void finished(GrGLuint programID); | 297 void finished(GrGLuint programID); |
273 | 298 |
274 const GrGLContextInfo& ctxInfo() const { return fCtxInfo; } | 299 const GrGLContextInfo& ctxInfo() const { return fCtxInfo; } |
275 | 300 |
276 private: | 301 private: |
(...skipping 12 matching lines...) Expand all Loading... |
289 public: | 314 public: |
290 | 315 |
291 SkString fHeader; // VS+FS, GLSL version, etc | 316 SkString fHeader; // VS+FS, GLSL version, etc |
292 VarArray fVSAttrs; | 317 VarArray fVSAttrs; |
293 VarArray fVSOutputs; | 318 VarArray fVSOutputs; |
294 VarArray fGSInputs; | 319 VarArray fGSInputs; |
295 VarArray fGSOutputs; | 320 VarArray fGSOutputs; |
296 VarArray fFSInputs; | 321 VarArray fFSInputs; |
297 SkString fGSHeader; // layout qualifiers specific to GS | 322 SkString fGSHeader; // layout qualifiers specific to GS |
298 VarArray fFSOutputs; | 323 VarArray fFSOutputs; |
299 bool fUsesGS; | |
300 | 324 |
301 private: | 325 private: |
302 enum { | 326 enum { |
303 kNonStageIdx = -1, | 327 kNonStageIdx = -1, |
304 }; | 328 }; |
305 | 329 |
| 330 // Interpretation of DstReadKey when generating code |
| 331 enum { |
| 332 kNoDstRead_DstReadKey = 0, |
| 333 kYesDstRead_DstReadKeyBit = 0x1, // Set if we do a dst-copy-read. |
| 334 kUseAlphaConfig_DstReadKeyBit = 0x2, // Set if dst-copy config is alpha
only. |
| 335 kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-le
ft. |
| 336 }; |
| 337 |
306 const GrGLContextInfo& fCtxInfo; | 338 const GrGLContextInfo& fCtxInfo; |
307 GrGLUniformManager& fUniformManager; | 339 GrGLUniformManager& fUniformManager; |
308 int fCurrentStageIdx; | 340 int fCurrentStageIdx; |
309 SkString fFSFunctions; | 341 SkString fFSFunctions; |
310 SkString fFSHeader; | 342 SkString fFSHeader; |
311 | 343 |
| 344 bool fUsesGS; |
| 345 |
312 SkString fFSCode; | 346 SkString fFSCode; |
313 SkString fVSCode; | 347 SkString fVSCode; |
314 SkString fGSCode; | 348 SkString fGSCode; |
315 | 349 |
316 bool fSetupFragPosition; | 350 bool fSetupFragPosition; |
| 351 TextureSampler fDstCopySampler; |
| 352 |
317 GrGLUniformManager::UniformHandle fRTHeightUniform; | 353 GrGLUniformManager::UniformHandle fRTHeightUniform; |
| 354 GrGLUniformManager::UniformHandle fDstCopyTopLeftUniform; |
| 355 GrGLUniformManager::UniformHandle fDstCopyScaleUniform; |
318 | 356 |
319 SkSTArray<10, AttributePair, true> fEffectAttributes; | 357 SkSTArray<10, AttributePair, true> fEffectAttributes; |
320 | 358 |
321 GrGLShaderVar* fPositionVar; | 359 GrGLShaderVar* fPositionVar; |
322 GrGLShaderVar* fLocalCoordsVar; | 360 GrGLShaderVar* fLocalCoordsVar; |
323 | 361 |
324 }; | 362 }; |
325 | 363 |
326 #endif | 364 #endif |
OLD | NEW |