From 292187b494a3b5a9ad59df46f150bba52ee9eda3 Mon Sep 17 00:00:00 2001 From: Greg Wells Date: Sun, 8 Jun 2025 09:49:48 -0400 Subject: [PATCH] uniform pool --- include/gryphn/gryphn.h | 1 + .../vulkan/src/buffers/vulkan_buffer.c | 38 ++++++++++++------- .../command_pool/vulkan_command_pool.c | 2 +- .../vulkan_graphics_pipeline.c | 2 +- .../vulkan/src/uniforms/vulkan_uniform_pool.c | 11 ++++++ .../vulkan/src/uniforms/vulkan_uniform_pool.h | 15 ++++++++ src/core/buffers/gryphn_buffer.h | 5 ++- src/core/gryphn_handles.h | 1 + src/core/gryphn_platform_functions.h | 2 + src/core/instance/init/gryphn_init.c | 1 + .../gryphn_graphics_pipeline.h | 2 +- src/core/uniforms/gryphn_uniform.h | 7 ++++ .../gryphn_uniform_layout.h | 2 +- src/core/uniforms/gryphn_uniform_pool.c | 10 +++++ src/core/uniforms/gryphn_uniform_pool.h | 13 +++++++ 15 files changed, 93 insertions(+), 19 deletions(-) create mode 100644 rendering_api/vulkan/src/uniforms/vulkan_uniform_pool.c create mode 100644 rendering_api/vulkan/src/uniforms/vulkan_uniform_pool.h create mode 100644 src/core/uniforms/gryphn_uniform.h rename src/core/{pipelines => uniforms}/gryphn_uniform_layout.h (92%) create mode 100644 src/core/uniforms/gryphn_uniform_pool.c create mode 100644 src/core/uniforms/gryphn_uniform_pool.h diff --git a/include/gryphn/gryphn.h b/include/gryphn/gryphn.h index 2e9ed1f..e8f4c55 100644 --- a/include/gryphn/gryphn.h +++ b/include/gryphn/gryphn.h @@ -22,3 +22,4 @@ #include #include #include +#include diff --git a/rendering_api/vulkan/src/buffers/vulkan_buffer.c b/rendering_api/vulkan/src/buffers/vulkan_buffer.c index dcf465b..c54f645 100644 --- a/rendering_api/vulkan/src/buffers/vulkan_buffer.c +++ b/rendering_api/vulkan/src/buffers/vulkan_buffer.c @@ -5,10 +5,13 @@ #include "output_device/vulkan_physical_device.h" VkBufferUsageFlags vkGryphnBufferType(gnBufferType type) { -switch (type) { -case GN_VERTEX_BUFFER: return VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; -case GN_INDEX_BUFFER: return VK_BUFFER_USAGE_INDEX_BUFFER_BIT; -} + VkBufferUsageFlags usageFlags = 0; + switch (type) { + case GN_VERTEX_BUFFER: usageFlags |= VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; break; + case GN_INDEX_BUFFER: usageFlags |= VK_BUFFER_USAGE_INDEX_BUFFER_BIT; break; + case GN_UNIFORM_BUFFER: usageFlags |= VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT; break; + } + return usageFlags; } gnReturnCode VkCreateBuffer( @@ -93,20 +96,29 @@ gnReturnCode gnCreateBufferFn(gnBufferHandle buffer, gnOutputDeviceHandle device VkBufferUsageFlags usage = vkGryphnBufferType(info.type); buffer->buffer->useStagingBuffer = gnFalse; if (info.usage == GN_STATIC_DRAW) { - gnReturnCode createdBuffer = VkCreateBuffer( + buffer->buffer->useStagingBuffer = gnTrue; + VkCreateBuffer( &buffer->buffer->stagingBuffer, &buffer->buffer->stagingBufferMemory, info, device->outputDevice->device, device->physicalDevice.physicalDevice->device, - VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, VK_BUFFER_USAGE_TRANSFER_SRC_BIT + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, + VK_BUFFER_USAGE_TRANSFER_SRC_BIT + ); + + return VkCreateBuffer( + &buffer->buffer->buffer, &buffer->buffer->bufferMemory, + info, device->outputDevice->device, device->physicalDevice.physicalDevice->device, + VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, + vkGryphnBufferType(info.type) | VK_BUFFER_USAGE_TRANSFER_DST_BIT + ); + } else { + return VkCreateBuffer( + &buffer->buffer->buffer, &buffer->buffer->bufferMemory, + info, device->outputDevice->device, device->physicalDevice.physicalDevice->device, + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, + vkGryphnBufferType(info.type) ); - usage |= VK_BUFFER_USAGE_TRANSFER_DST_BIT; - buffer->buffer->useStagingBuffer = gnTrue; } - gnReturnCode createdBuffer = VkCreateBuffer( - &buffer->buffer->buffer, &buffer->buffer->bufferMemory, - info, device->outputDevice->device, device->physicalDevice.physicalDevice->device, - VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, usage - ); return GN_SUCCESS; diff --git a/rendering_api/vulkan/src/commands/command_pool/vulkan_command_pool.c b/rendering_api/vulkan/src/commands/command_pool/vulkan_command_pool.c index c3d1d97..ba5aab1 100644 --- a/rendering_api/vulkan/src/commands/command_pool/vulkan_command_pool.c +++ b/rendering_api/vulkan/src/commands/command_pool/vulkan_command_pool.c @@ -7,7 +7,7 @@ gnReturnCode gnCreateCommandPoolFn(struct gnCommandPool_t* commandPool, struct g VkCommandPoolCreateInfo poolInfo = { .sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, .flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, - .queueFamilyIndex = info.queueIndex + .queueFamilyIndex = info.queueIndex, }; if (vkCreateCommandPool(device->outputDevice->device, &poolInfo, NULL, &commandPool->commandPool->commandPool) != VK_SUCCESS) { diff --git a/rendering_api/vulkan/src/pipelines/graphics_pipeline/vulkan_graphics_pipeline.c b/rendering_api/vulkan/src/pipelines/graphics_pipeline/vulkan_graphics_pipeline.c index ce11805..8b6dfb7 100644 --- a/rendering_api/vulkan/src/pipelines/graphics_pipeline/vulkan_graphics_pipeline.c +++ b/rendering_api/vulkan/src/pipelines/graphics_pipeline/vulkan_graphics_pipeline.c @@ -62,7 +62,7 @@ VkFormat vkGryphnVertexFormat(gnVertexFormat format) { VkDescriptorType vkGryphnUniformType(gnUniformType type) { switch(type) { - case GN_UNIFORM_BUFFER: return VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + case GN_UNIFORM_BUFFER_DESCRIPTOR: return VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; } } diff --git a/rendering_api/vulkan/src/uniforms/vulkan_uniform_pool.c b/rendering_api/vulkan/src/uniforms/vulkan_uniform_pool.c new file mode 100644 index 0000000..b2f4984 --- /dev/null +++ b/rendering_api/vulkan/src/uniforms/vulkan_uniform_pool.c @@ -0,0 +1,11 @@ +#include "vulkan_uniform_pool.h" +#include "stdlib.h" + +gnReturnCode gnCreateUniformPoolFn(gnUniformPool pool, gnDeviceHandle device) { + pool->uniformPool = malloc(sizeof(struct gnPlatformUniformPool_t)); + + pool->uniformPool->maxUniformPools = 0; + pool->uniformPool->uniformPoolCount = 0; + + return GN_SUCCESS; +} diff --git a/rendering_api/vulkan/src/uniforms/vulkan_uniform_pool.h b/rendering_api/vulkan/src/uniforms/vulkan_uniform_pool.h new file mode 100644 index 0000000..d21404b --- /dev/null +++ b/rendering_api/vulkan/src/uniforms/vulkan_uniform_pool.h @@ -0,0 +1,15 @@ +#pragma once +#include +#include +#include "core/uniforms/gryphn_uniform_layout.h" + +typedef struct VkGryphnUniformPool { + VkDescriptorPool pool; + gnUniformType type; +} VkGryphnUniformPool; + +struct gnPlatformUniformPool_t { + int maxUniformPools; + int uniformPoolCount; + VkGryphnUniformPool* pools; +}; diff --git a/src/core/buffers/gryphn_buffer.h b/src/core/buffers/gryphn_buffer.h index b1153c1..c854a84 100644 --- a/src/core/buffers/gryphn_buffer.h +++ b/src/core/buffers/gryphn_buffer.h @@ -8,8 +8,9 @@ typedef enum gnIndexType { } gnIndexType; typedef enum gnBufferType { - GN_VERTEX_BUFFER = 0x00000001f, - GN_INDEX_BUFFER = 0x00000002f, + GN_VERTEX_BUFFER = 0x00000001, + GN_INDEX_BUFFER = 0x00000002, + GN_UNIFORM_BUFFER = 0x00000004 } gnBufferType; typedef enum gnBufferUsage { diff --git a/src/core/gryphn_handles.h b/src/core/gryphn_handles.h index 78cc4e5..e77c9f8 100644 --- a/src/core/gryphn_handles.h +++ b/src/core/gryphn_handles.h @@ -24,3 +24,4 @@ GN_HANDLE(gnSemaphore); GN_HANDLE(gnFence); GN_HANDLE(gnFramebuffer); GN_HANDLE(gnBuffer); +GN_HANDLE(gnUniformPool); diff --git a/src/core/gryphn_platform_functions.h b/src/core/gryphn_platform_functions.h index b5b8029..a2f61cd 100644 --- a/src/core/gryphn_platform_functions.h +++ b/src/core/gryphn_platform_functions.h @@ -87,6 +87,8 @@ typedef struct gnDeviceFunctions_t { void* (*_gnMapBuffer)(gnBufferHandle buffer); void (*_gnDestroyBuffer)(gnBufferHandle buffer); + gnReturnCode (*_gnCreateUniformPool)(gnUniformPool pool, gnDeviceHandle device); + gnReturnCode (*_gnCreateFence)(gnFenceHandle fence, gnOutputDeviceHandle device); void (*_gnSignalFence)(gnFenceHandle fence); void (*_gnWaitForFence)(gnFenceHandle fence, uint64_t timeout); diff --git a/src/core/instance/init/gryphn_init.c b/src/core/instance/init/gryphn_init.c index 5585d3d..63ff6c9 100644 --- a/src/core/instance/init/gryphn_init.c +++ b/src/core/instance/init/gryphn_init.c @@ -88,6 +88,7 @@ void gnLoadDeviceFunctions(struct gnDynamicLibrary_t* lib, struct gnDeviceFuncti gnLoadDLLFunction(lib, functions->_gnBufferData, "gnBufferDataFn"); gnLoadDLLFunction(lib, functions->_gnMapBuffer, "gnMapBufferFn"); gnLoadDLLFunction(lib, functions->_gnDestroyBuffer, "gnDestroyBufferFn"); + gnLoadDLLFunction(lib, functions->_gnCreateUniformPool, "gnCreateUniformPoolFn"); gnLoadDLLFunction(lib, functions->_gnCreateFence, "gnCreateFenceFn"); gnLoadDLLFunction(lib, functions->_gnSignalFence, "gnSignalFenceFn"); gnLoadDLLFunction(lib, functions->_gnWaitForFence, "gnWaitForFenceFn"); diff --git a/src/core/pipelines/graphics_pipeline/gryphn_graphics_pipeline.h b/src/core/pipelines/graphics_pipeline/gryphn_graphics_pipeline.h index 688136a..e6c5c92 100644 --- a/src/core/pipelines/graphics_pipeline/gryphn_graphics_pipeline.h +++ b/src/core/pipelines/graphics_pipeline/gryphn_graphics_pipeline.h @@ -1,6 +1,6 @@ #pragma once #include -#include +#include #include #include #include "utils/math/gryphn_vec2.h" diff --git a/src/core/uniforms/gryphn_uniform.h b/src/core/uniforms/gryphn_uniform.h new file mode 100644 index 0000000..8379174 --- /dev/null +++ b/src/core/uniforms/gryphn_uniform.h @@ -0,0 +1,7 @@ +// #pragma once + +// #ifdef GN_REVEAL_IMPL +// struct gnUniform_t { + +// }; +// #endif diff --git a/src/core/pipelines/gryphn_uniform_layout.h b/src/core/uniforms/gryphn_uniform_layout.h similarity index 92% rename from src/core/pipelines/gryphn_uniform_layout.h rename to src/core/uniforms/gryphn_uniform_layout.h index 0214596..fdc08c0 100644 --- a/src/core/pipelines/gryphn_uniform_layout.h +++ b/src/core/uniforms/gryphn_uniform_layout.h @@ -3,7 +3,7 @@ #include "core/shader_module/gryphn_shader_module.h" typedef enum gnUniformType { - GN_UNIFORM_BUFFER + GN_UNIFORM_BUFFER_DESCRIPTOR } gnUniformType; typedef struct gnUniformBinding { diff --git a/src/core/uniforms/gryphn_uniform_pool.c b/src/core/uniforms/gryphn_uniform_pool.c new file mode 100644 index 0000000..5a41ed1 --- /dev/null +++ b/src/core/uniforms/gryphn_uniform_pool.c @@ -0,0 +1,10 @@ +#include "gryphn_uniform_pool.h" +#include "core/output_device/gryphn_output_device.h" +#include "core/gryphn_platform_functions.h" +#include "stdlib.h" + +gnReturnCode gnCreateUniformPool(gnUniformPool* pool, gnDeviceHandle device) { + *pool = malloc(sizeof(struct gnUniformPool_t)); + (*pool)->device = device; + return device->deviceFunctions->_gnCreateUniformPool(*pool, device); +} diff --git a/src/core/uniforms/gryphn_uniform_pool.h b/src/core/uniforms/gryphn_uniform_pool.h new file mode 100644 index 0000000..6ece5e3 --- /dev/null +++ b/src/core/uniforms/gryphn_uniform_pool.h @@ -0,0 +1,13 @@ +#pragma once +#include "core/gryphn_handles.h" +#include "utils/gryphn_error_code.h" + +#ifdef GN_REVEAL_IMPL +struct gnUniformPool_t { + struct gnPlatformUniformPool_t* uniformPool; + gnDeviceHandle device; +}; +#endif + +gnReturnCode gnCreateUniformPool(gnUniformPool* pool, gnDeviceHandle device); +// void gnUniformPoolAllocateUniforms