aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/instanced/InstancedRendering.cpp
blob: 7458437cd767d3f4a85edc7af1bca0d10c123140 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "InstancedRendering.h"

#include "InstancedOp.h"
#include "GrAppliedClip.h"
#include "GrCaps.h"
#include "GrPipeline.h"
#include "GrResourceProvider.h"

#include "instanced/InstanceProcessor.h"

namespace gr_instanced {


InstancedRendering::InstancedRendering(GrGpu* gpu)
    : fGpu(SkRef(gpu))
    SkDEBUGCODE(, fState(State::kRecordingDraws)) {
}


void InstancedRendering::beginFlush(GrResourceProvider* rp) {
#ifdef SK_DEBUG
    SkASSERT(State::kRecordingDraws == fState);
    fState = State::kFlushing;
#endif

    if (fTrackedOps.isEmpty()) {
        return;
    }

    if (!fVertexBuffer) {
        fVertexBuffer.reset(InstanceProcessor::FindOrCreateVertexBuffer(fGpu.get()));
        if (!fVertexBuffer) {
            return;
        }
    }

    if (!fIndexBuffer) {
      fIndexBuffer.reset(InstanceProcessor::FindOrCreateIndex8Buffer(fGpu.get()));
        if (!fIndexBuffer) {
            return;
        }
    }

    if (!fParams.empty()) {
        fParamsBuffer.reset(rp->createBuffer(fParams.count() * sizeof(ParamsTexel),
                                             kTexel_GrBufferType, kDynamic_GrAccessPattern,
                                             GrResourceProvider::kNoPendingIO_Flag |
                                             GrResourceProvider::kRequireGpuMemory_Flag,
                                             fParams.begin()));
        if (!fParamsBuffer) {
            return;
        }
    }

    this->onBeginFlush(rp);
}

void InstancedRendering::draw(const GrPipeline& pipeline,
                              OpInfo info,
                              const InstancedOp* baseOp) {
    InstanceProcessor instProc(info, fParamsBuffer.get());

    this->onDraw(pipeline, instProc, baseOp);
}

void InstancedRendering::endFlush() {
    // The caller is expected to delete all tracked ops (i.e. ops whose applyPipelineOptimizations
    // method has been called) before ending the flush.
    SkASSERT(fTrackedOps.isEmpty());
    fParams.reset();
    fParamsBuffer.reset();
    this->onEndFlush();
    SkDEBUGCODE(fState = State::kRecordingDraws;)
    // Hold on to the shape coords and index buffers.
}

void InstancedRendering::resetGpuResources(ResetType resetType) {
    fVertexBuffer.reset();
    fIndexBuffer.reset();
    fParamsBuffer.reset();
    this->onResetGpuResources(resetType);
}

int InstancedRendering::addOpParams(InstancedOp* op) {
    if (op->fParams.empty()) {
        return 0;
    }

    SkASSERT(fParams.count() < (int)kParamsIdx_InfoMask); // TODO: cleaner.
    int count = fParams.count();
    fParams.push_back_n(op->fParams.count(), op->fParams.begin());
    return count;
}
}