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" |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 12 | #include "gl/GrGLProcessor.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 | |
| 72 | bool operator== (const GrTextureDomain& that) const { |
egdaniel | 01a492f | 2014-08-21 06:47:50 -0700 | [diff] [blame] | 73 | return fMode == that.fMode && (kIgnore_Mode == fMode || fDomain == that.fDomain); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 77 | * 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] | 78 | * should include this helper. It generates the texture domain GLSL, produces the part of the |
| 79 | * effect key that reflects the texture domain code, and performs the uniform uploads necessary |
| 80 | * for texture domains. |
| 81 | */ |
| 82 | class GLDomain { |
| 83 | public: |
| 84 | GLDomain() { |
| 85 | fPrevDomain[0] = SK_FloatNaN; |
| 86 | SkDEBUGCODE(fMode = (Mode) -1;) |
| 87 | } |
| 88 | |
| 89 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 90 | * Call this from GrGLProcessor::emitCode() to sample the texture W.R.T. the domain and |
| 91 | * mode. |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 92 | * |
| 93 | * @param outcolor name of vec4 variable to hold the sampled color. |
| 94 | * @param inCoords name of vec2 variable containing the coords to be used with the domain. |
| 95 | * It is assumed that this is a variable and not an expression. |
| 96 | * @param inModulateColor if non-NULL the sampled color will be modulated with this |
| 97 | * expression before being written to outColor. |
| 98 | */ |
| 99 | void sampleTexture(GrGLShaderBuilder* builder, |
| 100 | const GrTextureDomain& textureDomain, |
| 101 | const char* outColor, |
| 102 | const SkString& inCoords, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 103 | const GrGLProcessor::TextureSampler sampler, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 104 | const char* inModulateColor = NULL); |
| 105 | |
| 106 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 107 | * Call this from GrGLProcessor::setData() to upload uniforms necessary for the texture |
| 108 | * 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] | 109 | */ |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 110 | void setData(const GrGLProgramDataManager& pdman, const GrTextureDomain& textureDomain, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 111 | GrSurfaceOrigin textureOrigin); |
| 112 | |
| 113 | enum { |
| 114 | kDomainKeyBits = 2, // See DomainKey(). |
| 115 | }; |
| 116 | |
| 117 | /** |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 118 | * GrGLProcessor::GenKey() must call this and include the returned value in it's computed |
| 119 | * 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] | 120 | */ |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 121 | static uint32_t DomainKey(const GrTextureDomain& domain) { |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 122 | GR_STATIC_ASSERT(kModeCount <= 4); |
| 123 | return domain.mode(); |
| 124 | } |
| 125 | |
| 126 | private: |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 127 | SkDEBUGCODE(Mode fMode;) |
| 128 | GrGLProgramDataManager::UniformHandle fDomainUni; |
| 129 | SkString fDomainName; |
| 130 | GrGLfloat fPrevDomain[4]; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | protected: |
| 134 | Mode fMode; |
| 135 | SkRect fDomain; |
| 136 | int fIndex; |
| 137 | |
| 138 | typedef GrSingleTextureEffect INHERITED; |
| 139 | }; |
| 140 | |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 141 | /** |
| 142 | * A basic texture effect that uses GrTextureDomain. |
| 143 | */ |
| 144 | class GrTextureDomainEffect : public GrSingleTextureEffect { |
| 145 | |
| 146 | public: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 147 | static GrFragmentProcessor* Create(GrTexture*, |
| 148 | const SkMatrix&, |
| 149 | const SkRect& domain, |
| 150 | GrTextureDomain::Mode, |
| 151 | GrTextureParams::FilterMode filterMode, |
| 152 | GrCoordSet = kLocal_GrCoordSet); |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 153 | |
| 154 | virtual ~GrTextureDomainEffect(); |
| 155 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 156 | const char* name() const SK_OVERRIDE { return "TextureDomain"; } |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 157 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 158 | void getGLProcessorKey(const GrGLCaps&, GrProcessorKeyBuilder*) const SK_OVERRIDE; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 159 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 160 | GrGLFragmentProcessor* createGLInstance() const SK_OVERRIDE; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 161 | |
| 162 | const GrTextureDomain& textureDomain() const { return fTextureDomain; } |
| 163 | |
| 164 | protected: |
| 165 | GrTextureDomain fTextureDomain; |
| 166 | |
| 167 | private: |
| 168 | GrTextureDomainEffect(GrTexture*, |
| 169 | const SkMatrix&, |
| 170 | const SkRect& domain, |
| 171 | GrTextureDomain::Mode, |
| 172 | GrTextureParams::FilterMode, |
| 173 | GrCoordSet); |
| 174 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 175 | bool onIsEqual(const GrFragmentProcessor&) const SK_OVERRIDE; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 176 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 177 | void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE; |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 178 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 179 | GR_DECLARE_FRAGMENT_PROCESSOR_TEST; |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 180 | |
| 181 | typedef GrSingleTextureEffect INHERITED; |
| 182 | }; |
| 183 | |
| 184 | #endif |