Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
DrawList.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
3#include "./DrawList.hpp"
4#include "./BatchVertex.hpp"
9#include "../../core/Hash.hpp"
10
11#include <glm/glm.hpp>
12#include <algorithm>
13#include <cstring>
14
15namespace esengine {
16
17namespace {
19constexpr u32 NO_EXPECTED_OFFSET = 0xFFFFFFFFu;
20
21static_assert((1u << BATCH_SLOT_BITS) == MAX_CMD_TEXTURE_SLOTS);
22
23void rewriteTexIndex(TransientBufferPool& pool, const DrawCommand& cmd, i32 slot) {
24 auto* verts = reinterpret_cast<BatchVertex*>(pool.vertexData(LayoutId::Batch) + cmd.vertex_byte_offset);
25 for (u32 k = 0; k < cmd.vertex_count; ++k) verts[k].texIndex = withTextureSlot(verts[k].texIndex, slot);
26}
27
34bool relocateInstance(TransientBufferPool& pool, DrawCommand& cmd, u32 expectedAt) {
35 // Every record of the draw: one for an entity, as many as it has instances for a set of placements.
36 const u32 bytes = cmd.instance_stride * std::max(cmd.instance_count, 1u);
37 const u32 at = pool.allocVertices(LayoutId::MeshInstance, bytes);
38 if (expectedAt != NO_EXPECTED_OFFSET && at != expectedAt) return false;
39 // After the alloc: growing the staging can move it.
40 u8* base = pool.vertexData(LayoutId::MeshInstance);
41 std::memmove(base + at, base + cmd.vertex_byte_offset, bytes);
42 cmd.vertex_byte_offset = at;
43 return true;
44}
45
46bool joinBatchIndexRanges(TransientBufferPool& pool, DrawCommand& head, DrawCommand& next) {
47 const u32 used = pool.indicesUsed(LayoutId::Batch);
48 const u32 vertexBytes = pool.vertexBytesUsed(LayoutId::Batch);
49 const u32 stride = pool.vertexStride(LayoutId::Batch);
50 if (stride == 0) return false;
51 const auto owned = [&](const DrawCommand& cmd) {
52 return cmd.vertex_buffer == BufferHandle::Invalid && cmd.index_buffer == BufferHandle::Invalid
53 && cmd.index_count != 0 && cmd.index_offset <= used && cmd.index_count <= used - cmd.index_offset
54 && cmd.vertex_byte_offset <= vertexBytes
55 && cmd.vertex_count <= (vertexBytes - cmd.vertex_byte_offset) / stride;
56 };
57 if (!owned(head) || !owned(next) || head.depth_bias != next.depth_bias
58 || head.topology != next.topology) return false;
59 u32 at;
60 if (head.index_offset + head.index_count != used) {
61 if (!pool.copyIndexRange(LayoutId::Batch, head.index_offset, head.index_count, at)) return false;
62 head.index_offset = at;
63 }
64 if (!pool.copyIndexRange(LayoutId::Batch, next.index_offset, next.index_count, at)) return false;
65 next.index_offset = at;
66 return head.index_offset + head.index_count == next.index_offset;
67}
68} // namespace
69
71 commands_.clear();
72 entity_counts_.fill(0);
73 skin_matrices_.clear();
74 morph_shapes_.clear();
75 probes_.clear();
76 planar_.clear();
77 probe_slots_.clear();
78 run_entities_.clear();
79 run_first_.clear();
80 merged_draw_calls_ = 0;
81 skipped_for_program_ = 0;
82 depth_required_ = false;
83 ui_stencil_required_ = false;
84}
85
86u32 DrawList::addSkinMatrices(const glm::mat4* matrices, u32 count) {
87 const u32 at = static_cast<u32>(skin_matrices_.size());
88 skin_matrices_.insert(skin_matrices_.end(), matrices, matrices + count);
89 return at;
90}
91
93 morph_shapes_.push_back(shapes);
94 return static_cast<u32>(morph_shapes_.size());
95}
96
98 probes_.push_back(probe);
99 return static_cast<u32>(probes_.size());
100}
101
102void DrawList::push(const DrawCommand& cmd) {
103 // Accumulated where commands ENTER, not scanned where the answer is wanted:
104 // push is the one funnel every render path goes through, so a path added
105 // later cannot produce a depth draw this fails to count. See needsDepth.
106 depth_required_ = depth_required_ || cmd.depth_test || cmd.depth_write;
107 const auto type = static_cast<u32>(cmd.type);
108 if (type < entity_counts_.size()) entity_counts_[type] += cmd.entity_count;
109 commands_.push_back(cmd);
110 ui_stencil_required_ = ui_stencil_required_ || (cmd.state_flags & CMD_STATE_UI_STENCIL);
111}
112
114 u32 count = static_cast<u32>(commands_.size());
115 if (count == 0) {
116 merged_draw_calls_ = 0;
117 merged_away_ = 0;
118 run_length_.clear();
119 entry_texel_.clear();
120 std::fill(std::begin(breaks_), std::end(breaks_), 0u);
121 return;
122 }
123
124 // The order the keys give is the order the last frame's identical keys gave: the sort below is a function of the key sequence alone, so a
125 // frame whose keys match is not sorted again. Sprites that move rarely change their key (2D keys hold layer, shader, blend and material, not
126 // where a sprite is), which makes this the common frame.
127 bool sameKeys = sorted_keys_.size() == count && sort_entries_.size() == count;
128 for (u32 i = 0; sameKeys && i < count; ++i) sameKeys = sorted_keys_[i] == commands_[i].sort_key;
129 ES_PROFILE_COUNTER("batch.sortReused", sameKeys ? 1 : 0);
130 if (!sameKeys) {
131 sort_entries_.resize(count);
132 sorted_keys_.resize(count);
133 for (u32 i = 0; i < count; ++i) {
134 sort_entries_[i] = { commands_[i].sort_key, i };
135 sorted_keys_[i] = commands_[i].sort_key;
136 }
137
138 // Tie-break on emit index so same-key commands keep submission order: that keeps an
139 // emit-contiguous run contiguous after sorting, which is what lets the merge coalesce it
140 // (including across different textures, now that texture is out of the key).
141 std::sort(sort_entries_.begin(), sort_entries_.end(),
142 [](const SortEntry& a, const SortEntry& b) {
143 return a.key < b.key || (a.key == b.key && a.index < b.index);
144 });
145 }
146
147 // Gather into a reused scratch buffer (not a fresh per-frame vector) and swap
148 // it with commands_, so the allocation is amortized across frames.
149 sorted_scratch_.resize(count);
150 for (u32 i = 0; i < count; ++i) {
151 sorted_scratch_[i] = commands_[sort_entries_[i].index];
152 }
153 commands_.swap(sorted_scratch_);
154
155 // Sorted UI order is a preorder walk. Reuse each mask's exact geometry to
156 // restore its parent when the walk leaves that subtree; no new vertices,
157 // rectangular approximation, or per-depth texture is needed.
158 if (ui_stencil_required_) {
159 stencil_commands_scratch_.clear();
160 stencil_stack_scratch_.clear();
161 stencil_level_first_.clear();
162 const auto leaveMask = [&]() {
163 const u32 first = stencil_level_first_.back();
164 stencil_level_first_.pop_back();
165 while (stencil_stack_scratch_.size() > first) {
166 stencil_commands_scratch_.push_back(stencil_stack_scratch_.back());
167 auto& restore = stencil_commands_scratch_.back();
168 stencil_stack_scratch_.pop_back();
169 restore.state_flags |= restore.stencil_depth == 1
171 restore.entity_count = 0;
172 restore.entity = INVALID_ENTITY;
173 restore.depth_test = false;
174 restore.depth_write = false;
175 }
176 };
177 for (const DrawCommand& source : commands_) {
178 const bool ui = (source.state_flags & CMD_STATE_UI_STENCIL) != 0;
179 const bool mask = ui && (source.state_flags & CMD_STATE_STENCIL_WRITE);
180 const bool continuation = mask && !stencil_stack_scratch_.empty()
181 && stencil_stack_scratch_.back().entity == source.entity
182 && stencil_stack_scratch_.back().stencil_depth == source.stencil_depth;
183 const u32 wanted = ui ? source.stencil_depth - (mask && !continuation ? 1 : 0) : 0;
184 while (stencil_level_first_.size() > wanted) leaveMask();
185 // A culled or absent mask cannot license a child's draw against stale
186 // attachment values. A later root can still enter normally.
187 if (ui && stencil_level_first_.size() != wanted) continue;
188 stencil_commands_scratch_.push_back(source);
189 auto& command = stencil_commands_scratch_.back();
190 if (mask) {
191 if (!continuation) {
192 stencil_level_first_.push_back(static_cast<u32>(stencil_stack_scratch_.size()));
193 }
194 stencil_stack_scratch_.push_back(command);
195 command.depth_test = false;
196 command.depth_write = false;
197 if (command.stencil_depth > 1) {
199 --command.stencil_ref;
200 }
201 }
202 }
203 while (!stencil_level_first_.empty()) leaveMask();
204 commands_.swap(stencil_commands_scratch_);
205 count = static_cast<u32>(commands_.size());
206 }
207
208 merged_draw_calls_ = 0;
209 run_entities_.clear();
210 run_first_.clear();
211 u32 writeIdx = 0;
212 u32 breaks[static_cast<u32>(BatchBreak::Count)] = {};
213
214 for (u32 i = 0; i < count; ++i) {
215 bool didMerge = false;
217 if (writeIdx > 0) {
218 DrawCommand& head = commands_[writeIdx - 1];
219 blocker = head.mergeBlocker(commands_[i]);
220 // Reject a new sampler before moving indices: a full run cannot
221 // use that copy. Existing samplers still fit at the slot ceiling.
222 if ((blocker == BatchBreak::None || blocker == BatchBreak::IndexGap)
223 && head.layout_id == LayoutId::Batch && commands_[i].texture_count >= 1
224 && head.texture_count >= head.textureSlotBudget(mask_2d_slots_)) {
225 const auto* begin = head.texture_ids;
226 const auto* end = begin + head.texture_count;
227 if (std::find(begin, end, commands_[i].texture_ids[0]) == end) {
228 blocker = BatchBreak::TextureSlots;
229 }
230 }
231 if (blocker == BatchBreak::IndexGap && head.layout_id == LayoutId::Batch
232 && joinBatchIndexRanges(pool, head, commands_[i])) {
233 blocker = head.mergeBlocker(commands_[i]);
234 }
235 if (blocker == BatchBreak::None && head.instance_count != 0) {
236 // One more instance of geometry already being drawn. The head's
237 // own record moves first, so the run has somewhere to grow.
238 const u32 stride = head.instance_stride;
239 bool ok = head.instances_relocated
240 || relocateInstance(pool, head, NO_EXPECTED_OFFSET);
241 if (ok) {
242 ok = relocateInstance(pool, commands_[i],
243 head.vertex_byte_offset + head.instance_count * stride);
244 }
245 if (ok) {
246 head.instances_relocated = true;
247 head.instance_count += commands_[i].instance_count;
248 head.entity_count += commands_[i].entity_count;
249 // One more instance in this run, and its own irradiance with
250 // it: the merge is the last place an instance's probe and its
251 // position in the run are both known.
252 probe_slots_.insert(probe_slots_.end(), std::max(commands_[i].instance_count, 1u), commands_[i].probe_index);
253 didMerge = true;
254 } else {
255 blocker = BatchBreak::Instanced;
256 }
257 } else if (blocker == BatchBreak::None) {
258 if (head.layout_id == LayoutId::Batch && commands_[i].texture_count >= 1) {
259 // Multi-texture: give this command's texture a slot in the head's set
260 // (or bail to a new draw if all 8 slots are taken), then stamp its verts.
261 const u8 slotBudget = head.textureSlotBudget(mask_2d_slots_);
262 i32 slot = head.addTextureSlot(commands_[i].texture_ids[0], slotBudget);
263 if (slot >= 0) {
264 // Staging verts default to texIndex 0, so only a non-zero slot
265 // needs the per-vertex rewrite; a same-texture merge reuses
266 // slot 0 and is already correct.
267 if (slot != 0) rewriteTexIndex(pool, commands_[i], slot);
268 head.index_count += commands_[i].index_count;
269 head.entity_count += commands_[i].entity_count;
270 didMerge = true;
271 } else {
272 blocker = BatchBreak::TextureSlots;
273 }
274 } else {
275 head.index_count += commands_[i].index_count;
276 head.entity_count += commands_[i].entity_count;
277 didMerge = true;
278 }
279 }
280 }
281 if (!didMerge) {
282 if (writeIdx != i) {
283 commands_[writeIdx] = commands_[i];
284 }
285 run_first_.push_back(static_cast<u32>(run_entities_.size()));
286 if (commands_[writeIdx].instance_count != 0) {
287 commands_[writeIdx].probe_base = static_cast<u32>(probe_slots_.size());
288 probe_slots_.insert(probe_slots_.end(), commands_[writeIdx].instance_count, commands_[writeIdx].probe_index);
289 }
290 commands_[writeIdx].break_reason = blocker;
291 ++breaks[static_cast<u32>(blocker)];
292 // The run head owns slot 0; staging verts already default to texIndex 0,
293 // so it needs no rewrite (this is the common single-texture case).
294 ++writeIdx;
295 }
296 if (commands_[i].entity != INVALID_ENTITY) run_entities_.push_back(commands_[i].entity);
297 }
298 commands_.resize(writeIdx);
299 merged_draw_calls_ = writeIdx;
300
301 merged_away_ = count - merged_draw_calls_;
302 std::copy(std::begin(breaks), std::end(breaks), std::begin(breaks_));
303 // Before the pool uploads: the entries are texels of the same record texture the records are.
304 writeEntries(pool);
305}
306
307std::span<const Entity> DrawList::runEntities(u32 index) const {
308 if (index >= run_first_.size()) return {};
309 const u32 first = run_first_[index];
310 const u32 end = index + 1 < run_first_.size() ? run_first_[index + 1]
311 : static_cast<u32>(run_entities_.size());
312 return {run_entities_.data() + first, end - first};
313}
314
315bool DrawList::readsRecords(const DrawCommand& d) {
316 return d.hasPersistentGeometry() && d.instance_count != 0;
317}
318
319bool DrawList::joinsRun(const DrawCommand& p, const DrawCommand& c) {
320 // A mesh drawn through its records, with nothing of its own that lives in a block but its records and its light: a pose, shapes
321 // and a mirror each are a block per draw. In one call the draws are still drawn in order, so blended ones may share one too.
322 const auto runnable = [](const DrawCommand& d) {
324 && d.layout_id == LayoutId::MeshInstance && d.skin_count == 0 && d.morph_index == 0 && d.planar_index == 0;
325 };
326 if (!runnable(p) || !runnable(c)) return false;
327 // Everything the draw binds or sets, the same: the pipeline's every input, its textures, its material, its dynamic state.
328 return p.vertex_buffer == c.vertex_buffer && p.index_buffer == c.index_buffer && p.vertex_layout == c.vertex_layout
329 && p.shader_id == c.shader_id && p.depth_shader_id == c.depth_shader_id && p.material_id == c.material_id
330 && p.blend_mode == c.blend_mode && p.depth_test == c.depth_test && p.depth_write == c.depth_write && p.cull == c.cull
331 && p.depth_bias == c.depth_bias && p.topology == c.topology && p.stage == c.stage && p.state_flags == c.state_flags
332 && (!(p.state_flags & CMD_STATE_SCISSOR) || p.scissor == c.scissor) && p.stencil_ref == c.stencil_ref
333 && p.texture_count == c.texture_count && std::equal(p.texture_ids, p.texture_ids + p.texture_count, c.texture_ids);
334}
335
336bool DrawList::probeLitAt(const DrawCommand& cmd) const {
337 const u32 n = std::min(cmd.instance_count > 0 ? cmd.instance_count : 1u, PROBE_MAX_INSTANCES);
338 for (u32 k = 0; k < n; ++k) {
339 const size_t at = static_cast<size_t>(cmd.probe_base) + k;
340 if (at < probe_slots_.size() && probe_slots_[at] != 0) return true;
341 }
342 return false;
343}
344
345void DrawList::writeEntries(TransientBufferPool& pool) {
346 run_length_.assign(merged_draw_calls_, 1u);
347 entry_texel_.assign(merged_draw_calls_, 0u);
348 // The runs: draws that differ only by their geometry's range and their records, drawn by one call where the device can. Their
349 // instances share the run's probe block, so a run that carries light holds no more instances than the block does. Decided here,
350 // with the entries, because an entry says where its draw's instances start in that block.
351 if (pool.device().supportsMultiDraw()) {
352 u32 head = 0, instances = 0;
353 bool lit = false;
354 for (u32 i = 0; i < merged_draw_calls_; ++i) {
355 const DrawCommand& cmd = commands_[i];
356 const bool cmdLit = probeLitAt(cmd);
357 if (i > 0 && run_length_[head] < MULTI_DRAW_MAX && joinsRun(commands_[i - 1], cmd)
358 && !((lit || cmdLit) && instances + cmd.instance_count > PROBE_MAX_INSTANCES)) {
359 ++run_length_[head];
360 run_length_[i] = 0;
361 instances += cmd.instance_count;
362 lit = lit || cmdLit;
363 continue;
364 }
365 head = i;
366 instances = cmd.instance_count;
367 lit = cmdLit;
368 }
369 }
370
371 // One texel per draw that reads records, in draw order so a run's entries follow its head's; then each morphed draw's shapes.
372 u32 entries = 0, shapeTexels = 0;
373 for (u32 i = 0; i < merged_draw_calls_; ++i) {
374 const DrawCommand& cmd = commands_[i];
375 if (!readsRecords(cmd)) continue;
376 ++entries;
377 if (cmd.morph_index > 0) shapeTexels += 1u + static_cast<u32>(morph_shapes_[cmd.morph_index - 1].info.x);
378 }
379 if (entries == 0) return;
380 // Whole records, so the records written after these still start on one.
381 const u32 bytes = (entries + shapeTexels) * 16u;
382 const u32 padded = (bytes + MESH_INSTANCE_STRIDE - 1) / MESH_INSTANCE_STRIDE * MESH_INSTANCE_STRIDE;
383 const u32 offset = pool.allocVertices(LayoutId::MeshInstance, padded);
384 u8* base = pool.vertexData(LayoutId::MeshInstance);
385 if (!base) return;
386 std::memset(base + offset, 0, padded);
387 auto* texels = reinterpret_cast<glm::vec4*>(base + offset);
388 const u32 first = offset / 16u;
389 u32 entry = 0, shapes = entries;
390 for (u32 i = 0; i < merged_draw_calls_;) {
391 const u32 length = std::max(run_length_[i], 1u);
392 u32 runInstance = 0;
393 for (u32 k = 0; k < length; ++k) {
394 const DrawCommand& cmd = commands_[i + k];
395 if (!readsRecords(cmd)) continue;
396 entry_texel_[i + k] = first + entry;
397 glm::vec4& e = texels[entry++];
398 e.x = static_cast<f32>(cmd.vertex_byte_offset / MESH_INSTANCE_STRIDE);
399 e.y = static_cast<f32>(runInstance);
400 runInstance += cmd.instance_count;
401 if (cmd.morph_index > 0) {
402 const MorphConstants& shaped = morph_shapes_[cmd.morph_index - 1];
403 const u32 live = static_cast<u32>(shaped.info.x);
404 e.z = static_cast<f32>(first + shapes);
405 texels[shapes] = shaped.info;
406 std::memcpy(&texels[shapes + 1], shaped.shape, live * sizeof(glm::vec4));
407 shapes += 1u + live;
408 }
409 }
410 i += length;
411 }
412}
413
414void DrawList::stageBlocks(const PerDrawBlockSet& blocks, bool drawRuns) {
415 staged_.assign(merged_draw_calls_, StagedBlocks{});
416 if (run_length_.size() != merged_draw_calls_) run_length_.assign(merged_draw_calls_, 1u);
417 if (entry_texel_.size() != merged_draw_calls_) entry_texel_.assign(merged_draw_calls_, 0u);
418 const auto stageEntry = [&](u32 i) {
419 if (!blocks.instance || !readsRecords(commands_[i])) return;
420 const InstanceConstants at{entry_texel_[i], {}};
421 staged_[i].instance = blocks.instance->stage(&at, sizeof(at));
422 };
423 for (u32 i = 0; i < merged_draw_calls_; ++i) {
424 const auto& cmd = commands_[i];
425 StagedBlocks& staged = staged_[i];
426 const u32 length = run_length_[i];
427 if (length == 0) continue; // inside a run: its head staged for it
428 if (length > 1) {
429 // One probe block for the run, its draws' instances one after another as their entries count them.
430 bool anyLit = false;
431 u32 instances = 0;
432 for (u32 k = 0; k < length; ++k) {
433 instances += commands_[i + k].instance_count;
434 anyLit = anyLit || probeLitAt(commands_[i + k]);
435 }
436 if (anyLit && blocks.probe) {
437 probe_run_.assign(static_cast<size_t>(instances) * PROBE_TEXELS, glm::vec4(0.0f));
438 u32 at = 0;
439 for (u32 k = 0; k < length; ++k) {
440 const DrawCommand& member = commands_[i + k];
441 for (u32 n = 0; n < member.instance_count; ++n, ++at) {
442 const size_t slot = static_cast<size_t>(member.probe_base) + n;
443 const u32 index = slot < probe_slots_.size() ? probe_slots_[slot] : 0u;
444 if (index == 0) continue;
445 std::memcpy(&probe_run_[static_cast<size_t>(at) * PROBE_TEXELS], &probes_[index - 1], sizeof(ProbeConstants));
446 }
447 }
448 staged.probe = blocks.probe->stage(probe_run_.data(), static_cast<u32>(probe_run_.size() * sizeof(glm::vec4)));
449 }
450 // The head's entry starts the run; gl_DrawID counts through the rest. Drawn one by one instead (a capture records draw by
451 // draw), each member names its own entry and reads the run's probe block, which its entry's place in it refers to.
452 stageEntry(i);
453 if (!drawRuns) {
454 for (u32 k = 1; k < length; ++k) {
455 stageEntry(i + k);
456 staged_[i + k].probe = staged.probe;
457 }
458 }
459 continue;
460 }
461
462 if (blocks.planar && cmd.planar_index > 0) {
463 staged.planar = blocks.planar->stage(&planar_[cmd.planar_index - 1], sizeof(PlanarConstants));
464 }
465 // The pose, for a draw that has one — in a range of this draw's own. A
466 // block shared between two draws of one pass is read by both with what
467 // the last wrote, which is a room of characters in one pose.
468 if (cmd.skin_count > 0 && blocks.skin) {
469 staged.skin = blocks.skin->stage(skin_matrices_.data() + cmd.skin_offset,
470 cmd.skin_count * static_cast<u32>(sizeof(glm::mat4)));
471 }
472
473 // WHERE this draw's instances read their irradiance, not what it is: the
474 // coefficients are in the frame's texture, one run per instance, so a
475 // merged draw holding objects in many places still says one thing here.
476 // A run no probe lights reads the zero block, which says the same thing, rather than zeroes of its own: staging
477 // them filled and uploaded up to sixteen kilobytes a draw, and gave every draw a range of its own to bind.
478 if (blocks.probe && probeLitAt(cmd)) {
479 // Every instance of this run, in the order it draws them, capped at
480 // what the block holds — a longer run carries no irradiance at all,
481 // so its tail reads zeroes and takes the environment.
482 const u32 runs = std::min(cmd.instance_count > 0 ? cmd.instance_count : 1u,
484 probe_run_.assign(static_cast<size_t>(runs) * PROBE_TEXELS, glm::vec4(0.0f));
485 for (u32 n = 0; n < runs; ++n) {
486 const size_t at = static_cast<size_t>(cmd.probe_base) + n;
487 const u32 index = at < probe_slots_.size() ? probe_slots_[at] : 0u;
488 if (index == 0) continue;
489 std::memcpy(&probe_run_[static_cast<size_t>(n) * PROBE_TEXELS],
490 &probes_[index - 1], sizeof(ProbeConstants));
491 }
492 staged.probe = blocks.probe->stage(
493 probe_run_.data(), static_cast<u32>(probe_run_.size() * sizeof(glm::vec4)));
494 }
495
496 // Where this draw's entry is: its records, and its shapes when it has any (see InstanceConstants.hpp).
497 stageEntry(i);
498 }
499 for (PerDrawBlocks* pool : {blocks.skin, blocks.probe, blocks.instance, blocks.planar}) {
500 if (pool) pool->upload();
501 }
502}
503
504bool DrawList::anyNeedsScenePicture(const MaterialStore& materials) const {
505 for (usize i = 0; i < commands_.size(); ++i) {
506 if (commands_[i].material_id != 0 && materials.needsScenePicture(commands_[i].material_id)) return true;
507 }
508 return false;
509}
510
511bool DrawList::anyUsesSceneColor(const MaterialStore& materials) const {
512 for (u32 i = 0; i < merged_draw_calls_; ++i) {
513 if (commands_[i].material_id != 0 && materials.usesSceneColor(commands_[i].material_id)) return true;
514 }
515 return false;
516}
517
518bool DrawList::anyUsesScreenColor(const MaterialStore& materials) const {
519 for (u32 i = 0; i < merged_draw_calls_; ++i) {
520 if (commands_[i].material_id != 0 && materials.usesScreenColor(commands_[i].material_id)) return true;
521 }
522 return false;
523}
524
527static bool isBlendedStage(RenderStage stage) {
528 return stage == RenderStage::Transparent || stage == RenderStage::Overlay;
529}
530
532 for (u32 i = 0; i < merged_draw_calls_; ++i) {
533 const auto& cmd = commands_[i];
534 if (isBlendedStage(cmd.stage) && !(cmd.material_id != 0 && materials.usesSceneColor(cmd.material_id))) return true;
535 }
536 return false;
537}
538
540static bool takesPartInPrepass(const DrawCommand& cmd) {
542}
543
545 u32 n = 0;
546 for (u32 i = 0; i < merged_draw_calls_; ++i) {
547 if (takesPartInPrepass(commands_[i])) ++n;
548 }
549 return n;
550}
551
552bool DrawList::anyUsesSceneDepth(const MaterialStore& materials) const {
553 for (u32 i = 0; i < merged_draw_calls_; ++i) {
554 if (commands_[i].material_id != 0 && materials.usesSceneDepth(commands_[i].material_id)) return true;
555 }
556 return false;
557}
558
560 MaterialStore& materials, u32 white_texture_id,
561 FrameCapture* capture, const PerDrawBlockSet& blocks, Phase phase, DrawPass pass) {
562 const bool prepass = pass == DrawPass::DepthPrepass;
563 const bool afterPrepass = pass == DrawPass::ColorAfterPrepass;
564 PipelineDesc lastDesc{};
566 const std::string* lastRun = nullptr;
567 skipped_for_program_ = 0;
568 const bool skipUnready = device.skipPendingPrograms();
569 // The draws of this phase read the scene's colour and compose it under themselves.
570 const bool composes = phase == Phase::SceneColorOnly;
571
572 // Once for the pass, not once per draw: every resident mesh reads the same
573 // texture. Rebound each pass because growing it mints a new handle.
574 if (buffers.instanceTexture() != TextureHandle::Invalid) {
576 }
577
578 // The colour pass after a prepass is the same list the prepass staged the per-draw blocks for. Runs where the device draws them
579 // as one call, and not while a capture records or replays draw by draw.
580 const bool capturing = capture && (capture->isCapturing() || capture->isReplaying());
581 const bool drawRuns = device.supportsMultiDraw() && !capturing;
582 if (!afterPrepass) stageBlocks(blocks, drawRuns);
583 if (prepass) prepass_skipped_.assign(merged_draw_calls_, 0u);
584 else if (!afterPrepass) prepass_skipped_.clear();
585 if (run_length_.size() != merged_draw_calls_) run_length_.assign(merged_draw_calls_, 1u);
586 u32 runCalls = 0;
587
588 for (u32 i = 0, next = 0; i < merged_draw_calls_; i = next) {
589 // A run is taken or passed over whole: its draws agree on everything a pass or a phase asks of them. Drawn one by one, a
590 // run's members are draws of their own, each with its own entry staged.
591 const u32 runLength = drawRuns ? std::max(run_length_[i], 1u) : 1u;
592 next = i + runLength;
593 const auto& cmd = commands_[i];
594 const StagedBlocks& staged = staged_[i];
595 if (prepass && !takesPartInPrepass(cmd)) continue;
596 if (defer_screen_color_ || phase == Phase::ScreenColorOnly) {
597 const bool screen = cmd.material_id != 0 && materials.usesScreenColor(cmd.material_id);
598 if (screen != (phase == Phase::ScreenColorOnly)) continue;
599 }
600 if (phase != Phase::All && phase != Phase::ScreenColorOnly) {
601 const bool refracts = cmd.material_id != 0 && materials.usesSceneColor(cmd.material_id);
602 const bool blended = isBlendedStage(cmd.stage);
603 bool drawn = false;
604 switch (phase) {
605 case Phase::WithoutSceneColor: drawn = !refracts; break;
606 case Phase::SceneColorOnly: drawn = refracts; break;
607 case Phase::OpaqueOnly: drawn = !refracts && !blended; break;
608 case Phase::BlendedOnly: drawn = !refracts && blended; break;
609 case Phase::All: drawn = true; break;
610 case Phase::ScreenColorOnly: drawn = true; break;
611 }
612 if (!drawn) continue;
613 }
614
615 if (shader_run_sink_) {
616 const std::string& run = materials.shaderLabel(cmd.material_id);
617 if (&run != lastRun && (!lastRun || run != *lastRun)) shader_run_sink_(run.c_str());
618 lastRun = &run;
619 }
620
621 const GfxStencilMode stencil = stencilModeOf(cmd.state_flags);
622
623 // Resolve the immutable pipeline. Depth/cull come from the command (resolved from
624 // its material, or the 2D defaults: depth_write on with the test off). createPipeline
625 // caches; a one-entry memo skips the lookup for identical consecutive (sorted) commands.
626 PipelineDesc desc{};
627 desc.program = ShaderHandle{cmd.shader_id};
628 desc.vertexLayout = cmd.hasPersistentGeometry()
629 ? cmd.vertex_layout
630 : buffers.layoutHandle(cmd.layout_id);
631 desc.blend = cmd.blend_mode;
632 // Opaque is a blend mode, not a second switch beside one: a material that
633 // says None is asking for the source to replace the destination, and any
634 // other value is asking to read it. Keeping it in the same field is what
635 // stops "Additive, blending off" from being expressible at all — and the
636 // sort key and canMergeWith already carry blend, so nothing else has to
637 // learn about this.
638 desc.blendEnabled = cmd.blend_mode != BlendMode::None;
639 desc.layerAlpha = layer_alpha_ && desc.blendEnabled;
640 desc.depthTest = cmd.depth_test;
641 desc.depthWrite = cmd.depth_write;
642 desc.stencil = stencil;
643 desc.cullEnabled = cmd.cull != 0;
644 desc.cullFront = cmd.cull == 2;
645 desc.depthBias = cmd.depth_bias;
646 desc.topology = cmd.topology;
647 desc.alphaToCoverage = !desc.blendEnabled && materials.alphaIsCoverage(cmd.material_id);
648 // Only what replaces what was behind it records a surface (a blended normal is no one's), except a
649 // draw that composes the scene behind it (water, refracting glass): it IS the pixel, so it owns its
650 // record, written unblended where the device can say so per target.
651 desc.gbuffer = gbuffer_pass_ && materials.programWritesGBuffer(cmd.shader_id)
652 && (!desc.blendEnabled || (composes && device.supportsUnblendedSurfaceTarget()));
653 desc.albedo = desc.gbuffer && materials.programWritesAlbedo(cmd.shader_id);
654 if (prepass) {
655 // Depth alone: the draw's depth program, unblended, no colour to either attachment. A cut-out keeps the alpha-to-coverage the colour pass draws with.
656 desc.program = ShaderHandle{cmd.depth_shader_id};
657 desc.blend = BlendMode::None;
658 desc.blendEnabled = false;
659 desc.gbuffer = false;
660 desc.albedo = false;
661 desc.colorWrite = false;
662 } else if (afterPrepass && takesPartInPrepass(cmd) && !(i < prepass_skipped_.size() && prepass_skipped_[i])) {
663 // Its depth is already in the buffer: it lands where it is level with it. It goes on writing depth, the value that is there
664 // already, and is not switched to testing alone: what did not take the prepass (a cut-out the prepass has no program for)
665 // is drawn in this pass too and does write depth, and on ANGLE's D3D11 backend with a multisampled target a draw that
666 // writes depth after draws that only test it is lost whole: with the prepass on, a wood of cut-out trees was not drawn.
667 desc.depthEqual = true;
668 }
669
670 // A program still compiling in the background: this draw waits for a later frame instead of making this one wait for it.
671 if (skipUnready && desc.program != ShaderHandle::Invalid && !device.programReady(desc.program)) {
672 ++skipped_for_program_;
673 // Its depth is not in the buffer: the colour pass must not look for it there (see prepass_skipped_).
674 if (prepass && i < prepass_skipped_.size()) prepass_skipped_[i] = 1u;
675 continue;
676 }
677
678 if (lastHandle == PipelineHandle::Invalid || !(desc == lastDesc)) {
679 lastHandle = device.createPipeline(desc);
680 lastDesc = desc;
681 }
682 device.setPipeline(lastHandle);
683 // The units this program samples: nothing else is bound for it. A depth program reads none, most of all.
684 const u32 sampled = device.boundSamplerUnits();
685
686 if (cmd.skin_count > 0 && blocks.skin) {
687 blocks.skin->bind(SKIN_CONSTANTS_BINDING, staged.skin);
688 }
689 if (blocks.probe) {
690 blocks.probe->bind(PROBE_CONSTANTS_BINDING, staged.probe);
691 }
692
693 if (blocks.planar) blocks.planar->bind(PLANAR_CONSTANTS_BINDING, staged.planar);
694
695 // Dynamic per-draw state (sorted+merged draws already group these coarsely).
696 if (cmd.state_flags & CMD_STATE_SCISSOR) {
697 device.setScissorTest(true);
698 device.setScissor(cmd.scissor.x, cmd.scissor.y, cmd.scissor.w, cmd.scissor.h);
699 } else {
700 device.setScissorTest(false);
701 }
702 if (stencil != GfxStencilMode::Off) {
703 device.setStencilReference(cmd.stencil_ref);
704 }
705 // A draw that samples binds every unit its shader declares, not only the ones it reads: an untouched unit holds what the last draw
706 // put there, in any pass, the prepass too (a vertex hook reads textures). The fill is a STABLE white, so unused units cost no rebind.
707 if (cmd.texture_count > 0) {
708 for (u8 slot = 0; slot < MAX_CMD_TEXTURE_SLOTS; ++slot) {
709 if (!((sampled >> slot) & 1u)) continue;
710 // The top units are the frame's screen-space light masks wherever one
711 // exists — the merge was kept off them, so what a draw holds there is
712 // the mask, or the fill where the pass drew nothing.
713 const bool masked = slot >= MAX_CMD_TEXTURE_SLOTS - mask_2d_slots_;
714 u32 tex = masked ? (mask_2d_textures_[slot] != 0 ? mask_2d_textures_[slot]
715 : white_texture_id)
716 : ((slot < cmd.texture_count) ? cmd.texture_ids[slot]
717 : white_texture_id);
718 device.bindTexture(slot, TextureHandle{tex});
719 }
720 }
721
722 // Per-material constants (binding 1) and textures: upload-if-dirty + bind this draw's material UBO. A no-op for material 0 and
723 // for materials whose shader declares no params. After the draw's own units, because a material past seven textures takes units
724 // below the base that its shader never reads from the draw (ShaderParser::materialOverflowUnits): the material's binding is the
725 // one this draw samples there.
726 if (cmd.material_id != 0) {
727 materials.bindForDraw(cmd.material_id, sampled);
728 }
729
730 if (cmd.hasPersistentGeometry()) {
731 // Geometry the GPU already holds: instanced when it is a mesh placed by records, plain when it is a run of quads a plugin keeps
732 // between frames (a tilemap's chunk strip), which has no records and draws once.
733 if (blocks.instance && cmd.instance_count != 0) {
734 blocks.instance->bind(INSTANCE_CONSTANTS_BINDING, staged.instance);
735 }
736 device.setVertexBuffer(0, cmd.vertex_buffer, 0);
737 device.setIndexBuffer(cmd.index_buffer);
738 const u32 firstIndexByte = static_cast<u32>(static_cast<uintptr_t>(cmd.index_offset) * sizeof(u32));
739 if (runLength > 1) {
740 run_counts_.resize(runLength);
741 run_offsets_.resize(runLength);
742 run_instances_.resize(runLength);
743 for (u32 k = 0; k < runLength; ++k) {
744 const DrawCommand& member = commands_[i + k];
745 run_counts_[k] = member.index_count;
746 run_offsets_[k] = static_cast<u32>(static_cast<uintptr_t>(member.index_offset) * sizeof(u32));
747 run_instances_[k] = member.instance_count;
748 }
749 device.drawElementsInstancedMulti(GfxDataType::UnsignedInt, run_counts_.data(), run_offsets_.data(),
750 run_instances_.data(), runLength);
751 ++runCalls;
752 } else if (cmd.instance_count != 0) {
753 device.drawElementsInstanced(cmd.index_count, GfxDataType::UnsignedInt, firstIndexByte, cmd.instance_count);
754 } else {
755 device.drawElements(cmd.index_count, GfxDataType::UnsignedInt, firstIndexByte);
756 }
757 } else if (cmd.instance_count > 0) {
758 // Instanced: static geometry (index_count indices from offset 0) drawn
759 // instance_count times, instance attributes rebased at vertex_byte_offset.
760 if (cmd.instance_buffer != BufferHandle::Invalid) {
761 buffers.bindInstanceBuffer(cmd.layout_id, cmd.instance_buffer);
762 } else {
763 buffers.bindInstanceLayout(cmd.layout_id, cmd.vertex_byte_offset);
764 }
765 device.drawElementsInstanced(cmd.index_count, GfxDataType::UnsignedInt, 0, cmd.instance_count);
766 } else {
767 buffers.bindLayout(cmd.layout_id);
768 device.drawElements(
769 cmd.index_count,
771 static_cast<u32>(static_cast<uintptr_t>(cmd.index_offset) * sizeof(u32)));
772 }
773
774 if (capture && !prepass) {
775 if (capture->isReplaying()) {
776 if (capture->stepReplay()) break;
777 } else if (capture->isCapturing()) {
778 for (u32 k = 0; k < runLength; ++k) capture->record(commands_[i + k], runEntities(i + k));
779 }
780 }
781 }
782 if (runCalls > 0) FrameProfiler::get().counter("render.multiDraw.calls", runCalls);
783}
784
786 const auto mix = [&h](u64 v) { h = hash::combine(h, v); };
787 const auto words = [&h](const void* data, usize bytes) { h = hash::combineWords(h, data, bytes); };
788 mix(commands_.size());
789 for (const DrawCommand& c : commands_) {
790 // What contentHash mixes, less where the records lie (the offset, and the sort key, which says no more than the state
791 // mixed here and the depth the records hold): commands are in the order they draw.
792 mix((static_cast<u64>(c.index_offset) << 32) | c.index_count);
793 mix(c.vertex_count);
794 mix((static_cast<u64>(c.shader_id) << 32) | c.material_id);
795 mix((static_cast<u64>(c.instance_count) << 32) | c.instance_stride);
796 mix((static_cast<u64>(static_cast<u32>(c.vertex_buffer)) << 32) | static_cast<u32>(c.index_buffer));
797 mix((static_cast<u64>(static_cast<u32>(c.vertex_layout)) << 32) | static_cast<u32>(c.instance_buffer));
798 mix((static_cast<u64>(c.skin_offset) << 32) | c.skin_count);
799 mix((static_cast<u64>(c.morph_index) << 32) | static_cast<u32>(c.layout_id));
800 mix((static_cast<u64>(static_cast<u16>(c.depth_bias)) << 32) | (static_cast<u64>(c.cull) << 24)
801 | (static_cast<u64>(c.depth_test) << 16) | (static_cast<u64>(c.depth_write) << 8)
802 | static_cast<u64>(c.blend_mode) | (static_cast<u64>(c.topology) << 40));
803 mix(c.texture_count);
804 for (u32 t = 0; t < c.texture_count && t < MAX_CMD_TEXTURE_SLOTS; ++t) mix(c.texture_ids[t]);
805 // The records themselves.
806 if (c.instance_buffer != BufferHandle::Invalid || c.instance_stride == 0 || c.instance_count == 0) {
807 trusted = false;
808 continue;
809 }
810 const usize bytes = static_cast<usize>(c.instance_count) * c.instance_stride;
811 if (static_cast<usize>(c.vertex_byte_offset) + bytes > pool.vertexBytesUsed(c.layout_id)) {
812 trusted = false;
813 continue;
814 }
815 words(pool.vertexData(c.layout_id) + c.vertex_byte_offset, bytes);
816 }
817 mix(skin_matrices_.size());
818 if (!skin_matrices_.empty()) words(skin_matrices_.data(), skin_matrices_.size() * sizeof(glm::mat4));
819 mix(morph_shapes_.size());
820 if (!morph_shapes_.empty()) words(morph_shapes_.data(), morph_shapes_.size() * sizeof(MorphConstants));
821 return h;
822}
823
825 const auto mix = [&h](u64 v) { h = hash::combine(h, v); };
826 mix(commands_.size());
827 for (const DrawCommand& c : commands_) {
828 mix(c.sort_key);
829 mix((static_cast<u64>(c.index_offset) << 32) | c.index_count);
830 mix((static_cast<u64>(c.vertex_byte_offset) << 32) | c.vertex_count);
831 mix((static_cast<u64>(c.shader_id) << 32) | c.material_id);
832 mix((static_cast<u64>(c.instance_count) << 32) | c.instance_stride);
833 mix((static_cast<u64>(static_cast<u32>(c.vertex_buffer)) << 32) | static_cast<u32>(c.index_buffer));
834 mix((static_cast<u64>(static_cast<u32>(c.vertex_layout)) << 32) | static_cast<u32>(c.instance_buffer));
835 mix((static_cast<u64>(c.skin_offset) << 32) | c.skin_count);
836 mix((static_cast<u64>(c.morph_index) << 32) | static_cast<u32>(c.layout_id));
837 mix((static_cast<u64>(static_cast<u16>(c.depth_bias)) << 32) | (static_cast<u64>(c.cull) << 24)
838 | (static_cast<u64>(c.depth_test) << 16) | (static_cast<u64>(c.depth_write) << 8)
839 | static_cast<u64>(c.blend_mode) | (static_cast<u64>(c.topology) << 40));
840 mix(c.texture_count);
841 for (u32 t = 0; t < c.texture_count && t < MAX_CMD_TEXTURE_SLOTS; ++t) mix(c.texture_ids[t]);
842 }
843 const auto words = [&h](const void* data, usize bytes) { h = hash::combineWords(h, data, bytes); };
844 mix(skin_matrices_.size());
845 if (!skin_matrices_.empty()) words(skin_matrices_.data(), skin_matrices_.size() * sizeof(glm::mat4));
846 mix(morph_shapes_.size());
847 if (!morph_shapes_.empty()) words(morph_shapes_.data(), morph_shapes_.size() * sizeof(MorphConstants));
848 return h;
849}
850
851} // namespace esengine
Per-frame CPU scope timing (ES_PROFILE_SCOPE) read by the editor profiler.
#define ES_PROFILE_COUNTER(name, value)
WHERE one draw's entry is in the frame's per-object record texture, and what an entry says.
Engine-side material registry — the resolved "how to draw" for a material handle.
The pose one skinned draw is deformed by: a bone matrix per joint.
bool anyUsesScreenColor(const MaterialStore &materials) const
Definition DrawList.cpp:518
u32 addProbe(const ProbeConstants &probe)
Definition DrawList.cpp:97
u64 contentHashOfRecords(u64 seed, TransientBufferPool &pool, bool &trusted) const
contentHash with the instance records each command reads from pool in place of where they lie in it: ...
Definition DrawList.cpp:785
void finalize(TransientBufferPool &pool)
Definition DrawList.cpp:113
bool anyUsesSceneColor(const MaterialStore &materials) const
Whether any merged draw reads the picture behind it.
Definition DrawList.cpp:511
u32 addSkinMatrices(const glm::mat4 *matrices, u32 count)
Definition DrawList.cpp:86
u32 depthPrepassDraws() const
How many of the merged draws take part in a depth prepass; 0 means a prepass has nothing to draw.
Definition DrawList.cpp:544
const DrawCommand & command(u32 index) const
Definition DrawList.hpp:179
void execute(GfxDevice &device, TransientBufferPool &buffers, MaterialStore &materials, u32 white_texture_id=0, FrameCapture *capture=nullptr, const PerDrawBlockSet &blocks={}, Phase phase=Phase::All, DrawPass pass=DrawPass::Color)
Definition DrawList.cpp:559
bool anyBlendedWithoutSceneColor(const MaterialStore &materials) const
Whether the list holds a blended draw that does not refract: what has to be ordered against a refract...
Definition DrawList.cpp:531
u32 addMorphShapes(const MorphConstants &shapes)
Definition DrawList.cpp:92
bool anyUsesSceneDepth(const MaterialStore &materials) const
Whether any merged draw reads the distance to the opaque scene behind it.
Definition DrawList.cpp:552
void push(const DrawCommand &cmd)
Definition DrawList.cpp:102
bool anyNeedsScenePicture(const MaterialStore &materials) const
Whether a draw is of a material that does not work without a picture of the scene (MaterialStore::nee...
Definition DrawList.cpp:504
u64 contentHash(u64 seed) const
A hash of what the list draws: each command's program, geometry, state and ranges,...
Definition DrawList.cpp:824
std::span< const Entity > runEntities(u32 index) const
Definition DrawList.cpp:307
void record(const DrawCommand &cmd, std::span< const Entity > entities)
void counter(const char *name, double value)
static FrameProfiler & get()
Abstract graphics device interface.
Definition GfxDevice.hpp:75
virtual void bindTexture(u32 slot, TextureHandle texture)=0
Activates a texture slot and binds a 2D texture.
bool programReady(ShaderHandle program)
Whether program can be drawn with now, without waiting: linked, or finished linking in the background...
virtual void setVertexBuffer(u32 slot, BufferHandle buffer, u32 offsetBytes)=0
Binds a vertex buffer to a layout slot for subsequent draws.
virtual void drawElementsInstancedMulti(GfxDataType indexType, const u32 *counts, const u32 *byteOffsets, const u32 *instanceCounts, u32 drawCount)
drawCount indexed instanced draws through the bound pipeline and buffers, as one call where supportsM...
bool skipPendingPrograms() const
virtual void drawElements(u32 indexCount, GfxDataType indexType, u32 byteOffset)=0
Draws indexed triangles.
virtual void setIndexBuffer(BufferHandle buffer)=0
Binds the index buffer for subsequent indexed draws.
u32 boundSamplerUnits()
The texture units the bound program samples, one bit a unit; every unit where the backend cannot say.
virtual void drawElementsInstanced(u32 indexCount, GfxDataType indexType, u32 byteOffset, u32 instanceCount)=0
Draws indexed triangles with instancing.
virtual void setScissorTest(bool enabled)=0
Enables or disables scissor test.
virtual bool supportsUnblendedSurfaceTarget() const
Whether a blended draw can write the surface record's second attachment unblended.
void setPipeline(PipelineHandle handle)
Binds a pipeline: applies its program, blend, depth, stencil compare/op and culling.
PipelineHandle createPipeline(const PipelineDesc &desc)
Resolves a pipeline description to a cached handle (creating it on first use).
virtual void setStencilReference(i32 ref)=0
Sets the dynamic stencil reference for the bound pipeline's stencil mode (no-op if Off).
virtual bool supportsMultiDraw() const
Whether drawElementsInstancedMulti is one call here, its draws told apart by gl_DrawID (WEBGL_multi_d...
virtual void setScissor(i32 x, i32 y, i32 w, i32 h)=0
Sets the scissor rectangle.
Maps a material handle to its resolved record. Owned by RenderContext so it is reachable from both th...
bool programWritesAlbedo(u32 programId) const
Of those, the ones that also write the surface's diffuse colour (location 2, fragAlbedo).
void bindForDraw(u32 materialId, u32 samplerUnits=0xFFFFFFFFu)
bool usesSceneColor(u32 materialId) const
const std::string & shaderLabel(u32 materialId) const
The name of the shader materialId draws with, or "default" for the path's own batch shader.
bool usesSceneDepth(u32 materialId) const
bool usesScreenColor(u32 materialId) const
bool alphaIsCoverage(u32 materialId) const
bool programWritesGBuffer(u32 programId) const
bool needsScenePicture(u32 materialId) const
void bind(u32 slot, Range range) const
Binds range to slot — the zero block for a range staging refused.
VertexLayoutHandle layoutHandle(LayoutId layout) const
void bindInstanceLayout(LayoutId layout, u32 instanceByteOffset)
TextureHandle instanceTexture() const
The frame's per-object records. Invalid until a frame writes some.
u32 vertexBytesUsed(LayoutId layout) const
void bindInstanceBuffer(LayoutId layout, BufferHandle instances)
constexpr u64 combine(u64 h, u64 v)
Definition Hash.hpp:33
u64 combineWords(u64 h, const void *data, usize bytes)
Definition Hash.hpp:37
GfxStencilMode stencilModeOf(u16 stateFlags)
The stencil mode a draw's state flags mean.
static constexpr u32 MESH_INSTANCE_STRIDE
Definition GfxEnums.hpp:121
static constexpr u16 CMD_STATE_UI_STENCIL
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
static constexpr u16 CMD_STATE_STENCIL_DECREMENT
std::uint64_t u64
Unsigned 64-bit integer.
Definition Types.hpp:47
f32 withTextureSlot(f32 texIndex, i32 slot)
GfxStencilMode
Stencil behaviour baked into a pipeline. The reference value is dynamic (GfxDevice::setStencilReferen...
@ Off
No stencil test; full color write.
std::size_t usize
Size type for indexing and sizes.
Definition Types.hpp:64
static constexpr u16 CMD_STATE_STENCIL_INCREMENT
constexpr Entity INVALID_ENTITY
Invalid entity sentinel value.
Definition Types.hpp:174
constexpr i32 BATCH_SLOT_BITS
static constexpr u16 CMD_STATE_STENCIL_ZERO
constexpr u32 SKIN_CONSTANTS_BINDING
Indexed UBO binding point for the per-draw SkinConstants block.
static constexpr u16 CMD_STATE_SCISSOR
constexpr u32 PROBE_CONSTANTS_BINDING
Indexed UBO binding point for the per-draw ProbeConstants block.
static bool takesPartInPrepass(const DrawCommand &cmd)
A draw the prepass takes in: opaque and depth-writing, with a program of its own for it,...
Definition DrawList.cpp:540
PipelineHandle
Opaque handle to a cached pipeline; PipelineHandle{0} is the null/invalid handle.
constexpr u32 MULTI_DRAW_MAX
The most draws one call may carry where the device draws runs (WEBGL_multi_draw).
constexpr u32 PROBE_MAX_INSTANCES
How many instances of one merged draw carry their own irradiance.
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
static constexpr u32 MESH_INSTANCE_TEXTURE_UNIT
Definition GfxEnums.hpp:133
std::uint8_t u8
Unsigned 8-bit integer.
Definition Types.hpp:41
static constexpr u16 CMD_STATE_STENCIL_WRITE
std::uint16_t u16
Unsigned 16-bit integer.
Definition Types.hpp:43
constexpr u32 PLANAR_CONSTANTS_BINDING
static constexpr u32 MAX_CMD_TEXTURE_SLOTS
constexpr u32 PROBE_TEXELS
Texels one instance's indirect light occupies in that block.
constexpr u32 INSTANCE_CONSTANTS_BINDING
Indexed UBO binding point for the per-draw InstanceConstants block. Nine and not four: slot 4 is DRAW...
static bool isBlendedStage(RenderStage stage)
Definition DrawList.cpp:527
float f32
32-bit floating point
Definition Types.hpp:59
One vertex of the shared batch stream.
i32 addTextureSlot(u32 texId, u8 limit=MAX_CMD_TEXTURE_SLOTS)
BufferHandle instance_buffer
BatchBreak mergeBlocker(const DrawCommand &next) const
bool hasPersistentGeometry() const
u32 texture_ids[MAX_CMD_TEXTURE_SLOTS]
u8 textureSlotBudget(u8 reservedSlots) const
Finds texId in this command's texture set, adds it (returns its slot), or -1 if the set is full....
One draw's shapes, laid out as the texels they are written as.
The uniform blocks a draw rewrites immediately before it.
Immutable description of a draw pipeline. Equality keys the pipeline cache, so two draws with the sam...
bool colorWrite
False writes no colour, to either attachment: a depth-only pass, whose surfaces are then drawn again ...
VertexLayoutHandle vertexLayout
The indirect light one instance stands in — both halves of it.