faster sort of command queue

This commit is contained in:
Alessandro Mauri 2025-10-07 17:47:24 +02:00
parent 89f19ccf2e
commit 884105a4e2
2 changed files with 36 additions and 6 deletions

View File

@ -59,6 +59,35 @@ fn int Cmd.compare_to(Cmd a, Cmd b)
return a.z_index > b.z_index ? 1 : -1;
}
// FIXME: This sorting method does not fully respect layer ordering for popups.
// Popups that start on the same layer but are enqueued at different times
// may still be rendered on top of each other, even if the first popup
// has elements in higher layers than the second. The current approach
// does not truly sort by layer; it only moves elements from higher layers
// to the end of the queue as they are encountered.
fn void? CmdQueue.sort(&queue)
{
CmdQueue stack;
stack.init(allocator::tmem);
for (isz i = queue.len()-1; i > 0; i--) {
Cmd cur = (*queue)[i];
Cmd next = (*queue)[i - 1];
// cur < next
if (cur.compare_to(next) < 0) {
stack.push(next);
queue.remove_at(i-1);
}
}
usz l = stack.len();
for (usz i; i < l; i++) {
queue.push(stack.pop())!;
}
}
// implement the Printable interface
fn usz? Cmd.to_format(Cmd* cmd, Formatter *f) @dynamic
{

View File

@ -294,6 +294,12 @@ fn void? Ctx.frame_end(&ctx)
ctx.sprite_atlas.should_update = false;
}
// sort the command buffer by the z-index
// FIXME: sorting the buffer fucks with scissor commands that have to be kept in place
// TODO: instead of sorting at the end perform ordered inserts into the command buffer
//sort::countingsort(ctx.cmd_queue, fn uint(Cmd c) => c.z_index+1);
ctx.cmd_queue.sort()!;
// debug
$if $feature(DEBUG_POINTER):
// draw mouse position
@ -311,11 +317,6 @@ $if $feature(DEBUG_POINTER):
ctx.cmd_queue.push(cmd);
$endif
// sort the command buffer by the z-index
// FIXME: sorting the buffer fucks with scissor commands that have to be kept in place
// TODO: instead of sorting at the end perform ordered inserts into the command buffer
sort::countingsort(ctx.cmd_queue, fn uint(Cmd c) => c.z_index+1);
// foreach (i, c: ctx.cmd_queue) {
// io::printf("[%d]: ", i);
// io::printn(c);