From a83e0c88967427d0bba6091534ddebce7f0e6c91 Mon Sep 17 00:00:00 2001 From: Robert Konrad Date: Mon, 16 Sep 2024 13:09:42 +0200 Subject: [PATCH] Fix the bvh --- .../Sources/kope/direct3d12/commandlist.cpp | 12 ++++++++++++ .../Sources/kope/direct3d12/descriptorset.cpp | 12 ++++++++++++ .../kope/direct3d12/descriptorset_functions.h | 1 + .../Direct3D12/Sources/kope/direct3d12/device.cpp | 11 ++++++++--- 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp index ff908fe39..8ffca944e 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp @@ -243,6 +243,12 @@ void kope_d3d12_command_list_prepare_raytracing_volume(kope_g5_command_list *lis build_desc.ScratchAccelerationStructureData = volume->d3d12.scratch_buffer.d3d12.resource->GetGPUVirtualAddress(); list->d3d12.list->BuildRaytracingAccelerationStructure(&build_desc, 0, nullptr); + + D3D12_RESOURCE_BARRIER barrier = {}; + barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV; + barrier.UAV.pResource = volume->d3d12.acceleration_structure.d3d12.resource; + + list->d3d12.list->ResourceBarrier(1, &barrier); } void kope_d3d12_command_list_prepare_raytracing_hierarchy(kope_g5_command_list *list, kope_g5_raytracing_hierarchy *hierarchy) { @@ -259,6 +265,12 @@ void kope_d3d12_command_list_prepare_raytracing_hierarchy(kope_g5_command_list * build_desc.ScratchAccelerationStructureData = hierarchy->d3d12.scratch_buffer.d3d12.resource->GetGPUVirtualAddress(); list->d3d12.list->BuildRaytracingAccelerationStructure(&build_desc, 0, nullptr); + + D3D12_RESOURCE_BARRIER barrier = {}; + barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV; + barrier.UAV.pResource = hierarchy->d3d12.acceleration_structure.d3d12.resource; + + list->d3d12.list->ResourceBarrier(1, &barrier); } void kope_d3d12_command_list_set_ray_pipeline(kope_g5_command_list *list, kope_d3d12_ray_pipeline *pipeline) { diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp index 34c7321d4..901b508a8 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp @@ -28,6 +28,18 @@ void kope_d3d12_descriptor_set_set_buffer_view_srv(kope_g5_device *device, kope_ device->d3d12.device->CreateShaderResourceView(buffer->d3d12.resource, &desc, descriptor_handle); } +void kope_d3d12_descriptor_set_set_bvh_view_srv(kope_g5_device *device, kope_d3d12_descriptor_set *set, kope_g5_buffer *buffer, uint32_t index) { + D3D12_SHADER_RESOURCE_VIEW_DESC desc = {}; + desc.ViewDimension = D3D12_SRV_DIMENSION_RAYTRACING_ACCELERATION_STRUCTURE; + desc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; + desc.Format = DXGI_FORMAT_UNKNOWN; + desc.RaytracingAccelerationStructure.Location = buffer->d3d12.resource->GetGPUVirtualAddress(); + + 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; + 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, kope_g5_texture *texture, uint32_t index) { D3D12_SHADER_RESOURCE_VIEW_DESC desc = {}; desc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D; diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_functions.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_functions.h index d0c49d945..1ce67425b 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_functions.h +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset_functions.h @@ -13,6 +13,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_buffer *buffer, uint32_t index); void kope_d3d12_descriptor_set_set_texture_view_srv(kope_g5_device *device, kope_d3d12_descriptor_set *set, kope_g5_texture *texture, uint32_t index); void kope_d3d12_descriptor_set_set_texture_view_uav(kope_g5_device *device, kope_d3d12_descriptor_set *set, kope_g5_texture *texture, uint32_t index); void kope_d3d12_descriptor_set_set_sampler(kope_g5_device *device, kope_d3d12_descriptor_set *set, kope_g5_sampler *sampler, uint32_t index); diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp index 54add779b..f7121fe76 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp @@ -202,10 +202,15 @@ void kope_d3d12_device_create_buffer(kope_g5_device *device, const kope_g5_buffe heapProperties.Type = D3D12_HEAP_TYPE_DEFAULT; } - D3D12_RESOURCE_DESC resourceDesc; + D3D12_RESOURCE_DESC resourceDesc = {}; resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; resourceDesc.Alignment = 0; - resourceDesc.Width = align_pow2((int)parameters->size, 256); // 256 required for CBVs + if ((parameters->usage_flags & KOPE_G5_BUFFER_USAGE_RAYTRACING_VOLUME) != 0 || (parameters->usage_flags & KOPE_G5_BUFFER_USAGE_READ_WRITE) != 0) { + resourceDesc.Width = parameters->size; + } + else { + resourceDesc.Width = align_pow2((int)parameters->size, 256); // 256 required for CBVs + } resourceDesc.Height = 1; resourceDesc.DepthOrArraySize = 1; resourceDesc.MipLevels = 1; @@ -227,7 +232,7 @@ void kope_d3d12_device_create_buffer(kope_g5_device *device, const kope_g5_buffe if ((parameters->usage_flags & KOPE_G5_BUFFER_USAGE_RAYTRACING_VOLUME) != 0) { buffer->d3d12.resource_state = D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE; - resourceDesc.Flags |= D3D12_RESOURCE_FLAG_RAYTRACING_ACCELERATION_STRUCTURE; + resourceDesc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; } kinc_microsoft_affirm(device->d3d12.device->CreateCommittedResource(&heapProperties, D3D12_HEAP_FLAG_NONE, &resourceDesc,