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