joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 1 | /* |
| 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 Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 8 | #include "GrDrawVerticesOp.h" |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 9 | #include "GrCaps.h" |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 10 | #include "GrDefaultGeoProcFactory.h" |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 11 | #include "GrOpFlushState.h" |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 12 | #include "SkGr.h" |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 13 | #include "SkRectPriv.h" |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 14 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 15 | std::unique_ptr<GrDrawOp> GrDrawVerticesOp::Make(GrContext* context, |
| 16 | GrPaint&& paint, |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 17 | sk_sp<SkVertices> vertices, |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 18 | const SkMatrix bones[], |
| 19 | int boneCount, |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 20 | const SkMatrix& viewMatrix, |
| 21 | GrAAType aaType, |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 22 | sk_sp<GrColorSpaceXform> colorSpaceXform, |
| 23 | GrPrimitiveType* overridePrimType) { |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 24 | SkASSERT(vertices); |
Brian Osman | ae0c50c | 2017-05-25 16:56:34 -0400 | [diff] [blame] | 25 | GrPrimitiveType primType = overridePrimType ? *overridePrimType |
| 26 | : SkVertexModeToGrPrimitiveType(vertices->mode()); |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 27 | return Helper::FactoryHelper<GrDrawVerticesOp>(context, std::move(paint), std::move(vertices), |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 28 | bones, boneCount, primType, aaType, |
| 29 | std::move(colorSpaceXform), viewMatrix); |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 30 | } |
| 31 | |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 32 | GrDrawVerticesOp::GrDrawVerticesOp(const Helper::MakeArgs& helperArgs, GrColor color, |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 33 | sk_sp<SkVertices> vertices, const SkMatrix bones[], |
| 34 | int boneCount, GrPrimitiveType primitiveType, GrAAType aaType, |
| 35 | sk_sp<GrColorSpaceXform> colorSpaceXform, |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 36 | const SkMatrix& viewMatrix) |
| 37 | : INHERITED(ClassID()) |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 38 | , fHelper(helperArgs, aaType) |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 39 | , fPrimitiveType(primitiveType) |
| 40 | , fColorSpaceXform(std::move(colorSpaceXform)) { |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 41 | SkASSERT(vertices); |
| 42 | |
| 43 | fVertexCount = vertices->vertexCount(); |
| 44 | fIndexCount = vertices->indexCount(); |
Brian Osman | ae0c50c | 2017-05-25 16:56:34 -0400 | [diff] [blame] | 45 | fColorArrayType = vertices->hasColors() ? ColorArrayType::kSkColor |
| 46 | : ColorArrayType::kPremulGrColor; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 47 | |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 48 | Mesh& mesh = fMeshes.push_back(); |
| 49 | mesh.fColor = color; |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 50 | mesh.fViewMatrix = viewMatrix; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 51 | mesh.fVertices = std::move(vertices); |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 52 | if (bones) { |
| 53 | mesh.fBones.assign(bones, bones + boneCount); |
| 54 | } |
Brian Osman | 8a03055 | 2017-05-23 15:03:18 -0400 | [diff] [blame] | 55 | mesh.fIgnoreTexCoords = false; |
| 56 | mesh.fIgnoreColors = false; |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 57 | mesh.fIgnoreBones = false; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 58 | |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 59 | fFlags = 0; |
| 60 | if (mesh.hasPerVertexColors()) { |
| 61 | fFlags |= kRequiresPerVertexColors_Flag; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 62 | } |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 63 | if (mesh.hasExplicitLocalCoords()) { |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 64 | fFlags |= kAnyMeshHasExplicitLocalCoords_Flag; |
| 65 | } |
| 66 | if (mesh.hasBones()) { |
| 67 | fFlags |= kHasBones_Flag; |
| 68 | } |
| 69 | |
| 70 | // Special case for meshes with a world transform but no bone weights. |
| 71 | // These will be considered normal vertices draws without bones. |
| 72 | if (!mesh.fVertices->hasBones() && boneCount == 1) { |
| 73 | mesh.fViewMatrix.preConcat(bones[0]); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 74 | } |
| 75 | |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 76 | IsZeroArea zeroArea; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 77 | if (GrIsPrimTypeLines(primitiveType) || GrPrimitiveType::kPoints == primitiveType) { |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 78 | zeroArea = IsZeroArea::kYes; |
| 79 | } else { |
| 80 | zeroArea = IsZeroArea::kNo; |
| 81 | } |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 82 | |
| 83 | if (this->hasBones()) { |
| 84 | // We don't know the bounds if there are deformations involved, so attempt to calculate |
| 85 | // the maximum possible. |
| 86 | SkRect bounds; |
| 87 | const SkRect& originalBounds = bones[0].mapRect(mesh.fVertices->bounds()); |
| 88 | for (int i = 1; i < boneCount; i++) { |
| 89 | const SkMatrix& matrix = bones[i]; |
| 90 | bounds.join(matrix.mapRect(originalBounds)); |
| 91 | } |
| 92 | |
| 93 | this->setTransformedBounds(bounds, |
| 94 | mesh.fViewMatrix, |
| 95 | HasAABloat::kNo, |
| 96 | zeroArea); |
| 97 | } else { |
| 98 | this->setTransformedBounds(mesh.fVertices->bounds(), |
| 99 | mesh.fViewMatrix, |
| 100 | HasAABloat::kNo, |
| 101 | zeroArea); |
| 102 | } |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 105 | SkString GrDrawVerticesOp::dumpInfo() const { |
| 106 | SkString string; |
| 107 | string.appendf("PrimType: %d, MeshCount %d, VCount: %d, ICount: %d\n", (int)fPrimitiveType, |
| 108 | fMeshes.count(), fVertexCount, fIndexCount); |
| 109 | string += fHelper.dumpInfo(); |
| 110 | string += INHERITED::dumpInfo(); |
| 111 | return string; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 114 | GrDrawOp::FixedFunctionFlags GrDrawVerticesOp::fixedFunctionFlags() const { |
| 115 | return fHelper.fixedFunctionFlags(); |
| 116 | } |
| 117 | |
| 118 | GrDrawOp::RequiresDstTexture GrDrawVerticesOp::finalize(const GrCaps& caps, |
Brian Osman | 9a725dd | 2017-09-20 09:53:22 -0400 | [diff] [blame] | 119 | const GrAppliedClip* clip, |
| 120 | GrPixelConfigIsClamped dstIsClamped) { |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 121 | GrProcessorAnalysisColor gpColor; |
| 122 | if (this->requiresPerVertexColors()) { |
| 123 | gpColor.setToUnknown(); |
| 124 | } else { |
| 125 | gpColor.setToConstant(fMeshes.front().fColor); |
| 126 | } |
Brian Osman | 9a725dd | 2017-09-20 09:53:22 -0400 | [diff] [blame] | 127 | auto result = fHelper.xpRequiresDstTexture(caps, clip, dstIsClamped, |
| 128 | GrProcessorAnalysisCoverage::kNone, &gpColor); |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 129 | if (gpColor.isConstant(&fMeshes.front().fColor)) { |
| 130 | fMeshes.front().fIgnoreColors = true; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 131 | fFlags &= ~kRequiresPerVertexColors_Flag; |
Brian Osman | ae0c50c | 2017-05-25 16:56:34 -0400 | [diff] [blame] | 132 | fColorArrayType = ColorArrayType::kPremulGrColor; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 133 | } |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 134 | if (!fHelper.usesLocalCoords()) { |
Brian Osman | 8a03055 | 2017-05-23 15:03:18 -0400 | [diff] [blame] | 135 | fMeshes[0].fIgnoreTexCoords = true; |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 136 | fFlags &= ~kAnyMeshHasExplicitLocalCoords_Flag; |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 137 | } |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 138 | return result; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 141 | sk_sp<GrGeometryProcessor> GrDrawVerticesOp::makeGP(bool* hasColorAttribute, |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 142 | bool* hasLocalCoordAttribute, |
| 143 | bool* hasBoneAttribute) const { |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 144 | using namespace GrDefaultGeoProcFactory; |
| 145 | LocalCoords::Type localCoordsType; |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 146 | if (fHelper.usesLocalCoords()) { |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 147 | // If we have multiple view matrices we will transform the positions into device space. We |
| 148 | // must then also provide untransformed positions as local coords. |
| 149 | if (this->anyMeshHasExplicitLocalCoords() || this->hasMultipleViewMatrices()) { |
| 150 | *hasLocalCoordAttribute = true; |
| 151 | localCoordsType = LocalCoords::kHasExplicit_Type; |
| 152 | } else { |
| 153 | *hasLocalCoordAttribute = false; |
| 154 | localCoordsType = LocalCoords::kUsePosition_Type; |
| 155 | } |
| 156 | } else { |
| 157 | localCoordsType = LocalCoords::kUnused_Type; |
| 158 | *hasLocalCoordAttribute = false; |
| 159 | } |
| 160 | |
| 161 | Color color(fMeshes[0].fColor); |
| 162 | if (this->requiresPerVertexColors()) { |
Brian Osman | 08a50e0 | 2018-06-15 15:06:48 -0400 | [diff] [blame] | 163 | if (fColorArrayType == ColorArrayType::kPremulGrColor) { |
| 164 | color.fType = Color::kPremulGrColorAttribute_Type; |
| 165 | } else { |
| 166 | color.fType = Color::kUnpremulSkColorAttribute_Type; |
| 167 | color.fColorSpaceXform = fColorSpaceXform; |
| 168 | } |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 169 | *hasColorAttribute = true; |
| 170 | } else { |
| 171 | *hasColorAttribute = false; |
| 172 | }; |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 173 | |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 174 | const SkMatrix& vm = this->hasMultipleViewMatrices() ? SkMatrix::I() : fMeshes[0].fViewMatrix; |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 175 | |
| 176 | Bones bones(fMeshes[0].fBones.data(), fMeshes[0].fBones.size()); |
| 177 | *hasBoneAttribute = this->hasBones(); |
| 178 | |
| 179 | if (this->hasBones()) { |
| 180 | return GrDefaultGeoProcFactory::MakeWithBones(color, |
| 181 | Coverage::kSolid_Type, |
| 182 | localCoordsType, |
| 183 | bones, |
| 184 | vm); |
| 185 | } else { |
| 186 | return GrDefaultGeoProcFactory::Make(color, |
| 187 | Coverage::kSolid_Type, |
| 188 | localCoordsType, |
| 189 | vm); |
| 190 | } |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 191 | } |
| 192 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 193 | void GrDrawVerticesOp::onPrepareDraws(Target* target) { |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 194 | if (fMeshes[0].fVertices->isVolatile()) { |
| 195 | this->drawVolatile(target); |
| 196 | } else { |
| 197 | this->drawNonVolatile(target); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | void GrDrawVerticesOp::drawVolatile(Target* target) { |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 202 | bool hasColorAttribute; |
| 203 | bool hasLocalCoordsAttribute; |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 204 | bool hasBoneAttribute; |
| 205 | sk_sp<GrGeometryProcessor> gp = this->makeGP(&hasColorAttribute, |
| 206 | &hasLocalCoordsAttribute, |
| 207 | &hasBoneAttribute); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 208 | |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 209 | // Calculate the stride. |
| 210 | size_t vertexStride = sizeof(SkPoint) + |
| 211 | (hasColorAttribute ? sizeof(uint32_t) : 0) + |
| 212 | (hasLocalCoordsAttribute ? sizeof(SkPoint) : 0) + |
| 213 | (hasBoneAttribute ? 4 * (sizeof(uint32_t) + sizeof(float)) : 0); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 214 | SkASSERT(vertexStride == gp->debugOnly_vertexStride()); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 215 | |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 216 | // Allocate buffers. |
| 217 | const GrBuffer* vertexBuffer = nullptr; |
| 218 | int firstVertex = 0; |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 219 | void* verts = target->makeVertexSpace(vertexStride, fVertexCount, &vertexBuffer, &firstVertex); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 220 | if (!verts) { |
| 221 | SkDebugf("Could not allocate vertices\n"); |
| 222 | return; |
| 223 | } |
| 224 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 225 | const GrBuffer* indexBuffer = nullptr; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 226 | int firstIndex = 0; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 227 | uint16_t* indices = nullptr; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 228 | if (this->isIndexed()) { |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 229 | indices = target->makeIndexSpace(fIndexCount, &indexBuffer, &firstIndex); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 230 | if (!indices) { |
| 231 | SkDebugf("Could not allocate indices\n"); |
| 232 | return; |
| 233 | } |
| 234 | } |
| 235 | |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 236 | // Fill the buffers. |
| 237 | this->fillBuffers(hasColorAttribute, |
| 238 | hasLocalCoordsAttribute, |
| 239 | hasBoneAttribute, |
| 240 | vertexStride, |
| 241 | verts, |
| 242 | indices); |
| 243 | |
| 244 | // Draw the vertices. |
| 245 | this->drawVertices(target, gp.get(), vertexBuffer, firstVertex, indexBuffer, firstIndex); |
| 246 | } |
| 247 | |
| 248 | void GrDrawVerticesOp::drawNonVolatile(Target* target) { |
| 249 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 250 | |
| 251 | bool hasColorAttribute; |
| 252 | bool hasLocalCoordsAttribute; |
| 253 | bool hasBoneAttribute; |
| 254 | sk_sp<GrGeometryProcessor> gp = this->makeGP(&hasColorAttribute, |
| 255 | &hasLocalCoordsAttribute, |
| 256 | &hasBoneAttribute); |
| 257 | |
| 258 | int instanceCount = fMeshes.count(); |
| 259 | SkASSERT(instanceCount == 1); // Non-volatile meshes should never combine. |
| 260 | |
| 261 | // Get the resource provider. |
| 262 | GrResourceProvider* rp = target->resourceProvider(); |
| 263 | |
| 264 | // Generate keys for the buffers. |
| 265 | GrUniqueKey vertexKey, indexKey; |
| 266 | GrUniqueKey::Builder vertexKeyBuilder(&vertexKey, kDomain, instanceCount + 1); |
| 267 | GrUniqueKey::Builder indexKeyBuilder(&indexKey, kDomain, instanceCount + 1); |
| 268 | for (int i = 0; i < instanceCount; i ++) { |
| 269 | vertexKeyBuilder[i] = indexKeyBuilder[i] = fMeshes[i].fVertices->uniqueID(); |
| 270 | } |
| 271 | vertexKeyBuilder[instanceCount] = 0; |
| 272 | indexKeyBuilder[instanceCount] = 1; |
| 273 | vertexKeyBuilder.finish(); |
| 274 | indexKeyBuilder.finish(); |
| 275 | |
| 276 | // Try to grab data from the cache. |
| 277 | sk_sp<GrBuffer> vertexBuffer = rp->findByUniqueKey<GrBuffer>(vertexKey); |
| 278 | sk_sp<GrBuffer> indexBuffer = rp->findByUniqueKey<GrBuffer>(indexKey); |
| 279 | |
| 280 | // Draw using the cached buffers if possible. |
| 281 | if (vertexBuffer) { |
| 282 | this->drawVertices(target, gp.get(), vertexBuffer.get(), 0, indexBuffer.get(), 0); |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | // Calculate the stride. |
| 287 | size_t vertexStride = sizeof(SkPoint) + |
| 288 | (hasColorAttribute ? sizeof(uint32_t) : 0) + |
| 289 | (hasLocalCoordsAttribute ? sizeof(SkPoint) : 0) + |
| 290 | (hasBoneAttribute ? 4 * (sizeof(uint32_t) + sizeof(float)) : 0); |
| 291 | SkASSERT(vertexStride == gp->debugOnly_vertexStride()); |
| 292 | |
| 293 | // Allocate vertex buffer. |
| 294 | vertexBuffer.reset(rp->createBuffer(fVertexCount * vertexStride, |
| 295 | kVertex_GrBufferType, |
| 296 | kStatic_GrAccessPattern, |
| 297 | 0)); |
| 298 | void* verts = vertexBuffer ? vertexBuffer->map() : nullptr; |
| 299 | if (!verts) { |
| 300 | SkDebugf("Could not allocate vertices\n"); |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | // Allocate index buffer. |
| 305 | uint16_t* indices = nullptr; |
| 306 | if (this->isIndexed()) { |
| 307 | indexBuffer.reset(rp->createBuffer(fIndexCount * sizeof(uint16_t), |
| 308 | kIndex_GrBufferType, |
| 309 | kStatic_GrAccessPattern, |
| 310 | 0)); |
| 311 | indices = indexBuffer ? static_cast<uint16_t*>(indexBuffer->map()) : nullptr; |
| 312 | if (!indices) { |
| 313 | SkDebugf("Could not allocate indices\n"); |
| 314 | return; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | // Fill the buffers. |
| 319 | this->fillBuffers(hasColorAttribute, |
| 320 | hasLocalCoordsAttribute, |
| 321 | hasBoneAttribute, |
| 322 | vertexStride, |
| 323 | verts, |
| 324 | indices); |
| 325 | |
| 326 | // Unmap the buffers. |
| 327 | vertexBuffer->unmap(); |
| 328 | if (indexBuffer) { |
| 329 | indexBuffer->unmap(); |
| 330 | } |
| 331 | |
| 332 | // Cache the buffers. |
| 333 | rp->assignUniqueKeyToResource(vertexKey, vertexBuffer.get()); |
| 334 | rp->assignUniqueKeyToResource(indexKey, indexBuffer.get()); |
| 335 | |
| 336 | // Draw the vertices. |
| 337 | this->drawVertices(target, gp.get(), vertexBuffer.get(), 0, indexBuffer.get(), 0); |
| 338 | } |
| 339 | |
| 340 | void GrDrawVerticesOp::fillBuffers(bool hasColorAttribute, |
| 341 | bool hasLocalCoordsAttribute, |
| 342 | bool hasBoneAttribute, |
| 343 | size_t vertexStride, |
| 344 | void* verts, |
| 345 | uint16_t* indices) const { |
| 346 | int instanceCount = fMeshes.count(); |
| 347 | |
| 348 | // Copy data into the buffers. |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 349 | int vertexOffset = 0; |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 350 | // We have a fast case below for uploading the vertex data when the matrix is translate |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 351 | // only and there are colors but not local coords. Fast case does not apply when there are bone |
| 352 | // transformations. |
| 353 | bool fastAttrs = hasColorAttribute && !hasLocalCoordsAttribute && !hasBoneAttribute; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 354 | for (int i = 0; i < instanceCount; i++) { |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 355 | // Get each mesh. |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 356 | const Mesh& mesh = fMeshes[i]; |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 357 | |
| 358 | // Copy data into the index buffer. |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 359 | if (indices) { |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 360 | int indexCount = mesh.fVertices->indexCount(); |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 361 | for (int j = 0; j < indexCount; ++j) { |
| 362 | *indices++ = mesh.fVertices->indices()[j] + vertexOffset; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 365 | |
| 366 | // Copy data into the vertex buffer. |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 367 | int vertexCount = mesh.fVertices->vertexCount(); |
| 368 | const SkPoint* positions = mesh.fVertices->positions(); |
| 369 | const SkColor* colors = mesh.fVertices->colors(); |
| 370 | const SkPoint* localCoords = mesh.fVertices->texCoords(); |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 371 | const SkVertices::BoneIndices* boneIndices = mesh.fVertices->boneIndices(); |
| 372 | const SkVertices::BoneWeights* boneWeights = mesh.fVertices->boneWeights(); |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 373 | bool fastMesh = (!this->hasMultipleViewMatrices() || |
| 374 | mesh.fViewMatrix.getType() <= SkMatrix::kTranslate_Mask) && |
| 375 | mesh.hasPerVertexColors(); |
| 376 | if (fastAttrs && fastMesh) { |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 377 | // Fast case. |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 378 | struct V { |
| 379 | SkPoint fPos; |
| 380 | uint32_t fColor; |
| 381 | }; |
| 382 | SkASSERT(sizeof(V) == vertexStride); |
| 383 | V* v = (V*)verts; |
| 384 | Sk2f t(0, 0); |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 385 | if (this->hasMultipleViewMatrices()) { |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 386 | t = Sk2f(mesh.fViewMatrix.getTranslateX(), mesh.fViewMatrix.getTranslateY()); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 387 | } |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 388 | for (int j = 0; j < vertexCount; ++j) { |
| 389 | Sk2f p = Sk2f::Load(positions++) + t; |
| 390 | p.store(&v[j].fPos); |
| 391 | v[j].fColor = colors[j]; |
| 392 | } |
| 393 | verts = v + vertexCount; |
| 394 | } else { |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 395 | // Normal case. |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 396 | static constexpr size_t kColorOffset = sizeof(SkPoint); |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 397 | size_t offset = kColorOffset; |
| 398 | if (hasColorAttribute) { |
| 399 | offset += sizeof(uint32_t); |
| 400 | } |
| 401 | size_t localCoordOffset = offset; |
| 402 | if (hasLocalCoordsAttribute) { |
| 403 | offset += sizeof(SkPoint); |
| 404 | } |
| 405 | size_t boneIndexOffset = offset; |
| 406 | offset += 4 * sizeof(uint32_t); |
| 407 | size_t boneWeightOffset = offset; |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 408 | |
| 409 | for (int j = 0; j < vertexCount; ++j) { |
| 410 | if (this->hasMultipleViewMatrices()) { |
| 411 | mesh.fViewMatrix.mapPoints(((SkPoint*)verts), &positions[j], 1); |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 412 | } else { |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 413 | *((SkPoint*)verts) = positions[j]; |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 414 | } |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 415 | if (hasColorAttribute) { |
| 416 | if (mesh.hasPerVertexColors()) { |
| 417 | *(uint32_t*)((intptr_t)verts + kColorOffset) = colors[j]; |
| 418 | } else { |
| 419 | *(uint32_t*)((intptr_t)verts + kColorOffset) = mesh.fColor; |
| 420 | } |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 421 | } |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 422 | if (hasLocalCoordsAttribute) { |
| 423 | if (mesh.hasExplicitLocalCoords()) { |
| 424 | *(SkPoint*)((intptr_t)verts + localCoordOffset) = localCoords[j]; |
| 425 | } else { |
| 426 | *(SkPoint*)((intptr_t)verts + localCoordOffset) = positions[j]; |
| 427 | } |
| 428 | } |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 429 | if (hasBoneAttribute) { |
| 430 | const SkVertices::BoneIndices& indices = boneIndices[j]; |
| 431 | const SkVertices::BoneWeights& weights = boneWeights[j]; |
| 432 | for (int k = 0; k < 4; k++) { |
| 433 | size_t indexOffset = boneIndexOffset + sizeof(uint32_t) * k; |
| 434 | size_t weightOffset = boneWeightOffset + sizeof(float) * k; |
| 435 | *(uint32_t*)((intptr_t)verts + indexOffset) = indices.indices[k]; |
| 436 | *(float*)((intptr_t)verts + weightOffset) = weights.weights[k]; |
| 437 | } |
| 438 | } |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 439 | verts = (void*)((intptr_t)verts + vertexStride); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 440 | } |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 441 | } |
Brian Salomon | fab30a3 | 2017-02-06 19:06:22 -0500 | [diff] [blame] | 442 | vertexOffset += vertexCount; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 443 | } |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 444 | } |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 445 | |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 446 | void GrDrawVerticesOp::drawVertices(Target* target, |
| 447 | GrGeometryProcessor* gp, |
| 448 | const GrBuffer* vertexBuffer, |
| 449 | int firstVertex, |
| 450 | const GrBuffer* indexBuffer, |
| 451 | int firstIndex) { |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 452 | GrMesh mesh(this->primitiveType()); |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 453 | if (this->isIndexed()) { |
| 454 | mesh.setIndexed(indexBuffer, fIndexCount, |
| 455 | firstIndex, 0, fVertexCount - 1, |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 456 | GrPrimitiveRestart::kNo); |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 457 | } else { |
| 458 | mesh.setNonIndexedNonInstanced(fVertexCount); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 459 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 460 | mesh.setVertexData(vertexBuffer, firstVertex); |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 461 | auto pipe = fHelper.makePipeline(target); |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 462 | target->draw(gp, pipe.fPipeline, pipe.fFixedDynamicState, mesh); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 465 | bool GrDrawVerticesOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) { |
| 466 | GrDrawVerticesOp* that = t->cast<GrDrawVerticesOp>(); |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 467 | |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 468 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 469 | return false; |
| 470 | } |
| 471 | |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 472 | // Meshes with bones cannot be combined because different meshes use different bones, so to |
| 473 | // combine them, the matrices would have to be combined, and the bone indices on each vertex |
| 474 | // would change, thus making the vertices uncacheable. |
| 475 | if (this->hasBones() || that->hasBones()) { |
| 476 | return false; |
| 477 | } |
| 478 | |
| 479 | // Non-volatile meshes cannot batch, because if a non-volatile mesh batches with another mesh, |
| 480 | // then on the next frame, if that non-volatile mesh is drawn, it will draw the other mesh |
| 481 | // that was saved in its vertex buffer, which is not necessarily there anymore. |
| 482 | if (!this->fMeshes[0].fVertices->isVolatile() || !that->fMeshes[0].fVertices->isVolatile()) { |
| 483 | return false; |
| 484 | } |
| 485 | |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 486 | if (!this->combinablePrimitive() || this->primitiveType() != that->primitiveType()) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 487 | return false; |
| 488 | } |
| 489 | |
Mike Reed | aa9e332 | 2017-03-16 14:38:48 -0400 | [diff] [blame] | 490 | if (fMeshes[0].fVertices->hasIndices() != that->fMeshes[0].fVertices->hasIndices()) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 491 | return false; |
| 492 | } |
| 493 | |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 494 | if (fColorArrayType != that->fColorArrayType) { |
| 495 | return false; |
| 496 | } |
| 497 | |
Ben Wagner | 9bc36fd | 2018-06-15 14:23:36 -0400 | [diff] [blame] | 498 | if (fVertexCount + that->fVertexCount > SkTo<int>(UINT16_MAX)) { |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 499 | return false; |
| 500 | } |
| 501 | |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 502 | // NOTE: For SkColor vertex colors, the source color space is always sRGB, and the destination |
| 503 | // gamut is determined by the render target context. A mis-match should be impossible. |
| 504 | SkASSERT(GrColorSpaceXform::Equals(fColorSpaceXform.get(), that->fColorSpaceXform.get())); |
| 505 | |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 506 | // If either op required explicit local coords or per-vertex colors the combined mesh does. Same |
| 507 | // with multiple view matrices. |
| 508 | fFlags |= that->fFlags; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 509 | |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 510 | if (!this->requiresPerVertexColors() && this->fMeshes[0].fColor != that->fMeshes[0].fColor) { |
| 511 | fFlags |= kRequiresPerVertexColors_Flag; |
| 512 | } |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 513 | // Check whether we are about to acquire a mesh with a different view matrix. |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 514 | if (!this->hasMultipleViewMatrices() && |
| 515 | !this->fMeshes[0].fViewMatrix.cheapEqualTo(that->fMeshes[0].fViewMatrix)) { |
| 516 | fFlags |= kHasMultipleViewMatrices_Flag; |
Brian Salomon | 3f36369 | 2017-02-02 21:05:19 -0500 | [diff] [blame] | 517 | } |
| 518 | |
bsalomon | d92b419 | 2016-06-30 07:59:23 -0700 | [diff] [blame] | 519 | fMeshes.push_back_n(that->fMeshes.count(), that->fMeshes.begin()); |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 520 | fVertexCount += that->fVertexCount; |
| 521 | fIndexCount += that->fIndexCount; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 522 | |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 523 | this->joinBounds(*that); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 524 | return true; |
| 525 | } |
| 526 | |
| 527 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 528 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 529 | #if GR_TEST_UTILS |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 530 | |
Brian Salomon | 5ec9def | 2016-12-20 15:34:05 -0500 | [diff] [blame] | 531 | #include "GrDrawOpTest.h" |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 532 | |
| 533 | static uint32_t seed_vertices(GrPrimitiveType type) { |
| 534 | switch (type) { |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 535 | case GrPrimitiveType::kTriangles: |
| 536 | case GrPrimitiveType::kTriangleStrip: |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 537 | return 3; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 538 | case GrPrimitiveType::kPoints: |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 539 | return 1; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 540 | case GrPrimitiveType::kLines: |
| 541 | case GrPrimitiveType::kLineStrip: |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 542 | return 2; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 543 | case GrPrimitiveType::kLinesAdjacency: |
| 544 | return 4; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 545 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 546 | SK_ABORT("Incomplete switch\n"); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | static uint32_t primitive_vertices(GrPrimitiveType type) { |
| 551 | switch (type) { |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 552 | case GrPrimitiveType::kTriangles: |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 553 | return 3; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 554 | case GrPrimitiveType::kLines: |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 555 | return 2; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 556 | case GrPrimitiveType::kTriangleStrip: |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 557 | case GrPrimitiveType::kPoints: |
| 558 | case GrPrimitiveType::kLineStrip: |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 559 | return 1; |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 560 | case GrPrimitiveType::kLinesAdjacency: |
| 561 | return 4; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 562 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 563 | SK_ABORT("Incomplete switch\n"); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | static SkPoint random_point(SkRandom* random, SkScalar min, SkScalar max) { |
| 568 | SkPoint p; |
| 569 | p.fX = random->nextRangeScalar(min, max); |
| 570 | p.fY = random->nextRangeScalar(min, max); |
| 571 | return p; |
| 572 | } |
| 573 | |
| 574 | static void randomize_params(size_t count, size_t maxVertex, SkScalar min, SkScalar max, |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 575 | SkRandom* random, SkTArray<SkPoint>* positions, |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 576 | SkTArray<SkPoint>* texCoords, bool hasTexCoords, |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 577 | SkTArray<uint32_t>* colors, bool hasColors, |
| 578 | SkTArray<uint16_t>* indices, bool hasIndices) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 579 | for (uint32_t v = 0; v < count; v++) { |
| 580 | positions->push_back(random_point(random, min, max)); |
| 581 | if (hasTexCoords) { |
| 582 | texCoords->push_back(random_point(random, min, max)); |
| 583 | } |
| 584 | if (hasColors) { |
| 585 | colors->push_back(GrRandomColor(random)); |
| 586 | } |
| 587 | if (hasIndices) { |
Ben Wagner | b089765 | 2018-06-15 15:37:57 +0000 | [diff] [blame] | 588 | SkASSERT(maxVertex <= UINT16_MAX); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 589 | indices->push_back(random->nextULessThan((uint16_t)maxVertex)); |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 594 | GR_DRAW_OP_TEST_DEFINE(GrDrawVerticesOp) { |
Chris Dalton | b894c2b | 2017-06-14 12:39:19 -0600 | [diff] [blame] | 595 | GrPrimitiveType type; |
| 596 | do { |
| 597 | type = GrPrimitiveType(random->nextULessThan(kNumGrPrimitiveTypes)); |
| 598 | } while (GrPrimTypeRequiresGeometryShaderSupport(type) && |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 599 | !context->contextPriv().caps()->shaderCaps()->geometryShaderSupport()); |
Chris Dalton | b894c2b | 2017-06-14 12:39:19 -0600 | [diff] [blame] | 600 | |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 601 | uint32_t primitiveCount = random->nextRangeU(1, 100); |
| 602 | |
| 603 | // TODO make 'sensible' indexbuffers |
| 604 | SkTArray<SkPoint> positions; |
| 605 | SkTArray<SkPoint> texCoords; |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 606 | SkTArray<uint32_t> colors; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 607 | SkTArray<uint16_t> indices; |
| 608 | |
| 609 | bool hasTexCoords = random->nextBool(); |
| 610 | bool hasIndices = random->nextBool(); |
| 611 | bool hasColors = random->nextBool(); |
| 612 | |
| 613 | uint32_t vertexCount = seed_vertices(type) + (primitiveCount - 1) * primitive_vertices(type); |
| 614 | |
| 615 | static const SkScalar kMinVertExtent = -100.f; |
| 616 | static const SkScalar kMaxVertExtent = 100.f; |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 617 | randomize_params(seed_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent, random, |
| 618 | &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices, |
| 619 | hasIndices); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 620 | |
| 621 | for (uint32_t i = 1; i < primitiveCount; i++) { |
| 622 | randomize_params(primitive_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent, |
Brian Salomon | fc527d2 | 2016-12-14 21:07:01 -0500 | [diff] [blame] | 623 | random, &positions, &texCoords, hasTexCoords, &colors, hasColors, &indices, |
| 624 | hasIndices); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 628 | |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 629 | sk_sp<GrColorSpaceXform> colorSpaceXform = GrTest::TestColorXform(random); |
Brian Osman | ae0c50c | 2017-05-25 16:56:34 -0400 | [diff] [blame] | 630 | |
| 631 | static constexpr SkVertices::VertexMode kIgnoredMode = SkVertices::kTriangles_VertexMode; |
| 632 | sk_sp<SkVertices> vertices = SkVertices::MakeCopy(kIgnoredMode, vertexCount, positions.begin(), |
| 633 | texCoords.begin(), colors.begin(), |
| 634 | hasIndices ? indices.count() : 0, |
| 635 | indices.begin()); |
Brian Salomon | c2f4254 | 2017-07-12 14:11:22 -0400 | [diff] [blame] | 636 | GrAAType aaType = GrAAType::kNone; |
| 637 | if (GrFSAAType::kUnifiedMSAA == fsaaType && random->nextBool()) { |
| 638 | aaType = GrAAType::kMSAA; |
| 639 | } |
Ruiqi Mao | b630734 | 2018-07-03 11:38:15 -0400 | [diff] [blame] | 640 | return GrDrawVerticesOp::Make(context, std::move(paint), std::move(vertices), nullptr, 0, |
| 641 | viewMatrix, aaType, std::move(colorSpaceXform), &type); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | #endif |