Skip the menu's draw for a frame when its vertex or index buffer cannot be allocated, instead of copying through a null pointer.

The menu's Vulkan back end went on after a failed buffer allocation: it bound and mapped memory it did not have and copied the frame's vertices to the address the failed mapping left, null. Two reports of 0.8.0 end that way right after the settings menu opens with video memory used up, one on an integrated AMD GPU and one on an RTX 2050: two "got VkResult -2" lines, then an access violation in the C runtime's copy, writing address 0x20, not on a guest thread. The allocation now leaves no buffer and size 0 when it fails, the draw returns before anything is recorded, and the next frame asks for the buffer again. The change is marked in the vendored file and named in THIRD_PARTY_NOTICES.md. Compiled; the failing allocation was not reproduced here.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-12 15:19:59 -05:00
parent 222a025c52
commit 18aa16f2df
2 changed files with 31 additions and 1 deletions

View File

@@ -427,6 +427,13 @@ static void CreateOrResizeBuffer(VkBuffer& buffer, VkDeviceMemory& buffer_memory
buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
err = vkCreateBuffer(v->Device, &buffer_info, v->Allocator, &buffer);
check_vk_result(err);
if (err != VK_SUCCESS) // NX2NAVI: as for the memory below
{
buffer = VK_NULL_HANDLE;
buffer_memory = VK_NULL_HANDLE;
buffer_size = 0;
return;
}
VkMemoryRequirements req;
vkGetBufferMemoryRequirements(v->Device, buffer, &req);
@@ -435,8 +442,20 @@ static void CreateOrResizeBuffer(VkBuffer& buffer, VkDeviceMemory& buffer_memory
alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
alloc_info.allocationSize = req.size;
alloc_info.memoryTypeIndex = ImGui_ImplVulkan_MemoryType(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, req.memoryTypeBits);
buffer_memory = VK_NULL_HANDLE; // NX2NAVI: the handle freed above is not kept when the allocation fails
err = vkAllocateMemory(v->Device, &alloc_info, v->Allocator, &buffer_memory);
check_vk_result(err);
// NX2NAVI: out of memory. No buffer, no memory and size 0, so the caller draws nothing this frame and the next
// frame asks again (ImGui_ImplVulkan_RenderDrawData). Unchecked, the mapping below it failed too and the copy of
// the vertices went through a null pointer.
if (err != VK_SUCCESS)
{
vkDestroyBuffer(v->Device, buffer, v->Allocator);
buffer = VK_NULL_HANDLE;
buffer_memory = VK_NULL_HANDLE;
buffer_size = 0;
return;
}
err = vkBindBufferMemory(v->Device, buffer, buffer_memory, 0);
check_vk_result(err);
@@ -523,14 +542,25 @@ void ImGui_ImplVulkan_RenderDrawData(ImDrawData* draw_data, VkCommandBuffer comm
CreateOrResizeBuffer(rb->VertexBuffer, rb->VertexBufferMemory, rb->VertexBufferSize, vertex_size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
if (rb->IndexBuffer == VK_NULL_HANDLE || rb->IndexBufferSize < index_size)
CreateOrResizeBuffer(rb->IndexBuffer, rb->IndexBufferMemory, rb->IndexBufferSize, index_size, VK_BUFFER_USAGE_INDEX_BUFFER_BIT);
// NX2NAVI: a buffer that could not be made (CreateOrResizeBuffer): nothing is drawn this frame
if (rb->VertexBufferMemory == VK_NULL_HANDLE || rb->IndexBufferMemory == VK_NULL_HANDLE)
return;
// Upload vertex/index data into a single contiguous GPU buffer
ImDrawVert* vtx_dst = nullptr;
ImDrawIdx* idx_dst = nullptr;
VkResult err = vkMapMemory(v->Device, rb->VertexBufferMemory, 0, vertex_size, 0, (void**)&vtx_dst);
check_vk_result(err);
// NX2NAVI: a mapping that failed leaves nothing to copy into
if (err != VK_SUCCESS || !vtx_dst)
return;
err = vkMapMemory(v->Device, rb->IndexBufferMemory, 0, index_size, 0, (void**)&idx_dst);
check_vk_result(err);
if (err != VK_SUCCESS || !idx_dst)
{
vkUnmapMemory(v->Device, rb->VertexBufferMemory);
return;
}
for (int n = 0; n < draw_data->CmdListsCount; n++)
{
const ImDrawList* draw_list = draw_data->CmdLists[n];