From a51ef12d8583522ee229a0195a4132652f0f9cd8 Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sat, 4 May 2024 15:47:54 +1000 Subject: finish swapchain creation --- assets/shaders/triangle.frag | 0 assets/shaders/triangle.vert | 0 src/renderer/backends/backend_vulkan.c | 74 +++++++++++++++++----------------- src/renderer/backends/backend_vulkan.h | 6 +++ src/renderer/backends/vulkan_helpers.h | 19 ++++++++- src/renderer/ral.h | 2 +- src/std/utils.h | 4 ++ src/systems/terrain.h | 2 +- 8 files changed, 66 insertions(+), 41 deletions(-) create mode 100644 assets/shaders/triangle.frag create mode 100644 assets/shaders/triangle.vert create mode 100644 src/std/utils.h diff --git a/assets/shaders/triangle.frag b/assets/shaders/triangle.frag new file mode 100644 index 0000000..e69de29 diff --git a/assets/shaders/triangle.vert b/assets/shaders/triangle.vert new file mode 100644 index 0000000..e69de29 diff --git a/src/renderer/backends/backend_vulkan.c b/src/renderer/backends/backend_vulkan.c index 08e62bf..028cde8 100644 --- a/src/renderer/backends/backend_vulkan.c +++ b/src/renderer/backends/backend_vulkan.c @@ -13,7 +13,7 @@ #include "defines.h" #include "log.h" #include "ral.h" -#include "ral_types.h" +#include "utils.h" // TEMP #define SCREEN_WIDTH 1000 @@ -182,32 +182,8 @@ bool gpu_device_create(gpu_device* out_device) { } TRACE("Physical device selected"); - // vulkan_device_query_swapchain_support(out_device->physical_device, context.surface, - // &context.swapchain_support); - - // Logical device + // Logical device & Queues create_logical_device(out_device); - // VkDeviceQueueCreateInfo queue_create_info = {}; - - // queue_family_indices indices = find_queue_families(context.device->physical_device); - // //.. - // VkDeviceCreateInfo device_create_info = { VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO }; - // device_create_info.queueCreateInfoCount = VULKAN_QUEUES_COUNT; - // device_create_info.pQueueCreateInfos = queue_create_info; - // device_create_info.pEnabledFeatures = &device_features; - // device_create_info.enabledExtensionCount = 1; - // const char* extension_names = VK_KHR_SWAPCHAIN_EXTENSION_NAME; - // device_create_info.ppEnabledExtensionNames = &extension_names; - - // VkResult result = vkCreateDevice(out_device->physical_device, &device_create_info, - // context.allocator, &out_device->logical_device); - // if (result != VK_SUCCESS) { - // FATAL("Error creating logical device with status %u\n", result); - // exit(1); - // } - // TRACE("Logical device created"); - - // Queues // Create the command pool @@ -216,29 +192,55 @@ bool gpu_device_create(gpu_device* out_device) { } bool gpu_swapchain_create(gpu_swapchain* out_swapchain) { - VkExtent2D swapchain_extent = { context.screen_width, context.screen_height }; + out_swapchain->swapchain_arena = arena_create(malloc(1024), 1024); + vulkan_swapchain_support_info swapchain_support = context.swapchain_support; - // find a format + // TODO: custom swapchain extents VkExtent2D swapchain_extent = { width, height }; + VkSurfaceFormatKHR image_format = choose_swapchain_format(&swapchain_support); + out_swapchain->image_format = image_format; VkPresentModeKHR present_mode = VK_PRESENT_MODE_FIFO_KHR; // guaranteed to be implemented + out_swapchain->present_mode = present_mode; + + u32 image_count = swapchain_support.capabilities.minImageCount + 1; + out_swapchain->image_count = image_count; VkSwapchainCreateInfoKHR swapchain_create_info = { VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR }; swapchain_create_info.surface = context.surface; - swapchain_create_info.minImageCount = 2; - // TODO: image_ fields + swapchain_create_info.minImageCount = image_count; + swapchain_create_info.imageFormat = image_format.format; + swapchain_create_info.imageColorSpace = image_format.colorSpace; + swapchain_create_info.imageExtent = swapchain_support.capabilities.currentExtent; + swapchain_create_info.imageArrayLayers = 1; + swapchain_create_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; + swapchain_create_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; swapchain_create_info.queueFamilyIndexCount = 0; - swapchain_create_info.pQueueFamilyIndices = 0; + swapchain_create_info.pQueueFamilyIndices = NULL; - // TODO: preTransform + swapchain_create_info.preTransform = swapchain_support.capabilities.currentTransform; swapchain_create_info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; swapchain_create_info.presentMode = present_mode; - swapchain_create_info.clipped = VK_TRUE; - swapchain_create_info.oldSwapchain = 0; + swapchain_create_info.oldSwapchain = VK_NULL_HANDLE; + + out_swapchain->extent = swapchain_support.capabilities.currentExtent; VK_CHECK(vkCreateSwapchainKHR(context.device->logical_device, &swapchain_create_info, context.allocator, &out_swapchain->handle)); TRACE("Vulkan Swapchain created"); + + // Retrieve Images + out_swapchain->images = + arena_alloc(&out_swapchain->swapchain_arena, image_count * sizeof(VkImage)); + VK_CHECK(vkGetSwapchainImagesKHR(context.device->logical_device, out_swapchain->handle, + &image_count, out_swapchain->images)); + + return true; +} + +void gpu_swapchain_destroy(gpu_swapchain* swapchain) { + arena_free_storage(&swapchain->swapchain_arena); + vkDestroySwapchainKHR(context.device, swapchain->handle, context.allocator); } gpu_pipeline* gpu_graphics_pipeline_create(struct graphics_pipeline_desc description) { @@ -482,7 +484,7 @@ queue_family_indices find_queue_families(VkPhysicalDevice device) { VkBool32 present_support = false; vkGetPhysicalDeviceSurfaceSupportKHR(device, q_fam_i, context.surface, &present_support); - if (present_support) { + if (present_support && !indices.has_present) { indices.present_family_index = q_fam_i; indices.has_present = true; } @@ -491,8 +493,6 @@ queue_family_indices find_queue_families(VkPhysicalDevice device) { return indices; } -const char* bool_str(bool input) { return input ? "True" : "False"; } - bool create_logical_device(gpu_device* out_device) { queue_family_indices indices = find_queue_families(out_device->physical_device); INFO(" %s | %s | %s | %s | %s", bool_str(indices.has_graphics), bool_str(indices.has_present), diff --git a/src/renderer/backends/backend_vulkan.h b/src/renderer/backends/backend_vulkan.h index 8c6b772..0114d7a 100644 --- a/src/renderer/backends/backend_vulkan.h +++ b/src/renderer/backends/backend_vulkan.h @@ -28,6 +28,12 @@ typedef struct queue_family_indices { typedef struct gpu_swapchain { VkSwapchainKHR handle; + arena swapchain_arena; + VkExtent2D extent; + VkSurfaceFormatKHR image_format; + VkPresentModeKHR present_mode; + VkImage* images; + u32 image_count; } gpu_swapchain; typedef struct gpu_device { diff --git a/src/renderer/backends/vulkan_helpers.h b/src/renderer/backends/vulkan_helpers.h index 03ee814..55d8846 100644 --- a/src/renderer/backends/vulkan_helpers.h +++ b/src/renderer/backends/vulkan_helpers.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -54,8 +55,8 @@ VKAPI_ATTR VkBool32 VKAPI_CALL vk_debug_callback( VkDebugUtilsMessageSeverityFlagBitsEXT severity, VkDebugUtilsMessageTypeFlagsEXT flags, const VkDebugUtilsMessengerCallbackDataEXT* callback_data, void* user_data); -void vulkan_device_query_swapchain_support(VkPhysicalDevice device, VkSurfaceKHR surface, - vulkan_swapchain_support_info* out_support_info) { +static void vulkan_device_query_swapchain_support(VkPhysicalDevice device, VkSurfaceKHR surface, + vulkan_swapchain_support_info* out_support_info) { // TODO: add VK_CHECK to these calls! // Surface capabilities @@ -78,6 +79,20 @@ void vulkan_device_query_swapchain_support(VkPhysicalDevice device, VkSurfaceKHR } } +static VkSurfaceFormatKHR choose_swapchain_format( + vulkan_swapchain_support_info* swapchain_support) { + assert(swapchain_support->format_count > 0); + // find a format + for (u32 i = 0; i < swapchain_support->format_count; i++) { + VkSurfaceFormatKHR format = swapchain_support->formats[i]; + if (format.format == VK_FORMAT_B8G8R8A8_SRGB && + format.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { + return format; + } + } + return swapchain_support->formats[0]; +} + // static bool physical_device_meets_requirements( // VkPhysicalDevice device, VkSurfaceKHR surface, const VkPhysicalDeviceProperties* properties, // const VkPhysicalDeviceFeatures* features, diff --git a/src/renderer/ral.h b/src/renderer/ral.h index 7c143f2..f202e51 100644 --- a/src/renderer/ral.h +++ b/src/renderer/ral.h @@ -77,7 +77,7 @@ gpu_pipeline* gpu_graphics_pipeline_create(struct graphics_pipeline_desc descrip void gpu_pipeline_destroy(gpu_pipeline* pipeline); bool gpu_swapchain_create(gpu_swapchain* out_swapchain); -void gpu_swapchain_destroy(); +void gpu_swapchain_destroy(gpu_swapchain* swapchain); void gpu_cmd_encoder_begin(); void gpu_cmd_encoder_begin_render(); diff --git a/src/std/utils.h b/src/std/utils.h new file mode 100644 index 0000000..c9827a3 --- /dev/null +++ b/src/std/utils.h @@ -0,0 +1,4 @@ +#pragma once +#include + +const char* bool_str(bool input) { return input ? "True" : "False"; } \ No newline at end of file diff --git a/src/systems/terrain.h b/src/systems/terrain.h index 745ca22..bfd90b5 100644 --- a/src/systems/terrain.h +++ b/src/systems/terrain.h @@ -18,8 +18,8 @@ Future: #include "defines.h" #include "maths_types.h" #include "mem.h" -#include "str.h" #include "render_types.h" +#include "str.h" typedef struct heightmap { str8 filepath; -- cgit v1.2.3-70-g09d2