blob: c74a9919c47e2ae34f45285de9199e3d2b097326 [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"
egdaniel64c47282015-11-13 06:54:19 -080012#include "glsl/GrGLSLFragmentProcessor.h"
egdaniel018fb622015-10-28 07:26:40 -070013#include "glsl/GrGLSLProgramDataManager.h"
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000014
joshualitt30ba4362014-08-21 20:18:45 -070015class GrGLProgramBuilder;
Brian Osmanc4689632016-12-19 17:04:59 -050016class GrGLSLColorSpaceXformHelper;
egdaniel2d721d32015-11-11 13:06:05 -080017class GrGLSLShaderBuilder;
egdaniel605dd0f2014-11-12 08:35:25 -080018class GrInvariantOutput;
egdaniel7ea439b2015-12-03 09:20:44 -080019class GrGLSLUniformHandler;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000020struct SkRect;
21
22/**
23 * Limits a texture's lookup coordinates to a domain. Samples outside the domain are either clamped
24 * the edge of the domain or result in a vec4 of zeros (decal mode). The domain is clipped to
25 * normalized texture coords ([0,1]x[0,1] square). Bilinear filtering can cause texels outside the
26 * domain to affect the read value unless the caller considers this when calculating the domain.
27 */
28class GrTextureDomain {
29public:
30 enum Mode {
joshualitt5ae5fc52014-07-29 12:59:27 -070031 // Ignore the texture domain rectangle.
32 kIgnore_Mode,
33 // Clamp texture coords to the domain rectangle.
34 kClamp_Mode,
35 // Treat the area outside the domain rectangle as fully transparent.
36 kDecal_Mode,
37 // Wrap texture coordinates. NOTE: filtering may not work as expected because Bilerp will
38 // read texels outside of the domain. We could perform additional texture reads and filter
39 // in the shader, but are not currently doing this for performance reasons
40 kRepeat_Mode,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000041
joshualitt5ae5fc52014-07-29 12:59:27 -070042 kLastMode = kRepeat_Mode
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000043 };
44 static const int kModeCount = kLastMode + 1;
45
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000046 static const GrTextureDomain& IgnoredDomain() {
Robert Phillips40fd7c92017-01-30 08:06:27 -050047 static const GrTextureDomain gDomain((GrTextureProxy*)nullptr,
48 SkRect::MakeEmpty(), kIgnore_Mode);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +000049 return gDomain;
50 }
51
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000052 /**
53 * @param index Pass a value >= 0 if using multiple texture domains in the same effect.
54 * It is used to keep inserted variables from causing name collisions.
55 */
Robert Phillips40fd7c92017-01-30 08:06:27 -050056 GrTextureDomain(GrTextureProxy*, const SkRect& domain, Mode, int index = -1);
57
Brian Salomonffc2ec42017-07-25 14:59:03 -040058 GrTextureDomain(const GrTextureDomain&) = default;
59
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000060 const SkRect& domain() const { return fDomain; }
61 Mode mode() const { return fMode; }
62
63 /* Computes a domain that bounds all the texels in texelRect. Note that with bilerp enabled
64 texels neighboring the domain may be read. */
Robert Phillipse98234f2017-01-09 14:23:59 -050065 static const SkRect MakeTexelDomain(const SkIRect& texelRect) {
66 return SkRect::Make(texelRect);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000067 }
68
Robert Phillipse98234f2017-01-09 14:23:59 -050069 static const SkRect MakeTexelDomainForMode(const SkIRect& texelRect, Mode mode) {
senorblancod0d37ca2015-04-02 04:54:56 -070070 // For Clamp mode, inset by half a texel.
senorblancod0d37ca2015-04-02 04:54:56 -070071 SkScalar inset = (mode == kClamp_Mode && !texelRect.isEmpty()) ? SK_ScalarHalf : 0;
Robert Phillipse98234f2017-01-09 14:23:59 -050072 return SkRect::MakeLTRB(texelRect.fLeft + inset, texelRect.fTop + inset,
73 texelRect.fRight - inset, texelRect.fBottom - inset);
senorblancod0d37ca2015-04-02 04:54:56 -070074 }
75
Brian Salomon2ebd0c82016-10-03 17:15:28 -040076 bool operator==(const GrTextureDomain& that) const {
egdaniel01a492f2014-08-21 06:47:50 -070077 return fMode == that.fMode && (kIgnore_Mode == fMode || fDomain == that.fDomain);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000078 }
79
80 /**
egdaniel64c47282015-11-13 06:54:19 -080081 * A GrGLSLFragmentProcessor subclass that corresponds to a GrProcessor subclass that uses
egdaniel7dc4bd02015-10-29 07:57:01 -070082 * GrTextureDomain should include this helper. It generates the texture domain GLSL, produces
83 * the part of the effect key that reflects the texture domain code, and performs the uniform
84 * uploads necessary for texture domains.
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000085 */
86 class GLDomain {
87 public:
88 GLDomain() {
joshualittb5fb5af2015-08-25 12:10:54 -070089 for (int i = 0; i < kPrevDomainCount; i++) {
90 fPrevDomain[i] = SK_FloatNaN;
91 }
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000092 SkDEBUGCODE(fMode = (Mode) -1;)
93 }
94
95 /**
egdaniel64c47282015-11-13 06:54:19 -080096 * Call this from GrGLSLFragmentProcessor::emitCode() to sample the texture W.R.T. the
97 * domain and mode.
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000098 *
99 * @param outcolor name of vec4 variable to hold the sampled color.
100 * @param inCoords name of vec2 variable containing the coords to be used with the domain.
101 * It is assumed that this is a variable and not an expression.
halcanary96fcdcc2015-08-27 07:41:13 -0700102 * @param inModulateColor if non-nullptr the sampled color will be modulated with this
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000103 * expression before being written to outColor.
104 */
egdaniel2d721d32015-11-11 13:06:05 -0800105 void sampleTexture(GrGLSLShaderBuilder* builder,
egdaniel7ea439b2015-12-03 09:20:44 -0800106 GrGLSLUniformHandler* uniformHandler,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500107 const GrShaderCaps* shaderCaps,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000108 const GrTextureDomain& textureDomain,
109 const char* outColor,
110 const SkString& inCoords,
egdaniel09aa1fc2016-04-20 07:09:46 -0700111 GrGLSLFragmentProcessor::SamplerHandle sampler,
Brian Osmanc4689632016-12-19 17:04:59 -0500112 const char* inModulateColor = nullptr,
113 GrGLSLColorSpaceXformHelper* colorXformHelper = nullptr);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000114
115 /**
egdaniel64c47282015-11-13 06:54:19 -0800116 * Call this from GrGLSLFragmentProcessor::setData() to upload uniforms necessary for the
egdaniel7dc4bd02015-10-29 07:57:01 -0700117 * texture domain. The rectangle is automatically adjusted to account for the texture's
118 * origin.
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000119 */
Robert Phillipsc686ce32017-07-21 14:12:29 -0400120 void setData(const GrGLSLProgramDataManager&, const GrTextureDomain&, GrSurfaceProxy*);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000121
122 enum {
123 kDomainKeyBits = 2, // See DomainKey().
124 };
125
126 /**
egdaniel64c47282015-11-13 06:54:19 -0800127 * GrGLSLFragmentProcessor::GenKey() must call this and include the returned value in it's
egdaniel7dc4bd02015-10-29 07:57:01 -0700128 * computed key. The returned will be limited to the lower kDomainKeyBits bits.
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000129 */
bsalomon63e99f72014-07-21 08:03:14 -0700130 static uint32_t DomainKey(const GrTextureDomain& domain) {
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400131 GR_STATIC_ASSERT(kModeCount <= (1 << kDomainKeyBits));
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000132 return domain.mode();
133 }
134
135 private:
joshualittb5fb5af2015-08-25 12:10:54 -0700136 static const int kPrevDomainCount = 4;
egdaniel018fb622015-10-28 07:26:40 -0700137 SkDEBUGCODE(Mode fMode;)
138 GrGLSLProgramDataManager::UniformHandle fDomainUni;
139 SkString fDomainName;
140 float fPrevDomain[kPrevDomainCount];
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000141 };
142
143protected:
144 Mode fMode;
145 SkRect fDomain;
146 int fIndex;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000147};
148
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000149/**
150 * A basic texture effect that uses GrTextureDomain.
151 */
152class GrTextureDomainEffect : public GrSingleTextureEffect {
153
154public:
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400155 static sk_sp<GrFragmentProcessor> Make(sk_sp<GrTextureProxy>,
Robert Phillips40fd7c92017-01-30 08:06:27 -0500156 sk_sp<GrColorSpaceXform>,
157 const SkMatrix&,
158 const SkRect& domain,
159 GrTextureDomain::Mode,
160 GrSamplerParams::FilterMode filterMode);
161
mtklein36352bf2015-03-25 18:17:31 -0700162 const char* name() const override { return "TextureDomain"; }
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000163
robertphillipse004bfc2015-11-16 09:06:59 -0800164 SkString dumpInfo() const override {
165 SkString str;
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400166 str.appendf("Domain: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]",
robertphillipse004bfc2015-11-16 09:06:59 -0800167 fTextureDomain.domain().fLeft, fTextureDomain.domain().fTop,
168 fTextureDomain.domain().fRight, fTextureDomain.domain().fBottom);
169 str.append(INHERITED::dumpInfo());
170 return str;
171 }
172
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400173private:
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000174 GrTextureDomain fTextureDomain;
175
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400176 GrTextureDomainEffect(sk_sp<GrTextureProxy>,
Robert Phillips40fd7c92017-01-30 08:06:27 -0500177 sk_sp<GrColorSpaceXform>,
178 const SkMatrix&,
179 const SkRect& domain,
180 GrTextureDomain::Mode,
181 GrSamplerParams::FilterMode);
182
183 static OptimizationFlags OptFlags(GrPixelConfig config, GrTextureDomain::Mode mode);
Brian Salomon587e08f2017-01-27 10:59:27 -0500184
egdaniel57d3b032015-11-13 11:57:27 -0800185 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
wangyixb1daa862015-08-18 11:29:31 -0700186
Brian Salomon94efbf52016-11-29 13:43:05 -0500187 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
wangyix4b3050b2015-08-04 07:59:37 -0700188
mtklein36352bf2015-03-25 18:17:31 -0700189 bool onIsEqual(const GrFragmentProcessor&) const override;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000190
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400191 GR_DECLARE_FRAGMENT_PROCESSOR_TEST
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000192
193 typedef GrSingleTextureEffect INHERITED;
194};
195
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400196class GrDeviceSpaceTextureDecalFragmentProcessor : public GrFragmentProcessor {
197public:
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400198 static sk_sp<GrFragmentProcessor> Make(sk_sp<GrTextureProxy>,
Robert Phillips296b1cc2017-03-15 10:42:12 -0400199 const SkIRect& subset,
Robert Phillips40fd7c92017-01-30 08:06:27 -0500200 const SkIPoint& deviceSpaceOffset);
201
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400202 const char* name() const override { return "GrDeviceSpaceTextureDecalFragmentProcessor"; }
203
204 SkString dumpInfo() const override {
205 SkString str;
206 str.appendf("Domain: [L: %.2f, T: %.2f, R: %.2f, B: %.2f] Offset: [%d %d]",
207 fTextureDomain.domain().fLeft, fTextureDomain.domain().fTop,
208 fTextureDomain.domain().fRight, fTextureDomain.domain().fBottom,
209 fDeviceSpaceOffset.fX, fDeviceSpaceOffset.fY);
210 str.append(INHERITED::dumpInfo());
211 return str;
212 }
213
Brian Salomon1a2a7ab2017-07-26 13:11:51 -0400214 sk_sp<GrFragmentProcessor> clone() const override;
215
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400216private:
Brian Salomon0bbecb22016-11-17 11:38:22 -0500217 TextureSampler fTextureSampler;
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400218 GrTextureDomain fTextureDomain;
219 SkIPoint fDeviceSpaceOffset;
220
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400221 GrDeviceSpaceTextureDecalFragmentProcessor(sk_sp<GrTextureProxy>,
Robert Phillips40fd7c92017-01-30 08:06:27 -0500222 const SkIRect&, const SkIPoint&);
Brian Salomon1a2a7ab2017-07-26 13:11:51 -0400223 GrDeviceSpaceTextureDecalFragmentProcessor(const GrDeviceSpaceTextureDecalFragmentProcessor&);
Robert Phillips40fd7c92017-01-30 08:06:27 -0500224
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400225 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
226
227 // Since we always use decal mode, there is no need for key data.
Brian Salomon94efbf52016-11-29 13:43:05 -0500228 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400229
230 bool onIsEqual(const GrFragmentProcessor& fp) const override;
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400231
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400232 GR_DECLARE_FRAGMENT_PROCESSOR_TEST
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400233
234 typedef GrFragmentProcessor INHERITED;
235};
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000236#endif