Compare commits

..
Author SHA1 Message Date
codegirl007 8d9faaaee0 Drop verbose comments from the clip-cache change. 2026-08-14 22:40:42 -07:00
codegirl007 41fb0a3d5a Cache clip defs on sprites to avoid per-frame map lookups.
Crowd stress (128 sprites) showed Clip_Def string map_get as the top engine cost; cache on set_sprite_clip and use it in update/draw.
2026-08-14 22:32:28 -07:00
4 changed files with 56 additions and 339 deletions
+1 -17
View File
@@ -11,7 +11,7 @@ Vertex :: struct {
} }
SPRITE_VERT_COUNT :: 6 SPRITE_VERT_COUNT :: 6
MAX_SPRITES :: 1012 // 1000 game sprites + FPS overlay glyphs MAX_SPRITES :: 128
SPRITE_VERTS_SIZE :: SPRITE_VERT_COUNT * size_of(Vertex) SPRITE_VERTS_SIZE :: SPRITE_VERT_COUNT * size_of(Vertex)
VERTEX_BUFFER_SIZE :: MAX_SPRITES * SPRITE_VERTS_SIZE VERTEX_BUFFER_SIZE :: MAX_SPRITES * SPRITE_VERTS_SIZE
@@ -36,12 +36,6 @@ App :: struct {
draw_list: [dynamic]Queued_Sprite, draw_list: [dynamic]Queued_Sprite,
clear_color: sdl.FColor, clear_color: sdl.FColor,
camera: Camera, 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 { Shader_Backend :: enum {
@@ -146,10 +140,6 @@ init :: proc(app: ^App, title: cstring, width, height: i32) -> bool {
app.camera = camera_default() app.camera = camera_default()
if !fps_overlay_init(app) {
fmt.eprintfln("fps overlay init failed; continuing without on-screen FPS")
}
return true return true
} }
@@ -162,8 +152,6 @@ shutdown :: proc(app: ^App) {
fmt.eprintfln("WaitForGPUIdle failed") fmt.eprintfln("WaitForGPUIdle failed")
} }
fps_overlay_shutdown(app)
if app.transfer_buffer != nil { if app.transfer_buffer != nil {
sdl.ReleaseGPUTransferBuffer(app.device, app.transfer_buffer) sdl.ReleaseGPUTransferBuffer(app.device, app.transfer_buffer)
} }
@@ -207,8 +195,6 @@ events :: proc() -> bool {
} }
begin_frame :: proc(app: ^App, clear_color: sdl.FColor = {0.12, 0.12, 0.16, 1}) { 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) clear(&app.draw_list)
app.clear_color = clear_color app.clear_color = clear_color
app.render_pass = nil app.render_pass = nil
@@ -255,8 +241,6 @@ end_frame :: proc(app: ^App) {
return return
} }
fps_overlay_queue(app)
n := len(app.draw_list) n := len(app.draw_list)
if n > 0 { if n > 0 {
map_ptr := sdl.MapGPUTransferBuffer(app.device, app.transfer_buffer, false) map_ptr := sdl.MapGPUTransferBuffer(app.device, app.transfer_buffer, false)
-279
View File
@@ -1,279 +0,0 @@
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})
}
}
+15 -17
View File
@@ -8,6 +8,8 @@ Sprite :: struct {
data: ^Character_Data, data: ^Character_Data,
position: Vec2, position: Vec2,
clip: string, clip: string,
clip_def: Clip_Def,
has_clip: bool,
frame: int, frame: int,
time: f32, time: f32,
flip_x: bool, flip_x: bool,
@@ -24,9 +26,8 @@ spawn_sprite :: proc(
position = position, position = position,
} }
set_sprite_clip(&sprite, clip) set_sprite_clip(&sprite, clip)
if frame != 0 { if frame != 0 && sprite.has_clip {
c, ok := character_clip(sprite.data, sprite.clip) c := sprite.clip_def
if ok {
if frame < 0 { if frame < 0 {
sprite.frame = 0 sprite.frame = 0
} else if frame >= len(c.frames) { } else if frame >= len(c.frames) {
@@ -35,16 +36,15 @@ spawn_sprite :: proc(
sprite.frame = frame sprite.frame = frame
} }
} }
}
return sprite return sprite
} }
update_sprite :: proc(sprite: ^Sprite, dt: f32) { update_sprite :: proc(sprite: ^Sprite, dt: f32) {
if sprite == nil || sprite.data == nil do return if sprite == nil || sprite.data == nil do return
if dt <= 0 do return if dt <= 0 do return
if !sprite.has_clip do return
clip, ok := character_clip(sprite.data, sprite.clip) clip := sprite.clip_def
if !ok do return
frame_count := len(clip.frames) frame_count := len(clip.frames)
if frame_count <= 0 do return if frame_count <= 0 do return
@@ -89,19 +89,15 @@ draw_sprite :: proc(app: ^App, sprite: ^Sprite) {
if sprite == nil || sprite.data == nil || sprite.data.texture == nil { if sprite == nil || sprite.data == nil || sprite.data.texture == nil {
return return
} }
// Reserve room for the engine FPS overlay glyphs when enabled. if len(app.draw_list) >= MAX_SPRITES {
max_game := MAX_SPRITES return
if app.show_fps && app.fps_texture != nil {
max_game -= FPS_OVERLAY_MAX_GLYPHS
} }
if len(app.draw_list) >= max_game { if !sprite.has_clip do return
if sprite.frame < 0 || sprite.frame >= len(sprite.clip_def.frames) {
return return
} }
frame, ok := character_frame(sprite.data, sprite.clip, sprite.frame) frame := sprite.clip_def.frames[sprite.frame]
if !ok {
return
}
src_w := f32(frame.source_size[0]) src_w := f32(frame.source_size[0])
src_h := f32(frame.source_size[1]) src_h := f32(frame.source_size[1])
@@ -153,12 +149,14 @@ draw_sprite :: proc(app: ^App, sprite: ^Sprite) {
set_sprite_clip :: proc(sprite: ^Sprite, clip: string) { set_sprite_clip :: proc(sprite: ^Sprite, clip: string) {
if sprite == nil || sprite.data == nil do return if sprite == nil || sprite.data == nil do return
if sprite.clip == clip do return if sprite.clip == clip && sprite.has_clip do return
_, ok := character_clip(sprite.data, clip) def, ok := character_clip(sprite.data, clip)
if !ok do return if !ok do return
sprite.clip = clip sprite.clip = clip
sprite.clip_def = def
sprite.has_clip = true
sprite.frame = 0 sprite.frame = 0
sprite.time = 0 sprite.time = 0
} }
+28 -14
View File
@@ -1,14 +1,12 @@
package main package main
import "core:fmt"
import eng "pkg:engine" import eng "pkg:engine"
// Many sprites sharing one Character_Data (Flyweight) — good batching demo. // Many sprites sharing one Character_Data (Flyweight) — good batching demo.
COUNT :: 1000 COUNT :: eng.MAX_SPRITES
COLS :: 40 COLS :: 16
CELL_W :: 20 FRAME_LOG_EVERY :: 60
CELL_H :: 24
ORIGIN_X :: 20
ORIGIN_Y :: 40
main :: proc() { main :: proc() {
app: eng.App app: eng.App
@@ -24,26 +22,42 @@ main :: proc() {
col := i % COLS col := i % COLS
row := i / COLS row := i / COLS
pos := eng.Vec2 { pos := eng.Vec2 {
f32(ORIGIN_X + col * CELL_W), f32(40 + col * 48),
f32(ORIGIN_Y + row * CELL_H), f32(80 + row * 60),
} }
clip := "idle" if (i % 2) == 0 else "walk" clip := "idle" if (i % 2) == 0 else "walk"
sprites[i] = eng.spawn_sprite(&data, pos, clip, i % 5) sprites[i] = eng.spawn_sprite(&data, pos, clip, i % 5)
} }
// Look at the middle of the grid app.camera.position = {400, 400}
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() last := eng.now_seconds()
frame_i := 0
sum_ms: f64
peak_ms: f64
for eng.events() { for eng.events() {
now := eng.now_seconds() now := eng.now_seconds()
dt := f32(now - last) dt := f32(now - last)
last = now 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 { for &s in sprites {
eng.update_sprite(&s, dt) eng.update_sprite(&s, dt)