-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathPrimitiveBatchRenderer.cpp
More file actions
287 lines (278 loc) · 10.3 KB
/
PrimitiveBatchRenderer.cpp
File metadata and controls
287 lines (278 loc) · 10.3 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "common/PrimitiveBatchRenderer.hpp"
namespace core {
void PrimitiveBatchRenderer::onGraphicsDeviceCreate() {
// nothing
}
void PrimitiveBatchRenderer::onGraphicsDeviceDestroy() {
if (m_initialized && m_batch_scope) {
mapBuffers(false);
}
}
PrimitiveBatchRenderer::PrimitiveBatchRenderer() = default;
PrimitiveBatchRenderer::~PrimitiveBatchRenderer() {
if (m_initialized) {
m_device->removeEventListener(this);
}
}
bool PrimitiveBatchRenderer::createResources(IGraphicsDevice* const device) {
if (m_initialized && m_device) {
m_device->removeEventListener(this);
}
if (device == nullptr) {
assert(false); return false;
}
m_device = device;
const auto vertex_buffer_size = static_cast<uint32_t>(m_vertex_count * sizeof(DrawVertex));
if (!m_device->createVertexBuffer(vertex_buffer_size, sizeof(DrawVertex), m_vertex_buffer.put())) {
return false;
}
const auto index_buffer_size = static_cast<uint32_t>(m_index_count * sizeof(DrawIndex));
if (!m_device->createIndexBuffer(index_buffer_size, GraphicsFormat::r16_uint, m_index_buffer.put())) {
return false;
}
m_device->addEventListener(this);
m_initialized = true;
return true;
}
bool PrimitiveBatchRenderer::isBatch() const noexcept {
return m_batch_scope;
}
bool PrimitiveBatchRenderer::beginBatch(const bool auto_draw) {
if (m_batch_scope) {
assert(false); return false;
}
const auto cycle = m_cycle_on_next_batch
|| m_vertex_current >= m_vertex_count
|| m_index_current >= m_index_count;
if (!mapBuffers(cycle)) {
return false;
}
if (cycle) {
m_vertex_begin = 0;
m_vertex_current = 0;
m_index_begin = 0;
m_index_current = 0;
}
if (auto_draw) {
const auto cmd = m_device->getCommandbuffer();
cmd->bindVertexBuffer(0, m_vertex_buffer.get());
cmd->bindIndexBuffer(m_index_buffer.get());
}
m_batch_scope = true;
m_auto_draw = auto_draw;
m_cycle_on_next_batch = false;
return true;
}
bool PrimitiveBatchRenderer::endBatch() {
if (!m_batch_scope) {
assert(false); return false;
}
m_batch_scope = false;
if (!unmapBuffers()) {
return false;
}
if (m_auto_draw) {
drawOnly();
}
m_auto_draw = false;
return true;
}
bool PrimitiveBatchRenderer::consume() {
if (!m_batch_scope) {
assert(false); return false;
}
if (const auto index_count = m_index_current - m_index_begin; index_count == 0) {
return true; // skip
}
return flush(false);
}
bool PrimitiveBatchRenderer::addTriangle(const DrawVertex& v1, const DrawVertex& v2, const DrawVertex& v3) {
const DrawVertex vertices[3]{ v1, v2, v3 };
return addTriangle(vertices);
}
bool PrimitiveBatchRenderer::addTriangle(const DrawVertex vertices[3]) {
if (!m_batch_scope) {
assert(false); return false;
}
if (m_vertex_current + 3 >= m_vertex_count || m_index_current + 3 >= m_index_count) {
if (m_auto_draw) {
if (!flush(true)) {
return false;
}
}
else {
return false;
}
}
const DrawIndex indices[3]{
static_cast<DrawIndex>(m_vertex_current),
static_cast<DrawIndex>(m_vertex_current + 1),
static_cast<DrawIndex>(m_vertex_current + 2),
};
std::memcpy(m_vertex_pointer + m_vertex_current, vertices, sizeof(DrawVertex) * 3);
m_vertex_current += 3;
std::memcpy(m_index_pointer + m_index_current, indices, sizeof(indices));
m_index_current += 3;
return true;
}
bool PrimitiveBatchRenderer::addQuad(const DrawVertex& v1, const DrawVertex& v2, const DrawVertex& v3, const DrawVertex& v4) {
const DrawVertex vertices[4]{ v1, v2, v3, v4 };
return addQuad(vertices);
}
bool PrimitiveBatchRenderer::addQuad(const DrawVertex vertices[4]) {
if (!m_batch_scope) {
assert(false); return false;
}
if (m_vertex_current + 4 >= m_vertex_count || m_index_current + 6 >= m_index_count) {
if (m_auto_draw) {
if (!flush(true)) {
return false;
}
}
else {
return false;
}
}
const DrawIndex indices[6]{
static_cast<DrawIndex>(m_vertex_current),
static_cast<DrawIndex>(m_vertex_current + 1),
static_cast<DrawIndex>(m_vertex_current + 2),
static_cast<DrawIndex>(m_vertex_current + 2),
static_cast<DrawIndex>(m_vertex_current + 3),
static_cast<DrawIndex>(m_vertex_current),
};
std::memcpy(m_vertex_pointer + m_vertex_current, vertices, sizeof(DrawVertex) * 4);
m_vertex_current += 4;
std::memcpy(m_index_pointer + m_index_current, indices, sizeof(indices));
m_index_current += 6;
return true;
}
bool PrimitiveBatchRenderer::addRaw(const DrawVertex* const vertices, const size_t vertex_count, const DrawIndex* const indices, const size_t index_count) {
if (!m_batch_scope) {
assert(false); return false;
}
if (vertex_count > m_vertex_count || index_count > m_index_count) {
assert(false); return false;
}
if (vertices == nullptr || indices == nullptr) {
assert(false); return false;
}
if (m_vertex_current + vertex_count >= m_vertex_count || m_index_current + index_count >= m_index_count) {
if (m_auto_draw) {
if (!flush(true)) {
return false;
}
}
else {
return false;
}
}
const DrawIndex offset = static_cast<DrawIndex>(m_vertex_current);
if (index_count > 0 && index_count <= 256) {
DrawIndex modified_indices[256]; // uninitialize, ok
for (size_t i = 0; i < index_count; i += 1) {
modified_indices[i] = indices[i] + offset;
}
std::memcpy(m_index_pointer + m_index_current, modified_indices, sizeof(DrawIndex) * index_count);
m_index_current += index_count;
}
else if (index_count > 256) {
std::vector<DrawIndex> modified_indices(index_count);
for (size_t i = 0; i < index_count; i += 1) {
modified_indices[i] = indices[i] + offset;
}
std::memcpy(m_index_pointer + m_index_current, modified_indices.data(), sizeof(DrawIndex) * index_count);
m_index_current += index_count;
}
if (vertex_count > 0) {
std::memcpy(m_vertex_pointer + m_vertex_current, vertices, sizeof(DrawVertex) * vertex_count);
m_vertex_current += vertex_count;
}
return true;
}
bool PrimitiveBatchRenderer::addRequest(const size_t vertex_count, const size_t index_count, DrawVertex** const out_vertices, DrawIndex** const out_indices, size_t* const out_index_offset) {
if (!m_batch_scope) {
assert(false); return false;
}
if (vertex_count > m_vertex_count || index_count > m_index_count) {
assert(false); return false;
}
if (out_vertices == nullptr || out_indices == nullptr || out_index_offset == nullptr) {
assert(false); return false;
}
if (m_vertex_current + vertex_count >= m_vertex_count || m_index_current + index_count >= m_index_count) {
if (m_auto_draw) {
if (!flush(true)) {
return false;
}
}
else {
return false;
}
}
*out_index_offset = m_vertex_current;
*out_vertices = m_vertex_pointer + m_vertex_current;
m_vertex_current += vertex_count;
*out_indices = m_index_pointer + m_index_current;
m_index_current += index_count;
return true;
}
bool PrimitiveBatchRenderer::draw() {
if (m_batch_scope) {
assert(false); return false;
}
const auto cmd = m_device->getCommandbuffer();
cmd->bindVertexBuffer(0, m_vertex_buffer.get());
cmd->bindIndexBuffer(m_index_buffer.get());
drawOnly();
return true;
}
void PrimitiveBatchRenderer::discard() {
m_vertex_begin = m_vertex_current;
m_index_begin = m_index_current;
}
void PrimitiveBatchRenderer::setCycleOnNextBatch() {
m_cycle_on_next_batch = true;
}
bool PrimitiveBatchRenderer::mapBuffers(const bool cycle) {
if (!m_vertex_buffer->map(reinterpret_cast<void**>(&m_vertex_pointer), cycle)) {
assert(false); return false;
}
if (!m_index_buffer->map(reinterpret_cast<void**>(&m_index_pointer), cycle)) {
assert(false); return false;
}
return true;
}
bool PrimitiveBatchRenderer::unmapBuffers() {
if (!m_vertex_buffer->unmap()) {
assert(false); return false;
}
if (!m_index_buffer->unmap()) {
assert(false); return false;
}
m_vertex_pointer = nullptr;
m_index_pointer = nullptr;
return true;
}
bool PrimitiveBatchRenderer::flush(const bool cycle) {
if (!unmapBuffers()) {
return false;
}
drawOnly();
if (cycle) {
m_vertex_begin = 0;
m_vertex_current = 0;
m_index_begin = 0;
m_index_current = 0;
}
return mapBuffers(cycle);
}
void PrimitiveBatchRenderer::drawOnly() {
const auto cmd = m_device->getCommandbuffer();
if (const auto index_count = m_index_current - m_index_begin; index_count > 0) {
cmd->drawIndexed(static_cast<uint32_t>(index_count), static_cast<uint32_t>(m_index_begin), 0);
}
m_vertex_begin = m_vertex_current;
m_index_begin = m_index_current;
}
}