tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +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 "effects/GrSingleTextureEffect.h" |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 9 | #include "gl/GrGLEffect.h" |
bsalomon@google.com | ce49e16 | 2012-11-01 13:50:50 +0000 | [diff] [blame^] | 10 | #include "gl/GrGLEffectMatrix.h" |
tomhudson@google.com | aa72eab | 2012-07-19 18:01:07 +0000 | [diff] [blame] | 11 | #include "gl/GrGLSL.h" |
| 12 | #include "gl/GrGLTexture.h" |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 13 | #include "GrTBackendEffectFactory.h" |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 14 | #include "GrTexture.h" |
| 15 | |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 16 | class GrGLSingleTextureEffect : public GrGLEffect { |
tomhudson@google.com | aa72eab | 2012-07-19 18:01:07 +0000 | [diff] [blame] | 17 | public: |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 18 | GrGLSingleTextureEffect(const GrBackendEffectFactory& factory, const GrEffect&) |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 19 | : INHERITED (factory) { |
bsalomon@google.com | ce49e16 | 2012-11-01 13:50:50 +0000 | [diff] [blame^] | 20 | fRequiresTextureMatrix = false; |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 21 | } |
tomhudson@google.com | aa72eab | 2012-07-19 18:01:07 +0000 | [diff] [blame] | 22 | |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 23 | virtual void emitCode(GrGLShaderBuilder* builder, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 24 | const GrEffectStage&, |
bsalomon@google.com | ce49e16 | 2012-11-01 13:50:50 +0000 | [diff] [blame^] | 25 | EffectKey key, |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 26 | const char* vertexCoords, |
| 27 | const char* outputColor, |
| 28 | const char* inputColor, |
| 29 | const TextureSamplerArray& samplers) SK_OVERRIDE { |
bsalomon@google.com | ce49e16 | 2012-11-01 13:50:50 +0000 | [diff] [blame^] | 30 | const char* coordName; |
| 31 | GrSLType coordType = fEffectMatrix.emitCode(builder, key, vertexCoords, &coordName); |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 32 | builder->fFSCode.appendf("\t%s = ", outputColor); |
bsalomon@google.com | ce49e16 | 2012-11-01 13:50:50 +0000 | [diff] [blame^] | 33 | builder->appendTextureLookupAndModulate(&builder->fFSCode, |
| 34 | inputColor, |
| 35 | samplers[0], |
| 36 | coordName, |
| 37 | coordType); |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 38 | builder->fFSCode.append(";\n"); |
tomhudson@google.com | aa72eab | 2012-07-19 18:01:07 +0000 | [diff] [blame] | 39 | } |
| 40 | |
bsalomon@google.com | ce49e16 | 2012-11-01 13:50:50 +0000 | [diff] [blame^] | 41 | static inline EffectKey GenKey(const GrEffectStage& stage, const GrGLCaps&) { |
| 42 | const GrSingleTextureEffect& ste = |
| 43 | static_cast<const GrSingleTextureEffect&>(*stage.getEffect()); |
| 44 | return GrGLEffectMatrix::GenKey(ste.getMatrix(), |
| 45 | stage.getCoordChangeMatrix(), |
| 46 | ste.texture(0)); |
| 47 | } |
| 48 | |
| 49 | virtual void setData(const GrGLUniformManager& uman, const GrEffectStage& stage) SK_OVERRIDE { |
| 50 | const GrSingleTextureEffect& ste = |
| 51 | static_cast<const GrSingleTextureEffect&>(*stage.getEffect()); |
| 52 | fEffectMatrix.setData(uman, ste.getMatrix(), stage.getCoordChangeMatrix(), ste.texture(0)); |
| 53 | } |
tomhudson@google.com | aa72eab | 2012-07-19 18:01:07 +0000 | [diff] [blame] | 54 | |
| 55 | private: |
bsalomon@google.com | ce49e16 | 2012-11-01 13:50:50 +0000 | [diff] [blame^] | 56 | GrGLEffectMatrix fEffectMatrix; |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 57 | typedef GrGLEffect INHERITED; |
tomhudson@google.com | aa72eab | 2012-07-19 18:01:07 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | /////////////////////////////////////////////////////////////////////////////// |
| 61 | |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 62 | GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture) |
bsalomon@google.com | e6e62d1 | 2012-10-04 14:38:48 +0000 | [diff] [blame] | 63 | : INHERITED(1) |
| 64 | , fTextureAccess(texture) { |
bsalomon@google.com | ce49e16 | 2012-11-01 13:50:50 +0000 | [diff] [blame^] | 65 | fMatrix.reset(); |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 66 | } |
| 67 | |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 68 | GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture, bool bilerp) |
bsalomon@google.com | e6e62d1 | 2012-10-04 14:38:48 +0000 | [diff] [blame] | 69 | : INHERITED(1) |
| 70 | , fTextureAccess(texture, bilerp) { |
bsalomon@google.com | ce49e16 | 2012-11-01 13:50:50 +0000 | [diff] [blame^] | 71 | fMatrix.reset(); |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture, const GrTextureParams& params) |
bsalomon@google.com | e6e62d1 | 2012-10-04 14:38:48 +0000 | [diff] [blame] | 75 | : INHERITED(1) |
| 76 | , fTextureAccess(texture, params) { |
bsalomon@google.com | ce49e16 | 2012-11-01 13:50:50 +0000 | [diff] [blame^] | 77 | fMatrix.reset(); |
| 78 | } |
| 79 | |
| 80 | GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture, const GrMatrix& m) |
| 81 | : INHERITED(1) |
| 82 | , fTextureAccess(texture) |
| 83 | , fMatrix(m) { |
| 84 | } |
| 85 | |
| 86 | GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture, const GrMatrix& m, bool bilerp) |
| 87 | : INHERITED(1) |
| 88 | , fTextureAccess(texture, bilerp) |
| 89 | , fMatrix(m) { |
| 90 | } |
| 91 | |
| 92 | GrSingleTextureEffect::GrSingleTextureEffect(GrTexture* texture, |
| 93 | const GrMatrix& m, |
| 94 | const GrTextureParams& params) |
| 95 | : INHERITED(1) |
| 96 | , fTextureAccess(texture, params) |
| 97 | , fMatrix(m) { |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 98 | } |
| 99 | |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 100 | GrSingleTextureEffect::~GrSingleTextureEffect() { |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 101 | } |
| 102 | |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 103 | const GrTextureAccess& GrSingleTextureEffect::textureAccess(int index) const { |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 104 | GrAssert(0 == index); |
bsalomon@google.com | 6d003d1 | 2012-09-11 15:45:20 +0000 | [diff] [blame] | 105 | return fTextureAccess; |
tomhudson@google.com | d0c1a06 | 2012-07-12 17:23:52 +0000 | [diff] [blame] | 106 | } |
| 107 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 108 | const GrBackendEffectFactory& GrSingleTextureEffect::getFactory() const { |
| 109 | return GrTBackendEffectFactory<GrSingleTextureEffect>::getInstance(); |
tomhudson@google.com | aa72eab | 2012-07-19 18:01:07 +0000 | [diff] [blame] | 110 | } |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 111 | |
| 112 | /////////////////////////////////////////////////////////////////////////////// |
| 113 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 114 | GR_DEFINE_EFFECT_TEST(GrSingleTextureEffect); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 115 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 116 | GrEffect* GrSingleTextureEffect::TestCreate(SkRandom* random, |
| 117 | GrContext* context, |
| 118 | GrTexture* textures[]) { |
bsalomon@google.com | 6f261be | 2012-10-24 19:07:10 +0000 | [diff] [blame] | 119 | int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 120 | GrEffectUnitTest::kAlphaTextureIdx; |
bsalomon@google.com | ce49e16 | 2012-11-01 13:50:50 +0000 | [diff] [blame^] | 121 | const SkMatrix& matrix = GrEffectUnitTest::TestMatrix(random); |
| 122 | return SkNEW_ARGS(GrSingleTextureEffect, (textures[texIdx], matrix)); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 123 | } |