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; |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 16 | class GrGLSLShaderBuilder; |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 17 | class GrInvariantOutput; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 18 | class GrGLSLUniformHandler; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 19 | struct SkRect; |
| 20 | |
| 21 | /** |
| 22 | * Limits a texture's lookup coordinates to a domain. Samples outside the domain are either clamped |
| 23 | * the edge of the domain or result in a vec4 of zeros (decal mode). The domain is clipped to |
| 24 | * normalized texture coords ([0,1]x[0,1] square). Bilinear filtering can cause texels outside the |
| 25 | * domain to affect the read value unless the caller considers this when calculating the domain. |
| 26 | */ |
| 27 | class GrTextureDomain { |
| 28 | public: |
| 29 | enum Mode { |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 30 | // Ignore the texture domain rectangle. |
| 31 | kIgnore_Mode, |
| 32 | // Clamp texture coords to the domain rectangle. |
| 33 | kClamp_Mode, |
| 34 | // Treat the area outside the domain rectangle as fully transparent. |
| 35 | kDecal_Mode, |
| 36 | // Wrap texture coordinates. NOTE: filtering may not work as expected because Bilerp will |
| 37 | // read texels outside of the domain. We could perform additional texture reads and filter |
| 38 | // in the shader, but are not currently doing this for performance reasons |
| 39 | kRepeat_Mode, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 40 | |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 41 | kLastMode = kRepeat_Mode |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 42 | }; |
| 43 | static const int kModeCount = kLastMode + 1; |
| 44 | |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 45 | static const GrTextureDomain& IgnoredDomain() { |
| 46 | static const SkRect gDummyRect = {0, 0, 0, 0}; |
| 47 | static const GrTextureDomain gDomain(gDummyRect, kIgnore_Mode); |
| 48 | return gDomain; |
| 49 | } |
| 50 | |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 51 | /** |
| 52 | * @param index Pass a value >= 0 if using multiple texture domains in the same effect. |
| 53 | * It is used to keep inserted variables from causing name collisions. |
| 54 | */ |
| 55 | GrTextureDomain(const SkRect& domain, Mode, int index = -1); |
| 56 | |
| 57 | const SkRect& domain() const { return fDomain; } |
| 58 | Mode mode() const { return fMode; } |
| 59 | |
| 60 | /* Computes a domain that bounds all the texels in texelRect. Note that with bilerp enabled |
| 61 | texels neighboring the domain may be read. */ |
| 62 | static const SkRect MakeTexelDomain(const GrTexture* texture, const SkIRect& texelRect) { |
| 63 | SkScalar wInv = SK_Scalar1 / texture->width(); |
| 64 | SkScalar hInv = SK_Scalar1 / texture->height(); |
| 65 | SkRect result = { |
| 66 | texelRect.fLeft * wInv, |
| 67 | texelRect.fTop * hInv, |
| 68 | texelRect.fRight * wInv, |
| 69 | texelRect.fBottom * hInv |
| 70 | }; |
| 71 | return result; |
| 72 | } |
| 73 | |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 74 | static const SkRect MakeTexelDomainForMode(const GrTexture* texture, const SkIRect& texelRect, Mode mode) { |
| 75 | // For Clamp mode, inset by half a texel. |
| 76 | SkScalar wInv = SK_Scalar1 / texture->width(); |
| 77 | SkScalar hInv = SK_Scalar1 / texture->height(); |
| 78 | SkScalar inset = (mode == kClamp_Mode && !texelRect.isEmpty()) ? SK_ScalarHalf : 0; |
| 79 | return SkRect::MakeLTRB( |
| 80 | (texelRect.fLeft + inset) * wInv, |
| 81 | (texelRect.fTop + inset) * hInv, |
| 82 | (texelRect.fRight - inset) * wInv, |
| 83 | (texelRect.fBottom - inset) * hInv |
| 84 | ); |
| 85 | } |
| 86 | |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 87 | bool operator==(const GrTextureDomain& that) const { |
egdaniel | 01a492f | 2014-08-21 06:47:50 -0700 | [diff] [blame] | 88 | return fMode == that.fMode && (kIgnore_Mode == fMode || fDomain == that.fDomain); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 92 | * A GrGLSLFragmentProcessor subclass that corresponds to a GrProcessor subclass that uses |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 93 | * GrTextureDomain should include this helper. It generates the texture domain GLSL, produces |
| 94 | * the part of the effect key that reflects the texture domain code, and performs the uniform |
| 95 | * uploads necessary for texture domains. |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 96 | */ |
| 97 | class GLDomain { |
| 98 | public: |
| 99 | GLDomain() { |
joshualitt | b5fb5af | 2015-08-25 12:10:54 -0700 | [diff] [blame] | 100 | for (int i = 0; i < kPrevDomainCount; i++) { |
| 101 | fPrevDomain[i] = SK_FloatNaN; |
| 102 | } |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 103 | SkDEBUGCODE(fMode = (Mode) -1;) |
| 104 | } |
| 105 | |
| 106 | /** |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 107 | * Call this from GrGLSLFragmentProcessor::emitCode() to sample the texture W.R.T. the |
| 108 | * domain and mode. |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 109 | * |
| 110 | * @param outcolor name of vec4 variable to hold the sampled color. |
| 111 | * @param inCoords name of vec2 variable containing the coords to be used with the domain. |
| 112 | * It is assumed that this is a variable and not an expression. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 113 | * @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] | 114 | * expression before being written to outColor. |
| 115 | */ |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 116 | void sampleTexture(GrGLSLShaderBuilder* builder, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 117 | GrGLSLUniformHandler* uniformHandler, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 118 | const GrShaderCaps* glslCaps, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 119 | const GrTextureDomain& textureDomain, |
| 120 | const char* outColor, |
| 121 | const SkString& inCoords, |
egdaniel | 09aa1fc | 2016-04-20 07:09:46 -0700 | [diff] [blame] | 122 | GrGLSLFragmentProcessor::SamplerHandle sampler, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 123 | const char* inModulateColor = nullptr); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 124 | |
| 125 | /** |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 126 | * Call this from GrGLSLFragmentProcessor::setData() to upload uniforms necessary for the |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 127 | * texture domain. The rectangle is automatically adjusted to account for the texture's |
| 128 | * origin. |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 129 | */ |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 130 | void setData(const GrGLSLProgramDataManager& pdman, const GrTextureDomain& textureDomain, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 131 | GrSurfaceOrigin textureOrigin); |
| 132 | |
| 133 | enum { |
| 134 | kDomainKeyBits = 2, // See DomainKey(). |
| 135 | }; |
| 136 | |
| 137 | /** |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 138 | * GrGLSLFragmentProcessor::GenKey() must call this and include the returned value in it's |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 139 | * 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] | 140 | */ |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 141 | static uint32_t DomainKey(const GrTextureDomain& domain) { |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 142 | GR_STATIC_ASSERT(kModeCount <= (1 << kDomainKeyBits)); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 143 | return domain.mode(); |
| 144 | } |
| 145 | |
| 146 | private: |
joshualitt | b5fb5af | 2015-08-25 12:10:54 -0700 | [diff] [blame] | 147 | static const int kPrevDomainCount = 4; |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 148 | SkDEBUGCODE(Mode fMode;) |
| 149 | GrGLSLProgramDataManager::UniformHandle fDomainUni; |
| 150 | SkString fDomainName; |
| 151 | float fPrevDomain[kPrevDomainCount]; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | protected: |
| 155 | Mode fMode; |
| 156 | SkRect fDomain; |
| 157 | int fIndex; |
| 158 | |
| 159 | typedef GrSingleTextureEffect INHERITED; |
| 160 | }; |
| 161 | |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 162 | /** |
| 163 | * A basic texture effect that uses GrTextureDomain. |
| 164 | */ |
| 165 | class GrTextureDomainEffect : public GrSingleTextureEffect { |
| 166 | |
| 167 | public: |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 168 | static sk_sp<GrFragmentProcessor> Make(GrTexture*, |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 169 | sk_sp<GrColorSpaceXform>, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 170 | const SkMatrix&, |
| 171 | const SkRect& domain, |
| 172 | GrTextureDomain::Mode, |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 173 | GrSamplerParams::FilterMode filterMode); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 174 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 175 | const char* name() const override { return "TextureDomain"; } |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 176 | |
robertphillips | e004bfc | 2015-11-16 09:06:59 -0800 | [diff] [blame] | 177 | SkString dumpInfo() const override { |
| 178 | SkString str; |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 179 | str.appendf("Domain: [L: %.2f, T: %.2f, R: %.2f, B: %.2f]", |
robertphillips | e004bfc | 2015-11-16 09:06:59 -0800 | [diff] [blame] | 180 | fTextureDomain.domain().fLeft, fTextureDomain.domain().fTop, |
| 181 | fTextureDomain.domain().fRight, fTextureDomain.domain().fBottom); |
| 182 | str.append(INHERITED::dumpInfo()); |
| 183 | return str; |
| 184 | } |
| 185 | |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 186 | private: |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 187 | GrTextureDomain fTextureDomain; |
| 188 | |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 189 | GrTextureDomainEffect(GrTexture*, |
brianosman | 54f30c1 | 2016-07-18 10:53:52 -0700 | [diff] [blame] | 190 | sk_sp<GrColorSpaceXform>, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 191 | const SkMatrix&, |
| 192 | const SkRect& domain, |
| 193 | GrTextureDomain::Mode, |
Brian Salomon | 514baff | 2016-11-17 15:17:07 -0500 | [diff] [blame] | 194 | GrSamplerParams::FilterMode); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 195 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 196 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 197 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 198 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 199 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 200 | bool onIsEqual(const GrFragmentProcessor&) const override; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 201 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 202 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override; |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 203 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 204 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 205 | |
| 206 | typedef GrSingleTextureEffect INHERITED; |
| 207 | }; |
| 208 | |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 209 | class GrDeviceSpaceTextureDecalFragmentProcessor : public GrFragmentProcessor { |
| 210 | public: |
| 211 | static sk_sp<GrFragmentProcessor> Make(GrTexture*, const SkIRect& subset, |
| 212 | const SkIPoint& deviceSpaceOffset); |
| 213 | |
| 214 | const char* name() const override { return "GrDeviceSpaceTextureDecalFragmentProcessor"; } |
| 215 | |
| 216 | SkString dumpInfo() const override { |
| 217 | SkString str; |
| 218 | str.appendf("Domain: [L: %.2f, T: %.2f, R: %.2f, B: %.2f] Offset: [%d %d]", |
| 219 | fTextureDomain.domain().fLeft, fTextureDomain.domain().fTop, |
| 220 | fTextureDomain.domain().fRight, fTextureDomain.domain().fBottom, |
| 221 | fDeviceSpaceOffset.fX, fDeviceSpaceOffset.fY); |
| 222 | str.append(INHERITED::dumpInfo()); |
| 223 | return str; |
| 224 | } |
| 225 | |
| 226 | private: |
Brian Salomon | 0bbecb2 | 2016-11-17 11:38:22 -0500 | [diff] [blame] | 227 | TextureSampler fTextureSampler; |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 228 | GrTextureDomain fTextureDomain; |
| 229 | SkIPoint fDeviceSpaceOffset; |
| 230 | |
| 231 | GrDeviceSpaceTextureDecalFragmentProcessor(GrTexture*, const SkIRect&, const SkIPoint&); |
| 232 | |
| 233 | GrGLSLFragmentProcessor* onCreateGLSLInstance() const override; |
| 234 | |
| 235 | // 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] | 236 | void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override {} |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 237 | |
| 238 | bool onIsEqual(const GrFragmentProcessor& fp) const override; |
| 239 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override; |
| 240 | |
| 241 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
| 242 | |
| 243 | typedef GrFragmentProcessor INHERITED; |
| 244 | }; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 245 | #endif |