blob: a4d662130ab1e7e19b379ff6687e70524826e73c [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 Dalton69669812021-07-27 10:00:12 -0600142 // We transform paths on the CPU. This allows for better batching.
143 const SkMatrix& shaderMatrix = SkMatrix::I();
Chris Dalton2f733ec2021-06-01 12:11:57 -0600144 const GrPipeline* stencilPipeline = GrPathTessellationShader::MakeStencilOnlyPipeline(
Chris Dalton917f9192021-06-08 14:32:37 -0600145 args, fAAType, fPathFlags, appliedClip.hardClip());
Chris Dalton2f733ec2021-06-01 12:11:57 -0600146 const GrUserStencilSettings* stencilPathSettings =
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600147 GrPathTessellationShader::StencilPathSettings(GrFillRuleForSkPath(fPath));
Chris Dalton569c01b2021-05-25 10:11:46 -0600148
Chris Dalton917f9192021-06-08 14:32:37 -0600149 if (fPath.countVerbs() > 50 && this->bounds().height() * this->bounds().width() > 256 * 256) {
Chris Daltond2b8ba32021-06-09 00:12:59 -0600150 // Large complex paths do better with a dedicated triangle shader for the inner fan.
151 // This takes less PCI bus bandwidth (6 floats per triangle instead of 8) and allows us
152 // to make sure it has an efficient middle-out topology.
Chris Dalton69669812021-07-27 10:00:12 -0600153 auto shader = GrPathTessellationShader::MakeSimpleTriangleShader(args.fArena,
154 shaderMatrix,
155 SK_PMColor4fTRANSPARENT);
156 fStencilFanProgram = GrTessellationShader::MakeProgram(args,
157 shader,
158 stencilPipeline,
Chris Daltona8c4de92021-07-26 21:46:28 +0000159 stencilPathSettings);
Chris Dalton69669812021-07-27 10:00:12 -0600160 fTessellator = GrPathCurveTessellator::Make(args.fArena,
161 shaderMatrix,
Chris Daltond2b8ba32021-06-09 00:12:59 -0600162 SK_PMColor4fTRANSPARENT,
163 GrPathCurveTessellator::DrawInnerFan::kNo,
Chris Dalton69669812021-07-27 10:00:12 -0600164 fPath.countVerbs(),
165 *stencilPipeline,
Chris Dalton198ac152021-06-09 13:49:43 -0600166 *args.fCaps);
Chris Dalton917f9192021-06-08 14:32:37 -0600167 } else {
Chris Dalton69669812021-07-27 10:00:12 -0600168 fTessellator = GrPathWedgeTessellator::Make(args.fArena,
169 shaderMatrix,
170 SK_PMColor4fTRANSPARENT,
171 fPath.countVerbs(),
172 *stencilPipeline,
173 *args.fCaps);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700174 }
Chris Dalton2f733ec2021-06-01 12:11:57 -0600175 fStencilPathProgram = GrTessellationShader::MakeProgram(args, fTessellator->shader(),
176 stencilPipeline, stencilPathSettings);
Chris Dalton569c01b2021-05-25 10:11:46 -0600177
Chris Dalton917f9192021-06-08 14:32:37 -0600178 if (!(fPathFlags & PathFlags::kStencilOnly)) {
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700179 // Create a program that draws a bounding box over the path and fills its stencil coverage
180 // into the color buffer.
Chris Daltona05ccc32021-06-29 19:42:13 -0600181 auto* bboxShader = args.fArena->make<BoundingBoxShader>(fViewMatrix, fColor,
182 *args.fCaps->shaderCaps());
Chris Dalton2f733ec2021-06-01 12:11:57 -0600183 auto* bboxPipeline = GrTessellationShader::MakePipeline(args, fAAType,
184 std::move(appliedClip),
185 std::move(fProcessors));
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600186 auto* bboxStencil =
187 GrPathTessellationShader::TestAndResetStencilSettings(fPath.isInverseFillType());
Chris Dalton8aec1242021-07-19 11:10:45 -0600188 fCoverBBoxProgram = GrSimpleMeshDrawOpHelper::CreateProgramInfo(
189 args.fArena,
190 bboxPipeline,
191 args.fWriteView,
192 bboxShader,
193 GrPrimitiveType::kTriangleStrip,
194 args.fXferBarrierFlags,
195 args.fColorLoadOp,
196 bboxStencil);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700197 }
198}
199
Chris Dalton031d76b2021-06-08 16:32:00 -0600200void GrPathStencilCoverOp::onPrePrepare(GrRecordingContext* context,
201 const GrSurfaceProxyView& writeView, GrAppliedClip* clip,
202 const GrDstProxyView& dstProxyView,
203 GrXferBarrierFlags renderPassXferBarriers,
204 GrLoadOp colorLoadOp) {
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700205 this->prePreparePrograms({context->priv().recordTimeAllocator(), writeView, &dstProxyView,
206 renderPassXferBarriers, colorLoadOp, context->priv().caps()},
207 (clip) ? std::move(*clip) : GrAppliedClip::Disabled());
208 if (fStencilFanProgram) {
209 context->priv().recordProgramInfo(fStencilFanProgram);
210 }
211 if (fStencilPathProgram) {
212 context->priv().recordProgramInfo(fStencilPathProgram);
213 }
Chris Dalton031d76b2021-06-08 16:32:00 -0600214 if (fCoverBBoxProgram) {
215 context->priv().recordProgramInfo(fCoverBBoxProgram);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700216 }
217}
218
Chris Daltona05ccc32021-06-29 19:42:13 -0600219GR_DECLARE_STATIC_UNIQUE_KEY(gUnitQuadBufferKey);
220
Chris Dalton031d76b2021-06-08 16:32:00 -0600221void GrPathStencilCoverOp::onPrepare(GrOpFlushState* flushState) {
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700222 if (!fTessellator) {
223 this->prePreparePrograms({flushState->allocator(), flushState->writeView(),
224 &flushState->dstProxyView(), flushState->renderPassBarriers(),
225 flushState->colorLoadOp(), &flushState->caps()},
226 flushState->detachAppliedClip());
227 if (!fTessellator) {
228 return;
229 }
230 }
231
Chris Dalton69669812021-07-27 10:00:12 -0600232 // We transform paths on the CPU. This allows for better batching.
233 const SkMatrix& pathMatrix = fViewMatrix;
234
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700235 if (fStencilFanProgram) {
236 // The inner fan isn't built into the tessellator. Generate a standard Redbook fan with a
237 // middle-out topology.
238 GrEagerDynamicVertexAllocator vertexAlloc(flushState, &fFanBuffer, &fFanBaseVertex);
239 int maxFanTriangles = fPath.countVerbs() - 2; // n - 2 triangles make an n-gon.
Chris Dalton8731a712021-05-14 14:48:54 -0600240 GrVertexWriter triangleVertexWriter = vertexAlloc.lock<SkPoint>(maxFanTriangles * 3);
Chris Dalton40c906f2021-07-26 11:27:05 -0600241 int numTrianglesWritten;
Chris Dalton69669812021-07-27 10:00:12 -0600242 GrMiddleOutPolygonTriangulator::WritePathInnerFan(std::move(triangleVertexWriter),
243 0,
244 0,
245 pathMatrix,
246 fPath,
247 &numTrianglesWritten);
Chris Dalton40c906f2021-07-26 11:27:05 -0600248 fFanVertexCount = 3 * numTrianglesWritten;
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700249 SkASSERT(fFanVertexCount <= maxFanTriangles * 3);
250 vertexAlloc.unlock(fFanVertexCount);
251 }
252
Chris Dalton69669812021-07-27 10:00:12 -0600253 fTessellator->prepare(flushState, this->bounds(), pathMatrix, fPath);
Chris Daltonc91dd692021-05-24 15:04:47 -0600254
Chris Dalton031d76b2021-06-08 16:32:00 -0600255 if (fCoverBBoxProgram) {
Chris Daltonc91dd692021-05-24 15:04:47 -0600256 GrVertexWriter vertexWriter = flushState->makeVertexSpace(sizeof(SkRect), 1, &fBBoxBuffer,
257 &fBBoxBaseInstance);
Chris Daltonbaae2dd2021-06-25 14:52:49 -0600258 if (fPath.isInverseFillType()) {
259 // Fill the entire backing store to make sure we clear every stencil value back to 0. If
260 // there is a scissor it will have already clipped the stencil draw.
261 auto rtBounds = flushState->writeView().asRenderTargetProxy()->backingStoreBoundsRect();
262 SkASSERT(rtBounds == fOriginalDrawBounds);
263 SkRect pathSpaceRTBounds;
264 if (SkMatrixPriv::InverseMapRect(fViewMatrix, &pathSpaceRTBounds, rtBounds)) {
265 vertexWriter.write(pathSpaceRTBounds);
266 } else {
267 vertexWriter.write(fPath.getBounds());
268 }
269 } else {
270 vertexWriter.write(fPath.getBounds());
271 }
Chris Daltonc91dd692021-05-24 15:04:47 -0600272 }
Chris Daltona05ccc32021-06-29 19:42:13 -0600273
274 if (!flushState->caps().shaderCaps()->vertexIDSupport()) {
275 constexpr static SkPoint kUnitQuad[4] = {{0,0}, {0,1}, {1,0}, {1,1}};
276
277 GR_DEFINE_STATIC_UNIQUE_KEY(gUnitQuadBufferKey);
278
279 fBBoxVertexBufferIfNoIDSupport = flushState->resourceProvider()->findOrMakeStaticBuffer(
280 GrGpuBufferType::kVertex, sizeof(kUnitQuad), kUnitQuad, gUnitQuadBufferKey);
281 }
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700282}
283
Chris Dalton031d76b2021-06-08 16:32:00 -0600284void GrPathStencilCoverOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700285 if (!fTessellator) {
286 return;
287 }
288
289 // Stencil the inner fan, if any.
290 if (fFanVertexCount > 0) {
291 SkASSERT(fStencilFanProgram);
292 SkASSERT(fFanBuffer);
293 flushState->bindPipelineAndScissorClip(*fStencilFanProgram, this->bounds());
294 flushState->bindBuffers(nullptr, nullptr, fFanBuffer);
295 flushState->draw(fFanVertexCount, fFanBaseVertex);
296 }
297
298 // Stencil the rest of the path.
299 SkASSERT(fStencilPathProgram);
300 flushState->bindPipelineAndScissorClip(*fStencilPathProgram, this->bounds());
301 fTessellator->draw(flushState);
Chris Daltond9bdc322021-06-01 19:22:05 -0600302 if (flushState->caps().requiresManualFBBarrierAfterTessellatedStencilDraw()) {
303 flushState->gpu()->insertManualFramebufferBarrier(); // http://skbug.com/9739
304 }
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700305
306 // Fill in the bounding box (if not in stencil-only mode).
Chris Dalton031d76b2021-06-08 16:32:00 -0600307 if (fCoverBBoxProgram) {
308 flushState->bindPipelineAndScissorClip(*fCoverBBoxProgram, this->bounds());
309 flushState->bindTextures(fCoverBBoxProgram->geomProc(), nullptr,
310 fCoverBBoxProgram->pipeline());
Chris Daltona05ccc32021-06-29 19:42:13 -0600311 flushState->bindBuffers(nullptr, fBBoxBuffer, fBBoxVertexBufferIfNoIDSupport);
Chris Daltonc91dd692021-05-24 15:04:47 -0600312 flushState->drawInstanced(1, fBBoxBaseInstance, 4, 0);
Chris Dalton70a0d2c2021-01-26 12:01:21 -0700313 }
314}