From e59126b873dc760d0e5da6e4d4e5feaeda447ea0 Mon Sep 17 00:00:00 2001 From: Warwick New Date: Tue, 31 Dec 2024 12:41:18 +0000 Subject: [PATCH] finished image view part of tutorial but found issues when running with address sanitiser --- CMakeLists.txt | 11 +++++++++++ src/main.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 86f06d3..9dbe85e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,17 @@ file(GLOB_RECURSE HEADER_FILES ${CMAKE_SOURCE_DIR}/src/*.h) add_executable(${PROJECT_NAME} ${HEADER_FILES} ${SOURCE_FILES}) +## Error on memory address issues in debug mode +#if(CMAKE_BUILD_TYPE MATCHES "Debug") +# set( +# CMAKE_C_FLAGS +# "${CMAKE_C_FLAGS} -Werror -fsanitize=undefined -fsanitize=address" +# ) +# target_link_options(${PROJECT_NAME} +# BEFORE PUBLIC -fsanitize=undefined PUBLIC -fsanitize=address +# ) +#endif() + # Link Libraries target_link_libraries(${PROJECT_NAME} ${GLFW_LIBRARIES} diff --git a/src/main.c b/src/main.c index fc9c921..09908bb 100644 --- a/src/main.c +++ b/src/main.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #define GLFW_INCLUDE_VULKAN @@ -49,8 +50,11 @@ typedef struct Application { VkQueue presentQueue; VkSwapchainKHR swapChain; VkImage *swapChainImages; + uint32_t swapChainImageCount; VkFormat swapChainImageFormat; VkExtent2D swapChainExtent; + VkImageView *swapChainImageViews; + uint32_t swapChainImageViewCount; } Application; typedef struct SwapChainSupportDetails { @@ -566,10 +570,47 @@ void createSwapChain(Application *app) { exit(EXIT_FAILURE); } + vkGetSwapchainImagesKHR(app->device, app->swapChain, + &app->swapChainImageCount, NULL); + app->swapChainImages = malloc(app->swapChainImageCount * sizeof(VkImage)); + vkGetSwapchainImagesKHR(app->device, app->swapChain, + &app->swapChainImageCount, app->swapChainImages); + app->swapChainImageFormat = surfaceFormat.format; app->swapChainExtent = extent; } +void createImageViews(Application *app) { + app->swapChainImageViewCount = app->swapChainImageCount; + app->swapChainImageViews = + malloc(app->swapChainImageViewCount * sizeof(VkImageView)); + + for (size_t i = 0; i < app->swapChainImageViewCount; i++) { + VkImageViewCreateInfo createInfo = {0}; + createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; + createInfo.image = app->swapChainImages[i]; + createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; + createInfo.format = app->swapChainImageFormat; + + createInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY; + createInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY; + createInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY; + createInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY; + + createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; + createInfo.subresourceRange.baseMipLevel = 0; + createInfo.subresourceRange.levelCount = 1; + createInfo.subresourceRange.baseArrayLayer = 0; + createInfo.subresourceRange.layerCount = 1; + + if (vkCreateImageView(app->device, &createInfo, NULL, + &app->swapChainImageViews[i]) != VK_SUCCESS) { + fprintf(stderr, "Failed to create image views!\n"); + exit(EXIT_FAILURE); + } + } +} + void initVulkan(Application *app) { createInstance(app); setupDebugMessenger(app); @@ -577,6 +618,7 @@ void initVulkan(Application *app) { pickPhysicalDevice(app); createLogicalDevice(app); createSwapChain(app); + createImageViews(app); } void mainLoop(Application *app) { @@ -586,6 +628,11 @@ void mainLoop(Application *app) { } void cleanup(Application *app) { + for (size_t i = 0; i < app->swapChainImageViewCount; i++) { + vkDestroyImageView(app->device, app->swapChainImageViews[i], NULL); + } + free(app->swapChainImageViews); + free(app->swapChainImages); vkDestroySwapchainKHR(app->device, app->swapChain, NULL); vkDestroyDevice(app->device, NULL); if (enableValidationLayers) {