Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(826)

Side by Side Diff: src/gpu/gl/GrGLShaderBuilder.h

Issue 13314002: Add support for reading the dst pixel value in an effect. Use in a new effect for the kDarken xfer … (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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
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
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 {
robertphillips 2013/03/29 18:32:57 } on own line
bsalomon 2013/03/29 18:59:38 Done.
275 return fDstCopyTopLeftUniform; }
276 GrGLUniformManager::UniformHandle getDstCopyScaleUniform() const {
277 return fDstCopyScaleUniform;
278 }
279 GrGLUniformManager::UniformHandle getDstCopySamplerUniform() const {
280 return fDstCopySampler.fSamplerUniform;
281 }
258 282
259 struct AttributePair { 283 struct AttributePair {
260 void set(int index, const SkString& name) { 284 void set(int index, const SkString& name) {
261 fIndex = index; fName = name; 285 fIndex = index; fName = name;
262 } 286 }
263 int fIndex; 287 int fIndex;
264 SkString fName; 288 SkString fName;
265 }; 289 };
266 const SkSTArray<10, AttributePair, true>& getEffectAttributes() const { 290 const SkTArray<AttributePair, true>& getEffectAttributes() const {
267 return fEffectAttributes; 291 return fEffectAttributes;
268 } 292 }
269 const SkString* getEffectAttributeName(int attributeIndex) const; 293 const SkString* getEffectAttributeName(int attributeIndex) const;
270 294
271 // TODO: Make this do all the compiling, linking, etc. 295 // TODO: Make this do all the compiling, linking, etc.
272 void finished(GrGLuint programID); 296 void finished(GrGLuint programID);
273 297
274 const GrGLContextInfo& ctxInfo() const { return fCtxInfo; } 298 const GrGLContextInfo& ctxInfo() const { return fCtxInfo; }
275 299
276 private: 300 private:
(...skipping 12 matching lines...) Expand all
289 public: 313 public:
290 314
291 SkString fHeader; // VS+FS, GLSL version, etc 315 SkString fHeader; // VS+FS, GLSL version, etc
292 VarArray fVSAttrs; 316 VarArray fVSAttrs;
293 VarArray fVSOutputs; 317 VarArray fVSOutputs;
294 VarArray fGSInputs; 318 VarArray fGSInputs;
295 VarArray fGSOutputs; 319 VarArray fGSOutputs;
296 VarArray fFSInputs; 320 VarArray fFSInputs;
297 SkString fGSHeader; // layout qualifiers specific to GS 321 SkString fGSHeader; // layout qualifiers specific to GS
298 VarArray fFSOutputs; 322 VarArray fFSOutputs;
299 bool fUsesGS;
300 323
301 private: 324 private:
302 enum { 325 enum {
303 kNonStageIdx = -1, 326 kNonStageIdx = -1,
304 }; 327 };
305 328
329 // Interpretation of DstReadKey when generating code
330 enum {
331 kNoDstRead_DstReadKey = 0,
332 kYesDstRead_DstReadKeyBit = 0x1, // Set if we do a dst-copy-read.
333 kUseAlphaConfig_DstReadKeyBit = 0x2, // Set if dst-copy config is alpha only.
334 kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-le ft.
335 };
336
306 const GrGLContextInfo& fCtxInfo; 337 const GrGLContextInfo& fCtxInfo;
307 GrGLUniformManager& fUniformManager; 338 GrGLUniformManager& fUniformManager;
308 int fCurrentStageIdx; 339 int fCurrentStageIdx;
309 SkString fFSFunctions; 340 SkString fFSFunctions;
310 SkString fFSHeader; 341 SkString fFSHeader;
311 342
343 bool fUsesGS;
344
312 SkString fFSCode; 345 SkString fFSCode;
313 SkString fVSCode; 346 SkString fVSCode;
314 SkString fGSCode; 347 SkString fGSCode;
315 348
316 bool fSetupFragPosition; 349 bool fSetupFragPosition;
350 TextureSampler fDstCopySampler;
351
317 GrGLUniformManager::UniformHandle fRTHeightUniform; 352 GrGLUniformManager::UniformHandle fRTHeightUniform;
353 GrGLUniformManager::UniformHandle fDstCopyTopLeftUniform;
354 GrGLUniformManager::UniformHandle fDstCopyScaleUniform;
318 355
319 SkSTArray<10, AttributePair, true> fEffectAttributes; 356 SkSTArray<10, AttributePair, true> fEffectAttributes;
320 357
321 GrGLShaderVar* fPositionVar; 358 GrGLShaderVar* fPositionVar;
322 GrGLShaderVar* fLocalCoordsVar; 359 GrGLShaderVar* fLocalCoordsVar;
323 360
324 }; 361 };
325 362
326 #endif 363 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698