OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef GrVertices_DEFINED | 8 #ifndef GrVertices_DEFINED |
9 #define GrVertices_DEFINED | 9 #define GrVertices_DEFINED |
10 | 10 |
11 #include "GrIndexBuffer.h" | 11 #include "GrIndexBuffer.h" |
12 #include "GrVertexBuffer.h" | 12 #include "GrVertexBuffer.h" |
13 | 13 |
| 14 class GrNonInstancedVertices { |
| 15 public: |
| 16 GrPrimitiveType primitiveType() const { return fPrimitiveType; } |
| 17 int startVertex() const { return fStartVertex; } |
| 18 int startIndex() const { return fStartIndex; } |
| 19 int vertexCount() const { return fVertexCount; } |
| 20 int indexCount() const { return fIndexCount; } |
| 21 bool isIndexed() const { return fIndexCount > 0; } |
| 22 |
| 23 const GrVertexBuffer* vertexBuffer() const { return fVertexBuffer.get(); } |
| 24 const GrIndexBuffer* indexBuffer() const { return fIndexBuffer.get(); } |
| 25 |
| 26 protected: |
| 27 GrPrimitiveType fPrimitiveType; |
| 28 int fStartVertex; |
| 29 int fStartIndex; |
| 30 int fVertexCount; |
| 31 int fIndexCount; |
| 32 GrPendingIOResource<const GrVertexBuffer, kRead_GrIOType> fVertexBuffer; |
| 33 GrPendingIOResource<const GrIndexBuffer, kRead_GrIOType> fIndexBuffer; |
| 34 friend class GrVertices; |
| 35 }; |
| 36 |
14 /** | 37 /** |
15 * Used to communicate index and vertex buffers, counts, and offsets for a draw
from GrBatch to | 38 * Used to communicate index and vertex buffers, counts, and offsets for a draw
from GrBatch to |
16 * GrGpu. It also holds the primitive type for the draw. TODO: Consider moving o
wnership of this | 39 * GrGpu. It also holds the primitive type for the draw. TODO: Consider moving o
wnership of this |
17 * and draw-issuing responsibility to GrPrimitiveProcessor. The rest of the vert
ex info lives there | 40 * and draw-issuing responsibility to GrPrimitiveProcessor. The rest of the vert
ex info lives there |
18 * already (stride, attribute mappings). | 41 * already (stride, attribute mappings). |
19 */ | 42 */ |
20 class GrVertices { | 43 class GrVertices : public GrNonInstancedVertices { |
21 public: | 44 public: |
22 GrVertices() {} | 45 GrVertices() {} |
23 GrVertices(const GrVertices& di) { (*this) = di; } | 46 GrVertices(const GrVertices& di) { (*this) = di; } |
24 GrVertices& operator =(const GrVertices& di); | 47 GrVertices& operator =(const GrVertices& di); |
25 | 48 |
26 void init(GrPrimitiveType primType, const GrVertexBuffer* vertexBuffer, int
startVertex, | 49 void init(GrPrimitiveType primType, const GrVertexBuffer* vertexBuffer, int
startVertex, |
27 int vertexCount) { | 50 int vertexCount) { |
28 SkASSERT(vertexBuffer); | 51 SkASSERT(vertexBuffer); |
29 SkASSERT(vertexCount); | 52 SkASSERT(vertexCount); |
30 SkASSERT(startVertex >= 0); | 53 SkASSERT(startVertex >= 0); |
31 fPrimitiveType = primType; | 54 fPrimitiveType = primType; |
32 fVertexBuffer.reset(vertexBuffer); | 55 fVertexBuffer.reset(vertexBuffer); |
33 fIndexBuffer.reset(NULL); | 56 fIndexBuffer.reset(NULL); |
34 fStartVertex = startVertex; | 57 fStartVertex = startVertex; |
35 fStartIndex = 0; | 58 fStartIndex = 0; |
36 fVertexCount = vertexCount; | 59 fVertexCount = vertexCount; |
37 fIndexCount = 0; | 60 fIndexCount = 0; |
38 fInstanceCount = 0; | 61 fInstanceCount = 0; |
39 fVerticesPerInstance = 0; | 62 fVerticesPerInstance = 0; |
40 fIndicesPerInstance = 0; | 63 fIndicesPerInstance = 0; |
| 64 fMaxInstancesPerDraw = 0; |
41 } | 65 } |
42 | 66 |
43 void initIndexed(GrPrimitiveType primType, | 67 void initIndexed(GrPrimitiveType primType, |
44 const GrVertexBuffer* vertexBuffer, | 68 const GrVertexBuffer* vertexBuffer, |
45 const GrIndexBuffer* indexBuffer, | 69 const GrIndexBuffer* indexBuffer, |
46 int startVertex, | 70 int startVertex, |
47 int startIndex, | 71 int startIndex, |
48 int vertexCount, | 72 int vertexCount, |
49 int indexCount) { | 73 int indexCount) { |
50 SkASSERT(indexBuffer); | 74 SkASSERT(indexBuffer); |
51 SkASSERT(vertexBuffer); | 75 SkASSERT(vertexBuffer); |
52 SkASSERT(indexCount); | 76 SkASSERT(indexCount); |
53 SkASSERT(vertexCount); | 77 SkASSERT(vertexCount); |
54 SkASSERT(startIndex >= 0); | 78 SkASSERT(startIndex >= 0); |
55 SkASSERT(startVertex >= 0); | 79 SkASSERT(startVertex >= 0); |
56 fPrimitiveType = primType; | 80 fPrimitiveType = primType; |
57 fVertexBuffer.reset(vertexBuffer); | 81 fVertexBuffer.reset(vertexBuffer); |
58 fIndexBuffer.reset(indexBuffer); | 82 fIndexBuffer.reset(indexBuffer); |
59 fStartVertex = startVertex; | 83 fStartVertex = startVertex; |
60 fStartIndex = startIndex; | 84 fStartIndex = startIndex; |
61 fVertexCount = vertexCount; | 85 fVertexCount = vertexCount; |
62 fIndexCount = indexCount; | 86 fIndexCount = indexCount; |
63 fInstanceCount = 0; | 87 fInstanceCount = 0; |
64 fVerticesPerInstance = 0; | 88 fVerticesPerInstance = 0; |
65 fIndicesPerInstance = 0; | 89 fIndicesPerInstance = 0; |
| 90 fMaxInstancesPerDraw = 0; |
66 } | 91 } |
67 | 92 |
| 93 |
| 94 /** Variation of the above that may be used when the total number of instanc
es may exceed |
| 95 the number of instances supported by the index buffer. To be used with |
| 96 nextInstances() to draw in max-sized batches.*/ |
68 void initInstanced(GrPrimitiveType primType, | 97 void initInstanced(GrPrimitiveType primType, |
69 const GrVertexBuffer* vertexBuffer, | 98 const GrVertexBuffer* vertexBuffer, |
70 const GrIndexBuffer* indexBuffer, | 99 const GrIndexBuffer* indexBuffer, |
71 int startVertex, | 100 int startVertex, |
72 int verticesPerInstance, | 101 int verticesPerInstance, |
73 int indicesPerInstance, | 102 int indicesPerInstance, |
74 int instanceCount) { | 103 int instanceCount, |
| 104 int maxInstancesPerDraw) { |
75 SkASSERT(vertexBuffer); | 105 SkASSERT(vertexBuffer); |
76 SkASSERT(indexBuffer); | 106 SkASSERT(indexBuffer); |
77 SkASSERT(instanceCount); | 107 SkASSERT(instanceCount); |
78 SkASSERT(verticesPerInstance); | 108 SkASSERT(verticesPerInstance); |
79 SkASSERT(indicesPerInstance); | 109 SkASSERT(indicesPerInstance); |
80 SkASSERT(startVertex >= 0); | 110 SkASSERT(startVertex >= 0); |
81 fPrimitiveType = primType; | 111 fPrimitiveType = primType; |
82 fVertexBuffer.reset(vertexBuffer); | 112 fVertexBuffer.reset(vertexBuffer); |
83 fIndexBuffer.reset(indexBuffer); | 113 fIndexBuffer.reset(indexBuffer); |
84 fStartVertex = startVertex; | 114 fStartVertex = startVertex; |
85 fStartIndex = 0; | 115 fStartIndex = 0; |
86 fVerticesPerInstance = verticesPerInstance; | 116 fVerticesPerInstance = verticesPerInstance; |
87 fIndicesPerInstance = indicesPerInstance; | 117 fIndicesPerInstance = indicesPerInstance; |
88 fInstanceCount = instanceCount; | 118 fInstanceCount = instanceCount; |
89 fVertexCount = instanceCount * fVerticesPerInstance; | 119 fVertexCount = instanceCount * fVerticesPerInstance; |
90 fIndexCount = instanceCount * fIndicesPerInstance; | 120 fIndexCount = instanceCount * fIndicesPerInstance; |
| 121 fMaxInstancesPerDraw = maxInstancesPerDraw; |
91 } | 122 } |
92 | 123 |
93 /** Variation of the above that may be used when the total number of instanc
es may exceed | |
94 the number of instances supported by the index buffer. To be used with | |
95 nextInstances() to draw in max-sized batches.*/ | |
96 void initInstanced(GrPrimitiveType primType, | |
97 const GrVertexBuffer* vertexBuffer, | |
98 const GrIndexBuffer* indexBuffer, | |
99 int startVertex, | |
100 int verticesPerInstance, | |
101 int indicesPerInstance, | |
102 int* instancesRemaining, | |
103 int maxInstancesPerDraw) { | |
104 int instanceCount = SkTMin(*instancesRemaining, maxInstancesPerDraw); | |
105 *instancesRemaining -= instanceCount; | |
106 this->initInstanced(primType, vertexBuffer, indexBuffer, startVertex, | |
107 verticesPerInstance, indicesPerInstance, instanceCou
nt); | |
108 } | |
109 | |
110 GrPrimitiveType primitiveType() const { return fPrimitiveType; } | |
111 int startVertex() const { return fStartVertex; } | |
112 int startIndex() const { return fStartIndex; } | |
113 int vertexCount() const { return fVertexCount; } | |
114 int indexCount() const { return fIndexCount; } | |
115 | 124 |
116 /** These return 0 if initInstanced was not used to initialize the GrVertice
s. */ | 125 /** These return 0 if initInstanced was not used to initialize the GrVertice
s. */ |
117 int verticesPerInstance() const { return fVerticesPerInstance; } | 126 int verticesPerInstance() const { return fVerticesPerInstance; } |
118 int indicesPerInstance() const { return fIndicesPerInstance; } | 127 int indicesPerInstance() const { return fIndicesPerInstance; } |
119 int instanceCount() const { return fInstanceCount; } | 128 int instanceCount() const { return fInstanceCount; } |
120 | 129 |
121 bool isIndexed() const { return fIndexCount > 0; } | |
122 bool isInstanced() const { return fInstanceCount > 0; } | 130 bool isInstanced() const { return fInstanceCount > 0; } |
123 | 131 |
124 /** Called after using this draw info to draw the next set of instances. | 132 class Iterator { |
125 The vertex offset is advanced while the index buffer is reused at the sa
me | 133 public: |
126 position. instancesRemaining is number of instances that remain, maxInst
ances is | 134 const GrNonInstancedVertices* init(const GrVertices& vertices) { |
127 the most number of instances that can be used with the index buffer. If
there | 135 fVertices = &vertices; |
128 are no instances remaining, the GrVertices is unmodified and false is re
turned.*/ | 136 if (vertices.fInstanceCount <= vertices.fMaxInstancesPerDraw) { |
129 bool nextInstances(int* instancesRemaining, int maxInstances) { | 137 fInstancesRemaining = 0; |
130 SkASSERT(this->isInstanced()); | 138 // Note, this also covers the non-instanced case! |
131 if (!*instancesRemaining) { | 139 return &vertices; |
132 return false; | 140 } |
| 141 SkASSERT(vertices.isInstanced()); |
| 142 fInstanceBatch.fIndexBuffer.reset(vertices.fIndexBuffer.get()); |
| 143 fInstanceBatch.fVertexBuffer.reset(vertices.fVertexBuffer.get()); |
| 144 fInstanceBatch.fIndexCount = vertices.fMaxInstancesPerDraw * |
| 145 vertices.fIndicesPerInstance; |
| 146 fInstanceBatch.fVertexCount = vertices.fMaxInstancesPerDraw * |
| 147 vertices.fVerticesPerInstance; |
| 148 fInstanceBatch.fPrimitiveType = vertices.fPrimitiveType; |
| 149 fInstanceBatch.fStartIndex = vertices.fStartIndex; |
| 150 fInstanceBatch.fStartVertex = vertices.fStartVertex; |
| 151 fInstancesRemaining = vertices.fInstanceCount - vertices.fMaxInstanc
esPerDraw; |
| 152 return &fInstanceBatch; |
133 } | 153 } |
134 fStartVertex += fVertexCount; | |
135 fInstanceCount = SkTMin(*instancesRemaining, maxInstances); | |
136 fVertexCount = fInstanceCount * fVerticesPerInstance; | |
137 fIndexCount = fInstanceCount * fIndicesPerInstance; | |
138 *instancesRemaining -= fInstanceCount; | |
139 return true; | |
140 } | |
141 | 154 |
142 const GrVertexBuffer* vertexBuffer() const { return fVertexBuffer.get(); } | 155 const GrNonInstancedVertices* next() { |
143 const GrIndexBuffer* indexBuffer() const { return fIndexBuffer.get(); } | 156 if (!fInstancesRemaining) { |
| 157 return NULL; |
| 158 } |
| 159 fInstanceBatch.fStartVertex += fInstanceBatch.fVertexCount; |
| 160 int instances = SkTMin(fInstancesRemaining, fVertices->fMaxInstances
PerDraw); |
| 161 fInstanceBatch.fIndexCount = instances * fVertices->fIndicesPerInsta
nce; |
| 162 fInstanceBatch.fVertexCount = instances * fVertices->fVerticesPerIns
tance; |
| 163 fInstancesRemaining -= instances; |
| 164 return &fInstanceBatch; |
| 165 } |
| 166 private: |
| 167 GrNonInstancedVertices fInstanceBatch; |
| 168 const GrVertices* fVertices; |
| 169 int fInstancesRemaining; |
| 170 }; |
144 | 171 |
145 private: | 172 private: |
146 GrPrimitiveType fPrimitiveType; | |
147 | |
148 int fStartVertex; | |
149 int fStartIndex; | |
150 int fVertexCount; | |
151 int fIndexCount; | |
152 | |
153 int fInstanceCount; | 173 int fInstanceCount; |
154 int fVerticesPerInstance; | 174 int fVerticesPerInstance; |
155 int fIndicesPerInstance; | 175 int fIndicesPerInstance; |
156 | 176 int fMaxInstancesPerDraw; |
157 GrPendingIOResource<const GrVertexBuffer, kRead_GrIOType> fVertexBuffer; | |
158 GrPendingIOResource<const GrIndexBuffer, kRead_GrIOType> fIndexBuffer; | |
159 }; | 177 }; |
160 | 178 |
161 #endif | 179 #endif |
OLD | NEW |