csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | |
| 8 | #include "InstancedRendering.h" |
| 9 | |
robertphillips | 5fa7f30 | 2016-07-21 09:21:04 -0700 | [diff] [blame] | 10 | #include "GrCaps.h" |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 11 | #include "GrOpFlushState.h" |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 12 | #include "GrPipeline.h" |
| 13 | #include "GrResourceProvider.h" |
| 14 | #include "instanced/InstanceProcessor.h" |
| 15 | |
| 16 | namespace gr_instanced { |
| 17 | |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame] | 18 | InstancedRendering::InstancedRendering(GrGpu* gpu) |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 19 | : fGpu(SkRef(gpu)), |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 20 | fState(State::kRecordingDraws), |
dskiba | e4cd006 | 2016-11-29 06:50:35 -0800 | [diff] [blame] | 21 | fDrawPool(1024, 1024) { |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 22 | } |
| 23 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 24 | sk_sp<GrDrawOp> InstancedRendering::recordRect(const SkRect& rect, const SkMatrix& viewMatrix, |
| 25 | GrColor color, GrAA aa, |
| 26 | const GrInstancedPipelineInfo& info, |
| 27 | GrAAType* aaType) { |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 28 | return this->recordShape(ShapeType::kRect, rect, viewMatrix, color, rect, aa, info, aaType); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 31 | sk_sp<GrDrawOp> InstancedRendering::recordRect(const SkRect& rect, const SkMatrix& viewMatrix, |
| 32 | GrColor color, const SkRect& localRect, GrAA aa, |
| 33 | const GrInstancedPipelineInfo& info, |
| 34 | GrAAType* aaType) { |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 35 | return this->recordShape(ShapeType::kRect, rect, viewMatrix, color, localRect, aa, info, |
| 36 | aaType); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 39 | sk_sp<GrDrawOp> InstancedRendering::recordRect(const SkRect& rect, const SkMatrix& viewMatrix, |
| 40 | GrColor color, const SkMatrix& localMatrix, GrAA aa, |
| 41 | const GrInstancedPipelineInfo& info, |
| 42 | GrAAType* aaType) { |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 43 | if (localMatrix.hasPerspective()) { |
| 44 | return nullptr; // Perspective is not yet supported in the local matrix. |
| 45 | } |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 46 | if (sk_sp<Op> op = this->recordShape(ShapeType::kRect, rect, viewMatrix, color, rect, aa, info, |
| 47 | aaType)) { |
| 48 | op->getSingleInstance().fInfo |= kLocalMatrix_InfoFlag; |
| 49 | op->appendParamsTexel(localMatrix.getScaleX(), localMatrix.getSkewX(), |
| 50 | localMatrix.getTranslateX()); |
| 51 | op->appendParamsTexel(localMatrix.getSkewY(), localMatrix.getScaleY(), |
| 52 | localMatrix.getTranslateY()); |
| 53 | op->fInfo.fHasLocalMatrix = true; |
| 54 | return std::move(op); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 55 | } |
| 56 | return nullptr; |
| 57 | } |
| 58 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 59 | sk_sp<GrDrawOp> InstancedRendering::recordOval(const SkRect& oval, const SkMatrix& viewMatrix, |
| 60 | GrColor color, GrAA aa, |
| 61 | const GrInstancedPipelineInfo& info, |
| 62 | GrAAType* aaType) { |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 63 | return this->recordShape(ShapeType::kOval, oval, viewMatrix, color, oval, aa, info, aaType); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 66 | sk_sp<GrDrawOp> InstancedRendering::recordRRect(const SkRRect& rrect, const SkMatrix& viewMatrix, |
| 67 | GrColor color, GrAA aa, |
| 68 | const GrInstancedPipelineInfo& info, |
| 69 | GrAAType* aaType) { |
| 70 | if (sk_sp<Op> op = this->recordShape(GetRRectShapeType(rrect), rrect.rect(), viewMatrix, color, |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 71 | rrect.rect(), aa, info, aaType)) { |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 72 | op->appendRRectParams(rrect); |
| 73 | return std::move(op); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 74 | } |
| 75 | return nullptr; |
| 76 | } |
| 77 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 78 | sk_sp<GrDrawOp> InstancedRendering::recordDRRect(const SkRRect& outer, const SkRRect& inner, |
| 79 | const SkMatrix& viewMatrix, GrColor color, GrAA aa, |
| 80 | const GrInstancedPipelineInfo& info, |
| 81 | GrAAType* aaType) { |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 82 | if (inner.getType() > SkRRect::kSimple_Type) { |
| 83 | return nullptr; // Complex inner round rects are not yet supported. |
| 84 | } |
| 85 | if (SkRRect::kEmpty_Type == inner.getType()) { |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 86 | return this->recordRRect(outer, viewMatrix, color, aa, info, aaType); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 87 | } |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 88 | if (sk_sp<Op> op = this->recordShape(GetRRectShapeType(outer), outer.rect(), viewMatrix, color, |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 89 | outer.rect(), aa, info, aaType)) { |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 90 | op->appendRRectParams(outer); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 91 | ShapeType innerShapeType = GetRRectShapeType(inner); |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 92 | op->fInfo.fInnerShapeTypes |= GetShapeFlag(innerShapeType); |
| 93 | op->getSingleInstance().fInfo |= ((int)innerShapeType << kInnerShapeType_InfoBit); |
| 94 | op->appendParamsTexel(inner.rect().asScalars(), 4); |
| 95 | op->appendRRectParams(inner); |
| 96 | return std::move(op); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 97 | } |
| 98 | return nullptr; |
| 99 | } |
| 100 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 101 | sk_sp<InstancedRendering::Op> InstancedRendering::recordShape( |
| 102 | ShapeType type, const SkRect& bounds, const SkMatrix& viewMatrix, GrColor color, |
| 103 | const SkRect& localRect, GrAA aa, const GrInstancedPipelineInfo& info, GrAAType* aaType) { |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 104 | SkASSERT(State::kRecordingDraws == fState); |
| 105 | |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame] | 106 | if (info.fIsRenderingToFloat && fGpu->caps()->avoidInstancedDrawsToFPTargets()) { |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 107 | return nullptr; |
| 108 | } |
| 109 | |
| 110 | AntialiasMode antialiasMode; |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 111 | if (!this->selectAntialiasMode(viewMatrix, aa, info, aaType, &antialiasMode)) { |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 112 | return nullptr; |
| 113 | } |
| 114 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 115 | sk_sp<Op> op = this->makeOp(); |
| 116 | op->fInfo.fAntialiasMode = antialiasMode; |
| 117 | op->fInfo.fShapeTypes = GetShapeFlag(type); |
| 118 | op->fInfo.fCannotDiscard = !info.fCanDiscard; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 119 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 120 | Instance& instance = op->getSingleInstance(); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 121 | instance.fInfo = (int)type << kShapeType_InfoBit; |
| 122 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 123 | Op::HasAABloat aaBloat = (antialiasMode == AntialiasMode::kCoverage) ? Op::HasAABloat::kYes |
| 124 | : Op::HasAABloat::kNo; |
| 125 | Op::IsZeroArea zeroArea = (bounds.isEmpty()) ? Op::IsZeroArea::kYes : Op::IsZeroArea::kNo; |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 126 | |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 127 | // The instanced shape renderer draws rectangles of [-1, -1, +1, +1], so we find the matrix that |
| 128 | // will map this rectangle to the same device coordinates as "viewMatrix * bounds". |
| 129 | float sx = 0.5f * bounds.width(); |
| 130 | float sy = 0.5f * bounds.height(); |
| 131 | float tx = sx + bounds.fLeft; |
| 132 | float ty = sy + bounds.fTop; |
| 133 | if (!viewMatrix.hasPerspective()) { |
| 134 | float* m = instance.fShapeMatrix2x3; |
| 135 | m[0] = viewMatrix.getScaleX() * sx; |
| 136 | m[1] = viewMatrix.getSkewX() * sy; |
| 137 | m[2] = viewMatrix.getTranslateX() + |
| 138 | viewMatrix.getScaleX() * tx + viewMatrix.getSkewX() * ty; |
| 139 | |
| 140 | m[3] = viewMatrix.getSkewY() * sx; |
| 141 | m[4] = viewMatrix.getScaleY() * sy; |
| 142 | m[5] = viewMatrix.getTranslateY() + |
| 143 | viewMatrix.getSkewY() * tx + viewMatrix.getScaleY() * ty; |
| 144 | |
| 145 | // Since 'm' is a 2x3 matrix that maps the rect [-1, +1] into the shape's device-space quad, |
| 146 | // it's quite simple to find the bounding rectangle: |
| 147 | float devBoundsHalfWidth = fabsf(m[0]) + fabsf(m[1]); |
| 148 | float devBoundsHalfHeight = fabsf(m[3]) + fabsf(m[4]); |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 149 | SkRect opBounds; |
| 150 | opBounds.fLeft = m[2] - devBoundsHalfWidth; |
| 151 | opBounds.fRight = m[2] + devBoundsHalfWidth; |
| 152 | opBounds.fTop = m[5] - devBoundsHalfHeight; |
| 153 | opBounds.fBottom = m[5] + devBoundsHalfHeight; |
| 154 | op->setBounds(opBounds, aaBloat, zeroArea); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 155 | |
| 156 | // TODO: Is this worth the CPU overhead? |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 157 | op->fInfo.fNonSquare = |
| 158 | fabsf(devBoundsHalfHeight - devBoundsHalfWidth) > 0.5f || // Early out. |
| 159 | fabs(m[0] * m[3] + m[1] * m[4]) > 1e-3f || // Skew? |
| 160 | fabs(m[0] * m[0] + m[1] * m[1] - m[3] * m[3] - m[4] * m[4]) > |
| 161 | 1e-2f; // Diff. lengths? |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 162 | } else { |
| 163 | SkMatrix shapeMatrix(viewMatrix); |
| 164 | shapeMatrix.preTranslate(tx, ty); |
| 165 | shapeMatrix.preScale(sx, sy); |
| 166 | instance.fInfo |= kPerspective_InfoFlag; |
| 167 | |
| 168 | float* m = instance.fShapeMatrix2x3; |
| 169 | m[0] = SkScalarToFloat(shapeMatrix.getScaleX()); |
| 170 | m[1] = SkScalarToFloat(shapeMatrix.getSkewX()); |
| 171 | m[2] = SkScalarToFloat(shapeMatrix.getTranslateX()); |
| 172 | m[3] = SkScalarToFloat(shapeMatrix.getSkewY()); |
| 173 | m[4] = SkScalarToFloat(shapeMatrix.getScaleY()); |
| 174 | m[5] = SkScalarToFloat(shapeMatrix.getTranslateY()); |
| 175 | |
| 176 | // Send the perspective column as a param. |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 177 | op->appendParamsTexel(shapeMatrix[SkMatrix::kMPersp0], shapeMatrix[SkMatrix::kMPersp1], |
| 178 | shapeMatrix[SkMatrix::kMPersp2]); |
| 179 | op->fInfo.fHasPerspective = true; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 180 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 181 | op->setBounds(bounds, aaBloat, zeroArea); |
| 182 | op->fInfo.fNonSquare = true; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | instance.fColor = color; |
| 186 | |
| 187 | const float* rectAsFloats = localRect.asScalars(); // Ensure SkScalar == float. |
| 188 | memcpy(&instance.fLocalRect, rectAsFloats, 4 * sizeof(float)); |
| 189 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 190 | op->fPixelLoad = op->bounds().height() * op->bounds().width(); |
| 191 | return op; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 194 | inline bool InstancedRendering::selectAntialiasMode(const SkMatrix& viewMatrix, GrAA aa, |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 195 | const GrInstancedPipelineInfo& info, |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 196 | GrAAType* aaType, |
| 197 | AntialiasMode* antialiasMode) { |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 198 | SkASSERT(!info.fColorDisabled || info.fDrawingShapeToStencil); |
| 199 | SkASSERT(!info.fIsMixedSampled || info.fIsMultisampled); |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame] | 200 | SkASSERT(GrCaps::InstancedSupport::kNone != fGpu->caps()->instancedSupport()); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 201 | |
| 202 | if (!info.fIsMultisampled || fGpu->caps()->multisampleDisableSupport()) { |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 203 | if (GrAA::kNo == aa) { |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 204 | if (info.fDrawingShapeToStencil && !info.fCanDiscard) { |
| 205 | // We can't draw to the stencil buffer without discard (or sample mask if MSAA). |
| 206 | return false; |
| 207 | } |
| 208 | *antialiasMode = AntialiasMode::kNone; |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 209 | *aaType = GrAAType::kNone; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 210 | return true; |
| 211 | } |
| 212 | |
| 213 | if (info.canUseCoverageAA() && viewMatrix.preservesRightAngles()) { |
| 214 | *antialiasMode = AntialiasMode::kCoverage; |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 215 | *aaType = GrAAType::kCoverage; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 216 | return true; |
| 217 | } |
| 218 | } |
| 219 | |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame] | 220 | if (info.fIsMultisampled && |
| 221 | fGpu->caps()->instancedSupport() >= GrCaps::InstancedSupport::kMultisampled) { |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 222 | if (!info.fIsMixedSampled || info.fColorDisabled) { |
| 223 | *antialiasMode = AntialiasMode::kMSAA; |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 224 | *aaType = GrAAType::kMSAA; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 225 | return true; |
| 226 | } |
csmartdalton | e0d3629 | 2016-07-29 08:14:20 -0700 | [diff] [blame] | 227 | if (fGpu->caps()->instancedSupport() >= GrCaps::InstancedSupport::kMixedSampled) { |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 228 | *antialiasMode = AntialiasMode::kMixedSamples; |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 229 | *aaType = GrAAType::kMixedSamples; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 230 | return true; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | return false; |
| 235 | } |
| 236 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 237 | InstancedRendering::Op::Op(uint32_t classID, InstancedRendering* ir) |
| 238 | : INHERITED(classID) |
| 239 | , fInstancedRendering(ir) |
| 240 | , fIsTracked(false) |
| 241 | , fNumDraws(1) |
| 242 | , fNumChangesInGeometry(0) { |
dskiba | e4cd006 | 2016-11-29 06:50:35 -0800 | [diff] [blame] | 243 | fHeadDraw = fTailDraw = fInstancedRendering->fDrawPool.allocate(); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 244 | #ifdef SK_DEBUG |
| 245 | fHeadDraw->fGeometry = {-1, 0}; |
| 246 | #endif |
| 247 | fHeadDraw->fNext = nullptr; |
| 248 | } |
| 249 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 250 | InstancedRendering::Op::~Op() { |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 251 | if (fIsTracked) { |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 252 | fInstancedRendering->fTrackedOps.remove(this); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | Draw* draw = fHeadDraw; |
| 256 | while (draw) { |
| 257 | Draw* next = draw->fNext; |
| 258 | fInstancedRendering->fDrawPool.release(draw); |
| 259 | draw = next; |
| 260 | } |
| 261 | } |
| 262 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 263 | void InstancedRendering::Op::appendRRectParams(const SkRRect& rrect) { |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 264 | SkASSERT(!fIsTracked); |
| 265 | switch (rrect.getType()) { |
| 266 | case SkRRect::kSimple_Type: { |
| 267 | const SkVector& radii = rrect.getSimpleRadii(); |
| 268 | this->appendParamsTexel(radii.x(), radii.y(), rrect.width(), rrect.height()); |
| 269 | return; |
| 270 | } |
| 271 | case SkRRect::kNinePatch_Type: { |
| 272 | float twoOverW = 2 / rrect.width(); |
| 273 | float twoOverH = 2 / rrect.height(); |
| 274 | const SkVector& radiiTL = rrect.radii(SkRRect::kUpperLeft_Corner); |
| 275 | const SkVector& radiiBR = rrect.radii(SkRRect::kLowerRight_Corner); |
| 276 | this->appendParamsTexel(radiiTL.x() * twoOverW, radiiBR.x() * twoOverW, |
| 277 | radiiTL.y() * twoOverH, radiiBR.y() * twoOverH); |
| 278 | return; |
| 279 | } |
| 280 | case SkRRect::kComplex_Type: { |
| 281 | /** |
| 282 | * The x and y radii of each arc are stored in separate vectors, |
| 283 | * in the following order: |
| 284 | * |
| 285 | * __x1 _ _ _ x3__ |
| 286 | * y1 | | y2 |
| 287 | * |
| 288 | * | | |
| 289 | * |
| 290 | * y3 |__ _ _ _ __| y4 |
| 291 | * x2 x4 |
| 292 | * |
| 293 | */ |
| 294 | float twoOverW = 2 / rrect.width(); |
| 295 | float twoOverH = 2 / rrect.height(); |
| 296 | const SkVector& radiiTL = rrect.radii(SkRRect::kUpperLeft_Corner); |
| 297 | const SkVector& radiiTR = rrect.radii(SkRRect::kUpperRight_Corner); |
| 298 | const SkVector& radiiBR = rrect.radii(SkRRect::kLowerRight_Corner); |
| 299 | const SkVector& radiiBL = rrect.radii(SkRRect::kLowerLeft_Corner); |
| 300 | this->appendParamsTexel(radiiTL.x() * twoOverW, radiiBL.x() * twoOverW, |
| 301 | radiiTR.x() * twoOverW, radiiBR.x() * twoOverW); |
| 302 | this->appendParamsTexel(radiiTL.y() * twoOverH, radiiTR.y() * twoOverH, |
| 303 | radiiBL.y() * twoOverH, radiiBR.y() * twoOverH); |
| 304 | return; |
| 305 | } |
| 306 | default: return; |
| 307 | } |
| 308 | } |
| 309 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 310 | void InstancedRendering::Op::appendParamsTexel(const SkScalar* vals, int count) { |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 311 | SkASSERT(!fIsTracked); |
| 312 | SkASSERT(count <= 4 && count >= 0); |
| 313 | const float* valsAsFloats = vals; // Ensure SkScalar == float. |
| 314 | memcpy(&fParams.push_back(), valsAsFloats, count * sizeof(float)); |
| 315 | fInfo.fHasParams = true; |
| 316 | } |
| 317 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 318 | void InstancedRendering::Op::appendParamsTexel(SkScalar x, SkScalar y, SkScalar z, SkScalar w) { |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 319 | SkASSERT(!fIsTracked); |
| 320 | ParamsTexel& texel = fParams.push_back(); |
| 321 | texel.fX = SkScalarToFloat(x); |
| 322 | texel.fY = SkScalarToFloat(y); |
| 323 | texel.fZ = SkScalarToFloat(z); |
| 324 | texel.fW = SkScalarToFloat(w); |
| 325 | fInfo.fHasParams = true; |
| 326 | } |
| 327 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 328 | void InstancedRendering::Op::appendParamsTexel(SkScalar x, SkScalar y, SkScalar z) { |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 329 | SkASSERT(!fIsTracked); |
| 330 | ParamsTexel& texel = fParams.push_back(); |
| 331 | texel.fX = SkScalarToFloat(x); |
| 332 | texel.fY = SkScalarToFloat(y); |
| 333 | texel.fZ = SkScalarToFloat(z); |
| 334 | fInfo.fHasParams = true; |
| 335 | } |
| 336 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 337 | void InstancedRendering::Op::computePipelineOptimizations(GrInitInvariantOutput* color, |
| 338 | GrInitInvariantOutput* coverage, |
| 339 | GrBatchToXPOverrides* overrides) const { |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 340 | color->setKnownFourComponents(this->getSingleInstance().fColor); |
| 341 | |
| 342 | if (AntialiasMode::kCoverage == fInfo.fAntialiasMode || |
| 343 | (AntialiasMode::kNone == fInfo.fAntialiasMode && |
| 344 | !fInfo.isSimpleRects() && fInfo.fCannotDiscard)) { |
| 345 | coverage->setUnknownSingleComponent(); |
| 346 | } else { |
| 347 | coverage->setKnownSingleComponent(255); |
| 348 | } |
| 349 | } |
| 350 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 351 | void InstancedRendering::Op::initBatchTracker(const GrXPOverridesForBatch& overrides) { |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 352 | Draw& draw = this->getSingleDraw(); // This will assert if we have > 1 command. |
| 353 | SkASSERT(draw.fGeometry.isEmpty()); |
| 354 | SkASSERT(SkIsPow2(fInfo.fShapeTypes)); |
| 355 | SkASSERT(!fIsTracked); |
| 356 | |
| 357 | if (kRect_ShapeFlag == fInfo.fShapeTypes) { |
| 358 | draw.fGeometry = InstanceProcessor::GetIndexRangeForRect(fInfo.fAntialiasMode); |
| 359 | } else if (kOval_ShapeFlag == fInfo.fShapeTypes) { |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 360 | draw.fGeometry = InstanceProcessor::GetIndexRangeForOval(fInfo.fAntialiasMode, |
| 361 | this->bounds()); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 362 | } else { |
| 363 | draw.fGeometry = InstanceProcessor::GetIndexRangeForRRect(fInfo.fAntialiasMode); |
| 364 | } |
| 365 | |
| 366 | if (!fParams.empty()) { |
| 367 | SkASSERT(fInstancedRendering->fParams.count() < (int)kParamsIdx_InfoMask); // TODO: cleaner. |
| 368 | this->getSingleInstance().fInfo |= fInstancedRendering->fParams.count(); |
| 369 | fInstancedRendering->fParams.push_back_n(fParams.count(), fParams.begin()); |
| 370 | } |
| 371 | |
| 372 | GrColor overrideColor; |
| 373 | if (overrides.getOverrideColorIfSet(&overrideColor)) { |
| 374 | SkASSERT(State::kRecordingDraws == fInstancedRendering->fState); |
| 375 | this->getSingleInstance().fColor = overrideColor; |
| 376 | } |
| 377 | fInfo.fUsesLocalCoords = overrides.readsLocalCoords(); |
| 378 | fInfo.fCannotTweakAlphaForCoverage = !overrides.canTweakAlphaForCoverage(); |
| 379 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 380 | fInstancedRendering->fTrackedOps.addToTail(this); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 381 | fIsTracked = true; |
| 382 | } |
| 383 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 384 | bool InstancedRendering::Op::onCombineIfPossible(GrOp* other, const GrCaps& caps) { |
| 385 | Op* that = static_cast<Op*>(other); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 386 | SkASSERT(fInstancedRendering == that->fInstancedRendering); |
| 387 | SkASSERT(fTailDraw); |
| 388 | SkASSERT(that->fTailDraw); |
| 389 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 390 | if (!OpInfo::CanCombine(fInfo, that->fInfo) || |
| 391 | !GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 392 | that->bounds(), caps)) { |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 393 | return false; |
| 394 | } |
| 395 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 396 | OpInfo combinedInfo = fInfo | that->fInfo; |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 397 | if (!combinedInfo.isSimpleRects()) { |
| 398 | // This threshold was chosen with the "shapes_mixed" bench on a MacBook with Intel graphics. |
| 399 | // There seems to be a wide range where it doesn't matter if we combine or not. What matters |
| 400 | // is that the itty bitty rects combine with other shapes and the giant ones don't. |
| 401 | constexpr SkScalar kMaxPixelsToGeneralizeRects = 256 * 256; |
| 402 | if (fInfo.isSimpleRects() && fPixelLoad > kMaxPixelsToGeneralizeRects) { |
| 403 | return false; |
| 404 | } |
| 405 | if (that->fInfo.isSimpleRects() && that->fPixelLoad > kMaxPixelsToGeneralizeRects) { |
| 406 | return false; |
| 407 | } |
| 408 | } |
| 409 | |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 410 | this->joinBounds(*that); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 411 | fInfo = combinedInfo; |
| 412 | fPixelLoad += that->fPixelLoad; |
| 413 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 414 | // Adopt the other op's draws. |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 415 | fNumDraws += that->fNumDraws; |
| 416 | fNumChangesInGeometry += that->fNumChangesInGeometry; |
| 417 | if (fTailDraw->fGeometry != that->fHeadDraw->fGeometry) { |
| 418 | ++fNumChangesInGeometry; |
| 419 | } |
| 420 | fTailDraw->fNext = that->fHeadDraw; |
| 421 | fTailDraw = that->fTailDraw; |
| 422 | |
| 423 | that->fHeadDraw = that->fTailDraw = nullptr; |
| 424 | |
| 425 | return true; |
| 426 | } |
| 427 | |
| 428 | void InstancedRendering::beginFlush(GrResourceProvider* rp) { |
| 429 | SkASSERT(State::kRecordingDraws == fState); |
| 430 | fState = State::kFlushing; |
| 431 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 432 | if (fTrackedOps.isEmpty()) { |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 433 | return; |
| 434 | } |
| 435 | |
| 436 | if (!fVertexBuffer) { |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 437 | fVertexBuffer.reset(InstanceProcessor::FindOrCreateVertexBuffer(fGpu.get())); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 438 | if (!fVertexBuffer) { |
| 439 | return; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | if (!fIndexBuffer) { |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 444 | fIndexBuffer.reset(InstanceProcessor::FindOrCreateIndex8Buffer(fGpu.get())); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 445 | if (!fIndexBuffer) { |
| 446 | return; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | if (!fParams.empty()) { |
| 451 | fParamsBuffer.reset(rp->createBuffer(fParams.count() * sizeof(ParamsTexel), |
| 452 | kTexel_GrBufferType, kDynamic_GrAccessPattern, |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 453 | GrResourceProvider::kNoPendingIO_Flag | |
| 454 | GrResourceProvider::kRequireGpuMemory_Flag, |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 455 | fParams.begin())); |
| 456 | if (!fParamsBuffer) { |
| 457 | return; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | this->onBeginFlush(rp); |
| 462 | } |
| 463 | |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 464 | void InstancedRendering::Op::onDraw(GrOpFlushState* state, const SkRect& bounds) { |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 465 | SkASSERT(State::kFlushing == fInstancedRendering->fState); |
| 466 | SkASSERT(state->gpu() == fInstancedRendering->gpu()); |
| 467 | |
| 468 | state->gpu()->handleDirtyContext(); |
| 469 | if (GrXferBarrierType barrierType = this->pipeline()->xferBarrierType(*state->gpu()->caps())) { |
| 470 | state->gpu()->xferBarrier(this->pipeline()->getRenderTarget(), barrierType); |
| 471 | } |
| 472 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 473 | InstanceProcessor instProc(fInfo, fInstancedRendering->fParamsBuffer.get()); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 474 | fInstancedRendering->onDraw(*this->pipeline(), instProc, this); |
| 475 | } |
| 476 | |
| 477 | void InstancedRendering::endFlush() { |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 478 | // The caller is expected to delete all tracked ops (i.e. ops whose initBatchTracker |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 479 | // method has been called) before ending the flush. |
Brian Salomon | 99ad164 | 2016-12-16 09:50:45 -0500 | [diff] [blame^] | 480 | SkASSERT(fTrackedOps.isEmpty()); |
csmartdalton | a7f2964 | 2016-07-07 08:49:11 -0700 | [diff] [blame] | 481 | fParams.reset(); |
| 482 | fParamsBuffer.reset(); |
| 483 | this->onEndFlush(); |
| 484 | fState = State::kRecordingDraws; |
| 485 | // Hold on to the shape coords and index buffers. |
| 486 | } |
| 487 | |
| 488 | void InstancedRendering::resetGpuResources(ResetType resetType) { |
| 489 | fVertexBuffer.reset(); |
| 490 | fIndexBuffer.reset(); |
| 491 | fParamsBuffer.reset(); |
| 492 | this->onResetGpuResources(resetType); |
| 493 | } |
| 494 | |
| 495 | } |