Add texture batching with TDD coverage at enqueue and prepare seams.

Opt-in batch_group regrouping cuts texture binds; prepare_draw_batches and draw_list tests lock the contract so end_frame cannot silently drop regroup.
This commit is contained in:
2026-08-14 22:15:08 -07:00
parent 0d4cdbd8ca
commit 25dd3aa920
3 changed files with 414 additions and 17 deletions
+34 -5
View File
@@ -82,7 +82,31 @@ to_clip :: proc(px, py, sw, sh: f32) -> [2]f32 {
}
}
// Axis-aligned quad: two unique x and y values, so scale once and reuse.
sprite_quad_to_clip :: proc(x0, y0, x1, y1, sw, sh: f32) -> [4]Vec2 {
sx := 2.0 / sw
sy := 2.0 / sh
left := x0 * sx - 1
right := x1 * sx - 1
top := 1 - y0 * sy
bottom := 1 - y1 * sy
return {
{left, top},
{right, top},
{right, bottom},
{left, bottom},
}
}
draw_sprite :: proc(app: ^App, sprite: ^Sprite) {
draw_sprite_batched(app, sprite, 0)
}
// Nonzero batch_group lets end_frame regroup consecutive same-group sprites by
// texture. Group 0 keeps exact submission order for correct alpha overlap.
draw_sprite_batched :: proc(app: ^App, sprite: ^Sprite, batch_group: u32) {
if app.cmd == nil || app.swapchain_texture == nil {
return
}
@@ -124,10 +148,8 @@ draw_sprite :: proc(app: ^App, sprite: ^Sprite) {
sw := f32(app.swapchain_w)
sh := f32(app.swapchain_h)
p0 := to_clip(x0_px, y0_px, sw, sh)
p1 := to_clip(x1_px, y0_px, sw, sh)
p2 := to_clip(x1_px, y1_px, sw, sh)
p3 := to_clip(x0_px, y1_px, sw, sh)
points := sprite_quad_to_clip(x0_px, y0_px, x1_px, y1_px, sw, sh)
p0, p1, p2, p3 := points[0], points[1], points[2], points[3]
tex_w := f32(sprite.data.width)
tex_h := f32(sprite.data.height)
@@ -142,7 +164,14 @@ draw_sprite :: proc(app: ^App, sprite: ^Sprite) {
{pos = p3, uv = {u0, v1}},
}
append(&app.draw_list, Queued_Sprite{texture = sprite.data.texture, verts = verts})
append(
&app.draw_list,
Queued_Sprite {
texture = sprite.data.texture,
verts = verts,
batch_group = batch_group,
},
)
}
set_sprite_clip :: proc(sprite: ^Sprite, clip: string) {