From 014b315faa23777033c87ca54220b02f24241388 Mon Sep 17 00:00:00 2001 From: Greg Wells Date: Fri, 30 May 2025 11:00:20 -0400 Subject: [PATCH] vulkan+gryphn sync impl --- .../vulkan/src/sync/fence/vulkan_fence.c | 23 ++++++++++++++++++ .../vulkan/src/sync/fence/vulkan_fence.h | 7 ++++++ .../src/sync/semaphore/vulkan_semaphore.c | 17 +++++++++++++ .../src/sync/semaphore/vulkan_semaphore.h | 7 ++++++ src/core/gryphn_platform_functions.h | 11 +++++++++ src/core/instance/init/gryphn_init.c | 7 ++++++ src/core/sync/fence/gryphn_fence.c | 24 +++++++++++++++++++ src/core/sync/fence/gryphn_fence.h | 2 ++ src/core/sync/semaphore/gryphn_semaphore.c | 10 ++++++++ src/core/sync/semaphore/gryphn_semaphore.h | 1 + src/utils/gryphn_error_code.h | 6 ++++- 11 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 rendering_api/vulkan/src/sync/fence/vulkan_fence.c create mode 100644 rendering_api/vulkan/src/sync/fence/vulkan_fence.h create mode 100644 rendering_api/vulkan/src/sync/semaphore/vulkan_semaphore.c create mode 100644 rendering_api/vulkan/src/sync/semaphore/vulkan_semaphore.h create mode 100644 src/core/sync/fence/gryphn_fence.c create mode 100644 src/core/sync/semaphore/gryphn_semaphore.c diff --git a/rendering_api/vulkan/src/sync/fence/vulkan_fence.c b/rendering_api/vulkan/src/sync/fence/vulkan_fence.c new file mode 100644 index 0000000..c2cd997 --- /dev/null +++ b/rendering_api/vulkan/src/sync/fence/vulkan_fence.c @@ -0,0 +1,23 @@ +#include "vulkan_fence.h" +#include "output_device/vulkan_output_devices.h" + +gnReturnCode gnCreateFenceFn(struct gnFence_t* fence, struct gnOutputDevice_t* device) { + fence->fence = malloc(sizeof(gnPlatformFence)); + VkFenceCreateInfo fenceInfo = { + .sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO + }; + if (vkCreateFence(device->outputDevice->device, &fenceInfo, NULL, &fence->fence->fence) != VK_SUCCESS) + return GN_FAILED_TO_CREATE_FENCE; + return GN_SUCCESS; +} +void gnSignalFenceFn(struct gnFence_t* fence) {} +void gnWaitForFenceFn(struct gnFence_t* fence, uint64_t timeout) { + vkWaitForFences(fence->device->outputDevice->device, 1, &fence->fence->fence, VK_TRUE, timeout); +} +void gnResetFenceFn(struct gnFence_t* fence) { + vkResetFences(fence->device->outputDevice->device, 1, &fence->fence->fence); +} +void gnDestroyFenceFn(struct gnFence_t* fence) { + vkDestroyFence(fence->device->outputDevice->device, fence->fence->fence, NULL); + free(fence->fence); +} diff --git a/rendering_api/vulkan/src/sync/fence/vulkan_fence.h b/rendering_api/vulkan/src/sync/fence/vulkan_fence.h new file mode 100644 index 0000000..8c140cf --- /dev/null +++ b/rendering_api/vulkan/src/sync/fence/vulkan_fence.h @@ -0,0 +1,7 @@ +#pragma once +#include +#include "core/sync/fence/gryphn_fence.h" + +typedef struct gnPlatformFence_t { + VkFence fence; +} gnPlatformFence; diff --git a/rendering_api/vulkan/src/sync/semaphore/vulkan_semaphore.c b/rendering_api/vulkan/src/sync/semaphore/vulkan_semaphore.c new file mode 100644 index 0000000..211b035 --- /dev/null +++ b/rendering_api/vulkan/src/sync/semaphore/vulkan_semaphore.c @@ -0,0 +1,17 @@ +#include "vulkan_semaphore.h" +#include "output_device/vulkan_output_devices.h" + +gnReturnCode gnCreateSemaphoreFn(struct gnSemaphore_t* semaphore, struct gnOutputDevice_t* device) { + semaphore->semaphore = malloc(sizeof(gnPlatformSemaphore)); + VkSemaphoreCreateInfo semaphoreInfo = { + .sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO + }; + + if (vkCreateSemaphore(device->outputDevice->device, &semaphoreInfo, NULL, &semaphore->semaphore->semaphore)) + return GN_FAILED_TO_CREATE_SEMAPHORE; + return GN_SUCCESS; +} +void gnDestroySemaphoreFn(struct gnSemaphore_t* semaphore) { + vkDestroySemaphore(semaphore->device->outputDevice->device, semaphore->semaphore->semaphore, NULL); + free(semaphore->semaphore); +} diff --git a/rendering_api/vulkan/src/sync/semaphore/vulkan_semaphore.h b/rendering_api/vulkan/src/sync/semaphore/vulkan_semaphore.h new file mode 100644 index 0000000..3c47937 --- /dev/null +++ b/rendering_api/vulkan/src/sync/semaphore/vulkan_semaphore.h @@ -0,0 +1,7 @@ +#pragma once +#include +#include "core/sync/semaphore/gryphn_semaphore.h" + +typedef struct gnPlatformSemaphore_t { + VkSemaphore semaphore; +} gnPlatformSemaphore; diff --git a/src/core/gryphn_platform_functions.h b/src/core/gryphn_platform_functions.h index 0843d53..91de13f 100644 --- a/src/core/gryphn_platform_functions.h +++ b/src/core/gryphn_platform_functions.h @@ -14,6 +14,8 @@ #include "command/command_pool/gryphn_command_pool.h" #include "command/command_buffer/gryphn_command_buffer.h" #include "renderpass/gryphn_render_pass.h" +#include "sync/fence/gryphn_fence.h" +#include "sync/semaphore/gryphn_semaphore.h" typedef struct gnFunctions_t { gnReturnCode (*_gnCreateInstance)(gnInstance* instance, struct gnInstanceInfo_t info); @@ -72,6 +74,15 @@ typedef struct gnDeviceFunctions_t { gnReturnCode (*_gnCreateCommandPool)(struct gnCommandPool_t* commandPool, struct gnOutputDevice_t* device, struct gnCommandPoolInfo_t info); void (*_gnDestroyCommandPool)(struct gnCommandPool_t* commandPool); + + gnReturnCode (*_gnCreateSemaphore)(struct gnSemaphore_t* semaphore, struct gnOutputDevice_t* device); + void (*_gnDestroySemaphore)(struct gnSemaphore_t* semaphore); + + gnReturnCode (*_gnCreateFence)(struct gnFence_t* fence, struct gnOutputDevice_t* device); + void (*_gnSignalFence)(struct gnFence_t* fence); + void (*_gnWaitForFence)(struct gnFence_t* fence, uint64_t timeout); + void (*_gnResetFence)(struct gnFence_t* fence); + void (*_gnDestroyFence)(struct gnFence_t* fence); } gnDeviceFunctions; typedef struct gnCommandFunctions_t { diff --git a/src/core/instance/init/gryphn_init.c b/src/core/instance/init/gryphn_init.c index 58fc06c..6c85556 100644 --- a/src/core/instance/init/gryphn_init.c +++ b/src/core/instance/init/gryphn_init.c @@ -81,6 +81,13 @@ void gnLoadDeviceFunctions(struct gnDynamicLibrary_t* lib, struct gnDeviceFuncti gnLoadDLLFunction(lib, functions->_gnDestroyFramebuffer, "gnDestroyFramebufferFn"); gnLoadDLLFunction(lib, functions->_gnCreateCommandPool, "gnCreateCommandPoolFn"); gnLoadDLLFunction(lib, functions->_gnDestroyCommandPool, "gnDestroyCommandPoolFn"); + gnLoadDLLFunction(lib, functions->_gnCreateSemaphore, "gnCreateSemaphoreFn"); + gnLoadDLLFunction(lib, functions->_gnDestroySemaphore, "gnDestroySemaphoreFn"); + gnLoadDLLFunction(lib, functions->_gnCreateFence, "gnCreateFenceFn"); + gnLoadDLLFunction(lib, functions->_gnSignalFence, "gnSignalFenceFn"); + gnLoadDLLFunction(lib, functions->_gnWaitForFence, "gnWaitForFenceFn"); + gnLoadDLLFunction(lib, functions->_gnResetFence, "gnResetFenceFn"); + gnLoadDLLFunction(lib, functions->_gnDestroyFence, "gnDestroyFenceFn"); } void gnLoadCommandFunctions(struct gnDynamicLibrary_t* lib, struct gnCommandFunctions_t* functions) { diff --git a/src/core/sync/fence/gryphn_fence.c b/src/core/sync/fence/gryphn_fence.c new file mode 100644 index 0000000..4b365ec --- /dev/null +++ b/src/core/sync/fence/gryphn_fence.c @@ -0,0 +1,24 @@ +#include "gryphn_fence.h" +#include "core/gryphn_platform_functions.h" +#include "stdio.h" + +gnReturnCode gnCreateFence(struct gnFence_t* fence, struct gnOutputDevice_t* device) { + fence->device = device; + fence->signaled = gnFalse; + return device->deviceFunctions->_gnCreateFence(fence, device); +} +void gnSignalFence(struct gnFence_t* fence) { + fence->signaled = gnTrue; + // fence->device->deviceFunctions->_gnSignalFence(fence); +} +void gnWaitForFence(struct gnFence_t* fence, uint64_t timeout) { + if (fence->signaled == gnTrue) return; + fence->device->deviceFunctions->_gnWaitForFence(fence, timeout); +} +void gnResetFence(struct gnFence_t* fence) { + fence->signaled = gnFalse; + fence->device->deviceFunctions->_gnResetFence(fence); +} +void gnDestroyFence(struct gnFence_t* fence) { + fence->device->deviceFunctions->_gnDestroyFence(fence); +} diff --git a/src/core/sync/fence/gryphn_fence.h b/src/core/sync/fence/gryphn_fence.h index 151fd83..eeb810d 100644 --- a/src/core/sync/fence/gryphn_fence.h +++ b/src/core/sync/fence/gryphn_fence.h @@ -4,6 +4,8 @@ typedef struct gnFence_t { struct gnPlatformFence_t* fence; + struct gnOutputDevice_t* device; + gnBool signaled; } gnFence; gnReturnCode gnCreateFence(struct gnFence_t* fence, struct gnOutputDevice_t* device); diff --git a/src/core/sync/semaphore/gryphn_semaphore.c b/src/core/sync/semaphore/gryphn_semaphore.c new file mode 100644 index 0000000..7be89aa --- /dev/null +++ b/src/core/sync/semaphore/gryphn_semaphore.c @@ -0,0 +1,10 @@ +#include "gryphn_semaphore.h" +#include "core/gryphn_platform_functions.h" + +gnReturnCode gnCreateSemaphore(struct gnSemaphore_t* semaphore, struct gnOutputDevice_t* device) { + semaphore->device = device; + return device->deviceFunctions->_gnCreateSemaphore(semaphore, device); +} +void gnDestroySemaphore(struct gnSemaphore_t* semaphore) { + semaphore->device->deviceFunctions->_gnDestroySemaphore(semaphore); +} diff --git a/src/core/sync/semaphore/gryphn_semaphore.h b/src/core/sync/semaphore/gryphn_semaphore.h index a4607dc..bb65e96 100644 --- a/src/core/sync/semaphore/gryphn_semaphore.h +++ b/src/core/sync/semaphore/gryphn_semaphore.h @@ -3,6 +3,7 @@ typedef struct gnSemaphore_t { struct gnPlatformSemaphore_t* semaphore; + struct gnOutputDevice_t* device; } gnSemaphore; gnReturnCode gnCreateSemaphore(struct gnSemaphore_t* semaphore, struct gnOutputDevice_t* device); diff --git a/src/utils/gryphn_error_code.h b/src/utils/gryphn_error_code.h index 7f8220d..c204169 100644 --- a/src/utils/gryphn_error_code.h +++ b/src/utils/gryphn_error_code.h @@ -30,7 +30,9 @@ typedef enum gnReturnCode_t { GN_FAILED_TO_CREATE_COMMAND_POOL, GN_FAILED_TO_BEGIN_RECORDING, GN_FAIELD_TO_END_RECORDING, - GN_FAILED_TO_ALLOCATE_COMMAND_BUFFERS + GN_FAILED_TO_ALLOCATE_COMMAND_BUFFERS, + GN_FAILED_TO_CREATE_FENCE, + GN_FAILED_TO_CREATE_SEMAPHORE } gnReturnCode; typedef gnReturnCode gnErrorCode; @@ -66,5 +68,7 @@ static const char* gnErrorCodeToCString(enum gnReturnCode_t returnCode) { case GN_FAILED_TO_BEGIN_RECORDING: return "GN_FAILED_TO_BEGIN_RECORDING"; case GN_FAIELD_TO_END_RECORDING: return "GN_FAIELD_TO_END_RECORDING"; case GN_FAILED_TO_ALLOCATE_COMMAND_BUFFERS: return "GN_FAILED_TO_ALLOCATE_COMMAND_BUFFERS"; + case GN_FAILED_TO_CREATE_FENCE: return "GN_FAILED_TO_CREATE_FENCE"; + case GN_FAILED_TO_CREATE_SEMAPHORE: return "GN_FAILED_TO_CREATE_SEMAPHORE"; } }