Compare commits

..
Author SHA1 Message Date
codegirl007 0c06d75e86 Cursor: Apply local changes for cloud agent 2026-08-09 21:12:57 -07:00
11 changed files with 359 additions and 794 deletions
+2 -17
View File
@@ -1,7 +1,7 @@
.PHONY: shaders-vulkan shaders-d3d12 shaders-metal shaders-all bake toad hello_sprite crowd crowd_batch crowd_overdraw clip_thrash camera_sandbox clips check test help
.PHONY: shaders-vulkan shaders-d3d12 shaders-metal shaders-all bake toad hello_sprite crowd camera_sandbox clips check test help
.PHONY: flame flame-build flame-record flame-svg flame-report flame-tools
# Flamegraph profiling (needs: pacman -S perf). Example: make flame or make flame FLAME_EXAMPLE=crowd_batch
# Flamegraph profiling (needs: pacman -S perf). Example: make flame or make flame FLAME_EXAMPLE=toad
FLAME_EXAMPLE ?= crowd
FLAME_BIN := $(FLAME_EXAMPLE)_perf
FLAMEGRAPH_DIR ?= tools/FlameGraph
@@ -24,9 +24,6 @@ help:
@echo " toad Run the toad example (full demo)"
@echo " hello_sprite Minimal load + draw"
@echo " crowd Many sprites, one Character_Data"
@echo " crowd_batch Multi-texture batching stress (two toad loads)"
@echo " crowd_overdraw Stacked sprites overdraw stress"
@echo " clip_thrash Per-frame idle/walk clip flip stress"
@echo " camera_sandbox Pan camera / Space toggles follow"
@echo " clips Keys 1/2 switch idle/walk"
@echo " flame Build+record+SVG+JPG+text report (FLAME_EXAMPLE=$(FLAME_EXAMPLE))"
@@ -57,9 +54,6 @@ check:
odin check examples/toad -collection:pkg=.
odin check examples/hello_sprite -collection:pkg=.
odin check examples/crowd -collection:pkg=.
odin check examples/crowd_batch -collection:pkg=.
odin check examples/crowd_overdraw -collection:pkg=.
odin check examples/clip_thrash -collection:pkg=.
odin check examples/camera_sandbox -collection:pkg=.
odin check examples/clips -collection:pkg=.
@@ -72,15 +66,6 @@ hello_sprite:
crowd:
odin run examples/crowd -collection:pkg=.
crowd_batch:
odin run examples/crowd_batch -collection:pkg=.
crowd_overdraw:
odin run examples/crowd_overdraw -collection:pkg=.
clip_thrash:
odin run examples/clip_thrash -collection:pkg=.
camera_sandbox:
odin run examples/camera_sandbox -collection:pkg=.
-57
View File
@@ -118,63 +118,6 @@ set_sprite_clip_switch_and_guards :: proc(t: ^testing.T) {
testing.expect_value(t, sprite.time, f32(0.09))
}
@(test)
set_sprite_clip_caches_clip_def :: proc(t: ^testing.T) {
data := make_test_character()
defer destroy_test_character(&data)
sprite := spawn_sprite(&data, {}, "idle", 0)
testing.expect(t, sprite.has_clip, "spawn should cache a valid clip")
testing.expect_value(t, sprite.clip, "idle")
testing.expect(t, sprite.clip_def.loop, "idle clip loops")
testing.expect_value(t, sprite.clip_def.fps, f32(10))
testing.expect_value(t, len(sprite.clip_def.frames), 3)
set_sprite_clip(&sprite, "once")
testing.expect(t, sprite.has_clip, "switch should refresh cache")
testing.expect_value(t, sprite.clip, "once")
testing.expect(t, !sprite.clip_def.loop, "once clip does not loop")
testing.expect_value(t, sprite.clip_def.fps, f32(10))
testing.expect_value(t, len(sprite.clip_def.frames), 3)
set_sprite_clip(&sprite, "nope")
testing.expect(t, sprite.has_clip, "bad clip must leave cache intact")
testing.expect_value(t, sprite.clip, "once")
testing.expect(t, !sprite.clip_def.loop, "cached once clip preserved")
testing.expect_value(t, len(sprite.clip_def.frames), 3)
}
@(test)
set_sprite_clip_def_applies_and_guards :: proc(t: ^testing.T) {
data := make_test_character()
defer destroy_test_character(&data)
sprite := spawn_sprite(&data, {}, "idle", 0)
sprite.frame = 2
sprite.time = 0.05
once := data.def.clips["once"]
set_sprite_clip_def(&sprite, "once", once)
testing.expect_value(t, sprite.clip, "once")
testing.expect(t, sprite.has_clip, "def apply should mark clip present")
testing.expect_value(t, sprite.frame, 0)
testing.expect_value(t, sprite.time, f32(0))
testing.expect(t, !sprite.clip_def.loop, "once clip does not loop")
testing.expect_value(t, len(sprite.clip_def.frames), 3)
sprite.frame = 1
sprite.time = 0.09
set_sprite_clip_def(&sprite, "once", once)
testing.expect_value(t, sprite.frame, 1)
testing.expect_value(t, sprite.time, f32(0.09))
set_sprite_clip_def(&sprite, "empty", Clip_Def{loop = true, fps = 10, frames = nil})
testing.expect_value(t, sprite.clip, "once")
testing.expect_value(t, sprite.frame, 1)
set_sprite_clip_def(nil, "once", once)
}
@(test)
spawn_sprite_valid_and_invalid :: proc(t: ^testing.T) {
data := make_test_character()
+30 -80
View File
@@ -11,20 +11,13 @@ Vertex :: struct {
}
SPRITE_VERT_COUNT :: 6
MAX_SPRITES :: 128
MAX_SPRITES :: 1012 // 1000 game sprites + FPS overlay glyphs
SPRITE_VERTS_SIZE :: SPRITE_VERT_COUNT * size_of(Vertex)
VERTEX_BUFFER_SIZE :: MAX_SPRITES * SPRITE_VERTS_SIZE
Queued_Sprite :: struct {
texture: ^sdl.GPUTexture,
verts: [SPRITE_VERT_COUNT]Vertex,
batch_group: u32, // 0 = strict order; nonzero = caller permits regrouping
}
Draw_Batch :: struct {
start: int,
count: int,
texture: ^sdl.GPUTexture,
verts: [SPRITE_VERT_COUNT]Vertex,
}
App :: struct {
@@ -43,6 +36,12 @@ App :: struct {
draw_list: [dynamic]Queued_Sprite,
clear_color: sdl.FColor,
camera: Camera,
show_fps: bool,
fps_texture: ^sdl.GPUTexture,
fps_smooth: f32,
fps_display: int,
fps_last_time: f64,
fps_update_accum: f32,
}
Shader_Backend :: enum {
@@ -72,7 +71,7 @@ init :: proc(app: ^App, title: cstring, width, height: i32) -> bool {
}
requested: sdl.GPUShaderFormat = {.SPIRV, .DXIL, .MSL}
app.device = sdl.CreateGPUDevice(requested, false, nil)
app.device = sdl.CreateGPUDevice(requested, true, nil)
if app.device == nil {
fmt.eprintfln("CreateGPUDevice failed: %s", sdl.GetError())
return false
@@ -83,10 +82,6 @@ init :: proc(app: ^App, title: cstring, width, height: i32) -> bool {
return false
}
if !sdl.SetGPUAllowedFramesInFlight(app.device, 3) {
fmt.eprintfln("SetGPUAllowedFramesInFlight failed: %s", sdl.GetError())
}
// Prefer uncapped present for profiling; fall back if unsupported.
present := sdl.GPUPresentMode.VSYNC
if sdl.WindowSupportsGPUPresentMode(app.device, app.window, .IMMEDIATE) {
@@ -151,6 +146,10 @@ init :: proc(app: ^App, title: cstring, width, height: i32) -> bool {
app.camera = camera_default()
if !fps_overlay_init(app) {
fmt.eprintfln("fps overlay init failed; continuing without on-screen FPS")
}
return true
}
@@ -163,6 +162,8 @@ shutdown :: proc(app: ^App) {
fmt.eprintfln("WaitForGPUIdle failed")
}
fps_overlay_shutdown(app)
if app.transfer_buffer != nil {
sdl.ReleaseGPUTransferBuffer(app.device, app.transfer_buffer)
}
@@ -206,6 +207,8 @@ events :: proc() -> bool {
}
begin_frame :: proc(app: ^App, clear_color: sdl.FColor = {0.12, 0.12, 0.16, 1}) {
fps_overlay_begin_frame(app)
clear(&app.draw_list)
app.clear_color = clear_color
app.render_pass = nil
@@ -252,12 +255,10 @@ end_frame :: proc(app: ^App) {
return
}
n := len(app.draw_list)
batches: [dynamic]Draw_Batch
defer delete(batches)
if n > 0 {
prepare_draw_batches(app.draw_list[:], &batches)
fps_overlay_queue(app)
n := len(app.draw_list)
if n > 0 {
map_ptr := sdl.MapGPUTransferBuffer(app.device, app.transfer_buffer, false)
if map_ptr == nil {
fmt.eprintfln("MapGPUTransferBuffer failed: %s", sdl.GetError())
@@ -301,20 +302,26 @@ end_frame :: proc(app: ^App) {
app.render_pass = sdl.BeginGPURenderPass(cmd, &color_info, 1, nil)
sdl.BindGPUGraphicsPipeline(app.render_pass, app.pipeline)
for batch in batches {
i := 0
for i < n {
run := texture_run_len(app.draw_list[:], i)
q0 := app.draw_list[i]
sampler_binding := sdl.GPUTextureSamplerBinding {
texture = batch.texture,
texture = q0.texture,
sampler = app.sampler,
}
sdl.BindGPUFragmentSamplers(app.render_pass, 0, &sampler_binding, 1)
vb_binding := sdl.GPUBufferBinding {
buffer = app.vertex_buffer,
offset = u32(batch.start * SPRITE_VERTS_SIZE),
offset = u32(i * SPRITE_VERTS_SIZE),
}
sdl.BindGPUVertexBuffers(app.render_pass, 0, &vb_binding, 1)
sdl.DrawGPUPrimitives(app.render_pass, u32(batch.count * SPRITE_VERT_COUNT), 1, 0, 0)
sdl.DrawGPUPrimitives(app.render_pass, u32(run * SPRITE_VERT_COUNT), 1, 0, 0)
i += run
}
sdl.EndGPURenderPass(app.render_pass)
app.render_pass = nil
@@ -463,60 +470,3 @@ texture_run_len :: proc(list: []Queued_Sprite, start: int) -> int {
return n
}
texture_run_count :: proc(list: []Queued_Sprite) -> int {
if len(list) == 0 do return 0
count := 0
i := 0
for i < len(list) {
run := texture_run_len(list, i)
count += 1
i += run
}
return count
}
prepare_draw_batches :: proc(list: []Queued_Sprite, batches: ^[dynamic]Draw_Batch) {
clear(batches)
group_texture_runs(list)
i := 0
for i < len(list) {
run := texture_run_len(list, i)
append(batches, Draw_Batch{start = i, count = run, texture = list[i].texture})
i += run
}
}
// Within each contiguous nonzero batch_group, stably sort by texture so
// consecutive same-texture sprites become one draw. Group 0 and group
// boundaries are never crossed.
group_texture_runs :: proc(list: []Queued_Sprite) {
start := 0
for start < len(list) {
group := list[start].batch_group
if group == 0 {
start += 1
continue
}
end := start + 1
for end < len(list) && list[end].batch_group == group {
end += 1
}
// Stable insertion sort is sufficient while MAX_SPRITES is 128.
for i in start + 1 ..< end {
item := list[i]
j := i
for j > start {
if uintptr(list[j - 1].texture) <= uintptr(item.texture) {
break
}
list[j] = list[j - 1]
j -= 1
}
list[j] = item
}
start = end
}
}
-302
View File
@@ -7,18 +7,6 @@ fake_tex :: proc(id: uintptr) -> ^sdl.GPUTexture {
return cast(^sdl.GPUTexture)id
}
queued :: proc(tex_id: uintptr, group: u32, marker: f32) -> Queued_Sprite {
q: Queued_Sprite
q.texture = fake_tex(tex_id)
q.batch_group = group
q.verts[0].pos = {marker, 0}
return q
}
marker_of :: proc(q: Queued_Sprite) -> f32 {
return q.verts[0].pos.x
}
@(test)
texture_run_len_empty_or_oob :: proc(t: ^testing.T) {
testing.expect_value(t, texture_run_len(nil, 0), 0)
@@ -74,293 +62,3 @@ texture_run_len_all_different :: proc(t: ^testing.T) {
testing.expect_value(t, texture_run_len(list, 1), 1)
testing.expect_value(t, texture_run_len(list, 2), 1)
}
@(test)
group_texture_runs_strict_order_unchanged :: proc(t: ^testing.T) {
// Group 0 alternates textures; sorting must not reorder (alpha order).
list := []Queued_Sprite {
queued(2, 0, 1),
queued(1, 0, 2),
queued(2, 0, 3),
queued(1, 0, 4),
}
before_runs := texture_run_count(list)
group_texture_runs(list)
testing.expect_value(t, texture_run_count(list), before_runs)
testing.expect_value(t, marker_of(list[0]), f32(1))
testing.expect_value(t, marker_of(list[1]), f32(2))
testing.expect_value(t, marker_of(list[2]), f32(3))
testing.expect_value(t, marker_of(list[3]), f32(4))
}
@(test)
group_texture_runs_reduces_runs_inside_group :: proc(t: ^testing.T) {
list := []Queued_Sprite {
queued(2, 1, 1),
queued(1, 1, 2),
queued(2, 1, 3),
queued(1, 1, 4),
}
testing.expect_value(t, texture_run_count(list), 4)
group_texture_runs(list)
testing.expect_value(t, texture_run_count(list), 2)
testing.expect(t, list[0].texture == fake_tex(1), "lower texture pointer first")
testing.expect(t, list[1].texture == fake_tex(1), "same texture run")
testing.expect(t, list[2].texture == fake_tex(2), "second texture run")
testing.expect(t, list[3].texture == fake_tex(2), "second texture run cont")
}
@(test)
group_texture_runs_stable_same_texture :: proc(t: ^testing.T) {
list := []Queued_Sprite {
queued(2, 1, 10),
queued(1, 1, 20),
queued(2, 1, 30),
queued(1, 1, 40),
}
group_texture_runs(list)
// Same-texture relative order preserved (stable sort).
testing.expect_value(t, marker_of(list[0]), f32(20))
testing.expect_value(t, marker_of(list[1]), f32(40))
testing.expect_value(t, marker_of(list[2]), f32(10))
testing.expect_value(t, marker_of(list[3]), f32(30))
}
@(test)
group_texture_runs_respects_group_boundaries :: proc(t: ^testing.T) {
list := []Queued_Sprite {
queued(2, 1, 1),
queued(1, 1, 2),
queued(2, 0, 3), // strict barrier
queued(1, 2, 4),
queued(2, 2, 5),
}
group_texture_runs(list)
testing.expect_value(t, marker_of(list[2]), f32(3)) // barrier stays put
testing.expect(t, list[0].texture == fake_tex(1))
testing.expect(t, list[1].texture == fake_tex(2))
testing.expect(t, list[3].texture == fake_tex(1))
testing.expect(t, list[4].texture == fake_tex(2))
testing.expect_value(t, list[0].batch_group, u32(1))
testing.expect_value(t, list[1].batch_group, u32(1))
testing.expect_value(t, list[2].batch_group, u32(0))
testing.expect_value(t, list[3].batch_group, u32(2))
testing.expect_value(t, list[4].batch_group, u32(2))
}
@(test)
group_texture_runs_does_not_merge_across_different_groups :: proc(t: ^testing.T) {
// Adjacent nonzero groups with different ids must not merge runs across.
list := []Queued_Sprite {
queued(1, 1, 1),
queued(2, 1, 2),
queued(1, 2, 3),
queued(2, 2, 4),
}
group_texture_runs(list)
testing.expect_value(t, texture_run_count(list), 4)
testing.expect_value(t, list[0].batch_group, u32(1))
testing.expect_value(t, list[1].batch_group, u32(1))
testing.expect_value(t, list[2].batch_group, u32(2))
testing.expect_value(t, list[3].batch_group, u32(2))
}
@(test)
group_texture_runs_empty :: proc(t: ^testing.T) {
list := []Queued_Sprite{}
testing.expect_value(t, texture_run_count(list), 0)
group_texture_runs(list)
testing.expect_value(t, texture_run_count(list), 0)
}
@(test)
group_texture_runs_already_optimal :: proc(t: ^testing.T) {
list := []Queued_Sprite {
queued(1, 1, 10),
queued(1, 1, 20),
queued(2, 1, 30),
queued(2, 1, 40),
}
testing.expect_value(t, texture_run_count(list), 2)
group_texture_runs(list)
testing.expect_value(t, texture_run_count(list), 2)
testing.expect_value(t, marker_of(list[0]), f32(10))
testing.expect_value(t, marker_of(list[1]), f32(20))
testing.expect_value(t, marker_of(list[2]), f32(30))
testing.expect_value(t, marker_of(list[3]), f32(40))
}
@(test)
group_texture_runs_noncontiguous_same_group_id :: proc(t: ^testing.T) {
// Same nonzero id split by group 0: each window regroups alone.
list := []Queued_Sprite {
queued(2, 1, 1),
queued(1, 1, 2),
queued(2, 0, 3),
queued(2, 1, 4),
queued(1, 1, 5),
}
group_texture_runs(list)
testing.expect_value(t, marker_of(list[2]), f32(3))
testing.expect(t, list[0].texture == fake_tex(1))
testing.expect(t, list[1].texture == fake_tex(2))
testing.expect_value(t, list[2].batch_group, u32(0))
testing.expect(t, list[3].texture == fake_tex(1))
testing.expect(t, list[4].texture == fake_tex(2))
testing.expect_value(t, list[0].batch_group, u32(1))
testing.expect_value(t, list[1].batch_group, u32(1))
testing.expect_value(t, list[3].batch_group, u32(1))
testing.expect_value(t, list[4].batch_group, u32(1))
}
make_test_draw_app :: proc() -> App {
app: App
app.cmd = cast(^sdl.GPUCommandBuffer)uintptr(1)
app.swapchain_texture = fake_tex(99)
app.swapchain_w = 800
app.swapchain_h = 600
app.camera = camera_default()
app.draw_list = make([dynamic]Queued_Sprite)
return app
}
destroy_test_draw_app :: proc(app: ^App) {
if app == nil do return
delete(app.draw_list)
app^ = {}
}
make_test_draw_character :: proc() -> Character_Data {
data: Character_Data
data.texture = fake_tex(42)
data.width = 100
data.height = 100
data.def.pivot = {0.5, 1.0}
data.def.clips = make(map[string]Clip_Def)
frames := make([]Frame_Def, 1)
frames[0] = Frame_Def {
rect = {0, 0, 10, 10},
source_size = {10, 10},
trim_offset = {0, 0},
}
data.def.clips["idle"] = Clip_Def {
loop = true,
fps = 10,
frames = frames,
}
return data
}
destroy_test_draw_character :: proc(data: ^Character_Data) {
if data == nil do return
keys := make([dynamic]string, context.temp_allocator)
for key, clip in data.def.clips {
delete(clip.frames)
append(&keys, key)
}
for key in keys {
delete_key(&data.def.clips, key)
}
delete(data.def.clips)
data^ = {}
}
@(test)
draw_sprite_stamps_batch_group_zero :: proc(t: ^testing.T) {
app := make_test_draw_app()
defer destroy_test_draw_app(&app)
data := make_test_draw_character()
defer destroy_test_draw_character(&data)
sprite := spawn_sprite(&data, {100, 200}, "idle", 0)
draw_sprite(&app, &sprite)
testing.expect_value(t, len(app.draw_list), 1)
testing.expect_value(t, app.draw_list[0].batch_group, u32(0))
testing.expect(t, app.draw_list[0].texture == data.texture)
}
@(test)
draw_sprite_batched_stamps_batch_group :: proc(t: ^testing.T) {
app := make_test_draw_app()
defer destroy_test_draw_app(&app)
data := make_test_draw_character()
defer destroy_test_draw_character(&data)
sprite := spawn_sprite(&data, {100, 200}, "idle", 0)
draw_sprite_batched(&app, &sprite, 7)
testing.expect_value(t, len(app.draw_list), 1)
testing.expect_value(t, app.draw_list[0].batch_group, u32(7))
testing.expect(t, app.draw_list[0].texture == data.texture)
}
@(test)
draw_sprite_batched_guards_leave_list_unchanged :: proc(t: ^testing.T) {
app := make_test_draw_app()
defer destroy_test_draw_app(&app)
data := make_test_draw_character()
defer destroy_test_draw_character(&data)
sprite := spawn_sprite(&data, {100, 200}, "idle", 0)
app.cmd = nil
draw_sprite_batched(&app, &sprite, 1)
testing.expect_value(t, len(app.draw_list), 0)
app.cmd = cast(^sdl.GPUCommandBuffer)uintptr(1)
app.swapchain_texture = nil
draw_sprite_batched(&app, &sprite, 1)
testing.expect_value(t, len(app.draw_list), 0)
app.swapchain_texture = fake_tex(99)
draw_sprite_batched(&app, nil, 1)
testing.expect_value(t, len(app.draw_list), 0)
no_data := sprite
no_data.data = nil
draw_sprite_batched(&app, &no_data, 1)
testing.expect_value(t, len(app.draw_list), 0)
no_tex := sprite
tex_data := data
tex_data.texture = nil
no_tex.data = &tex_data
draw_sprite_batched(&app, &no_tex, 1)
testing.expect_value(t, len(app.draw_list), 0)
}
@(test)
prepare_draw_batches_regroups_then_plans_runs :: proc(t: ^testing.T) {
list := []Queued_Sprite {
queued(2, 1, 1),
queued(1, 1, 2),
queued(2, 1, 3),
queued(1, 1, 4),
queued(3, 0, 5),
queued(1, 0, 6),
}
batches := make([dynamic]Draw_Batch)
defer delete(batches)
prepare_draw_batches(list, &batches)
testing.expect_value(t, len(batches), 4)
testing.expect_value(t, batches[0].start, 0)
testing.expect_value(t, batches[0].count, 2)
testing.expect(t, batches[0].texture == fake_tex(1))
testing.expect_value(t, batches[1].start, 2)
testing.expect_value(t, batches[1].count, 2)
testing.expect(t, batches[1].texture == fake_tex(2))
testing.expect_value(t, batches[2].start, 4)
testing.expect_value(t, batches[2].count, 1)
testing.expect(t, batches[2].texture == fake_tex(3))
testing.expect_value(t, batches[3].start, 5)
testing.expect_value(t, batches[3].count, 1)
testing.expect(t, batches[3].texture == fake_tex(1))
// Group 0 submission order preserved.
testing.expect_value(t, marker_of(list[4]), f32(5))
testing.expect_value(t, marker_of(list[5]), f32(6))
testing.expect_value(t, list[4].batch_group, u32(0))
testing.expect_value(t, list[5].batch_group, u32(0))
}
+279
View File
@@ -0,0 +1,279 @@
package engine
import "core:fmt"
import "core:mem"
import sdl "vendor:sdl3"
// Enough for an 8-digit rate plus " FPS".
FPS_OVERLAY_MAX_GLYPHS :: 12
FPS_GLYPH_W :: 5
FPS_GLYPH_H :: 7
FPS_CELL_W :: 6
FPS_CELL_H :: 8
FPS_ATLAS_COLS :: 16
FPS_SCALE :: 2
FPS_MARGIN :: 8
FPS_UPDATE_INTERVAL :: 0.25
// Glyph indices in the debug atlas.
FPS_GLYPH_DIGIT_0 :: 0
FPS_GLYPH_F :: 10
FPS_GLYPH_P :: 11
FPS_GLYPH_S :: 12
FPS_GLYPH_SPACE :: 13
// 5x7 bitmaps (MSB = left). Digits 0-9, then F/P/S/space.
@(private)
fps_glyph_rows := [14][7]u8 {
{0x0E, 0x11, 0x13, 0x15, 0x19, 0x11, 0x0E}, // 0
{0x04, 0x0C, 0x04, 0x04, 0x04, 0x04, 0x0E}, // 1
{0x0E, 0x11, 0x01, 0x06, 0x08, 0x10, 0x1F}, // 2
{0x0E, 0x11, 0x01, 0x06, 0x01, 0x11, 0x0E}, // 3
{0x02, 0x06, 0x0A, 0x12, 0x1F, 0x02, 0x02}, // 4
{0x1F, 0x10, 0x1E, 0x01, 0x01, 0x11, 0x0E}, // 5
{0x06, 0x08, 0x10, 0x1E, 0x11, 0x11, 0x0E}, // 6
{0x1F, 0x01, 0x02, 0x04, 0x08, 0x08, 0x08}, // 7
{0x0E, 0x11, 0x11, 0x0E, 0x11, 0x11, 0x0E}, // 8
{0x0E, 0x11, 0x11, 0x0F, 0x01, 0x02, 0x0C}, // 9
{0x1F, 0x10, 0x10, 0x1E, 0x10, 0x10, 0x10}, // F
{0x1E, 0x11, 0x11, 0x1E, 0x10, 0x10, 0x10}, // P
{0x0F, 0x10, 0x10, 0x0E, 0x01, 0x01, 0x1E}, // S
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // space
}
set_show_fps :: proc(app: ^App, enabled: bool) {
if app == nil do return
app.show_fps = enabled
}
@(private)
fps_overlay_init :: proc(app: ^App) -> bool {
app.show_fps = true
app.fps_smooth = 0
app.fps_display = 0
app.fps_last_time = 0
app.fps_update_accum = 0
app.fps_texture = nil
atlas_w := FPS_ATLAS_COLS * FPS_CELL_W
atlas_h := FPS_CELL_H
pixels := make([]u8, atlas_w * atlas_h * 4)
defer delete(pixels)
for gi in 0 ..< len(fps_glyph_rows) {
gx := gi * FPS_CELL_W
rows := fps_glyph_rows[gi]
for row in 0 ..< FPS_GLYPH_H {
bits := rows[row]
for col in 0 ..< FPS_GLYPH_W {
on := (bits >> u8(FPS_GLYPH_W - 1 - col)) & 1 == 1
px := gx + col
py := row
i := (py * atlas_w + px) * 4
if on {
pixels[i + 0] = 255
pixels[i + 1] = 255
pixels[i + 2] = 255
pixels[i + 3] = 255
}
}
}
}
app.fps_texture = sdl.CreateGPUTexture(
app.device,
{
type = .D2,
format = .R8G8B8A8_UNORM,
usage = {.SAMPLER},
width = u32(atlas_w),
height = u32(atlas_h),
layer_count_or_depth = 1,
num_levels = 1,
sample_count = ._1,
},
)
if app.fps_texture == nil {
fmt.eprintfln("FPS overlay CreateGPUTexture failed: %s", sdl.GetError())
return false
}
upload_size := atlas_w * atlas_h * 4
tbuf := sdl.CreateGPUTransferBuffer(app.device, {usage = .UPLOAD, size = u32(upload_size)})
if tbuf == nil {
fmt.eprintfln("FPS overlay CreateGPUTransferBuffer failed: %s", sdl.GetError())
sdl.ReleaseGPUTexture(app.device, app.fps_texture)
app.fps_texture = nil
return false
}
defer sdl.ReleaseGPUTransferBuffer(app.device, tbuf)
map_ptr := sdl.MapGPUTransferBuffer(app.device, tbuf, false)
if map_ptr == nil {
fmt.eprintfln("FPS overlay MapGPUTransferBuffer failed: %s", sdl.GetError())
sdl.ReleaseGPUTexture(app.device, app.fps_texture)
app.fps_texture = nil
return false
}
mem.copy(map_ptr, raw_data(pixels), upload_size)
sdl.UnmapGPUTransferBuffer(app.device, tbuf)
cmd := sdl.AcquireGPUCommandBuffer(app.device)
if cmd == nil {
fmt.eprintfln("FPS overlay AcquireGPUCommandBuffer failed: %s", sdl.GetError())
sdl.ReleaseGPUTexture(app.device, app.fps_texture)
app.fps_texture = nil
return false
}
copy_pass := sdl.BeginGPUCopyPass(cmd)
transfer := sdl.GPUTextureTransferInfo {
transfer_buffer = tbuf,
offset = 0,
pixels_per_row = u32(atlas_w),
rows_per_layer = u32(atlas_h),
}
region := sdl.GPUTextureRegion {
texture = app.fps_texture,
w = u32(atlas_w),
h = u32(atlas_h),
d = 1,
}
sdl.UploadToGPUTexture(copy_pass, transfer, region, false)
sdl.EndGPUCopyPass(copy_pass)
fence := sdl.SubmitGPUCommandBufferAndAcquireFence(cmd)
if fence == nil {
fmt.eprintfln("FPS overlay texture upload submit failed: %s", sdl.GetError())
sdl.ReleaseGPUTexture(app.device, app.fps_texture)
app.fps_texture = nil
return false
}
defer sdl.ReleaseGPUFence(app.device, fence)
if !sdl.WaitForGPUFences(app.device, true, &fence, 1) {
fmt.eprintfln("FPS overlay WaitForGPUFences failed: %s", sdl.GetError())
sdl.ReleaseGPUTexture(app.device, app.fps_texture)
app.fps_texture = nil
return false
}
return true
}
@(private)
fps_overlay_shutdown :: proc(app: ^App) {
if app.device != nil && app.fps_texture != nil {
sdl.ReleaseGPUTexture(app.device, app.fps_texture)
}
app.fps_texture = nil
}
@(private)
fps_overlay_begin_frame :: proc(app: ^App) {
now := now_seconds()
if app.fps_last_time > 0 {
dt := now - app.fps_last_time
if dt > 0 {
instant := f32(1.0 / dt)
if app.fps_smooth <= 0 {
app.fps_smooth = instant
} else {
app.fps_smooth = app.fps_smooth * 0.9 + instant * 0.1
}
app.fps_update_accum += f32(dt)
if app.fps_display == 0 || app.fps_update_accum >= FPS_UPDATE_INTERVAL {
app.fps_display = int(app.fps_smooth + 0.5)
app.fps_update_accum = 0
}
}
}
app.fps_last_time = now
}
@(private)
fps_overlay_queue :: proc(app: ^App) {
if !app.show_fps || app.fps_texture == nil do return
if app.cmd == nil || app.swapchain_texture == nil do return
if app.swapchain_w == 0 || app.swapchain_h == 0 do return
fps := app.fps_display
if fps < 0 do fps = 0
// Build "N… FPS" with the full measured rate (no artificial cap).
glyphs: [FPS_OVERLAY_MAX_GLYPHS]int
count := 0
digits: [8]int
dcount := 0
if fps == 0 {
digits[0] = 0
dcount = 1
} else {
v := fps
for v > 0 && dcount < len(digits) {
digits[dcount] = v % 10
dcount += 1
v /= 10
}
for i in 0 ..< dcount / 2 {
digits[i], digits[dcount - 1 - i] = digits[dcount - 1 - i], digits[i]
}
}
for i in 0 ..< dcount {
if count >= FPS_OVERLAY_MAX_GLYPHS do break
glyphs[count] = FPS_GLYPH_DIGIT_0 + digits[i]
count += 1
}
if count + 4 <= FPS_OVERLAY_MAX_GLYPHS {
glyphs[count] = FPS_GLYPH_SPACE
count += 1
glyphs[count] = FPS_GLYPH_F
count += 1
glyphs[count] = FPS_GLYPH_P
count += 1
glyphs[count] = FPS_GLYPH_S
count += 1
}
sw := f32(app.swapchain_w)
sh := f32(app.swapchain_h)
atlas_w := f32(FPS_ATLAS_COLS * FPS_CELL_W)
atlas_h := f32(FPS_CELL_H)
gw := f32(FPS_GLYPH_W * FPS_SCALE)
gh := f32(FPS_GLYPH_H * FPS_SCALE)
advance := f32(FPS_CELL_W * FPS_SCALE)
x := f32(FPS_MARGIN)
y := f32(FPS_MARGIN)
for i in 0 ..< count {
if len(app.draw_list) >= MAX_SPRITES do break
gi := glyphs[i]
u0 := f32(gi * FPS_CELL_W) / atlas_w
v0 := f32(0)
u1 := f32(gi * FPS_CELL_W + FPS_GLYPH_W) / atlas_w
v1 := f32(FPS_GLYPH_H) / atlas_h
x0 := x + f32(i) * advance
y0 := y
x1 := x0 + gw
y1 := y0 + gh
p0 := to_clip(x0, y0, sw, sh)
p1 := to_clip(x1, y0, sw, sh)
p2 := to_clip(x1, y1, sw, sh)
p3 := to_clip(x0, y1, sw, sh)
verts := [SPRITE_VERT_COUNT]Vertex {
{pos = p0, uv = {u0, v0}},
{pos = p1, uv = {u1, v0}},
{pos = p2, uv = {u1, v1}},
{pos = p0, uv = {u0, v0}},
{pos = p2, uv = {u1, v1}},
{pos = p3, uv = {u0, v1}},
}
append(&app.draw_list, Queued_Sprite{texture = app.fps_texture, verts = verts})
}
}
-9
View File
@@ -27,15 +27,6 @@ choose_shader_runtime_prefers_msl :: proc(t: ^testing.T) {
testing.expect_value(t, rt.format, sdl.GPUShaderFormat{.MSL})
}
@(test)
choose_shader_runtime_prefers_dxil_without_msl :: proc(t: ^testing.T) {
rt, ok := choose_shader_runtime_from_formats({.DXIL, .SPIRV})
testing.expect(t, ok, "should pick a runtime when DXIL is available")
testing.expect_value(t, rt.backend, Shader_Backend.DSD12_DXIL)
testing.expect_value(t, rt.shader_dir, "shaders/d3d12")
testing.expect_value(t, rt.format, sdl.GPUShaderFormat{.DXIL})
}
@(test)
choose_shader_runtime_spirv_only :: proc(t: ^testing.T) {
rt, ok := choose_shader_runtime_from_formats({.SPIRV})
+34 -72
View File
@@ -5,14 +5,12 @@ import sdl "vendor:sdl3"
Vec2 :: [2]f32
Sprite :: struct {
data: ^Character_Data,
position: Vec2,
clip: string,
clip_def: Clip_Def,
has_clip: bool,
frame: int,
time: f32,
flip_x: bool,
data: ^Character_Data,
position: Vec2,
clip: string,
frame: int,
time: f32,
flip_x: bool,
}
spawn_sprite :: proc(
@@ -26,14 +24,16 @@ spawn_sprite :: proc(
position = position,
}
set_sprite_clip(&sprite, clip)
if frame != 0 && sprite.has_clip {
c := sprite.clip_def
if frame < 0 {
sprite.frame = 0
} else if frame >= len(c.frames) {
sprite.frame = len(c.frames) - 1
} else {
sprite.frame = frame
if frame != 0 {
c, ok := character_clip(sprite.data, sprite.clip)
if ok {
if frame < 0 {
sprite.frame = 0
} else if frame >= len(c.frames) {
sprite.frame = len(c.frames) - 1
} else {
sprite.frame = frame
}
}
}
return sprite
@@ -42,9 +42,9 @@ spawn_sprite :: proc(
update_sprite :: proc(sprite: ^Sprite, dt: f32) {
if sprite == nil || sprite.data == nil do return
if dt <= 0 do return
if !sprite.has_clip do return
clip := sprite.clip_def
clip, ok := character_clip(sprite.data, sprite.clip)
if !ok do return
frame_count := len(clip.frames)
if frame_count <= 0 do return
@@ -82,46 +82,26 @@ 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
}
if sprite == nil || sprite.data == nil || sprite.data.texture == nil {
return
}
if len(app.draw_list) >= MAX_SPRITES {
return
// Reserve room for the engine FPS overlay glyphs when enabled.
max_game := MAX_SPRITES
if app.show_fps && app.fps_texture != nil {
max_game -= FPS_OVERLAY_MAX_GLYPHS
}
if !sprite.has_clip do return
if sprite.frame < 0 || sprite.frame >= len(sprite.clip_def.frames) {
if len(app.draw_list) >= max_game {
return
}
frame := sprite.clip_def.frames[sprite.frame]
frame, ok := character_frame(sprite.data, sprite.clip, sprite.frame)
if !ok {
return
}
src_w := f32(frame.source_size[0])
src_h := f32(frame.source_size[1])
@@ -149,8 +129,10 @@ draw_sprite_batched :: proc(app: ^App, sprite: ^Sprite, batch_group: u32) {
sw := f32(app.swapchain_w)
sh := f32(app.swapchain_h)
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]
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)
tex_w := f32(sprite.data.width)
tex_h := f32(sprite.data.height)
@@ -165,38 +147,18 @@ draw_sprite_batched :: proc(app: ^App, sprite: ^Sprite, batch_group: u32) {
{pos = p3, uv = {u0, v1}},
}
append(
&app.draw_list,
Queued_Sprite {
texture = sprite.data.texture,
verts = verts,
batch_group = batch_group,
},
)
append(&app.draw_list, Queued_Sprite{texture = sprite.data.texture, verts = verts})
}
set_sprite_clip :: proc(sprite: ^Sprite, clip: string) {
if sprite == nil || sprite.data == nil do return
if sprite.clip == clip && sprite.has_clip do return
if sprite.clip == clip do return
def, ok := character_clip(sprite.data, clip)
_, ok := character_clip(sprite.data, clip)
if !ok do return
set_sprite_clip_def(sprite, clip, def)
}
// Applies a pre-resolved clip without looking up the character clip map.
// Use when callers already hold Clip_Def (e.g. thrashing between known clips).
set_sprite_clip_def :: proc(sprite: ^Sprite, clip: string, def: Clip_Def) {
if sprite == nil do return
if len(def.frames) == 0 do return
if sprite.clip == clip && sprite.has_clip do return
sprite.clip = clip
sprite.clip_def = def
sprite.has_clip = true
sprite.frame = 0
sprite.time = 0
}
-80
View File
@@ -1,80 +0,0 @@
package main
import "core:fmt"
import eng "pkg:engine"
// Clip thrash: flip every sprite between idle/walk each frame via set_sprite_clip_def.
COUNT :: eng.MAX_SPRITES
COLS :: 16
FRAME_LOG_EVERY :: 60
main :: proc() {
app: eng.App
if !eng.init(&app, "clip_thrash", 800, 600) do return
defer eng.shutdown(&app)
data, ok := eng.load_character_data(&app, "assets_baked/characters/toad/toad.char.json")
if !ok do return
defer eng.destroy_character_data(&app, &data)
idle_def, idle_ok := eng.character_clip(&data, "idle")
walk_def, walk_ok := eng.character_clip(&data, "walk")
if !idle_ok || !walk_ok do return
sprites: [COUNT]eng.Sprite
for i in 0 ..< COUNT {
col := i % COLS
row := i / COLS
pos := eng.Vec2 {
f32(40 + col * 48),
f32(80 + row * 60),
}
sprites[i] = eng.spawn_sprite(&data, pos, "idle", i % 5)
}
app.camera.position = {400, 400}
last := eng.now_seconds()
frame_i := 0
sum_ms: f64
peak_ms: f64
use_walk := false
for eng.events() {
now := eng.now_seconds()
dt := f32(now - last)
last = now
frame_ms := f64(dt) * 1000.0
sum_ms += frame_ms
if frame_ms > peak_ms do peak_ms = frame_ms
frame_i += 1
if frame_i % FRAME_LOG_EVERY == 0 {
avg := sum_ms / f64(FRAME_LOG_EVERY)
fps := 1000.0 / avg if avg > 0 else 0
fmt.printfln(
"clip_thrash frame: avg=%.2f ms (%.1f FPS) peak=%.2f ms over %d frames",
avg,
fps,
peak_ms,
FRAME_LOG_EVERY,
)
sum_ms = 0
peak_ms = 0
}
clip := "walk" if use_walk else "idle"
def := walk_def if use_walk else idle_def
use_walk = !use_walk
for &s in sprites {
eng.set_sprite_clip_def(&s, clip, def)
eng.update_sprite(&s, dt)
}
eng.begin_frame(&app)
for &s in sprites {
eng.draw_sprite(&app, &s)
}
eng.end_frame(&app)
}
}
+14 -28
View File
@@ -1,12 +1,14 @@
package main
import "core:fmt"
import eng "pkg:engine"
// Many sprites sharing one Character_Data (Flyweight) — good batching demo.
COUNT :: eng.MAX_SPRITES
COLS :: 16
FRAME_LOG_EVERY :: 60
COUNT :: 1000
COLS :: 40
CELL_W :: 20
CELL_H :: 24
ORIGIN_X :: 20
ORIGIN_Y :: 40
main :: proc() {
app: eng.App
@@ -22,42 +24,26 @@ main :: proc() {
col := i % COLS
row := i / COLS
pos := eng.Vec2 {
f32(40 + col * 48),
f32(80 + row * 60),
f32(ORIGIN_X + col * CELL_W),
f32(ORIGIN_Y + row * CELL_H),
}
clip := "idle" if (i % 2) == 0 else "walk"
sprites[i] = eng.spawn_sprite(&data, pos, clip, i % 5)
}
app.camera.position = {400, 400}
// Look at the middle of the grid
rows := (COUNT + COLS - 1) / COLS
app.camera.position = {
f32(ORIGIN_X + (COLS - 1) * CELL_W / 2),
f32(ORIGIN_Y + (rows - 1) * CELL_H / 2),
}
last := eng.now_seconds()
frame_i := 0
sum_ms: f64
peak_ms: f64
for eng.events() {
now := eng.now_seconds()
dt := f32(now - last)
last = now
frame_ms := f64(dt) * 1000.0
sum_ms += frame_ms
if frame_ms > peak_ms do peak_ms = frame_ms
frame_i += 1
if frame_i % FRAME_LOG_EVERY == 0 {
avg := sum_ms / f64(FRAME_LOG_EVERY)
fps := 1000.0 / avg if avg > 0 else 0
fmt.printfln(
"crowd frame: avg=%.2f ms (%.1f FPS) peak=%.2f ms over %d frames",
avg,
fps,
peak_ms,
FRAME_LOG_EVERY,
)
sum_ms = 0
peak_ms = 0
}
for &s in sprites {
eng.update_sprite(&s, dt)
-79
View File
@@ -1,79 +0,0 @@
package main
import "core:fmt"
import eng "pkg:engine"
// Multi-texture batching stress: two Character_Data (two GPU textures), alternating sprites.
COUNT :: eng.MAX_SPRITES
COLS :: 16
FRAME_LOG_EVERY :: 60
BATCH_GROUP :: u32(1)
TOAD_JSON :: "assets_baked/characters/toad/toad.char.json"
main :: proc() {
app: eng.App
if !eng.init(&app, "crowd_batch", 800, 600) do return
defer eng.shutdown(&app)
data_a, ok_a := eng.load_character_data(&app, TOAD_JSON)
if !ok_a do return
defer eng.destroy_character_data(&app, &data_a)
data_b, ok_b := eng.load_character_data(&app, TOAD_JSON)
if !ok_b do return
defer eng.destroy_character_data(&app, &data_b)
sprites: [COUNT]eng.Sprite
for i in 0 ..< COUNT {
col := i % COLS
row := i / COLS
pos := eng.Vec2 {
f32(40 + col * 48),
f32(80 + row * 60),
}
clip := "idle" if (i % 2) == 0 else "walk"
data := &data_a if (i % 2) == 0 else &data_b
sprites[i] = eng.spawn_sprite(data, pos, clip, i % 5)
}
app.camera.position = {400, 400}
last := eng.now_seconds()
frame_i := 0
sum_ms: f64
peak_ms: f64
for eng.events() {
now := eng.now_seconds()
dt := f32(now - last)
last = now
frame_ms := f64(dt) * 1000.0
sum_ms += frame_ms
if frame_ms > peak_ms do peak_ms = frame_ms
frame_i += 1
if frame_i % FRAME_LOG_EVERY == 0 {
avg := sum_ms / f64(FRAME_LOG_EVERY)
fps := 1000.0 / avg if avg > 0 else 0
fmt.printfln(
"crowd_batch frame: avg=%.2f ms (%.1f FPS) peak=%.2f ms over %d frames",
avg,
fps,
peak_ms,
FRAME_LOG_EVERY,
)
sum_ms = 0
peak_ms = 0
}
for &s in sprites {
eng.update_sprite(&s, dt)
}
eng.begin_frame(&app)
for &s in sprites {
eng.draw_sprite_batched(&app, &s, BATCH_GROUP)
}
eng.end_frame(&app)
}
}
-70
View File
@@ -1,70 +0,0 @@
package main
import "core:fmt"
import eng "pkg:engine"
// GPU overdraw stress: MAX_SPRITES stacked near one world point.
COUNT :: eng.MAX_SPRITES
FRAME_LOG_EVERY :: 60
main :: proc() {
app: eng.App
if !eng.init(&app, "crowd_overdraw", 800, 600) do return
defer eng.shutdown(&app)
data, ok := eng.load_character_data(&app, "assets_baked/characters/toad/toad.char.json")
if !ok do return
defer eng.destroy_character_data(&app, &data)
center := eng.Vec2{400, 400}
sprites: [COUNT]eng.Sprite
for i in 0 ..< COUNT {
jitter := eng.Vec2 {
f32((i % 7) - 3),
f32((i % 5) - 2),
}
clip := "idle" if (i % 2) == 0 else "walk"
sprites[i] = eng.spawn_sprite(&data, center + jitter, clip, i % 5)
}
app.camera.position = {400, 400}
last := eng.now_seconds()
frame_i := 0
sum_ms: f64
peak_ms: f64
for eng.events() {
now := eng.now_seconds()
dt := f32(now - last)
last = now
frame_ms := f64(dt) * 1000.0
sum_ms += frame_ms
if frame_ms > peak_ms do peak_ms = frame_ms
frame_i += 1
if frame_i % FRAME_LOG_EVERY == 0 {
avg := sum_ms / f64(FRAME_LOG_EVERY)
fps := 1000.0 / avg if avg > 0 else 0
fmt.printfln(
"crowd_overdraw frame: avg=%.2f ms (%.1f FPS) peak=%.2f ms over %d frames",
avg,
fps,
peak_ms,
FRAME_LOG_EVERY,
)
sum_ms = 0
peak_ms = 0
}
for &s in sprites {
eng.update_sprite(&s, dt)
}
eng.begin_frame(&app)
for &s in sprites {
eng.draw_sprite(&app, &s)
}
eng.end_frame(&app)
}
}