michael@0: /* michael@0: * Copyright 2013 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "GrGLProgramEffects.h" michael@0: #include "GrDrawEffect.h" michael@0: #include "gl/GrGLEffect.h" michael@0: #include "gl/GrGLShaderBuilder.h" michael@0: #include "gl/GrGLVertexEffect.h" michael@0: #include "gl/GrGpuGL.h" michael@0: michael@0: typedef GrGLProgramEffects::EffectKey EffectKey; michael@0: typedef GrGLProgramEffects::TransformedCoords TransformedCoords; michael@0: typedef GrGLProgramEffects::TransformedCoordsArray TransformedCoordsArray; michael@0: typedef GrGLProgramEffects::TextureSampler TextureSampler; michael@0: typedef GrGLProgramEffects::TextureSamplerArray TextureSamplerArray; michael@0: michael@0: /** michael@0: * We specialize the vertex code for each of these matrix types. michael@0: */ michael@0: enum MatrixType { michael@0: kIdentity_MatrixType = 0, michael@0: kTrans_MatrixType = 1, michael@0: kNoPersp_MatrixType = 2, michael@0: kGeneral_MatrixType = 3, michael@0: }; michael@0: michael@0: /** michael@0: * The key for an individual coord transform is made up of a matrix type and a bit that michael@0: * indicates the source of the input coords. michael@0: */ michael@0: enum { michael@0: kMatrixTypeKeyBits = 2, michael@0: kMatrixTypeKeyMask = (1 << kMatrixTypeKeyBits) - 1, michael@0: kPositionCoords_Flag = (1 << kMatrixTypeKeyBits), michael@0: kTransformKeyBits = kMatrixTypeKeyBits + 1, michael@0: }; michael@0: michael@0: namespace { michael@0: michael@0: /** michael@0: * Do we need to either map r,g,b->a or a->r. configComponentMask indicates which channels are michael@0: * present in the texture's config. swizzleComponentMask indicates the channels present in the michael@0: * shader swizzle. michael@0: */ michael@0: inline bool swizzle_requires_alpha_remapping(const GrGLCaps& caps, michael@0: uint32_t configComponentMask, michael@0: uint32_t swizzleComponentMask) { michael@0: if (caps.textureSwizzleSupport()) { michael@0: // Any remapping is handled using texture swizzling not shader modifications. michael@0: return false; michael@0: } michael@0: // check if the texture is alpha-only michael@0: if (kA_GrColorComponentFlag == configComponentMask) { michael@0: if (caps.textureRedSupport() && (kA_GrColorComponentFlag & swizzleComponentMask)) { michael@0: // we must map the swizzle 'a's to 'r'. michael@0: return true; michael@0: } michael@0: if (kRGB_GrColorComponentFlags & swizzleComponentMask) { michael@0: // The 'r', 'g', and/or 'b's must be mapped to 'a' according to our semantics that michael@0: // alpha-only textures smear alpha across all four channels when read. michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: /** michael@0: * Retrieves the matrix type from transformKey for the transform at transformIdx. michael@0: */ michael@0: MatrixType get_matrix_type(EffectKey transformKey, int transformIdx) { michael@0: return static_cast( michael@0: (transformKey >> (kTransformKeyBits * transformIdx)) & kMatrixTypeKeyMask); michael@0: } michael@0: michael@0: /** michael@0: * Retrieves the source coords from transformKey for the transform at transformIdx. It may not be michael@0: * the same coordinate set as the original GrCoordTransform if the position and local coords are michael@0: * identical for this program. michael@0: */ michael@0: GrCoordSet get_source_coords(EffectKey transformKey, int transformIdx) { michael@0: return (transformKey >> (kTransformKeyBits * transformIdx)) & kPositionCoords_Flag ? michael@0: kPosition_GrCoordSet : michael@0: kLocal_GrCoordSet; michael@0: } michael@0: michael@0: /** michael@0: * Retrieves the final translation that a transform needs to apply to its source coords (and michael@0: * verifies that a translation is all it needs). michael@0: */ michael@0: void get_transform_translation(const GrDrawEffect& drawEffect, michael@0: int transformIdx, michael@0: GrGLfloat* tx, michael@0: GrGLfloat* ty) { michael@0: const GrCoordTransform& coordTransform = (*drawEffect.effect())->coordTransform(transformIdx); michael@0: SkASSERT(!coordTransform.reverseY()); michael@0: const SkMatrix& matrix = coordTransform.getMatrix(); michael@0: if (kLocal_GrCoordSet == coordTransform.sourceCoords() && michael@0: !drawEffect.programHasExplicitLocalCoords()) { michael@0: const SkMatrix& coordChangeMatrix = drawEffect.getCoordChangeMatrix(); michael@0: SkASSERT(SkMatrix::kTranslate_Mask == (matrix.getType() | coordChangeMatrix.getType())); michael@0: *tx = SkScalarToFloat(matrix[SkMatrix::kMTransX] + coordChangeMatrix[SkMatrix::kMTransX]); michael@0: *ty = SkScalarToFloat(matrix[SkMatrix::kMTransY] + coordChangeMatrix[SkMatrix::kMTransY]); michael@0: } else { michael@0: SkASSERT(SkMatrix::kTranslate_Mask == matrix.getType()); michael@0: *tx = SkScalarToFloat(matrix[SkMatrix::kMTransX]); michael@0: *ty = SkScalarToFloat(matrix[SkMatrix::kMTransY]); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Retrieves the final matrix that a transform needs to apply to its source coords. michael@0: */ michael@0: SkMatrix get_transform_matrix(const GrDrawEffect& drawEffect, int transformIdx) { michael@0: const GrCoordTransform& coordTransform = (*drawEffect.effect())->coordTransform(transformIdx); michael@0: SkMatrix combined; michael@0: if (kLocal_GrCoordSet == coordTransform.sourceCoords() && michael@0: !drawEffect.programHasExplicitLocalCoords()) { michael@0: combined.setConcat(coordTransform.getMatrix(), drawEffect.getCoordChangeMatrix()); michael@0: } else { michael@0: combined = coordTransform.getMatrix(); michael@0: } michael@0: if (coordTransform.reverseY()) { michael@0: // combined.postScale(1,-1); michael@0: // combined.postTranslate(0,1); michael@0: combined.set(SkMatrix::kMSkewY, michael@0: combined[SkMatrix::kMPersp0] - combined[SkMatrix::kMSkewY]); michael@0: combined.set(SkMatrix::kMScaleY, michael@0: combined[SkMatrix::kMPersp1] - combined[SkMatrix::kMScaleY]); michael@0: combined.set(SkMatrix::kMTransY, michael@0: combined[SkMatrix::kMPersp2] - combined[SkMatrix::kMTransY]); michael@0: } michael@0: return combined; michael@0: } michael@0: michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: EffectKey GrGLProgramEffects::GenAttribKey(const GrDrawEffect& drawEffect) { michael@0: EffectKey key = 0; michael@0: int numAttributes = drawEffect.getVertexAttribIndexCount(); michael@0: SkASSERT(numAttributes <= 2); michael@0: const int* attributeIndices = drawEffect.getVertexAttribIndices(); michael@0: for (int a = 0; a < numAttributes; ++a) { michael@0: EffectKey value = attributeIndices[a] << 3 * a; michael@0: SkASSERT(0 == (value & key)); // keys for each attribute ought not to overlap michael@0: key |= value; michael@0: } michael@0: return key; michael@0: } michael@0: michael@0: EffectKey GrGLProgramEffects::GenTransformKey(const GrDrawEffect& drawEffect) { michael@0: EffectKey totalKey = 0; michael@0: int numTransforms = (*drawEffect.effect())->numTransforms(); michael@0: for (int t = 0; t < numTransforms; ++t) { michael@0: EffectKey key = 0; michael@0: const GrCoordTransform& coordTransform = (*drawEffect.effect())->coordTransform(t); michael@0: SkMatrix::TypeMask type0 = coordTransform.getMatrix().getType(); michael@0: SkMatrix::TypeMask type1; michael@0: if (kLocal_GrCoordSet == coordTransform.sourceCoords()) { michael@0: type1 = drawEffect.getCoordChangeMatrix().getType(); michael@0: } else { michael@0: if (drawEffect.programHasExplicitLocalCoords()) { michael@0: // We only make the key indicate that device coords are referenced when the local coords michael@0: // are not actually determined by positions. Otherwise the local coords var and position michael@0: // var are identical. michael@0: key |= kPositionCoords_Flag; michael@0: } michael@0: type1 = SkMatrix::kIdentity_Mask; michael@0: } michael@0: michael@0: int combinedTypes = type0 | type1; michael@0: michael@0: bool reverseY = coordTransform.reverseY(); michael@0: michael@0: if (SkMatrix::kPerspective_Mask & combinedTypes) { michael@0: key |= kGeneral_MatrixType; michael@0: } else if (((SkMatrix::kAffine_Mask | SkMatrix::kScale_Mask) & combinedTypes) || reverseY) { michael@0: key |= kNoPersp_MatrixType; michael@0: } else if (SkMatrix::kTranslate_Mask & combinedTypes) { michael@0: key |= kTrans_MatrixType; michael@0: } else { michael@0: key |= kIdentity_MatrixType; michael@0: } michael@0: key <<= kTransformKeyBits * t; michael@0: SkASSERT(0 == (totalKey & key)); // keys for each transform ought not to overlap michael@0: totalKey |= key; michael@0: } michael@0: return totalKey; michael@0: } michael@0: michael@0: EffectKey GrGLProgramEffects::GenTextureKey(const GrDrawEffect& drawEffect, const GrGLCaps& caps) { michael@0: EffectKey key = 0; michael@0: int numTextures = (*drawEffect.effect())->numTextures(); michael@0: for (int t = 0; t < numTextures; ++t) { michael@0: const GrTextureAccess& access = (*drawEffect.effect())->textureAccess(t); michael@0: uint32_t configComponentMask = GrPixelConfigComponentMask(access.getTexture()->config()); michael@0: if (swizzle_requires_alpha_remapping(caps, configComponentMask, access.swizzleMask())) { michael@0: key |= 1 << t; michael@0: } michael@0: } michael@0: return key; michael@0: } michael@0: michael@0: GrGLProgramEffects::~GrGLProgramEffects() { michael@0: int numEffects = fGLEffects.count(); michael@0: for (int e = 0; e < numEffects; ++e) { michael@0: SkDELETE(fGLEffects[e]); michael@0: } michael@0: } michael@0: michael@0: void GrGLProgramEffects::emitSamplers(GrGLShaderBuilder* builder, michael@0: const GrEffectRef& effect, michael@0: TextureSamplerArray* outSamplers) { michael@0: SkTArray& samplers = fSamplers.push_back(); michael@0: int numTextures = effect->numTextures(); michael@0: samplers.push_back_n(numTextures); michael@0: SkString name; michael@0: for (int t = 0; t < numTextures; ++t) { michael@0: name.printf("Sampler%d", t); michael@0: samplers[t].fUniform = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, michael@0: kSampler2D_GrSLType, michael@0: name.c_str()); michael@0: SkNEW_APPEND_TO_TARRAY(outSamplers, TextureSampler, michael@0: (samplers[t].fUniform, effect->textureAccess(t))); michael@0: } michael@0: } michael@0: michael@0: void GrGLProgramEffects::initSamplers(const GrGLUniformManager& uniformManager, int* texUnitIdx) { michael@0: int numEffects = fGLEffects.count(); michael@0: SkASSERT(numEffects == fSamplers.count()); michael@0: for (int e = 0; e < numEffects; ++e) { michael@0: SkTArray& samplers = fSamplers[e]; michael@0: int numSamplers = samplers.count(); michael@0: for (int s = 0; s < numSamplers; ++s) { michael@0: SkASSERT(samplers[s].fUniform.isValid()); michael@0: uniformManager.setSampler(samplers[s].fUniform, *texUnitIdx); michael@0: samplers[s].fTextureUnit = (*texUnitIdx)++; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void GrGLProgramEffects::bindTextures(GrGpuGL* gpu, const GrEffectRef& effect, int effectIdx) { michael@0: const SkTArray& samplers = fSamplers[effectIdx]; michael@0: int numSamplers = samplers.count(); michael@0: SkASSERT(numSamplers == effect->numTextures()); michael@0: for (int s = 0; s < numSamplers; ++s) { michael@0: SkASSERT(samplers[s].fTextureUnit >= 0); michael@0: const GrTextureAccess& textureAccess = effect->textureAccess(s); michael@0: gpu->bindTexture(samplers[s].fTextureUnit, michael@0: textureAccess.getParams(), michael@0: static_cast(textureAccess.getTexture())); michael@0: } michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void GrGLVertexProgramEffects::emitEffect(GrGLFullShaderBuilder* builder, michael@0: const GrEffectStage& stage, michael@0: EffectKey key, michael@0: const char* outColor, michael@0: const char* inColor, michael@0: int stageIndex) { michael@0: GrDrawEffect drawEffect(stage, fHasExplicitLocalCoords); michael@0: const GrEffectRef& effect = *stage.getEffect(); michael@0: SkSTArray<2, TransformedCoords> coords(effect->numTransforms()); michael@0: SkSTArray<4, TextureSampler> samplers(effect->numTextures()); michael@0: michael@0: this->emitAttributes(builder, stage); michael@0: this->emitTransforms(builder, effect, key, &coords); michael@0: this->emitSamplers(builder, effect, &samplers); michael@0: michael@0: GrGLEffect* glEffect = effect->getFactory().createGLInstance(drawEffect); michael@0: fGLEffects.push_back(glEffect); michael@0: michael@0: // Enclose custom code in a block to avoid namespace conflicts michael@0: SkString openBrace; michael@0: openBrace.printf("\t{ // Stage %d: %s\n", stageIndex, glEffect->name()); michael@0: builder->vsCodeAppend(openBrace.c_str()); michael@0: builder->fsCodeAppend(openBrace.c_str()); michael@0: michael@0: if (glEffect->isVertexEffect()) { michael@0: GrGLVertexEffect* vertexEffect = static_cast(glEffect); michael@0: vertexEffect->emitCode(builder, drawEffect, key, outColor, inColor, coords, samplers); michael@0: } else { michael@0: glEffect->emitCode(builder, drawEffect, key, outColor, inColor, coords, samplers); michael@0: } michael@0: michael@0: builder->vsCodeAppend("\t}\n"); michael@0: builder->fsCodeAppend("\t}\n"); michael@0: } michael@0: michael@0: void GrGLVertexProgramEffects::emitAttributes(GrGLFullShaderBuilder* builder, michael@0: const GrEffectStage& stage) { michael@0: int numAttributes = stage.getVertexAttribIndexCount(); michael@0: const int* attributeIndices = stage.getVertexAttribIndices(); michael@0: for (int a = 0; a < numAttributes; ++a) { michael@0: // TODO: Make addAttribute mangle the name. michael@0: SkString attributeName("aAttr"); michael@0: attributeName.appendS32(attributeIndices[a]); michael@0: builder->addEffectAttribute(attributeIndices[a], michael@0: (*stage.getEffect())->vertexAttribType(a), michael@0: attributeName); michael@0: } michael@0: } michael@0: michael@0: void GrGLVertexProgramEffects::emitTransforms(GrGLFullShaderBuilder* builder, michael@0: const GrEffectRef& effect, michael@0: EffectKey effectKey, michael@0: TransformedCoordsArray* outCoords) { michael@0: SkTArray& transforms = fTransforms.push_back(); michael@0: EffectKey totalKey = GrBackendEffectFactory::GetTransformKey(effectKey); michael@0: int numTransforms = effect->numTransforms(); michael@0: transforms.push_back_n(numTransforms); michael@0: for (int t = 0; t < numTransforms; t++) { michael@0: GrSLType varyingType = kVoid_GrSLType; michael@0: const char* uniName; michael@0: switch (get_matrix_type(totalKey, t)) { michael@0: case kIdentity_MatrixType: michael@0: transforms[t].fType = kVoid_GrSLType; michael@0: uniName = NULL; michael@0: varyingType = kVec2f_GrSLType; michael@0: break; michael@0: case kTrans_MatrixType: michael@0: transforms[t].fType = kVec2f_GrSLType; michael@0: uniName = "StageTranslate"; michael@0: varyingType = kVec2f_GrSLType; michael@0: break; michael@0: case kNoPersp_MatrixType: michael@0: transforms[t].fType = kMat33f_GrSLType; michael@0: uniName = "StageMatrix"; michael@0: varyingType = kVec2f_GrSLType; michael@0: break; michael@0: case kGeneral_MatrixType: michael@0: transforms[t].fType = kMat33f_GrSLType; michael@0: uniName = "StageMatrix"; michael@0: varyingType = kVec3f_GrSLType; michael@0: break; michael@0: default: michael@0: GrCrash("Unexpected key."); michael@0: } michael@0: SkString suffixedUniName; michael@0: if (kVoid_GrSLType != transforms[t].fType) { michael@0: if (0 != t) { michael@0: suffixedUniName.append(uniName); michael@0: suffixedUniName.appendf("_%i", t); michael@0: uniName = suffixedUniName.c_str(); michael@0: } michael@0: transforms[t].fHandle = builder->addUniform(GrGLShaderBuilder::kVertex_Visibility, michael@0: transforms[t].fType, michael@0: uniName, michael@0: &uniName); michael@0: } michael@0: michael@0: const char* varyingName = "MatrixCoord"; michael@0: SkString suffixedVaryingName; michael@0: if (0 != t) { michael@0: suffixedVaryingName.append(varyingName); michael@0: suffixedVaryingName.appendf("_%i", t); michael@0: varyingName = suffixedVaryingName.c_str(); michael@0: } michael@0: const char* vsVaryingName; michael@0: const char* fsVaryingName; michael@0: builder->addVarying(varyingType, varyingName, &vsVaryingName, &fsVaryingName); michael@0: michael@0: const GrGLShaderVar& coords = kPosition_GrCoordSet == get_source_coords(totalKey, t) ? michael@0: builder->positionAttribute() : michael@0: builder->localCoordsAttribute(); michael@0: // varying = matrix * coords (logically) michael@0: switch (transforms[t].fType) { michael@0: case kVoid_GrSLType: michael@0: SkASSERT(kVec2f_GrSLType == varyingType); michael@0: builder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, coords.c_str()); michael@0: break; michael@0: case kVec2f_GrSLType: michael@0: SkASSERT(kVec2f_GrSLType == varyingType); michael@0: builder->vsCodeAppendf("\t%s = %s + %s;\n", michael@0: vsVaryingName, uniName, coords.c_str()); michael@0: break; michael@0: case kMat33f_GrSLType: { michael@0: SkASSERT(kVec2f_GrSLType == varyingType || kVec3f_GrSLType == varyingType); michael@0: if (kVec2f_GrSLType == varyingType) { michael@0: builder->vsCodeAppendf("\t%s = (%s * vec3(%s, 1)).xy;\n", michael@0: vsVaryingName, uniName, coords.c_str()); michael@0: } else { michael@0: builder->vsCodeAppendf("\t%s = %s * vec3(%s, 1);\n", michael@0: vsVaryingName, uniName, coords.c_str()); michael@0: } michael@0: break; michael@0: } michael@0: default: michael@0: GrCrash("Unexpected uniform type."); michael@0: } michael@0: SkNEW_APPEND_TO_TARRAY(outCoords, TransformedCoords, michael@0: (SkString(fsVaryingName), varyingType)); michael@0: } michael@0: } michael@0: michael@0: void GrGLVertexProgramEffects::setData(GrGpuGL* gpu, michael@0: const GrGLUniformManager& uniformManager, michael@0: const GrEffectStage* effectStages[]) { michael@0: int numEffects = fGLEffects.count(); michael@0: SkASSERT(numEffects == fTransforms.count()); michael@0: SkASSERT(numEffects == fSamplers.count()); michael@0: for (int e = 0; e < numEffects; ++e) { michael@0: GrDrawEffect drawEffect(*effectStages[e], fHasExplicitLocalCoords); michael@0: fGLEffects[e]->setData(uniformManager, drawEffect); michael@0: this->setTransformData(uniformManager, drawEffect, e); michael@0: this->bindTextures(gpu, *drawEffect.effect(), e); michael@0: } michael@0: } michael@0: michael@0: void GrGLVertexProgramEffects::setTransformData(const GrGLUniformManager& uniformManager, michael@0: const GrDrawEffect& drawEffect, michael@0: int effectIdx) { michael@0: SkTArray& transforms = fTransforms[effectIdx]; michael@0: int numTransforms = transforms.count(); michael@0: SkASSERT(numTransforms == (*drawEffect.effect())->numTransforms()); michael@0: for (int t = 0; t < numTransforms; ++t) { michael@0: SkASSERT(transforms[t].fHandle.isValid() != (kVoid_GrSLType == transforms[t].fType)); michael@0: switch (transforms[t].fType) { michael@0: case kVoid_GrSLType: michael@0: SkASSERT(get_transform_matrix(drawEffect, t).isIdentity()); michael@0: return; michael@0: case kVec2f_GrSLType: { michael@0: GrGLfloat tx, ty; michael@0: get_transform_translation(drawEffect, t, &tx, &ty); michael@0: if (transforms[t].fCurrentValue.get(SkMatrix::kMTransX) != tx || michael@0: transforms[t].fCurrentValue.get(SkMatrix::kMTransY) != ty) { michael@0: uniformManager.set2f(transforms[t].fHandle, tx, ty); michael@0: transforms[t].fCurrentValue.set(SkMatrix::kMTransX, tx); michael@0: transforms[t].fCurrentValue.set(SkMatrix::kMTransY, ty); michael@0: } michael@0: break; michael@0: } michael@0: case kMat33f_GrSLType: { michael@0: const SkMatrix& matrix = get_transform_matrix(drawEffect, t); michael@0: if (!transforms[t].fCurrentValue.cheapEqualTo(matrix)) { michael@0: uniformManager.setSkMatrix(transforms[t].fHandle, matrix); michael@0: transforms[t].fCurrentValue = matrix; michael@0: } michael@0: break; michael@0: } michael@0: default: michael@0: GrCrash("Unexpected uniform type."); michael@0: } michael@0: } michael@0: } michael@0: michael@0: GrGLVertexProgramEffectsBuilder::GrGLVertexProgramEffectsBuilder(GrGLFullShaderBuilder* builder, michael@0: int reserveCount) michael@0: : fBuilder(builder) michael@0: , fProgramEffects(SkNEW_ARGS(GrGLVertexProgramEffects, michael@0: (reserveCount, fBuilder->hasExplicitLocalCoords()))) { michael@0: } michael@0: michael@0: void GrGLVertexProgramEffectsBuilder::emitEffect(const GrEffectStage& stage, michael@0: GrGLProgramEffects::EffectKey key, michael@0: const char* outColor, michael@0: const char* inColor, michael@0: int stageIndex) { michael@0: SkASSERT(NULL != fProgramEffects.get()); michael@0: fProgramEffects->emitEffect(fBuilder, stage, key, outColor, inColor, stageIndex); michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: void GrGLTexGenProgramEffects::emitEffect(GrGLFragmentOnlyShaderBuilder* builder, michael@0: const GrEffectStage& stage, michael@0: EffectKey key, michael@0: const char* outColor, michael@0: const char* inColor, michael@0: int stageIndex) { michael@0: GrDrawEffect drawEffect(stage, false); michael@0: const GrEffectRef& effect = *stage.getEffect(); michael@0: SkSTArray<2, TransformedCoords> coords(effect->numTransforms()); michael@0: SkSTArray<4, TextureSampler> samplers(effect->numTextures()); michael@0: michael@0: SkASSERT(0 == stage.getVertexAttribIndexCount()); michael@0: this->setupTexGen(builder, effect, key, &coords); michael@0: this->emitSamplers(builder, effect, &samplers); michael@0: michael@0: GrGLEffect* glEffect = effect->getFactory().createGLInstance(drawEffect); michael@0: fGLEffects.push_back(glEffect); michael@0: michael@0: // Enclose custom code in a block to avoid namespace conflicts michael@0: SkString openBrace; michael@0: openBrace.printf("\t{ // Stage %d: %s\n", stageIndex, glEffect->name()); michael@0: builder->fsCodeAppend(openBrace.c_str()); michael@0: michael@0: SkASSERT(!glEffect->isVertexEffect()); michael@0: glEffect->emitCode(builder, drawEffect, key, outColor, inColor, coords, samplers); michael@0: michael@0: builder->fsCodeAppend("\t}\n"); michael@0: } michael@0: michael@0: void GrGLTexGenProgramEffects::setupTexGen(GrGLFragmentOnlyShaderBuilder* builder, michael@0: const GrEffectRef& effect, michael@0: EffectKey effectKey, michael@0: TransformedCoordsArray* outCoords) { michael@0: int numTransforms = effect->numTransforms(); michael@0: EffectKey totalKey = GrBackendEffectFactory::GetTransformKey(effectKey); michael@0: int texCoordIndex = builder->addTexCoordSets(numTransforms); michael@0: SkNEW_APPEND_TO_TARRAY(&fTransforms, Transforms, (totalKey, texCoordIndex)); michael@0: SkString name; michael@0: for (int t = 0; t < numTransforms; ++t) { michael@0: GrSLType type = kGeneral_MatrixType == get_matrix_type(totalKey, t) ? michael@0: kVec3f_GrSLType : michael@0: kVec2f_GrSLType; michael@0: name.printf("%s(gl_TexCoord[%i])", GrGLSLTypeString(type), texCoordIndex++); michael@0: SkNEW_APPEND_TO_TARRAY(outCoords, TransformedCoords, (name, type)); michael@0: } michael@0: } michael@0: michael@0: void GrGLTexGenProgramEffects::setData(GrGpuGL* gpu, michael@0: const GrGLUniformManager& uniformManager, michael@0: const GrEffectStage* effectStages[]) { michael@0: int numEffects = fGLEffects.count(); michael@0: SkASSERT(numEffects == fTransforms.count()); michael@0: SkASSERT(numEffects == fSamplers.count()); michael@0: for (int e = 0; e < numEffects; ++e) { michael@0: GrDrawEffect drawEffect(*effectStages[e], false); michael@0: fGLEffects[e]->setData(uniformManager, drawEffect); michael@0: this->setTexGenState(gpu, drawEffect, e); michael@0: this->bindTextures(gpu, *drawEffect.effect(), e); michael@0: } michael@0: } michael@0: michael@0: void GrGLTexGenProgramEffects::setTexGenState(GrGpuGL* gpu, michael@0: const GrDrawEffect& drawEffect, michael@0: int effectIdx) { michael@0: EffectKey totalKey = fTransforms[effectIdx].fTransformKey; michael@0: int texCoordIndex = fTransforms[effectIdx].fTexCoordIndex; michael@0: int numTransforms = (*drawEffect.effect())->numTransforms(); michael@0: for (int t = 0; t < numTransforms; ++t) { michael@0: switch (get_matrix_type(totalKey, t)) { michael@0: case kIdentity_MatrixType: { michael@0: SkASSERT(get_transform_matrix(drawEffect, t).isIdentity()); michael@0: GrGLfloat identity[] = {1, 0, 0, michael@0: 0, 1, 0}; michael@0: gpu->enableTexGen(texCoordIndex++, GrGpuGL::kST_TexGenComponents, identity); michael@0: break; michael@0: } michael@0: case kTrans_MatrixType: { michael@0: GrGLfloat tx, ty; michael@0: get_transform_translation(drawEffect, t, &tx, &ty); michael@0: GrGLfloat translate[] = {1, 0, tx, michael@0: 0, 1, ty}; michael@0: gpu->enableTexGen(texCoordIndex++, GrGpuGL::kST_TexGenComponents, translate); michael@0: break; michael@0: } michael@0: case kNoPersp_MatrixType: { michael@0: const SkMatrix& transform = get_transform_matrix(drawEffect, t); michael@0: gpu->enableTexGen(texCoordIndex++, GrGpuGL::kST_TexGenComponents, transform); michael@0: break; michael@0: } michael@0: case kGeneral_MatrixType: { michael@0: const SkMatrix& transform = get_transform_matrix(drawEffect, t); michael@0: gpu->enableTexGen(texCoordIndex++, GrGpuGL::kSTR_TexGenComponents, transform); michael@0: break; michael@0: } michael@0: default: michael@0: GrCrash("Unexpected matrixs type."); michael@0: } michael@0: } michael@0: } michael@0: michael@0: GrGLTexGenProgramEffectsBuilder::GrGLTexGenProgramEffectsBuilder( michael@0: GrGLFragmentOnlyShaderBuilder* builder, michael@0: int reserveCount) michael@0: : fBuilder(builder) michael@0: , fProgramEffects(SkNEW_ARGS(GrGLTexGenProgramEffects, (reserveCount))) { michael@0: } michael@0: michael@0: void GrGLTexGenProgramEffectsBuilder::emitEffect(const GrEffectStage& stage, michael@0: GrGLProgramEffects::EffectKey key, michael@0: const char* outColor, michael@0: const char* inColor, michael@0: int stageIndex) { michael@0: SkASSERT(NULL != fProgramEffects.get()); michael@0: fProgramEffects->emitEffect(fBuilder, stage, key, outColor, inColor, stageIndex); michael@0: }