blob: 17bcf8e3aff11f07c8d1dffb1038c714cbd53fae [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
bsalomonf045d602015-11-18 19:01:12 -080012GrVertexBatch::GrVertexBatch(uint32_t classID) : INHERITED(classID) {}
bsalomon16b99132015-08-13 14:55:50 -070013
bsalomon53469832015-08-18 09:20:09 -070014void GrVertexBatch::onPrepare(GrBatchFlushState* state) {
bsalomon75398562015-08-17 12:55:38 -070015 Target target(state, this);
16 this->onPrepareDraws(&target);
17}
18
19void* GrVertexBatch::InstancedHelper::init(Target* target, GrPrimitiveType primType,
robertphillipsf8c3ba42016-03-25 04:55:58 -070020 size_t vertexStride, const GrIndexBuffer* indexBuffer,
bsalomon75398562015-08-17 12:55:38 -070021 int verticesPerInstance, int indicesPerInstance,
22 int instancesToDraw) {
23 SkASSERT(target);
bsalomon16b99132015-08-13 14:55:50 -070024 if (!indexBuffer) {
halcanary96fcdcc2015-08-27 07:41:13 -070025 return nullptr;
bsalomon16b99132015-08-13 14:55:50 -070026 }
robertphillipsf8c3ba42016-03-25 04:55:58 -070027 const GrVertexBuffer* vertexBuffer;
bsalomon16b99132015-08-13 14:55:50 -070028 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.");
halcanary96fcdcc2015-08-27 07:41:13 -070033 return nullptr;
bsalomon16b99132015-08-13 14:55:50 -070034 }
35 SkASSERT(vertexBuffer);
36 size_t ibSize = indexBuffer->gpuMemorySize();
37 int maxInstancesPerDraw = static_cast<int>(ibSize / (sizeof(uint16_t) * indicesPerInstance));
38
egdaniel0e1853c2016-03-17 11:35:45 -070039 fMesh.initInstanced(primType, vertexBuffer, indexBuffer,
bsalomon16b99132015-08-13 14:55:50 -070040 firstVertex, verticesPerInstance, indicesPerInstance, instancesToDraw,
41 maxInstancesPerDraw);
42 return vertices;
43}
44
bsalomon75398562015-08-17 12:55:38 -070045void GrVertexBatch::InstancedHelper::recordDraw(Target* target) {
egdaniel0e1853c2016-03-17 11:35:45 -070046 SkASSERT(fMesh.instanceCount());
47 target->draw(fMesh);
bsalomon75398562015-08-17 12:55:38 -070048}
49
50void* GrVertexBatch::QuadHelper::init(Target* target, size_t vertexStride,
bsalomon16b99132015-08-13 14:55:50 -070051 int quadsToDraw) {
robertphillipsf8c3ba42016-03-25 04:55:58 -070052 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.");
halcanary96fcdcc2015-08-27 07:41:13 -070056 return nullptr;
bsalomon16b99132015-08-13 14:55:50 -070057 }
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
bsalomon53469832015-08-18 09:20:09 -070062void GrVertexBatch::onDraw(GrBatchFlushState* state) {
bsalomon75398562015-08-17 12:55:38 -070063 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.
bsalomonf045d602015-11-18 19:01:12 -080068 for (DrawArrayList::Iter da(fDrawArrays); da.get(); da.next()) {
bsalomon75398562015-08-17 12:55:38 -070069 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();
bsalomon75398562015-08-17 12:55:38 -070075
egdaniel0e1853c2016-03-17 11:35:45 -070076 state->gpu()->draw(*this->pipeline(),
77 *drawArray.fPrimitiveProcessor.get(),
78 drawArray.fDraws.begin(),
79 drawArray.fDraws.count());
bsalomon75398562015-08-17 12:55:38 -070080 }
81}