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 | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 11 | #include "src/gpu/GrGpu.h" |
| 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 | 17dc418 | 2020-03-25 16:18:16 -0600 | [diff] [blame] | 14 | #include "src/gpu/GrTriangulator.h" |
Chris Dalton | 753c1b3 | 2020-11-02 12:00:19 -0700 | [diff] [blame] | 15 | #include "src/gpu/geometry/GrPathUtils.h" |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 16 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h" |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 17 | #include "src/gpu/tessellate/GrFillPathShader.h" |
Chris Dalton | f5132a0 | 2020-04-27 23:40:03 -0600 | [diff] [blame] | 18 | #include "src/gpu/tessellate/GrMiddleOutPolygonTriangulator.h" |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 19 | #include "src/gpu/tessellate/GrMidpointContourParser.h" |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 20 | #include "src/gpu/tessellate/GrResolveLevelCounter.h" |
Chris Dalton | f9aea7f | 2020-01-21 11:19:26 -0700 | [diff] [blame] | 21 | #include "src/gpu/tessellate/GrStencilPathShader.h" |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 22 | #include "src/gpu/tessellate/GrTessellationPathRenderer.h" |
Chris Dalton | b27f39c | 2020-11-23 09:30:24 -0700 | [diff] [blame] | 23 | #include "src/gpu/tessellate/GrWangsFormula.h" |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 24 | |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 25 | constexpr static float kLinearizationIntolerance = |
| 26 | GrTessellationPathRenderer::kLinearizationIntolerance; |
| 27 | |
| 28 | constexpr static int kMaxResolveLevel = GrTessellationPathRenderer::kMaxResolveLevel; |
| 29 | |
| 30 | using OpFlags = GrTessellationPathRenderer::OpFlags; |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 31 | |
Chris Dalton | b064334 | 2020-12-15 01:04:12 -0700 | [diff] [blame^] | 32 | void GrPathTessellateOp::visitProxies(const VisitProxyFunc& fn) const { |
| 33 | if (fPipelineForFills) { |
| 34 | fPipelineForFills->visitProxies(fn); |
| 35 | } else { |
| 36 | fProcessors.visitProxies(fn); |
| 37 | } |
| 38 | } |
| 39 | |
Chris Dalton | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 40 | GrPathTessellateOp::FixedFunctionFlags GrPathTessellateOp::fixedFunctionFlags() const { |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 41 | auto flags = FixedFunctionFlags::kUsesStencil; |
| 42 | if (GrAAType::kNone != fAAType) { |
| 43 | flags |= FixedFunctionFlags::kUsesHWAA; |
| 44 | } |
| 45 | return flags; |
| 46 | } |
| 47 | |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 48 | namespace { |
Robert Phillips | c655c3a | 2020-03-18 13:23:45 -0400 | [diff] [blame] | 49 | |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 50 | class CpuTriangleAllocator : public GrEagerVertexAllocator { |
| 51 | public: |
| 52 | CpuTriangleAllocator(SkArenaAlloc* arena, const SkPoint** data) : fArena(arena), fData(data) {} |
| 53 | |
| 54 | void* lock(size_t stride, int eagerCount) override { |
| 55 | SkASSERT(!*fData); |
| 56 | SkASSERT(stride == sizeof(SkPoint)); |
| 57 | SkPoint* data = fArena->makeArray<SkPoint>(eagerCount); |
| 58 | *fData = data; |
| 59 | return data; |
| 60 | } |
| 61 | |
| 62 | void unlock(int actualCount) override { SkASSERT(*fData); } |
| 63 | |
| 64 | private: |
| 65 | SkArenaAlloc* const fArena; |
| 66 | const SkPoint** fData; |
| 67 | }; |
| 68 | |
| 69 | } |
| 70 | |
| 71 | void GrPathTessellateOp::onPrePrepare(GrRecordingContext* context, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 72 | const GrSurfaceProxyView& writeView, GrAppliedClip* clip, |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 73 | const GrXferProcessor::DstProxyView& dstProxyView, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 74 | GrXferBarrierFlags renderPassXferBarriers, |
| 75 | GrLoadOp colorLoadOp) { |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 76 | SkArenaAlloc* recordTimeAllocator = context->priv().recordTimeAllocator(); |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 77 | GrAppliedHardClip hardClip = GrAppliedHardClip( |
| 78 | (clip) ? clip->hardClip() : GrAppliedHardClip::Disabled()); |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 79 | CpuTriangleAllocator cpuTriangleAllocator(recordTimeAllocator, &fOffThreadInnerTriangulation); |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 80 | PrePrepareArgs args{recordTimeAllocator, writeView, &hardClip, clip, &dstProxyView, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 81 | renderPassXferBarriers, colorLoadOp, context->priv().caps(), |
| 82 | &cpuTriangleAllocator}; |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 83 | |
| 84 | this->prePreparePrograms(args); |
| 85 | |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 86 | if (fStencilTrianglesProgram) { |
| 87 | context->priv().recordProgramInfo(fStencilTrianglesProgram); |
| 88 | } |
| 89 | if (fStencilCubicsProgram) { |
| 90 | context->priv().recordProgramInfo(fStencilCubicsProgram); |
| 91 | } |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 92 | if (fFillTrianglesProgram) { |
| 93 | context->priv().recordProgramInfo(fFillTrianglesProgram); |
| 94 | } |
| 95 | if (fFillPathProgram) { |
| 96 | context->priv().recordProgramInfo(fFillPathProgram); |
| 97 | } |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void GrPathTessellateOp::prePreparePrograms(const PrePrepareArgs& args) { |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 101 | int numVerbs = fPath.countVerbs(); |
| 102 | if (numVerbs <= 0) { |
| 103 | return; |
| 104 | } |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 105 | |
| 106 | // First check if the path is large and/or simple enough that we can actually triangulate the |
| 107 | // inner polygon(s) on the CPU. This is our fastest approach. It allows us to stencil only the |
| 108 | // curves, and then fill the internal polygons directly to the final render target, thus drawing |
| 109 | // the majority of pixels in a single render pass. |
| 110 | SkScalar scales[2]; |
| 111 | SkAssertResult(fViewMatrix.getMinMaxScales(scales)); // Will fail if perspective. |
| 112 | const SkRect& bounds = fPath.getBounds(); |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 113 | float gpuFragmentWork = bounds.height() * scales[0] * bounds.width() * scales[1]; |
| 114 | float cpuTessellationWork = (float)numVerbs * SkNextLog2(numVerbs); // N log N. |
| 115 | if (cpuTessellationWork * 500 + (256 * 256) < gpuFragmentWork) { // Don't try below 256x256. |
Chris Dalton | 86d4cfd | 2020-11-03 13:51:21 -0700 | [diff] [blame] | 116 | bool isLinear; |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 117 | // This will fail if the inner triangles do not form a simple polygon (e.g., self |
| 118 | // intersection, double winding). |
Chris Dalton | 86d4cfd | 2020-11-03 13:51:21 -0700 | [diff] [blame] | 119 | if (this->prePrepareInnerPolygonTriangulation(args, &isLinear)) { |
| 120 | if (!isLinear) { |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 121 | // Always use indirect draws for cubics instead of tessellation here. Our goal in |
| 122 | // this mode is to maximize GPU performance, and the middle-out topology used by our |
| 123 | // indirect draws is easier on the rasterizer than a tessellated fan. There also |
| 124 | // seems to be a small amount of fixed tessellation overhead that this avoids. |
| 125 | this->prePrepareStencilCubicsProgram<GrMiddleOutCubicShader>(args); |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 126 | // We will need one final pass to cover the convex hulls of the cubics after |
| 127 | // drawing the inner triangles. |
| 128 | this->prePrepareFillCubicHullsProgram(args); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 129 | } |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 130 | return; |
| 131 | } |
| 132 | } |
| 133 | |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 134 | // If we didn't triangulate the inner fan then the fill program will be a simple bounding box. |
| 135 | this->prePrepareFillBoundingBoxProgram(args); |
| 136 | |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 137 | // When there are only a few verbs, it seems to always be fastest to make a single indirect draw |
| 138 | // that contains both the inner triangles and the outer cubics, instead of using hardware |
| 139 | // tessellation. Also take this path if tessellation is not supported. |
| 140 | bool drawTrianglesAsIndirectCubicDraw = (numVerbs < 50); |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 141 | if (drawTrianglesAsIndirectCubicDraw || (fOpFlags & OpFlags::kDisableHWTessellation)) { |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 142 | if (!drawTrianglesAsIndirectCubicDraw) { |
| 143 | this->prePrepareStencilTrianglesProgram(args); |
| 144 | } |
| 145 | this->prePrepareStencilCubicsProgram<GrMiddleOutCubicShader>(args); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 146 | return; |
| 147 | } |
| 148 | |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 149 | // The caller should have sent Flags::kDisableHWTessellation if it was not supported. |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 150 | SkASSERT(args.fCaps->shaderCaps()->tessellationSupport()); |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 151 | |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 152 | // Next see if we can split up the inner triangles and outer cubics into two draw calls. This |
| 153 | // allows for a more efficient inner triangle topology that can reduce the rasterizer load by a |
| 154 | // large margin on complex paths, but also causes greater CPU overhead due to the extra shader |
| 155 | // switches and draw calls. |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 156 | // NOTE: Raster-edge work is 1-dimensional, so we sum height and width instead of multiplying. |
| 157 | float rasterEdgeWork = (bounds.height() + bounds.width()) * scales[1] * fPath.countVerbs(); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 158 | if (rasterEdgeWork > 300 * 300) { |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 159 | this->prePrepareStencilTrianglesProgram(args); |
| 160 | this->prePrepareStencilCubicsProgram<GrCubicTessellateShader>(args); |
Chris Dalton | f9aea7f | 2020-01-21 11:19:26 -0700 | [diff] [blame] | 161 | return; |
| 162 | } |
| 163 | |
| 164 | // 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] | 165 | this->prePrepareStencilCubicsProgram<GrWedgeTessellateShader>(args); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 168 | bool GrPathTessellateOp::prePrepareInnerPolygonTriangulation(const PrePrepareArgs& args, |
Chris Dalton | 86d4cfd | 2020-11-03 13:51:21 -0700 | [diff] [blame] | 169 | bool* isLinear) { |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 170 | SkASSERT(!fTriangleBuffer); |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 171 | SkASSERT(fTriangleVertexCount == 0); |
| 172 | SkASSERT(!fStencilTrianglesProgram); |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 173 | SkASSERT(!fFillTrianglesProgram); |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 174 | |
| 175 | using GrTriangulator::Mode; |
| 176 | |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 177 | fTriangleVertexCount = GrTriangulator::PathToTriangles(fPath, 0, SkRect::MakeEmpty(), |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 178 | args.fInnerTriangleAllocator, |
| 179 | Mode::kSimpleInnerPolygons, |
Chris Dalton | 86d4cfd | 2020-11-03 13:51:21 -0700 | [diff] [blame] | 180 | isLinear); |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 181 | if (fTriangleVertexCount == 0) { |
| 182 | // Mode::kSimpleInnerPolygons causes PathToTriangles to fail if the inner polygon(s) are not |
| 183 | // simple. |
| 184 | return false; |
| 185 | } |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 186 | if ((fOpFlags & (OpFlags::kStencilOnly | OpFlags::kWireframe)) || |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 187 | GrAAType::kCoverage == fAAType || |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 188 | (args.fClip && args.fClip->hasStencilClip())) { |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 189 | // If we have certain flags, mixed samples, or a stencil clip then we unfortunately |
| 190 | // can't fill the inner polygon directly. Indicate that these triangles need to be |
| 191 | // stencilled. |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 192 | this->prePrepareStencilTrianglesProgram(args); |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 193 | } |
Chris Dalton | 86d4cfd | 2020-11-03 13:51:21 -0700 | [diff] [blame] | 194 | this->prePrepareFillTrianglesProgram(args, *isLinear); |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 195 | return true; |
| 196 | } |
| 197 | |
Chris Dalton | 1b6a43c | 2020-09-25 12:21:18 -0600 | [diff] [blame] | 198 | // Increments clockwise triangles and decrements counterclockwise. Used for "winding" fill. |
| 199 | constexpr static GrUserStencilSettings kIncrDecrStencil( |
| 200 | GrUserStencilSettings::StaticInitSeparate< |
| 201 | 0x0000, 0x0000, |
| 202 | GrUserStencilTest::kAlwaysIfInClip, GrUserStencilTest::kAlwaysIfInClip, |
| 203 | 0xffff, 0xffff, |
| 204 | GrUserStencilOp::kIncWrap, GrUserStencilOp::kDecWrap, |
| 205 | GrUserStencilOp::kKeep, GrUserStencilOp::kKeep, |
| 206 | 0xffff, 0xffff>()); |
| 207 | |
| 208 | // Inverts the bottom stencil bit. Used for "even/odd" fill. |
| 209 | constexpr static GrUserStencilSettings kInvertStencil( |
| 210 | GrUserStencilSettings::StaticInit< |
| 211 | 0x0000, |
| 212 | GrUserStencilTest::kAlwaysIfInClip, |
| 213 | 0xffff, |
| 214 | GrUserStencilOp::kInvert, |
| 215 | GrUserStencilOp::kKeep, |
| 216 | 0x0001>()); |
| 217 | |
| 218 | constexpr static const GrUserStencilSettings* stencil_pass_settings(SkPathFillType fillType) { |
| 219 | return (fillType == SkPathFillType::kWinding) ? &kIncrDecrStencil : &kInvertStencil; |
| 220 | } |
| 221 | |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 222 | void GrPathTessellateOp::prePrepareStencilTrianglesProgram(const PrePrepareArgs& args) { |
| 223 | SkASSERT(!fStencilTrianglesProgram); |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 224 | |
| 225 | this->prePreparePipelineForStencils(args); |
| 226 | |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 227 | auto* shader = args.fArena->make<GrStencilTriangleShader>(fViewMatrix); |
Chris Dalton | 1b6a43c | 2020-09-25 12:21:18 -0600 | [diff] [blame] | 228 | fStencilTrianglesProgram = GrPathShader::MakeProgramInfo( |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 229 | shader, args.fArena, args.fWriteView, fPipelineForStencils, *args.fDstProxyView, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 230 | args.fXferBarrierFlags, args.fColorLoadOp, stencil_pass_settings(fPath.getFillType()), |
| 231 | *args.fCaps); |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | template<typename ShaderType> |
| 235 | void GrPathTessellateOp::prePrepareStencilCubicsProgram(const PrePrepareArgs& args) { |
| 236 | SkASSERT(!fStencilCubicsProgram); |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 237 | |
| 238 | this->prePreparePipelineForStencils(args); |
| 239 | |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 240 | auto* shader = args.fArena->make<ShaderType>(fViewMatrix); |
Chris Dalton | 1b6a43c | 2020-09-25 12:21:18 -0600 | [diff] [blame] | 241 | fStencilCubicsProgram = GrPathShader::MakeProgramInfo( |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 242 | shader, args.fArena, args.fWriteView, fPipelineForStencils, *args.fDstProxyView, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 243 | args.fXferBarrierFlags, args.fColorLoadOp, stencil_pass_settings(fPath.getFillType()), |
| 244 | *args.fCaps); |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 245 | } |
| 246 | |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 247 | void GrPathTessellateOp::prePreparePipelineForStencils(const PrePrepareArgs& args) { |
| 248 | if (fPipelineForStencils) { |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 249 | return; |
| 250 | } |
| 251 | |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 252 | GrPipeline::InitArgs initArgs; |
| 253 | if (GrAAType::kNone != fAAType) { |
| 254 | initArgs.fInputFlags |= GrPipeline::InputFlags::kHWAntialias; |
| 255 | } |
| 256 | if (args.fCaps->wireframeSupport() && (OpFlags::kWireframe & fOpFlags)) { |
| 257 | initArgs.fInputFlags |= GrPipeline::InputFlags::kWireframe; |
| 258 | } |
| 259 | SkASSERT(SkPathFillType::kWinding == fPath.getFillType() || |
| 260 | SkPathFillType::kEvenOdd == fPath.getFillType()); |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 261 | initArgs.fCaps = args.fCaps; |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 262 | fPipelineForStencils = args.fArena->make<GrPipeline>( |
| 263 | initArgs, GrDisableColorXPFactory::MakeXferProcessor(), *args.fHardClip); |
| 264 | } |
| 265 | |
| 266 | // Allows non-zero stencil values to pass and write a color, and resets the stencil value back to |
| 267 | // zero; discards immediately on stencil values of zero. |
| 268 | // NOTE: It's ok to not check the clip here because the previous stencil pass will have only written |
| 269 | // to samples already inside the clip. |
| 270 | constexpr static GrUserStencilSettings kTestAndResetStencil( |
| 271 | GrUserStencilSettings::StaticInit< |
| 272 | 0x0000, |
| 273 | GrUserStencilTest::kNotEqual, |
| 274 | 0xffff, |
| 275 | GrUserStencilOp::kZero, |
| 276 | GrUserStencilOp::kKeep, |
| 277 | 0xffff>()); |
| 278 | |
Chris Dalton | 86d4cfd | 2020-11-03 13:51:21 -0700 | [diff] [blame] | 279 | void GrPathTessellateOp::prePrepareFillTrianglesProgram(const PrePrepareArgs& args, bool isLinear) { |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 280 | SkASSERT(!fFillTrianglesProgram); |
| 281 | |
| 282 | if (fOpFlags & OpFlags::kStencilOnly) { |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | // These are a twist on the standard red book stencil settings that allow us to fill the inner |
| 287 | // polygon directly to the final render target. At this point, the curves are already stencilled |
| 288 | // in. So if the stencil value is zero, then it means the path at our sample is not affected by |
| 289 | // any curves and we fill the path in directly. If the stencil value is nonzero, then we don't |
| 290 | // fill and instead continue the standard red book stencil process. |
| 291 | // |
| 292 | // NOTE: These settings are currently incompatible with a stencil clip. |
| 293 | constexpr static GrUserStencilSettings kFillOrIncrDecrStencil( |
| 294 | GrUserStencilSettings::StaticInitSeparate< |
| 295 | 0x0000, 0x0000, |
| 296 | GrUserStencilTest::kEqual, GrUserStencilTest::kEqual, |
| 297 | 0xffff, 0xffff, |
| 298 | GrUserStencilOp::kKeep, GrUserStencilOp::kKeep, |
| 299 | GrUserStencilOp::kIncWrap, GrUserStencilOp::kDecWrap, |
| 300 | 0xffff, 0xffff>()); |
| 301 | |
| 302 | constexpr static GrUserStencilSettings kFillOrInvertStencil( |
| 303 | GrUserStencilSettings::StaticInit< |
| 304 | 0x0000, |
| 305 | GrUserStencilTest::kEqual, |
| 306 | 0xffff, |
| 307 | GrUserStencilOp::kKeep, |
| 308 | GrUserStencilOp::kZero, |
| 309 | 0xffff>()); |
| 310 | |
| 311 | this->prePreparePipelineForFills(args); |
| 312 | |
| 313 | const GrUserStencilSettings* stencil; |
| 314 | if (fStencilTrianglesProgram) { |
| 315 | // The path was already stencilled. Here we just need to do a cover pass. |
| 316 | stencil = &kTestAndResetStencil; |
Chris Dalton | 86d4cfd | 2020-11-03 13:51:21 -0700 | [diff] [blame] | 317 | } else if (isLinear) { |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 318 | // There are no stencilled curves. We can ignore stencil and fill the path directly. |
| 319 | stencil = &GrUserStencilSettings::kUnused; |
| 320 | } else if (SkPathFillType::kWinding == fPath.getFillType()) { |
| 321 | // Fill in the path pixels not touched by curves, incr/decr stencil otherwise. |
| 322 | SkASSERT(!fPipelineForFills->hasStencilClip()); |
| 323 | stencil = &kFillOrIncrDecrStencil; |
| 324 | } else { |
| 325 | // Fill in the path pixels not touched by curves, invert stencil otherwise. |
| 326 | SkASSERT(!fPipelineForFills->hasStencilClip()); |
| 327 | stencil = &kFillOrInvertStencil; |
| 328 | } |
| 329 | |
| 330 | auto* fillTriangleShader = args.fArena->make<GrFillTriangleShader>(fViewMatrix, fColor); |
| 331 | fFillTrianglesProgram = GrPathShader::MakeProgramInfo( |
| 332 | fillTriangleShader, args.fArena, args.fWriteView, fPipelineForFills, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 333 | *args.fDstProxyView, args.fXferBarrierFlags, args.fColorLoadOp, stencil, *args.fCaps); |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | void GrPathTessellateOp::prePrepareFillCubicHullsProgram(const PrePrepareArgs& args) { |
| 337 | SkASSERT(!fFillPathProgram); |
| 338 | |
| 339 | if (fOpFlags & OpFlags::kStencilOnly) { |
| 340 | return; |
| 341 | } |
| 342 | |
| 343 | this->prePreparePipelineForFills(args); |
| 344 | |
| 345 | auto* fillCubicHullsShader = args.fArena->make<GrFillCubicHullShader>(fViewMatrix, fColor); |
| 346 | fFillPathProgram = GrPathShader::MakeProgramInfo( |
| 347 | fillCubicHullsShader, args.fArena, args.fWriteView, fPipelineForFills, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 348 | *args.fDstProxyView, args.fXferBarrierFlags, args.fColorLoadOp, &kTestAndResetStencil, |
| 349 | *args.fCaps); |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | void GrPathTessellateOp::prePrepareFillBoundingBoxProgram(const PrePrepareArgs& args) { |
| 353 | SkASSERT(!fFillPathProgram); |
| 354 | |
| 355 | if (fOpFlags & OpFlags::kStencilOnly) { |
| 356 | return; |
| 357 | } |
| 358 | |
| 359 | this->prePreparePipelineForFills(args); |
| 360 | |
| 361 | auto* fillBoundingBoxShader = args.fArena->make<GrFillBoundingBoxShader>(fViewMatrix, fColor, |
| 362 | fPath.getBounds()); |
| 363 | fFillPathProgram = GrPathShader::MakeProgramInfo( |
| 364 | fillBoundingBoxShader, args.fArena, args.fWriteView, fPipelineForFills, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 365 | *args.fDstProxyView, args.fXferBarrierFlags, args.fColorLoadOp, &kTestAndResetStencil, |
| 366 | *args.fCaps); |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | void GrPathTessellateOp::prePreparePipelineForFills(const PrePrepareArgs& args) { |
| 370 | SkASSERT(!(fOpFlags & OpFlags::kStencilOnly)); |
| 371 | |
| 372 | if (fPipelineForFills) { |
| 373 | return; |
| 374 | } |
| 375 | |
| 376 | auto pipelineFlags = GrPipeline::InputFlags::kNone; |
| 377 | if (GrAAType::kNone != fAAType) { |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 378 | if (args.fWriteView.asRenderTargetProxy()->numSamples() == 1) { |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 379 | // We are mixed sampled. We need to either enable conservative raster (preferred) or |
| 380 | // disable MSAA in order to avoid double blend artifacts. (Even if we disable MSAA for |
| 381 | // the cover geometry, the stencil test is still multisampled and will still produce |
| 382 | // smooth results.) |
| 383 | SkASSERT(GrAAType::kCoverage == fAAType); |
| 384 | if (args.fCaps->conservativeRasterSupport()) { |
| 385 | pipelineFlags |= GrPipeline::InputFlags::kHWAntialias; |
| 386 | pipelineFlags |= GrPipeline::InputFlags::kConservativeRaster; |
| 387 | } |
| 388 | } else { |
| 389 | // We are standard MSAA. Leave MSAA enabled for the cover geometry. |
| 390 | pipelineFlags |= GrPipeline::InputFlags::kHWAntialias; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | fPipelineForFills = GrSimpleMeshDrawOpHelper::CreatePipeline( |
Chris Dalton | b064334 | 2020-12-15 01:04:12 -0700 | [diff] [blame^] | 395 | args.fCaps, args.fArena, args.fWriteView.swizzle(), |
| 396 | (args.fClip) ? std::move(*args.fClip) : GrAppliedClip::Disabled(), *args.fDstProxyView, |
| 397 | std::move(fProcessors), pipelineFlags); |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | void GrPathTessellateOp::onPrepare(GrOpFlushState* flushState) { |
| 401 | int numVerbs = fPath.countVerbs(); |
| 402 | if (numVerbs <= 0) { |
| 403 | return; |
| 404 | } |
| 405 | |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 406 | if (!fPipelineForStencils && !fPipelineForFills) { |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 407 | // Nothing has been prePrepared yet. Do it now. |
| 408 | GrEagerDynamicVertexAllocator innerTriangleAllocator(flushState, &fTriangleBuffer, |
| 409 | &fBaseTriangleVertex); |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 410 | GrAppliedHardClip hardClip = GrAppliedHardClip(flushState->appliedHardClip()); |
| 411 | GrAppliedClip clip = flushState->detachAppliedClip(); |
| 412 | PrePrepareArgs args{flushState->allocator(), flushState->writeView(), &hardClip, |
| 413 | &clip, &flushState->dstProxyView(), |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 414 | flushState->renderPassBarriers(), flushState->colorLoadOp(), |
| 415 | &flushState->caps(), &innerTriangleAllocator}; |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 416 | this->prePreparePrograms(args); |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | if (fTriangleVertexCount != 0) { |
| 420 | // prePreparePrograms was able to generate an inner polygon triangulation. It will exist in |
| 421 | // either fOffThreadInnerTriangulation or fTriangleBuffer exclusively. |
| 422 | SkASSERT(SkToBool(fOffThreadInnerTriangulation) != SkToBool(fTriangleBuffer)); |
| 423 | if (fOffThreadInnerTriangulation) { |
| 424 | // DDL generated the triangle buffer data off thread. Copy it to GPU. |
| 425 | void* data = flushState->makeVertexSpace(sizeof(SkPoint), fTriangleVertexCount, |
| 426 | &fTriangleBuffer, &fBaseTriangleVertex); |
| 427 | memcpy(data, fOffThreadInnerTriangulation, fTriangleVertexCount * sizeof(SkPoint)); |
| 428 | } |
| 429 | if (fStencilCubicsProgram) { |
| 430 | // We always use indirect draws for inner-polygon-triangulation mode instead of |
| 431 | // tessellation. |
| 432 | SkASSERT(GrPrimitiveType::kPatches != |
| 433 | fStencilCubicsProgram->primProc().cast<GrStencilPathShader>().primitiveType()); |
| 434 | GrResolveLevelCounter resolveLevelCounter; |
| 435 | resolveLevelCounter.reset(fPath, fViewMatrix, kLinearizationIntolerance); |
| 436 | this->prepareIndirectOuterCubics(flushState, resolveLevelCounter); |
| 437 | } |
| 438 | return; |
| 439 | } |
| 440 | |
| 441 | SkASSERT(fStencilCubicsProgram); |
| 442 | const auto& stencilCubicsShader = fStencilCubicsProgram->primProc().cast<GrPathShader>(); |
| 443 | |
| 444 | if (stencilCubicsShader.primitiveType() != GrPrimitiveType::kPatches) { |
| 445 | // Outer cubics need indirect draws. |
| 446 | GrResolveLevelCounter resolveLevelCounter; |
| 447 | this->prepareMiddleOutTrianglesAndCubics(flushState, &resolveLevelCounter); |
| 448 | return; |
| 449 | } |
| 450 | |
| 451 | if (stencilCubicsShader.tessellationPatchVertexCount() == 4) { |
| 452 | // Triangles and tessellated curves will be drawn separately. |
| 453 | this->prepareMiddleOutTrianglesAndCubics(flushState); |
| 454 | return; |
| 455 | } |
| 456 | |
| 457 | // We are drawing tessellated wedges. |
| 458 | SkASSERT(stencilCubicsShader.tessellationPatchVertexCount() == 5); |
| 459 | this->prepareTessellatedCubicWedges(flushState); |
| 460 | } |
| 461 | |
Chris Dalton | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 462 | void GrPathTessellateOp::prepareMiddleOutTrianglesAndCubics( |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 463 | GrMeshDrawOp::Target* target, GrResolveLevelCounter* resolveLevelCounter) { |
| 464 | SkASSERT(fStencilCubicsProgram); |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 465 | SkASSERT(!fTriangleBuffer); |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 466 | SkASSERT(!fFillTrianglesProgram); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 467 | SkASSERT(!fCubicBuffer); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 468 | SkASSERT(!fIndirectDrawBuffer); |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 469 | SkASSERT(fTriangleVertexCount == 0); |
| 470 | SkASSERT(fCubicVertexCount == 0); |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 471 | |
Chris Dalton | f5132a0 | 2020-04-27 23:40:03 -0600 | [diff] [blame] | 472 | // No initial moveTo, plus an implicit close at the end; n-2 triangles fill an n-gon. |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 473 | int maxInnerTriangles = fPath.countVerbs() - 1; |
| 474 | int maxCubics = fPath.countVerbs(); |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 475 | |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 476 | SkPoint* vertexData; |
| 477 | int vertexAdvancePerTriangle; |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 478 | if (!fStencilTrianglesProgram) { |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 479 | // Allocate the triangles as 4-point instances at the beginning of the cubic buffer. |
| 480 | SkASSERT(resolveLevelCounter); |
| 481 | vertexAdvancePerTriangle = 4; |
| 482 | int baseTriangleInstance; |
| 483 | vertexData = static_cast<SkPoint*>(target->makeVertexSpace( |
| 484 | sizeof(SkPoint) * 4, maxInnerTriangles + maxCubics, &fCubicBuffer, |
| 485 | &baseTriangleInstance)); |
| 486 | fBaseCubicVertex = baseTriangleInstance * 4; |
| 487 | } else { |
| 488 | // Allocate the triangles as normal 3-point instances in the triangle buffer. |
| 489 | vertexAdvancePerTriangle = 3; |
| 490 | vertexData = static_cast<SkPoint*>(target->makeVertexSpace( |
| 491 | sizeof(SkPoint), maxInnerTriangles * 3, &fTriangleBuffer, &fBaseTriangleVertex)); |
| 492 | } |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 493 | if (!vertexData) { |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 494 | return; |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 495 | } |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 496 | |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 497 | GrVectorXform xform(fViewMatrix); |
| 498 | GrMiddleOutPolygonTriangulator middleOut(vertexData, vertexAdvancePerTriangle, |
| 499 | fPath.countVerbs()); |
| 500 | if (resolveLevelCounter) { |
| 501 | resolveLevelCounter->reset(); |
| 502 | } |
| 503 | int numCountedCurves = 0; |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 504 | for (auto [verb, pts, w] : SkPathPriv::Iterate(fPath)) { |
| 505 | switch (verb) { |
| 506 | case SkPathVerb::kMove: |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 507 | middleOut.closeAndMove(pts[0]); |
Chris Dalton | f5132a0 | 2020-04-27 23:40:03 -0600 | [diff] [blame] | 508 | break; |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 509 | case SkPathVerb::kLine: |
| 510 | middleOut.pushVertex(pts[1]); |
| 511 | break; |
Chris Dalton | b27f39c | 2020-11-23 09:30:24 -0700 | [diff] [blame] | 512 | case SkPathVerb::kConic: |
| 513 | // We use the same quadratic formula for conics, ignoring w. This appears to be an |
| 514 | // upper bound on what the actual number of subdivisions would have been. |
| 515 | [[fallthrough]]; |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 516 | case SkPathVerb::kQuad: |
| 517 | middleOut.pushVertex(pts[2]); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 518 | if (resolveLevelCounter) { |
Chris Dalton | b27f39c | 2020-11-23 09:30:24 -0700 | [diff] [blame] | 519 | resolveLevelCounter->countInstance(GrWangsFormula::quadratic_log2( |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 520 | kLinearizationIntolerance, pts, xform)); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 521 | break; |
| 522 | } |
| 523 | ++numCountedCurves; |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 524 | break; |
| 525 | case SkPathVerb::kCubic: |
| 526 | middleOut.pushVertex(pts[3]); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 527 | if (resolveLevelCounter) { |
Chris Dalton | b27f39c | 2020-11-23 09:30:24 -0700 | [diff] [blame] | 528 | resolveLevelCounter->countInstance(GrWangsFormula::cubic_log2( |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 529 | kLinearizationIntolerance, pts, xform)); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 530 | break; |
| 531 | } |
| 532 | ++numCountedCurves; |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 533 | break; |
| 534 | case SkPathVerb::kClose: |
Chris Dalton | f5132a0 | 2020-04-27 23:40:03 -0600 | [diff] [blame] | 535 | middleOut.close(); |
| 536 | break; |
Chris Dalton | f5132a0 | 2020-04-27 23:40:03 -0600 | [diff] [blame] | 537 | } |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 538 | } |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 539 | int triangleCount = middleOut.close(); |
| 540 | SkASSERT(triangleCount <= maxInnerTriangles); |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 541 | |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 542 | if (!fStencilTrianglesProgram) { |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 543 | SkASSERT(resolveLevelCounter); |
Chris Dalton | b27f39c | 2020-11-23 09:30:24 -0700 | [diff] [blame] | 544 | int totalInstanceCount = triangleCount + resolveLevelCounter->totalInstanceCount(); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 545 | SkASSERT(vertexAdvancePerTriangle == 4); |
| 546 | target->putBackVertices(maxInnerTriangles + maxCubics - totalInstanceCount, |
| 547 | sizeof(SkPoint) * 4); |
| 548 | if (totalInstanceCount) { |
| 549 | this->prepareIndirectOuterCubicsAndTriangles(target, *resolveLevelCounter, vertexData, |
| 550 | triangleCount); |
| 551 | } |
| 552 | } else { |
| 553 | SkASSERT(vertexAdvancePerTriangle == 3); |
| 554 | target->putBackVertices(maxInnerTriangles - triangleCount, sizeof(SkPoint) * 3); |
| 555 | fTriangleVertexCount = triangleCount * 3; |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 556 | if (resolveLevelCounter) { |
| 557 | this->prepareIndirectOuterCubics(target, *resolveLevelCounter); |
| 558 | } else { |
| 559 | this->prepareTessellatedOuterCubics(target, numCountedCurves); |
| 560 | } |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 561 | } |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 562 | } |
| 563 | |
Chris Dalton | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 564 | void GrPathTessellateOp::prepareIndirectOuterCubics( |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 565 | GrMeshDrawOp::Target* target, const GrResolveLevelCounter& resolveLevelCounter) { |
Chris Dalton | b27f39c | 2020-11-23 09:30:24 -0700 | [diff] [blame] | 566 | SkASSERT(resolveLevelCounter.totalInstanceCount() >= 0); |
| 567 | if (resolveLevelCounter.totalInstanceCount() == 0) { |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 568 | return; |
| 569 | } |
| 570 | // Allocate a buffer to store the cubic data. |
| 571 | SkPoint* cubicData; |
| 572 | int baseInstance; |
| 573 | cubicData = static_cast<SkPoint*>(target->makeVertexSpace( |
Chris Dalton | b27f39c | 2020-11-23 09:30:24 -0700 | [diff] [blame] | 574 | sizeof(SkPoint) * 4, resolveLevelCounter.totalInstanceCount(), &fCubicBuffer, |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 575 | &baseInstance)); |
| 576 | if (!cubicData) { |
| 577 | return; |
| 578 | } |
| 579 | fBaseCubicVertex = baseInstance * 4; |
| 580 | this->prepareIndirectOuterCubicsAndTriangles(target, resolveLevelCounter, cubicData, |
| 581 | /*numTrianglesAtBeginningOfData=*/0); |
| 582 | } |
| 583 | |
Chris Dalton | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 584 | void GrPathTessellateOp::prepareIndirectOuterCubicsAndTriangles( |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 585 | GrMeshDrawOp::Target* target, const GrResolveLevelCounter& resolveLevelCounter, |
| 586 | SkPoint* cubicData, int numTrianglesAtBeginningOfData) { |
Chris Dalton | 1443c9d | 2020-05-27 09:43:34 -0600 | [diff] [blame] | 587 | SkASSERT(target->caps().drawInstancedSupport()); |
Chris Dalton | b27f39c | 2020-11-23 09:30:24 -0700 | [diff] [blame] | 588 | SkASSERT(numTrianglesAtBeginningOfData + resolveLevelCounter.totalInstanceCount() > 0); |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 589 | SkASSERT(fStencilCubicsProgram); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 590 | SkASSERT(cubicData); |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 591 | SkASSERT(fCubicVertexCount == 0); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 592 | |
Chris Dalton | 1443c9d | 2020-05-27 09:43:34 -0600 | [diff] [blame] | 593 | fIndirectIndexBuffer = GrMiddleOutCubicShader::FindOrMakeMiddleOutIndexBuffer( |
| 594 | target->resourceProvider()); |
| 595 | if (!fIndirectIndexBuffer) { |
| 596 | return; |
| 597 | } |
| 598 | |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 599 | // Here we treat fCubicBuffer as an instance buffer. It should have been prepared with the base |
| 600 | // vertex on an instance boundary in order to accommodate this. |
| 601 | SkASSERT(fBaseCubicVertex % 4 == 0); |
| 602 | int baseInstance = fBaseCubicVertex >> 2; |
| 603 | |
| 604 | // Start preparing the indirect draw buffer. |
Chris Dalton | b27f39c | 2020-11-23 09:30:24 -0700 | [diff] [blame] | 605 | fIndirectDrawCount = resolveLevelCounter.totalIndirectDrawCount(); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 606 | if (numTrianglesAtBeginningOfData) { |
| 607 | ++fIndirectDrawCount; // Add an indirect draw for the triangles at the beginning. |
| 608 | } |
| 609 | |
| 610 | // Allocate space for the GrDrawIndexedIndirectCommand structs. |
| 611 | GrDrawIndexedIndirectCommand* indirectData = target->makeDrawIndexedIndirectSpace( |
| 612 | fIndirectDrawCount, &fIndirectDrawBuffer, &fIndirectDrawOffset); |
| 613 | if (!indirectData) { |
| 614 | SkASSERT(!fIndirectDrawBuffer); |
| 615 | return; |
| 616 | } |
| 617 | |
| 618 | // Fill out the GrDrawIndexedIndirectCommand structs and determine the starting instance data |
| 619 | // location at each resolve level. |
| 620 | SkPoint* instanceLocations[kMaxResolveLevel + 1]; |
| 621 | int indirectIdx = 0; |
| 622 | int runningInstanceCount = 0; |
| 623 | if (numTrianglesAtBeginningOfData) { |
| 624 | // The caller has already packed "triangleInstanceCount" triangles into 4-point instances |
| 625 | // at the beginning of the instance buffer. Add a special-case indirect draw here that will |
| 626 | // emit the triangles [P0, P1, P2] from these 4-point instances. |
| 627 | indirectData[0] = GrMiddleOutCubicShader::MakeDrawTrianglesIndirectCmd( |
| 628 | numTrianglesAtBeginningOfData, baseInstance); |
| 629 | indirectIdx = 1; |
| 630 | runningInstanceCount = numTrianglesAtBeginningOfData; |
| 631 | } |
| 632 | for (int resolveLevel = 1; resolveLevel <= kMaxResolveLevel; ++resolveLevel) { |
Chris Dalton | a6858ae | 2020-06-25 07:04:33 -0600 | [diff] [blame] | 633 | int instanceCountAtCurrLevel = resolveLevelCounter[resolveLevel]; |
| 634 | if (!instanceCountAtCurrLevel) { |
| 635 | SkDEBUGCODE(instanceLocations[resolveLevel] = nullptr;) |
| 636 | continue; |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 637 | } |
Chris Dalton | a6858ae | 2020-06-25 07:04:33 -0600 | [diff] [blame] | 638 | instanceLocations[resolveLevel] = cubicData + runningInstanceCount * 4; |
| 639 | indirectData[indirectIdx++] = GrMiddleOutCubicShader::MakeDrawCubicsIndirectCmd( |
| 640 | resolveLevel, instanceCountAtCurrLevel, baseInstance + runningInstanceCount); |
| 641 | runningInstanceCount += instanceCountAtCurrLevel; |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | #ifdef SK_DEBUG |
| 645 | SkASSERT(indirectIdx == fIndirectDrawCount); |
| 646 | SkASSERT(runningInstanceCount == numTrianglesAtBeginningOfData + |
Chris Dalton | b27f39c | 2020-11-23 09:30:24 -0700 | [diff] [blame] | 647 | resolveLevelCounter.totalInstanceCount()); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 648 | SkASSERT(fIndirectDrawCount > 0); |
| 649 | |
| 650 | SkPoint* endLocations[kMaxResolveLevel + 1]; |
Chris Dalton | a6858ae | 2020-06-25 07:04:33 -0600 | [diff] [blame] | 651 | int lastResolveLevel = 0; |
| 652 | for (int resolveLevel = 1; resolveLevel <= kMaxResolveLevel; ++resolveLevel) { |
| 653 | if (!instanceLocations[resolveLevel]) { |
| 654 | endLocations[resolveLevel] = nullptr; |
| 655 | continue; |
| 656 | } |
| 657 | endLocations[lastResolveLevel] = instanceLocations[resolveLevel]; |
| 658 | lastResolveLevel = resolveLevel; |
| 659 | } |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 660 | int totalInstanceCount = numTrianglesAtBeginningOfData + |
Chris Dalton | b27f39c | 2020-11-23 09:30:24 -0700 | [diff] [blame] | 661 | resolveLevelCounter.totalInstanceCount(); |
Chris Dalton | a6858ae | 2020-06-25 07:04:33 -0600 | [diff] [blame] | 662 | endLocations[lastResolveLevel] = cubicData + totalInstanceCount * 4; |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 663 | #endif |
| 664 | |
| 665 | fCubicVertexCount = numTrianglesAtBeginningOfData * 4; |
| 666 | |
Chris Dalton | b27f39c | 2020-11-23 09:30:24 -0700 | [diff] [blame] | 667 | if (resolveLevelCounter.totalInstanceCount()) { |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 668 | GrVectorXform xform(fViewMatrix); |
| 669 | for (auto [verb, pts, w] : SkPathPriv::Iterate(fPath)) { |
| 670 | int level; |
| 671 | switch (verb) { |
| 672 | default: |
| 673 | continue; |
Chris Dalton | b27f39c | 2020-11-23 09:30:24 -0700 | [diff] [blame] | 674 | case SkPathVerb::kConic: |
| 675 | // We use the same quadratic formula for conics, ignoring w. This appears to be |
| 676 | // an upper bound on what the actual number of subdivisions would have been. |
| 677 | [[fallthrough]]; |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 678 | case SkPathVerb::kQuad: |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 679 | level = GrWangsFormula::quadratic_log2(kLinearizationIntolerance, pts, xform); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 680 | break; |
| 681 | case SkPathVerb::kCubic: |
Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 682 | level = GrWangsFormula::cubic_log2(kLinearizationIntolerance, pts, xform); |
Chris Dalton | b27f39c | 2020-11-23 09:30:24 -0700 | [diff] [blame] | 683 | break; |
| 684 | } |
| 685 | if (level == 0) { |
| 686 | continue; |
| 687 | } |
| 688 | level = std::min(level, kMaxResolveLevel); |
| 689 | switch (verb) { |
| 690 | case SkPathVerb::kQuad: |
| 691 | GrPathUtils::convertQuadToCubic(pts, instanceLocations[level]); |
| 692 | break; |
| 693 | case SkPathVerb::kCubic: |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 694 | memcpy(instanceLocations[level], pts, sizeof(SkPoint) * 4); |
| 695 | break; |
Chris Dalton | b27f39c | 2020-11-23 09:30:24 -0700 | [diff] [blame] | 696 | case SkPathVerb::kConic: |
| 697 | GrPathShader::WriteConicPatch(pts, *w, instanceLocations[level]); |
| 698 | break; |
| 699 | default: |
| 700 | SkUNREACHABLE; |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 701 | } |
| 702 | instanceLocations[level] += 4; |
| 703 | fCubicVertexCount += 4; |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | #ifdef SK_DEBUG |
| 708 | for (int i = 1; i <= kMaxResolveLevel; ++i) { |
| 709 | SkASSERT(instanceLocations[i] == endLocations[i]); |
| 710 | } |
| 711 | SkASSERT(fCubicVertexCount == (numTrianglesAtBeginningOfData + |
Chris Dalton | b27f39c | 2020-11-23 09:30:24 -0700 | [diff] [blame] | 712 | resolveLevelCounter.totalInstanceCount()) * 4); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 713 | #endif |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 714 | } |
| 715 | |
Chris Dalton | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 716 | void GrPathTessellateOp::prepareTessellatedOuterCubics(GrMeshDrawOp::Target* target, |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 717 | int numCountedCurves) { |
Chris Dalton | 1443c9d | 2020-05-27 09:43:34 -0600 | [diff] [blame] | 718 | SkASSERT(target->caps().shaderCaps()->tessellationSupport()); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 719 | SkASSERT(numCountedCurves >= 0); |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 720 | SkASSERT(!fCubicBuffer); |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 721 | SkASSERT(fStencilCubicsProgram); |
| 722 | SkASSERT(fCubicVertexCount == 0); |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 723 | |
| 724 | if (numCountedCurves == 0) { |
| 725 | return; |
| 726 | } |
| 727 | |
Chris Dalton | 2f2d81c | 2020-05-13 17:57:37 -0600 | [diff] [blame] | 728 | auto* vertexData = static_cast<SkPoint*>(target->makeVertexSpace( |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 729 | sizeof(SkPoint), numCountedCurves * 4, &fCubicBuffer, &fBaseCubicVertex)); |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 730 | if (!vertexData) { |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 731 | return; |
| 732 | } |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 733 | |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 734 | for (auto [verb, pts, w] : SkPathPriv::Iterate(fPath)) { |
| 735 | switch (verb) { |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 736 | default: |
| 737 | continue; |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 738 | case SkPathVerb::kQuad: |
| 739 | SkASSERT(fCubicVertexCount < numCountedCurves * 4); |
Chris Dalton | 753c1b3 | 2020-11-02 12:00:19 -0700 | [diff] [blame] | 740 | GrPathUtils::convertQuadToCubic(pts, vertexData + fCubicVertexCount); |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 741 | break; |
| 742 | case SkPathVerb::kCubic: |
| 743 | SkASSERT(fCubicVertexCount < numCountedCurves * 4); |
| 744 | memcpy(vertexData + fCubicVertexCount, pts, sizeof(SkPoint) * 4); |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 745 | break; |
Chris Dalton | b27f39c | 2020-11-23 09:30:24 -0700 | [diff] [blame] | 746 | case SkPathVerb::kConic: |
| 747 | SkASSERT(fCubicVertexCount < numCountedCurves * 4); |
| 748 | GrPathShader::WriteConicPatch(pts, *w, vertexData + fCubicVertexCount); |
| 749 | break; |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 750 | } |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 751 | fCubicVertexCount += 4; |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 752 | } |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 753 | SkASSERT(fCubicVertexCount == numCountedCurves * 4); |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 754 | } |
| 755 | |
Chris Dalton | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 756 | void GrPathTessellateOp::prepareTessellatedCubicWedges(GrMeshDrawOp::Target* target) { |
Chris Dalton | 1443c9d | 2020-05-27 09:43:34 -0600 | [diff] [blame] | 757 | SkASSERT(target->caps().shaderCaps()->tessellationSupport()); |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 758 | SkASSERT(!fCubicBuffer); |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 759 | SkASSERT(fStencilCubicsProgram); |
| 760 | SkASSERT(fCubicVertexCount == 0); |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 761 | |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 762 | // No initial moveTo, one wedge per verb, plus an implicit close at the end. |
| 763 | // Each wedge has 5 vertices. |
| 764 | int maxVertices = (fPath.countVerbs() + 1) * 5; |
| 765 | |
Chris Dalton | 2f2d81c | 2020-05-13 17:57:37 -0600 | [diff] [blame] | 766 | GrEagerDynamicVertexAllocator vertexAlloc(target, &fCubicBuffer, &fBaseCubicVertex); |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 767 | auto* vertexData = vertexAlloc.lock<SkPoint>(maxVertices); |
| 768 | if (!vertexData) { |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 769 | return; |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 770 | } |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 771 | |
| 772 | GrMidpointContourParser parser(fPath); |
| 773 | while (parser.parseNextContour()) { |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 774 | SkPoint midpoint = parser.currentMidpoint(); |
| 775 | SkPoint startPoint = {0, 0}; |
| 776 | SkPoint lastPoint = startPoint; |
| 777 | for (auto [verb, pts, w] : parser.currentContour()) { |
| 778 | switch (verb) { |
| 779 | case SkPathVerb::kMove: |
| 780 | startPoint = lastPoint = pts[0]; |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 781 | continue; |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 782 | case SkPathVerb::kClose: |
| 783 | continue; // Ignore. We can assume an implicit close at the end. |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 784 | case SkPathVerb::kLine: |
Chris Dalton | 753c1b3 | 2020-11-02 12:00:19 -0700 | [diff] [blame] | 785 | GrPathUtils::convertLineToCubic(pts[0], pts[1], vertexData + fCubicVertexCount); |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 786 | lastPoint = pts[1]; |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 787 | break; |
| 788 | case SkPathVerb::kQuad: |
Chris Dalton | 753c1b3 | 2020-11-02 12:00:19 -0700 | [diff] [blame] | 789 | GrPathUtils::convertQuadToCubic(pts, vertexData + fCubicVertexCount); |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 790 | lastPoint = pts[2]; |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 791 | break; |
| 792 | case SkPathVerb::kCubic: |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 793 | memcpy(vertexData + fCubicVertexCount, pts, sizeof(SkPoint) * 4); |
| 794 | lastPoint = pts[3]; |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 795 | break; |
| 796 | case SkPathVerb::kConic: |
Chris Dalton | b27f39c | 2020-11-23 09:30:24 -0700 | [diff] [blame] | 797 | GrPathShader::WriteConicPatch(pts, *w, vertexData + fCubicVertexCount); |
| 798 | lastPoint = pts[2]; |
| 799 | break; |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 800 | } |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 801 | vertexData[fCubicVertexCount + 4] = midpoint; |
| 802 | fCubicVertexCount += 5; |
| 803 | } |
| 804 | if (lastPoint != startPoint) { |
Chris Dalton | 753c1b3 | 2020-11-02 12:00:19 -0700 | [diff] [blame] | 805 | GrPathUtils::convertLineToCubic(lastPoint, startPoint, vertexData + fCubicVertexCount); |
Chris Dalton | f7a3307 | 2020-05-01 10:33:08 -0600 | [diff] [blame] | 806 | vertexData[fCubicVertexCount + 4] = midpoint; |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 807 | fCubicVertexCount += 5; |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 808 | } |
| 809 | } |
| 810 | |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 811 | vertexAlloc.unlock(fCubicVertexCount); |
Chris Dalton | 42915c2 | 2020-04-22 16:24:43 -0600 | [diff] [blame] | 812 | } |
| 813 | |
Chris Dalton | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 814 | void GrPathTessellateOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) { |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 815 | this->drawStencilPass(flushState); |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 816 | this->drawCoverPass(flushState); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Chris Dalton | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 819 | void GrPathTessellateOp::drawStencilPass(GrOpFlushState* flushState) { |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 820 | if (fStencilTrianglesProgram && fTriangleVertexCount > 0) { |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 821 | SkASSERT(fTriangleBuffer); |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 822 | flushState->bindPipelineAndScissorClip(*fStencilTrianglesProgram, this->bounds()); |
Chris Dalton | 8513867 | 2020-07-24 08:47:03 -0600 | [diff] [blame] | 823 | flushState->bindBuffers(nullptr, nullptr, fTriangleBuffer); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 824 | flushState->draw(fTriangleVertexCount, fBaseTriangleVertex); |
Chris Dalton | f9aea7f | 2020-01-21 11:19:26 -0700 | [diff] [blame] | 825 | } |
| 826 | |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 827 | if (fCubicVertexCount > 0) { |
| 828 | SkASSERT(fStencilCubicsProgram); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 829 | SkASSERT(fCubicBuffer); |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 830 | flushState->bindPipelineAndScissorClip(*fStencilCubicsProgram, this->bounds()); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 831 | if (fIndirectDrawBuffer) { |
Chris Dalton | 1443c9d | 2020-05-27 09:43:34 -0600 | [diff] [blame] | 832 | SkASSERT(fIndirectIndexBuffer); |
Greg Daniel | 426274b | 2020-07-20 11:37:38 -0400 | [diff] [blame] | 833 | flushState->bindBuffers(fIndirectIndexBuffer, fCubicBuffer, nullptr); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 834 | flushState->drawIndexedIndirect(fIndirectDrawBuffer.get(), fIndirectDrawOffset, |
| 835 | fIndirectDrawCount); |
| 836 | } else { |
Greg Daniel | 426274b | 2020-07-20 11:37:38 -0400 | [diff] [blame] | 837 | flushState->bindBuffers(nullptr, nullptr, fCubicBuffer); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 838 | flushState->draw(fCubicVertexCount, fBaseCubicVertex); |
| 839 | if (flushState->caps().requiresManualFBBarrierAfterTessellatedStencilDraw()) { |
| 840 | flushState->gpu()->insertManualFramebufferBarrier(); // http://skbug.com/9739 |
| 841 | } |
| 842 | } |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 843 | } |
| 844 | } |
| 845 | |
Chris Dalton | 078f875 | 2020-07-30 19:50:46 -0600 | [diff] [blame] | 846 | void GrPathTessellateOp::drawCoverPass(GrOpFlushState* flushState) { |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 847 | if (fFillTrianglesProgram) { |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 848 | SkASSERT(fTriangleBuffer); |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 849 | SkASSERT(fTriangleVertexCount > 0); |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 850 | |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 851 | // We have a triangulation of the path's inner polygon. This is the fast path. Fill those |
| 852 | // triangles directly to the screen. |
| 853 | flushState->bindPipelineAndScissorClip(*fFillTrianglesProgram, this->bounds()); |
| 854 | flushState->bindTextures(fFillTrianglesProgram->primProc(), nullptr, *fPipelineForFills); |
Greg Daniel | 426274b | 2020-07-20 11:37:38 -0400 | [diff] [blame] | 855 | flushState->bindBuffers(nullptr, nullptr, fTriangleBuffer); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 856 | flushState->draw(fTriangleVertexCount, fBaseTriangleVertex); |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 857 | |
Chris Dalton | e74cebe | 2020-09-24 09:32:54 -0600 | [diff] [blame] | 858 | if (fCubicVertexCount > 0) { |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 859 | SkASSERT(fFillPathProgram); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 860 | SkASSERT(fCubicBuffer); |
| 861 | |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 862 | // At this point, every pixel is filled in except the ones touched by curves. |
| 863 | // fFillPathProgram will issue a final cover pass over the curves by drawing their |
| 864 | // convex hulls. This will fill in any remaining samples and reset the stencil buffer. |
| 865 | flushState->bindPipelineAndScissorClip(*fFillPathProgram, this->bounds()); |
| 866 | flushState->bindTextures(fFillPathProgram->primProc(), nullptr, *fPipelineForFills); |
Chris Dalton | 04f9cda | 2020-04-23 10:04:25 -0600 | [diff] [blame] | 867 | |
| 868 | // Here we treat fCubicBuffer as an instance buffer. It should have been prepared with |
| 869 | // the base vertex on an instance boundary in order to accommodate this. |
| 870 | SkASSERT((fCubicVertexCount % 4) == 0); |
| 871 | SkASSERT((fBaseCubicVertex % 4) == 0); |
Greg Daniel | 426274b | 2020-07-20 11:37:38 -0400 | [diff] [blame] | 872 | flushState->bindBuffers(nullptr, fCubicBuffer, nullptr); |
Chris Dalton | b5391d9 | 2020-05-24 14:55:54 -0600 | [diff] [blame] | 873 | flushState->drawInstanced(fCubicVertexCount >> 2, fBaseCubicVertex >> 2, 4, 0); |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 874 | } |
Chris Dalton | 5e1545f | 2020-09-25 16:24:03 -0600 | [diff] [blame] | 875 | } else if (fFillPathProgram) { |
| 876 | // There are no triangles to fill. Just draw a bounding box. |
| 877 | flushState->bindPipelineAndScissorClip(*fFillPathProgram, this->bounds()); |
| 878 | flushState->bindTextures(fFillPathProgram->primProc(), nullptr, *fPipelineForFills); |
| 879 | flushState->bindBuffers(nullptr, nullptr, nullptr); |
| 880 | flushState->draw(4, 0); |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 881 | } |
Chris Dalton | b832ce6 | 2020-01-06 19:49:37 -0700 | [diff] [blame] | 882 | } |