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