blob: 1bf0512da71a51606f12832bbe339c4fc1831477 [file] [log] [blame]
tomhudson@google.com2f68e762012-07-17 18:43:21 +00001/*
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
12class GrGLTextureDomainEffect : public GrGLProgramStage {
13public:
14 GrGLTextureDomainEffect(const GrProgramStageFactory& factory,
15 const GrCustomStage& stage);
16
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000017 virtual void setupVariables(GrGLShaderBuilder* builder) SK_OVERRIDE;
tomhudson@google.com57143a22012-07-17 19:07:35 +000018 virtual void emitVS(GrGLShaderBuilder* builder,
tomhudson@google.com2f68e762012-07-17 18:43:21 +000019 const char* vertexCoords) SK_OVERRIDE { }
tomhudson@google.com57143a22012-07-17 19:07:35 +000020 virtual void emitFS(GrGLShaderBuilder* builder,
tomhudson@google.com2f68e762012-07-17 18:43:21 +000021 const char* outputColor,
22 const char* inputColor,
23 const char* samplerName) SK_OVERRIDE;
24
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000025 virtual void setData(const GrGLUniformManager&,
tomhudson@google.com2f68e762012-07-17 18:43:21 +000026 const GrCustomStage&,
27 const GrRenderTarget*,
28 int stageNum) SK_OVERRIDE;
29
30 static inline StageKey GenKey(const GrCustomStage&) { return 0; }
31
32private:
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000033 GrGLUniformManager::UniformHandle fNameUni;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000034
35 typedef GrGLProgramStage INHERITED;
36};
37
38GrGLTextureDomainEffect::GrGLTextureDomainEffect(const GrProgramStageFactory& factory,
39 const GrCustomStage& stage)
40 : GrGLProgramStage(factory)
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000041 , fNameUni(GrGLUniformManager::kInvalidUniformHandle) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000042}
43
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000044void GrGLTextureDomainEffect::setupVariables(GrGLShaderBuilder* builder) {
tomhudson@google.com57143a22012-07-17 19:07:35 +000045 fNameUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000046 kVec4f_GrSLType, "TexDom");
tomhudson@google.com2f68e762012-07-17 18:43:21 +000047};
48
tomhudson@google.com57143a22012-07-17 19:07:35 +000049void GrGLTextureDomainEffect::emitFS(GrGLShaderBuilder* builder,
tomhudson@google.com2f68e762012-07-17 18:43:21 +000050 const char* outputColor,
51 const char* inputColor,
52 const char* samplerName) {
53 SkString coordVar("clampCoord");
tomhudson@google.com57143a22012-07-17 19:07:35 +000054 builder->fFSCode.appendf("\t%s %s = clamp(%s, %s.xy, %s.zw);\n",
55 GrGLShaderVar::TypeString(GrSLFloatVectorType(builder->fCoordDims)),
tomhudson@google.com2f68e762012-07-17 18:43:21 +000056 coordVar.c_str(),
tomhudson@google.com57143a22012-07-17 19:07:35 +000057 builder->fSampleCoords.c_str(),
58 builder->getUniformCStr(fNameUni),
59 builder->getUniformCStr(fNameUni));
60 builder->fSampleCoords = coordVar;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000061
tomhudson@google.com57143a22012-07-17 19:07:35 +000062 builder->emitDefaultFetch(outputColor, samplerName);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000063}
64
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000065void GrGLTextureDomainEffect::setData(const GrGLUniformManager& uman,
66 const GrCustomStage& data,
67 const GrRenderTarget*,
68 int stageNum) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000069 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.comdbbc4e22012-07-25 17:48:39 +000087 uman.set4fv(fNameUni, 0, 1, values);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000088}
89
90
91///////////////////////////////////////////////////////////////////////////////
92
93GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture, GrRect domain)
94 : GrSingleTextureEffect(texture)
95 , fTextureDomain(domain) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000096}
97
98GrTextureDomainEffect::~GrTextureDomainEffect() {
99
100}
101
102const GrProgramStageFactory& GrTextureDomainEffect::getFactory() const {
103 return GrTProgramStageFactory<GrTextureDomainEffect>::getInstance();
104}
105
106bool 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}
110
111