blob: ca8561ce4804d79a87c838f8c77172c46103040c [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
tomhudson@google.com57143a22012-07-17 19:07:35 +000017 virtual void setupVariables(GrGLShaderBuilder* builder,
tomhudson@google.com2f68e762012-07-17 18:43:21 +000018 int stage) SK_OVERRIDE;
tomhudson@google.com57143a22012-07-17 19:07:35 +000019 virtual void emitVS(GrGLShaderBuilder* builder,
tomhudson@google.com2f68e762012-07-17 18:43:21 +000020 const char* vertexCoords) SK_OVERRIDE { }
tomhudson@google.com57143a22012-07-17 19:07:35 +000021 virtual void emitFS(GrGLShaderBuilder* builder,
tomhudson@google.com2f68e762012-07-17 18:43:21 +000022 const char* outputColor,
23 const char* inputColor,
24 const char* samplerName) SK_OVERRIDE;
25
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000026 virtual void setData(const GrGLUniformManager&,
tomhudson@google.com2f68e762012-07-17 18:43:21 +000027 const GrCustomStage&,
28 const GrRenderTarget*,
29 int stageNum) SK_OVERRIDE;
30
31 static inline StageKey GenKey(const GrCustomStage&) { return 0; }
32
33private:
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000034 GrGLUniformManager::UniformHandle fNameUni;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000035
36 typedef GrGLProgramStage INHERITED;
37};
38
39GrGLTextureDomainEffect::GrGLTextureDomainEffect(const GrProgramStageFactory& factory,
40 const GrCustomStage& stage)
41 : GrGLProgramStage(factory)
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000042 , fNameUni(GrGLUniformManager::kInvalidUniformHandle) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000043}
44
tomhudson@google.com57143a22012-07-17 19:07:35 +000045void GrGLTextureDomainEffect::setupVariables(GrGLShaderBuilder* builder,
tomhudson@google.com2f68e762012-07-17 18:43:21 +000046 int stage) {
tomhudson@google.com57143a22012-07-17 19:07:35 +000047 fNameUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
48 kVec4f_GrSLType, "uTexDom", stage);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000049};
50
tomhudson@google.com57143a22012-07-17 19:07:35 +000051void GrGLTextureDomainEffect::emitFS(GrGLShaderBuilder* builder,
tomhudson@google.com2f68e762012-07-17 18:43:21 +000052 const char* outputColor,
53 const char* inputColor,
54 const char* samplerName) {
55 SkString coordVar("clampCoord");
tomhudson@google.com57143a22012-07-17 19:07:35 +000056 builder->fFSCode.appendf("\t%s %s = clamp(%s, %s.xy, %s.zw);\n",
57 GrGLShaderVar::TypeString(GrSLFloatVectorType(builder->fCoordDims)),
tomhudson@google.com2f68e762012-07-17 18:43:21 +000058 coordVar.c_str(),
tomhudson@google.com57143a22012-07-17 19:07:35 +000059 builder->fSampleCoords.c_str(),
60 builder->getUniformCStr(fNameUni),
61 builder->getUniformCStr(fNameUni));
62 builder->fSampleCoords = coordVar;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000063
tomhudson@google.com57143a22012-07-17 19:07:35 +000064 builder->emitDefaultFetch(outputColor, samplerName);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000065}
66
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000067void GrGLTextureDomainEffect::setData(const GrGLUniformManager& uman,
68 const GrCustomStage& data,
69 const GrRenderTarget*,
70 int stageNum) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000071 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.comdbbc4e22012-07-25 17:48:39 +000089 uman.set4fv(fNameUni, 0, 1, values);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000090}
91
92
93///////////////////////////////////////////////////////////////////////////////
94
95GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture, GrRect domain)
96 : GrSingleTextureEffect(texture)
97 , fTextureDomain(domain) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000098}
99
100GrTextureDomainEffect::~GrTextureDomainEffect() {
101
102}
103
104const GrProgramStageFactory& GrTextureDomainEffect::getFactory() const {
105 return GrTProgramStageFactory<GrTextureDomainEffect>::getInstance();
106}
107
108bool 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