blob: f1589ed2016be4986836e262bee91112039e2c93 [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
41 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip) override;
42
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
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500208GrProcessorSet::Analysis DrawVerticesOp::finalize(const GrCaps& caps, const GrAppliedClip* clip) {
Brian Salomonc2f42542017-07-12 14:11:22 -0400209 GrProcessorAnalysisColor gpColor;
210 if (this->requiresPerVertexColors()) {
211 gpColor.setToUnknown();
212 } else {
213 gpColor.setToConstant(fMeshes.front().fColor);
214 }
Chris Dalton4b62aed2019-01-15 11:53:00 -0700215 auto result = fHelper.finalizeProcessors(caps, clip, GrProcessorAnalysisCoverage::kNone,
216 &gpColor);
Brian Salomonc2f42542017-07-12 14:11:22 -0400217 if (gpColor.isConstant(&fMeshes.front().fColor)) {
218 fMeshes.front().fIgnoreColors = true;
Brian Salomon199fb872017-02-06 09:41:10 -0500219 fFlags &= ~kRequiresPerVertexColors_Flag;
Brian Osmanae0c50c2017-05-25 16:56:34 -0400220 fColorArrayType = ColorArrayType::kPremulGrColor;
joshualitt2771b562015-08-07 12:46:26 -0700221 }
Brian Salomonc2f42542017-07-12 14:11:22 -0400222 if (!fHelper.usesLocalCoords()) {
Brian Osman8a030552017-05-23 15:03:18 -0400223 fMeshes[0].fIgnoreTexCoords = true;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400224 fFlags &= ~kAnyMeshHasExplicitLocalCoords_Flag;
bsalomon14eaaa62015-09-24 07:01:26 -0700225 }
Brian Salomonc2f42542017-07-12 14:11:22 -0400226 return result;
joshualitt2771b562015-08-07 12:46:26 -0700227}
228
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500229sk_sp<GrGeometryProcessor> DrawVerticesOp::makeGP(const GrShaderCaps* shaderCaps,
230 bool* hasColorAttribute,
231 bool* hasLocalCoordAttribute) const {
Brian Salomon199fb872017-02-06 09:41:10 -0500232 using namespace GrDefaultGeoProcFactory;
233 LocalCoords::Type localCoordsType;
Brian Salomonc2f42542017-07-12 14:11:22 -0400234 if (fHelper.usesLocalCoords()) {
Brian Salomon199fb872017-02-06 09:41:10 -0500235 // If we have multiple view matrices we will transform the positions into device space. We
236 // must then also provide untransformed positions as local coords.
237 if (this->anyMeshHasExplicitLocalCoords() || this->hasMultipleViewMatrices()) {
238 *hasLocalCoordAttribute = true;
239 localCoordsType = LocalCoords::kHasExplicit_Type;
240 } else {
241 *hasLocalCoordAttribute = false;
242 localCoordsType = LocalCoords::kUsePosition_Type;
243 }
244 } else {
245 localCoordsType = LocalCoords::kUnused_Type;
246 *hasLocalCoordAttribute = false;
247 }
248
249 Color color(fMeshes[0].fColor);
250 if (this->requiresPerVertexColors()) {
Brian Osman08a50e02018-06-15 15:06:48 -0400251 if (fColorArrayType == ColorArrayType::kPremulGrColor) {
252 color.fType = Color::kPremulGrColorAttribute_Type;
253 } else {
254 color.fType = Color::kUnpremulSkColorAttribute_Type;
255 color.fColorSpaceXform = fColorSpaceXform;
256 }
Brian Salomon199fb872017-02-06 09:41:10 -0500257 *hasColorAttribute = true;
258 } else {
259 *hasColorAttribute = false;
Brian Salomon23356442018-11-30 15:33:19 -0500260 }
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400261
Brian Salomon199fb872017-02-06 09:41:10 -0500262 const SkMatrix& vm = this->hasMultipleViewMatrices() ? SkMatrix::I() : fMeshes[0].fViewMatrix;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400263
Brian Osman37064c12019-02-08 10:53:07 -0500264 return GrDefaultGeoProcFactory::Make(shaderCaps,
265 color,
266 Coverage::kSolid_Type,
267 localCoordsType,
268 vm);
Brian Salomon199fb872017-02-06 09:41:10 -0500269}
270
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500271void DrawVerticesOp::onPrepareDraws(Target* target) {
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400272 bool hasMapBufferSupport = GrCaps::kNone_MapFlags != target->caps().mapBufferFlags();
273 if (fMeshes[0].fVertices->isVolatile() || !hasMapBufferSupport) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400274 this->drawVolatile(target);
275 } else {
276 this->drawNonVolatile(target);
277 }
278}
279
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500280void DrawVerticesOp::drawVolatile(Target* target) {
Brian Salomon199fb872017-02-06 09:41:10 -0500281 bool hasColorAttribute;
282 bool hasLocalCoordsAttribute;
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400283 sk_sp<GrGeometryProcessor> gp = this->makeGP(target->caps().shaderCaps(),
284 &hasColorAttribute,
Brian Osman37064c12019-02-08 10:53:07 -0500285 &hasLocalCoordsAttribute);
joshualitt2771b562015-08-07 12:46:26 -0700286
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400287 // Allocate buffers.
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500288 size_t vertexStride = gp->vertexStride();
Brian Salomondbf70722019-02-07 11:31:24 -0500289 sk_sp<const GrBuffer> vertexBuffer;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400290 int firstVertex = 0;
bsalomon14eaaa62015-09-24 07:01:26 -0700291 void* verts = target->makeVertexSpace(vertexStride, fVertexCount, &vertexBuffer, &firstVertex);
joshualitt2771b562015-08-07 12:46:26 -0700292 if (!verts) {
293 SkDebugf("Could not allocate vertices\n");
294 return;
295 }
296
Brian Salomondbf70722019-02-07 11:31:24 -0500297 sk_sp<const GrBuffer> indexBuffer;
joshualitt2771b562015-08-07 12:46:26 -0700298 int firstIndex = 0;
halcanary96fcdcc2015-08-27 07:41:13 -0700299 uint16_t* indices = nullptr;
Brian Salomon199fb872017-02-06 09:41:10 -0500300 if (this->isIndexed()) {
bsalomon14eaaa62015-09-24 07:01:26 -0700301 indices = target->makeIndexSpace(fIndexCount, &indexBuffer, &firstIndex);
joshualitt2771b562015-08-07 12:46:26 -0700302 if (!indices) {
303 SkDebugf("Could not allocate indices\n");
304 return;
305 }
306 }
307
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400308 // Fill the buffers.
309 this->fillBuffers(hasColorAttribute,
310 hasLocalCoordsAttribute,
311 vertexStride,
312 verts,
313 indices);
314
315 // Draw the vertices.
Brian Salomon12d22642019-01-29 14:38:50 -0500316 this->drawVertices(target, std::move(gp), std::move(vertexBuffer), firstVertex, indexBuffer,
317 firstIndex);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400318}
319
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500320void DrawVerticesOp::drawNonVolatile(Target* target) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400321 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
322
323 bool hasColorAttribute;
324 bool hasLocalCoordsAttribute;
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400325 sk_sp<GrGeometryProcessor> gp = this->makeGP(target->caps().shaderCaps(),
326 &hasColorAttribute,
Brian Osman37064c12019-02-08 10:53:07 -0500327 &hasLocalCoordsAttribute);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400328
329 SkASSERT(fMeshes.count() == 1); // Non-volatile meshes should never combine.
330
331 // Get the resource provider.
332 GrResourceProvider* rp = target->resourceProvider();
333
334 // Generate keys for the buffers.
335 GrUniqueKey vertexKey, indexKey;
336 GrUniqueKey::Builder vertexKeyBuilder(&vertexKey, kDomain, 2);
337 GrUniqueKey::Builder indexKeyBuilder(&indexKey, kDomain, 2);
338 vertexKeyBuilder[0] = indexKeyBuilder[0] = fMeshes[0].fVertices->uniqueID();
339 vertexKeyBuilder[1] = 0;
340 indexKeyBuilder[1] = 1;
341 vertexKeyBuilder.finish();
342 indexKeyBuilder.finish();
343
344 // Try to grab data from the cache.
Brian Salomondbf70722019-02-07 11:31:24 -0500345 sk_sp<GrGpuBuffer> vertexBuffer = rp->findByUniqueKey<GrGpuBuffer>(vertexKey);
346 sk_sp<GrGpuBuffer> indexBuffer =
347 this->isIndexed() ? rp->findByUniqueKey<GrGpuBuffer>(indexKey) : nullptr;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400348
349 // Draw using the cached buffers if possible.
350 if (vertexBuffer && (!this->isIndexed() || indexBuffer)) {
Brian Salomon12d22642019-01-29 14:38:50 -0500351 this->drawVertices(target, std::move(gp), std::move(vertexBuffer), 0,
352 std::move(indexBuffer), 0);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400353 return;
354 }
355
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400356 // Allocate vertex buffer.
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500357 size_t vertexStride = gp->vertexStride();
Brian Salomondbf70722019-02-07 11:31:24 -0500358 vertexBuffer = rp->createBuffer(
359 fVertexCount * vertexStride, GrGpuBufferType::kVertex, kStatic_GrAccessPattern);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400360 void* verts = vertexBuffer ? vertexBuffer->map() : nullptr;
361 if (!verts) {
362 SkDebugf("Could not allocate vertices\n");
363 return;
364 }
365
366 // Allocate index buffer.
367 uint16_t* indices = nullptr;
368 if (this->isIndexed()) {
Brian Salomondbf70722019-02-07 11:31:24 -0500369 indexBuffer = rp->createBuffer(
370 fIndexCount * sizeof(uint16_t), GrGpuBufferType::kIndex, kStatic_GrAccessPattern);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400371 indices = indexBuffer ? static_cast<uint16_t*>(indexBuffer->map()) : nullptr;
372 if (!indices) {
373 SkDebugf("Could not allocate indices\n");
374 return;
375 }
376 }
377
378 // Fill the buffers.
379 this->fillBuffers(hasColorAttribute,
380 hasLocalCoordsAttribute,
381 vertexStride,
382 verts,
383 indices);
384
385 // Unmap the buffers.
386 vertexBuffer->unmap();
387 if (indexBuffer) {
388 indexBuffer->unmap();
389 }
390
391 // Cache the buffers.
392 rp->assignUniqueKeyToResource(vertexKey, vertexBuffer.get());
393 rp->assignUniqueKeyToResource(indexKey, indexBuffer.get());
394
395 // Draw the vertices.
Brian Salomon12d22642019-01-29 14:38:50 -0500396 this->drawVertices(target, std::move(gp), std::move(vertexBuffer), 0, std::move(indexBuffer),
397 0);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400398}
399
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500400void DrawVerticesOp::fillBuffers(bool hasColorAttribute,
401 bool hasLocalCoordsAttribute,
402 size_t vertexStride,
403 void* verts,
404 uint16_t* indices) const {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400405 int instanceCount = fMeshes.count();
406
407 // Copy data into the buffers.
joshualitt2771b562015-08-07 12:46:26 -0700408 int vertexOffset = 0;
Brian Salomonfab30a32017-02-06 19:06:22 -0500409 // We have a fast case below for uploading the vertex data when the matrix is translate
Brian Osman37064c12019-02-08 10:53:07 -0500410 // only and there are colors but not local coords.
411 bool fastAttrs = hasColorAttribute && !hasLocalCoordsAttribute;
joshualitt2771b562015-08-07 12:46:26 -0700412 for (int i = 0; i < instanceCount; i++) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400413 // Get each mesh.
bsalomond92b4192016-06-30 07:59:23 -0700414 const Mesh& mesh = fMeshes[i];
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400415
416 // Copy data into the index buffer.
bsalomon14eaaa62015-09-24 07:01:26 -0700417 if (indices) {
Brian Salomon199fb872017-02-06 09:41:10 -0500418 int indexCount = mesh.fVertices->indexCount();
Brian Salomonfab30a32017-02-06 19:06:22 -0500419 for (int j = 0; j < indexCount; ++j) {
420 *indices++ = mesh.fVertices->indices()[j] + vertexOffset;
joshualitt2771b562015-08-07 12:46:26 -0700421 }
422 }
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400423
424 // Copy data into the vertex buffer.
Brian Salomon199fb872017-02-06 09:41:10 -0500425 int vertexCount = mesh.fVertices->vertexCount();
426 const SkPoint* positions = mesh.fVertices->positions();
427 const SkColor* colors = mesh.fVertices->colors();
428 const SkPoint* localCoords = mesh.fVertices->texCoords();
Brian Salomonfab30a32017-02-06 19:06:22 -0500429 bool fastMesh = (!this->hasMultipleViewMatrices() ||
430 mesh.fViewMatrix.getType() <= SkMatrix::kTranslate_Mask) &&
431 mesh.hasPerVertexColors();
432 if (fastAttrs && fastMesh) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400433 // Fast case.
Brian Salomonfab30a32017-02-06 19:06:22 -0500434 struct V {
435 SkPoint fPos;
436 uint32_t fColor;
437 };
438 SkASSERT(sizeof(V) == vertexStride);
439 V* v = (V*)verts;
440 Sk2f t(0, 0);
Brian Salomon199fb872017-02-06 09:41:10 -0500441 if (this->hasMultipleViewMatrices()) {
Brian Salomonfab30a32017-02-06 19:06:22 -0500442 t = Sk2f(mesh.fViewMatrix.getTranslateX(), mesh.fViewMatrix.getTranslateY());
joshualitt2771b562015-08-07 12:46:26 -0700443 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500444 for (int j = 0; j < vertexCount; ++j) {
445 Sk2f p = Sk2f::Load(positions++) + t;
446 p.store(&v[j].fPos);
447 v[j].fColor = colors[j];
448 }
449 verts = v + vertexCount;
450 } else {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400451 // Normal case.
Brian Salomonfab30a32017-02-06 19:06:22 -0500452 static constexpr size_t kColorOffset = sizeof(SkPoint);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400453 size_t offset = kColorOffset;
454 if (hasColorAttribute) {
455 offset += sizeof(uint32_t);
456 }
457 size_t localCoordOffset = offset;
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400458 if (hasLocalCoordsAttribute) {
459 offset += sizeof(SkPoint);
460 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500461
Brian Osman1be2b7c2018-10-29 16:07:15 -0400462 // TODO4F: Preserve float colors
Brian Osmancf860852018-10-31 14:04:39 -0400463 GrColor color = mesh.fColor.toBytes_RGBA();
Brian Osman1be2b7c2018-10-29 16:07:15 -0400464
Brian Salomonfab30a32017-02-06 19:06:22 -0500465 for (int j = 0; j < vertexCount; ++j) {
466 if (this->hasMultipleViewMatrices()) {
467 mesh.fViewMatrix.mapPoints(((SkPoint*)verts), &positions[j], 1);
Brian Salomon3f363692017-02-02 21:05:19 -0500468 } else {
Brian Salomonfab30a32017-02-06 19:06:22 -0500469 *((SkPoint*)verts) = positions[j];
Brian Salomon199fb872017-02-06 09:41:10 -0500470 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500471 if (hasColorAttribute) {
472 if (mesh.hasPerVertexColors()) {
473 *(uint32_t*)((intptr_t)verts + kColorOffset) = colors[j];
474 } else {
Brian Osman1be2b7c2018-10-29 16:07:15 -0400475 *(uint32_t*)((intptr_t)verts + kColorOffset) = color;
Brian Salomonfab30a32017-02-06 19:06:22 -0500476 }
Brian Salomon3f363692017-02-02 21:05:19 -0500477 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500478 if (hasLocalCoordsAttribute) {
479 if (mesh.hasExplicitLocalCoords()) {
480 *(SkPoint*)((intptr_t)verts + localCoordOffset) = localCoords[j];
481 } else {
482 *(SkPoint*)((intptr_t)verts + localCoordOffset) = positions[j];
483 }
484 }
485 verts = (void*)((intptr_t)verts + vertexStride);
joshualitt2771b562015-08-07 12:46:26 -0700486 }
joshualitt2771b562015-08-07 12:46:26 -0700487 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500488 vertexOffset += vertexCount;
joshualitt2771b562015-08-07 12:46:26 -0700489 }
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400490}
joshualitt2771b562015-08-07 12:46:26 -0700491
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500492void DrawVerticesOp::drawVertices(Target* target,
493 sk_sp<const GrGeometryProcessor> gp,
494 sk_sp<const GrBuffer> vertexBuffer,
495 int firstVertex,
496 sk_sp<const GrBuffer> indexBuffer,
497 int firstIndex) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000498 GrMesh* mesh = target->allocMesh(this->primitiveType());
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400499 if (this->isIndexed()) {
Brian Salomon12d22642019-01-29 14:38:50 -0500500 mesh->setIndexed(std::move(indexBuffer), fIndexCount, firstIndex, 0, fVertexCount - 1,
Brian Salomon7eae3e02018-08-07 14:02:38 +0000501 GrPrimitiveRestart::kNo);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400502 } else {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000503 mesh->setNonIndexedNonInstanced(fVertexCount);
joshualitt2771b562015-08-07 12:46:26 -0700504 }
Brian Salomon12d22642019-01-29 14:38:50 -0500505 mesh->setVertexData(std::move(vertexBuffer), firstVertex);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700506 target->recordDraw(std::move(gp), mesh);
507}
508
509void DrawVerticesOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
510 fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
joshualitt2771b562015-08-07 12:46:26 -0700511}
512
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500513GrOp::CombineResult DrawVerticesOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
514 DrawVerticesOp* that = t->cast<DrawVerticesOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700515
Brian Salomonc2f42542017-07-12 14:11:22 -0400516 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000517 return CombineResult::kCannotCombine;
joshualitt2771b562015-08-07 12:46:26 -0700518 }
519
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400520 // Non-volatile meshes cannot batch, because if a non-volatile mesh batches with another mesh,
521 // then on the next frame, if that non-volatile mesh is drawn, it will draw the other mesh
522 // that was saved in its vertex buffer, which is not necessarily there anymore.
523 if (!this->fMeshes[0].fVertices->isVolatile() || !that->fMeshes[0].fVertices->isVolatile()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000524 return CombineResult::kCannotCombine;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400525 }
526
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500527 if (!this->combinablePrimitive() || this->primitiveType() != that->primitiveType()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000528 return CombineResult::kCannotCombine;
joshualitt2771b562015-08-07 12:46:26 -0700529 }
530
Mike Reedaa9e3322017-03-16 14:38:48 -0400531 if (fMeshes[0].fVertices->hasIndices() != that->fMeshes[0].fVertices->hasIndices()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000532 return CombineResult::kCannotCombine;
joshualitt2771b562015-08-07 12:46:26 -0700533 }
534
Brian Salomon3de0aee2017-01-29 09:34:17 -0500535 if (fColorArrayType != that->fColorArrayType) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000536 return CombineResult::kCannotCombine;
Brian Salomon3de0aee2017-01-29 09:34:17 -0500537 }
538
Ben Wagner9bc36fd2018-06-15 14:23:36 -0400539 if (fVertexCount + that->fVertexCount > SkTo<int>(UINT16_MAX)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000540 return CombineResult::kCannotCombine;
Brian Salomon3f363692017-02-02 21:05:19 -0500541 }
542
Brian Osmanfa6d8652017-05-31 09:37:27 -0400543 // NOTE: For SkColor vertex colors, the source color space is always sRGB, and the destination
544 // gamut is determined by the render target context. A mis-match should be impossible.
545 SkASSERT(GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get()));
546
Brian Salomon199fb872017-02-06 09:41:10 -0500547 // If either op required explicit local coords or per-vertex colors the combined mesh does. Same
548 // with multiple view matrices.
549 fFlags |= that->fFlags;
joshualitt2771b562015-08-07 12:46:26 -0700550
Brian Salomon199fb872017-02-06 09:41:10 -0500551 if (!this->requiresPerVertexColors() && this->fMeshes[0].fColor != that->fMeshes[0].fColor) {
552 fFlags |= kRequiresPerVertexColors_Flag;
553 }
Brian Salomon3f363692017-02-02 21:05:19 -0500554 // Check whether we are about to acquire a mesh with a different view matrix.
Brian Salomon199fb872017-02-06 09:41:10 -0500555 if (!this->hasMultipleViewMatrices() &&
556 !this->fMeshes[0].fViewMatrix.cheapEqualTo(that->fMeshes[0].fViewMatrix)) {
557 fFlags |= kHasMultipleViewMatrices_Flag;
Brian Salomon3f363692017-02-02 21:05:19 -0500558 }
559
bsalomond92b4192016-06-30 07:59:23 -0700560 fMeshes.push_back_n(that->fMeshes.count(), that->fMeshes.begin());
bsalomon14eaaa62015-09-24 07:01:26 -0700561 fVertexCount += that->fVertexCount;
562 fIndexCount += that->fIndexCount;
joshualitt2771b562015-08-07 12:46:26 -0700563
Brian Salomon7eae3e02018-08-07 14:02:38 +0000564 return CombineResult::kMerged;
joshualitt2771b562015-08-07 12:46:26 -0700565}
566
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500567} // anonymous namespace
568
Robert Phillipsb97da532019-02-12 15:24:12 -0500569std::unique_ptr<GrDrawOp> GrDrawVerticesOp::Make(GrRecordingContext* context,
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500570 GrPaint&& paint,
571 sk_sp<SkVertices> vertices,
572 const SkVertices::Bone bones[],
573 int boneCount,
574 const SkMatrix& viewMatrix,
575 GrAAType aaType,
576 sk_sp<GrColorSpaceXform> colorSpaceXform,
577 GrPrimitiveType* overridePrimType) {
578 SkASSERT(vertices);
579 GrPrimitiveType primType = overridePrimType ? *overridePrimType
580 : SkVertexModeToGrPrimitiveType(vertices->mode());
581 return GrSimpleMeshDrawOpHelper::FactoryHelper<DrawVerticesOp>(context, std::move(paint),
582 std::move(vertices),
583 bones, boneCount,
584 primType, aaType,
585 std::move(colorSpaceXform),
586 viewMatrix);
587}
588
joshualitt2771b562015-08-07 12:46:26 -0700589///////////////////////////////////////////////////////////////////////////////////////////////////
590
Hal Canary6f6961e2017-01-31 13:50:44 -0500591#if GR_TEST_UTILS
joshualitt2771b562015-08-07 12:46:26 -0700592
Brian Salomon5ec9def2016-12-20 15:34:05 -0500593#include "GrDrawOpTest.h"
joshualitt2771b562015-08-07 12:46:26 -0700594
595static uint32_t seed_vertices(GrPrimitiveType type) {
596 switch (type) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600597 case GrPrimitiveType::kTriangles:
598 case GrPrimitiveType::kTriangleStrip:
joshualitt2771b562015-08-07 12:46:26 -0700599 return 3;
Chris Dalton3809bab2017-06-13 10:55:06 -0600600 case GrPrimitiveType::kPoints:
joshualitt2771b562015-08-07 12:46:26 -0700601 return 1;
Chris Dalton3809bab2017-06-13 10:55:06 -0600602 case GrPrimitiveType::kLines:
603 case GrPrimitiveType::kLineStrip:
joshualitt2771b562015-08-07 12:46:26 -0700604 return 2;
Chris Dalton3809bab2017-06-13 10:55:06 -0600605 case GrPrimitiveType::kLinesAdjacency:
606 return 4;
joshualitt2771b562015-08-07 12:46:26 -0700607 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400608 SK_ABORT("Incomplete switch\n");
joshualitt2771b562015-08-07 12:46:26 -0700609 return 0;
610}
611
612static uint32_t primitive_vertices(GrPrimitiveType type) {
613 switch (type) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600614 case GrPrimitiveType::kTriangles:
joshualitt2771b562015-08-07 12:46:26 -0700615 return 3;
Chris Dalton3809bab2017-06-13 10:55:06 -0600616 case GrPrimitiveType::kLines:
joshualitt2771b562015-08-07 12:46:26 -0700617 return 2;
Chris Dalton3809bab2017-06-13 10:55:06 -0600618 case GrPrimitiveType::kTriangleStrip:
Chris Dalton3809bab2017-06-13 10:55:06 -0600619 case GrPrimitiveType::kPoints:
620 case GrPrimitiveType::kLineStrip:
joshualitt2771b562015-08-07 12:46:26 -0700621 return 1;
Chris Dalton3809bab2017-06-13 10:55:06 -0600622 case GrPrimitiveType::kLinesAdjacency:
623 return 4;
joshualitt2771b562015-08-07 12:46:26 -0700624 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400625 SK_ABORT("Incomplete switch\n");
joshualitt2771b562015-08-07 12:46:26 -0700626 return 0;
627}
628
629static SkPoint random_point(SkRandom* random, SkScalar min, SkScalar max) {
630 SkPoint p;
631 p.fX = random->nextRangeScalar(min, max);
632 p.fY = random->nextRangeScalar(min, max);
633 return p;
634}
635
636static void randomize_params(size_t count, size_t maxVertex, SkScalar min, SkScalar max,
Brian Salomonfc527d22016-12-14 21:07:01 -0500637 SkRandom* random, SkTArray<SkPoint>* positions,
joshualitt2771b562015-08-07 12:46:26 -0700638 SkTArray<SkPoint>* texCoords, bool hasTexCoords,
Brian Salomon3de0aee2017-01-29 09:34:17 -0500639 SkTArray<uint32_t>* colors, bool hasColors,
640 SkTArray<uint16_t>* indices, bool hasIndices) {
joshualitt2771b562015-08-07 12:46:26 -0700641 for (uint32_t v = 0; v < count; v++) {
642 positions->push_back(random_point(random, min, max));
643 if (hasTexCoords) {
644 texCoords->push_back(random_point(random, min, max));
645 }
646 if (hasColors) {
647 colors->push_back(GrRandomColor(random));
648 }
649 if (hasIndices) {
Ben Wagnerb0897652018-06-15 15:37:57 +0000650 SkASSERT(maxVertex <= UINT16_MAX);
joshualitt2771b562015-08-07 12:46:26 -0700651 indices->push_back(random->nextULessThan((uint16_t)maxVertex));
652 }
653 }
654}
655
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500656GR_DRAW_OP_TEST_DEFINE(DrawVerticesOp) {
Chris Daltonb894c2b2017-06-14 12:39:19 -0600657 GrPrimitiveType type;
658 do {
659 type = GrPrimitiveType(random->nextULessThan(kNumGrPrimitiveTypes));
660 } while (GrPrimTypeRequiresGeometryShaderSupport(type) &&
Robert Phillips9da87e02019-02-04 13:26:26 -0500661 !context->priv().caps()->shaderCaps()->geometryShaderSupport());
Chris Daltonb894c2b2017-06-14 12:39:19 -0600662
joshualitt2771b562015-08-07 12:46:26 -0700663 uint32_t primitiveCount = random->nextRangeU(1, 100);
664
665 // TODO make 'sensible' indexbuffers
666 SkTArray<SkPoint> positions;
667 SkTArray<SkPoint> texCoords;
Brian Salomon3de0aee2017-01-29 09:34:17 -0500668 SkTArray<uint32_t> colors;
joshualitt2771b562015-08-07 12:46:26 -0700669 SkTArray<uint16_t> indices;
670
671 bool hasTexCoords = random->nextBool();
672 bool hasIndices = random->nextBool();
673 bool hasColors = random->nextBool();
674
675 uint32_t vertexCount = seed_vertices(type) + (primitiveCount - 1) * primitive_vertices(type);
676
677 static const SkScalar kMinVertExtent = -100.f;
678 static const SkScalar kMaxVertExtent = 100.f;
Brian Salomonfc527d22016-12-14 21:07:01 -0500679 randomize_params(seed_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent, random,
680 &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices,
681 hasIndices);
joshualitt2771b562015-08-07 12:46:26 -0700682
683 for (uint32_t i = 1; i < primitiveCount; i++) {
684 randomize_params(primitive_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent,
Brian Salomonfc527d22016-12-14 21:07:01 -0500685 random, &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices,
686 hasIndices);
joshualitt2771b562015-08-07 12:46:26 -0700687 }
688
689 SkMatrix viewMatrix = GrTest::TestMatrix(random);
joshualitt2771b562015-08-07 12:46:26 -0700690
Brian Osmanfa6d8652017-05-31 09:37:27 -0400691 sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(random);
Brian Osmanae0c50c2017-05-25 16:56:34 -0400692
693 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
694 sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, vertexCount, positions.begin(),
695 texCoords.begin(), colors.begin(),
696 hasIndices ? indices.count() : 0,
697 indices.begin());
Brian Salomonc2f42542017-07-12 14:11:22 -0400698 GrAAType aaType = GrAAType::kNone;
699 if (GrFSAAType::kUnifiedMSAA == fsaaType && random->nextBool()) {
700 aaType = GrAAType::kMSAA;
701 }
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400702 return GrDrawVerticesOp::Make(context, std::move(paint), std::move(vertices), nullptr, 0,
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400703 viewMatrix, aaType, std::move(colorSpaceXform), &type);
joshualitt2771b562015-08-07 12:46:26 -0700704}
705
706#endif