OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrVertexBatch.h" | 8 #include "GrVertexBatch.h" |
9 #include "GrBatchTarget.h" | 9 #include "GrBatchFlushState.h" |
10 #include "GrResourceProvider.h" | 10 #include "GrResourceProvider.h" |
11 | 11 |
12 GrVertexBatch::GrVertexBatch() : fNumberOfDraws(0) {} | 12 GrVertexBatch::GrVertexBatch() : fDrawArrays(1) {} |
13 | 13 |
14 void* GrVertexBatch::InstancedHelper::init(GrBatchTarget* batchTarget, GrPrimiti
veType primType, | 14 void GrVertexBatch::prepareDraws(GrBatchFlushState* state) { |
15 size_t vertexStride, const GrIndexBuffer* i
ndexBuffer, | 15 Target target(state, this); |
16 int verticesPerInstance, int indicesPerInst
ance, | 16 this->onPrepareDraws(&target); |
17 int instancesToDraw) { | 17 } |
18 SkASSERT(batchTarget); | 18 |
| 19 void* GrVertexBatch::InstancedHelper::init(Target* target, GrPrimitiveType primT
ype, |
| 20 size_t vertexStride, const GrIndexBuf
fer* indexBuffer, |
| 21 int verticesPerInstance, int indicesP
erInstance, |
| 22 int instancesToDraw) { |
| 23 SkASSERT(target); |
19 if (!indexBuffer) { | 24 if (!indexBuffer) { |
20 return NULL; | 25 return NULL; |
21 } | 26 } |
22 const GrVertexBuffer* vertexBuffer; | 27 const GrVertexBuffer* vertexBuffer; |
23 int firstVertex; | 28 int firstVertex; |
24 int vertexCount = verticesPerInstance * instancesToDraw; | 29 int vertexCount = verticesPerInstance * instancesToDraw; |
25 void* vertices = batchTarget->makeVertSpace(vertexStride, vertexCount, | 30 void* vertices = target->makeVertexSpace(vertexStride, vertexCount, &vertexB
uffer, &firstVertex); |
26 &vertexBuffer, &firstVertex); | |
27 if (!vertices) { | 31 if (!vertices) { |
28 SkDebugf("Vertices could not be allocated for instanced rendering."); | 32 SkDebugf("Vertices could not be allocated for instanced rendering."); |
29 return NULL; | 33 return NULL; |
30 } | 34 } |
31 SkASSERT(vertexBuffer); | 35 SkASSERT(vertexBuffer); |
32 size_t ibSize = indexBuffer->gpuMemorySize(); | 36 size_t ibSize = indexBuffer->gpuMemorySize(); |
33 int maxInstancesPerDraw = static_cast<int>(ibSize / (sizeof(uint16_t) * indi
cesPerInstance)); | 37 int maxInstancesPerDraw = static_cast<int>(ibSize / (sizeof(uint16_t) * indi
cesPerInstance)); |
34 | 38 |
35 fVertices.initInstanced(primType, vertexBuffer, indexBuffer, | 39 fVertices.initInstanced(primType, vertexBuffer, indexBuffer, |
36 firstVertex, verticesPerInstance, indicesPerInstance, instancesToDraw, | 40 firstVertex, verticesPerInstance, indicesPerInstance, instancesToDraw, |
37 maxInstancesPerDraw); | 41 maxInstancesPerDraw); |
38 return vertices; | 42 return vertices; |
39 } | 43 } |
40 | 44 |
41 void* GrVertexBatch::QuadHelper::init(GrBatchTarget* batchTarget, size_t vertexS
tride, | 45 void GrVertexBatch::InstancedHelper::recordDraw(Target* target) { |
| 46 SkASSERT(fVertices.instanceCount()); |
| 47 target->draw(fVertices); |
| 48 } |
| 49 |
| 50 void* GrVertexBatch::QuadHelper::init(Target* target, size_t vertexStride, |
42 int quadsToDraw) { | 51 int quadsToDraw) { |
43 SkAutoTUnref<const GrIndexBuffer> quadIndexBuffer( | 52 SkAutoTUnref<const GrIndexBuffer> quadIndexBuffer( |
44 batchTarget->resourceProvider()->refQuadIndexBuffer()); | 53 target->resourceProvider()->refQuadIndexBuffer()); |
45 if (!quadIndexBuffer) { | 54 if (!quadIndexBuffer) { |
46 SkDebugf("Could not get quad index buffer."); | 55 SkDebugf("Could not get quad index buffer."); |
47 return NULL; | 56 return NULL; |
48 } | 57 } |
49 return this->INHERITED::init(batchTarget, kTriangles_GrPrimitiveType, vertex
Stride, | 58 return this->INHERITED::init(target, kTriangles_GrPrimitiveType, vertexStrid
e, |
50 quadIndexBuffer, kVerticesPerQuad, kIndicesPerQ
uad, quadsToDraw); | 59 quadIndexBuffer, kVerticesPerQuad, kIndicesPerQ
uad, quadsToDraw); |
51 } | 60 } |
| 61 |
| 62 void GrVertexBatch::issueDraws(GrBatchFlushState* state) { |
| 63 int uploadCnt = fInlineUploads.count(); |
| 64 int currUpload = 0; |
| 65 |
| 66 // Iterate of all the drawArrays. Before issuing the draws in each array, pe
rform any inline |
| 67 // uploads. |
| 68 for (SkTLList<DrawArray>::Iter da(fDrawArrays); da.get(); da.next()) { |
| 69 state->advanceLastFlushedToken(); |
| 70 while (currUpload < uploadCnt && |
| 71 fInlineUploads[currUpload]->lastUploadToken() <= state->lastFlush
edToken()) { |
| 72 fInlineUploads[currUpload++]->upload(state->uploader()); |
| 73 } |
| 74 const GrVertexBatch::DrawArray& drawArray = *da.get(); |
| 75 GrProgramDesc desc; |
| 76 const GrPipeline* pipeline = this->pipeline(); |
| 77 const GrPrimitiveProcessor* primProc = drawArray.fPrimitiveProcessor.get
(); |
| 78 state->gpu()->buildProgramDesc(&desc, *primProc, *pipeline, fBatchTracke
r); |
| 79 GrGpu::DrawArgs args(primProc, pipeline, &desc, &fBatchTracker); |
| 80 |
| 81 int drawCount = drawArray.fDraws.count(); |
| 82 for (int i = 0; i < drawCount; i++) { |
| 83 state->gpu()->draw(args, drawArray.fDraws[i]); |
| 84 } |
| 85 } |
| 86 } |
OLD | NEW |