From 5158bef0ddeb255ed9343e0c5eb2feda9bf8af59 Mon Sep 17 00:00:00 2001 From: Greg Wells Date: Thu, 29 May 2025 12:26:02 -0400 Subject: [PATCH] vulkan framebuffers --- .../src/framebuffers/vulkan_framebuffer.c | 34 +++++++++++++++++++ .../src/framebuffers/vulkan_framebuffer.h | 7 ++++ src/core/framebuffer/gryphn_framebuffer.c | 4 ++- src/core/gryphn_platform_functions.h | 6 +++- src/core/instance/init/gryphn_init.c | 2 ++ src/utils/gryphn_error_code.h | 4 ++- 6 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 rendering_api/vulkan/src/framebuffers/vulkan_framebuffer.c create mode 100644 rendering_api/vulkan/src/framebuffers/vulkan_framebuffer.h diff --git a/rendering_api/vulkan/src/framebuffers/vulkan_framebuffer.c b/rendering_api/vulkan/src/framebuffers/vulkan_framebuffer.c new file mode 100644 index 0000000..d735975 --- /dev/null +++ b/rendering_api/vulkan/src/framebuffers/vulkan_framebuffer.c @@ -0,0 +1,34 @@ +#include "vulkan_framebuffer.h" +#include "textures/vulkan_texture.h" +#include "renderpass/vulkan_render_pass_descriptor.h" +#include "output_device/vulkan_output_devices.h" + +gnReturnCode gnCreateFramebufferFn(struct gnFramebuffer_t* framebuffer, struct gnOutputDevice_t* device, struct gnFramebufferInfo_t info) { + framebuffer->framebuffer = malloc(sizeof(struct gnPlatformFramebuffer_t)); + + VkImageView* attachments = malloc(sizeof(VkImageView) * info.attachmentCount); + for (int i = 0; i < info.attachmentCount; i++) + attachments[i] = info.attachments[i].texture->imageView; + + VkFramebufferCreateInfo framebufferInfo = { + .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, + .renderPass = info.renderPassDescriptor->renderPassDescriptor->renderPass, + .attachmentCount = info.attachmentCount, + .pAttachments = attachments, + .width = info.size.x, + .height = info.size.y, + .layers = 1 + }; + + if (vkCreateFramebuffer(device->outputDevice->device, &framebufferInfo, NULL, &framebuffer->framebuffer->framebuffer) != VK_SUCCESS) { + return GN_FAILED_TO_CREATE_FRAMEBUFFER; + } + + free(attachments); + return GN_SUCCESS; +} + +void gnDestroyFramebufferFn(struct gnFramebuffer_t* framebuffer) { + vkDestroyFramebuffer(framebuffer->device->outputDevice->device, framebuffer->framebuffer->framebuffer, NULL); + free(framebuffer->framebuffer); +} diff --git a/rendering_api/vulkan/src/framebuffers/vulkan_framebuffer.h b/rendering_api/vulkan/src/framebuffers/vulkan_framebuffer.h new file mode 100644 index 0000000..70a5d28 --- /dev/null +++ b/rendering_api/vulkan/src/framebuffers/vulkan_framebuffer.h @@ -0,0 +1,7 @@ +#pragma once +#include "core/framebuffer/gryphn_framebuffer.h" +#include + +typedef struct gnPlatformFramebuffer_t { + VkFramebuffer framebuffer; +} gnPlatformFramebuffer; diff --git a/src/core/framebuffer/gryphn_framebuffer.c b/src/core/framebuffer/gryphn_framebuffer.c index 27ddec4..466ff1d 100644 --- a/src/core/framebuffer/gryphn_framebuffer.c +++ b/src/core/framebuffer/gryphn_framebuffer.c @@ -1,9 +1,11 @@ #include "gryphn_framebuffer.h" +#include "core/gryphn_platform_functions.h" gnReturnCode gnCreateFramebuffer(struct gnFramebuffer_t* framebuffer, struct gnOutputDevice_t* device, struct gnFramebufferInfo_t framebufferInfo) { framebuffer->device = device; + return device->deviceFunctions->_gnCreateFramebuffer(framebuffer, device, framebufferInfo); } void gnDestroyFramebuffer(struct gnFramebuffer_t *framebuffer) { - + framebuffer->device->deviceFunctions->_gnDestroyFramebuffer(framebuffer); } diff --git a/src/core/gryphn_platform_functions.h b/src/core/gryphn_platform_functions.h index 25ec9e8..538b35a 100644 --- a/src/core/gryphn_platform_functions.h +++ b/src/core/gryphn_platform_functions.h @@ -8,8 +8,9 @@ #include "window_surface/gryphn_surface.h" #include "window_surface/gryphn_surface_create_functions.h" #include "shader_module/gryphn_shader_module.h" -#include "pipelines/graphics_pipeline/gryphn_graphics_pipeline.h" #include "renderpass/gryphn_render_pass_descriptor.h" +#include "pipelines/graphics_pipeline/gryphn_graphics_pipeline.h" +#include "framebuffer/gryphn_framebuffer.h" typedef struct gnFunctions_t { gnReturnCode (*_gnCreateInstance)(gnInstance* instance, struct gnInstanceInfo_t info); @@ -62,4 +63,7 @@ typedef struct gnDeviceFunctions_t { gnReturnCode (*_gnCreateGraphicsPipeline)(struct gnGraphicsPipeline_t* pipeline, struct gnOutputDevice_t* device, struct gnGraphicsPipelineInfo_t pipelineInfo); void (*_gnDestroyGraphicsPipeline)(struct gnGraphicsPipeline_t* pipeline); + + gnReturnCode (*_gnCreateFramebuffer)(struct gnFramebuffer_t* framebuffer, struct gnOutputDevice_t* device, struct gnFramebufferInfo_t framebufferInfo); + void (*_gnDestroyFramebuffer)(struct gnFramebuffer_t* framebuffer); } gnDeviceFunctions; diff --git a/src/core/instance/init/gryphn_init.c b/src/core/instance/init/gryphn_init.c index ec56b57..ed4e9a5 100644 --- a/src/core/instance/init/gryphn_init.c +++ b/src/core/instance/init/gryphn_init.c @@ -77,4 +77,6 @@ void gnLoadDeviceFunctions(struct gnDynamicLibrary_t* lib, struct gnDeviceFuncti gnLoadDLLFunction(lib, functions->_gnDestroyRenderPassDescriptor, "gnDestroyRenderPassDescriptorFn"); gnLoadDLLFunction(lib, functions->_gnCreateGraphicsPipeline, "gnCreateGraphicsPipelineFn"); gnLoadDLLFunction(lib, functions->_gnDestroyGraphicsPipeline, "gnDestroyGraphicsPipelineFn"); + gnLoadDLLFunction(lib, functions->_gnCreateFramebuffer, "gnCreateFramebufferFn"); + gnLoadDLLFunction(lib, functions->_gnDestroyFramebuffer, "gnDestroyFramebufferFn"); } diff --git a/src/utils/gryphn_error_code.h b/src/utils/gryphn_error_code.h index df07224..aed17a4 100644 --- a/src/utils/gryphn_error_code.h +++ b/src/utils/gryphn_error_code.h @@ -24,7 +24,8 @@ typedef enum gnReturnCode_t { GN_FAILED_TO_CREATE_RENDER_PASS, GN_FAILED_TO_CREATE_GRAPHICS_PIPELINE, GN_UNSUPPORTED_SHADER_MODULE, - GN_UNKNOWN_SUBPASS + GN_UNKNOWN_SUBPASS, + GN_FAILED_TO_CREATE_FRAMEBUFFER } gnReturnCode; typedef gnReturnCode gnErrorCode; @@ -54,5 +55,6 @@ static const char* gnErrorCodeToCString(enum gnReturnCode_t returnCode) { case GN_FAILED_TO_CREATE_GRAPHICS_PIPELINE: return "GN_FAILED_TO_CREATE_GRAPHICS_PIPELINE"; case GN_UNSUPPORTED_SHADER_MODULE: return "GN_UNSUPPORTED_SHADER_MODULE"; case GN_UNKNOWN_SUBPASS: return "GN_UNKNOWN_SUBPASS"; + case GN_FAILED_TO_CREATE_FRAMEBUFFER: return "GN_FAILED_TO_CREATE_FRAMEBUFFER"; } }