Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame^] | 8 | #include "src/gpu/tessellate/GrStrokeTessellateOp.h" |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 9 | |
| 10 | #include "src/core/SkPathPriv.h" |
| 11 | #include "src/gpu/GrRecordingContextPriv.h" |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 12 | #include "src/gpu/tessellate/GrFillPathShader.h" |
| 13 | #include "src/gpu/tessellate/GrStencilPathShader.h" |
| 14 | #include "src/gpu/tessellate/GrStrokeHardwareTessellator.h" |
| 15 | #include "src/gpu/tessellate/GrStrokeIndirectTessellator.h" |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 16 | |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame^] | 17 | GrStrokeTessellateOp::GrStrokeTessellateOp(GrAAType aaType, const SkMatrix& viewMatrix, |
| 18 | const SkPath& path, const SkStrokeRec& stroke, |
| 19 | GrPaint&& paint) |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 20 | : GrDrawOp(ClassID()) |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 21 | , fAAType(aaType) |
| 22 | , fViewMatrix(viewMatrix) |
| 23 | , fStroke(stroke) |
Chris Dalton | 55abaf5 | 2020-12-08 10:25:13 -0700 | [diff] [blame] | 24 | , fColor(paint.getColor4f()) |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 25 | , fProcessors(std::move(paint)) |
| 26 | , fPathList(path) |
Chris Dalton | 7b80726 | 2020-12-10 10:22:50 -0700 | [diff] [blame] | 27 | , fTotalCombinedVerbCnt(path.countVerbs()) |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 28 | , fHasConics(SkPathPriv::ConicWeightCnt(path) != 0) { |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 29 | SkRect devBounds = path.getBounds(); |
| 30 | float inflationRadius = fStroke.getInflationRadius(); |
| 31 | devBounds.outset(inflationRadius, inflationRadius); |
| 32 | viewMatrix.mapRect(&devBounds, devBounds); |
| 33 | this->setBounds(devBounds, HasAABloat(GrAAType::kCoverage == fAAType), IsHairline::kNo); |
| 34 | } |
| 35 | |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame^] | 36 | void GrStrokeTessellateOp::visitProxies(const VisitProxyFunc& fn) const { |
Chris Dalton | b064334 | 2020-12-15 01:04:12 -0700 | [diff] [blame] | 37 | if (fFillProgram) { |
| 38 | fFillProgram->visitFPProxies(fn); |
| 39 | } else if (fStencilProgram) { |
| 40 | fStencilProgram->visitFPProxies(fn); |
| 41 | } else { |
| 42 | fProcessors.visitProxies(fn); |
| 43 | } |
| 44 | } |
| 45 | |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame^] | 46 | GrDrawOp::FixedFunctionFlags GrStrokeTessellateOp::fixedFunctionFlags() const { |
Chris Dalton | 55abaf5 | 2020-12-08 10:25:13 -0700 | [diff] [blame] | 47 | // We might not actually end up needing stencil, but won't know for sure until finalize(). |
| 48 | // Request it just in case we do end up needing it. |
| 49 | auto flags = FixedFunctionFlags::kUsesStencil; |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 50 | if (GrAAType::kNone != fAAType) { |
| 51 | flags |= FixedFunctionFlags::kUsesHWAA; |
| 52 | } |
| 53 | return flags; |
| 54 | } |
| 55 | |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame^] | 56 | GrProcessorSet::Analysis GrStrokeTessellateOp::finalize(const GrCaps& caps, |
| 57 | const GrAppliedClip* clip, |
| 58 | bool hasMixedSampledCoverage, |
| 59 | GrClampType clampType) { |
Chris Dalton | 55abaf5 | 2020-12-08 10:25:13 -0700 | [diff] [blame] | 60 | // Make sure the finalize happens before combining. We might change fNeedsStencil here. |
| 61 | SkASSERT(fPathList.begin().fCurr->fNext == nullptr); |
| 62 | const GrProcessorSet::Analysis& analysis = fProcessors.finalize( |
| 63 | fColor, GrProcessorAnalysisCoverage::kNone, clip, &GrUserStencilSettings::kUnused, |
| 64 | hasMixedSampledCoverage, caps, clampType, &fColor); |
| 65 | fNeedsStencil = !analysis.unaffectedByDstValue(); |
| 66 | return analysis; |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame^] | 69 | GrOp::CombineResult GrStrokeTessellateOp::onCombineIfPossible(GrOp* grOp, SkArenaAlloc* alloc, |
| 70 | const GrCaps&) { |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 71 | SkASSERT(grOp->classID() == this->classID()); |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame^] | 72 | auto* op = static_cast<GrStrokeTessellateOp*>(grOp); |
Chris Dalton | 55abaf5 | 2020-12-08 10:25:13 -0700 | [diff] [blame] | 73 | if (fNeedsStencil || |
| 74 | op->fNeedsStencil || |
| 75 | fColor != op->fColor || |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 76 | fViewMatrix != op->fViewMatrix || |
| 77 | fAAType != op->fAAType || |
| 78 | !fStroke.hasEqualEffect(op->fStroke) || |
| 79 | fProcessors != op->fProcessors) { |
| 80 | return CombineResult::kCannotCombine; |
| 81 | } |
| 82 | |
| 83 | fPathList.concat(std::move(op->fPathList), alloc); |
| 84 | fTotalCombinedVerbCnt += op->fTotalCombinedVerbCnt; |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 85 | fHasConics |= op->fHasConics; |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 86 | |
| 87 | return CombineResult::kMerged; |
| 88 | } |
| 89 | |
Chris Dalton | 55abaf5 | 2020-12-08 10:25:13 -0700 | [diff] [blame] | 90 | // Marks every stencil value as "1". |
| 91 | constexpr static GrUserStencilSettings kMarkStencil( |
| 92 | GrUserStencilSettings::StaticInit< |
| 93 | 0x0001, |
| 94 | GrUserStencilTest::kLessIfInClip, // Match kTestAndResetStencil. |
| 95 | 0x0000, // Always fail. |
| 96 | GrUserStencilOp::kZero, |
| 97 | GrUserStencilOp::kReplace, |
| 98 | 0xffff>()); |
| 99 | |
| 100 | // Passes if the stencil value is nonzero. Also resets the stencil value to zero on pass. This is |
| 101 | // formulated to match kMarkStencil everywhere except the ref and compare mask. This will allow us |
| 102 | // to use the same pipeline for both stencil and fill if dynamic stencil state is supported. |
| 103 | constexpr static GrUserStencilSettings kTestAndResetStencil( |
| 104 | GrUserStencilSettings::StaticInit< |
| 105 | 0x0000, |
| 106 | GrUserStencilTest::kLessIfInClip, // i.e., "not equal to zero, if in clip". |
| 107 | 0x0001, |
| 108 | GrUserStencilOp::kZero, |
| 109 | GrUserStencilOp::kReplace, |
| 110 | 0xffff>()); |
| 111 | |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame^] | 112 | void GrStrokeTessellateOp::prePrepareTessellator(GrPathShader::ProgramArgs&& args, |
| 113 | GrAppliedClip&& clip) { |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 114 | SkASSERT(!fTessellator); |
Chris Dalton | 55abaf5 | 2020-12-08 10:25:13 -0700 | [diff] [blame] | 115 | SkASSERT(!fFillProgram); |
| 116 | SkASSERT(!fStencilProgram); |
| 117 | |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 118 | const GrCaps& caps = *args.fCaps; |
| 119 | SkArenaAlloc* arena = args.fArena; |
Chris Dalton | 55abaf5 | 2020-12-08 10:25:13 -0700 | [diff] [blame] | 120 | |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 121 | // Only use hardware tessellation if the path has a somewhat large number of verbs. Otherwise we |
| 122 | // seem to be better off using indirect draws. Our back door for HW tessellation shaders isn't |
| 123 | // currently capable of passing varyings to the fragment shader either, so if the processors |
| 124 | // have varyings we need to use indirect draws. |
| 125 | GrStrokeTessellateShader::Mode shaderMode; |
| 126 | if (caps.shaderCaps()->tessellationSupport() && |
| 127 | fTotalCombinedVerbCnt > 50 && |
| 128 | !fProcessors.usesVaryingCoords()) { |
| 129 | fTessellator = arena->make<GrStrokeHardwareTessellator>(*caps.shaderCaps(), fViewMatrix, |
| 130 | fStroke); |
| 131 | shaderMode = GrStrokeTessellateShader::Mode::kTessellation; |
| 132 | } else { |
| 133 | fTessellator = arena->make<GrStrokeIndirectTessellator>(fViewMatrix, fPathList, fStroke, |
| 134 | fTotalCombinedVerbCnt, arena); |
| 135 | shaderMode = GrStrokeTessellateShader::Mode::kIndirect; |
| 136 | } |
| 137 | |
| 138 | // If we are mixed sampled then we need a separate pipeline for the stencil pass. This is |
| 139 | // because mixed samples either needs conservative raster enabled or MSAA disabled during fill. |
| 140 | const GrPipeline* mixedSampledStencilPipeline = nullptr; |
| 141 | if (fAAType == GrAAType::kCoverage) { |
| 142 | SkASSERT(args.fWriteView.asRenderTargetProxy()->numSamples() == 1); |
| 143 | SkASSERT(fNeedsStencil); // Mixed samples always needs stencil. |
| 144 | mixedSampledStencilPipeline = GrStencilPathShader::MakeStencilPassPipeline( |
| 145 | args, fAAType, GrTessellationPathRenderer::OpFlags::kNone, clip.hardClip()); |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 146 | } |
Chris Dalton | 55abaf5 | 2020-12-08 10:25:13 -0700 | [diff] [blame] | 147 | |
Chris Dalton | 06b52ad | 2020-12-15 10:01:35 -0700 | [diff] [blame] | 148 | auto* strokeTessellateShader = arena->make<GrStrokeTessellateShader>( |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 149 | shaderMode, fHasConics, fStroke, fViewMatrix, fColor); |
| 150 | auto* fillPipeline = GrFillPathShader::MakeFillPassPipeline(args, fAAType, std::move(clip), |
| 151 | std::move(fProcessors)); |
Chris Dalton | 55abaf5 | 2020-12-08 10:25:13 -0700 | [diff] [blame] | 152 | auto fillStencil = &GrUserStencilSettings::kUnused; |
Chris Dalton | 55abaf5 | 2020-12-08 10:25:13 -0700 | [diff] [blame] | 153 | if (fNeedsStencil) { |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 154 | auto* stencilPipeline = (mixedSampledStencilPipeline) ? mixedSampledStencilPipeline |
| 155 | : fillPipeline; |
| 156 | fStencilProgram = GrPathShader::MakeProgram(args, strokeTessellateShader, stencilPipeline, |
| 157 | &kMarkStencil); |
Chris Dalton | 55abaf5 | 2020-12-08 10:25:13 -0700 | [diff] [blame] | 158 | fillStencil = &kTestAndResetStencil; |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 159 | args.fXferBarrierFlags = GrXferBarrierFlags::kNone; |
Chris Dalton | 55abaf5 | 2020-12-08 10:25:13 -0700 | [diff] [blame] | 160 | } |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 161 | |
| 162 | fFillProgram = GrPathShader::MakeProgram(args, strokeTessellateShader, fillPipeline, |
| 163 | fillStencil); |
| 164 | } |
| 165 | |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame^] | 166 | void GrStrokeTessellateOp::onPrePrepare(GrRecordingContext* context, |
| 167 | const GrSurfaceProxyView& writeView, GrAppliedClip* clip, |
| 168 | const GrXferProcessor::DstProxyView& dstProxyView, |
| 169 | GrXferBarrierFlags renderPassXferBarriers, GrLoadOp |
| 170 | colorLoadOp) { |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 171 | this->prePrepareTessellator({context->priv().recordTimeAllocator(), writeView, &dstProxyView, |
| 172 | renderPassXferBarriers, colorLoadOp, context->priv().caps()}, |
| 173 | (clip) ? std::move(*clip) : GrAppliedClip::Disabled()); |
| 174 | if (fStencilProgram) { |
| 175 | context->priv().recordProgramInfo(fStencilProgram); |
| 176 | } |
| 177 | if (fFillProgram) { |
| 178 | context->priv().recordProgramInfo(fFillProgram); |
| 179 | } |
| 180 | } |
| 181 | |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame^] | 182 | void GrStrokeTessellateOp::onPrepare(GrOpFlushState* flushState) { |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 183 | if (!fTessellator) { |
| 184 | this->prePrepareTessellator({flushState->allocator(), flushState->writeView(), |
| 185 | &flushState->dstProxyView(), flushState->renderPassBarriers(), |
| 186 | flushState->colorLoadOp(), &flushState->caps()}, |
| 187 | flushState->detachAppliedClip()); |
| 188 | } |
| 189 | SkASSERT(fTessellator); |
| 190 | fTessellator->prepare(flushState, fViewMatrix, fPathList, fStroke, fTotalCombinedVerbCnt); |
| 191 | } |
| 192 | |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame^] | 193 | void GrStrokeTessellateOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) { |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 194 | SkASSERT(chainBounds == this->bounds()); |
| 195 | if (fStencilProgram) { |
| 196 | flushState->bindPipelineAndScissorClip(*fStencilProgram, this->bounds()); |
| 197 | flushState->bindTextures(fStencilProgram->primProc(), nullptr, fStencilProgram->pipeline()); |
| 198 | fTessellator->draw(flushState); |
| 199 | } |
| 200 | if (fFillProgram) { |
| 201 | flushState->bindPipelineAndScissorClip(*fFillProgram, this->bounds()); |
| 202 | flushState->bindTextures(fFillProgram->primProc(), nullptr, fFillProgram->pipeline()); |
| 203 | fTessellator->draw(flushState); |
| 204 | } |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 205 | } |