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 | |
tomhudson@google.com | 57143a2 | 2012-07-17 19:07:35 +0000 | [diff] [blame] | 17 | virtual void setupVariables(GrGLShaderBuilder* builder, |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 18 | int stage) SK_OVERRIDE; |
tomhudson@google.com | 57143a2 | 2012-07-17 19:07:35 +0000 | [diff] [blame] | 19 | virtual void emitVS(GrGLShaderBuilder* builder, |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 20 | const char* vertexCoords) SK_OVERRIDE { } |
tomhudson@google.com | 57143a2 | 2012-07-17 19:07:35 +0000 | [diff] [blame] | 21 | virtual void emitFS(GrGLShaderBuilder* builder, |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 22 | const char* outputColor, |
| 23 | const char* inputColor, |
| 24 | const char* samplerName) SK_OVERRIDE; |
| 25 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame^] | 26 | virtual void setData(const GrGLUniformManager&, |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 27 | const GrCustomStage&, |
| 28 | const GrRenderTarget*, |
| 29 | int stageNum) SK_OVERRIDE; |
| 30 | |
| 31 | static inline StageKey GenKey(const GrCustomStage&) { return 0; } |
| 32 | |
| 33 | private: |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame^] | 34 | GrGLUniformManager::UniformHandle fNameUni; |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 35 | |
| 36 | typedef GrGLProgramStage INHERITED; |
| 37 | }; |
| 38 | |
| 39 | GrGLTextureDomainEffect::GrGLTextureDomainEffect(const GrProgramStageFactory& factory, |
| 40 | const GrCustomStage& stage) |
| 41 | : GrGLProgramStage(factory) |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame^] | 42 | , fNameUni(GrGLUniformManager::kInvalidUniformHandle) { |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 43 | } |
| 44 | |
tomhudson@google.com | 57143a2 | 2012-07-17 19:07:35 +0000 | [diff] [blame] | 45 | void GrGLTextureDomainEffect::setupVariables(GrGLShaderBuilder* builder, |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 46 | int stage) { |
tomhudson@google.com | 57143a2 | 2012-07-17 19:07:35 +0000 | [diff] [blame] | 47 | fNameUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType, |
| 48 | kVec4f_GrSLType, "uTexDom", stage); |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
tomhudson@google.com | 57143a2 | 2012-07-17 19:07:35 +0000 | [diff] [blame] | 51 | void GrGLTextureDomainEffect::emitFS(GrGLShaderBuilder* builder, |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 52 | const char* outputColor, |
| 53 | const char* inputColor, |
| 54 | const char* samplerName) { |
| 55 | SkString coordVar("clampCoord"); |
tomhudson@google.com | 57143a2 | 2012-07-17 19:07:35 +0000 | [diff] [blame] | 56 | builder->fFSCode.appendf("\t%s %s = clamp(%s, %s.xy, %s.zw);\n", |
| 57 | GrGLShaderVar::TypeString(GrSLFloatVectorType(builder->fCoordDims)), |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 58 | coordVar.c_str(), |
tomhudson@google.com | 57143a2 | 2012-07-17 19:07:35 +0000 | [diff] [blame] | 59 | builder->fSampleCoords.c_str(), |
| 60 | builder->getUniformCStr(fNameUni), |
| 61 | builder->getUniformCStr(fNameUni)); |
| 62 | builder->fSampleCoords = coordVar; |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 63 | |
tomhudson@google.com | 57143a2 | 2012-07-17 19:07:35 +0000 | [diff] [blame] | 64 | builder->emitDefaultFetch(outputColor, samplerName); |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 65 | } |
| 66 | |
bsalomon@google.com | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame^] | 67 | void GrGLTextureDomainEffect::setData(const GrGLUniformManager& uman, |
| 68 | const GrCustomStage& data, |
| 69 | const GrRenderTarget*, |
| 70 | int stageNum) { |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 71 | const GrTextureDomainEffect& effect = static_cast<const GrTextureDomainEffect&>(data); |
| 72 | const GrRect& domain = effect.domain(); |
| 73 | |
| 74 | float values[4] = { |
| 75 | GrScalarToFloat(domain.left()), |
| 76 | GrScalarToFloat(domain.top()), |
| 77 | GrScalarToFloat(domain.right()), |
| 78 | GrScalarToFloat(domain.bottom()) |
| 79 | }; |
| 80 | // vertical flip if necessary |
| 81 | const GrGLTexture* texture = static_cast<const GrGLTexture*>(effect.texture(0)); |
| 82 | if (GrGLTexture::kBottomUp_Orientation == texture->orientation()) { |
| 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 | dbbc4e2 | 2012-07-25 17:48:39 +0000 | [diff] [blame^] | 89 | uman.set4fv(fNameUni, 0, 1, values); |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | |
| 93 | /////////////////////////////////////////////////////////////////////////////// |
| 94 | |
| 95 | GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture, GrRect domain) |
| 96 | : GrSingleTextureEffect(texture) |
| 97 | , fTextureDomain(domain) { |
tomhudson@google.com | 2f68e76 | 2012-07-17 18:43:21 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | GrTextureDomainEffect::~GrTextureDomainEffect() { |
| 101 | |
| 102 | } |
| 103 | |
| 104 | const GrProgramStageFactory& GrTextureDomainEffect::getFactory() const { |
| 105 | return GrTProgramStageFactory<GrTextureDomainEffect>::getInstance(); |
| 106 | } |
| 107 | |
| 108 | bool GrTextureDomainEffect::isEqual(const GrCustomStage& sBase) const { |
| 109 | const GrTextureDomainEffect& s = static_cast<const GrTextureDomainEffect&>(sBase); |
| 110 | return (INHERITED::isEqual(sBase) && this->fTextureDomain == s.fTextureDomain); |
| 111 | } |
| 112 | |
| 113 | |