blob: 13087638f0b211ac04cd1a677dabf569b0c0fa54 [file] [log] [blame]
Chris Dalton70a0d2c2021-01-26 12:01:21 -07001/*
2 * Copyright 2021 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
Chris Dalton2ed22fa2021-05-06 16:08:30 -06008#include "src/gpu/tessellate/GrPathStencilFillOp.h"
Chris Dalton70a0d2c2021-01-26 12:01:21 -07009
10#include "src/gpu/GrEagerVertexAllocator.h"
11#include "src/gpu/GrOpFlushState.h"
12#include "src/gpu/GrRecordingContextPriv.h"
13#include "src/gpu/tessellate/GrFillPathShader.h"
14#include "src/gpu/tessellate/GrMiddleOutPolygonTriangulator.h"
15#include "src/gpu/tessellate/GrPathTessellator.h"
16#include "src/gpu/tessellate/GrStencilPathShader.h"
17#include "src/gpu/tessellate/GrTessellationPathRenderer.h"
18
19using OpFlags = GrTessellationPathRenderer::OpFlags;
20
Chris Dalton2ed22fa2021-05-06 16:08:30 -060021void GrPathStencilFillOp::visitProxies(const VisitProxyFunc& fn) const {
Chris Dalton70a0d2c2021-01-26 12:01:21 -070022 if (fFillBBoxProgram) {
23 fFillBBoxProgram->pipeline().visitProxies(fn);
24 } else {
25 fProcessors.visitProxies(fn);
26 }
27}
28
Chris Dalton2ed22fa2021-05-06 16:08:30 -060029GrDrawOp::FixedFunctionFlags GrPathStencilFillOp::fixedFunctionFlags() const {
Chris Dalton70a0d2c2021-01-26 12:01:21 -070030 auto flags = FixedFunctionFlags::kUsesStencil;
31 if (fAAType != GrAAType::kNone) {
32 flags |= FixedFunctionFlags::kUsesHWAA;
33 }
34 return flags;
35}
36
Chris Dalton2ed22fa2021-05-06 16:08:30 -060037GrProcessorSet::Analysis GrPathStencilFillOp::finalize(const GrCaps& caps,
38 const GrAppliedClip* clip,
39 GrClampType clampType) {
Chris Dalton57ab06c2021-04-22 12:57:28 -060040 return fProcessors.finalize(fColor, GrProcessorAnalysisCoverage::kNone, clip, nullptr, caps,
41 clampType, &fColor);
Chris Dalton70a0d2c2021-01-26 12:01:21 -070042}
43
Chris Dalton2ed22fa2021-05-06 16:08:30 -060044void GrPathStencilFillOp::prePreparePrograms(const GrPathShader::ProgramArgs& args,
45 GrAppliedClip&& appliedClip) {
Chris Dalton70a0d2c2021-01-26 12:01:21 -070046 SkASSERT(!fStencilFanProgram);
47 SkASSERT(!fStencilPathProgram);
48 SkASSERT(!fFillBBoxProgram);
49
50 int numVerbs = fPath.countVerbs();
51 if (numVerbs <= 0) {
52 return;
53 }
54
55 // When there are only a few verbs, it seems to always be fastest to make a single indirect draw
56 // that contains both the inner triangles and the outer curves, instead of using hardware
57 // tessellation. Also take this path if tessellation is not supported.
58 bool drawTrianglesAsIndirectCurveDraw = (numVerbs < 50);
59 const GrPipeline* stencilPassPipeline = GrStencilPathShader::MakeStencilPassPipeline(
60 args, fAAType, fOpFlags, appliedClip.hardClip());
61 if (drawTrianglesAsIndirectCurveDraw || (fOpFlags & OpFlags::kDisableHWTessellation)) {
62 fTessellator = args.fArena->make<GrPathIndirectTessellator>(
Chris Dalton2758a312021-05-20 10:46:25 -060063 fViewMatrix, fPath,
64 GrPathTessellator::DrawInnerFan(drawTrianglesAsIndirectCurveDraw));
Chris Dalton70a0d2c2021-01-26 12:01:21 -070065 if (!drawTrianglesAsIndirectCurveDraw) {
66 fStencilFanProgram = GrStencilPathShader::MakeStencilProgram<GrStencilTriangleShader>(
67 args, fViewMatrix, stencilPassPipeline, fPath.getFillType());
68 }
Chris Daltona9f759d2021-05-18 12:37:08 -060069 fStencilPathProgram = GrStencilPathShader::MakeStencilProgram<GrCurveMiddleOutShader>(
Chris Dalton70a0d2c2021-01-26 12:01:21 -070070 args, fViewMatrix, stencilPassPipeline, fPath.getFillType());
71 } else {
72 // The caller should have sent Flags::kDisableHWTessellation if it was not supported.
73 SkASSERT(args.fCaps->shaderCaps()->tessellationSupport());
74 // Next see if we can split up the inner triangles and outer curves into two draw calls.
75 // This allows for a more efficient inner fan topology that can reduce the rasterizer load
76 // by a large margin on complex paths, but also causes greater CPU overhead due to the extra
77 // shader switches and draw calls.
78 // NOTE: Raster-edge work is 1-dimensional, so we sum height and width instead of
79 // multiplying.
80 SkScalar scales[2];
81 SkAssertResult(fViewMatrix.getMinMaxScales(scales)); // Will fail if perspective.
82 const SkRect& bounds = fPath.getBounds();
83 float rasterEdgeWork = (bounds.height() + bounds.width()) * scales[1] * fPath.countVerbs();
84 if (rasterEdgeWork > 300 * 300) {
Chris Dalton2758a312021-05-20 10:46:25 -060085 fTessellator = args.fArena->make<GrPathOuterCurveTessellator>(
86 GrPathTessellator::DrawInnerFan::kNo);
Chris Dalton70a0d2c2021-01-26 12:01:21 -070087 fStencilFanProgram = GrStencilPathShader::MakeStencilProgram<GrStencilTriangleShader>(
88 args, fViewMatrix, stencilPassPipeline, fPath.getFillType());
Chris Daltona9f759d2021-05-18 12:37:08 -060089 fStencilPathProgram = GrStencilPathShader::MakeStencilProgram<GrCurveTessellateShader>(
Chris Dalton70a0d2c2021-01-26 12:01:21 -070090 args, fViewMatrix, stencilPassPipeline, fPath.getFillType());
91 } else {
92 // Fastest CPU approach: emit one cubic wedge per verb, fanning out from the center.
93 fTessellator = args.fArena->make<GrPathWedgeTessellator>();
94 fStencilPathProgram = GrStencilPathShader::MakeStencilProgram<GrWedgeTessellateShader>(
95 args, fViewMatrix, stencilPassPipeline, fPath.getFillType());
96 }
97 }
98
99 if (!(fOpFlags & OpFlags::kStencilOnly)) {
100 // Create a program that draws a bounding box over the path and fills its stencil coverage
101 // into the color buffer.
102 auto* bboxShader = args.fArena->make<GrFillBoundingBoxShader>(fViewMatrix, fColor,
103 fPath.getBounds());
104 auto* bboxPipeline = GrFillPathShader::MakeFillPassPipeline(args, fAAType,
105 std::move(appliedClip),
106 std::move(fProcessors));
107 auto* bboxStencil = GrFillPathShader::TestAndResetStencilSettings();
108 fFillBBoxProgram = GrPathShader::MakeProgram(args, bboxShader, bboxPipeline, bboxStencil);
109 }
110}
111
Chris Dalton2ed22fa2021-05-06 16:08:30 -0600112void GrPathStencilFillOp::onPrePrepare(GrRecordingContext* context,
113 const GrSurfaceProxyView& writeView, GrAppliedClip* clip,
114 const GrXferProcessor::DstProxyView& dstProxyView,
115 GrXferBarrierFlags renderPassXferBarriers,
116 GrLoadOp colorLoadOp) {
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700117 this->prePreparePrograms({context->priv().recordTimeAllocator(), writeView, &dstProxyView,
118 renderPassXferBarriers, colorLoadOp, context->priv().caps()},
119 (clip) ? std::move(*clip) : GrAppliedClip::Disabled());
120 if (fStencilFanProgram) {
121 context->priv().recordProgramInfo(fStencilFanProgram);
122 }
123 if (fStencilPathProgram) {
124 context->priv().recordProgramInfo(fStencilPathProgram);
125 }
126 if (fFillBBoxProgram) {
127 context->priv().recordProgramInfo(fFillBBoxProgram);
128 }
129}
130
Chris Dalton2ed22fa2021-05-06 16:08:30 -0600131void GrPathStencilFillOp::onPrepare(GrOpFlushState* flushState) {
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700132 if (!fTessellator) {
133 this->prePreparePrograms({flushState->allocator(), flushState->writeView(),
134 &flushState->dstProxyView(), flushState->renderPassBarriers(),
135 flushState->colorLoadOp(), &flushState->caps()},
136 flushState->detachAppliedClip());
137 if (!fTessellator) {
138 return;
139 }
140 }
141
142 if (fStencilFanProgram) {
143 // The inner fan isn't built into the tessellator. Generate a standard Redbook fan with a
144 // middle-out topology.
145 GrEagerDynamicVertexAllocator vertexAlloc(flushState, &fFanBuffer, &fFanBaseVertex);
146 int maxFanTriangles = fPath.countVerbs() - 2; // n - 2 triangles make an n-gon.
Chris Dalton8731a712021-05-14 14:48:54 -0600147 GrVertexWriter triangleVertexWriter = vertexAlloc.lock<SkPoint>(maxFanTriangles * 3);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700148 fFanVertexCount = GrMiddleOutPolygonTriangulator::WritePathInnerFan(
Chris Daltondf2dbad2021-05-14 16:21:15 -0600149 &triangleVertexWriter, GrMiddleOutPolygonTriangulator::OutputType::kTriangles,
150 fPath) * 3;
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700151 SkASSERT(fFanVertexCount <= maxFanTriangles * 3);
152 vertexAlloc.unlock(fFanVertexCount);
153 }
154
Chris Dalton8447f132021-05-21 15:54:23 -0600155 fTessellator->prepare(flushState, this->bounds(), fViewMatrix, fPath);
Chris Daltonc91dd692021-05-24 15:04:47 -0600156
157 if (fFillBBoxProgram) {
158 GrVertexWriter vertexWriter = flushState->makeVertexSpace(sizeof(SkRect), 1, &fBBoxBuffer,
159 &fBBoxBaseInstance);
160 vertexWriter.write(fPath.getBounds());
161 }
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700162}
163
Chris Dalton2ed22fa2021-05-06 16:08:30 -0600164void GrPathStencilFillOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700165 if (!fTessellator) {
166 return;
167 }
168
169 // Stencil the inner fan, if any.
170 if (fFanVertexCount > 0) {
171 SkASSERT(fStencilFanProgram);
172 SkASSERT(fFanBuffer);
173 flushState->bindPipelineAndScissorClip(*fStencilFanProgram, this->bounds());
174 flushState->bindBuffers(nullptr, nullptr, fFanBuffer);
175 flushState->draw(fFanVertexCount, fFanBaseVertex);
176 }
177
178 // Stencil the rest of the path.
179 SkASSERT(fStencilPathProgram);
180 flushState->bindPipelineAndScissorClip(*fStencilPathProgram, this->bounds());
181 fTessellator->draw(flushState);
182
183 // Fill in the bounding box (if not in stencil-only mode).
184 if (fFillBBoxProgram) {
185 flushState->bindPipelineAndScissorClip(*fFillBBoxProgram, this->bounds());
Robert Phillips787fd9d2021-03-22 14:48:09 -0400186 flushState->bindTextures(fFillBBoxProgram->geomProc(), nullptr,
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700187 fFillBBoxProgram->pipeline());
Chris Daltonc91dd692021-05-24 15:04:47 -0600188 flushState->bindBuffers(nullptr, fBBoxBuffer, nullptr);
189 flushState->drawInstanced(1, fBBoxBaseInstance, 4, 0);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700190 }
191}