blob: e67a8be9775accad9863c9cf979df637b6855c14 [file] [log] [blame]
csmartdaltona7f29642016-07-07 08:49:11 -07001/*
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
robertphillips5fa7f302016-07-21 09:21:04 -070010#include "GrCaps.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050011#include "GrOpFlushState.h"
csmartdaltona7f29642016-07-07 08:49:11 -070012#include "GrPipeline.h"
13#include "GrResourceProvider.h"
14#include "instanced/InstanceProcessor.h"
15
16namespace gr_instanced {
17
csmartdaltone0d36292016-07-29 08:14:20 -070018InstancedRendering::InstancedRendering(GrGpu* gpu)
csmartdaltona7f29642016-07-07 08:49:11 -070019 : fGpu(SkRef(gpu)),
csmartdaltona7f29642016-07-07 08:49:11 -070020 fState(State::kRecordingDraws),
dskibae4cd0062016-11-29 06:50:35 -080021 fDrawPool(1024, 1024) {
csmartdaltona7f29642016-07-07 08:49:11 -070022}
23
Brian Salomonf8334782017-01-03 09:42:58 -050024std::unique_ptr<GrDrawOp> InstancedRendering::recordRect(const SkRect& rect,
Brian Salomon54d212e2017-03-21 14:22:38 -040025 const SkMatrix& viewMatrix,
26 GrPaint&& paint, GrAA aa,
27 const GrInstancedPipelineInfo& info) {
28 return this->recordShape(ShapeType::kRect, rect, viewMatrix, std::move(paint), rect, aa, info);
29}
30
31std::unique_ptr<GrDrawOp> InstancedRendering::recordRect(const SkRect& rect,
32 const SkMatrix& viewMatrix,
33 GrPaint&& paint, const SkRect& localRect,
Brian Salomonf8334782017-01-03 09:42:58 -050034 GrAA aa,
Brian Salomon54d212e2017-03-21 14:22:38 -040035 const GrInstancedPipelineInfo& info) {
36 return this->recordShape(ShapeType::kRect, rect, viewMatrix, std::move(paint), localRect, aa,
37 info);
csmartdaltona7f29642016-07-07 08:49:11 -070038}
39
Brian Salomonf8334782017-01-03 09:42:58 -050040std::unique_ptr<GrDrawOp> InstancedRendering::recordRect(const SkRect& rect,
Brian Salomon54d212e2017-03-21 14:22:38 -040041 const SkMatrix& viewMatrix,
42 GrPaint&& paint,
Brian Salomonf8334782017-01-03 09:42:58 -050043 const SkMatrix& localMatrix, GrAA aa,
Brian Salomon54d212e2017-03-21 14:22:38 -040044 const GrInstancedPipelineInfo& info) {
csmartdaltona7f29642016-07-07 08:49:11 -070045 if (localMatrix.hasPerspective()) {
46 return nullptr; // Perspective is not yet supported in the local matrix.
47 }
Brian Salomon54d212e2017-03-21 14:22:38 -040048 if (std::unique_ptr<Op> op = this->recordShape(ShapeType::kRect, rect, viewMatrix,
49 std::move(paint), rect, aa, info)) {
Brian Salomon99ad1642016-12-16 09:50:45 -050050 op->getSingleInstance().fInfo |= kLocalMatrix_InfoFlag;
51 op->appendParamsTexel(localMatrix.getScaleX(), localMatrix.getSkewX(),
52 localMatrix.getTranslateX());
53 op->appendParamsTexel(localMatrix.getSkewY(), localMatrix.getScaleY(),
54 localMatrix.getTranslateY());
55 op->fInfo.fHasLocalMatrix = true;
56 return std::move(op);
csmartdaltona7f29642016-07-07 08:49:11 -070057 }
58 return nullptr;
59}
60
Brian Salomonf8334782017-01-03 09:42:58 -050061std::unique_ptr<GrDrawOp> InstancedRendering::recordOval(const SkRect& oval,
Brian Salomon54d212e2017-03-21 14:22:38 -040062 const SkMatrix& viewMatrix,
63 GrPaint&& paint, GrAA aa,
64 const GrInstancedPipelineInfo& info) {
65 return this->recordShape(ShapeType::kOval, oval, viewMatrix, std::move(paint), oval, aa, info);
csmartdaltona7f29642016-07-07 08:49:11 -070066}
67
Brian Salomonf8334782017-01-03 09:42:58 -050068std::unique_ptr<GrDrawOp> InstancedRendering::recordRRect(const SkRRect& rrect,
Brian Salomon54d212e2017-03-21 14:22:38 -040069 const SkMatrix& viewMatrix,
70 GrPaint&& paint, GrAA aa,
71 const GrInstancedPipelineInfo& info) {
Brian Salomonf8334782017-01-03 09:42:58 -050072 if (std::unique_ptr<Op> op =
Brian Salomon54d212e2017-03-21 14:22:38 -040073 this->recordShape(GetRRectShapeType(rrect), rrect.rect(), viewMatrix,
74 std::move(paint), rrect.rect(), aa, info)) {
Brian Salomon99ad1642016-12-16 09:50:45 -050075 op->appendRRectParams(rrect);
76 return std::move(op);
csmartdaltona7f29642016-07-07 08:49:11 -070077 }
78 return nullptr;
79}
80
Brian Salomon54d212e2017-03-21 14:22:38 -040081std::unique_ptr<GrDrawOp> InstancedRendering::recordDRRect(const SkRRect& outer,
82 const SkRRect& inner,
83 const SkMatrix& viewMatrix,
84 GrPaint&& paint, GrAA aa,
85 const GrInstancedPipelineInfo& info) {
csmartdaltona7f29642016-07-07 08:49:11 -070086 if (inner.getType() > SkRRect::kSimple_Type) {
87 return nullptr; // Complex inner round rects are not yet supported.
88 }
89 if (SkRRect::kEmpty_Type == inner.getType()) {
Brian Salomon54d212e2017-03-21 14:22:38 -040090 return this->recordRRect(outer, viewMatrix, std::move(paint), aa, info);
csmartdaltona7f29642016-07-07 08:49:11 -070091 }
Brian Salomonf8334782017-01-03 09:42:58 -050092 if (std::unique_ptr<Op> op =
Brian Salomon54d212e2017-03-21 14:22:38 -040093 this->recordShape(GetRRectShapeType(outer), outer.rect(), viewMatrix,
94 std::move(paint), outer.rect(), aa, info)) {
Brian Salomon99ad1642016-12-16 09:50:45 -050095 op->appendRRectParams(outer);
csmartdaltona7f29642016-07-07 08:49:11 -070096 ShapeType innerShapeType = GetRRectShapeType(inner);
Brian Salomon99ad1642016-12-16 09:50:45 -050097 op->fInfo.fInnerShapeTypes |= GetShapeFlag(innerShapeType);
98 op->getSingleInstance().fInfo |= ((int)innerShapeType << kInnerShapeType_InfoBit);
99 op->appendParamsTexel(inner.rect().asScalars(), 4);
100 op->appendRRectParams(inner);
101 return std::move(op);
csmartdaltona7f29642016-07-07 08:49:11 -0700102 }
103 return nullptr;
104}
105
Brian Salomonf8334782017-01-03 09:42:58 -0500106std::unique_ptr<InstancedRendering::Op> InstancedRendering::recordShape(
Brian Salomon54d212e2017-03-21 14:22:38 -0400107 ShapeType type, const SkRect& bounds, const SkMatrix& viewMatrix, GrPaint&& paint,
108 const SkRect& localRect, GrAA aa, const GrInstancedPipelineInfo& info) {
csmartdaltona7f29642016-07-07 08:49:11 -0700109 SkASSERT(State::kRecordingDraws == fState);
110
csmartdaltone0d36292016-07-29 08:14:20 -0700111 if (info.fIsRenderingToFloat && fGpu->caps()->avoidInstancedDrawsToFPTargets()) {
csmartdaltona7f29642016-07-07 08:49:11 -0700112 return nullptr;
113 }
114
Brian Salomon54d212e2017-03-21 14:22:38 -0400115 GrAAType aaType;
116 if (!this->selectAntialiasMode(viewMatrix, aa, info, &aaType)) {
csmartdaltona7f29642016-07-07 08:49:11 -0700117 return nullptr;
118 }
119
Brian Salomon54d212e2017-03-21 14:22:38 -0400120 GrColor color = paint.getColor();
121 std::unique_ptr<Op> op = this->makeOp(std::move(paint));
122 op->fInfo.setAAType(aaType);
Brian Salomon99ad1642016-12-16 09:50:45 -0500123 op->fInfo.fShapeTypes = GetShapeFlag(type);
Brian Salomon5ff3a5c2017-03-01 14:34:41 -0500124 op->fInfo.fCannotDiscard = true;
Brian Salomon54d212e2017-03-21 14:22:38 -0400125 op->fDrawColorsAreOpaque = GrColorIsOpaque(color);
126 op->fDrawColorsAreSame = true;
Brian Salomon99ad1642016-12-16 09:50:45 -0500127 Instance& instance = op->getSingleInstance();
csmartdaltona7f29642016-07-07 08:49:11 -0700128 instance.fInfo = (int)type << kShapeType_InfoBit;
129
Brian Salomonaf9847e2017-03-01 11:28:27 -0500130 Op::HasAABloat aaBloat =
Brian Salomon54d212e2017-03-21 14:22:38 -0400131 (aaType == GrAAType::kCoverage) ? Op::HasAABloat::kYes : Op::HasAABloat::kNo;
Brian Salomon99ad1642016-12-16 09:50:45 -0500132 Op::IsZeroArea zeroArea = (bounds.isEmpty()) ? Op::IsZeroArea::kYes : Op::IsZeroArea::kNo;
bsalomon88cf17d2016-07-08 06:40:56 -0700133
csmartdaltona7f29642016-07-07 08:49:11 -0700134 // The instanced shape renderer draws rectangles of [-1, -1, +1, +1], so we find the matrix that
135 // will map this rectangle to the same device coordinates as "viewMatrix * bounds".
136 float sx = 0.5f * bounds.width();
137 float sy = 0.5f * bounds.height();
138 float tx = sx + bounds.fLeft;
139 float ty = sy + bounds.fTop;
140 if (!viewMatrix.hasPerspective()) {
141 float* m = instance.fShapeMatrix2x3;
142 m[0] = viewMatrix.getScaleX() * sx;
143 m[1] = viewMatrix.getSkewX() * sy;
144 m[2] = viewMatrix.getTranslateX() +
145 viewMatrix.getScaleX() * tx + viewMatrix.getSkewX() * ty;
146
147 m[3] = viewMatrix.getSkewY() * sx;
148 m[4] = viewMatrix.getScaleY() * sy;
149 m[5] = viewMatrix.getTranslateY() +
150 viewMatrix.getSkewY() * tx + viewMatrix.getScaleY() * ty;
151
152 // Since 'm' is a 2x3 matrix that maps the rect [-1, +1] into the shape's device-space quad,
153 // it's quite simple to find the bounding rectangle:
154 float devBoundsHalfWidth = fabsf(m[0]) + fabsf(m[1]);
155 float devBoundsHalfHeight = fabsf(m[3]) + fabsf(m[4]);
Brian Salomon99ad1642016-12-16 09:50:45 -0500156 SkRect opBounds;
157 opBounds.fLeft = m[2] - devBoundsHalfWidth;
158 opBounds.fRight = m[2] + devBoundsHalfWidth;
159 opBounds.fTop = m[5] - devBoundsHalfHeight;
160 opBounds.fBottom = m[5] + devBoundsHalfHeight;
161 op->setBounds(opBounds, aaBloat, zeroArea);
csmartdaltona7f29642016-07-07 08:49:11 -0700162
163 // TODO: Is this worth the CPU overhead?
Brian Salomon99ad1642016-12-16 09:50:45 -0500164 op->fInfo.fNonSquare =
165 fabsf(devBoundsHalfHeight - devBoundsHalfWidth) > 0.5f || // Early out.
166 fabs(m[0] * m[3] + m[1] * m[4]) > 1e-3f || // Skew?
167 fabs(m[0] * m[0] + m[1] * m[1] - m[3] * m[3] - m[4] * m[4]) >
168 1e-2f; // Diff. lengths?
csmartdaltona7f29642016-07-07 08:49:11 -0700169 } else {
170 SkMatrix shapeMatrix(viewMatrix);
171 shapeMatrix.preTranslate(tx, ty);
172 shapeMatrix.preScale(sx, sy);
173 instance.fInfo |= kPerspective_InfoFlag;
174
175 float* m = instance.fShapeMatrix2x3;
176 m[0] = SkScalarToFloat(shapeMatrix.getScaleX());
177 m[1] = SkScalarToFloat(shapeMatrix.getSkewX());
178 m[2] = SkScalarToFloat(shapeMatrix.getTranslateX());
179 m[3] = SkScalarToFloat(shapeMatrix.getSkewY());
180 m[4] = SkScalarToFloat(shapeMatrix.getScaleY());
181 m[5] = SkScalarToFloat(shapeMatrix.getTranslateY());
182
183 // Send the perspective column as a param.
Brian Salomon99ad1642016-12-16 09:50:45 -0500184 op->appendParamsTexel(shapeMatrix[SkMatrix::kMPersp0], shapeMatrix[SkMatrix::kMPersp1],
185 shapeMatrix[SkMatrix::kMPersp2]);
186 op->fInfo.fHasPerspective = true;
csmartdaltona7f29642016-07-07 08:49:11 -0700187
Brian Salomon99ad1642016-12-16 09:50:45 -0500188 op->setBounds(bounds, aaBloat, zeroArea);
189 op->fInfo.fNonSquare = true;
csmartdaltona7f29642016-07-07 08:49:11 -0700190 }
191
192 instance.fColor = color;
193
194 const float* rectAsFloats = localRect.asScalars(); // Ensure SkScalar == float.
195 memcpy(&instance.fLocalRect, rectAsFloats, 4 * sizeof(float));
196
Brian Salomon99ad1642016-12-16 09:50:45 -0500197 op->fPixelLoad = op->bounds().height() * op->bounds().width();
198 return op;
csmartdaltona7f29642016-07-07 08:49:11 -0700199}
200
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500201inline bool InstancedRendering::selectAntialiasMode(const SkMatrix& viewMatrix, GrAA aa,
csmartdaltona7f29642016-07-07 08:49:11 -0700202 const GrInstancedPipelineInfo& info,
Brian Salomonaf9847e2017-03-01 11:28:27 -0500203 GrAAType* aaType) {
csmartdaltona7f29642016-07-07 08:49:11 -0700204 SkASSERT(!info.fIsMixedSampled || info.fIsMultisampled);
csmartdaltone0d36292016-07-29 08:14:20 -0700205 SkASSERT(GrCaps::InstancedSupport::kNone != fGpu->caps()->instancedSupport());
csmartdaltona7f29642016-07-07 08:49:11 -0700206
207 if (!info.fIsMultisampled || fGpu->caps()->multisampleDisableSupport()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500208 if (GrAA::kNo == aa) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500209 *aaType = GrAAType::kNone;
csmartdaltona7f29642016-07-07 08:49:11 -0700210 return true;
211 }
212
213 if (info.canUseCoverageAA() && viewMatrix.preservesRightAngles()) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500214 *aaType = GrAAType::kCoverage;
csmartdaltona7f29642016-07-07 08:49:11 -0700215 return true;
216 }
217 }
218
csmartdaltone0d36292016-07-29 08:14:20 -0700219 if (info.fIsMultisampled &&
220 fGpu->caps()->instancedSupport() >= GrCaps::InstancedSupport::kMultisampled) {
Brian Salomon5ff3a5c2017-03-01 14:34:41 -0500221 if (!info.fIsMixedSampled) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500222 *aaType = GrAAType::kMSAA;
csmartdaltona7f29642016-07-07 08:49:11 -0700223 return true;
224 }
csmartdaltone0d36292016-07-29 08:14:20 -0700225 if (fGpu->caps()->instancedSupport() >= GrCaps::InstancedSupport::kMixedSampled) {
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500226 *aaType = GrAAType::kMixedSamples;
csmartdaltona7f29642016-07-07 08:49:11 -0700227 return true;
228 }
229 }
230
231 return false;
232}
233
Brian Salomon54d212e2017-03-21 14:22:38 -0400234InstancedRendering::Op::Op(uint32_t classID, GrPaint&& paint, InstancedRendering* ir)
Brian Salomon99ad1642016-12-16 09:50:45 -0500235 : INHERITED(classID)
236 , fInstancedRendering(ir)
Brian Salomon54d212e2017-03-21 14:22:38 -0400237 , fProcessors(std::move(paint))
Brian Salomon99ad1642016-12-16 09:50:45 -0500238 , fIsTracked(false)
239 , fNumDraws(1)
240 , fNumChangesInGeometry(0) {
dskibae4cd0062016-11-29 06:50:35 -0800241 fHeadDraw = fTailDraw = fInstancedRendering->fDrawPool.allocate();
csmartdaltona7f29642016-07-07 08:49:11 -0700242#ifdef SK_DEBUG
243 fHeadDraw->fGeometry = {-1, 0};
244#endif
245 fHeadDraw->fNext = nullptr;
246}
247
Brian Salomon99ad1642016-12-16 09:50:45 -0500248InstancedRendering::Op::~Op() {
csmartdaltona7f29642016-07-07 08:49:11 -0700249 if (fIsTracked) {
Brian Salomon99ad1642016-12-16 09:50:45 -0500250 fInstancedRendering->fTrackedOps.remove(this);
csmartdaltona7f29642016-07-07 08:49:11 -0700251 }
252
253 Draw* draw = fHeadDraw;
254 while (draw) {
255 Draw* next = draw->fNext;
256 fInstancedRendering->fDrawPool.release(draw);
257 draw = next;
258 }
259}
260
Brian Salomon99ad1642016-12-16 09:50:45 -0500261void InstancedRendering::Op::appendRRectParams(const SkRRect& rrect) {
csmartdaltona7f29642016-07-07 08:49:11 -0700262 SkASSERT(!fIsTracked);
263 switch (rrect.getType()) {
264 case SkRRect::kSimple_Type: {
265 const SkVector& radii = rrect.getSimpleRadii();
266 this->appendParamsTexel(radii.x(), radii.y(), rrect.width(), rrect.height());
267 return;
268 }
269 case SkRRect::kNinePatch_Type: {
270 float twoOverW = 2 / rrect.width();
271 float twoOverH = 2 / rrect.height();
272 const SkVector& radiiTL = rrect.radii(SkRRect::kUpperLeft_Corner);
273 const SkVector& radiiBR = rrect.radii(SkRRect::kLowerRight_Corner);
274 this->appendParamsTexel(radiiTL.x() * twoOverW, radiiBR.x() * twoOverW,
275 radiiTL.y() * twoOverH, radiiBR.y() * twoOverH);
276 return;
277 }
278 case SkRRect::kComplex_Type: {
279 /**
280 * The x and y radii of each arc are stored in separate vectors,
281 * in the following order:
282 *
283 * __x1 _ _ _ x3__
284 * y1 | | y2
285 *
286 * | |
287 *
288 * y3 |__ _ _ _ __| y4
289 * x2 x4
290 *
291 */
292 float twoOverW = 2 / rrect.width();
293 float twoOverH = 2 / rrect.height();
294 const SkVector& radiiTL = rrect.radii(SkRRect::kUpperLeft_Corner);
295 const SkVector& radiiTR = rrect.radii(SkRRect::kUpperRight_Corner);
296 const SkVector& radiiBR = rrect.radii(SkRRect::kLowerRight_Corner);
297 const SkVector& radiiBL = rrect.radii(SkRRect::kLowerLeft_Corner);
298 this->appendParamsTexel(radiiTL.x() * twoOverW, radiiBL.x() * twoOverW,
299 radiiTR.x() * twoOverW, radiiBR.x() * twoOverW);
300 this->appendParamsTexel(radiiTL.y() * twoOverH, radiiTR.y() * twoOverH,
301 radiiBL.y() * twoOverH, radiiBR.y() * twoOverH);
302 return;
303 }
304 default: return;
305 }
306}
307
Brian Salomon99ad1642016-12-16 09:50:45 -0500308void InstancedRendering::Op::appendParamsTexel(const SkScalar* vals, int count) {
csmartdaltona7f29642016-07-07 08:49:11 -0700309 SkASSERT(!fIsTracked);
310 SkASSERT(count <= 4 && count >= 0);
311 const float* valsAsFloats = vals; // Ensure SkScalar == float.
312 memcpy(&fParams.push_back(), valsAsFloats, count * sizeof(float));
313 fInfo.fHasParams = true;
314}
315
Brian Salomon99ad1642016-12-16 09:50:45 -0500316void InstancedRendering::Op::appendParamsTexel(SkScalar x, SkScalar y, SkScalar z, SkScalar w) {
csmartdaltona7f29642016-07-07 08:49:11 -0700317 SkASSERT(!fIsTracked);
318 ParamsTexel& texel = fParams.push_back();
319 texel.fX = SkScalarToFloat(x);
320 texel.fY = SkScalarToFloat(y);
321 texel.fZ = SkScalarToFloat(z);
322 texel.fW = SkScalarToFloat(w);
323 fInfo.fHasParams = true;
324}
325
Brian Salomon99ad1642016-12-16 09:50:45 -0500326void InstancedRendering::Op::appendParamsTexel(SkScalar x, SkScalar y, SkScalar z) {
csmartdaltona7f29642016-07-07 08:49:11 -0700327 SkASSERT(!fIsTracked);
328 ParamsTexel& texel = fParams.push_back();
329 texel.fX = SkScalarToFloat(x);
330 texel.fY = SkScalarToFloat(y);
331 texel.fZ = SkScalarToFloat(z);
332 fInfo.fHasParams = true;
333}
334
Brian Salomon54d212e2017-03-21 14:22:38 -0400335bool InstancedRendering::Op::xpRequiresDstTexture(const GrCaps& caps, const GrAppliedClip* clip) {
336 GrProcessorSet::FragmentProcessorAnalysis analysis;
Brian Salomonc0b642c2017-03-27 13:09:36 -0400337 GrPipelineAnalysisCoverage coverageInput;
Brian Salomonaf9847e2017-03-01 11:28:27 -0500338 if (GrAAType::kCoverage == fInfo.aaType() ||
339 (GrAAType::kNone == fInfo.aaType() && !fInfo.isSimpleRects() && fInfo.fCannotDiscard)) {
Brian Salomonc0b642c2017-03-27 13:09:36 -0400340 coverageInput = GrPipelineAnalysisCoverage::kSingleChannel;
csmartdaltona7f29642016-07-07 08:49:11 -0700341 } else {
Brian Salomonc0b642c2017-03-27 13:09:36 -0400342 coverageInput = GrPipelineAnalysisCoverage::kNone;
csmartdaltona7f29642016-07-07 08:49:11 -0700343 }
Brian Salomon70288c02017-03-24 12:27:17 -0400344 fProcessors.analyzeAndEliminateFragmentProcessors(&analysis, this->getSingleInstance().fColor,
345 coverageInput, clip, caps);
csmartdaltona7f29642016-07-07 08:49:11 -0700346 Draw& draw = this->getSingleDraw(); // This will assert if we have > 1 command.
347 SkASSERT(draw.fGeometry.isEmpty());
348 SkASSERT(SkIsPow2(fInfo.fShapeTypes));
349 SkASSERT(!fIsTracked);
350
351 if (kRect_ShapeFlag == fInfo.fShapeTypes) {
Brian Salomonaf9847e2017-03-01 11:28:27 -0500352 draw.fGeometry = InstanceProcessor::GetIndexRangeForRect(fInfo.aaType());
csmartdaltona7f29642016-07-07 08:49:11 -0700353 } else if (kOval_ShapeFlag == fInfo.fShapeTypes) {
Brian Salomonaf9847e2017-03-01 11:28:27 -0500354 draw.fGeometry = InstanceProcessor::GetIndexRangeForOval(fInfo.aaType(), this->bounds());
csmartdaltona7f29642016-07-07 08:49:11 -0700355 } else {
Brian Salomonaf9847e2017-03-01 11:28:27 -0500356 draw.fGeometry = InstanceProcessor::GetIndexRangeForRRect(fInfo.aaType());
csmartdaltona7f29642016-07-07 08:49:11 -0700357 }
358
359 if (!fParams.empty()) {
360 SkASSERT(fInstancedRendering->fParams.count() < (int)kParamsIdx_InfoMask); // TODO: cleaner.
361 this->getSingleInstance().fInfo |= fInstancedRendering->fParams.count();
362 fInstancedRendering->fParams.push_back_n(fParams.count(), fParams.begin());
363 }
364
365 GrColor overrideColor;
Brian Salomon70288c02017-03-24 12:27:17 -0400366 if (analysis.getInputColorOverrideAndColorProcessorEliminationCount(&overrideColor) >= 0) {
csmartdaltona7f29642016-07-07 08:49:11 -0700367 SkASSERT(State::kRecordingDraws == fInstancedRendering->fState);
Brian Salomon54d212e2017-03-21 14:22:38 -0400368 this->getSingleDraw().fInstance.fColor = overrideColor;
csmartdaltona7f29642016-07-07 08:49:11 -0700369 }
Brian Salomon54d212e2017-03-21 14:22:38 -0400370 fInfo.fCannotTweakAlphaForCoverage =
371 !analysis.isCompatibleWithCoverageAsAlpha() ||
372 !GrXPFactory::CompatibleWithCoverageAsAlpha(fProcessors.xpFactory(),
373 analysis.isOutputColorOpaque());
374
375 fInfo.fUsesLocalCoords = analysis.usesLocalCoords();
376 return GrXPFactory::WillNeedDstTexture(fProcessors.xpFactory(), caps, analysis);
Brian Salomond543e0a2017-03-06 16:36:49 -0500377}
csmartdaltona7f29642016-07-07 08:49:11 -0700378
Brian Salomond543e0a2017-03-06 16:36:49 -0500379void InstancedRendering::Op::wasRecorded() {
380 SkASSERT(!fIsTracked);
Brian Salomon99ad1642016-12-16 09:50:45 -0500381 fInstancedRendering->fTrackedOps.addToTail(this);
Brian Salomon54d212e2017-03-21 14:22:38 -0400382 fProcessors.makePendingExecution();
csmartdaltona7f29642016-07-07 08:49:11 -0700383 fIsTracked = true;
384}
385
Brian Salomon99ad1642016-12-16 09:50:45 -0500386bool InstancedRendering::Op::onCombineIfPossible(GrOp* other, const GrCaps& caps) {
387 Op* that = static_cast<Op*>(other);
csmartdaltona7f29642016-07-07 08:49:11 -0700388 SkASSERT(fInstancedRendering == that->fInstancedRendering);
389 SkASSERT(fTailDraw);
390 SkASSERT(that->fTailDraw);
391
Brian Salomon54d212e2017-03-21 14:22:38 -0400392 if (!OpInfo::CanCombine(fInfo, that->fInfo) || fProcessors != that->fProcessors) {
csmartdaltona7f29642016-07-07 08:49:11 -0700393 return false;
394 }
395
Brian Salomon99ad1642016-12-16 09:50:45 -0500396 OpInfo combinedInfo = fInfo | that->fInfo;
csmartdaltona7f29642016-07-07 08:49:11 -0700397 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
bsalomon88cf17d2016-07-08 06:40:56 -0700410 this->joinBounds(*that);
csmartdaltona7f29642016-07-07 08:49:11 -0700411 fInfo = combinedInfo;
412 fPixelLoad += that->fPixelLoad;
Brian Salomon54d212e2017-03-21 14:22:38 -0400413 fDrawColorsAreOpaque = fDrawColorsAreOpaque && that->fDrawColorsAreOpaque;
414 fDrawColorsAreSame = fDrawColorsAreSame && that->fDrawColorsAreSame &&
415 fHeadDraw->fInstance.fColor == that->fHeadDraw->fInstance.fColor;
Brian Salomon99ad1642016-12-16 09:50:45 -0500416 // Adopt the other op's draws.
csmartdaltona7f29642016-07-07 08:49:11 -0700417 fNumDraws += that->fNumDraws;
418 fNumChangesInGeometry += that->fNumChangesInGeometry;
419 if (fTailDraw->fGeometry != that->fHeadDraw->fGeometry) {
420 ++fNumChangesInGeometry;
421 }
422 fTailDraw->fNext = that->fHeadDraw;
423 fTailDraw = that->fTailDraw;
424
425 that->fHeadDraw = that->fTailDraw = nullptr;
426
427 return true;
428}
429
430void InstancedRendering::beginFlush(GrResourceProvider* rp) {
431 SkASSERT(State::kRecordingDraws == fState);
432 fState = State::kFlushing;
433
Brian Salomon99ad1642016-12-16 09:50:45 -0500434 if (fTrackedOps.isEmpty()) {
csmartdaltona7f29642016-07-07 08:49:11 -0700435 return;
436 }
437
438 if (!fVertexBuffer) {
Hal Canary144caf52016-11-07 17:57:18 -0500439 fVertexBuffer.reset(InstanceProcessor::FindOrCreateVertexBuffer(fGpu.get()));
csmartdaltona7f29642016-07-07 08:49:11 -0700440 if (!fVertexBuffer) {
441 return;
442 }
443 }
444
445 if (!fIndexBuffer) {
Hal Canary144caf52016-11-07 17:57:18 -0500446 fIndexBuffer.reset(InstanceProcessor::FindOrCreateIndex8Buffer(fGpu.get()));
csmartdaltona7f29642016-07-07 08:49:11 -0700447 if (!fIndexBuffer) {
448 return;
449 }
450 }
451
452 if (!fParams.empty()) {
453 fParamsBuffer.reset(rp->createBuffer(fParams.count() * sizeof(ParamsTexel),
454 kTexel_GrBufferType, kDynamic_GrAccessPattern,
csmartdalton485a1202016-07-13 10:16:32 -0700455 GrResourceProvider::kNoPendingIO_Flag |
456 GrResourceProvider::kRequireGpuMemory_Flag,
csmartdaltona7f29642016-07-07 08:49:11 -0700457 fParams.begin()));
458 if (!fParamsBuffer) {
459 return;
460 }
461 }
462
463 this->onBeginFlush(rp);
464}
465
Brian Salomon9e50f7b2017-03-06 12:02:34 -0500466void InstancedRendering::Op::onExecute(GrOpFlushState* state) {
csmartdaltona7f29642016-07-07 08:49:11 -0700467 SkASSERT(State::kFlushing == fInstancedRendering->fState);
468 SkASSERT(state->gpu() == fInstancedRendering->gpu());
469
470 state->gpu()->handleDirtyContext();
Brian Salomon2bf4b3a2017-03-16 14:19:07 -0400471
Brian Salomon54d212e2017-03-21 14:22:38 -0400472 GrProcessorSet::FragmentProcessorAnalysis analysis;
Brian Salomonc0b642c2017-03-27 13:09:36 -0400473 GrPipelineAnalysisCoverage coverageInput;
Brian Salomon54d212e2017-03-21 14:22:38 -0400474 if (GrAAType::kCoverage == fInfo.aaType() ||
475 (GrAAType::kNone == fInfo.aaType() && !fInfo.isSimpleRects() && fInfo.fCannotDiscard)) {
Brian Salomonc0b642c2017-03-27 13:09:36 -0400476 coverageInput = GrPipelineAnalysisCoverage::kSingleChannel;
Brian Salomon54d212e2017-03-21 14:22:38 -0400477 } else {
Brian Salomonc0b642c2017-03-27 13:09:36 -0400478 coverageInput = GrPipelineAnalysisCoverage::kNone;
Brian Salomon54d212e2017-03-21 14:22:38 -0400479 }
Brian Salomonc0b642c2017-03-27 13:09:36 -0400480 GrPipelineAnalysisColor colorInput;
Brian Salomon54d212e2017-03-21 14:22:38 -0400481 if (fDrawColorsAreSame) {
482 colorInput = fHeadDraw->fInstance.fColor;
483 } else if (fDrawColorsAreOpaque) {
Brian Salomonc0b642c2017-03-27 13:09:36 -0400484 colorInput = GrPipelineAnalysisColor::Opaque::kYes;
Brian Salomon54d212e2017-03-21 14:22:38 -0400485 }
486 const GrAppliedClip* clip = state->drawOpArgs().fAppliedClip;
487 analysis.init(colorInput, coverageInput, fProcessors, clip, state->caps());
488
489 GrPipeline pipeline;
490 GrPipeline::InitArgs args;
491 args.fAnalysis = &analysis;
492 args.fAppliedClip = clip;
493 args.fCaps = &state->caps();
494 args.fProcessors = &fProcessors;
495 args.fFlags = GrAATypeIsHW(fInfo.aaType()) ? GrPipeline::kHWAntialias_Flag : 0;
496 args.fRenderTarget = state->drawOpArgs().fRenderTarget;
497 args.fDstTexture = state->drawOpArgs().fDstTexture;
498 pipeline.init(args);
499
500 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(*state->gpu()->caps())) {
501 state->gpu()->xferBarrier(pipeline.getRenderTarget(), barrierType);
502 }
Hal Canary144caf52016-11-07 17:57:18 -0500503 InstanceProcessor instProc(fInfo, fInstancedRendering->fParamsBuffer.get());
Brian Salomon54d212e2017-03-21 14:22:38 -0400504 fInstancedRendering->onDraw(pipeline, instProc, this);
csmartdaltona7f29642016-07-07 08:49:11 -0700505}
506
507void InstancedRendering::endFlush() {
Brian Salomon92aee3d2016-12-21 09:20:25 -0500508 // The caller is expected to delete all tracked ops (i.e. ops whose applyPipelineOptimizations
csmartdaltona7f29642016-07-07 08:49:11 -0700509 // method has been called) before ending the flush.
Brian Salomon99ad1642016-12-16 09:50:45 -0500510 SkASSERT(fTrackedOps.isEmpty());
csmartdaltona7f29642016-07-07 08:49:11 -0700511 fParams.reset();
512 fParamsBuffer.reset();
513 this->onEndFlush();
514 fState = State::kRecordingDraws;
515 // Hold on to the shape coords and index buffers.
516}
517
518void InstancedRendering::resetGpuResources(ResetType resetType) {
519 fVertexBuffer.reset();
520 fIndexBuffer.reset();
521 fParamsBuffer.reset();
522 this->onResetGpuResources(resetType);
523}
524
525}