| 1 |
/* |
| 2 |
* Copyright 2022 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/TessellateWedgesRenderStep.h" |
| 9 |
|
| 10 |
#include "include/core/SkPath.h" |
| 11 |
#include "include/core/SkPathTypes.h" |
| 12 |
#include "include/private/base/SkAssert.h" |
| 13 |
#include "include/private/base/SkDebug.h" |
| 14 |
#include "include/private/base/SkPoint_impl.h" |
| 15 |
#include "include/private/base/SkSpan_impl.h" |
| 16 |
#include "src/base/SkEnumBitMask.h" |
| 17 |
#include "src/core/SkSLTypeShared.h" |
| 18 |
#include "src/gpu/BufferWriter.h" |
| 19 |
#include "src/gpu/graphite/Attribute.h" |
| 20 |
#include "src/gpu/graphite/BufferManager.h" |
| 21 |
#include "src/gpu/graphite/DrawOrder.h" |
| 22 |
#include "src/gpu/graphite/DrawParams.h" |
| 23 |
#include "src/gpu/graphite/DrawTypes.h" |
| 24 |
#include "src/gpu/graphite/PipelineData.h" |
| 25 |
#include "src/gpu/graphite/geom/Geometry.h" |
| 26 |
#include "src/gpu/graphite/geom/Shape.h" |
| 27 |
#include "src/gpu/graphite/geom/Transform.h" |
| 28 |
#include "src/gpu/graphite/render/DynamicInstancesPatchAllocator.h" |
| 29 |
#include "src/gpu/tessellate/FixedCountBufferUtils.h" |
| 30 |
#include "src/gpu/tessellate/MidpointContourParser.h" |
| 31 |
#include "src/gpu/tessellate/PatchWriter.h" |
| 32 |
#include "src/gpu/tessellate/Tessellation.h" |
| 33 |
#include "src/gpu/tessellate/WangsFormula.h" |
| 34 |
#include "src/sksl/SkSLString.h" |
| 35 |
|
| 36 |
#include <cstddef> |
| 37 |
|
| 38 |
namespace skgpu::graphite { |
| 39 |
|
| 40 |
namespace { |
| 41 |
|
| 42 |
using namespace skgpu::tess; |
| 43 |
|
| 44 |
// Only kFanPoint, no stroke params, since this is for filled wedges. |
| 45 |
// No color or wide color attribs, since it might always be part of the PaintParams |
| 46 |
// or we'll add a color-only fast path to RenderStep later. |
| 47 |
// No explicit curve type on platforms that support infinity. |
| 48 |
static constexpr PatchAttribs kAttribs = PatchAttribs::kFanPoint | |
| 49 |
PatchAttribs::kPaintDepth | |
| 50 |
PatchAttribs::kSsboIndex; |
| 51 |
static constexpr PatchAttribs kAttribsWithCurveType = kAttribs | PatchAttribs::kExplicitCurveType; |
| 52 |
|
| 53 |
using Writer = PatchWriter<DynamicInstancesPatchAllocator<FixedCountWedges>, |
| 54 |
Required<PatchAttribs::kFanPoint>, |
| 55 |
Required<PatchAttribs::kPaintDepth>, |
| 56 |
Required<PatchAttribs::kSsboIndex>, |
| 57 |
Optional<PatchAttribs::kExplicitCurveType>>; |
| 58 |
|
| 59 |
// The order of the attribute declarations must match the order used by |
| 60 |
// PatchWriter::emitPatchAttribs, i.e.: |
| 61 |
// join << fanPoint << stroke << color << depth << curveType << ssboIndex |
| 62 |
static constexpr Attribute kBaseAttributes[] = { |
| 63 |
{"p01", VertexAttribType::kFloat4, SkSLType::kFloat4}, |
| 64 |
{"p23", VertexAttribType::kFloat4, SkSLType::kFloat4}, |
| 65 |
{"fanPointAttrib", VertexAttribType::kFloat2, SkSLType::kFloat2}, |
| 66 |
{"depth", VertexAttribType::kFloat, SkSLType::kFloat}, |
| 67 |
{"ssboIndex", VertexAttribType::kUInt, SkSLType::kUInt}}; |
| 68 |
|
| 69 |
static constexpr Attribute kAttributesWithCurveType[] = { |
| 70 |
{"p01", VertexAttribType::kFloat4, SkSLType::kFloat4}, |
| 71 |
{"p23", VertexAttribType::kFloat4, SkSLType::kFloat4}, |
| 72 |
{"fanPointAttrib", VertexAttribType::kFloat2, SkSLType::kFloat2}, |
| 73 |
{"depth", VertexAttribType::kFloat, SkSLType::kFloat}, |
| 74 |
{"curveType", VertexAttribType::kFloat, SkSLType::kFloat}, |
| 75 |
{"ssboIndex", VertexAttribType::kUInt, SkSLType::kUInt}}; |
| 76 |
|
| 77 |
static constexpr SkSpan<const Attribute> kAttributes[2] = {kAttributesWithCurveType, |
| 78 |
kBaseAttributes}; |
| 79 |
|
| 80 |
} // namespace |
| 81 |
|
| 82 |
TessellateWedgesRenderStep::TessellateWedgesRenderStep(Layout layout, |
| 83 |
RenderStepID renderStepID, |
| 84 |
bool infinitySupport, |
| 85 |
DepthStencilSettings depthStencilSettings, |
| 86 |
StaticBufferManager* bufferManager) |
| 87 |
: RenderStep(layout, |
| 88 |
renderStepID, |
| 89 |
Flags::kRequiresMSAA | |
| 90 |
Flags::kAppendDynamicInstances | |
| 91 |
Flags::kIgnoreInverseFill | |
| 92 |
(depthStencilSettings.fDepthWriteEnabled ? Flags::kPerformsShading |
| 93 |
: Flags::kNone), |
| 94 |
/*uniforms=*/{{"localToDevice", SkSLType::kFloat4x4}}, |
| 95 |
PrimitiveType::kTriangles, |
| 96 |
depthStencilSettings, |
| 97 |
/*staticAttrs=*/{{{"resolveLevel_and_idx", |
| 98 |
VertexAttribType::kFloat2, SkSLType::kFloat2}}}, |
| 99 |
/*appendAttrs=*/kAttributes[infinitySupport]) |
| 100 |
, fInfinitySupport(infinitySupport) { |
| 101 |
SkASSERT(this->appendDataStride() == |
| 102 |
PatchStride(infinitySupport ? kAttribs : kAttribsWithCurveType)); |
| 103 |
|
| 104 |
// Initialize the static buffers we'll use when recording draw calls. |
| 105 |
// NOTE: Each instance of this RenderStep gets its own copy of the data. If this ends up causing |
| 106 |
// problems, we can modify StaticBufferManager to de-duplicate requests. |
| 107 |
auto vertexData = bufferManager->getVertexWriter(FixedCountWedges::VertexBufferVertexCount(), |
| 108 |
FixedCountWedges::VertexBufferStride(), |
| 109 |
&fVertexBuffer); |
| 110 |
if (vertexData) { |
| 111 |
FixedCountWedges::WriteVertexBuffer(std::move(vertexData), |
| 112 |
FixedCountWedges::VertexBufferSize()); |
| 113 |
} // otherwise static buffer creation failed, so do nothing; Context initialization will fail. |
| 114 |
|
| 115 |
const size_t indexSize = FixedCountWedges::IndexBufferSize(); |
| 116 |
auto indexData = bufferManager->getIndexWriter(indexSize, &fIndexBuffer); |
| 117 |
if (indexData) { |
| 118 |
FixedCountWedges::WriteIndexBuffer(std::move(indexData), indexSize); |
| 119 |
} // otherwise static buffer creation failed, so do nothing; Context initialization will fail. |
| 120 |
} |
| 121 |
|
| 122 |
TessellateWedgesRenderStep::~TessellateWedgesRenderStep() {} |
| 123 |
|
| 124 |
std::string TessellateWedgesRenderStep::vertexSkSL() const { |
| 125 |
return SkSL::String::printf( |
| 126 |
"float2 localCoord;\n" |
| 127 |
"if (resolveLevel_and_idx.x < 0) {\n" |
| 128 |
// A negative resolve level means this is the fan point. |
| 129 |
"localCoord = fanPointAttrib;\n" |
| 130 |
"} else {\n" |
| 131 |
// TODO: Approximate perspective scaling to match how PatchWriter is configured |
| 132 |
// (or provide explicit tessellation level in instance data instead of |
| 133 |
// replicating work) |
| 134 |
"float2x2 vectorXform = float2x2(localToDevice[0].xy, localToDevice[1].xy);\n" |
| 135 |
"localCoord = tessellate_filled_curve(" |
| 136 |
"vectorXform, resolveLevel_and_idx.x, resolveLevel_and_idx.y, p01, p23, %s);\n" |
| 137 |
"}\n" |
| 138 |
"float4 devPosition = localToDevice * float4(localCoord, 0.0, 1.0);\n" |
| 139 |
"devPosition.z = depth;\n" |
| 140 |
"stepLocalCoords = localCoord;\n", |
| 141 |
fInfinitySupport ? "curve_type_using_inf_support(p23)" : "curveType"); |
| 142 |
} |
| 143 |
|
| 144 |
void TessellateWedgesRenderStep::writeVertices(DrawWriter* dw, |
| 145 |
const DrawParams& params, |
| 146 |
uint32_t ssboIndex) const { |
| 147 |
SkPath path = params.geometry().shape().asPath(); // TODO: Iterate the Shape directly |
| 148 |
|
| 149 |
int patchReserveCount = FixedCountWedges::PreallocCount(path.countVerbs()); |
| 150 |
Writer writer{fInfinitySupport ? kAttribs : kAttribsWithCurveType, |
| 151 |
*dw, |
| 152 |
fVertexBuffer, |
| 153 |
fIndexBuffer, |
| 154 |
patchReserveCount}; |
| 155 |
writer.updatePaintDepthAttrib(params.order().depthAsFloat()); |
| 156 |
writer.updateSsboIndexAttrib(ssboIndex); |
| 157 |
|
| 158 |
// The vector xform approximates how the control points are transformed by the shader to |
| 159 |
// more accurately compute how many *parametric* segments are needed. |
| 160 |
// TODO: This doesn't account for perspective division yet, which will require updating the |
| 161 |
// approximate transform based on each verb's control points' bounding box. |
| 162 |
SkASSERT(params.transform().type() < Transform::Type::kPerspective); |
| 163 |
writer.setShaderTransform(wangs_formula::VectorXform{params.transform().matrix()}, |
| 164 |
params.transform().maxScaleFactor()); |
| 165 |
|
| 166 |
// TODO: Essentially the same as PathWedgeTessellator::write_patches but with a different |
| 167 |
// PatchWriter template. |
| 168 |
// For wedges, we iterate over each contour explicitly, using a fan point position that is in |
| 169 |
// the midpoint of the current contour. |
| 170 |
MidpointContourParser parser{path}; |
| 171 |
while (parser.parseNextContour()) { |
| 172 |
writer.updateFanPointAttrib(parser.currentMidpoint()); |
| 173 |
SkPoint lastPoint = {0, 0}; |
| 174 |
SkPoint startPoint = {0, 0}; |
| 175 |
for (auto [verb, pts, w] : parser.currentContour()) { |
| 176 |
switch (verb) { |
| 177 |
case SkPathVerb::kMove: |
| 178 |
startPoint = lastPoint = pts[0]; |
| 179 |
break; |
| 180 |
case SkPathVerb::kLine: |
| 181 |
// Unlike curve tessellation, wedges have to handle lines as part of the patch, |
| 182 |
// effectively forming a single triangle with the fan point. |
| 183 |
writer.writeLine(pts[0], pts[1]); |
| 184 |
lastPoint = pts[1]; |
| 185 |
break; |
| 186 |
case SkPathVerb::kQuad: |
| 187 |
writer.writeQuadratic(pts); |
| 188 |
lastPoint = pts[2]; |
| 189 |
break; |
| 190 |
case SkPathVerb::kConic: |
| 191 |
writer.writeConic(pts, *w); |
| 192 |
lastPoint = pts[2]; |
| 193 |
break; |
| 194 |
case SkPathVerb::kCubic: |
| 195 |
writer.writeCubic(pts); |
| 196 |
lastPoint = pts[3]; |
| 197 |
break; |
| 198 |
default: break; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
// Explicitly close the contour with another line segment, which also differs from curve |
| 203 |
// tessellation since that approach's triangle step automatically closes the contour. |
| 204 |
if (lastPoint != startPoint) { |
| 205 |
writer.writeLine(lastPoint, startPoint); |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
void TessellateWedgesRenderStep::writeUniformsAndTextures(const DrawParams& params, |
| 211 |
PipelineDataGatherer* gatherer) const { |
| 212 |
SkDEBUGCODE(gatherer->checkRewind()); |
| 213 |
SkDEBUGCODE(UniformExpectationsValidator uev(gatherer, this->uniforms());) |
| 214 |
|
| 215 |
gatherer->write(params.transform().matrix()); |
| 216 |
} |
| 217 |
|
| 218 |
} // namespace skgpu::graphite |