From 93a10cbf7ee1d84f63df3948bdf4dd5c3827f7f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cle=CC=81ment=20Foucault?= Date: Fri, 8 Nov 2024 20:00:54 +0100 Subject: [PATCH] GPU: Enable printf only between render boundaries and use a stack Printf buffer read needs to be inside render boundaries to work. Since render boundaries can be nested, use a stack. Fixes assert when quitting blender. --- source/blender/gpu/intern/gpu_context.cc | 5 ----- source/blender/gpu/intern/gpu_context_private.hh | 3 ++- source/blender/gpu/intern/gpu_shader.cc | 4 ++-- source/blender/gpu/intern/gpu_shader_log.cc | 15 ++++++++------- 4 files changed, 12 insertions(+), 15 deletions(-) diff --git a/source/blender/gpu/intern/gpu_context.cc b/source/blender/gpu/intern/gpu_context.cc index 3d9b6d8ab6a..0840650ceba 100644 --- a/source/blender/gpu/intern/gpu_context.cc +++ b/source/blender/gpu/intern/gpu_context.cc @@ -120,7 +120,6 @@ GPUContext *GPU_context_create(void *ghost_window, void *ghost_context) void GPU_context_discard(GPUContext *ctx_) { Context *ctx = unwrap(ctx_); - printf_end(ctx); delete ctx; active_ctx = nullptr; @@ -140,7 +139,6 @@ void GPU_context_active_set(GPUContext *ctx_) Context *ctx = unwrap(ctx_); if (active_ctx) { - printf_end(active_ctx); active_ctx->deactivate(); } @@ -148,7 +146,6 @@ void GPU_context_active_set(GPUContext *ctx_) if (ctx) { ctx->activate(); - printf_begin(ctx); } } @@ -208,7 +205,6 @@ void GPU_render_begin() * but should be fixed for Metal. */ if (backend) { backend->render_begin(); - printf_end(active_ctx); printf_begin(active_ctx); } } @@ -218,7 +214,6 @@ void GPU_render_end() BLI_assert(backend); if (backend) { printf_end(active_ctx); - printf_begin(active_ctx); backend->render_end(); } } diff --git a/source/blender/gpu/intern/gpu_context_private.hh b/source/blender/gpu/intern/gpu_context_private.hh index ccbf00bec58..ec34cbb50aa 100644 --- a/source/blender/gpu/intern/gpu_context_private.hh +++ b/source/blender/gpu/intern/gpu_context_private.hh @@ -60,7 +60,8 @@ class Context { static int context_counter; int context_id = 0; - GPUStorageBuf *printf_buf = nullptr; + /* Used as a stack. Each render_begin/end pair will push pop from the stack. */ + Vector printf_buf; protected: /** Thread on which this context is active. */ diff --git a/source/blender/gpu/intern/gpu_shader.cc b/source/blender/gpu/intern/gpu_shader.cc index 79f85fc003b..d38c3792678 100644 --- a/source/blender/gpu/intern/gpu_shader.cc +++ b/source/blender/gpu/intern/gpu_shader.cc @@ -451,8 +451,8 @@ void GPU_shader_bind(GPUShader *gpu_shader) } } #if GPU_SHADER_PRINTF_ENABLE - if (ctx->printf_buf) { - GPU_storagebuf_bind(ctx->printf_buf, GPU_SHADER_PRINTF_SLOT); + if (!ctx->printf_buf.is_empty()) { + GPU_storagebuf_bind(ctx->printf_buf.last(), GPU_SHADER_PRINTF_SLOT); } #endif } diff --git a/source/blender/gpu/intern/gpu_shader_log.cc b/source/blender/gpu/intern/gpu_shader_log.cc index 01ef65dc1aa..89b7ae3429a 100644 --- a/source/blender/gpu/intern/gpu_shader_log.cc +++ b/source/blender/gpu/intern/gpu_shader_log.cc @@ -333,9 +333,10 @@ void printf_begin(Context *ctx) if (!shader::gpu_shader_dependency_has_printf()) { return; } - BLI_assert(ctx->printf_buf == nullptr); - ctx->printf_buf = GPU_storagebuf_create(GPU_SHADER_PRINTF_MAX_CAPACITY * sizeof(uint32_t)); - GPU_storagebuf_clear_to_zero(ctx->printf_buf); + GPUStorageBuf *printf_buf = GPU_storagebuf_create(GPU_SHADER_PRINTF_MAX_CAPACITY * + sizeof(uint32_t)); + GPU_storagebuf_clear_to_zero(printf_buf); + ctx->printf_buf.append(printf_buf); } void printf_end(Context *ctx) @@ -343,14 +344,14 @@ void printf_end(Context *ctx) if (ctx == nullptr) { return; } - if (ctx->printf_buf == nullptr) { + if (ctx->printf_buf.is_empty()) { return; } + GPUStorageBuf *printf_buf = ctx->printf_buf.pop_last(); Vector data(GPU_SHADER_PRINTF_MAX_CAPACITY); - GPU_storagebuf_read(ctx->printf_buf, data.data()); - GPU_storagebuf_free(ctx->printf_buf); - ctx->printf_buf = nullptr; + GPU_storagebuf_read(printf_buf, data.data()); + GPU_storagebuf_free(printf_buf); uint32_t data_len = data[0]; if (data_len == 0) {