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 #include "gl/GrGLShaderBuilder.h" | 8 #include "gl/GrGLShaderBuilder.h" |
9 #include "gl/GrGLProgram.h" | 9 #include "gl/GrGLProgram.h" |
10 #include "gl/GrGLUniformHandle.h" | 10 #include "gl/GrGLUniformHandle.h" |
| 11 #include "GrDrawEffect.h" |
11 #include "GrTexture.h" | 12 #include "GrTexture.h" |
12 | 13 |
13 // number of each input/output type in a single allocation block | 14 // number of each input/output type in a single allocation block |
14 static const int kVarsPerBlock = 8; | 15 static const int kVarsPerBlock = 8; |
15 | 16 |
16 // except FS outputs where we expect 2 at most. | 17 // except FS outputs where we expect 2 at most. |
17 static const int kMaxFSOutputs = 2; | 18 static const int kMaxFSOutputs = 2; |
18 | 19 |
19 // ES2 FS only guarantees mediump and lowp support | 20 // ES2 FS only guarantees mediump and lowp support |
20 static const GrGLShaderVar::Precision kDefaultFragmentPrecision = GrGLShaderVar:
:kMedium_Precision; | 21 static const GrGLShaderVar::Precision kDefaultFragmentPrecision = GrGLShaderVar:
:kMedium_Precision; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 } | 76 } |
76 | 77 |
77 /////////////////////////////////////////////////////////////////////////////// | 78 /////////////////////////////////////////////////////////////////////////////// |
78 | 79 |
79 // Architectural assumption: always 2-d input coords. | 80 // Architectural assumption: always 2-d input coords. |
80 // Likely to become non-constant and non-static, perhaps even | 81 // Likely to become non-constant and non-static, perhaps even |
81 // varying by stage, if we use 1D textures for gradients! | 82 // varying by stage, if we use 1D textures for gradients! |
82 //const int GrGLShaderBuilder::fCoordDims = 2; | 83 //const int GrGLShaderBuilder::fCoordDims = 2; |
83 | 84 |
84 GrGLShaderBuilder::GrGLShaderBuilder(const GrGLContextInfo& ctxInfo, | 85 GrGLShaderBuilder::GrGLShaderBuilder(const GrGLContextInfo& ctxInfo, |
85 GrGLUniformManager& uniformManager) | 86 GrGLUniformManager& uniformManager, |
| 87 bool explicitLocalCoords) |
86 : fUniforms(kVarsPerBlock) | 88 : fUniforms(kVarsPerBlock) |
87 , fVSAttrs(kVarsPerBlock) | 89 , fVSAttrs(kVarsPerBlock) |
88 , fVSOutputs(kVarsPerBlock) | 90 , fVSOutputs(kVarsPerBlock) |
89 , fGSInputs(kVarsPerBlock) | 91 , fGSInputs(kVarsPerBlock) |
90 , fGSOutputs(kVarsPerBlock) | 92 , fGSOutputs(kVarsPerBlock) |
91 , fFSInputs(kVarsPerBlock) | 93 , fFSInputs(kVarsPerBlock) |
92 , fFSOutputs(kMaxFSOutputs) | 94 , fFSOutputs(kMaxFSOutputs) |
93 , fUsesGS(false) | 95 , fUsesGS(false) |
94 , fCtxInfo(ctxInfo) | 96 , fCtxInfo(ctxInfo) |
95 , fUniformManager(uniformManager) | 97 , fUniformManager(uniformManager) |
96 , fCurrentStageIdx(kNonStageIdx) | 98 , fCurrentStageIdx(kNonStageIdx) |
97 , fSetupFragPosition(false) | 99 , fSetupFragPosition(false) |
98 , fRTHeightUniform(GrGLUniformManager::kInvalidUniformHandle) { | 100 , fRTHeightUniform(GrGLUniformManager::kInvalidUniformHandle) { |
99 | 101 |
100 fPositionVar = &fVSAttrs.push_back(); | 102 fPositionVar = &fVSAttrs.push_back(); |
101 fPositionVar->set(kVec2f_GrSLType, GrGLShaderVar::kAttribute_TypeModifier, "
aPosition"); | 103 fPositionVar->set(kVec2f_GrSLType, GrGLShaderVar::kAttribute_TypeModifier, "
aPosition"); |
| 104 if (explicitLocalCoords) { |
| 105 fLocalCoordsVar = &fVSAttrs.push_back(); |
| 106 fLocalCoordsVar->set(kVec2f_GrSLType, |
| 107 GrGLShaderVar::kAttribute_TypeModifier, |
| 108 "aLocalCoords"); |
| 109 } else { |
| 110 fLocalCoordsVar = fPositionVar; |
| 111 } |
102 } | 112 } |
103 | 113 |
104 void GrGLShaderBuilder::codeAppendf(ShaderType type, const char format[], va_lis
t args) { | 114 void GrGLShaderBuilder::codeAppendf(ShaderType type, const char format[], va_lis
t args) { |
105 SkString* string = NULL; | 115 SkString* string = NULL; |
106 switch (type) { | 116 switch (type) { |
107 case kVertex_ShaderType: | 117 case kVertex_ShaderType: |
108 string = &fVSCode; | 118 string = &fVSCode; |
109 break; | 119 break; |
110 case kGeometry_ShaderType: | 120 case kGeometry_ShaderType: |
111 string = &fGSCode; | 121 string = &fGSCode; |
(...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
487 | 497 |
488 void GrGLShaderBuilder::finished(GrGLuint programID) { | 498 void GrGLShaderBuilder::finished(GrGLuint programID) { |
489 fUniformManager.getUniformLocations(programID, fUniforms); | 499 fUniformManager.getUniformLocations(programID, fUniforms); |
490 } | 500 } |
491 | 501 |
492 GrGLEffect* GrGLShaderBuilder::createAndEmitGLEffect( | 502 GrGLEffect* GrGLShaderBuilder::createAndEmitGLEffect( |
493 const GrEffectStage& stage, | 503 const GrEffectStage& stage, |
494 GrGLEffect::EffectKey key, | 504 GrGLEffect::EffectKey key, |
495 const char* fsInColor, | 505 const char* fsInColor, |
496 const char* fsOutColor, | 506 const char* fsOutColor, |
497 const char* vsInCoord, | |
498 SkTArray<GrGLUniformManager::UniformHandle, true
>* samplerHandles) { | 507 SkTArray<GrGLUniformManager::UniformHandle, true
>* samplerHandles) { |
499 GrAssert(NULL != stage.getEffect()); | 508 GrAssert(NULL != stage.getEffect()); |
500 | 509 |
501 const GrEffectRef& effect = *stage.getEffect(); | 510 const GrEffectRef& effect = *stage.getEffect(); |
502 int numTextures = effect->numTextures(); | 511 int numTextures = effect->numTextures(); |
503 SkSTArray<8, GrGLShaderBuilder::TextureSampler> textureSamplers; | 512 SkSTArray<8, GrGLShaderBuilder::TextureSampler> textureSamplers; |
504 textureSamplers.push_back_n(numTextures); | 513 textureSamplers.push_back_n(numTextures); |
505 for (int i = 0; i < numTextures; ++i) { | 514 for (int i = 0; i < numTextures; ++i) { |
506 textureSamplers[i].init(this, &effect->textureAccess(i), i); | 515 textureSamplers[i].init(this, &effect->textureAccess(i), i); |
507 samplerHandles->push_back(textureSamplers[i].fSamplerUniform); | 516 samplerHandles->push_back(textureSamplers[i].fSamplerUniform); |
508 } | 517 } |
| 518 GrDrawEffect drawEffect(stage, this->hasExplicitLocalCoords()); |
509 | 519 |
510 int numAttributes = stage.getVertexAttribIndexCount(); | 520 int numAttributes = stage.getVertexAttribIndexCount(); |
511 const int* attributeIndices = stage.getVertexAttribIndices(); | 521 const int* attributeIndices = stage.getVertexAttribIndices(); |
512 SkSTArray<GrEffect::kMaxVertexAttribs, SkString> attributeNames; | 522 SkSTArray<GrEffect::kMaxVertexAttribs, SkString> attributeNames; |
513 for (int i = 0; i < numAttributes; ++i) { | 523 for (int i = 0; i < numAttributes; ++i) { |
514 SkString attributeName("aAttr"); | 524 SkString attributeName("aAttr"); |
515 attributeName.appendS32(attributeIndices[i]); | 525 attributeName.appendS32(attributeIndices[i]); |
516 | 526 |
517 if (this->addAttribute(effect->vertexAttribType(i), attributeName.c_str(
))) { | 527 if (this->addAttribute(effect->vertexAttribType(i), attributeName.c_str(
))) { |
518 fEffectAttributes.push_back().set(attributeIndices[i], attributeName
); | 528 fEffectAttributes.push_back().set(attributeIndices[i], attributeName
); |
519 } | 529 } |
520 } | 530 } |
521 | 531 |
522 GrGLEffect* glEffect = effect->getFactory().createGLInstance(effect); | 532 GrGLEffect* glEffect = effect->getFactory().createGLInstance(drawEffect); |
523 | 533 |
524 // Enclose custom code in a block to avoid namespace conflicts | 534 // Enclose custom code in a block to avoid namespace conflicts |
525 this->fVSCode.appendf("\t{ // %s\n", glEffect->name()); | 535 this->fVSCode.appendf("\t{ // %s\n", glEffect->name()); |
526 this->fFSCode.appendf("\t{ // %s \n", glEffect->name()); | 536 this->fFSCode.appendf("\t{ // %s \n", glEffect->name()); |
| 537 |
527 glEffect->emitCode(this, | 538 glEffect->emitCode(this, |
528 stage, | 539 drawEffect, |
529 key, | 540 key, |
530 vsInCoord, | |
531 fsOutColor, | 541 fsOutColor, |
532 fsInColor, | 542 fsInColor, |
533 textureSamplers); | 543 textureSamplers); |
534 this->fVSCode.appendf("\t}\n"); | 544 this->fVSCode.appendf("\t}\n"); |
535 this->fFSCode.appendf("\t}\n"); | 545 this->fFSCode.appendf("\t}\n"); |
536 | 546 |
537 return glEffect; | 547 return glEffect; |
538 } | 548 } |
539 | 549 |
540 const SkString* GrGLShaderBuilder::getEffectAttributeName(int attributeIndex) co
nst { | 550 const SkString* GrGLShaderBuilder::getEffectAttributeName(int attributeIndex) co
nst { |
541 const AttributePair* attribEnd = this->getEffectAttributes().end(); | 551 const AttributePair* attribEnd = this->getEffectAttributes().end(); |
542 for (const AttributePair* attrib = this->getEffectAttributes().begin(); | 552 for (const AttributePair* attrib = this->getEffectAttributes().begin(); |
543 attrib != attribEnd; | 553 attrib != attribEnd; |
544 ++attrib) { | 554 ++attrib) { |
545 if (attrib->fIndex == attributeIndex) { | 555 if (attrib->fIndex == attributeIndex) { |
546 return &attrib->fName; | 556 return &attrib->fName; |
547 } | 557 } |
548 } | 558 } |
549 | 559 |
550 return NULL; | 560 return NULL; |
551 } | 561 } |
OLD | NEW |