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