From 5698746f42998167b580c3ba719ed090e845130a Mon Sep 17 00:00:00 2001 From: Ivan 'provod' Avdeev Date: Tue, 31 Oct 2023 11:58:30 -0400 Subject: [PATCH] vk: do not crash on corrupted scopes stack 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 --- ref/vk/r_speeds.c | 21 ++++++++++++++++++--- ref/vk/vk_rtx.c | 3 ++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/ref/vk/r_speeds.c b/ref/vk/r_speeds.c index 3e00aa46..a61a2bac 100644 --- a/ref/vk/r_speeds.c +++ b/ref/vk/r_speeds.c @@ -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; @@ -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; @@ -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) diff --git a/ref/vk/vk_rtx.c b/ref/vk/vk_rtx.c index 2fd7a31a..7b486121 100644 --- a/ref/vk/vk_rtx.c +++ b/ref/vk/vk_rtx.c @@ -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 = { @@ -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); }