2 * Copyright 2017 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef GrTextureEffect_DEFINED
9 #define GrTextureEffect_DEFINED
11 #include "include/core/SkImageInfo.h"
12 #include "include/core/SkMatrix.h"
13 #include "src/gpu/ganesh/GrFragmentProcessor.h"
14 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
15 #include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h"
17 class GrTextureEffect : public GrFragmentProcessor {
19 inline static constexpr float kDefaultBorder[4] = {0};
21 /** Make from a filter. The sampler will be configured with clamp mode. */
22 static std::unique_ptr<GrFragmentProcessor> Make(
25 const SkMatrix& = SkMatrix::I(),
26 GrSamplerState::Filter = GrSamplerState::Filter::kNearest,
27 GrSamplerState::MipmapMode mipmapMode = GrSamplerState::MipmapMode::kNone);
30 * Make from a full GrSamplerState. Caps are required to determine support for kClampToBorder.
31 * This will be emulated in the shader if there is no hardware support.
33 static std::unique_ptr<GrFragmentProcessor> Make(GrSurfaceProxyView, SkAlphaType,
34 const SkMatrix&, GrSamplerState,
36 const float border[4] = kDefaultBorder);
39 * Makes a texture effect that samples a subset of a texture. The wrap modes of the
40 * GrSampleState are applied to the subset in the shader rather than using HW samplers.
41 * The 'subset' parameter specifies the texels in the base level. The shader code will
42 * avoid allowing linear filtering to read outside the texel window. However, if MIP
43 * filtering is used and a shader invocation reads from a level other than the base
44 * then it may read texel values that were computed from in part from base level texels
45 * outside the window. More specifically, we treat the MIP map case exactly like the
46 * linear case in terms of how the final texture coords are computed. If
47 * alwaysUseShaderTileMode is true then MakeSubset won't attempt to use HW wrap modes if the
48 * subset contains the entire texture.
50 static std::unique_ptr<GrFragmentProcessor> MakeSubset(GrSurfaceProxyView,
56 const float border[4] = kDefaultBorder,
57 bool alwaysUseShaderTileMode = false);
60 * The same as above but also takes a 'domain' that specifies any known limit on the post-
61 * matrix texture coords that will be used to sample the texture. Specifying this requires
62 * knowledge of how this effect will be nested into a paint, the local coords used with the
63 * draw, etc. It is only used to attempt to optimize away the shader subset calculations.
65 static std::unique_ptr<GrFragmentProcessor> MakeSubset(GrSurfaceProxyView,
72 const float border[4] = kDefaultBorder);
75 * Like MakeSubset() but always uses kLinear filtering. MakeSubset() uses the subset rect
76 * dimensions to determine the period of the wrap mode (for repeat and mirror). Once it computes
77 * the wrapped texture coordinate inside subset rect it further clamps it to a 0.5 inset rect of
78 * subset. When subset is an integer rectangle this clamping avoids the hw linear filtering from
79 * reading texels just outside the subset rect. This factory allows a custom inset clamping
80 * distance rather than 0.5, allowing those neighboring texels to influence the linear filtering
81 * sample result. If there is a known restriction on the post-matrix texture coords it can be
82 * specified using domain.
84 static std::unique_ptr<GrFragmentProcessor> MakeCustomLinearFilterInset(
88 GrSamplerState::WrapMode wx,
89 GrSamplerState::WrapMode wy,
94 const float border[4] = kDefaultBorder);
96 std::unique_ptr<GrFragmentProcessor> clone() const override;
98 const char* name() const override { return "TextureEffect"; }
100 GrSamplerState samplerState() const { return fSamplerState; }
102 GrTexture* texture() const { return fView.asTextureProxy()->peekTexture(); }
104 const GrSurfaceProxyView& view() const { return fView; }
106 // Gets a matrix that is concat'ed by wrapping GrMatrixEffect that handles y-flip and coord
107 // normalization if required. This matrix is not always known when we make the GrTextureEffect
108 // because of fully-lazy proxies. Hence, this method exists to allow this concat to happen
109 // after proxy instantiation with coordination from GrMatrixEffect.
110 SkMatrix coordAdjustmentMatrix() const;
112 class Impl : public ProgramImpl {
114 void emitCode(EmitArgs&) override;
116 void setSamplerHandle(GrGLSLShaderBuilder::SamplerHandle handle) {
117 fSamplerHandle = handle;
121 void onSetData(const GrGLSLProgramDataManager&, const GrFragmentProcessor&) override;
123 UniformHandle fSubsetUni;
124 UniformHandle fClampUni;
125 UniformHandle fIDimsUni;
126 UniformHandle fBorderUni;
127 GrGLSLShaderBuilder::SamplerHandle fSamplerHandle;
134 * Possible implementation of wrap mode in shader code. Some modes are specialized by
137 enum class ShaderMode : uint16_t {
138 kNone, // Using HW mode
139 kClamp, // Shader based clamp, no filter specialization
140 kRepeat_Nearest_None, // Simple repeat for nearest sampling, no mipmapping
141 kRepeat_Linear_None, // Filter the subset boundary for kRepeat mode, no mip mapping
142 kRepeat_Linear_Mipmap, // Logic for linear filtering and LOD selection with kRepeat mode.
143 kRepeat_Nearest_Mipmap, // Logic for nearest filtering and LOD selection with kRepeat mode.
144 kMirrorRepeat, // Mirror repeat (doesn't depend on filter))
145 kClampToBorder_Nearest, // Logic for hard transition to border color when not filtering.
146 kClampToBorder_Filter, // Logic for fading to border color when filtering.
148 static ShaderMode GetShaderMode(GrSamplerState::WrapMode,
149 GrSamplerState::Filter,
150 GrSamplerState::MipmapMode);
151 static bool ShaderModeIsClampToBorder(ShaderMode);
152 // To keep things a little simpler, when we have filtering logic in the shader we
153 // operate on unnormalized texture coordinates. We will add a uniform that stores
154 // {1/w, 1/h} in a float2 and normalizes after the mode is handled if the texture
156 static bool ShaderModeRequiresUnormCoord(ShaderMode);
158 GrSurfaceProxyView fView;
159 GrSamplerState fSamplerState;
163 ShaderMode fShaderModes[2];
165 inline GrTextureEffect(GrSurfaceProxyView, SkAlphaType, const Sampling&);
167 explicit GrTextureEffect(const GrTextureEffect& src);
169 std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override;
171 void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override;
173 bool onIsEqual(const GrFragmentProcessor&) const override;
175 bool matrixEffectShouldNormalize() const;
177 bool hasClampToBorderShaderMode() const {
178 return ShaderModeIsClampToBorder(fShaderModes[0]) ||
179 ShaderModeIsClampToBorder(fShaderModes[1]);
182 GR_DECLARE_FRAGMENT_PROCESSOR_TEST
184 using INHERITED = GrFragmentProcessor;