tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +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 "GrTextureDomainEffect.h" |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 9 | #include "GrTBackendEffectFactory.h" |
bsalomon@google.com | d698f77 | 2012-10-25 13:22:00 +0000 | [diff] [blame] | 10 | #include "gl/GrGLEffect.h" |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 11 | #include "gl/GrGLEffectMatrix.h" |
| 12 | #include "SkFloatingPoint.h" |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 13 | |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 14 | class GrGLTextureDomainEffect : public GrGLEffect { |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 15 | public: |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 16 | GrGLTextureDomainEffect(const GrBackendEffectFactory&, const GrEffect&); |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 17 | |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 18 | virtual void emitCode(GrGLShaderBuilder*, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 19 | const GrEffectStage&, |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 20 | EffectKey, |
| 21 | const char* vertexCoords, |
| 22 | const char* outputColor, |
| 23 | const char* inputColor, |
| 24 | const TextureSamplerArray&) SK_OVERRIDE; |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 25 | |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 26 | virtual void setData(const GrGLUniformManager&, const GrEffectStage&) SK_OVERRIDE; |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 27 | |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 28 | static inline EffectKey GenKey(const GrEffectStage&, const GrGLCaps&); |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 29 | |
| 30 | private: |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 31 | GrGLUniformManager::UniformHandle fNameUni; |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 32 | GrGLEffectMatrix fEffectMatrix; |
| 33 | GrGLfloat fPrevDomain[4]; |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 34 | |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 35 | typedef GrGLEffect INHERITED; |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 38 | GrGLTextureDomainEffect::GrGLTextureDomainEffect(const GrBackendEffectFactory& factory, |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 39 | const GrEffect&) |
bsalomon@google.com | 374e759 | 2012-10-23 17:30:45 +0000 | [diff] [blame] | 40 | : INHERITED(factory) |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame] | 41 | , fNameUni(GrGLUniformManager::kInvalidUniformHandle) { |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 42 | fPrevDomain[0] = SK_FloatNaN; |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 43 | } |
| 44 | |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 45 | void GrGLTextureDomainEffect::emitCode(GrGLShaderBuilder* builder, |
bsalomon@google.com | 2eaaefd | 2012-10-29 19:51:22 +0000 | [diff] [blame] | 46 | const GrEffectStage&, |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 47 | EffectKey key, |
bsalomon@google.com | 22a800a | 2012-10-26 19:16:46 +0000 | [diff] [blame] | 48 | const char* vertexCoords, |
| 49 | const char* outputColor, |
| 50 | const char* inputColor, |
| 51 | const TextureSamplerArray& samplers) { |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 52 | const char* coords; |
| 53 | fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, vertexCoords, &coords); |
tomhudson@google.com | 57143a2 | 2012-07-17 19:07:35 +0000 | [diff] [blame] | 54 | fNameUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
bsalomon@google.com | 777c3aa | 2012-07-25 20:58:20 +0000 | [diff] [blame] | 55 | kVec4f_GrSLType, "TexDom"); |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 56 | |
bsalomon@google.com | 34bcb9f | 2012-08-28 18:20:18 +0000 | [diff] [blame] | 57 | builder->fFSCode.appendf("\tvec2 clampCoord = clamp(%s, %s.xy, %s.zw);\n", |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 58 | coords, |
tomhudson@google.com | 57143a2 | 2012-07-17 19:07:35 +0000 | [diff] [blame] | 59 | builder->getUniformCStr(fNameUni), |
| 60 | builder->getUniformCStr(fNameUni)); |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 61 | |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 62 | builder->fFSCode.appendf("\t%s = ", outputColor); |
bsalomon@google.com | f06df1b | 2012-09-06 20:22:31 +0000 | [diff] [blame] | 63 | builder->appendTextureLookupAndModulate(&builder->fFSCode, |
| 64 | inputColor, |
| 65 | samplers[0], |
| 66 | "clampCoord"); |
bsalomon@google.com | 868a8e7 | 2012-08-30 19:11:34 +0000 | [diff] [blame] | 67 | builder->fFSCode.append(";\n"); |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 68 | } |
| 69 | |
bsalomon@google.com | 28a15fb | 2012-10-26 17:53:18 +0000 | [diff] [blame] | 70 | void GrGLTextureDomainEffect::setData(const GrGLUniformManager& uman, const GrEffectStage& stage) { |
| 71 | const GrTextureDomainEffect& effect = |
| 72 | static_cast<const GrTextureDomainEffect&>(*stage.getEffect()); |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 73 | const GrRect& domain = effect.domain(); |
| 74 | |
| 75 | float values[4] = { |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 76 | SkScalarToFloat(domain.left()), |
| 77 | SkScalarToFloat(domain.top()), |
| 78 | SkScalarToFloat(domain.right()), |
| 79 | SkScalarToFloat(domain.bottom()) |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 80 | }; |
| 81 | // vertical flip if necessary |
bsalomon@google.com | 2d0bade | 2012-10-26 19:01:17 +0000 | [diff] [blame] | 82 | if (GrSurface::kBottomLeft_Origin == effect.texture(0)->origin()) { |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 83 | values[1] = 1.0f - values[1]; |
| 84 | values[3] = 1.0f - values[3]; |
| 85 | // The top and bottom were just flipped, so correct the ordering |
| 86 | // of elements so that values = (l, t, r, b). |
| 87 | SkTSwap(values[1], values[3]); |
| 88 | } |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 89 | if (0 != memcmp(values, fPrevDomain, 4 * sizeof(GrGLfloat))) { |
| 90 | uman.set4fv(fNameUni, 0, 1, values); |
| 91 | } |
| 92 | fEffectMatrix.setData(uman, |
| 93 | effect.getMatrix(), |
| 94 | stage.getCoordChangeMatrix(), |
| 95 | effect.texture(0)); |
| 96 | } |
| 97 | |
| 98 | GrGLEffect::EffectKey GrGLTextureDomainEffect::GenKey(const GrEffectStage& stage, const GrGLCaps&) { |
| 99 | const GrTextureDomainEffect& effect = |
| 100 | static_cast<const GrTextureDomainEffect&>(*stage.getEffect()); |
bsalomon@google.com | 31ca955 | 2012-11-05 19:28:12 +0000 | [diff] [blame] | 101 | return GrGLEffectMatrix::GenKey(effect.getMatrix(), stage.getCoordChangeMatrix(), effect.texture(0)); |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | |
| 105 | /////////////////////////////////////////////////////////////////////////////// |
| 106 | |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 107 | GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture, const GrRect& domain) |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 108 | : GrSingleTextureEffect(texture) |
| 109 | , fTextureDomain(domain) { |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 110 | } |
| 111 | |
bsalomon@google.com | 1ce49fc | 2012-09-18 14:14:49 +0000 | [diff] [blame] | 112 | GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture, |
| 113 | const GrRect& domain, |
| 114 | const GrTextureParams& params) |
| 115 | : GrSingleTextureEffect(texture, params) |
| 116 | , fTextureDomain(domain) { |
| 117 | } |
| 118 | |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 119 | GrTextureDomainEffect::~GrTextureDomainEffect() { |
| 120 | |
| 121 | } |
| 122 | |
bsalomon@google.com | 396e61f | 2012-10-25 19:00:29 +0000 | [diff] [blame] | 123 | const GrBackendEffectFactory& GrTextureDomainEffect::getFactory() const { |
| 124 | return GrTBackendEffectFactory<GrTextureDomainEffect>::getInstance(); |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 125 | } |
| 126 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 127 | bool GrTextureDomainEffect::isEqual(const GrEffect& sBase) const { |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 128 | const GrTextureDomainEffect& s = static_cast<const GrTextureDomainEffect&>(sBase); |
| 129 | return (INHERITED::isEqual(sBase) && this->fTextureDomain == s.fTextureDomain); |
| 130 | } |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 131 | |
| 132 | /////////////////////////////////////////////////////////////////////////////// |
| 133 | |
bsalomon@google.com | f271cc7 | 2012-10-24 19:35:13 +0000 | [diff] [blame] | 134 | GR_DEFINE_EFFECT_TEST(GrTextureDomainEffect); |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 135 | |
bsalomon@google.com | a469c28 | 2012-10-24 18:28:34 +0000 | [diff] [blame] | 136 | GrEffect* GrTextureDomainEffect::TestCreate(SkRandom* random, |
| 137 | GrContext* context, |
| 138 | GrTexture* textures[]) { |
bsalomon@google.com | 6f261be | 2012-10-24 19:07:10 +0000 | [diff] [blame] | 139 | int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx : |
| 140 | GrEffectUnitTest::kAlphaTextureIdx; |
bsalomon@google.com | 0a7672f | 2012-08-03 18:12:20 +0000 | [diff] [blame] | 141 | GrRect domain; |
| 142 | domain.fLeft = random->nextUScalar1(); |
| 143 | domain.fRight = random->nextRangeScalar(domain.fLeft, SK_Scalar1); |
| 144 | domain.fTop = random->nextUScalar1(); |
| 145 | domain.fBottom = random->nextRangeScalar(domain.fTop, SK_Scalar1); |
| 146 | return SkNEW_ARGS(GrTextureDomainEffect, (textures[texIdx], domain)); |
| 147 | } |