From 4b1eafb737c3c8876e716406ebc4f2c306f76142 Mon Sep 17 00:00:00 2001 From: Robert Konrad Date: Mon, 21 Oct 2024 18:43:22 +0200 Subject: [PATCH] Make descriptor sets support bindless --- .../Sources/kope/direct3d12/commandlist.cpp | 12 ++++++++++++ .../Sources/kope/direct3d12/descriptorset.cpp | 5 ++--- .../kope/direct3d12/descriptorset_functions.h | 3 +-- .../Sources/kope/direct3d12/descriptorset_structs.h | 3 +++ .../Direct3D12/Sources/kope/direct3d12/device.cpp | 10 ++++++++-- .../Sources/kope/direct3d12/device_functions.h | 4 ++-- 6 files changed, 28 insertions(+), 9 deletions(-) diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp index 857bf66ff..e4c221df2 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp @@ -270,6 +270,18 @@ void kope_d3d12_command_list_set_descriptor_table(kope_g5_command_list *list, ui table_index += 1; } + if (set->bindless_descriptor_count > 0) { + D3D12_GPU_DESCRIPTOR_HANDLE gpu_descriptor = list->d3d12.device->descriptor_heap->GetGPUDescriptorHandleForHeapStart(); + gpu_descriptor.ptr += set->bindless_descriptor_allocation.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); + } + else { + list->d3d12.list->SetGraphicsRootDescriptorTable(table_index, gpu_descriptor); + } + table_index += 1; + } + if (set->sampler_count > 0) { D3D12_GPU_DESCRIPTOR_HANDLE gpu_descriptor = list->d3d12.device->sampler_heap->GetGPUDescriptorHandleForHeapStart(); gpu_descriptor.ptr += set->sampler_allocation.offset * list->d3d12.device->sampler_increment; diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp index 8d4f8f961..29e32f9e1 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp @@ -42,8 +42,7 @@ void kope_d3d12_descriptor_set_set_bvh_view_srv(kope_g5_device *device, kope_d3d device->d3d12.device->CreateShaderResourceView(nullptr, &desc, descriptor_handle); } -void kope_d3d12_descriptor_set_set_texture_view_srv(kope_g5_device *device, kope_d3d12_descriptor_set *set, const kope_g5_texture_view *texture_view, - uint32_t index) { +void kope_d3d12_descriptor_set_set_texture_view_srv(kope_g5_device *device, uint32_t offset, const kope_g5_texture_view *texture_view) { D3D12_SHADER_RESOURCE_VIEW_DESC desc = {}; desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D; desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; @@ -66,7 +65,7 @@ void kope_d3d12_descriptor_set_set_texture_view_srv(kope_g5_device *device, kope desc.Texture2D.ResourceMinLODClamp = 0.0f; D3D12_CPU_DESCRIPTOR_HANDLE descriptor_handle = device->d3d12.descriptor_heap->GetCPUDescriptorHandleForHeapStart(); - descriptor_handle.ptr += (set->descriptor_allocation.offset + index) * device->d3d12.cbv_srv_uav_increment; + descriptor_handle.ptr += offset * device->d3d12.cbv_srv_uav_increment; device->d3d12.device->CreateShaderResourceView(texture_view->texture->d3d12.resource, &desc, descriptor_handle); } diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_functions.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_functions.h index 9deeafbff..8f9ea5283 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_functions.h +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_functions.h @@ -14,8 +14,7 @@ extern "C" { void kope_d3d12_descriptor_set_set_buffer_view_cbv(kope_g5_device *device, kope_d3d12_descriptor_set *set, kope_g5_buffer *buffer, uint32_t index); void kope_d3d12_descriptor_set_set_buffer_view_srv(kope_g5_device *device, kope_d3d12_descriptor_set *set, kope_g5_buffer *buffer, uint32_t index); void kope_d3d12_descriptor_set_set_bvh_view_srv(kope_g5_device *device, kope_d3d12_descriptor_set *set, kope_g5_raytracing_hierarchy *bvh, uint32_t index); -void kope_d3d12_descriptor_set_set_texture_view_srv(kope_g5_device *device, kope_d3d12_descriptor_set *set, const kope_g5_texture_view *texture_view, - uint32_t index); +void kope_d3d12_descriptor_set_set_texture_view_srv(kope_g5_device *device, uint32_t offset, const kope_g5_texture_view *texture_view); void kope_d3d12_descriptor_set_set_texture_array_view_srv(kope_g5_device *device, kope_d3d12_descriptor_set *set, const kope_g5_texture_view *texture_view, uint32_t index); void kope_d3d12_descriptor_set_set_texture_cube_view_srv(kope_g5_device *device, kope_d3d12_descriptor_set *set, const kope_g5_texture_view *texture_view, diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_structs.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_structs.h index 4b3eb95ff..c4cb645da 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_structs.h +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_structs.h @@ -17,6 +17,9 @@ typedef struct kope_d3d12_descriptor_set { size_t dynamic_descriptor_count; + oa_allocation_t bindless_descriptor_allocation; + size_t bindless_descriptor_count; + oa_allocation_t sampler_allocation; size_t sampler_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 cd0911185..dfe273cb8 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp @@ -570,6 +570,7 @@ void kope_d3d12_device_execute_command_list(kope_g5_device *device, kope_g5_comm kope_d3d12_buffer_access access = list->d3d12.queued_buffer_accesses[buffer_access_index]; kope_g5_buffer *buffer = access.buffer; + assert(buffer->d3d12.ranges_count < KOPE_D3D12_MAX_BUFFER_RANGES); buffer->d3d12.ranges[buffer->d3d12.ranges_count].execution_index = device->d3d12.execution_index; buffer->d3d12.ranges[buffer->d3d12.ranges_count].offset = access.offset; buffer->d3d12.ranges[buffer->d3d12.ranges_count].size = access.size; @@ -618,8 +619,8 @@ void kope_d3d12_device_wait_until_idle(kope_g5_device *device) { wait_for_fence(device, device->d3d12.execution_fence, device->d3d12.execution_event, device->d3d12.execution_index - 1); } -void kope_d3d12_device_create_descriptor_set(kope_g5_device *device, uint32_t descriptor_count, uint32_t dynamic_descriptor_count, uint32_t sampler_count, - kope_d3d12_descriptor_set *set) { +void kope_d3d12_device_create_descriptor_set(kope_g5_device *device, uint32_t descriptor_count, uint32_t dynamic_descriptor_count, + uint32_t bindless_descriptor_count, uint32_t sampler_count, kope_d3d12_descriptor_set *set) { if (descriptor_count > 0) { oa_allocate(&device->d3d12.descriptor_heap_allocator, descriptor_count, &set->descriptor_allocation); } @@ -627,6 +628,11 @@ void kope_d3d12_device_create_descriptor_set(kope_g5_device *device, uint32_t de set->dynamic_descriptor_count = dynamic_descriptor_count; + if (bindless_descriptor_count > 0) { + oa_allocate(&device->d3d12.descriptor_heap_allocator, bindless_descriptor_count, &set->bindless_descriptor_allocation); + } + set->bindless_descriptor_count = bindless_descriptor_count; + if (sampler_count > 0) { oa_allocate(&device->d3d12.sampler_heap_allocator, sampler_count, &set->sampler_allocation); } diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_functions.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_functions.h index 9843b836b..35f14e58e 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_functions.h +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_functions.h @@ -23,8 +23,8 @@ void kope_d3d12_device_create_command_list(kope_g5_device *device, kope_g5_comma void kope_d3d12_device_create_texture(kope_g5_device *device, const kope_g5_texture_parameters *parameters, kope_g5_texture *texture); -void kope_d3d12_device_create_descriptor_set(kope_g5_device *device, uint32_t descriptor_count, uint32_t dynamic_descriptor_count, uint32_t sampler_count, - kope_d3d12_descriptor_set *set); +void kope_d3d12_device_create_descriptor_set(kope_g5_device *device, uint32_t descriptor_count, uint32_t dynamic_descriptor_count, + uint32_t bindless_descriptor_count, uint32_t sampler_count, kope_d3d12_descriptor_set *set); void kope_d3d12_device_create_sampler(kope_g5_device *device, const kope_g5_sampler_parameters *parameters, kope_g5_sampler *sampler);