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