Add tracy profiling
This commit is contained in:
parent
7b43b5726e
commit
ebe28e5669
5 changed files with 55 additions and 12 deletions
13
build.zig
13
build.zig
|
|
@ -4,6 +4,9 @@ pub fn build(b: *std.Build) void {
|
|||
const target = b.standardTargetOptions(.{});
|
||||
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", .{
|
||||
.target = target,
|
||||
.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.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);
|
||||
|
||||
const sandbox_run_cmd = b.addRunArtifact(sandbox_exe);
|
||||
|
|
|
|||
|
|
@ -7,6 +7,10 @@
|
|||
.url = "git+https://github.com/raylib-zig/raylib-zig#a4d18b2d1cf8fdddec68b5b084535fca0475f466",
|
||||
.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 = .{
|
||||
"build.zig",
|
||||
|
|
|
|||
7
justfile
7
justfile
|
|
@ -42,7 +42,7 @@ check:
|
|||
test:
|
||||
zig build test
|
||||
|
||||
# auto-benchmark (ramps entities until performance degrades, works on linux/windows)
|
||||
# auto-benchmark (ramps entities until performance degrades)
|
||||
bench:
|
||||
zig build -Doptimize=ReleaseFast run -- --bench
|
||||
cat benchmark.log
|
||||
|
|
@ -58,3 +58,8 @@ bench-sw:
|
|||
bench-sw:
|
||||
@echo "bench-sw: windows doesn't have xvfb equivalent"
|
||||
@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
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
const std = @import("std");
|
||||
const rl = @import("raylib");
|
||||
const ztracy = @import("ztracy");
|
||||
const sandbox = @import("sandbox.zig");
|
||||
const ui = @import("ui.zig");
|
||||
const SsboRenderer = @import("ssbo_renderer.zig").SsboRenderer;
|
||||
|
|
@ -331,12 +332,16 @@ pub fn main() !void {
|
|||
|
||||
// update
|
||||
if (!paused) {
|
||||
const tracy_update = ztracy.ZoneN(@src(), "update");
|
||||
defer tracy_update.End();
|
||||
const update_start = std.time.microTimestamp();
|
||||
sandbox.update(&entities, &rng);
|
||||
update_time_us = std.time.microTimestamp() - update_start;
|
||||
}
|
||||
|
||||
// render
|
||||
const tracy_render = ztracy.ZoneN(@src(), "render");
|
||||
defer tracy_render.End();
|
||||
const render_start = std.time.microTimestamp();
|
||||
|
||||
rl.beginDrawing();
|
||||
|
|
@ -407,6 +412,9 @@ pub fn main() !void {
|
|||
const update_ms = @as(f32, @floatFromInt(update_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);
|
||||
|
||||
// tracy frame mark
|
||||
ztracy.FrameMark();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
const std = @import("std");
|
||||
const rl = @import("raylib");
|
||||
const ztracy = @import("ztracy");
|
||||
const sandbox = @import("sandbox.zig");
|
||||
|
||||
const SCREEN_WIDTH = sandbox.SCREEN_WIDTH;
|
||||
|
|
@ -142,6 +143,9 @@ pub const SsboRenderer = struct {
|
|||
rl.gl.rlDrawRenderBatchActive();
|
||||
|
||||
// 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| {
|
||||
self.gpu_buffer[i] = .{
|
||||
.x = entity.x,
|
||||
|
|
@ -149,10 +153,15 @@ pub const SsboRenderer = struct {
|
|||
.color = entity.color,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// upload to SSBO
|
||||
{
|
||||
const zone = ztracy.ZoneN(@src(), "ssbo_upload");
|
||||
defer zone.End();
|
||||
const data_size: u32 = @intCast(entities.count * @sizeOf(sandbox.GpuEntity));
|
||||
rl.gl.rlUpdateShaderBuffer(self.ssbo_id, self.gpu_buffer.ptr, data_size, 0);
|
||||
}
|
||||
|
||||
// bind shader
|
||||
rl.gl.rlEnableShader(self.shader_id);
|
||||
|
|
@ -183,9 +192,13 @@ pub const SsboRenderer = struct {
|
|||
rl.gl.rlSetBlendMode(@intFromEnum(rl.gl.rlBlendMode.rl_blend_alpha));
|
||||
|
||||
// bind VAO and draw
|
||||
{
|
||||
const zone = ztracy.ZoneN(@src(), "ssbo_draw");
|
||||
defer zone.End();
|
||||
_ = rl.gl.rlEnableVertexArray(self.vao_id);
|
||||
rl.gl.rlEnableVertexBuffer(self.vbo_id);
|
||||
rl.gl.rlDrawVertexArrayInstanced(0, 6, @intCast(entities.count));
|
||||
}
|
||||
|
||||
// cleanup - restore raylib's expected state
|
||||
rl.gl.rlDisableVertexArray();
|
||||
|
|
|
|||
Loading…
Reference in a new issue