blob: 9909c3a6498b35242d31d1376097e9a2521c1d6b [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.com2eaaefd2012-10-29 19:51:22 +00009#include "GrTBackendEffectFactory.h"
bsalomon@google.comd698f772012-10-25 13:22:00 +000010#include "gl/GrGLEffect.h"
bsalomon@google.com92b6a942012-11-02 19:50:26 +000011#include "gl/GrGLEffectMatrix.h"
12#include "SkFloatingPoint.h"
tomhudson@google.com2f68e762012-07-17 18:43:21 +000013
bsalomon@google.com22a800a2012-10-26 19:16:46 +000014class GrGLTextureDomainEffect : public GrGLEffect {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000015public:
bsalomon@google.com396e61f2012-10-25 19:00:29 +000016 GrGLTextureDomainEffect(const GrBackendEffectFactory&, const GrEffect&);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000017
bsalomon@google.com22a800a2012-10-26 19:16:46 +000018 virtual void emitCode(GrGLShaderBuilder*,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000019 const GrEffectStage&,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000020 EffectKey,
21 const char* vertexCoords,
22 const char* outputColor,
23 const char* inputColor,
24 const TextureSamplerArray&) SK_OVERRIDE;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000025
bsalomon@google.com28a15fb2012-10-26 17:53:18 +000026 virtual void setData(const GrGLUniformManager&, const GrEffectStage&) SK_OVERRIDE;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000027
bsalomon@google.com92b6a942012-11-02 19:50:26 +000028 static inline EffectKey GenKey(const GrEffectStage&, const GrGLCaps&);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000029
30private:
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000031 GrGLUniformManager::UniformHandle fNameUni;
bsalomon@google.com92b6a942012-11-02 19:50:26 +000032 GrGLEffectMatrix fEffectMatrix;
33 GrGLfloat fPrevDomain[4];
tomhudson@google.com2f68e762012-07-17 18:43:21 +000034
bsalomon@google.com22a800a2012-10-26 19:16:46 +000035 typedef GrGLEffect INHERITED;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000036};
37
bsalomon@google.com396e61f2012-10-25 19:00:29 +000038GrGLTextureDomainEffect::GrGLTextureDomainEffect(const GrBackendEffectFactory& factory,
bsalomon@google.com021fc732012-10-25 12:47:42 +000039 const GrEffect&)
bsalomon@google.com374e7592012-10-23 17:30:45 +000040 : INHERITED(factory)
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000041 , fNameUni(GrGLUniformManager::kInvalidUniformHandle) {
bsalomon@google.com92b6a942012-11-02 19:50:26 +000042 fRequiresTextureMatrix = false;
43 fPrevDomain[0] = SK_FloatNaN;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000044}
45
bsalomon@google.com22a800a2012-10-26 19:16:46 +000046void GrGLTextureDomainEffect::emitCode(GrGLShaderBuilder* builder,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000047 const GrEffectStage&,
bsalomon@google.com92b6a942012-11-02 19:50:26 +000048 EffectKey key,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000049 const char* vertexCoords,
50 const char* outputColor,
51 const char* inputColor,
52 const TextureSamplerArray& samplers) {
bsalomon@google.com92b6a942012-11-02 19:50:26 +000053 const char* coords;
54 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, vertexCoords, &coords);
tomhudson@google.com57143a22012-07-17 19:07:35 +000055 fNameUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000056 kVec4f_GrSLType, "TexDom");
tomhudson@google.com2f68e762012-07-17 18:43:21 +000057
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000058 builder->fFSCode.appendf("\tvec2 clampCoord = clamp(%s, %s.xy, %s.zw);\n",
bsalomon@google.com92b6a942012-11-02 19:50:26 +000059 coords,
tomhudson@google.com57143a22012-07-17 19:07:35 +000060 builder->getUniformCStr(fNameUni),
61 builder->getUniformCStr(fNameUni));
tomhudson@google.com2f68e762012-07-17 18:43:21 +000062
bsalomon@google.com868a8e72012-08-30 19:11:34 +000063 builder->fFSCode.appendf("\t%s = ", outputColor);
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000064 builder->appendTextureLookupAndModulate(&builder->fFSCode,
65 inputColor,
66 samplers[0],
67 "clampCoord");
bsalomon@google.com868a8e72012-08-30 19:11:34 +000068 builder->fFSCode.append(";\n");
tomhudson@google.com2f68e762012-07-17 18:43:21 +000069}
70
bsalomon@google.com28a15fb2012-10-26 17:53:18 +000071void GrGLTextureDomainEffect::setData(const GrGLUniformManager& uman, const GrEffectStage& stage) {
72 const GrTextureDomainEffect& effect =
73 static_cast<const GrTextureDomainEffect&>(*stage.getEffect());
tomhudson@google.com2f68e762012-07-17 18:43:21 +000074 const GrRect& domain = effect.domain();
75
76 float values[4] = {
bsalomon@google.com81712882012-11-01 17:12:34 +000077 SkScalarToFloat(domain.left()),
78 SkScalarToFloat(domain.top()),
79 SkScalarToFloat(domain.right()),
80 SkScalarToFloat(domain.bottom())
tomhudson@google.com2f68e762012-07-17 18:43:21 +000081 };
82 // vertical flip if necessary
bsalomon@google.com2d0bade2012-10-26 19:01:17 +000083 if (GrSurface::kBottomLeft_Origin == effect.texture(0)->origin()) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000084 values[1] = 1.0f - values[1];
85 values[3] = 1.0f - values[3];
86 // The top and bottom were just flipped, so correct the ordering
87 // of elements so that values = (l, t, r, b).
88 SkTSwap(values[1], values[3]);
89 }
bsalomon@google.com92b6a942012-11-02 19:50:26 +000090 if (0 != memcmp(values, fPrevDomain, 4 * sizeof(GrGLfloat))) {
91 uman.set4fv(fNameUni, 0, 1, values);
92 }
93 fEffectMatrix.setData(uman,
94 effect.getMatrix(),
95 stage.getCoordChangeMatrix(),
96 effect.texture(0));
97}
98
99GrGLEffect::EffectKey GrGLTextureDomainEffect::GenKey(const GrEffectStage& stage, const GrGLCaps&) {
100 const GrTextureDomainEffect& effect =
101 static_cast<const GrTextureDomainEffect&>(*stage.getEffect());
102 return GrGLEffectMatrix::GenKey(effect.getMatrix(), effect.getMatrix(), effect.texture(0));
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000103}
104
105
106///////////////////////////////////////////////////////////////////////////////
107
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000108GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture, const GrRect& domain)
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000109 : GrSingleTextureEffect(texture)
110 , fTextureDomain(domain) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000111}
112
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000113GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture,
114 const GrRect& domain,
115 const GrTextureParams& params)
116 : GrSingleTextureEffect(texture, params)
117 , fTextureDomain(domain) {
118}
119
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000120GrTextureDomainEffect::~GrTextureDomainEffect() {
121
122}
123
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000124const GrBackendEffectFactory& GrTextureDomainEffect::getFactory() const {
125 return GrTBackendEffectFactory<GrTextureDomainEffect>::getInstance();
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000126}
127
bsalomon@google.coma469c282012-10-24 18:28:34 +0000128bool GrTextureDomainEffect::isEqual(const GrEffect& sBase) const {
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000129 const GrTextureDomainEffect& s = static_cast<const GrTextureDomainEffect&>(sBase);
130 return (INHERITED::isEqual(sBase) && this->fTextureDomain == s.fTextureDomain);
131}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000132
133///////////////////////////////////////////////////////////////////////////////
134
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000135GR_DEFINE_EFFECT_TEST(GrTextureDomainEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000136
bsalomon@google.coma469c282012-10-24 18:28:34 +0000137GrEffect* GrTextureDomainEffect::TestCreate(SkRandom* random,
138 GrContext* context,
139 GrTexture* textures[]) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000140 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
141 GrEffectUnitTest::kAlphaTextureIdx;
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000142 GrRect domain;
143 domain.fLeft = random->nextUScalar1();
144 domain.fRight = random->nextRangeScalar(domain.fLeft, SK_Scalar1);
145 domain.fTop = random->nextUScalar1();
146 domain.fBottom = random->nextRangeScalar(domain.fTop, SK_Scalar1);
147 return SkNEW_ARGS(GrTextureDomainEffect, (textures[texIdx], domain));
148}