• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2024 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 
8 #include "src/gpu/graphite/render/CircularArcRenderStep.h"
9 
10 #include "include/core/SkArc.h"
11 #include "include/core/SkM44.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkRect.h"
14 #include "include/core/SkScalar.h"
15 #include "include/private/base/SkAssert.h"
16 #include "include/private/base/SkPoint_impl.h"
17 #include "src/base/SkEnumBitMask.h"
18 #include "src/core/SkSLTypeShared.h"
19 #include "src/gpu/BufferWriter.h"
20 #include "src/gpu/graphite/Attribute.h"
21 #include "src/gpu/graphite/BufferManager.h"
22 #include "src/gpu/graphite/DrawOrder.h"
23 #include "src/gpu/graphite/DrawParams.h"
24 #include "src/gpu/graphite/DrawTypes.h"
25 #include "src/gpu/graphite/DrawWriter.h"
26 #include "src/gpu/graphite/geom/Geometry.h"
27 #include "src/gpu/graphite/geom/Shape.h"
28 #include "src/gpu/graphite/geom/Transform.h"
29 #include "src/gpu/graphite/render/CommonDepthStencilSettings.h"
30 
31 #include <utility>
32 
33 // This RenderStep is used to render filled circular arcs and stroked circular arcs that
34 // don't include the center. Currently it only supports butt caps but will be extended
35 // to include round caps.
36 //
37 // Each arc is represented by a single instance. The instance attributes are enough to
38 // describe the given arc types without relying on uniforms to define its operation.
39 // The attributes encode shape as follows:
40 
41 // float4 centerScales - used to transform the vertex data into local space.
42 //    The vertex data represents interleaved octagons that are respectively circumscribed
43 //    and inscribed on a unit circle, and have to be transformed into local space.
44 //    So the .xy values here are the center of the arc in local space, and .zw its outer and inner
45 //    radii, respectively. If the vertex is an outer vertex its local position will be computed as
46 //         centerScales.xy + position.xy * centerScales.z
47 //    Otherwise it will be computed as
48 //         centerScales.xy + position.xy * centerScales.w
49 //    We can tell whether a vertex is an outer or inner vertex by looking at the sign
50 //    of its z component. This z value is also used to compute half-pixel anti-aliasing offsets
51 //    once the vertex data is transformed into device space.
52 // float3 radiiAndFlags - in the fragment shader we will pass an offset in unit circle space to
53 //    determine the circle edge and for use for clipping. The .x value here is outerRadius+0.5 and
54 //    will be compared against the unit circle radius (i.e., 1.0) to compute the outer edge. The .y
55 //    value is innerRadius-0.5/outerRadius+0.5 and will be used as the comparison point for the
56 //    inner edge. The .z value is a flag which indicates whether fragClipPlane1 is for intersection
57 //    (+) or for union (-), and whether to set up rounded caps (-2/+2).
58 // float3 geoClipPlane - For very thin acute arcs, because of the 1/2 pixel boundary we can get
59 //    non-clipped artifacts beyond the center of the circle. To solve this, we clip the geometry
60 //    so any rendering doesn't cross that point.
61 
62 // In addition, these values will be passed to the fragment shader:
63 //
64 // float3 fragClipPlane0 - the arc will always be clipped against this half plane, and passed as
65 //    the varying clipPlane.
66 // float3 fragClipPlane1 - for convex/acute arcs, we pass this via the varying isectPlane to clip
67 //    against this and multiply its value by the ClipPlane clip result. For concave/obtuse arcs,
68 //    we pass this via the varying unionPlane which will clip against this and add its value to the
69 //    ClipPlane clip result. This is controlled by the flag value in radiiAndFlags: if the
70 //    flag is > 0, it's passed as isectClip, if it's < 0 it's passed as unionClip. We set default
71 //    values for the alternative clip plane that end up being a null clip.
72 // float  roundCapRadius - this is computed in the vertex shader. If we're using round caps (i.e.,
73 //    if abs(flags) > 1), this will be half the distance between the outer and inner radii.
74 //    Otherwise it will be 0 which will end up zeroing out any round cap calculation.
75 // float4 inRoundCapPos - locations of the centers of the round caps in normalized space. This
76 //    will be all zeroes if not needed.
77 
78 namespace skgpu::graphite {
79 
80 // Represents the per-vertex attributes used in each instance.
81 struct Vertex {
82     // Unit circle local space position (.xy) and AA offset (.z)
83     SkV3 fPosition;
84 };
85 
86 static constexpr int kVertexCount = 18;
87 
write_vertex_buffer(VertexWriter writer)88 static void write_vertex_buffer(VertexWriter writer) {
89     // Normalized geometry for octagons that circumscribe/inscribe a unit circle.
90     // Outer ring offset
91     static constexpr float kOctOffset = 0.41421356237f;  // sqrt(2) - 1
92     // Inner ring points
93     static constexpr SkScalar kCosPi8 = 0.923579533f;
94     static constexpr SkScalar kSinPi8 = 0.382683432f;
95 
96     // Directional offset for anti-aliasing.
97     // Also used as marker for whether this is an outer or inner vertex.
98     static constexpr float kOuterAAOffset = 0.5f;
99     static constexpr float kInnerAAOffset = -0.5f;
100 
101     static constexpr SkV3 kOctagonVertices[kVertexCount] = {
102         {-kOctOffset, -1,          kOuterAAOffset},
103         {-kSinPi8,    -kCosPi8,    kInnerAAOffset},
104         { kOctOffset, -1,          kOuterAAOffset},
105         {kSinPi8,     -kCosPi8,    kInnerAAOffset},
106         { 1,          -kOctOffset, kOuterAAOffset},
107         {kCosPi8,     -kSinPi8,    kInnerAAOffset},
108         { 1,           kOctOffset, kOuterAAOffset},
109         {kCosPi8,      kSinPi8,    kInnerAAOffset},
110         { kOctOffset,  1,          kOuterAAOffset},
111         {kSinPi8,      kCosPi8,    kInnerAAOffset},
112         {-kOctOffset,  1,          kOuterAAOffset},
113         {-kSinPi8,     kCosPi8,    kInnerAAOffset},
114         {-1,           kOctOffset, kOuterAAOffset},
115         {-kCosPi8,     kSinPi8,    kInnerAAOffset},
116         {-1,          -kOctOffset, kOuterAAOffset},
117         {-kCosPi8,    -kSinPi8,    kInnerAAOffset},
118         {-kOctOffset, -1,          kOuterAAOffset},
119         {-kSinPi8,    -kCosPi8,    kInnerAAOffset},
120     };
121 
122     if (writer) {
123         writer << kOctagonVertices;
124     } // otherwise static buffer creation failed, so do nothing; Context initialization will fail.
125 }
126 
CircularArcRenderStep(StaticBufferManager * bufferManager)127 CircularArcRenderStep::CircularArcRenderStep(StaticBufferManager* bufferManager)
128         : RenderStep(RenderStepID::kCircularArc,
129                      Flags::kPerformsShading | Flags::kEmitsCoverage | Flags::kOutsetBoundsForAA,
130                      /*uniforms=*/{},
131                      PrimitiveType::kTriangleStrip,
132                      kDirectDepthGreaterPass,
133                      /*vertexAttrs=*/{
134                              {"position", VertexAttribType::kFloat3, SkSLType::kFloat3},
135                      },
136                      /*instanceAttrs=*/{
137                              // Center plus radii, used to transform to local position
138                              {"centerScales", VertexAttribType::kFloat4, SkSLType::kFloat4},
139                              // Outer (device space) and inner (normalized) radii
140                              // + flags for determining clipping and roundcaps
141                              {"radiiAndFlags", VertexAttribType::kFloat3, SkSLType::kFloat3},
142                              // Clips the geometry for acute arcs
143                              {"geoClipPlane", VertexAttribType::kFloat3, SkSLType::kFloat3},
144                              // Clip planes sent to the fragment shader for arc extents
145                              {"fragClipPlane0", VertexAttribType::kFloat3, SkSLType::kFloat3},
146                              {"fragClipPlane1", VertexAttribType::kFloat3, SkSLType::kFloat3},
147                              // Roundcap positions, if needed
148                              {"inRoundCapPos", VertexAttribType::kFloat4, SkSLType::kFloat4},
149 
150                              {"depth", VertexAttribType::kFloat, SkSLType::kFloat},
151                              {"ssboIndices", VertexAttribType::kUInt2, SkSLType::kUInt2},
152 
153                              {"mat0", VertexAttribType::kFloat3, SkSLType::kFloat3},
154                              {"mat1", VertexAttribType::kFloat3, SkSLType::kFloat3},
155                              {"mat2", VertexAttribType::kFloat3, SkSLType::kFloat3},
156                      },
157                      /*varyings=*/{
158                              // Normalized offset vector plus radii
159                              {"circleEdge", SkSLType::kFloat4},
160                              // Half-planes used to clip to arc shape.
161                              {"clipPlane", SkSLType::kFloat3},
162                              {"isectPlane", SkSLType::kFloat3},
163                              {"unionPlane", SkSLType::kFloat3},
164                              // Roundcap data
165                              {"roundCapRadius", SkSLType::kFloat},
166                              {"roundCapPos", SkSLType::kFloat4},
167                      }) {
168     // Initialize the static buffer we'll use when recording draw calls.
169     // NOTE: Each instance of this RenderStep gets its own copy of the data. Since there should only
170     // ever be one CircularArcRenderStep at a time, this shouldn't be an issue.
171     write_vertex_buffer(bufferManager->getVertexWriter(sizeof(Vertex) * kVertexCount,
172                                                        &fVertexBuffer));
173 }
174 
~CircularArcRenderStep()175 CircularArcRenderStep::~CircularArcRenderStep() {}
176 
vertexSkSL() const177 std::string CircularArcRenderStep::vertexSkSL() const {
178     // Returns the body of a vertex function, which must define a float4 devPosition variable and
179     // must write to an already-defined float2 stepLocalCoords variable.
180     return "float4 devPosition = circular_arc_vertex_fn("
181                    // Vertex Attributes
182                    "position, "
183                    // Instance Attributes
184                    "centerScales, radiiAndFlags, geoClipPlane, fragClipPlane0, fragClipPlane1, "
185                    "inRoundCapPos, depth, float3x3(mat0, mat1, mat2), "
186                    // Varyings
187                    "circleEdge, clipPlane, isectPlane, unionPlane, "
188                    "roundCapRadius, roundCapPos, "
189                    // Render Step
190                    "stepLocalCoords);\n";
191 }
192 
fragmentCoverageSkSL() const193 const char* CircularArcRenderStep::fragmentCoverageSkSL() const {
194     // The returned SkSL must write its coverage into a 'half4 outputCoverage' variable (defined in
195     // the calling code) with the actual coverage splatted out into all four channels.
196     return "outputCoverage = circular_arc_coverage_fn(circleEdge, "
197                                                      "clipPlane, "
198                                                      "isectPlane, "
199                                                      "unionPlane, "
200                                                      "roundCapRadius, "
201                                                      "roundCapPos);";
202 }
203 
writeVertices(DrawWriter * writer,const DrawParams & params,skvx::uint2 ssboIndices) const204 void CircularArcRenderStep::writeVertices(DrawWriter* writer,
205                                           const DrawParams& params,
206                                           skvx::uint2 ssboIndices) const {
207     SkASSERT(params.geometry().isShape() && params.geometry().shape().isArc());
208 
209     DrawWriter::Instances instance{*writer, fVertexBuffer, {}, kVertexCount};
210     auto vw = instance.append(1);
211 
212     const Shape& shape = params.geometry().shape();
213     const SkArc& arc = shape.arc();
214 
215     SkPoint localCenter = arc.oval().center();
216     float localOuterRadius = arc.oval().width() / 2;
217     float localInnerRadius = 0.0f;
218 
219     // We know that we have a similarity matrix so this will transform radius to device space
220     const Transform& transform = params.transform();
221     float radius = localOuterRadius * transform.maxScaleFactor();
222     bool isStroke = params.isStroke();
223 
224     float innerRadius = -SK_ScalarHalf;
225     float outerRadius = radius;
226     float halfWidth = 0;
227     if (isStroke) {
228         float localHalfWidth = params.strokeStyle().halfWidth();
229 
230         halfWidth = localHalfWidth * transform.maxScaleFactor();
231         if (SkScalarNearlyZero(halfWidth)) {
232             halfWidth = SK_ScalarHalf;
233             // Need to map this back to local space
234             localHalfWidth = halfWidth / transform.maxScaleFactor();
235         }
236 
237         outerRadius += halfWidth;
238         innerRadius = radius - halfWidth;
239         localInnerRadius = localOuterRadius - localHalfWidth;
240         localOuterRadius += localHalfWidth;
241     }
242 
243     // The radii are outset for two reasons. First, it allows the shader to simply perform
244     // simpler computation because the computed alpha is zero, rather than 50%, at the radius.
245     // Second, the outer radius is used to compute the verts of the bounding box that is
246     // rendered and the outset ensures the box will cover all partially covered by the circle.
247     outerRadius += SK_ScalarHalf;
248     innerRadius -= SK_ScalarHalf;
249 
250     // The shader operates in a space where the circle is translated to be centered at the
251     // origin. Here we compute points on the unit circle at the starting and ending angles.
252     SkV2 localPoints[3];
253     float startAngleRadians = SkDegreesToRadians(arc.startAngle());
254     float sweepAngleRadians = SkDegreesToRadians(arc.sweepAngle());
255     localPoints[0].y = SkScalarSin(startAngleRadians);
256     localPoints[0].x = SkScalarCos(startAngleRadians);
257     SkScalar endAngle = startAngleRadians + sweepAngleRadians;
258     localPoints[1].y = SkScalarSin(endAngle);
259     localPoints[1].x = SkScalarCos(endAngle);
260     localPoints[2] = {0, 0};
261 
262     // Adjust the start and end points based on the view matrix (to handle rotated arcs)
263     SkV4 devPoints[3];
264     transform.mapPoints(localPoints, devPoints, 3);
265     // Translate the point relative to the transformed origin
266     SkV2 startPoint = {devPoints[0].x - devPoints[2].x, devPoints[0].y - devPoints[2].y};
267     SkV2 stopPoint = {devPoints[1].x - devPoints[2].x, devPoints[1].y - devPoints[2].y};
268     startPoint = startPoint.normalize();
269     stopPoint = stopPoint.normalize();
270 
271     // We know the matrix is a similarity here. Detect mirroring which will affect how we
272     // should orient the clip planes for arcs.
273     const SkM44& m = transform.matrix();
274     auto upperLeftDet = m.rc(0,0) * m.rc(1,1) -
275                         m.rc(0,1) * m.rc(1,0);
276     if (upperLeftDet < 0) {
277         std::swap(startPoint, stopPoint);
278     }
279 
280     // Like a fill without useCenter, butt-cap stroke can be implemented by clipping against
281     // radial lines. We treat round caps the same way, but track coverage of circles at the
282     // center of the butts.
283     // However, in both cases we have to be careful about the half-circle.
284     // case. In that case the two radial lines are equal and so that edge gets clipped
285     // twice. Since the shared edge goes through the center we fall back on the !useCenter
286     // case.
287     auto absSweep = SkScalarAbs(sweepAngleRadians);
288     bool useCenter = (arc.isWedge() || isStroke) &&
289                      !SkScalarNearlyEqual(absSweep, SK_ScalarPI);
290 
291     // This makes every point fully inside the plane.
292     SkV3 geoClipPlane = {0.f, 0.f, 1.f};
293     SkV3 clipPlane0;
294     SkV3 clipPlane1;
295     SkV2 roundCapPos0 = {0, 0};
296     SkV2 roundCapPos1 = {0, 0};
297     static constexpr float kIntersection_NoRoundCaps = 1;
298     static constexpr float kIntersection_RoundCaps = 2;
299 
300     // Default to intersection and no round caps.
301     float flags = kIntersection_NoRoundCaps;
302     // Determine if we need round caps.
303     if (isStroke && innerRadius > -SK_ScalarHalf &&
304         params.strokeStyle().halfWidth() > 0 &&
305         params.strokeStyle().cap() == SkPaint::kRound_Cap) {
306         // Compute the cap center points in the normalized space.
307         float midRadius = (innerRadius + outerRadius) / (2 * outerRadius);
308         roundCapPos0 = startPoint * midRadius;
309         roundCapPos1 = stopPoint * midRadius;
310         flags = kIntersection_RoundCaps;
311     }
312 
313     // Determine clip planes.
314     if (useCenter) {
315         SkV2 norm0 = {startPoint.y, -startPoint.x};
316         SkV2 norm1 = {stopPoint.y, -stopPoint.x};
317         // This ensures that norm0 is always the clockwise plane, and norm1 is CCW.
318         if (sweepAngleRadians < 0) {
319             std::swap(norm0, norm1);
320         }
321         norm0 = -norm0;
322         clipPlane0 = {norm0.x, norm0.y, 0.5f};
323         clipPlane1 = {norm1.x, norm1.y, 0.5f};
324         if (absSweep > SK_ScalarPI) {
325             // Union
326             flags = -flags;
327         } else {
328             // Intersection
329             // Highly acute arc. We need to clip the vertices to the perpendicular half-plane.
330             if (!isStroke && absSweep < 0.5f*SK_ScalarPI) {
331                 // We do this clipping in normalized space so use our original local points.
332                 // Should already be normalized since they're sin/cos.
333                 SkV2 localNorm0 = {localPoints[0].y, -localPoints[0].x};
334                 SkV2 localNorm1 = {localPoints[1].y, -localPoints[1].x};
335                 // This ensures that norm0 is always the clockwise plane, and norm1 is CCW.
336                 if (sweepAngleRadians < 0) {
337                     std::swap(localNorm0, localNorm1);
338                 }
339                 // Negate norm0 and compute the perpendicular of the difference
340                 SkV2 clipNorm = {-localNorm0.y - localNorm1.y, localNorm1.x + localNorm0.x};
341                 clipNorm = clipNorm.normalize();
342                 // This should give us 1/2 pixel spacing from the half-plane
343                 // after transforming from normalized to local to device space.
344                 float dist = 0.5f / radius / transform.maxScaleFactor();
345                 geoClipPlane = {clipNorm.x, clipNorm.y, dist};
346             }
347         }
348     } else {
349         // We clip to a secant of the original circle, only one clip plane
350         startPoint *= radius;
351         stopPoint *= radius;
352         SkV2 norm = {startPoint.y - stopPoint.y, stopPoint.x - startPoint.x};
353         norm = norm.normalize();
354         if (sweepAngleRadians > 0) {
355             norm = -norm;
356         }
357         float d = -norm.dot(startPoint) + 0.5f;
358         clipPlane0 = {norm.x, norm.y, d};
359         clipPlane1 = {0.f, 0.f, 1.f}; // no clipping
360     }
361 
362     // The inner radius in the vertex data must be specified in normalized space.
363     innerRadius = innerRadius / outerRadius;
364 
365     vw << localCenter << localOuterRadius << localInnerRadius
366        << outerRadius << innerRadius << flags
367        << geoClipPlane << clipPlane0 << clipPlane1
368        << roundCapPos0 << roundCapPos1
369        << params.order().depthAsFloat()
370        << ssboIndices
371        << m.rc(0,0) << m.rc(1,0) << m.rc(3,0)  // mat0
372        << m.rc(0,1) << m.rc(1,1) << m.rc(3,1)  // mat1
373        << m.rc(0,3) << m.rc(1,3) << m.rc(3,3); // mat2
374 }
375 
writeUniformsAndTextures(const DrawParams &,PipelineDataGatherer *) const376 void CircularArcRenderStep::writeUniformsAndTextures(const DrawParams&,
377                                                      PipelineDataGatherer*) const {
378     // All data is uploaded as instance attributes, so no uniforms are needed.
379 }
380 
381 }  // namespace skgpu::graphite
382