Chromium Code Reviews| 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" |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 inline const char* sample_function_name(GrSLType type, GrGLSLGeneration glslGen) { | 28 inline const char* sample_function_name(GrSLType type, GrGLSLGeneration glslGen) { |
| 29 if (kVec2f_GrSLType == type) { | 29 if (kVec2f_GrSLType == type) { |
| 30 return glslGen >= k130_GrGLSLGeneration ? "texture" : "texture2D"; | 30 return glslGen >= k130_GrGLSLGeneration ? "texture" : "texture2D"; |
| 31 } else { | 31 } else { |
| 32 GrAssert(kVec3f_GrSLType == type); | 32 GrAssert(kVec3f_GrSLType == type); |
| 33 return glslGen >= k130_GrGLSLGeneration ? "textureProj" : "texture2DProj "; | 33 return glslGen >= k130_GrGLSLGeneration ? "textureProj" : "texture2DProj "; |
| 34 } | 34 } |
| 35 } | 35 } |
| 36 | 36 |
| 37 /** | 37 /** |
| 38 * Do we need to either map r,g,b->a or a->r. | 38 * Do we need to either map r,g,b->a or a->r. configComponentMask indicates whic h channels are |
| 39 * present in the texture's config. swizzleComponentMask indicates the channels present in the | |
| 40 * shader swizzle. | |
| 39 */ | 41 */ |
| 40 inline bool swizzle_requires_alpha_remapping(const GrGLCaps& caps, | 42 inline bool swizzle_requires_alpha_remapping(const GrGLCaps& caps, |
| 41 const GrTextureAccess& access) { | 43 uint32_t configComponentMask, |
| 42 if (GrPixelConfigIsAlphaOnly(access.getTexture()->config())) { | 44 uint32_t swizzleComponentMask) { |
| 43 if (caps.textureRedSupport() && (kA_GrColorComponentFlag & access.swizzl eMask())) { | 45 if (caps.textureSwizzleSupport()){ |
| 46 // Any remapping is handled using texture swizzling not shader modificat ions. | |
| 47 return false; | |
| 48 } | |
| 49 // check if the texture is alpha-only | |
| 50 if (kA_GrColorComponentFlag == configComponentMask) { | |
| 51 if (caps.textureRedSupport() && (kA_GrColorComponentFlag & swizzleCompon entMask)) { | |
| 52 // we must map the swizzle 'a's to 'r'. | |
| 44 return true; | 53 return true; |
| 45 } | 54 } |
| 46 if (kRGB_GrColorComponentFlags & access.swizzleMask()) { | 55 if (kRGB_GrColorComponentFlags & swizzleComponentMask) { |
| 56 // The 'r', 'g', and/or 'b's must be mapped to 'a' according to our semantics that | |
| 57 // alpha-only textures smear alpha across all four channels when rea d. | |
| 47 return true; | 58 return true; |
| 48 } | 59 } |
| 49 } | 60 } |
| 50 return false; | 61 return false; |
| 51 } | 62 } |
| 52 | 63 |
| 53 void append_swizzle(SkString* outAppend, | 64 void append_swizzle(SkString* outAppend, |
| 54 const GrGLShaderBuilder::TextureSampler& texSampler, | 65 const GrGLShaderBuilder::TextureSampler& texSampler, |
| 55 const GrGLCaps& caps) { | 66 const GrGLCaps& caps) { |
| 56 const char* swizzle = texSampler.swizzle(); | 67 const char* swizzle = texSampler.swizzle(); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 69 swizzle = mangledSwizzle; | 80 swizzle = mangledSwizzle; |
| 70 } | 81 } |
| 71 // For shader prettiness we omit the swizzle rather than appending ".rgba". | 82 // For shader prettiness we omit the swizzle rather than appending ".rgba". |
| 72 if (memcmp(swizzle, "rgba", 4)) { | 83 if (memcmp(swizzle, "rgba", 4)) { |
| 73 outAppend->appendf(".%s", swizzle); | 84 outAppend->appendf(".%s", swizzle); |
| 74 } | 85 } |
| 75 } | 86 } |
| 76 | 87 |
| 77 } | 88 } |
| 78 | 89 |
| 90 static const char kDstColorName[] = "_dstColor"; | |
| 91 | |
| 79 /////////////////////////////////////////////////////////////////////////////// | 92 /////////////////////////////////////////////////////////////////////////////// |
| 80 | 93 |
| 81 // Architectural assumption: always 2-d input coords. | |
| 82 // Likely to become non-constant and non-static, perhaps even | |
| 83 // varying by stage, if we use 1D textures for gradients! | |
| 84 //const int GrGLShaderBuilder::fCoordDims = 2; | |
| 85 | |
| 86 GrGLShaderBuilder::GrGLShaderBuilder(const GrGLContextInfo& ctxInfo, | 94 GrGLShaderBuilder::GrGLShaderBuilder(const GrGLContextInfo& ctxInfo, |
| 87 GrGLUniformManager& uniformManager, | 95 GrGLUniformManager& uniformManager, |
| 88 bool explicitLocalCoords) | 96 const GrGLProgramDesc& desc) |
| 89 : fUniforms(kVarsPerBlock) | 97 : fUniforms(kVarsPerBlock) |
| 90 , fVSAttrs(kVarsPerBlock) | 98 , fVSAttrs(kVarsPerBlock) |
| 91 , fVSOutputs(kVarsPerBlock) | 99 , fVSOutputs(kVarsPerBlock) |
| 92 , fGSInputs(kVarsPerBlock) | 100 , fGSInputs(kVarsPerBlock) |
| 93 , fGSOutputs(kVarsPerBlock) | 101 , fGSOutputs(kVarsPerBlock) |
| 94 , fFSInputs(kVarsPerBlock) | 102 , fFSInputs(kVarsPerBlock) |
| 95 , fFSOutputs(kMaxFSOutputs) | 103 , fFSOutputs(kMaxFSOutputs) |
| 104 #if GR_GL_EXPERIMENTAL_GS | |
| 105 , fUsesGS(desc.fExperimentalGS) | |
| 106 #else | |
| 96 , fUsesGS(false) | 107 , fUsesGS(false) |
| 108 #endif | |
| 97 , fCtxInfo(ctxInfo) | 109 , fCtxInfo(ctxInfo) |
| 98 , fUniformManager(uniformManager) | 110 , fUniformManager(uniformManager) |
| 99 , fCurrentStageIdx(kNonStageIdx) | 111 , fCurrentStageIdx(kNonStageIdx) |
| 100 , fSetupFragPosition(false) | 112 , fSetupFragPosition(false) |
| 101 , fRTHeightUniform(GrGLUniformManager::kInvalidUniformHandle) { | 113 , fRTHeightUniform(GrGLUniformManager::kInvalidUniformHandle) |
| 114 , fDstCopyTopLeftUniform (GrGLUniformManager::kInvalidUniformHandle) | |
| 115 , fDstCopyScaleUniform (GrGLUniformManager::kInvalidUniformHandle){ | |
| 102 | 116 |
| 103 fPositionVar = &fVSAttrs.push_back(); | 117 fPositionVar = &fVSAttrs.push_back(); |
| 104 fPositionVar->set(kVec2f_GrSLType, GrGLShaderVar::kAttribute_TypeModifier, " aPosition"); | 118 fPositionVar->set(kVec2f_GrSLType, GrGLShaderVar::kAttribute_TypeModifier, " aPosition"); |
| 105 if (explicitLocalCoords) { | 119 if (desc.fAttribBindings & GrDrawState::kLocalCoords_AttribBindingsBit) { |
| 106 fLocalCoordsVar = &fVSAttrs.push_back(); | 120 fLocalCoordsVar = &fVSAttrs.push_back(); |
| 107 fLocalCoordsVar->set(kVec2f_GrSLType, | 121 fLocalCoordsVar->set(kVec2f_GrSLType, |
| 108 GrGLShaderVar::kAttribute_TypeModifier, | 122 GrGLShaderVar::kAttribute_TypeModifier, |
| 109 "aLocalCoords"); | 123 "aLocalCoords"); |
| 110 } else { | 124 } else { |
| 111 fLocalCoordsVar = fPositionVar; | 125 fLocalCoordsVar = fPositionVar; |
| 112 } | 126 } |
| 127 if (kNoDstRead_DstReadKey != desc.fDstRead) { | |
| 128 bool topDown = SkToBool(kTopLeftOrigin_DstReadKeyBit & desc.fDstRead); | |
| 129 const char* dstCopyTopLeftName; | |
| 130 const char* dstCopyCoordScaleName; | |
| 131 uint32_t configMask = SkToBool(kUseAlphaConfig_DstReadKeyBit & desc.fDst Read) ? | |
|
robertphillips
2013/03/29 18:32:57
align with (?
bsalomon
2013/03/29 18:59:38
Done. (made an if/else)
| |
| 132 kA_GrColorComponentFlag : | |
| 133 kRGBA_GrColorComponentFlags; | |
| 134 fDstCopySampler.init(this, configMask, "rgba", 0); | |
| 135 | |
| 136 fDstCopyTopLeftUniform = this->addUniform(kFragment_ShaderType, | |
|
robertphillips
2013/03/29 18:32:57
align with (?
bsalomon
2013/03/29 18:59:38
Done.
| |
| 137 kVec2f_GrSLType, | |
| 138 "DstCopyUpperLeft", | |
| 139 &dstCopyTopLeftName); | |
| 140 fDstCopyScaleUniform = this->addUniform(kFragment_ShaderType, | |
| 141 kVec2f_GrSLType, | |
| 142 "DstCopyCoordScale", | |
| 143 &dstCopyCoordScaleName); | |
| 144 const char* fragPos = this->fragmentPosition(); | |
| 145 this->fsCodeAppend("\t// Read color from copy of the destination.\n"); | |
| 146 this->fsCodeAppendf("\tvec2 _dstTexCoord = (%s.xy - %s) * %s;\n", | |
| 147 fragPos, dstCopyTopLeftName, dstCopyCoordScaleName); | |
| 148 if (!topDown) { | |
| 149 this->fsCodeAppend("\t_dstTexCoord.y = 1.0 - _dstTexCoord.y;\n"); | |
| 150 } | |
| 151 this->fsCodeAppendf("\tvec4 %s = ", kDstColorName); | |
| 152 this->appendTextureLookup(kFragment_ShaderType, fDstCopySampler, "_dstTe xCoord"); | |
| 153 this->fsCodeAppend(";\n\n"); | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 const char* GrGLShaderBuilder::dstColor() const { | |
| 158 if (fDstCopySampler.isInitialized()) { | |
| 159 return kDstColorName; | |
| 160 } else { | |
| 161 return NULL; | |
| 162 } | |
| 113 } | 163 } |
| 114 | 164 |
| 115 void GrGLShaderBuilder::codeAppendf(ShaderType type, const char format[], va_lis t args) { | 165 void GrGLShaderBuilder::codeAppendf(ShaderType type, const char format[], va_lis t args) { |
| 116 SkString* string = NULL; | 166 SkString* string = NULL; |
| 117 switch (type) { | 167 switch (type) { |
| 118 case kVertex_ShaderType: | 168 case kVertex_ShaderType: |
| 119 string = &fVSCode; | 169 string = &fVSCode; |
| 120 break; | 170 break; |
| 121 case kGeometry_ShaderType: | 171 case kGeometry_ShaderType: |
| 122 string = &fGSCode; | 172 string = &fGSCode; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 177 GrSLType varyingType) { | 227 GrSLType varyingType) { |
| 178 GrAssert(kFragment_ShaderType == type); | 228 GrAssert(kFragment_ShaderType == type); |
| 179 SkString lookup; | 229 SkString lookup; |
| 180 this->appendTextureLookup(&lookup, sampler, coordName, varyingType); | 230 this->appendTextureLookup(&lookup, sampler, coordName, varyingType); |
| 181 GrGLSLModulate4f(&fFSCode, modulation, lookup.c_str()); | 231 GrGLSLModulate4f(&fFSCode, modulation, lookup.c_str()); |
| 182 } | 232 } |
| 183 | 233 |
| 184 GrBackendEffectFactory::EffectKey GrGLShaderBuilder::KeyForTextureAccess( | 234 GrBackendEffectFactory::EffectKey GrGLShaderBuilder::KeyForTextureAccess( |
| 185 const GrTextureAcces s& access, | 235 const GrTextureAcces s& access, |
| 186 const GrGLCaps& caps ) { | 236 const GrGLCaps& caps ) { |
| 187 GrBackendEffectFactory::EffectKey key = 0; | 237 uint32_t configComponentMask = GrPixelConfigComponentMask(access.getTexture( )->config()); |
| 238 if (swizzle_requires_alpha_remapping(caps, configComponentMask, access.swizz leMask())) { | |
| 239 return 1; | |
| 240 } else { | |
| 241 return 0; | |
| 242 } | |
| 243 } | |
| 188 | 244 |
| 189 // Assume that swizzle support implies that we never have to modify a shader to adjust | 245 GrGLShaderBuilder::DstReadKey GrGLShaderBuilder::KeyForDstRead(const GrTexture* dstCopy, |
| 190 // for texture format/swizzle settings. | 246 const GrGLCaps& c aps) { |
| 191 if (!caps.textureSwizzleSupport() && swizzle_requires_alpha_remapping(caps, access)) { | 247 uint32_t key = kYesDstRead_DstReadKeyBit; |
| 192 key = 1; | 248 if (!caps.textureSwizzleSupport() && GrPixelConfigIsAlphaOnly(dstCopy->confi g())) { |
| 249 // The fact that the config is alpha-only must be considered when genera ting code. | |
| 250 key |= kUseAlphaConfig_DstReadKeyBit; | |
| 193 } | 251 } |
| 194 return key; | 252 if (kTopLeft_GrSurfaceOrigin == dstCopy->origin()) { |
| 253 key |= kTopLeftOrigin_DstReadKeyBit; | |
| 254 } | |
| 255 GrAssert(static_cast<DstReadKey>(key) == key); | |
| 256 return static_cast<DstReadKey>(key); | |
| 195 } | 257 } |
| 196 | 258 |
| 197 const GrGLenum* GrGLShaderBuilder::GetTexParamSwizzle(GrPixelConfig config, cons t GrGLCaps& caps) { | 259 const GrGLenum* GrGLShaderBuilder::GetTexParamSwizzle(GrPixelConfig config, cons t GrGLCaps& caps) { |
| 198 if (caps.textureSwizzleSupport() && GrPixelConfigIsAlphaOnly(config)) { | 260 if (caps.textureSwizzleSupport() && GrPixelConfigIsAlphaOnly(config)) { |
| 199 if (caps.textureRedSupport()) { | 261 if (caps.textureRedSupport()) { |
| 200 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED, GR_GL_RE D, GR_GL_RED }; | 262 static const GrGLenum gRedSmear[] = { GR_GL_RED, GR_GL_RED, GR_GL_RE D, GR_GL_RED }; |
| 201 return gRedSmear; | 263 return gRedSmear; |
| 202 } else { | 264 } else { |
| 203 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA, | 265 static const GrGLenum gAlphaSmear[] = { GR_GL_ALPHA, GR_GL_ALPHA, |
| 204 GR_GL_ALPHA, GR_GL_ALPHA }; | 266 GR_GL_ALPHA, GR_GL_ALPHA }; |
| (...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 541 for (const AttributePair* attrib = this->getEffectAttributes().begin(); | 603 for (const AttributePair* attrib = this->getEffectAttributes().begin(); |
| 542 attrib != attribEnd; | 604 attrib != attribEnd; |
| 543 ++attrib) { | 605 ++attrib) { |
| 544 if (attrib->fIndex == attributeIndex) { | 606 if (attrib->fIndex == attributeIndex) { |
| 545 return &attrib->fName; | 607 return &attrib->fName; |
| 546 } | 608 } |
| 547 } | 609 } |
| 548 | 610 |
| 549 return NULL; | 611 return NULL; |
| 550 } | 612 } |
| OLD | NEW |