blob: bfbab2995f6c09ff0945bf38316b64520214ea7a [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"
bsalomon@google.comd698f772012-10-25 13:22:00 +00009#include "gl/GrGLEffect.h"
bsalomon@google.com396e61f2012-10-25 19:00:29 +000010#include "GrBackendEffectFactory.h"
tomhudson@google.com2f68e762012-07-17 18:43:21 +000011
bsalomon@google.comaa600932012-10-25 13:29:20 +000012class GrGLTextureDomainEffect : public GrGLLegacyEffect {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000013public:
bsalomon@google.com396e61f2012-10-25 19:00:29 +000014 GrGLTextureDomainEffect(const GrBackendEffectFactory&, const GrEffect&);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000015
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000016 virtual void setupVariables(GrGLShaderBuilder* builder) SK_OVERRIDE;
tomhudson@google.com57143a22012-07-17 19:07:35 +000017 virtual void emitVS(GrGLShaderBuilder* builder,
tomhudson@google.com2f68e762012-07-17 18:43:21 +000018 const char* vertexCoords) SK_OVERRIDE { }
tomhudson@google.com57143a22012-07-17 19:07:35 +000019 virtual void emitFS(GrGLShaderBuilder* builder,
tomhudson@google.com2f68e762012-07-17 18:43:21 +000020 const char* outputColor,
21 const char* inputColor,
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000022 const TextureSamplerArray&) SK_OVERRIDE;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000023
bsalomon@google.coma469c282012-10-24 18:28:34 +000024 virtual void setData(const GrGLUniformManager&, const GrEffect&) SK_OVERRIDE;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000025
bsalomon@google.coma469c282012-10-24 18:28:34 +000026 static inline StageKey GenKey(const GrEffect&, const GrGLCaps&) { return 0; }
tomhudson@google.com2f68e762012-07-17 18:43:21 +000027
28private:
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000029 GrGLUniformManager::UniformHandle fNameUni;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000030
bsalomon@google.comaa600932012-10-25 13:29:20 +000031 typedef GrGLLegacyEffect INHERITED;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000032};
33
bsalomon@google.com396e61f2012-10-25 19:00:29 +000034GrGLTextureDomainEffect::GrGLTextureDomainEffect(const GrBackendEffectFactory& factory,
bsalomon@google.com021fc732012-10-25 12:47:42 +000035 const GrEffect&)
bsalomon@google.com374e7592012-10-23 17:30:45 +000036 : INHERITED(factory)
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000037 , fNameUni(GrGLUniformManager::kInvalidUniformHandle) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000038}
39
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000040void GrGLTextureDomainEffect::setupVariables(GrGLShaderBuilder* builder) {
tomhudson@google.com57143a22012-07-17 19:07:35 +000041 fNameUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000042 kVec4f_GrSLType, "TexDom");
tomhudson@google.com2f68e762012-07-17 18:43:21 +000043};
44
tomhudson@google.com57143a22012-07-17 19:07:35 +000045void GrGLTextureDomainEffect::emitFS(GrGLShaderBuilder* builder,
tomhudson@google.com2f68e762012-07-17 18:43:21 +000046 const char* outputColor,
47 const char* inputColor,
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000048 const TextureSamplerArray& samplers) {
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000049 builder->fFSCode.appendf("\tvec2 clampCoord = clamp(%s, %s.xy, %s.zw);\n",
50 builder->defaultTexCoordsName(),
tomhudson@google.com57143a22012-07-17 19:07:35 +000051 builder->getUniformCStr(fNameUni),
52 builder->getUniformCStr(fNameUni));
tomhudson@google.com2f68e762012-07-17 18:43:21 +000053
bsalomon@google.com868a8e72012-08-30 19:11:34 +000054 builder->fFSCode.appendf("\t%s = ", outputColor);
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000055 builder->appendTextureLookupAndModulate(&builder->fFSCode,
56 inputColor,
57 samplers[0],
58 "clampCoord");
bsalomon@google.com868a8e72012-08-30 19:11:34 +000059 builder->fFSCode.append(";\n");
tomhudson@google.com2f68e762012-07-17 18:43:21 +000060}
61
bsalomon@google.coma469c282012-10-24 18:28:34 +000062void GrGLTextureDomainEffect::setData(const GrGLUniformManager& uman, const GrEffect& data) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000063 const GrTextureDomainEffect& effect = static_cast<const GrTextureDomainEffect&>(data);
64 const GrRect& domain = effect.domain();
65
66 float values[4] = {
67 GrScalarToFloat(domain.left()),
68 GrScalarToFloat(domain.top()),
69 GrScalarToFloat(domain.right()),
70 GrScalarToFloat(domain.bottom())
71 };
72 // vertical flip if necessary
73 const GrGLTexture* texture = static_cast<const GrGLTexture*>(effect.texture(0));
74 if (GrGLTexture::kBottomUp_Orientation == texture->orientation()) {
75 values[1] = 1.0f - values[1];
76 values[3] = 1.0f - values[3];
77 // The top and bottom were just flipped, so correct the ordering
78 // of elements so that values = (l, t, r, b).
79 SkTSwap(values[1], values[3]);
80 }
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000081 uman.set4fv(fNameUni, 0, 1, values);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000082}
83
84
85///////////////////////////////////////////////////////////////////////////////
86
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000087GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture, const GrRect& domain)
tomhudson@google.com2f68e762012-07-17 18:43:21 +000088 : GrSingleTextureEffect(texture)
89 , fTextureDomain(domain) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000090}
91
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000092GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture,
93 const GrRect& domain,
94 const GrTextureParams& params)
95 : GrSingleTextureEffect(texture, params)
96 , fTextureDomain(domain) {
97}
98
tomhudson@google.com2f68e762012-07-17 18:43:21 +000099GrTextureDomainEffect::~GrTextureDomainEffect() {
100
101}
102
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000103const GrBackendEffectFactory& GrTextureDomainEffect::getFactory() const {
104 return GrTBackendEffectFactory<GrTextureDomainEffect>::getInstance();
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000105}
106
bsalomon@google.coma469c282012-10-24 18:28:34 +0000107bool GrTextureDomainEffect::isEqual(const GrEffect& sBase) const {
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000108 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
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000114GR_DEFINE_EFFECT_TEST(GrTextureDomainEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000115
bsalomon@google.coma469c282012-10-24 18:28:34 +0000116GrEffect* GrTextureDomainEffect::TestCreate(SkRandom* random,
117 GrContext* context,
118 GrTexture* textures[]) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000119 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
120 GrEffectUnitTest::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}