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