GrSimpleTextureEffect.cpp revision 74a3a2135ca82ab9324b7e499caa3280348a4fda
1/* 2 * Copyright 2012 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8#include "GrSimpleTextureEffect.h" 9#include "gl/GrGLEffect.h" 10#include "gl/GrGLEffectMatrix.h" 11#include "gl/GrGLSL.h" 12#include "gl/GrGLTexture.h" 13#include "GrTBackendEffectFactory.h" 14#include "GrTexture.h" 15 16class GrGLSimpleTextureEffect : public GrGLEffect { 17public: 18 GrGLSimpleTextureEffect(const GrBackendEffectFactory& factory, const GrDrawEffect& drawEffect) 19 : INHERITED (factory) { 20 GrEffect::CoordsType coordsType = 21 drawEffect.castEffect<GrSimpleTextureEffect>().coordsType(); 22 if (GrEffect::kCustom_CoordsType != coordsType) { 23 SkNEW_IN_TLAZY(&fEffectMatrix, GrGLEffectMatrix, (coordsType)); 24 } 25 } 26 27 virtual void emitCode(GrGLShaderBuilder* builder, 28 const GrDrawEffect& drawEffect, 29 EffectKey key, 30 const char* outputColor, 31 const char* inputColor, 32 const TextureSamplerArray& samplers) SK_OVERRIDE { 33 const GrSimpleTextureEffect& ste = drawEffect.castEffect<GrSimpleTextureEffect>(); 34 SkString fsCoordName; 35 GrSLType fsCoordSLType; 36 if (GrEffect::kCustom_CoordsType == ste.coordsType()) { 37 SkASSERT(ste.getMatrix().isIdentity()); 38 SkASSERT(1 == ste.numVertexAttribs()); 39 fsCoordSLType = kVec2f_GrSLType; 40 const char* vsVaryingName; 41 const char* fsVaryingNamePtr; 42 builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsVaryingName, &fsVaryingNamePtr); 43 fsCoordName = fsVaryingNamePtr; 44 const char* attrName = 45 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0])->c_str(); 46 builder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, attrName); 47 } else { 48 fsCoordSLType = fEffectMatrix.get()->emitCode(builder, key, &fsCoordName); 49 } 50 builder->fsCodeAppendf("\t%s = ", outputColor); 51 builder->fsAppendTextureLookupAndModulate(inputColor, 52 samplers[0], 53 fsCoordName.c_str(), 54 fsCoordSLType); 55 builder->fsCodeAppend(";\n"); 56 } 57 58 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { 59 const GrSimpleTextureEffect& ste = drawEffect.castEffect<GrSimpleTextureEffect>(); 60 if (GrEffect::kCustom_CoordsType == ste.coordsType()) { 61 return 1 << GrGLEffectMatrix::kKeyBits; 62 } else { 63 return GrGLEffectMatrix::GenKey(ste.getMatrix(), 64 drawEffect, 65 ste.coordsType(), 66 ste.texture(0)); 67 } 68 } 69 70 virtual void setData(const GrGLUniformManager& uman, 71 const GrDrawEffect& drawEffect) SK_OVERRIDE { 72 const GrSimpleTextureEffect& ste = drawEffect.castEffect<GrSimpleTextureEffect>(); 73 if (GrEffect::kCustom_CoordsType == ste.coordsType()) { 74 SkASSERT(ste.getMatrix().isIdentity()); 75 } else { 76 fEffectMatrix.get()->setData(uman, ste.getMatrix(), drawEffect, ste.texture(0)); 77 } 78 } 79 80private: 81 SkTLazy<GrGLEffectMatrix> fEffectMatrix; 82 typedef GrGLEffect INHERITED; 83}; 84 85/////////////////////////////////////////////////////////////////////////////// 86 87void GrSimpleTextureEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { 88 this->updateConstantColorComponentsForModulation(color, validFlags); 89} 90 91const GrBackendEffectFactory& GrSimpleTextureEffect::getFactory() const { 92 return GrTBackendEffectFactory<GrSimpleTextureEffect>::getInstance(); 93} 94 95/////////////////////////////////////////////////////////////////////////////// 96 97GR_DEFINE_EFFECT_TEST(GrSimpleTextureEffect); 98 99GrEffectRef* GrSimpleTextureEffect::TestCreate(SkMWCRandom* random, 100 GrContext*, 101 const GrDrawTargetCaps&, 102 GrTexture* textures[]) { 103 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : 104 GrEffectUnitTest::kAlphaTextureIdx; 105 static const SkShader::TileMode kTileModes[] = { 106 SkShader::kClamp_TileMode, 107 SkShader::kRepeat_TileMode, 108 SkShader::kMirror_TileMode, 109 }; 110 SkShader::TileMode tileModes[] = { 111 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], 112 kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], 113 }; 114 GrTextureParams params(tileModes, random->nextBool() ? GrTextureParams::kBilerp_FilterMode : 115 GrTextureParams::kNone_FilterMode); 116 117 static const CoordsType kCoordsTypes[] = { 118 kLocal_CoordsType, 119 kPosition_CoordsType, 120 kCustom_CoordsType 121 }; 122 CoordsType coordsType = kCoordsTypes[random->nextULessThan(GR_ARRAY_COUNT(kCoordsTypes))]; 123 124 if (kCustom_CoordsType == coordsType) { 125 return GrSimpleTextureEffect::CreateWithCustomCoords(textures[texIdx], params); 126 } else { 127 const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random); 128 return GrSimpleTextureEffect::Create(textures[texIdx], matrix); 129 } 130} 131