Skip to content

Commit

Permalink
Build a raytracing hierarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Sep 15, 2024
1 parent 36dcd53 commit 2837c7f
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,19 @@ void kope_d3d12_command_list_prepare_raytracing_volume(kope_g5_command_list *lis

list->d3d12.list->BuildRaytracingAccelerationStructure(&build_desc, 0, nullptr);
}

void kope_d3d12_command_list_prepare_raytracing_hierarchy(kope_g5_command_list *list, kope_g5_raytracing_hierarchy *hierarchy) {
D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS inputs = {};
inputs.Type = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL;
inputs.Flags = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_ALLOW_UPDATE;
inputs.NumDescs = hierarchy->d3d12.volumes_count;
inputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;
inputs.InstanceDescs = hierarchy->d3d12.instances.d3d12.resource->GetGPUVirtualAddress();

D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC build_desc = {};
build_desc.DestAccelerationStructureData = hierarchy->d3d12.acceleration_structure.d3d12.resource->GetGPUVirtualAddress();
build_desc.Inputs = inputs;
build_desc.ScratchAccelerationStructureData = hierarchy->d3d12.scratch_buffer.d3d12.resource->GetGPUVirtualAddress();

list->d3d12.list->BuildRaytracingAccelerationStructure(&build_desc, 0, nullptr);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ void kope_d3d12_command_list_compute(kope_g5_command_list *list, uint32_t workgr

void kope_d3d12_command_list_prepare_raytracing_volume(kope_g5_command_list *list, kope_g5_raytracing_volume *volume);

void kope_d3d12_command_list_prepare_raytracing_hierarchy(kope_g5_command_list *list, kope_g5_raytracing_hierarchy *hierarchy);

#ifdef __cplusplus
}
#endif
Expand Down
39 changes: 39 additions & 0 deletions Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,3 +610,42 @@ void kope_d3d12_device_create_raytracing_volume(kope_g5_device *device, kope_g5_
as_params.usage_flags = KOPE_G5_BUFFER_USAGE_READ_WRITE | KOPE_G5_BUFFER_USAGE_RAYTRACING_VOLUME;
kope_g5_device_create_buffer(device, &as_params, &volume->d3d12.acceleration_structure);
}

void kope_d3d12_device_create_raytracing_hierarchy(kope_g5_device *device, kope_g5_raytracing_volume **volumes, uint32_t volumes_count,
kope_g5_raytracing_hierarchy *hierarchy) {
hierarchy->d3d12.volumes_count = volumes_count;

kope_g5_buffer_parameters instances_params;
instances_params.size = sizeof(D3D12_RAYTRACING_INSTANCE_DESC) * hierarchy->d3d12.volumes_count;
instances_params.usage_flags = KOPE_G5_BUFFER_USAGE_CPU_WRITE;
kope_g5_device_create_buffer(device, &instances_params, &hierarchy->d3d12.instances);

D3D12_RAYTRACING_INSTANCE_DESC *descs = (D3D12_RAYTRACING_INSTANCE_DESC *)kope_g5_buffer_lock(&hierarchy->d3d12.instances);
for (uint32_t volume_index = 0; volume_index < hierarchy->d3d12.volumes_count; ++volume_index) {
memset(&descs[volume_index], 0, sizeof(D3D12_RAYTRACING_INSTANCE_DESC));
descs[volume_index].InstanceID = volume_index;
descs[volume_index].InstanceMask = 1;
descs[volume_index].AccelerationStructure = volumes[volume_index]->d3d12.acceleration_structure.d3d12.resource->GetGPUVirtualAddress();
}
kope_g5_buffer_unlock(&hierarchy->d3d12.instances);

D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS inputs = {};
inputs.Type = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL;
inputs.Flags = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_ALLOW_UPDATE;
inputs.NumDescs = hierarchy->d3d12.volumes_count;
inputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;
inputs.InstanceDescs = hierarchy->d3d12.instances.d3d12.resource->GetGPUVirtualAddress();

D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO prebuild_info = {};
device->d3d12.device->GetRaytracingAccelerationStructurePrebuildInfo(&inputs, &prebuild_info);

kope_g5_buffer_parameters scratch_params;
scratch_params.size = prebuild_info.ScratchDataSizeInBytes;
scratch_params.usage_flags = KOPE_G5_BUFFER_USAGE_READ_WRITE;
kope_g5_device_create_buffer(device, &scratch_params, &hierarchy->d3d12.scratch_buffer); // TODO: delete later

kope_g5_buffer_parameters as_params;
as_params.size = prebuild_info.ResultDataMaxSizeInBytes;
as_params.usage_flags = KOPE_G5_BUFFER_USAGE_READ_WRITE | KOPE_G5_BUFFER_USAGE_RAYTRACING_VOLUME;
kope_g5_device_create_buffer(device, &as_params, &hierarchy->d3d12.acceleration_structure);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ void kope_d3d12_device_execute_command_list(kope_g5_device *device, kope_g5_comm
void kope_d3d12_device_create_raytracing_volume(kope_g5_device *device, kope_g5_buffer *vertex_buffer, uint64_t vertex_count, kope_g5_buffer *index_buffer,
uint32_t index_count, kope_g5_raytracing_volume *volume);

void kope_d3d12_device_create_raytracing_hierarchy(kope_g5_device *device, kope_g5_raytracing_volume **volumes, uint32_t volumes_count,
kope_g5_raytracing_hierarchy *hierarchy);

#ifdef __cplusplus
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ typedef struct kope_d3d12_raytracing_volume {
kope_g5_buffer acceleration_structure;
} kope_d3d12_raytracing_volume;

typedef struct kope_d3d12_raytracing_hierarchy {
uint32_t volumes_count;
kope_g5_buffer instances;

kope_g5_buffer scratch_buffer;
kope_g5_buffer acceleration_structure;
} kope_d3d12_raytracing_hierarchy;

#ifdef __cplusplus
}
#endif
Expand Down
3 changes: 3 additions & 0 deletions Sources/kope/graphics5/commandlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ void kope_g5_command_list_compute(kope_g5_command_list *list, uint32_t workgroup
void kope_g5_command_list_prepare_raytracing_volume(kope_g5_command_list *list, kope_g5_raytracing_volume *volume) {
KOPE_G5_CALL2(command_list_prepare_raytracing_volume, list, volume);
}
void kope_g5_command_list_prepare_raytracing_hierarchy(kope_g5_command_list *list, kope_g5_raytracing_hierarchy *hierarchy) {
KOPE_G5_CALL2(command_list_prepare_raytracing_hierarchy, list, hierarchy);
}
4 changes: 4 additions & 0 deletions Sources/kope/graphics5/commandlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ struct kope_g5_raytracing_volume;

KOPE_FUNC void kope_g5_command_list_prepare_raytracing_volume(kope_g5_command_list *list, struct kope_g5_raytracing_volume *volume);

struct kope_g5_raytracing_hierarchy;

KOPE_FUNC void kope_g5_command_list_prepare_raytracing_hierarchy(kope_g5_command_list *list, struct kope_g5_raytracing_hierarchy *hierarchy);

KOPE_FUNC void kope_g5_command_list_present(kope_g5_command_list *list);

#ifdef __cplusplus
Expand Down
5 changes: 5 additions & 0 deletions Sources/kope/graphics5/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,8 @@ void kope_g5_device_create_raytracing_volume(kope_g5_device *device, kope_g5_buf
uint32_t index_count, kope_g5_raytracing_volume *volume) {
KOPE_G5_CALL6(device_create_raytracing_volume, device, vertex_buffer, vertex_count, index_buffer, index_count, volume);
}

void kope_g5_device_create_raytracing_hierarchy(kope_g5_device *device, kope_g5_raytracing_volume **volumes, uint32_t volumes_count,
kope_g5_raytracing_hierarchy *hierarchy) {
KOPE_G5_CALL4(device_create_raytracing_hierarchy, device, volumes, volumes_count, hierarchy);
}
7 changes: 7 additions & 0 deletions Sources/kope/graphics5/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,16 @@ typedef struct kope_g5_raytracing_volume {
KOPE_G5_IMPL(raytracing_volume);
} kope_g5_raytracing_volume;

typedef struct kope_g5_raytracing_hierarchy {
KOPE_G5_IMPL(raytracing_hierarchy);
} kope_g5_raytracing_hierarchy;

KOPE_FUNC void kope_g5_device_create_raytracing_volume(kope_g5_device *device, kope_g5_buffer *vertex_buffer, uint64_t vertex_count,
kope_g5_buffer *index_buffer, uint32_t index_count, kope_g5_raytracing_volume *volume);

KOPE_FUNC void kope_g5_device_create_raytracing_hierarchy(kope_g5_device *device, kope_g5_raytracing_volume **volumes, uint32_t volumes_count,
kope_g5_raytracing_hierarchy *hierarchy);

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit 2837c7f

Please sign in to comment.