| 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 |
|
| 8 |
#include "src/gpu/graphite/render/PerEdgeAAQuadRenderStep.h" |
| 9 |
|
| 10 |
#include "include/core/SkM44.h" |
| 11 |
#include "include/private/base/SkAssert.h" |
| 12 |
#include "include/private/base/SkDebug.h" |
| 13 |
#include "include/private/base/SkFloatingPoint.h" |
| 14 |
#include "src/base/SkEnumBitMask.h" |
| 15 |
#include "src/base/SkVx.h" |
| 16 |
#include "src/core/SkSLTypeShared.h" |
| 17 |
#include "src/gpu/BufferWriter.h" |
| 18 |
#include "src/gpu/graphite/Attribute.h" |
| 19 |
#include "src/gpu/graphite/BufferManager.h" |
| 20 |
#include "src/gpu/graphite/DrawOrder.h" |
| 21 |
#include "src/gpu/graphite/DrawParams.h" |
| 22 |
#include "src/gpu/graphite/DrawTypes.h" |
| 23 |
#include "src/gpu/graphite/DrawWriter.h" |
| 24 |
#include "src/gpu/graphite/PipelineData.h" |
| 25 |
#include "src/gpu/graphite/geom/EdgeAAQuad.h" |
| 26 |
#include "src/gpu/graphite/geom/Geometry.h" |
| 27 |
#include "src/gpu/graphite/geom/Rect.h" |
| 28 |
#include "src/gpu/graphite/geom/Transform.h" |
| 29 |
#include "src/gpu/graphite/render/CommonDepthStencilSettings.h" |
| 30 |
|
| 31 |
#include <array> |
| 32 |
#include <cstdint> |
| 33 |
|
| 34 |
// This RenderStep is specialized to draw filled rectangles with per-edge AA. |
| 35 |
// |
| 36 |
// Each of these "primitives" is represented by a single instance. The instance attributes are |
| 37 |
// flexible enough to describe per-edge AA quads without relying on uniforms to define its |
| 38 |
// operation. The attributes encode shape as follows: |
| 39 |
// |
| 40 |
// float4 edgeFlags - per-edge AA defined by each component: aa != 0. |
| 41 |
// float4 quadXs - these values provide the X coordinates of the quadrilateral in top-left CW order. |
| 42 |
// float4 quadYs - these values provide the Y coordinates of the quadrilateral. |
| 43 |
// |
| 44 |
// From the other direction, per-edge AA quads produce instance values like: |
| 45 |
// - [aa(t,r,b,l) ? 255 : 0] [xs(tl,tr,br,bl)] [ys(tl,tr,br,bl)] |
| 46 |
// |
| 47 |
// From this encoding, data can be unpacked for each corner, which are equivalent under |
| 48 |
// rotational symmetry. Per-edge quads are always mitered and fill the interior, but the |
| 49 |
// vertices are placed such that the edge coverage ramps can collapse to 0 area on non-AA edges. |
| 50 |
// |
| 51 |
// The vertices that describe each corner are placed so that edges and miters calculate |
| 52 |
// coverage by interpolating a varying and then clamping in the fragment shader. Triangles that |
| 53 |
// cover the inner and outer curves calculate distance to the curve within the fragment shader. |
| 54 |
// |
| 55 |
// See https://docs.google.com/presentation/d/1MCPstNsSlDBhR8CrsJo0r-cZNbu-sEJEvU9W94GOJoY/edit?usp=sharing |
| 56 |
// for diagrams and explanation of how the geometry is defined. |
| 57 |
// |
| 58 |
// PerEdgeAAQuadRenderStep uses the common technique of approximating distance to the level set by |
| 59 |
// one expansion of the Taylor's series for the level set's equation. Given a level set function |
| 60 |
// C(x,y), this amounts to calculating C(px,py)/|∇C(px,py)|. For the straight edges the level set |
| 61 |
// is linear and calculated in the vertex shader and then interpolated exactly over the rectangle. |
| 62 |
// This provides distances to all four exterior edges within the fragment shader and allows it to |
| 63 |
// reconstruct a relative position per elliptical corner. Unfortunately this requires the fragment |
| 64 |
// shader to calculate the length of the gradient for straight edges instead of interpolating |
| 65 |
// exact device-space distance. |
| 66 |
// |
| 67 |
// Unlike AnalyticRRectRenderStep, for per-edge AA quads it's valid to have each pixel calculate a |
| 68 |
// single corner's coverage that's controlled via the vertex shader. Any bias is a constant 1/2, |
| 69 |
// so this is also added in the vertex shader. |
| 70 |
// |
| 71 |
// Analytic derivatives are used so that a single pipeline can be used regardless of HW derivative |
| 72 |
// support or for geometry that would prove difficult for forward differencing. The device-space |
| 73 |
// gradient for ellipses is calculated per-pixel by transforming a per-pixel local gradient vector |
| 74 |
// with the Jacobian of the inverse local-to-device transform: |
| 75 |
// |
| 76 |
// (px,py) is the projected point of (u,v) transformed by a 3x3 matrix, M: |
| 77 |
// [x(u,v) / w(u,v)] [x] [m00 m01 m02] [u] |
| 78 |
// (px,py) = [y(u,v) / w(u,v)] where [y] = [m10 m11 m12]X[v] = M*(u,v,1) |
| 79 |
// [w] [m20 m21 m22] [1] |
| 80 |
// |
| 81 |
// C(px,py) can be defined in terms of a local Cl(u,v) as C(px,py) = Cl(p^-1(px,py)), where p^-1 = |
| 82 |
// |
| 83 |
// [x'(px,py) / w'(px,py)] [x'] [m00' m01' * m02'] [px] |
| 84 |
// (u,v) = [y'(px,py) / w'(px,py)] where [y'] = [m10' m11' * m12']X[py] = M^-1*(px,py,0,1) |
| 85 |
// [w'] [m20' m21' * m22'] [ 1] |
| 86 |
// |
| 87 |
// Note that if the 3x3 M was arrived by dropping the 3rd row and column from a 4x4 since we assume |
| 88 |
// a local 3rd coordinate of 0, M^-1 is not equal to the 4x4 inverse with dropped rows and columns. |
| 89 |
// |
| 90 |
// Using the chain rule, then ∇C(px,py) |
| 91 |
// = ∇Cl(u,v)X[1/w'(px,py) 0 -x'(px,py)/w'(px,py)^2] [m00' m01'] |
| 92 |
// [ 0 1/w'(px,py) -y'(px,py)/w'(px,py)^2]X[m10' m11'] |
| 93 |
// [m20' m21'] |
| 94 |
// |
| 95 |
// = 1/w'(px,py)*∇Cl(u,v)X[1 0 -x'(px,py)/w'(px,py)] [m00' m01'] |
| 96 |
// [0 1 -y'(px,py)/w'(px,py)]X[m10' m11'] |
| 97 |
// [m20' m21'] |
| 98 |
// |
| 99 |
// = w(u,v)*∇Cl(u,v)X[1 0 0 -u] [m00' m01'] |
| 100 |
// [0 1 0 -v]X[m10' m11'] |
| 101 |
// [m20' m21'] |
| 102 |
// |
| 103 |
// = w(u,v)*∇Cl(u,v)X[m00'-m20'u m01'-m21'u] |
| 104 |
// [m10'-m20'v m11'-m21'v] |
| 105 |
// |
| 106 |
// The vertex shader calculates the rightmost 2x2 matrix and interpolates it across the shape since |
| 107 |
// each component is linear in (u,v). ∇Cl(u,v) is evaluated per pixel in the fragment shader and |
| 108 |
// depends on which corner and edge being evaluated. w(u,v) is the device-space W coordinate, so |
| 109 |
// its reciprocal is provided in sk_FragCoord.w. |
| 110 |
namespace skgpu::graphite { |
| 111 |
|
| 112 |
using AAFlags = EdgeAAQuad::Flags; |
| 113 |
|
| 114 |
static bool is_clockwise(const EdgeAAQuad& quad) { |
| 115 |
if (quad.isRect()) { |
| 116 |
return true; // by construction, these are always locally clockwise |
| 117 |
} |
| 118 |
|
| 119 |
// This assumes that each corner has a consistent winding, which is the case for convex inputs, |
| 120 |
// which is an assumption of the per-edge AA API. Check the sign of cross product between the |
| 121 |
// first two edges. |
| 122 |
const skvx::float4& xs = quad.xs(); |
| 123 |
const skvx::float4& ys = quad.ys(); |
| 124 |
|
| 125 |
float winding = (xs[0] - xs[3])*(ys[1] - ys[0]) - (ys[0] - ys[3])*(xs[1] - xs[0]); |
| 126 |
if (winding == 0.f) { |
| 127 |
// The input possibly forms a triangle with duplicate vertices, so check the opposite corner |
| 128 |
winding = (xs[2] - xs[1])*(ys[3] - ys[2]) - (ys[2] - ys[1])*(xs[3] - xs[2]); |
| 129 |
} |
| 130 |
|
| 131 |
// At this point if winding is < 0, the quad's vertices are CCW. If it's still 0, the vertices |
| 132 |
// form a line, in which case the vertex shader constructs a correct CW winding. Otherwise, |
| 133 |
// the quad or triangle vertices produce a positive winding and are CW. |
| 134 |
return winding >= 0.f; |
| 135 |
} |
| 136 |
|
| 137 |
// Allowed values for the center weight instance value (selected at record time based on style |
| 138 |
// and transform), and are defined such that when (insance-weight > vertex-weight) is true, the |
| 139 |
// vertex should be snapped to the center instead of its regular calculation. |
| 140 |
static constexpr int kCornerVertexCount = 4; // sk_VertexID is divided by this in SkSL |
| 141 |
static constexpr int kVertexCount = 4 * kCornerVertexCount; |
| 142 |
static constexpr int kIndexCount = 29; |
| 143 |
|
| 144 |
// Represents the per-vertex attributes used in each instance. |
| 145 |
struct Vertex { |
| 146 |
uint32_t fCornerID; |
| 147 |
SkV2 fNormal; |
| 148 |
}; |
| 149 |
|
| 150 |
// This template is repeated 4 times in the vertex buffer, for each of the four corners: TL -> TR |
| 151 |
// -> BR -> BL. The corner ID is used to lookup per-corner instance properties such as positions. |
| 152 |
template<uint32_t kCornerID> |
| 153 |
constexpr std::array<Vertex, kCornerVertexCount> get_per_corner_vertex_attrs() { |
| 154 |
constexpr float kHR2 = 0.5f * SK_FloatSqrt2; // "half root 2" |
| 155 |
|
| 156 |
return {{ |
| 157 |
// Normals for device-space AA outsets from outer curve |
| 158 |
{ kCornerID, {1.0f, 0.0f} }, |
| 159 |
{ kCornerID, {kHR2, kHR2} }, |
| 160 |
{ kCornerID, {0.0f, 1.0f} }, |
| 161 |
|
| 162 |
// Normal for outer anchor (zero length to signal no local or device-space normal outset) |
| 163 |
{ kCornerID, {0.0f, 0.0f} }, |
| 164 |
}}; |
| 165 |
} |
| 166 |
|
| 167 |
static void write_index_buffer(VertexWriter writer) { |
| 168 |
static constexpr uint16_t kTL = 0 * kCornerVertexCount; |
| 169 |
static constexpr uint16_t kTR = 1 * kCornerVertexCount; |
| 170 |
static constexpr uint16_t kBR = 2 * kCornerVertexCount; |
| 171 |
static constexpr uint16_t kBL = 3 * kCornerVertexCount; |
| 172 |
|
| 173 |
static const uint16_t kIndices[kIndexCount] = { |
| 174 |
// Exterior AA ramp outset |
| 175 |
kTL+1,kTL+2,kTL+3,kTR+0,kTR+3,kTR+1, |
| 176 |
kTR+1,kTR+2,kTR+3,kBR+0,kBR+3,kBR+1, |
| 177 |
kBR+1,kBR+2,kBR+3,kBL+0,kBL+3,kBL+1, |
| 178 |
kBL+1,kBL+2,kBL+3,kTL+0,kTL+3,kTL+1, |
| 179 |
kTL+3, |
| 180 |
// Fill triangles |
| 181 |
kTL+3,kTR+3,kBL+3,kBR+3 |
| 182 |
}; |
| 183 |
|
| 184 |
if (writer) { |
| 185 |
writer << kIndices; |
| 186 |
} // otherwise static buffer creation failed, so do nothing; Context initialization will fail. |
| 187 |
} |
| 188 |
|
| 189 |
static void write_vertex_buffer(VertexWriter writer) { |
| 190 |
if (writer) { |
| 191 |
writer << get_per_corner_vertex_attrs<0>() // TL |
| 192 |
<< get_per_corner_vertex_attrs<1>() // TR |
| 193 |
<< get_per_corner_vertex_attrs<2>() // BR |
| 194 |
<< get_per_corner_vertex_attrs<3>(); // BL |
| 195 |
} // otherwise static buffer creation failed, so do nothing; Context initialization will fail. |
| 196 |
} |
| 197 |
|
| 198 |
PerEdgeAAQuadRenderStep::PerEdgeAAQuadRenderStep(Layout layout, StaticBufferManager* bufferManager) |
| 199 |
: RenderStep(layout, |
| 200 |
RenderStepID::kPerEdgeAAQuad, |
| 201 |
Flags::kPerformsShading | Flags::kEmitsCoverage | Flags::kOutsetBoundsForAA | |
| 202 |
Flags::kUseNonAAInnerFill | Flags::kAppendInstances, |
| 203 |
/*uniforms=*/{}, |
| 204 |
PrimitiveType::kTriangleStrip, |
| 205 |
kDirectDepthLessPass, |
| 206 |
/*staticAttrs=*/{{ |
| 207 |
{"cornerID", VertexAttribType::kUInt, SkSLType::kUInt }, |
| 208 |
{"normal", VertexAttribType::kFloat2, SkSLType::kFloat2}, |
| 209 |
}}, |
| 210 |
/*appendAttrs=*/{{ |
| 211 |
{"edgeFlags", VertexAttribType::kUByte4_norm, SkSLType::kFloat4}, |
| 212 |
{"quadXs", VertexAttribType::kFloat4, SkSLType::kFloat4}, |
| 213 |
{"quadYs", VertexAttribType::kFloat4, SkSLType::kFloat4}, |
| 214 |
|
| 215 |
// TODO: pack depth and ssbo index into one 32-bit attribute, if we can |
| 216 |
// go without needing both render step and paint ssbo index attributes. |
| 217 |
{"depth", VertexAttribType::kFloat, SkSLType::kFloat}, |
| 218 |
{"ssboIndex", VertexAttribType::kUInt, SkSLType::kUInt}, |
| 219 |
|
| 220 |
{"mat0", VertexAttribType::kFloat3, SkSLType::kFloat3}, |
| 221 |
{"mat1", VertexAttribType::kFloat3, SkSLType::kFloat3}, |
| 222 |
{"mat2", VertexAttribType::kFloat3, SkSLType::kFloat3}}}, |
| 223 |
/*varyings=*/{{ |
| 224 |
// Device-space distance to LTRB edges of quad. |
| 225 |
{"edgeDistances", SkSLType::kFloat4}, // distance to LTRB edges |
| 226 |
}}) { |
| 227 |
// Initialize the static buffers we'll use when recording draw calls. |
| 228 |
// NOTE: Each instance of this RenderStep gets its own copy of the data. Since there should only |
| 229 |
// ever be one PerEdgeAAQuadRenderStep at a time, this shouldn't be an issue. |
| 230 |
write_vertex_buffer(bufferManager->getVertexWriter(kVertexCount, sizeof(Vertex), |
| 231 |
&fVertexBuffer)); |
| 232 |
write_index_buffer(bufferManager->getIndexWriter(sizeof(uint16_t) * kIndexCount, |
| 233 |
&fIndexBuffer)); |
| 234 |
} |
| 235 |
|
| 236 |
PerEdgeAAQuadRenderStep::~PerEdgeAAQuadRenderStep() {} |
| 237 |
|
| 238 |
std::string PerEdgeAAQuadRenderStep::vertexSkSL() const { |
| 239 |
// Returns the body of a vertex function, which must define a float4 devPosition variable and |
| 240 |
// must write to an already-defined float2 stepLocalCoords variable. |
| 241 |
return "float4 devPosition = per_edge_aa_quad_vertex_fn(" |
| 242 |
// Static Data Attributes |
| 243 |
"cornerID, normal, " |
| 244 |
// Append Data Attributes |
| 245 |
"edgeFlags, quadXs, quadYs, depth, " |
| 246 |
"float3x3(mat0, mat1, mat2), " |
| 247 |
// Varyings |
| 248 |
"edgeDistances, " |
| 249 |
// Render Step |
| 250 |
"stepLocalCoords);\n"; |
| 251 |
} |
| 252 |
|
| 253 |
const char* PerEdgeAAQuadRenderStep::fragmentCoverageSkSL() const { |
| 254 |
// The returned SkSL must write its coverage into a 'half4 outputCoverage' variable (defined in |
| 255 |
// the calling code) with the actual coverage splatted out into all four channels. |
| 256 |
return "outputCoverage = per_edge_aa_quad_coverage_fn(sk_FragCoord, edgeDistances);"; |
| 257 |
} |
| 258 |
|
| 259 |
void PerEdgeAAQuadRenderStep::writeVertices(DrawWriter* writer, |
| 260 |
const DrawParams& params, |
| 261 |
uint32_t ssboIndex) const { |
| 262 |
SkASSERT(params.geometry().isEdgeAAQuad()); |
| 263 |
const EdgeAAQuad& quad = params.geometry().edgeAAQuad(); |
| 264 |
|
| 265 |
DrawWriter::Instances instance{*writer, fVertexBuffer, fIndexBuffer, kIndexCount}; |
| 266 |
auto vw = instance.append(1); |
| 267 |
|
| 268 |
// Empty fills should not have been recorded at all. |
| 269 |
SkDEBUGCODE(Rect bounds = params.geometry().bounds()); |
| 270 |
SkASSERT(!bounds.isEmptyNegativeOrNaN()); |
| 271 |
|
| 272 |
constexpr uint8_t kAAOn = 255; |
| 273 |
constexpr uint8_t kAAOff = 0; |
| 274 |
auto edgeSigns = skvx::byte4{quad.edgeFlags() & AAFlags::kLeft ? kAAOn : kAAOff, |
| 275 |
quad.edgeFlags() & AAFlags::kTop ? kAAOn : kAAOff, |
| 276 |
quad.edgeFlags() & AAFlags::kRight ? kAAOn : kAAOff, |
| 277 |
quad.edgeFlags() & AAFlags::kBottom ? kAAOn : kAAOff}; |
| 278 |
|
| 279 |
// The vertex shader expects points to be in clockwise order. EdgeAAQuad is the only |
| 280 |
// shape that *might* have counter-clockwise input. |
| 281 |
if (is_clockwise(quad)) { |
| 282 |
vw << edgeSigns << quad.xs() << quad.ys(); |
| 283 |
} else { |
| 284 |
vw << skvx::shuffle<2,1,0,3>(edgeSigns) // swap left and right AA bits |
| 285 |
<< skvx::shuffle<1,0,3,2>(quad.xs()) // swap TL with TR, and BL with BR |
| 286 |
<< skvx::shuffle<1,0,3,2>(quad.ys()); // "" |
| 287 |
} |
| 288 |
|
| 289 |
// All instance types share the remaining instance attribute definitions |
| 290 |
const SkM44& m = params.transform().matrix(); |
| 291 |
|
| 292 |
vw << params.order().depthAsFloat() |
| 293 |
<< ssboIndex |
| 294 |
<< m.rc(0,0) << m.rc(1,0) << m.rc(3,0) // mat0 |
| 295 |
<< m.rc(0,1) << m.rc(1,1) << m.rc(3,1) // mat1 |
| 296 |
<< m.rc(0,3) << m.rc(1,3) << m.rc(3,3); // mat2 |
| 297 |
} |
| 298 |
|
| 299 |
void PerEdgeAAQuadRenderStep::writeUniformsAndTextures(const DrawParams&, |
| 300 |
PipelineDataGatherer* gatherer) const { |
| 301 |
// All data is uploaded as instance attributes, so no uniforms are needed. |
| 302 |
SkDEBUGCODE(gatherer->checkRewind()); |
| 303 |
} |
| 304 |
|
| 305 |
} // namespace skgpu::graphite |