diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp index b6cbd362c..15fd9d898 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist.cpp @@ -218,3 +218,27 @@ void kope_d3d12_command_list_prepare_raytracing_hierarchy(kope_g5_command_list * list->d3d12.list->BuildRaytracingAccelerationStructure(&build_desc, 0, nullptr); } + +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); + list->d3d12.ray_pipe = pipeline; + list->d3d12.compute_pipeline_set = true; +} + +void kope_d3d12_command_list_trace_rays(kope_g5_command_list *list) { + D3D12_DISPATCH_RAYS_DESC desc = {}; + desc.RayGenerationShaderRecord.StartAddress = list->d3d12.ray_pipe->shader_ids.d3d12.resource->GetGPUVirtualAddress(); + desc.RayGenerationShaderRecord.SizeInBytes = D3D12_SHADER_IDENTIFIER_SIZE_IN_BYTES; + desc.MissShaderTable.StartAddress = list->d3d12.ray_pipe->shader_ids.d3d12.resource->GetGPUVirtualAddress() + D3D12_RAYTRACING_SHADER_TABLE_BYTE_ALIGNMENT; + desc.MissShaderTable.SizeInBytes = D3D12_SHADER_IDENTIFIER_SIZE_IN_BYTES; + desc.HitGroupTable.StartAddress = + list->d3d12.ray_pipe->shader_ids.d3d12.resource->GetGPUVirtualAddress() + 2 * D3D12_RAYTRACING_SHADER_TABLE_BYTE_ALIGNMENT; + desc.HitGroupTable.SizeInBytes = D3D12_SHADER_IDENTIFIER_SIZE_IN_BYTES; + + desc.Width = 1024; + desc.Height = 768; + desc.Depth = 1; + + list->d3d12.list->DispatchRays(&desc); +} diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist_functions.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist_functions.h index 6fef594a6..84f23e2f8 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist_functions.h +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist_functions.h @@ -37,6 +37,10 @@ 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_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); + #ifdef __cplusplus } #endif diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist_structs.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist_structs.h index 91eb54e0c..d52e0286b 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist_structs.h +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/commandlist_structs.h @@ -9,6 +9,7 @@ extern "C" { struct kope_d3d12_device; struct kope_d3d12_texture; +struct kope_d3d12_ray_pipeline; struct ID3D12Fence; // Allocators can not be re-used while a command-list is executing. We carry along a bag of allocators so we only have to wait when we ran out of in-flight @@ -32,6 +33,8 @@ typedef struct kope_d3d12_command_list { bool compute_pipeline_set; + struct kope_d3d12_ray_pipeline *ray_pipe; + bool presenting; } kope_d3d12_command_list; diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp index 9679b478b..751a51673 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp @@ -229,6 +229,8 @@ void kope_d3d12_device_create_command_list(kope_g5_device *device, kope_g5_comma list->d3d12.compute_pipeline_set = false; + list->d3d12.ray_pipe = NULL; + list->d3d12.blocking_frame_index = 0; list->d3d12.presenting = false; @@ -611,8 +613,12 @@ void kope_d3d12_device_create_raytracing_volume(kope_g5_device *device, kope_g5_ kope_g5_device_create_buffer(device, &as_params, &volume->d3d12.acceleration_structure); } +#include // temporary + 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) { + using namespace DirectX; // temporary + hierarchy->d3d12.volumes_count = volumes_count; kope_g5_buffer_parameters instances_params; @@ -627,6 +633,35 @@ void kope_d3d12_device_create_raytracing_hierarchy(kope_g5_device *device, kope_ descs[volume_index].InstanceMask = 1; descs[volume_index].AccelerationStructure = volumes[volume_index]->d3d12.acceleration_structure.d3d12.resource->GetGPUVirtualAddress(); } + + // temporary + + auto time = static_cast(GetTickCount64()) / 1000; + + { + auto cube = XMMatrixRotationRollPitchYaw(time / 2, time / 3, time / 5); + cube *= XMMatrixTranslation(-1.5, 2, 2); + auto *ptr = reinterpret_cast(&descs[0].Transform); + XMStoreFloat3x4(ptr, cube); + } + + { + auto mirror = XMMatrixRotationX(-1.8f); + mirror *= XMMatrixRotationY(XMScalarSinEst(time) / 8 + 1); + mirror *= XMMatrixTranslation(2, 2, 2); + auto *ptr = reinterpret_cast(&descs[1].Transform); + XMStoreFloat3x4(ptr, mirror); + } + + { + auto floor = XMMatrixScaling(5, 5, 5); + floor *= XMMatrixTranslation(0, 0, 2); + auto *ptr = reinterpret_cast(&descs[2].Transform); + XMStoreFloat3x4(ptr, floor); + } + + // + kope_g5_buffer_unlock(&hierarchy->d3d12.instances); D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS inputs = {}; diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline.cpp index ec1ec8f23..d6820eead 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline.cpp @@ -362,7 +362,7 @@ void kope_d3d12_compute_pipeline_destroy(kope_d3d12_compute_pipeline *pipe) { } } -void kope_d3d12_ray_pipeline_init(kope_d3d12_device *device, kope_d3d12_ray_pipeline *pipe, const kope_d3d12_ray_pipeline_parameters *parameters, +void kope_d3d12_ray_pipeline_init(kope_g5_device *device, kope_d3d12_ray_pipeline *pipe, const kope_d3d12_ray_pipeline_parameters *parameters, ID3D12RootSignature *root_signature) { D3D12_DXIL_LIBRARY_DESC lib = {0}; lib.DXILLibrary.pShaderBytecode = ray_code; @@ -421,7 +421,34 @@ void kope_d3d12_ray_pipeline_init(kope_d3d12_device *device, kope_d3d12_ray_pipe desc.NumSubobjects = 5; desc.pSubobjects = subobjects; - kinc_microsoft_affirm(device->device->CreateStateObject(&desc, IID_PPV_ARGS(&pipe->pipe))); + kinc_microsoft_affirm(device->d3d12.device->CreateStateObject(&desc, IID_PPV_ARGS(&pipe->pipe))); + + uint32_t shader_id_count = 3; + kope_g5_buffer_parameters id_buffer_params; + id_buffer_params.size = shader_id_count * D3D12_RAYTRACING_SHADER_TABLE_BYTE_ALIGNMENT; + id_buffer_params.usage_flags = KOPE_G5_BUFFER_USAGE_CPU_WRITE; + kope_g5_device_create_buffer(device, &id_buffer_params, &pipe->shader_ids); + + ID3D12StateObjectProperties *props; + pipe->pipe->QueryInterface(&props); + + uint8_t *data = (uint8_t *)kope_g5_buffer_lock(&pipe->shader_ids); + + wchar_t raygen[1024]; + kinc_microsoft_convert_string(raygen, parameters->gen_shader_name, 1024); + memcpy(data, props->GetShaderIdentifier(raygen), D3D12_SHADER_IDENTIFIER_SIZE_IN_BYTES); + + wchar_t miss[1024]; + kinc_microsoft_convert_string(miss, parameters->miss_shader_name, 1024); + memcpy(&data[D3D12_RAYTRACING_SHADER_TABLE_BYTE_ALIGNMENT], props->GetShaderIdentifier(miss), D3D12_SHADER_IDENTIFIER_SIZE_IN_BYTES); + + memcpy(&data[D3D12_RAYTRACING_SHADER_TABLE_BYTE_ALIGNMENT * 2], props->GetShaderIdentifier(L"HitGroup"), D3D12_SHADER_IDENTIFIER_SIZE_IN_BYTES); + + kope_g5_buffer_unlock(&pipe->shader_ids); + + props->Release(); + + pipe->root_signature = root_signature; } void kope_d3d12_ray_pipeline_destroy(kope_d3d12_ray_pipeline *pipe) { diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline_functions.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline_functions.h index 4f4c84cda..341aff7c1 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline_functions.h +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline_functions.h @@ -18,7 +18,7 @@ void kope_d3d12_compute_pipeline_destroy(kope_d3d12_compute_pipeline *pipe); struct ID3D12RootSignature; -void kope_d3d12_ray_pipeline_init(kope_d3d12_device *device, kope_d3d12_ray_pipeline *pipe, const kope_d3d12_ray_pipeline_parameters *parameters, +void kope_d3d12_ray_pipeline_init(kope_g5_device *device, kope_d3d12_ray_pipeline *pipe, const kope_d3d12_ray_pipeline_parameters *parameters, struct ID3D12RootSignature *root_signature); void kope_d3d12_ray_pipeline_destroy(kope_d3d12_ray_pipeline *pipe); diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline_structs.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline_structs.h index 0fa31f824..410fbff9c 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline_structs.h +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline_structs.h @@ -226,6 +226,7 @@ struct ID3D12StateObject; typedef struct kope_d3d12_ray_pipeline { struct ID3D12StateObject *pipe; struct ID3D12RootSignature *root_signature; + kope_g5_buffer shader_ids; } kope_d3d12_ray_pipeline; #ifdef __cplusplus diff --git a/Sources/kope/graphics5/commandlist.c b/Sources/kope/graphics5/commandlist.c index 068f2b535..072ecef07 100644 --- a/Sources/kope/graphics5/commandlist.c +++ b/Sources/kope/graphics5/commandlist.c @@ -40,3 +40,7 @@ void kope_g5_command_list_prepare_raytracing_volume(kope_g5_command_list *list, 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); } + +void kope_g5_command_list_trace_rays(kope_g5_command_list *list) { + KOPE_G5_CALL1(command_list_trace_rays, list); +} diff --git a/Sources/kope/graphics5/commandlist.h b/Sources/kope/graphics5/commandlist.h index 8c5e158fd..bf7a11651 100644 --- a/Sources/kope/graphics5/commandlist.h +++ b/Sources/kope/graphics5/commandlist.h @@ -133,6 +133,8 @@ 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_trace_rays(kope_g5_command_list *list); + KOPE_FUNC void kope_g5_command_list_present(kope_g5_command_list *list); #ifdef __cplusplus