-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathCommandKeys.h
360 lines (310 loc) · 11.5 KB
/
CommandKeys.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
//
// CommandKeys.h
//
#pragma once
#include <cassert>
#include <cstdint>
#ifndef NDEBUG
#include "MemoryUtil.h"
#endif
namespace cb
{
enum class ViewLayerType {
e3D = 0,
eSkybox,
eHighest = eSkybox + 1
};
enum class TranslucencyType
{
eSubtractive = 0,
eAdditive,
eNormal,
eOpaque
};
/// Decoded material info.
struct MaterialId
{
inline MaterialId(uint32_t id, uint32_t pass)
: id(id)
, pass(pass)
{
}
/// Material id to bind.
uint32_t id;
/// The pass number of material.
uint32_t pass;
};
#pragma pack(push, 1)
///@brief The key class for draw commands used for sorting and decoding the commands.
struct DrawKey
{
static const uint64_t kOpaqueModeMask = 0x1F80000000000000;
static const uint64_t kOpaqueModeValue = 0x0B00000000000000;
static const uint32_t kPriorityBits = 7;
/// The transparent mode for 3d translucent geometry.
struct Transparent
{
/// The pass number of material.
uint64_t materialPass : 8;
/// Material id to bind.
uint64_t materialId : 23;
/// The depth order of a command, back to front.
uint64_t depth : 24;
/// Custom command bit, 0 for normal commands otherwise using Custom command mode.
uint64_t cmd : 1;
uint64_t: 3 + 3 + 2; // padding
};
/// The normal mode for opaque 3d geometry where the ordered of the material and depth is swapped.
/// @note The reason for the swapped depth is to minimize state changes ahead of early Z culling.
struct Opaque
{
/// The depth order of a command for opaque 3d geometry, front to back order.
uint64_t depth : 24;
/// The pass number of material.
uint64_t materialPass : 8;
/// Material id to bind.
uint64_t materialId : 23;
/// Custom command bit, 0 for normal commands otherwise using Custom command mode.
uint64_t cmd : 1;
uint64_t: 3 + 3 + 2; // padding
};
/// Custom command mode, can be use to add custom commands to switch a render target, clear buffers and
/// other.
struct Custom
{
/// Dummy bits, not used.
uint64_t dummy : 48;
/// The priority(sequence order) number of a custom command.
uint64_t priority : kPriorityBits;
/// Custom command bit, 0 for normal commands otherwise using custom Command mode.
uint64_t enabled : 1;
uint64_t: 3 + 3 + 2; // padding
};
union {
struct
{
uint64_t: 64 - (3 + 3 + 2); ; // padding
/// The translucency type of a command, @see cb::TranslucencyType
uint64_t translucency : 2;
/// The view layer type, @see cb::ViewLayerType
uint64_t viewLayer : 3;
/// The viewport id.
uint64_t viewportId : 3;
};
/// Transparent command settings for 3d geometry.
Transparent transparent;
/// Normal opaque command settings, same as previous but with material and depth swapped.
Opaque opaque;
/// Custom command settings.
Custom custom;
uint64_t value;
};
DrawKey();
explicit DrawKey(uint64_t value);
explicit DrawKey(cb::ViewLayerType viewLayer);
DrawKey(cb::ViewLayerType viewLayer, cb::TranslucencyType translucency);
void setView(uint32_t viewportId, cb::ViewLayerType viewLayer, cb::TranslucencyType translucency);
void setViewLayer(cb::ViewLayerType viewLayer, cb::TranslucencyType translucency);
void setMaterial(uint32_t materialId);
void setDepth(uint32_t depth);
void setMaterialDepth(uint32_t materialdId, uint32_t depth);
///@return The actual material based on the active mode, opaque or transparent.
cb::MaterialId material() const;
bool isOpaqueMode() const;
///@note Used for sorting commands based on their precedence order.
bool operator<(DrawKey other) const;
explicit operator uint64_t();
static DrawKey makeDefault(cb::ViewLayerType viewLayer);
static DrawKey makeDefault(uint32_t viewportId, cb::ViewLayerType viewLayer = ViewLayerType::e3D);
///@note The priority is inversed, i.e. lower values have highest priority.
static DrawKey makeCustom(cb::ViewLayerType viewLayer, uint32_t priority);
static DrawKey makeCustom(uint32_t viewportId, cb::ViewLayerType viewLayer, uint32_t priority);
static void sanityChecks();
}; // struct DrawKey
#pragma pack(pop)
////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline DrawKey::DrawKey()
{
static_assert(sizeof(DrawKey) == sizeof(uint64_t), "INVALID_DRAW_KEY_SIZE");
}
inline DrawKey::DrawKey(uint64_t value)
: value(value)
{
this->viewLayer = static_cast<uint32_t>(viewLayer);
}
inline DrawKey::DrawKey(cb::ViewLayerType viewLayer)
: value(0)
{
this->viewLayer = static_cast<uint32_t>(viewLayer);
}
inline DrawKey::DrawKey(cb::ViewLayerType viewLayer, cb::TranslucencyType translucency)
{
setViewLayer(viewLayer, translucency);
}
inline void DrawKey::setView(uint32_t viewportId, ViewLayerType viewLayer, cb::TranslucencyType translucency)
{
this->viewportId = viewportId;
this->viewLayer = static_cast<uint32_t>(viewLayer);
this->translucency = static_cast<uint32_t>(translucency);
}
inline void DrawKey::setViewLayer(ViewLayerType viewLayer, cb::TranslucencyType translucency)
{
this->viewLayer = static_cast<uint32_t>(viewLayer);
this->translucency = static_cast<uint32_t>(translucency);
}
inline void DrawKey::setMaterial(uint32_t materialId)
{
if (isOpaqueMode())
opaque.materialId = materialId;
else
// same memory layout as normal
transparent.materialId = materialId;
}
inline void DrawKey::setDepth(uint32_t depth)
{
if (isOpaqueMode())
opaque.depth = depth;
else
transparent.depth = depth;
}
inline void DrawKey::setMaterialDepth(uint32_t materialdId, uint32_t depth)
{
if (isOpaqueMode())
{
opaque.depth = depth;
opaque.materialId = materialdId;
}
else
{
transparent.depth = depth;
transparent.materialId = materialdId;
}
}
inline cb::MaterialId DrawKey::material() const
{
return isOpaqueMode() ? cb::MaterialId(opaque.materialId, opaque.materialPass)
: cb::MaterialId(transparent.materialId, transparent.materialPass);
}
inline bool DrawKey::isOpaqueMode() const
{
uint64_t keyValue = reinterpret_cast<const uint64_t&>(*this);
return (keyValue & kOpaqueModeMask) == kOpaqueModeValue;
}
inline DrawKey::operator uint64_t()
{
return value;
}
inline bool DrawKey::operator<(DrawKey other) const
{
return value > other.value;
}
inline DrawKey DrawKey::makeDefault(cb::ViewLayerType viewLayer)
{
DrawKey key(0);
key.viewLayer = static_cast<uint32_t>(viewLayer);
return key;
}
inline DrawKey DrawKey::makeDefault(uint32_t viewportId, cb::ViewLayerType viewLayer)
{
DrawKey key(0);
key.viewportId = viewportId;
key.viewLayer = static_cast<uint32_t>(viewLayer);
return key;
}
inline DrawKey DrawKey::makeCustom(cb::ViewLayerType viewLayer, uint32_t priority)
{
DrawKey key(viewLayer);
key.custom.enabled = true;
key.custom.priority = (2 << kPriorityBits) - 1 - priority;
return key;
}
inline DrawKey DrawKey::makeCustom(uint32_t viewportId, cb::ViewLayerType viewLayer, uint32_t priority)
{
DrawKey key(0);
key.viewportId = viewportId;
key.viewLayer = static_cast<uint32_t>(viewLayer);
key.custom.enabled = true;
key.custom.priority = (2 << kPriorityBits) - 1 - priority;
return key;
}
inline void DrawKey::sanityChecks()
{
// sanity checks for little-endian platforms
assert(cb::mem::isLittleEndian());
DrawKey drawKey;
drawKey.value = 0;
drawKey.viewportId = 3;
drawKey.viewLayer = 0x2;
drawKey.translucency = (uint32_t)TranslucencyType::eNormal; // = 0x2
drawKey.custom.enabled = 0;
drawKey.transparent.depth = 0x1234;
drawKey.transparent.materialId = 0x5678;
drawKey.transparent.materialPass = 5;
assert(drawKey.value == 0x6a00091A00567805);
// material and depth must be swapped
drawKey.opaque.depth = 0x1234;
drawKey.opaque.materialId = 0x5678;
drawKey.opaque.materialPass = 5;
assert(drawKey.value == 0x6a00567805001234);
// command mode check
drawKey.custom.enabled = 1;
drawKey.custom.priority = 9;
drawKey.custom.dummy = 0;
assert(drawKey.value == 0x6a89000000000000);
// masks for opaque mode checks
drawKey.value = 0;
drawKey.translucency = 0x3;
drawKey.viewLayer = 0x7;
drawKey.custom.enabled = 1;
assert(drawKey.value == DrawKey::kOpaqueModeMask);
drawKey.custom.enabled = 0;
drawKey.translucency = (uint32_t)TranslucencyType::eOpaque; // = 0x3
drawKey.viewLayer = 0x2;
assert(drawKey.value == DrawKey::kOpaqueModeValue);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
inline const char* toString(cb::ViewLayerType type)
{
switch (type)
{
case cb::ViewLayerType::e3D:
return "3D";
case cb::ViewLayerType::eSkybox:
return "Skybox";
case cb::ViewLayerType::eHighest:
return "Highest";
default:
assert(false);
return "Unknown";
}
}
inline const char* toString(cb::TranslucencyType type)
{
switch (type)
{
case cb::TranslucencyType::eSubtractive:
return "Subtractive";
case cb::TranslucencyType::eAdditive:
return "Additive";
case cb::TranslucencyType::eNormal:
return "Normal";
case cb::TranslucencyType::eOpaque:
return "Opaque";
default:
assert(false);
return "Unknown";
}
}
inline std::ostream& operator<<(std::ostream& stream, cb::DrawKey key)
{
stream << key.viewportId << ", layer: " << cb::toString(cb::ViewLayerType(key.viewLayer)) << "-"
<< cb::toString(cb::TranslucencyType(key.translucency)) << ", ";
if (key.custom.enabled)
stream << "custom command, priority: " << key.custom.priority;
else if (cb::TranslucencyType(key.translucency) != cb::TranslucencyType::eOpaque)
stream << ", depth: " << key.transparent.depth << ", material id: " << key.transparent.materialId;
else if (key.isOpaqueMode())
stream << ", depth: " << key.opaque.depth << ", material id: " << key.opaque.materialId;
return stream;
}
} // namespace cb