blob: 30ab521b1acd8b2c78ac3334493606bcff410846 [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 Osmancf860852018-10-31 14:04:39 -040032GrDrawVerticesOp::GrDrawVerticesOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& 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 Osman9a390ac2018-11-12 09:47:48 -0500117#ifdef SK_DEBUG
Brian Salomonc2f42542017-07-12 14:11:22 -0400118SkString GrDrawVerticesOp::dumpInfo() const {
119 SkString string;
120 string.appendf("PrimType: %d, MeshCount %d, VCount: %d, ICount: %d\n", (int)fPrimitiveType,
121 fMeshes.count(), fVertexCount, fIndexCount);
122 string += fHelper.dumpInfo();
123 string += INHERITED::dumpInfo();
124 return string;
joshualitt2771b562015-08-07 12:46:26 -0700125}
Brian Osman9a390ac2018-11-12 09:47:48 -0500126#endif
joshualitt2771b562015-08-07 12:46:26 -0700127
Brian Salomonc2f42542017-07-12 14:11:22 -0400128GrDrawOp::FixedFunctionFlags GrDrawVerticesOp::fixedFunctionFlags() const {
129 return fHelper.fixedFunctionFlags();
130}
131
132GrDrawOp::RequiresDstTexture GrDrawVerticesOp::finalize(const GrCaps& caps,
Brian Osman532b3f92018-07-11 10:02:07 -0400133 const GrAppliedClip* clip) {
Brian Salomonc2f42542017-07-12 14:11:22 -0400134 GrProcessorAnalysisColor gpColor;
135 if (this->requiresPerVertexColors()) {
136 gpColor.setToUnknown();
137 } else {
138 gpColor.setToConstant(fMeshes.front().fColor);
139 }
Brian Osman532b3f92018-07-11 10:02:07 -0400140 auto result = fHelper.xpRequiresDstTexture(caps, clip, GrProcessorAnalysisCoverage::kNone,
141 &gpColor);
Brian Salomonc2f42542017-07-12 14:11:22 -0400142 if (gpColor.isConstant(&fMeshes.front().fColor)) {
143 fMeshes.front().fIgnoreColors = true;
Brian Salomon199fb872017-02-06 09:41:10 -0500144 fFlags &= ~kRequiresPerVertexColors_Flag;
Brian Osmanae0c50c2017-05-25 16:56:34 -0400145 fColorArrayType = ColorArrayType::kPremulGrColor;
joshualitt2771b562015-08-07 12:46:26 -0700146 }
Brian Salomonc2f42542017-07-12 14:11:22 -0400147 if (!fHelper.usesLocalCoords()) {
Brian Osman8a030552017-05-23 15:03:18 -0400148 fMeshes[0].fIgnoreTexCoords = true;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400149 fFlags &= ~kAnyMeshHasExplicitLocalCoords_Flag;
bsalomon14eaaa62015-09-24 07:01:26 -0700150 }
Brian Salomonc2f42542017-07-12 14:11:22 -0400151 return result;
joshualitt2771b562015-08-07 12:46:26 -0700152}
153
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400154sk_sp<GrGeometryProcessor> GrDrawVerticesOp::makeGP(const GrShaderCaps* shaderCaps,
155 bool* hasColorAttribute,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400156 bool* hasLocalCoordAttribute,
157 bool* hasBoneAttribute) const {
Brian Salomon199fb872017-02-06 09:41:10 -0500158 using namespace GrDefaultGeoProcFactory;
159 LocalCoords::Type localCoordsType;
Brian Salomonc2f42542017-07-12 14:11:22 -0400160 if (fHelper.usesLocalCoords()) {
Brian Salomon199fb872017-02-06 09:41:10 -0500161 // If we have multiple view matrices we will transform the positions into device space. We
162 // must then also provide untransformed positions as local coords.
163 if (this->anyMeshHasExplicitLocalCoords() || this->hasMultipleViewMatrices()) {
164 *hasLocalCoordAttribute = true;
165 localCoordsType = LocalCoords::kHasExplicit_Type;
166 } else {
167 *hasLocalCoordAttribute = false;
168 localCoordsType = LocalCoords::kUsePosition_Type;
169 }
170 } else {
171 localCoordsType = LocalCoords::kUnused_Type;
172 *hasLocalCoordAttribute = false;
173 }
174
175 Color color(fMeshes[0].fColor);
176 if (this->requiresPerVertexColors()) {
Brian Osman08a50e02018-06-15 15:06:48 -0400177 if (fColorArrayType == ColorArrayType::kPremulGrColor) {
178 color.fType = Color::kPremulGrColorAttribute_Type;
179 } else {
180 color.fType = Color::kUnpremulSkColorAttribute_Type;
181 color.fColorSpaceXform = fColorSpaceXform;
182 }
Brian Salomon199fb872017-02-06 09:41:10 -0500183 *hasColorAttribute = true;
184 } else {
185 *hasColorAttribute = false;
186 };
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400187
Brian Salomon199fb872017-02-06 09:41:10 -0500188 const SkMatrix& vm = this->hasMultipleViewMatrices() ? SkMatrix::I() : fMeshes[0].fViewMatrix;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400189
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400190 // The bones are packed as 6 floats in column major order, so we can directly upload them to
191 // the GPU as groups of 3 vec2s.
192 Bones bones(reinterpret_cast<const float*>(fBones.data()), fBones.size());
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400193 *hasBoneAttribute = this->hasBones();
194
195 if (this->hasBones()) {
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400196 return GrDefaultGeoProcFactory::MakeWithBones(shaderCaps,
197 color,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400198 Coverage::kSolid_Type,
199 localCoordsType,
200 bones,
201 vm);
202 } else {
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400203 return GrDefaultGeoProcFactory::Make(shaderCaps,
204 color,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400205 Coverage::kSolid_Type,
206 localCoordsType,
207 vm);
208 }
Brian Salomon199fb872017-02-06 09:41:10 -0500209}
210
Brian Salomon91326c32017-08-09 16:02:19 -0400211void GrDrawVerticesOp::onPrepareDraws(Target* target) {
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400212 bool hasMapBufferSupport = GrCaps::kNone_MapFlags != target->caps().mapBufferFlags();
213 if (fMeshes[0].fVertices->isVolatile() || !hasMapBufferSupport) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400214 this->drawVolatile(target);
215 } else {
216 this->drawNonVolatile(target);
217 }
218}
219
220void GrDrawVerticesOp::drawVolatile(Target* target) {
Brian Salomon199fb872017-02-06 09:41:10 -0500221 bool hasColorAttribute;
222 bool hasLocalCoordsAttribute;
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400223 bool hasBoneAttribute;
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400224 sk_sp<GrGeometryProcessor> gp = this->makeGP(target->caps().shaderCaps(),
225 &hasColorAttribute,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400226 &hasLocalCoordsAttribute,
227 &hasBoneAttribute);
joshualitt2771b562015-08-07 12:46:26 -0700228
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400229 // Allocate buffers.
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500230 size_t vertexStride = gp->vertexStride();
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400231 const GrBuffer* vertexBuffer = nullptr;
232 int firstVertex = 0;
bsalomon14eaaa62015-09-24 07:01:26 -0700233 void* verts = target->makeVertexSpace(vertexStride, fVertexCount, &vertexBuffer, &firstVertex);
joshualitt2771b562015-08-07 12:46:26 -0700234 if (!verts) {
235 SkDebugf("Could not allocate vertices\n");
236 return;
237 }
238
cdalton397536c2016-03-25 12:15:03 -0700239 const GrBuffer* indexBuffer = nullptr;
joshualitt2771b562015-08-07 12:46:26 -0700240 int firstIndex = 0;
halcanary96fcdcc2015-08-27 07:41:13 -0700241 uint16_t* indices = nullptr;
Brian Salomon199fb872017-02-06 09:41:10 -0500242 if (this->isIndexed()) {
bsalomon14eaaa62015-09-24 07:01:26 -0700243 indices = target->makeIndexSpace(fIndexCount, &indexBuffer, &firstIndex);
joshualitt2771b562015-08-07 12:46:26 -0700244 if (!indices) {
245 SkDebugf("Could not allocate indices\n");
246 return;
247 }
248 }
249
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400250 // Fill the buffers.
251 this->fillBuffers(hasColorAttribute,
252 hasLocalCoordsAttribute,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400253 hasBoneAttribute,
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400254 vertexStride,
255 verts,
256 indices);
257
258 // Draw the vertices.
Brian Salomon7eae3e02018-08-07 14:02:38 +0000259 this->drawVertices(target, std::move(gp), vertexBuffer, firstVertex, indexBuffer, firstIndex);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400260}
261
262void GrDrawVerticesOp::drawNonVolatile(Target* target) {
263 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
264
265 bool hasColorAttribute;
266 bool hasLocalCoordsAttribute;
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400267 bool hasBoneAttribute;
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400268 sk_sp<GrGeometryProcessor> gp = this->makeGP(target->caps().shaderCaps(),
269 &hasColorAttribute,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400270 &hasLocalCoordsAttribute,
271 &hasBoneAttribute);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400272
273 SkASSERT(fMeshes.count() == 1); // Non-volatile meshes should never combine.
274
275 // Get the resource provider.
276 GrResourceProvider* rp = target->resourceProvider();
277
278 // Generate keys for the buffers.
279 GrUniqueKey vertexKey, indexKey;
280 GrUniqueKey::Builder vertexKeyBuilder(&vertexKey, kDomain, 2);
281 GrUniqueKey::Builder indexKeyBuilder(&indexKey, kDomain, 2);
282 vertexKeyBuilder[0] = indexKeyBuilder[0] = fMeshes[0].fVertices->uniqueID();
283 vertexKeyBuilder[1] = 0;
284 indexKeyBuilder[1] = 1;
285 vertexKeyBuilder.finish();
286 indexKeyBuilder.finish();
287
288 // Try to grab data from the cache.
289 sk_sp<GrBuffer> vertexBuffer = rp->findByUniqueKey<GrBuffer>(vertexKey);
290 sk_sp<GrBuffer> indexBuffer = this->isIndexed() ?
291 rp->findByUniqueKey<GrBuffer>(indexKey) :
292 nullptr;
293
294 // Draw using the cached buffers if possible.
295 if (vertexBuffer && (!this->isIndexed() || indexBuffer)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000296 this->drawVertices(target, std::move(gp), vertexBuffer.get(), 0, indexBuffer.get(), 0);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400297 return;
298 }
299
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400300 // Allocate vertex buffer.
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500301 size_t vertexStride = gp->vertexStride();
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400302 vertexBuffer.reset(rp->createBuffer(fVertexCount * vertexStride,
303 kVertex_GrBufferType,
304 kStatic_GrAccessPattern,
Chris Daltond004e0b2018-09-27 09:28:03 -0600305 GrResourceProvider::Flags::kNone));
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400306 void* verts = vertexBuffer ? vertexBuffer->map() : nullptr;
307 if (!verts) {
308 SkDebugf("Could not allocate vertices\n");
309 return;
310 }
311
312 // Allocate index buffer.
313 uint16_t* indices = nullptr;
314 if (this->isIndexed()) {
315 indexBuffer.reset(rp->createBuffer(fIndexCount * sizeof(uint16_t),
316 kIndex_GrBufferType,
317 kStatic_GrAccessPattern,
Chris Daltond004e0b2018-09-27 09:28:03 -0600318 GrResourceProvider::Flags::kNone));
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400319 indices = indexBuffer ? static_cast<uint16_t*>(indexBuffer->map()) : nullptr;
320 if (!indices) {
321 SkDebugf("Could not allocate indices\n");
322 return;
323 }
324 }
325
326 // Fill the buffers.
327 this->fillBuffers(hasColorAttribute,
328 hasLocalCoordsAttribute,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400329 hasBoneAttribute,
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400330 vertexStride,
331 verts,
332 indices);
333
334 // Unmap the buffers.
335 vertexBuffer->unmap();
336 if (indexBuffer) {
337 indexBuffer->unmap();
338 }
339
340 // Cache the buffers.
341 rp->assignUniqueKeyToResource(vertexKey, vertexBuffer.get());
342 rp->assignUniqueKeyToResource(indexKey, indexBuffer.get());
343
344 // Draw the vertices.
Brian Salomon7eae3e02018-08-07 14:02:38 +0000345 this->drawVertices(target, std::move(gp), vertexBuffer.get(), 0, indexBuffer.get(), 0);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400346}
347
348void GrDrawVerticesOp::fillBuffers(bool hasColorAttribute,
349 bool hasLocalCoordsAttribute,
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400350 bool hasBoneAttribute,
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400351 size_t vertexStride,
352 void* verts,
353 uint16_t* indices) const {
354 int instanceCount = fMeshes.count();
355
356 // Copy data into the buffers.
joshualitt2771b562015-08-07 12:46:26 -0700357 int vertexOffset = 0;
Brian Salomonfab30a32017-02-06 19:06:22 -0500358 // We have a fast case below for uploading the vertex data when the matrix is translate
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400359 // only and there are colors but not local coords. Fast case does not apply when there are bone
360 // transformations.
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400361 bool fastAttrs = hasColorAttribute && !hasLocalCoordsAttribute && !hasBoneAttribute;
joshualitt2771b562015-08-07 12:46:26 -0700362 for (int i = 0; i < instanceCount; i++) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400363 // Get each mesh.
bsalomond92b4192016-06-30 07:59:23 -0700364 const Mesh& mesh = fMeshes[i];
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400365
366 // Copy data into the index buffer.
bsalomon14eaaa62015-09-24 07:01:26 -0700367 if (indices) {
Brian Salomon199fb872017-02-06 09:41:10 -0500368 int indexCount = mesh.fVertices->indexCount();
Brian Salomonfab30a32017-02-06 19:06:22 -0500369 for (int j = 0; j < indexCount; ++j) {
370 *indices++ = mesh.fVertices->indices()[j] + vertexOffset;
joshualitt2771b562015-08-07 12:46:26 -0700371 }
372 }
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400373
374 // Copy data into the vertex buffer.
Brian Salomon199fb872017-02-06 09:41:10 -0500375 int vertexCount = mesh.fVertices->vertexCount();
376 const SkPoint* positions = mesh.fVertices->positions();
377 const SkColor* colors = mesh.fVertices->colors();
378 const SkPoint* localCoords = mesh.fVertices->texCoords();
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400379 const SkVertices::BoneIndices* boneIndices = mesh.fVertices->boneIndices();
380 const SkVertices::BoneWeights* boneWeights = mesh.fVertices->boneWeights();
Brian Salomonfab30a32017-02-06 19:06:22 -0500381 bool fastMesh = (!this->hasMultipleViewMatrices() ||
382 mesh.fViewMatrix.getType() <= SkMatrix::kTranslate_Mask) &&
383 mesh.hasPerVertexColors();
384 if (fastAttrs && fastMesh) {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400385 // Fast case.
Brian Salomonfab30a32017-02-06 19:06:22 -0500386 struct V {
387 SkPoint fPos;
388 uint32_t fColor;
389 };
390 SkASSERT(sizeof(V) == vertexStride);
391 V* v = (V*)verts;
392 Sk2f t(0, 0);
Brian Salomon199fb872017-02-06 09:41:10 -0500393 if (this->hasMultipleViewMatrices()) {
Brian Salomonfab30a32017-02-06 19:06:22 -0500394 t = Sk2f(mesh.fViewMatrix.getTranslateX(), mesh.fViewMatrix.getTranslateY());
joshualitt2771b562015-08-07 12:46:26 -0700395 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500396 for (int j = 0; j < vertexCount; ++j) {
397 Sk2f p = Sk2f::Load(positions++) + t;
398 p.store(&v[j].fPos);
399 v[j].fColor = colors[j];
400 }
401 verts = v + vertexCount;
402 } else {
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400403 // Normal case.
Brian Salomonfab30a32017-02-06 19:06:22 -0500404 static constexpr size_t kColorOffset = sizeof(SkPoint);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400405 size_t offset = kColorOffset;
406 if (hasColorAttribute) {
407 offset += sizeof(uint32_t);
408 }
409 size_t localCoordOffset = offset;
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400410 if (hasLocalCoordsAttribute) {
411 offset += sizeof(SkPoint);
412 }
413 size_t boneIndexOffset = offset;
414 if (hasBoneAttribute) {
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400415 offset += 4 * sizeof(int8_t);
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400416 }
417 size_t boneWeightOffset = offset;
Brian Salomonfab30a32017-02-06 19:06:22 -0500418
Brian Osman1be2b7c2018-10-29 16:07:15 -0400419 // TODO4F: Preserve float colors
Brian Osmancf860852018-10-31 14:04:39 -0400420 GrColor color = mesh.fColor.toBytes_RGBA();
Brian Osman1be2b7c2018-10-29 16:07:15 -0400421
Brian Salomonfab30a32017-02-06 19:06:22 -0500422 for (int j = 0; j < vertexCount; ++j) {
423 if (this->hasMultipleViewMatrices()) {
424 mesh.fViewMatrix.mapPoints(((SkPoint*)verts), &positions[j], 1);
Brian Salomon3f363692017-02-02 21:05:19 -0500425 } else {
Brian Salomonfab30a32017-02-06 19:06:22 -0500426 *((SkPoint*)verts) = positions[j];
Brian Salomon199fb872017-02-06 09:41:10 -0500427 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500428 if (hasColorAttribute) {
429 if (mesh.hasPerVertexColors()) {
430 *(uint32_t*)((intptr_t)verts + kColorOffset) = colors[j];
431 } else {
Brian Osman1be2b7c2018-10-29 16:07:15 -0400432 *(uint32_t*)((intptr_t)verts + kColorOffset) = color;
Brian Salomonfab30a32017-02-06 19:06:22 -0500433 }
Brian Salomon3f363692017-02-02 21:05:19 -0500434 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500435 if (hasLocalCoordsAttribute) {
436 if (mesh.hasExplicitLocalCoords()) {
437 *(SkPoint*)((intptr_t)verts + localCoordOffset) = localCoords[j];
438 } else {
439 *(SkPoint*)((intptr_t)verts + localCoordOffset) = positions[j];
440 }
441 }
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400442 if (hasBoneAttribute) {
443 const SkVertices::BoneIndices& indices = boneIndices[j];
444 const SkVertices::BoneWeights& weights = boneWeights[j];
445 for (int k = 0; k < 4; k++) {
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400446 size_t indexOffset = boneIndexOffset + sizeof(int8_t) * k;
447 size_t weightOffset = boneWeightOffset + sizeof(uint8_t) * k;
448 *(int8_t*)((intptr_t)verts + indexOffset) = indices.indices[k];
449 *(uint8_t*)((intptr_t)verts + weightOffset) = weights.weights[k] * 255.0f;
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400450 }
451 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500452 verts = (void*)((intptr_t)verts + vertexStride);
joshualitt2771b562015-08-07 12:46:26 -0700453 }
joshualitt2771b562015-08-07 12:46:26 -0700454 }
Brian Salomonfab30a32017-02-06 19:06:22 -0500455 vertexOffset += vertexCount;
joshualitt2771b562015-08-07 12:46:26 -0700456 }
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400457}
joshualitt2771b562015-08-07 12:46:26 -0700458
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400459void GrDrawVerticesOp::drawVertices(Target* target,
Brian Salomon7eae3e02018-08-07 14:02:38 +0000460 sk_sp<const GrGeometryProcessor> gp,
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400461 const GrBuffer* vertexBuffer,
462 int firstVertex,
463 const GrBuffer* indexBuffer,
464 int firstIndex) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000465 GrMesh* mesh = target->allocMesh(this->primitiveType());
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400466 if (this->isIndexed()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000467 mesh->setIndexed(indexBuffer, fIndexCount, firstIndex, 0, fVertexCount - 1,
468 GrPrimitiveRestart::kNo);
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400469 } else {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000470 mesh->setNonIndexedNonInstanced(fVertexCount);
joshualitt2771b562015-08-07 12:46:26 -0700471 }
Brian Salomon7eae3e02018-08-07 14:02:38 +0000472 mesh->setVertexData(vertexBuffer, firstVertex);
Brian Salomon49348902018-06-26 09:12:38 -0400473 auto pipe = fHelper.makePipeline(target);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000474 target->draw(std::move(gp), pipe.fPipeline, pipe.fFixedDynamicState, mesh);
joshualitt2771b562015-08-07 12:46:26 -0700475}
476
Brian Salomon7eae3e02018-08-07 14:02:38 +0000477GrOp::CombineResult GrDrawVerticesOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
Brian Salomonfc527d22016-12-14 21:07:01 -0500478 GrDrawVerticesOp* that = t->cast<GrDrawVerticesOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700479
Brian Salomonc2f42542017-07-12 14:11:22 -0400480 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000481 return CombineResult::kCannotCombine;
joshualitt2771b562015-08-07 12:46:26 -0700482 }
483
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400484 // Meshes with bones cannot be combined because different meshes use different bones, so to
485 // combine them, the matrices would have to be combined, and the bone indices on each vertex
486 // would change, thus making the vertices uncacheable.
487 if (this->hasBones() || that->hasBones()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000488 return CombineResult::kCannotCombine;
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400489 }
490
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400491 // Non-volatile meshes cannot batch, because if a non-volatile mesh batches with another mesh,
492 // then on the next frame, if that non-volatile mesh is drawn, it will draw the other mesh
493 // that was saved in its vertex buffer, which is not necessarily there anymore.
494 if (!this->fMeshes[0].fVertices->isVolatile() || !that->fMeshes[0].fVertices->isVolatile()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000495 return CombineResult::kCannotCombine;
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400496 }
497
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500498 if (!this->combinablePrimitive() || this->primitiveType() != that->primitiveType()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000499 return CombineResult::kCannotCombine;
joshualitt2771b562015-08-07 12:46:26 -0700500 }
501
Mike Reedaa9e3322017-03-16 14:38:48 -0400502 if (fMeshes[0].fVertices->hasIndices() != that->fMeshes[0].fVertices->hasIndices()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000503 return CombineResult::kCannotCombine;
joshualitt2771b562015-08-07 12:46:26 -0700504 }
505
Brian Salomon3de0aee2017-01-29 09:34:17 -0500506 if (fColorArrayType != that->fColorArrayType) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000507 return CombineResult::kCannotCombine;
Brian Salomon3de0aee2017-01-29 09:34:17 -0500508 }
509
Ben Wagner9bc36fd2018-06-15 14:23:36 -0400510 if (fVertexCount + that->fVertexCount > SkTo<int>(UINT16_MAX)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000511 return CombineResult::kCannotCombine;
Brian Salomon3f363692017-02-02 21:05:19 -0500512 }
513
Brian Osmanfa6d8652017-05-31 09:37:27 -0400514 // NOTE: For SkColor vertex colors, the source color space is always sRGB, and the destination
515 // gamut is determined by the render target context. A mis-match should be impossible.
516 SkASSERT(GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get()));
517
Brian Salomon199fb872017-02-06 09:41:10 -0500518 // If either op required explicit local coords or per-vertex colors the combined mesh does. Same
519 // with multiple view matrices.
520 fFlags |= that->fFlags;
joshualitt2771b562015-08-07 12:46:26 -0700521
Brian Salomon199fb872017-02-06 09:41:10 -0500522 if (!this->requiresPerVertexColors() && this->fMeshes[0].fColor != that->fMeshes[0].fColor) {
523 fFlags |= kRequiresPerVertexColors_Flag;
524 }
Brian Salomon3f363692017-02-02 21:05:19 -0500525 // Check whether we are about to acquire a mesh with a different view matrix.
Brian Salomon199fb872017-02-06 09:41:10 -0500526 if (!this->hasMultipleViewMatrices() &&
527 !this->fMeshes[0].fViewMatrix.cheapEqualTo(that->fMeshes[0].fViewMatrix)) {
528 fFlags |= kHasMultipleViewMatrices_Flag;
Brian Salomon3f363692017-02-02 21:05:19 -0500529 }
530
bsalomond92b4192016-06-30 07:59:23 -0700531 fMeshes.push_back_n(that->fMeshes.count(), that->fMeshes.begin());
bsalomon14eaaa62015-09-24 07:01:26 -0700532 fVertexCount += that->fVertexCount;
533 fIndexCount += that->fIndexCount;
joshualitt2771b562015-08-07 12:46:26 -0700534
Brian Salomon7eae3e02018-08-07 14:02:38 +0000535 return CombineResult::kMerged;
joshualitt2771b562015-08-07 12:46:26 -0700536}
537
538///////////////////////////////////////////////////////////////////////////////////////////////////
539
Hal Canary6f6961e2017-01-31 13:50:44 -0500540#if GR_TEST_UTILS
joshualitt2771b562015-08-07 12:46:26 -0700541
Brian Salomon5ec9def2016-12-20 15:34:05 -0500542#include "GrDrawOpTest.h"
joshualitt2771b562015-08-07 12:46:26 -0700543
544static uint32_t seed_vertices(GrPrimitiveType type) {
545 switch (type) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600546 case GrPrimitiveType::kTriangles:
547 case GrPrimitiveType::kTriangleStrip:
joshualitt2771b562015-08-07 12:46:26 -0700548 return 3;
Chris Dalton3809bab2017-06-13 10:55:06 -0600549 case GrPrimitiveType::kPoints:
joshualitt2771b562015-08-07 12:46:26 -0700550 return 1;
Chris Dalton3809bab2017-06-13 10:55:06 -0600551 case GrPrimitiveType::kLines:
552 case GrPrimitiveType::kLineStrip:
joshualitt2771b562015-08-07 12:46:26 -0700553 return 2;
Chris Dalton3809bab2017-06-13 10:55:06 -0600554 case GrPrimitiveType::kLinesAdjacency:
555 return 4;
joshualitt2771b562015-08-07 12:46:26 -0700556 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400557 SK_ABORT("Incomplete switch\n");
joshualitt2771b562015-08-07 12:46:26 -0700558 return 0;
559}
560
561static uint32_t primitive_vertices(GrPrimitiveType type) {
562 switch (type) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600563 case GrPrimitiveType::kTriangles:
joshualitt2771b562015-08-07 12:46:26 -0700564 return 3;
Chris Dalton3809bab2017-06-13 10:55:06 -0600565 case GrPrimitiveType::kLines:
joshualitt2771b562015-08-07 12:46:26 -0700566 return 2;
Chris Dalton3809bab2017-06-13 10:55:06 -0600567 case GrPrimitiveType::kTriangleStrip:
Chris Dalton3809bab2017-06-13 10:55:06 -0600568 case GrPrimitiveType::kPoints:
569 case GrPrimitiveType::kLineStrip:
joshualitt2771b562015-08-07 12:46:26 -0700570 return 1;
Chris Dalton3809bab2017-06-13 10:55:06 -0600571 case GrPrimitiveType::kLinesAdjacency:
572 return 4;
joshualitt2771b562015-08-07 12:46:26 -0700573 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400574 SK_ABORT("Incomplete switch\n");
joshualitt2771b562015-08-07 12:46:26 -0700575 return 0;
576}
577
578static SkPoint random_point(SkRandom* random, SkScalar min, SkScalar max) {
579 SkPoint p;
580 p.fX = random->nextRangeScalar(min, max);
581 p.fY = random->nextRangeScalar(min, max);
582 return p;
583}
584
585static void randomize_params(size_t count, size_t maxVertex, SkScalar min, SkScalar max,
Brian Salomonfc527d22016-12-14 21:07:01 -0500586 SkRandom* random, SkTArray<SkPoint>* positions,
joshualitt2771b562015-08-07 12:46:26 -0700587 SkTArray<SkPoint>* texCoords, bool hasTexCoords,
Brian Salomon3de0aee2017-01-29 09:34:17 -0500588 SkTArray<uint32_t>* colors, bool hasColors,
589 SkTArray<uint16_t>* indices, bool hasIndices) {
joshualitt2771b562015-08-07 12:46:26 -0700590 for (uint32_t v = 0; v < count; v++) {
591 positions->push_back(random_point(random, min, max));
592 if (hasTexCoords) {
593 texCoords->push_back(random_point(random, min, max));
594 }
595 if (hasColors) {
596 colors->push_back(GrRandomColor(random));
597 }
598 if (hasIndices) {
Ben Wagnerb0897652018-06-15 15:37:57 +0000599 SkASSERT(maxVertex <= UINT16_MAX);
joshualitt2771b562015-08-07 12:46:26 -0700600 indices->push_back(random->nextULessThan((uint16_t)maxVertex));
601 }
602 }
603}
604
Brian Salomonc2f42542017-07-12 14:11:22 -0400605GR_DRAW_OP_TEST_DEFINE(GrDrawVerticesOp) {
Chris Daltonb894c2b2017-06-14 12:39:19 -0600606 GrPrimitiveType type;
607 do {
608 type = GrPrimitiveType(random->nextULessThan(kNumGrPrimitiveTypes));
609 } while (GrPrimTypeRequiresGeometryShaderSupport(type) &&
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400610 !context->contextPriv().caps()->shaderCaps()->geometryShaderSupport());
Chris Daltonb894c2b2017-06-14 12:39:19 -0600611
joshualitt2771b562015-08-07 12:46:26 -0700612 uint32_t primitiveCount = random->nextRangeU(1, 100);
613
614 // TODO make 'sensible' indexbuffers
615 SkTArray<SkPoint> positions;
616 SkTArray<SkPoint> texCoords;
Brian Salomon3de0aee2017-01-29 09:34:17 -0500617 SkTArray<uint32_t> colors;
joshualitt2771b562015-08-07 12:46:26 -0700618 SkTArray<uint16_t> indices;
619
620 bool hasTexCoords = random->nextBool();
621 bool hasIndices = random->nextBool();
622 bool hasColors = random->nextBool();
623
624 uint32_t vertexCount = seed_vertices(type) + (primitiveCount - 1) * primitive_vertices(type);
625
626 static const SkScalar kMinVertExtent = -100.f;
627 static const SkScalar kMaxVertExtent = 100.f;
Brian Salomonfc527d22016-12-14 21:07:01 -0500628 randomize_params(seed_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent, random,
629 &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices,
630 hasIndices);
joshualitt2771b562015-08-07 12:46:26 -0700631
632 for (uint32_t i = 1; i < primitiveCount; i++) {
633 randomize_params(primitive_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent,
Brian Salomonfc527d22016-12-14 21:07:01 -0500634 random, &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices,
635 hasIndices);
joshualitt2771b562015-08-07 12:46:26 -0700636 }
637
638 SkMatrix viewMatrix = GrTest::TestMatrix(random);
joshualitt2771b562015-08-07 12:46:26 -0700639
Brian Osmanfa6d8652017-05-31 09:37:27 -0400640 sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(random);
Brian Osmanae0c50c2017-05-25 16:56:34 -0400641
642 static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode;
643 sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, vertexCount, positions.begin(),
644 texCoords.begin(), colors.begin(),
645 hasIndices ? indices.count() : 0,
646 indices.begin());
Brian Salomonc2f42542017-07-12 14:11:22 -0400647 GrAAType aaType = GrAAType::kNone;
648 if (GrFSAAType::kUnifiedMSAA == fsaaType && random->nextBool()) {
649 aaType = GrAAType::kMSAA;
650 }
Ruiqi Mao4ec72f72018-07-10 17:21:07 -0400651 return GrDrawVerticesOp::Make(context, std::move(paint), std::move(vertices), nullptr, 0,
Ruiqi Mao9a6e42f2018-07-09 14:16:56 -0400652 viewMatrix, aaType, std::move(colorSpaceXform), &type);
joshualitt2771b562015-08-07 12:46:26 -0700653}
654
655#endif