Skip to content

Commit 9ea97c0

Browse files
committed
Fix a command buffer leak that occurs in dx12
1 parent 2b7ea62 commit 9ea97c0

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

drivers/d3d12/rendering_device_driver_d3d12.cpp

+17-1
Original file line numberDiff line numberDiff line change
@@ -2292,6 +2292,19 @@ bool RenderingDeviceDriverD3D12::command_pool_reset(CommandPoolID p_cmd_pool) {
22922292

22932293
void RenderingDeviceDriverD3D12::command_pool_free(CommandPoolID p_cmd_pool) {
22942294
CommandPoolInfo *command_pool = (CommandPoolInfo *)(p_cmd_pool.id);
2295+
2296+
// Destroy all command buffers associated with this command pool, mirroring Vulkan's behavior.
2297+
SelfList<CommandBufferInfo> *cmd_buf_elem = command_pool->command_buffers.first();
2298+
while (cmd_buf_elem != nullptr) {
2299+
CommandBufferInfo *cmd_buf_info = cmd_buf_elem->self();
2300+
cmd_buf_elem = cmd_buf_elem->next();
2301+
2302+
cmd_buf_info->cmd_list.Reset();
2303+
cmd_buf_info->cmd_allocator.Reset();
2304+
2305+
VersatileResource::free(resources_allocator, cmd_buf_info);
2306+
}
2307+
22952308
memdelete(command_pool);
22962309
}
22972310

@@ -2300,7 +2313,7 @@ void RenderingDeviceDriverD3D12::command_pool_free(CommandPoolID p_cmd_pool) {
23002313
RDD::CommandBufferID RenderingDeviceDriverD3D12::command_buffer_create(CommandPoolID p_cmd_pool) {
23012314
DEV_ASSERT(p_cmd_pool);
23022315

2303-
const CommandPoolInfo *command_pool = (CommandPoolInfo *)(p_cmd_pool.id);
2316+
CommandPoolInfo *command_pool = (CommandPoolInfo *)(p_cmd_pool.id);
23042317
D3D12_COMMAND_LIST_TYPE list_type;
23052318
if (command_pool->buffer_type == COMMAND_BUFFER_TYPE_SECONDARY) {
23062319
list_type = D3D12_COMMAND_LIST_TYPE_BUNDLE;
@@ -2336,6 +2349,9 @@ RDD::CommandBufferID RenderingDeviceDriverD3D12::command_buffer_create(CommandPo
23362349
cmd_buf_info->cmd_allocator = cmd_allocator;
23372350
cmd_buf_info->cmd_list = cmd_list;
23382351

2352+
// Add this command buffer to the command pool's list of command buffers.
2353+
command_pool->command_buffers.add(&cmd_buf_info->command_buffer_info_elem);
2354+
23392355
return CommandBufferID(cmd_buf_info);
23402356
}
23412357

drivers/d3d12/rendering_device_driver_d3d12.h

+6
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,9 @@ class RenderingDeviceDriverD3D12 : public RenderingDeviceDriver {
430430
struct CommandPoolInfo {
431431
CommandQueueFamilyID queue_family;
432432
CommandBufferType buffer_type = COMMAND_BUFFER_TYPE_PRIMARY;
433+
// Since there are no command pools in D3D12, we need to track the command buffers created by this pool
434+
// so that we can free them when the pool is freed.
435+
SelfList<CommandBufferInfo>::List command_buffers;
433436
};
434437

435438
public:
@@ -458,6 +461,9 @@ class RenderingDeviceDriverD3D12 : public RenderingDeviceDriver {
458461
// Leveraging knowledge of actual usage and D3D12 specifics (namely, command lists from the same allocator
459462
// can't be freely begun and ended), an allocator per list works better.
460463
struct CommandBufferInfo {
464+
// Store a self list reference to be used by the command pool.
465+
SelfList<CommandBufferInfo> command_buffer_info_elem{ this };
466+
461467
ComPtr<ID3D12CommandAllocator> cmd_allocator;
462468
ComPtr<ID3D12GraphicsCommandList> cmd_list;
463469

0 commit comments

Comments
 (0)