generate visual profiler zones in pre pass

This commit is contained in:
jacob 2026-04-01 03:54:18 -05:00
parent aa78bf649a
commit b7c454e7ef
2 changed files with 150 additions and 123 deletions

View File

@ -604,7 +604,7 @@ i32 W32_Main(void)
i64 start_tsc; i64 start_tsc;
i64 end_tsc; i64 end_tsc;
// Collect samples // Collect samples
u32 tsc_sleep_calibration_ms = 20; u32 tsc_sleep_calibration_ms = 10;
{ {
start_tsc = __rdtsc(); start_tsc = __rdtsc();
QueryPerformanceCounter(&start_qpc); QueryPerformanceCounter(&start_qpc);

View File

@ -5730,6 +5730,9 @@ void V_TickForever(WaveLaneCtx *lane)
f64 view_start_ns = profiler->view_ns; f64 view_start_ns = profiler->view_ns;
f64 view_end_ns = view_start_ns + view_range_len_ns; f64 view_end_ns = view_start_ns + view_range_len_ns;
f64 view_start_px = view_start_ns / profiler->ns_per_px;
f64 view_end_px = view_end_ns / profiler->ns_per_px;
@ -5745,20 +5748,28 @@ void V_TickForever(WaveLaneCtx *lane)
//- Visible zones //- Visible zones
Struct(VisRow) Struct(VisZone)
{ {
VisRow *next; VisZone *next;
V_ZoneRow *zone_row; u32 depth;
V_ZoneChunk *closest_zone_chunk; u64 id;
String name;
Vec4 color;
f64 start_px;
f64 len_px;
i64 elapsed_ns;
}; };
Struct(VisTrack) Struct(VisTrack)
{ {
VisTrack *next; VisTrack *next;
V_ZoneTrack *zone_track; u64 id;
VisRow *first_row; u32 rows_count;
VisRow *last_row; VisZone *first_zone;
VisZone *last_zone;
}; };
VisTrack *first_vis_track = 0; VisTrack *first_vis_track = 0;
@ -5772,7 +5783,7 @@ void V_TickForever(WaveLaneCtx *lane)
// TODO: Ignore non-visible tracks // TODO: Ignore non-visible tracks
VisTrack *vis_track = PushStruct(frame->arena, VisTrack); VisTrack *vis_track = PushStruct(frame->arena, VisTrack);
SllQueuePush(first_vis_track, last_vis_track, vis_track); SllQueuePush(first_vis_track, last_vis_track, vis_track);
vis_track->zone_track = zone_track; vis_track->id = zone_track->id;
// TODO: Ignore non-visible rows // TODO: Ignore non-visible rows
for (u64 row_idx = 0; row_idx < zone_track->rows_count; ++row_idx) for (u64 row_idx = 0; row_idx < zone_track->rows_count; ++row_idx)
@ -5792,10 +5803,54 @@ void V_TickForever(WaveLaneCtx *lane)
if (closest_start->end_ns >= view_start_ns && closest_start->start_ns <= view_end_ns) if (closest_start->end_ns >= view_start_ns && closest_start->start_ns <= view_end_ns)
{ {
VisRow *vis_row = PushStruct(perm, VisRow); f32 zone_collapse_threshold_px = 5;
SllQueuePush(vis_track->first_row, vis_track->last_row, vis_row); for (V_ZoneChunk *chunk = closest_start; chunk && chunk->start_ns <= view_end_ns; chunk = chunk->nexts[0])
vis_row->zone_row = zone_row; {
vis_row->closest_zone_chunk = closest_start; i64 visual_chunk_start_ns = MaxI64(chunk->start_ns, profiler->view_ns);
i64 visual_chunk_end_ns = MinI64(chunk->end_ns, profiler->view_ns + view_range_len_ns);
f64 visual_chunk_len_px = (visual_chunk_end_ns - visual_chunk_start_ns) / profiler->ns_per_px;
b32 should_collapse_chunk = visual_chunk_len_px <= zone_collapse_threshold_px;
if (!should_collapse_chunk)
{
b32 reached_end_of_view = 0;
for (u64 chunk_zone_idx = 0; chunk_zone_idx < chunk->zones_count && !reached_end_of_view; ++chunk_zone_idx)
{
V_Zone *zone = &chunk->zones[chunk_zone_idx];
i64 zone_start_ns = zone->start_ns;
i64 zone_end_ns = MinI64(zone->end_ns, frame->time_ns);
i64 zone_elapsed_ns = zone_end_ns - zone_start_ns;
i64 visual_zone_start_ns = MaxI64(zone_start_ns, profiler->view_ns);
i64 visual_zone_end_ns = MinI64(zone_end_ns, profiler->view_ns + view_range_len_ns);
f64 visual_zone_start_px = visual_zone_start_ns / profiler->ns_per_px;
f64 visual_zone_len_px = (visual_zone_end_ns - visual_zone_start_ns) / profiler->ns_per_px;
b32 zone_should_collapse = visual_zone_len_px <= zone_collapse_threshold_px;
if (visual_zone_start_ns > view_end_ns)
{
reached_end_of_view = 1;
}
if (visual_zone_end_ns >= view_start_ns && visual_zone_start_ns <= view_end_ns && !zone_should_collapse)
{
VisZone *vis_zone = PushStruct(frame->arena, VisZone);
SllQueuePush(vis_track->first_zone, vis_track->last_zone, vis_zone);
vis_zone->name = zone->name;
vis_zone->id = zone->id;
vis_zone->color = zone->color;
vis_zone->start_px = visual_zone_start_px;
vis_zone->len_px = visual_zone_len_px;
vis_zone->elapsed_ns = zone_elapsed_ns;
vis_zone->depth = row_idx;
vis_track->rows_count = MaxU32(vis_track->rows_count, vis_zone->depth + 1);
}
}
}
}
} }
} }
} }
@ -5827,7 +5882,7 @@ void V_TickForever(WaveLaneCtx *lane)
UI_Size timeline_cursor_width = UI_Fnt(0.15, 1); UI_Size timeline_cursor_width = UI_Fnt(0.15, 1);
UI_Key hovered_zone_box = Zi; UI_Key hovered_zone_box = Zi;
V_Zone *hovered_zone = 0; VisZone *hovered_zone = 0;
f64 ruler_len_ns = 0; f64 ruler_len_ns = 0;
Vec4 main_color_sampled = profiler_color; Vec4 main_color_sampled = profiler_color;
@ -5926,52 +5981,30 @@ void V_TickForever(WaveLaneCtx *lane)
//- Zone tracks //- Zone tracks
ProfZoneDF("Build zones") ProfZoneDF("Build zones")
{ {
f32 zone_collapse_threshold_px = 5;
for (VisTrack *vis_track = first_vis_track; vis_track; vis_track = vis_track->next) for (VisTrack *vis_track = first_vis_track; vis_track; vis_track = vis_track->next)
{ {
V_ZoneTrack *zone_track = vis_track->zone_track;
UI_SetNext(Height, track_height); UI_SetNext(Height, track_height);
UI_SetDF(Tag, HashF("vis track %F", FmtUint(vis_track->id)))
UI_SetDF(Parent, UI_BuildColumn()) UI_SetDF(Parent, UI_BuildColumn())
{ {
for (VisRow *row = vis_track->first_row; row; row = row->next) //- Zone rows
UI_Key *zone_row_boxes = PushStructs(frame->arena, UI_Key, vis_track->rows_count);
for (u64 zone_row_box_idx = 0; zone_row_box_idx < vis_track->rows_count; ++zone_row_box_idx)
{ {
//- Zone row UI_Key zone_row_box = UI_KeyF("zone row %F", FmtUint(zone_row_box_idx));
zone_row_boxes[zone_row_box_idx] = zone_row_box;
// UI_SetNext(Height, zone_height); // UI_SetNext(Height, zone_height);
UI_SetNext(Height, zone_height); UI_SetNext(Height, zone_height);
UI_SetDF(Parent, UI_BuildRow()) UI_SetDF(Parent, UI_BuildRowEx(zone_row_box))
{ {
for (V_ZoneChunk *chunk = row->closest_zone_chunk; chunk && chunk->start_ns <= view_end_ns; chunk = chunk->nexts[0]) }
{
i64 visual_chunk_start_ns = MaxI64(chunk->start_ns, profiler->view_ns);
i64 visual_chunk_end_ns = MinI64(chunk->end_ns, profiler->view_ns + view_range_len_ns);
f64 visual_chunk_len_px = (visual_chunk_end_ns - visual_chunk_start_ns) / profiler->ns_per_px;
b32 should_collapse_chunk = visual_chunk_len_px <= zone_collapse_threshold_px;
if (!should_collapse_chunk)
{
b32 reached_end_of_view = 0;
for (u64 chunk_zone_idx = 0; chunk_zone_idx < chunk->zones_count && !reached_end_of_view; ++chunk_zone_idx)
{
V_Zone *zone = &chunk->zones[chunk_zone_idx];
i64 zone_start_ns = zone->start_ns;
i64 zone_end_ns = MinI64(zone->end_ns, frame->time_ns);
i64 visual_zone_start_ns = MaxI64(zone_start_ns, profiler->view_ns);
i64 visual_zone_end_ns = MinI64(zone_end_ns, profiler->view_ns + view_range_len_ns);
f64 visual_zone_len_px = (visual_zone_end_ns - visual_zone_start_ns) / profiler->ns_per_px;
b32 zone_should_collapse = visual_zone_len_px <= zone_collapse_threshold_px;
if (visual_zone_start_ns > view_end_ns)
{
reached_end_of_view = 1;
} }
if (visual_zone_end_ns >= view_start_ns && visual_zone_start_ns <= view_end_ns && !zone_should_collapse) //- Zones
for (VisZone *zone = vis_track->first_zone; zone; zone = zone->next)
{ {
UI_Key zone_row_box = zone_row_boxes[zone->depth];
UI_Key zone_box = UI_KeyF("Zone %F", FmtUint(zone->id)); UI_Key zone_box = UI_KeyF("Zone %F", FmtUint(zone->id));
if (UI_HotAbsolute(zone_box)) if (UI_HotAbsolute(zone_box))
{ {
@ -6007,9 +6040,8 @@ void V_TickForever(WaveLaneCtx *lane)
f64 zone_offset_px = (visual_zone_start_ns - profiler->view_ns) / profiler->ns_per_px; f64 zone_offset_px = zone->start_px - view_start_px;
f64 zone_len_ns = visual_zone_end_ns - visual_zone_start_ns; f64 zone_len_px = zone->len_px;
f64 zone_len_px = zone_len_ns / profiler->ns_per_px;
@ -6050,6 +6082,7 @@ void V_TickForever(WaveLaneCtx *lane)
UI_SetNext(FloatingPos, zone_pos); UI_SetNext(FloatingPos, zone_pos);
UI_SetNext(ChildAlignment, UI_Region_Center); UI_SetNext(ChildAlignment, UI_Region_Center);
UI_SetNext(Flags, UI_BoxFlag_Floating | UI_BoxFlag_DontClampFloatingX | UI_BoxFlag_DontClampFloatingY | UI_BoxFlag_CaptureMouse | UI_BoxFlag_Scissor); UI_SetNext(Flags, UI_BoxFlag_Floating | UI_BoxFlag_DontClampFloatingX | UI_BoxFlag_DontClampFloatingY | UI_BoxFlag_CaptureMouse | UI_BoxFlag_Scissor);
UI_SetNext(Parent, zone_row_box);
// UI_SetNext(Flags, UI_BoxFlag_Floating | UI_BoxFlag_DontClampFloatingX | UI_BoxFlag_DontClampFloatingY); // UI_SetNext(Flags, UI_BoxFlag_Floating | UI_BoxFlag_DontClampFloatingX | UI_BoxFlag_DontClampFloatingY);
UI_SetDF(Parent, UI_BuildRowEx(zone_box)) UI_SetDF(Parent, UI_BuildRowEx(zone_box))
{ {
@ -6077,11 +6110,6 @@ void V_TickForever(WaveLaneCtx *lane)
} }
} }
} }
}
}
}
}
}
@ -6196,9 +6224,8 @@ void V_TickForever(WaveLaneCtx *lane)
if (show_hovered_zone) if (show_hovered_zone)
{ {
V_Zone *zone = hovered_zone; VisZone *zone = hovered_zone;
// UI_BuildLabelF("Hi!!"); // UI_BuildLabelF("Hi!!");
i64 zone_elapsed_ns = zone->end_ns - zone->start_ns;
@ -6208,7 +6235,7 @@ void V_TickForever(WaveLaneCtx *lane)
UI_SetNext(TextColor, theme.col.hint); UI_SetNext(TextColor, theme.col.hint);
UI_BuildLabelF("%F: ", FmtString(zone->name)); UI_BuildLabelF("%F: ", FmtString(zone->name));
UI_BuildLabelF("%F", FmtTimeNs(zone_elapsed_ns, .p = 2)); UI_BuildLabelF("%F", FmtTimeNs(zone->elapsed_ns, .p = 2));
} }
} }