xref: /aosp_15_r20/external/skia/src/gpu/graphite/render/CoverageMaskRenderStep.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2023 Google LLC
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 #include "src/gpu/graphite/render/CoverageMaskRenderStep.h"
8 
9 #include "include/core/SkM44.h"
10 #include "include/core/SkMatrix.h"
11 #include "include/core/SkRefCnt.h"
12 #include "include/core/SkSamplingOptions.h"
13 #include "include/core/SkScalar.h"
14 #include "include/core/SkSize.h"
15 #include "include/core/SkTileMode.h"
16 #include "include/private/base/SkAssert.h"
17 #include "include/private/base/SkDebug.h"
18 #include "src/base/SkEnumBitMask.h"
19 #include "src/core/SkSLTypeShared.h"
20 #include "src/gpu/BufferWriter.h"
21 #include "src/gpu/graphite/Attribute.h"
22 #include "src/gpu/graphite/ContextUtils.h"
23 #include "src/gpu/graphite/DrawOrder.h"
24 #include "src/gpu/graphite/DrawParams.h"
25 #include "src/gpu/graphite/DrawTypes.h"
26 #include "src/gpu/graphite/DrawWriter.h"
27 #include "src/gpu/graphite/PipelineData.h"
28 #include "src/gpu/graphite/TextureProxy.h"
29 #include "src/gpu/graphite/geom/CoverageMaskShape.h"
30 #include "src/gpu/graphite/geom/Geometry.h"
31 #include "src/gpu/graphite/geom/Rect.h"
32 #include "src/gpu/graphite/geom/Transform_graphite.h"
33 #include "src/gpu/graphite/render/CommonDepthStencilSettings.h"
34 
35 #include <cstdint>
36 #include <string_view>
37 
38 namespace skgpu::graphite {
39 
40 // The device origin is applied *before* the maskToDeviceRemainder matrix so that it can be
41 // combined with the mask atlas origin. This is necessary so that the mask bounds can be inset or
42 // outset for clamping w/o affecting the alignment of the mask sampling.
get_device_translation(const SkM44 & localToDevice)43 static skvx::float2 get_device_translation(const SkM44& localToDevice) {
44     float m00 = localToDevice.rc(0,0), m01 = localToDevice.rc(0,1);
45     float m10 = localToDevice.rc(1,0), m11 = localToDevice.rc(1,1);
46 
47     float det = m00*m11 - m01*m10;
48     if (SkScalarNearlyZero(det)) {
49         // We can't extract any pre-translation, since the upper 2x2 is not invertible. Return (0,0)
50         // so that the maskToDeviceRemainder matrix remains the full transform.
51         return {0.f, 0.f};
52     }
53 
54     // Calculate inv([[m00,m01][m10,m11]])*[[m30][m31]] to get the pre-remainder device translation.
55     float tx = localToDevice.rc(0,3), ty = localToDevice.rc(1,3);
56     skvx::float4 invT = skvx::float4{m11, -m10, -m01, m00} * skvx::float4{tx,tx,ty,ty};
57     return (invT.xy() + invT.zw()) / det;
58 }
59 
CoverageMaskRenderStep()60 CoverageMaskRenderStep::CoverageMaskRenderStep()
61         : RenderStep("CoverageMaskRenderStep",
62                      "",
63                      // The mask will have AA outsets baked in, but the original bounds for clipping
64                      // still require the outset for analytic coverage.
65                      Flags::kPerformsShading | Flags::kHasTextures | Flags::kEmitsCoverage |
66                      Flags::kOutsetBoundsForAA,
67                      /*uniforms=*/{{"maskToDeviceRemainder", SkSLType::kFloat3x3}},
68                      PrimitiveType::kTriangleStrip,
69                      kDirectDepthGreaterPass,
70                      /*vertexAttrs=*/{},
71                      /*instanceAttrs=*/
72                      // Draw bounds and mask bounds are in normalized relative to the mask texture,
73                      // but 'drawBounds' is stored in float since the coords may map outside of
74                      // [0,1] for inverse-filled masks. 'drawBounds' is relative to the logical mask
75                      // entry's origin, while 'maskBoundsIn' is atlas-relative. Inverse fills swap
76                      // the order in 'maskBoundsIn' to be RBLT.
77                      {{"drawBounds", VertexAttribType::kFloat4 , SkSLType::kFloat4},  // ltrb
78                       {"maskBoundsIn", VertexAttribType::kUShort4_norm, SkSLType::kFloat4},
79                       // Remaining translation extracted from actual 'maskToDevice' transform.
80                       {"deviceOrigin", VertexAttribType::kFloat2, SkSLType::kFloat2},
81                       {"depth"     , VertexAttribType::kFloat, SkSLType::kFloat},
82                       {"ssboIndices", VertexAttribType::kUInt2, SkSLType::kUInt2},
83                       // deviceToLocal matrix for producing local coords for shader evaluation
84                       {"mat0", VertexAttribType::kFloat3, SkSLType::kFloat3},
85                       {"mat1", VertexAttribType::kFloat3, SkSLType::kFloat3},
86                       {"mat2", VertexAttribType::kFloat3, SkSLType::kFloat3}},
87                      /*varyings=*/
88                      {// `maskBounds` are the atlas-relative, sorted bounds of the coverage mask.
89                       // `textureCoords` are the atlas-relative UV coordinates of the draw, which
90                       // can spill beyond `maskBounds` for inverse fills.
91                       // TODO: maskBounds is constant for all fragments for a given instance,
92                       // could we store them in the draw's SSBO?
93                       {"maskBounds"   , SkSLType::kFloat4},
94                       {"textureCoords", SkSLType::kFloat2},
95                       // 'invert' is set to 0 use unmodified coverage, and set to 1 for "1-c".
96                       {"invert", SkSLType::kHalf}}) {}
97 
vertexSkSL() const98 std::string CoverageMaskRenderStep::vertexSkSL() const {
99     // Returns the body of a vertex function, which must define a float4 devPosition variable and
100     // must write to an already-defined float2 stepLocalCoords variable.
101     return "float4 devPosition = coverage_mask_vertex_fn("
102                     "float2(sk_VertexID >> 1, sk_VertexID & 1), "
103                     "maskToDeviceRemainder, drawBounds, maskBoundsIn, deviceOrigin, "
104                     "depth, float3x3(mat0, mat1, mat2), "
105                     "maskBounds, textureCoords, invert, stepLocalCoords);\n";
106 }
107 
texturesAndSamplersSkSL(const ResourceBindingRequirements & bindingReqs,int * nextBindingIndex) const108 std::string CoverageMaskRenderStep::texturesAndSamplersSkSL(
109         const ResourceBindingRequirements& bindingReqs, int* nextBindingIndex) const {
110     return EmitSamplerLayout(bindingReqs, nextBindingIndex) + " sampler2D pathAtlas;";
111 }
112 
fragmentCoverageSkSL() const113 const char* CoverageMaskRenderStep::fragmentCoverageSkSL() const {
114     return R"(
115         half c = sample(pathAtlas, clamp(textureCoords, maskBounds.LT, maskBounds.RB)).r;
116         outputCoverage = half4(mix(c, 1 - c, invert));
117     )";
118 }
119 
writeVertices(DrawWriter * dw,const DrawParams & params,skvx::uint2 ssboIndices) const120 void CoverageMaskRenderStep::writeVertices(DrawWriter* dw,
121                                            const DrawParams& params,
122                                            skvx::uint2 ssboIndices) const {
123     const CoverageMaskShape& coverageMask = params.geometry().coverageMaskShape();
124     const TextureProxy* proxy = coverageMask.textureProxy();
125     SkASSERT(proxy);
126 
127     // A quad is a 4-vertex instance. The coordinates are derived from the vertex IDs.
128     DrawWriter::Instances instances(*dw, {}, {}, 4);
129 
130     // The device origin is the  translation extracted from the mask-to-device matrix so
131     // that the remaining matrix uniform has less variance between draws.
132     const auto& maskToDevice = params.transform().matrix();
133     skvx::float2 deviceOrigin = get_device_translation(maskToDevice);
134 
135     // Relative to mask space (device origin and mask-to-device remainder must be applied in shader)
136     skvx::float4 maskBounds = coverageMask.bounds().ltrb();
137     skvx::float4 drawBounds;
138 
139     if (coverageMask.inverted()) {
140         // Only mask filters trigger complex transforms, and they are never inverse filled. Since
141         // we know this is an inverted mask, then we can exactly map the draw's clip bounds to mask
142         // space so that the clip is still fully covered without branching in the vertex shader.
143         SkASSERT(maskToDevice == SkM44::Translate(deviceOrigin.x(), deviceOrigin.y()));
144         drawBounds = params.clip().drawBounds().makeOffset(-deviceOrigin).ltrb();
145 
146         // If the mask is fully clipped out, then the shape's mask info should be (0,0,0,0).
147         // If it's not fully clipped out, then the mask info should be non-empty.
148         SkASSERT(!params.clip().transformedShapeBounds().isEmptyNegativeOrNaN() ^
149                  all(maskBounds == 0.f));
150 
151         if (params.clip().transformedShapeBounds().isEmptyNegativeOrNaN()) {
152             // The inversion check is strict inequality, so (0,0,0,0) would not be detected. Adjust
153             // to (0,0,1/2,1/2) to restrict sampling to the top-left quarter of the top-left pixel,
154             // which should have a value of 0 regardless of filtering mode.
155             maskBounds = skvx::float4{0.f, 0.f, 0.5f, 0.5f};
156         } else {
157             // Add 1/2px outset to the mask bounds so that clamped coordinates sample the texel
158             // center of the padding around the atlas entry.
159             maskBounds += skvx::float4{-0.5f, -0.5f, 0.5f, 0.5f};
160         }
161 
162         // and store RBLT so that the 'maskBoundsIn' attribute has xy > zw to detect inverse fill.
163         maskBounds = skvx::shuffle<2,3,0,1>(maskBounds);
164     } else {
165         // If we aren't inverted, then the originally assigned values don't need to be adjusted, but
166         // also ensure the mask isn't empty (otherwise the draw should have been skipped earlier).
167         SkASSERT(!coverageMask.bounds().isEmptyNegativeOrNaN());
168         SkASSERT(all(maskBounds.xy() < maskBounds.zw()));
169 
170         // Since the mask bounds and draw bounds are 1-to-1 with each other, the clamping of texture
171         // coords is mostly a formality. We inset the mask bounds by 1/2px so that we clamp to the
172         // texel center of the outer row/column of the mask. This should be a no-op for nearest
173         // sampling but prevents any linear sampling from incorporating adjacent data; for atlases
174         // this would just be 0 but for non-atlas coverage masks that might not have padding this
175         // avoids filtering unknown values in an approx-fit texture.
176         drawBounds = maskBounds;
177         maskBounds -= skvx::float4{-0.5f, -0.5f, 0.5f, 0.5f};
178     }
179 
180     // Move 'drawBounds' and 'maskBounds' into the atlas coordinate space, then adjust the
181     // device translation to undo the atlas origin automatically in the vertex shader.
182     skvx::float2 textureOrigin = skvx::cast<float>(coverageMask.textureOrigin());
183     maskBounds += textureOrigin.xyxy();
184     drawBounds += textureOrigin.xyxy();
185     deviceOrigin -= textureOrigin;
186 
187     // Normalize drawBounds and maskBounds after possibly correcting drawBounds for inverse fills.
188     // The maskToDevice matrix uniform will handle de-normalizing drawBounds for vertex positions.
189     auto atlasSizeInv = skvx::float2{1.f / proxy->dimensions().width(),
190                                      1.f / proxy->dimensions().height()};
191     drawBounds *= atlasSizeInv.xyxy();
192     maskBounds *= atlasSizeInv.xyxy();
193     deviceOrigin *= atlasSizeInv;
194 
195     // Since the mask bounds define normalized texels of the texture, we can encode them as
196     // ushort_norm without losing precision to save space.
197     SkASSERT(all((maskBounds >= 0.f) & (maskBounds <= 1.f)));
198     maskBounds = 65535.f * maskBounds + 0.5f;
199 
200     const SkM44& m = coverageMask.deviceToLocal();
201     instances.append(1) << drawBounds << skvx::cast<uint16_t>(maskBounds) << deviceOrigin
202                         << params.order().depthAsFloat() << ssboIndices
203                         << m.rc(0,0) << m.rc(1,0) << m.rc(3,0)   // mat0
204                         << m.rc(0,1) << m.rc(1,1) << m.rc(3,1)   // mat1
205                         << m.rc(0,3) << m.rc(1,3) << m.rc(3,3);  // mat2
206 }
207 
writeUniformsAndTextures(const DrawParams & params,PipelineDataGatherer * gatherer) const208 void CoverageMaskRenderStep::writeUniformsAndTextures(const DrawParams& params,
209                                                       PipelineDataGatherer* gatherer) const {
210     SkDEBUGCODE(UniformExpectationsValidator uev(gatherer, this->uniforms());)
211 
212     const CoverageMaskShape& coverageMask = params.geometry().coverageMaskShape();
213     const TextureProxy* proxy = coverageMask.textureProxy();
214     SkASSERT(proxy);
215 
216     // Most coverage masks are aligned with the device pixels, so the params' transform is an
217     // integer translation matrix. This translation is extracted as an instance attribute so that
218     // the remaining transform has a much lower frequency of changing (only complex-transformed
219     // mask filters).
220     skvx::float2 deviceOrigin = get_device_translation(params.transform().matrix());
221     SkMatrix maskToDevice = params.transform().matrix().asM33();
222     maskToDevice.preTranslate(-deviceOrigin.x(), -deviceOrigin.y());
223 
224     // The mask coordinates in the vertex shader will be normalized, so scale by the proxy size
225     // to get back to Skia's texel-based coords.
226     maskToDevice.preScale(proxy->dimensions().width(), proxy->dimensions().height());
227 
228     // Write uniforms:
229     gatherer->write(maskToDevice);
230 
231     // Write textures and samplers:
232     const bool pixelAligned =
233             params.transform().type() <= Transform::Type::kSimpleRectStaysRect &&
234             params.transform().maxScaleFactor() == 1.f &&
235             all(deviceOrigin == floor(deviceOrigin + SK_ScalarNearlyZero));
236     gatherer->add(sk_ref_sp(proxy), {pixelAligned ? SkFilterMode::kNearest : SkFilterMode::kLinear,
237                                      SkTileMode::kClamp});
238 }
239 
240 }  // namespace skgpu::graphite
241