From 2fc8123daec54bb01cc2291691bccdcef91f51e4 Mon Sep 17 00:00:00 2001 From: Robert Konrad Date: Sun, 1 Dec 2024 02:00:33 +0100 Subject: [PATCH] Add fences --- .../Sources/kope/direct3d12/descriptorset.cpp | 3 ++ .../Sources/kope/direct3d12/device.cpp | 33 +++++++++++++++++++ .../kope/direct3d12/device_functions.h | 6 ++++ .../Sources/kope/direct3d12/fence.cpp | 9 +++++ .../Sources/kope/direct3d12/fence_functions.h | 16 +++++++++ .../Sources/kope/direct3d12/fence_structs.h | 20 +++++++++++ Sources/kope/graphics5/device.c | 12 +++++++ Sources/kope/graphics5/device.h | 7 ++++ Sources/kope/graphics5/fence.c | 9 +++++ Sources/kope/graphics5/fence.h | 26 +++++++++++++++ 10 files changed, 141 insertions(+) create mode 100644 Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/fence.cpp create mode 100644 Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/fence_functions.h create mode 100644 Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/fence_structs.h create mode 100644 Sources/kope/graphics5/fence.c create mode 100644 Sources/kope/graphics5/fence.h diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp index 29e32f9e1..69e67b68e 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/descriptorset.cpp @@ -199,6 +199,9 @@ void kope_d3d12_descriptor_set_prepare_uav_texture(kope_g5_command_list *list, c barrier.Transition.StateBefore = (D3D12_RESOURCE_STATES) texture_view->texture->d3d12.resource_states[kope_d3d12_texture_resource_state_index(texture_view->texture, texture_view->base_mip_level, 0)]; + if (list->d3d12.list_type == D3D12_COMMAND_LIST_TYPE_COMPUTE && barrier.Transition.StateBefore == D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE) { + barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COMMON; + } barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_UNORDERED_ACCESS; barrier.Transition.Subresource = D3D12CalcSubresource(texture_view->base_mip_level, 0, 0, texture_view->texture->d3d12.mip_level_count, texture_view->texture->d3d12.depth_or_array_layers); diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp index b9b5cf730..c2be0b612 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device.cpp @@ -327,6 +327,7 @@ static D3D12_COMMAND_LIST_TYPE convert_command_list_type(kope_g5_command_list_ty case KOPE_G5_COMMAND_LIST_TYPE_COPY: return D3D12_COMMAND_LIST_TYPE_COPY; } + return D3D12_COMMAND_LIST_TYPE_DIRECT; } void kope_d3d12_device_create_command_list(kope_g5_device *device, kope_g5_command_list_type type, kope_g5_command_list *list) { @@ -881,3 +882,35 @@ void kope_d3d12_device_create_query_set(kope_g5_device *device, const kope_g5_qu uint32_t kope_d3d12_device_align_texture_row_bytes(kope_g5_device *device, uint32_t row_bytes) { return (uint32_t)align_pow2((int)row_bytes, D3D12_TEXTURE_DATA_PITCH_ALIGNMENT); } + +void kope_d3d12_device_create_fence(kope_g5_device *device, kope_g5_fence *fence) { + device->d3d12.device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_GRAPHICS_PPV_ARGS(&fence->d3d12.fence)); +} + +void kope_d3d12_device_signal(kope_g5_device *device, kope_g5_command_list_type list_type, kope_g5_fence *fence, uint64_t value) { + switch (list_type) { + case KOPE_G5_COMMAND_LIST_TYPE_GRAPHICS: + device->d3d12.queue->Signal(fence->d3d12.fence, value); + break; + case KOPE_G5_COMMAND_LIST_TYPE_ASYNC: + device->d3d12.async_queue->Signal(fence->d3d12.fence, value); + break; + case KOPE_G5_COMMAND_LIST_TYPE_COPY: + device->d3d12.copy_queue->Signal(fence->d3d12.fence, value); + break; + } +} + +void kope_d3d12_device_wait(kope_g5_device *device, kope_g5_command_list_type list_type, kope_g5_fence *fence, uint64_t value) { + switch (list_type) { + case KOPE_G5_COMMAND_LIST_TYPE_GRAPHICS: + device->d3d12.queue->Wait(fence->d3d12.fence, value); + break; + case KOPE_G5_COMMAND_LIST_TYPE_ASYNC: + device->d3d12.async_queue->Wait(fence->d3d12.fence, value); + break; + case KOPE_G5_COMMAND_LIST_TYPE_COPY: + device->d3d12.copy_queue->Wait(fence->d3d12.fence, value); + break; + } +} diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_functions.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_functions.h index 2594620f0..8299e5631 100644 --- a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_functions.h +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/device_functions.h @@ -42,8 +42,14 @@ void kope_d3d12_device_create_raytracing_hierarchy(kope_g5_device *device, kope_ void kope_d3d12_device_create_query_set(kope_g5_device *device, const kope_g5_query_set_parameters *parameters, kope_g5_query_set *query_set); +void kope_d3d12_device_create_fence(kope_g5_device *device, kope_g5_fence *fence); + uint32_t kope_d3d12_device_align_texture_row_bytes(kope_g5_device *device, uint32_t row_bytes); +void kope_d3d12_device_signal(kope_g5_device *device, kope_g5_command_list_type list_type, kope_g5_fence *fence, uint64_t value); + +void kope_d3d12_device_wait(kope_g5_device *device, kope_g5_command_list_type list_type, kope_g5_fence *fence, uint64_t value); + #ifdef __cplusplus } #endif diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/fence.cpp b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/fence.cpp new file mode 100644 index 000000000..63186565d --- /dev/null +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/fence.cpp @@ -0,0 +1,9 @@ +#include "fence_functions.h" + +#include + +#include + +void kope_d3d12_fence_destroy(kope_g5_fence *fence) { + fence->d3d12.fence->Release(); +} diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/fence_functions.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/fence_functions.h new file mode 100644 index 000000000..07ac93783 --- /dev/null +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/fence_functions.h @@ -0,0 +1,16 @@ +#ifndef KOPE_D3D12_FENCE_FUNCTIONS_HEADER +#define KOPE_D3D12_FENCE_FUNCTIONS_HEADER + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +void kope_d3d12_fence_destroy(kope_g5_fence *fence); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/fence_structs.h b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/fence_structs.h new file mode 100644 index 000000000..bf01d8285 --- /dev/null +++ b/Backends/Graphics5/Direct3D12/Sources/kope/direct3d12/fence_structs.h @@ -0,0 +1,20 @@ +#ifndef KOPE_D3D12_FENCE_STRUCTS_HEADER +#define KOPE_D3D12_FENCE_STRUCTS_HEADER + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +struct ID3D12Fence; + +typedef struct kope_d3d12_fence { + struct ID3D12Fence *fence; +} kope_d3d12_fence; + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/Sources/kope/graphics5/device.c b/Sources/kope/graphics5/device.c index 980dec9e7..ed743aa83 100644 --- a/Sources/kope/graphics5/device.c +++ b/Sources/kope/graphics5/device.c @@ -52,6 +52,10 @@ void kope_g5_device_create_query_set(kope_g5_device *device, const kope_g5_query KOPE_G5_CALL3(device_create_query_set, device, parameters, query_set); } +void kope_g5_device_create_fence(kope_g5_device *device, kope_g5_fence *fence) { + KOPE_G5_CALL2(device_create_fence, device, fence); +} + void kope_g5_device_execute_command_list(kope_g5_device *device, kope_g5_command_list *list) { KOPE_G5_CALL2(device_execute_command_list, device, list); } @@ -77,3 +81,11 @@ void kope_g5_device_wait_until_idle(kope_g5_device *device) { uint32_t kope_g5_device_align_texture_row_bytes(kope_g5_device *device, uint32_t row_bytes) { return KOPE_G5_CALL2(device_align_texture_row_bytes, device, row_bytes); } + +void kope_g5_device_signal(kope_g5_device *device, kope_g5_command_list_type list_type, kope_g5_fence *fence, uint64_t value) { + KOPE_G5_CALL4(device_signal, device, list_type, fence, value); +} + +void kope_g5_device_wait(kope_g5_device *device, kope_g5_command_list_type list_type, kope_g5_fence *fence, uint64_t value) { + KOPE_G5_CALL4(device_wait, device, list_type, fence, value); +} diff --git a/Sources/kope/graphics5/device.h b/Sources/kope/graphics5/device.h index 601d415ad..f0f65908c 100644 --- a/Sources/kope/graphics5/device.h +++ b/Sources/kope/graphics5/device.h @@ -8,6 +8,7 @@ #include "api.h" #include "buffer.h" #include "commandlist.h" +#include "fence.h" #include "sampler.h" #include "textureformat.h" @@ -138,6 +139,8 @@ typedef struct kope_g5_query_set_parameters { KOPE_FUNC void kope_g5_device_create_query_set(kope_g5_device *device, const kope_g5_query_set_parameters *parameters, kope_g5_query_set *query_set); +KOPE_FUNC void kope_g5_device_create_fence(kope_g5_device *device, kope_g5_fence *fence); + KOPE_FUNC void kope_g5_device_execute_command_list(kope_g5_device *device, kope_g5_command_list *list); KOPE_FUNC void kope_g5_device_wait_until_idle(kope_g5_device *device); @@ -158,6 +161,10 @@ KOPE_FUNC void kope_g5_device_create_raytracing_hierarchy(kope_g5_device *device KOPE_FUNC uint32_t kope_g5_device_align_texture_row_bytes(kope_g5_device *device, uint32_t row_bytes); +KOPE_FUNC void kope_g5_device_signal(kope_g5_device *device, kope_g5_command_list_type list_type, kope_g5_fence *fence, uint64_t value); + +KOPE_FUNC void kope_g5_device_wait(kope_g5_device *device, kope_g5_command_list_type list_type, kope_g5_fence *fence, uint64_t value); + #ifdef __cplusplus } #endif diff --git a/Sources/kope/graphics5/fence.c b/Sources/kope/graphics5/fence.c new file mode 100644 index 000000000..1cd8b9e85 --- /dev/null +++ b/Sources/kope/graphics5/fence.c @@ -0,0 +1,9 @@ +#include "fence.h" + +#ifdef KOPE_DIRECT3D12 +#include +#endif + +void kope_g5_fence_destroy(kope_g5_fence *fence) { + KOPE_G5_CALL1(fence_destroy, fence); +} diff --git a/Sources/kope/graphics5/fence.h b/Sources/kope/graphics5/fence.h new file mode 100644 index 000000000..77c3c160e --- /dev/null +++ b/Sources/kope/graphics5/fence.h @@ -0,0 +1,26 @@ +#ifndef KOPE_G5_FENCE_HEADER +#define KOPE_G5_FENCE_HEADER + +#include + +#include "api.h" + +#ifdef KOPE_DIRECT3D12 +#include +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct kope_g5_fence { + KOPE_G5_IMPL(fence); +} kope_g5_fence; + +KOPE_FUNC void kope_g5_fence_destroy(kope_g5_fence *fence); + +#ifdef __cplusplus +} +#endif + +#endif