Skip to content

Commit

Permalink
vk: do not crash on corrupted scopes stack
Browse files Browse the repository at this point in the history
This is not a valid reason to assert/crash, even though it is clearly a programming mistake.
If the stack is corrupted, just print its contents as an S_ERROR and continue.

Also:
- Fix the ultimate reason for stack being unbalanced for #604
- Do not analyze scopes when not needed, i.e. when `r_speeds` is zero.

Fixes #604
  • Loading branch information
w23 committed Oct 31, 2023
1 parent f1c8b3e commit 5698746
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
21 changes: 18 additions & 3 deletions ref/vk/r_speeds.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,24 @@ static void drawCPUProfilerScopes(int draw, const aprof_event_t *events, uint64_
ASSERT(depth > 0);
--depth;

ASSERT(stack[depth].scope_id == scope_id);
ASSERT(scope_id >= 0);
ASSERT(scope_id < APROF_MAX_SCOPES);

if (stack[depth].scope_id != scope_id) {
gEngine.Con_Printf(S_ERROR "scope_id mismatch at stack depth=%d: found %d(%s), expected %d(%s)\n",
depth,
scope_id, g_aprof.scopes[scope_id].name,
stack[depth].scope_id, g_aprof.scopes[stack[depth].scope_id].name);

gEngine.Con_Printf(S_ERROR "Full stack:\n");
for (int i = depth; i >= 0; --i) {
gEngine.Con_Printf(S_ERROR " %d: scope_id=%d(%s)\n", i,
stack[i].scope_id, g_aprof.scopes[stack[i].scope_id].name);
}

return;
}

const aprof_scope_t *const scope = g_aprof.scopes + scope_id;
const uint64_t delta_ns = timestamp_ns - stack[depth].begin_ns;

Expand Down Expand Up @@ -482,7 +496,7 @@ static void drawGPUProfilerScopes(qboolean draw, int y, uint64_t frame_begin_tim
}
}

static int drawFrames( int draw, uint32_t prev_frame_index, int y, const vk_combuf_scopes_t *gpurofls, int gpurofls_count) {
static int analyzeScopesAndDrawFrames( int draw, uint32_t prev_frame_index, int y, const vk_combuf_scopes_t *gpurofls, int gpurofls_count) {
// Draw latest 2 frames; find their boundaries
uint32_t rewind_frame = prev_frame_index;
const int max_frames_to_draw = 2;
Expand Down Expand Up @@ -974,10 +988,11 @@ void R_SpeedsDisplayMore(uint32_t prev_frame_index, const struct vk_combuf_scope

handlePause( prev_frame_index );

if (speeds_bits != 0)
{
int y = 100;
const int draw_frame = speeds_bits & SPEEDS_BIT_FRAME;
y = drawFrames( draw_frame, prev_frame_index, y, gpurofl, gpurofl_count );
y = analyzeScopesAndDrawFrames( draw_frame, prev_frame_index, y, gpurofl, gpurofl_count );

const int draw_graphs = speeds_bits & SPEEDS_BIT_GRAPHS;
if (draw_graphs)
Expand Down
3 changes: 2 additions & 1 deletion ref/vk/vk_rtx.c
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ void VK_RayFrameEnd(const vk_ray_frame_render_args_t* args)

// Do not draw when we have no swapchain
if (args->dst.image_view == VK_NULL_HANDLE)
return;
goto tail;

if (g_ray_model_state.frame.instances_count == 0) {
const r_vkimage_blit_args blit_args = {
Expand Down Expand Up @@ -592,6 +592,7 @@ void VK_RayFrameEnd(const vk_ray_frame_render_args_t* args)
performTracing( args->combuf, &trace_args );
}

tail:
APROF_SCOPE_END(ray_frame_end);
}

Expand Down

0 comments on commit 5698746

Please sign in to comment.