blob: 580edd6a60cbdee7ffabeb4b1e9b4b02581c8613 [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
Brian Osman5ced0bf2019-03-15 10:15:29 -040041 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, GrFSAAType,
42 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
Brian Osman5ced0bf2019-03-15 10:15:29 -0400209GrProcessorSet::Analysis DrawVerticesOp::finalize(const GrCaps& caps, const GrAppliedClip* clip,
210 GrFSAAType fsaaType, GrClampType clampType) {
Brian Salomonc2f42542017-07-12 14:11:22 -0400211 GrProcessorAnalysisColor gpColor;
212 if (this->requiresPerVertexColors()) {
213 gpColor.setToUnknown();
214 } else {
215 gpColor.setToConstant(fMeshes.front().fColor);
216 }
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700217 auto result = fHelper.finalizeProcessors(
Brian Osman5ced0bf2019-03-15 10:15:29 -0400218 caps, clip, fsaaType, clampType, GrProcessorAnalysisCoverage::kNone, &gpColor);
Brian Salomonc2f42542017-07-12 14:11:22 -0400219 if (gpColor.isConstant(&fMeshes.front().fColor)) {
220 fMeshes.front().fIgnoreColors = true;
Brian Salomon199fb872017-02-06 09:41:10 -0500221 fFlags &= ~kRequiresPerVertexColors_Flag;
Brian Osmanae0c50c2017-05-25 16:56:34 -0400222 fColorArrayType = ColorArrayType::kPremulGrColor;
joshualitt2771b562015-08-07 12:46:26 -0700223 }
Brian Salomonc2f42542017-07-12 14:11:22 -0400224 if (!fHelper.usesLocalCoords()) {
Brian Osman8a030552017-05-23 15:03:18 -0400225 fMeshes[0].fIgnoreTexCoords = true;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400226 fFlags &= ~kAnyMeshHasExplicitLocalCoords_Flag;
bsalomon14eaaa62015-09-24 07:01:26 -0700227 }
Brian Salomonc2f42542017-07-12 14:11:22 -0400228 return result;
joshualitt2771b562015-08-07 12:46:26 -0700229}
230
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500231sk_sp<GrGeometryProcessor> DrawVerticesOp::makeGP(const GrShaderCaps* shaderCaps,
232 bool* hasColorAttribute,
233 bool* hasLocalCoordAttribute) const {
Brian Salomon199fb872017-02-06 09:41:10 -0500234 using namespace GrDefaultGeoProcFactory;
235 LocalCoords::Type localCoordsType;
Brian Salomonc2f42542017-07-12 14:11:22 -0400236 if (fHelper.usesLocalCoords()) {
Brian Salomon199fb872017-02-06 09:41:10 -0500237 // If we have multiple view matrices we will transform the positions into device space. We
238 // must then also provide untransformed positions as local coords.
239 if (this->anyMeshHasExplicitLocalCoords() || this->hasMultipleViewMatrices()) {
240 *hasLocalCoordAttribute = true;
241 localCoordsType = LocalCoords::kHasExplicit_Type;
242 } else {
243 *hasLocalCoordAttribute = false;
244 localCoordsType = LocalCoords::kUsePosition_Type;
245 }
246 } else {
247 localCoordsType = LocalCoords::kUnused_Type;
248 *hasLocalCoordAttribute = false;
249 }
250
251 Color color(fMeshes[0].fColor);
252 if (this->requiresPerVertexColors()) {
Brian Osman08a50e02018-06-15 15:06:48 -0400253 if (fColorArrayType == ColorArrayType::kPremulGrColor) {
254 color.fType = Color::kPremulGrColorAttribute_Type;
255 } else {
256 color.fType = Color::kUnpremulSkColorAttribute_Type;
257 color.fColorSpaceXform = fColorSpaceXform;
258 }
Brian Salomon199fb872017-02-06 09:41:10 -0500259 *hasColorAttribute = true;
260 } else {
261 *hasColorAttribute = false;
Brian Salomon23356442018-11-30 15:33:19 -0500262 }
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400263
Brian Salomon199fb872017-02-06 09:41:10 -0500264 const SkMatrix& vm = this->hasMultipleViewMatrices() ? SkMatrix::I() : fMeshes[0].fViewMatrix;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400265
Brian Osman37064c12019-02-08 10:53:07 -0500266 return GrDefaultGeoProcFactory::Make(shaderCaps,
267 color,
268 Coverage::kSolid_Type,
269 localCoordsType,
270 vm);
Brian Salomon199fb872017-02-06 09:41:10 -0500271}
272
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500273void DrawVerticesOp::onPrepareDraws(Target* target) {
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400274 bool hasMapBufferSupport = GrCaps::kNone_MapFlags != target->caps().mapBufferFlags();
275 if (fMeshes[0].fVertices->isVolatile() || !hasMapBufferSupport) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400276 this->drawVolatile(target);
277 } else {
278 this->drawNonVolatile(target);
279 }
280}
281
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500282void DrawVerticesOp::drawVolatile(Target* target) {
Brian Salomon199fb872017-02-06 09:41:10 -0500283 bool hasColorAttribute;
284 bool hasLocalCoordsAttribute;
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400285 sk_sp<GrGeometryProcessor> gp = this->makeGP(target->caps().shaderCaps(),
286 &hasColorAttribute,
Brian Osman37064c12019-02-08 10:53:07 -0500287 &hasLocalCoordsAttribute);
joshualitt2771b562015-08-07 12:46:26 -0700288
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400289 // Allocate buffers.
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500290 size_t vertexStride = gp->vertexStride();
Brian Salomondbf70722019-02-07 11:31:24 -0500291 sk_sp<const GrBuffer> vertexBuffer;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400292 int firstVertex = 0;
bsalomon14eaaa62015-09-24 07:01:26 -0700293 void* verts = target->makeVertexSpace(vertexStride, fVertexCount, &vertexBuffer, &firstVertex);
joshualitt2771b562015-08-07 12:46:26 -0700294 if (!verts) {
295 SkDebugf("Could not allocate vertices\n");
296 return;
297 }
298
Brian Salomondbf70722019-02-07 11:31:24 -0500299 sk_sp<const GrBuffer> indexBuffer;
joshualitt2771b562015-08-07 12:46:26 -0700300 int firstIndex = 0;
halcanary96fcdcc2015-08-27 07:41:13 -0700301 uint16_t* indices = nullptr;
Brian Salomon199fb872017-02-06 09:41:10 -0500302 if (this->isIndexed()) {
bsalomon14eaaa62015-09-24 07:01:26 -0700303 indices = target->makeIndexSpace(fIndexCount, &indexBuffer, &firstIndex);
joshualitt2771b562015-08-07 12:46:26 -0700304 if (!indices) {
305 SkDebugf("Could not allocate indices\n");
306 return;
307 }
308 }
309
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400310 // Fill the buffers.
311 this->fillBuffers(hasColorAttribute,
312 hasLocalCoordsAttribute,
313 vertexStride,
314 verts,
315 indices);
316
317 // Draw the vertices.
Brian Salomon12d22642019-01-29 14:38:50 -0500318 this->drawVertices(target, std::move(gp), std::move(vertexBuffer), firstVertex, indexBuffer,
319 firstIndex);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400320}
321
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500322void DrawVerticesOp::drawNonVolatile(Target* target) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400323 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
324
325 bool hasColorAttribute;
326 bool hasLocalCoordsAttribute;
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400327 sk_sp<GrGeometryProcessor> gp = this->makeGP(target->caps().shaderCaps(),
328 &hasColorAttribute,
Brian Osman37064c12019-02-08 10:53:07 -0500329 &hasLocalCoordsAttribute);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400330
331 SkASSERT(fMeshes.count() == 1); // Non-volatile meshes should never combine.
332
333 // Get the resource provider.
334 GrResourceProvider* rp = target->resourceProvider();
335
336 // Generate keys for the buffers.
337 GrUniqueKey vertexKey, indexKey;
338 GrUniqueKey::Builder vertexKeyBuilder(&vertexKey, kDomain, 2);
339 GrUniqueKey::Builder indexKeyBuilder(&indexKey, kDomain, 2);
340 vertexKeyBuilder[0] = indexKeyBuilder[0] = fMeshes[0].fVertices->uniqueID();
341 vertexKeyBuilder[1] = 0;
342 indexKeyBuilder[1] = 1;
343 vertexKeyBuilder.finish();
344 indexKeyBuilder.finish();
345
346 // Try to grab data from the cache.
Brian Salomondbf70722019-02-07 11:31:24 -0500347 sk_sp<GrGpuBuffer> vertexBuffer = rp->findByUniqueKey<GrGpuBuffer>(vertexKey);
348 sk_sp<GrGpuBuffer> indexBuffer =
349 this->isIndexed() ? rp->findByUniqueKey<GrGpuBuffer>(indexKey) : nullptr;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400350
351 // Draw using the cached buffers if possible.
352 if (vertexBuffer && (!this->isIndexed() || indexBuffer)) {
Brian Salomon12d22642019-01-29 14:38:50 -0500353 this->drawVertices(target, std::move(gp), std::move(vertexBuffer), 0,
354 std::move(indexBuffer), 0);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400355 return;
356 }
357
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400358 // Allocate vertex buffer.
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500359 size_t vertexStride = gp->vertexStride();
Brian Salomondbf70722019-02-07 11:31:24 -0500360 vertexBuffer = rp->createBuffer(
361 fVertexCount * vertexStride, GrGpuBufferType::kVertex, kStatic_GrAccessPattern);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400362 void* verts = vertexBuffer ? vertexBuffer->map() : nullptr;
363 if (!verts) {
364 SkDebugf("Could not allocate vertices\n");
365 return;
366 }
367
368 // Allocate index buffer.
369 uint16_t* indices = nullptr;
370 if (this->isIndexed()) {
Brian Salomondbf70722019-02-07 11:31:24 -0500371 indexBuffer = rp->createBuffer(
372 fIndexCount * sizeof(uint16_t), GrGpuBufferType::kIndex, kStatic_GrAccessPattern);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400373 indices = indexBuffer ? static_cast<uint16_t*>(indexBuffer->map()) : nullptr;
374 if (!indices) {
375 SkDebugf("Could not allocate indices\n");
376 return;
377 }
378 }
379
380 // Fill the buffers.
381 this->fillBuffers(hasColorAttribute,
382 hasLocalCoordsAttribute,
383 vertexStride,
384 verts,
385 indices);
386
387 // Unmap the buffers.
388 vertexBuffer->unmap();
389 if (indexBuffer) {
390 indexBuffer->unmap();
391 }
392
393 // Cache the buffers.
394 rp->assignUniqueKeyToResource(vertexKey, vertexBuffer.get());
395 rp->assignUniqueKeyToResource(indexKey, indexBuffer.get());
396
397 // Draw the vertices.
Brian Salomon12d22642019-01-29 14:38:50 -0500398 this->drawVertices(target, std::move(gp), std::move(vertexBuffer), 0, std::move(indexBuffer),
399 0);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400400}
401
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500402void DrawVerticesOp::fillBuffers(bool hasColorAttribute,
403 bool hasLocalCoordsAttribute,
404 size_t vertexStride,
405 void* verts,
406 uint16_t* indices) const {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400407 int instanceCount = fMeshes.count();
408
409 // Copy data into the buffers.
joshualitt2771b562015-08-07 12:46:26 -0700410 int vertexOffset = 0;
Brian Salomonfab30a32017-02-06 19:06:22 -0500411 // We have a fast case below for uploading the vertex data when the matrix is translate
Brian Osman37064c12019-02-08 10:53:07 -0500412 // only and there are colors but not local coords.
413 bool fastAttrs = hasColorAttribute && !hasLocalCoordsAttribute;
joshualitt2771b562015-08-07 12:46:26 -0700414 for (int i = 0; i < instanceCount; i++) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400415 // Get each mesh.
bsalomond92b4192016-06-30 07:59:23 -0700416 const Mesh& mesh = fMeshes[i];
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400417
418 // Copy data into the index buffer.
bsalomon14eaaa62015-09-24 07:01:26 -0700419 if (indices) {
Brian Salomon199fb872017-02-06 09:41:10 -0500420 int indexCount = mesh.fVertices->indexCount();
Brian Salomonfab30a32017-02-06 19:06:22 -0500421 for (int j = 0; j < indexCount; ++j) {
422 *indices++ = mesh.fVertices->indices()[j] + vertexOffset;
joshualitt2771b562015-08-07 12:46:26 -0700423 }
424 }
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400425
426 // Copy data into the vertex buffer.
Brian Salomon199fb872017-02-06 09:41:10 -0500427 int vertexCount = mesh.fVertices->vertexCount();
428 const SkPoint* positions = mesh.fVertices->positions();
429 const SkColor* colors = mesh.fVertices->colors();
430 const SkPoint* localCoords = mesh.fVertices->texCoords();
Brian Salomonfab30a32017-02-06 19:06:22 -0500431 bool fastMesh = (!this->hasMultipleViewMatrices() ||
432 mesh.fViewMatrix.getType() <= SkMatrix::kTranslate_Mask) &&
433 mesh.hasPerVertexColors();
434 if (fastAttrs && fastMesh) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400435 // Fast case.
Brian Salomonfab30a32017-02-06 19:06:22 -0500436 struct V {
437 SkPoint fPos;
438 uint32_t fColor;
439 };
440 SkASSERT(sizeof(V) == vertexStride);
441 V* v = (V*)verts;
442 Sk2f t(0, 0);
Brian Salomon199fb872017-02-06 09:41:10 -0500443 if (this->hasMultipleViewMatrices()) {
Brian Salomonfab30a32017-02-06 19:06:22 -0500444 t = Sk2f(mesh.fViewMatrix.getTranslateX(), mesh.fViewMatrix.getTranslateY());
joshualitt2771b562015-08-07 12:46:26 -0700445 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500446 for (int j = 0; j < vertexCount; ++j) {
447 Sk2f p = Sk2f::Load(positions++) + t;
448 p.store(&v[j].fPos);
449 v[j].fColor = colors[j];
450 }
451 verts = v + vertexCount;
452 } else {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400453 // Normal case.
Brian Salomonfab30a32017-02-06 19:06:22 -0500454 static constexpr size_t kColorOffset = sizeof(SkPoint);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400455 size_t offset = kColorOffset;
456 if (hasColorAttribute) {
457 offset += sizeof(uint32_t);
458 }
459 size_t localCoordOffset = offset;
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400460 if (hasLocalCoordsAttribute) {
461 offset += sizeof(SkPoint);
462 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500463
Brian Osman1be2b7c2018-10-29 16:07:15 -0400464 // TODO4F: Preserve float colors
Brian Osmancf860852018-10-31 14:04:39 -0400465 GrColor color = mesh.fColor.toBytes_RGBA();
Brian Osman1be2b7c2018-10-29 16:07:15 -0400466
Brian Salomonfab30a32017-02-06 19:06:22 -0500467 for (int j = 0; j < vertexCount; ++j) {
468 if (this->hasMultipleViewMatrices()) {
469 mesh.fViewMatrix.mapPoints(((SkPoint*)verts), &positions[j], 1);
Brian Salomon3f363692017-02-02 21:05:19 -0500470 } else {
Brian Salomonfab30a32017-02-06 19:06:22 -0500471 *((SkPoint*)verts) = positions[j];
Brian Salomon199fb872017-02-06 09:41:10 -0500472 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500473 if (hasColorAttribute) {
474 if (mesh.hasPerVertexColors()) {
475 *(uint32_t*)((intptr_t)verts + kColorOffset) = colors[j];
476 } else {
Brian Osman1be2b7c2018-10-29 16:07:15 -0400477 *(uint32_t*)((intptr_t)verts + kColorOffset) = color;
Brian Salomonfab30a32017-02-06 19:06:22 -0500478 }
Brian Salomon3f363692017-02-02 21:05:19 -0500479 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500480 if (hasLocalCoordsAttribute) {
481 if (mesh.hasExplicitLocalCoords()) {
482 *(SkPoint*)((intptr_t)verts + localCoordOffset) = localCoords[j];
483 } else {
484 *(SkPoint*)((intptr_t)verts + localCoordOffset) = positions[j];
485 }
486 }
487 verts = (void*)((intptr_t)verts + vertexStride);
joshualitt2771b562015-08-07 12:46:26 -0700488 }
joshualitt2771b562015-08-07 12:46:26 -0700489 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500490 vertexOffset += vertexCount;
joshualitt2771b562015-08-07 12:46:26 -0700491 }
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400492}
joshualitt2771b562015-08-07 12:46:26 -0700493
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500494void DrawVerticesOp::drawVertices(Target* target,
495 sk_sp<const GrGeometryProcessor> gp,
496 sk_sp<const GrBuffer> vertexBuffer,
497 int firstVertex,
498 sk_sp<const GrBuffer> indexBuffer,
499 int firstIndex) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000500 GrMesh* mesh = target->allocMesh(this->primitiveType());
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400501 if (this->isIndexed()) {
Brian Salomon12d22642019-01-29 14:38:50 -0500502 mesh->setIndexed(std::move(indexBuffer), fIndexCount, firstIndex, 0, fVertexCount - 1,
Brian Salomon7eae3e02018-08-07 14:02:38 +0000503 GrPrimitiveRestart::kNo);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400504 } else {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000505 mesh->setNonIndexedNonInstanced(fVertexCount);
joshualitt2771b562015-08-07 12:46:26 -0700506 }
Brian Salomon12d22642019-01-29 14:38:50 -0500507 mesh->setVertexData(std::move(vertexBuffer), firstVertex);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700508 target->recordDraw(std::move(gp), mesh);
509}
510
511void DrawVerticesOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
512 fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
joshualitt2771b562015-08-07 12:46:26 -0700513}
514
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500515GrOp::CombineResult DrawVerticesOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
516 DrawVerticesOp* that = t->cast<DrawVerticesOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700517
Brian Salomonc2f42542017-07-12 14:11:22 -0400518 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000519 return CombineResult::kCannotCombine;
joshualitt2771b562015-08-07 12:46:26 -0700520 }
521
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400522 // Non-volatile meshes cannot batch, because if a non-volatile mesh batches with another mesh,
523 // then on the next frame, if that non-volatile mesh is drawn, it will draw the other mesh
524 // that was saved in its vertex buffer, which is not necessarily there anymore.
525 if (!this->fMeshes[0].fVertices->isVolatile() || !that->fMeshes[0].fVertices->isVolatile()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000526 return CombineResult::kCannotCombine;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400527 }
528
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500529 if (!this->combinablePrimitive() || this->primitiveType() != that->primitiveType()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000530 return CombineResult::kCannotCombine;
joshualitt2771b562015-08-07 12:46:26 -0700531 }
532
Mike Reedaa9e3322017-03-16 14:38:48 -0400533 if (fMeshes[0].fVertices->hasIndices() != that->fMeshes[0].fVertices->hasIndices()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000534 return CombineResult::kCannotCombine;
joshualitt2771b562015-08-07 12:46:26 -0700535 }
536
Brian Salomon3de0aee2017-01-29 09:34:17 -0500537 if (fColorArrayType != that->fColorArrayType) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000538 return CombineResult::kCannotCombine;
Brian Salomon3de0aee2017-01-29 09:34:17 -0500539 }
540
Ben Wagner9bc36fd2018-06-15 14:23:36 -0400541 if (fVertexCount + that->fVertexCount > SkTo<int>(UINT16_MAX)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000542 return CombineResult::kCannotCombine;
Brian Salomon3f363692017-02-02 21:05:19 -0500543 }
544
Brian Osmanfa6d8652017-05-31 09:37:27 -0400545 // NOTE: For SkColor vertex colors, the source color space is always sRGB, and the destination
546 // gamut is determined by the render target context. A mis-match should be impossible.
547 SkASSERT(GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get()));
548
Brian Salomon199fb872017-02-06 09:41:10 -0500549 // If either op required explicit local coords or per-vertex colors the combined mesh does. Same
550 // with multiple view matrices.
551 fFlags |= that->fFlags;
joshualitt2771b562015-08-07 12:46:26 -0700552
Brian Salomon199fb872017-02-06 09:41:10 -0500553 if (!this->requiresPerVertexColors() && this->fMeshes[0].fColor != that->fMeshes[0].fColor) {
554 fFlags |= kRequiresPerVertexColors_Flag;
555 }
Brian Salomon3f363692017-02-02 21:05:19 -0500556 // Check whether we are about to acquire a mesh with a different view matrix.
Brian Salomon199fb872017-02-06 09:41:10 -0500557 if (!this->hasMultipleViewMatrices() &&
558 !this->fMeshes[0].fViewMatrix.cheapEqualTo(that->fMeshes[0].fViewMatrix)) {
559 fFlags |= kHasMultipleViewMatrices_Flag;
Brian Salomon3f363692017-02-02 21:05:19 -0500560 }
561
bsalomond92b4192016-06-30 07:59:23 -0700562 fMeshes.push_back_n(that->fMeshes.count(), that->fMeshes.begin());
bsalomon14eaaa62015-09-24 07:01:26 -0700563 fVertexCount += that->fVertexCount;
564 fIndexCount += that->fIndexCount;
joshualitt2771b562015-08-07 12:46:26 -0700565
Brian Salomon7eae3e02018-08-07 14:02:38 +0000566 return CombineResult::kMerged;
joshualitt2771b562015-08-07 12:46:26 -0700567}
568
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500569} // anonymous namespace
570
Robert Phillipsb97da532019-02-12 15:24:12 -0500571std::unique_ptr<GrDrawOp> GrDrawVerticesOp::Make(GrRecordingContext* context,
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500572 GrPaint&& paint,
573 sk_sp<SkVertices> vertices,
574 const SkVertices::Bone bones[],
575 int boneCount,
576 const SkMatrix& viewMatrix,
577 GrAAType aaType,
578 sk_sp<GrColorSpaceXform> colorSpaceXform,
579 GrPrimitiveType* overridePrimType) {
580 SkASSERT(vertices);
581 GrPrimitiveType primType = overridePrimType ? *overridePrimType
582 : SkVertexModeToGrPrimitiveType(vertices->mode());
583 return GrSimpleMeshDrawOpHelper::FactoryHelper<DrawVerticesOp>(context, std::move(paint),
584 std::move(vertices),
585 bones, boneCount,
586 primType, aaType,
587 std::move(colorSpaceXform),
588 viewMatrix);
589}
590
joshualitt2771b562015-08-07 12:46:26 -0700591///////////////////////////////////////////////////////////////////////////////////////////////////
592
Hal Canary6f6961e2017-01-31 13:50:44 -0500593#if GR_TEST_UTILS
joshualitt2771b562015-08-07 12:46:26 -0700594
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500595#include "src/gpu/GrDrawOpTest.h"
joshualitt2771b562015-08-07 12:46:26 -0700596
597static uint32_t seed_vertices(GrPrimitiveType type) {
598 switch (type) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600599 case GrPrimitiveType::kTriangles:
600 case GrPrimitiveType::kTriangleStrip:
joshualitt2771b562015-08-07 12:46:26 -0700601 return 3;
Chris Dalton3809bab2017-06-13 10:55:06 -0600602 case GrPrimitiveType::kPoints:
joshualitt2771b562015-08-07 12:46:26 -0700603 return 1;
Chris Dalton3809bab2017-06-13 10:55:06 -0600604 case GrPrimitiveType::kLines:
605 case GrPrimitiveType::kLineStrip:
joshualitt2771b562015-08-07 12:46:26 -0700606 return 2;
Chris Dalton3809bab2017-06-13 10:55:06 -0600607 case GrPrimitiveType::kLinesAdjacency:
608 return 4;
joshualitt2771b562015-08-07 12:46:26 -0700609 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400610 SK_ABORT("Incomplete switch\n");
joshualitt2771b562015-08-07 12:46:26 -0700611 return 0;
612}
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 return 0;
629}
630
631static SkPoint random_point(SkRandom* random, SkScalar min, SkScalar max) {
632 SkPoint p;
633 p.fX = random->nextRangeScalar(min, max);
634 p.fY = random->nextRangeScalar(min, max);
635 return p;
636}
637
638static void randomize_params(size_t count, size_t maxVertex, SkScalar min, SkScalar max,
Brian Salomonfc527d22016-12-14 21:07:01 -0500639 SkRandom* random, SkTArray<SkPoint>* positions,
joshualitt2771b562015-08-07 12:46:26 -0700640 SkTArray<SkPoint>* texCoords, bool hasTexCoords,
Brian Salomon3de0aee2017-01-29 09:34:17 -0500641 SkTArray<uint32_t>* colors, bool hasColors,
642 SkTArray<uint16_t>* indices, bool hasIndices) {
joshualitt2771b562015-08-07 12:46:26 -0700643 for (uint32_t v = 0; v < count; v++) {
644 positions->push_back(random_point(random, min, max));
645 if (hasTexCoords) {
646 texCoords->push_back(random_point(random, min, max));
647 }
648 if (hasColors) {
649 colors->push_back(GrRandomColor(random));
650 }
651 if (hasIndices) {
Ben Wagnerb0897652018-06-15 15:37:57 +0000652 SkASSERT(maxVertex <= UINT16_MAX);
joshualitt2771b562015-08-07 12:46:26 -0700653 indices->push_back(random->nextULessThan((uint16_t)maxVertex));
654 }
655 }
656}
657
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500658GR_DRAW_OP_TEST_DEFINE(DrawVerticesOp) {
Chris Daltonb894c2b2017-06-14 12:39:19 -0600659 GrPrimitiveType type;
660 do {
661 type = GrPrimitiveType(random->nextULessThan(kNumGrPrimitiveTypes));
662 } while (GrPrimTypeRequiresGeometryShaderSupport(type) &&
Robert Phillips9da87e02019-02-04 13:26:26 -0500663 !context->priv().caps()->shaderCaps()->geometryShaderSupport());
Chris Daltonb894c2b2017-06-14 12:39:19 -0600664
joshualitt2771b562015-08-07 12:46:26 -0700665 uint32_t primitiveCount = random->nextRangeU(1, 100);
666
667 // TODO make 'sensible' indexbuffers
668 SkTArray<SkPoint> positions;
669 SkTArray<SkPoint> texCoords;
Brian Salomon3de0aee2017-01-29 09:34:17 -0500670 SkTArray<uint32_t> colors;
joshualitt2771b562015-08-07 12:46:26 -0700671 SkTArray<uint16_t> indices;
672
673 bool hasTexCoords = random->nextBool();
674 bool hasIndices = random->nextBool();
675 bool hasColors = random->nextBool();
676
677 uint32_t vertexCount = seed_vertices(type) + (primitiveCount - 1) * primitive_vertices(type);
678
679 static const SkScalar kMinVertExtent = -100.f;
680 static const SkScalar kMaxVertExtent = 100.f;
Brian Salomonfc527d22016-12-14 21:07:01 -0500681 randomize_params(seed_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent, random,
682 &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices,
683 hasIndices);
joshualitt2771b562015-08-07 12:46:26 -0700684
685 for (uint32_t i = 1; i < primitiveCount; i++) {
686 randomize_params(primitive_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent,
Brian Salomonfc527d22016-12-14 21:07:01 -0500687 random, &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices,
688 hasIndices);
joshualitt2771b562015-08-07 12:46:26 -0700689 }
690
691 SkMatrix viewMatrix = GrTest::TestMatrix(random);
joshualitt2771b562015-08-07 12:46:26 -0700692
Brian Osmanfa6d8652017-05-31 09:37:27 -0400693 sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(random);
Brian Osmanae0c50c2017-05-25 16:56:34 -0400694
695 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
696 sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, vertexCount, positions.begin(),
697 texCoords.begin(), colors.begin(),
698 hasIndices ? indices.count() : 0,
699 indices.begin());
Brian Salomonc2f42542017-07-12 14:11:22 -0400700 GrAAType aaType = GrAAType::kNone;
701 if (GrFSAAType::kUnifiedMSAA == fsaaType && random->nextBool()) {
702 aaType = GrAAType::kMSAA;
703 }
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400704 return GrDrawVerticesOp::Make(context, std::move(paint), std::move(vertices), nullptr, 0,
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400705 viewMatrix, aaType, std::move(colorSpaceXform), &type);
joshualitt2771b562015-08-07 12:46:26 -0700706}
707
708#endif