blob: 7458437cd767d3f4a85edc7af1bca0d10c123140 [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"
csmartdaltona7f29642016-07-07 08:49:11 -070013#include "GrPipeline.h"
14#include "GrResourceProvider.h"
Robert Phillipse3302df2017-04-24 07:31:02 -040015
csmartdaltona7f29642016-07-07 08:49:11 -070016#include "instanced/InstanceProcessor.h"
17
18namespace gr_instanced {
19
Robert Phillipse3302df2017-04-24 07:31:02 -040020
csmartdaltone0d36292016-07-29 08:14:20 -070021InstancedRendering::InstancedRendering(GrGpu* gpu)
Robert Phillipse3302df2017-04-24 07:31:02 -040022 : fGpu(SkRef(gpu))
23 SkDEBUGCODE(, fState(State::kRecordingDraws)) {
csmartdaltona7f29642016-07-07 08:49:11 -070024}
25
csmartdaltona7f29642016-07-07 08:49:11 -070026
27void InstancedRendering::beginFlush(GrResourceProvider* rp) {
Robert Phillipse3302df2017-04-24 07:31:02 -040028#ifdef SK_DEBUG
csmartdaltona7f29642016-07-07 08:49:11 -070029 SkASSERT(State::kRecordingDraws == fState);
30 fState = State::kFlushing;
Robert Phillipse3302df2017-04-24 07:31:02 -040031#endif
csmartdaltona7f29642016-07-07 08:49:11 -070032
Brian Salomon99ad1642016-12-16 09:50:45 -050033 if (fTrackedOps.isEmpty()) {
csmartdaltona7f29642016-07-07 08:49:11 -070034 return;
35 }
36
37 if (!fVertexBuffer) {
Hal Canary144caf52016-11-07 17:57:18 -050038 fVertexBuffer.reset(InstanceProcessor::FindOrCreateVertexBuffer(fGpu.get()));
csmartdaltona7f29642016-07-07 08:49:11 -070039 if (!fVertexBuffer) {
40 return;
41 }
42 }
43
44 if (!fIndexBuffer) {
Hal Canary144caf52016-11-07 17:57:18 -050045 fIndexBuffer.reset(InstanceProcessor::FindOrCreateIndex8Buffer(fGpu.get()));
csmartdaltona7f29642016-07-07 08:49:11 -070046 if (!fIndexBuffer) {
47 return;
48 }
49 }
50
51 if (!fParams.empty()) {
52 fParamsBuffer.reset(rp->createBuffer(fParams.count() * sizeof(ParamsTexel),
53 kTexel_GrBufferType, kDynamic_GrAccessPattern,
csmartdalton485a1202016-07-13 10:16:32 -070054 GrResourceProvider::kNoPendingIO_Flag |
55 GrResourceProvider::kRequireGpuMemory_Flag,
csmartdaltona7f29642016-07-07 08:49:11 -070056 fParams.begin()));
57 if (!fParamsBuffer) {
58 return;
59 }
60 }
61
62 this->onBeginFlush(rp);
63}
64
Robert Phillipse3302df2017-04-24 07:31:02 -040065void InstancedRendering::draw(const GrPipeline& pipeline,
66 OpInfo info,
67 const InstancedOp* baseOp) {
68 InstanceProcessor instProc(info, fParamsBuffer.get());
csmartdaltona7f29642016-07-07 08:49:11 -070069
Robert Phillipse3302df2017-04-24 07:31:02 -040070 this->onDraw(pipeline, instProc, baseOp);
csmartdaltona7f29642016-07-07 08:49:11 -070071}
72
73void InstancedRendering::endFlush() {
Brian Salomon92aee3d2016-12-21 09:20:25 -050074 // The caller is expected to delete all tracked ops (i.e. ops whose applyPipelineOptimizations
csmartdaltona7f29642016-07-07 08:49:11 -070075 // method has been called) before ending the flush.
Brian Salomon99ad1642016-12-16 09:50:45 -050076 SkASSERT(fTrackedOps.isEmpty());
csmartdaltona7f29642016-07-07 08:49:11 -070077 fParams.reset();
78 fParamsBuffer.reset();
79 this->onEndFlush();
Robert Phillipse3302df2017-04-24 07:31:02 -040080 SkDEBUGCODE(fState = State::kRecordingDraws;)
csmartdaltona7f29642016-07-07 08:49:11 -070081 // Hold on to the shape coords and index buffers.
82}
83
84void InstancedRendering::resetGpuResources(ResetType resetType) {
85 fVertexBuffer.reset();
86 fIndexBuffer.reset();
87 fParamsBuffer.reset();
88 this->onResetGpuResources(resetType);
89}
90
Robert Phillipse3302df2017-04-24 07:31:02 -040091int InstancedRendering::addOpParams(InstancedOp* op) {
92 if (op->fParams.empty()) {
93 return 0;
94 }
95
96 SkASSERT(fParams.count() < (int)kParamsIdx_InfoMask); // TODO: cleaner.
97 int count = fParams.count();
98 fParams.push_back_n(op->fParams.count(), op->fParams.begin());
99 return count;
100}
csmartdaltona7f29642016-07-07 08:49:11 -0700101}