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