From 20cc6745671efa5f5f30e5ff62157dbf3739dcb2 Mon Sep 17 00:00:00 2001 From: Robert Konrad Date: Mon, 9 Sep 2024 21:47:40 +0200 Subject: [PATCH] Simplify descriptor management --- .../Sources/kope/direct3d12/commandlist.cpp | 15 +--- .../Sources/kope/direct3d12/descriptorset.cpp | 2 +- .../kope/direct3d12/descriptorset_structs.h | 1 - .../Sources/kope/direct3d12/device.cpp | 85 +++++++------------ .../Sources/kope/direct3d12/device_structs.h | 18 ++-- 5 files changed, 37 insertions(+), 84 deletions(-) diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp index 6f6865468..67bfb42c7 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp @@ -89,20 +89,7 @@ void kope_d3d12_command_list_draw_indexed(kope_g5_command_list *list, uint32_t i } void kope_d3d12_command_list_set_descriptor_table(kope_g5_command_list *list, uint32_t table_index, kope_d3d12_descriptor_set *set) { - uint32_t execution_index = list->d3d12.device->execution_context_index; - - if (!set->copied_to_execution_context[execution_index]) { - D3D12_CPU_DESCRIPTOR_HANDLE src_descriptor = list->d3d12.device->staging_descriptor_heap->GetCPUDescriptorHandleForHeapStart(); - src_descriptor.ptr += set->allocation.offset * list->d3d12.device->cbv_srv_uav_increment; - - D3D12_CPU_DESCRIPTOR_HANDLE dst_descriptor = - list->d3d12.device->execution_contexts[execution_index].descriptor_heap->GetCPUDescriptorHandleForHeapStart(); - dst_descriptor.ptr += set->allocation.offset * list->d3d12.device->cbv_srv_uav_increment; - - list->d3d12.device->device->CopyDescriptorsSimple((UINT)set->descriptor_count, dst_descriptor, src_descriptor, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); - } - - D3D12_GPU_DESCRIPTOR_HANDLE gpu_descriptor = list->d3d12.device->execution_contexts[execution_index].descriptor_heap->GetGPUDescriptorHandleForHeapStart(); + D3D12_GPU_DESCRIPTOR_HANDLE gpu_descriptor = list->d3d12.device->descriptor_heap->GetGPUDescriptorHandleForHeapStart(); gpu_descriptor.ptr += set->allocation.offset * list->d3d12.device->cbv_srv_uav_increment; list->d3d12.list->SetGraphicsRootDescriptorTable(table_index, gpu_descriptor); } diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp index bfddf3592..6df7d1e46 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp @@ -6,7 +6,7 @@ void kope_d3d12_descriptor_set_set_buffer_view_cbv(kope_g5_device *device, kope_ desc.BufferLocation = buffer->d3d12.resource->GetGPUVirtualAddress(); desc.SizeInBytes = (UINT)buffer->d3d12.size; - D3D12_CPU_DESCRIPTOR_HANDLE descriptor_handle = device->d3d12.staging_descriptor_heap->GetCPUDescriptorHandleForHeapStart(); + D3D12_CPU_DESCRIPTOR_HANDLE descriptor_handle = device->d3d12.descriptor_heap->GetCPUDescriptorHandleForHeapStart(); descriptor_handle.ptr += (set->allocation.offset + index) * device->d3d12.cbv_srv_uav_increment; device->d3d12.device->CreateConstantBufferView(&desc, descriptor_handle); } diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_structs.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_structs.h index 3cf6dc744..f8fd5a579 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_structs.h +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_structs.h @@ -12,7 +12,6 @@ extern "C" { #endif typedef struct kope_d3d12_descriptor_set { - bool copied_to_execution_context[KOPE_D3D12_NUM_EXECUTION_CONTEXTS]; oa_allocation_t allocation; size_t descriptor_count; } kope_d3d12_descriptor_set; diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp index 70abf699d..163bcc75d 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp @@ -96,51 +96,41 @@ void kope_d3d12_device_create(kope_g5_device *device, const kope_g5_device_wishl device->d3d12.device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_GRAPHICS_PPV_ARGS(&device->d3d12.frame_fence)); device->d3d12.frame_event = CreateEvent(NULL, FALSE, FALSE, NULL); - for (int i = 0; i < KOPE_D3D12_NUM_EXECUTION_CONTEXTS; ++i) { - device->d3d12.execution_contexts[i] = {0}; - - { - D3D12_DESCRIPTOR_HEAP_DESC desc = {}; - desc.NumDescriptors = 1024; - desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; - desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; - kinc_microsoft_affirm( - device->d3d12.device->CreateDescriptorHeap(&desc, IID_GRAPHICS_PPV_ARGS(&device->d3d12.execution_contexts[i].descriptor_heap))); - } - - { - D3D12_DESCRIPTOR_HEAP_DESC desc = {}; - desc.NumDescriptors = 1024; - desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER; - desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; - kinc_microsoft_affirm(device->d3d12.device->CreateDescriptorHeap(&desc, IID_GRAPHICS_PPV_ARGS(&device->d3d12.execution_contexts[i].sampler_heap))); - } - } - device->d3d12.current_frame_index = 1; - device->d3d12.execution_context_index = 0; - device->d3d12.execution_contexts[device->d3d12.execution_context_index].blocking_frame_index = 1; - { + const uint32_t descriptor_count = 1024 * 10; + D3D12_DESCRIPTOR_HEAP_DESC desc = {}; - desc.NumDescriptors = 1024 * 10; + desc.NumDescriptors = descriptor_count; desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; - desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE; - kinc_microsoft_affirm(device->d3d12.device->CreateDescriptorHeap(&desc, IID_GRAPHICS_PPV_ARGS(&device->d3d12.staging_descriptor_heap))); + desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; + kinc_microsoft_affirm(device->d3d12.device->CreateDescriptorHeap(&desc, IID_GRAPHICS_PPV_ARGS(&device->d3d12.descriptor_heap))); - oa_create(&device->d3d12.descriptor_sets_allocator, 1024 * 10, 4096); + oa_create(&device->d3d12.descriptor_heap_allocator, descriptor_count, 4096); } { - kope_d3d12_device_create_command_list(device, &device->d3d12.management_list); + const uint32_t sampler_count = 1024; - kope_d3d12_execution_context *execution_context = &device->d3d12.execution_contexts[device->d3d12.execution_context_index]; - ID3D12DescriptorHeap *heaps[] = {execution_context->descriptor_heap, execution_context->sampler_heap}; - device->d3d12.management_list.d3d12.list->SetDescriptorHeaps(2, heaps); + D3D12_DESCRIPTOR_HEAP_DESC desc = {}; + desc.NumDescriptors = sampler_count; + desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER; + desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; + kinc_microsoft_affirm(device->d3d12.device->CreateDescriptorHeap(&desc, IID_GRAPHICS_PPV_ARGS(&device->d3d12.sampler_heap))); - kope_g5_device_execute_command_list(device, &device->d3d12.management_list); + oa_create(&device->d3d12.sampler_heap_allocator, sampler_count, 4096); } + + /*{ + kope_d3d12_device_create_command_list(device, &device->d3d12.management_list); + + kope_d3d12_execution_context *execution_context = &device->d3d12.execution_contexts[device->d3d12.execution_context_index]; + ID3D12DescriptorHeap *heaps[] = {execution_context->descriptor_heap, execution_context->sampler_heap}; + device->d3d12.management_list.d3d12.list->SetDescriptorHeaps(2, heaps); + + kope_g5_device_execute_command_list(device, &device->d3d12.management_list); + }*/ } void kope_d3d12_device_destroy(kope_g5_device *device) { @@ -216,6 +206,9 @@ void kope_d3d12_device_create_command_list(kope_g5_device *device, kope_g5_comma list->d3d12.blocking_frame_index = 0; list->d3d12.presenting = false; + + ID3D12DescriptorHeap *heaps[] = {list->d3d12.device->descriptor_heap, list->d3d12.device->sampler_heap}; + list->d3d12.list->SetDescriptorHeaps(2, heaps); } static DXGI_FORMAT convert_texture_format(kope_g5_texture_format format) { @@ -465,22 +458,6 @@ void kope_d3d12_device_execute_command_list(kope_g5_device *device, kope_g5_comm list->d3d12.blocking_frame_index = 0; } - { - uint64_t completed_frame = device->d3d12.frame_fence->GetCompletedValue(); - - for (uint32_t execution_index = 0; execution_index < KOPE_D3D12_NUM_EXECUTION_CONTEXTS; ++execution_index) { - kope_d3d12_execution_context *execution_context = &device->d3d12.execution_contexts[execution_index]; - if (execution_context->blocking_frame_index <= completed_frame) { - device->d3d12.execution_context_index = execution_index; - - ID3D12DescriptorHeap *heaps[] = {execution_context->descriptor_heap, execution_context->sampler_heap}; - list->d3d12.list->SetDescriptorHeaps(2, heaps); - - break; - } - } - } - list->d3d12.list->Close(); ID3D12CommandList *lists[] = {list->d3d12.list}; @@ -496,6 +473,9 @@ void kope_d3d12_device_execute_command_list(kope_g5_device *device, kope_g5_comm list->d3d12.allocator[allocator_index]->Reset(); list->d3d12.list->Reset(list->d3d12.allocator[allocator_index], NULL); + ID3D12DescriptorHeap *heaps[] = {list->d3d12.device->descriptor_heap, list->d3d12.device->sampler_heap}; + list->d3d12.list->SetDescriptorHeaps(2, heaps); + if (list->d3d12.presenting) { kope_g5_texture *framebuffer = kope_d3d12_device_get_framebuffer(device); framebuffer->d3d12.in_flight_frame_index = device->d3d12.current_frame_index; @@ -507,16 +487,11 @@ void kope_d3d12_device_execute_command_list(kope_g5_device *device, kope_g5_comm device->d3d12.current_frame_index += 1; device->d3d12.framebuffer_index = (device->d3d12.framebuffer_index + 1) % KOPE_D3D12_FRAME_COUNT; - device->d3d12.execution_contexts[device->d3d12.execution_context_index].blocking_frame_index = device->d3d12.current_frame_index; - list->d3d12.presenting = false; } } void kope_d3d12_device_create_descriptor_set(kope_g5_device *device, uint32_t descriptor_count, kope_d3d12_descriptor_set *set) { - for (uint32_t index = 0; index < KOPE_D3D12_NUM_EXECUTION_CONTEXTS; ++index) { - set->copied_to_execution_context[index] = false; - } - oa_allocate(&device->d3d12.descriptor_sets_allocator, descriptor_count, &set->allocation); + oa_allocate(&device->d3d12.descriptor_heap_allocator, descriptor_count, &set->allocation); set->descriptor_count = descriptor_count; } diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_structs.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_structs.h index afbf152e2..277c7bba6 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_structs.h +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_structs.h @@ -15,14 +15,6 @@ struct ID3D12DescriptorHeap; #define KOPE_D3D12_FRAME_COUNT 2 -typedef struct kope_d3d12_execution_context { - uint64_t blocking_frame_index; - struct ID3D12DescriptorHeap *descriptor_heap; - struct ID3D12DescriptorHeap *sampler_heap; -} kope_d3d12_execution_context; - -#define KOPE_D3D12_NUM_EXECUTION_CONTEXTS 2 - typedef struct kope_d3d12_device { struct ID3D12Device *device; struct ID3D12CommandQueue *queue; @@ -39,9 +31,6 @@ typedef struct kope_d3d12_device { uint32_t cbv_srv_uav_increment; uint32_t sampler_increment; - kope_d3d12_execution_context execution_contexts[KOPE_D3D12_NUM_EXECUTION_CONTEXTS]; - uint32_t execution_context_index; - uint32_t framebuffer_index; kope_g5_texture framebuffer_textures[KOPE_D3D12_FRAME_COUNT]; @@ -49,8 +38,11 @@ typedef struct kope_d3d12_device { HANDLE frame_event; uint64_t current_frame_index; - struct ID3D12DescriptorHeap *staging_descriptor_heap; - oa_allocator_t descriptor_sets_allocator; + struct ID3D12DescriptorHeap *descriptor_heap; + oa_allocator_t descriptor_heap_allocator; + + struct ID3D12DescriptorHeap *sampler_heap; + oa_allocator_t sampler_heap_allocator; kope_g5_command_list management_list; } kope_d3d12_device;