blob: 823296d50e585a9c1e2d33910ff21f0d5f4cbf2a [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 Dalton031d76b2021-06-08 16:32:00 -06008#include "src/gpu/tessellate/GrPathStencilCoverOp.h"
Chris Dalton70a0d2c2021-01-26 12:01:21 -07009
10#include "src/gpu/GrEagerVertexAllocator.h"
Chris Daltond9bdc322021-06-01 19:22:05 -060011#include "src/gpu/GrGpu.h"
Chris Dalton70a0d2c2021-01-26 12:01:21 -070012#include "src/gpu/GrOpFlushState.h"
13#include "src/gpu/GrRecordingContextPriv.h"
Chris Dalton2f733ec2021-06-01 12:11:57 -060014#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
Chris Dalton70a0d2c2021-01-26 12:01:21 -070015#include "src/gpu/tessellate/GrMiddleOutPolygonTriangulator.h"
Chris Daltond9bdc322021-06-01 19:22:05 -060016#include "src/gpu/tessellate/GrPathCurveTessellator.h"
Chris Daltond9bdc322021-06-01 19:22:05 -060017#include "src/gpu/tessellate/GrPathWedgeTessellator.h"
Chris Dalton70a0d2c2021-01-26 12:01:21 -070018#include "src/gpu/tessellate/GrTessellationPathRenderer.h"
Chris Dalton3b412782021-06-01 13:40:03 -060019#include "src/gpu/tessellate/shaders/GrPathTessellationShader.h"
Chris Dalton70a0d2c2021-01-26 12:01:21 -070020
Chris Dalton917f9192021-06-08 14:32:37 -060021using PathFlags = GrTessellationPathRenderer::PathFlags;
Chris Dalton70a0d2c2021-01-26 12:01:21 -070022
Chris Dalton2f733ec2021-06-01 12:11:57 -060023namespace {
24
25// Fills a path's bounding box, with subpixel outset to avoid possible T-junctions with extreme
26// edges of the path.
27// NOTE: The emitted geometry may not be axis-aligned, depending on the view matrix.
28class BoundingBoxShader : public GrPathTessellationShader {
29public:
30 BoundingBoxShader(const SkMatrix& viewMatrix, SkPMColor4f color)
31 : GrPathTessellationShader(kTessellate_BoundingBoxShader_ClassID,
32 GrPrimitiveType::kTriangleStrip, 0, viewMatrix, color) {
33 constexpr static Attribute kPathBoundsAttrib = {"pathBounds", kFloat4_GrVertexAttribType,
34 kFloat4_GrSLType};
35 this->setInstanceAttributes(&kPathBoundsAttrib, 1);
36 }
37
38private:
Chris Daltonb63711a2021-06-01 14:52:02 -060039 const char* name() const final { return "tessellate_BoundingBoxShader"; }
Chris Dalton2f733ec2021-06-01 12:11:57 -060040 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const final {}
41 GrGLSLGeometryProcessor* createGLSLInstance(const GrShaderCaps&) const final;
42};
43
44GrGLSLGeometryProcessor* BoundingBoxShader::createGLSLInstance(const GrShaderCaps&) const {
45 class Impl : public GrPathTessellationShader::Impl {
Chris Daltond2b8ba32021-06-09 00:12:59 -060046 void emitVertexCode(const GrPathTessellationShader&, GrGLSLVertexBuilder* v,
47 GrGPArgs* gpArgs) override {
Chris Dalton2f733ec2021-06-01 12:11:57 -060048 v->codeAppend(R"(
49 // Bloat the bounding box by 1/4px to avoid potential T-junctions at the edges.
50 float2x2 M_ = inverse(AFFINE_MATRIX);
51 float2 bloat = float2(abs(M_[0]) + abs(M_[1])) * .25;
52
53 // Find the vertex position.
54 float2 T = float2(sk_VertexID & 1, sk_VertexID >> 1);
55 float2 localcoord = mix(pathBounds.xy - bloat, pathBounds.zw + bloat, T);
56 float2 vertexpos = AFFINE_MATRIX * localcoord + TRANSLATE;)");
57 gpArgs->fLocalCoordVar.set(kFloat2_GrSLType, "localcoord");
58 gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertexpos");
59 }
60 };
61 return new Impl;
62}
63
64} // namespace
65
Robert Phillips294723d2021-06-17 09:23:58 -040066void GrPathStencilCoverOp::visitProxies(const GrVisitProxyFunc& func) const {
Chris Dalton031d76b2021-06-08 16:32:00 -060067 if (fCoverBBoxProgram) {
Robert Phillips294723d2021-06-17 09:23:58 -040068 fCoverBBoxProgram->pipeline().visitProxies(func);
Chris Dalton70a0d2c2021-01-26 12:01:21 -070069 } else {
Robert Phillips294723d2021-06-17 09:23:58 -040070 fProcessors.visitProxies(func);
Chris Dalton70a0d2c2021-01-26 12:01:21 -070071 }
72}
73
Chris Dalton031d76b2021-06-08 16:32:00 -060074GrDrawOp::FixedFunctionFlags GrPathStencilCoverOp::fixedFunctionFlags() const {
Chris Dalton70a0d2c2021-01-26 12:01:21 -070075 auto flags = FixedFunctionFlags::kUsesStencil;
76 if (fAAType != GrAAType::kNone) {
77 flags |= FixedFunctionFlags::kUsesHWAA;
78 }
79 return flags;
80}
81
Chris Dalton031d76b2021-06-08 16:32:00 -060082GrProcessorSet::Analysis GrPathStencilCoverOp::finalize(const GrCaps& caps,
83 const GrAppliedClip* clip,
84 GrClampType clampType) {
Chris Dalton57ab06c2021-04-22 12:57:28 -060085 return fProcessors.finalize(fColor, GrProcessorAnalysisCoverage::kNone, clip, nullptr, caps,
86 clampType, &fColor);
Chris Dalton70a0d2c2021-01-26 12:01:21 -070087}
88
Chris Dalton031d76b2021-06-08 16:32:00 -060089void GrPathStencilCoverOp::prePreparePrograms(const GrTessellationShader::ProgramArgs& args,
90 GrAppliedClip&& appliedClip) {
Chris Dalton569c01b2021-05-25 10:11:46 -060091 SkASSERT(!fTessellator);
Chris Dalton70a0d2c2021-01-26 12:01:21 -070092 SkASSERT(!fStencilFanProgram);
93 SkASSERT(!fStencilPathProgram);
Chris Dalton031d76b2021-06-08 16:32:00 -060094 SkASSERT(!fCoverBBoxProgram);
Chris Dalton70a0d2c2021-01-26 12:01:21 -070095
Chris Dalton569c01b2021-05-25 10:11:46 -060096 if (fPath.countVerbs() <= 0) {
Chris Dalton70a0d2c2021-01-26 12:01:21 -070097 return;
98 }
99
Chris Dalton2f733ec2021-06-01 12:11:57 -0600100 const GrPipeline* stencilPipeline = GrPathTessellationShader::MakeStencilOnlyPipeline(
Chris Dalton917f9192021-06-08 14:32:37 -0600101 args, fAAType, fPathFlags, appliedClip.hardClip());
Chris Dalton2f733ec2021-06-01 12:11:57 -0600102 const GrUserStencilSettings* stencilPathSettings =
103 GrPathTessellationShader::StencilPathSettings(fPath.getFillType());
Chris Dalton569c01b2021-05-25 10:11:46 -0600104
Chris Dalton917f9192021-06-08 14:32:37 -0600105 if (fPath.countVerbs() > 50 && this->bounds().height() * this->bounds().width() > 256 * 256) {
Chris Daltond2b8ba32021-06-09 00:12:59 -0600106 // Large complex paths do better with a dedicated triangle shader for the inner fan.
107 // This takes less PCI bus bandwidth (6 floats per triangle instead of 8) and allows us
108 // to make sure it has an efficient middle-out topology.
Chris Dalton917f9192021-06-08 14:32:37 -0600109 auto shader = GrPathTessellationShader::MakeSimpleTriangleShader(
110 args.fArena, fViewMatrix, SK_PMColor4fTRANSPARENT);
111 fStencilFanProgram = GrTessellationShader::MakeProgram(args, shader, stencilPipeline,
112 stencilPathSettings);
Chris Daltond2b8ba32021-06-09 00:12:59 -0600113 fTessellator = GrPathCurveTessellator::Make(args.fArena, fViewMatrix,
114 SK_PMColor4fTRANSPARENT,
115 GrPathCurveTessellator::DrawInnerFan::kNo,
Chris Dalton198ac152021-06-09 13:49:43 -0600116 fPath.countVerbs(), *stencilPipeline,
117 *args.fCaps);
Chris Dalton917f9192021-06-08 14:32:37 -0600118 } else {
Chris Dalton2f733ec2021-06-01 12:11:57 -0600119 fTessellator = GrPathWedgeTessellator::Make(args.fArena, fViewMatrix,
Chris Daltond2b8ba32021-06-09 00:12:59 -0600120 SK_PMColor4fTRANSPARENT, fPath.countVerbs(),
Chris Dalton198ac152021-06-09 13:49:43 -0600121 *stencilPipeline, *args.fCaps);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700122 }
Chris Dalton2f733ec2021-06-01 12:11:57 -0600123 fStencilPathProgram = GrTessellationShader::MakeProgram(args, fTessellator->shader(),
124 stencilPipeline, stencilPathSettings);
Chris Dalton569c01b2021-05-25 10:11:46 -0600125
Chris Dalton917f9192021-06-08 14:32:37 -0600126 if (!(fPathFlags & PathFlags::kStencilOnly)) {
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700127 // Create a program that draws a bounding box over the path and fills its stencil coverage
128 // into the color buffer.
Chris Dalton2f733ec2021-06-01 12:11:57 -0600129 auto* bboxShader = args.fArena->make<BoundingBoxShader>(fViewMatrix, fColor);
130 auto* bboxPipeline = GrTessellationShader::MakePipeline(args, fAAType,
131 std::move(appliedClip),
132 std::move(fProcessors));
133 auto* bboxStencil = GrPathTessellationShader::TestAndResetStencilSettings();
Chris Dalton031d76b2021-06-08 16:32:00 -0600134 fCoverBBoxProgram = GrTessellationShader::MakeProgram(args, bboxShader, bboxPipeline,
135 bboxStencil);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700136 }
137}
138
Chris Dalton031d76b2021-06-08 16:32:00 -0600139void GrPathStencilCoverOp::onPrePrepare(GrRecordingContext* context,
140 const GrSurfaceProxyView& writeView, GrAppliedClip* clip,
141 const GrDstProxyView& dstProxyView,
142 GrXferBarrierFlags renderPassXferBarriers,
143 GrLoadOp colorLoadOp) {
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700144 this->prePreparePrograms({context->priv().recordTimeAllocator(), writeView, &dstProxyView,
145 renderPassXferBarriers, colorLoadOp, context->priv().caps()},
146 (clip) ? std::move(*clip) : GrAppliedClip::Disabled());
147 if (fStencilFanProgram) {
148 context->priv().recordProgramInfo(fStencilFanProgram);
149 }
150 if (fStencilPathProgram) {
151 context->priv().recordProgramInfo(fStencilPathProgram);
152 }
Chris Dalton031d76b2021-06-08 16:32:00 -0600153 if (fCoverBBoxProgram) {
154 context->priv().recordProgramInfo(fCoverBBoxProgram);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700155 }
156}
157
Chris Dalton031d76b2021-06-08 16:32:00 -0600158void GrPathStencilCoverOp::onPrepare(GrOpFlushState* flushState) {
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700159 if (!fTessellator) {
160 this->prePreparePrograms({flushState->allocator(), flushState->writeView(),
161 &flushState->dstProxyView(), flushState->renderPassBarriers(),
162 flushState->colorLoadOp(), &flushState->caps()},
163 flushState->detachAppliedClip());
164 if (!fTessellator) {
165 return;
166 }
167 }
168
169 if (fStencilFanProgram) {
170 // The inner fan isn't built into the tessellator. Generate a standard Redbook fan with a
171 // middle-out topology.
172 GrEagerDynamicVertexAllocator vertexAlloc(flushState, &fFanBuffer, &fFanBaseVertex);
173 int maxFanTriangles = fPath.countVerbs() - 2; // n - 2 triangles make an n-gon.
Chris Dalton8731a712021-05-14 14:48:54 -0600174 GrVertexWriter triangleVertexWriter = vertexAlloc.lock<SkPoint>(maxFanTriangles * 3);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700175 fFanVertexCount = GrMiddleOutPolygonTriangulator::WritePathInnerFan(
Chris Daltondf2dbad2021-05-14 16:21:15 -0600176 &triangleVertexWriter, GrMiddleOutPolygonTriangulator::OutputType::kTriangles,
177 fPath) * 3;
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700178 SkASSERT(fFanVertexCount <= maxFanTriangles * 3);
179 vertexAlloc.unlock(fFanVertexCount);
180 }
181
Chris Dalton569c01b2021-05-25 10:11:46 -0600182 fTessellator->prepare(flushState, this->bounds(), fPath);
Chris Daltonc91dd692021-05-24 15:04:47 -0600183
Chris Dalton031d76b2021-06-08 16:32:00 -0600184 if (fCoverBBoxProgram) {
Chris Daltonc91dd692021-05-24 15:04:47 -0600185 GrVertexWriter vertexWriter = flushState->makeVertexSpace(sizeof(SkRect), 1, &fBBoxBuffer,
186 &fBBoxBaseInstance);
187 vertexWriter.write(fPath.getBounds());
188 }
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700189}
190
Chris Dalton031d76b2021-06-08 16:32:00 -0600191void GrPathStencilCoverOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700192 if (!fTessellator) {
193 return;
194 }
195
196 // Stencil the inner fan, if any.
197 if (fFanVertexCount > 0) {
198 SkASSERT(fStencilFanProgram);
199 SkASSERT(fFanBuffer);
200 flushState->bindPipelineAndScissorClip(*fStencilFanProgram, this->bounds());
201 flushState->bindBuffers(nullptr, nullptr, fFanBuffer);
202 flushState->draw(fFanVertexCount, fFanBaseVertex);
203 }
204
205 // Stencil the rest of the path.
206 SkASSERT(fStencilPathProgram);
207 flushState->bindPipelineAndScissorClip(*fStencilPathProgram, this->bounds());
208 fTessellator->draw(flushState);
Chris Daltond9bdc322021-06-01 19:22:05 -0600209 if (flushState->caps().requiresManualFBBarrierAfterTessellatedStencilDraw()) {
210 flushState->gpu()->insertManualFramebufferBarrier(); // http://skbug.com/9739
211 }
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700212
213 // Fill in the bounding box (if not in stencil-only mode).
Chris Dalton031d76b2021-06-08 16:32:00 -0600214 if (fCoverBBoxProgram) {
215 flushState->bindPipelineAndScissorClip(*fCoverBBoxProgram, this->bounds());
216 flushState->bindTextures(fCoverBBoxProgram->geomProc(), nullptr,
217 fCoverBBoxProgram->pipeline());
Chris Daltonc91dd692021-05-24 15:04:47 -0600218 flushState->bindBuffers(nullptr, fBBoxBuffer, nullptr);
219 flushState->drawInstanced(1, fBBoxBaseInstance, 4, 0);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700220 }
221}