| 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/TessellateCurvesRenderStep.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/SkSpan_impl.h" |
| 15 |
#include "src/base/SkEnumBitMask.h" |
| 16 |
#include "src/core/SkPathPriv.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/CommonDepthStencilSettings.h" |
| 29 |
#include "src/gpu/graphite/render/DynamicInstancesPatchAllocator.h" |
| 30 |
#include "src/gpu/tessellate/FixedCountBufferUtils.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 |
#include <utility> |
| 38 |
|
| 39 |
namespace skgpu::graphite { |
| 40 |
|
| 41 |
namespace { |
| 42 |
|
| 43 |
using namespace skgpu::tess; |
| 44 |
|
| 45 |
// No fan point or stroke params, since this is for filled curves (not strokes or wedges) |
| 46 |
// No explicit curve type on platforms that support infinity. |
| 47 |
// No color or wide color attribs, since it might always be part of the PaintParams |
| 48 |
// or we'll add a color-only fast path to RenderStep later. |
| 49 |
static constexpr PatchAttribs kAttribs = PatchAttribs::kPaintDepth | |
| 50 |
PatchAttribs::kSsboIndex; |
| 51 |
static constexpr PatchAttribs kAttribsWithCurveType = kAttribs | PatchAttribs::kExplicitCurveType; |
| 52 |
using Writer = PatchWriter<DynamicInstancesPatchAllocator<FixedCountCurves>, |
| 53 |
Required<PatchAttribs::kPaintDepth>, |
| 54 |
Required<PatchAttribs::kSsboIndex>, |
| 55 |
Optional<PatchAttribs::kExplicitCurveType>, |
| 56 |
AddTrianglesWhenChopping, |
| 57 |
DiscardFlatCurves>; |
| 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 |
{"depth", VertexAttribType::kFloat, SkSLType::kFloat}, |
| 66 |
{"ssboIndex", VertexAttribType::kUInt, SkSLType::kUInt}}; |
| 67 |
|
| 68 |
static constexpr Attribute kAttributesWithCurveType[] = { |
| 69 |
{"p01", VertexAttribType::kFloat4, SkSLType::kFloat4}, |
| 70 |
{"p23", VertexAttribType::kFloat4, SkSLType::kFloat4}, |
| 71 |
{"depth", VertexAttribType::kFloat, SkSLType::kFloat}, |
| 72 |
{"curveType", VertexAttribType::kFloat, SkSLType::kFloat}, |
| 73 |
{"ssboIndex", VertexAttribType::kUInt, SkSLType::kUInt}}; |
| 74 |
|
| 75 |
static constexpr SkSpan<const Attribute> kAttributes[2] = {kAttributesWithCurveType, |
| 76 |
kBaseAttributes}; |
| 77 |
|
| 78 |
} // namespace |
| 79 |
|
| 80 |
TessellateCurvesRenderStep::TessellateCurvesRenderStep(Layout layout, |
| 81 |
bool evenOdd, |
| 82 |
bool infinitySupport, |
| 83 |
StaticBufferManager* bufferManager) |
| 84 |
: RenderStep(layout, |
| 85 |
evenOdd ? RenderStepID::kTessellateCurves_EvenOdd |
| 86 |
: RenderStepID::kTessellateCurves_Winding, |
| 87 |
Flags::kRequiresMSAA | |
| 88 |
Flags::kAppendDynamicInstances | |
| 89 |
Flags::kIgnoreInverseFill, |
| 90 |
/*uniforms=*/{{"localToDevice", SkSLType::kFloat4x4}}, |
| 91 |
PrimitiveType::kTriangles, |
| 92 |
evenOdd ? kEvenOddStencilPass : kWindingStencilPass, |
| 93 |
/*staticAttrs=*/{{{"resolveLevel_and_idx", |
| 94 |
VertexAttribType::kFloat2, SkSLType::kFloat2}}}, |
| 95 |
/*appendAttrs=*/kAttributes[infinitySupport]) |
| 96 |
, fInfinitySupport(infinitySupport) { |
| 97 |
SkASSERT(this->appendDataStride() == |
| 98 |
PatchStride(infinitySupport ? kAttribs : kAttribsWithCurveType)); |
| 99 |
|
| 100 |
// Initialize the static buffers we'll use when recording draw calls. |
| 101 |
// NOTE: Each instance of this RenderStep gets its own copy of the data. If this ends up causing |
| 102 |
// problems, we can modify StaticBufferManager to de-duplicate requests. |
| 103 |
auto vertexData = bufferManager->getVertexWriter(FixedCountCurves::VertexBufferVertexCount(), |
| 104 |
FixedCountCurves::VertexBufferStride(), |
| 105 |
&fVertexBuffer); |
| 106 |
if (vertexData) { |
| 107 |
FixedCountCurves::WriteVertexBuffer(std::move(vertexData), |
| 108 |
FixedCountCurves::VertexBufferSize()); |
| 109 |
} // otherwise static buffer creation failed, so do nothing; Context initialization will fail. |
| 110 |
|
| 111 |
const size_t indexSize = FixedCountCurves::IndexBufferSize(); |
| 112 |
auto indexData = bufferManager->getIndexWriter(indexSize, &fIndexBuffer); |
| 113 |
if (indexData) { |
| 114 |
FixedCountCurves::WriteIndexBuffer(std::move(indexData), indexSize); |
| 115 |
} // "" |
| 116 |
} |
| 117 |
|
| 118 |
TessellateCurvesRenderStep::~TessellateCurvesRenderStep() {} |
| 119 |
|
| 120 |
std::string TessellateCurvesRenderStep::vertexSkSL() const { |
| 121 |
return SkSL::String::printf( |
| 122 |
// TODO: Approximate perspective scaling to match how PatchWriter is configured (or |
| 123 |
// provide explicit tessellation level in instance data instead of replicating |
| 124 |
// work). |
| 125 |
"float2x2 vectorXform = float2x2(localToDevice[0].xy, localToDevice[1].xy);\n" |
| 126 |
"float2 localCoord = tessellate_filled_curve(" |
| 127 |
"vectorXform, resolveLevel_and_idx.x, resolveLevel_and_idx.y, p01, p23, %s);\n" |
| 128 |
"float4 devPosition = localToDevice * float4(localCoord, 0.0, 1.0);\n" |
| 129 |
"devPosition.z = depth;\n" |
| 130 |
"stepLocalCoords = localCoord;\n", |
| 131 |
fInfinitySupport ? "curve_type_using_inf_support(p23)" : "curveType"); |
| 132 |
} |
| 133 |
|
| 134 |
void TessellateCurvesRenderStep::writeVertices(DrawWriter* dw, |
| 135 |
const DrawParams& params, |
| 136 |
uint32_t ssboIndex) const { |
| 137 |
SkPath path = params.geometry().shape().asPath(); // TODO: Iterate the Shape directly |
| 138 |
|
| 139 |
int patchReserveCount = FixedCountCurves::PreallocCount(path.countVerbs()); |
| 140 |
Writer writer{fInfinitySupport ? kAttribs : kAttribsWithCurveType, |
| 141 |
*dw, |
| 142 |
fVertexBuffer, |
| 143 |
fIndexBuffer, |
| 144 |
patchReserveCount}; |
| 145 |
writer.updatePaintDepthAttrib(params.order().depthAsFloat()); |
| 146 |
writer.updateSsboIndexAttrib(ssboIndex); |
| 147 |
|
| 148 |
// The vector xform approximates how the control points are transformed by the shader to |
| 149 |
// more accurately compute how many *parametric* segments are needed. |
| 150 |
// TODO: This doesn't account for perspective division yet, which will require updating the |
| 151 |
// approximate transform based on each verb's control points' bounding box. |
| 152 |
SkASSERT(params.transform().type() < Transform::Type::kPerspective); |
| 153 |
writer.setShaderTransform(wangs_formula::VectorXform{params.transform().matrix()}, |
| 154 |
params.transform().maxScaleFactor()); |
| 155 |
|
| 156 |
// TODO: For filled curves, the path verb loop is simple enough that it's not too big a deal |
| 157 |
// to copy the logic from PathCurveTessellator::write_patches. It may be required if we end |
| 158 |
// up switching to a shape iterator in graphite vs. a path iterator in ganesh, or if |
| 159 |
// graphite does not control point transformation on the CPU. On the other hand, if we |
| 160 |
// provide a templated WritePatches function, the iterator could also be a template arg in |
| 161 |
// addition to PatchWriter's traits. Whatever pattern we choose will be based more on what's |
| 162 |
// best for the wedge and stroke case, which have more complex loops. |
| 163 |
for (auto [verb, pts, w] : SkPathPriv::Iterate(path)) { |
| 164 |
switch (verb) { |
| 165 |
case SkPathVerb::kQuad: writer.writeQuadratic(pts); break; |
| 166 |
case SkPathVerb::kConic: writer.writeConic(pts, *w); break; |
| 167 |
case SkPathVerb::kCubic: writer.writeCubic(pts); break; |
| 168 |
default: break; |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
void TessellateCurvesRenderStep::writeUniformsAndTextures(const DrawParams& params, |
| 174 |
PipelineDataGatherer* gatherer) const { |
| 175 |
SkDEBUGCODE(gatherer->checkRewind()); |
| 176 |
SkDEBUGCODE(UniformExpectationsValidator uev(gatherer, this->uniforms());) |
| 177 |
|
| 178 |
gatherer->write(params.transform().matrix()); |
| 179 |
} |
| 180 |
|
| 181 |
} // namespace skgpu::graphite |