From 813760678ba082f8519342fee98d5efc8f3c5251 Mon Sep 17 00:00:00 2001 From: Greg Wells Date: Sat, 7 Jun 2025 14:46:35 -0400 Subject: [PATCH] Uniform layouts --- .../vulkan_graphics_pipeline.c | 54 ++++++++++++------- .../vulkan_graphics_pipeline.h | 4 +- .../gryphn_graphics_pipeline.h | 3 +- src/core/pipelines/gryphn_uniform_layout.h | 16 +++++- 4 files changed, 54 insertions(+), 23 deletions(-) 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 570a094..ce11805 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 @@ -1,9 +1,7 @@ #include "vulkan_graphics_pipeline.h" -#include "core/debugger/gryphn_debugger.h" #include "output_device/vulkan_output_devices.h" #include "shader_module/vulkan_shader_module.h" #include "renderpass/vulkan_render_pass_descriptor.h" -#include "core/instance/gryphn_instance.h" VkDynamicState vkGryphnDynamicStateToVulkanDynamicState(enum gnDynamicState_e state) { switch (state) { @@ -62,6 +60,12 @@ VkFormat vkGryphnVertexFormat(gnVertexFormat format) { } } +VkDescriptorType vkGryphnUniformType(gnUniformType type) { + switch(type) { + case GN_UNIFORM_BUFFER: return VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + } +} + gnReturnCode gnCreateGraphicsPipelineFn(struct gnGraphicsPipeline_t* graphicsPipeline, struct gnOutputDevice_t* device, struct gnGraphicsPipelineInfo_t info) { graphicsPipeline->graphicsPipeline = malloc(sizeof(gnPlatformGraphicsPipeline)); @@ -180,25 +184,38 @@ gnReturnCode gnCreateGraphicsPipelineFn(struct gnGraphicsPipeline_t* graphicsPip .blendConstants[3] = 0.0f }; - if (info.uniformLayout == NULL) { - VkPipelineLayoutCreateInfo pipelineLayoutInfo = { - .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, - .setLayoutCount = 0, - pipelineLayoutInfo.pSetLayouts = NULL, - pipelineLayoutInfo.pushConstantRangeCount = 0, - pipelineLayoutInfo.pPushConstantRanges = NULL + 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) }; - if (vkCreatePipelineLayout(device->outputDevice->device, &pipelineLayoutInfo, NULL, &graphicsPipeline->graphicsPipeline->pipelineLayout) != VK_SUCCESS) { + 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->createdPipelineLayout = gnTrue; - } else { - graphicsPipeline->graphicsPipeline->createdPipelineLayout = gnFalse; - gnDebuggerSetErrorMessage(device->instance->debugger, (gnMessageData){ - .message = gnCreateString("Graphics pipelines can not currently accept uniform layouts") - }); + + VkPipelineLayoutCreateInfo pipelineLayoutInfo = { + .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, + .setLayoutCount = graphicsPipeline->graphicsPipeline->setCount, + pipelineLayoutInfo.pSetLayouts = graphicsPipeline->graphicsPipeline->sets, + pipelineLayoutInfo.pushConstantRangeCount = 0, + pipelineLayoutInfo.pPushConstantRanges = NULL + }; + + if (vkCreatePipelineLayout(device->outputDevice->device, &pipelineLayoutInfo, NULL, &graphicsPipeline->graphicsPipeline->pipelineLayout) != VK_SUCCESS) { + return GN_FAILED_TO_CREATE_UNIFORM_LAYOUT; } VkPipelineShaderStageCreateInfo* modules = malloc(sizeof(VkPipelineShaderStageCreateInfo) * info.shaderModuleCount); @@ -236,8 +253,9 @@ gnReturnCode gnCreateGraphicsPipelineFn(struct gnGraphicsPipeline_t* graphicsPip } void gnDestroyGraphicsPipelineFn(struct gnGraphicsPipeline_t *graphicsPipeline) { - if (graphicsPipeline->graphicsPipeline->createdPipelineLayout) - vkDestroyPipelineLayout(graphicsPipeline->device->outputDevice->device, graphicsPipeline->graphicsPipeline->pipelineLayout, NULL); + for (int i = 0; i < graphicsPipeline->graphicsPipeline->setCount; i++) + vkDestroyDescriptorSetLayout(graphicsPipeline->device->outputDevice->device, graphicsPipeline->graphicsPipeline->sets[i], NULL); + vkDestroyPipelineLayout(graphicsPipeline->device->outputDevice->device, graphicsPipeline->graphicsPipeline->pipelineLayout, NULL); vkDestroyPipeline(graphicsPipeline->device->outputDevice->device, graphicsPipeline->graphicsPipeline->graphicsPipeline, NULL); free(graphicsPipeline->graphicsPipeline); diff --git a/rendering_api/vulkan/src/pipelines/graphics_pipeline/vulkan_graphics_pipeline.h b/rendering_api/vulkan/src/pipelines/graphics_pipeline/vulkan_graphics_pipeline.h index 3671f41..dee5fa5 100644 --- a/rendering_api/vulkan/src/pipelines/graphics_pipeline/vulkan_graphics_pipeline.h +++ b/rendering_api/vulkan/src/pipelines/graphics_pipeline/vulkan_graphics_pipeline.h @@ -15,7 +15,9 @@ typedef struct gnPlatformGraphicsPipeline_t { VkPipelineColorBlendAttachmentState colorBlendAttachment; VkPipelineColorBlendStateCreateInfo colorBlending; - gnBool createdPipelineLayout; + // gnBool createdPipelineLayout; + uint32_t setCount; + VkDescriptorSetLayout* sets; VkPipelineLayout pipelineLayout; diff --git a/src/core/pipelines/graphics_pipeline/gryphn_graphics_pipeline.h b/src/core/pipelines/graphics_pipeline/gryphn_graphics_pipeline.h index 2bfcffa..688136a 100644 --- a/src/core/pipelines/graphics_pipeline/gryphn_graphics_pipeline.h +++ b/src/core/pipelines/graphics_pipeline/gryphn_graphics_pipeline.h @@ -83,14 +83,13 @@ typedef struct gnGraphicsPipelineInfo_t { struct gnCullMode_t cullMode; struct gnColorBlending_t colorBlending; - struct gnUniformLayout_t* uniformLayout; - uint32_t subpassIndex; gnRenderPassDescriptorHandle renderPassDescriptor; uint32_t shaderModuleCount; gnShaderModuleHandle* shaderModules; gnShaderInputLayout shaderInputLayout; + gnUniformLayout uniformLayout; } gnGraphicsPipelineInfo; #ifdef GN_REVEAL_IMPL diff --git a/src/core/pipelines/gryphn_uniform_layout.h b/src/core/pipelines/gryphn_uniform_layout.h index 60b3dc9..0214596 100644 --- a/src/core/pipelines/gryphn_uniform_layout.h +++ b/src/core/pipelines/gryphn_uniform_layout.h @@ -1,6 +1,18 @@ #pragma once #include "stdint.h" +#include "core/shader_module/gryphn_shader_module.h" -typedef struct gnUniformLayout_t { - uint32_t pushConstantCount; +typedef enum gnUniformType { + GN_UNIFORM_BUFFER +} gnUniformType; + +typedef struct gnUniformBinding { + uint32_t binding; + gnUniformType type; + gnShaderModuleStage stage; +} gnUniformBinding; + +typedef struct gnUniformLayout { + uint32_t uniformBindingCount; + gnUniformBinding* uniformBindings; } gnUniformLayout;