blob: d957d1cbe427cbb298a9ac8e84a6675aa25df236 [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 fPrevDomain[0] = SK_FloatNaN;
tomhudson@google.com2f68e762012-07-17 18:43:21 +000043}
44
bsalomon@google.com22a800a2012-10-26 19:16:46 +000045void GrGLTextureDomainEffect::emitCode(GrGLShaderBuilder* builder,
bsalomon@google.com2eaaefd2012-10-29 19:51:22 +000046 const GrEffectStage&,
bsalomon@google.com92b6a942012-11-02 19:50:26 +000047 EffectKey key,
bsalomon@google.com22a800a2012-10-26 19:16:46 +000048 const char* vertexCoords,
49 const char* outputColor,
50 const char* inputColor,
51 const TextureSamplerArray& samplers) {
bsalomon@google.com92b6a942012-11-02 19:50:26 +000052 const char* coords;
53 fEffectMatrix.emitCodeMakeFSCoords2D(builder, key, vertexCoords, &coords);
tomhudson@google.com57143a22012-07-17 19:07:35 +000054 fNameUni = builder->addUniform(GrGLShaderBuilder::kFragment_ShaderType,
bsalomon@google.com777c3aa2012-07-25 20:58:20 +000055 kVec4f_GrSLType, "TexDom");
tomhudson@google.com2f68e762012-07-17 18:43:21 +000056
bsalomon@google.com34bcb9f2012-08-28 18:20:18 +000057 builder->fFSCode.appendf("\tvec2 clampCoord = clamp(%s, %s.xy, %s.zw);\n",
bsalomon@google.com92b6a942012-11-02 19:50:26 +000058 coords,
tomhudson@google.com57143a22012-07-17 19:07:35 +000059 builder->getUniformCStr(fNameUni),
60 builder->getUniformCStr(fNameUni));
tomhudson@google.com2f68e762012-07-17 18:43:21 +000061
bsalomon@google.com868a8e72012-08-30 19:11:34 +000062 builder->fFSCode.appendf("\t%s = ", outputColor);
bsalomon@google.comf06df1b2012-09-06 20:22:31 +000063 builder->appendTextureLookupAndModulate(&builder->fFSCode,
64 inputColor,
65 samplers[0],
66 "clampCoord");
bsalomon@google.com868a8e72012-08-30 19:11:34 +000067 builder->fFSCode.append(";\n");
tomhudson@google.com2f68e762012-07-17 18:43:21 +000068}
69
bsalomon@google.com28a15fb2012-10-26 17:53:18 +000070void GrGLTextureDomainEffect::setData(const GrGLUniformManager& uman, const GrEffectStage& stage) {
71 const GrTextureDomainEffect& effect =
72 static_cast<const GrTextureDomainEffect&>(*stage.getEffect());
tomhudson@google.com2f68e762012-07-17 18:43:21 +000073 const GrRect& domain = effect.domain();
74
75 float values[4] = {
bsalomon@google.com81712882012-11-01 17:12:34 +000076 SkScalarToFloat(domain.left()),
77 SkScalarToFloat(domain.top()),
78 SkScalarToFloat(domain.right()),
79 SkScalarToFloat(domain.bottom())
tomhudson@google.com2f68e762012-07-17 18:43:21 +000080 };
81 // vertical flip if necessary
bsalomon@google.com2d0bade2012-10-26 19:01:17 +000082 if (GrSurface::kBottomLeft_Origin == effect.texture(0)->origin()) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +000083 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.com92b6a942012-11-02 19:50:26 +000089 if (0 != memcmp(values, fPrevDomain, 4 * sizeof(GrGLfloat))) {
90 uman.set4fv(fNameUni, 0, 1, values);
91 }
92 fEffectMatrix.setData(uman,
93 effect.getMatrix(),
94 stage.getCoordChangeMatrix(),
95 effect.texture(0));
96}
97
98GrGLEffect::EffectKey GrGLTextureDomainEffect::GenKey(const GrEffectStage& stage, const GrGLCaps&) {
99 const GrTextureDomainEffect& effect =
100 static_cast<const GrTextureDomainEffect&>(*stage.getEffect());
bsalomon@google.com31ca9552012-11-05 19:28:12 +0000101 return GrGLEffectMatrix::GenKey(effect.getMatrix(), stage.getCoordChangeMatrix(), effect.texture(0));
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000102}
103
104
105///////////////////////////////////////////////////////////////////////////////
106
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000107GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture, const GrRect& domain)
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000108 : GrSingleTextureEffect(texture)
109 , fTextureDomain(domain) {
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000110}
111
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000112GrTextureDomainEffect::GrTextureDomainEffect(GrTexture* texture,
113 const GrRect& domain,
114 const GrTextureParams& params)
115 : GrSingleTextureEffect(texture, params)
116 , fTextureDomain(domain) {
117}
118
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000119GrTextureDomainEffect::~GrTextureDomainEffect() {
120
121}
122
bsalomon@google.com396e61f2012-10-25 19:00:29 +0000123const GrBackendEffectFactory& GrTextureDomainEffect::getFactory() const {
124 return GrTBackendEffectFactory<GrTextureDomainEffect>::getInstance();
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000125}
126
bsalomon@google.coma469c282012-10-24 18:28:34 +0000127bool GrTextureDomainEffect::isEqual(const GrEffect& sBase) const {
tomhudson@google.com2f68e762012-07-17 18:43:21 +0000128 const GrTextureDomainEffect& s = static_cast<const GrTextureDomainEffect&>(sBase);
129 return (INHERITED::isEqual(sBase) && this->fTextureDomain == s.fTextureDomain);
130}
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000131
132///////////////////////////////////////////////////////////////////////////////
133
bsalomon@google.comf271cc72012-10-24 19:35:13 +0000134GR_DEFINE_EFFECT_TEST(GrTextureDomainEffect);
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000135
bsalomon@google.coma469c282012-10-24 18:28:34 +0000136GrEffect* GrTextureDomainEffect::TestCreate(SkRandom* random,
137 GrContext* context,
138 GrTexture* textures[]) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000139 int texIdx = random->nextBool() ? GrEffectUnitTest::kSkiaPMTextureIdx :
140 GrEffectUnitTest::kAlphaTextureIdx;
bsalomon@google.com0a7672f2012-08-03 18:12:20 +0000141 GrRect domain;
142 domain.fLeft = random->nextUScalar1();
143 domain.fRight = random->nextRangeScalar(domain.fLeft, SK_Scalar1);
144 domain.fTop = random->nextUScalar1();
145 domain.fBottom = random->nextRangeScalar(domain.fTop, SK_Scalar1);
146 return SkNEW_ARGS(GrTextureDomainEffect, (textures[texIdx], domain));
147}