From ab3632c504942289f491342fd48a5b33dcb36877 Mon Sep 17 00:00:00 2001 From: Robert Konrad Date: Wed, 9 Oct 2024 18:06:55 +0200 Subject: [PATCH] Allocate descriptor space for dynamic offsets --- .../Sources/kope/direct3d12/commandlist.cpp | 24 +++++++++++++++---- .../kope/direct3d12/commandlist_structs.h | 6 +++++ .../Sources/kope/direct3d12/device.cpp | 4 ++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp index 76a8c3e11..bdf29602c 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp @@ -240,14 +240,30 @@ void kope_d3d12_command_list_set_descriptor_table(kope_g5_command_list *list, ui } if (set->dynamic_descriptor_count > 0) { + uint32_t offset = list->d3d12.dynamic_descriptor_allocations[list->d3d12.current_allocator_index].offset + + list->d3d12.dynamic_descriptor_offsets[list->d3d12.current_allocator_index]; + + for (uint32_t descriptor_index = 0; descriptor_index < set->dynamic_descriptor_count; ++descriptor_index) { + D3D12_CONSTANT_BUFFER_VIEW_DESC desc = {}; + // desc.BufferLocation = buffer->d3d12.resource->GetGPUVirtualAddress() + dynamic_offsets[descriptor_index]; + // desc.SizeInBytes = align_pow2((int)buffer->d3d12.size, 256); + + D3D12_CPU_DESCRIPTOR_HANDLE descriptor_handle = list->d3d12.device->descriptor_heap->GetCPUDescriptorHandleForHeapStart(); + descriptor_handle.ptr += (offset + descriptor_index) * list->d3d12.device->cbv_srv_uav_increment; + list->d3d12.device->device->CreateConstantBufferView(&desc, descriptor_handle); + } + D3D12_GPU_DESCRIPTOR_HANDLE gpu_descriptor = list->d3d12.device->descriptor_heap->GetGPUDescriptorHandleForHeapStart(); - /*gpu_descriptor.ptr += set->descriptor_allocation.offset * list->d3d12.device->cbv_srv_uav_increment; + gpu_descriptor.ptr += offset * list->d3d12.device->cbv_srv_uav_increment; if (list->d3d12.compute_pipe != NULL || list->d3d12.ray_pipe != NULL) { - list->d3d12.list->SetComputeRootDescriptorTable(table_index, gpu_descriptor); + list->d3d12.list->SetComputeRootDescriptorTable(table_index, gpu_descriptor); } else { - list->d3d12.list->SetGraphicsRootDescriptorTable(table_index, gpu_descriptor); - }*/ + list->d3d12.list->SetGraphicsRootDescriptorTable(table_index, gpu_descriptor); + } + + list->d3d12.dynamic_descriptor_offsets[list->d3d12.current_allocator_index] += (uint32_t)set->dynamic_descriptor_count; + table_index += 1; } diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist_structs.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist_structs.h index c09a34d59..e1cc39bfe 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist_structs.h +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist_structs.h @@ -3,6 +3,8 @@ #include "d3d12mini.h" +#include + #ifdef __cplusplus extern "C" { #endif @@ -20,6 +22,8 @@ struct ID3D12Fence; // allocators. Increasing this value exchanges more memory against potentially less wait-times (depending on actual command-list usage). #define KOPE_D3D12_COMMAND_LIST_ALLOCATOR_COUNT 3 +#define KOPE_D3D12_COMMAND_LIST_DYNAMIC_DESCRIPTORS_COUNT 64 + #define KOPE_D3D12_COMMAND_LIST_MAX_QUEUED_BUFFER_ACCESSES 256 typedef struct kope_d3d12_buffer_access { @@ -33,6 +37,8 @@ typedef struct kope_d3d12_command_list { struct ID3D12CommandAllocator *allocator[KOPE_D3D12_COMMAND_LIST_ALLOCATOR_COUNT]; uint64_t allocator_execution_index[KOPE_D3D12_COMMAND_LIST_ALLOCATOR_COUNT]; + oa_allocation_t dynamic_descriptor_allocations[KOPE_D3D12_COMMAND_LIST_ALLOCATOR_COUNT]; + uint32_t dynamic_descriptor_offsets[KOPE_D3D12_COMMAND_LIST_ALLOCATOR_COUNT]; uint8_t current_allocator_index; struct ID3D12GraphicsCommandList4 *list; diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp index 7370a7813..ea00064e2 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp @@ -288,6 +288,10 @@ void kope_d3d12_device_create_command_list(kope_g5_device *device, kope_g5_comma for (int i = 0; i < KOPE_D3D12_COMMAND_LIST_ALLOCATOR_COUNT; ++i) { kinc_microsoft_affirm(device->d3d12.device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_GRAPHICS_PPV_ARGS(&list->d3d12.allocator[i]))); list->d3d12.allocator_execution_index[i] = 0; + + oa_allocate(&device->d3d12.descriptor_heap_allocator, KOPE_D3D12_COMMAND_LIST_DYNAMIC_DESCRIPTORS_COUNT, + &list->d3d12.dynamic_descriptor_allocations[i]); + list->d3d12.dynamic_descriptor_offsets[i] = 0; } list->d3d12.current_allocator_index = 0;