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