blob: 72204b1f8ea954fba31f2171da2519a7a3cbb2c1 [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() {
47 static const SkRect gDummyRect = {0, 0, 0, 0};
48 static const GrTextureDomain gDomain(gDummyRect, kIgnore_Mode);
49 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 */
56 GrTextureDomain(const SkRect& domain, Mode, int index = -1);
57
58 const SkRect& domain() const { return fDomain; }
59 Mode mode() const { return fMode; }
60
61 /* Computes a domain that bounds all the texels in texelRect. Note that with bilerp enabled
62 texels neighboring the domain may be read. */
63 static const SkRect MakeTexelDomain(const GrTexture* texture, const SkIRect& texelRect) {
64 SkScalar wInv = SK_Scalar1 / texture->width();
65 SkScalar hInv = SK_Scalar1 / texture->height();
66 SkRect result = {
67 texelRect.fLeft * wInv,
68 texelRect.fTop * hInv,
69 texelRect.fRight * wInv,
70 texelRect.fBottom * hInv
71 };
72 return result;
73 }
74
senorblancod0d37ca2015-04-02 04:54:56 -070075 static const SkRect MakeTexelDomainForMode(const GrTexture* texture, const SkIRect& texelRect, Mode mode) {
76 // For Clamp mode, inset by half a texel.
77 SkScalar wInv = SK_Scalar1 / texture->width();
78 SkScalar hInv = SK_Scalar1 / texture->height();
79 SkScalar inset = (mode == kClamp_Mode && !texelRect.isEmpty()) ? SK_ScalarHalf : 0;
80 return SkRect::MakeLTRB(
81 (texelRect.fLeft + inset) * wInv,
82 (texelRect.fTop + inset) * hInv,
83 (texelRect.fRight - inset) * wInv,
84 (texelRect.fBottom - inset) * hInv
85 );
86 }
87
Brian Salomon2ebd0c82016-10-03 17:15:28 -040088 bool operator==(const GrTextureDomain& that) const {
egdaniel01a492f2014-08-21 06:47:50 -070089 return fMode == that.fMode && (kIgnore_Mode == fMode || fDomain == that.fDomain);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000090 }
91
92 /**
egdaniel64c47282015-11-13 06:54:19 -080093 * A GrGLSLFragmentProcessor subclass that corresponds to a GrProcessor subclass that uses
egdaniel7dc4bd02015-10-29 07:57:01 -070094 * GrTextureDomain should include this helper. It generates the texture domain GLSL, produces
95 * the part of the effect key that reflects the texture domain code, and performs the uniform
96 * uploads necessary for texture domains.
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000097 */
98 class GLDomain {
99 public:
100 GLDomain() {
joshualittb5fb5af2015-08-25 12:10:54 -0700101 for (int i = 0; i < kPrevDomainCount; i++) {
102 fPrevDomain[i] = SK_FloatNaN;
103 }
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000104 SkDEBUGCODE(fMode = (Mode) -1;)
105 }
106
107 /**
egdaniel64c47282015-11-13 06:54:19 -0800108 * Call this from GrGLSLFragmentProcessor::emitCode() to sample the texture W.R.T. the
109 * domain and mode.
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000110 *
111 * @param outcolor name of vec4 variable to hold the sampled color.
112 * @param inCoords name of vec2 variable containing the coords to be used with the domain.
113 * It is assumed that this is a variable and not an expression.
halcanary96fcdcc2015-08-27 07:41:13 -0700114 * @param inModulateColor if non-nullptr the sampled color will be modulated with this
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000115 * expression before being written to outColor.
116 */
egdaniel2d721d32015-11-11 13:06:05 -0800117 void sampleTexture(GrGLSLShaderBuilder* builder,
egdaniel7ea439b2015-12-03 09:20:44 -0800118 GrGLSLUniformHandler* uniformHandler,
Brian Salomon1edc5b92016-11-29 13:43:46 -0500119 const GrShaderCaps* shaderCaps,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000120 const GrTextureDomain& textureDomain,
121 const char* outColor,
122 const SkString& inCoords,
egdaniel09aa1fc2016-04-20 07:09:46 -0700123 GrGLSLFragmentProcessor::SamplerHandle sampler,
Brian Osmanc4689632016-12-19 17:04:59 -0500124 const char* inModulateColor = nullptr,
125 GrGLSLColorSpaceXformHelper* colorXformHelper = nullptr);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000126
127 /**
egdaniel64c47282015-11-13 06:54:19 -0800128 * Call this from GrGLSLFragmentProcessor::setData() to upload uniforms necessary for the
egdaniel7dc4bd02015-10-29 07:57:01 -0700129 * texture domain. The rectangle is automatically adjusted to account for the texture's
130 * origin.
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000131 */
egdaniel018fb622015-10-28 07:26:40 -0700132 void setData(const GrGLSLProgramDataManager& pdman, const GrTextureDomain& textureDomain,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000133 GrSurfaceOrigin textureOrigin);
134
135 enum {
136 kDomainKeyBits = 2, // See DomainKey().
137 };
138
139 /**
egdaniel64c47282015-11-13 06:54:19 -0800140 * GrGLSLFragmentProcessor::GenKey() must call this and include the returned value in it's
egdaniel7dc4bd02015-10-29 07:57:01 -0700141 * computed key. The returned will be limited to the lower kDomainKeyBits bits.
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000142 */
bsalomon63e99f72014-07-21 08:03:14 -0700143 static uint32_t DomainKey(const GrTextureDomain& domain) {
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400144 GR_STATIC_ASSERT(kModeCount <= (1 << kDomainKeyBits));
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000145 return domain.mode();
146 }
147
148 private:
joshualittb5fb5af2015-08-25 12:10:54 -0700149 static const int kPrevDomainCount = 4;
egdaniel018fb622015-10-28 07:26:40 -0700150 SkDEBUGCODE(Mode fMode;)
151 GrGLSLProgramDataManager::UniformHandle fDomainUni;
152 SkString fDomainName;
153 float fPrevDomain[kPrevDomainCount];
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000154 };
155
156protected:
157 Mode fMode;
158 SkRect fDomain;
159 int fIndex;
160
161 typedef GrSingleTextureEffect INHERITED;
162};
163
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000164/**
165 * A basic texture effect that uses GrTextureDomain.
166 */
167class GrTextureDomainEffect : public GrSingleTextureEffect {
168
169public:
bungeman06ca8ec2016-06-09 08:01:03 -0700170 static sk_sp<GrFragmentProcessor> Make(GrTexture*,
brianosman54f30c12016-07-18 10:53:52 -0700171 sk_sp<GrColorSpaceXform>,
bungeman06ca8ec2016-06-09 08:01:03 -0700172 const SkMatrix&,
173 const SkRect& domain,
174 GrTextureDomain::Mode,
Brian Salomon514baff2016-11-17 15:17:07 -0500175 GrSamplerParams::FilterMode filterMode);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000176
mtklein36352bf2015-03-25 18:17:31 -0700177 const char* name() const override { return "TextureDomain"; }
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000178
robertphillipse004bfc2015-11-16 09:06:59 -0800179 SkString dumpInfo() const override {
180 SkString str;
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400181 str.appendf("Domain: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]",
robertphillipse004bfc2015-11-16 09:06:59 -0800182 fTextureDomain.domain().fLeft, fTextureDomain.domain().fTop,
183 fTextureDomain.domain().fRight, fTextureDomain.domain().fBottom);
184 str.append(INHERITED::dumpInfo());
185 return str;
186 }
187
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400188private:
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000189 GrTextureDomain fTextureDomain;
190
bsalomon4a339522015-10-06 08:40:50 -0700191 GrTextureDomainEffect(GrTexture*,
brianosman54f30c12016-07-18 10:53:52 -0700192 sk_sp<GrColorSpaceXform>,
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000193 const SkMatrix&,
194 const SkRect& domain,
195 GrTextureDomain::Mode,
Brian Salomon514baff2016-11-17 15:17:07 -0500196 GrSamplerParams::FilterMode);
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000197
egdaniel57d3b032015-11-13 11:57:27 -0800198 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
wangyixb1daa862015-08-18 11:29:31 -0700199
Brian Salomon94efbf52016-11-29 13:43:05 -0500200 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
wangyix4b3050b2015-08-04 07:59:37 -0700201
mtklein36352bf2015-03-25 18:17:31 -0700202 bool onIsEqual(const GrFragmentProcessor&) const override;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000203
mtklein36352bf2015-03-25 18:17:31 -0700204 void onComputeInvariantOutput(GrInvariantOutput* inout) const override;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700205
joshualittb0a8a372014-09-23 09:50:21 -0700206 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000207
208 typedef GrSingleTextureEffect INHERITED;
209};
210
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400211class GrDeviceSpaceTextureDecalFragmentProcessor : public GrFragmentProcessor {
212public:
213 static sk_sp<GrFragmentProcessor> Make(GrTexture*, const SkIRect& subset,
214 const SkIPoint& deviceSpaceOffset);
215
216 const char* name() const override { return "GrDeviceSpaceTextureDecalFragmentProcessor"; }
217
218 SkString dumpInfo() const override {
219 SkString str;
220 str.appendf("Domain: [L: %.2f, T: %.2f, R: %.2f, B: %.2f] Offset: [%d %d]",
221 fTextureDomain.domain().fLeft, fTextureDomain.domain().fTop,
222 fTextureDomain.domain().fRight, fTextureDomain.domain().fBottom,
223 fDeviceSpaceOffset.fX, fDeviceSpaceOffset.fY);
224 str.append(INHERITED::dumpInfo());
225 return str;
226 }
227
228private:
Brian Salomon0bbecb22016-11-17 11:38:22 -0500229 TextureSampler fTextureSampler;
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400230 GrTextureDomain fTextureDomain;
231 SkIPoint fDeviceSpaceOffset;
232
233 GrDeviceSpaceTextureDecalFragmentProcessor(GrTexture*, const SkIRect&, const SkIPoint&);
234
235 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
236
237 // Since we always use decal mode, there is no need for key data.
Brian Salomon94efbf52016-11-29 13:43:05 -0500238 void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {}
Brian Salomon2ebd0c82016-10-03 17:15:28 -0400239
240 bool onIsEqual(const GrFragmentProcessor& fp) const override;
241 void onComputeInvariantOutput(GrInvariantOutput* inout) const override;
242
243 GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
244
245 typedef GrFragmentProcessor INHERITED;
246};
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +0000247#endif