bsalomon | 16b9913 | 2015-08-13 14:55:50 -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 "GrVertexBatch.h" |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 9 | #include "GrBatchFlushState.h" |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 10 | #include "GrResourceProvider.h" |
| 11 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 12 | GrVertexBatch::GrVertexBatch(uint32_t classID) |
| 13 | : INHERITED(classID) |
| 14 | , fBaseDrawToken(GrBatchDrawToken::AlreadyFlushedToken()) { |
| 15 | } |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 16 | |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 17 | void GrVertexBatch::onPrepare(GrBatchFlushState* state) { |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 18 | Target target(state, this); |
| 19 | this->onPrepareDraws(&target); |
| 20 | } |
| 21 | |
| 22 | void* GrVertexBatch::InstancedHelper::init(Target* target, GrPrimitiveType primType, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 23 | size_t vertexStride, const GrBuffer* indexBuffer, |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 24 | int verticesPerInstance, int indicesPerInstance, |
| 25 | int instancesToDraw) { |
| 26 | SkASSERT(target); |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 27 | if (!indexBuffer) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 28 | return nullptr; |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 29 | } |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 30 | const GrBuffer* vertexBuffer; |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 31 | int firstVertex; |
| 32 | int vertexCount = verticesPerInstance * instancesToDraw; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 33 | void* vertices = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, &firstVertex); |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 34 | if (!vertices) { |
| 35 | SkDebugf("Vertices could not be allocated for instanced rendering."); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 36 | return nullptr; |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 37 | } |
| 38 | SkASSERT(vertexBuffer); |
| 39 | size_t ibSize = indexBuffer->gpuMemorySize(); |
| 40 | int maxInstancesPerDraw = static_cast<int>(ibSize / (sizeof(uint16_t) * indicesPerInstance)); |
| 41 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 42 | fMesh.initInstanced(primType, vertexBuffer, indexBuffer, |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 43 | firstVertex, verticesPerInstance, indicesPerInstance, instancesToDraw, |
| 44 | maxInstancesPerDraw); |
| 45 | return vertices; |
| 46 | } |
| 47 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 48 | void GrVertexBatch::InstancedHelper::recordDraw(Target* target, const GrGeometryProcessor* gp) { |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 49 | SkASSERT(fMesh.instanceCount()); |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 50 | target->draw(gp, fMesh); |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void* GrVertexBatch::QuadHelper::init(Target* target, size_t vertexStride, |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 54 | int quadsToDraw) { |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 55 | SkAutoTUnref<const GrBuffer> quadIndexBuffer( |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 56 | target->resourceProvider()->refQuadIndexBuffer()); |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 57 | if (!quadIndexBuffer) { |
| 58 | SkDebugf("Could not get quad index buffer."); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 59 | return nullptr; |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 60 | } |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 61 | return this->INHERITED::init(target, kTriangles_GrPrimitiveType, vertexStride, |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 62 | quadIndexBuffer, kVerticesPerQuad, kIndicesPerQuad, quadsToDraw); |
| 63 | } |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 64 | |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame^] | 65 | void GrVertexBatch::onDraw(GrBatchFlushState* state, const SkRect& bounds) { |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 66 | int currUploadIdx = 0; |
| 67 | int currMeshIdx = 0; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 68 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 69 | SkASSERT(fQueuedDraws.empty() || fBaseDrawToken == state->nextTokenToFlush()); |
| 70 | |
| 71 | for (int currDrawIdx = 0; currDrawIdx < fQueuedDraws.count(); ++currDrawIdx) { |
| 72 | GrBatchDrawToken drawToken = state->nextTokenToFlush(); |
| 73 | while (currUploadIdx < fInlineUploads.count() && |
| 74 | fInlineUploads[currUploadIdx].fUploadBeforeToken == drawToken) { |
| 75 | state->doUpload(fInlineUploads[currUploadIdx++].fUpload); |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 76 | } |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 77 | const QueuedDraw &draw = fQueuedDraws[currDrawIdx]; |
egdaniel | 9cb6340 | 2016-06-23 08:37:05 -0700 | [diff] [blame] | 78 | state->commandBuffer()->draw(*this->pipeline(), *draw.fGeometryProcessor.get(), |
Greg Daniel | 36a77ee | 2016-10-18 10:33:25 -0400 | [diff] [blame^] | 79 | fMeshes.begin() + currMeshIdx, draw.fMeshCnt, bounds); |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 80 | currMeshIdx += draw.fMeshCnt; |
| 81 | state->flushToken(); |
| 82 | } |
| 83 | SkASSERT(currUploadIdx == fInlineUploads.count()); |
| 84 | SkASSERT(currMeshIdx == fMeshes.count()); |
| 85 | fQueuedDraws.reset(); |
| 86 | fInlineUploads.reset(); |
| 87 | } |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 88 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 89 | ////////////////////////////////////////////////////////////////////////////// |
| 90 | |
| 91 | void GrVertexBatch::Target::draw(const GrGeometryProcessor* gp, const GrMesh& mesh) { |
| 92 | GrVertexBatch* batch = this->vertexBatch(); |
| 93 | batch->fMeshes.push_back(mesh); |
| 94 | if (!batch->fQueuedDraws.empty()) { |
| 95 | // If the last draw shares a geometry processor and there are no intervening uploads, |
| 96 | // add this mesh to it. |
| 97 | GrVertexBatch::QueuedDraw& lastDraw = this->vertexBatch()->fQueuedDraws.back(); |
| 98 | if (lastDraw.fGeometryProcessor == gp && |
| 99 | (batch->fInlineUploads.empty() || |
| 100 | batch->fInlineUploads.back().fUploadBeforeToken != this->nextDrawToken())) { |
| 101 | ++lastDraw.fMeshCnt; |
| 102 | return; |
| 103 | } |
| 104 | } |
| 105 | GrVertexBatch::QueuedDraw& draw = this->vertexBatch()->fQueuedDraws.push_back(); |
| 106 | GrBatchDrawToken token = this->state()->issueDrawToken(); |
| 107 | draw.fGeometryProcessor.reset(gp); |
| 108 | draw.fMeshCnt = 1; |
| 109 | if (batch->fQueuedDraws.count() == 1) { |
| 110 | batch->fBaseDrawToken = token; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 111 | } |
| 112 | } |