Skip to content

Commit

Permalink
Try to trace some rays
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Sep 15, 2024
1 parent 2837c7f commit 930495f
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand Down
35 changes: 35 additions & 0 deletions Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 <DirectXMath.h> // 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;
Expand All @@ -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<float>(GetTickCount64()) / 1000;

{
auto cube = XMMatrixRotationRollPitchYaw(time / 2, time / 3, time / 5);
cube *= XMMatrixTranslation(-1.5, 2, 2);
auto *ptr = reinterpret_cast<XMFLOAT3X4 *>(&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<XMFLOAT3X4 *>(&descs[1].Transform);
XMStoreFloat3x4(ptr, mirror);
}

{
auto floor = XMMatrixScaling(5, 5, 5);
floor *= XMMatrixTranslation(0, 0, 2);
auto *ptr = reinterpret_cast<XMFLOAT3X4 *>(&descs[2].Transform);
XMStoreFloat3x4(ptr, floor);
}

//

kope_g5_buffer_unlock(&hierarchy->d3d12.instances);

D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS inputs = {};
Expand Down
31 changes: 29 additions & 2 deletions Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Sources/kope/graphics5/commandlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 2 additions & 0 deletions Sources/kope/graphics5/commandlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 930495f

Please sign in to comment.