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