Add tracy profiling

This commit is contained in:
Jared Miller 2025-12-16 16:32:47 -05:00
parent 7b43b5726e
commit ebe28e5669
No known key found for this signature in database
5 changed files with 55 additions and 12 deletions

View file

@ -4,6 +4,9 @@ pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});
// tracy profiling (run with -Dtracy=true)
const enable_tracy = b.option(bool, "tracy", "Enable Tracy profiler") orelse false;
const raylib_dep = b.dependency("raylib_zig", .{ const raylib_dep = b.dependency("raylib_zig", .{
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
@ -24,6 +27,16 @@ pub fn build(b: *std.Build) void {
sandbox_exe.root_module.addImport("raylib", raylib_dep.module("raylib")); sandbox_exe.root_module.addImport("raylib", raylib_dep.module("raylib"));
sandbox_exe.linkLibrary(raylib_dep.artifact("raylib")); sandbox_exe.linkLibrary(raylib_dep.artifact("raylib"));
// tracy integration (optional)
const ztracy = b.dependency("ztracy", .{
.enable_ztracy = enable_tracy,
.on_demand = true, // allow connecting after app starts
});
sandbox_exe.root_module.addImport("ztracy", ztracy.module("root"));
if (enable_tracy) {
sandbox_exe.linkLibrary(ztracy.artifact("tracy"));
}
b.installArtifact(sandbox_exe); b.installArtifact(sandbox_exe);
const sandbox_run_cmd = b.addRunArtifact(sandbox_exe); const sandbox_run_cmd = b.addRunArtifact(sandbox_exe);

View file

@ -7,6 +7,10 @@
.url = "git+https://github.com/raylib-zig/raylib-zig#a4d18b2d1cf8fdddec68b5b084535fca0475f466", .url = "git+https://github.com/raylib-zig/raylib-zig#a4d18b2d1cf8fdddec68b5b084535fca0475f466",
.hash = "raylib_zig-5.6.0-dev-KE8REL5MBQAf3p497t52Xw9P7ojndIkVOWPXnLiLLw2P", .hash = "raylib_zig-5.6.0-dev-KE8REL5MBQAf3p497t52Xw9P7ojndIkVOWPXnLiLLw2P",
}, },
.ztracy = .{
.url = "git+https://github.com/zig-gamedev/ztracy?ref=main#e7b401dea9ce006f8b236e3a2ca1a9f3d5c3e896",
.hash = "ztracy-0.14.0-dev-zHJSq78GGQC904aYvBPn6OOvRVOq_opAwDfeHZdvQyej",
},
}, },
.paths = .{ .paths = .{
"build.zig", "build.zig",

View file

@ -42,7 +42,7 @@ check:
test: test:
zig build test zig build test
# auto-benchmark (ramps entities until performance degrades, works on linux/windows) # auto-benchmark (ramps entities until performance degrades)
bench: bench:
zig build -Doptimize=ReleaseFast run -- --bench zig build -Doptimize=ReleaseFast run -- --bench
cat benchmark.log cat benchmark.log
@ -58,3 +58,8 @@ bench-sw:
bench-sw: bench-sw:
@echo "bench-sw: windows doesn't have xvfb equivalent" @echo "bench-sw: windows doesn't have xvfb equivalent"
@echo "use 'just bench' if you have a GPU, or run in WSL/linux CI" @echo "use 'just bench' if you have a GPU, or run in WSL/linux CI"
[linux]
profile port="9876":
# start Tracy: tracy-profiler -a 127.0.0.1 -p {{port}}
zig build -Dtracy=true -Doptimize=ReleaseFast && TRACY_PORT={{port}} ./zig-out/bin/sandbox

View file

@ -3,6 +3,7 @@
const std = @import("std"); const std = @import("std");
const rl = @import("raylib"); const rl = @import("raylib");
const ztracy = @import("ztracy");
const sandbox = @import("sandbox.zig"); const sandbox = @import("sandbox.zig");
const ui = @import("ui.zig"); const ui = @import("ui.zig");
const SsboRenderer = @import("ssbo_renderer.zig").SsboRenderer; const SsboRenderer = @import("ssbo_renderer.zig").SsboRenderer;
@ -331,12 +332,16 @@ pub fn main() !void {
// update // update
if (!paused) { if (!paused) {
const tracy_update = ztracy.ZoneN(@src(), "update");
defer tracy_update.End();
const update_start = std.time.microTimestamp(); const update_start = std.time.microTimestamp();
sandbox.update(&entities, &rng); sandbox.update(&entities, &rng);
update_time_us = std.time.microTimestamp() - update_start; update_time_us = std.time.microTimestamp() - update_start;
} }
// render // render
const tracy_render = ztracy.ZoneN(@src(), "render");
defer tracy_render.End();
const render_start = std.time.microTimestamp(); const render_start = std.time.microTimestamp();
rl.beginDrawing(); rl.beginDrawing();
@ -407,6 +412,9 @@ pub fn main() !void {
const update_ms = @as(f32, @floatFromInt(update_time_us)) / 1000.0; const update_ms = @as(f32, @floatFromInt(update_time_us)) / 1000.0;
const render_ms = @as(f32, @floatFromInt(render_time_us)) / 1000.0; const render_ms = @as(f32, @floatFromInt(render_time_us)) / 1000.0;
logger.log(elapsed, entities.count, frame_ms, update_ms, render_ms); logger.log(elapsed, entities.count, frame_ms, update_ms, render_ms);
// tracy frame mark
ztracy.FrameMark();
} }
} }

View file

@ -3,6 +3,7 @@
const std = @import("std"); const std = @import("std");
const rl = @import("raylib"); const rl = @import("raylib");
const ztracy = @import("ztracy");
const sandbox = @import("sandbox.zig"); const sandbox = @import("sandbox.zig");
const SCREEN_WIDTH = sandbox.SCREEN_WIDTH; const SCREEN_WIDTH = sandbox.SCREEN_WIDTH;
@ -142,6 +143,9 @@ pub const SsboRenderer = struct {
rl.gl.rlDrawRenderBatchActive(); rl.gl.rlDrawRenderBatchActive();
// copy entity data to GPU buffer (position + color only) // copy entity data to GPU buffer (position + color only)
{
const zone = ztracy.ZoneN(@src(), "ssbo_copy");
defer zone.End();
for (entities.items[0..entities.count], 0..) |entity, i| { for (entities.items[0..entities.count], 0..) |entity, i| {
self.gpu_buffer[i] = .{ self.gpu_buffer[i] = .{
.x = entity.x, .x = entity.x,
@ -149,10 +153,15 @@ pub const SsboRenderer = struct {
.color = entity.color, .color = entity.color,
}; };
} }
}
// upload to SSBO // upload to SSBO
{
const zone = ztracy.ZoneN(@src(), "ssbo_upload");
defer zone.End();
const data_size: u32 = @intCast(entities.count * @sizeOf(sandbox.GpuEntity)); const data_size: u32 = @intCast(entities.count * @sizeOf(sandbox.GpuEntity));
rl.gl.rlUpdateShaderBuffer(self.ssbo_id, self.gpu_buffer.ptr, data_size, 0); rl.gl.rlUpdateShaderBuffer(self.ssbo_id, self.gpu_buffer.ptr, data_size, 0);
}
// bind shader // bind shader
rl.gl.rlEnableShader(self.shader_id); rl.gl.rlEnableShader(self.shader_id);
@ -183,9 +192,13 @@ pub const SsboRenderer = struct {
rl.gl.rlSetBlendMode(@intFromEnum(rl.gl.rlBlendMode.rl_blend_alpha)); rl.gl.rlSetBlendMode(@intFromEnum(rl.gl.rlBlendMode.rl_blend_alpha));
// bind VAO and draw // bind VAO and draw
{
const zone = ztracy.ZoneN(@src(), "ssbo_draw");
defer zone.End();
_ = rl.gl.rlEnableVertexArray(self.vao_id); _ = rl.gl.rlEnableVertexArray(self.vao_id);
rl.gl.rlEnableVertexBuffer(self.vbo_id); rl.gl.rlEnableVertexBuffer(self.vbo_id);
rl.gl.rlDrawVertexArrayInstanced(0, 6, @intCast(entities.count)); rl.gl.rlDrawVertexArrayInstanced(0, 6, @intCast(entities.count));
}
// cleanup - restore raylib's expected state // cleanup - restore raylib's expected state
rl.gl.rlDisableVertexArray(); rl.gl.rlDisableVertexArray();