aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/GrMesh.h
blob: 6aed8d3ca54e840d96ae2696da9cec086cfce734 (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
/*
 * 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"

/**
 * 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:
    GrMesh(GrPrimitiveType primitiveType)
        : fPrimitiveType(primitiveType) {
        SkDEBUGCODE(fVertexCount = 0;)
        SkDEBUGCODE(fBaseVertex = -1;)
    }

    void setNonIndexed();
    void setIndexed(const GrBuffer* indexBuffer, int indexCount, int baseIndex = 0);
    void setIndexedPatterned(const GrBuffer* indexBuffer, int indexCount,
                             int patternRepeatCount, int maxPatternRepetitionsInIndexBuffer);

    void setVertices(const GrBuffer* vertexBuffer, int vertexCount, int baseVertex = 0);

    GrPrimitiveType primitiveType() const { return fPrimitiveType; }

    bool isIndexed() const { return SkToBool(fIndexBuffer.get()); }
    const GrBuffer* indexBuffer() const { return fIndexBuffer.get(); }
    int indexCount() const { SkASSERT(this->isIndexed()); return fIndexCount; }
    int baseIndex() const { SkASSERT(this->isIndexed()); return fBaseIndex; }

    const GrBuffer* vertexBuffer() const { return fVertexBuffer.get(); }
    int vertexCount() const { SkASSERT(fVertexCount >= 1); return fVertexCount; }
    int baseVertex() const { SkASSERT(fBaseVertex >= 0); return fBaseVertex; }

    struct PatternBatch;

private:
    using PendingBuffer = GrPendingIOResource<const GrBuffer, kRead_GrIOType>;

    GrPrimitiveType   fPrimitiveType;

    PendingBuffer     fIndexBuffer;
    int               fIndexCount;
    int               fBaseIndex;
    int               fPatternRepeatCount;
    int               fMaxPatternRepetitionsInIndexBuffer;

    PendingBuffer     fVertexBuffer;
    int               fVertexCount;
    int               fBaseVertex;

    class PatternIterator;
    friend GrMesh::PatternIterator begin(const GrMesh&);
    friend GrMesh::PatternIterator end(const GrMesh&);
};

inline void GrMesh::setNonIndexed() {
    fIndexBuffer.reset(nullptr);
}

inline void GrMesh::setIndexed(const GrBuffer* indexBuffer, int indexCount, int baseIndex) {
    SkASSERT(indexBuffer);
    SkASSERT(indexCount >= 1);
    SkASSERT(baseIndex >= 0);
    fIndexBuffer.reset(indexBuffer);
    fIndexCount = indexCount;
    fBaseIndex = baseIndex;
    fPatternRepeatCount = fMaxPatternRepetitionsInIndexBuffer = 1;
}

inline void GrMesh::setIndexedPatterned(const GrBuffer* indexBuffer, int indexCount,
                                        int patternRepeatCount,
                                        int maxPatternRepetitionsInIndexBuffer) {
    SkASSERT(indexBuffer);
    SkASSERT(indexCount >= 1);
    SkASSERT(patternRepeatCount >= 1);
    SkASSERT(maxPatternRepetitionsInIndexBuffer >= 1);
    fIndexBuffer.reset(indexBuffer);
    fIndexCount = indexCount;
    fBaseIndex = 0;
    fPatternRepeatCount = patternRepeatCount;
    fMaxPatternRepetitionsInIndexBuffer = maxPatternRepetitionsInIndexBuffer;
}

inline void GrMesh::setVertices(const GrBuffer* vertexBuffer, int vertexCount, int baseVertex) {
    fVertexBuffer.reset(vertexBuffer);
    fVertexCount = vertexCount;
    fBaseVertex = baseVertex;
}

struct GrMesh::PatternBatch {
    int   fBaseVertex;
    int   fRepeatCount;
};

class GrMesh::PatternIterator {
public:
    PatternIterator(const GrMesh& mesh, int repetitionIdx)
        : fMesh(mesh)
        , fRepetitionIdx(repetitionIdx) {
        SkASSERT(fMesh.isIndexed());
    }

    bool operator!=(const PatternIterator& that) {
        SkASSERT(&fMesh == &that.fMesh);
        return fRepetitionIdx != that.fRepetitionIdx;
    }

    const PatternBatch operator*() {
        PatternBatch batch;
        batch.fBaseVertex = fMesh.fBaseVertex + fRepetitionIdx * fMesh.fVertexCount;
        batch.fRepeatCount = SkTMin(fMesh.fPatternRepeatCount - fRepetitionIdx,
                                    fMesh.fMaxPatternRepetitionsInIndexBuffer);
        return batch;
    }

    void operator++() {
        fRepetitionIdx = SkTMin(fRepetitionIdx + fMesh.fMaxPatternRepetitionsInIndexBuffer,
                                fMesh.fPatternRepeatCount);
    }

private:
    const GrMesh&    fMesh;
    int              fRepetitionIdx;
};

inline GrMesh::PatternIterator begin(const GrMesh& mesh) {
    return GrMesh::PatternIterator(mesh, 0);
}

inline GrMesh::PatternIterator end(const GrMesh& mesh) {
    return GrMesh::PatternIterator(mesh, mesh.fPatternRepeatCount);
}

#endif