blob: 2b56078805fcaf133a3484d2c99674641de91e97 [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"
Robert Phillips1a82a4e2021-07-01 10:27:44 -040014#include "src/gpu/GrResourceProvider.h"
Chris Dalton8aec1242021-07-19 11:10:45 -060015#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
16#include "src/gpu/glsl/GrGLSLVarying.h"
Chris Dalton2f733ec2021-06-01 12:11:57 -060017#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
Chris Dalton70a0d2c2021-01-26 12:01:21 -070018#include "src/gpu/tessellate/GrMiddleOutPolygonTriangulator.h"
Chris Daltond9bdc322021-06-01 19:22:05 -060019#include "src/gpu/tessellate/GrPathCurveTessellator.h"
Chris Daltond9bdc322021-06-01 19:22:05 -060020#include "src/gpu/tessellate/GrPathWedgeTessellator.h"
Chris Dalton70a0d2c2021-01-26 12:01:21 -070021#include "src/gpu/tessellate/GrTessellationPathRenderer.h"
Chris Dalton3b412782021-06-01 13:40:03 -060022#include "src/gpu/tessellate/shaders/GrPathTessellationShader.h"
Chris Dalton70a0d2c2021-01-26 12:01:21 -070023
Chris Dalton917f9192021-06-08 14:32:37 -060024using PathFlags = GrTessellationPathRenderer::PathFlags;
Chris Dalton70a0d2c2021-01-26 12:01:21 -070025
Chris Dalton2f733ec2021-06-01 12:11:57 -060026namespace {
27
28// Fills a path's bounding box, with subpixel outset to avoid possible T-junctions with extreme
29// edges of the path.
30// NOTE: The emitted geometry may not be axis-aligned, depending on the view matrix.
Chris Dalton8aec1242021-07-19 11:10:45 -060031class BoundingBoxShader : public GrGeometryProcessor {
Chris Dalton2f733ec2021-06-01 12:11:57 -060032public:
Chris Daltona05ccc32021-06-29 19:42:13 -060033 BoundingBoxShader(const SkMatrix& viewMatrix, SkPMColor4f color, const GrShaderCaps& shaderCaps)
Chris Dalton8aec1242021-07-19 11:10:45 -060034 : GrGeometryProcessor(kTessellate_BoundingBoxShader_ClassID)
35 , fViewMatrix(viewMatrix)
36 , fColor(color) {
37 // The 1/4px outset logic does not work with perspective yet.
38 SkASSERT(!fViewMatrix.hasPerspective());
Chris Daltona05ccc32021-06-29 19:42:13 -060039 if (!shaderCaps.vertexIDSupport()) {
40 constexpr static Attribute kUnitCoordAttrib("unitCoord", kFloat2_GrVertexAttribType,
41 kFloat2_GrSLType);
42 this->setVertexAttributes(&kUnitCoordAttrib, 1);
43 }
Chris Dalton8aec1242021-07-19 11:10:45 -060044 constexpr static Attribute kPathBoundsAttrib("pathBounds", kFloat4_GrVertexAttribType,
45 kFloat4_GrSLType);
46 this->setInstanceAttributes(&kPathBoundsAttrib, 1);
Chris Dalton2f733ec2021-06-01 12:11:57 -060047 }
48
49private:
Chris Daltonb63711a2021-06-01 14:52:02 -060050 const char* name() const final { return "tessellate_BoundingBoxShader"; }
Chris Dalton2f733ec2021-06-01 12:11:57 -060051 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const final {}
52 GrGLSLGeometryProcessor* createGLSLInstance(const GrShaderCaps&) const final;
Chris Dalton8aec1242021-07-19 11:10:45 -060053
54 const SkMatrix fViewMatrix;
55 const SkPMColor4f fColor;
Chris Dalton2f733ec2021-06-01 12:11:57 -060056};
57
58GrGLSLGeometryProcessor* BoundingBoxShader::createGLSLInstance(const GrShaderCaps&) const {
Chris Dalton8aec1242021-07-19 11:10:45 -060059 class Impl : public GrGLSLGeometryProcessor {
60 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final {
61 args.fVaryingHandler->emitAttributes(args.fGeomProc);
62
63 // Vertex shader.
64 const char* viewMatrix;
65 fViewMatrixUniform = args.fUniformHandler->addUniform(nullptr, kVertex_GrShaderFlag,
66 kFloat3x3_GrSLType, "viewMatrix",
67 &viewMatrix);
68 if (args.fShaderCaps->vertexIDSupport()) {
Chris Daltona05ccc32021-06-29 19:42:13 -060069 // If we don't have sk_VertexID support then "unitCoord" already came in as a vertex
70 // attrib.
Chris Dalton8aec1242021-07-19 11:10:45 -060071 args.fVertBuilder->codeAppendf(R"(
Chris Daltona05ccc32021-06-29 19:42:13 -060072 float2 unitCoord = float2(sk_VertexID & 1, sk_VertexID >> 1);)");
73 }
Chris Dalton8aec1242021-07-19 11:10:45 -060074 args.fVertBuilder->codeAppendf(R"(
75 float3x3 VIEW_MATRIX = %s;
Chris Daltona05ccc32021-06-29 19:42:13 -060076
Chris Dalton8aec1242021-07-19 11:10:45 -060077 // Bloat the bounding box by 1/4px to be certain we will reset every stencil value.
78 float2x2 M_ = inverse(float2x2(VIEW_MATRIX));
Chris Dalton2f733ec2021-06-01 12:11:57 -060079 float2 bloat = float2(abs(M_[0]) + abs(M_[1])) * .25;
80
81 // Find the vertex position.
Chris Daltona05ccc32021-06-29 19:42:13 -060082 float2 localcoord = mix(pathBounds.xy - bloat, pathBounds.zw + bloat, unitCoord);
Chris Dalton8aec1242021-07-19 11:10:45 -060083 float2 vertexpos = (VIEW_MATRIX * float3(localcoord, 1)).xy;)", viewMatrix);
Chris Dalton2f733ec2021-06-01 12:11:57 -060084 gpArgs->fLocalCoordVar.set(kFloat2_GrSLType, "localcoord");
85 gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertexpos");
Chris Dalton8aec1242021-07-19 11:10:45 -060086
87 // Fragment shader.
88 const char* color;
89 fColorUniform = args.fUniformHandler->addUniform(nullptr, kFragment_GrShaderFlag,
90 kHalf4_GrSLType, "color", &color);
91 args.fFragBuilder->codeAppendf("half4 %s = %s;", args.fOutputColor, color);
92 args.fFragBuilder->codeAppendf("const half4 %s = half4(1);", args.fOutputCoverage);
Chris Dalton2f733ec2021-06-01 12:11:57 -060093 }
Chris Dalton8aec1242021-07-19 11:10:45 -060094
95 void setData(const GrGLSLProgramDataManager& pdman, const GrShaderCaps&,
96 const GrGeometryProcessor& gp) override {
97 const auto& bboxShader = gp.cast<BoundingBoxShader>();
98 pdman.setSkMatrix(fViewMatrixUniform, bboxShader.fViewMatrix);
99 const SkPMColor4f& color = bboxShader.fColor;
100 pdman.set4f(fColorUniform, color.fR, color.fG, color.fB, color.fA);
101 }
102
103 GrGLSLUniformHandler::UniformHandle fViewMatrixUniform;
104 GrGLSLUniformHandler::UniformHandle fColorUniform;
Chris Dalton2f733ec2021-06-01 12:11:57 -0600105 };
Chris Dalton8aec1242021-07-19 11:10:45 -0600106
Chris Dalton2f733ec2021-06-01 12:11:57 -0600107 return new Impl;
108}
109
110} // namespace
111
Robert Phillips294723d2021-06-17 09:23:58 -0400112void GrPathStencilCoverOp::visitProxies(const GrVisitProxyFunc& func) const {
Chris Dalton031d76b2021-06-08 16:32:00 -0600113 if (fCoverBBoxProgram) {
Robert Phillips294723d2021-06-17 09:23:58 -0400114 fCoverBBoxProgram->pipeline().visitProxies(func);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700115 } else {
Robert Phillips294723d2021-06-17 09:23:58 -0400116 fProcessors.visitProxies(func);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700117 }
118}
119
Chris Dalton031d76b2021-06-08 16:32:00 -0600120GrDrawOp::FixedFunctionFlags GrPathStencilCoverOp::fixedFunctionFlags() const {
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700121 auto flags = FixedFunctionFlags::kUsesStencil;
122 if (fAAType != GrAAType::kNone) {
123 flags |= FixedFunctionFlags::kUsesHWAA;
124 }
125 return flags;
126}
127
Chris Dalton031d76b2021-06-08 16:32:00 -0600128GrProcessorSet::Analysis GrPathStencilCoverOp::finalize(const GrCaps& caps,
129 const GrAppliedClip* clip,
130 GrClampType clampType) {
Chris Dalton57ab06c2021-04-22 12:57:28 -0600131 return fProcessors.finalize(fColor, GrProcessorAnalysisCoverage::kNone, clip, nullptr, caps,
132 clampType, &fColor);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700133}
134
Chris Dalton031d76b2021-06-08 16:32:00 -0600135void GrPathStencilCoverOp::prePreparePrograms(const GrTessellationShader::ProgramArgs& args,
136 GrAppliedClip&& appliedClip) {
Chris Dalton569c01b2021-05-25 10:11:46 -0600137 SkASSERT(!fTessellator);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700138 SkASSERT(!fStencilFanProgram);
139 SkASSERT(!fStencilPathProgram);
Chris Dalton031d76b2021-06-08 16:32:00 -0600140 SkASSERT(!fCoverBBoxProgram);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700141
Chris Dalton2f733ec2021-06-01 12:11:57 -0600142 const GrPipeline* stencilPipeline = GrPathTessellationShader::MakeStencilOnlyPipeline(
Chris Dalton917f9192021-06-08 14:32:37 -0600143 args, fAAType, fPathFlags, appliedClip.hardClip());
Chris Dalton2f733ec2021-06-01 12:11:57 -0600144 const GrUserStencilSettings* stencilPathSettings =
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600145 GrPathTessellationShader::StencilPathSettings(GrFillRuleForSkPath(fPath));
Chris Dalton569c01b2021-05-25 10:11:46 -0600146
Chris Dalton917f9192021-06-08 14:32:37 -0600147 if (fPath.countVerbs() > 50 && this->bounds().height() * this->bounds().width() > 256 * 256) {
Chris Daltond2b8ba32021-06-09 00:12:59 -0600148 // Large complex paths do better with a dedicated triangle shader for the inner fan.
149 // This takes less PCI bus bandwidth (6 floats per triangle instead of 8) and allows us
150 // to make sure it has an efficient middle-out topology.
Chris Daltona8c4de92021-07-26 21:46:28 +0000151 auto shader = GrPathTessellationShader::MakeSimpleTriangleShader(
152 args.fArena, fViewMatrix, SK_PMColor4fTRANSPARENT);
153 fStencilFanProgram = GrTessellationShader::MakeProgram(args, shader, stencilPipeline,
154 stencilPathSettings);
Chris Daltond2b8ba32021-06-09 00:12:59 -0600155 fTessellator = GrPathCurveTessellator::Make(args.fArena, fViewMatrix,
156 SK_PMColor4fTRANSPARENT,
157 GrPathCurveTessellator::DrawInnerFan::kNo,
Chris Dalton198ac152021-06-09 13:49:43 -0600158 fPath.countVerbs(), *stencilPipeline,
159 *args.fCaps);
Chris Dalton917f9192021-06-08 14:32:37 -0600160 } else {
Chris Dalton2f733ec2021-06-01 12:11:57 -0600161 fTessellator = GrPathWedgeTessellator::Make(args.fArena, fViewMatrix,
Chris Daltond2b8ba32021-06-09 00:12:59 -0600162 SK_PMColor4fTRANSPARENT, fPath.countVerbs(),
Chris Dalton198ac152021-06-09 13:49:43 -0600163 *stencilPipeline, *args.fCaps);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700164 }
Chris Dalton2f733ec2021-06-01 12:11:57 -0600165 fStencilPathProgram = GrTessellationShader::MakeProgram(args, fTessellator->shader(),
166 stencilPipeline, stencilPathSettings);
Chris Dalton569c01b2021-05-25 10:11:46 -0600167
Chris Dalton917f9192021-06-08 14:32:37 -0600168 if (!(fPathFlags & PathFlags::kStencilOnly)) {
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700169 // Create a program that draws a bounding box over the path and fills its stencil coverage
170 // into the color buffer.
Chris Daltona05ccc32021-06-29 19:42:13 -0600171 auto* bboxShader = args.fArena->make<BoundingBoxShader>(fViewMatrix, fColor,
172 *args.fCaps->shaderCaps());
Chris Dalton2f733ec2021-06-01 12:11:57 -0600173 auto* bboxPipeline = GrTessellationShader::MakePipeline(args, fAAType,
174 std::move(appliedClip),
175 std::move(fProcessors));
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600176 auto* bboxStencil =
177 GrPathTessellationShader::TestAndResetStencilSettings(fPath.isInverseFillType());
Chris Dalton8aec1242021-07-19 11:10:45 -0600178 fCoverBBoxProgram = GrSimpleMeshDrawOpHelper::CreateProgramInfo(
179 args.fArena,
180 bboxPipeline,
181 args.fWriteView,
182 bboxShader,
183 GrPrimitiveType::kTriangleStrip,
184 args.fXferBarrierFlags,
185 args.fColorLoadOp,
186 bboxStencil);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700187 }
188}
189
Chris Dalton031d76b2021-06-08 16:32:00 -0600190void GrPathStencilCoverOp::onPrePrepare(GrRecordingContext* context,
191 const GrSurfaceProxyView& writeView, GrAppliedClip* clip,
192 const GrDstProxyView& dstProxyView,
193 GrXferBarrierFlags renderPassXferBarriers,
194 GrLoadOp colorLoadOp) {
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700195 this->prePreparePrograms({context->priv().recordTimeAllocator(), writeView, &dstProxyView,
196 renderPassXferBarriers, colorLoadOp, context->priv().caps()},
197 (clip) ? std::move(*clip) : GrAppliedClip::Disabled());
198 if (fStencilFanProgram) {
199 context->priv().recordProgramInfo(fStencilFanProgram);
200 }
201 if (fStencilPathProgram) {
202 context->priv().recordProgramInfo(fStencilPathProgram);
203 }
Chris Dalton031d76b2021-06-08 16:32:00 -0600204 if (fCoverBBoxProgram) {
205 context->priv().recordProgramInfo(fCoverBBoxProgram);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700206 }
207}
208
Chris Daltona05ccc32021-06-29 19:42:13 -0600209GR_DECLARE_STATIC_UNIQUE_KEY(gUnitQuadBufferKey);
210
Chris Dalton031d76b2021-06-08 16:32:00 -0600211void GrPathStencilCoverOp::onPrepare(GrOpFlushState* flushState) {
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700212 if (!fTessellator) {
213 this->prePreparePrograms({flushState->allocator(), flushState->writeView(),
214 &flushState->dstProxyView(), flushState->renderPassBarriers(),
215 flushState->colorLoadOp(), &flushState->caps()},
216 flushState->detachAppliedClip());
217 if (!fTessellator) {
218 return;
219 }
220 }
221
222 if (fStencilFanProgram) {
223 // The inner fan isn't built into the tessellator. Generate a standard Redbook fan with a
224 // middle-out topology.
225 GrEagerDynamicVertexAllocator vertexAlloc(flushState, &fFanBuffer, &fFanBaseVertex);
226 int maxFanTriangles = fPath.countVerbs() - 2; // n - 2 triangles make an n-gon.
Chris Dalton8731a712021-05-14 14:48:54 -0600227 GrVertexWriter triangleVertexWriter = vertexAlloc.lock<SkPoint>(maxFanTriangles * 3);
Chris Dalton40c906f2021-07-26 11:27:05 -0600228 int numTrianglesWritten;
229 GrMiddleOutPolygonTriangulator::WritePathInnerFan(std::move(triangleVertexWriter), 0, 0,
230 fPath, &numTrianglesWritten);
231 fFanVertexCount = 3 * numTrianglesWritten;
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700232 SkASSERT(fFanVertexCount <= maxFanTriangles * 3);
233 vertexAlloc.unlock(fFanVertexCount);
234 }
235
Chris Dalton569c01b2021-05-25 10:11:46 -0600236 fTessellator->prepare(flushState, this->bounds(), fPath);
Chris Daltonc91dd692021-05-24 15:04:47 -0600237
Chris Dalton031d76b2021-06-08 16:32:00 -0600238 if (fCoverBBoxProgram) {
Chris Daltonc91dd692021-05-24 15:04:47 -0600239 GrVertexWriter vertexWriter = flushState->makeVertexSpace(sizeof(SkRect), 1, &fBBoxBuffer,
240 &fBBoxBaseInstance);
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600241 if (fPath.isInverseFillType()) {
242 // Fill the entire backing store to make sure we clear every stencil value back to 0. If
243 // there is a scissor it will have already clipped the stencil draw.
244 auto rtBounds = flushState->writeView().asRenderTargetProxy()->backingStoreBoundsRect();
245 SkASSERT(rtBounds == fOriginalDrawBounds);
246 SkRect pathSpaceRTBounds;
247 if (SkMatrixPriv::InverseMapRect(fViewMatrix, &pathSpaceRTBounds, rtBounds)) {
248 vertexWriter.write(pathSpaceRTBounds);
249 } else {
250 vertexWriter.write(fPath.getBounds());
251 }
252 } else {
253 vertexWriter.write(fPath.getBounds());
254 }
Chris Daltonc91dd692021-05-24 15:04:47 -0600255 }
Chris Daltona05ccc32021-06-29 19:42:13 -0600256
257 if (!flushState->caps().shaderCaps()->vertexIDSupport()) {
258 constexpr static SkPoint kUnitQuad[4] = {{0,0}, {0,1}, {1,0}, {1,1}};
259
260 GR_DEFINE_STATIC_UNIQUE_KEY(gUnitQuadBufferKey);
261
262 fBBoxVertexBufferIfNoIDSupport = flushState->resourceProvider()->findOrMakeStaticBuffer(
263 GrGpuBufferType::kVertex, sizeof(kUnitQuad), kUnitQuad, gUnitQuadBufferKey);
264 }
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700265}
266
Chris Dalton031d76b2021-06-08 16:32:00 -0600267void GrPathStencilCoverOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700268 if (!fTessellator) {
269 return;
270 }
271
272 // Stencil the inner fan, if any.
273 if (fFanVertexCount > 0) {
274 SkASSERT(fStencilFanProgram);
275 SkASSERT(fFanBuffer);
276 flushState->bindPipelineAndScissorClip(*fStencilFanProgram, this->bounds());
277 flushState->bindBuffers(nullptr, nullptr, fFanBuffer);
278 flushState->draw(fFanVertexCount, fFanBaseVertex);
279 }
280
281 // Stencil the rest of the path.
282 SkASSERT(fStencilPathProgram);
283 flushState->bindPipelineAndScissorClip(*fStencilPathProgram, this->bounds());
284 fTessellator->draw(flushState);
Chris Daltond9bdc322021-06-01 19:22:05 -0600285 if (flushState->caps().requiresManualFBBarrierAfterTessellatedStencilDraw()) {
286 flushState->gpu()->insertManualFramebufferBarrier(); // http://skbug.com/9739
287 }
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700288
289 // Fill in the bounding box (if not in stencil-only mode).
Chris Dalton031d76b2021-06-08 16:32:00 -0600290 if (fCoverBBoxProgram) {
291 flushState->bindPipelineAndScissorClip(*fCoverBBoxProgram, this->bounds());
292 flushState->bindTextures(fCoverBBoxProgram->geomProc(), nullptr,
293 fCoverBBoxProgram->pipeline());
Chris Daltona05ccc32021-06-29 19:42:13 -0600294 flushState->bindBuffers(nullptr, fBBoxBuffer, fBBoxVertexBufferIfNoIDSupport);
Chris Daltonc91dd692021-05-24 15:04:47 -0600295 flushState->drawInstanced(1, fBBoxBaseInstance, 4, 0);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700296 }
297}