| 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/GrGLUniform.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 class GrGLProgramDesc; |
| 23 class GrGLProgram; |
| 23 | 24 |
| 24 /** | 25 /** |
| 25 Contains all the incremental state of a shader as it is being built,as well as
helpers to | 26 Contains all the incremental state of a shader as it is being built,as well as
helpers to |
| 26 manipulate that state. | 27 manipulate that state. |
| 27 */ | 28 */ |
| 28 class GrGLShaderBuilder { | 29 class GrGLShaderBuilder { |
| 29 public: | 30 public: |
| 31 /** Structure containing information about a uniform during the program buil
d phase. */ |
| 32 class Uniform { |
| 33 public: |
| 34 GrGLUniform* glUniform() const { return fGLUniform; } |
| 35 |
| 36 const char* c_str() const { return fVariable.c_str(); } |
| 37 |
| 38 void appendArrayAccess(int index, SkString* out) const { |
| 39 fVariable.appendArrayAccess(index, out); |
| 40 } |
| 41 |
| 42 void appendArrayAccess(const char* indexName, SkString* out) const { |
| 43 fVariable.appendArrayAccess(indexName, out); |
| 44 } |
| 45 |
| 46 private: |
| 47 GrGLShaderVar fVariable; |
| 48 uint32_t fVisibility; |
| 49 GrGLUniform* fGLUniform; |
| 50 |
| 51 friend class GrGLShaderBuilder; |
| 52 }; |
| 53 |
| 30 /** | 54 /** |
| 31 * Passed to GrGLEffects to add texture reads to their shader code. | 55 * Passed to GrGLEffects to add texture reads to their shader code. |
| 32 */ | 56 */ |
| 33 class TextureSampler { | 57 class TextureSampler { |
| 34 public: | 58 public: |
| 35 TextureSampler() | 59 TextureSampler() |
| 36 : fConfigComponentMask(0) | 60 : fConfigComponentMask(0) |
| 37 , fSamplerUniform(GrGLUniformManager::kInvalidUniformHandle) { | 61 , fSamplerUniform(NULL) { |
| 38 // we will memcpy the first 4 bytes from passed in swizzle. This ens
ures the string is | 62 // we will memcpy the first 4 bytes from passed in swizzle. This ens
ures the string is |
| 39 // terminated. | 63 // terminated. |
| 40 fSwizzle[4] = '\0'; | 64 fSwizzle[4] = '\0'; |
| 41 } | 65 } |
| 42 | 66 |
| 43 TextureSampler(const TextureSampler& other) { *this = other; } | 67 TextureSampler(const TextureSampler& other) { *this = other; } |
| 44 | 68 |
| 45 TextureSampler& operator= (const TextureSampler& other) { | 69 TextureSampler& operator= (const TextureSampler& other) { |
| 46 GrAssert(0 == fConfigComponentMask); | 70 GrAssert(0 == fConfigComponentMask); |
| 47 GrAssert(GrGLUniformManager::kInvalidUniformHandle == fSamplerUnifor
m); | 71 GrAssert(NULL == fSamplerUniform); |
| 48 | 72 |
| 49 fConfigComponentMask = other.fConfigComponentMask; | 73 fConfigComponentMask = other.fConfigComponentMask; |
| 50 fSamplerUniform = other.fSamplerUniform; | 74 fSamplerUniform = other.fSamplerUniform; |
| 51 return *this; | 75 return *this; |
| 52 } | 76 } |
| 53 | 77 |
| 54 // bitfield of GrColorComponentFlags present in the texture's config. | 78 // bitfield of GrColorComponentFlags present in the texture's config. |
| 55 uint32_t configComponentMask() const { return fConfigComponentMask; } | 79 uint32_t configComponentMask() const { return fConfigComponentMask; } |
| 56 | 80 |
| 57 const char* swizzle() const { return fSwizzle; } | 81 const char* swizzle() const { return fSwizzle; } |
| 58 | 82 |
| 59 bool isInitialized() const { return 0 != fConfigComponentMask; } | 83 bool isInitialized() const { return 0 != fConfigComponentMask; } |
| 60 | 84 |
| 61 private: | 85 private: |
| 62 // The idx param is used to ensure multiple samplers within a single eff
ect have unique | 86 // The idx param is used to ensure multiple samplers within a single eff
ect have unique |
| 63 // uniform names. swizzle is a four char max string made up of chars 'r'
, 'g', 'b', and 'a'. | 87 // uniform names. swizzle is a four char max string made up of chars 'r'
, 'g', 'b', and 'a'. |
| 64 void init(GrGLShaderBuilder* builder, | 88 void init(GrGLShaderBuilder* builder, |
| 65 uint32_t configComponentMask, | 89 uint32_t configComponentMask, |
| 66 const char* swizzle, | 90 const char* swizzle, |
| 67 int idx) { | 91 int idx) { |
| 68 GrAssert(!this->isInitialized()); | 92 GrAssert(!this->isInitialized()); |
| 69 GrAssert(0 != configComponentMask); | 93 GrAssert(0 != configComponentMask); |
| 70 GrAssert(GrGLUniformManager::kInvalidUniformHandle == fSamplerUnifor
m); | 94 GrAssert(NULL == fSamplerUniform); |
| 71 | 95 |
| 72 GrAssert(NULL != builder); | 96 GrAssert(NULL != builder); |
| 73 SkString name; | 97 SkString name; |
| 74 name.printf("Sampler%d", idx); | 98 name.printf("Sampler%d", idx); |
| 75 fSamplerUniform = builder->addUniform(GrGLShaderBuilder::kFragment_S
haderType, | 99 fSamplerUniform = builder->addUniform(GrGLShaderBuilder::kFragment_S
haderType, |
| 76 kSampler2D_GrSLType, | 100 kSampler2D_GrSLType, |
| 77 name.c_str()); | 101 name.c_str()); |
| 78 GrAssert(GrGLUniformManager::kInvalidUniformHandle != fSamplerUnifor
m); | 102 GrAssert(NULL != fSamplerUniform); |
| 79 | 103 |
| 80 fConfigComponentMask = configComponentMask; | 104 fConfigComponentMask = configComponentMask; |
| 81 memcpy(fSwizzle, swizzle, 4); | 105 memcpy(fSwizzle, swizzle, 4); |
| 82 } | 106 } |
| 83 | 107 |
| 84 void init(GrGLShaderBuilder* builder, const GrTextureAccess* access, int
idx) { | 108 void init(GrGLShaderBuilder* builder, const GrTextureAccess* access, int
idx) { |
| 85 GrAssert(NULL != access); | 109 GrAssert(NULL != access); |
| 86 this->init(builder, | 110 this->init(builder, |
| 87 GrPixelConfigComponentMask(access->getTexture()->config()
), | 111 GrPixelConfigComponentMask(access->getTexture()->config()
), |
| 88 access->getSwizzle(), | 112 access->getSwizzle(), |
| 89 idx); | 113 idx); |
| 90 } | 114 } |
| 91 | 115 |
| 92 uint32_t fConfigComponentMask; | 116 uint32_t fConfigComponentMask; |
| 93 char fSwizzle[5]; | 117 char fSwizzle[5]; |
| 94 GrGLUniformManager::UniformHandle fSamplerUniform; | 118 Uniform* fSamplerUniform; |
| 95 | 119 |
| 96 friend class GrGLShaderBuilder; // to call init(). | 120 friend class GrGLShaderBuilder; // to call init(). |
| 97 }; | 121 }; |
| 98 | 122 |
| 99 typedef SkTArray<TextureSampler> TextureSamplerArray; | 123 typedef SkTArray<TextureSampler> TextureSamplerArray; |
| 100 | 124 |
| 101 enum ShaderType { | 125 enum ShaderType { |
| 102 kVertex_ShaderType = 0x1, | 126 kVertex_ShaderType = 0x1, |
| 103 kGeometry_ShaderType = 0x2, | 127 kGeometry_ShaderType = 0x2, |
| 104 kFragment_ShaderType = 0x4, | 128 kFragment_ShaderType = 0x4, |
| 105 }; | 129 }; |
| 106 | 130 |
| 107 GrGLShaderBuilder(const GrGLContextInfo&, GrGLUniformManager&, const GrGLPro
gramDesc&); | 131 GrGLShaderBuilder(const GrGLContextInfo&, GrGLProgram&, const GrGLProgramDes
c&); |
| 108 | 132 |
| 109 /** | 133 /** |
| 110 * Use of these features may require a GLSL extension to be enabled. Shaders
may not compile | 134 * Use of these features may require a GLSL extension to be enabled. Shaders
may not compile |
| 111 * if code is added that uses one of these features without calling enableFe
ature() | 135 * if code is added that uses one of these features without calling enableFe
ature() |
| 112 */ | 136 */ |
| 113 enum GLSLFeature { | 137 enum GLSLFeature { |
| 114 kStandardDerivatives_GLSLFeature = 0, | 138 kStandardDerivatives_GLSLFeature = 0, |
| 115 | 139 |
| 116 kLastGLSLFeature = kStandardDerivatives_GLSLFeature | 140 kLastGLSLFeature = kStandardDerivatives_GLSLFeature |
| 117 }; | 141 }; |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 the generated shader code. This potentially allows greater reuse of cach
ed shaders. */ | 233 the generated shader code. This potentially allows greater reuse of cach
ed shaders. */ |
| 210 static const GrGLenum* GetTexParamSwizzle(GrPixelConfig config, const GrGLCa
ps& caps); | 234 static const GrGLenum* GetTexParamSwizzle(GrPixelConfig config, const GrGLCa
ps& caps); |
| 211 | 235 |
| 212 /** Add a uniform variable to the current program, that has visibility in on
e or more shaders. | 236 /** Add a uniform variable to the current program, that has visibility in on
e or more shaders. |
| 213 visibility is a bitfield of ShaderType values indicating from which shad
ers the uniform | 237 visibility is a bitfield of ShaderType values indicating from which shad
ers the uniform |
| 214 should be accessible. At least one bit must be set. Geometry shader unif
orms are not | 238 should be accessible. At least one bit must be set. Geometry shader unif
orms are not |
| 215 supported at this time. The actual uniform name will be mangled. If outN
ame is not NULL then | 239 supported at this time. The actual uniform name will be mangled. If outN
ame is not NULL then |
| 216 it will refer to the final uniform name after return. Use the addUniform
Array variant to add | 240 it will refer to the final uniform name after return. Use the addUniform
Array variant to add |
| 217 an array of uniforms. | 241 an array of uniforms. |
| 218 */ | 242 */ |
| 219 GrGLUniformManager::UniformHandle addUniform(uint32_t visibility, | 243 Uniform* addUniform(uint32_t visibility, GrSLType type, const char* name, |
| 220 GrSLType type, | 244 const char** outName = NULL) { |
| 221 const char* name, | |
| 222 const char** outName = NULL) { | |
| 223 return this->addUniformArray(visibility, type, name, GrGLShaderVar::kNon
Array, outName); | 245 return this->addUniformArray(visibility, type, name, GrGLShaderVar::kNon
Array, outName); |
| 224 } | 246 } |
| 225 GrGLUniformManager::UniformHandle addUniformArray(uint32_t visibility, | 247 Uniform* addUniformArray(uint32_t visibility, GrSLType type, const char* nam
e, int arrayCount, |
| 226 GrSLType type, | 248 const char** outName = NULL); |
| 227 const char* name, | |
| 228 int arrayCount, | |
| 229 const char** outName = NUL
L); | |
| 230 | |
| 231 const GrGLShaderVar& getUniformVariable(GrGLUniformManager::UniformHandle) c
onst; | |
| 232 | |
| 233 /** | |
| 234 * Shortcut for getUniformVariable(u).c_str() | |
| 235 */ | |
| 236 const char* getUniformCStr(GrGLUniformManager::UniformHandle u) const { | |
| 237 return this->getUniformVariable(u).c_str(); | |
| 238 } | |
| 239 | 249 |
| 240 /** Add a vertex attribute to the current program that is passed in from the
vertex data. | 250 /** Add a vertex attribute to the current program that is passed in from the
vertex data. |
| 241 Returns false if the attribute was already there, true otherwise. */ | 251 Returns false if the attribute was already there, true otherwise. */ |
| 242 bool addAttribute(GrSLType type, const char* name); | 252 bool addAttribute(GrSLType type, const char* name); |
| 243 | 253 |
| 244 /** Add a varying variable to the current program to pass values between vert
ex and fragment | 254 /** Add a varying variable to the current program to pass values between vert
ex and fragment |
| 245 shaders. If the last two parameters are non-NULL, they are filled in wit
h the name | 255 shaders. If the last two parameters are non-NULL, they are filled in wit
h the name |
| 246 generated. */ | 256 generated. */ |
| 247 void addVarying(GrSLType type, | 257 void addVarying(GrSLType type, |
| 248 const char* name, | 258 const char* name, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 * has a known constant value and is updated to refer to the status of the o
utput color. | 300 * has a known constant value and is updated to refer to the status of the o
utput color. |
| 291 * The handles to texture samplers for effectStage[i] are added to effectSam
plerHandles[i]. The | 301 * The handles to texture samplers for effectStage[i] are added to effectSam
plerHandles[i]. The |
| 292 * glEffects array is updated to contain the GrGLEffect generated for each e
ntry in | 302 * glEffects array is updated to contain the GrGLEffect generated for each e
ntry in |
| 293 * effectStages. | 303 * effectStages. |
| 294 */ | 304 */ |
| 295 void emitEffects(const GrEffectStage* effectStages[], | 305 void emitEffects(const GrEffectStage* effectStages[], |
| 296 const GrBackendEffectFactory::EffectKey effectKeys[], | 306 const GrBackendEffectFactory::EffectKey effectKeys[], |
| 297 int effectCnt, | 307 int effectCnt, |
| 298 SkString* inOutFSColor, | 308 SkString* inOutFSColor, |
| 299 GrSLConstantVec* fsInOutColorKnownValue, | 309 GrSLConstantVec* fsInOutColorKnownValue, |
| 300 SkTArray<GrGLUniformManager::UniformHandle, true>* effectSa
mplerHandles[], | 310 SkTArray<GrGLUniform*, true>* effectSamplerHandles[], |
| 301 GrGLEffect* glEffects[]); | 311 GrGLEffect* glEffects[]); |
| 302 | 312 |
| 303 GrGLUniformManager::UniformHandle getRTHeightUniform() const { return fRTHei
ghtUniform; } | 313 GrGLUniform* getRTHeightUniform() const { return fRTHeightUniform; } |
| 304 GrGLUniformManager::UniformHandle getDstCopyTopLeftUniform() const { | 314 GrGLUniform* getDstCopyTopLeftUniform() const { |
| 305 return fDstCopyTopLeftUniform; | 315 return fDstCopyTopLeftUniform; |
| 306 } | 316 } |
| 307 GrGLUniformManager::UniformHandle getDstCopyScaleUniform() const { | 317 GrGLUniform* getDstCopyScaleUniform() const { |
| 308 return fDstCopyScaleUniform; | 318 return fDstCopyScaleUniform; |
| 309 } | 319 } |
| 310 GrGLUniformManager::UniformHandle getDstCopySamplerUniform() const { | 320 GrGLUniform* getDstCopySamplerUniform() const { |
| 311 return fDstCopySampler.fSamplerUniform; | 321 return fDstCopySampler.fSamplerUniform ? fDstCopySampler.fSamplerUniform
->glUniform() : NULL; |
| 312 } | 322 } |
| 313 | 323 |
| 314 struct AttributePair { | 324 struct AttributePair { |
| 315 void set(int index, const SkString& name) { | 325 void set(int index, const SkString& name) { |
| 316 fIndex = index; fName = name; | 326 fIndex = index; fName = name; |
| 317 } | 327 } |
| 318 int fIndex; | 328 int fIndex; |
| 319 SkString fName; | 329 SkString fName; |
| 320 }; | 330 }; |
| 321 const SkTArray<AttributePair, true>& getEffectAttributes() const { | 331 const SkTArray<AttributePair, true>& getEffectAttributes() const { |
| 322 return fEffectAttributes; | 332 return fEffectAttributes; |
| 323 } | 333 } |
| 324 const SkString* getEffectAttributeName(int attributeIndex) const; | 334 const SkString* getEffectAttributeName(int attributeIndex) const; |
| 325 | 335 |
| 326 // TODO: Make this do all the compiling, linking, etc. | 336 // TODO: Make this do all the compiling, linking, etc. |
| 327 void finished(GrGLuint programID); | 337 void finished(const GrGLContext&, GrGLuint programID); |
| 328 | 338 |
| 329 const GrGLContextInfo& ctxInfo() const { return fCtxInfo; } | 339 const GrGLContextInfo& ctxInfo() const { return fCtxInfo; } |
| 330 | 340 |
| 331 private: | 341 private: |
| 332 void codeAppendf(ShaderType type, const char format[], va_list args); | 342 void codeAppendf(ShaderType type, const char format[], va_list args); |
| 333 void codeAppend(ShaderType type, const char* str); | 343 void codeAppend(ShaderType type, const char* str); |
| 334 | 344 |
| 335 typedef GrTAllocator<GrGLShaderVar> VarArray; | 345 typedef GrTAllocator<GrGLShaderVar> VarArray; |
| 336 | 346 |
| 337 void appendDecls(const VarArray&, SkString*) const; | 347 void appendDecls(const VarArray&, SkString*) const; |
| 338 void appendUniformDecls(ShaderType, SkString*) const; | 348 void appendUniformDecls(ShaderType, SkString*) const; |
| 339 | 349 |
| 340 typedef GrGLUniformManager::BuilderUniform BuilderUniform; | 350 // This uses an allocator rather than array so that the GrGLShaderVars don't
move in memory |
| 341 GrGLUniformManager::BuilderUniformArray fUniforms; | 351 // after they are inserted. Users of GrGLShaderBuilder get refs to the vars
and ptrs to their |
| 352 // name strings. Otherwise, we'd have to hand out copies. |
| 353 typedef GrTAllocator<Uniform> UniformArray; |
| 354 UniformArray fUniforms; |
| 342 | 355 |
| 343 // TODO: Everything below here private. | 356 // TODO: Everything below here private. |
| 344 public: | 357 public: |
| 345 | 358 |
| 346 VarArray fVSAttrs; | 359 VarArray fVSAttrs; |
| 347 VarArray fVSOutputs; | 360 VarArray fVSOutputs; |
| 348 VarArray fGSInputs; | 361 VarArray fGSInputs; |
| 349 VarArray fGSOutputs; | 362 VarArray fGSOutputs; |
| 350 VarArray fFSInputs; | 363 VarArray fFSInputs; |
| 351 SkString fGSHeader; // layout qualifiers specific to GS | 364 SkString fGSHeader; // layout qualifiers specific to GS |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-le
ft. | 443 kTopLeftOrigin_DstReadKeyBit = 0x4, // Set if dst-copy origin is top-le
ft. |
| 431 }; | 444 }; |
| 432 | 445 |
| 433 enum { | 446 enum { |
| 434 kNoFragPosRead_FragPosKey = 0, // The fragment positition wil
l not be needed. | 447 kNoFragPosRead_FragPosKey = 0, // The fragment positition wil
l not be needed. |
| 435 kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to t
op-left. | 448 kTopLeftFragPosRead_FragPosKey = 0x1,// Read frag pos relative to t
op-left. |
| 436 kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to b
ottom-left. | 449 kBottomLeftFragPosRead_FragPosKey = 0x2,// Read frag pos relative to b
ottom-left. |
| 437 }; | 450 }; |
| 438 | 451 |
| 439 const GrGLContextInfo& fCtxInfo; | 452 const GrGLContextInfo& fCtxInfo; |
| 440 GrGLUniformManager& fUniformManager; | 453 GrGLProgram& fProgram; |
| 441 uint32_t fFSFeaturesAddedMask; | 454 uint32_t fFSFeaturesAddedMask; |
| 442 SkString fFSFunctions; | 455 SkString fFSFunctions; |
| 443 SkString fFSExtensions; | 456 SkString fFSExtensions; |
| 444 | 457 |
| 445 bool fUsesGS; | 458 bool fUsesGS; |
| 446 | 459 |
| 447 SkString fFSCode; | 460 SkString fFSCode; |
| 448 SkString fVSCode; | 461 SkString fVSCode; |
| 449 SkString fGSCode; | 462 SkString fGSCode; |
| 450 | 463 |
| 451 bool fSetupFragPosition; | 464 bool fSetupFragPosition; |
| 452 TextureSampler fDstCopySampler; | 465 TextureSampler fDstCopySampler; |
| 453 | 466 |
| 454 GrGLUniformManager::UniformHandle fRTHeightUniform; | 467 GrGLUniform* fRTHeightUniform; |
| 455 GrGLUniformManager::UniformHandle fDstCopyTopLeftUniform; | 468 GrGLUniform* fDstCopyTopLeftUniform; |
| 456 GrGLUniformManager::UniformHandle fDstCopyScaleUniform; | 469 GrGLUniform* fDstCopyScaleUniform; |
| 457 | 470 |
| 458 bool fTopLeftFragPosRead; | 471 bool fTopLeftFragPosRead; |
| 459 | 472 |
| 460 SkSTArray<10, AttributePair, true> fEffectAttributes; | 473 SkSTArray<10, AttributePair, true> fEffectAttributes; |
| 461 | 474 |
| 462 GrGLShaderVar* fPositionVar; | 475 GrGLShaderVar* fPositionVar; |
| 463 GrGLShaderVar* fLocalCoordsVar; | 476 GrGLShaderVar* fLocalCoordsVar; |
| 464 | 477 |
| 465 }; | 478 }; |
| 466 | 479 |
| 467 #endif | 480 #endif |
| OLD | NEW |