blob: b36d8bfc809cc3b2f2b856fefdcf989d03d63146 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/core/SkRectPriv.h"
9#include "src/gpu/GrCaps.h"
10#include "src/gpu/GrDefaultGeoProcFactory.h"
11#include "src/gpu/GrOpFlushState.h"
12#include "src/gpu/SkGr.h"
13#include "src/gpu/ops/GrDrawVerticesOp.h"
14#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.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
Chris Dalton1706cbf2019-05-21 19:35:29 -060031 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -050032 fHelper.visitProxies(func);
33 }
34
35#ifdef SK_DEBUG
36 SkString dumpInfo() const override;
37#endif
38
39 FixedFunctionFlags fixedFunctionFlags() const override;
40
Chris Dalton6ce447a2019-06-23 18:07:38 -060041 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
42 bool hasMixedSampledCoverage, GrClampType) override;
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -050043
44private:
45 enum class ColorArrayType {
46 kPremulGrColor,
47 kSkColor,
48 };
49
50 void onPrepareDraws(Target*) override;
Chris Dalton07cdcfc92019-02-26 11:13:22 -070051 void onExecute(GrOpFlushState*, const SkRect& chainBounds) override;
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -050052
53 void drawVolatile(Target*);
54 void drawNonVolatile(Target*);
55
56 void fillBuffers(bool hasColorAttribute,
57 bool hasLocalCoordsAttribute,
58 size_t vertexStride,
59 void* verts,
60 uint16_t* indices) const;
61
62 void drawVertices(Target*,
63 sk_sp<const GrGeometryProcessor>,
64 sk_sp<const GrBuffer> vertexBuffer,
65 int firstVertex,
66 sk_sp<const GrBuffer> indexBuffer,
67 int firstIndex);
68
69 sk_sp<GrGeometryProcessor> makeGP(const GrShaderCaps* shaderCaps,
70 bool* hasColorAttribute,
71 bool* hasLocalCoordAttribute) const;
72
73 GrPrimitiveType primitiveType() const { return fPrimitiveType; }
74 bool combinablePrimitive() const {
75 return GrPrimitiveType::kTriangles == fPrimitiveType ||
76 GrPrimitiveType::kLines == fPrimitiveType ||
77 GrPrimitiveType::kPoints == fPrimitiveType;
78 }
79
80 CombineResult onCombineIfPossible(GrOp* t, const GrCaps&) override;
81
82 struct Mesh {
83 SkPMColor4f fColor; // Used if this->hasPerVertexColors() is false.
84 sk_sp<SkVertices> fVertices;
85 SkMatrix fViewMatrix;
86 bool fIgnoreTexCoords;
87 bool fIgnoreColors;
88
89 bool hasExplicitLocalCoords() const {
90 return fVertices->hasTexCoords() && !fIgnoreTexCoords;
91 }
92
93 bool hasPerVertexColors() const {
94 return fVertices->hasColors() && !fIgnoreColors;
95 }
96 };
97
98 bool isIndexed() const {
99 // Consistency enforced in onCombineIfPossible.
100 return fMeshes[0].fVertices->hasIndices();
101 }
102
103 bool requiresPerVertexColors() const {
104 return SkToBool(kRequiresPerVertexColors_Flag & fFlags);
105 }
106
107 bool anyMeshHasExplicitLocalCoords() const {
108 return SkToBool(kAnyMeshHasExplicitLocalCoords_Flag & fFlags);
109 }
110
111 bool hasMultipleViewMatrices() const {
112 return SkToBool(kHasMultipleViewMatrices_Flag & fFlags);
113 }
114
115 enum Flags {
116 kRequiresPerVertexColors_Flag = 0x1,
117 kAnyMeshHasExplicitLocalCoords_Flag = 0x2,
118 kHasMultipleViewMatrices_Flag = 0x4,
119 };
120
121 Helper fHelper;
122 SkSTArray<1, Mesh, true> fMeshes;
123 // GrPrimitiveType is more expressive than fVertices.mode() so it is used instead and we ignore
124 // the SkVertices mode (though fPrimitiveType may have been inferred from it).
125 GrPrimitiveType fPrimitiveType;
126 uint32_t fFlags;
127 int fVertexCount;
128 int fIndexCount;
129 ColorArrayType fColorArrayType;
130 sk_sp<GrColorSpaceXform> fColorSpaceXform;
131
132 typedef GrMeshDrawOp INHERITED;
133};
134
135DrawVerticesOp::DrawVerticesOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
136 sk_sp<SkVertices> vertices, const SkVertices::Bone bones[],
137 int boneCount, GrPrimitiveType primitiveType, GrAAType aaType,
138 sk_sp<GrColorSpaceXform> colorSpaceXform,
139 const SkMatrix& viewMatrix)
Brian Osmanfa6d8652017-05-31 09:37:27 -0400140 : INHERITED(ClassID())
Brian Salomonc2f42542017-07-12 14:11:22 -0400141 , fHelper(helperArgs, aaType)
Brian Osmanfa6d8652017-05-31 09:37:27 -0400142 , fPrimitiveType(primitiveType)
143 , fColorSpaceXform(std::move(colorSpaceXform)) {
Brian Salomon199fb872017-02-06 09:41:10 -0500144 SkASSERT(vertices);
145
146 fVertexCount = vertices->vertexCount();
147 fIndexCount = vertices->indexCount();
Brian Osmanae0c50c2017-05-25 16:56:34 -0400148 fColorArrayType = vertices->hasColors() ? ColorArrayType::kSkColor
149 : ColorArrayType::kPremulGrColor;
joshualitt2771b562015-08-07 12:46:26 -0700150
bsalomond92b4192016-06-30 07:59:23 -0700151 Mesh& mesh = fMeshes.push_back();
152 mesh.fColor = color;
Brian Salomon3f363692017-02-02 21:05:19 -0500153 mesh.fViewMatrix = viewMatrix;
Brian Salomon199fb872017-02-06 09:41:10 -0500154 mesh.fVertices = std::move(vertices);
Brian Osman8a030552017-05-23 15:03:18 -0400155 mesh.fIgnoreTexCoords = false;
156 mesh.fIgnoreColors = false;
joshualitt2771b562015-08-07 12:46:26 -0700157
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400158 if (mesh.fVertices->hasBones() && bones) {
159 // Perform the transformations on the CPU instead of the GPU.
160 mesh.fVertices = mesh.fVertices->applyBones(bones, boneCount);
161 } else {
Brian Osman37064c12019-02-08 10:53:07 -0500162 SkASSERT(!bones || boneCount == 1);
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400163 }
164
Brian Salomon199fb872017-02-06 09:41:10 -0500165 fFlags = 0;
166 if (mesh.hasPerVertexColors()) {
167 fFlags |= kRequiresPerVertexColors_Flag;
joshualitt2771b562015-08-07 12:46:26 -0700168 }
Brian Salomon199fb872017-02-06 09:41:10 -0500169 if (mesh.hasExplicitLocalCoords()) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400170 fFlags |= kAnyMeshHasExplicitLocalCoords_Flag;
joshualitt2771b562015-08-07 12:46:26 -0700171 }
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400172
173 // Special case for meshes with a world transform but no bone weights.
174 // These will be considered normal vertices draws without bones.
175 if (!mesh.fVertices->hasBones() && boneCount == 1) {
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400176 SkMatrix worldTransform;
177 worldTransform.setAffine(bones[0].values);
178 mesh.fViewMatrix.preConcat(worldTransform);
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400179 }
joshualitt2771b562015-08-07 12:46:26 -0700180
bsalomon88cf17d2016-07-08 06:40:56 -0700181 IsZeroArea zeroArea;
Chris Dalton3809bab2017-06-13 10:55:06 -0600182 if (GrIsPrimTypeLines(primitiveType) || GrPrimitiveType::kPoints == primitiveType) {
bsalomon88cf17d2016-07-08 06:40:56 -0700183 zeroArea = IsZeroArea::kYes;
184 } else {
185 zeroArea = IsZeroArea::kNo;
186 }
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400187
Brian Osman37064c12019-02-08 10:53:07 -0500188 this->setTransformedBounds(mesh.fVertices->bounds(),
189 mesh.fViewMatrix,
190 HasAABloat::kNo,
191 zeroArea);
joshualitt2771b562015-08-07 12:46:26 -0700192}
193
Brian Osman9a390ac2018-11-12 09:47:48 -0500194#ifdef SK_DEBUG
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500195SkString DrawVerticesOp::dumpInfo() const {
Brian Salomonc2f42542017-07-12 14:11:22 -0400196 SkString string;
197 string.appendf("PrimType: %d, MeshCount %d, VCount: %d, ICount: %d\n", (int)fPrimitiveType,
198 fMeshes.count(), fVertexCount, fIndexCount);
199 string += fHelper.dumpInfo();
200 string += INHERITED::dumpInfo();
201 return string;
joshualitt2771b562015-08-07 12:46:26 -0700202}
Brian Osman9a390ac2018-11-12 09:47:48 -0500203#endif
joshualitt2771b562015-08-07 12:46:26 -0700204
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500205GrDrawOp::FixedFunctionFlags DrawVerticesOp::fixedFunctionFlags() const {
Brian Salomonc2f42542017-07-12 14:11:22 -0400206 return fHelper.fixedFunctionFlags();
207}
208
Chris Dalton6ce447a2019-06-23 18:07:38 -0600209GrProcessorSet::Analysis DrawVerticesOp::finalize(
210 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
211 GrClampType clampType) {
Brian Salomonc2f42542017-07-12 14:11:22 -0400212 GrProcessorAnalysisColor gpColor;
213 if (this->requiresPerVertexColors()) {
214 gpColor.setToUnknown();
215 } else {
216 gpColor.setToConstant(fMeshes.front().fColor);
217 }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600218 auto result = fHelper.finalizeProcessors(caps, clip, hasMixedSampledCoverage, clampType,
219 GrProcessorAnalysisCoverage::kNone, &gpColor);
Brian Salomonc2f42542017-07-12 14:11:22 -0400220 if (gpColor.isConstant(&fMeshes.front().fColor)) {
221 fMeshes.front().fIgnoreColors = true;
Brian Salomon199fb872017-02-06 09:41:10 -0500222 fFlags &= ~kRequiresPerVertexColors_Flag;
Brian Osmanae0c50c2017-05-25 16:56:34 -0400223 fColorArrayType = ColorArrayType::kPremulGrColor;
joshualitt2771b562015-08-07 12:46:26 -0700224 }
Brian Salomonc2f42542017-07-12 14:11:22 -0400225 if (!fHelper.usesLocalCoords()) {
Brian Osman8a030552017-05-23 15:03:18 -0400226 fMeshes[0].fIgnoreTexCoords = true;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400227 fFlags &= ~kAnyMeshHasExplicitLocalCoords_Flag;
bsalomon14eaaa62015-09-24 07:01:26 -0700228 }
Brian Salomonc2f42542017-07-12 14:11:22 -0400229 return result;
joshualitt2771b562015-08-07 12:46:26 -0700230}
231
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500232sk_sp<GrGeometryProcessor> DrawVerticesOp::makeGP(const GrShaderCaps* shaderCaps,
233 bool* hasColorAttribute,
234 bool* hasLocalCoordAttribute) const {
Brian Salomon199fb872017-02-06 09:41:10 -0500235 using namespace GrDefaultGeoProcFactory;
236 LocalCoords::Type localCoordsType;
Brian Salomonc2f42542017-07-12 14:11:22 -0400237 if (fHelper.usesLocalCoords()) {
Brian Salomon199fb872017-02-06 09:41:10 -0500238 // If we have multiple view matrices we will transform the positions into device space. We
239 // must then also provide untransformed positions as local coords.
240 if (this->anyMeshHasExplicitLocalCoords() || this->hasMultipleViewMatrices()) {
241 *hasLocalCoordAttribute = true;
242 localCoordsType = LocalCoords::kHasExplicit_Type;
243 } else {
244 *hasLocalCoordAttribute = false;
245 localCoordsType = LocalCoords::kUsePosition_Type;
246 }
247 } else {
248 localCoordsType = LocalCoords::kUnused_Type;
249 *hasLocalCoordAttribute = false;
250 }
251
252 Color color(fMeshes[0].fColor);
253 if (this->requiresPerVertexColors()) {
Brian Osman08a50e02018-06-15 15:06:48 -0400254 if (fColorArrayType == ColorArrayType::kPremulGrColor) {
255 color.fType = Color::kPremulGrColorAttribute_Type;
256 } else {
257 color.fType = Color::kUnpremulSkColorAttribute_Type;
258 color.fColorSpaceXform = fColorSpaceXform;
259 }
Brian Salomon199fb872017-02-06 09:41:10 -0500260 *hasColorAttribute = true;
261 } else {
262 *hasColorAttribute = false;
Brian Salomon23356442018-11-30 15:33:19 -0500263 }
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400264
Brian Salomon199fb872017-02-06 09:41:10 -0500265 const SkMatrix& vm = this->hasMultipleViewMatrices() ? SkMatrix::I() : fMeshes[0].fViewMatrix;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400266
Brian Osman37064c12019-02-08 10:53:07 -0500267 return GrDefaultGeoProcFactory::Make(shaderCaps,
268 color,
269 Coverage::kSolid_Type,
270 localCoordsType,
271 vm);
Brian Salomon199fb872017-02-06 09:41:10 -0500272}
273
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500274void DrawVerticesOp::onPrepareDraws(Target* target) {
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400275 bool hasMapBufferSupport = GrCaps::kNone_MapFlags != target->caps().mapBufferFlags();
276 if (fMeshes[0].fVertices->isVolatile() || !hasMapBufferSupport) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400277 this->drawVolatile(target);
278 } else {
279 this->drawNonVolatile(target);
280 }
281}
282
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500283void DrawVerticesOp::drawVolatile(Target* target) {
Brian Salomon199fb872017-02-06 09:41:10 -0500284 bool hasColorAttribute;
285 bool hasLocalCoordsAttribute;
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400286 sk_sp<GrGeometryProcessor> gp = this->makeGP(target->caps().shaderCaps(),
287 &hasColorAttribute,
Brian Osman37064c12019-02-08 10:53:07 -0500288 &hasLocalCoordsAttribute);
joshualitt2771b562015-08-07 12:46:26 -0700289
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400290 // Allocate buffers.
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500291 size_t vertexStride = gp->vertexStride();
Brian Salomondbf70722019-02-07 11:31:24 -0500292 sk_sp<const GrBuffer> vertexBuffer;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400293 int firstVertex = 0;
bsalomon14eaaa62015-09-24 07:01:26 -0700294 void* verts = target->makeVertexSpace(vertexStride, fVertexCount, &vertexBuffer, &firstVertex);
joshualitt2771b562015-08-07 12:46:26 -0700295 if (!verts) {
296 SkDebugf("Could not allocate vertices\n");
297 return;
298 }
299
Brian Salomondbf70722019-02-07 11:31:24 -0500300 sk_sp<const GrBuffer> indexBuffer;
joshualitt2771b562015-08-07 12:46:26 -0700301 int firstIndex = 0;
halcanary96fcdcc2015-08-27 07:41:13 -0700302 uint16_t* indices = nullptr;
Brian Salomon199fb872017-02-06 09:41:10 -0500303 if (this->isIndexed()) {
bsalomon14eaaa62015-09-24 07:01:26 -0700304 indices = target->makeIndexSpace(fIndexCount, &indexBuffer, &firstIndex);
joshualitt2771b562015-08-07 12:46:26 -0700305 if (!indices) {
306 SkDebugf("Could not allocate indices\n");
307 return;
308 }
309 }
310
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400311 // Fill the buffers.
312 this->fillBuffers(hasColorAttribute,
313 hasLocalCoordsAttribute,
314 vertexStride,
315 verts,
316 indices);
317
318 // Draw the vertices.
Brian Salomon12d22642019-01-29 14:38:50 -0500319 this->drawVertices(target, std::move(gp), std::move(vertexBuffer), firstVertex, indexBuffer,
320 firstIndex);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400321}
322
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500323void DrawVerticesOp::drawNonVolatile(Target* target) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400324 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
325
326 bool hasColorAttribute;
327 bool hasLocalCoordsAttribute;
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400328 sk_sp<GrGeometryProcessor> gp = this->makeGP(target->caps().shaderCaps(),
329 &hasColorAttribute,
Brian Osman37064c12019-02-08 10:53:07 -0500330 &hasLocalCoordsAttribute);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400331
332 SkASSERT(fMeshes.count() == 1); // Non-volatile meshes should never combine.
333
334 // Get the resource provider.
335 GrResourceProvider* rp = target->resourceProvider();
336
337 // Generate keys for the buffers.
338 GrUniqueKey vertexKey, indexKey;
339 GrUniqueKey::Builder vertexKeyBuilder(&vertexKey, kDomain, 2);
340 GrUniqueKey::Builder indexKeyBuilder(&indexKey, kDomain, 2);
341 vertexKeyBuilder[0] = indexKeyBuilder[0] = fMeshes[0].fVertices->uniqueID();
342 vertexKeyBuilder[1] = 0;
343 indexKeyBuilder[1] = 1;
344 vertexKeyBuilder.finish();
345 indexKeyBuilder.finish();
346
347 // Try to grab data from the cache.
Brian Salomondbf70722019-02-07 11:31:24 -0500348 sk_sp<GrGpuBuffer> vertexBuffer = rp->findByUniqueKey<GrGpuBuffer>(vertexKey);
349 sk_sp<GrGpuBuffer> indexBuffer =
350 this->isIndexed() ? rp->findByUniqueKey<GrGpuBuffer>(indexKey) : nullptr;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400351
352 // Draw using the cached buffers if possible.
353 if (vertexBuffer && (!this->isIndexed() || indexBuffer)) {
Brian Salomon12d22642019-01-29 14:38:50 -0500354 this->drawVertices(target, std::move(gp), std::move(vertexBuffer), 0,
355 std::move(indexBuffer), 0);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400356 return;
357 }
358
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400359 // Allocate vertex buffer.
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500360 size_t vertexStride = gp->vertexStride();
Brian Salomondbf70722019-02-07 11:31:24 -0500361 vertexBuffer = rp->createBuffer(
362 fVertexCount * vertexStride, GrGpuBufferType::kVertex, kStatic_GrAccessPattern);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400363 void* verts = vertexBuffer ? vertexBuffer->map() : nullptr;
364 if (!verts) {
365 SkDebugf("Could not allocate vertices\n");
366 return;
367 }
368
369 // Allocate index buffer.
370 uint16_t* indices = nullptr;
371 if (this->isIndexed()) {
Brian Salomondbf70722019-02-07 11:31:24 -0500372 indexBuffer = rp->createBuffer(
373 fIndexCount * sizeof(uint16_t), GrGpuBufferType::kIndex, kStatic_GrAccessPattern);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400374 indices = indexBuffer ? static_cast<uint16_t*>(indexBuffer->map()) : nullptr;
375 if (!indices) {
376 SkDebugf("Could not allocate indices\n");
377 return;
378 }
379 }
380
381 // Fill the buffers.
382 this->fillBuffers(hasColorAttribute,
383 hasLocalCoordsAttribute,
384 vertexStride,
385 verts,
386 indices);
387
388 // Unmap the buffers.
389 vertexBuffer->unmap();
390 if (indexBuffer) {
391 indexBuffer->unmap();
392 }
393
394 // Cache the buffers.
395 rp->assignUniqueKeyToResource(vertexKey, vertexBuffer.get());
396 rp->assignUniqueKeyToResource(indexKey, indexBuffer.get());
397
398 // Draw the vertices.
Brian Salomon12d22642019-01-29 14:38:50 -0500399 this->drawVertices(target, std::move(gp), std::move(vertexBuffer), 0, std::move(indexBuffer),
400 0);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400401}
402
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500403void DrawVerticesOp::fillBuffers(bool hasColorAttribute,
404 bool hasLocalCoordsAttribute,
405 size_t vertexStride,
406 void* verts,
407 uint16_t* indices) const {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400408 int instanceCount = fMeshes.count();
409
410 // Copy data into the buffers.
joshualitt2771b562015-08-07 12:46:26 -0700411 int vertexOffset = 0;
Brian Salomonfab30a32017-02-06 19:06:22 -0500412 // We have a fast case below for uploading the vertex data when the matrix is translate
Brian Osman37064c12019-02-08 10:53:07 -0500413 // only and there are colors but not local coords.
414 bool fastAttrs = hasColorAttribute && !hasLocalCoordsAttribute;
joshualitt2771b562015-08-07 12:46:26 -0700415 for (int i = 0; i < instanceCount; i++) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400416 // Get each mesh.
bsalomond92b4192016-06-30 07:59:23 -0700417 const Mesh& mesh = fMeshes[i];
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400418
419 // Copy data into the index buffer.
bsalomon14eaaa62015-09-24 07:01:26 -0700420 if (indices) {
Brian Salomon199fb872017-02-06 09:41:10 -0500421 int indexCount = mesh.fVertices->indexCount();
Brian Salomonfab30a32017-02-06 19:06:22 -0500422 for (int j = 0; j < indexCount; ++j) {
423 *indices++ = mesh.fVertices->indices()[j] + vertexOffset;
joshualitt2771b562015-08-07 12:46:26 -0700424 }
425 }
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400426
427 // Copy data into the vertex buffer.
Brian Salomon199fb872017-02-06 09:41:10 -0500428 int vertexCount = mesh.fVertices->vertexCount();
429 const SkPoint* positions = mesh.fVertices->positions();
430 const SkColor* colors = mesh.fVertices->colors();
431 const SkPoint* localCoords = mesh.fVertices->texCoords();
Brian Salomonfab30a32017-02-06 19:06:22 -0500432 bool fastMesh = (!this->hasMultipleViewMatrices() ||
433 mesh.fViewMatrix.getType() <= SkMatrix::kTranslate_Mask) &&
434 mesh.hasPerVertexColors();
435 if (fastAttrs && fastMesh) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400436 // Fast case.
Brian Salomonfab30a32017-02-06 19:06:22 -0500437 struct V {
438 SkPoint fPos;
439 uint32_t fColor;
440 };
441 SkASSERT(sizeof(V) == vertexStride);
442 V* v = (V*)verts;
443 Sk2f t(0, 0);
Brian Salomon199fb872017-02-06 09:41:10 -0500444 if (this->hasMultipleViewMatrices()) {
Brian Salomonfab30a32017-02-06 19:06:22 -0500445 t = Sk2f(mesh.fViewMatrix.getTranslateX(), mesh.fViewMatrix.getTranslateY());
joshualitt2771b562015-08-07 12:46:26 -0700446 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500447 for (int j = 0; j < vertexCount; ++j) {
448 Sk2f p = Sk2f::Load(positions++) + t;
449 p.store(&v[j].fPos);
450 v[j].fColor = colors[j];
451 }
452 verts = v + vertexCount;
453 } else {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400454 // Normal case.
Brian Salomonfab30a32017-02-06 19:06:22 -0500455 static constexpr size_t kColorOffset = sizeof(SkPoint);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400456 size_t offset = kColorOffset;
457 if (hasColorAttribute) {
458 offset += sizeof(uint32_t);
459 }
460 size_t localCoordOffset = offset;
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400461 if (hasLocalCoordsAttribute) {
462 offset += sizeof(SkPoint);
463 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500464
Brian Osman1be2b7c2018-10-29 16:07:15 -0400465 // TODO4F: Preserve float colors
Brian Osmancf860852018-10-31 14:04:39 -0400466 GrColor color = mesh.fColor.toBytes_RGBA();
Brian Osman1be2b7c2018-10-29 16:07:15 -0400467
Brian Salomonfab30a32017-02-06 19:06:22 -0500468 for (int j = 0; j < vertexCount; ++j) {
469 if (this->hasMultipleViewMatrices()) {
470 mesh.fViewMatrix.mapPoints(((SkPoint*)verts), &positions[j], 1);
Brian Salomon3f363692017-02-02 21:05:19 -0500471 } else {
Brian Salomonfab30a32017-02-06 19:06:22 -0500472 *((SkPoint*)verts) = positions[j];
Brian Salomon199fb872017-02-06 09:41:10 -0500473 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500474 if (hasColorAttribute) {
475 if (mesh.hasPerVertexColors()) {
476 *(uint32_t*)((intptr_t)verts + kColorOffset) = colors[j];
477 } else {
Brian Osman1be2b7c2018-10-29 16:07:15 -0400478 *(uint32_t*)((intptr_t)verts + kColorOffset) = color;
Brian Salomonfab30a32017-02-06 19:06:22 -0500479 }
Brian Salomon3f363692017-02-02 21:05:19 -0500480 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500481 if (hasLocalCoordsAttribute) {
482 if (mesh.hasExplicitLocalCoords()) {
483 *(SkPoint*)((intptr_t)verts + localCoordOffset) = localCoords[j];
484 } else {
485 *(SkPoint*)((intptr_t)verts + localCoordOffset) = positions[j];
486 }
487 }
488 verts = (void*)((intptr_t)verts + vertexStride);
joshualitt2771b562015-08-07 12:46:26 -0700489 }
joshualitt2771b562015-08-07 12:46:26 -0700490 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500491 vertexOffset += vertexCount;
joshualitt2771b562015-08-07 12:46:26 -0700492 }
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400493}
joshualitt2771b562015-08-07 12:46:26 -0700494
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500495void DrawVerticesOp::drawVertices(Target* target,
496 sk_sp<const GrGeometryProcessor> gp,
497 sk_sp<const GrBuffer> vertexBuffer,
498 int firstVertex,
499 sk_sp<const GrBuffer> indexBuffer,
500 int firstIndex) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000501 GrMesh* mesh = target->allocMesh(this->primitiveType());
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400502 if (this->isIndexed()) {
Brian Salomon12d22642019-01-29 14:38:50 -0500503 mesh->setIndexed(std::move(indexBuffer), fIndexCount, firstIndex, 0, fVertexCount - 1,
Brian Salomon7eae3e02018-08-07 14:02:38 +0000504 GrPrimitiveRestart::kNo);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400505 } else {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000506 mesh->setNonIndexedNonInstanced(fVertexCount);
joshualitt2771b562015-08-07 12:46:26 -0700507 }
Brian Salomon12d22642019-01-29 14:38:50 -0500508 mesh->setVertexData(std::move(vertexBuffer), firstVertex);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700509 target->recordDraw(std::move(gp), mesh);
510}
511
512void DrawVerticesOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
513 fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
joshualitt2771b562015-08-07 12:46:26 -0700514}
515
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500516GrOp::CombineResult DrawVerticesOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
517 DrawVerticesOp* that = t->cast<DrawVerticesOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700518
Brian Salomonc2f42542017-07-12 14:11:22 -0400519 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000520 return CombineResult::kCannotCombine;
joshualitt2771b562015-08-07 12:46:26 -0700521 }
522
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400523 // Non-volatile meshes cannot batch, because if a non-volatile mesh batches with another mesh,
524 // then on the next frame, if that non-volatile mesh is drawn, it will draw the other mesh
525 // that was saved in its vertex buffer, which is not necessarily there anymore.
526 if (!this->fMeshes[0].fVertices->isVolatile() || !that->fMeshes[0].fVertices->isVolatile()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000527 return CombineResult::kCannotCombine;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400528 }
529
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500530 if (!this->combinablePrimitive() || this->primitiveType() != that->primitiveType()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000531 return CombineResult::kCannotCombine;
joshualitt2771b562015-08-07 12:46:26 -0700532 }
533
Mike Reedaa9e3322017-03-16 14:38:48 -0400534 if (fMeshes[0].fVertices->hasIndices() != that->fMeshes[0].fVertices->hasIndices()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000535 return CombineResult::kCannotCombine;
joshualitt2771b562015-08-07 12:46:26 -0700536 }
537
Brian Salomon3de0aee2017-01-29 09:34:17 -0500538 if (fColorArrayType != that->fColorArrayType) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000539 return CombineResult::kCannotCombine;
Brian Salomon3de0aee2017-01-29 09:34:17 -0500540 }
541
Ben Wagner9bc36fd2018-06-15 14:23:36 -0400542 if (fVertexCount + that->fVertexCount > SkTo<int>(UINT16_MAX)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000543 return CombineResult::kCannotCombine;
Brian Salomon3f363692017-02-02 21:05:19 -0500544 }
545
Brian Osmanfa6d8652017-05-31 09:37:27 -0400546 // NOTE: For SkColor vertex colors, the source color space is always sRGB, and the destination
547 // gamut is determined by the render target context. A mis-match should be impossible.
548 SkASSERT(GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get()));
549
Brian Salomon199fb872017-02-06 09:41:10 -0500550 // If either op required explicit local coords or per-vertex colors the combined mesh does. Same
551 // with multiple view matrices.
552 fFlags |= that->fFlags;
joshualitt2771b562015-08-07 12:46:26 -0700553
Brian Salomon199fb872017-02-06 09:41:10 -0500554 if (!this->requiresPerVertexColors() && this->fMeshes[0].fColor != that->fMeshes[0].fColor) {
555 fFlags |= kRequiresPerVertexColors_Flag;
556 }
Brian Salomon3f363692017-02-02 21:05:19 -0500557 // Check whether we are about to acquire a mesh with a different view matrix.
Brian Salomon199fb872017-02-06 09:41:10 -0500558 if (!this->hasMultipleViewMatrices() &&
559 !this->fMeshes[0].fViewMatrix.cheapEqualTo(that->fMeshes[0].fViewMatrix)) {
560 fFlags |= kHasMultipleViewMatrices_Flag;
Brian Salomon3f363692017-02-02 21:05:19 -0500561 }
562
bsalomond92b4192016-06-30 07:59:23 -0700563 fMeshes.push_back_n(that->fMeshes.count(), that->fMeshes.begin());
bsalomon14eaaa62015-09-24 07:01:26 -0700564 fVertexCount += that->fVertexCount;
565 fIndexCount += that->fIndexCount;
joshualitt2771b562015-08-07 12:46:26 -0700566
Brian Salomon7eae3e02018-08-07 14:02:38 +0000567 return CombineResult::kMerged;
joshualitt2771b562015-08-07 12:46:26 -0700568}
569
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500570} // anonymous namespace
571
Robert Phillipsb97da532019-02-12 15:24:12 -0500572std::unique_ptr<GrDrawOp> GrDrawVerticesOp::Make(GrRecordingContext* context,
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500573 GrPaint&& paint,
574 sk_sp<SkVertices> vertices,
575 const SkVertices::Bone bones[],
576 int boneCount,
577 const SkMatrix& viewMatrix,
578 GrAAType aaType,
579 sk_sp<GrColorSpaceXform> colorSpaceXform,
580 GrPrimitiveType* overridePrimType) {
581 SkASSERT(vertices);
582 GrPrimitiveType primType = overridePrimType ? *overridePrimType
583 : SkVertexModeToGrPrimitiveType(vertices->mode());
584 return GrSimpleMeshDrawOpHelper::FactoryHelper<DrawVerticesOp>(context, std::move(paint),
585 std::move(vertices),
586 bones, boneCount,
587 primType, aaType,
588 std::move(colorSpaceXform),
589 viewMatrix);
590}
591
joshualitt2771b562015-08-07 12:46:26 -0700592///////////////////////////////////////////////////////////////////////////////////////////////////
593
Hal Canary6f6961e2017-01-31 13:50:44 -0500594#if GR_TEST_UTILS
joshualitt2771b562015-08-07 12:46:26 -0700595
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500596#include "src/gpu/GrDrawOpTest.h"
joshualitt2771b562015-08-07 12:46:26 -0700597
598static uint32_t seed_vertices(GrPrimitiveType type) {
599 switch (type) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600600 case GrPrimitiveType::kTriangles:
601 case GrPrimitiveType::kTriangleStrip:
joshualitt2771b562015-08-07 12:46:26 -0700602 return 3;
Chris Dalton3809bab2017-06-13 10:55:06 -0600603 case GrPrimitiveType::kPoints:
joshualitt2771b562015-08-07 12:46:26 -0700604 return 1;
Chris Dalton3809bab2017-06-13 10:55:06 -0600605 case GrPrimitiveType::kLines:
606 case GrPrimitiveType::kLineStrip:
joshualitt2771b562015-08-07 12:46:26 -0700607 return 2;
Chris Dalton3809bab2017-06-13 10:55:06 -0600608 case GrPrimitiveType::kLinesAdjacency:
609 return 4;
joshualitt2771b562015-08-07 12:46:26 -0700610 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400611 SK_ABORT("Incomplete switch\n");
joshualitt2771b562015-08-07 12:46:26 -0700612}
613
614static uint32_t primitive_vertices(GrPrimitiveType type) {
615 switch (type) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600616 case GrPrimitiveType::kTriangles:
joshualitt2771b562015-08-07 12:46:26 -0700617 return 3;
Chris Dalton3809bab2017-06-13 10:55:06 -0600618 case GrPrimitiveType::kLines:
joshualitt2771b562015-08-07 12:46:26 -0700619 return 2;
Chris Dalton3809bab2017-06-13 10:55:06 -0600620 case GrPrimitiveType::kTriangleStrip:
Chris Dalton3809bab2017-06-13 10:55:06 -0600621 case GrPrimitiveType::kPoints:
622 case GrPrimitiveType::kLineStrip:
joshualitt2771b562015-08-07 12:46:26 -0700623 return 1;
Chris Dalton3809bab2017-06-13 10:55:06 -0600624 case GrPrimitiveType::kLinesAdjacency:
625 return 4;
joshualitt2771b562015-08-07 12:46:26 -0700626 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400627 SK_ABORT("Incomplete switch\n");
joshualitt2771b562015-08-07 12:46:26 -0700628}
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;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600700 if (numSamples > 1 && random->nextBool()) {
Brian Salomonc2f42542017-07-12 14:11:22 -0400701 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