Skip to content

Commit

Permalink
Support bvh updates
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Sep 16, 2024
1 parent 7e64c54 commit 5c9f9fe
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,52 @@ void kope_d3d12_command_list_prepare_raytracing_hierarchy(kope_g5_command_list *
list->d3d12.list->ResourceBarrier(1, &barrier);
}

void kope_d3d12_command_list_update_raytracing_hierarchy(kope_g5_command_list *list, kinc_matrix4x4_t *volume_transforms, uint32_t volumes_count,
kope_g5_raytracing_hierarchy *hierarchy) {
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) {
descs[volume_index].Transform[0][0] = volume_transforms[volume_index].m[0];
descs[volume_index].Transform[1][0] = volume_transforms[volume_index].m[1];
descs[volume_index].Transform[2][0] = volume_transforms[volume_index].m[2];

descs[volume_index].Transform[0][1] = volume_transforms[volume_index].m[4];
descs[volume_index].Transform[1][1] = volume_transforms[volume_index].m[5];
descs[volume_index].Transform[2][1] = volume_transforms[volume_index].m[6];

descs[volume_index].Transform[0][2] = volume_transforms[volume_index].m[8];
descs[volume_index].Transform[1][2] = volume_transforms[volume_index].m[9];
descs[volume_index].Transform[2][2] = volume_transforms[volume_index].m[10];

descs[volume_index].Transform[0][3] = volume_transforms[volume_index].m[12];
descs[volume_index].Transform[1][3] = volume_transforms[volume_index].m[13];
descs[volume_index].Transform[2][3] = volume_transforms[volume_index].m[14];
}

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_PERFORM_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.Inputs = inputs;
build_desc.SourceAccelerationStructureData = hierarchy->d3d12.acceleration_structure.d3d12.resource->GetGPUVirtualAddress();
build_desc.DestAccelerationStructureData = hierarchy->d3d12.acceleration_structure.d3d12.resource->GetGPUVirtualAddress();
build_desc.ScratchAccelerationStructureData = hierarchy->d3d12.update_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) {
list->d3d12.list->SetPipelineState1(pipeline->pipe);
list->d3d12.list->SetComputeRootSignature(pipeline->root_signature);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ void kope_d3d12_command_list_prepare_raytracing_volume(kope_g5_command_list *lis

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

void kope_d3d12_command_list_update_raytracing_hierarchy(kope_g5_command_list *list, kinc_matrix4x4_t *volume_transforms, uint32_t volumes_count,
kope_g5_raytracing_hierarchy *hierarchy);

void kope_d3d12_command_list_set_ray_pipeline(kope_g5_command_list *list, kope_d3d12_ray_pipeline *pipeline);

void kope_d3d12_command_list_trace_rays(kope_g5_command_list *list);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ void kope_d3d12_device_create(kope_g5_device *device, const kope_g5_device_wishl

kinc_microsoft_affirm(D3D12CreateDevice(NULL, D3D_FEATURE_LEVEL_12_2, IID_PPV_ARGS(&device->d3d12.device)));

NvAPI_Initialize();
#if defined(KOPE_NVAPI) && !defined(NDEBUG)
NvAPI_Initialize();
NvAPI_D3D12_EnableRaytracingValidation(device->d3d12.device, NVAPI_D3D12_RAYTRACING_VALIDATION_FLAG_NONE);
void *handle = nullptr;
NvAPI_D3D12_RegisterRaytracingValidationMessageCallback(device->d3d12.device, &myValidationMessageCallback, nullptr, &handle);
Expand Down Expand Up @@ -701,6 +701,11 @@ void kope_d3d12_device_create_raytracing_hierarchy(kope_g5_device *device, kope_
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 update_scratch_params;
update_scratch_params.size = prebuild_info.UpdateScratchDataSizeInBytes;
update_scratch_params.usage_flags = KOPE_G5_BUFFER_USAGE_READ_WRITE;
kope_g5_device_create_buffer(device, &update_scratch_params, &hierarchy->d3d12.update_scratch_buffer);

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ typedef struct kope_d3d12_raytracing_hierarchy {
kope_g5_buffer instances;

kope_g5_buffer scratch_buffer;
kope_g5_buffer update_scratch_buffer;
kope_g5_buffer acceleration_structure;
} kope_d3d12_raytracing_hierarchy;

Expand Down
5 changes: 5 additions & 0 deletions Sources/kope/graphics5/commandlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ void kope_g5_command_list_prepare_raytracing_hierarchy(kope_g5_command_list *lis
KOPE_G5_CALL2(command_list_prepare_raytracing_hierarchy, list, hierarchy);
}

void kope_g5_command_list_update_raytracing_hierarchy(kope_g5_command_list *list, kinc_matrix4x4_t *volume_transforms, uint32_t volumes_count,
kope_g5_raytracing_hierarchy *hierarchy) {
KOPE_G5_CALL4(command_list_update_raytracing_hierarchy, list, volume_transforms, volumes_count, hierarchy);
}

void kope_g5_command_list_trace_rays(kope_g5_command_list *list) {
KOPE_G5_CALL1(command_list_trace_rays, list);
}
5 changes: 5 additions & 0 deletions Sources/kope/graphics5/commandlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <kope/global.h>

#include <kinc/math/matrix.h>

#include "api.h"
#include "buffer.h"
#include "texture.h"
Expand Down Expand Up @@ -133,6 +135,9 @@ 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_update_raytracing_hierarchy(kope_g5_command_list *list, kinc_matrix4x4_t *volume_transforms, uint32_t volumes_count,
struct kope_g5_raytracing_hierarchy *hierarchy);

KOPE_FUNC void kope_g5_command_list_trace_rays(kope_g5_command_list *list);

KOPE_FUNC void kope_g5_command_list_present(kope_g5_command_list *list);
Expand Down

0 comments on commit 5c9f9fe

Please sign in to comment.