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" |
wangyix | 6af0c93 | 2015-07-22 10:21:17 -0700 | [diff] [blame] | 12 | #include "gl/GrGLFragmentProcessor.h" |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 13 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 14 | class GrGLProgramBuilder; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 15 | class GrGLShaderBuilder; |
egdaniel | 605dd0f | 2014-11-12 08:35:25 -0800 | [diff] [blame] | 16 | class GrInvariantOutput; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 17 | struct SkRect; |
| 18 | |
| 19 | /** |
| 20 | * Limits a texture's lookup coordinates to a domain. Samples outside the domain are either clamped |
| 21 | * the edge of the domain or result in a vec4 of zeros (decal mode). The domain is clipped to |
| 22 | * normalized texture coords ([0,1]x[0,1] square). Bilinear filtering can cause texels outside the |
| 23 | * domain to affect the read value unless the caller considers this when calculating the domain. |
| 24 | */ |
| 25 | class GrTextureDomain { |
| 26 | public: |
| 27 | enum Mode { |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 28 | // Ignore the texture domain rectangle. |
| 29 | kIgnore_Mode, |
| 30 | // Clamp texture coords to the domain rectangle. |
| 31 | kClamp_Mode, |
| 32 | // Treat the area outside the domain rectangle as fully transparent. |
| 33 | kDecal_Mode, |
| 34 | // Wrap texture coordinates. NOTE: filtering may not work as expected because Bilerp will |
| 35 | // read texels outside of the domain. We could perform additional texture reads and filter |
| 36 | // in the shader, but are not currently doing this for performance reasons |
| 37 | kRepeat_Mode, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 38 | |
joshualitt | 5ae5fc5 | 2014-07-29 12:59:27 -0700 | [diff] [blame] | 39 | kLastMode = kRepeat_Mode |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 40 | }; |
| 41 | static const int kModeCount = kLastMode + 1; |
| 42 | |
commit-bot@chromium.org | 7d7f314 | 2013-12-16 15:18:11 +0000 | [diff] [blame] | 43 | static const GrTextureDomain& IgnoredDomain() { |
| 44 | static const SkRect gDummyRect = {0, 0, 0, 0}; |
| 45 | static const GrTextureDomain gDomain(gDummyRect, kIgnore_Mode); |
| 46 | return gDomain; |
| 47 | } |
| 48 | |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 49 | /** |
| 50 | * @param index Pass a value >= 0 if using multiple texture domains in the same effect. |
| 51 | * It is used to keep inserted variables from causing name collisions. |
| 52 | */ |
| 53 | GrTextureDomain(const SkRect& domain, Mode, int index = -1); |
| 54 | |
| 55 | const SkRect& domain() const { return fDomain; } |
| 56 | Mode mode() const { return fMode; } |
| 57 | |
| 58 | /* Computes a domain that bounds all the texels in texelRect. Note that with bilerp enabled |
| 59 | texels neighboring the domain may be read. */ |
| 60 | static const SkRect MakeTexelDomain(const GrTexture* texture, const SkIRect& texelRect) { |
| 61 | SkScalar wInv = SK_Scalar1 / texture->width(); |
| 62 | SkScalar hInv = SK_Scalar1 / texture->height(); |
| 63 | SkRect result = { |
| 64 | texelRect.fLeft * wInv, |
| 65 | texelRect.fTop * hInv, |
| 66 | texelRect.fRight * wInv, |
| 67 | texelRect.fBottom * hInv |
| 68 | }; |
| 69 | return result; |
| 70 | } |
| 71 | |
senorblanco | d0d37ca | 2015-04-02 04:54:56 -0700 | [diff] [blame] | 72 | static const SkRect MakeTexelDomainForMode(const GrTexture* texture, const SkIRect& texelRect, Mode mode) { |
| 73 | // For Clamp mode, inset by half a texel. |
| 74 | SkScalar wInv = SK_Scalar1 / texture->width(); |
| 75 | SkScalar hInv = SK_Scalar1 / texture->height(); |
| 76 | SkScalar inset = (mode == kClamp_Mode && !texelRect.isEmpty()) ? SK_ScalarHalf : 0; |
| 77 | return SkRect::MakeLTRB( |
| 78 | (texelRect.fLeft + inset) * wInv, |
| 79 | (texelRect.fTop + inset) * hInv, |
| 80 | (texelRect.fRight - inset) * wInv, |
| 81 | (texelRect.fBottom - inset) * hInv |
| 82 | ); |
| 83 | } |
| 84 | |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 85 | bool operator== (const GrTextureDomain& that) const { |
egdaniel | 01a492f | 2014-08-21 06:47:50 -0700 | [diff] [blame] | 86 | return fMode == that.fMode && (kIgnore_Mode == fMode || fDomain == that.fDomain); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 90 | * A GrGLProcessor subclass that corresponds to a GrProcessor subclass that uses GrTextureDomain |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 91 | * should include this helper. It generates the texture domain GLSL, produces the part of the |
| 92 | * effect key that reflects the texture domain code, and performs the uniform uploads necessary |
| 93 | * for texture domains. |
| 94 | */ |
| 95 | class GLDomain { |
| 96 | public: |
| 97 | GLDomain() { |
joshualitt | b5fb5af | 2015-08-25 12:10:54 -0700 | [diff] [blame^] | 98 | for (int i = 0; i < kPrevDomainCount; i++) { |
| 99 | fPrevDomain[i] = SK_FloatNaN; |
| 100 | } |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 101 | SkDEBUGCODE(fMode = (Mode) -1;) |
| 102 | } |
| 103 | |
| 104 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 105 | * Call this from GrGLProcessor::emitCode() to sample the texture W.R.T. the domain and |
| 106 | * mode. |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 107 | * |
| 108 | * @param outcolor name of vec4 variable to hold the sampled color. |
| 109 | * @param inCoords name of vec2 variable containing the coords to be used with the domain. |
| 110 | * It is assumed that this is a variable and not an expression. |
| 111 | * @param inModulateColor if non-NULL the sampled color will be modulated with this |
| 112 | * expression before being written to outColor. |
| 113 | */ |
| 114 | void sampleTexture(GrGLShaderBuilder* builder, |
| 115 | const GrTextureDomain& textureDomain, |
| 116 | const char* outColor, |
| 117 | const SkString& inCoords, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 118 | const GrGLProcessor::TextureSampler sampler, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 119 | const char* inModulateColor = NULL); |
| 120 | |
| 121 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 122 | * Call this from GrGLProcessor::setData() to upload uniforms necessary for the texture |
| 123 | * domain. The rectangle is automatically adjusted to account for the texture's origin. |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 124 | */ |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 125 | void setData(const GrGLProgramDataManager& pdman, const GrTextureDomain& textureDomain, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 126 | GrSurfaceOrigin textureOrigin); |
| 127 | |
| 128 | enum { |
| 129 | kDomainKeyBits = 2, // See DomainKey(). |
| 130 | }; |
| 131 | |
| 132 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 133 | * GrGLProcessor::GenKey() must call this and include the returned value in it's computed |
| 134 | * 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] | 135 | */ |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 136 | static uint32_t DomainKey(const GrTextureDomain& domain) { |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 137 | GR_STATIC_ASSERT(kModeCount <= 4); |
| 138 | return domain.mode(); |
| 139 | } |
| 140 | |
| 141 | private: |
joshualitt | b5fb5af | 2015-08-25 12:10:54 -0700 | [diff] [blame^] | 142 | static const int kPrevDomainCount = 4; |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 143 | SkDEBUGCODE(Mode fMode;) |
| 144 | GrGLProgramDataManager::UniformHandle fDomainUni; |
| 145 | SkString fDomainName; |
joshualitt | b5fb5af | 2015-08-25 12:10:54 -0700 | [diff] [blame^] | 146 | GrGLfloat fPrevDomain[kPrevDomainCount]; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | protected: |
| 150 | Mode fMode; |
| 151 | SkRect fDomain; |
| 152 | int fIndex; |
| 153 | |
| 154 | typedef GrSingleTextureEffect INHERITED; |
| 155 | }; |
| 156 | |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 157 | /** |
| 158 | * A basic texture effect that uses GrTextureDomain. |
| 159 | */ |
| 160 | class GrTextureDomainEffect : public GrSingleTextureEffect { |
| 161 | |
| 162 | public: |
joshualitt | 5f10b5c | 2015-07-09 10:24:35 -0700 | [diff] [blame] | 163 | static GrFragmentProcessor* Create(GrProcessorDataManager*, |
| 164 | GrTexture*, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 165 | const SkMatrix&, |
| 166 | const SkRect& domain, |
| 167 | GrTextureDomain::Mode, |
| 168 | GrTextureParams::FilterMode filterMode, |
| 169 | GrCoordSet = kLocal_GrCoordSet); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 170 | |
| 171 | virtual ~GrTextureDomainEffect(); |
| 172 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 173 | const char* name() const override { return "TextureDomain"; } |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 174 | |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 175 | const GrTextureDomain& textureDomain() const { return fTextureDomain; } |
| 176 | |
| 177 | protected: |
| 178 | GrTextureDomain fTextureDomain; |
| 179 | |
| 180 | private: |
joshualitt | 5f10b5c | 2015-07-09 10:24:35 -0700 | [diff] [blame] | 181 | GrTextureDomainEffect(GrProcessorDataManager*, |
| 182 | GrTexture*, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 183 | const SkMatrix&, |
| 184 | const SkRect& domain, |
| 185 | GrTextureDomain::Mode, |
| 186 | GrTextureParams::FilterMode, |
| 187 | GrCoordSet); |
| 188 | |
wangyix | b1daa86 | 2015-08-18 11:29:31 -0700 | [diff] [blame] | 189 | GrGLFragmentProcessor* onCreateGLInstance() const override; |
| 190 | |
wangyix | 4b3050b | 2015-08-04 07:59:37 -0700 | [diff] [blame] | 191 | void onGetGLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override; |
| 192 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 193 | bool onIsEqual(const GrFragmentProcessor&) const override; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 194 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 195 | void onComputeInvariantOutput(GrInvariantOutput* inout) const override; |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 196 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 197 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 198 | |
| 199 | typedef GrSingleTextureEffect INHERITED; |
| 200 | }; |
| 201 | |
| 202 | #endif |