-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathCommandBuffer.h
493 lines (420 loc) · 18.2 KB
/
CommandBuffer.h
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//
// CommandBuffer.h
//
#pragma once
#include <algorithm>
#include <atomic>
#include <cstdint>
#include <functional>
#include <vector>
#include "CommandKeys.h"
#include "CommandPacket.h"
#include "LinearAllocator.h"
namespace cb
{
struct DefaultMaterialBinder
{
///@note Returns true if there are more passes to bind.
CB_FORCE_INLINE bool operator()(cb::MaterialId material) const
{
(void)(material);
return false;
}
#if CB_DEBUG_COMMANDS_PRINT
void debugMsg(cb::MaterialId) {}
#endif
};
struct DefaultKeyDecoder
{
/// note Each command key must define a decode function that will be used by the command buffer.
CB_FORCE_INLINE cb::MaterialId operator()(cb::DrawKey key) const
{
return key.material();
}
};
template<typename T>
struct DummyKeyDecoder
{
CB_FORCE_INLINE cb::MaterialId operator()(T) const
{
return cb::MaterialId(0, 0);
}
};
/// Utility to create global functions for commands with execute member method.
template<class CommandClass>
void makeExecuteFunction(const void* data, cb::RenderContext* rc)
{
auto& cmd = *reinterpret_cast<const CommandClass*>(data);
cmd.execute();
}
template <typename KeyType>
struct CommandPair
{
CommandPacket* cmd;
KeyType key;
CB_FORCE_INLINE bool operator<(const CommandPair& other) const
{
assert(cmd);
return key < other.key;
}
template<typename T>
explicit operator T()
{
return(T)key;
}
};
/// The command buffer is composed of more CommandPackets and their corresponding keys.
template <typename KeyType = cb::DrawKey, class KeyDecoderClass = DefaultKeyDecoder, class MaterialBinderClass = DefaultMaterialBinder>
class CommandBuffer
{
public:
typedef KeyType key_t;
typedef CommandPair<key_t> command_t;
typedef std::function<void(command_t*, command_t*)> sort_func_t;
typedef MaterialBinderClass binder_t;
typedef KeyDecoderClass decoder_t;
typedef int(*log_function_t)(const char* fmt, ...);
explicit CommandBuffer(uint32_t commandCount = 5000, uint32_t commandKBytes = 512);
explicit CommandBuffer(const MaterialBinderClass& materialBinder);
MaterialBinderClass& materialBinder();
const MaterialBinderClass& materialBinder() const;
/// Returns the count of the commands in the buffer.
/// @param countChainCommands - will also count chained commands
size_t count(bool countChainCommands = false) const;
/// Returns the consumed memory of the commands in the buffer, in bytes.
size_t allocations() const;
///@warning Should never resize when dispatching commands in progress, only before.
void resize(uint32_t commandCount, uint32_t commandKBs);
/// Sorts the created commands based on their key priority.
void sort(sort_func_t sortFunc = std::sort<command_t*>);
/// Submits the sorted commands to the GPU.
/// @param clearBuffer - clear all created commands from the buffer.
void submit(cb::RenderContext* rc, bool clearBuffer = true);
/// Creates a new command in the command buffer.
///@param auxilarySize Size of auxiliary memory required by the command.
///@tparam CommandClass must be a POD and must provide a kDispatchFunction member of
/// cb::RenderContext::function_t.
///@note Must use CommandPacket::getAuxilaryData to get the auxilary data pointer.
template <class CommandClass>
CommandClass* addCommand(const key_t& key, uint32_t auxilarySize = 0);
///@tparam AuxilaryData must be POD
///@note Also copies data from the given auxiliary data.
template <class CommandClass, typename AuxilaryData>
CommandClass* addCommandData(const key_t& key, const AuxilaryData& data);
template <class CommandClass, typename AuxilaryData>
CommandClass* addCommandData(const key_t& key, const std::vector<AuxilaryData>& data);
template <class CommandClass, typename AuxilaryData>
CommandClass* addCommandData(const key_t& key, const AuxilaryData* data, uint32_t count);
/// Adds the given command packet with the given key.
///@note Only references the original packet's auxiliary data.
cb::CommandPacket* addCommandFrom(const key_t& key, const cb::CommandPacket* referencePacket);
/// Adds a new command and append(chain) it to the given command.
///@tparam CommandClass must be a POD and must provide a kDispatchFunction member of
/// cb::RenderContext::function_t.
///@note Must use CommandPacket::getAuxilaryData to get the auxilary data pointer.
template <class CommandClass, class AppendCommandClass>
CommandClass* appendCommand(AppendCommandClass* cmd, uint32_t auxilarySize = 0);
template <class CommandClass>
CommandClass* appendCommand(cb::CommandPacket* cmd, uint32_t auxilarySize = 0);
///@tparam AuxilaryData must be POD
///@note Also copies data from the given auxiliary data.
template <class CommandClass, class AppendCommandClass, typename AuxilaryData>
CommandClass* appendCommandData(AppendCommandClass* cmd, const AuxilaryData& data);
template <class CommandClass, class AppendCommandClass, typename AuxilaryData>
CommandClass* appendCommandData(AppendCommandClass* prevCmd, const std::vector<AuxilaryData>& data);
template <class CommandClass, class AppendCommandClass, typename AuxilaryData>
CommandClass* appendCommandData(AppendCommandClass* prevCmd, const AuxilaryData* data, uint32_t count);
template <class CommandClass, typename AuxilaryData>
CommandClass* appendCommandData(cb::CommandPacket* cmd, const AuxilaryData& data);
///@tparam CommandClass must be a POD and must provide a kDispatchFunction member of
/// cb::RenderContext::function_t.
template <class CommandClass>
cb::CommandPacket* createCommandPacket(uint32_t auxilarySize = 0);
template <class CommandClass, typename AuxilaryData>
cb::CommandPacket* createCommandPacketData(const AuxilaryData& data);
#if CB_DEBUG_COMMANDS_PRINT
void setLogFunction(log_function_t logger) { m_logger = logger; }
#endif
private:
void clear();
private:
struct CommandPacketReference
{
CB_COMMAND_PACKET_ALIGN()
};
#if CB_COMMAND_PACKET_ALIGNED
static const uint32_t kALignment = alignof(CommandPacket);
#else
static const uint32_t kALignment = 0;
#endif
cb::LinearAllocator<kALignment> m_allocator;
MaterialBinderClass m_materialBinder;
std::vector<command_t> m_commands;
std::atomic<uint32_t> m_currentIndex;
#if CB_DEBUG_COMMANDS_PRINT
log_function_t m_logger = printf;
std::stringstream m_stringStream;
#endif
private:
CommandBuffer(const CommandBuffer&) = delete;
void operator=(const CommandBuffer&) = delete;
}; // class CommandBuffer
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define COMMAND_TEMPLATE template <typename KeyType, class KeyDecoderClass, class MaterialBinderClass>
#define COMMAND_QUAL CommandBuffer<KeyType, KeyDecoderClass, MaterialBinderClass>
COMMAND_TEMPLATE
COMMAND_QUAL::CommandBuffer(uint32_t commandCount, uint32_t commandKBytes)
: m_allocator(commandKBytes * 1024) // TODO: can use TLS to avoid false sharing
, m_materialBinder()
, m_currentIndex(0)
{
assert(m_currentIndex.is_lock_free());
m_commands.resize(commandCount);
}
COMMAND_TEMPLATE
COMMAND_QUAL::CommandBuffer(const MaterialBinderClass& materialBinder)
: m_allocator(kDefaultCommandKBs * 1024) // TODO: can use TLS to avoid false sharing
, m_materialBinder(materialBinder)
, m_currentIndex(0)
{
assert(m_currentIndex.is_lock_free());
m_commands.resize(kDefaultCommandCount);
}
COMMAND_TEMPLATE
MaterialBinderClass& COMMAND_QUAL::materialBinder()
{
return m_materialBinder;
}
COMMAND_TEMPLATE
const MaterialBinderClass& COMMAND_QUAL::materialBinder() const
{
return m_materialBinder;
}
COMMAND_TEMPLATE
size_t COMMAND_QUAL::count(bool countChainCommands /*= false*/) const
{
if (!countChainCommands)
return m_currentIndex;
size_t res = 0;
const auto end = m_commands.begin() + (int)m_currentIndex.load(std::memory_order_acquire);
for (auto it = m_commands.begin(); it != end; ++it)
{
const CommandPacket* packet = it->cmd;
do
{
++res;
packet = packet->nextCommand;
} while (packet != NULL);
}
return res;
}
COMMAND_TEMPLATE
size_t COMMAND_QUAL::allocations() const
{
return m_allocator.size();
}
COMMAND_TEMPLATE
void COMMAND_QUAL::resize(uint32_t commandCount, uint32_t commandKBs)
{
assert(m_currentIndex == 0);
m_commands.resize(commandCount);
m_allocator.resize(commandKBs * 1024);
}
COMMAND_TEMPLATE
void COMMAND_QUAL::submit(cb::RenderContext* rc, bool clearBuffer /*= true*/)
{
// dispatches commands
#if CB_DEBUG_COMMANDS_PRINT
(*m_logger)("\n\n++++ Submit ++++\n\n");
#endif
const auto end = m_commands.begin() + (int)m_currentIndex.load(std::memory_order_acquire);
for (auto it = m_commands.begin(); it != end; ++it)
{
const key_t& key = it->key;
// decode material id from key
cb::MaterialId material = KeyDecoderClass()(key);
#if CB_DEBUG_COMMANDS_PRINT
m_stringStream.str(std::string());
m_stringStream.clear();
m_stringStream << key;
(*m_logger)("%s\n", m_stringStream.str().c_str());
#endif
const CommandPacket* packet = it->cmd;
// apply the material dispatch commands
bool nextPass;
do
{
// if true then we have more passes presents in the material
nextPass = m_materialBinder(material);
#if CB_DEBUG_COMMANDS_PRINT
m_materialBinder.debugMsg(material);
CommandPacket::log(packet, *m_logger);
#endif
CommandPacket::dispatch(packet, rc);
++material.pass;
} while (nextPass);
}
// safe to dealloc all
if (clearBuffer)
clear();
}
COMMAND_TEMPLATE
template <class CommandClass>
CommandClass* COMMAND_QUAL::addCommand(const key_t& key, uint32_t auxilarySize)
{
CommandPacket* packet = CommandPacket::create<CommandClass>(m_allocator, auxilarySize);
// store the key and command packet ptr
{
const uint32_t currentIndex = m_currentIndex.fetch_add(1, std::memory_order_relaxed);
assert(currentIndex < (uint32_t)m_commands.size());
command_t& pair = m_commands[currentIndex];
pair.cmd = packet;
pair.key = key;
}
packet->dispatchFunction = CommandClass::kDispatchFunction;
assert(packet->dispatchFunction);
return CommandPacket::getCommandData<CommandClass>(packet);
}
COMMAND_TEMPLATE
template <class CommandClass, typename AuxilaryData>
CommandClass* COMMAND_QUAL::addCommandData(const key_t& key, const AuxilaryData& data)
{
return addCommandData<CommandClass>(key, &data, 1);
}
COMMAND_TEMPLATE
template <class CommandClass, typename AuxilaryData>
CommandClass* COMMAND_QUAL::addCommandData(const key_t& key, const std::vector<AuxilaryData>& data)
{
return addCommandData<CommandClass>(key, data.data(), data.size());
}
COMMAND_TEMPLATE
template <class CommandClass, typename AuxilaryData>
CommandClass* COMMAND_QUAL::addCommandData(const key_t& key, const AuxilaryData* data, uint32_t count)
{
static_assert(cb::detail::is_pod<AuxilaryData>::value, "AUXILARY_DATA_INVALID_TYPE");
const uint32_t size = sizeof(AuxilaryData) * count;
CommandClass* cmd = addCommand<CommandClass>(key, size);
CommandPacket* packet = CommandPacket::getCommandPacket<CommandClass>(cmd);
memcpy(packet->auxilaryData, data, size);
// enforce that the command class has data and size members
cmd->data = packet->auxilaryData;
cmd->size = size;
return cmd;
}
COMMAND_TEMPLATE
cb::CommandPacket* COMMAND_QUAL::addCommandFrom(const key_t& key, const cb::CommandPacket* referencePacket)
{
assert(referencePacket->nextCommand == NULL);
CommandPacket* packet = CommandPacket::create<CommandPacketReference>(m_allocator, 0);
// store the key and command packet ptr
{
const uint32_t currentIndex = m_currentIndex.fetch_add(1, boost::memory_order_relaxed);
assert(m_commands.size() > currentIndex);
assert(currentIndex < (uint32_t)m_commands.size());
CommandPair& pair = m_commands[currentIndex];
pair.cmd = packet;
pair.key = key;
}
// reference the packet
*packet = *referencePacket;
assert(packet->nextCommand == NULL);
assert(packet->dispatchFunction);
return packet;
}
COMMAND_TEMPLATE
template <class CommandClass>
CommandClass* COMMAND_QUAL::appendCommand(cb::CommandPacket* prevPacket, uint32_t auxilarySize)
{
CommandPacket* packet = CommandPacket::create<CommandClass>(m_allocator, auxilarySize);
packet->dispatchFunction = CommandClass::kDispatchFunction;
assert(packet->dispatchFunction);
assert(prevPacket->nextCommand == NULL);
prevPacket->nextCommand = packet;
return CommandPacket::getCommandData<CommandClass>(packet);
}
COMMAND_TEMPLATE
template <class CommandClass, class AppendCommandClass>
CommandClass* COMMAND_QUAL::appendCommand(AppendCommandClass* prevCmd, uint32_t auxilarySize)
{
CommandPacket* packet = CommandPacket::create<CommandClass>(m_allocator, auxilarySize);
packet->dispatchFunction = CommandClass::kDispatchFunction;
assert(packet->dispatchFunction);
CommandPacket* prevPacket = CommandPacket::getCommandPacket<AppendCommandClass>(prevCmd);
assert(prevPacket->nextCommand == NULL);
prevPacket->nextCommand = packet;
return CommandPacket::getCommandData<CommandClass>(packet);
}
COMMAND_TEMPLATE
template <class CommandClass, class AppendCommandClass, typename AuxilaryData>
CommandClass* COMMAND_QUAL::appendCommandData(AppendCommandClass* prevCmd, const AuxilaryData& data)
{
return appendCommandData<CommandClass>(prevCmd, &data, 1);
}
COMMAND_TEMPLATE
template <class CommandClass, class AppendCommandClass, typename AuxilaryData>
CommandClass* COMMAND_QUAL::appendCommandData(AppendCommandClass* prevCmd, const std::vector<AuxilaryData>& data)
{
return appendCommandData<CommandClass>(prevCmd, data.data(), data.size());
}
COMMAND_TEMPLATE
template <class CommandClass, class AppendCommandClass, typename AuxilaryData>
CommandClass* COMMAND_QUAL::appendCommandData(AppendCommandClass* prevCmd, const AuxilaryData* data, uint32_t count)
{
static_assert(cb::detail::is_pod<AuxilaryData>::value, "AUXILARY_DATA_INVALID_TYPE");
const uint32_t size = sizeof(AuxilaryData) * count;
CommandClass* cmd = appendCommand<CommandClass>(prevCmd, size);
CommandPacket* packet = CommandPacket::getCommandPacket<CommandClass>(cmd);
memcpy(packet->auxilaryData, data, size);
// enforce that the command class has data and size members
cmd->data = packet->auxilaryData;
cmd->size = size;
return cmd;
}
COMMAND_TEMPLATE
template <class CommandClass, typename AuxilaryData>
CommandClass* COMMAND_QUAL::appendCommandData(cb::CommandPacket* prevPacket, const AuxilaryData& data)
{
CommandClass* cmd = appendCommand<CommandClass>(prevPacket, sizeof(AuxilaryData));
CommandPacket* packet = CommandPacket::getCommandPacket<CommandClass>(cmd);
memcpy(packet->auxilaryData, &data, sizeof(AuxilaryData));
// enforce that the command class has data and size members
cmd->data = packet->auxilaryData;
cmd->size = sizeof(AuxilaryData);
return cmd;
}
COMMAND_TEMPLATE
template <class CommandClass>
cb::CommandPacket* COMMAND_QUAL::createCommandPacket(uint32_t auxilarySize /*= 0*/)
{
CommandPacket* packet = CommandPacket::create<CommandClass>(m_allocator, auxilarySize);
packet->dispatchFunction = CommandClass::kDispatchFunction;
assert(packet->dispatchFunction);
return packet;
}
COMMAND_TEMPLATE
template <class CommandClass, typename AuxilaryData>
cb::CommandPacket* COMMAND_QUAL::createCommandPacketData(const AuxilaryData& data)
{
CommandPacket* packet = createCommandPacket<CommandClass>(sizeof(AuxilaryData));
memcpy(packet->auxilaryData, &data, sizeof(AuxilaryData));
CommandClass* cmd = CommandPacket::getCommandData<CommandClass>(packet);
// enforce that the command class has data and size members
cmd->data = packet->auxilaryData;
cmd->size = sizeof(AuxilaryData);
return packet;
}
COMMAND_TEMPLATE
void COMMAND_QUAL::sort(sort_func_t sortFunc /*= std::sort<CommandPair*>*/)
{
sortFunc(m_commands.data(), m_commands.data() + (int)m_currentIndex.load(std::memory_order_acquire));
assert(m_commands.size() > m_currentIndex.load(std::memory_order_acquire));
}
COMMAND_TEMPLATE
void COMMAND_QUAL::clear()
{
m_allocator.deallocAll();
m_currentIndex = 0;
}
#undef COMMAND_TEMPLATE
#undef COMMAND_QUAL
} // namespace cb