From 520a164fcc71b16884fdd1c1324f4548fa063c59 Mon Sep 17 00:00:00 2001 From: Gatgat Date: Tue, 8 Oct 2024 17:40:55 +0200 Subject: [PATCH 1/3] WebGPU: remove ImageBindGroup member since it is already stored in ImageBindGroups --- backends/imgui_impl_wgpu.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/backends/imgui_impl_wgpu.cpp b/backends/imgui_impl_wgpu.cpp index 4109e392eef5..be2830759d6a 100644 --- a/backends/imgui_impl_wgpu.cpp +++ b/backends/imgui_impl_wgpu.cpp @@ -67,7 +67,6 @@ struct RenderResources WGPUBuffer Uniforms = nullptr; // Shader uniforms WGPUBindGroup CommonBindGroup = nullptr; // Resources bind-group to bind the common resources to pipeline ImGuiStorage ImageBindGroups; // Resources bind-group to bind the font/image resources to pipeline (this is a key->value map) - WGPUBindGroup ImageBindGroup = nullptr; // Default font-resource of Dear ImGui WGPUBindGroupLayout ImageBindGroupLayout = nullptr; // Cache layout used for the image bind group. Avoids allocating unnecessary JS objects when working with WebASM }; @@ -241,7 +240,6 @@ static void SafeRelease(RenderResources& res) SafeRelease(res.Sampler); SafeRelease(res.Uniforms); SafeRelease(res.CommonBindGroup); - SafeRelease(res.ImageBindGroup); SafeRelease(res.ImageBindGroupLayout); }; @@ -715,10 +713,7 @@ bool ImGui_ImplWGPU_CreateDeviceObjects() common_bg_descriptor.entries = common_bg_entries; bd->renderResources.CommonBindGroup = wgpuDeviceCreateBindGroup(bd->wgpuDevice, &common_bg_descriptor); - WGPUBindGroup image_bind_group = ImGui_ImplWGPU_CreateImageBindGroup(bg_layouts[1], bd->renderResources.FontTextureView); - bd->renderResources.ImageBindGroup = image_bind_group; bd->renderResources.ImageBindGroupLayout = bg_layouts[1]; - bd->renderResources.ImageBindGroups.SetVoidPtr(ImHashData(&bd->renderResources.FontTextureView, sizeof(ImTextureID)), image_bind_group); SafeRelease(vertex_shader_desc.module); SafeRelease(pixel_shader_desc.module); @@ -778,7 +773,6 @@ bool ImGui_ImplWGPU_Init(ImGui_ImplWGPU_InitInfo* init_info) bd->renderResources.Uniforms = nullptr; bd->renderResources.CommonBindGroup = nullptr; bd->renderResources.ImageBindGroups.Data.reserve(100); - bd->renderResources.ImageBindGroup = nullptr; bd->renderResources.ImageBindGroupLayout = nullptr; // Create buffers with a default size (they will later be grown as needed) From ac2c01235843b7f56d87a231fa77a47ff188e767 Mon Sep 17 00:00:00 2001 From: Gatgat Date: Tue, 8 Oct 2024 14:23:39 +0200 Subject: [PATCH 2/3] WebGPU: release image bind groups in ImGui_ImplWGPU_NewFrame We can't store the bind groups during all the execution because user could release the texture. --- backends/imgui_impl_wgpu.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/backends/imgui_impl_wgpu.cpp b/backends/imgui_impl_wgpu.cpp index be2830759d6a..962398104c84 100644 --- a/backends/imgui_impl_wgpu.cpp +++ b/backends/imgui_impl_wgpu.cpp @@ -190,6 +190,15 @@ static void SafeRelease(WGPUBindGroup& res) wgpuBindGroupRelease(res); res = nullptr; } +template +static void SafeRelease(ImGuiStorage& bind_groups_storage) +{ + for (int i = 0; i < bind_groups_storage.Data.size(); i++) + { + SafeRelease((T&)bind_groups_storage.Data[i].val_p); + } + bind_groups_storage.Clear(); +} static void SafeRelease(WGPUBuffer& res) { if (res) @@ -816,6 +825,12 @@ void ImGui_ImplWGPU_NewFrame() ImGui_ImplWGPU_Data* bd = ImGui_ImplWGPU_GetBackendData(); if (!bd->pipelineState) ImGui_ImplWGPU_CreateDeviceObjects(); + + // The user could have deallocated a stored texture, and + // potentially allocated a new one that has the same id as + // a previously registered one. + // To mitigate this, we release the registered bind groups. + SafeRelease(bd->renderResources.ImageBindGroups); } //----------------------------------------------------------------------------- From 10b98e00ac440eb0dceacbd26efb185a7491e273 Mon Sep 17 00:00:00 2001 From: Gatgat Date: Tue, 8 Oct 2024 14:24:01 +0200 Subject: [PATCH 3/3] WebGPU: release image bind groups on shutdown --- backends/imgui_impl_wgpu.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/backends/imgui_impl_wgpu.cpp b/backends/imgui_impl_wgpu.cpp index 962398104c84..387b3852d356 100644 --- a/backends/imgui_impl_wgpu.cpp +++ b/backends/imgui_impl_wgpu.cpp @@ -249,6 +249,7 @@ static void SafeRelease(RenderResources& res) SafeRelease(res.Sampler); SafeRelease(res.Uniforms); SafeRelease(res.CommonBindGroup); + SafeRelease(res.ImageBindGroups); SafeRelease(res.ImageBindGroupLayout); };