blob: 7efd9f2dee1850d1d054582be3e577b9df5ff829 [file] [log] [blame]
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +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#ifndef GrTextureDomainEffect_DEFINED
9#define GrTextureDomainEffect_DEFINED
10
11#include "GrSingleTextureEffect.h"
joshualittb0a8a372014-09-23 09:50:21 -070012#include "gl/GrGLProcessor.h"
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000013
joshualitt30ba4362014-08-21 20:18:45 -070014class GrGLProgramBuilder;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000015class GrGLShaderBuilder;
egdaniel605dd0f2014-11-12 08:35:25 -080016class GrInvariantOutput;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000017struct SkRect;
18
19/**
20 * Limits a texture's lookup coordinates to a domain. Samples outside the domain are either clamped
21 * the edge of the domain or result in a vec4 of zeros (decal mode). The domain is clipped to
22 * normalized texture coords ([0,1]x[0,1] square). Bilinear filtering can cause texels outside the
23 * domain to affect the read value unless the caller considers this when calculating the domain.
24 */
25class GrTextureDomain {
26public:
27 enum Mode {
joshualitt5ae5fc52014-07-29 12:59:27 -070028 // Ignore the texture domain rectangle.
29 kIgnore_Mode,
30 // Clamp texture coords to the domain rectangle.
31 kClamp_Mode,
32 // Treat the area outside the domain rectangle as fully transparent.
33 kDecal_Mode,
34 // Wrap texture coordinates. NOTE: filtering may not work as expected because Bilerp will
35 // read texels outside of the domain. We could perform additional texture reads and filter
36 // in the shader, but are not currently doing this for performance reasons
37 kRepeat_Mode,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000038
joshualitt5ae5fc52014-07-29 12:59:27 -070039 kLastMode = kRepeat_Mode
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000040 };
41 static const int kModeCount = kLastMode + 1;
42
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000043 static const GrTextureDomain& IgnoredDomain() {
44 static const SkRect gDummyRect = {0, 0, 0, 0};
45 static const GrTextureDomain gDomain(gDummyRect, kIgnore_Mode);
46 return gDomain;
47 }
48
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000049 /**
50 * @param index Pass a value >= 0 if using multiple texture domains in the same effect.
51 * It is used to keep inserted variables from causing name collisions.
52 */
53 GrTextureDomain(const SkRect& domain, Mode, int index = -1);
54
55 const SkRect& domain() const { return fDomain; }
56 Mode mode() const { return fMode; }
57
58 /* Computes a domain that bounds all the texels in texelRect. Note that with bilerp enabled
59 texels neighboring the domain may be read. */
60 static const SkRect MakeTexelDomain(const GrTexture* texture, const SkIRect& texelRect) {
61 SkScalar wInv = SK_Scalar1 / texture->width();
62 SkScalar hInv = SK_Scalar1 / texture->height();
63 SkRect result = {
64 texelRect.fLeft * wInv,
65 texelRect.fTop * hInv,
66 texelRect.fRight * wInv,
67 texelRect.fBottom * hInv
68 };
69 return result;
70 }
71
72 bool operator== (const GrTextureDomain& that) const {
egdaniel01a492f2014-08-21 06:47:50 -070073 return fMode == that.fMode && (kIgnore_Mode == fMode || fDomain == that.fDomain);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000074 }
75
76 /**
joshualittb0a8a372014-09-23 09:50:21 -070077 * A GrGLProcessor subclass that corresponds to a GrProcessor subclass that uses GrTextureDomain
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000078 * should include this helper. It generates the texture domain GLSL, produces the part of the
79 * effect key that reflects the texture domain code, and performs the uniform uploads necessary
80 * for texture domains.
81 */
82 class GLDomain {
83 public:
84 GLDomain() {
85 fPrevDomain[0] = SK_FloatNaN;
86 SkDEBUGCODE(fMode = (Mode) -1;)
87 }
88
89 /**
joshualittb0a8a372014-09-23 09:50:21 -070090 * Call this from GrGLProcessor::emitCode() to sample the texture W.R.T. the domain and
91 * mode.
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000092 *
93 * @param outcolor name of vec4 variable to hold the sampled color.
94 * @param inCoords name of vec2 variable containing the coords to be used with the domain.
95 * It is assumed that this is a variable and not an expression.
96 * @param inModulateColor if non-NULL the sampled color will be modulated with this
97 * expression before being written to outColor.
98 */
99 void sampleTexture(GrGLShaderBuilder* builder,
100 const GrTextureDomain& textureDomain,
101 const char* outColor,
102 const SkString& inCoords,
joshualittb0a8a372014-09-23 09:50:21 -0700103 const GrGLProcessor::TextureSampler sampler,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000104 const char* inModulateColor = NULL);
105
106 /**
joshualittb0a8a372014-09-23 09:50:21 -0700107 * Call this from GrGLProcessor::setData() to upload uniforms necessary for the texture
108 * domain. The rectangle is automatically adjusted to account for the texture's origin.
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000109 */
kkinnunen7510b222014-07-30 00:04:16 -0700110 void setData(const GrGLProgramDataManager& pdman, const GrTextureDomain& textureDomain,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000111 GrSurfaceOrigin textureOrigin);
112
113 enum {
114 kDomainKeyBits = 2, // See DomainKey().
115 };
116
117 /**
joshualittb0a8a372014-09-23 09:50:21 -0700118 * GrGLProcessor::GenKey() must call this and include the returned value in it's computed
119 * key. The returned will be limited to the lower kDomainKeyBits bits.
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000120 */
bsalomon63e99f72014-07-21 08:03:14 -0700121 static uint32_t DomainKey(const GrTextureDomain& domain) {
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000122 GR_STATIC_ASSERT(kModeCount <= 4);
123 return domain.mode();
124 }
125
126 private:
kkinnunen7510b222014-07-30 00:04:16 -0700127 SkDEBUGCODE(Mode fMode;)
128 GrGLProgramDataManager::UniformHandle fDomainUni;
129 SkString fDomainName;
130 GrGLfloat fPrevDomain[4];
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000131 };
132
133protected:
134 Mode fMode;
135 SkRect fDomain;
136 int fIndex;
137
138 typedef GrSingleTextureEffect INHERITED;
139};
140
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000141/**
142 * A basic texture effect that uses GrTextureDomain.
143 */
144class GrTextureDomainEffect : public GrSingleTextureEffect {
145
146public:
joshualittb0a8a372014-09-23 09:50:21 -0700147 static GrFragmentProcessor* Create(GrTexture*,
148 const SkMatrix&,
149 const SkRect& domain,
150 GrTextureDomain::Mode,
151 GrTextureParams::FilterMode filterMode,
152 GrCoordSet = kLocal_GrCoordSet);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000153
154 virtual ~GrTextureDomainEffect();
155
mtklein36352bf2015-03-25 18:17:31 -0700156 const char* name() const override { return "TextureDomain"; }
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000157
mtklein36352bf2015-03-25 18:17:31 -0700158 void getGLProcessorKey(const GrGLCaps&, GrProcessorKeyBuilder*) const override;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000159
mtklein36352bf2015-03-25 18:17:31 -0700160 GrGLFragmentProcessor* createGLInstance() const override;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000161
162 const GrTextureDomain& textureDomain() const { return fTextureDomain; }
163
164protected:
165 GrTextureDomain fTextureDomain;
166
167private:
168 GrTextureDomainEffect(GrTexture*,
169 const SkMatrix&,
170 const SkRect& domain,
171 GrTextureDomain::Mode,
172 GrTextureParams::FilterMode,
173 GrCoordSet);
174
mtklein36352bf2015-03-25 18:17:31 -0700175 bool onIsEqual(const GrFragmentProcessor&) const override;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000176
mtklein36352bf2015-03-25 18:17:31 -0700177 void onComputeInvariantOutput(GrInvariantOutput* inout) const override;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700178
joshualittb0a8a372014-09-23 09:50:21 -0700179 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000180
181 typedef GrSingleTextureEffect INHERITED;
182};
183
184#endif