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