blob: a00af15179c8eca5d04677ade0014a67a6406b7e [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
Robert Phillips009e9af2017-06-15 14:01:04 -04008#include "GrGpuCommandBuffer.h"
Brian Salomondad29232016-12-01 16:40:24 -05009#include "GrMeshDrawOp.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050010#include "GrOpFlushState.h"
bsalomon16b99132015-08-13 14:55:50 -070011#include "GrResourceProvider.h"
12
Brian Salomondad29232016-12-01 16:40:24 -050013GrMeshDrawOp::GrMeshDrawOp(uint32_t classID)
Brian Salomon943ed792017-10-30 09:37:55 -040014 : INHERITED(classID), fBaseDrawToken(GrDeferredUploadToken::AlreadyFlushedToken()) {}
bsalomon16b99132015-08-13 14:55:50 -070015
Brian Salomon29b60c92017-10-31 14:42:10 -040016void GrMeshDrawOp::onPrepare(GrOpFlushState* state) { this->onPrepareDraws(state); }
bsalomon75398562015-08-17 12:55:38 -070017
Chris Daltonbca46e22017-05-15 11:03:26 -060018void* GrMeshDrawOp::PatternHelper::init(Target* target, size_t vertexStride,
19 const GrBuffer* indexBuffer, int verticesPerRepetition,
20 int indicesPerRepetition, int repeatCount) {
bsalomon75398562015-08-17 12:55:38 -070021 SkASSERT(target);
bsalomon16b99132015-08-13 14:55:50 -070022 if (!indexBuffer) {
halcanary96fcdcc2015-08-27 07:41:13 -070023 return nullptr;
bsalomon16b99132015-08-13 14:55:50 -070024 }
cdalton397536c2016-03-25 12:15:03 -070025 const GrBuffer* vertexBuffer;
bsalomon16b99132015-08-13 14:55:50 -070026 int firstVertex;
Chris Daltonff926502017-05-03 14:36:54 -040027 int vertexCount = verticesPerRepetition * repeatCount;
Brian Salomondad29232016-12-01 16:40:24 -050028 void* vertices =
29 target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, &firstVertex);
bsalomon16b99132015-08-13 14:55:50 -070030 if (!vertices) {
31 SkDebugf("Vertices could not be allocated for instanced rendering.");
halcanary96fcdcc2015-08-27 07:41:13 -070032 return nullptr;
bsalomon16b99132015-08-13 14:55:50 -070033 }
34 SkASSERT(vertexBuffer);
35 size_t ibSize = indexBuffer->gpuMemorySize();
Chris Daltonff926502017-05-03 14:36:54 -040036 int maxRepetitions = static_cast<int>(ibSize / (sizeof(uint16_t) * indicesPerRepetition));
bsalomon16b99132015-08-13 14:55:50 -070037
Chris Dalton114a3c02017-05-26 15:17:19 -060038 fMesh.setIndexedPatterned(indexBuffer, indicesPerRepetition, verticesPerRepetition,
39 repeatCount, maxRepetitions);
40 fMesh.setVertexData(vertexBuffer, firstVertex);
bsalomon16b99132015-08-13 14:55:50 -070041 return vertices;
42}
43
Chris Daltonff926502017-05-03 14:36:54 -040044void GrMeshDrawOp::PatternHelper::recordDraw(Target* target, const GrGeometryProcessor* gp,
Chris Daltonbca46e22017-05-15 11:03:26 -060045 const GrPipeline* pipeline) {
Brian Salomond3ccb0a2017-04-03 10:38:00 -040046 target->draw(gp, pipeline, fMesh);
bsalomon75398562015-08-17 12:55:38 -070047}
48
Brian Salomondad29232016-12-01 16:40:24 -050049void* GrMeshDrawOp::QuadHelper::init(Target* target, size_t vertexStride, int quadsToDraw) {
Brian Salomond28a79d2017-10-16 13:01:07 -040050 sk_sp<const GrBuffer> quadIndexBuffer = target->resourceProvider()->refQuadIndexBuffer();
bsalomon16b99132015-08-13 14:55:50 -070051 if (!quadIndexBuffer) {
52 SkDebugf("Could not get quad index buffer.");
halcanary96fcdcc2015-08-27 07:41:13 -070053 return nullptr;
bsalomon16b99132015-08-13 14:55:50 -070054 }
Chris Daltonbca46e22017-05-15 11:03:26 -060055 return this->INHERITED::init(target, vertexStride, quadIndexBuffer.get(), kVerticesPerQuad,
56 kIndicesPerQuad, quadsToDraw);
bsalomon16b99132015-08-13 14:55:50 -070057}
bsalomon75398562015-08-17 12:55:38 -070058
Brian Salomon9e50f7b2017-03-06 12:02:34 -050059void GrMeshDrawOp::onExecute(GrOpFlushState* state) {
bsalomon342bfc22016-04-01 06:06:20 -070060 int currUploadIdx = 0;
61 int currMeshIdx = 0;
bsalomon75398562015-08-17 12:55:38 -070062
bsalomon342bfc22016-04-01 06:06:20 -070063 SkASSERT(fQueuedDraws.empty() || fBaseDrawToken == state->nextTokenToFlush());
Greg Daniel500d58b2017-08-24 15:59:33 -040064 SkASSERT(state->rtCommandBuffer());
bsalomon342bfc22016-04-01 06:06:20 -070065
66 for (int currDrawIdx = 0; currDrawIdx < fQueuedDraws.count(); ++currDrawIdx) {
Brian Salomon943ed792017-10-30 09:37:55 -040067 GrDeferredUploadToken drawToken = state->nextTokenToFlush();
bsalomon342bfc22016-04-01 06:06:20 -070068 while (currUploadIdx < fInlineUploads.count() &&
69 fInlineUploads[currUploadIdx].fUploadBeforeToken == drawToken) {
Greg Daniel500d58b2017-08-24 15:59:33 -040070 state->rtCommandBuffer()->inlineUpload(state, fInlineUploads[currUploadIdx++].fUpload);
bsalomon75398562015-08-17 12:55:38 -070071 }
Brian Salomondad29232016-12-01 16:40:24 -050072 const QueuedDraw& draw = fQueuedDraws[currDrawIdx];
Robert Phillips2890fbf2017-07-26 15:48:41 -040073 SkASSERT(draw.fPipeline->proxy() == state->drawOpArgs().fProxy);
Greg Daniel500d58b2017-08-24 15:59:33 -040074 state->rtCommandBuffer()->draw(*draw.fPipeline, *draw.fGeometryProcessor.get(),
75 fMeshes.begin() + currMeshIdx, nullptr, draw.fMeshCnt,
76 this->bounds());
bsalomon342bfc22016-04-01 06:06:20 -070077 currMeshIdx += draw.fMeshCnt;
78 state->flushToken();
79 }
80 SkASSERT(currUploadIdx == fInlineUploads.count());
81 SkASSERT(currMeshIdx == fMeshes.count());
82 fQueuedDraws.reset();
83 fInlineUploads.reset();
84}