Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. |
| 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/ccpr/GrCCFiller.h" |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkPath.h" |
| 11 | #include "include/core/SkPoint.h" |
| 12 | #include "src/core/SkMathPriv.h" |
| 13 | #include "src/core/SkPathPriv.h" |
| 14 | #include "src/gpu/GrCaps.h" |
| 15 | #include "src/gpu/GrGpuCommandBuffer.h" |
| 16 | #include "src/gpu/GrOnFlushResourceProvider.h" |
| 17 | #include "src/gpu/GrOpFlushState.h" |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 18 | #include <stdlib.h> |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 19 | |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 20 | using TriPointInstance = GrCCCoverageProcessor::TriPointInstance; |
| 21 | using QuadPointInstance = GrCCCoverageProcessor::QuadPointInstance; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 22 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame^] | 23 | GrCCFiller::GrCCFiller(Algorithm algorithm, int numPaths, int numSkPoints, int numSkVerbs, |
| 24 | int numConicWeights) |
| 25 | : fAlgorithm(algorithm) |
| 26 | , fGeometry(numSkPoints, numSkVerbs, numConicWeights) |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 27 | , fPathInfos(numPaths) |
Chris Dalton | 3917b1e | 2018-05-09 00:40:52 -0600 | [diff] [blame] | 28 | , fScissorSubBatches(numPaths) |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 29 | , fTotalPrimitiveCounts{PrimitiveTallies(), PrimitiveTallies()} { |
| 30 | // Batches decide what to draw by looking where the previous one ended. Define initial batches |
| 31 | // that "end" at the beginning of the data. These will not be drawn, but will only be be read by |
| 32 | // the first actual batch. |
| 33 | fScissorSubBatches.push_back() = {PrimitiveTallies(), SkIRect::MakeEmpty()}; |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 34 | fBatches.push_back() = {PrimitiveTallies(), fScissorSubBatches.count(), PrimitiveTallies()}; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 35 | } |
| 36 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 37 | void GrCCFiller::parseDeviceSpaceFill(const SkPath& path, const SkPoint* deviceSpacePts, |
| 38 | GrScissorTest scissorTest, const SkIRect& clippedDevIBounds, |
| 39 | const SkIVector& devToAtlasOffset) { |
| 40 | SkASSERT(!fInstanceBuffer); // Can't call after prepareToDraw(). |
| 41 | SkASSERT(!path.isEmpty()); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 42 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 43 | int currPathPointsIdx = fGeometry.points().count(); |
| 44 | int currPathVerbsIdx = fGeometry.verbs().count(); |
| 45 | PrimitiveTallies currPathPrimitiveCounts = PrimitiveTallies(); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 46 | |
| 47 | fGeometry.beginPath(); |
| 48 | |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 49 | const float* conicWeights = SkPathPriv::ConicWeightData(path); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 50 | int ptsIdx = 0; |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 51 | int conicWeightsIdx = 0; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 52 | bool insideContour = false; |
| 53 | |
| 54 | for (SkPath::Verb verb : SkPathPriv::Verbs(path)) { |
| 55 | switch (verb) { |
| 56 | case SkPath::kMove_Verb: |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 57 | if (insideContour) { |
| 58 | currPathPrimitiveCounts += fGeometry.endContour(); |
| 59 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 60 | fGeometry.beginContour(deviceSpacePts[ptsIdx]); |
| 61 | ++ptsIdx; |
| 62 | insideContour = true; |
| 63 | continue; |
| 64 | case SkPath::kClose_Verb: |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 65 | if (insideContour) { |
| 66 | currPathPrimitiveCounts += fGeometry.endContour(); |
| 67 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 68 | insideContour = false; |
| 69 | continue; |
| 70 | case SkPath::kLine_Verb: |
Chris Dalton | 6f5e77a | 2018-04-23 21:14:42 -0600 | [diff] [blame] | 71 | fGeometry.lineTo(&deviceSpacePts[ptsIdx - 1]); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 72 | ++ptsIdx; |
| 73 | continue; |
| 74 | case SkPath::kQuad_Verb: |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 75 | fGeometry.quadraticTo(&deviceSpacePts[ptsIdx - 1]); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 76 | ptsIdx += 2; |
| 77 | continue; |
| 78 | case SkPath::kCubic_Verb: |
Chris Dalton | 7ca3b7b | 2018-04-10 00:21:19 -0600 | [diff] [blame] | 79 | fGeometry.cubicTo(&deviceSpacePts[ptsIdx - 1]); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 80 | ptsIdx += 3; |
| 81 | continue; |
| 82 | case SkPath::kConic_Verb: |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 83 | fGeometry.conicTo(&deviceSpacePts[ptsIdx - 1], conicWeights[conicWeightsIdx]); |
| 84 | ptsIdx += 2; |
| 85 | ++conicWeightsIdx; |
| 86 | continue; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 87 | default: |
| 88 | SK_ABORT("Unexpected path verb."); |
| 89 | } |
| 90 | } |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 91 | SkASSERT(ptsIdx == path.countPoints()); |
| 92 | SkASSERT(conicWeightsIdx == SkPathPriv::ConicWeightCnt(path)); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 93 | |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 94 | if (insideContour) { |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 95 | currPathPrimitiveCounts += fGeometry.endContour(); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 96 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 97 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 98 | fPathInfos.emplace_back(scissorTest, devToAtlasOffset); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 99 | |
| 100 | // Tessellate fans from very large and/or simple paths, in order to reduce overdraw. |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 101 | int numVerbs = fGeometry.verbs().count() - currPathVerbsIdx - 1; |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 102 | int64_t tessellationWork = (int64_t)numVerbs * (32 - SkCLZ(numVerbs)); // N log N. |
| 103 | int64_t fanningWork = (int64_t)clippedDevIBounds.height() * clippedDevIBounds.width(); |
| 104 | if (tessellationWork * (50*50) + (100*100) < fanningWork) { // Don't tessellate under 100x100. |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame^] | 105 | fPathInfos.back().tessellateFan( |
| 106 | fAlgorithm, path, fGeometry, currPathVerbsIdx, currPathPointsIdx, clippedDevIBounds, |
| 107 | &currPathPrimitiveCounts); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 108 | } |
| 109 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 110 | fTotalPrimitiveCounts[(int)scissorTest] += currPathPrimitiveCounts; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 111 | |
Chris Dalton | 916c498 | 2018-08-15 00:53:25 -0600 | [diff] [blame] | 112 | if (GrScissorTest::kEnabled == scissorTest) { |
| 113 | fScissorSubBatches.push_back() = {fTotalPrimitiveCounts[(int)GrScissorTest::kEnabled], |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 114 | clippedDevIBounds.makeOffset(devToAtlasOffset.fX, |
| 115 | devToAtlasOffset.fY)}; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 116 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame^] | 119 | void GrCCFiller::PathInfo::tessellateFan( |
| 120 | Algorithm algorithm, const SkPath& originalPath, const GrCCFillGeometry& geometry, |
| 121 | int verbsIdx, int ptsIdx, const SkIRect& clippedDevIBounds, |
| 122 | PrimitiveTallies* newTriangleCounts) { |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 123 | using Verb = GrCCFillGeometry::Verb; |
| 124 | SkASSERT(-1 == fFanTessellationCount); |
| 125 | SkASSERT(!fFanTessellation); |
| 126 | |
| 127 | const SkTArray<Verb, true>& verbs = geometry.verbs(); |
| 128 | const SkTArray<SkPoint, true>& pts = geometry.points(); |
| 129 | |
| 130 | newTriangleCounts->fTriangles = |
| 131 | newTriangleCounts->fWeightedTriangles = 0; |
| 132 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame^] | 133 | // Build an SkPath of the Redbook fan. |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 134 | SkPath fan; |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame^] | 135 | if (Algorithm::kCoverageCount == algorithm) { |
| 136 | // We use "winding" fill type right now because we are producing a coverage count, and must |
| 137 | // fill in every region that has non-zero wind. The path processor will convert coverage |
| 138 | // count to the appropriate fill type later. |
| 139 | fan.setFillType(SkPath::kWinding_FillType); |
| 140 | } else { |
| 141 | // When counting winding numbers in the stencil buffer, it works to just tessellate the |
| 142 | // Redbook fan with the same fill type as the path. |
| 143 | fan.setFillType(originalPath.getFillType()); |
| 144 | } |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 145 | SkASSERT(Verb::kBeginPath == verbs[verbsIdx]); |
| 146 | for (int i = verbsIdx + 1; i < verbs.count(); ++i) { |
| 147 | switch (verbs[i]) { |
| 148 | case Verb::kBeginPath: |
| 149 | SK_ABORT("Invalid GrCCFillGeometry"); |
| 150 | continue; |
| 151 | |
| 152 | case Verb::kBeginContour: |
| 153 | fan.moveTo(pts[ptsIdx++]); |
| 154 | continue; |
| 155 | |
| 156 | case Verb::kLineTo: |
| 157 | fan.lineTo(pts[ptsIdx++]); |
| 158 | continue; |
| 159 | |
| 160 | case Verb::kMonotonicQuadraticTo: |
| 161 | case Verb::kMonotonicConicTo: |
| 162 | fan.lineTo(pts[ptsIdx + 1]); |
| 163 | ptsIdx += 2; |
| 164 | continue; |
| 165 | |
| 166 | case Verb::kMonotonicCubicTo: |
| 167 | fan.lineTo(pts[ptsIdx + 2]); |
| 168 | ptsIdx += 3; |
| 169 | continue; |
| 170 | |
| 171 | case Verb::kEndClosedContour: |
| 172 | case Verb::kEndOpenContour: |
| 173 | fan.close(); |
| 174 | continue; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | GrTessellator::WindingVertex* vertices = nullptr; |
| 179 | fFanTessellationCount = |
| 180 | GrTessellator::PathToVertices(fan, std::numeric_limits<float>::infinity(), |
| 181 | SkRect::Make(clippedDevIBounds), &vertices); |
| 182 | if (fFanTessellationCount <= 0) { |
| 183 | SkASSERT(0 == fFanTessellationCount); |
| 184 | SkASSERT(nullptr == vertices); |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | SkASSERT(0 == fFanTessellationCount % 3); |
| 189 | for (int i = 0; i < fFanTessellationCount; i += 3) { |
| 190 | int tessWinding = vertices[i].fWinding; |
| 191 | SkASSERT(tessWinding == vertices[i + 1].fWinding); |
| 192 | SkASSERT(tessWinding == vertices[i + 2].fWinding); |
| 193 | |
| 194 | // Ensure this triangle's points actually wind in the same direction as tessWinding. |
| 195 | // CCPR shaders use the sign of wind to determine which direction to bloat, so even for |
| 196 | // "wound" triangles the winding sign and point ordering need to agree. |
| 197 | float ax = vertices[i].fPos.fX - vertices[i + 1].fPos.fX; |
| 198 | float ay = vertices[i].fPos.fY - vertices[i + 1].fPos.fY; |
| 199 | float bx = vertices[i].fPos.fX - vertices[i + 2].fPos.fX; |
| 200 | float by = vertices[i].fPos.fY - vertices[i + 2].fPos.fY; |
| 201 | float wind = ax*by - ay*bx; |
| 202 | if ((wind > 0) != (-tessWinding > 0)) { // Tessellator has opposite winding sense. |
| 203 | std::swap(vertices[i + 1].fPos, vertices[i + 2].fPos); |
| 204 | } |
| 205 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame^] | 206 | int weight = abs(tessWinding); |
| 207 | SkASSERT(SkPath::kEvenOdd_FillType != fan.getFillType() || weight == 1); |
| 208 | if (weight > 1 && Algorithm::kCoverageCount == algorithm) { |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 209 | ++newTriangleCounts->fWeightedTriangles; |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame^] | 210 | } else { |
| 211 | newTriangleCounts->fTriangles += weight; |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| 215 | fFanTessellation.reset(vertices); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 218 | GrCCFiller::BatchID GrCCFiller::closeCurrentBatch() { |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 219 | SkASSERT(!fInstanceBuffer); |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 220 | SkASSERT(!fBatches.empty()); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 221 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 222 | const auto& lastBatch = fBatches.back(); |
Chris Dalton | a883aca | 2018-03-08 23:05:30 -0700 | [diff] [blame] | 223 | int maxMeshes = 1 + fScissorSubBatches.count() - lastBatch.fEndScissorSubBatchIdx; |
| 224 | fMaxMeshesPerDraw = SkTMax(fMaxMeshesPerDraw, maxMeshes); |
| 225 | |
| 226 | const auto& lastScissorSubBatch = fScissorSubBatches[lastBatch.fEndScissorSubBatchIdx - 1]; |
Chris Dalton | 916c498 | 2018-08-15 00:53:25 -0600 | [diff] [blame] | 227 | PrimitiveTallies batchTotalCounts = fTotalPrimitiveCounts[(int)GrScissorTest::kDisabled] - |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 228 | lastBatch.fEndNonScissorIndices; |
Chris Dalton | 916c498 | 2018-08-15 00:53:25 -0600 | [diff] [blame] | 229 | batchTotalCounts += fTotalPrimitiveCounts[(int)GrScissorTest::kEnabled] - |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 230 | lastScissorSubBatch.fEndPrimitiveIndices; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 231 | |
Chris Dalton | a883aca | 2018-03-08 23:05:30 -0700 | [diff] [blame] | 232 | // This will invalidate lastBatch. |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 233 | fBatches.push_back() = { |
Chris Dalton | 916c498 | 2018-08-15 00:53:25 -0600 | [diff] [blame] | 234 | fTotalPrimitiveCounts[(int)GrScissorTest::kDisabled], |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 235 | fScissorSubBatches.count(), |
| 236 | batchTotalCounts |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 237 | }; |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 238 | return fBatches.count() - 1; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // Emits a contour's triangle fan. |
| 242 | // |
| 243 | // Classic Redbook fanning would be the triangles: [0 1 2], [0 2 3], ..., [0 n-2 n-1]. |
| 244 | // |
| 245 | // This function emits the triangle: [0 n/3 n*2/3], and then recurses on all three sides. The |
| 246 | // advantage to this approach is that for a convex-ish contour, it generates larger triangles. |
| 247 | // Classic fanning tends to generate long, skinny triangles, which are expensive to draw since they |
| 248 | // have a longer perimeter to rasterize and antialias. |
| 249 | // |
| 250 | // The indices array indexes the fan's points (think: glDrawElements), and must have at least log3 |
| 251 | // elements past the end for this method to use as scratch space. |
| 252 | // |
| 253 | // Returns the next triangle instance after the final one emitted. |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame^] | 254 | static TriPointInstance* emit_recursive_fan( |
| 255 | const SkTArray<SkPoint, true>& pts, SkTArray<int32_t, true>& indices, int firstIndex, |
| 256 | int indexCount, const Sk2f& devToAtlasOffset, TriPointInstance::Ordering ordering, |
| 257 | TriPointInstance out[]) { |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 258 | if (indexCount < 3) { |
| 259 | return out; |
| 260 | } |
| 261 | |
| 262 | int32_t oneThirdCount = indexCount / 3; |
| 263 | int32_t twoThirdsCount = (2 * indexCount) / 3; |
| 264 | out++->set(pts[indices[firstIndex]], pts[indices[firstIndex + oneThirdCount]], |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame^] | 265 | pts[indices[firstIndex + twoThirdsCount]], devToAtlasOffset, ordering); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 266 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame^] | 267 | out = emit_recursive_fan( |
| 268 | pts, indices, firstIndex, oneThirdCount + 1, devToAtlasOffset, ordering, out); |
| 269 | out = emit_recursive_fan( |
| 270 | pts, indices, firstIndex + oneThirdCount, twoThirdsCount - oneThirdCount + 1, |
| 271 | devToAtlasOffset, ordering, out); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 272 | |
| 273 | int endIndex = firstIndex + indexCount; |
| 274 | int32_t oldValue = indices[endIndex]; |
| 275 | indices[endIndex] = indices[firstIndex]; |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame^] | 276 | out = emit_recursive_fan( |
| 277 | pts, indices, firstIndex + twoThirdsCount, indexCount - twoThirdsCount + 1, |
| 278 | devToAtlasOffset, ordering, out); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 279 | indices[endIndex] = oldValue; |
| 280 | |
| 281 | return out; |
| 282 | } |
| 283 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame^] | 284 | void GrCCFiller::emitTessellatedFan( |
| 285 | const GrTessellator::WindingVertex* vertices, int numVertices, const Sk2f& devToAtlasOffset, |
| 286 | TriPointInstance::Ordering ordering, TriPointInstance* triPointInstanceData, |
| 287 | QuadPointInstance* quadPointInstanceData, GrCCFillGeometry::PrimitiveTallies* indices) { |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 288 | for (int i = 0; i < numVertices; i += 3) { |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame^] | 289 | int weight = abs(vertices[i].fWinding); |
| 290 | SkASSERT(weight >= 1); |
| 291 | if (weight > 1 && Algorithm::kStencilWindingCount != fAlgorithm) { |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame] | 292 | quadPointInstanceData[indices->fWeightedTriangles++].setW( |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 293 | vertices[i].fPos, vertices[i+1].fPos, vertices[i + 2].fPos, devToAtlasOffset, |
Chris Dalton | 6f5e77a | 2018-04-23 21:14:42 -0600 | [diff] [blame] | 294 | static_cast<float>(abs(vertices[i].fWinding))); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame^] | 295 | } else for (int j = 0; j < weight; ++j) { |
| 296 | // Unfortunately, there is not a way to increment stencil values by an amount larger |
| 297 | // than 1. Instead we draw the triangle 'weight' times. |
| 298 | triPointInstanceData[indices->fTriangles++].set( |
| 299 | vertices[i].fPos, vertices[i + 1].fPos, vertices[i + 2].fPos, devToAtlasOffset, |
| 300 | ordering); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | } |
| 304 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 305 | bool GrCCFiller::prepareToDraw(GrOnFlushResourceProvider* onFlushRP) { |
| 306 | using Verb = GrCCFillGeometry::Verb; |
| 307 | SkASSERT(!fInstanceBuffer); |
| 308 | SkASSERT(fBatches.back().fEndNonScissorIndices == // Call closeCurrentBatch(). |
Chris Dalton | 916c498 | 2018-08-15 00:53:25 -0600 | [diff] [blame] | 309 | fTotalPrimitiveCounts[(int)GrScissorTest::kDisabled]); |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 310 | SkASSERT(fBatches.back().fEndScissorSubBatchIdx == fScissorSubBatches.count()); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 311 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame^] | 312 | auto triangleOrdering = (Algorithm::kCoverageCount == fAlgorithm) |
| 313 | ? TriPointInstance::Ordering::kXYTransposed |
| 314 | : TriPointInstance::Ordering::kXYInterleaved; |
| 315 | |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 316 | // Here we build a single instance buffer to share with every internal batch. |
| 317 | // |
| 318 | // CCPR processs 3 different types of primitives: triangles, quadratics, cubics. Each primitive |
| 319 | // type is further divided into instances that require a scissor and those that don't. This |
| 320 | // leaves us with 3*2 = 6 independent instance arrays to build for the GPU. |
| 321 | // |
| 322 | // Rather than place each instance array in its own GPU buffer, we allocate a single |
| 323 | // megabuffer and lay them all out side-by-side. We can offset the "baseInstance" parameter in |
| 324 | // our draw calls to direct the GPU to the applicable elements within a given array. |
| 325 | // |
| 326 | // We already know how big to make each of the 6 arrays from fTotalPrimitiveCounts, so layout is |
| 327 | // straightforward. Start with triangles and quadratics. They both view the instance buffer as |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 328 | // an array of TriPointInstance[], so we can begin at zero and lay them out one after the other. |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 329 | fBaseInstances[0].fTriangles = 0; |
| 330 | fBaseInstances[1].fTriangles = fBaseInstances[0].fTriangles + |
| 331 | fTotalPrimitiveCounts[0].fTriangles; |
| 332 | fBaseInstances[0].fQuadratics = fBaseInstances[1].fTriangles + |
| 333 | fTotalPrimitiveCounts[1].fTriangles; |
| 334 | fBaseInstances[1].fQuadratics = fBaseInstances[0].fQuadratics + |
| 335 | fTotalPrimitiveCounts[0].fQuadratics; |
| 336 | int triEndIdx = fBaseInstances[1].fQuadratics + fTotalPrimitiveCounts[1].fQuadratics; |
| 337 | |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 338 | // Wound triangles and cubics both view the same instance buffer as an array of |
| 339 | // QuadPointInstance[]. So, reinterpreting the instance data as QuadPointInstance[], we start |
| 340 | // them on the first index that will not overwrite previous TriPointInstance data. |
| 341 | int quadBaseIdx = |
Brian Salomon | 24d377e | 2019-04-23 15:24:31 -0400 | [diff] [blame] | 342 | GrSizeDivRoundUp(triEndIdx * sizeof(TriPointInstance), sizeof(QuadPointInstance)); |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame] | 343 | fBaseInstances[0].fWeightedTriangles = quadBaseIdx; |
| 344 | fBaseInstances[1].fWeightedTriangles = fBaseInstances[0].fWeightedTriangles + |
| 345 | fTotalPrimitiveCounts[0].fWeightedTriangles; |
| 346 | fBaseInstances[0].fCubics = fBaseInstances[1].fWeightedTriangles + |
| 347 | fTotalPrimitiveCounts[1].fWeightedTriangles; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 348 | fBaseInstances[1].fCubics = fBaseInstances[0].fCubics + fTotalPrimitiveCounts[0].fCubics; |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 349 | fBaseInstances[0].fConics = fBaseInstances[1].fCubics + fTotalPrimitiveCounts[1].fCubics; |
| 350 | fBaseInstances[1].fConics = fBaseInstances[0].fConics + fTotalPrimitiveCounts[0].fConics; |
| 351 | int quadEndIdx = fBaseInstances[1].fConics + fTotalPrimitiveCounts[1].fConics; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 352 | |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 353 | fInstanceBuffer = |
| 354 | onFlushRP->makeBuffer(GrGpuBufferType::kVertex, quadEndIdx * sizeof(QuadPointInstance)); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 355 | if (!fInstanceBuffer) { |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 356 | SkDebugf("WARNING: failed to allocate CCPR fill instance buffer.\n"); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 357 | return false; |
| 358 | } |
| 359 | |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 360 | TriPointInstance* triPointInstanceData = static_cast<TriPointInstance*>(fInstanceBuffer->map()); |
| 361 | QuadPointInstance* quadPointInstanceData = |
| 362 | reinterpret_cast<QuadPointInstance*>(triPointInstanceData); |
| 363 | SkASSERT(quadPointInstanceData); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 364 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 365 | PathInfo* nextPathInfo = fPathInfos.begin(); |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 366 | Sk2f devToAtlasOffset; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 367 | PrimitiveTallies instanceIndices[2] = {fBaseInstances[0], fBaseInstances[1]}; |
| 368 | PrimitiveTallies* currIndices = nullptr; |
| 369 | SkSTArray<256, int32_t, true> currFan; |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 370 | bool currFanIsTessellated = false; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 371 | |
| 372 | const SkTArray<SkPoint, true>& pts = fGeometry.points(); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 373 | int ptsIdx = -1; |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 374 | int nextConicWeightIdx = 0; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 375 | |
| 376 | // Expand the ccpr verbs into GPU instance buffers. |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 377 | for (Verb verb : fGeometry.verbs()) { |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 378 | switch (verb) { |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 379 | case Verb::kBeginPath: |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 380 | SkASSERT(currFan.empty()); |
Chris Dalton | 916c498 | 2018-08-15 00:53:25 -0600 | [diff] [blame] | 381 | currIndices = &instanceIndices[(int)nextPathInfo->scissorTest()]; |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 382 | devToAtlasOffset = Sk2f(static_cast<float>(nextPathInfo->devToAtlasOffset().fX), |
| 383 | static_cast<float>(nextPathInfo->devToAtlasOffset().fY)); |
Chris Dalton | ad06544 | 2018-03-08 22:41:33 -0700 | [diff] [blame] | 384 | currFanIsTessellated = nextPathInfo->hasFanTessellation(); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 385 | if (currFanIsTessellated) { |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame^] | 386 | this->emitTessellatedFan( |
| 387 | nextPathInfo->fanTessellation(), nextPathInfo->fanTessellationCount(), |
| 388 | devToAtlasOffset, triangleOrdering, triPointInstanceData, |
| 389 | quadPointInstanceData, currIndices); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 390 | } |
| 391 | ++nextPathInfo; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 392 | continue; |
| 393 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 394 | case Verb::kBeginContour: |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 395 | SkASSERT(currFan.empty()); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 396 | ++ptsIdx; |
| 397 | if (!currFanIsTessellated) { |
| 398 | currFan.push_back(ptsIdx); |
| 399 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 400 | continue; |
| 401 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 402 | case Verb::kLineTo: |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 403 | ++ptsIdx; |
| 404 | if (!currFanIsTessellated) { |
| 405 | SkASSERT(!currFan.empty()); |
| 406 | currFan.push_back(ptsIdx); |
| 407 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 408 | continue; |
| 409 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 410 | case Verb::kMonotonicQuadraticTo: |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame^] | 411 | triPointInstanceData[currIndices->fQuadratics++].set( |
| 412 | &pts[ptsIdx], devToAtlasOffset, TriPointInstance::Ordering::kXYTransposed); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 413 | ptsIdx += 2; |
| 414 | if (!currFanIsTessellated) { |
| 415 | SkASSERT(!currFan.empty()); |
| 416 | currFan.push_back(ptsIdx); |
| 417 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 418 | continue; |
| 419 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 420 | case Verb::kMonotonicCubicTo: |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame^] | 421 | quadPointInstanceData[currIndices->fCubics++].set( |
| 422 | &pts[ptsIdx], devToAtlasOffset[0], devToAtlasOffset[1]); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 423 | ptsIdx += 3; |
| 424 | if (!currFanIsTessellated) { |
| 425 | SkASSERT(!currFan.empty()); |
| 426 | currFan.push_back(ptsIdx); |
| 427 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 428 | continue; |
| 429 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 430 | case Verb::kMonotonicConicTo: |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 431 | quadPointInstanceData[currIndices->fConics++].setW( |
Chris Dalton | 9414c96 | 2018-06-14 10:14:50 -0600 | [diff] [blame] | 432 | &pts[ptsIdx], devToAtlasOffset, |
| 433 | fGeometry.getConicWeight(nextConicWeightIdx)); |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 434 | ptsIdx += 2; |
| 435 | ++nextConicWeightIdx; |
| 436 | if (!currFanIsTessellated) { |
| 437 | SkASSERT(!currFan.empty()); |
| 438 | currFan.push_back(ptsIdx); |
| 439 | } |
| 440 | continue; |
| 441 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 442 | case Verb::kEndClosedContour: // endPt == startPt. |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 443 | if (!currFanIsTessellated) { |
| 444 | SkASSERT(!currFan.empty()); |
| 445 | currFan.pop_back(); |
| 446 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 447 | // fallthru. |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 448 | case Verb::kEndOpenContour: // endPt != startPt. |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 449 | SkASSERT(!currFanIsTessellated || currFan.empty()); |
| 450 | if (!currFanIsTessellated && currFan.count() >= 3) { |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 451 | int fanSize = currFan.count(); |
| 452 | // Reserve space for emit_recursive_fan. Technically this can grow to |
| 453 | // fanSize + log3(fanSize), but we approximate with log2. |
| 454 | currFan.push_back_n(SkNextLog2(fanSize)); |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame^] | 455 | SkDEBUGCODE(TriPointInstance* end =) emit_recursive_fan( |
| 456 | pts, currFan, 0, fanSize, devToAtlasOffset, triangleOrdering, |
| 457 | triPointInstanceData + currIndices->fTriangles); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 458 | currIndices->fTriangles += fanSize - 2; |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 459 | SkASSERT(triPointInstanceData + currIndices->fTriangles == end); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 460 | } |
| 461 | currFan.reset(); |
| 462 | continue; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | fInstanceBuffer->unmap(); |
| 467 | |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 468 | SkASSERT(nextPathInfo == fPathInfos.end()); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 469 | SkASSERT(ptsIdx == pts.count() - 1); |
| 470 | SkASSERT(instanceIndices[0].fTriangles == fBaseInstances[1].fTriangles); |
| 471 | SkASSERT(instanceIndices[1].fTriangles == fBaseInstances[0].fQuadratics); |
| 472 | SkASSERT(instanceIndices[0].fQuadratics == fBaseInstances[1].fQuadratics); |
| 473 | SkASSERT(instanceIndices[1].fQuadratics == triEndIdx); |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame] | 474 | SkASSERT(instanceIndices[0].fWeightedTriangles == fBaseInstances[1].fWeightedTriangles); |
| 475 | SkASSERT(instanceIndices[1].fWeightedTriangles == fBaseInstances[0].fCubics); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 476 | SkASSERT(instanceIndices[0].fCubics == fBaseInstances[1].fCubics); |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 477 | SkASSERT(instanceIndices[1].fCubics == fBaseInstances[0].fConics); |
| 478 | SkASSERT(instanceIndices[0].fConics == fBaseInstances[1].fConics); |
| 479 | SkASSERT(instanceIndices[1].fConics == quadEndIdx); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 480 | |
| 481 | fMeshesScratchBuffer.reserve(fMaxMeshesPerDraw); |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 482 | fScissorRectScratchBuffer.reserve(fMaxMeshesPerDraw); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 483 | |
| 484 | return true; |
| 485 | } |
| 486 | |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame^] | 487 | void GrCCFiller::drawFills( |
| 488 | GrOpFlushState* flushState, GrCCCoverageProcessor* proc, const GrPipeline& pipeline, |
| 489 | BatchID batchID, const SkIRect& drawBounds) const { |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 490 | using PrimitiveType = GrCCCoverageProcessor::PrimitiveType; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 491 | |
| 492 | SkASSERT(fInstanceBuffer); |
| 493 | |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 494 | GrResourceProvider* rp = flushState->resourceProvider(); |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 495 | const PrimitiveTallies& batchTotalCounts = fBatches[batchID].fTotalPrimitiveCounts; |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 496 | |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 497 | if (batchTotalCounts.fTriangles) { |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 498 | proc->reset(PrimitiveType::kTriangles, rp); |
| 499 | this->drawPrimitives( |
| 500 | flushState, *proc, pipeline, batchID, &PrimitiveTallies::fTriangles, drawBounds); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 501 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 502 | |
Chris Dalton | 703b476 | 2018-04-06 16:11:48 -0600 | [diff] [blame] | 503 | if (batchTotalCounts.fWeightedTriangles) { |
Chris Dalton | c3318f0 | 2019-07-19 14:20:53 -0600 | [diff] [blame^] | 504 | SkASSERT(Algorithm::kStencilWindingCount != fAlgorithm); |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 505 | proc->reset(PrimitiveType::kWeightedTriangles, rp); |
| 506 | this->drawPrimitives( |
| 507 | flushState, *proc, pipeline, batchID, &PrimitiveTallies::fWeightedTriangles, |
| 508 | drawBounds); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 509 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 510 | |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 511 | if (batchTotalCounts.fQuadratics) { |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 512 | proc->reset(PrimitiveType::kQuadratics, rp); |
| 513 | this->drawPrimitives( |
| 514 | flushState, *proc, pipeline, batchID, &PrimitiveTallies::fQuadratics, drawBounds); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | if (batchTotalCounts.fCubics) { |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 518 | proc->reset(PrimitiveType::kCubics, rp); |
| 519 | this->drawPrimitives( |
| 520 | flushState, *proc, pipeline, batchID, &PrimitiveTallies::fCubics, drawBounds); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 521 | } |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 522 | |
| 523 | if (batchTotalCounts.fConics) { |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 524 | proc->reset(PrimitiveType::kConics, rp); |
| 525 | this->drawPrimitives( |
| 526 | flushState, *proc, pipeline, batchID, &PrimitiveTallies::fConics, drawBounds); |
Chris Dalton | 9f2dab0 | 2018-04-18 14:07:03 -0600 | [diff] [blame] | 527 | } |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Chris Dalton | 2c5e011 | 2019-03-29 13:14:18 -0500 | [diff] [blame] | 530 | void GrCCFiller::drawPrimitives( |
| 531 | GrOpFlushState* flushState, const GrCCCoverageProcessor& proc, const GrPipeline& pipeline, |
| 532 | BatchID batchID, int PrimitiveTallies::*instanceType, const SkIRect& drawBounds) const { |
Brian Salomon | d818ebf | 2018-07-02 14:08:49 +0000 | [diff] [blame] | 533 | SkASSERT(pipeline.isScissorEnabled()); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 534 | |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 535 | // Don't call reset(), as that also resets the reserve count. |
| 536 | fMeshesScratchBuffer.pop_back_n(fMeshesScratchBuffer.count()); |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 537 | fScissorRectScratchBuffer.pop_back_n(fScissorRectScratchBuffer.count()); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 538 | |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 539 | SkASSERT(batchID > 0); |
Chris Dalton | e163969 | 2018-08-20 14:00:30 -0600 | [diff] [blame] | 540 | SkASSERT(batchID < fBatches.count()); |
| 541 | const Batch& previousBatch = fBatches[batchID - 1]; |
| 542 | const Batch& batch = fBatches[batchID]; |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 543 | SkDEBUGCODE(int totalInstanceCount = 0); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 544 | |
| 545 | if (int instanceCount = batch.fEndNonScissorIndices.*instanceType - |
| 546 | previousBatch.fEndNonScissorIndices.*instanceType) { |
| 547 | SkASSERT(instanceCount > 0); |
Chris Dalton | 916c498 | 2018-08-15 00:53:25 -0600 | [diff] [blame] | 548 | int baseInstance = fBaseInstances[(int)GrScissorTest::kDisabled].*instanceType + |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 549 | previousBatch.fEndNonScissorIndices.*instanceType; |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 550 | proc.appendMesh(fInstanceBuffer, instanceCount, baseInstance, &fMeshesScratchBuffer); |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 551 | fScissorRectScratchBuffer.push_back().setXYWH(0, 0, drawBounds.width(), |
| 552 | drawBounds.height()); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 553 | SkDEBUGCODE(totalInstanceCount += instanceCount); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | SkASSERT(previousBatch.fEndScissorSubBatchIdx > 0); |
| 557 | SkASSERT(batch.fEndScissorSubBatchIdx <= fScissorSubBatches.count()); |
Chris Dalton | 916c498 | 2018-08-15 00:53:25 -0600 | [diff] [blame] | 558 | int baseScissorInstance = fBaseInstances[(int)GrScissorTest::kEnabled].*instanceType; |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 559 | for (int i = previousBatch.fEndScissorSubBatchIdx; i < batch.fEndScissorSubBatchIdx; ++i) { |
| 560 | const ScissorSubBatch& previousSubBatch = fScissorSubBatches[i - 1]; |
| 561 | const ScissorSubBatch& scissorSubBatch = fScissorSubBatches[i]; |
| 562 | int startIndex = previousSubBatch.fEndPrimitiveIndices.*instanceType; |
| 563 | int instanceCount = scissorSubBatch.fEndPrimitiveIndices.*instanceType - startIndex; |
| 564 | if (!instanceCount) { |
| 565 | continue; |
| 566 | } |
| 567 | SkASSERT(instanceCount > 0); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 568 | proc.appendMesh(fInstanceBuffer, instanceCount, baseScissorInstance + startIndex, |
| 569 | &fMeshesScratchBuffer); |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 570 | fScissorRectScratchBuffer.push_back() = scissorSubBatch.fScissor; |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 571 | SkDEBUGCODE(totalInstanceCount += instanceCount); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 574 | SkASSERT(fMeshesScratchBuffer.count() == fScissorRectScratchBuffer.count()); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 575 | SkASSERT(fMeshesScratchBuffer.count() <= fMaxMeshesPerDraw); |
Chris Dalton | 84403d7 | 2018-02-13 21:46:17 -0500 | [diff] [blame] | 576 | SkASSERT(totalInstanceCount == batch.fTotalPrimitiveCounts.*instanceType); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 577 | |
| 578 | if (!fMeshesScratchBuffer.empty()) { |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 579 | proc.draw(flushState, pipeline, fScissorRectScratchBuffer.begin(), |
| 580 | fMeshesScratchBuffer.begin(), fMeshesScratchBuffer.count(), |
Chris Dalton | 8dfc70f | 2018-03-26 19:15:22 -0600 | [diff] [blame] | 581 | SkRect::Make(drawBounds)); |
Chris Dalton | 9ca2784 | 2018-01-18 12:24:50 -0700 | [diff] [blame] | 582 | } |
| 583 | } |