bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 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 | |
| 16 | class GrGLSimpleTextureEffect : public GrGLEffect { |
| 17 | public: |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 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 | } |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 26 | |
| 27 | virtual void emitCode(GrGLShaderBuilder* builder, |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 28 | const GrDrawEffect& drawEffect, |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 29 | EffectKey key, |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 30 | const char* outputColor, |
| 31 | const char* inputColor, |
| 32 | const TextureSamplerArray& samplers) SK_OVERRIDE { |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 33 | const GrSimpleTextureEffect& ste = drawEffect.castEffect<GrSimpleTextureEffect>(); |
| 34 | const char* fsCoordName; |
| 35 | GrSLType fsCoordSLType; |
| 36 | if (GrEffect::kCustom_CoordsType == ste.coordsType()) { |
| 37 | GrAssert(ste.getMatrix().isIdentity()); |
| 38 | GrAssert(1 == ste.numVertexAttribs()); |
| 39 | fsCoordSLType = kVec2f_GrSLType; |
| 40 | const char* vsVaryingName; |
| 41 | builder->addVarying(kVec2f_GrSLType, "textureCoords", &vsVaryingName, &fsCoordName); |
| 42 | const char* attrName = |
| 43 | builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0])->c_str(); |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 44 | builder->vsCodeAppendf("\t%s = %s;\n", vsVaryingName, attrName); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 45 | } else { |
| 46 | fsCoordSLType = fEffectMatrix.get()->emitCode(builder, key, &fsCoordName); |
| 47 | } |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 48 | builder->fsCodeAppendf("\t%s = ", outputColor); |
| 49 | builder->appendTextureLookupAndModulate(GrGLShaderBuilder::kFragment_ShaderType, |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 50 | inputColor, |
| 51 | samplers[0], |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 52 | fsCoordName, |
| 53 | fsCoordSLType); |
bsalomon@google.com | f910d3b | 2013-03-07 17:06:57 +0000 | [diff] [blame] | 54 | builder->fsCodeAppend(";\n"); |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 55 | } |
| 56 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 57 | static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { |
| 58 | const GrSimpleTextureEffect& ste = drawEffect.castEffect<GrSimpleTextureEffect>(); |
| 59 | if (GrEffect::kCustom_CoordsType == ste.coordsType()) { |
| 60 | return 1 << GrGLEffectMatrix::kKeyBits; |
| 61 | } else { |
| 62 | return GrGLEffectMatrix::GenKey(ste.getMatrix(), |
| 63 | drawEffect, |
| 64 | ste.coordsType(), |
| 65 | ste.texture(0)); |
| 66 | } |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 67 | } |
| 68 | |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 69 | virtual void setData(const GrGLUniformManager& uman, |
| 70 | const GrDrawEffect& drawEffect) SK_OVERRIDE { |
| 71 | const GrSimpleTextureEffect& ste = drawEffect.castEffect<GrSimpleTextureEffect>(); |
| 72 | if (GrEffect::kCustom_CoordsType == ste.coordsType()) { |
| 73 | GrAssert(ste.getMatrix().isIdentity()); |
| 74 | } else { |
| 75 | fEffectMatrix.get()->setData(uman, ste.getMatrix(), drawEffect, ste.texture(0)); |
| 76 | } |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | private: |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 80 | SkTLazy<GrGLEffectMatrix> fEffectMatrix; |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 81 | typedef GrGLEffect INHERITED; |
| 82 | }; |
| 83 | |
| 84 | /////////////////////////////////////////////////////////////////////////////// |
| 85 | |
| 86 | void GrSimpleTextureEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |
| 87 | this->updateConstantColorComponentsForModulation(color, validFlags); |
| 88 | } |
| 89 | |
| 90 | const GrBackendEffectFactory& GrSimpleTextureEffect::getFactory() const { |
| 91 | return GrTBackendEffectFactory<GrSimpleTextureEffect>::getInstance(); |
| 92 | } |
| 93 | |
| 94 | /////////////////////////////////////////////////////////////////////////////// |
| 95 | |
| 96 | GR_DEFINE_EFFECT_TEST(GrSimpleTextureEffect); |
| 97 | |
bsalomon@google.com | 73a9694 | 2013-02-13 16:31:19 +0000 | [diff] [blame] | 98 | GrEffectRef* GrSimpleTextureEffect::TestCreate(SkMWCRandom* random, |
sugoi@google.com | e0e385c | 2013-03-11 18:50:03 +0000 | [diff] [blame] | 99 | GrContext*, |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 100 | const GrDrawTargetCaps&, |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 101 | GrTexture* textures[]) { |
| 102 | int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 103 | GrEffectUnitTest::kAlphaTextureIdx; |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 104 | static const SkShader::TileMode kTileModes[] = { |
| 105 | SkShader::kClamp_TileMode, |
| 106 | SkShader::kRepeat_TileMode, |
| 107 | SkShader::kMirror_TileMode, |
| 108 | }; |
| 109 | SkShader::TileMode tileModes[] = { |
| 110 | kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 111 | kTileModes[random->nextULessThan(SK_ARRAY_COUNT(kTileModes))], |
| 112 | }; |
| 113 | GrTextureParams params(tileModes, random->nextBool()); |
| 114 | |
| 115 | static const CoordsType kCoordsTypes[] = { |
| 116 | kLocal_CoordsType, |
| 117 | kPosition_CoordsType, |
| 118 | kCustom_CoordsType |
| 119 | }; |
| 120 | CoordsType coordsType = kCoordsTypes[random->nextULessThan(GR_ARRAY_COUNT(kCoordsTypes))]; |
| 121 | |
| 122 | if (kCustom_CoordsType == coordsType) { |
| 123 | return GrSimpleTextureEffect::CreateWithCustomCoords(textures[texIdx], params); |
| 124 | } else { |
| 125 | const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random); |
| 126 | return GrSimpleTextureEffect::Create(textures[texIdx], matrix); |
| 127 | } |
bsalomon@google.com | 68b58c9 | 2013-01-17 16:50:08 +0000 | [diff] [blame] | 128 | } |