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