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 | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 18 | class GrGLSLTextureSampler; |
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() { |
| 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.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 | */ |
| 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 | |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 75 | 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 | |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 88 | bool operator== (const GrTextureDomain& that) const { |
egdaniel | 01a492f | 2014-08-21 06:47:50 -0700 | [diff] [blame] | 89 | return fMode == that.fMode && (kIgnore_Mode == fMode || fDomain == that.fDomain); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /** |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 93 | * A GrGLSLFragmentProcessor subclass that corresponds to a GrProcessor subclass that uses |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 94 | * 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.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 97 | */ |
| 98 | class GLDomain { |
| 99 | public: |
| 100 | GLDomain() { |
joshualitt | b5fb5af | 2015-08-25 12:10:54 -0700 | [diff] [blame] | 101 | for (int i = 0; i < kPrevDomainCount; i++) { |
| 102 | fPrevDomain[i] = SK_FloatNaN; |
| 103 | } |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 104 | SkDEBUGCODE(fMode = (Mode) -1;) |
| 105 | } |
| 106 | |
| 107 | /** |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 108 | * Call this from GrGLSLFragmentProcessor::emitCode() to sample the texture W.R.T. the |
| 109 | * domain and mode. |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 110 | * |
| 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. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 114 | * @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] | 115 | * expression before being written to outColor. |
| 116 | */ |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 117 | void sampleTexture(GrGLSLShaderBuilder* builder, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 118 | GrGLSLUniformHandler* uniformHandler, |
egdaniel | a2e3e0f | 2015-11-19 07:23:45 -0800 | [diff] [blame] | 119 | const GrGLSLCaps* glslCaps, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 120 | const GrTextureDomain& textureDomain, |
| 121 | const char* outColor, |
| 122 | const SkString& inCoords, |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 123 | const GrGLSLTextureSampler& sampler, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 124 | const char* inModulateColor = nullptr); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 125 | |
| 126 | /** |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 127 | * Call this from GrGLSLFragmentProcessor::setData() to upload uniforms necessary for the |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 128 | * texture domain. The rectangle is automatically adjusted to account for the texture's |
| 129 | * origin. |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 130 | */ |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 131 | void setData(const GrGLSLProgramDataManager& pdman, const GrTextureDomain& textureDomain, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 132 | GrSurfaceOrigin textureOrigin); |
| 133 | |
| 134 | enum { |
| 135 | kDomainKeyBits = 2, // See DomainKey(). |
| 136 | }; |
| 137 | |
| 138 | /** |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 139 | * GrGLSLFragmentProcessor::GenKey() must call this and include the returned value in it's |
egdaniel | 7dc4bd0 | 2015-10-29 07:57:01 -0700 | [diff] [blame] | 140 | * 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] | 141 | */ |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 142 | static uint32_t DomainKey(const GrTextureDomain& domain) { |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 143 | GR_STATIC_ASSERT(kModeCount <= 4); |
| 144 | return domain.mode(); |
| 145 | } |
| 146 | |
| 147 | private: |
joshualitt | b5fb5af | 2015-08-25 12:10:54 -0700 | [diff] [blame] | 148 | static const int kPrevDomainCount = 4; |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 149 | SkDEBUGCODE(Mode fMode;) |
| 150 | GrGLSLProgramDataManager::UniformHandle fDomainUni; |
| 151 | SkString fDomainName; |
| 152 | float fPrevDomain[kPrevDomainCount]; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | protected: |
| 156 | Mode fMode; |
| 157 | SkRect fDomain; |
| 158 | int fIndex; |
| 159 | |
| 160 | typedef GrSingleTextureEffect INHERITED; |
| 161 | }; |
| 162 | |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 163 | /** |
| 164 | * A basic texture effect that uses GrTextureDomain. |
| 165 | */ |
| 166 | class GrTextureDomainEffect : public GrSingleTextureEffect { |
| 167 | |
| 168 | public: |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 169 | static const GrFragmentProcessor* Create(GrTexture*, |
| 170 | const SkMatrix&, |
| 171 | const SkRect& domain, |
| 172 | GrTextureDomain::Mode, |
| 173 | GrTextureParams::FilterMode filterMode, |
| 174 | GrCoordSet = kLocal_GrCoordSet); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 175 | |
| 176 | virtual ~GrTextureDomainEffect(); |
| 177 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 178 | const char* name() const override { return "TextureDomain"; } |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 179 | |
robertphillips | e004bfc | 2015-11-16 09:06:59 -0800 | [diff] [blame] | 180 | SkString dumpInfo() const override { |
| 181 | SkString str; |
| 182 | str.appendf("Domain: [L: %.2f, T: %.2f, R: %.2f, B: %.2f] ", |
| 183 | fTextureDomain.domain().fLeft, fTextureDomain.domain().fTop, |
| 184 | fTextureDomain.domain().fRight, fTextureDomain.domain().fBottom); |
| 185 | str.append(INHERITED::dumpInfo()); |
| 186 | return str; |
| 187 | } |
| 188 | |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 189 | const GrTextureDomain& textureDomain() const { return fTextureDomain; } |
| 190 | |
| 191 | protected: |
| 192 | GrTextureDomain fTextureDomain; |
| 193 | |
| 194 | private: |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 195 | GrTextureDomainEffect(GrTexture*, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 196 | const SkMatrix&, |
| 197 | const SkRect& domain, |
| 198 | GrTextureDomain::Mode, |
| 199 | GrTextureParams::FilterMode, |
| 200 | GrCoordSet); |
| 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 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 204 | void onGetGLSLProcessorKey(const GrGLSLCaps&, 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 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 208 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override; |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 209 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 210 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 211 | |
| 212 | typedef GrSingleTextureEffect INHERITED; |
| 213 | }; |
| 214 | |
| 215 | #endif |