blob: 2550f9d9b518ccd47c3107bbc0a8d4a4cc9fc96b [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 Salomon31853842017-03-28 16:32:05 -0400370 fInfo.fCannotTweakAlphaForCoverage = !analysis.isCompatibleWithCoverageAsAlpha();
Brian Salomon54d212e2017-03-21 14:22:38 -0400371
372 fInfo.fUsesLocalCoords = analysis.usesLocalCoords();
Brian Salomon31853842017-03-28 16:32:05 -0400373 return analysis.requiresDstTexture();
Brian Salomond543e0a2017-03-06 16:36:49 -0500374}
csmartdaltona7f29642016-07-07 08:49:11 -0700375
Brian Salomond543e0a2017-03-06 16:36:49 -0500376void InstancedRendering::Op::wasRecorded() {
377 SkASSERT(!fIsTracked);
Brian Salomon99ad1642016-12-16 09:50:45 -0500378 fInstancedRendering->fTrackedOps.addToTail(this);
Brian Salomon54d212e2017-03-21 14:22:38 -0400379 fProcessors.makePendingExecution();
csmartdaltona7f29642016-07-07 08:49:11 -0700380 fIsTracked = true;
381}
382
Brian Salomon99ad1642016-12-16 09:50:45 -0500383bool InstancedRendering::Op::onCombineIfPossible(GrOp* other, const GrCaps& caps) {
384 Op* that = static_cast<Op*>(other);
csmartdaltona7f29642016-07-07 08:49:11 -0700385 SkASSERT(fInstancedRendering == that->fInstancedRendering);
386 SkASSERT(fTailDraw);
387 SkASSERT(that->fTailDraw);
388
Brian Salomon54d212e2017-03-21 14:22:38 -0400389 if (!OpInfo::CanCombine(fInfo, that->fInfo) || fProcessors != that->fProcessors) {
csmartdaltona7f29642016-07-07 08:49:11 -0700390 return false;
391 }
392
Brian Salomon99ad1642016-12-16 09:50:45 -0500393 OpInfo combinedInfo = fInfo | that->fInfo;
csmartdaltona7f29642016-07-07 08:49:11 -0700394 if (!combinedInfo.isSimpleRects()) {
395 // This threshold was chosen with the "shapes_mixed" bench on a MacBook with Intel graphics.
396 // There seems to be a wide range where it doesn't matter if we combine or not. What matters
397 // is that the itty bitty rects combine with other shapes and the giant ones don't.
398 constexpr SkScalar kMaxPixelsToGeneralizeRects = 256 * 256;
399 if (fInfo.isSimpleRects() && fPixelLoad > kMaxPixelsToGeneralizeRects) {
400 return false;
401 }
402 if (that->fInfo.isSimpleRects() && that->fPixelLoad > kMaxPixelsToGeneralizeRects) {
403 return false;
404 }
405 }
406
bsalomon88cf17d2016-07-08 06:40:56 -0700407 this->joinBounds(*that);
csmartdaltona7f29642016-07-07 08:49:11 -0700408 fInfo = combinedInfo;
409 fPixelLoad += that->fPixelLoad;
Brian Salomon54d212e2017-03-21 14:22:38 -0400410 fDrawColorsAreOpaque = fDrawColorsAreOpaque && that->fDrawColorsAreOpaque;
411 fDrawColorsAreSame = fDrawColorsAreSame && that->fDrawColorsAreSame &&
412 fHeadDraw->fInstance.fColor == that->fHeadDraw->fInstance.fColor;
Brian Salomon99ad1642016-12-16 09:50:45 -0500413 // Adopt the other op's draws.
csmartdaltona7f29642016-07-07 08:49:11 -0700414 fNumDraws += that->fNumDraws;
415 fNumChangesInGeometry += that->fNumChangesInGeometry;
416 if (fTailDraw->fGeometry != that->fHeadDraw->fGeometry) {
417 ++fNumChangesInGeometry;
418 }
419 fTailDraw->fNext = that->fHeadDraw;
420 fTailDraw = that->fTailDraw;
421
422 that->fHeadDraw = that->fTailDraw = nullptr;
423
424 return true;
425}
426
427void InstancedRendering::beginFlush(GrResourceProvider* rp) {
428 SkASSERT(State::kRecordingDraws == fState);
429 fState = State::kFlushing;
430
Brian Salomon99ad1642016-12-16 09:50:45 -0500431 if (fTrackedOps.isEmpty()) {
csmartdaltona7f29642016-07-07 08:49:11 -0700432 return;
433 }
434
435 if (!fVertexBuffer) {
Hal Canary144caf52016-11-07 17:57:18 -0500436 fVertexBuffer.reset(InstanceProcessor::FindOrCreateVertexBuffer(fGpu.get()));
csmartdaltona7f29642016-07-07 08:49:11 -0700437 if (!fVertexBuffer) {
438 return;
439 }
440 }
441
442 if (!fIndexBuffer) {
Hal Canary144caf52016-11-07 17:57:18 -0500443 fIndexBuffer.reset(InstanceProcessor::FindOrCreateIndex8Buffer(fGpu.get()));
csmartdaltona7f29642016-07-07 08:49:11 -0700444 if (!fIndexBuffer) {
445 return;
446 }
447 }
448
449 if (!fParams.empty()) {
450 fParamsBuffer.reset(rp->createBuffer(fParams.count() * sizeof(ParamsTexel),
451 kTexel_GrBufferType, kDynamic_GrAccessPattern,
csmartdalton485a1202016-07-13 10:16:32 -0700452 GrResourceProvider::kNoPendingIO_Flag |
453 GrResourceProvider::kRequireGpuMemory_Flag,
csmartdaltona7f29642016-07-07 08:49:11 -0700454 fParams.begin()));
455 if (!fParamsBuffer) {
456 return;
457 }
458 }
459
460 this->onBeginFlush(rp);
461}
462
Brian Salomon9e50f7b2017-03-06 12:02:34 -0500463void InstancedRendering::Op::onExecute(GrOpFlushState* state) {
csmartdaltona7f29642016-07-07 08:49:11 -0700464 SkASSERT(State::kFlushing == fInstancedRendering->fState);
465 SkASSERT(state->gpu() == fInstancedRendering->gpu());
466
467 state->gpu()->handleDirtyContext();
Brian Salomon2bf4b3a2017-03-16 14:19:07 -0400468
Brian Salomon1c6025c2017-03-29 14:25:04 -0400469 // TODO: Don't reanalyze the processors.
Brian Salomon54d212e2017-03-21 14:22:38 -0400470 GrProcessorSet::FragmentProcessorAnalysis analysis;
Brian Salomonc0b642c2017-03-27 13:09:36 -0400471 GrPipelineAnalysisCoverage coverageInput;
Brian Salomon54d212e2017-03-21 14:22:38 -0400472 if (GrAAType::kCoverage == fInfo.aaType() ||
473 (GrAAType::kNone == fInfo.aaType() && !fInfo.isSimpleRects() && fInfo.fCannotDiscard)) {
Brian Salomonc0b642c2017-03-27 13:09:36 -0400474 coverageInput = GrPipelineAnalysisCoverage::kSingleChannel;
Brian Salomon54d212e2017-03-21 14:22:38 -0400475 } else {
Brian Salomonc0b642c2017-03-27 13:09:36 -0400476 coverageInput = GrPipelineAnalysisCoverage::kNone;
Brian Salomon54d212e2017-03-21 14:22:38 -0400477 }
Brian Salomonc0b642c2017-03-27 13:09:36 -0400478 GrPipelineAnalysisColor colorInput;
Brian Salomon54d212e2017-03-21 14:22:38 -0400479 if (fDrawColorsAreSame) {
480 colorInput = fHeadDraw->fInstance.fColor;
481 } else if (fDrawColorsAreOpaque) {
Brian Salomonc0b642c2017-03-27 13:09:36 -0400482 colorInput = GrPipelineAnalysisColor::Opaque::kYes;
Brian Salomon54d212e2017-03-21 14:22:38 -0400483 }
484 const GrAppliedClip* clip = state->drawOpArgs().fAppliedClip;
485 analysis.init(colorInput, coverageInput, fProcessors, clip, state->caps());
486
487 GrPipeline pipeline;
488 GrPipeline::InitArgs args;
Brian Salomon1c6025c2017-03-29 14:25:04 -0400489 args.fInputColor = analysis.outputColor();
490 args.fInputCoverage = analysis.outputCoverage();
Brian Salomon54d212e2017-03-21 14:22:38 -0400491 args.fAppliedClip = clip;
492 args.fCaps = &state->caps();
493 args.fProcessors = &fProcessors;
494 args.fFlags = GrAATypeIsHW(fInfo.aaType()) ? GrPipeline::kHWAntialias_Flag : 0;
495 args.fRenderTarget = state->drawOpArgs().fRenderTarget;
496 args.fDstTexture = state->drawOpArgs().fDstTexture;
497 pipeline.init(args);
498
499 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(*state->gpu()->caps())) {
500 state->gpu()->xferBarrier(pipeline.getRenderTarget(), barrierType);
501 }
Hal Canary144caf52016-11-07 17:57:18 -0500502 InstanceProcessor instProc(fInfo, fInstancedRendering->fParamsBuffer.get());
Brian Salomon54d212e2017-03-21 14:22:38 -0400503 fInstancedRendering->onDraw(pipeline, instProc, this);
csmartdaltona7f29642016-07-07 08:49:11 -0700504}
505
506void InstancedRendering::endFlush() {
Brian Salomon92aee3d2016-12-21 09:20:25 -0500507 // The caller is expected to delete all tracked ops (i.e. ops whose applyPipelineOptimizations
csmartdaltona7f29642016-07-07 08:49:11 -0700508 // method has been called) before ending the flush.
Brian Salomon99ad1642016-12-16 09:50:45 -0500509 SkASSERT(fTrackedOps.isEmpty());
csmartdaltona7f29642016-07-07 08:49:11 -0700510 fParams.reset();
511 fParamsBuffer.reset();
512 this->onEndFlush();
513 fState = State::kRecordingDraws;
514 // Hold on to the shape coords and index buffers.
515}
516
517void InstancedRendering::resetGpuResources(ResetType resetType) {
518 fVertexBuffer.reset();
519 fIndexBuffer.reset();
520 fParamsBuffer.reset();
521 this->onResetGpuResources(resetType);
522}
523
524}