From 5b6360f26f13bb92bf9d6a6aa075abff3e235522 Mon Sep 17 00:00:00 2001 From: Greg Wells Date: Sun, 25 May 2025 07:05:23 -0400 Subject: [PATCH] creating the presentation queue --- .../vulkan_presentation_queue.c | 150 ++++-------------- src/core/gryphn_platform_functions.h | 1 + src/core/instance/init/gryphn_init.c | 1 + src/core/output_device/gryphn_output_device.c | 1 + src/core/output_device/gryphn_output_device.h | 3 +- .../gryphn_presentation_queue.c | 10 +- .../gryphn_presentation_queue.h | 23 +-- src/utils/gryphn_error_code.h | 4 +- src/utils/types/gryphn_image_format.h | 4 + 9 files changed, 56 insertions(+), 141 deletions(-) diff --git a/rendering_api/vulkan/src/presentation_queue/vulkan_presentation_queue.c b/rendering_api/vulkan/src/presentation_queue/vulkan_presentation_queue.c index 5f04620..46a1871 100644 --- a/rendering_api/vulkan/src/presentation_queue/vulkan_presentation_queue.c +++ b/rendering_api/vulkan/src/presentation_queue/vulkan_presentation_queue.c @@ -5,6 +5,8 @@ #include "core/debugger/gryphn_debugger.h" gnReturnCode gnCreatePresentationQueueFn(gnPresentationQueue* presentationQueue, const gnOutputDevice* device, struct gnPresentationQueueInfo_t presentationInfo) { + presentationQueue->presentationQueue = malloc(sizeof(struct gnPlatformPresentationQueue_t)); + vkSwapchainSupportDetails details = vkGetSwapchainSupport(device->physicalDevice.physicalDevice->device, presentationInfo.surface.windowSurface->surface); if (details.formatCount == 0) { gnDebuggerSetErrorMessage(device->instance->debugger, @@ -45,124 +47,36 @@ gnReturnCode gnCreatePresentationQueueFn(gnPresentationQueue* presentationQueue, .height = presentationInfo.ImageSize.y }; + VkSwapchainCreateInfoKHR createInfo = {}; + createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; + createInfo.surface = presentationInfo.surface.windowSurface->surface; + + createInfo.minImageCount = presentationInfo.ImageCount; + createInfo.imageFormat = details.formats[index].format; + createInfo.imageColorSpace = details.formats[index].colorSpace; + createInfo.imageExtent = extent; + createInfo.imageArrayLayers = 1; + createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; + + if (presentationInfo.imageSharingMode == GN_SHARING_MODE_EXCLUSIVE) + createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; + else + createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT; + createInfo.queueFamilyIndexCount = presentationInfo.queueFamilyCount; + createInfo.pQueueFamilyIndices = presentationInfo.queueFamilies; + createInfo.preTransform = details.capabilities.currentTransform; + createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; + createInfo.presentMode = presentMode; + createInfo.clipped = VK_TRUE; + createInfo.oldSwapchain = VK_NULL_HANDLE; + + if (vkCreateSwapchainKHR(device->outputDevice->device, &createInfo, NULL, &presentationQueue->presentationQueue->swapChain) != VK_SUCCESS) { + return GN_FAILED_TO_CREATE_PRESENTATION_QUEUE; + } + return GN_SUCCESS; } -// #include "core/presentation_queue/gryphn_presentation_queue.h" -// #include "vulkan_queue_families.h" -// #include "vulkan_swapchain_support.h" -// #include "vulkan_presentation_queue.h" -// #include "../sync_objects/vulkan_sync_semaphore.h" -// #include "../textures/vulkan_texture.h" -// #include "../instance/vulkan_instance.h" - -// GN_EXPORT gnImageFormat gnPresentationQueueGetImageFormatFn(gnPresentationQueue& presentationQueue) { -// return (gnImageFormat)presentationQueue.presentationQueue->swapchainDetails.surfaceFormat.format; -// } - -// GN_EXPORT gnReturnCode gnCreatePresentationQueueFn(gnPresentationQueue* presentationQueue, const gnOutputDevice& device, gnPresentationDetails& details) { -// presentationQueue->presentationQueue = new gnPlatformPresentationQueue(); -// presentationQueue->presentationQueue->outputDevice = const_cast(&device); - -// vulkanSwapchainDetails swapchain_details = vulkanGetSwapchainDetails( -// { (float)details.ImageSize.x, (float)details.ImageSize.y }, -// device.physicalOutputDevice->physicalOutputDevice->instance->instance->window_surface, -// device.physicalOutputDevice->physicalOutputDevice->device -// ); -// presentationQueue->presentationQueue->swapchainDetails = swapchain_details; - -// VkSwapchainCreateInfoKHR createInfo{}; -// createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; -// createInfo.surface = device.physicalOutputDevice->physicalOutputDevice->instance->instance->window_surface; - -// createInfo.minImageCount = details.ImageCount; -// createInfo.imageFormat = swapchain_details.surfaceFormat.format; -// createInfo.imageColorSpace = swapchain_details.surfaceFormat.colorSpace; -// createInfo.imageExtent = swapchain_details.extent; -// createInfo.imageArrayLayers = 1; -// createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; - -// details.ImageSize = { swapchain_details.extent.width, swapchain_details.extent.height }; - -// QueueFamilyIndices indices = findQueueFamilies(device.physicalOutputDevice->physicalOutputDevice->instance->instance->window_surface, device.physicalOutputDevice->physicalOutputDevice->device); -// uint32_t queueFamilyIndices[] = {indices.graphicsFamily.value(), indices.presentFamily.value()}; - -// if (indices.graphicsFamily != indices.presentFamily) { -// createInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT; -// createInfo.queueFamilyIndexCount = 2; -// createInfo.pQueueFamilyIndices = queueFamilyIndices; -// } else { -// createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; -// createInfo.queueFamilyIndexCount = 0; // Optional -// createInfo.pQueueFamilyIndices = nullptr; // Optional -// } - -// createInfo.preTransform = swapchain_details.swapChainSupport.capabilities.currentTransform; -// createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; -// createInfo.presentMode = swapchain_details.presentMode; -// createInfo.clipped = VK_TRUE; -// createInfo.oldSwapchain = VK_NULL_HANDLE; - -// if (vkCreateSwapchainKHR(device.outputDevice->device, &createInfo, nullptr, &presentationQueue->presentationQueue->swapChain) != VK_SUCCESS) { -// gnReturnError(GN_FAILED_CREATE_PRESENTATION_QUEUE, "too lazy to query vulkan why"); -// } - -// std::vector swapChainImages; -// vkGetSwapchainImagesKHR(device.outputDevice->device, presentationQueue->presentationQueue->swapChain, &details.ImageCount, nullptr); -// swapChainImages.resize(details.ImageCount); -// vkGetSwapchainImagesKHR(device.outputDevice->device, presentationQueue->presentationQueue->swapChain, &details.ImageCount, swapChainImages.data()); - -// presentationQueue->valid = true; -// presentationQueue->presentationQueue->device = &device.outputDevice->device; - -// if (gnListLength(presentationQueue->images) < swapChainImages.size()) { -// for (size_t i = gnListLength(presentationQueue->images); i < swapChainImages.size(); i++) { -// gnTexture newImage = gnTexture(); -// newImage.texture = new gnPlatformTexture(); -// gnListAdd(presentationQueue->images, newImage); -// } -// } - -// for (size_t i = 0; i < swapChainImages.size(); i++) { -// VkImageView imageView; -// gnReturnCode code = createImageView(device, swapChainImages[i], swapchain_details.surfaceFormat.format, VK_IMAGE_ASPECT_COLOR_BIT, &imageView); -// presentationQueue->images[i].texture->textureImageView = imageView; -// presentationQueue->images[i].texture->textureImage = swapChainImages[i]; -// presentationQueue->images[i].texture->outputDevice = const_cast(&device); -// presentationQueue->images[i].texture->swapchainImage = true; -// if (code != GN_SUCCESS) return code; -// } - -// return GN_SUCCESS; -// } - -// GN_EXPORT void gnDestroyPresentationQueueFn(gnPresentationQueue& queue) { -// for (size_t i = 0; i < gnListLength(queue.images); i++) -// vkDestroyImageView(*queue.presentationQueue->device, queue.images[i].texture->textureImageView, nullptr); -// vkDestroySwapchainKHR(*queue.presentationQueue->device, queue.presentationQueue->swapChain, nullptr); -// } - -// GN_EXPORT gnReturnCode gnPresentationQueueGetNextImageAsyncFn(gnPresentationQueue& presentationQueue, const gnSyncSemaphore& semaphore, gnUInt* imageIndex) { -// gnUInt nextImageIndex = 0; -// presentationQueue.presentationQueue->result = -// vkAcquireNextImageKHR(*presentationQueue.presentationQueue->device, presentationQueue.presentationQueue->swapChain, UINT64_MAX, semaphore.semaphore->semaphore, VK_NULL_HANDLE, &nextImageIndex); -// *imageIndex = nextImageIndex; - -// if (presentationQueue.presentationQueue->result != VK_SUCCESS) { -// return GN_FAILED; -// } - -// return GN_SUCCESS; -// } - -// GN_EXPORT gnPresentationQueueState gnPresentationQueueGetStateFn(gnPresentationQueue& presentationQueue) { -// if (presentationQueue.presentationQueue->result == VK_ERROR_OUT_OF_DATE_KHR) { -// return GN_OUT_OF_DATE; -// } else if (presentationQueue.presentationQueue->result == VK_SUBOPTIMAL_KHR) { -// return GN_SUBOPTIMAL; -// } -// else if (presentationQueue.presentationQueue->result == VK_SUCCESS) { -// return GN_VALID; -// } -// return GN_VALID; -// } +void gnDestroyPresentationQueueFn(gnPresentationQueue* queue) { + vkDestroySwapchainKHR(queue->outputDevice->outputDevice->device, queue->presentationQueue->swapChain, NULL); +} diff --git a/src/core/gryphn_platform_functions.h b/src/core/gryphn_platform_functions.h index f1ba209..8c385c9 100644 --- a/src/core/gryphn_platform_functions.h +++ b/src/core/gryphn_platform_functions.h @@ -49,4 +49,5 @@ typedef struct gnFunctions_t { #include "core/presentation_queue/gryphn_presentation_queue.h" typedef struct gnDeviceFunctions_t { gnReturnCode (*_gnCreatePresentationQueue)(gnPresentationQueue* presentationQueue, const gnOutputDevice* device, struct gnPresentationQueueInfo_t presentationInfo); + void (*_gnDestroyPresentationQueue)(gnPresentationQueue *presentationQueue); } gnDeviceFunctions; diff --git a/src/core/instance/init/gryphn_init.c b/src/core/instance/init/gryphn_init.c index d10e932..a1865bd 100644 --- a/src/core/instance/init/gryphn_init.c +++ b/src/core/instance/init/gryphn_init.c @@ -70,4 +70,5 @@ void gnLoadFunctions(struct gnDynamicLibrary_t* lib, struct gnFunctions_t* funct void gnLoadDeviceFunctions(struct gnDynamicLibrary_t* lib, struct gnDeviceFunctions_t* functions) { gnLoadDLLFunction(lib, functions->_gnCreatePresentationQueue, "gnCreatePresentationQueueFn"); + gnLoadDLLFunction(lib, functions->_gnDestroyPresentationQueue, "gnDestroyPresentationQueueFn"); } diff --git a/src/core/output_device/gryphn_output_device.c b/src/core/output_device/gryphn_output_device.c index 42a467a..2418ada 100644 --- a/src/core/output_device/gryphn_output_device.c +++ b/src/core/output_device/gryphn_output_device.c @@ -8,6 +8,7 @@ gnReturnCode gnCreateOutputDevice(gnOutputDevice* outputDevice, gnInstance* inst gnLoadDeviceFunctions(instance->dynamicLib, outputDevice->deviceFunctions); outputDevice->instance = instance; outputDevice->physicalDevice = deviceInfo.physicalDevice; + outputDevice->deviceInfo = deviceInfo; return instance->functions->_gnCreateOutputDevoce(outputDevice, instance, deviceInfo); } void gnDestroyOutputDevice(gnOutputDevice* device) { diff --git a/src/core/output_device/gryphn_output_device.h b/src/core/output_device/gryphn_output_device.h index 863c8f4..32d25b6 100644 --- a/src/core/output_device/gryphn_output_device.h +++ b/src/core/output_device/gryphn_output_device.h @@ -14,12 +14,13 @@ typedef struct gnOutputDeviceInfo_t { uint32_t queueInfoCount; struct gnDeviceQueueInfo_t* queueInfos; struct gnPhysicalDeviceFeatures_t enabledFeatures; - const gnPhysicalDevice physicalDevice; + struct gnPhysicalDevice_t physicalDevice; } gnOutputDeviceInfo; typedef struct gnOutputDevice_t { struct gnPlatformOutputDevice_t* outputDevice; struct gnDeviceFunctions_t* deviceFunctions; + struct gnOutputDeviceInfo_t deviceInfo; gnInstance* instance; gnPhysicalDevice physicalDevice; } gnOutputDevice; diff --git a/src/core/presentation_queue/gryphn_presentation_queue.c b/src/core/presentation_queue/gryphn_presentation_queue.c index e267ff6..900d725 100644 --- a/src/core/presentation_queue/gryphn_presentation_queue.c +++ b/src/core/presentation_queue/gryphn_presentation_queue.c @@ -1,11 +1,11 @@ #include "gryphn_presentation_queue.h" #include "core/gryphn_platform_functions.h" -gnReturnCode gnCreatePresentationQueue(gnPresentationQueue* presentationQueue, const gnOutputDevice* device, struct gnPresentationQueueInfo_t presentationInfo){ +gnReturnCode gnCreatePresentationQueue(gnPresentationQueue* presentationQueue, struct gnOutputDevice_t* device, struct gnPresentationQueueInfo_t presentationInfo){ + presentationQueue->outputDevice = device; return device->deviceFunctions->_gnCreatePresentationQueue(presentationQueue, device, presentationInfo); } -// gnTexture* gnGetPresentationQueueImage(gnPresentationQueue& presentationQueue, int index) { -// // if (index < gnListLength(presentationQueue.images)) -// return gnListGetPtr(presentationQueue.images, index); // zero fucking error checking in this file, im not doing that shit -// } +void gnDestroyPresentationQueue(gnPresentationQueue *presentationQueue) { + presentationQueue->outputDevice->deviceFunctions->_gnDestroyPresentationQueue(presentationQueue); +} diff --git a/src/core/presentation_queue/gryphn_presentation_queue.h b/src/core/presentation_queue/gryphn_presentation_queue.h index 702b0bf..d145b2a 100644 --- a/src/core/presentation_queue/gryphn_presentation_queue.h +++ b/src/core/presentation_queue/gryphn_presentation_queue.h @@ -9,27 +9,18 @@ typedef struct gnPresentationQueueInfo_t { struct gnUInt2_t ImageSize; struct gnWindowSurface_t surface; struct gnSurfaceFormat_t format; + enum gnImageSharingMode_e imageSharingMode; + uint32_t queueFamilyCount; + uint32_t* queueFamilies; } gnPresentationQueueInfo; struct gnPlatformPresentationQueue_t; typedef struct gnPresentationQueue_t { - struct gnPlatformPresentationQueue* presentationQueue; + struct gnPlatformPresentationQueue_t* presentationQueue; + struct gnOutputDevice_t* outputDevice; gnBool valid; - // uint32_t textureCount; - // gnTexture* texturs; } gnPresentationQueue; -// gnTexture* gnGetPresentationQueueImage(gnPresentationQueue& presentationQueue, int index); - -gnReturnCode gnCreatePresentationQueue(gnPresentationQueue* presentationQueue, const gnOutputDevice* device, struct gnPresentationQueueInfo_t presentationInfo); - -// inline gnReturnCode (*gnCreatePresentationQueue)(gnPresentationQueue* presentationQueue, const gnOutputDevice& device, gnPresentationDetails& details); -// inline void (*gnDestroyPresentationQueue)(gnPresentationQueue& queue); -// inline gnImageFormat (*_gnPresentationQueueGetImageFormat)(gnPresentationQueue& presentationQueue); -// inline gnImageFormat gnPresentationQueueGetImageFormat(gnPresentationQueue& presentationQueue) { - // std::cout << "gnPresentationQueueGetImageFormat should lowkey become supported\n"; - // return _gnPresentationQueueGetImageFormat(presentationQueue); -// } -// inline gnPresentationQueueState (*gnPresentationQueueGetState)(gnPresentationQueue& presentationQueue); -// inline gnReturnCode (*gnPresentationQueueGetNextImageAsync)(gnPresentationQueue& presentationQueue, const gnSyncSemaphore& semaphore, gnUInt* imageIndex); +gnReturnCode gnCreatePresentationQueue(gnPresentationQueue* presentationQueue, struct gnOutputDevice_t* device, struct gnPresentationQueueInfo_t presentationInfo); +void gnDestroyPresentationQueue(gnPresentationQueue* presentationQueue); diff --git a/src/utils/gryphn_error_code.h b/src/utils/gryphn_error_code.h index 0b7a6f1..10d8341 100644 --- a/src/utils/gryphn_error_code.h +++ b/src/utils/gryphn_error_code.h @@ -13,7 +13,8 @@ typedef enum gnReturnCode_t { GN_DEBUGGER_EXISTS, GN_NO_SUPPORTED_FORMATS, GN_NO_SUPPORTED_PRESENT_MODES, - GN_UNKNOWN_IMAGE_FORMAT + GN_UNKNOWN_IMAGE_FORMAT, + GN_FAILED_TO_CREATE_PRESENTATION_QUEUE // GN_UNKNOWN_ERROR, // GN_UNKNOWN_FRAMEBUFFER_ATTACHMENT, @@ -47,5 +48,6 @@ static const char* gnErrorCodeToCString(enum gnReturnCode_t returnCode) { case GN_NO_SUPPORTED_FORMATS: return "GN_NO_SUPPORTED_FORMATS"; case GN_NO_SUPPORTED_PRESENT_MODES: return "GN_NO_SUPPORTED_PRESENT_MODES"; case GN_UNKNOWN_IMAGE_FORMAT: return "GN_UNKNOWN_IMAGE_FORMAT"; + case GN_FAILED_TO_CREATE_PRESENTATION_QUEUE: return "GN_FAILED_TO_CREATE_PRESENTATION_QUEUE"; } } diff --git a/src/utils/types/gryphn_image_format.h b/src/utils/types/gryphn_image_format.h index dc61747..92d21bc 100644 --- a/src/utils/types/gryphn_image_format.h +++ b/src/utils/types/gryphn_image_format.h @@ -8,3 +8,7 @@ typedef enum gnImageFormat_e { typedef enum gnColorSpace_e { GN_COLOR_SPACE_SRGB_NONLINEAR } gnColorSpace; + +typedef enum gnImageSharingMode_e { + GN_SHARING_MODE_EXCLUSIVE, GN_SHARING_MODE_CONCURRENT +} gnImageSharingMode;