From 2135baca6f89cf8b8e26fe3e8cb3209494406f49 Mon Sep 17 00:00:00 2001 From: Gregory Wells Date: Thu, 12 Jun 2025 18:08:08 -0400 Subject: [PATCH] add some utilities for double buffering --- src/core/buffers/gryphn_buffer.h | 8 ++++++-- .../command/command_buffer/gryphn_command_buffer.c | 10 +++++++++- .../command/command_buffer/gryphn_command_buffer.h | 13 ++++++++++++- src/core/sync/fence/gryphn_fence.h | 2 ++ src/core/sync/semaphore/gryphn_semaphore.h | 2 ++ src/core/uniforms/gryphn_uniform.h | 2 ++ src/core/uniforms/gryphn_uniform_pool.c | 8 ++++++-- src/core/uniforms/gryphn_uniform_pool.h | 3 ++- 8 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/core/buffers/gryphn_buffer.h b/src/core/buffers/gryphn_buffer.h index c854a84..f1a4ca5 100644 --- a/src/core/buffers/gryphn_buffer.h +++ b/src/core/buffers/gryphn_buffer.h @@ -1,6 +1,7 @@ #pragma once #include "stdlib.h" #include "utils/gryphn_error_code.h" +#include "utils/lists/gryphn_array_list.h" #include typedef enum gnIndexType { @@ -30,8 +31,11 @@ struct gnBuffer_t { gnBufferInfo info; }; #endif +typedef void* gnBufferMemory; +GN_ARRAY_LIST(gnBuffer); +GN_ARRAY_LIST(gnBufferMemory); gnReturnCode gnCreateBuffer(gnBufferHandle* buffer, gnOutputDeviceHandle device, gnBufferInfo info); -void gnBufferData(gnBufferHandle buffer, size_t dataSize, void* data); -void* gnMapBuffer(gnBufferHandle buffer); +void gnBufferData(gnBufferHandle buffer, size_t dataSize, gnBufferMemory data); +gnBufferMemory gnMapBuffer(gnBufferHandle buffer); void gnDestroyBuffer(gnBufferHandle buffer); diff --git a/src/core/command/command_buffer/gryphn_command_buffer.c b/src/core/command/command_buffer/gryphn_command_buffer.c index 3a1713d..beb89f1 100644 --- a/src/core/command/command_buffer/gryphn_command_buffer.c +++ b/src/core/command/command_buffer/gryphn_command_buffer.c @@ -1,7 +1,7 @@ #include "gryphn_command_buffer.h" #include "core/gryphn_platform_functions.h" -gnReturnCode gnCommandPoolAllocateCommandBuffers(gnCommandBufferHandle* buffers, uint32_t count, gnCommandPoolHandle commandPool) { +gnReturnCode gnCommandPoolAllocateCommandBuffersFromPointer(gnCommandBufferHandle* buffers, uint32_t count, gnCommandPoolHandle commandPool) { for (int i = 0; i < count; i++) { buffers[i] = malloc(sizeof(struct gnCommandBuffer_t)); buffers[i]->commandPool = commandPool; @@ -9,6 +9,14 @@ gnReturnCode gnCommandPoolAllocateCommandBuffers(gnCommandBufferHandle* buffers, return commandPool->commandFunctions->_gnCommandPoolAllocateCommandBuffers(buffers, count, commandPool); } +gnReturnCode gnCommandPoolAllocateCommandBuffersFromList(gnCommandBufferArrayList buffers, uint32_t count, gnCommandPoolHandle commandPool) { + for (int i = 0; i < count; i++) { + buffers.data[i] = malloc(sizeof(struct gnCommandBuffer_t)); + buffers.data[i]->commandPool = commandPool; + } + return gnCommandPoolAllocateCommandBuffersFromPointer(buffers.data, count, commandPool); +} + void gnResetCommandBuffer(gnCommandBufferHandle commandBuffer) { commandBuffer->commandPool->commandFunctions->_gnResetCommandBuffer(commandBuffer); } diff --git a/src/core/command/command_buffer/gryphn_command_buffer.h b/src/core/command/command_buffer/gryphn_command_buffer.h index 5ce643d..cecfcff 100644 --- a/src/core/command/command_buffer/gryphn_command_buffer.h +++ b/src/core/command/command_buffer/gryphn_command_buffer.h @@ -1,6 +1,7 @@ #pragma once #include "stdint.h" #include "utils/gryphn_error_code.h" +#include "utils/lists/gryphn_array_list.h" #include "core/gryphn_handles.h" #ifdef GN_REVEAL_IMPL @@ -10,7 +11,17 @@ struct gnCommandBuffer_t { }; #endif -gnReturnCode gnCommandPoolAllocateCommandBuffers(gnCommandBufferHandle* buffers, uint32_t count, gnCommandPoolHandle commandPool); +GN_ARRAY_LIST(gnCommandBuffer); + +gnReturnCode gnCommandPoolAllocateCommandBuffersFromPointer(gnCommandBufferHandle* buffers, uint32_t count, gnCommandPoolHandle commandPool); +// will reserve the space for ${count} number of elements +gnReturnCode gnCommandPoolAllocateCommandBuffersFromList(gnCommandBufferArrayList buffers, uint32_t count, gnCommandPoolHandle commandPool); + +#define gnCommandPoolAllocateCommandBuffers(buffers, count, commandPool) \ + _Generic((buffers), \ + gnCommandBufferArrayList: gnCommandPoolAllocateCommandBuffersFromList, \ + default: gnCommandPoolAllocateCommandBuffersFromPointer \ + )(buffers, count, commandPool) void gnResetCommandBuffer(gnCommandBufferHandle commandBuffer); gnReturnCode gnBeginCommandBuffer(gnCommandBufferHandle commandBuffer); diff --git a/src/core/sync/fence/gryphn_fence.h b/src/core/sync/fence/gryphn_fence.h index d58a936..c028bd2 100644 --- a/src/core/sync/fence/gryphn_fence.h +++ b/src/core/sync/fence/gryphn_fence.h @@ -2,6 +2,7 @@ #include "stdint.h" #include "utils/gryphn_bool.h" #include "utils/gryphn_error_code.h" +#include "utils/lists/gryphn_array_list.h" #include "core/gryphn_handles.h" #ifdef GN_REVEAL_IMPL @@ -11,6 +12,7 @@ struct gnFence_t { gnBool signaled; }; #endif +GN_ARRAY_LIST(gnFence); gnReturnCode gnCreateFence(gnFenceHandle* fence, gnOutputDeviceHandle device); void gnSignalFence(gnFenceHandle fence); diff --git a/src/core/sync/semaphore/gryphn_semaphore.h b/src/core/sync/semaphore/gryphn_semaphore.h index b647218..58f8ca3 100644 --- a/src/core/sync/semaphore/gryphn_semaphore.h +++ b/src/core/sync/semaphore/gryphn_semaphore.h @@ -1,6 +1,7 @@ #pragma once #include "core/gryphn_handles.h" #include "utils/gryphn_error_code.h" +#include "utils/lists/gryphn_array_list.h" #ifdef GN_REVEAL_IMPL struct gnSemaphore_t { @@ -8,6 +9,7 @@ struct gnSemaphore_t { gnOutputDeviceHandle device; }; #endif +GN_ARRAY_LIST(gnSemaphore); gnReturnCode gnCreateSemaphore(gnSemaphore* semaphore, struct gnOutputDevice_t* device); void gnDestroySemaphore(gnSemaphore semaphore); diff --git a/src/core/uniforms/gryphn_uniform.h b/src/core/uniforms/gryphn_uniform.h index b7be46a..65a6dcb 100644 --- a/src/core/uniforms/gryphn_uniform.h +++ b/src/core/uniforms/gryphn_uniform.h @@ -1,6 +1,7 @@ #pragma once #include "stdint.h" #include "stdlib.h" +#include "utils/lists/gryphn_array_list.h" #include "core/gryphn_handles.h" typedef struct gnBufferUniformInfo { @@ -16,5 +17,6 @@ struct gnUniform_t { gnUniformPool pool; }; #endif +GN_ARRAY_LIST(gnUniform) void gnUpdateBufferUniform(gnUniform uniform, gnBufferUniformInfo bufferInfo); diff --git a/src/core/uniforms/gryphn_uniform_pool.c b/src/core/uniforms/gryphn_uniform_pool.c index 7fafa44..abc7f02 100644 --- a/src/core/uniforms/gryphn_uniform_pool.c +++ b/src/core/uniforms/gryphn_uniform_pool.c @@ -11,11 +11,15 @@ gnReturnCode gnCreateUniformPool(gnUniformPool* pool, gnDeviceHandle device) { } // you own this memory now -gnUniform* gnUniformPoolAllocateUniforms(gnUniformPool pool, const gnUniformLayout layout) { +gnUniformArrayList gnUniformPoolAllocateUniforms(gnUniformPool pool, const gnUniformLayout layout, uint32_t count) { gnUniform* uniforms = pool->device->deviceFunctions->_gnUniformPoolAllocateUniforms(pool, layout); for (int i = 0; i < layout.uniformBindingCount; i++) uniforms[i]->pool = pool; - return uniforms; + + gnUniformArrayList list = gnUniformArrayListCreate(); + gnUniformArrayListResize(&list, layout.uniformBindingCount); + for (int i = 0; i < layout.uniformBindingCount; i++) list.data[i] = uniforms[i]; + return list; } void gnDestroyUniformPool(gnUniformPool pool) { diff --git a/src/core/uniforms/gryphn_uniform_pool.h b/src/core/uniforms/gryphn_uniform_pool.h index 348aef7..d2707ef 100644 --- a/src/core/uniforms/gryphn_uniform_pool.h +++ b/src/core/uniforms/gryphn_uniform_pool.h @@ -2,6 +2,7 @@ #include "core/gryphn_handles.h" #include "utils/gryphn_error_code.h" #include "core/uniforms/gryphn_uniform_layout.h" +#include "core/uniforms/gryphn_uniform.h" #ifdef GN_REVEAL_IMPL struct gnUniformPool_t { @@ -11,5 +12,5 @@ struct gnUniformPool_t { #endif gnReturnCode gnCreateUniformPool(gnUniformPool* pool, gnDeviceHandle device); -gnUniform* gnUniformPoolAllocateUniforms(gnUniformPool pool, const gnUniformLayout layout); +gnUniformArrayList gnUniformPoolAllocateUniforms(gnUniformPool pool, const gnUniformLayout layout); void gnDestroyUniformPool(gnUniformPool pool);