blob: 6081e2637176140c89acc2044b080d6311968ab9 [file] [log] [blame]
bsalomon16b99132015-08-13 14:55:50 -07001/*
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"
bsalomon75398562015-08-17 12:55:38 -07009#include "GrBatchFlushState.h"
bsalomon16b99132015-08-13 14:55:50 -070010#include "GrResourceProvider.h"
11
bsalomon75398562015-08-17 12:55:38 -070012GrVertexBatch::GrVertexBatch() : fDrawArrays(1) {}
bsalomon16b99132015-08-13 14:55:50 -070013
bsalomon75398562015-08-17 12:55:38 -070014void GrVertexBatch::prepareDraws(GrBatchFlushState* state) {
15 Target target(state, this);
16 this->onPrepareDraws(&target);
17}
18
19void* GrVertexBatch::InstancedHelper::init(Target* target, GrPrimitiveType primType,
20 size_t vertexStride, const GrIndexBuffer* indexBuffer,
21 int verticesPerInstance, int indicesPerInstance,
22 int instancesToDraw) {
23 SkASSERT(target);
bsalomon16b99132015-08-13 14:55:50 -070024 if (!indexBuffer) {
25 return NULL;
26 }
27 const GrVertexBuffer* vertexBuffer;
28 int firstVertex;
29 int vertexCount = verticesPerInstance * instancesToDraw;
bsalomon75398562015-08-17 12:55:38 -070030 void* vertices = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, &firstVertex);
bsalomon16b99132015-08-13 14:55:50 -070031 if (!vertices) {
32 SkDebugf("Vertices could not be allocated for instanced rendering.");
33 return NULL;
34 }
35 SkASSERT(vertexBuffer);
36 size_t ibSize = indexBuffer->gpuMemorySize();
37 int maxInstancesPerDraw = static_cast<int>(ibSize / (sizeof(uint16_t) * indicesPerInstance));
38
39 fVertices.initInstanced(primType, vertexBuffer, indexBuffer,
40 firstVertex, verticesPerInstance, indicesPerInstance, instancesToDraw,
41 maxInstancesPerDraw);
42 return vertices;
43}
44
bsalomon75398562015-08-17 12:55:38 -070045void GrVertexBatch::InstancedHelper::recordDraw(Target* target) {
46 SkASSERT(fVertices.instanceCount());
47 target->draw(fVertices);
48}
49
50void* GrVertexBatch::QuadHelper::init(Target* target, size_t vertexStride,
bsalomon16b99132015-08-13 14:55:50 -070051 int quadsToDraw) {
52 SkAutoTUnref<const GrIndexBuffer> quadIndexBuffer(
bsalomon75398562015-08-17 12:55:38 -070053 target->resourceProvider()->refQuadIndexBuffer());
bsalomon16b99132015-08-13 14:55:50 -070054 if (!quadIndexBuffer) {
55 SkDebugf("Could not get quad index buffer.");
56 return NULL;
57 }
bsalomon75398562015-08-17 12:55:38 -070058 return this->INHERITED::init(target, kTriangles_GrPrimitiveType, vertexStride,
bsalomon16b99132015-08-13 14:55:50 -070059 quadIndexBuffer, kVerticesPerQuad, kIndicesPerQuad, quadsToDraw);
60}
bsalomon75398562015-08-17 12:55:38 -070061
62void 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, perform 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->lastFlushedToken()) {
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, fBatchTracker);
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}