Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebGPU: fix image leaks and allow reuse of WGPUTextureView #8046

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions backends/imgui_impl_wgpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down Expand Up @@ -191,6 +190,15 @@ static void SafeRelease(WGPUBindGroup& res)
wgpuBindGroupRelease(res);
res = nullptr;
}
template <typename T>
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)
Expand Down Expand Up @@ -241,7 +249,7 @@ static void SafeRelease(RenderResources& res)
SafeRelease(res.Sampler);
SafeRelease(res.Uniforms);
SafeRelease(res.CommonBindGroup);
SafeRelease(res.ImageBindGroup);
SafeRelease<WGPUBindGroup>(res.ImageBindGroups);
SafeRelease(res.ImageBindGroupLayout);
};

Expand Down Expand Up @@ -715,10 +723,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);
Expand Down Expand Up @@ -778,7 +783,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)
Expand Down Expand Up @@ -822,6 +826,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<WGPUBindGroup>(bd->renderResources.ImageBindGroups);
}

//-----------------------------------------------------------------------------
Expand Down