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 | |
| 8 | #include "GrDrawVerticesBatch.h" |
| 9 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 10 | #include "GrBatchFlushState.h" |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 11 | #include "GrInvariantOutput.h" |
| 12 | #include "GrDefaultGeoProcFactory.h" |
| 13 | |
| 14 | static const GrGeometryProcessor* set_vertex_attributes(bool hasLocalCoords, |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 15 | int* colorOffset, |
| 16 | int* texOffset, |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 17 | const SkMatrix& viewMatrix, |
| 18 | bool coverageIgnored) { |
| 19 | using namespace GrDefaultGeoProcFactory; |
| 20 | *texOffset = -1; |
| 21 | *colorOffset = -1; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 22 | |
| 23 | Coverage coverage(coverageIgnored ? Coverage::kNone_Type : Coverage::kSolid_Type); |
| 24 | LocalCoords localCoords(hasLocalCoords ? LocalCoords::kHasExplicit_Type : |
| 25 | LocalCoords::kUsePosition_Type); |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 26 | *colorOffset = sizeof(SkPoint); |
| 27 | if (hasLocalCoords) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 28 | *texOffset = sizeof(SkPoint) + sizeof(GrColor); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 29 | } |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 30 | return GrDefaultGeoProcFactory::Create(Color(Color::kAttribute_Type), |
| 31 | coverage, localCoords, viewMatrix); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | GrDrawVerticesBatch::GrDrawVerticesBatch(const Geometry& geometry, GrPrimitiveType primitiveType, |
| 35 | const SkMatrix& viewMatrix, |
| 36 | const SkPoint* positions, int vertexCount, |
| 37 | const uint16_t* indices, int indexCount, |
| 38 | const GrColor* colors, const SkPoint* localCoords, |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 39 | const SkRect& bounds) |
| 40 | : INHERITED(ClassID()) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 41 | SkASSERT(positions); |
| 42 | |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 43 | fViewMatrix = viewMatrix; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 44 | Geometry& installedGeo = fGeoData.push_back(geometry); |
| 45 | |
| 46 | installedGeo.fPositions.append(vertexCount, positions); |
| 47 | if (indices) { |
| 48 | installedGeo.fIndices.append(indexCount, indices); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | if (colors) { |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 52 | fVariableColor = true; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 53 | installedGeo.fColors.append(vertexCount, colors); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 54 | } else { |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 55 | fVariableColor = false; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | if (localCoords) { |
| 59 | installedGeo.fLocalCoords.append(vertexCount, localCoords); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 60 | } |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 61 | fVertexCount = vertexCount; |
| 62 | fIndexCount = indexCount; |
| 63 | fPrimitiveType = primitiveType; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 64 | |
| 65 | this->setBounds(bounds); |
| 66 | } |
| 67 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 68 | void GrDrawVerticesBatch::computePipelineOptimizations(GrInitInvariantOutput* color, |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 69 | GrInitInvariantOutput* coverage, |
| 70 | GrBatchToXPOverrides* overrides) const { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 71 | // When this is called on a batch, there is only one geometry bundle |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 72 | if (fVariableColor) { |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 73 | color->setUnknownFourComponents(); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 74 | } else { |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 75 | color->setKnownFourComponents(fGeoData[0].fColor); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 76 | } |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 77 | coverage->setKnownSingleComponent(0xff); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 78 | } |
| 79 | |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 80 | void GrDrawVerticesBatch::initBatchTracker(const GrXPOverridesForBatch& overrides) { |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 81 | SkASSERT(fGeoData.count() == 1); |
| 82 | GrColor overrideColor; |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 83 | if (overrides.getOverrideColorIfSet(&overrideColor)) { |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 84 | fGeoData[0].fColor = overrideColor; |
| 85 | fGeoData[0].fColors.reset(); |
| 86 | fVariableColor = false; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 87 | } |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 88 | fCoverageIgnored = !overrides.readsCoverage(); |
| 89 | if (!overrides.readsLocalCoords()) { |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 90 | fGeoData[0].fLocalCoords.reset(); |
| 91 | } |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 92 | } |
| 93 | |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 94 | void GrDrawVerticesBatch::onPrepareDraws(Target* target) const { |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 95 | bool hasLocalCoords = !fGeoData[0].fLocalCoords.isEmpty(); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 96 | int colorOffset = -1, texOffset = -1; |
| 97 | SkAutoTUnref<const GrGeometryProcessor> gp( |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 98 | set_vertex_attributes(hasLocalCoords, &colorOffset, &texOffset, fViewMatrix, |
| 99 | fCoverageIgnored)); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 100 | size_t vertexStride = gp->getVertexStride(); |
| 101 | |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 102 | SkASSERT(vertexStride == sizeof(SkPoint) + (hasLocalCoords ? sizeof(SkPoint) : 0) |
| 103 | + sizeof(GrColor)); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 104 | |
| 105 | int instanceCount = fGeoData.count(); |
| 106 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 107 | const GrBuffer* vertexBuffer; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 108 | int firstVertex; |
| 109 | |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 110 | void* verts = target->makeVertexSpace(vertexStride, fVertexCount, &vertexBuffer, &firstVertex); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 111 | |
| 112 | if (!verts) { |
| 113 | SkDebugf("Could not allocate vertices\n"); |
| 114 | return; |
| 115 | } |
| 116 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 117 | const GrBuffer* indexBuffer = nullptr; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 118 | int firstIndex = 0; |
| 119 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 120 | uint16_t* indices = nullptr; |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 121 | if (!fGeoData[0].fIndices.isEmpty()) { |
| 122 | indices = target->makeIndexSpace(fIndexCount, &indexBuffer, &firstIndex); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 123 | |
| 124 | if (!indices) { |
| 125 | SkDebugf("Could not allocate indices\n"); |
| 126 | return; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | int indexOffset = 0; |
| 131 | int vertexOffset = 0; |
| 132 | for (int i = 0; i < instanceCount; i++) { |
| 133 | const Geometry& args = fGeoData[i]; |
| 134 | |
| 135 | // TODO we can actually cache this interleaved and then just memcopy |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 136 | if (indices) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 137 | for (int j = 0; j < args.fIndices.count(); ++j, ++indexOffset) { |
| 138 | *(indices + indexOffset) = args.fIndices[j] + vertexOffset; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | for (int j = 0; j < args.fPositions.count(); ++j) { |
| 143 | *((SkPoint*)verts) = args.fPositions[j]; |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 144 | if (args.fColors.isEmpty()) { |
| 145 | *(GrColor*)((intptr_t)verts + colorOffset) = args.fColor; |
| 146 | } else { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 147 | *(GrColor*)((intptr_t)verts + colorOffset) = args.fColors[j]; |
| 148 | } |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 149 | if (hasLocalCoords) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 150 | *(SkPoint*)((intptr_t)verts + texOffset) = args.fLocalCoords[j]; |
| 151 | } |
| 152 | verts = (void*)((intptr_t)verts + vertexStride); |
| 153 | vertexOffset++; |
| 154 | } |
| 155 | } |
| 156 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 157 | GrMesh mesh; |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 158 | if (indices) { |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 159 | mesh.initIndexed(this->primitiveType(), vertexBuffer, indexBuffer, firstVertex, |
| 160 | firstIndex, fVertexCount, fIndexCount); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 161 | |
| 162 | } else { |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 163 | mesh.init(this->primitiveType(), vertexBuffer, firstVertex, fVertexCount); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 164 | } |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame^] | 165 | target->draw(gp, mesh); |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 166 | } |
| 167 | |
bsalomon | cb02b38 | 2015-08-12 11:14:50 -0700 | [diff] [blame] | 168 | bool GrDrawVerticesBatch::onCombineIfPossible(GrBatch* t, const GrCaps& caps) { |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 169 | GrDrawVerticesBatch* that = t->cast<GrDrawVerticesBatch>(); |
| 170 | |
| 171 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 172 | that->bounds(), caps)) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 173 | return false; |
| 174 | } |
| 175 | |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 176 | if (!this->batchablePrimitiveType() || this->primitiveType() != that->primitiveType()) { |
| 177 | return false; |
| 178 | } |
| 179 | |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 180 | // We currently use a uniform viewmatrix for this batch |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 181 | if (!fViewMatrix.cheapEqualTo(that->fViewMatrix)) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 182 | return false; |
| 183 | } |
| 184 | |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 185 | if (fGeoData[0].fIndices.isEmpty() != that->fGeoData[0].fIndices.isEmpty()) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 186 | return false; |
| 187 | } |
| 188 | |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 189 | if (fGeoData[0].fLocalCoords.isEmpty() != that->fGeoData[0].fLocalCoords.isEmpty()) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 190 | return false; |
| 191 | } |
| 192 | |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 193 | if (!fVariableColor) { |
| 194 | if (that->fVariableColor || that->fGeoData[0].fColor != fGeoData[0].fColor) { |
| 195 | fVariableColor = true; |
| 196 | } |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 197 | } |
| 198 | |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 199 | fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()); |
bsalomon | 14eaaa6 | 2015-09-24 07:01:26 -0700 | [diff] [blame] | 200 | fVertexCount += that->fVertexCount; |
| 201 | fIndexCount += that->fIndexCount; |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 202 | |
| 203 | this->joinBounds(that->bounds()); |
| 204 | return true; |
| 205 | } |
| 206 | |
| 207 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 208 | |
| 209 | #ifdef GR_TEST_UTILS |
| 210 | |
| 211 | #include "GrBatchTest.h" |
| 212 | |
| 213 | static uint32_t seed_vertices(GrPrimitiveType type) { |
| 214 | switch (type) { |
| 215 | case kTriangles_GrPrimitiveType: |
| 216 | case kTriangleStrip_GrPrimitiveType: |
| 217 | case kTriangleFan_GrPrimitiveType: |
| 218 | return 3; |
| 219 | case kPoints_GrPrimitiveType: |
| 220 | return 1; |
| 221 | case kLines_GrPrimitiveType: |
| 222 | case kLineStrip_GrPrimitiveType: |
| 223 | return 2; |
| 224 | } |
| 225 | SkFAIL("Incomplete switch\n"); |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | static uint32_t primitive_vertices(GrPrimitiveType type) { |
| 230 | switch (type) { |
| 231 | case kTriangles_GrPrimitiveType: |
| 232 | return 3; |
| 233 | case kLines_GrPrimitiveType: |
| 234 | return 2; |
| 235 | case kTriangleStrip_GrPrimitiveType: |
| 236 | case kTriangleFan_GrPrimitiveType: |
| 237 | case kPoints_GrPrimitiveType: |
| 238 | case kLineStrip_GrPrimitiveType: |
| 239 | return 1; |
| 240 | } |
| 241 | SkFAIL("Incomplete switch\n"); |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static SkPoint random_point(SkRandom* random, SkScalar min, SkScalar max) { |
| 246 | SkPoint p; |
| 247 | p.fX = random->nextRangeScalar(min, max); |
| 248 | p.fY = random->nextRangeScalar(min, max); |
| 249 | return p; |
| 250 | } |
| 251 | |
| 252 | static void randomize_params(size_t count, size_t maxVertex, SkScalar min, SkScalar max, |
| 253 | SkRandom* random, |
| 254 | SkTArray<SkPoint>* positions, |
| 255 | SkTArray<SkPoint>* texCoords, bool hasTexCoords, |
| 256 | SkTArray<GrColor>* colors, bool hasColors, |
| 257 | SkTArray<uint16_t>* indices, bool hasIndices) { |
| 258 | for (uint32_t v = 0; v < count; v++) { |
| 259 | positions->push_back(random_point(random, min, max)); |
| 260 | if (hasTexCoords) { |
| 261 | texCoords->push_back(random_point(random, min, max)); |
| 262 | } |
| 263 | if (hasColors) { |
| 264 | colors->push_back(GrRandomColor(random)); |
| 265 | } |
| 266 | if (hasIndices) { |
| 267 | SkASSERT(maxVertex <= SK_MaxU16); |
| 268 | indices->push_back(random->nextULessThan((uint16_t)maxVertex)); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 273 | DRAW_BATCH_TEST_DEFINE(VerticesBatch) { |
joshualitt | 2771b56 | 2015-08-07 12:46:26 -0700 | [diff] [blame] | 274 | GrPrimitiveType type = GrPrimitiveType(random->nextULessThan(kLast_GrPrimitiveType + 1)); |
| 275 | uint32_t primitiveCount = random->nextRangeU(1, 100); |
| 276 | |
| 277 | // TODO make 'sensible' indexbuffers |
| 278 | SkTArray<SkPoint> positions; |
| 279 | SkTArray<SkPoint> texCoords; |
| 280 | SkTArray<GrColor> colors; |
| 281 | SkTArray<uint16_t> indices; |
| 282 | |
| 283 | bool hasTexCoords = random->nextBool(); |
| 284 | bool hasIndices = random->nextBool(); |
| 285 | bool hasColors = random->nextBool(); |
| 286 | |
| 287 | uint32_t vertexCount = seed_vertices(type) + (primitiveCount - 1) * primitive_vertices(type); |
| 288 | |
| 289 | static const SkScalar kMinVertExtent = -100.f; |
| 290 | static const SkScalar kMaxVertExtent = 100.f; |
| 291 | randomize_params(seed_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent, |
| 292 | random, |
| 293 | &positions, |
| 294 | &texCoords, hasTexCoords, |
| 295 | &colors, hasColors, |
| 296 | &indices, hasIndices); |
| 297 | |
| 298 | for (uint32_t i = 1; i < primitiveCount; i++) { |
| 299 | randomize_params(primitive_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent, |
| 300 | random, |
| 301 | &positions, |
| 302 | &texCoords, hasTexCoords, |
| 303 | &colors, hasColors, |
| 304 | &indices, hasIndices); |
| 305 | } |
| 306 | |
| 307 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
| 308 | SkRect bounds; |
| 309 | SkDEBUGCODE(bool result = ) bounds.setBoundsCheck(positions.begin(), vertexCount); |
| 310 | SkASSERT(result); |
| 311 | |
| 312 | viewMatrix.mapRect(&bounds); |
| 313 | |
| 314 | GrDrawVerticesBatch::Geometry geometry; |
| 315 | geometry.fColor = GrRandomColor(random); |
| 316 | return GrDrawVerticesBatch::Create(geometry, type, viewMatrix, |
| 317 | positions.begin(), vertexCount, |
| 318 | indices.begin(), hasIndices ? vertexCount : 0, |
| 319 | colors.begin(), |
| 320 | texCoords.begin(), |
| 321 | bounds); |
| 322 | } |
| 323 | |
| 324 | #endif |