blob: fcfae1490761490e60cbd3e5ff668361d9676a21 [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"
tomhudson@google.com2f68e762012-07-17 18:43:21 +000011
bsalomon@google.com22a800a2012-10-26 19:16:46 +000012class GrGLTextureDomainEffect : public GrGLEffect {
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.com22a800a2012-10-26 19:16:46 +000016 virtual void emitCode(GrGLShaderBuilder*,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000017 const GrEffectStage&,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000018 EffectKey,
19 const char* vertexCoords,
20 const char* outputColor,
21 const char* inputColor,
22 const TextureSamplerArray&) SK_OVERRIDE;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000023
bsalomon@google.com28a15fb2012-10-26 17:53:18 +000024 virtual void setData(const GrGLUniformManager&, const GrEffectStage&) SK_OVERRIDE;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000025
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000026 static inline EffectKey GenKey(const GrEffectStage&, 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.com22a800a2012-10-26 19:16:46 +000031 typedef GrGLEffect 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.com22a800a2012-10-26 19:16:46 +000040void GrGLTextureDomainEffect::emitCode(GrGLShaderBuilder* builder,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000041 const GrEffectStage&,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000042 EffectKey,
43 const char* vertexCoords,
44 const char* outputColor,
45 const char* inputColor,
46 const TextureSamplerArray& samplers) {
47
tomhudson@google.com57143a22012-07-17 19:07:35 +000048 fNameUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000049 kVec4f_GrSLType, "TexDom");
tomhudson@google.com2f68e762012-07-17 18:43:21 +000050
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000051 builder->fFSCode.appendf("\tvec2 clampCoord = clamp(%s, %s.xy, %s.zw);\n",
52 builder->defaultTexCoordsName(),
tomhudson@google.com57143a22012-07-17 19:07:35 +000053 builder->getUniformCStr(fNameUni),
54 builder->getUniformCStr(fNameUni));
tomhudson@google.com2f68e762012-07-17 18:43:21 +000055
bsalomon@google.com868a8e72012-08-30 19:11:34 +000056 builder->fFSCode.appendf("\t%s = ", outputColor);
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000057 builder->appendTextureLookupAndModulate(&builder->fFSCode,
58 inputColor,
59 samplers[0],
60 "clampCoord");
bsalomon@google.com868a8e72012-08-30 19:11:34 +000061 builder->fFSCode.append(";\n");
tomhudson@google.com2f68e762012-07-17 18:43:21 +000062}
63
bsalomon@google.com28a15fb2012-10-26 17:53:18 +000064void GrGLTextureDomainEffect::setData(const GrGLUniformManager& uman, const GrEffectStage& stage) {
65 const GrTextureDomainEffect& effect =
66 static_cast<const GrTextureDomainEffect&>(*stage.getEffect());
tomhudson@google.com2f68e762012-07-17 18:43:21 +000067 const GrRect& domain = effect.domain();
68
69 float values[4] = {
70 GrScalarToFloat(domain.left()),
71 GrScalarToFloat(domain.top()),
72 GrScalarToFloat(domain.right()),
73 GrScalarToFloat(domain.bottom())
74 };
75 // vertical flip if necessary
bsalomon@google.com2d0bade2012-10-26 19:01:17 +000076 if (GrSurface::kBottomLeft_Origin == effect.texture(0)->origin()) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000077 values[1] = 1.0f - values[1];
78 values[3] = 1.0f - values[3];
79 // The top and bottom were just flipped, so correct the ordering
80 // of elements so that values = (l, t, r, b).
81 SkTSwap(values[1], values[3]);
82 }
bsalomon@google.comdbbc4e22012-07-25 17:48:39 +000083 uman.set4fv(fNameUni, 0, 1, values);
tomhudson@google.com2f68e762012-07-17 18:43:21 +000084}
85
86
87///////////////////////////////////////////////////////////////////////////////
88
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000089GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture, const GrRect& domain)
tomhudson@google.com2f68e762012-07-17 18:43:21 +000090 : GrSingleTextureEffect(texture)
91 , fTextureDomain(domain) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000092}
93
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +000094GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture,
95 const GrRect& domain,
96 const GrTextureParams& params)
97 : GrSingleTextureEffect(texture, params)
98 , fTextureDomain(domain) {
99}
100
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000101GrTextureDomainEffect::~GrTextureDomainEffect() {
102
103}
104
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000105const GrBackendEffectFactory& GrTextureDomainEffect::getFactory() const {
106 return GrTBackendEffectFactory<GrTextureDomainEffect>::getInstance();
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000107}
108
bsalomon@google.coma469c282012-10-24 18:28:34 +0000109bool GrTextureDomainEffect::isEqual(const GrEffect& sBase) const {
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000110 const GrTextureDomainEffect& s = static_cast<const GrTextureDomainEffect&>(sBase);
111 return (INHERITED::isEqual(sBase) && this->fTextureDomain == s.fTextureDomain);
112}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000113
114///////////////////////////////////////////////////////////////////////////////
115
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000116GR_DEFINE_EFFECT_TEST(GrTextureDomainEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000117
bsalomon@google.coma469c282012-10-24 18:28:34 +0000118GrEffect* GrTextureDomainEffect::TestCreate(SkRandom* random,
119 GrContext* context,
120 GrTexture* textures[]) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000121 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
122 GrEffectUnitTest::kAlphaTextureIdx;
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000123 GrRect domain;
124 domain.fLeft = random->nextUScalar1();
125 domain.fRight = random->nextRangeScalar(domain.fLeft, SK_Scalar1);
126 domain.fTop = random->nextUScalar1();
127 domain.fBottom = random->nextRangeScalar(domain.fTop, SK_Scalar1);
128 return SkNEW_ARGS(GrTextureDomainEffect, (textures[texIdx], domain));
129}