Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 1 | /* |
| 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 Dalton | 2ed22fa | 2021-05-06 16:08:30 -0600 | [diff] [blame] | 8 | #include "src/gpu/tessellate/GrPathStencilFillOp.h" |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 9 | |
| 10 | #include "src/gpu/GrEagerVertexAllocator.h" |
Chris Dalton | d9bdc32 | 2021-06-01 19:22:05 -0600 | [diff] [blame] | 11 | #include "src/gpu/GrGpu.h" |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 12 | #include "src/gpu/GrOpFlushState.h" |
| 13 | #include "src/gpu/GrRecordingContextPriv.h" |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 14 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 15 | #include "src/gpu/tessellate/GrMiddleOutPolygonTriangulator.h" |
Chris Dalton | d9bdc32 | 2021-06-01 19:22:05 -0600 | [diff] [blame] | 16 | #include "src/gpu/tessellate/GrPathCurveTessellator.h" |
| 17 | #include "src/gpu/tessellate/GrPathIndirectTessellator.h" |
| 18 | #include "src/gpu/tessellate/GrPathWedgeTessellator.h" |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 19 | #include "src/gpu/tessellate/GrTessellationPathRenderer.h" |
Chris Dalton | 3b41278 | 2021-06-01 13:40:03 -0600 | [diff] [blame] | 20 | #include "src/gpu/tessellate/shaders/GrPathTessellationShader.h" |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 21 | |
Chris Dalton | 917f919 | 2021-06-08 14:32:37 -0600 | [diff] [blame^] | 22 | using PathFlags = GrTessellationPathRenderer::PathFlags; |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 23 | |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 24 | namespace { |
| 25 | |
| 26 | // Fills a path's bounding box, with subpixel outset to avoid possible T-junctions with extreme |
| 27 | // edges of the path. |
| 28 | // NOTE: The emitted geometry may not be axis-aligned, depending on the view matrix. |
| 29 | class BoundingBoxShader : public GrPathTessellationShader { |
| 30 | public: |
| 31 | BoundingBoxShader(const SkMatrix& viewMatrix, SkPMColor4f color) |
| 32 | : GrPathTessellationShader(kTessellate_BoundingBoxShader_ClassID, |
| 33 | GrPrimitiveType::kTriangleStrip, 0, viewMatrix, color) { |
| 34 | constexpr static Attribute kPathBoundsAttrib = {"pathBounds", kFloat4_GrVertexAttribType, |
| 35 | kFloat4_GrSLType}; |
| 36 | this->setInstanceAttributes(&kPathBoundsAttrib, 1); |
| 37 | } |
| 38 | |
| 39 | private: |
Chris Dalton | b63711a | 2021-06-01 14:52:02 -0600 | [diff] [blame] | 40 | const char* name() const final { return "tessellate_BoundingBoxShader"; } |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 41 | void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const final {} |
| 42 | GrGLSLGeometryProcessor* createGLSLInstance(const GrShaderCaps&) const final; |
| 43 | }; |
| 44 | |
| 45 | GrGLSLGeometryProcessor* BoundingBoxShader::createGLSLInstance(const GrShaderCaps&) const { |
| 46 | class Impl : public GrPathTessellationShader::Impl { |
| 47 | void emitVertexCode(GrGLSLVertexBuilder* v, GrGPArgs* gpArgs) override { |
| 48 | 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 | |
Chris Dalton | 2ed22fa | 2021-05-06 16:08:30 -0600 | [diff] [blame] | 66 | void GrPathStencilFillOp::visitProxies(const VisitProxyFunc& fn) const { |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 67 | if (fFillBBoxProgram) { |
| 68 | fFillBBoxProgram->pipeline().visitProxies(fn); |
| 69 | } else { |
| 70 | fProcessors.visitProxies(fn); |
| 71 | } |
| 72 | } |
| 73 | |
Chris Dalton | 2ed22fa | 2021-05-06 16:08:30 -0600 | [diff] [blame] | 74 | GrDrawOp::FixedFunctionFlags GrPathStencilFillOp::fixedFunctionFlags() const { |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 75 | auto flags = FixedFunctionFlags::kUsesStencil; |
| 76 | if (fAAType != GrAAType::kNone) { |
| 77 | flags |= FixedFunctionFlags::kUsesHWAA; |
| 78 | } |
| 79 | return flags; |
| 80 | } |
| 81 | |
Chris Dalton | 2ed22fa | 2021-05-06 16:08:30 -0600 | [diff] [blame] | 82 | GrProcessorSet::Analysis GrPathStencilFillOp::finalize(const GrCaps& caps, |
| 83 | const GrAppliedClip* clip, |
| 84 | GrClampType clampType) { |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 85 | return fProcessors.finalize(fColor, GrProcessorAnalysisCoverage::kNone, clip, nullptr, caps, |
| 86 | clampType, &fColor); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 89 | void GrPathStencilFillOp::prePreparePrograms(const GrTessellationShader::ProgramArgs& args, |
Chris Dalton | 2ed22fa | 2021-05-06 16:08:30 -0600 | [diff] [blame] | 90 | GrAppliedClip&& appliedClip) { |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 91 | SkASSERT(!fTessellator); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 92 | SkASSERT(!fStencilFanProgram); |
| 93 | SkASSERT(!fStencilPathProgram); |
| 94 | SkASSERT(!fFillBBoxProgram); |
| 95 | |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 96 | if (fPath.countVerbs() <= 0) { |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 100 | const GrPipeline* stencilPipeline = GrPathTessellationShader::MakeStencilOnlyPipeline( |
Chris Dalton | 917f919 | 2021-06-08 14:32:37 -0600 | [diff] [blame^] | 101 | args, fAAType, fPathFlags, appliedClip.hardClip()); |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 102 | const GrUserStencilSettings* stencilPathSettings = |
| 103 | GrPathTessellationShader::StencilPathSettings(fPath.getFillType()); |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 104 | |
Chris Dalton | 917f919 | 2021-06-08 14:32:37 -0600 | [diff] [blame^] | 105 | auto drawFanWithTessellator = GrPathTessellator::DrawInnerFan::kYes; |
| 106 | if (fPath.countVerbs() > 50 && this->bounds().height() * this->bounds().width() > 256 * 256) { |
| 107 | // Large complex paths do better with a dedicated triangle shader for the inner fan. This |
| 108 | // takes less PCI bus bandwidth (6 floats per triangle instead of 8) and allows us to make |
| 109 | // sure it has an efficient middle-out topology. |
| 110 | auto shader = GrPathTessellationShader::MakeSimpleTriangleShader( |
| 111 | args.fArena, fViewMatrix, SK_PMColor4fTRANSPARENT); |
| 112 | fStencilFanProgram = GrTessellationShader::MakeProgram(args, shader, stencilPipeline, |
| 113 | stencilPathSettings); |
| 114 | drawFanWithTessellator = GrPathTessellator::DrawInnerFan::kNo; |
| 115 | } |
| 116 | if (!args.fCaps->shaderCaps()->tessellationSupport() || |
| 117 | fPath.countVerbs() < args.fCaps->minPathVerbsForHwTessellation()) { |
| 118 | fTessellator = GrPathIndirectTessellator::Make(args.fArena, fPath, fViewMatrix, |
| 119 | SK_PMColor4fTRANSPARENT, |
| 120 | drawFanWithTessellator); |
| 121 | } else if (drawFanWithTessellator == GrPathTessellator::DrawInnerFan::kNo) { |
| 122 | fTessellator = GrPathCurveTessellator::Make(args.fArena, fViewMatrix, |
| 123 | SK_PMColor4fTRANSPARENT, |
| 124 | GrPathTessellator::DrawInnerFan::kNo); |
| 125 | } else { |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 126 | fTessellator = GrPathWedgeTessellator::Make(args.fArena, fViewMatrix, |
| 127 | SK_PMColor4fTRANSPARENT); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 128 | } |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 129 | fStencilPathProgram = GrTessellationShader::MakeProgram(args, fTessellator->shader(), |
| 130 | stencilPipeline, stencilPathSettings); |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 131 | |
Chris Dalton | 917f919 | 2021-06-08 14:32:37 -0600 | [diff] [blame^] | 132 | if (!(fPathFlags & PathFlags::kStencilOnly)) { |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 133 | // Create a program that draws a bounding box over the path and fills its stencil coverage |
| 134 | // into the color buffer. |
Chris Dalton | 2f733ec | 2021-06-01 12:11:57 -0600 | [diff] [blame] | 135 | auto* bboxShader = args.fArena->make<BoundingBoxShader>(fViewMatrix, fColor); |
| 136 | auto* bboxPipeline = GrTessellationShader::MakePipeline(args, fAAType, |
| 137 | std::move(appliedClip), |
| 138 | std::move(fProcessors)); |
| 139 | auto* bboxStencil = GrPathTessellationShader::TestAndResetStencilSettings(); |
| 140 | fFillBBoxProgram = GrTessellationShader::MakeProgram(args, bboxShader, bboxPipeline, |
| 141 | bboxStencil); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Chris Dalton | 2ed22fa | 2021-05-06 16:08:30 -0600 | [diff] [blame] | 145 | void GrPathStencilFillOp::onPrePrepare(GrRecordingContext* context, |
| 146 | const GrSurfaceProxyView& writeView, GrAppliedClip* clip, |
John Stiles | 52cb1d0 | 2021-06-02 11:58:05 -0400 | [diff] [blame] | 147 | const GrDstProxyView& dstProxyView, |
Chris Dalton | 2ed22fa | 2021-05-06 16:08:30 -0600 | [diff] [blame] | 148 | GrXferBarrierFlags renderPassXferBarriers, |
| 149 | GrLoadOp colorLoadOp) { |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 150 | this->prePreparePrograms({context->priv().recordTimeAllocator(), writeView, &dstProxyView, |
| 151 | renderPassXferBarriers, colorLoadOp, context->priv().caps()}, |
| 152 | (clip) ? std::move(*clip) : GrAppliedClip::Disabled()); |
| 153 | if (fStencilFanProgram) { |
| 154 | context->priv().recordProgramInfo(fStencilFanProgram); |
| 155 | } |
| 156 | if (fStencilPathProgram) { |
| 157 | context->priv().recordProgramInfo(fStencilPathProgram); |
| 158 | } |
| 159 | if (fFillBBoxProgram) { |
| 160 | context->priv().recordProgramInfo(fFillBBoxProgram); |
| 161 | } |
| 162 | } |
| 163 | |
Chris Dalton | 2ed22fa | 2021-05-06 16:08:30 -0600 | [diff] [blame] | 164 | void GrPathStencilFillOp::onPrepare(GrOpFlushState* flushState) { |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 165 | if (!fTessellator) { |
| 166 | this->prePreparePrograms({flushState->allocator(), flushState->writeView(), |
| 167 | &flushState->dstProxyView(), flushState->renderPassBarriers(), |
| 168 | flushState->colorLoadOp(), &flushState->caps()}, |
| 169 | flushState->detachAppliedClip()); |
| 170 | if (!fTessellator) { |
| 171 | return; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | if (fStencilFanProgram) { |
| 176 | // The inner fan isn't built into the tessellator. Generate a standard Redbook fan with a |
| 177 | // middle-out topology. |
| 178 | GrEagerDynamicVertexAllocator vertexAlloc(flushState, &fFanBuffer, &fFanBaseVertex); |
| 179 | int maxFanTriangles = fPath.countVerbs() - 2; // n - 2 triangles make an n-gon. |
Chris Dalton | 8731a71 | 2021-05-14 14:48:54 -0600 | [diff] [blame] | 180 | GrVertexWriter triangleVertexWriter = vertexAlloc.lock<SkPoint>(maxFanTriangles * 3); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 181 | fFanVertexCount = GrMiddleOutPolygonTriangulator::WritePathInnerFan( |
Chris Dalton | df2dbad | 2021-05-14 16:21:15 -0600 | [diff] [blame] | 182 | &triangleVertexWriter, GrMiddleOutPolygonTriangulator::OutputType::kTriangles, |
| 183 | fPath) * 3; |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 184 | SkASSERT(fFanVertexCount <= maxFanTriangles * 3); |
| 185 | vertexAlloc.unlock(fFanVertexCount); |
| 186 | } |
| 187 | |
Chris Dalton | 569c01b | 2021-05-25 10:11:46 -0600 | [diff] [blame] | 188 | fTessellator->prepare(flushState, this->bounds(), fPath); |
Chris Dalton | c91dd69 | 2021-05-24 15:04:47 -0600 | [diff] [blame] | 189 | |
| 190 | if (fFillBBoxProgram) { |
| 191 | GrVertexWriter vertexWriter = flushState->makeVertexSpace(sizeof(SkRect), 1, &fBBoxBuffer, |
| 192 | &fBBoxBaseInstance); |
| 193 | vertexWriter.write(fPath.getBounds()); |
| 194 | } |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Chris Dalton | 2ed22fa | 2021-05-06 16:08:30 -0600 | [diff] [blame] | 197 | void GrPathStencilFillOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) { |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 198 | if (!fTessellator) { |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | // Stencil the inner fan, if any. |
| 203 | if (fFanVertexCount > 0) { |
| 204 | SkASSERT(fStencilFanProgram); |
| 205 | SkASSERT(fFanBuffer); |
| 206 | flushState->bindPipelineAndScissorClip(*fStencilFanProgram, this->bounds()); |
| 207 | flushState->bindBuffers(nullptr, nullptr, fFanBuffer); |
| 208 | flushState->draw(fFanVertexCount, fFanBaseVertex); |
| 209 | } |
| 210 | |
| 211 | // Stencil the rest of the path. |
| 212 | SkASSERT(fStencilPathProgram); |
| 213 | flushState->bindPipelineAndScissorClip(*fStencilPathProgram, this->bounds()); |
| 214 | fTessellator->draw(flushState); |
Chris Dalton | d9bdc32 | 2021-06-01 19:22:05 -0600 | [diff] [blame] | 215 | if (flushState->caps().requiresManualFBBarrierAfterTessellatedStencilDraw()) { |
| 216 | flushState->gpu()->insertManualFramebufferBarrier(); // http://skbug.com/9739 |
| 217 | } |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 218 | |
| 219 | // Fill in the bounding box (if not in stencil-only mode). |
| 220 | if (fFillBBoxProgram) { |
| 221 | flushState->bindPipelineAndScissorClip(*fFillBBoxProgram, this->bounds()); |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 222 | flushState->bindTextures(fFillBBoxProgram->geomProc(), nullptr, |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 223 | fFillBBoxProgram->pipeline()); |
Chris Dalton | c91dd69 | 2021-05-24 15:04:47 -0600 | [diff] [blame] | 224 | flushState->bindBuffers(nullptr, fBBoxBuffer, nullptr); |
| 225 | flushState->drawInstanced(1, fBBoxBaseInstance, 4, 0); |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 226 | } |
| 227 | } |