blob: 10ff9da9d4b9053d46033c10a2a0c143b25d5d83 [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"
Brian Osman3b655982017-03-07 16:58:08 -050012#include "SkGr.h"
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -040013#include "SkRectPriv.h"
joshualitt2771b562015-08-07 12:46:26 -070014
Robert Phillips7c525e62018-06-12 10:11:12 -040015std::unique_ptr<GrDrawOp> GrDrawVerticesOp::Make(GrContext* context,
16 GrPaint&& paint,
Brian Salomonc2f42542017-07-12 14:11:22 -040017 sk_sp<SkVertices> vertices,
Ruiqi Maoc97a3392018-08-15 10:44:19 -040018 const SkVertices::Bone bones[],
Ruiqi Mao4ec72f72018-07-10 17:21:07 -040019 int boneCount,
Brian Salomonc2f42542017-07-12 14:11:22 -040020 const SkMatrix& viewMatrix,
21 GrAAType aaType,
Brian Salomonc2f42542017-07-12 14:11:22 -040022 sk_sp<GrColorSpaceXform> colorSpaceXform,
23 GrPrimitiveType* overridePrimType) {
Brian Salomon199fb872017-02-06 09:41:10 -050024 SkASSERT(vertices);
Brian Osmanae0c50c2017-05-25 16:56:34 -040025 GrPrimitiveType primType = overridePrimType ? *overridePrimType
26 : SkVertexModeToGrPrimitiveType(vertices->mode());
Robert Phillips7c525e62018-06-12 10:11:12 -040027 return Helper::FactoryHelper<GrDrawVerticesOp>(context, std::move(paint), std::move(vertices),
Ruiqi Mao4ec72f72018-07-10 17:21:07 -040028 bones, boneCount, primType, aaType,
29 std::move(colorSpaceXform), viewMatrix);
Brian Salomon199fb872017-02-06 09:41:10 -050030}
31
Brian Osman1be2b7c2018-10-29 16:07:15 -040032GrDrawVerticesOp::GrDrawVerticesOp(const Helper::MakeArgs& helperArgs, GrColor4h color,
Ruiqi Maoc97a3392018-08-15 10:44:19 -040033 sk_sp<SkVertices> vertices, const SkVertices::Bone bones[],
Ruiqi Mao4ec72f72018-07-10 17:21:07 -040034 int boneCount, GrPrimitiveType primitiveType, GrAAType aaType,
35 sk_sp<GrColorSpaceXform> colorSpaceXform,
Brian Osmanfa6d8652017-05-31 09:37:27 -040036 const SkMatrix& viewMatrix)
37 : INHERITED(ClassID())
Brian Salomonc2f42542017-07-12 14:11:22 -040038 , fHelper(helperArgs, aaType)
Brian Osmanfa6d8652017-05-31 09:37:27 -040039 , fPrimitiveType(primitiveType)
40 , fColorSpaceXform(std::move(colorSpaceXform)) {
Brian Salomon199fb872017-02-06 09:41:10 -050041 SkASSERT(vertices);
42
43 fVertexCount = vertices->vertexCount();
44 fIndexCount = vertices->indexCount();
Brian Osmanae0c50c2017-05-25 16:56:34 -040045 fColorArrayType = vertices->hasColors() ? ColorArrayType::kSkColor
46 : ColorArrayType::kPremulGrColor;
joshualitt2771b562015-08-07 12:46:26 -070047
bsalomond92b4192016-06-30 07:59:23 -070048 Mesh& mesh = fMeshes.push_back();
49 mesh.fColor = color;
Brian Salomon3f363692017-02-02 21:05:19 -050050 mesh.fViewMatrix = viewMatrix;
Brian Salomon199fb872017-02-06 09:41:10 -050051 mesh.fVertices = std::move(vertices);
Brian Osman8a030552017-05-23 15:03:18 -040052 mesh.fIgnoreTexCoords = false;
53 mesh.fIgnoreColors = false;
Ruiqi Mao4ec72f72018-07-10 17:21:07 -040054 mesh.fIgnoreBones = false;
joshualitt2771b562015-08-07 12:46:26 -070055
Ruiqi Maoc97a3392018-08-15 10:44:19 -040056 if (mesh.fVertices->hasBones() && bones) {
57 // Perform the transformations on the CPU instead of the GPU.
58 mesh.fVertices = mesh.fVertices->applyBones(bones, boneCount);
59 } else {
60 if (bones && boneCount > 1) {
61 // NOTE: This should never be used. All bone transforms are being done on the CPU
62 // instead of the GPU.
63
64 // Copy the bone data.
65 fBones.assign(bones, bones + boneCount);
66 }
67 }
68
Brian Salomon199fb872017-02-06 09:41:10 -050069 fFlags = 0;
70 if (mesh.hasPerVertexColors()) {
71 fFlags |= kRequiresPerVertexColors_Flag;
joshualitt2771b562015-08-07 12:46:26 -070072 }
Brian Salomon199fb872017-02-06 09:41:10 -050073 if (mesh.hasExplicitLocalCoords()) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -040074 fFlags |= kAnyMeshHasExplicitLocalCoords_Flag;
joshualitt2771b562015-08-07 12:46:26 -070075 }
Ruiqi Mao4ec72f72018-07-10 17:21:07 -040076 if (mesh.hasBones()) {
77 fFlags |= kHasBones_Flag;
78 }
79
80 // Special case for meshes with a world transform but no bone weights.
81 // These will be considered normal vertices draws without bones.
82 if (!mesh.fVertices->hasBones() && boneCount == 1) {
Ruiqi Maoc97a3392018-08-15 10:44:19 -040083 SkMatrix worldTransform;
84 worldTransform.setAffine(bones[0].values);
85 mesh.fViewMatrix.preConcat(worldTransform);
Ruiqi Mao4ec72f72018-07-10 17:21:07 -040086 }
joshualitt2771b562015-08-07 12:46:26 -070087
bsalomon88cf17d2016-07-08 06:40:56 -070088 IsZeroArea zeroArea;
Chris Dalton3809bab2017-06-13 10:55:06 -060089 if (GrIsPrimTypeLines(primitiveType) || GrPrimitiveType::kPoints == primitiveType) {
bsalomon88cf17d2016-07-08 06:40:56 -070090 zeroArea = IsZeroArea::kYes;
91 } else {
92 zeroArea = IsZeroArea::kNo;
93 }
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -040094
Ruiqi Mao4ec72f72018-07-10 17:21:07 -040095 if (this->hasBones()) {
96 // We don't know the bounds if there are deformations involved, so attempt to calculate
97 // the maximum possible.
Robert Phillipsdc97fdc2018-07-18 08:27:19 -040098 SkRect bounds = SkRect::MakeEmpty();
Ruiqi Mao4ec72f72018-07-10 17:21:07 -040099 const SkRect originalBounds = bones[0].mapRect(mesh.fVertices->bounds());
100 for (int i = 1; i < boneCount; i++) {
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400101 const SkVertices::Bone& matrix = bones[i];
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400102 bounds.join(matrix.mapRect(originalBounds));
103 }
104
105 this->setTransformedBounds(bounds,
106 mesh.fViewMatrix,
107 HasAABloat::kNo,
108 zeroArea);
109 } else {
110 this->setTransformedBounds(mesh.fVertices->bounds(),
111 mesh.fViewMatrix,
112 HasAABloat::kNo,
113 zeroArea);
114 }
joshualitt2771b562015-08-07 12:46:26 -0700115}
116
Brian Salomonc2f42542017-07-12 14:11:22 -0400117SkString GrDrawVerticesOp::dumpInfo() const {
118 SkString string;
119 string.appendf("PrimType: %d, MeshCount %d, VCount: %d, ICount: %d\n", (int)fPrimitiveType,
120 fMeshes.count(), fVertexCount, fIndexCount);
121 string += fHelper.dumpInfo();
122 string += INHERITED::dumpInfo();
123 return string;
joshualitt2771b562015-08-07 12:46:26 -0700124}
125
Brian Salomonc2f42542017-07-12 14:11:22 -0400126GrDrawOp::FixedFunctionFlags GrDrawVerticesOp::fixedFunctionFlags() const {
127 return fHelper.fixedFunctionFlags();
128}
129
130GrDrawOp::RequiresDstTexture GrDrawVerticesOp::finalize(const GrCaps& caps,
Brian Osman532b3f92018-07-11 10:02:07 -0400131 const GrAppliedClip* clip) {
Brian Salomonc2f42542017-07-12 14:11:22 -0400132 GrProcessorAnalysisColor gpColor;
133 if (this->requiresPerVertexColors()) {
134 gpColor.setToUnknown();
135 } else {
136 gpColor.setToConstant(fMeshes.front().fColor);
137 }
Brian Osman532b3f92018-07-11 10:02:07 -0400138 auto result = fHelper.xpRequiresDstTexture(caps, clip, GrProcessorAnalysisCoverage::kNone,
139 &gpColor);
Brian Salomonc2f42542017-07-12 14:11:22 -0400140 if (gpColor.isConstant(&fMeshes.front().fColor)) {
141 fMeshes.front().fIgnoreColors = true;
Brian Salomon199fb872017-02-06 09:41:10 -0500142 fFlags &= ~kRequiresPerVertexColors_Flag;
Brian Osmanae0c50c2017-05-25 16:56:34 -0400143 fColorArrayType = ColorArrayType::kPremulGrColor;
joshualitt2771b562015-08-07 12:46:26 -0700144 }
Brian Salomonc2f42542017-07-12 14:11:22 -0400145 if (!fHelper.usesLocalCoords()) {
Brian Osman8a030552017-05-23 15:03:18 -0400146 fMeshes[0].fIgnoreTexCoords = true;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400147 fFlags &= ~kAnyMeshHasExplicitLocalCoords_Flag;
bsalomon14eaaa62015-09-24 07:01:26 -0700148 }
Brian Salomonc2f42542017-07-12 14:11:22 -0400149 return result;
joshualitt2771b562015-08-07 12:46:26 -0700150}
151
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400152sk_sp<GrGeometryProcessor> GrDrawVerticesOp::makeGP(const GrShaderCaps* shaderCaps,
153 bool* hasColorAttribute,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400154 bool* hasLocalCoordAttribute,
155 bool* hasBoneAttribute) const {
Brian Salomon199fb872017-02-06 09:41:10 -0500156 using namespace GrDefaultGeoProcFactory;
157 LocalCoords::Type localCoordsType;
Brian Salomonc2f42542017-07-12 14:11:22 -0400158 if (fHelper.usesLocalCoords()) {
Brian Salomon199fb872017-02-06 09:41:10 -0500159 // If we have multiple view matrices we will transform the positions into device space. We
160 // must then also provide untransformed positions as local coords.
161 if (this->anyMeshHasExplicitLocalCoords() || this->hasMultipleViewMatrices()) {
162 *hasLocalCoordAttribute = true;
163 localCoordsType = LocalCoords::kHasExplicit_Type;
164 } else {
165 *hasLocalCoordAttribute = false;
166 localCoordsType = LocalCoords::kUsePosition_Type;
167 }
168 } else {
169 localCoordsType = LocalCoords::kUnused_Type;
170 *hasLocalCoordAttribute = false;
171 }
172
173 Color color(fMeshes[0].fColor);
174 if (this->requiresPerVertexColors()) {
Brian Osman08a50e02018-06-15 15:06:48 -0400175 if (fColorArrayType == ColorArrayType::kPremulGrColor) {
176 color.fType = Color::kPremulGrColorAttribute_Type;
177 } else {
178 color.fType = Color::kUnpremulSkColorAttribute_Type;
179 color.fColorSpaceXform = fColorSpaceXform;
180 }
Brian Salomon199fb872017-02-06 09:41:10 -0500181 *hasColorAttribute = true;
182 } else {
183 *hasColorAttribute = false;
184 };
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400185
Brian Salomon199fb872017-02-06 09:41:10 -0500186 const SkMatrix& vm = this->hasMultipleViewMatrices() ? SkMatrix::I() : fMeshes[0].fViewMatrix;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400187
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400188 // The bones are packed as 6 floats in column major order, so we can directly upload them to
189 // the GPU as groups of 3 vec2s.
190 Bones bones(reinterpret_cast<const float*>(fBones.data()), fBones.size());
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400191 *hasBoneAttribute = this->hasBones();
192
193 if (this->hasBones()) {
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400194 return GrDefaultGeoProcFactory::MakeWithBones(shaderCaps,
195 color,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400196 Coverage::kSolid_Type,
197 localCoordsType,
198 bones,
199 vm);
200 } else {
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400201 return GrDefaultGeoProcFactory::Make(shaderCaps,
202 color,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400203 Coverage::kSolid_Type,
204 localCoordsType,
205 vm);
206 }
Brian Salomon199fb872017-02-06 09:41:10 -0500207}
208
Brian Salomon91326c32017-08-09 16:02:19 -0400209void GrDrawVerticesOp::onPrepareDraws(Target* target) {
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400210 bool hasMapBufferSupport = GrCaps::kNone_MapFlags != target->caps().mapBufferFlags();
211 if (fMeshes[0].fVertices->isVolatile() || !hasMapBufferSupport) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400212 this->drawVolatile(target);
213 } else {
214 this->drawNonVolatile(target);
215 }
216}
217
218void GrDrawVerticesOp::drawVolatile(Target* target) {
Brian Salomon199fb872017-02-06 09:41:10 -0500219 bool hasColorAttribute;
220 bool hasLocalCoordsAttribute;
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400221 bool hasBoneAttribute;
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400222 sk_sp<GrGeometryProcessor> gp = this->makeGP(target->caps().shaderCaps(),
223 &hasColorAttribute,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400224 &hasLocalCoordsAttribute,
225 &hasBoneAttribute);
joshualitt2771b562015-08-07 12:46:26 -0700226
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400227 // Calculate the stride.
228 size_t vertexStride = sizeof(SkPoint) +
229 (hasColorAttribute ? sizeof(uint32_t) : 0) +
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400230 (hasLocalCoordsAttribute ? sizeof(SkPoint) : 0) +
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400231 (hasBoneAttribute ? 4 * (sizeof(int8_t) + sizeof(uint8_t)) : 0);
Brian Salomon92be2f72018-06-19 14:33:47 -0400232 SkASSERT(vertexStride == gp->debugOnly_vertexStride());
joshualitt2771b562015-08-07 12:46:26 -0700233
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400234 // Allocate buffers.
235 const GrBuffer* vertexBuffer = nullptr;
236 int firstVertex = 0;
bsalomon14eaaa62015-09-24 07:01:26 -0700237 void* verts = target->makeVertexSpace(vertexStride, fVertexCount, &vertexBuffer, &firstVertex);
joshualitt2771b562015-08-07 12:46:26 -0700238 if (!verts) {
239 SkDebugf("Could not allocate vertices\n");
240 return;
241 }
242
cdalton397536c2016-03-25 12:15:03 -0700243 const GrBuffer* indexBuffer = nullptr;
joshualitt2771b562015-08-07 12:46:26 -0700244 int firstIndex = 0;
halcanary96fcdcc2015-08-27 07:41:13 -0700245 uint16_t* indices = nullptr;
Brian Salomon199fb872017-02-06 09:41:10 -0500246 if (this->isIndexed()) {
bsalomon14eaaa62015-09-24 07:01:26 -0700247 indices = target->makeIndexSpace(fIndexCount, &indexBuffer, &firstIndex);
joshualitt2771b562015-08-07 12:46:26 -0700248 if (!indices) {
249 SkDebugf("Could not allocate indices\n");
250 return;
251 }
252 }
253
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400254 // Fill the buffers.
255 this->fillBuffers(hasColorAttribute,
256 hasLocalCoordsAttribute,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400257 hasBoneAttribute,
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400258 vertexStride,
259 verts,
260 indices);
261
262 // Draw the vertices.
Brian Salomon7eae3e02018-08-07 14:02:38 +0000263 this->drawVertices(target, std::move(gp), vertexBuffer, firstVertex, indexBuffer, firstIndex);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400264}
265
266void GrDrawVerticesOp::drawNonVolatile(Target* target) {
267 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
268
269 bool hasColorAttribute;
270 bool hasLocalCoordsAttribute;
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400271 bool hasBoneAttribute;
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400272 sk_sp<GrGeometryProcessor> gp = this->makeGP(target->caps().shaderCaps(),
273 &hasColorAttribute,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400274 &hasLocalCoordsAttribute,
275 &hasBoneAttribute);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400276
277 SkASSERT(fMeshes.count() == 1); // Non-volatile meshes should never combine.
278
279 // Get the resource provider.
280 GrResourceProvider* rp = target->resourceProvider();
281
282 // Generate keys for the buffers.
283 GrUniqueKey vertexKey, indexKey;
284 GrUniqueKey::Builder vertexKeyBuilder(&vertexKey, kDomain, 2);
285 GrUniqueKey::Builder indexKeyBuilder(&indexKey, kDomain, 2);
286 vertexKeyBuilder[0] = indexKeyBuilder[0] = fMeshes[0].fVertices->uniqueID();
287 vertexKeyBuilder[1] = 0;
288 indexKeyBuilder[1] = 1;
289 vertexKeyBuilder.finish();
290 indexKeyBuilder.finish();
291
292 // Try to grab data from the cache.
293 sk_sp<GrBuffer> vertexBuffer = rp->findByUniqueKey<GrBuffer>(vertexKey);
294 sk_sp<GrBuffer> indexBuffer = this->isIndexed() ?
295 rp->findByUniqueKey<GrBuffer>(indexKey) :
296 nullptr;
297
298 // Draw using the cached buffers if possible.
299 if (vertexBuffer && (!this->isIndexed() || indexBuffer)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000300 this->drawVertices(target, std::move(gp), vertexBuffer.get(), 0, indexBuffer.get(), 0);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400301 return;
302 }
303
304 // Calculate the stride.
305 size_t vertexStride = sizeof(SkPoint) +
306 (hasColorAttribute ? sizeof(uint32_t) : 0) +
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400307 (hasLocalCoordsAttribute ? sizeof(SkPoint) : 0) +
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400308 (hasBoneAttribute ? 4 * (sizeof(int8_t) + sizeof(uint8_t)) : 0);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400309 SkASSERT(vertexStride == gp->debugOnly_vertexStride());
310
311 // Allocate vertex buffer.
312 vertexBuffer.reset(rp->createBuffer(fVertexCount * vertexStride,
313 kVertex_GrBufferType,
314 kStatic_GrAccessPattern,
Chris Daltond004e0b2018-09-27 09:28:03 -0600315 GrResourceProvider::Flags::kNone));
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400316 void* verts = vertexBuffer ? vertexBuffer->map() : nullptr;
317 if (!verts) {
318 SkDebugf("Could not allocate vertices\n");
319 return;
320 }
321
322 // Allocate index buffer.
323 uint16_t* indices = nullptr;
324 if (this->isIndexed()) {
325 indexBuffer.reset(rp->createBuffer(fIndexCount * sizeof(uint16_t),
326 kIndex_GrBufferType,
327 kStatic_GrAccessPattern,
Chris Daltond004e0b2018-09-27 09:28:03 -0600328 GrResourceProvider::Flags::kNone));
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400329 indices = indexBuffer ? static_cast<uint16_t*>(indexBuffer->map()) : nullptr;
330 if (!indices) {
331 SkDebugf("Could not allocate indices\n");
332 return;
333 }
334 }
335
336 // Fill the buffers.
337 this->fillBuffers(hasColorAttribute,
338 hasLocalCoordsAttribute,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400339 hasBoneAttribute,
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400340 vertexStride,
341 verts,
342 indices);
343
344 // Unmap the buffers.
345 vertexBuffer->unmap();
346 if (indexBuffer) {
347 indexBuffer->unmap();
348 }
349
350 // Cache the buffers.
351 rp->assignUniqueKeyToResource(vertexKey, vertexBuffer.get());
352 rp->assignUniqueKeyToResource(indexKey, indexBuffer.get());
353
354 // Draw the vertices.
Brian Salomon7eae3e02018-08-07 14:02:38 +0000355 this->drawVertices(target, std::move(gp), vertexBuffer.get(), 0, indexBuffer.get(), 0);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400356}
357
358void GrDrawVerticesOp::fillBuffers(bool hasColorAttribute,
359 bool hasLocalCoordsAttribute,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400360 bool hasBoneAttribute,
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400361 size_t vertexStride,
362 void* verts,
363 uint16_t* indices) const {
364 int instanceCount = fMeshes.count();
365
366 // Copy data into the buffers.
joshualitt2771b562015-08-07 12:46:26 -0700367 int vertexOffset = 0;
Brian Salomonfab30a32017-02-06 19:06:22 -0500368 // We have a fast case below for uploading the vertex data when the matrix is translate
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400369 // only and there are colors but not local coords. Fast case does not apply when there are bone
370 // transformations.
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400371 bool fastAttrs = hasColorAttribute && !hasLocalCoordsAttribute && !hasBoneAttribute;
joshualitt2771b562015-08-07 12:46:26 -0700372 for (int i = 0; i < instanceCount; i++) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400373 // Get each mesh.
bsalomond92b4192016-06-30 07:59:23 -0700374 const Mesh& mesh = fMeshes[i];
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400375
376 // Copy data into the index buffer.
bsalomon14eaaa62015-09-24 07:01:26 -0700377 if (indices) {
Brian Salomon199fb872017-02-06 09:41:10 -0500378 int indexCount = mesh.fVertices->indexCount();
Brian Salomonfab30a32017-02-06 19:06:22 -0500379 for (int j = 0; j < indexCount; ++j) {
380 *indices++ = mesh.fVertices->indices()[j] + vertexOffset;
joshualitt2771b562015-08-07 12:46:26 -0700381 }
382 }
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400383
384 // Copy data into the vertex buffer.
Brian Salomon199fb872017-02-06 09:41:10 -0500385 int vertexCount = mesh.fVertices->vertexCount();
386 const SkPoint* positions = mesh.fVertices->positions();
387 const SkColor* colors = mesh.fVertices->colors();
388 const SkPoint* localCoords = mesh.fVertices->texCoords();
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400389 const SkVertices::BoneIndices* boneIndices = mesh.fVertices->boneIndices();
390 const SkVertices::BoneWeights* boneWeights = mesh.fVertices->boneWeights();
Brian Salomonfab30a32017-02-06 19:06:22 -0500391 bool fastMesh = (!this->hasMultipleViewMatrices() ||
392 mesh.fViewMatrix.getType() <= SkMatrix::kTranslate_Mask) &&
393 mesh.hasPerVertexColors();
394 if (fastAttrs && fastMesh) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400395 // Fast case.
Brian Salomonfab30a32017-02-06 19:06:22 -0500396 struct V {
397 SkPoint fPos;
398 uint32_t fColor;
399 };
400 SkASSERT(sizeof(V) == vertexStride);
401 V* v = (V*)verts;
402 Sk2f t(0, 0);
Brian Salomon199fb872017-02-06 09:41:10 -0500403 if (this->hasMultipleViewMatrices()) {
Brian Salomonfab30a32017-02-06 19:06:22 -0500404 t = Sk2f(mesh.fViewMatrix.getTranslateX(), mesh.fViewMatrix.getTranslateY());
joshualitt2771b562015-08-07 12:46:26 -0700405 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500406 for (int j = 0; j < vertexCount; ++j) {
407 Sk2f p = Sk2f::Load(positions++) + t;
408 p.store(&v[j].fPos);
409 v[j].fColor = colors[j];
410 }
411 verts = v + vertexCount;
412 } else {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400413 // Normal case.
Brian Salomonfab30a32017-02-06 19:06:22 -0500414 static constexpr size_t kColorOffset = sizeof(SkPoint);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400415 size_t offset = kColorOffset;
416 if (hasColorAttribute) {
417 offset += sizeof(uint32_t);
418 }
419 size_t localCoordOffset = offset;
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400420 if (hasLocalCoordsAttribute) {
421 offset += sizeof(SkPoint);
422 }
423 size_t boneIndexOffset = offset;
424 if (hasBoneAttribute) {
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400425 offset += 4 * sizeof(int8_t);
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400426 }
427 size_t boneWeightOffset = offset;
Brian Salomonfab30a32017-02-06 19:06:22 -0500428
Brian Osman1be2b7c2018-10-29 16:07:15 -0400429 // TODO4F: Preserve float colors
430 GrColor color = mesh.fColor.toGrColor();
431
Brian Salomonfab30a32017-02-06 19:06:22 -0500432 for (int j = 0; j < vertexCount; ++j) {
433 if (this->hasMultipleViewMatrices()) {
434 mesh.fViewMatrix.mapPoints(((SkPoint*)verts), &positions[j], 1);
Brian Salomon3f363692017-02-02 21:05:19 -0500435 } else {
Brian Salomonfab30a32017-02-06 19:06:22 -0500436 *((SkPoint*)verts) = positions[j];
Brian Salomon199fb872017-02-06 09:41:10 -0500437 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500438 if (hasColorAttribute) {
439 if (mesh.hasPerVertexColors()) {
440 *(uint32_t*)((intptr_t)verts + kColorOffset) = colors[j];
441 } else {
Brian Osman1be2b7c2018-10-29 16:07:15 -0400442 *(uint32_t*)((intptr_t)verts + kColorOffset) = color;
Brian Salomonfab30a32017-02-06 19:06:22 -0500443 }
Brian Salomon3f363692017-02-02 21:05:19 -0500444 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500445 if (hasLocalCoordsAttribute) {
446 if (mesh.hasExplicitLocalCoords()) {
447 *(SkPoint*)((intptr_t)verts + localCoordOffset) = localCoords[j];
448 } else {
449 *(SkPoint*)((intptr_t)verts + localCoordOffset) = positions[j];
450 }
451 }
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400452 if (hasBoneAttribute) {
453 const SkVertices::BoneIndices& indices = boneIndices[j];
454 const SkVertices::BoneWeights& weights = boneWeights[j];
455 for (int k = 0; k < 4; k++) {
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400456 size_t indexOffset = boneIndexOffset + sizeof(int8_t) * k;
457 size_t weightOffset = boneWeightOffset + sizeof(uint8_t) * k;
458 *(int8_t*)((intptr_t)verts + indexOffset) = indices.indices[k];
459 *(uint8_t*)((intptr_t)verts + weightOffset) = weights.weights[k] * 255.0f;
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400460 }
461 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500462 verts = (void*)((intptr_t)verts + vertexStride);
joshualitt2771b562015-08-07 12:46:26 -0700463 }
joshualitt2771b562015-08-07 12:46:26 -0700464 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500465 vertexOffset += vertexCount;
joshualitt2771b562015-08-07 12:46:26 -0700466 }
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400467}
joshualitt2771b562015-08-07 12:46:26 -0700468
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400469void GrDrawVerticesOp::drawVertices(Target* target,
Brian Salomon7eae3e02018-08-07 14:02:38 +0000470 sk_sp<const GrGeometryProcessor> gp,
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400471 const GrBuffer* vertexBuffer,
472 int firstVertex,
473 const GrBuffer* indexBuffer,
474 int firstIndex) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000475 GrMesh* mesh = target->allocMesh(this->primitiveType());
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400476 if (this->isIndexed()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000477 mesh->setIndexed(indexBuffer, fIndexCount, firstIndex, 0, fVertexCount - 1,
478 GrPrimitiveRestart::kNo);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400479 } else {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000480 mesh->setNonIndexedNonInstanced(fVertexCount);
joshualitt2771b562015-08-07 12:46:26 -0700481 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000482 mesh->setVertexData(vertexBuffer, firstVertex);
Brian Salomon49348902018-06-26 09:12:38 -0400483 auto pipe = fHelper.makePipeline(target);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000484 target->draw(std::move(gp), pipe.fPipeline, pipe.fFixedDynamicState, mesh);
joshualitt2771b562015-08-07 12:46:26 -0700485}
486
Brian Salomon7eae3e02018-08-07 14:02:38 +0000487GrOp::CombineResult GrDrawVerticesOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
Brian Salomonfc527d22016-12-14 21:07:01 -0500488 GrDrawVerticesOp* that = t->cast<GrDrawVerticesOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700489
Brian Salomonc2f42542017-07-12 14:11:22 -0400490 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000491 return CombineResult::kCannotCombine;
joshualitt2771b562015-08-07 12:46:26 -0700492 }
493
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400494 // Meshes with bones cannot be combined because different meshes use different bones, so to
495 // combine them, the matrices would have to be combined, and the bone indices on each vertex
496 // would change, thus making the vertices uncacheable.
497 if (this->hasBones() || that->hasBones()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000498 return CombineResult::kCannotCombine;
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400499 }
500
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400501 // Non-volatile meshes cannot batch, because if a non-volatile mesh batches with another mesh,
502 // then on the next frame, if that non-volatile mesh is drawn, it will draw the other mesh
503 // that was saved in its vertex buffer, which is not necessarily there anymore.
504 if (!this->fMeshes[0].fVertices->isVolatile() || !that->fMeshes[0].fVertices->isVolatile()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000505 return CombineResult::kCannotCombine;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400506 }
507
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500508 if (!this->combinablePrimitive() || this->primitiveType() != that->primitiveType()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000509 return CombineResult::kCannotCombine;
joshualitt2771b562015-08-07 12:46:26 -0700510 }
511
Mike Reedaa9e3322017-03-16 14:38:48 -0400512 if (fMeshes[0].fVertices->hasIndices() != that->fMeshes[0].fVertices->hasIndices()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000513 return CombineResult::kCannotCombine;
joshualitt2771b562015-08-07 12:46:26 -0700514 }
515
Brian Salomon3de0aee2017-01-29 09:34:17 -0500516 if (fColorArrayType != that->fColorArrayType) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000517 return CombineResult::kCannotCombine;
Brian Salomon3de0aee2017-01-29 09:34:17 -0500518 }
519
Ben Wagner9bc36fd2018-06-15 14:23:36 -0400520 if (fVertexCount + that->fVertexCount > SkTo<int>(UINT16_MAX)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000521 return CombineResult::kCannotCombine;
Brian Salomon3f363692017-02-02 21:05:19 -0500522 }
523
Brian Osmanfa6d8652017-05-31 09:37:27 -0400524 // NOTE: For SkColor vertex colors, the source color space is always sRGB, and the destination
525 // gamut is determined by the render target context. A mis-match should be impossible.
526 SkASSERT(GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get()));
527
Brian Salomon199fb872017-02-06 09:41:10 -0500528 // If either op required explicit local coords or per-vertex colors the combined mesh does. Same
529 // with multiple view matrices.
530 fFlags |= that->fFlags;
joshualitt2771b562015-08-07 12:46:26 -0700531
Brian Salomon199fb872017-02-06 09:41:10 -0500532 if (!this->requiresPerVertexColors() && this->fMeshes[0].fColor != that->fMeshes[0].fColor) {
533 fFlags |= kRequiresPerVertexColors_Flag;
534 }
Brian Salomon3f363692017-02-02 21:05:19 -0500535 // Check whether we are about to acquire a mesh with a different view matrix.
Brian Salomon199fb872017-02-06 09:41:10 -0500536 if (!this->hasMultipleViewMatrices() &&
537 !this->fMeshes[0].fViewMatrix.cheapEqualTo(that->fMeshes[0].fViewMatrix)) {
538 fFlags |= kHasMultipleViewMatrices_Flag;
Brian Salomon3f363692017-02-02 21:05:19 -0500539 }
540
bsalomond92b4192016-06-30 07:59:23 -0700541 fMeshes.push_back_n(that->fMeshes.count(), that->fMeshes.begin());
bsalomon14eaaa62015-09-24 07:01:26 -0700542 fVertexCount += that->fVertexCount;
543 fIndexCount += that->fIndexCount;
joshualitt2771b562015-08-07 12:46:26 -0700544
Brian Salomon7eae3e02018-08-07 14:02:38 +0000545 return CombineResult::kMerged;
joshualitt2771b562015-08-07 12:46:26 -0700546}
547
548///////////////////////////////////////////////////////////////////////////////////////////////////
549
Hal Canary6f6961e2017-01-31 13:50:44 -0500550#if GR_TEST_UTILS
joshualitt2771b562015-08-07 12:46:26 -0700551
Brian Salomon5ec9def2016-12-20 15:34:05 -0500552#include "GrDrawOpTest.h"
joshualitt2771b562015-08-07 12:46:26 -0700553
554static uint32_t seed_vertices(GrPrimitiveType type) {
555 switch (type) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600556 case GrPrimitiveType::kTriangles:
557 case GrPrimitiveType::kTriangleStrip:
joshualitt2771b562015-08-07 12:46:26 -0700558 return 3;
Chris Dalton3809bab2017-06-13 10:55:06 -0600559 case GrPrimitiveType::kPoints:
joshualitt2771b562015-08-07 12:46:26 -0700560 return 1;
Chris Dalton3809bab2017-06-13 10:55:06 -0600561 case GrPrimitiveType::kLines:
562 case GrPrimitiveType::kLineStrip:
joshualitt2771b562015-08-07 12:46:26 -0700563 return 2;
Chris Dalton3809bab2017-06-13 10:55:06 -0600564 case GrPrimitiveType::kLinesAdjacency:
565 return 4;
joshualitt2771b562015-08-07 12:46:26 -0700566 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400567 SK_ABORT("Incomplete switch\n");
joshualitt2771b562015-08-07 12:46:26 -0700568 return 0;
569}
570
571static uint32_t primitive_vertices(GrPrimitiveType type) {
572 switch (type) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600573 case GrPrimitiveType::kTriangles:
joshualitt2771b562015-08-07 12:46:26 -0700574 return 3;
Chris Dalton3809bab2017-06-13 10:55:06 -0600575 case GrPrimitiveType::kLines:
joshualitt2771b562015-08-07 12:46:26 -0700576 return 2;
Chris Dalton3809bab2017-06-13 10:55:06 -0600577 case GrPrimitiveType::kTriangleStrip:
Chris Dalton3809bab2017-06-13 10:55:06 -0600578 case GrPrimitiveType::kPoints:
579 case GrPrimitiveType::kLineStrip:
joshualitt2771b562015-08-07 12:46:26 -0700580 return 1;
Chris Dalton3809bab2017-06-13 10:55:06 -0600581 case GrPrimitiveType::kLinesAdjacency:
582 return 4;
joshualitt2771b562015-08-07 12:46:26 -0700583 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400584 SK_ABORT("Incomplete switch\n");
joshualitt2771b562015-08-07 12:46:26 -0700585 return 0;
586}
587
588static SkPoint random_point(SkRandom* random, SkScalar min, SkScalar max) {
589 SkPoint p;
590 p.fX = random->nextRangeScalar(min, max);
591 p.fY = random->nextRangeScalar(min, max);
592 return p;
593}
594
595static void randomize_params(size_t count, size_t maxVertex, SkScalar min, SkScalar max,
Brian Salomonfc527d22016-12-14 21:07:01 -0500596 SkRandom* random, SkTArray<SkPoint>* positions,
joshualitt2771b562015-08-07 12:46:26 -0700597 SkTArray<SkPoint>* texCoords, bool hasTexCoords,
Brian Salomon3de0aee2017-01-29 09:34:17 -0500598 SkTArray<uint32_t>* colors, bool hasColors,
599 SkTArray<uint16_t>* indices, bool hasIndices) {
joshualitt2771b562015-08-07 12:46:26 -0700600 for (uint32_t v = 0; v < count; v++) {
601 positions->push_back(random_point(random, min, max));
602 if (hasTexCoords) {
603 texCoords->push_back(random_point(random, min, max));
604 }
605 if (hasColors) {
606 colors->push_back(GrRandomColor(random));
607 }
608 if (hasIndices) {
Ben Wagnerb0897652018-06-15 15:37:57 +0000609 SkASSERT(maxVertex <= UINT16_MAX);
joshualitt2771b562015-08-07 12:46:26 -0700610 indices->push_back(random->nextULessThan((uint16_t)maxVertex));
611 }
612 }
613}
614
Brian Salomonc2f42542017-07-12 14:11:22 -0400615GR_DRAW_OP_TEST_DEFINE(GrDrawVerticesOp) {
Chris Daltonb894c2b2017-06-14 12:39:19 -0600616 GrPrimitiveType type;
617 do {
618 type = GrPrimitiveType(random->nextULessThan(kNumGrPrimitiveTypes));
619 } while (GrPrimTypeRequiresGeometryShaderSupport(type) &&
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400620 !context->contextPriv().caps()->shaderCaps()->geometryShaderSupport());
Chris Daltonb894c2b2017-06-14 12:39:19 -0600621
joshualitt2771b562015-08-07 12:46:26 -0700622 uint32_t primitiveCount = random->nextRangeU(1, 100);
623
624 // TODO make 'sensible' indexbuffers
625 SkTArray<SkPoint> positions;
626 SkTArray<SkPoint> texCoords;
Brian Salomon3de0aee2017-01-29 09:34:17 -0500627 SkTArray<uint32_t> colors;
joshualitt2771b562015-08-07 12:46:26 -0700628 SkTArray<uint16_t> indices;
629
630 bool hasTexCoords = random->nextBool();
631 bool hasIndices = random->nextBool();
632 bool hasColors = random->nextBool();
633
634 uint32_t vertexCount = seed_vertices(type) + (primitiveCount - 1) * primitive_vertices(type);
635
636 static const SkScalar kMinVertExtent = -100.f;
637 static const SkScalar kMaxVertExtent = 100.f;
Brian Salomonfc527d22016-12-14 21:07:01 -0500638 randomize_params(seed_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent, random,
639 &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices,
640 hasIndices);
joshualitt2771b562015-08-07 12:46:26 -0700641
642 for (uint32_t i = 1; i < primitiveCount; i++) {
643 randomize_params(primitive_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent,
Brian Salomonfc527d22016-12-14 21:07:01 -0500644 random, &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices,
645 hasIndices);
joshualitt2771b562015-08-07 12:46:26 -0700646 }
647
648 SkMatrix viewMatrix = GrTest::TestMatrix(random);
joshualitt2771b562015-08-07 12:46:26 -0700649
Brian Osmanfa6d8652017-05-31 09:37:27 -0400650 sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(random);
Brian Osmanae0c50c2017-05-25 16:56:34 -0400651
652 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
653 sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, vertexCount, positions.begin(),
654 texCoords.begin(), colors.begin(),
655 hasIndices ? indices.count() : 0,
656 indices.begin());
Brian Salomonc2f42542017-07-12 14:11:22 -0400657 GrAAType aaType = GrAAType::kNone;
658 if (GrFSAAType::kUnifiedMSAA == fsaaType && random->nextBool()) {
659 aaType = GrAAType::kMSAA;
660 }
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400661 return GrDrawVerticesOp::Make(context, std::move(paint), std::move(vertices), nullptr, 0,
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400662 viewMatrix, aaType, std::move(colorSpaceXform), &type);
joshualitt2771b562015-08-07 12:46:26 -0700663}
664
665#endif