From 1c0f552032a161e49b46fcf6306f4088efd10e47 Mon Sep 17 00:00:00 2001 From: Jared Miller Date: Mon, 15 Dec 2025 22:02:48 -0500 Subject: [PATCH] Swap static buffer for heap-allocated --- src/sandbox_main.zig | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/sandbox_main.zig b/src/sandbox_main.zig index 62a42d7..f6fc53f 100644 --- a/src/sandbox_main.zig +++ b/src/sandbox_main.zig @@ -191,10 +191,14 @@ pub fn main() !void { var instance_material: ?rl.Material = null; var ortho_camera: rl.Camera3D = undefined; - // static buffer for transforms - allocated once, reused each frame - var transforms: [sandbox.MAX_ENTITIES]rl.Matrix = undefined; + // heap-allocated transforms buffer (64MB is too big for stack) + var transforms: ?[]rl.Matrix = null; if (use_instancing) { + transforms = std.heap.page_allocator.alloc(rl.Matrix, sandbox.MAX_ENTITIES) catch { + std.debug.print("failed to allocate transforms buffer\n", .{}); + return; + }; // create quad mesh (XZ plane, will view from above) quad_mesh = rl.genMeshPlane(MESH_SIZE, MESH_SIZE, 1, 1); rl.uploadMesh(&quad_mesh.?, false); // upload to GPU @@ -214,6 +218,7 @@ pub fn main() !void { defer { if (quad_mesh) |*m| rl.unloadMesh(m.*); if (instance_material) |mat| mat.unload(); + if (transforms) |t| std.heap.page_allocator.free(t); } // load UI font (embedded) @@ -298,15 +303,16 @@ pub fn main() !void { if (use_instancing) { // GPU instancing path + const xforms = transforms.?; // fill transforms array with entity positions for (entities.items[0..entities.count], 0..) |entity, i| { // entity (x, y) maps to 3D (x, 0, y) on XZ plane - transforms[i] = rl.Matrix.translate(entity.x, 0, entity.y); + xforms[i] = rl.Matrix.translate(entity.x, 0, entity.y); } // draw all entities with single instanced call ortho_camera.begin(); - rl.drawMeshInstanced(quad_mesh.?, instance_material.?, transforms[0..entities.count]); + rl.drawMeshInstanced(quad_mesh.?, instance_material.?, xforms[0..entities.count]); ortho_camera.end(); } else { // rlgl quad batching path (original)