blob: 279e48137e7ca19deb8be688da04e30f55d30b5c [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,
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000023 const TextureSamplerArray&) SK_OVERRIDE;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000024
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
twiz@google.coma5e65ec2012-08-02 15:15:16 +000030 static inline StageKey GenKey(const GrCustomStage&, const GrGLCaps&) { return 0; }
tomhudson@google.com2f68e762012-07-17 18:43:21 +000031
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,
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000052 const TextureSamplerArray& samplers) {
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000053 builder->fFSCode.appendf("\tvec2 clampCoord = clamp(%s, %s.xy, %s.zw);\n",
54 builder->defaultTexCoordsName(),
tomhudson@google.com57143a22012-07-17 19:07:35 +000055 builder->getUniformCStr(fNameUni),
56 builder->getUniformCStr(fNameUni));
tomhudson@google.com2f68e762012-07-17 18:43:21 +000057
bsalomon@google.com868a8e72012-08-30 19:11:34 +000058 builder->fFSCode.appendf("\t%s = ", outputColor);
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000059 builder->appendTextureLookupAndModulate(&builder->fFSCode,
60 inputColor,
61 samplers[0],
62 "clampCoord");
bsalomon@google.com868a8e72012-08-30 19:11:34 +000063 builder->fFSCode.append(";\n");
tomhudson@google.com2f68e762012-07-17 18:43:21 +000064}
65
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000066void GrGLTextureDomainEffect::setData(const GrGLUniformManager& uman,
67 const GrCustomStage& data,
68 const GrRenderTarget*,
69 int stageNum) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000070 const GrTextureDomainEffect& effect = static_cast<const GrTextureDomainEffect&>(data);
71 const GrRect& domain = effect.domain();
72
73 float values[4] = {
74 GrScalarToFloat(domain.left()),
75 GrScalarToFloat(domain.top()),
76 GrScalarToFloat(domain.right()),
77 GrScalarToFloat(domain.bottom())
78 };
79 // vertical flip if necessary
80 const GrGLTexture* texture = static_cast<const GrGLTexture*>(effect.texture(0));
81 if (GrGLTexture::kBottomUp_Orientation == texture->orientation()) {
82 values[1] = 1.0f - values[1];
83 values[3] = 1.0f - values[3];
84 // The top and bottom were just flipped, so correct the ordering
85 // of elements so that values = (l, t, r, b).
86 SkTSwap(values[1], values[3]);
87 }
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000088 uman.set4fv(fNameUni, 0, 1, values);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000089}
90
91
92///////////////////////////////////////////////////////////////////////////////
93
94GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture, GrRect domain)
95 : GrSingleTextureEffect(texture)
96 , fTextureDomain(domain) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000097}
98
99GrTextureDomainEffect::~GrTextureDomainEffect() {
100
101}
102
103const GrProgramStageFactory& GrTextureDomainEffect::getFactory() const {
104 return GrTProgramStageFactory<GrTextureDomainEffect>::getInstance();
105}
106
107bool GrTextureDomainEffect::isEqual(const GrCustomStage& sBase) const {
108 const GrTextureDomainEffect& s = static_cast<const GrTextureDomainEffect&>(sBase);
109 return (INHERITED::isEqual(sBase) && this->fTextureDomain == s.fTextureDomain);
110}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000111
112///////////////////////////////////////////////////////////////////////////////
113
114GR_DEFINE_CUSTOM_STAGE_TEST(GrTextureDomainEffect);
115
116GrCustomStage* GrTextureDomainEffect::TestCreate(SkRandom* random,
117 GrContext* context,
118 GrTexture* textures[]) {
bsalomon@google.com8d3d2102012-08-03 18:49:51 +0000119 int texIdx = random->nextBool() ? GrCustomStageUnitTest::kSkiaPMTextureIdx :
120 GrCustomStageUnitTest::kAlphaTextureIdx;
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000121 GrRect domain;
122 domain.fLeft = random->nextUScalar1();
123 domain.fRight = random->nextRangeScalar(domain.fLeft, SK_Scalar1);
124 domain.fTop = random->nextUScalar1();
125 domain.fBottom = random->nextRangeScalar(domain.fTop, SK_Scalar1);
126 return SkNEW_ARGS(GrTextureDomainEffect, (textures[texIdx], domain));
127}