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