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 | f045d60 | 2015-11-18 19:01:12 -0800 | [diff] [blame] | 12 | GrVertexBatch::GrVertexBatch(uint32_t classID) : INHERITED(classID) {} |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 13 | |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 14 | void GrVertexBatch::onPrepare(GrBatchFlushState* state) { |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 15 | Target target(state, this); |
| 16 | this->onPrepareDraws(&target); |
| 17 | } |
| 18 | |
| 19 | void* GrVertexBatch::InstancedHelper::init(Target* target, GrPrimitiveType primType, |
robertphillips | f8c3ba4 | 2016-03-25 04:55:58 -0700 | [diff] [blame^] | 20 | size_t vertexStride, const GrIndexBuffer* indexBuffer, |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 21 | int verticesPerInstance, int indicesPerInstance, |
| 22 | int instancesToDraw) { |
| 23 | SkASSERT(target); |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 24 | if (!indexBuffer) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 25 | return nullptr; |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 26 | } |
robertphillips | f8c3ba4 | 2016-03-25 04:55:58 -0700 | [diff] [blame^] | 27 | const GrVertexBuffer* vertexBuffer; |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 28 | int firstVertex; |
| 29 | int vertexCount = verticesPerInstance * instancesToDraw; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 30 | void* vertices = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, &firstVertex); |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 31 | if (!vertices) { |
| 32 | SkDebugf("Vertices could not be allocated for instanced rendering."); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 33 | return nullptr; |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 34 | } |
| 35 | SkASSERT(vertexBuffer); |
| 36 | size_t ibSize = indexBuffer->gpuMemorySize(); |
| 37 | int maxInstancesPerDraw = static_cast<int>(ibSize / (sizeof(uint16_t) * indicesPerInstance)); |
| 38 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 39 | fMesh.initInstanced(primType, vertexBuffer, indexBuffer, |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 40 | firstVertex, verticesPerInstance, indicesPerInstance, instancesToDraw, |
| 41 | maxInstancesPerDraw); |
| 42 | return vertices; |
| 43 | } |
| 44 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 45 | void GrVertexBatch::InstancedHelper::recordDraw(Target* target) { |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 46 | SkASSERT(fMesh.instanceCount()); |
| 47 | target->draw(fMesh); |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void* GrVertexBatch::QuadHelper::init(Target* target, size_t vertexStride, |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 51 | int quadsToDraw) { |
robertphillips | f8c3ba4 | 2016-03-25 04:55:58 -0700 | [diff] [blame^] | 52 | SkAutoTUnref<const GrIndexBuffer> quadIndexBuffer( |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 53 | target->resourceProvider()->refQuadIndexBuffer()); |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 54 | if (!quadIndexBuffer) { |
| 55 | SkDebugf("Could not get quad index buffer."); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 56 | return nullptr; |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 57 | } |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 58 | return this->INHERITED::init(target, kTriangles_GrPrimitiveType, vertexStride, |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 59 | quadIndexBuffer, kVerticesPerQuad, kIndicesPerQuad, quadsToDraw); |
| 60 | } |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 61 | |
bsalomon | 5346983 | 2015-08-18 09:20:09 -0700 | [diff] [blame] | 62 | void GrVertexBatch::onDraw(GrBatchFlushState* state) { |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 63 | int uploadCnt = fInlineUploads.count(); |
| 64 | int currUpload = 0; |
| 65 | |
| 66 | // Iterate of all the drawArrays. Before issuing the draws in each array, perform any inline |
| 67 | // uploads. |
bsalomon | f045d60 | 2015-11-18 19:01:12 -0800 | [diff] [blame] | 68 | for (DrawArrayList::Iter da(fDrawArrays); da.get(); da.next()) { |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 69 | state->advanceLastFlushedToken(); |
| 70 | while (currUpload < uploadCnt && |
| 71 | fInlineUploads[currUpload]->lastUploadToken() <= state->lastFlushedToken()) { |
| 72 | fInlineUploads[currUpload++]->upload(state->uploader()); |
| 73 | } |
| 74 | const GrVertexBatch::DrawArray& drawArray = *da.get(); |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 75 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 76 | state->gpu()->draw(*this->pipeline(), |
| 77 | *drawArray.fPrimitiveProcessor.get(), |
| 78 | drawArray.fDraws.begin(), |
| 79 | drawArray.fDraws.count()); |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 80 | } |
| 81 | } |