Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 8 | #include "src/gpu/tessellate/GrPathTessellateOp.h" |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 9 | |
Chris Dalton | d081dce | 2020-01-23 12:09:04 -0700 | [diff] [blame] | 10 | #include "src/gpu/GrEagerVertexAllocator.h" |
Chris Dalton | e465205 | 2021-01-21 18:31:28 -0700 | [diff] [blame] | 11 | #include "src/gpu/GrInnerFanTriangulator.h" |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 12 | #include "src/gpu/GrOpFlushState.h" |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 13 | #include "src/gpu/GrRecordingContextPriv.h" |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 14 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h" |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 15 | #include "src/gpu/tessellate/GrFillPathShader.h" |
Chris Dalton | f5132a0 | 2020-04-27 23:40:03 -0600 | [diff] [blame] | 16 | #include "src/gpu/tessellate/GrMiddleOutPolygonTriangulator.h" |
Chris Dalton | f9aea7f | 2020-01-21 11:19:26 -0700 | [diff] [blame] | 17 | #include "src/gpu/tessellate/GrStencilPathShader.h" |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 18 | #include "src/gpu/tessellate/GrTessellationPathRenderer.h" |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 19 | |
| 20 | using OpFlags = GrTessellationPathRenderer::OpFlags; |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 21 | |
Chris Dalton | b064334 | 2020-12-15 01:04:12 -0700 | [diff] [blame] | 22 | void GrPathTessellateOp::visitProxies(const VisitProxyFunc& fn) const { |
| 23 | if (fPipelineForFills) { |
| 24 | fPipelineForFills->visitProxies(fn); |
| 25 | } else { |
| 26 | fProcessors.visitProxies(fn); |
| 27 | } |
| 28 | } |
| 29 | |
Chris Dalton | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 30 | GrPathTessellateOp::FixedFunctionFlags GrPathTessellateOp::fixedFunctionFlags() const { |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 31 | auto flags = FixedFunctionFlags::kUsesStencil; |
| 32 | if (GrAAType::kNone != fAAType) { |
| 33 | flags |= FixedFunctionFlags::kUsesHWAA; |
| 34 | } |
| 35 | return flags; |
| 36 | } |
| 37 | |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 38 | void GrPathTessellateOp::onPrePrepare(GrRecordingContext* context, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 39 | const GrSurfaceProxyView& writeView, GrAppliedClip* clip, |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 40 | const GrXferProcessor::DstProxyView& dstProxyView, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 41 | GrXferBarrierFlags renderPassXferBarriers, |
| 42 | GrLoadOp colorLoadOp) { |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 43 | SkArenaAlloc* recordTimeAllocator = context->priv().recordTimeAllocator(); |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 44 | GrAppliedHardClip hardClip = GrAppliedHardClip( |
| 45 | (clip) ? clip->hardClip() : GrAppliedHardClip::Disabled()); |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 46 | PrePrepareArgs args{recordTimeAllocator, writeView, &hardClip, clip, &dstProxyView, |
Chris Dalton | e465205 | 2021-01-21 18:31:28 -0700 | [diff] [blame] | 47 | renderPassXferBarriers, colorLoadOp, context->priv().caps()}; |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 48 | |
| 49 | this->prePreparePrograms(args); |
| 50 | |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 51 | if (fStencilTrianglesProgram) { |
| 52 | context->priv().recordProgramInfo(fStencilTrianglesProgram); |
| 53 | } |
| 54 | if (fStencilCubicsProgram) { |
| 55 | context->priv().recordProgramInfo(fStencilCubicsProgram); |
| 56 | } |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 57 | if (fFillTrianglesProgram) { |
| 58 | context->priv().recordProgramInfo(fFillTrianglesProgram); |
| 59 | } |
| 60 | if (fFillPathProgram) { |
| 61 | context->priv().recordProgramInfo(fFillPathProgram); |
| 62 | } |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void GrPathTessellateOp::prePreparePrograms(const PrePrepareArgs& args) { |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 66 | using DrawInnerFan = GrPathIndirectTessellator::DrawInnerFan; |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 67 | int numVerbs = fPath.countVerbs(); |
| 68 | if (numVerbs <= 0) { |
| 69 | return; |
| 70 | } |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 71 | |
| 72 | // First check if the path is large and/or simple enough that we can actually triangulate the |
| 73 | // inner polygon(s) on the CPU. This is our fastest approach. It allows us to stencil only the |
| 74 | // curves, and then fill the internal polygons directly to the final render target, thus drawing |
| 75 | // the majority of pixels in a single render pass. |
| 76 | SkScalar scales[2]; |
| 77 | SkAssertResult(fViewMatrix.getMinMaxScales(scales)); // Will fail if perspective. |
| 78 | const SkRect& bounds = fPath.getBounds(); |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 79 | float gpuFragmentWork = bounds.height() * scales[0] * bounds.width() * scales[1]; |
| 80 | float cpuTessellationWork = (float)numVerbs * SkNextLog2(numVerbs); // N log N. |
Chris Dalton | 70a0d2c | 2021-01-26 12:01:21 -0700 | [diff] [blame] | 81 | if (cpuTessellationWork * 500 + (256 * 256) < gpuFragmentWork) { // Don't try below 256x256. |
Chris Dalton | 86d4cfd | 2020-11-03 13:51:21 -0700 | [diff] [blame] | 82 | bool isLinear; |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 83 | // This will fail if the inner triangles do not form a simple polygon (e.g., self |
| 84 | // intersection, double winding). |
Chris Dalton | 86d4cfd | 2020-11-03 13:51:21 -0700 | [diff] [blame] | 85 | if (this->prePrepareInnerPolygonTriangulation(args, &isLinear)) { |
| 86 | if (!isLinear) { |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 87 | // Always use indirect draws for cubics instead of tessellation here. Our goal in |
| 88 | // this mode is to maximize GPU performance, and the middle-out topology used by our |
| 89 | // indirect draws is easier on the rasterizer than a tessellated fan. There also |
| 90 | // seems to be a small amount of fixed tessellation overhead that this avoids. |
| 91 | this->prePrepareStencilCubicsProgram<GrMiddleOutCubicShader>(args); |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 92 | // We will need one final pass to cover the convex hulls of the cubics after |
| 93 | // drawing the inner triangles. |
| 94 | this->prePrepareFillCubicHullsProgram(args); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 95 | fTessellator = args.fArena->make<GrPathIndirectTessellator>(fViewMatrix, fPath, |
| 96 | DrawInnerFan::kNo); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 97 | } |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 98 | return; |
| 99 | } |
| 100 | } |
| 101 | |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 102 | // If we didn't triangulate the inner fan then the fill program will be a simple bounding box. |
| 103 | this->prePrepareFillBoundingBoxProgram(args); |
| 104 | |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 105 | // When there are only a few verbs, it seems to always be fastest to make a single indirect draw |
| 106 | // that contains both the inner triangles and the outer cubics, instead of using hardware |
| 107 | // tessellation. Also take this path if tessellation is not supported. |
| 108 | bool drawTrianglesAsIndirectCubicDraw = (numVerbs < 50); |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 109 | if (drawTrianglesAsIndirectCubicDraw || (fOpFlags & OpFlags::kDisableHWTessellation)) { |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 110 | if (!drawTrianglesAsIndirectCubicDraw) { |
| 111 | this->prePrepareStencilTrianglesProgram(args); |
| 112 | } |
| 113 | this->prePrepareStencilCubicsProgram<GrMiddleOutCubicShader>(args); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 114 | fTessellator = args.fArena->make<GrPathIndirectTessellator>( |
| 115 | fViewMatrix, fPath, DrawInnerFan(drawTrianglesAsIndirectCubicDraw)); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 116 | return; |
| 117 | } |
| 118 | |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 119 | // The caller should have sent Flags::kDisableHWTessellation if it was not supported. |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 120 | SkASSERT(args.fCaps->shaderCaps()->tessellationSupport()); |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 121 | |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 122 | // Next see if we can split up the inner triangles and outer cubics into two draw calls. This |
| 123 | // allows for a more efficient inner triangle topology that can reduce the rasterizer load by a |
| 124 | // large margin on complex paths, but also causes greater CPU overhead due to the extra shader |
| 125 | // switches and draw calls. |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 126 | // NOTE: Raster-edge work is 1-dimensional, so we sum height and width instead of multiplying. |
| 127 | float rasterEdgeWork = (bounds.height() + bounds.width()) * scales[1] * fPath.countVerbs(); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 128 | if (rasterEdgeWork > 300 * 300) { |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 129 | this->prePrepareStencilTrianglesProgram(args); |
| 130 | this->prePrepareStencilCubicsProgram<GrCubicTessellateShader>(args); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 131 | fTessellator = args.fArena->make<GrPathOuterCurveTessellator>(); |
Chris Dalton | f9aea7f | 2020-01-21 11:19:26 -0700 | [diff] [blame] | 132 | return; |
| 133 | } |
| 134 | |
| 135 | // Fastest CPU approach: emit one cubic wedge per verb, fanning out from the center. |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 136 | this->prePrepareStencilCubicsProgram<GrWedgeTessellateShader>(args); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 137 | fTessellator = args.fArena->make<GrPathWedgeTessellator>(); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 140 | bool GrPathTessellateOp::prePrepareInnerPolygonTriangulation(const PrePrepareArgs& args, |
Chris Dalton | 86d4cfd | 2020-11-03 13:51:21 -0700 | [diff] [blame] | 141 | bool* isLinear) { |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 142 | SkASSERT(!fTriangleBuffer); |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 143 | SkASSERT(fTriangleVertexCount == 0); |
| 144 | SkASSERT(!fStencilTrianglesProgram); |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 145 | SkASSERT(!fFillTrianglesProgram); |
Chris Dalton | 8a42b09 | 2021-01-21 22:09:26 -0700 | [diff] [blame] | 146 | fInnerFanTriangulator = args.fArena->make<GrInnerFanTriangulator>(fPath, args.fArena, nullptr); |
Chris Dalton | e465205 | 2021-01-21 18:31:28 -0700 | [diff] [blame] | 147 | fInnerFanPolys = fInnerFanTriangulator->pathToPolys(isLinear); |
| 148 | if (!fInnerFanPolys) { |
| 149 | // pathToPolys will fail if the inner polygon(s) are not simple. |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 150 | return false; |
| 151 | } |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 152 | if ((fOpFlags & (OpFlags::kStencilOnly | OpFlags::kWireframe)) || |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 153 | GrAAType::kCoverage == fAAType || |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 154 | (args.fClip && args.fClip->hasStencilClip())) { |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 155 | // If we have certain flags, mixed samples, or a stencil clip then we unfortunately |
| 156 | // can't fill the inner polygon directly. Indicate that these triangles need to be |
| 157 | // stencilled. |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 158 | this->prePrepareStencilTrianglesProgram(args); |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 159 | } |
Chris Dalton | 86d4cfd | 2020-11-03 13:51:21 -0700 | [diff] [blame] | 160 | this->prePrepareFillTrianglesProgram(args, *isLinear); |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 161 | return true; |
| 162 | } |
| 163 | |
Chris Dalton | 1b6a43c | 2020-09-25 12:21:18 -0600 | [diff] [blame] | 164 | // Increments clockwise triangles and decrements counterclockwise. Used for "winding" fill. |
| 165 | constexpr static GrUserStencilSettings kIncrDecrStencil( |
| 166 | GrUserStencilSettings::StaticInitSeparate< |
| 167 | 0x0000, 0x0000, |
| 168 | GrUserStencilTest::kAlwaysIfInClip, GrUserStencilTest::kAlwaysIfInClip, |
| 169 | 0xffff, 0xffff, |
| 170 | GrUserStencilOp::kIncWrap, GrUserStencilOp::kDecWrap, |
| 171 | GrUserStencilOp::kKeep, GrUserStencilOp::kKeep, |
| 172 | 0xffff, 0xffff>()); |
| 173 | |
| 174 | // Inverts the bottom stencil bit. Used for "even/odd" fill. |
| 175 | constexpr static GrUserStencilSettings kInvertStencil( |
| 176 | GrUserStencilSettings::StaticInit< |
| 177 | 0x0000, |
| 178 | GrUserStencilTest::kAlwaysIfInClip, |
| 179 | 0xffff, |
| 180 | GrUserStencilOp::kInvert, |
| 181 | GrUserStencilOp::kKeep, |
| 182 | 0x0001>()); |
| 183 | |
| 184 | constexpr static const GrUserStencilSettings* stencil_pass_settings(SkPathFillType fillType) { |
| 185 | return (fillType == SkPathFillType::kWinding) ? &kIncrDecrStencil : &kInvertStencil; |
| 186 | } |
| 187 | |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 188 | void GrPathTessellateOp::prePrepareStencilTrianglesProgram(const PrePrepareArgs& args) { |
| 189 | SkASSERT(!fStencilTrianglesProgram); |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 190 | |
| 191 | this->prePreparePipelineForStencils(args); |
| 192 | |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 193 | auto* shader = args.fArena->make<GrStencilTriangleShader>(fViewMatrix); |
Chris Dalton | 1b6a43c | 2020-09-25 12:21:18 -0600 | [diff] [blame] | 194 | fStencilTrianglesProgram = GrPathShader::MakeProgramInfo( |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 195 | shader, args.fArena, args.fWriteView, fPipelineForStencils, *args.fDstProxyView, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 196 | args.fXferBarrierFlags, args.fColorLoadOp, stencil_pass_settings(fPath.getFillType()), |
| 197 | *args.fCaps); |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | template<typename ShaderType> |
| 201 | void GrPathTessellateOp::prePrepareStencilCubicsProgram(const PrePrepareArgs& args) { |
| 202 | SkASSERT(!fStencilCubicsProgram); |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 203 | |
| 204 | this->prePreparePipelineForStencils(args); |
| 205 | |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 206 | auto* shader = args.fArena->make<ShaderType>(fViewMatrix); |
Chris Dalton | 1b6a43c | 2020-09-25 12:21:18 -0600 | [diff] [blame] | 207 | fStencilCubicsProgram = GrPathShader::MakeProgramInfo( |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 208 | shader, args.fArena, args.fWriteView, fPipelineForStencils, *args.fDstProxyView, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 209 | args.fXferBarrierFlags, args.fColorLoadOp, stencil_pass_settings(fPath.getFillType()), |
| 210 | *args.fCaps); |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 211 | } |
| 212 | |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 213 | void GrPathTessellateOp::prePreparePipelineForStencils(const PrePrepareArgs& args) { |
| 214 | if (fPipelineForStencils) { |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 215 | return; |
| 216 | } |
| 217 | |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 218 | GrPipeline::InitArgs initArgs; |
| 219 | if (GrAAType::kNone != fAAType) { |
| 220 | initArgs.fInputFlags |= GrPipeline::InputFlags::kHWAntialias; |
| 221 | } |
| 222 | if (args.fCaps->wireframeSupport() && (OpFlags::kWireframe & fOpFlags)) { |
| 223 | initArgs.fInputFlags |= GrPipeline::InputFlags::kWireframe; |
| 224 | } |
| 225 | SkASSERT(SkPathFillType::kWinding == fPath.getFillType() || |
| 226 | SkPathFillType::kEvenOdd == fPath.getFillType()); |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 227 | initArgs.fCaps = args.fCaps; |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 228 | fPipelineForStencils = args.fArena->make<GrPipeline>( |
| 229 | initArgs, GrDisableColorXPFactory::MakeXferProcessor(), *args.fHardClip); |
| 230 | } |
| 231 | |
| 232 | // Allows non-zero stencil values to pass and write a color, and resets the stencil value back to |
| 233 | // zero; discards immediately on stencil values of zero. |
| 234 | // NOTE: It's ok to not check the clip here because the previous stencil pass will have only written |
| 235 | // to samples already inside the clip. |
| 236 | constexpr static GrUserStencilSettings kTestAndResetStencil( |
| 237 | GrUserStencilSettings::StaticInit< |
| 238 | 0x0000, |
| 239 | GrUserStencilTest::kNotEqual, |
| 240 | 0xffff, |
| 241 | GrUserStencilOp::kZero, |
| 242 | GrUserStencilOp::kKeep, |
| 243 | 0xffff>()); |
| 244 | |
Chris Dalton | 86d4cfd | 2020-11-03 13:51:21 -0700 | [diff] [blame] | 245 | void GrPathTessellateOp::prePrepareFillTrianglesProgram(const PrePrepareArgs& args, bool isLinear) { |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 246 | SkASSERT(!fFillTrianglesProgram); |
| 247 | |
| 248 | if (fOpFlags & OpFlags::kStencilOnly) { |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | // These are a twist on the standard red book stencil settings that allow us to fill the inner |
| 253 | // polygon directly to the final render target. At this point, the curves are already stencilled |
| 254 | // in. So if the stencil value is zero, then it means the path at our sample is not affected by |
| 255 | // any curves and we fill the path in directly. If the stencil value is nonzero, then we don't |
| 256 | // fill and instead continue the standard red book stencil process. |
| 257 | // |
| 258 | // NOTE: These settings are currently incompatible with a stencil clip. |
| 259 | constexpr static GrUserStencilSettings kFillOrIncrDecrStencil( |
| 260 | GrUserStencilSettings::StaticInitSeparate< |
| 261 | 0x0000, 0x0000, |
| 262 | GrUserStencilTest::kEqual, GrUserStencilTest::kEqual, |
| 263 | 0xffff, 0xffff, |
| 264 | GrUserStencilOp::kKeep, GrUserStencilOp::kKeep, |
| 265 | GrUserStencilOp::kIncWrap, GrUserStencilOp::kDecWrap, |
| 266 | 0xffff, 0xffff>()); |
| 267 | |
| 268 | constexpr static GrUserStencilSettings kFillOrInvertStencil( |
| 269 | GrUserStencilSettings::StaticInit< |
| 270 | 0x0000, |
| 271 | GrUserStencilTest::kEqual, |
| 272 | 0xffff, |
| 273 | GrUserStencilOp::kKeep, |
| 274 | GrUserStencilOp::kZero, |
| 275 | 0xffff>()); |
| 276 | |
| 277 | this->prePreparePipelineForFills(args); |
| 278 | |
| 279 | const GrUserStencilSettings* stencil; |
| 280 | if (fStencilTrianglesProgram) { |
| 281 | // The path was already stencilled. Here we just need to do a cover pass. |
| 282 | stencil = &kTestAndResetStencil; |
Chris Dalton | 86d4cfd | 2020-11-03 13:51:21 -0700 | [diff] [blame] | 283 | } else if (isLinear) { |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 284 | // There are no stencilled curves. We can ignore stencil and fill the path directly. |
| 285 | stencil = &GrUserStencilSettings::kUnused; |
| 286 | } else if (SkPathFillType::kWinding == fPath.getFillType()) { |
| 287 | // Fill in the path pixels not touched by curves, incr/decr stencil otherwise. |
| 288 | SkASSERT(!fPipelineForFills->hasStencilClip()); |
| 289 | stencil = &kFillOrIncrDecrStencil; |
| 290 | } else { |
| 291 | // Fill in the path pixels not touched by curves, invert stencil otherwise. |
| 292 | SkASSERT(!fPipelineForFills->hasStencilClip()); |
| 293 | stencil = &kFillOrInvertStencil; |
| 294 | } |
| 295 | |
| 296 | auto* fillTriangleShader = args.fArena->make<GrFillTriangleShader>(fViewMatrix, fColor); |
| 297 | fFillTrianglesProgram = GrPathShader::MakeProgramInfo( |
| 298 | fillTriangleShader, args.fArena, args.fWriteView, fPipelineForFills, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 299 | *args.fDstProxyView, args.fXferBarrierFlags, args.fColorLoadOp, stencil, *args.fCaps); |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | void GrPathTessellateOp::prePrepareFillCubicHullsProgram(const PrePrepareArgs& args) { |
| 303 | SkASSERT(!fFillPathProgram); |
| 304 | |
| 305 | if (fOpFlags & OpFlags::kStencilOnly) { |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | this->prePreparePipelineForFills(args); |
| 310 | |
| 311 | auto* fillCubicHullsShader = args.fArena->make<GrFillCubicHullShader>(fViewMatrix, fColor); |
| 312 | fFillPathProgram = GrPathShader::MakeProgramInfo( |
| 313 | fillCubicHullsShader, args.fArena, args.fWriteView, fPipelineForFills, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 314 | *args.fDstProxyView, args.fXferBarrierFlags, args.fColorLoadOp, &kTestAndResetStencil, |
| 315 | *args.fCaps); |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | void GrPathTessellateOp::prePrepareFillBoundingBoxProgram(const PrePrepareArgs& args) { |
| 319 | SkASSERT(!fFillPathProgram); |
| 320 | |
| 321 | if (fOpFlags & OpFlags::kStencilOnly) { |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | this->prePreparePipelineForFills(args); |
| 326 | |
| 327 | auto* fillBoundingBoxShader = args.fArena->make<GrFillBoundingBoxShader>(fViewMatrix, fColor, |
| 328 | fPath.getBounds()); |
| 329 | fFillPathProgram = GrPathShader::MakeProgramInfo( |
| 330 | fillBoundingBoxShader, args.fArena, args.fWriteView, fPipelineForFills, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 331 | *args.fDstProxyView, args.fXferBarrierFlags, args.fColorLoadOp, &kTestAndResetStencil, |
| 332 | *args.fCaps); |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | void GrPathTessellateOp::prePreparePipelineForFills(const PrePrepareArgs& args) { |
| 336 | SkASSERT(!(fOpFlags & OpFlags::kStencilOnly)); |
| 337 | |
| 338 | if (fPipelineForFills) { |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | auto pipelineFlags = GrPipeline::InputFlags::kNone; |
| 343 | if (GrAAType::kNone != fAAType) { |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 344 | if (args.fWriteView.asRenderTargetProxy()->numSamples() == 1) { |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 345 | // We are mixed sampled. We need to either enable conservative raster (preferred) or |
| 346 | // disable MSAA in order to avoid double blend artifacts. (Even if we disable MSAA for |
| 347 | // the cover geometry, the stencil test is still multisampled and will still produce |
| 348 | // smooth results.) |
| 349 | SkASSERT(GrAAType::kCoverage == fAAType); |
| 350 | if (args.fCaps->conservativeRasterSupport()) { |
| 351 | pipelineFlags |= GrPipeline::InputFlags::kHWAntialias; |
| 352 | pipelineFlags |= GrPipeline::InputFlags::kConservativeRaster; |
| 353 | } |
| 354 | } else { |
| 355 | // We are standard MSAA. Leave MSAA enabled for the cover geometry. |
| 356 | pipelineFlags |= GrPipeline::InputFlags::kHWAntialias; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | fPipelineForFills = GrSimpleMeshDrawOpHelper::CreatePipeline( |
Chris Dalton | b064334 | 2020-12-15 01:04:12 -0700 | [diff] [blame] | 361 | args.fCaps, args.fArena, args.fWriteView.swizzle(), |
| 362 | (args.fClip) ? std::move(*args.fClip) : GrAppliedClip::Disabled(), *args.fDstProxyView, |
| 363 | std::move(fProcessors), pipelineFlags); |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | void GrPathTessellateOp::onPrepare(GrOpFlushState* flushState) { |
| 367 | int numVerbs = fPath.countVerbs(); |
| 368 | if (numVerbs <= 0) { |
| 369 | return; |
| 370 | } |
| 371 | |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 372 | if (!fPipelineForStencils && !fPipelineForFills) { |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 373 | // Nothing has been prePrepared yet. Do it now. |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 374 | GrAppliedHardClip hardClip = GrAppliedHardClip(flushState->appliedHardClip()); |
| 375 | GrAppliedClip clip = flushState->detachAppliedClip(); |
| 376 | PrePrepareArgs args{flushState->allocator(), flushState->writeView(), &hardClip, |
| 377 | &clip, &flushState->dstProxyView(), |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 378 | flushState->renderPassBarriers(), flushState->colorLoadOp(), |
Chris Dalton | e465205 | 2021-01-21 18:31:28 -0700 | [diff] [blame] | 379 | &flushState->caps()}; |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 380 | this->prePreparePrograms(args); |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 381 | } |
| 382 | |
Chris Dalton | e465205 | 2021-01-21 18:31:28 -0700 | [diff] [blame] | 383 | if (fInnerFanPolys) { |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 384 | // prePreparePrograms was able to generate an inner polygon triangulation. It will exist in |
| 385 | // either fOffThreadInnerTriangulation or fTriangleBuffer exclusively. |
Chris Dalton | e465205 | 2021-01-21 18:31:28 -0700 | [diff] [blame] | 386 | SkASSERT(fInnerFanTriangulator); |
| 387 | GrEagerDynamicVertexAllocator alloc(flushState, &fTriangleBuffer, &fBaseTriangleVertex); |
| 388 | fTriangleVertexCount = fInnerFanTriangulator->polysToTriangles(fInnerFanPolys, &alloc); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 389 | } else if (fStencilTrianglesProgram) { |
| 390 | // The inner fan isn't built into the tessellator. Generate a standard Redbook fan with a |
| 391 | // middle-out topology. |
| 392 | GrEagerDynamicVertexAllocator vertexAlloc(flushState, &fTriangleBuffer, |
| 393 | &fBaseTriangleVertex); |
| 394 | // No initial moveTo, plus an implicit close at the end; n-2 triangles fill an n-gon. |
| 395 | int maxInnerTriangles = fPath.countVerbs() - 1; |
| 396 | auto* triangleVertexData = vertexAlloc.lock<SkPoint>(maxInnerTriangles * 3); |
| 397 | fTriangleVertexCount = GrMiddleOutPolygonTriangulator::WritePathInnerFan( |
| 398 | triangleVertexData, 3/*perTriangleVertexAdvance*/, fPath) * 3; |
| 399 | vertexAlloc.unlock(fTriangleVertexCount); |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 400 | } |
| 401 | |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 402 | if (fTessellator) { |
| 403 | fTessellator->prepare(flushState, fViewMatrix, fPath); |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 404 | } |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 405 | } |
| 406 | |
Chris Dalton | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 407 | void GrPathTessellateOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) { |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 408 | this->drawStencilPass(flushState); |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 409 | this->drawCoverPass(flushState); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Chris Dalton | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 412 | void GrPathTessellateOp::drawStencilPass(GrOpFlushState* flushState) { |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 413 | if (fStencilTrianglesProgram && fTriangleVertexCount > 0) { |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 414 | SkASSERT(fTriangleBuffer); |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 415 | flushState->bindPipelineAndScissorClip(*fStencilTrianglesProgram, this->bounds()); |
Chris Dalton | 8513867 | 2020-07-24 08:47:03 -0600 | [diff] [blame] | 416 | flushState->bindBuffers(nullptr, nullptr, fTriangleBuffer); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 417 | flushState->draw(fTriangleVertexCount, fBaseTriangleVertex); |
Chris Dalton | f9aea7f | 2020-01-21 11:19:26 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 420 | if (fTessellator) { |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 421 | flushState->bindPipelineAndScissorClip(*fStencilCubicsProgram, this->bounds()); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 422 | fTessellator->draw(flushState); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
Chris Dalton | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 426 | void GrPathTessellateOp::drawCoverPass(GrOpFlushState* flushState) { |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 427 | if (fFillTrianglesProgram) { |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 428 | SkASSERT(fTriangleBuffer); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 429 | |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 430 | // We have a triangulation of the path's inner polygon. This is the fast path. Fill those |
| 431 | // triangles directly to the screen. |
Chris Dalton | e465205 | 2021-01-21 18:31:28 -0700 | [diff] [blame] | 432 | if (fTriangleVertexCount > 0) { |
| 433 | flushState->bindPipelineAndScissorClip(*fFillTrianglesProgram, this->bounds()); |
| 434 | flushState->bindTextures(fFillTrianglesProgram->primProc(), nullptr, |
| 435 | *fPipelineForFills); |
| 436 | flushState->bindBuffers(nullptr, nullptr, fTriangleBuffer); |
| 437 | flushState->draw(fTriangleVertexCount, fBaseTriangleVertex); |
| 438 | } |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 439 | |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 440 | if (fTessellator) { |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 441 | // At this point, every pixel is filled in except the ones touched by curves. |
| 442 | // fFillPathProgram will issue a final cover pass over the curves by drawing their |
| 443 | // convex hulls. This will fill in any remaining samples and reset the stencil buffer. |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 444 | SkASSERT(fFillPathProgram); |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 445 | flushState->bindPipelineAndScissorClip(*fFillPathProgram, this->bounds()); |
| 446 | flushState->bindTextures(fFillPathProgram->primProc(), nullptr, *fPipelineForFills); |
Chris Dalton | d717743 | 2021-01-15 13:12:50 -0700 | [diff] [blame] | 447 | fTessellator->drawHullInstances(flushState); |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 448 | } |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 449 | } else if (fFillPathProgram) { |
| 450 | // There are no triangles to fill. Just draw a bounding box. |
| 451 | flushState->bindPipelineAndScissorClip(*fFillPathProgram, this->bounds()); |
| 452 | flushState->bindTextures(fFillPathProgram->primProc(), nullptr, *fPipelineForFills); |
| 453 | flushState->bindBuffers(nullptr, nullptr, nullptr); |
| 454 | flushState->draw(4, 0); |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 455 | } |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 456 | } |