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