aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/GrMesh.h
blob: 5d1ce6f3e4f57e7d6a703dc76358f54e3050b18a (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * 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 GrMesh_DEFINED
#define GrMesh_DEFINED

#include "GrBuffer.h"
#include "GrGpuResourceRef.h"

class GrNonInstancedMesh {
public:
    GrPrimitiveType primitiveType() const { return fPrimitiveType; }
    int startVertex() const { return fStartVertex; }
    int startIndex() const { return fStartIndex; }
    int vertexCount() const { return fVertexCount; }
    int indexCount() const { return fIndexCount; }
    bool isIndexed() const { return fIndexCount > 0; }

    const GrBuffer* vertexBuffer() const { return fVertexBuffer.get(); }
    const GrBuffer* indexBuffer() const { return fIndexBuffer.get(); }

protected:
    GrPrimitiveType         fPrimitiveType;
    int                     fStartVertex;
    int                     fStartIndex;
    int                     fVertexCount;
    int                     fIndexCount;
    GrPendingIOResource<const GrBuffer, kRead_GrIOType> fVertexBuffer;
    GrPendingIOResource<const GrBuffer, kRead_GrIOType> fIndexBuffer;
    friend class GrMesh;
};

/**
 * Used to communicate index and vertex buffers, counts, and offsets for a draw from GrOp to
 * GrGpu. It also holds the primitive type for the draw. TODO: Consider moving ownership of this
 * and draw-issuing responsibility to GrPrimitiveProcessor. The rest of the vertex info lives there
 * already (stride, attribute mappings).
 */
class GrMesh : public GrNonInstancedMesh {
public:
    GrMesh() {}
    GrMesh(const GrMesh& di) { (*this) = di; }
    GrMesh& operator =(const GrMesh& di);

    void init(GrPrimitiveType primType, const GrBuffer* vertexBuffer, int startVertex,
                int vertexCount) {
        SkASSERT(vertexBuffer);
        SkASSERT(vertexCount);
        SkASSERT(startVertex >= 0);
        fPrimitiveType = primType;
        fVertexBuffer.reset(vertexBuffer);
        fIndexBuffer.reset(nullptr);
        fStartVertex = startVertex;
        fStartIndex = 0;
        fVertexCount = vertexCount;
        fIndexCount = 0;
        fInstanceCount = 0;
        fVerticesPerInstance = 0;
        fIndicesPerInstance = 0;
        fMaxInstancesPerDraw = 0;
    }

    void initIndexed(GrPrimitiveType primType,
                        const GrBuffer* vertexBuffer,
                        const GrBuffer* indexBuffer,
                        int startVertex,
                        int startIndex,
                        int vertexCount,
                        int indexCount) {
        SkASSERT(indexBuffer);
        SkASSERT(vertexBuffer);
        SkASSERT(indexCount);
        SkASSERT(vertexCount);
        SkASSERT(startIndex >= 0);
        SkASSERT(startVertex >= 0);
        fPrimitiveType = primType;
        fVertexBuffer.reset(vertexBuffer);
        fIndexBuffer.reset(indexBuffer);
        fStartVertex = startVertex;
        fStartIndex = startIndex;
        fVertexCount = vertexCount;
        fIndexCount = indexCount;
        fInstanceCount = 0;
        fVerticesPerInstance = 0;
        fIndicesPerInstance = 0;
        fMaxInstancesPerDraw = 0;
    }


    /** Variation of the above that may be used when the total number of instances may exceed
        the number of instances supported by the index buffer. To be used with
        nextInstances() to draw in max-sized batches.*/
    void initInstanced(GrPrimitiveType primType,
                        const GrBuffer* vertexBuffer,
                        const GrBuffer* indexBuffer,
                        int startVertex,
                        int verticesPerInstance,
                        int indicesPerInstance,
                        int instanceCount,
                        int maxInstancesPerDraw) {
        SkASSERT(vertexBuffer);
        SkASSERT(indexBuffer);
        SkASSERT(instanceCount);
        SkASSERT(verticesPerInstance);
        SkASSERT(indicesPerInstance);
        SkASSERT(startVertex >= 0);
        fPrimitiveType = primType;
        fVertexBuffer.reset(vertexBuffer);
        fIndexBuffer.reset(indexBuffer);
        fStartVertex = startVertex;
        fStartIndex = 0;
        fVerticesPerInstance = verticesPerInstance;
        fIndicesPerInstance = indicesPerInstance;
        fInstanceCount = instanceCount;
        fVertexCount = instanceCount * fVerticesPerInstance;
        fIndexCount = instanceCount * fIndicesPerInstance;
        fMaxInstancesPerDraw = maxInstancesPerDraw;
    }


    /** These return 0 if initInstanced was not used to initialize the GrVertices. */
    int verticesPerInstance() const { return fVerticesPerInstance; }
    int indicesPerInstance() const { return fIndicesPerInstance; }
    int instanceCount() const { return fInstanceCount; }

    bool isInstanced() const { return fInstanceCount > 0; }

    class Iterator {
    public:
        const GrNonInstancedMesh* init(const GrMesh& mesh) {
            fMesh = &mesh;
            if (mesh.fInstanceCount <= mesh.fMaxInstancesPerDraw) {
                fInstancesRemaining = 0;
                // Note, this also covers the non-instanced case!
                return &mesh;
            }
            SkASSERT(mesh.isInstanced());
            fInstanceBatch.fIndexBuffer.reset(mesh.fIndexBuffer.get());
            fInstanceBatch.fVertexBuffer.reset(mesh.fVertexBuffer.get());
            fInstanceBatch.fIndexCount = mesh.fMaxInstancesPerDraw *
                                         mesh.fIndicesPerInstance;
            fInstanceBatch.fVertexCount = mesh.fMaxInstancesPerDraw *
                                          mesh.fVerticesPerInstance;
            fInstanceBatch.fPrimitiveType = mesh.fPrimitiveType;
            fInstanceBatch.fStartIndex = mesh.fStartIndex;
            fInstanceBatch.fStartVertex = mesh.fStartVertex;
            fInstancesRemaining = mesh.fInstanceCount - mesh.fMaxInstancesPerDraw;
            return &fInstanceBatch;
        }

        const GrNonInstancedMesh* next() {
            if (!fInstancesRemaining) {
                return nullptr;
            }
            fInstanceBatch.fStartVertex += fInstanceBatch.fVertexCount;
            int instances = SkTMin(fInstancesRemaining, fMesh->fMaxInstancesPerDraw);
            fInstanceBatch.fIndexCount = instances * fMesh->fIndicesPerInstance;
            fInstanceBatch.fVertexCount = instances * fMesh->fVerticesPerInstance;
            fInstancesRemaining -= instances;
            return &fInstanceBatch;
        }
    private:
        GrNonInstancedMesh fInstanceBatch;
        const GrMesh* fMesh;
        int fInstancesRemaining;
    };

private:
    int                     fInstanceCount;
    int                     fVerticesPerInstance;
    int                     fIndicesPerInstance;
    int                     fMaxInstancesPerDraw;
};

#endif