From 61b00950f16e3f679ad9c55821034620e2d3ab43 Mon Sep 17 00:00:00 2001 From: Robert Konrad Date: Fri, 13 Sep 2024 18:25:27 +0200 Subject: [PATCH] Try to create raytracing pipelines --- .../Sources/kope/direct3d12/device_structs.h | 2 +- .../Sources/kope/direct3d12/pipeline.cpp | 67 +++++++++++++++++++ .../kope/direct3d12/pipeline_functions.h | 4 ++ .../kope/direct3d12/pipeline_structs.h | 23 +++++-- 4 files changed, 91 insertions(+), 5 deletions(-) diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_structs.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_structs.h index 5fdb480e1..6da55043e 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_structs.h +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_structs.h @@ -18,7 +18,7 @@ struct ID3D12DescriptorHeap; #define KOPE_D3D12_FRAME_COUNT 2 typedef struct kope_d3d12_device { - struct ID3D12Device *device; + struct ID3D12Device5 *device; struct ID3D12CommandQueue *queue; struct IDXGISwapChain *swap_chain; diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline.cpp index 2df6303db..78111b458 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline.cpp @@ -5,6 +5,8 @@ #include +#include + static D3D12_BLEND convert_blend_factor(kope_d3d12_blend_factor factor) { switch (factor) { case KOPE_D3D12_BLEND_FACTOR_ZERO: @@ -359,3 +361,68 @@ void kope_d3d12_compute_pipeline_destroy(kope_d3d12_compute_pipeline *pipe) { pipe->pipe = NULL; } } + +void kope_d3d12_ray_pipeline_init(kope_d3d12_device *device, kope_d3d12_ray_pipeline *pipe, const kope_d3d12_ray_pipeline_parameters *parameters) { + D3D12_DXIL_LIBRARY_DESC lib = {0}; + lib.DXILLibrary.pShaderBytecode = ray_code; + lib.DXILLibrary.BytecodeLength = ray_code_size; + + D3D12_HIT_GROUP_DESC hit_group = {0}; + hit_group.HitGroupExport = L"HitGroup"; + hit_group.Type = D3D12_HIT_GROUP_TYPE_TRIANGLES; + + wchar_t closest_hit[1024]; + kinc_microsoft_convert_string(closest_hit, parameters->closest_shader_name, 1024); + hit_group.ClosestHitShaderImport = closest_hit; + + wchar_t any_hit[1024]; + wchar_t intersection[1024]; + + if (parameters->any_shader_name != NULL) { + kinc_microsoft_convert_string(any_hit, parameters->any_shader_name, 1024); + hit_group.AnyHitShaderImport = any_hit; + } + + if (parameters->intersection_shader_name != NULL) { + kinc_microsoft_convert_string(intersection, parameters->intersection_shader_name, 1024); + hit_group.IntersectionShaderImport = intersection; + } + + D3D12_RAYTRACING_SHADER_CONFIG shader_config = {0}; + shader_config.MaxPayloadSizeInBytes = 20; + shader_config.MaxAttributeSizeInBytes = 8; + + D3D12_RAYTRACING_PIPELINE_CONFIG pipeline_config = {0}; + pipeline_config.MaxTraceRecursionDepth = 3; + + D3D12_STATE_SUBOBJECT subobjects[4]; + + subobjects[0].Type = D3D12_STATE_SUBOBJECT_TYPE_DXIL_LIBRARY; + subobjects[0].pDesc = &lib; + + subobjects[1].Type = D3D12_STATE_SUBOBJECT_TYPE_HIT_GROUP; + subobjects[1].pDesc = &hit_group; + + subobjects[2].Type = D3D12_STATE_SUBOBJECT_TYPE_RAYTRACING_SHADER_CONFIG; + subobjects[2].pDesc = &shader_config; + + // subobjects[3].Type = D3D12_STATE_SUBOBJECT_TYPE_GLOBAL_ROOT_SIGNATURE; + // subobjects[3].pDesc = &globalSig; + + subobjects[3].Type = D3D12_STATE_SUBOBJECT_TYPE_RAYTRACING_PIPELINE_CONFIG; + subobjects[3].pDesc = &pipeline_config; + + D3D12_STATE_OBJECT_DESC desc; + desc.Type = D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE; + desc.NumSubobjects = 4; + desc.pSubobjects = subobjects; + + kinc_microsoft_affirm(device->device->CreateStateObject(&desc, IID_PPV_ARGS(&pipe->pipe))); +} + +void kope_d3d12_ray_pipeline_destroy(kope_d3d12_ray_pipeline *pipe) { + if (pipe->pipe != NULL) { + pipe->pipe->Release(); + pipe->pipe = NULL; + } +} diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline_functions.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline_functions.h index e8b51fece..3e9063195 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline_functions.h +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline_functions.h @@ -16,6 +16,10 @@ void kope_d3d12_compute_pipeline_init(kope_d3d12_device *device, kope_d3d12_comp 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_destroy(kope_d3d12_ray_pipeline *pipe); + #ifdef __cplusplus } #endif diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline_structs.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline_structs.h index 7e06a26ad..0fa31f824 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline_structs.h +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/pipeline_structs.h @@ -196,10 +196,6 @@ typedef struct kope_d3d12_render_pipeline_parameters { kope_d3d12_fragment_state fragment; } kope_d3d12_render_pipeline_parameters; -typedef struct kope_d3d12_compute_pipeline_parameters { - kope_d3d12_shader shader; -} kope_d3d12_compute_pipeline_parameters; - struct ID3D12PipelineState; struct ID3D12RootSignature; @@ -208,11 +204,30 @@ typedef struct kope_d3d12_render_pipeline { struct ID3D12RootSignature *root_signature; } kope_d3d12_render_pipeline; +typedef struct kope_d3d12_compute_pipeline_parameters { + kope_d3d12_shader shader; +} kope_d3d12_compute_pipeline_parameters; + typedef struct kope_d3d12_compute_pipeline { struct ID3D12PipelineState *pipe; struct ID3D12RootSignature *root_signature; } kope_d3d12_compute_pipeline; +typedef struct kope_d3d12_ray_pipeline_parameters { + const char *gen_shader_name; + const char *miss_shader_name; + const char *closest_shader_name; + const char *intersection_shader_name; + const char *any_shader_name; +} kope_d3d12_ray_pipeline_parameters; + +struct ID3D12StateObject; + +typedef struct kope_d3d12_ray_pipeline { + struct ID3D12StateObject *pipe; + struct ID3D12RootSignature *root_signature; +} kope_d3d12_ray_pipeline; + #ifdef __cplusplus } #endif