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" |
Chris Dalton | 82007f5 | 2021-04-20 00:45:50 -0600 | [diff] [blame] | 14 | #include "src/gpu/tessellate/GrStrokeFixedCountTessellator.h" |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 15 | #include "src/gpu/tessellate/GrStrokeHardwareTessellator.h" |
| 16 | #include "src/gpu/tessellate/GrStrokeIndirectTessellator.h" |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 17 | |
Chris Dalton | 82094cd | 2021-02-19 01:11:27 -0700 | [diff] [blame] | 18 | using DynamicStroke = GrStrokeTessellateShader::DynamicStroke; |
| 19 | |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame] | 20 | GrStrokeTessellateOp::GrStrokeTessellateOp(GrAAType aaType, const SkMatrix& viewMatrix, |
| 21 | const SkPath& path, const SkStrokeRec& stroke, |
| 22 | GrPaint&& paint) |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 23 | : GrDrawOp(ClassID()) |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 24 | , fAAType(aaType) |
| 25 | , fViewMatrix(viewMatrix) |
Chris Dalton | 1017a35 | 2021-02-18 15:22:54 -0700 | [diff] [blame] | 26 | , fPathStrokeList(path, stroke, paint.getColor4f()) |
| 27 | , fTotalCombinedVerbCnt(path.countVerbs()) |
| 28 | , fProcessors(std::move(paint)) { |
Chris Dalton | 42582fc | 2021-02-18 11:29:49 -0700 | [diff] [blame] | 29 | if (SkPathPriv::ConicWeightCnt(path) != 0) { |
| 30 | fShaderFlags |= ShaderFlags::kHasConics; |
| 31 | } |
Chris Dalton | 1017a35 | 2021-02-18 15:22:54 -0700 | [diff] [blame] | 32 | if (!this->headColor().fitsInBytes()) { |
| 33 | fShaderFlags |= ShaderFlags::kWideColor; |
| 34 | } |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 35 | SkRect devBounds = path.getBounds(); |
Chris Dalton | 42582fc | 2021-02-18 11:29:49 -0700 | [diff] [blame] | 36 | float inflationRadius = stroke.getInflationRadius(); |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 37 | devBounds.outset(inflationRadius, inflationRadius); |
| 38 | viewMatrix.mapRect(&devBounds, devBounds); |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 39 | this->setBounds(devBounds, HasAABloat::kNo, IsHairline::kNo); |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame] | 42 | void GrStrokeTessellateOp::visitProxies(const VisitProxyFunc& fn) const { |
Chris Dalton | b064334 | 2020-12-15 01:04:12 -0700 | [diff] [blame] | 43 | if (fFillProgram) { |
| 44 | fFillProgram->visitFPProxies(fn); |
| 45 | } else if (fStencilProgram) { |
| 46 | fStencilProgram->visitFPProxies(fn); |
| 47 | } else { |
| 48 | fProcessors.visitProxies(fn); |
| 49 | } |
| 50 | } |
| 51 | |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame] | 52 | GrDrawOp::FixedFunctionFlags GrStrokeTessellateOp::fixedFunctionFlags() const { |
Chris Dalton | 55abaf5 | 2020-12-08 10:25:13 -0700 | [diff] [blame] | 53 | // We might not actually end up needing stencil, but won't know for sure until finalize(). |
| 54 | // Request it just in case we do end up needing it. |
| 55 | auto flags = FixedFunctionFlags::kUsesStencil; |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 56 | if (GrAAType::kNone != fAAType) { |
| 57 | flags |= FixedFunctionFlags::kUsesHWAA; |
| 58 | } |
| 59 | return flags; |
| 60 | } |
| 61 | |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame] | 62 | GrProcessorSet::Analysis GrStrokeTessellateOp::finalize(const GrCaps& caps, |
| 63 | const GrAppliedClip* clip, |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame] | 64 | GrClampType clampType) { |
Chris Dalton | 55abaf5 | 2020-12-08 10:25:13 -0700 | [diff] [blame] | 65 | // Make sure the finalize happens before combining. We might change fNeedsStencil here. |
Chris Dalton | ed82686 | 2021-02-22 12:01:12 -0700 | [diff] [blame] | 66 | SkASSERT(fPathStrokeList.fNext == nullptr); |
Chris Dalton | 55abaf5 | 2020-12-08 10:25:13 -0700 | [diff] [blame] | 67 | const GrProcessorSet::Analysis& analysis = fProcessors.finalize( |
Chris Dalton | 1017a35 | 2021-02-18 15:22:54 -0700 | [diff] [blame] | 68 | this->headColor(), GrProcessorAnalysisCoverage::kNone, clip, |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 69 | &GrUserStencilSettings::kUnused, caps, clampType, &this->headColor()); |
Chris Dalton | 55abaf5 | 2020-12-08 10:25:13 -0700 | [diff] [blame] | 70 | fNeedsStencil = !analysis.unaffectedByDstValue(); |
| 71 | return analysis; |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame] | 74 | GrOp::CombineResult GrStrokeTessellateOp::onCombineIfPossible(GrOp* grOp, SkArenaAlloc* alloc, |
Chris Dalton | 1017a35 | 2021-02-18 15:22:54 -0700 | [diff] [blame] | 75 | const GrCaps& caps) { |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 76 | SkASSERT(grOp->classID() == this->classID()); |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame] | 77 | auto* op = static_cast<GrStrokeTessellateOp*>(grOp); |
Chris Dalton | 42582fc | 2021-02-18 11:29:49 -0700 | [diff] [blame] | 78 | |
Chris Dalton | 55abaf5 | 2020-12-08 10:25:13 -0700 | [diff] [blame] | 79 | if (fNeedsStencil || |
| 80 | op->fNeedsStencil || |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 81 | fViewMatrix != op->fViewMatrix || |
| 82 | fAAType != op->fAAType || |
Chris Dalton | 42582fc | 2021-02-18 11:29:49 -0700 | [diff] [blame] | 83 | fProcessors != op->fProcessors || |
| 84 | this->headStroke().isHairlineStyle() != op->headStroke().isHairlineStyle()) { |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 85 | return CombineResult::kCannotCombine; |
| 86 | } |
| 87 | |
Chris Dalton | 42582fc | 2021-02-18 11:29:49 -0700 | [diff] [blame] | 88 | auto combinedFlags = fShaderFlags | op->fShaderFlags; |
| 89 | if (!(combinedFlags & ShaderFlags::kDynamicStroke) && |
| 90 | !DynamicStroke::StrokesHaveEqualDynamicState(this->headStroke(), op->headStroke())) { |
| 91 | // The paths have different stroke properties. We will need to enable dynamic stroke if we |
| 92 | // still decide to combine them. |
Chris Dalton | bb33be2 | 2021-02-24 16:30:34 -0700 | [diff] [blame] | 93 | if (this->headStroke().isHairlineStyle()) { |
| 94 | return CombineResult::kCannotCombine; // Dynamic hairlines aren't supported. |
| 95 | } |
Chris Dalton | 42582fc | 2021-02-18 11:29:49 -0700 | [diff] [blame] | 96 | combinedFlags |= ShaderFlags::kDynamicStroke; |
| 97 | } |
Chris Dalton | 1017a35 | 2021-02-18 15:22:54 -0700 | [diff] [blame] | 98 | if (!(combinedFlags & ShaderFlags::kDynamicColor) && this->headColor() != op->headColor()) { |
| 99 | // The paths have different colors. We will need to enable dynamic color if we still decide |
| 100 | // to combine them. |
| 101 | combinedFlags |= ShaderFlags::kDynamicColor; |
| 102 | } |
| 103 | |
| 104 | // Don't actually enable new dynamic state on ops that already have lots of verbs. |
| 105 | constexpr static GrTFlagsMask<ShaderFlags> kDynamicStatesMask(ShaderFlags::kDynamicStroke | |
| 106 | ShaderFlags::kDynamicColor); |
| 107 | ShaderFlags neededDynamicStates = combinedFlags & kDynamicStatesMask; |
| 108 | if (neededDynamicStates != ShaderFlags::kNone) { |
| 109 | if (!this->shouldUseDynamicStates(neededDynamicStates) || |
| 110 | !op->shouldUseDynamicStates(neededDynamicStates)) { |
Chris Dalton | 42582fc | 2021-02-18 11:29:49 -0700 | [diff] [blame] | 111 | return CombineResult::kCannotCombine; |
| 112 | } |
| 113 | } |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 114 | |
Chris Dalton | 42582fc | 2021-02-18 11:29:49 -0700 | [diff] [blame] | 115 | fShaderFlags = combinedFlags; |
Chris Dalton | ed82686 | 2021-02-22 12:01:12 -0700 | [diff] [blame] | 116 | |
| 117 | // Concat the op's PathStrokeList. Since the head element is allocated inside the op, we need to |
| 118 | // copy it. |
| 119 | auto* headCopy = alloc->make<PathStrokeList>(std::move(op->fPathStrokeList)); |
| 120 | *fPathStrokeTail = headCopy; |
| 121 | fPathStrokeTail = (op->fPathStrokeTail == &op->fPathStrokeList.fNext) ? &headCopy->fNext |
| 122 | : op->fPathStrokeTail; |
| 123 | |
| 124 | fTotalCombinedVerbCnt += op->fTotalCombinedVerbCnt; |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 125 | return CombineResult::kMerged; |
| 126 | } |
| 127 | |
Chris Dalton | 55abaf5 | 2020-12-08 10:25:13 -0700 | [diff] [blame] | 128 | // Marks every stencil value as "1". |
| 129 | constexpr static GrUserStencilSettings kMarkStencil( |
| 130 | GrUserStencilSettings::StaticInit< |
| 131 | 0x0001, |
| 132 | GrUserStencilTest::kLessIfInClip, // Match kTestAndResetStencil. |
| 133 | 0x0000, // Always fail. |
| 134 | GrUserStencilOp::kZero, |
| 135 | GrUserStencilOp::kReplace, |
| 136 | 0xffff>()); |
| 137 | |
| 138 | // Passes if the stencil value is nonzero. Also resets the stencil value to zero on pass. This is |
| 139 | // formulated to match kMarkStencil everywhere except the ref and compare mask. This will allow us |
| 140 | // to use the same pipeline for both stencil and fill if dynamic stencil state is supported. |
| 141 | constexpr static GrUserStencilSettings kTestAndResetStencil( |
| 142 | GrUserStencilSettings::StaticInit< |
| 143 | 0x0000, |
| 144 | GrUserStencilTest::kLessIfInClip, // i.e., "not equal to zero, if in clip". |
| 145 | 0x0001, |
| 146 | GrUserStencilOp::kZero, |
| 147 | GrUserStencilOp::kReplace, |
| 148 | 0xffff>()); |
| 149 | |
Chris Dalton | 7d97991 | 2021-05-11 12:36:51 -0600 | [diff] [blame^] | 150 | bool GrStrokeTessellateOp::canUseHardwareTessellation(int numVerbs, const GrCaps& caps) { |
| 151 | SkASSERT(!fStencilProgram && !fFillProgram); // Ensure we haven't std::moved fProcessors. |
| 152 | if (!caps.shaderCaps()->tessellationSupport()) { |
| 153 | return false; |
| 154 | } |
| 155 | if (fProcessors.usesVaryingCoords()) { |
| 156 | // Our back door for HW tessellation shaders isn't currently capable of passing varyings to |
| 157 | // the fragment shader, so if the processors have varyings, we need to use instanced draws |
| 158 | // instead. |
| 159 | return false; |
| 160 | } |
| 161 | #if GR_TEST_UTILS |
| 162 | if (caps.shaderCaps()->maxTessellationSegments() < 64) { |
| 163 | // If maxTessellationSegments is lower than the spec minimum, it means we've overriden it |
| 164 | // for testing. Always use hardware tessellation if this is the case. |
| 165 | return true; |
| 166 | } |
| 167 | #endif |
| 168 | // Only use hardware tessellation if we're drawing a somewhat large number of verbs. Otherwise |
| 169 | // we seem to be better off using instanced draws. |
| 170 | return numVerbs >= 50; |
| 171 | } |
| 172 | |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame] | 173 | void GrStrokeTessellateOp::prePrepareTessellator(GrPathShader::ProgramArgs&& args, |
| 174 | GrAppliedClip&& clip) { |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 175 | SkASSERT(!fTessellator); |
Chris Dalton | 55abaf5 | 2020-12-08 10:25:13 -0700 | [diff] [blame] | 176 | SkASSERT(!fFillProgram); |
| 177 | SkASSERT(!fStencilProgram); |
| 178 | |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 179 | const GrCaps& caps = *args.fCaps; |
| 180 | SkArenaAlloc* arena = args.fArena; |
Chris Dalton | 55abaf5 | 2020-12-08 10:25:13 -0700 | [diff] [blame] | 181 | |
Chris Dalton | 7d97991 | 2021-05-11 12:36:51 -0600 | [diff] [blame^] | 182 | if (this->canUseHardwareTessellation(fTotalCombinedVerbCnt, caps)) { |
Chris Dalton | 82007f5 | 2021-04-20 00:45:50 -0600 | [diff] [blame] | 183 | // Only use hardware tessellation if we're drawing a somewhat large number of verbs. |
| 184 | // Otherwise we seem to be better off using instanced draws. |
Chris Dalton | 6ca9e18 | 2021-04-19 20:49:45 -0600 | [diff] [blame] | 185 | fTessellator = arena->make<GrStrokeHardwareTessellator>(fShaderFlags, fViewMatrix, |
| 186 | &fPathStrokeList, |
Chris Dalton | 82094cd | 2021-02-19 01:11:27 -0700 | [diff] [blame] | 187 | *caps.shaderCaps()); |
Chris Dalton | 82007f5 | 2021-04-20 00:45:50 -0600 | [diff] [blame] | 188 | } else if (fTotalCombinedVerbCnt > 50 && !(fShaderFlags & ShaderFlags::kDynamicColor)) { |
| 189 | // Only use the log2 indirect tessellator if we're drawing a somewhat large number of verbs |
| 190 | // and the stroke doesn't use dynamic color. (The log2 indirect tessellator can't support |
| 191 | // dynamic color without a z-buffer, due to how it reorders strokes.) |
| 192 | fTessellator = arena->make<GrStrokeIndirectTessellator>(fShaderFlags, fViewMatrix, |
| 193 | &fPathStrokeList, |
| 194 | fTotalCombinedVerbCnt, arena); |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 195 | } else { |
Chris Dalton | 82007f5 | 2021-04-20 00:45:50 -0600 | [diff] [blame] | 196 | fTessellator = arena->make<GrStrokeFixedCountTessellator>(fShaderFlags, fViewMatrix, |
| 197 | &fPathStrokeList); |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 200 | auto* pipeline = GrFillPathShader::MakeFillPassPipeline(args, fAAType, std::move(clip), |
| 201 | std::move(fProcessors)); |
Chris Dalton | 55abaf5 | 2020-12-08 10:25:13 -0700 | [diff] [blame] | 202 | auto fillStencil = &GrUserStencilSettings::kUnused; |
Chris Dalton | 55abaf5 | 2020-12-08 10:25:13 -0700 | [diff] [blame] | 203 | if (fNeedsStencil) { |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 204 | fStencilProgram = GrPathShader::MakeProgram(args, fTessellator->shader(), pipeline, |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 205 | &kMarkStencil); |
Chris Dalton | 55abaf5 | 2020-12-08 10:25:13 -0700 | [diff] [blame] | 206 | fillStencil = &kTestAndResetStencil; |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 207 | args.fXferBarrierFlags = GrXferBarrierFlags::kNone; |
Chris Dalton | 55abaf5 | 2020-12-08 10:25:13 -0700 | [diff] [blame] | 208 | } |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 209 | |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 210 | fFillProgram = GrPathShader::MakeProgram(args, fTessellator->shader(), pipeline, fillStencil); |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame] | 213 | void GrStrokeTessellateOp::onPrePrepare(GrRecordingContext* context, |
| 214 | const GrSurfaceProxyView& writeView, GrAppliedClip* clip, |
| 215 | const GrXferProcessor::DstProxyView& dstProxyView, |
| 216 | GrXferBarrierFlags renderPassXferBarriers, GrLoadOp |
| 217 | colorLoadOp) { |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 218 | this->prePrepareTessellator({context->priv().recordTimeAllocator(), writeView, &dstProxyView, |
| 219 | renderPassXferBarriers, colorLoadOp, context->priv().caps()}, |
| 220 | (clip) ? std::move(*clip) : GrAppliedClip::Disabled()); |
| 221 | if (fStencilProgram) { |
| 222 | context->priv().recordProgramInfo(fStencilProgram); |
| 223 | } |
| 224 | if (fFillProgram) { |
| 225 | context->priv().recordProgramInfo(fFillProgram); |
| 226 | } |
| 227 | } |
| 228 | |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame] | 229 | void GrStrokeTessellateOp::onPrepare(GrOpFlushState* flushState) { |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 230 | if (!fTessellator) { |
| 231 | this->prePrepareTessellator({flushState->allocator(), flushState->writeView(), |
| 232 | &flushState->dstProxyView(), flushState->renderPassBarriers(), |
| 233 | flushState->colorLoadOp(), &flushState->caps()}, |
| 234 | flushState->detachAppliedClip()); |
| 235 | } |
| 236 | SkASSERT(fTessellator); |
Chris Dalton | 6ca9e18 | 2021-04-19 20:49:45 -0600 | [diff] [blame] | 237 | fTessellator->prepare(flushState, fTotalCombinedVerbCnt); |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Chris Dalton | 05007df | 2021-02-04 00:24:52 -0700 | [diff] [blame] | 240 | void GrStrokeTessellateOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) { |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 241 | if (fStencilProgram) { |
Chris Dalton | 82094cd | 2021-02-19 01:11:27 -0700 | [diff] [blame] | 242 | flushState->bindPipelineAndScissorClip(*fStencilProgram, chainBounds); |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 243 | flushState->bindTextures(fStencilProgram->geomProc(), nullptr, fStencilProgram->pipeline()); |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 244 | fTessellator->draw(flushState); |
| 245 | } |
| 246 | if (fFillProgram) { |
Chris Dalton | 82094cd | 2021-02-19 01:11:27 -0700 | [diff] [blame] | 247 | flushState->bindPipelineAndScissorClip(*fFillProgram, chainBounds); |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 248 | flushState->bindTextures(fFillProgram->geomProc(), nullptr, fFillProgram->pipeline()); |
Chris Dalton | 2224100 | 2021-02-04 09:47:40 -0700 | [diff] [blame] | 249 | fTessellator->draw(flushState); |
| 250 | } |
Chris Dalton | 0e54309 | 2020-11-03 14:09:16 -0700 | [diff] [blame] | 251 | } |