aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/batches/GrVertexBatch.h
blob: 2af4dd1caddeb96abbf9d0d172471d1d954df72d (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
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef GrVertexBatch_DEFINED
#define GrVertexBatch_DEFINED

#include "GrDrawBatch.h"
#include "GrMesh.h"
#include "GrPrimitiveProcessor.h"
#include "GrPendingProgramElement.h"

#include "SkTLList.h"

class GrBatchFlushState;

/**
 * Base class for vertex-based GrBatches.
 */
class GrVertexBatch : public GrDrawBatch {
public:
    class Target;

    GrVertexBatch(uint32_t classID);

protected:
    /** Helper for rendering instances using an instanced index index buffer. This class creates the
        space for the vertices and flushes the draws to the batch target. */
   class InstancedHelper {
   public:
        InstancedHelper() {}
        /** Returns the allocated storage for the vertices. The caller should populate the before
            vertices before calling issueDraws(). */
        void* init(Target*, GrPrimitiveType, size_t vertexStride,
                   const GrBuffer*, int verticesPerInstance, int indicesPerInstance,
                   int instancesToDraw);

        /** Call after init() to issue draws to the batch target.*/
        void recordDraw(Target* target);
    private:
        GrMesh  fMesh;
    };

    static const int kVerticesPerQuad = 4;
    static const int kIndicesPerQuad = 6;

    /** A specialization of InstanceHelper for quad rendering. */
    class QuadHelper : private InstancedHelper {
    public:
        QuadHelper() : INHERITED() {}
        /** Finds the cached quad index buffer and reserves vertex space. Returns nullptr on failure
            and on sucess a pointer to the vertex data that the caller should populate before
            calling issueDraws(). */
        void* init(Target* batchTarget, size_t vertexStride, int quadsToDraw);

        using InstancedHelper::recordDraw;
    private:
        typedef InstancedHelper INHERITED;
    };

private:
    void onPrepare(GrBatchFlushState* state) final;
    void onDraw(GrBatchFlushState* state) final;

    virtual void onPrepareDraws(Target*) const = 0;

    // A set of contiguous draws with no inline uploads between them that all use the same
    // primitive processor. All the draws in a DrawArray share a primitive processor and use the
    // the batch's GrPipeline.
    struct DrawArray {
        SkSTArray<1, GrMesh, true>                          fDraws;
        GrPendingProgramElement<const GrPrimitiveProcessor> fPrimitiveProcessor;
    };

    // Array of DrawArray. There may be inline uploads between each DrawArray and each DrawArray
    // may use a different primitive processor.
    typedef SkTLList<DrawArray, 4> DrawArrayList;
    DrawArrayList fDrawArrays;

    typedef GrDrawBatch INHERITED;
};

#endif