From 04808a14ddc20f965b3e5cfb6f1d1d0d9d8d6c99 Mon Sep 17 00:00:00 2001 From: Greg Wells Date: Sun, 8 Jun 2025 13:40:40 -0400 Subject: [PATCH] horrible uniform pool API --- .../vulkan_graphics_pipeline.c | 33 +++----------- .../vulkan/src/uniforms/vulkan_uniform.h | 6 +++ .../src/uniforms/vulkan_uniform_layout.c | 38 ++++++++++++++++ .../src/uniforms/vulkan_uniform_layout.h | 5 +++ .../vulkan/src/uniforms/vulkan_uniform_pool.c | 43 +++++++++++++++++-- .../vulkan/src/uniforms/vulkan_uniform_pool.h | 10 +---- src/core/gryphn_handles.h | 1 + src/core/gryphn_platform_functions.h | 1 + src/core/instance/init/gryphn_init.c | 1 + src/core/uniforms/gryphn_uniform.h | 12 +++--- src/core/uniforms/gryphn_uniform_layout.h | 3 +- src/core/uniforms/gryphn_uniform_pool.c | 4 ++ src/core/uniforms/gryphn_uniform_pool.h | 3 +- 13 files changed, 112 insertions(+), 48 deletions(-) create mode 100644 rendering_api/vulkan/src/uniforms/vulkan_uniform.h create mode 100644 rendering_api/vulkan/src/uniforms/vulkan_uniform_layout.c create mode 100644 rendering_api/vulkan/src/uniforms/vulkan_uniform_layout.h 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 8b6dfb7..03d624c 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 @@ -2,6 +2,7 @@ #include "output_device/vulkan_output_devices.h" #include "shader_module/vulkan_shader_module.h" #include "renderpass/vulkan_render_pass_descriptor.h" +#include "uniforms/vulkan_uniform_layout.h" VkDynamicState vkGryphnDynamicStateToVulkanDynamicState(enum gnDynamicState_e state) { switch (state) { @@ -60,13 +61,7 @@ VkFormat vkGryphnVertexFormat(gnVertexFormat format) { } } -VkDescriptorType vkGryphnUniformType(gnUniformType type) { - switch(type) { - case GN_UNIFORM_BUFFER_DESCRIPTOR: return VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - } -} - -gnReturnCode gnCreateGraphicsPipelineFn(struct gnGraphicsPipeline_t* graphicsPipeline, struct gnOutputDevice_t* device, struct gnGraphicsPipelineInfo_t info) { +gnReturnCode gnCreateGraphicsPipelineFn(gnGraphicsPipeline graphicsPipeline, gnDevice device, gnGraphicsPipelineInfo info) { graphicsPipeline->graphicsPipeline = malloc(sizeof(gnPlatformGraphicsPipeline)); for (int i = 0; i < GN_DYNAMIC_STATE_MAX; i++) graphicsPipeline->graphicsPipeline->isDynamic[i] = gnFalse; @@ -184,27 +179,9 @@ gnReturnCode gnCreateGraphicsPipelineFn(struct gnGraphicsPipeline_t* graphicsPip .blendConstants[3] = 0.0f }; - graphicsPipeline->graphicsPipeline->setCount = info.uniformLayout.uniformBindingCount; - graphicsPipeline->graphicsPipeline->sets = malloc(sizeof(VkDescriptorSetLayoutBinding) * info.uniformLayout.uniformBindingCount); - for (int i = 0; i < info.uniformLayout.uniformBindingCount; i++) { - VkDescriptorSetLayoutBinding setLayout = { - .binding = info.uniformLayout.uniformBindings[i].binding, - .descriptorCount = 1, - .descriptorType = vkGryphnUniformType(info.uniformLayout.uniformBindings[i].type), - .stageFlags = vkGryphnShaderModuleStage(info.uniformLayout.uniformBindings[i].stage) - }; - - VkDescriptorSetLayoutCreateInfo info = { - .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, - .bindingCount = 1, - .pBindings = &setLayout - }; - - if (vkCreateDescriptorSetLayout(device->outputDevice->device, &info, NULL, &graphicsPipeline->graphicsPipeline->sets[i]) != VK_SUCCESS) { - return GN_FAILED_TO_CREATE_UNIFORM_LAYOUT; - } - } - + graphicsPipeline->graphicsPipeline->sets = vkGryphnCreateSetLayouts( + &info.uniformLayout, &graphicsPipeline->graphicsPipeline->setCount, device->outputDevice->device + ); VkPipelineLayoutCreateInfo pipelineLayoutInfo = { .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, diff --git a/rendering_api/vulkan/src/uniforms/vulkan_uniform.h b/rendering_api/vulkan/src/uniforms/vulkan_uniform.h new file mode 100644 index 0000000..109c64c --- /dev/null +++ b/rendering_api/vulkan/src/uniforms/vulkan_uniform.h @@ -0,0 +1,6 @@ +#pragma once +#include + +typedef struct gnPlatformUniform_t { + VkDescriptorSet set; +} gnPlatformUniform; diff --git a/rendering_api/vulkan/src/uniforms/vulkan_uniform_layout.c b/rendering_api/vulkan/src/uniforms/vulkan_uniform_layout.c new file mode 100644 index 0000000..6ed8131 --- /dev/null +++ b/rendering_api/vulkan/src/uniforms/vulkan_uniform_layout.c @@ -0,0 +1,38 @@ +#include "vulkan_uniform_layout.h" +#include + +VkDescriptorType vkGryphnUniformType(gnUniformType type) { + switch(type) { + case GN_UNIFORM_BUFFER_DESCRIPTOR: return VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + case GN_UNIFORM_TYPE_MAX: return VK_DESCRIPTOR_TYPE_MAX_ENUM; + } +} + +VkDescriptorSetLayout* vkGryphnCreateSetLayouts( + const gnUniformLayout* layout, uint32_t* setCount, + VkDevice device +) { + gnUniformLayout uniformLayout = *layout; + + *setCount = uniformLayout.uniformBindingCount; + VkDescriptorSetLayout* sets = malloc(sizeof(VkDescriptorSetLayoutBinding) * uniformLayout.uniformBindingCount); + for (int i = 0; i < uniformLayout.uniformBindingCount; i++) { + VkDescriptorSetLayoutBinding setLayout = { + .binding = uniformLayout.uniformBindings[i].binding, + .descriptorCount = 1, + .descriptorType = vkGryphnUniformType(uniformLayout.uniformBindings[i].type), + .stageFlags = vkGryphnShaderModuleStage(uniformLayout.uniformBindings[i].stage) + }; + + VkDescriptorSetLayoutCreateInfo info = { + .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO, + .bindingCount = 1, + .pBindings = &setLayout + }; + + if (vkCreateDescriptorSetLayout(device, &info, NULL, sets) != VK_SUCCESS) { + return NULL; + } + } + return sets; +} diff --git a/rendering_api/vulkan/src/uniforms/vulkan_uniform_layout.h b/rendering_api/vulkan/src/uniforms/vulkan_uniform_layout.h new file mode 100644 index 0000000..086d65b --- /dev/null +++ b/rendering_api/vulkan/src/uniforms/vulkan_uniform_layout.h @@ -0,0 +1,5 @@ +#pragma once +#include +#include + +VkDescriptorSetLayout* vkGryphnCreateSetLayouts(const gnUniformLayout* layout, uint32_t* setCount, VkDevice device); diff --git a/rendering_api/vulkan/src/uniforms/vulkan_uniform_pool.c b/rendering_api/vulkan/src/uniforms/vulkan_uniform_pool.c index b2f4984..ef3436b 100644 --- a/rendering_api/vulkan/src/uniforms/vulkan_uniform_pool.c +++ b/rendering_api/vulkan/src/uniforms/vulkan_uniform_pool.c @@ -1,11 +1,46 @@ #include "vulkan_uniform_pool.h" +#include "vulkan_uniform_layout.h" #include "stdlib.h" +#include "output_device/vulkan_output_devices.h" gnReturnCode gnCreateUniformPoolFn(gnUniformPool pool, gnDeviceHandle device) { pool->uniformPool = malloc(sizeof(struct gnPlatformUniformPool_t)); - - pool->uniformPool->maxUniformPools = 0; - pool->uniformPool->uniformPoolCount = 0; - + pool->uniformPool->poolCount = pool->uniformPool->maxPoolCount = 1; + pool->uniformPool->pools = malloc(sizeof(VkDescriptorPool) * pool->uniformPool->maxPoolCount); return GN_SUCCESS; } + +gnUniform* gnUniformPoolAllocateUniformsFn(gnUniformPool pool, const gnUniformLayout layout) { + uint32_t setCount; + VkDescriptorSetLayout* layouts = vkGryphnCreateSetLayouts(&layout, &setCount, pool->device->outputDevice->device); + VkDescriptorPoolSize* sizes = malloc(sizeof(VkDescriptorPoolSize) * layout.uniformBindingCount); + + gnUniform* uniforms = malloc(sizeof(gnUniform) * setCount); + for (int i = 0; i < layout.uniformBindingCount; i++) { + + } + + VkDescriptorPoolCreateInfo poolInfo = { + .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO, + .poolSizeCount = layout.uniformBindingCount, + .maxSets = layout.uniformBindingCount, + .pPoolSizes = sizes, + }; + + if (pool->uniformPool->poolCount >= pool->uniformPool->maxPoolCount) { + pool->uniformPool->maxPoolCount *= 2; + pool->uniformPool->pools = realloc(pool->uniformPool->pools, sizeof(VkDescriptorPool) * pool->uniformPool->maxPoolCount); + } + + if (vkCreateDescriptorPool( + pool->device->outputDevice->device, &poolInfo, NULL, + &pool->uniformPool->pools[pool->uniformPool->poolCount] + ) != VK_SUCCESS) + return NULL; + pool->uniformPool->poolCount++; + + + free(layouts); + free(sizes); + return uniforms; +} diff --git a/rendering_api/vulkan/src/uniforms/vulkan_uniform_pool.h b/rendering_api/vulkan/src/uniforms/vulkan_uniform_pool.h index d21404b..547f0e6 100644 --- a/rendering_api/vulkan/src/uniforms/vulkan_uniform_pool.h +++ b/rendering_api/vulkan/src/uniforms/vulkan_uniform_pool.h @@ -3,13 +3,7 @@ #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; + uint32_t poolCount, maxPoolCount; + VkDescriptorPool* pools; }; diff --git a/src/core/gryphn_handles.h b/src/core/gryphn_handles.h index e77c9f8..09099f3 100644 --- a/src/core/gryphn_handles.h +++ b/src/core/gryphn_handles.h @@ -25,3 +25,4 @@ GN_HANDLE(gnFence); GN_HANDLE(gnFramebuffer); GN_HANDLE(gnBuffer); GN_HANDLE(gnUniformPool); +GN_HANDLE(gnUniform); diff --git a/src/core/gryphn_platform_functions.h b/src/core/gryphn_platform_functions.h index a2f61cd..3a291a5 100644 --- a/src/core/gryphn_platform_functions.h +++ b/src/core/gryphn_platform_functions.h @@ -88,6 +88,7 @@ typedef struct gnDeviceFunctions_t { void (*_gnDestroyBuffer)(gnBufferHandle buffer); gnReturnCode (*_gnCreateUniformPool)(gnUniformPool pool, gnDeviceHandle device); + gnUniform* (*_gnUniformPoolAllocateUniforms)(gnUniformPool pool, const gnUniformLayout layout); gnReturnCode (*_gnCreateFence)(gnFenceHandle fence, gnOutputDeviceHandle device); void (*_gnSignalFence)(gnFenceHandle fence); diff --git a/src/core/instance/init/gryphn_init.c b/src/core/instance/init/gryphn_init.c index 91d0d03..60803fa 100644 --- a/src/core/instance/init/gryphn_init.c +++ b/src/core/instance/init/gryphn_init.c @@ -90,6 +90,7 @@ void gnLoadDeviceFunctions(struct gnDynamicLibrary_t* lib, struct gnDeviceFuncti gnLoadDLLFunction(lib, functions->_gnMapBuffer, "gnMapBufferFn"); gnLoadDLLFunction(lib, functions->_gnDestroyBuffer, "gnDestroyBufferFn"); gnLoadDLLFunction(lib, functions->_gnCreateUniformPool, "gnCreateUniformPoolFn"); + gnLoadDLLFunction(lib, functions->_gnUniformPoolAllocateUniforms, "gnUniformPoolAllocateUniformsFn"); gnLoadDLLFunction(lib, functions->_gnCreateFence, "gnCreateFenceFn"); gnLoadDLLFunction(lib, functions->_gnSignalFence, "gnSignalFenceFn"); gnLoadDLLFunction(lib, functions->_gnWaitForFence, "gnWaitForFenceFn"); diff --git a/src/core/uniforms/gryphn_uniform.h b/src/core/uniforms/gryphn_uniform.h index 8379174..a710f8c 100644 --- a/src/core/uniforms/gryphn_uniform.h +++ b/src/core/uniforms/gryphn_uniform.h @@ -1,7 +1,7 @@ -// #pragma once +#pragma once -// #ifdef GN_REVEAL_IMPL -// struct gnUniform_t { - -// }; -// #endif +#ifdef GN_REVEAL_IMPL +struct gnUniform_t { + struct gnPlatformUniform_t* uniform; +}; +#endif diff --git a/src/core/uniforms/gryphn_uniform_layout.h b/src/core/uniforms/gryphn_uniform_layout.h index fdc08c0..327c11a 100644 --- a/src/core/uniforms/gryphn_uniform_layout.h +++ b/src/core/uniforms/gryphn_uniform_layout.h @@ -3,7 +3,8 @@ #include "core/shader_module/gryphn_shader_module.h" typedef enum gnUniformType { - GN_UNIFORM_BUFFER_DESCRIPTOR + GN_UNIFORM_BUFFER_DESCRIPTOR, + GN_UNIFORM_TYPE_MAX } gnUniformType; typedef struct gnUniformBinding { diff --git a/src/core/uniforms/gryphn_uniform_pool.c b/src/core/uniforms/gryphn_uniform_pool.c index 5a41ed1..b5c5f5d 100644 --- a/src/core/uniforms/gryphn_uniform_pool.c +++ b/src/core/uniforms/gryphn_uniform_pool.c @@ -8,3 +8,7 @@ gnReturnCode gnCreateUniformPool(gnUniformPool* pool, gnDeviceHandle device) { (*pool)->device = device; return device->deviceFunctions->_gnCreateUniformPool(*pool, device); } + +gnUniform* gnUniformPoolAllocateUniforms(gnUniformPool pool, const gnUniformLayout layout) { + return pool->device->deviceFunctions->_gnUniformPoolAllocateUniforms(pool, layout); +} diff --git a/src/core/uniforms/gryphn_uniform_pool.h b/src/core/uniforms/gryphn_uniform_pool.h index 6ece5e3..6b47ae4 100644 --- a/src/core/uniforms/gryphn_uniform_pool.h +++ b/src/core/uniforms/gryphn_uniform_pool.h @@ -1,6 +1,7 @@ #pragma once #include "core/gryphn_handles.h" #include "utils/gryphn_error_code.h" +#include "core/uniforms/gryphn_uniform_layout.h" #ifdef GN_REVEAL_IMPL struct gnUniformPool_t { @@ -10,4 +11,4 @@ struct gnUniformPool_t { #endif gnReturnCode gnCreateUniformPool(gnUniformPool* pool, gnDeviceHandle device); -// void gnUniformPoolAllocateUniforms +gnUniform* gnUniformPoolAllocateUniforms(gnUniformPool pool, const gnUniformLayout layout);