blob: 6a8fc64222be3f225188fae3acf0fa9c019c506b [file] [log] [blame]
csmartdaltona7f29642016-07-07 08:49:11 -07001/*
2 * Copyright 2016 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 "InstancedRendering.h"
Robert Phillipse3302df2017-04-24 07:31:02 -04009
10#include "InstancedOp.h"
Brian Salomon13071c52017-03-29 21:28:20 -040011#include "GrAppliedClip.h"
robertphillips5fa7f302016-07-21 09:21:04 -070012#include "GrCaps.h"
Robert Phillips646e4292017-06-13 12:44:56 -040013#include "GrGpu.h"
csmartdaltona7f29642016-07-07 08:49:11 -070014#include "GrPipeline.h"
15#include "GrResourceProvider.h"
Robert Phillipse3302df2017-04-24 07:31:02 -040016
csmartdaltona7f29642016-07-07 08:49:11 -070017#include "instanced/InstanceProcessor.h"
18
19namespace gr_instanced {
20
Robert Phillipse3302df2017-04-24 07:31:02 -040021
csmartdaltone0d36292016-07-29 08:14:20 -070022InstancedRendering::InstancedRendering(GrGpu* gpu)
Robert Phillipse3302df2017-04-24 07:31:02 -040023 : fGpu(SkRef(gpu))
24 SkDEBUGCODE(, fState(State::kRecordingDraws)) {
csmartdaltona7f29642016-07-07 08:49:11 -070025}
26
csmartdaltona7f29642016-07-07 08:49:11 -070027
28void InstancedRendering::beginFlush(GrResourceProvider* rp) {
Robert Phillipse3302df2017-04-24 07:31:02 -040029#ifdef SK_DEBUG
csmartdaltona7f29642016-07-07 08:49:11 -070030 SkASSERT(State::kRecordingDraws == fState);
31 fState = State::kFlushing;
Robert Phillipse3302df2017-04-24 07:31:02 -040032#endif
csmartdaltona7f29642016-07-07 08:49:11 -070033
Brian Salomon99ad1642016-12-16 09:50:45 -050034 if (fTrackedOps.isEmpty()) {
csmartdaltona7f29642016-07-07 08:49:11 -070035 return;
36 }
37
38 if (!fVertexBuffer) {
Hal Canary144caf52016-11-07 17:57:18 -050039 fVertexBuffer.reset(InstanceProcessor::FindOrCreateVertexBuffer(fGpu.get()));
csmartdaltona7f29642016-07-07 08:49:11 -070040 if (!fVertexBuffer) {
41 return;
42 }
43 }
44
45 if (!fIndexBuffer) {
Hal Canary144caf52016-11-07 17:57:18 -050046 fIndexBuffer.reset(InstanceProcessor::FindOrCreateIndex8Buffer(fGpu.get()));
csmartdaltona7f29642016-07-07 08:49:11 -070047 if (!fIndexBuffer) {
48 return;
49 }
50 }
51
52 if (!fParams.empty()) {
53 fParamsBuffer.reset(rp->createBuffer(fParams.count() * sizeof(ParamsTexel),
54 kTexel_GrBufferType, kDynamic_GrAccessPattern,
csmartdalton485a1202016-07-13 10:16:32 -070055 GrResourceProvider::kNoPendingIO_Flag |
56 GrResourceProvider::kRequireGpuMemory_Flag,
csmartdaltona7f29642016-07-07 08:49:11 -070057 fParams.begin()));
58 if (!fParamsBuffer) {
59 return;
60 }
61 }
62
63 this->onBeginFlush(rp);
64}
65
Robert Phillipse3302df2017-04-24 07:31:02 -040066void InstancedRendering::draw(const GrPipeline& pipeline,
67 OpInfo info,
68 const InstancedOp* baseOp) {
69 InstanceProcessor instProc(info, fParamsBuffer.get());
csmartdaltona7f29642016-07-07 08:49:11 -070070
Robert Phillipse3302df2017-04-24 07:31:02 -040071 this->onDraw(pipeline, instProc, baseOp);
csmartdaltona7f29642016-07-07 08:49:11 -070072}
73
74void InstancedRendering::endFlush() {
Brian Salomon92aee3d2016-12-21 09:20:25 -050075 // The caller is expected to delete all tracked ops (i.e. ops whose applyPipelineOptimizations
csmartdaltona7f29642016-07-07 08:49:11 -070076 // method has been called) before ending the flush.
Brian Salomon99ad1642016-12-16 09:50:45 -050077 SkASSERT(fTrackedOps.isEmpty());
csmartdaltona7f29642016-07-07 08:49:11 -070078 fParams.reset();
79 fParamsBuffer.reset();
80 this->onEndFlush();
Robert Phillipse3302df2017-04-24 07:31:02 -040081 SkDEBUGCODE(fState = State::kRecordingDraws;)
csmartdaltona7f29642016-07-07 08:49:11 -070082 // Hold on to the shape coords and index buffers.
83}
84
85void InstancedRendering::resetGpuResources(ResetType resetType) {
86 fVertexBuffer.reset();
87 fIndexBuffer.reset();
88 fParamsBuffer.reset();
89 this->onResetGpuResources(resetType);
90}
91
Robert Phillipse3302df2017-04-24 07:31:02 -040092int InstancedRendering::addOpParams(InstancedOp* op) {
93 if (op->fParams.empty()) {
94 return 0;
95 }
96
97 SkASSERT(fParams.count() < (int)kParamsIdx_InfoMask); // TODO: cleaner.
98 int count = fParams.count();
99 fParams.push_back_n(op->fParams.count(), op->fParams.begin());
100 return count;
101}
csmartdaltona7f29642016-07-07 08:49:11 -0700102}