commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 1 | /* |
| 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" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 12 | #include "glsl/GrGLSLFragmentProcessor.h" |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 13 | #include "glsl/GrGLSLProgramDataManager.h" |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 14 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 15 | class GrGLProgramBuilder; |
Brian Osman | c468963 | 2016-12-19 17:04:59 -0500 | [diff] [blame] | 16 | class GrGLSLColorSpaceXformHelper; |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 17 | class GrGLSLShaderBuilder; |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 18 | class GrInvariantOutput; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 19 | class GrGLSLUniformHandler; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 20 | struct 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 | */ |
| 28 | class GrTextureDomain { |
| 29 | public: |
| 30 | enum Mode { |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 31 | // 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.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 41 | |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 42 | kLastMode = kRepeat_Mode |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 43 | }; |
| 44 | static const int kModeCount = kLastMode + 1; |
| 45 | |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 46 | static const GrTextureDomain& IgnoredDomain() { |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 47 | static const GrTextureDomain gDomain((GrTextureProxy*)nullptr, |
| 48 | SkRect::MakeEmpty(), kIgnore_Mode); |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 49 | return gDomain; |
| 50 | } |
| 51 | |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 52 | /** |
| 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 Phillips | e98234f | 2017-01-09 14:23:59 -0500 | [diff] [blame] | 56 | GrTextureDomain(GrTexture*, const SkRect& domain, Mode, int index = -1); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 57 | |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 58 | GrTextureDomain(GrTextureProxy*, const SkRect& domain, Mode, int index = -1); |
| 59 | |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 60 | 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 Phillips | e98234f | 2017-01-09 14:23:59 -0500 | [diff] [blame] | 65 | static const SkRect MakeTexelDomain(const SkIRect& texelRect) { |
| 66 | return SkRect::Make(texelRect); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Robert Phillips | e98234f | 2017-01-09 14:23:59 -0500 | [diff] [blame] | 69 | static const SkRect MakeTexelDomainForMode(const SkIRect& texelRect, Mode mode) { |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 70 | // For Clamp mode, inset by half a texel. |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 71 | SkScalar inset = (mode == kClamp_Mode && !texelRect.isEmpty()) ? SK_ScalarHalf : 0; |
Robert Phillips | e98234f | 2017-01-09 14:23:59 -0500 | [diff] [blame] | 72 | return SkRect::MakeLTRB(texelRect.fLeft + inset, texelRect.fTop + inset, |
| 73 | texelRect.fRight - inset, texelRect.fBottom - inset); |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 76 | bool operator==(const GrTextureDomain& that) const { |
egdaniel | 01a492f | 2014-08-21 06:47:50 -0700 | [diff] [blame] | 77 | return fMode == that.fMode && (kIgnore_Mode == fMode || fDomain == that.fDomain); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 81 | * A GrGLSLFragmentProcessor subclass that corresponds to a GrProcessor subclass that uses |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 82 | * 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.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 85 | */ |
| 86 | class GLDomain { |
| 87 | public: |
| 88 | GLDomain() { |
joshualitt | b5fb5af | 2015-08-25 12:10:54 -0700 | [diff] [blame] | 89 | for (int i = 0; i < kPrevDomainCount; i++) { |
| 90 | fPrevDomain[i] = SK_FloatNaN; |
| 91 | } |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 92 | SkDEBUGCODE(fMode = (Mode) -1;) |
| 93 | } |
| 94 | |
| 95 | /** |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 96 | * Call this from GrGLSLFragmentProcessor::emitCode() to sample the texture W.R.T. the |
| 97 | * domain and mode. |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 98 | * |
| 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. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 102 | * @param inModulateColor if non-nullptr the sampled color will be modulated with this |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 103 | * expression before being written to outColor. |
| 104 | */ |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 105 | void sampleTexture(GrGLSLShaderBuilder* builder, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 106 | GrGLSLUniformHandler* uniformHandler, |
Brian Salomon | 1edc5b9 | 2016-11-29 13:43:46 -0500 | [diff] [blame] | 107 | const GrShaderCaps* shaderCaps, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 108 | const GrTextureDomain& textureDomain, |
| 109 | const char* outColor, |
| 110 | const SkString& inCoords, |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 111 | GrGLSLFragmentProcessor::SamplerHandle sampler, |
Brian Osman | c468963 | 2016-12-19 17:04:59 -0500 | [diff] [blame] | 112 | const char* inModulateColor = nullptr, |
| 113 | GrGLSLColorSpaceXformHelper* colorXformHelper = nullptr); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 114 | |
| 115 | /** |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 116 | * Call this from GrGLSLFragmentProcessor::setData() to upload uniforms necessary for the |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 117 | * texture domain. The rectangle is automatically adjusted to account for the texture's |
| 118 | * origin. |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 119 | */ |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 120 | void setData(const GrGLSLProgramDataManager& pdman, const GrTextureDomain& textureDomain, |
Robert Phillips | e98234f | 2017-01-09 14:23:59 -0500 | [diff] [blame] | 121 | GrTexture* texure); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 122 | |
| 123 | enum { |
| 124 | kDomainKeyBits = 2, // See DomainKey(). |
| 125 | }; |
| 126 | |
| 127 | /** |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 128 | * GrGLSLFragmentProcessor::GenKey() must call this and include the returned value in it's |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 129 | * computed key. The returned will be limited to the lower kDomainKeyBits bits. |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 130 | */ |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 131 | static uint32_t DomainKey(const GrTextureDomain& domain) { |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 132 | GR_STATIC_ASSERT(kModeCount <= (1 << kDomainKeyBits)); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 133 | return domain.mode(); |
| 134 | } |
| 135 | |
| 136 | private: |
joshualitt | b5fb5af | 2015-08-25 12:10:54 -0700 | [diff] [blame] | 137 | static const int kPrevDomainCount = 4; |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 138 | SkDEBUGCODE(Mode fMode;) |
| 139 | GrGLSLProgramDataManager::UniformHandle fDomainUni; |
| 140 | SkString fDomainName; |
| 141 | float fPrevDomain[kPrevDomainCount]; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | protected: |
| 145 | Mode fMode; |
| 146 | SkRect fDomain; |
| 147 | int fIndex; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 150 | /** |
| 151 | * A basic texture effect that uses GrTextureDomain. |
| 152 | */ |
| 153 | class GrTextureDomainEffect : public GrSingleTextureEffect { |
| 154 | |
| 155 | public: |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 156 | static sk_sp<GrFragmentProcessor> Make(GrTexture*, |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 157 | sk_sp<GrColorSpaceXform>, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 158 | const SkMatrix&, |
| 159 | const SkRect& domain, |
| 160 | GrTextureDomain::Mode, |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 161 | GrSamplerParams::FilterMode filterMode); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 162 | |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 163 | static sk_sp<GrFragmentProcessor> Make(GrResourceProvider*, |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 164 | sk_sp<GrTextureProxy>, |
| 165 | sk_sp<GrColorSpaceXform>, |
| 166 | const SkMatrix&, |
| 167 | const SkRect& domain, |
| 168 | GrTextureDomain::Mode, |
| 169 | GrSamplerParams::FilterMode filterMode); |
| 170 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 171 | const char* name() const override { return "TextureDomain"; } |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 172 | |
robertphillips | e004bfc | 2015-11-16 09:06:59 -0800 | [diff] [blame] | 173 | SkString dumpInfo() const override { |
| 174 | SkString str; |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 175 | str.appendf("Domain: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]", |
robertphillips | e004bfc | 2015-11-16 09:06:59 -0800 | [diff] [blame] | 176 | fTextureDomain.domain().fLeft, fTextureDomain.domain().fTop, |
| 177 | fTextureDomain.domain().fRight, fTextureDomain.domain().fBottom); |
| 178 | str.append(INHERITED::dumpInfo()); |
| 179 | return str; |
| 180 | } |
| 181 | |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 182 | private: |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 183 | GrTextureDomain fTextureDomain; |
| 184 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 185 | GrTextureDomainEffect(GrTexture*, |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 186 | sk_sp<GrColorSpaceXform>, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 187 | const SkMatrix&, |
| 188 | const SkRect& domain, |
| 189 | GrTextureDomain::Mode, |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 190 | GrSamplerParams::FilterMode); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 191 | |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 192 | GrTextureDomainEffect(GrResourceProvider*, |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 193 | sk_sp<GrTextureProxy>, |
| 194 | sk_sp<GrColorSpaceXform>, |
| 195 | const SkMatrix&, |
| 196 | const SkRect& domain, |
| 197 | GrTextureDomain::Mode, |
| 198 | GrSamplerParams::FilterMode); |
| 199 | |
| 200 | static OptimizationFlags OptFlags(GrPixelConfig config, GrTextureDomain::Mode mode); |
Brian Salomon | 587e08f | 2017-01-27 10:59:27 -0500 | [diff] [blame] | 201 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 202 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 203 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 204 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 205 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 206 | bool onIsEqual(const GrFragmentProcessor&) const override; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 207 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 208 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 209 | |
| 210 | typedef GrSingleTextureEffect INHERITED; |
| 211 | }; |
| 212 | |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 213 | class GrDeviceSpaceTextureDecalFragmentProcessor : public GrFragmentProcessor { |
| 214 | public: |
| 215 | static sk_sp<GrFragmentProcessor> Make(GrTexture*, const SkIRect& subset, |
| 216 | const SkIPoint& deviceSpaceOffset); |
| 217 | |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 218 | static sk_sp<GrFragmentProcessor> Make(GrResourceProvider*, sk_sp<GrTextureProxy>, |
| 219 | const SkIRect& subset, |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 220 | const SkIPoint& deviceSpaceOffset); |
| 221 | |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 222 | const char* name() const override { return "GrDeviceSpaceTextureDecalFragmentProcessor"; } |
| 223 | |
| 224 | SkString dumpInfo() const override { |
| 225 | SkString str; |
| 226 | str.appendf("Domain: [L: %.2f, T: %.2f, R: %.2f, B: %.2f] Offset: [%d %d]", |
| 227 | fTextureDomain.domain().fLeft, fTextureDomain.domain().fTop, |
| 228 | fTextureDomain.domain().fRight, fTextureDomain.domain().fBottom, |
| 229 | fDeviceSpaceOffset.fX, fDeviceSpaceOffset.fY); |
| 230 | str.append(INHERITED::dumpInfo()); |
| 231 | return str; |
| 232 | } |
| 233 | |
| 234 | private: |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 235 | TextureSampler fTextureSampler; |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 236 | GrTextureDomain fTextureDomain; |
| 237 | SkIPoint fDeviceSpaceOffset; |
| 238 | |
| 239 | GrDeviceSpaceTextureDecalFragmentProcessor(GrTexture*, const SkIRect&, const SkIPoint&); |
| 240 | |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 241 | GrDeviceSpaceTextureDecalFragmentProcessor(GrResourceProvider*, sk_sp<GrTextureProxy>, |
Robert Phillips | 40fd7c9 | 2017-01-30 08:06:27 -0500 | [diff] [blame] | 242 | const SkIRect&, const SkIPoint&); |
| 243 | |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 244 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
| 245 | |
| 246 | // Since we always use decal mode, there is no need for key data. |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 247 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 248 | |
| 249 | bool onIsEqual(const GrFragmentProcessor& fp) const override; |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 250 | |
| 251 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
| 252 | |
| 253 | typedef GrFragmentProcessor INHERITED; |
| 254 | }; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 255 | #endif |