Uniform layouts

This commit is contained in:
Greg Wells
2025-06-07 14:46:35 -04:00
parent 77002cd26a
commit 813760678b
4 changed files with 54 additions and 23 deletions

View File

@@ -1,9 +1,7 @@
#include "vulkan_graphics_pipeline.h" #include "vulkan_graphics_pipeline.h"
#include "core/debugger/gryphn_debugger.h"
#include "output_device/vulkan_output_devices.h" #include "output_device/vulkan_output_devices.h"
#include "shader_module/vulkan_shader_module.h" #include "shader_module/vulkan_shader_module.h"
#include "renderpass/vulkan_render_pass_descriptor.h" #include "renderpass/vulkan_render_pass_descriptor.h"
#include "core/instance/gryphn_instance.h"
VkDynamicState vkGryphnDynamicStateToVulkanDynamicState(enum gnDynamicState_e state) { VkDynamicState vkGryphnDynamicStateToVulkanDynamicState(enum gnDynamicState_e state) {
switch (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) { gnReturnCode gnCreateGraphicsPipelineFn(struct gnGraphicsPipeline_t* graphicsPipeline, struct gnOutputDevice_t* device, struct gnGraphicsPipelineInfo_t info) {
graphicsPipeline->graphicsPipeline = malloc(sizeof(gnPlatformGraphicsPipeline)); graphicsPipeline->graphicsPipeline = malloc(sizeof(gnPlatformGraphicsPipeline));
@@ -180,25 +184,38 @@ gnReturnCode gnCreateGraphicsPipelineFn(struct gnGraphicsPipeline_t* graphicsPip
.blendConstants[3] = 0.0f .blendConstants[3] = 0.0f
}; };
if (info.uniformLayout == NULL) { graphicsPipeline->graphicsPipeline->setCount = info.uniformLayout.uniformBindingCount;
VkPipelineLayoutCreateInfo pipelineLayoutInfo = { graphicsPipeline->graphicsPipeline->sets = malloc(sizeof(VkDescriptorSetLayoutBinding) * info.uniformLayout.uniformBindingCount);
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO, for (int i = 0; i < info.uniformLayout.uniformBindingCount; i++) {
.setLayoutCount = 0, VkDescriptorSetLayoutBinding setLayout = {
pipelineLayoutInfo.pSetLayouts = NULL, .binding = info.uniformLayout.uniformBindings[i].binding,
pipelineLayoutInfo.pushConstantRangeCount = 0, .descriptorCount = 1,
pipelineLayoutInfo.pPushConstantRanges = NULL .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; return GN_FAILED_TO_CREATE_UNIFORM_LAYOUT;
} }
}
graphicsPipeline->graphicsPipeline->createdPipelineLayout = gnTrue;
} else { VkPipelineLayoutCreateInfo pipelineLayoutInfo = {
graphicsPipeline->graphicsPipeline->createdPipelineLayout = gnFalse; .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
gnDebuggerSetErrorMessage(device->instance->debugger, (gnMessageData){ .setLayoutCount = graphicsPipeline->graphicsPipeline->setCount,
.message = gnCreateString("Graphics pipelines can not currently accept uniform layouts") 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); VkPipelineShaderStageCreateInfo* modules = malloc(sizeof(VkPipelineShaderStageCreateInfo) * info.shaderModuleCount);
@@ -236,8 +253,9 @@ gnReturnCode gnCreateGraphicsPipelineFn(struct gnGraphicsPipeline_t* graphicsPip
} }
void gnDestroyGraphicsPipelineFn(struct gnGraphicsPipeline_t *graphicsPipeline) { void gnDestroyGraphicsPipelineFn(struct gnGraphicsPipeline_t *graphicsPipeline) {
if (graphicsPipeline->graphicsPipeline->createdPipelineLayout) for (int i = 0; i < graphicsPipeline->graphicsPipeline->setCount; i++)
vkDestroyPipelineLayout(graphicsPipeline->device->outputDevice->device, graphicsPipeline->graphicsPipeline->pipelineLayout, NULL); 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); vkDestroyPipeline(graphicsPipeline->device->outputDevice->device, graphicsPipeline->graphicsPipeline->graphicsPipeline, NULL);
free(graphicsPipeline->graphicsPipeline); free(graphicsPipeline->graphicsPipeline);

View File

@@ -15,7 +15,9 @@ typedef struct gnPlatformGraphicsPipeline_t {
VkPipelineColorBlendAttachmentState colorBlendAttachment; VkPipelineColorBlendAttachmentState colorBlendAttachment;
VkPipelineColorBlendStateCreateInfo colorBlending; VkPipelineColorBlendStateCreateInfo colorBlending;
gnBool createdPipelineLayout; // gnBool createdPipelineLayout;
uint32_t setCount;
VkDescriptorSetLayout* sets;
VkPipelineLayout pipelineLayout; VkPipelineLayout pipelineLayout;

View File

@@ -83,14 +83,13 @@ typedef struct gnGraphicsPipelineInfo_t {
struct gnCullMode_t cullMode; struct gnCullMode_t cullMode;
struct gnColorBlending_t colorBlending; struct gnColorBlending_t colorBlending;
struct gnUniformLayout_t* uniformLayout;
uint32_t subpassIndex; uint32_t subpassIndex;
gnRenderPassDescriptorHandle renderPassDescriptor; gnRenderPassDescriptorHandle renderPassDescriptor;
uint32_t shaderModuleCount; uint32_t shaderModuleCount;
gnShaderModuleHandle* shaderModules; gnShaderModuleHandle* shaderModules;
gnShaderInputLayout shaderInputLayout; gnShaderInputLayout shaderInputLayout;
gnUniformLayout uniformLayout;
} gnGraphicsPipelineInfo; } gnGraphicsPipelineInfo;
#ifdef GN_REVEAL_IMPL #ifdef GN_REVEAL_IMPL

View File

@@ -1,6 +1,18 @@
#pragma once #pragma once
#include "stdint.h" #include "stdint.h"
#include "core/shader_module/gryphn_shader_module.h"
typedef struct gnUniformLayout_t { typedef enum gnUniformType {
uint32_t pushConstantCount; GN_UNIFORM_BUFFER
} gnUniformType;
typedef struct gnUniformBinding {
uint32_t binding;
gnUniformType type;
gnShaderModuleStage stage;
} gnUniformBinding;
typedef struct gnUniformLayout {
uint32_t uniformBindingCount;
gnUniformBinding* uniformBindings;
} gnUniformLayout; } gnUniformLayout;