blob: b05481c87ca9e2ed39fed68fb6229f42d15958bf [file] [log] [blame]
joshualitt2771b562015-08-07 12:46:26 -07001/*
2 * Copyright 2015 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
Brian Salomonfc527d22016-12-14 21:07:01 -05008#include "GrDrawVerticesOp.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -04009#include "GrCaps.h"
joshualitt2771b562015-08-07 12:46:26 -070010#include "GrDefaultGeoProcFactory.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050011#include "GrOpFlushState.h"
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -050012#include "GrSimpleMeshDrawOpHelper.h"
Brian Osman3b655982017-03-07 16:58:08 -050013#include "SkGr.h"
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -040014#include "SkRectPriv.h"
joshualitt2771b562015-08-07 12:46:26 -070015
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -050016namespace {
Brian Salomon199fb872017-02-06 09:41:10 -050017
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -050018class DrawVerticesOp final : public GrMeshDrawOp {
19private:
20 using Helper = GrSimpleMeshDrawOpHelper;
21
22public:
23 DEFINE_OP_CLASS_ID
24
25 DrawVerticesOp(const Helper::MakeArgs&, const SkPMColor4f&, sk_sp<SkVertices>,
26 const SkVertices::Bone bones[], int boneCount, GrPrimitiveType, GrAAType,
27 sk_sp<GrColorSpaceXform>, const SkMatrix& viewMatrix);
28
29 const char* name() const override { return "DrawVerticesOp"; }
30
31 void visitProxies(const VisitProxyFunc& func, VisitorType) const override {
32 fHelper.visitProxies(func);
33 }
34
35#ifdef SK_DEBUG
36 SkString dumpInfo() const override;
37#endif
38
39 FixedFunctionFlags fixedFunctionFlags() const override;
40
Chris Daltonb8fff0d2019-03-05 10:11:58 -070041 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, GrFSAAType) override;
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -050042
43private:
44 enum class ColorArrayType {
45 kPremulGrColor,
46 kSkColor,
47 };
48
49 void onPrepareDraws(Target*) override;
Chris Dalton07cdcfc92019-02-26 11:13:22 -070050 void onExecute(GrOpFlushState*, const SkRect& chainBounds) override;
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -050051
52 void drawVolatile(Target*);
53 void drawNonVolatile(Target*);
54
55 void fillBuffers(bool hasColorAttribute,
56 bool hasLocalCoordsAttribute,
57 size_t vertexStride,
58 void* verts,
59 uint16_t* indices) const;
60
61 void drawVertices(Target*,
62 sk_sp<const GrGeometryProcessor>,
63 sk_sp<const GrBuffer> vertexBuffer,
64 int firstVertex,
65 sk_sp<const GrBuffer> indexBuffer,
66 int firstIndex);
67
68 sk_sp<GrGeometryProcessor> makeGP(const GrShaderCaps* shaderCaps,
69 bool* hasColorAttribute,
70 bool* hasLocalCoordAttribute) const;
71
72 GrPrimitiveType primitiveType() const { return fPrimitiveType; }
73 bool combinablePrimitive() const {
74 return GrPrimitiveType::kTriangles == fPrimitiveType ||
75 GrPrimitiveType::kLines == fPrimitiveType ||
76 GrPrimitiveType::kPoints == fPrimitiveType;
77 }
78
79 CombineResult onCombineIfPossible(GrOp* t, const GrCaps&) override;
80
81 struct Mesh {
82 SkPMColor4f fColor; // Used if this->hasPerVertexColors() is false.
83 sk_sp<SkVertices> fVertices;
84 SkMatrix fViewMatrix;
85 bool fIgnoreTexCoords;
86 bool fIgnoreColors;
87
88 bool hasExplicitLocalCoords() const {
89 return fVertices->hasTexCoords() && !fIgnoreTexCoords;
90 }
91
92 bool hasPerVertexColors() const {
93 return fVertices->hasColors() && !fIgnoreColors;
94 }
95 };
96
97 bool isIndexed() const {
98 // Consistency enforced in onCombineIfPossible.
99 return fMeshes[0].fVertices->hasIndices();
100 }
101
102 bool requiresPerVertexColors() const {
103 return SkToBool(kRequiresPerVertexColors_Flag & fFlags);
104 }
105
106 bool anyMeshHasExplicitLocalCoords() const {
107 return SkToBool(kAnyMeshHasExplicitLocalCoords_Flag & fFlags);
108 }
109
110 bool hasMultipleViewMatrices() const {
111 return SkToBool(kHasMultipleViewMatrices_Flag & fFlags);
112 }
113
114 enum Flags {
115 kRequiresPerVertexColors_Flag = 0x1,
116 kAnyMeshHasExplicitLocalCoords_Flag = 0x2,
117 kHasMultipleViewMatrices_Flag = 0x4,
118 };
119
120 Helper fHelper;
121 SkSTArray<1, Mesh, true> fMeshes;
122 // GrPrimitiveType is more expressive than fVertices.mode() so it is used instead and we ignore
123 // the SkVertices mode (though fPrimitiveType may have been inferred from it).
124 GrPrimitiveType fPrimitiveType;
125 uint32_t fFlags;
126 int fVertexCount;
127 int fIndexCount;
128 ColorArrayType fColorArrayType;
129 sk_sp<GrColorSpaceXform> fColorSpaceXform;
130
131 typedef GrMeshDrawOp INHERITED;
132};
133
134DrawVerticesOp::DrawVerticesOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
135 sk_sp<SkVertices> vertices, const SkVertices::Bone bones[],
136 int boneCount, GrPrimitiveType primitiveType, GrAAType aaType,
137 sk_sp<GrColorSpaceXform> colorSpaceXform,
138 const SkMatrix& viewMatrix)
Brian Osmanfa6d8652017-05-31 09:37:27 -0400139 : INHERITED(ClassID())
Brian Salomonc2f42542017-07-12 14:11:22 -0400140 , fHelper(helperArgs, aaType)
Brian Osmanfa6d8652017-05-31 09:37:27 -0400141 , fPrimitiveType(primitiveType)
142 , fColorSpaceXform(std::move(colorSpaceXform)) {
Brian Salomon199fb872017-02-06 09:41:10 -0500143 SkASSERT(vertices);
144
145 fVertexCount = vertices->vertexCount();
146 fIndexCount = vertices->indexCount();
Brian Osmanae0c50c2017-05-25 16:56:34 -0400147 fColorArrayType = vertices->hasColors() ? ColorArrayType::kSkColor
148 : ColorArrayType::kPremulGrColor;
joshualitt2771b562015-08-07 12:46:26 -0700149
bsalomond92b4192016-06-30 07:59:23 -0700150 Mesh& mesh = fMeshes.push_back();
151 mesh.fColor = color;
Brian Salomon3f363692017-02-02 21:05:19 -0500152 mesh.fViewMatrix = viewMatrix;
Brian Salomon199fb872017-02-06 09:41:10 -0500153 mesh.fVertices = std::move(vertices);
Brian Osman8a030552017-05-23 15:03:18 -0400154 mesh.fIgnoreTexCoords = false;
155 mesh.fIgnoreColors = false;
joshualitt2771b562015-08-07 12:46:26 -0700156
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400157 if (mesh.fVertices->hasBones() && bones) {
158 // Perform the transformations on the CPU instead of the GPU.
159 mesh.fVertices = mesh.fVertices->applyBones(bones, boneCount);
160 } else {
Brian Osman37064c12019-02-08 10:53:07 -0500161 SkASSERT(!bones || boneCount == 1);
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400162 }
163
Brian Salomon199fb872017-02-06 09:41:10 -0500164 fFlags = 0;
165 if (mesh.hasPerVertexColors()) {
166 fFlags |= kRequiresPerVertexColors_Flag;
joshualitt2771b562015-08-07 12:46:26 -0700167 }
Brian Salomon199fb872017-02-06 09:41:10 -0500168 if (mesh.hasExplicitLocalCoords()) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400169 fFlags |= kAnyMeshHasExplicitLocalCoords_Flag;
joshualitt2771b562015-08-07 12:46:26 -0700170 }
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400171
172 // Special case for meshes with a world transform but no bone weights.
173 // These will be considered normal vertices draws without bones.
174 if (!mesh.fVertices->hasBones() && boneCount == 1) {
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400175 SkMatrix worldTransform;
176 worldTransform.setAffine(bones[0].values);
177 mesh.fViewMatrix.preConcat(worldTransform);
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400178 }
joshualitt2771b562015-08-07 12:46:26 -0700179
bsalomon88cf17d2016-07-08 06:40:56 -0700180 IsZeroArea zeroArea;
Chris Dalton3809bab2017-06-13 10:55:06 -0600181 if (GrIsPrimTypeLines(primitiveType) || GrPrimitiveType::kPoints == primitiveType) {
bsalomon88cf17d2016-07-08 06:40:56 -0700182 zeroArea = IsZeroArea::kYes;
183 } else {
184 zeroArea = IsZeroArea::kNo;
185 }
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400186
Brian Osman37064c12019-02-08 10:53:07 -0500187 this->setTransformedBounds(mesh.fVertices->bounds(),
188 mesh.fViewMatrix,
189 HasAABloat::kNo,
190 zeroArea);
joshualitt2771b562015-08-07 12:46:26 -0700191}
192
Brian Osman9a390ac2018-11-12 09:47:48 -0500193#ifdef SK_DEBUG
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500194SkString DrawVerticesOp::dumpInfo() const {
Brian Salomonc2f42542017-07-12 14:11:22 -0400195 SkString string;
196 string.appendf("PrimType: %d, MeshCount %d, VCount: %d, ICount: %d\n", (int)fPrimitiveType,
197 fMeshes.count(), fVertexCount, fIndexCount);
198 string += fHelper.dumpInfo();
199 string += INHERITED::dumpInfo();
200 return string;
joshualitt2771b562015-08-07 12:46:26 -0700201}
Brian Osman9a390ac2018-11-12 09:47:48 -0500202#endif
joshualitt2771b562015-08-07 12:46:26 -0700203
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500204GrDrawOp::FixedFunctionFlags DrawVerticesOp::fixedFunctionFlags() const {
Brian Salomonc2f42542017-07-12 14:11:22 -0400205 return fHelper.fixedFunctionFlags();
206}
207
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700208GrProcessorSet::Analysis DrawVerticesOp::finalize(
209 const GrCaps& caps, const GrAppliedClip* clip, GrFSAAType fsaaType) {
Brian Salomonc2f42542017-07-12 14:11:22 -0400210 GrProcessorAnalysisColor gpColor;
211 if (this->requiresPerVertexColors()) {
212 gpColor.setToUnknown();
213 } else {
214 gpColor.setToConstant(fMeshes.front().fColor);
215 }
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700216 auto result = fHelper.finalizeProcessors(
217 caps, clip, fsaaType, GrProcessorAnalysisCoverage::kNone, &gpColor);
Brian Salomonc2f42542017-07-12 14:11:22 -0400218 if (gpColor.isConstant(&fMeshes.front().fColor)) {
219 fMeshes.front().fIgnoreColors = true;
Brian Salomon199fb872017-02-06 09:41:10 -0500220 fFlags &= ~kRequiresPerVertexColors_Flag;
Brian Osmanae0c50c2017-05-25 16:56:34 -0400221 fColorArrayType = ColorArrayType::kPremulGrColor;
joshualitt2771b562015-08-07 12:46:26 -0700222 }
Brian Salomonc2f42542017-07-12 14:11:22 -0400223 if (!fHelper.usesLocalCoords()) {
Brian Osman8a030552017-05-23 15:03:18 -0400224 fMeshes[0].fIgnoreTexCoords = true;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400225 fFlags &= ~kAnyMeshHasExplicitLocalCoords_Flag;
bsalomon14eaaa62015-09-24 07:01:26 -0700226 }
Brian Salomonc2f42542017-07-12 14:11:22 -0400227 return result;
joshualitt2771b562015-08-07 12:46:26 -0700228}
229
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500230sk_sp<GrGeometryProcessor> DrawVerticesOp::makeGP(const GrShaderCaps* shaderCaps,
231 bool* hasColorAttribute,
232 bool* hasLocalCoordAttribute) const {
Brian Salomon199fb872017-02-06 09:41:10 -0500233 using namespace GrDefaultGeoProcFactory;
234 LocalCoords::Type localCoordsType;
Brian Salomonc2f42542017-07-12 14:11:22 -0400235 if (fHelper.usesLocalCoords()) {
Brian Salomon199fb872017-02-06 09:41:10 -0500236 // If we have multiple view matrices we will transform the positions into device space. We
237 // must then also provide untransformed positions as local coords.
238 if (this->anyMeshHasExplicitLocalCoords() || this->hasMultipleViewMatrices()) {
239 *hasLocalCoordAttribute = true;
240 localCoordsType = LocalCoords::kHasExplicit_Type;
241 } else {
242 *hasLocalCoordAttribute = false;
243 localCoordsType = LocalCoords::kUsePosition_Type;
244 }
245 } else {
246 localCoordsType = LocalCoords::kUnused_Type;
247 *hasLocalCoordAttribute = false;
248 }
249
250 Color color(fMeshes[0].fColor);
251 if (this->requiresPerVertexColors()) {
Brian Osman08a50e02018-06-15 15:06:48 -0400252 if (fColorArrayType == ColorArrayType::kPremulGrColor) {
253 color.fType = Color::kPremulGrColorAttribute_Type;
254 } else {
255 color.fType = Color::kUnpremulSkColorAttribute_Type;
256 color.fColorSpaceXform = fColorSpaceXform;
257 }
Brian Salomon199fb872017-02-06 09:41:10 -0500258 *hasColorAttribute = true;
259 } else {
260 *hasColorAttribute = false;
Brian Salomon23356442018-11-30 15:33:19 -0500261 }
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400262
Brian Salomon199fb872017-02-06 09:41:10 -0500263 const SkMatrix& vm = this->hasMultipleViewMatrices() ? SkMatrix::I() : fMeshes[0].fViewMatrix;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400264
Brian Osman37064c12019-02-08 10:53:07 -0500265 return GrDefaultGeoProcFactory::Make(shaderCaps,
266 color,
267 Coverage::kSolid_Type,
268 localCoordsType,
269 vm);
Brian Salomon199fb872017-02-06 09:41:10 -0500270}
271
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500272void DrawVerticesOp::onPrepareDraws(Target* target) {
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400273 bool hasMapBufferSupport = GrCaps::kNone_MapFlags != target->caps().mapBufferFlags();
274 if (fMeshes[0].fVertices->isVolatile() || !hasMapBufferSupport) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400275 this->drawVolatile(target);
276 } else {
277 this->drawNonVolatile(target);
278 }
279}
280
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500281void DrawVerticesOp::drawVolatile(Target* target) {
Brian Salomon199fb872017-02-06 09:41:10 -0500282 bool hasColorAttribute;
283 bool hasLocalCoordsAttribute;
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400284 sk_sp<GrGeometryProcessor> gp = this->makeGP(target->caps().shaderCaps(),
285 &hasColorAttribute,
Brian Osman37064c12019-02-08 10:53:07 -0500286 &hasLocalCoordsAttribute);
joshualitt2771b562015-08-07 12:46:26 -0700287
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400288 // Allocate buffers.
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500289 size_t vertexStride = gp->vertexStride();
Brian Salomondbf70722019-02-07 11:31:24 -0500290 sk_sp<const GrBuffer> vertexBuffer;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400291 int firstVertex = 0;
bsalomon14eaaa62015-09-24 07:01:26 -0700292 void* verts = target->makeVertexSpace(vertexStride, fVertexCount, &vertexBuffer, &firstVertex);
joshualitt2771b562015-08-07 12:46:26 -0700293 if (!verts) {
294 SkDebugf("Could not allocate vertices\n");
295 return;
296 }
297
Brian Salomondbf70722019-02-07 11:31:24 -0500298 sk_sp<const GrBuffer> indexBuffer;
joshualitt2771b562015-08-07 12:46:26 -0700299 int firstIndex = 0;
halcanary96fcdcc2015-08-27 07:41:13 -0700300 uint16_t* indices = nullptr;
Brian Salomon199fb872017-02-06 09:41:10 -0500301 if (this->isIndexed()) {
bsalomon14eaaa62015-09-24 07:01:26 -0700302 indices = target->makeIndexSpace(fIndexCount, &indexBuffer, &firstIndex);
joshualitt2771b562015-08-07 12:46:26 -0700303 if (!indices) {
304 SkDebugf("Could not allocate indices\n");
305 return;
306 }
307 }
308
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400309 // Fill the buffers.
310 this->fillBuffers(hasColorAttribute,
311 hasLocalCoordsAttribute,
312 vertexStride,
313 verts,
314 indices);
315
316 // Draw the vertices.
Brian Salomon12d22642019-01-29 14:38:50 -0500317 this->drawVertices(target, std::move(gp), std::move(vertexBuffer), firstVertex, indexBuffer,
318 firstIndex);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400319}
320
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500321void DrawVerticesOp::drawNonVolatile(Target* target) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400322 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
323
324 bool hasColorAttribute;
325 bool hasLocalCoordsAttribute;
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400326 sk_sp<GrGeometryProcessor> gp = this->makeGP(target->caps().shaderCaps(),
327 &hasColorAttribute,
Brian Osman37064c12019-02-08 10:53:07 -0500328 &hasLocalCoordsAttribute);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400329
330 SkASSERT(fMeshes.count() == 1); // Non-volatile meshes should never combine.
331
332 // Get the resource provider.
333 GrResourceProvider* rp = target->resourceProvider();
334
335 // Generate keys for the buffers.
336 GrUniqueKey vertexKey, indexKey;
337 GrUniqueKey::Builder vertexKeyBuilder(&vertexKey, kDomain, 2);
338 GrUniqueKey::Builder indexKeyBuilder(&indexKey, kDomain, 2);
339 vertexKeyBuilder[0] = indexKeyBuilder[0] = fMeshes[0].fVertices->uniqueID();
340 vertexKeyBuilder[1] = 0;
341 indexKeyBuilder[1] = 1;
342 vertexKeyBuilder.finish();
343 indexKeyBuilder.finish();
344
345 // Try to grab data from the cache.
Brian Salomondbf70722019-02-07 11:31:24 -0500346 sk_sp<GrGpuBuffer> vertexBuffer = rp->findByUniqueKey<GrGpuBuffer>(vertexKey);
347 sk_sp<GrGpuBuffer> indexBuffer =
348 this->isIndexed() ? rp->findByUniqueKey<GrGpuBuffer>(indexKey) : nullptr;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400349
350 // Draw using the cached buffers if possible.
351 if (vertexBuffer && (!this->isIndexed() || indexBuffer)) {
Brian Salomon12d22642019-01-29 14:38:50 -0500352 this->drawVertices(target, std::move(gp), std::move(vertexBuffer), 0,
353 std::move(indexBuffer), 0);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400354 return;
355 }
356
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400357 // Allocate vertex buffer.
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500358 size_t vertexStride = gp->vertexStride();
Brian Salomondbf70722019-02-07 11:31:24 -0500359 vertexBuffer = rp->createBuffer(
360 fVertexCount * vertexStride, GrGpuBufferType::kVertex, kStatic_GrAccessPattern);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400361 void* verts = vertexBuffer ? vertexBuffer->map() : nullptr;
362 if (!verts) {
363 SkDebugf("Could not allocate vertices\n");
364 return;
365 }
366
367 // Allocate index buffer.
368 uint16_t* indices = nullptr;
369 if (this->isIndexed()) {
Brian Salomondbf70722019-02-07 11:31:24 -0500370 indexBuffer = rp->createBuffer(
371 fIndexCount * sizeof(uint16_t), GrGpuBufferType::kIndex, kStatic_GrAccessPattern);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400372 indices = indexBuffer ? static_cast<uint16_t*>(indexBuffer->map()) : nullptr;
373 if (!indices) {
374 SkDebugf("Could not allocate indices\n");
375 return;
376 }
377 }
378
379 // Fill the buffers.
380 this->fillBuffers(hasColorAttribute,
381 hasLocalCoordsAttribute,
382 vertexStride,
383 verts,
384 indices);
385
386 // Unmap the buffers.
387 vertexBuffer->unmap();
388 if (indexBuffer) {
389 indexBuffer->unmap();
390 }
391
392 // Cache the buffers.
393 rp->assignUniqueKeyToResource(vertexKey, vertexBuffer.get());
394 rp->assignUniqueKeyToResource(indexKey, indexBuffer.get());
395
396 // Draw the vertices.
Brian Salomon12d22642019-01-29 14:38:50 -0500397 this->drawVertices(target, std::move(gp), std::move(vertexBuffer), 0, std::move(indexBuffer),
398 0);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400399}
400
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500401void DrawVerticesOp::fillBuffers(bool hasColorAttribute,
402 bool hasLocalCoordsAttribute,
403 size_t vertexStride,
404 void* verts,
405 uint16_t* indices) const {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400406 int instanceCount = fMeshes.count();
407
408 // Copy data into the buffers.
joshualitt2771b562015-08-07 12:46:26 -0700409 int vertexOffset = 0;
Brian Salomonfab30a32017-02-06 19:06:22 -0500410 // We have a fast case below for uploading the vertex data when the matrix is translate
Brian Osman37064c12019-02-08 10:53:07 -0500411 // only and there are colors but not local coords.
412 bool fastAttrs = hasColorAttribute && !hasLocalCoordsAttribute;
joshualitt2771b562015-08-07 12:46:26 -0700413 for (int i = 0; i < instanceCount; i++) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400414 // Get each mesh.
bsalomond92b4192016-06-30 07:59:23 -0700415 const Mesh& mesh = fMeshes[i];
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400416
417 // Copy data into the index buffer.
bsalomon14eaaa62015-09-24 07:01:26 -0700418 if (indices) {
Brian Salomon199fb872017-02-06 09:41:10 -0500419 int indexCount = mesh.fVertices->indexCount();
Brian Salomonfab30a32017-02-06 19:06:22 -0500420 for (int j = 0; j < indexCount; ++j) {
421 *indices++ = mesh.fVertices->indices()[j] + vertexOffset;
joshualitt2771b562015-08-07 12:46:26 -0700422 }
423 }
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400424
425 // Copy data into the vertex buffer.
Brian Salomon199fb872017-02-06 09:41:10 -0500426 int vertexCount = mesh.fVertices->vertexCount();
427 const SkPoint* positions = mesh.fVertices->positions();
428 const SkColor* colors = mesh.fVertices->colors();
429 const SkPoint* localCoords = mesh.fVertices->texCoords();
Brian Salomonfab30a32017-02-06 19:06:22 -0500430 bool fastMesh = (!this->hasMultipleViewMatrices() ||
431 mesh.fViewMatrix.getType() <= SkMatrix::kTranslate_Mask) &&
432 mesh.hasPerVertexColors();
433 if (fastAttrs && fastMesh) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400434 // Fast case.
Brian Salomonfab30a32017-02-06 19:06:22 -0500435 struct V {
436 SkPoint fPos;
437 uint32_t fColor;
438 };
439 SkASSERT(sizeof(V) == vertexStride);
440 V* v = (V*)verts;
441 Sk2f t(0, 0);
Brian Salomon199fb872017-02-06 09:41:10 -0500442 if (this->hasMultipleViewMatrices()) {
Brian Salomonfab30a32017-02-06 19:06:22 -0500443 t = Sk2f(mesh.fViewMatrix.getTranslateX(), mesh.fViewMatrix.getTranslateY());
joshualitt2771b562015-08-07 12:46:26 -0700444 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500445 for (int j = 0; j < vertexCount; ++j) {
446 Sk2f p = Sk2f::Load(positions++) + t;
447 p.store(&v[j].fPos);
448 v[j].fColor = colors[j];
449 }
450 verts = v + vertexCount;
451 } else {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400452 // Normal case.
Brian Salomonfab30a32017-02-06 19:06:22 -0500453 static constexpr size_t kColorOffset = sizeof(SkPoint);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400454 size_t offset = kColorOffset;
455 if (hasColorAttribute) {
456 offset += sizeof(uint32_t);
457 }
458 size_t localCoordOffset = offset;
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400459 if (hasLocalCoordsAttribute) {
460 offset += sizeof(SkPoint);
461 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500462
Brian Osman1be2b7c2018-10-29 16:07:15 -0400463 // TODO4F: Preserve float colors
Brian Osmancf860852018-10-31 14:04:39 -0400464 GrColor color = mesh.fColor.toBytes_RGBA();
Brian Osman1be2b7c2018-10-29 16:07:15 -0400465
Brian Salomonfab30a32017-02-06 19:06:22 -0500466 for (int j = 0; j < vertexCount; ++j) {
467 if (this->hasMultipleViewMatrices()) {
468 mesh.fViewMatrix.mapPoints(((SkPoint*)verts), &positions[j], 1);
Brian Salomon3f363692017-02-02 21:05:19 -0500469 } else {
Brian Salomonfab30a32017-02-06 19:06:22 -0500470 *((SkPoint*)verts) = positions[j];
Brian Salomon199fb872017-02-06 09:41:10 -0500471 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500472 if (hasColorAttribute) {
473 if (mesh.hasPerVertexColors()) {
474 *(uint32_t*)((intptr_t)verts + kColorOffset) = colors[j];
475 } else {
Brian Osman1be2b7c2018-10-29 16:07:15 -0400476 *(uint32_t*)((intptr_t)verts + kColorOffset) = color;
Brian Salomonfab30a32017-02-06 19:06:22 -0500477 }
Brian Salomon3f363692017-02-02 21:05:19 -0500478 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500479 if (hasLocalCoordsAttribute) {
480 if (mesh.hasExplicitLocalCoords()) {
481 *(SkPoint*)((intptr_t)verts + localCoordOffset) = localCoords[j];
482 } else {
483 *(SkPoint*)((intptr_t)verts + localCoordOffset) = positions[j];
484 }
485 }
486 verts = (void*)((intptr_t)verts + vertexStride);
joshualitt2771b562015-08-07 12:46:26 -0700487 }
joshualitt2771b562015-08-07 12:46:26 -0700488 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500489 vertexOffset += vertexCount;
joshualitt2771b562015-08-07 12:46:26 -0700490 }
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400491}
joshualitt2771b562015-08-07 12:46:26 -0700492
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500493void DrawVerticesOp::drawVertices(Target* target,
494 sk_sp<const GrGeometryProcessor> gp,
495 sk_sp<const GrBuffer> vertexBuffer,
496 int firstVertex,
497 sk_sp<const GrBuffer> indexBuffer,
498 int firstIndex) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000499 GrMesh* mesh = target->allocMesh(this->primitiveType());
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400500 if (this->isIndexed()) {
Brian Salomon12d22642019-01-29 14:38:50 -0500501 mesh->setIndexed(std::move(indexBuffer), fIndexCount, firstIndex, 0, fVertexCount - 1,
Brian Salomon7eae3e02018-08-07 14:02:38 +0000502 GrPrimitiveRestart::kNo);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400503 } else {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000504 mesh->setNonIndexedNonInstanced(fVertexCount);
joshualitt2771b562015-08-07 12:46:26 -0700505 }
Brian Salomon12d22642019-01-29 14:38:50 -0500506 mesh->setVertexData(std::move(vertexBuffer), firstVertex);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700507 target->recordDraw(std::move(gp), mesh);
508}
509
510void DrawVerticesOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
511 fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
joshualitt2771b562015-08-07 12:46:26 -0700512}
513
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500514GrOp::CombineResult DrawVerticesOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
515 DrawVerticesOp* that = t->cast<DrawVerticesOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700516
Brian Salomonc2f42542017-07-12 14:11:22 -0400517 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000518 return CombineResult::kCannotCombine;
joshualitt2771b562015-08-07 12:46:26 -0700519 }
520
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400521 // Non-volatile meshes cannot batch, because if a non-volatile mesh batches with another mesh,
522 // then on the next frame, if that non-volatile mesh is drawn, it will draw the other mesh
523 // that was saved in its vertex buffer, which is not necessarily there anymore.
524 if (!this->fMeshes[0].fVertices->isVolatile() || !that->fMeshes[0].fVertices->isVolatile()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000525 return CombineResult::kCannotCombine;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400526 }
527
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500528 if (!this->combinablePrimitive() || this->primitiveType() != that->primitiveType()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000529 return CombineResult::kCannotCombine;
joshualitt2771b562015-08-07 12:46:26 -0700530 }
531
Mike Reedaa9e3322017-03-16 14:38:48 -0400532 if (fMeshes[0].fVertices->hasIndices() != that->fMeshes[0].fVertices->hasIndices()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000533 return CombineResult::kCannotCombine;
joshualitt2771b562015-08-07 12:46:26 -0700534 }
535
Brian Salomon3de0aee2017-01-29 09:34:17 -0500536 if (fColorArrayType != that->fColorArrayType) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000537 return CombineResult::kCannotCombine;
Brian Salomon3de0aee2017-01-29 09:34:17 -0500538 }
539
Ben Wagner9bc36fd2018-06-15 14:23:36 -0400540 if (fVertexCount + that->fVertexCount > SkTo<int>(UINT16_MAX)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000541 return CombineResult::kCannotCombine;
Brian Salomon3f363692017-02-02 21:05:19 -0500542 }
543
Brian Osmanfa6d8652017-05-31 09:37:27 -0400544 // NOTE: For SkColor vertex colors, the source color space is always sRGB, and the destination
545 // gamut is determined by the render target context. A mis-match should be impossible.
546 SkASSERT(GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get()));
547
Brian Salomon199fb872017-02-06 09:41:10 -0500548 // If either op required explicit local coords or per-vertex colors the combined mesh does. Same
549 // with multiple view matrices.
550 fFlags |= that->fFlags;
joshualitt2771b562015-08-07 12:46:26 -0700551
Brian Salomon199fb872017-02-06 09:41:10 -0500552 if (!this->requiresPerVertexColors() && this->fMeshes[0].fColor != that->fMeshes[0].fColor) {
553 fFlags |= kRequiresPerVertexColors_Flag;
554 }
Brian Salomon3f363692017-02-02 21:05:19 -0500555 // Check whether we are about to acquire a mesh with a different view matrix.
Brian Salomon199fb872017-02-06 09:41:10 -0500556 if (!this->hasMultipleViewMatrices() &&
557 !this->fMeshes[0].fViewMatrix.cheapEqualTo(that->fMeshes[0].fViewMatrix)) {
558 fFlags |= kHasMultipleViewMatrices_Flag;
Brian Salomon3f363692017-02-02 21:05:19 -0500559 }
560
bsalomond92b4192016-06-30 07:59:23 -0700561 fMeshes.push_back_n(that->fMeshes.count(), that->fMeshes.begin());
bsalomon14eaaa62015-09-24 07:01:26 -0700562 fVertexCount += that->fVertexCount;
563 fIndexCount += that->fIndexCount;
joshualitt2771b562015-08-07 12:46:26 -0700564
Brian Salomon7eae3e02018-08-07 14:02:38 +0000565 return CombineResult::kMerged;
joshualitt2771b562015-08-07 12:46:26 -0700566}
567
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500568} // anonymous namespace
569
Robert Phillipsb97da532019-02-12 15:24:12 -0500570std::unique_ptr<GrDrawOp> GrDrawVerticesOp::Make(GrRecordingContext* context,
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500571 GrPaint&& paint,
572 sk_sp<SkVertices> vertices,
573 const SkVertices::Bone bones[],
574 int boneCount,
575 const SkMatrix& viewMatrix,
576 GrAAType aaType,
577 sk_sp<GrColorSpaceXform> colorSpaceXform,
578 GrPrimitiveType* overridePrimType) {
579 SkASSERT(vertices);
580 GrPrimitiveType primType = overridePrimType ? *overridePrimType
581 : SkVertexModeToGrPrimitiveType(vertices->mode());
582 return GrSimpleMeshDrawOpHelper::FactoryHelper<DrawVerticesOp>(context, std::move(paint),
583 std::move(vertices),
584 bones, boneCount,
585 primType, aaType,
586 std::move(colorSpaceXform),
587 viewMatrix);
588}
589
joshualitt2771b562015-08-07 12:46:26 -0700590///////////////////////////////////////////////////////////////////////////////////////////////////
591
Hal Canary6f6961e2017-01-31 13:50:44 -0500592#if GR_TEST_UTILS
joshualitt2771b562015-08-07 12:46:26 -0700593
Brian Salomon5ec9def2016-12-20 15:34:05 -0500594#include "GrDrawOpTest.h"
joshualitt2771b562015-08-07 12:46:26 -0700595
596static uint32_t seed_vertices(GrPrimitiveType type) {
597 switch (type) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600598 case GrPrimitiveType::kTriangles:
599 case GrPrimitiveType::kTriangleStrip:
joshualitt2771b562015-08-07 12:46:26 -0700600 return 3;
Chris Dalton3809bab2017-06-13 10:55:06 -0600601 case GrPrimitiveType::kPoints:
joshualitt2771b562015-08-07 12:46:26 -0700602 return 1;
Chris Dalton3809bab2017-06-13 10:55:06 -0600603 case GrPrimitiveType::kLines:
604 case GrPrimitiveType::kLineStrip:
joshualitt2771b562015-08-07 12:46:26 -0700605 return 2;
Chris Dalton3809bab2017-06-13 10:55:06 -0600606 case GrPrimitiveType::kLinesAdjacency:
607 return 4;
joshualitt2771b562015-08-07 12:46:26 -0700608 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400609 SK_ABORT("Incomplete switch\n");
joshualitt2771b562015-08-07 12:46:26 -0700610 return 0;
611}
612
613static uint32_t primitive_vertices(GrPrimitiveType type) {
614 switch (type) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600615 case GrPrimitiveType::kTriangles:
joshualitt2771b562015-08-07 12:46:26 -0700616 return 3;
Chris Dalton3809bab2017-06-13 10:55:06 -0600617 case GrPrimitiveType::kLines:
joshualitt2771b562015-08-07 12:46:26 -0700618 return 2;
Chris Dalton3809bab2017-06-13 10:55:06 -0600619 case GrPrimitiveType::kTriangleStrip:
Chris Dalton3809bab2017-06-13 10:55:06 -0600620 case GrPrimitiveType::kPoints:
621 case GrPrimitiveType::kLineStrip:
joshualitt2771b562015-08-07 12:46:26 -0700622 return 1;
Chris Dalton3809bab2017-06-13 10:55:06 -0600623 case GrPrimitiveType::kLinesAdjacency:
624 return 4;
joshualitt2771b562015-08-07 12:46:26 -0700625 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400626 SK_ABORT("Incomplete switch\n");
joshualitt2771b562015-08-07 12:46:26 -0700627 return 0;
628}
629
630static SkPoint random_point(SkRandom* random, SkScalar min, SkScalar max) {
631 SkPoint p;
632 p.fX = random->nextRangeScalar(min, max);
633 p.fY = random->nextRangeScalar(min, max);
634 return p;
635}
636
637static void randomize_params(size_t count, size_t maxVertex, SkScalar min, SkScalar max,
Brian Salomonfc527d22016-12-14 21:07:01 -0500638 SkRandom* random, SkTArray<SkPoint>* positions,
joshualitt2771b562015-08-07 12:46:26 -0700639 SkTArray<SkPoint>* texCoords, bool hasTexCoords,
Brian Salomon3de0aee2017-01-29 09:34:17 -0500640 SkTArray<uint32_t>* colors, bool hasColors,
641 SkTArray<uint16_t>* indices, bool hasIndices) {
joshualitt2771b562015-08-07 12:46:26 -0700642 for (uint32_t v = 0; v < count; v++) {
643 positions->push_back(random_point(random, min, max));
644 if (hasTexCoords) {
645 texCoords->push_back(random_point(random, min, max));
646 }
647 if (hasColors) {
648 colors->push_back(GrRandomColor(random));
649 }
650 if (hasIndices) {
Ben Wagnerb0897652018-06-15 15:37:57 +0000651 SkASSERT(maxVertex <= UINT16_MAX);
joshualitt2771b562015-08-07 12:46:26 -0700652 indices->push_back(random->nextULessThan((uint16_t)maxVertex));
653 }
654 }
655}
656
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500657GR_DRAW_OP_TEST_DEFINE(DrawVerticesOp) {
Chris Daltonb894c2b2017-06-14 12:39:19 -0600658 GrPrimitiveType type;
659 do {
660 type = GrPrimitiveType(random->nextULessThan(kNumGrPrimitiveTypes));
661 } while (GrPrimTypeRequiresGeometryShaderSupport(type) &&
Robert Phillips9da87e02019-02-04 13:26:26 -0500662 !context->priv().caps()->shaderCaps()->geometryShaderSupport());
Chris Daltonb894c2b2017-06-14 12:39:19 -0600663
joshualitt2771b562015-08-07 12:46:26 -0700664 uint32_t primitiveCount = random->nextRangeU(1, 100);
665
666 // TODO make 'sensible' indexbuffers
667 SkTArray<SkPoint> positions;
668 SkTArray<SkPoint> texCoords;
Brian Salomon3de0aee2017-01-29 09:34:17 -0500669 SkTArray<uint32_t> colors;
joshualitt2771b562015-08-07 12:46:26 -0700670 SkTArray<uint16_t> indices;
671
672 bool hasTexCoords = random->nextBool();
673 bool hasIndices = random->nextBool();
674 bool hasColors = random->nextBool();
675
676 uint32_t vertexCount = seed_vertices(type) + (primitiveCount - 1) * primitive_vertices(type);
677
678 static const SkScalar kMinVertExtent = -100.f;
679 static const SkScalar kMaxVertExtent = 100.f;
Brian Salomonfc527d22016-12-14 21:07:01 -0500680 randomize_params(seed_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent, random,
681 &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices,
682 hasIndices);
joshualitt2771b562015-08-07 12:46:26 -0700683
684 for (uint32_t i = 1; i < primitiveCount; i++) {
685 randomize_params(primitive_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent,
Brian Salomonfc527d22016-12-14 21:07:01 -0500686 random, &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices,
687 hasIndices);
joshualitt2771b562015-08-07 12:46:26 -0700688 }
689
690 SkMatrix viewMatrix = GrTest::TestMatrix(random);
joshualitt2771b562015-08-07 12:46:26 -0700691
Brian Osmanfa6d8652017-05-31 09:37:27 -0400692 sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(random);
Brian Osmanae0c50c2017-05-25 16:56:34 -0400693
694 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
695 sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, vertexCount, positions.begin(),
696 texCoords.begin(), colors.begin(),
697 hasIndices ? indices.count() : 0,
698 indices.begin());
Brian Salomonc2f42542017-07-12 14:11:22 -0400699 GrAAType aaType = GrAAType::kNone;
700 if (GrFSAAType::kUnifiedMSAA == fsaaType && random->nextBool()) {
701 aaType = GrAAType::kMSAA;
702 }
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400703 return GrDrawVerticesOp::Make(context, std::move(paint), std::move(vertices), nullptr, 0,
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400704 viewMatrix, aaType, std::move(colorSpaceXform), &type);
joshualitt2771b562015-08-07 12:46:26 -0700705}
706
707#endif