blob: 315a1ecd3586fc74044cdfeb641de37df8d661db [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;
50
51 void drawVolatile(Target*);
52 void drawNonVolatile(Target*);
53
54 void fillBuffers(bool hasColorAttribute,
55 bool hasLocalCoordsAttribute,
56 size_t vertexStride,
57 void* verts,
58 uint16_t* indices) const;
59
60 void drawVertices(Target*,
61 sk_sp<const GrGeometryProcessor>,
62 sk_sp<const GrBuffer> vertexBuffer,
63 int firstVertex,
64 sk_sp<const GrBuffer> indexBuffer,
65 int firstIndex);
66
67 sk_sp<GrGeometryProcessor> makeGP(const GrShaderCaps* shaderCaps,
68 bool* hasColorAttribute,
69 bool* hasLocalCoordAttribute) const;
70
71 GrPrimitiveType primitiveType() const { return fPrimitiveType; }
72 bool combinablePrimitive() const {
73 return GrPrimitiveType::kTriangles == fPrimitiveType ||
74 GrPrimitiveType::kLines == fPrimitiveType ||
75 GrPrimitiveType::kPoints == fPrimitiveType;
76 }
77
78 CombineResult onCombineIfPossible(GrOp* t, const GrCaps&) override;
79
80 struct Mesh {
81 SkPMColor4f fColor; // Used if this->hasPerVertexColors() is false.
82 sk_sp<SkVertices> fVertices;
83 SkMatrix fViewMatrix;
84 bool fIgnoreTexCoords;
85 bool fIgnoreColors;
86
87 bool hasExplicitLocalCoords() const {
88 return fVertices->hasTexCoords() && !fIgnoreTexCoords;
89 }
90
91 bool hasPerVertexColors() const {
92 return fVertices->hasColors() && !fIgnoreColors;
93 }
94 };
95
96 bool isIndexed() const {
97 // Consistency enforced in onCombineIfPossible.
98 return fMeshes[0].fVertices->hasIndices();
99 }
100
101 bool requiresPerVertexColors() const {
102 return SkToBool(kRequiresPerVertexColors_Flag & fFlags);
103 }
104
105 bool anyMeshHasExplicitLocalCoords() const {
106 return SkToBool(kAnyMeshHasExplicitLocalCoords_Flag & fFlags);
107 }
108
109 bool hasMultipleViewMatrices() const {
110 return SkToBool(kHasMultipleViewMatrices_Flag & fFlags);
111 }
112
113 enum Flags {
114 kRequiresPerVertexColors_Flag = 0x1,
115 kAnyMeshHasExplicitLocalCoords_Flag = 0x2,
116 kHasMultipleViewMatrices_Flag = 0x4,
117 };
118
119 Helper fHelper;
120 SkSTArray<1, Mesh, true> fMeshes;
121 // GrPrimitiveType is more expressive than fVertices.mode() so it is used instead and we ignore
122 // the SkVertices mode (though fPrimitiveType may have been inferred from it).
123 GrPrimitiveType fPrimitiveType;
124 uint32_t fFlags;
125 int fVertexCount;
126 int fIndexCount;
127 ColorArrayType fColorArrayType;
128 sk_sp<GrColorSpaceXform> fColorSpaceXform;
129
130 typedef GrMeshDrawOp INHERITED;
131};
132
133DrawVerticesOp::DrawVerticesOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
134 sk_sp<SkVertices> vertices, const SkVertices::Bone bones[],
135 int boneCount, GrPrimitiveType primitiveType, GrAAType aaType,
136 sk_sp<GrColorSpaceXform> colorSpaceXform,
137 const SkMatrix& viewMatrix)
Brian Osmanfa6d8652017-05-31 09:37:27 -0400138 : INHERITED(ClassID())
Brian Salomonc2f42542017-07-12 14:11:22 -0400139 , fHelper(helperArgs, aaType)
Brian Osmanfa6d8652017-05-31 09:37:27 -0400140 , fPrimitiveType(primitiveType)
141 , fColorSpaceXform(std::move(colorSpaceXform)) {
Brian Salomon199fb872017-02-06 09:41:10 -0500142 SkASSERT(vertices);
143
144 fVertexCount = vertices->vertexCount();
145 fIndexCount = vertices->indexCount();
Brian Osmanae0c50c2017-05-25 16:56:34 -0400146 fColorArrayType = vertices->hasColors() ? ColorArrayType::kSkColor
147 : ColorArrayType::kPremulGrColor;
joshualitt2771b562015-08-07 12:46:26 -0700148
bsalomond92b4192016-06-30 07:59:23 -0700149 Mesh& mesh = fMeshes.push_back();
150 mesh.fColor = color;
Brian Salomon3f363692017-02-02 21:05:19 -0500151 mesh.fViewMatrix = viewMatrix;
Brian Salomon199fb872017-02-06 09:41:10 -0500152 mesh.fVertices = std::move(vertices);
Brian Osman8a030552017-05-23 15:03:18 -0400153 mesh.fIgnoreTexCoords = false;
154 mesh.fIgnoreColors = false;
joshualitt2771b562015-08-07 12:46:26 -0700155
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400156 if (mesh.fVertices->hasBones() && bones) {
157 // Perform the transformations on the CPU instead of the GPU.
158 mesh.fVertices = mesh.fVertices->applyBones(bones, boneCount);
159 } else {
Brian Osman37064c12019-02-08 10:53:07 -0500160 SkASSERT(!bones || boneCount == 1);
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400161 }
162
Brian Salomon199fb872017-02-06 09:41:10 -0500163 fFlags = 0;
164 if (mesh.hasPerVertexColors()) {
165 fFlags |= kRequiresPerVertexColors_Flag;
joshualitt2771b562015-08-07 12:46:26 -0700166 }
Brian Salomon199fb872017-02-06 09:41:10 -0500167 if (mesh.hasExplicitLocalCoords()) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400168 fFlags |= kAnyMeshHasExplicitLocalCoords_Flag;
joshualitt2771b562015-08-07 12:46:26 -0700169 }
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400170
171 // Special case for meshes with a world transform but no bone weights.
172 // These will be considered normal vertices draws without bones.
173 if (!mesh.fVertices->hasBones() && boneCount == 1) {
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400174 SkMatrix worldTransform;
175 worldTransform.setAffine(bones[0].values);
176 mesh.fViewMatrix.preConcat(worldTransform);
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400177 }
joshualitt2771b562015-08-07 12:46:26 -0700178
bsalomon88cf17d2016-07-08 06:40:56 -0700179 IsZeroArea zeroArea;
Chris Dalton3809bab2017-06-13 10:55:06 -0600180 if (GrIsPrimTypeLines(primitiveType) || GrPrimitiveType::kPoints == primitiveType) {
bsalomon88cf17d2016-07-08 06:40:56 -0700181 zeroArea = IsZeroArea::kYes;
182 } else {
183 zeroArea = IsZeroArea::kNo;
184 }
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400185
Brian Osman37064c12019-02-08 10:53:07 -0500186 this->setTransformedBounds(mesh.fVertices->bounds(),
187 mesh.fViewMatrix,
188 HasAABloat::kNo,
189 zeroArea);
joshualitt2771b562015-08-07 12:46:26 -0700190}
191
Brian Osman9a390ac2018-11-12 09:47:48 -0500192#ifdef SK_DEBUG
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500193SkString DrawVerticesOp::dumpInfo() const {
Brian Salomonc2f42542017-07-12 14:11:22 -0400194 SkString string;
195 string.appendf("PrimType: %d, MeshCount %d, VCount: %d, ICount: %d\n", (int)fPrimitiveType,
196 fMeshes.count(), fVertexCount, fIndexCount);
197 string += fHelper.dumpInfo();
198 string += INHERITED::dumpInfo();
199 return string;
joshualitt2771b562015-08-07 12:46:26 -0700200}
Brian Osman9a390ac2018-11-12 09:47:48 -0500201#endif
joshualitt2771b562015-08-07 12:46:26 -0700202
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500203GrDrawOp::FixedFunctionFlags DrawVerticesOp::fixedFunctionFlags() const {
Brian Salomonc2f42542017-07-12 14:11:22 -0400204 return fHelper.fixedFunctionFlags();
205}
206
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500207GrProcessorSet::Analysis DrawVerticesOp::finalize(const GrCaps& caps, const GrAppliedClip* clip) {
Brian Salomonc2f42542017-07-12 14:11:22 -0400208 GrProcessorAnalysisColor gpColor;
209 if (this->requiresPerVertexColors()) {
210 gpColor.setToUnknown();
211 } else {
212 gpColor.setToConstant(fMeshes.front().fColor);
213 }
Chris Dalton4b62aed2019-01-15 11:53:00 -0700214 auto result = fHelper.finalizeProcessors(caps, clip, GrProcessorAnalysisCoverage::kNone,
215 &gpColor);
Brian Salomonc2f42542017-07-12 14:11:22 -0400216 if (gpColor.isConstant(&fMeshes.front().fColor)) {
217 fMeshes.front().fIgnoreColors = true;
Brian Salomon199fb872017-02-06 09:41:10 -0500218 fFlags &= ~kRequiresPerVertexColors_Flag;
Brian Osmanae0c50c2017-05-25 16:56:34 -0400219 fColorArrayType = ColorArrayType::kPremulGrColor;
joshualitt2771b562015-08-07 12:46:26 -0700220 }
Brian Salomonc2f42542017-07-12 14:11:22 -0400221 if (!fHelper.usesLocalCoords()) {
Brian Osman8a030552017-05-23 15:03:18 -0400222 fMeshes[0].fIgnoreTexCoords = true;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400223 fFlags &= ~kAnyMeshHasExplicitLocalCoords_Flag;
bsalomon14eaaa62015-09-24 07:01:26 -0700224 }
Brian Salomonc2f42542017-07-12 14:11:22 -0400225 return result;
joshualitt2771b562015-08-07 12:46:26 -0700226}
227
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500228sk_sp<GrGeometryProcessor> DrawVerticesOp::makeGP(const GrShaderCaps* shaderCaps,
229 bool* hasColorAttribute,
230 bool* hasLocalCoordAttribute) const {
Brian Salomon199fb872017-02-06 09:41:10 -0500231 using namespace GrDefaultGeoProcFactory;
232 LocalCoords::Type localCoordsType;
Brian Salomonc2f42542017-07-12 14:11:22 -0400233 if (fHelper.usesLocalCoords()) {
Brian Salomon199fb872017-02-06 09:41:10 -0500234 // If we have multiple view matrices we will transform the positions into device space. We
235 // must then also provide untransformed positions as local coords.
236 if (this->anyMeshHasExplicitLocalCoords() || this->hasMultipleViewMatrices()) {
237 *hasLocalCoordAttribute = true;
238 localCoordsType = LocalCoords::kHasExplicit_Type;
239 } else {
240 *hasLocalCoordAttribute = false;
241 localCoordsType = LocalCoords::kUsePosition_Type;
242 }
243 } else {
244 localCoordsType = LocalCoords::kUnused_Type;
245 *hasLocalCoordAttribute = false;
246 }
247
248 Color color(fMeshes[0].fColor);
249 if (this->requiresPerVertexColors()) {
Brian Osman08a50e02018-06-15 15:06:48 -0400250 if (fColorArrayType == ColorArrayType::kPremulGrColor) {
251 color.fType = Color::kPremulGrColorAttribute_Type;
252 } else {
253 color.fType = Color::kUnpremulSkColorAttribute_Type;
254 color.fColorSpaceXform = fColorSpaceXform;
255 }
Brian Salomon199fb872017-02-06 09:41:10 -0500256 *hasColorAttribute = true;
257 } else {
258 *hasColorAttribute = false;
Brian Salomon23356442018-11-30 15:33:19 -0500259 }
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400260
Brian Salomon199fb872017-02-06 09:41:10 -0500261 const SkMatrix& vm = this->hasMultipleViewMatrices() ? SkMatrix::I() : fMeshes[0].fViewMatrix;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400262
Brian Osman37064c12019-02-08 10:53:07 -0500263 return GrDefaultGeoProcFactory::Make(shaderCaps,
264 color,
265 Coverage::kSolid_Type,
266 localCoordsType,
267 vm);
Brian Salomon199fb872017-02-06 09:41:10 -0500268}
269
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500270void DrawVerticesOp::onPrepareDraws(Target* target) {
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400271 bool hasMapBufferSupport = GrCaps::kNone_MapFlags != target->caps().mapBufferFlags();
272 if (fMeshes[0].fVertices->isVolatile() || !hasMapBufferSupport) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400273 this->drawVolatile(target);
274 } else {
275 this->drawNonVolatile(target);
276 }
277}
278
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500279void DrawVerticesOp::drawVolatile(Target* target) {
Brian Salomon199fb872017-02-06 09:41:10 -0500280 bool hasColorAttribute;
281 bool hasLocalCoordsAttribute;
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400282 sk_sp<GrGeometryProcessor> gp = this->makeGP(target->caps().shaderCaps(),
283 &hasColorAttribute,
Brian Osman37064c12019-02-08 10:53:07 -0500284 &hasLocalCoordsAttribute);
joshualitt2771b562015-08-07 12:46:26 -0700285
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400286 // Allocate buffers.
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500287 size_t vertexStride = gp->vertexStride();
Brian Salomondbf70722019-02-07 11:31:24 -0500288 sk_sp<const GrBuffer> vertexBuffer;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400289 int firstVertex = 0;
bsalomon14eaaa62015-09-24 07:01:26 -0700290 void* verts = target->makeVertexSpace(vertexStride, fVertexCount, &vertexBuffer, &firstVertex);
joshualitt2771b562015-08-07 12:46:26 -0700291 if (!verts) {
292 SkDebugf("Could not allocate vertices\n");
293 return;
294 }
295
Brian Salomondbf70722019-02-07 11:31:24 -0500296 sk_sp<const GrBuffer> indexBuffer;
joshualitt2771b562015-08-07 12:46:26 -0700297 int firstIndex = 0;
halcanary96fcdcc2015-08-27 07:41:13 -0700298 uint16_t* indices = nullptr;
Brian Salomon199fb872017-02-06 09:41:10 -0500299 if (this->isIndexed()) {
bsalomon14eaaa62015-09-24 07:01:26 -0700300 indices = target->makeIndexSpace(fIndexCount, &indexBuffer, &firstIndex);
joshualitt2771b562015-08-07 12:46:26 -0700301 if (!indices) {
302 SkDebugf("Could not allocate indices\n");
303 return;
304 }
305 }
306
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400307 // Fill the buffers.
308 this->fillBuffers(hasColorAttribute,
309 hasLocalCoordsAttribute,
310 vertexStride,
311 verts,
312 indices);
313
314 // Draw the vertices.
Brian Salomon12d22642019-01-29 14:38:50 -0500315 this->drawVertices(target, std::move(gp), std::move(vertexBuffer), firstVertex, indexBuffer,
316 firstIndex);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400317}
318
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500319void DrawVerticesOp::drawNonVolatile(Target* target) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400320 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
321
322 bool hasColorAttribute;
323 bool hasLocalCoordsAttribute;
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400324 sk_sp<GrGeometryProcessor> gp = this->makeGP(target->caps().shaderCaps(),
325 &hasColorAttribute,
Brian Osman37064c12019-02-08 10:53:07 -0500326 &hasLocalCoordsAttribute);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400327
328 SkASSERT(fMeshes.count() == 1); // Non-volatile meshes should never combine.
329
330 // Get the resource provider.
331 GrResourceProvider* rp = target->resourceProvider();
332
333 // Generate keys for the buffers.
334 GrUniqueKey vertexKey, indexKey;
335 GrUniqueKey::Builder vertexKeyBuilder(&vertexKey, kDomain, 2);
336 GrUniqueKey::Builder indexKeyBuilder(&indexKey, kDomain, 2);
337 vertexKeyBuilder[0] = indexKeyBuilder[0] = fMeshes[0].fVertices->uniqueID();
338 vertexKeyBuilder[1] = 0;
339 indexKeyBuilder[1] = 1;
340 vertexKeyBuilder.finish();
341 indexKeyBuilder.finish();
342
343 // Try to grab data from the cache.
Brian Salomondbf70722019-02-07 11:31:24 -0500344 sk_sp<GrGpuBuffer> vertexBuffer = rp->findByUniqueKey<GrGpuBuffer>(vertexKey);
345 sk_sp<GrGpuBuffer> indexBuffer =
346 this->isIndexed() ? rp->findByUniqueKey<GrGpuBuffer>(indexKey) : nullptr;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400347
348 // Draw using the cached buffers if possible.
349 if (vertexBuffer && (!this->isIndexed() || indexBuffer)) {
Brian Salomon12d22642019-01-29 14:38:50 -0500350 this->drawVertices(target, std::move(gp), std::move(vertexBuffer), 0,
351 std::move(indexBuffer), 0);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400352 return;
353 }
354
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400355 // Allocate vertex buffer.
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500356 size_t vertexStride = gp->vertexStride();
Brian Salomondbf70722019-02-07 11:31:24 -0500357 vertexBuffer = rp->createBuffer(
358 fVertexCount * vertexStride, GrGpuBufferType::kVertex, kStatic_GrAccessPattern);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400359 void* verts = vertexBuffer ? vertexBuffer->map() : nullptr;
360 if (!verts) {
361 SkDebugf("Could not allocate vertices\n");
362 return;
363 }
364
365 // Allocate index buffer.
366 uint16_t* indices = nullptr;
367 if (this->isIndexed()) {
Brian Salomondbf70722019-02-07 11:31:24 -0500368 indexBuffer = rp->createBuffer(
369 fIndexCount * sizeof(uint16_t), GrGpuBufferType::kIndex, kStatic_GrAccessPattern);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400370 indices = indexBuffer ? static_cast<uint16_t*>(indexBuffer->map()) : nullptr;
371 if (!indices) {
372 SkDebugf("Could not allocate indices\n");
373 return;
374 }
375 }
376
377 // Fill the buffers.
378 this->fillBuffers(hasColorAttribute,
379 hasLocalCoordsAttribute,
380 vertexStride,
381 verts,
382 indices);
383
384 // Unmap the buffers.
385 vertexBuffer->unmap();
386 if (indexBuffer) {
387 indexBuffer->unmap();
388 }
389
390 // Cache the buffers.
391 rp->assignUniqueKeyToResource(vertexKey, vertexBuffer.get());
392 rp->assignUniqueKeyToResource(indexKey, indexBuffer.get());
393
394 // Draw the vertices.
Brian Salomon12d22642019-01-29 14:38:50 -0500395 this->drawVertices(target, std::move(gp), std::move(vertexBuffer), 0, std::move(indexBuffer),
396 0);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400397}
398
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500399void DrawVerticesOp::fillBuffers(bool hasColorAttribute,
400 bool hasLocalCoordsAttribute,
401 size_t vertexStride,
402 void* verts,
403 uint16_t* indices) const {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400404 int instanceCount = fMeshes.count();
405
406 // Copy data into the buffers.
joshualitt2771b562015-08-07 12:46:26 -0700407 int vertexOffset = 0;
Brian Salomonfab30a32017-02-06 19:06:22 -0500408 // We have a fast case below for uploading the vertex data when the matrix is translate
Brian Osman37064c12019-02-08 10:53:07 -0500409 // only and there are colors but not local coords.
410 bool fastAttrs = hasColorAttribute && !hasLocalCoordsAttribute;
joshualitt2771b562015-08-07 12:46:26 -0700411 for (int i = 0; i < instanceCount; i++) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400412 // Get each mesh.
bsalomond92b4192016-06-30 07:59:23 -0700413 const Mesh& mesh = fMeshes[i];
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400414
415 // Copy data into the index buffer.
bsalomon14eaaa62015-09-24 07:01:26 -0700416 if (indices) {
Brian Salomon199fb872017-02-06 09:41:10 -0500417 int indexCount = mesh.fVertices->indexCount();
Brian Salomonfab30a32017-02-06 19:06:22 -0500418 for (int j = 0; j < indexCount; ++j) {
419 *indices++ = mesh.fVertices->indices()[j] + vertexOffset;
joshualitt2771b562015-08-07 12:46:26 -0700420 }
421 }
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400422
423 // Copy data into the vertex buffer.
Brian Salomon199fb872017-02-06 09:41:10 -0500424 int vertexCount = mesh.fVertices->vertexCount();
425 const SkPoint* positions = mesh.fVertices->positions();
426 const SkColor* colors = mesh.fVertices->colors();
427 const SkPoint* localCoords = mesh.fVertices->texCoords();
Brian Salomonfab30a32017-02-06 19:06:22 -0500428 bool fastMesh = (!this->hasMultipleViewMatrices() ||
429 mesh.fViewMatrix.getType() <= SkMatrix::kTranslate_Mask) &&
430 mesh.hasPerVertexColors();
431 if (fastAttrs && fastMesh) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400432 // Fast case.
Brian Salomonfab30a32017-02-06 19:06:22 -0500433 struct V {
434 SkPoint fPos;
435 uint32_t fColor;
436 };
437 SkASSERT(sizeof(V) == vertexStride);
438 V* v = (V*)verts;
439 Sk2f t(0, 0);
Brian Salomon199fb872017-02-06 09:41:10 -0500440 if (this->hasMultipleViewMatrices()) {
Brian Salomonfab30a32017-02-06 19:06:22 -0500441 t = Sk2f(mesh.fViewMatrix.getTranslateX(), mesh.fViewMatrix.getTranslateY());
joshualitt2771b562015-08-07 12:46:26 -0700442 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500443 for (int j = 0; j < vertexCount; ++j) {
444 Sk2f p = Sk2f::Load(positions++) + t;
445 p.store(&v[j].fPos);
446 v[j].fColor = colors[j];
447 }
448 verts = v + vertexCount;
449 } else {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400450 // Normal case.
Brian Salomonfab30a32017-02-06 19:06:22 -0500451 static constexpr size_t kColorOffset = sizeof(SkPoint);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400452 size_t offset = kColorOffset;
453 if (hasColorAttribute) {
454 offset += sizeof(uint32_t);
455 }
456 size_t localCoordOffset = offset;
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400457 if (hasLocalCoordsAttribute) {
458 offset += sizeof(SkPoint);
459 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500460
Brian Osman1be2b7c2018-10-29 16:07:15 -0400461 // TODO4F: Preserve float colors
Brian Osmancf860852018-10-31 14:04:39 -0400462 GrColor color = mesh.fColor.toBytes_RGBA();
Brian Osman1be2b7c2018-10-29 16:07:15 -0400463
Brian Salomonfab30a32017-02-06 19:06:22 -0500464 for (int j = 0; j < vertexCount; ++j) {
465 if (this->hasMultipleViewMatrices()) {
466 mesh.fViewMatrix.mapPoints(((SkPoint*)verts), &positions[j], 1);
Brian Salomon3f363692017-02-02 21:05:19 -0500467 } else {
Brian Salomonfab30a32017-02-06 19:06:22 -0500468 *((SkPoint*)verts) = positions[j];
Brian Salomon199fb872017-02-06 09:41:10 -0500469 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500470 if (hasColorAttribute) {
471 if (mesh.hasPerVertexColors()) {
472 *(uint32_t*)((intptr_t)verts + kColorOffset) = colors[j];
473 } else {
Brian Osman1be2b7c2018-10-29 16:07:15 -0400474 *(uint32_t*)((intptr_t)verts + kColorOffset) = color;
Brian Salomonfab30a32017-02-06 19:06:22 -0500475 }
Brian Salomon3f363692017-02-02 21:05:19 -0500476 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500477 if (hasLocalCoordsAttribute) {
478 if (mesh.hasExplicitLocalCoords()) {
479 *(SkPoint*)((intptr_t)verts + localCoordOffset) = localCoords[j];
480 } else {
481 *(SkPoint*)((intptr_t)verts + localCoordOffset) = positions[j];
482 }
483 }
484 verts = (void*)((intptr_t)verts + vertexStride);
joshualitt2771b562015-08-07 12:46:26 -0700485 }
joshualitt2771b562015-08-07 12:46:26 -0700486 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500487 vertexOffset += vertexCount;
joshualitt2771b562015-08-07 12:46:26 -0700488 }
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400489}
joshualitt2771b562015-08-07 12:46:26 -0700490
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500491void DrawVerticesOp::drawVertices(Target* target,
492 sk_sp<const GrGeometryProcessor> gp,
493 sk_sp<const GrBuffer> vertexBuffer,
494 int firstVertex,
495 sk_sp<const GrBuffer> indexBuffer,
496 int firstIndex) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000497 GrMesh* mesh = target->allocMesh(this->primitiveType());
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400498 if (this->isIndexed()) {
Brian Salomon12d22642019-01-29 14:38:50 -0500499 mesh->setIndexed(std::move(indexBuffer), fIndexCount, firstIndex, 0, fVertexCount - 1,
Brian Salomon7eae3e02018-08-07 14:02:38 +0000500 GrPrimitiveRestart::kNo);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400501 } else {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000502 mesh->setNonIndexedNonInstanced(fVertexCount);
joshualitt2771b562015-08-07 12:46:26 -0700503 }
Brian Salomon12d22642019-01-29 14:38:50 -0500504 mesh->setVertexData(std::move(vertexBuffer), firstVertex);
Brian Salomon49348902018-06-26 09:12:38 -0400505 auto pipe = fHelper.makePipeline(target);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000506 target->draw(std::move(gp), pipe.fPipeline, pipe.fFixedDynamicState, mesh);
joshualitt2771b562015-08-07 12:46:26 -0700507}
508
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500509GrOp::CombineResult DrawVerticesOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
510 DrawVerticesOp* that = t->cast<DrawVerticesOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700511
Brian Salomonc2f42542017-07-12 14:11:22 -0400512 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000513 return CombineResult::kCannotCombine;
joshualitt2771b562015-08-07 12:46:26 -0700514 }
515
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400516 // Non-volatile meshes cannot batch, because if a non-volatile mesh batches with another mesh,
517 // then on the next frame, if that non-volatile mesh is drawn, it will draw the other mesh
518 // that was saved in its vertex buffer, which is not necessarily there anymore.
519 if (!this->fMeshes[0].fVertices->isVolatile() || !that->fMeshes[0].fVertices->isVolatile()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000520 return CombineResult::kCannotCombine;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400521 }
522
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500523 if (!this->combinablePrimitive() || this->primitiveType() != that->primitiveType()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000524 return CombineResult::kCannotCombine;
joshualitt2771b562015-08-07 12:46:26 -0700525 }
526
Mike Reedaa9e3322017-03-16 14:38:48 -0400527 if (fMeshes[0].fVertices->hasIndices() != that->fMeshes[0].fVertices->hasIndices()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000528 return CombineResult::kCannotCombine;
joshualitt2771b562015-08-07 12:46:26 -0700529 }
530
Brian Salomon3de0aee2017-01-29 09:34:17 -0500531 if (fColorArrayType != that->fColorArrayType) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000532 return CombineResult::kCannotCombine;
Brian Salomon3de0aee2017-01-29 09:34:17 -0500533 }
534
Ben Wagner9bc36fd2018-06-15 14:23:36 -0400535 if (fVertexCount + that->fVertexCount > SkTo<int>(UINT16_MAX)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000536 return CombineResult::kCannotCombine;
Brian Salomon3f363692017-02-02 21:05:19 -0500537 }
538
Brian Osmanfa6d8652017-05-31 09:37:27 -0400539 // NOTE: For SkColor vertex colors, the source color space is always sRGB, and the destination
540 // gamut is determined by the render target context. A mis-match should be impossible.
541 SkASSERT(GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get()));
542
Brian Salomon199fb872017-02-06 09:41:10 -0500543 // If either op required explicit local coords or per-vertex colors the combined mesh does. Same
544 // with multiple view matrices.
545 fFlags |= that->fFlags;
joshualitt2771b562015-08-07 12:46:26 -0700546
Brian Salomon199fb872017-02-06 09:41:10 -0500547 if (!this->requiresPerVertexColors() && this->fMeshes[0].fColor != that->fMeshes[0].fColor) {
548 fFlags |= kRequiresPerVertexColors_Flag;
549 }
Brian Salomon3f363692017-02-02 21:05:19 -0500550 // Check whether we are about to acquire a mesh with a different view matrix.
Brian Salomon199fb872017-02-06 09:41:10 -0500551 if (!this->hasMultipleViewMatrices() &&
552 !this->fMeshes[0].fViewMatrix.cheapEqualTo(that->fMeshes[0].fViewMatrix)) {
553 fFlags |= kHasMultipleViewMatrices_Flag;
Brian Salomon3f363692017-02-02 21:05:19 -0500554 }
555
bsalomond92b4192016-06-30 07:59:23 -0700556 fMeshes.push_back_n(that->fMeshes.count(), that->fMeshes.begin());
bsalomon14eaaa62015-09-24 07:01:26 -0700557 fVertexCount += that->fVertexCount;
558 fIndexCount += that->fIndexCount;
joshualitt2771b562015-08-07 12:46:26 -0700559
Brian Salomon7eae3e02018-08-07 14:02:38 +0000560 return CombineResult::kMerged;
joshualitt2771b562015-08-07 12:46:26 -0700561}
562
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500563} // anonymous namespace
564
565std::unique_ptr<GrDrawOp> GrDrawVerticesOp::Make(GrContext* context,
566 GrPaint&& paint,
567 sk_sp<SkVertices> vertices,
568 const SkVertices::Bone bones[],
569 int boneCount,
570 const SkMatrix& viewMatrix,
571 GrAAType aaType,
572 sk_sp<GrColorSpaceXform> colorSpaceXform,
573 GrPrimitiveType* overridePrimType) {
574 SkASSERT(vertices);
575 GrPrimitiveType primType = overridePrimType ? *overridePrimType
576 : SkVertexModeToGrPrimitiveType(vertices->mode());
577 return GrSimpleMeshDrawOpHelper::FactoryHelper<DrawVerticesOp>(context, std::move(paint),
578 std::move(vertices),
579 bones, boneCount,
580 primType, aaType,
581 std::move(colorSpaceXform),
582 viewMatrix);
583}
584
joshualitt2771b562015-08-07 12:46:26 -0700585///////////////////////////////////////////////////////////////////////////////////////////////////
586
Hal Canary6f6961e2017-01-31 13:50:44 -0500587#if GR_TEST_UTILS
joshualitt2771b562015-08-07 12:46:26 -0700588
Brian Salomon5ec9def2016-12-20 15:34:05 -0500589#include "GrDrawOpTest.h"
joshualitt2771b562015-08-07 12:46:26 -0700590
591static uint32_t seed_vertices(GrPrimitiveType type) {
592 switch (type) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600593 case GrPrimitiveType::kTriangles:
594 case GrPrimitiveType::kTriangleStrip:
joshualitt2771b562015-08-07 12:46:26 -0700595 return 3;
Chris Dalton3809bab2017-06-13 10:55:06 -0600596 case GrPrimitiveType::kPoints:
joshualitt2771b562015-08-07 12:46:26 -0700597 return 1;
Chris Dalton3809bab2017-06-13 10:55:06 -0600598 case GrPrimitiveType::kLines:
599 case GrPrimitiveType::kLineStrip:
joshualitt2771b562015-08-07 12:46:26 -0700600 return 2;
Chris Dalton3809bab2017-06-13 10:55:06 -0600601 case GrPrimitiveType::kLinesAdjacency:
602 return 4;
joshualitt2771b562015-08-07 12:46:26 -0700603 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400604 SK_ABORT("Incomplete switch\n");
joshualitt2771b562015-08-07 12:46:26 -0700605 return 0;
606}
607
608static uint32_t primitive_vertices(GrPrimitiveType type) {
609 switch (type) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600610 case GrPrimitiveType::kTriangles:
joshualitt2771b562015-08-07 12:46:26 -0700611 return 3;
Chris Dalton3809bab2017-06-13 10:55:06 -0600612 case GrPrimitiveType::kLines:
joshualitt2771b562015-08-07 12:46:26 -0700613 return 2;
Chris Dalton3809bab2017-06-13 10:55:06 -0600614 case GrPrimitiveType::kTriangleStrip:
Chris Dalton3809bab2017-06-13 10:55:06 -0600615 case GrPrimitiveType::kPoints:
616 case GrPrimitiveType::kLineStrip:
joshualitt2771b562015-08-07 12:46:26 -0700617 return 1;
Chris Dalton3809bab2017-06-13 10:55:06 -0600618 case GrPrimitiveType::kLinesAdjacency:
619 return 4;
joshualitt2771b562015-08-07 12:46:26 -0700620 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400621 SK_ABORT("Incomplete switch\n");
joshualitt2771b562015-08-07 12:46:26 -0700622 return 0;
623}
624
625static SkPoint random_point(SkRandom* random, SkScalar min, SkScalar max) {
626 SkPoint p;
627 p.fX = random->nextRangeScalar(min, max);
628 p.fY = random->nextRangeScalar(min, max);
629 return p;
630}
631
632static void randomize_params(size_t count, size_t maxVertex, SkScalar min, SkScalar max,
Brian Salomonfc527d22016-12-14 21:07:01 -0500633 SkRandom* random, SkTArray<SkPoint>* positions,
joshualitt2771b562015-08-07 12:46:26 -0700634 SkTArray<SkPoint>* texCoords, bool hasTexCoords,
Brian Salomon3de0aee2017-01-29 09:34:17 -0500635 SkTArray<uint32_t>* colors, bool hasColors,
636 SkTArray<uint16_t>* indices, bool hasIndices) {
joshualitt2771b562015-08-07 12:46:26 -0700637 for (uint32_t v = 0; v < count; v++) {
638 positions->push_back(random_point(random, min, max));
639 if (hasTexCoords) {
640 texCoords->push_back(random_point(random, min, max));
641 }
642 if (hasColors) {
643 colors->push_back(GrRandomColor(random));
644 }
645 if (hasIndices) {
Ben Wagnerb0897652018-06-15 15:37:57 +0000646 SkASSERT(maxVertex <= UINT16_MAX);
joshualitt2771b562015-08-07 12:46:26 -0700647 indices->push_back(random->nextULessThan((uint16_t)maxVertex));
648 }
649 }
650}
651
Robert Phillipsb6e9d3c2019-02-11 14:29:34 -0500652GR_DRAW_OP_TEST_DEFINE(DrawVerticesOp) {
Chris Daltonb894c2b2017-06-14 12:39:19 -0600653 GrPrimitiveType type;
654 do {
655 type = GrPrimitiveType(random->nextULessThan(kNumGrPrimitiveTypes));
656 } while (GrPrimTypeRequiresGeometryShaderSupport(type) &&
Robert Phillips9da87e02019-02-04 13:26:26 -0500657 !context->priv().caps()->shaderCaps()->geometryShaderSupport());
Chris Daltonb894c2b2017-06-14 12:39:19 -0600658
joshualitt2771b562015-08-07 12:46:26 -0700659 uint32_t primitiveCount = random->nextRangeU(1, 100);
660
661 // TODO make 'sensible' indexbuffers
662 SkTArray<SkPoint> positions;
663 SkTArray<SkPoint> texCoords;
Brian Salomon3de0aee2017-01-29 09:34:17 -0500664 SkTArray<uint32_t> colors;
joshualitt2771b562015-08-07 12:46:26 -0700665 SkTArray<uint16_t> indices;
666
667 bool hasTexCoords = random->nextBool();
668 bool hasIndices = random->nextBool();
669 bool hasColors = random->nextBool();
670
671 uint32_t vertexCount = seed_vertices(type) + (primitiveCount - 1) * primitive_vertices(type);
672
673 static const SkScalar kMinVertExtent = -100.f;
674 static const SkScalar kMaxVertExtent = 100.f;
Brian Salomonfc527d22016-12-14 21:07:01 -0500675 randomize_params(seed_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent, random,
676 &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices,
677 hasIndices);
joshualitt2771b562015-08-07 12:46:26 -0700678
679 for (uint32_t i = 1; i < primitiveCount; i++) {
680 randomize_params(primitive_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent,
Brian Salomonfc527d22016-12-14 21:07:01 -0500681 random, &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices,
682 hasIndices);
joshualitt2771b562015-08-07 12:46:26 -0700683 }
684
685 SkMatrix viewMatrix = GrTest::TestMatrix(random);
joshualitt2771b562015-08-07 12:46:26 -0700686
Brian Osmanfa6d8652017-05-31 09:37:27 -0400687 sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(random);
Brian Osmanae0c50c2017-05-25 16:56:34 -0400688
689 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
690 sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, vertexCount, positions.begin(),
691 texCoords.begin(), colors.begin(),
692 hasIndices ? indices.count() : 0,
693 indices.begin());
Brian Salomonc2f42542017-07-12 14:11:22 -0400694 GrAAType aaType = GrAAType::kNone;
695 if (GrFSAAType::kUnifiedMSAA == fsaaType && random->nextBool()) {
696 aaType = GrAAType::kMSAA;
697 }
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400698 return GrDrawVerticesOp::Make(context, std::move(paint), std::move(vertices), nullptr, 0,
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400699 viewMatrix, aaType, std::move(colorSpaceXform), &type);
joshualitt2771b562015-08-07 12:46:26 -0700700}
701
702#endif