diff --git a/.agents/skills/mirai-agent-api/references/methods.md b/.agents/skills/mirai-agent-api/references/methods.md index ed2af3827..b72e56630 100644 --- a/.agents/skills/mirai-agent-api/references/methods.md +++ b/.agents/skills/mirai-agent-api/references/methods.md @@ -320,6 +320,10 @@ Reading a capture: per second, the mean interval between two and its deviation (how even the pace is) and the latency from the start of a frame to its image. With generation there are twice as many images as frames. +- **The GPU of a frame.** A frame's `gpu_ms` and passes are those of its own submissions, at the + time they ran (`start_ms` since the frame began on the CPU). A frame whose submissions did not all + come back says `gpu_measured:false`, and the GPU numbers of the summary and of `profile.compare` + leave it out (`gpu_frames`). - **Own time.** `self_ms` excludes the zones inside: a function that waits for its tasks has them as children (`job`, `range`); what the workers run appears with `thread:"workers"`. - **Conditions.** `notes` warns about what invalidates a reading: engine at `-o:minimal`, diff --git a/.agents/skills/mirai-core/references/host.md b/.agents/skills/mirai-core/references/host.md index 247c36ec3..9d48dde8d 100644 --- a/.agents/skills/mirai-core/references/host.md +++ b/.agents/skills/mirai-core/references/host.md @@ -190,12 +190,15 @@ entities or persistent data. always covered, like the editor bars, is not drawn. **Profiling (`profile.odin`).** `profile_start(app, frames)` records the following frames: zones of -the main thread, one per loop function, the tasks of each worker and what the renderer -measures. The first `profile_start` reserves the memory: recording does -not allocate, without recording a zone costs one branch and what does not fit is counted in `lost`. -Whoever reads the zones as they happen (a profiling plugin) calls `profile_request` in every frame -and reads them with its own cursor; without a recording or a request, the device writes no -per-pass timestamps. The editor exposes it with +the main thread, one per loop function, the tasks of each worker, what the renderer measures and the +GPU passes of every submission of the frame (a capture or a thumbnail besides the window). Those +come back `rhi.MAX_FRAMES_IN_FLIGHT` submissions later, by `rhi` frame number, and go to the frame +that submitted them; a frame is measured once all of its submissions came back, and a recording runs +a few frames past its last one, keeping no zones, until they do. The first `profile_start` reserves +the memory: recording does not allocate, without recording a zone costs one branch and what does not +fit is counted in `lost`. Whoever reads the zones as they happen (a profiling plugin) calls +`profile_request` in every frame and reads them with its own cursor; without a recording or a +request, the device writes no per-pass timestamps. The editor exposes it with [`profile.*`](../../mirai-agent-api/references/methods.md#profiling). **Loads (`load_profile.odin`).** `app.load_profile` keeps the last load of each kind (scene, Play, diff --git a/.agents/skills/mirai-rendering/references/frame-graph.md b/.agents/skills/mirai-rendering/references/frame-graph.md index 390becdf7..51ba55004 100644 --- a/.agents/skills/mirai-rendering/references/frame-graph.md +++ b/.agents/skills/mirai-rendering/references/frame-graph.md @@ -223,6 +223,9 @@ or interleave its own passes. of `tick_now` (QPC on Windows, `CLOCK_MONOTONIC_RAW` on Linux). Without the extension, Vulkan places the last timestamp of the frame at the moment it collects it: it keeps the durations but arrives late; +- `renderer_gpu_submissions`: a copy of the timings of each submission since + `renderer_gpu_clear`, with the `rhi` frame it was (`rhi.frame_timings`), for whoever accounts for + every submission of a host frame; `Stats.timings` is only the last one the GPU finished; - visible, culled and occluded instances, probes and estimated rays per frame; - what was discarded by capacity ([UI limits](ui.md#capacities)); - materials, textures, their memory, reduced textures and compression diff --git a/editor/api_profile.odin b/editor/api_profile.odin index 1bdb7f57e..efa078bb0 100644 --- a/editor/api_profile.odin +++ b/editor/api_profile.odin @@ -72,7 +72,8 @@ Api_Profile_Frame_Stats :: struct { wait_ms:f32 `mirai:"label=Mean wait for a frame slot of the GPU"`, pacing_ms:f32 `mirai:"label=Mean wait for the frame limit or VSync"`, between_ms:f32 `mirai:"label=Mean from the end of a frame to the start of the next"`, - gpu_ms:f32 `mirai:"label=Mean of the GPU"`, + gpu_ms:f32 `mirai:"label=Mean of the GPU over the frames whose GPU times came back"`, + gpu_frames:int `mirai:"label=Frames whose every submission gave back its GPU times: gpu_ms and gpu count only them"`, bound:Api_Profile_Bounds `mirai:"label=Frames by what they waited for"`, spikes:int `mirai:"label=Frames over twice the median"`, worst:[]int `mirai:"label=The slowest frames by index; the slowest first"`, @@ -129,6 +130,7 @@ Api_Profile_Tree :: struct { } Api_Profile_Pass :: struct { name:string, + start_ms:f32 `mirai:"label=When it ran on the GPU, since the frame began on the CPU"`, ms:f32, } Api_Profile_Frame :: struct { @@ -138,7 +140,8 @@ Api_Profile_Frame :: struct { wait_ms:f32 `mirai:"label=For a frame slot of the GPU"`, pacing_ms:f32 `mirai:"label=For the frame limit or VSync"`, between_ms:f32 `mirai:"label=From its end to the start of the next"`, - gpu_ms:f32 `mirai:"label=Of the last frame the GPU finished"`, + gpu_ms:f32 `mirai:"label=Of its own submissions"`, + gpu_measured:bool `mirai:"label=Every submission of the frame gave back its GPU times; without them gpu_ms and gpu are partial"`, bound:Profile_Bound `mirai:"label=What it waited for"`, workers_busy_ms:f32, jobs:int, @@ -303,6 +306,8 @@ api_profile_capture :: proc(s:^Session,call:^api.Call) { return } api_frames(s,p.frames,p.dt) + // The GPU times of the last frames come back in the frames after them. + for late:=0;s.app.profiler.recording && late (out:Api_Profile_Summary) out.notes=notes return } - totals,cpu,wait,pacing,between,gpu:=make([]f64,n),make([]f64,n),make([]f64,n),make([]f64,n),make([]f64,n),make([]f64,n) + totals,cpu,wait,pacing,between:=make([]f64,n),make([]f64,n),make([]f64,n),make([]f64,n),make([]f64,n) for f,i in c.frames { totals[i],cpu[i],wait[i]=profile_frame_ms(f),profile_frame_cpu_ms(f),f64(f.wait_ms) - pacing[i],between[i],gpu[i]=f64(f.pacing_ns)/1e6,f64(f.between_ns)/1e6,f64(f.gpu_ms) + pacing[i],between[i]=f64(f.pacing_ns)/1e6,f64(f.between_ns)/1e6 switch profile_frame_bound(f) { case .Cpu: out.frame.bound.cpu+=1 case .Gpu: out.frame.bound.gpu+=1 @@ -377,7 +382,8 @@ profile_summary :: proc(c:^Profile_Capture,top:int) -> (out:Api_Profile_Summary) if spread.mean>0 {out.frame.fps=profile_ms(1000/spread.mean)} out.frame.cpu_ms,out.frame.wait_ms=profile_ms(profile_spread(cpu).mean),profile_ms(profile_spread(wait).mean) out.frame.pacing_ms,out.frame.between_ms=profile_ms(profile_spread(pacing).mean),profile_ms(profile_spread(between).mean) - out.frame.gpu_ms=profile_ms(profile_spread(gpu).mean) + gpu:=profile_gpu_ms(c) + out.frame.gpu_ms,out.frame.gpu_frames=profile_ms(profile_spread(gpu).mean),len(gpu) for t in totals {if t>2*spread.p50 {out.frame.spikes+=1}} out.frame.worst=profile_worst(c,min(3,n)) out.presentation=profile_presentation(c,spread.mean*f64(n)) @@ -527,6 +533,9 @@ profile_notes :: proc(c:^Profile_Capture,out:Api_Profile_Summary) -> []string { append(¬es,fmt.tprintf("%d of %d frames wait for the GPU (wait_ms): CPU savings will not shorten them; the gpu passes say where the GPU goes",out.frame.bound.gpu,out.frame.count)) if k.vsync {append(¬es,"vsync is on: the wait for a frame slot includes the display; with VSync off (settings.set) it is only the GPU")} } + drawn:=0 + for f in c.frames {if f.drawn {drawn+=1}} + if k.backend!="" && out.frame.gpu_frames0 {append(¬es,fmt.tprintf("%d frames took more than twice the median: profile.frames shows the worst with their zones",out.frame.spikes))} if c.lost>0 {append(¬es,fmt.tprintf("%d zones were lost: record fewer frames for an exact count",c.lost))} @@ -562,6 +571,7 @@ profile_frame_out :: proc(c:^Profile_Capture,index,depth:int,min_ms:f64) -> (out out.index=index out.ms,out.cpu_ms,out.wait_ms=profile_ms(profile_frame_ms(f)),profile_ms(profile_frame_cpu_ms(f)),profile_ms(f64(f.wait_ms)) out.pacing_ms,out.between_ms,out.gpu_ms=profile_ms(f64(f.pacing_ns)/1e6),profile_ms(f64(f.between_ns)/1e6),profile_ms(f64(f.gpu_ms)) + out.gpu_measured=f.gpu_measured out.bound=profile_frame_bound(f) out.workers_busy_ms,out.jobs=profile_ms(f64(f.worker_ns)/1e6),f.jobs zones:=make([dynamic]Api_Profile_Tree) @@ -572,7 +582,7 @@ profile_frame_out :: proc(c:^Profile_Capture,index,depth:int,min_ms:f64) -> (out } out.zones=zones[:] passes:=make([dynamic]Api_Profile_Pass) - for pass in c.passes[f.pass_first:][:f.pass_count] {append(&passes,Api_Profile_Pass{c.pass_names[pass.name],profile_ms(f64(pass.ms))})} + for pass in c.passes[f.pass_first:][:f.pass_count] {append(&passes,Api_Profile_Pass{c.pass_names[pass.name],profile_ms(f64(pass.start_ns-f.start_ns)/1e6),profile_ms(f64(pass.ns)/1e6)})} slice.sort_by(passes[:],proc(a,b:Api_Profile_Pass) -> bool {return a.ms>b.ms}) out.gpu=passes[:min(len(passes),8)] return @@ -688,7 +698,7 @@ profile_compare :: proc(a,b:^Profile_Capture,top:int) -> (out:Api_Profile_Compar fa,fb:=profile_spread(frame_values(a,0)),profile_spread(frame_values(b,0)) out.mean_ms,out.p95_ms=delta(fa.mean,fb.mean),delta(fa.p95,fb.p95) out.cpu_ms=delta(profile_spread(frame_values(a,1)).mean,profile_spread(frame_values(b,1)).mean) - out.gpu_ms=delta(profile_spread(frame_values(a,2)).mean,profile_spread(frame_values(b,2)).mean) + out.gpu_ms=delta(profile_spread(profile_gpu_ms(a)).mean,profile_spread(profile_gpu_ms(b)).mean) ma,mb:=means(a),means(b) changes:=make([dynamic]Api_Profile_Change) @@ -735,7 +745,6 @@ frame_values :: proc(c:^Profile_Capture,which:int) -> []f64 { switch which { case 0: values[i]=profile_frame_ms(f) case 1: values[i]=profile_frame_cpu_ms(f) - case 2: values[i]=f64(f.gpu_ms) } } return values @@ -811,7 +820,8 @@ api_profile_export :: proc(s:^Session,call:^api.Call) { } // The capture in the Trace Event Format: a complete event per zone, a thread -// per ring, and counters with the time of each frame and of the GPU. +// per ring, one per GPU pass where it ran, and counters with the time of +// each frame and of the GPU. profile_trace :: proc(c:^Profile_Capture) -> (text:string,events:int) { b:=strings.builder_make(context.temp_allocator) strings.write_string(&b,`{"displayTimeUnit":"ms","traceEvents":[`) @@ -826,6 +836,19 @@ profile_trace :: proc(c:^Profile_Capture) -> (text:string,events:int) { fmt.sbprintf(&b,`{{"name":"thread_name","ph":"M","pid":1,"tid":%d,"args":{{"name":"%s"}}}}`,thread,thread==0?"main":fmt.tprintf("worker %d",thread)) events+=1 } + gpu_thread:=max(c.threads,1) + if len(c.passes)>0 { + event(&b,&first) + fmt.sbprintf(&b,`{{"name":"thread_name","ph":"M","pid":1,"tid":%d,"args":{{"name":"GPU"}}}}`,gpu_thread) + events+=1 + } + for pass in c.passes { + event(&b,&first) + strings.write_string(&b,`{"name":`) + strings.write_string(&b,serial.text_quote(c.pass_names[pass.name])) + fmt.sbprintf(&b,`,"ph":"X","pid":1,"tid":%d,"ts":%.3f,"dur":%.3f}}`,gpu_thread,f64(pass.start_ns)/1e3,f64(pass.ns)/1e3) + events+=1 + } for f,i in c.frames { event(&b,&first) fmt.sbprintf(&b,`{{"name":"frame %d","ph":"X","pid":1,"tid":0,"ts":%.3f,"dur":%.3f}}`,i,f64(f.start_ns)/1e3,f64(f.ns)/1e3) diff --git a/editor/profile.odin b/editor/profile.odin index 3e7cd50db..e96dda6c6 100644 --- a/editor/profile.odin +++ b/editor/profile.odin @@ -61,7 +61,8 @@ Profile_Frame_Record :: struct { start_ns,ns:i64, // the frame zone, since the first frame began between_ns:i64, // from its end to the start of the next one: what runs between frames pacing_ns:i64, - wait_ms,gpu_ms:f32, // the wait for a frame slot, and the last frame the GPU finished + wait_ms,gpu_ms:f32, // the wait for a frame slot, and the sum of its own GPU passes + gpu_measured:bool, // every submission of the frame gave back its passes render_prepare_ms,render_record_ms,render_end_ms:f32, drawn:bool, generated:bool, // it drew an image shown before its own @@ -79,7 +80,10 @@ Profile_Zone_Record :: struct { start_ns,ns,self_ns:i64, } -Profile_Pass_Record :: struct {name:int,ms:f32} +Profile_Pass_Record :: struct { + name:int, + start_ns,ns:i64, // where it ran on the GPU, on the clock of the zones +} Profile_Capture :: struct { arena:vmem.Arena, @@ -280,14 +284,14 @@ profile_build :: proc(c:^Profile_Capture,recording:host.Profile_Recording,app:^h append(&pass_names,strings.clone(host.profile_pass_name(app,pass.name))) pass_ids[pass.name]=index } - c.passes[i]={index,pass.ms} + c.passes[i]={index,pass.start_ns-epoch,max(pass.end_ns-pass.start_ns,0)} } c.pass_names=pass_names[:] for frame,i in recording.frames { // The size the scene was drawn at is known once a frame drew. if c.conditions.render=={} && frame.render_width>0 {c.conditions.render={int(frame.render_width),int(frame.render_height)}} record:=&c.frames[i] - record.wait_ms,record.gpu_ms=frame.render_wait_ms,frame.gpu_ms + record.wait_ms,record.gpu_ms,record.gpu_measured=frame.render_wait_ms,frame.gpu_ms,host.profile_gpu_measured(frame) record.render_prepare_ms,record.render_record_ms,record.render_end_ms=frame.render_prepare_ms,frame.render_record_ms,frame.render_end_ms record.drawn=frame.drawn record.generated,record.presented,record.intervals=frame.generated,int(frame.presented),int(frame.intervals) @@ -439,13 +443,29 @@ profile_is_part :: proc(name:string) -> bool { return false } -// The per frame time of each pass of the GPU. +// The time of each pass of the GPU in each frame whose GPU times came back. profile_passes_by_name :: proc(c:^Profile_Capture,allocator:=context.temp_allocator) -> [][]f64 { context.allocator=allocator + measured:=profile_gpu_frames(c) values:=make([][]f64,len(c.pass_names)) - for &v in values {v=make([]f64,len(c.frames))} - for frame,i in c.frames { - for pass in c.passes[frame.pass_first:][:frame.pass_count] {values[pass.name][i]+=f64(pass.ms)} + for &v in values {v=make([]f64,measured)} + i:=0 + for frame in c.frames { + if !frame.gpu_measured {continue} + for pass in c.passes[frame.pass_first:][:frame.pass_count] {values[pass.name][i]+=f64(pass.ns)/1e6} + i+=1 } return values } + +profile_gpu_frames :: proc(c:^Profile_Capture) -> (measured:int) { + for frame in c.frames {if frame.gpu_measured {measured+=1}} + return +} + +// The GPU time of each frame whose GPU times came back. +profile_gpu_ms :: proc(c:^Profile_Capture,allocator:=context.temp_allocator) -> []f64 { + values:=make([dynamic]f64,0,len(c.frames),allocator) + for frame in c.frames {if frame.gpu_measured {append(&values,f64(frame.gpu_ms))}} + return values[:] +} diff --git a/editor/profile_test.odin b/editor/profile_test.odin index 28130f6ca..4b215ff52 100644 --- a/editor/profile_test.odin +++ b/editor/profile_test.odin @@ -39,8 +39,9 @@ recording_fixture :: proc(a_ms:i64) -> host.Profile_Recording { ) } frames:=make([]host.Profile_Frame,2,context.temp_allocator) - frames[0]={number=5,drawn=true,render_wait_ms=0.5,gpu_ms=3} - frames[1]={number=6,drawn=true,render_wait_ms=0.5,gpu_ms=3} + // The second submitted twice and only one came back. + frames[0]={number=5,drawn=true,render_wait_ms=0.5,gpu_ms=3,device_first=1,device_end=2,gpu_submissions=1} + frames[1]={number=6,drawn=true,render_wait_ms=0.5,gpu_ms=1,device_first=2,device_end=4,gpu_submissions=1} return {frames=frames,zones=zones[:],threads=2,complete=true} } @@ -102,6 +103,8 @@ the_summary_ranks_zones_by_their_own_time_and_reads_what_bounds_the_frames :: pr for part in summary.parts {parts[part.name]=true} testing.expect(t,parts["editor"] && parts["pacing"] && parts["other"] && parts["between"],"the parts of the frame, what they leave and what runs between frames") testing.expect_value(t,summary.frame.bound.cpu,2) + testing.expect_value(t,summary.frame.gpu_frames,1) + testing.expect_value(t,summary.frame.gpu_ms,3.0) // the partial frame is left out testing.expect_value(t,summary.workers.threads,1) testing.expect_value(t,summary.workers.busy_ms,2.0) testing.expect(t,len(summary.notes)>0,"two frames are too few for percentiles") diff --git a/host/profile.odin b/host/profile.odin index 34c6ddce5..62fb412cb 100644 --- a/host/profile.odin +++ b/host/profile.odin @@ -11,10 +11,10 @@ import "mirai:std/jobs" // methods of the editor): on the main thread a zone for the frame and one for // each of its parts, and one for each function of the loops, which the world // opens with profile_step_begin; on the ring of each worker of the job -// system its jobs; and per frame what the -// zones do not carry, the times of the -// renderer and of the GPU. Rings are drained at the end of every frame, so a -// ring only has to hold one frame. While nothing records a zone costs a +// system its jobs; and per frame what the zones do not carry, the times of +// the renderer and the GPU passes of every submission of the frame, which +// come back rhi.MAX_FRAMES_IN_FLIGHT submissions later. Rings are drained at +// the end of every frame, so a ring only has to hold one frame. While nothing records a zone costs a // branch and the GPU reports only its whole frame. The first profile_start // reserves the memory; a recording allocates nothing. Whoever reads the zones // as they happen (a profiler plugin) asks for them every frame with @@ -27,6 +27,7 @@ PROFILE_PASSES :: 1<<17 // GPU pass times of a whole recording PROFILE_PASS_NAMES :: 128 // the last one names every pass that did not fit PROFILE_PASS_NAME_BYTES :: 32 PROFILE_MAIN :: jobs.OWNER // the ring of the main thread, worker 0 of the job system +PROFILE_GPU_LATE :: rhi.MAX_FRAMES_IN_FLIGHT+2 // frames a recording runs past its last one for the GPU times of that one Profile_Frame :: struct { number:u32, // the frame of base.Profile that the zones of this frame carry @@ -35,7 +36,9 @@ Profile_Frame :: struct { render_wait_ms:f32, // the wait for a frame slot of the GPU (and for the display with vsync) render_record_ms:f32, render_end_ms:f32, - gpu_ms:f32, // the last frame the GPU finished + gpu_ms:f32, // the sum of its own passes + device_first,device_end:u64, // the rhi frames it submitted, [first,end) + gpu_submissions:u32, // of them, those whose passes came back render_width,render_height:u32, generated:bool, // it drew an image shown before its own (frame generation) presented:u32, // images given to the display since the frame before @@ -47,7 +50,12 @@ Profile_Frame :: struct { Profile_Pass :: struct { name:int, // into the names of the pass of the Profiler - ms:f32, + start_ns,end_ns:i64, // on the clock of the zones +} + +// Whether every submission of the frame gave back its GPU passes. +profile_gpu_measured :: proc "contextless" (f:Profile_Frame) -> bool { + return f.device_end>f.device_first && u64(f.gpu_submissions)==f.device_end-f.device_first } Profiler :: struct { @@ -55,6 +63,7 @@ Profiler :: struct { rings_ready:bool, // the base.Profile rings, which profile_request needs without the recording buffers reserved:bool, // the recording buffers below recording:bool, + late:int, // >0: the frames asked for were recorded and the recording waits for the GPU times of the last ones, this many frames at most requested:u8, // frames profile_request keeps the zones and the GPU passes on wanted:int, // frames the recording ends after frames:[]Profile_Frame, @@ -65,6 +74,8 @@ Profiler :: struct { cursors:[]u64, // per ring: the next zone to drain passes:[]Profile_Pass, pass_count:int, + gpu_next:int, // the first frame that may still get GPU passes + device_first:u64, // rhi.frame_number when the frame being recorded began pass_names:[][PROFILE_PASS_NAME_BYTES]u8, // the GPU reuses the memory of its names every frame pass_name_lengths:[]u8, pass_name_count:int, @@ -90,7 +101,7 @@ profile_start :: proc(app:^App,frames:int) -> bool { if p.recording || frames<=0 {return false} if !p.reserved && !profile_reserve(app) {return false} p.wanted=min(frames,PROFILE_FRAMES_MAX) - p.frame_count,p.zone_count,p.pass_count,p.lost=0,0,0,0 + p.frame_count,p.zone_count,p.pass_count,p.lost,p.gpu_next,p.late=0,0,0,0,0,0 p.frame_zone={} if app.device!=nil {p.presented=rhi.present_stats(app.device)} out:[0]base.Profile_Zone @@ -105,7 +116,7 @@ profile_stop :: proc(app:^App) { p:=&app.profiler if !p.recording {return} base.profile_set_enabled(&p.profile,false) - p.recording=false + p.recording,p.late=false,0 } profile_recording :: proc(app:^App) -> Profile_Recording { @@ -129,7 +140,7 @@ profile_request :: proc(app:^App) -> bool { // Whether the zones and the GPU passes are being recorded. profile_active :: proc "contextless" (app:^App) -> bool { p:=&app.profiler - return p.rings_ready && (p.recording || p.requested>0) + return p.rings_ready && ((p.recording && p.late==0) || p.requested>0) } // The rings: one per thread, the main one first. @@ -182,6 +193,7 @@ profile_frame_open :: proc(app:^App) { active:=profile_active(app) if p.rings_ready {base.profile_set_enabled(&p.profile,active)} if !active {return} + if app.device!=nil {p.device_first=rhi.frame_number(app.device)} base.profile_frame(&p.profile) p.frame_zone=base.profile_begin(&p.profile,PROFILE_MAIN,"frame") } @@ -207,11 +219,15 @@ profile_frame_end :: proc(app:^App,drawn:bool) { opened:=p.frame_zone.profile!=nil if opened {profile_frame_close(app)} if p.requested>0 {p.requested-=1} - if !p.recording {return} - if opened && p.frame_count=len(p.passes) {break} - p.passes[p.pass_count]={profile_pass_intern(p,timing.name),timing.ms} - p.pass_count+=1 - } } } - frame.pass_count=p.pass_count-frame.pass_first p.frame_count+=1 } + if r:=app.renderer;r!=nil { + epoch:=profile_epoch_ns(app) + for submission in render.renderer_gpu_submissions(r) {profile_gpu_add(p,submission.frame,submission.timings,epoch)} + render.renderer_gpu_clear(r) + } for &cursor,thread in p.cursors { + if p.late>0 { + out:[0]base.Profile_Zone + _,cursor,_=base.profile_read_since(&p.profile,thread,0,out[:]) + continue + } count,next,lost:=base.profile_read_since(&p.profile,thread,cursor,p.zones[p.zone_count:]) p.zone_count+=count p.lost+=lost cursor=next } - if p.frame_count>=p.wanted {profile_stop(app)} + if p.frame_countf.device_first && !profile_gpu_measured(f) {waiting=true;break} + } + } + if !waiting || p.late>=PROFILE_GPU_LATE {profile_stop(app);return} + p.late+=1 +} + +// Gives the GPU timings of the rhi frame to the recorded frame that +// submitted it. Batches come in the order they were submitted, so the passes +// of a frame stay together; those of a submission between frames, or of +// one before the recording, belong to no frame and are left out. +profile_gpu_add :: proc(p:^Profiler,frame:u64,timings:[]rhi.Gpu_Timing,epoch_ns:i64) { + for p.gpu_next=f.device_end {p.gpu_next+=1;continue} + if f.gpu_submissions==0 {f.pass_first=p.pass_count} + for timing in timings { + if p.pass_count>=len(p.passes) {break} + p.passes[p.pass_count]={profile_pass_intern(p,timing.name),timing.start_ns-epoch_ns,timing.end_ns-epoch_ns} + p.pass_count+=1 + f.gpu_ms+=timing.ms + } + f.pass_count=p.pass_count-f.pass_first + f.gpu_submissions+=1 + return + } } @(private) diff --git a/host/profile_gpu_test.odin b/host/profile_gpu_test.odin new file mode 100644 index 000000000..ebf910f15 --- /dev/null +++ b/host/profile_gpu_test.odin @@ -0,0 +1,42 @@ +package host + +import "core:testing" +import render "mirai:render" +import "mirai:rhi" +import "mirai:rhi/shared_device" + +// A frame that submits twice (an offscreen capture, then its window) gets +// the GPU passes of both submissions, though they come back in later frames. +@(test) +a_recorded_frame_keeps_the_gpu_passes_of_every_submission :: proc(t: ^testing.T) { + shared_device.hold(t) + app, error := app_create({gpu = true, gpu_validation = true, window_desc = {size = {64, 48}}, fixed_dt = 1.0 / 60, threads = 1}) + if app == nil { + testing.expectf(t, rhi.gpu_skip_allowed(), "no application with a GPU: %v (set MIRAI_ALLOW_GPU_SKIP=1 to skip)", error) + return + } + defer app_destroy(app) + FRAMES :: 4 + if !testing.expect(t, profile_start(app, FRAMES)) {return} + for _ in 0 ..< FRAMES + PROFILE_GPU_LATE { + if !app.profiler.recording {break} + if !testing.expect(t, app_frame_begin(app)) {return} + if app.profiler.late == 0 {testing.expect(t, render.renderer_frame(app.renderer, {offscreen = true, ui_only = true, time = app.time}), app.renderer.error)} + app_frame_end(app) + } + recording := profile_recording(app) + testing.expect(t, recording.complete, "the recording waited for the GPU times of its last frame") + testing.expect_value(t, len(recording.frames), FRAMES) + for frame, i in recording.frames { + testing.expectf(t, frame.drawn, "frame %d drew", i) + testing.expect_value(t, frame.device_end - frame.device_first, 2) + testing.expectf(t, profile_gpu_measured(frame), "frame %d got %d of its 2 submissions", i, frame.gpu_submissions) + total: f64 + for pass in recording.passes[frame.pass_first:][:frame.pass_count] { + testing.expectf(t, pass.end_ns >= pass.start_ns, "frame %d: a pass ends before it starts", i) + total += f64(pass.end_ns - pass.start_ns) / 1e6 + } + testing.expectf(t, abs(total - f64(frame.gpu_ms)) < 0.01, "frame %d: gpu_ms %.3f is not the sum of its passes %.3f", i, frame.gpu_ms, total) + } + testing.expectf(t, rhi.device_validation_errors(app.device) == 0, "validation: %s", rhi.device_validation_message(app.device)) +} diff --git a/plugins/profiler/editor/profiler.odin b/plugins/profiler/editor/profiler.odin index 5fb10a824..97dc9f0f1 100644 --- a/plugins/profiler/editor/profiler.odin +++ b/plugins/profiler/editor/profiler.odin @@ -130,12 +130,12 @@ profiler_header :: proc(s:^editor.Session,p:^Profiler_Panel,app:^host.App) { @(private="file") profiler_show :: proc(p:^Profiler_Panel,c:^editor.Profile_Capture) { p.shown,p.frame,p.workers,p.depth,p.thread_count,p.top_count=c.id,0,0,1,0,0 - frame,gpu:=make([]f64,len(c.frames),context.temp_allocator),make([]f64,len(c.frames),context.temp_allocator) + frame:=make([]f64,len(c.frames),context.temp_allocator) for f,i in c.frames { p.workers+=f.zone_count - frame[i],gpu[i]=editor.profile_frame_ms(f),f64(f.gpu_ms) + frame[i]=editor.profile_frame_ms(f) } - spread,gpu_spread:=editor.profile_spread(frame),editor.profile_spread(gpu) + spread,gpu_spread:=editor.profile_spread(frame),editor.profile_spread(editor.profile_gpu_ms(c)) p.graph_ms=max(spread.p95,gpu_spread.p95,0.001)*1.5 nearest:=max(f64) for ms,i in frame { @@ -172,9 +172,9 @@ profiler_show :: proc(p:^Profiler_Panel,c:^editor.Profile_Capture) { @(private="file") profiler_summary :: proc(p:^Profiler_Panel,c:^editor.Profile_Capture) -> string { - frame,gpu:=make([]f64,len(c.frames),context.temp_allocator),make([]f64,len(c.frames),context.temp_allocator) - for f,i in c.frames {frame[i],gpu[i]=editor.profile_frame_ms(f),f64(f.gpu_ms)} - spread,gpu_spread:=editor.profile_spread(frame),editor.profile_spread(gpu) + frame:=make([]f64,len(c.frames),context.temp_allocator) + for f,i in c.frames {frame[i]=editor.profile_frame_ms(f)} + spread,gpu_spread:=editor.profile_spread(frame),editor.profile_spread(editor.profile_gpu_ms(c)) return fmt.tprintf("Capture %d · %d frames · frame %.2f ms mean, %.2f ms p95 · GPU %.2f ms · %s %s", c.id,len(c.frames),spread.mean,spread.p95,gpu_spread.mean,c.conditions.backend,c.conditions.device) } @@ -184,8 +184,9 @@ profiler_frame_text :: proc(p:^Profiler_Panel,c:^editor.Profile_Capture) -> stri if p.frame>=len(c.frames) {return ""} f:=c.frames[p.frame] bounds:=[editor.Profile_Bound]string{.Cpu="CPU",.Gpu="GPU",.Paced="pacing"} - return fmt.tprintf("Frame %d · %.2f ms · CPU %.2f ms · GPU %.2f ms · waits for %s", - p.frame+1,editor.profile_frame_ms(f),editor.profile_frame_cpu_ms(f),f.gpu_ms,bounds[editor.profile_frame_bound(f)]) + gpu:=fmt.tprintf("%.2f ms",f.gpu_ms) if f.gpu_measured else "not measured" + return fmt.tprintf("Frame %d · %.2f ms · CPU %.2f ms · GPU %s · waits for %s", + p.frame+1,editor.profile_frame_ms(f),editor.profile_frame_cpu_ms(f),gpu,bounds[editor.profile_frame_bound(f)]) } // A color of its own for each name, among muted hues. @@ -217,6 +218,7 @@ frames_draw :: proc(ctx:^ui.Context,rect:ui.Rect,user:rawptr) { x:=rect.x+f32(i)*step ui.draw_rect(ctx,{x+step*0.12,rect.y+rect.h-2-h,max(step*0.76,1),h},color) if ms>top {ui.draw_rect(ctx,{x,rect.y+2,step,3},ui.tone(ctx,"danger"))} + if !f.gpu_measured {continue} gpu:=f32(min(f64(f.gpu_ms)/top,1))*(rect.h-4) ui.draw_rect(ctx,{x,rect.y+rect.h-2-gpu,step,1.5},ui.tone(ctx,"text")) } @@ -228,15 +230,16 @@ frames_draw :: proc(ctx:^ui.Context,rect:ui.Rect,user:rawptr) { } // The frame chosen: the zones of the main thread by depth, those of the -// other threads, and the GPU passes one after another, on one time axis. +// other threads, and the GPU passes where they ran, on one time axis. @(private="file") timeline_draw :: proc(ctx:^ui.Context,rect:ui.Rect,user:rawptr) { p:=(^Profiler_Panel)(user) c:=p.capture if c==nil || p.frame>=len(c.frames) {return} f:=c.frames[p.frame] - gpu_ns:=i64(f64(f.gpu_ms)*1e6) - span:=max(f.ns+f.between_ns,gpu_ns,1) + passes:=c.passes[f.pass_first:][:f.pass_count] + span:=max(f.ns+f.between_ns,1) + for pass in passes {span=max(span,pass.start_ns+pass.ns-f.start_ns)} label:=f32(64) x0:=rect.x+label w:=rect.w-label @@ -264,12 +267,10 @@ timeline_draw :: proc(ctx:^ui.Context,rect:ui.Rect,user:rawptr) { row_y+=ROW+2 } ui.draw_text(ctx,"GPU",rect.x,row_y+2,ui.metric(ctx,"font_small"),ui.tone(ctx,"text_muted")) - at:=f32(0) - for pass in c.passes[f.pass_first:][:f.pass_count] { - width:=f32(f64(pass.ms)*1e6)*scale - bar(ctx,x0+at,row_y,width,c.pass_names[pass.name],f64(pass.ms)) - at+=width + for pass in passes { + bar(ctx,x0+f32(max(pass.start_ns-f.start_ns,0))*scale,row_y,f32(pass.ns)*scale,c.pass_names[pass.name],f64(pass.ns)/1e6) } + if !f.gpu_measured {ui.draw_text(ctx,"not measured",x0+4,row_y+2,ui.metric(ctx,"font_small"),ui.tone(ctx,"text_muted"))} ruler:=row_y+ROW+4 for i in 0..=4 { x:=x0+w*f32(i)/4 diff --git a/render/frame.odin b/render/frame.odin index 954a33de1..0a00cb4f2 100644 --- a/render/frame.odin +++ b/render/frame.odin @@ -157,7 +157,7 @@ read_stats :: proc(r: ^Renderer, slot: int) { r.stats.occluded_instances = stats.occluded_instances r.stats.probes_traced = stats.probes_traced } - r.stats.timings = rhi.frame_timings(r.device) + r.stats.timings, _, _ = rhi.frame_timings(r.device) total: f32 for t in r.stats.timings {total += t.ms} r.stats.gpu_ms = total @@ -457,6 +457,7 @@ renderer_frame :: proc(r:^Renderer,frame:Frame_Desc) -> bool { } prepared:=time.tick_now() cmd,began:=rhi.frame_begin(d,!frame.offscreen) + gpu_submission_keep(r) if !began { set_error(r,"the device gave no frame to draw: %s",rhi.device_error(d)!=""?rhi.device_error(d):"the window is being resized or its image is not ready") return false @@ -1765,6 +1766,52 @@ update_stats :: proc(r: ^Renderer) { renderer_stats :: proc(r: ^Renderer) -> ^Stats {return &r.stats} +RENDER_GPU_SUBMISSIONS :: 8 // whose timings wait between two renderer_gpu_clear +RENDER_GPU_TIMINGS :: 1024 + +// The GPU timings of one submission of the device, copied out of it, which +// reuses its own at the next frame. +Gpu_Submission :: struct { + frame: u64, // the rhi.frame_number it was + timings: []rhi.Gpu_Timing, +} + +Gpu_Submissions :: struct { + list: [RENDER_GPU_SUBMISSIONS]Gpu_Submission, + count: int, + timings: [RENDER_GPU_TIMINGS]rhi.Gpu_Timing, + names: [RENDER_GPU_TIMINGS][rhi.MAX_TIMESTAMP_NAME_BYTES]u8, + timing_count: int, + kept_end: u64, // one past the rhi frame of the last submission seen +} + +// The submissions whose GPU timings came back since renderer_gpu_clear, in +// the order they were submitted. A host frame may submit several (captures, +// thumbnails); whoever accounts for each one takes them every frame and +// clears them. A submission that does not fit is left out. +renderer_gpu_submissions :: proc(r: ^Renderer) -> []Gpu_Submission {return r.submissions.list[:r.submissions.count]} + +renderer_gpu_clear :: proc(r: ^Renderer) {r.submissions.count, r.submissions.timing_count = 0, 0} + +@(private) +gpu_submission_keep :: proc(r: ^Renderer) { + timings, frame, measured := rhi.frame_timings(r.device) + g := &r.submissions + // A frame_begin that fails before the device collects leaves the timings of the one before. + if !measured || frame < g.kept_end {return} + g.kept_end = frame + 1 + if g.count >= len(g.list) || g.timing_count + len(timings) > len(g.timings) {return} + first := g.timing_count + for timing, i in timings { + k := first + i + g.timings[k] = timing + g.timings[k].name = string(g.names[k][:copy(g.names[k][:], timing.name)]) + } + g.timing_count += len(timings) + g.list[g.count] = {frame, g.timings[first:g.timing_count]} + g.count += 1 +} + // Stats with the counters of the last frame, which it reads back by the time // a later frame needs its slot: for whoever has just read an image of that // frame (renderer_capture, view_texture_capture), so the GPU is idle. diff --git a/render/renderer.odin b/render/renderer.odin index 0b62d6f1f..4b7bfe0de 100644 --- a/render/renderer.odin +++ b/render/renderer.odin @@ -266,7 +266,7 @@ Stats :: struct { probes_per_frame: u32, probes_traced: u32, // in the frame two frames ago rays_per_frame: u64, - gpu_ms: f32, + gpu_ms: f32, // of the last frame the GPU finished ev100: f32, // the main view is exposed at, before its compensation: what its meter took, as read back, or what its camera gives timings: []rhi.Gpu_Timing, // Time of the calling thread in the parts of the last frame, in ms. @@ -669,6 +669,7 @@ Renderer :: struct { pixel_density: f32, // device pixels per native window coordinate frame: u64, stats: Stats, + submissions: Gpu_Submissions, error_buffer: [512]u8, error: string, problem_buffer: [1024]u8, // of Stats.problem diff --git a/rhi/metal_device.odin b/rhi/metal_device.odin index d580f9d25..219f29c78 100644 --- a/rhi/metal_device.odin +++ b/rhi/metal_device.odin @@ -103,6 +103,8 @@ Mtl_Frame :: struct { query_bytes: [MAX_TIMESTAMPS][MAX_TIMESTAMP_NAME_BYTES]u8, encoder_count: u32, encoder_marks: [MAX_TIMESTAMPS]u16, // of each sampled encoder: the mark it runs after + number: u64, // the frame_number it committed + submitted: bool, // until frame_begin collects its timings paced: bool, // the frame draws into a pair of the presenter begun_ns: i64, submitted_ns: i64, @@ -238,6 +240,8 @@ Device :: struct { timings: [MAX_TIMESTAMPS]Gpu_Timing, timing_bytes: [MAX_TIMESTAMPS][MAX_TIMESTAMP_NAME_BYTES]u8, timing_count: int, + timing_frame: u64, // the frame_number the timings measured + timing_measured: bool, // the timings are of a committed frame timestamps_wanted: bool, clock_cpu, clock_gpu: MTL.Timestamp, // sampled together when the device was made: the rate of the GPU clock error_buffer: [512]u8, @@ -798,7 +802,13 @@ mtl_flush_ranges :: proc(d: ^Device, f: ^Mtl_Frame) { // ---------------------------------------------------------------- frames -frame_timings :: proc(d: ^Device) -> []Gpu_Timing {return d.timings[:d.timing_count]} +// The timings the last frame_begin collected and the frame_number of the +// frame they measured, MAX_FRAMES_IN_FLIGHT before it; measured is false when +// that slot had committed nothing since its timings were last collected, or +// the device could not read its times. +frame_timings :: proc(d: ^Device) -> (timings: []Gpu_Timing, frame: u64, measured: bool) { + return d.timings[:d.timing_count], d.timing_frame, d.timing_measured +} // Asks for one timing per cmd_timestamp interval in the frames that begin // from now on; without it a frame reports only its whole GPU time. @@ -878,8 +888,13 @@ frame_begin :: proc(d: ^Device, present := true) -> (cmd: ^Command_List, ok: boo if d.in_frame {set_error(d, "frame_begin while a frame is being recorded: end it with frame_end first"); return nil, false} f := &d.frames[d.slot] d.timing_count = 0 + d.timing_frame, d.timing_measured = f.number, false if f.cmd != nil { - if mtl_wait(d, f.cmd, "frame") {mtl_collect_timestamps(d, f)} + if mtl_wait(d, f.cmd, "frame") { + mtl_collect_timestamps(d, f) + d.timing_measured = f.submitted + } + f.submitted = false mtl_release(f.cmd) f.cmd = nil } @@ -961,6 +976,7 @@ frame_end :: proc(d: ^Device, cmd: ^Command_List, generated := false) -> bool { } MTL.CommandBuffer_commit(f.cmd) f.submitted_ns = present_now_ns() + f.number, f.submitted = d.frame_number, true if presented {present_count_direct(d)} mtl_release_drawable(d) if f.paced { diff --git a/rhi/tests/rhi_test.odin b/rhi/tests/rhi_test.odin index 4d5458e24..4c524dfc0 100644 --- a/rhi/tests/rhi_test.odin +++ b/rhi/tests/rhi_test.odin @@ -802,7 +802,11 @@ rhi_timestamps_report_ordered_nanoseconds :: proc(t: ^testing.T) { cmd, ok := rhi.frame_begin(d, false) if !testing.expect(t, ok) {return nil} testing.expect(t, rhi.frame_end(d, cmd)) - return rhi.frame_timings(d) + timings, frame, measured := rhi.frame_timings(d) + // The last frame_begin collected the frame its slot submitted before it. + testing.expect(t, measured, "the slot had submitted a frame") + testing.expect_value(t, frame, rhi.frame_number(d) - 1 - rhi.MAX_FRAMES_IN_FLIGHT) + return timings } now := time.duration_nanoseconds(time.tick_diff({}, time.tick_now())) frame := timings_of(t, d, buffer, false) diff --git a/rhi/vulkan_device.odin b/rhi/vulkan_device.odin index ae947e0f0..d67b347ff 100644 --- a/rhi/vulkan_device.odin +++ b/rhi/vulkan_device.odin @@ -128,6 +128,8 @@ Vk_Frame :: struct { acquired: bool, paced: bool, // the frame draws into a pair of the presenter begun_ns: i64, + number: u64, // the frame_number it submitted + submitted: bool, // until frame_begin collects its timings submitted_ns: i64, marks: Vk_Marks, // where the GPU went (vulkan_marks.odin) } @@ -247,6 +249,8 @@ Device :: struct { timings: [MAX_TIMESTAMPS]Gpu_Timing, timing_bytes: [MAX_TIMESTAMPS][MAX_TIMESTAMP_NAME_BYTES]u8, // the names of timings, kept until the next ones timing_count: int, + timing_frame: u64, // the frame_number the timings measured + timing_measured: bool, // the timings are of a submitted frame timestamps_wanted: bool, timestamp_bits: u32, // timestampValidBits of the queue: a GPU timestamp wraps past them // VK_KHR or VK_EXT_calibrated_timestamps with the device and host domains; nil without them. @@ -1168,11 +1172,13 @@ vk_collect_timestamps :: proc(d: ^Device, f: ^Vk_Frame) { count := f.query_count f.query_count = 0 d.timing_count = 0 + d.timing_frame, d.timing_measured = f.number, f.submitted && f.queries != 0 + f.submitted = false if f.queries == 0 || count < 2 {return} values: [MAX_TIMESTAMPS]u64 // The slot's submission already completed, so WAIT returns immediately. result := d.dvk.GetQueryPoolResults(d.handle, f.queries, 0, count, size_of(values), &values, size_of(u64), {._64, .WAIT}) - if result != .SUCCESS {vk_lost(d, result, "vkGetQueryPoolResults"); return} + if result != .SUCCESS {d.timing_measured = false; vk_lost(d, result, "vkGetQueryPoolResults"); return} gpu, now := values[count - 1], time.duration_nanoseconds(time.tick_diff({}, time.tick_now())) if d.calibrate != nil { infos := [2]vk.CalibratedTimestampInfoKHR{{sType = .CALIBRATED_TIMESTAMP_INFO_KHR, timeDomain = .DEVICE}, {sType = .CALIBRATED_TIMESTAMP_INFO_KHR, timeDomain = VK_HOST_TIME_DOMAIN}} @@ -1197,7 +1203,13 @@ vk_collect_timestamps :: proc(d: ^Device, f: ^Vk_Frame) { } } -frame_timings :: proc(d: ^Device) -> []Gpu_Timing {return d.timings[:d.timing_count]} +// The timings the last frame_begin collected and the frame_number of the +// frame they measured, MAX_FRAMES_IN_FLIGHT before it; measured is false when +// that slot had submitted nothing since its timings were last collected, or +// the device could not read its times. +frame_timings :: proc(d: ^Device) -> (timings: []Gpu_Timing, frame: u64, measured: bool) { + return d.timings[:d.timing_count], d.timing_frame, d.timing_measured +} // Begins recording a frame. Without present it takes no image of the // swapchain: the frame draws only into textures, for a capture. @@ -1299,6 +1311,7 @@ frame_end :: proc(d: ^Device, cmd: ^Command_List, generated := false) -> bool { f.marks.sent = submitted == .SUCCESS ok := vk_check(d, submitted, "vkQueueSubmit2") f.signal_value = d.timeline_value + f.number, f.submitted = d.frame_number, submitted == .SUCCESS f.submitted_ns = present_now_ns() if ok && f.acquired { present := vk.PresentInfoKHR{