diff --git a/src/main.c b/src/main.c index b161e13..d0c4530 100644 --- a/src/main.c +++ b/src/main.c @@ -47,6 +47,12 @@ typedef struct ApplicationState { //swapchain details: SwapChainSupportDetails details; VkSwapchainKHR swapchain; + uint32_t swapchain_image_count; + VkImage* swapchain_images; + VkFormat swapchain_image_format; + VkExtent2D swapchain_extent; + //describes how to access an image and allows access to it + VkImageView* swapchain_image_views; } ApplicationState; typedef struct QueueFamilyIndices { @@ -470,6 +476,43 @@ void create_swap_chain(ApplicationState* state) { } printf("swapchain created\n"); + + vkGetSwapchainImagesKHR(state->device, state->swapchain, &image_count, NULL); + printf("image count %x\n", image_count); + state->swapchain_images = malloc(sizeof(VkImage) * image_count); + state->swapchain_image_count = image_count; + vkGetSwapchainImagesKHR(state->device, state->swapchain, &image_count, state->swapchain_images); + state->swapchain_image_format = format.format; + state->swapchain_extent = extent; +} + +void create_image_views(ApplicationState* state) { + printf("creating image views\n"); + state->swapchain_image_views = malloc(sizeof(VkImageView) * state->swapchain_image_count); + + for (int i = 0; i < state->swapchain_image_count; i++) { + VkImageViewCreateInfo create_info = {0}; + create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; + create_info.image = state->swapchain_images[i]; + create_info.viewType = VK_IMAGE_VIEW_TYPE_2D; + create_info.format = state->swapchain_image_format; + create_info.components.r = VK_COMPONENT_SWIZZLE_IDENTITY; + create_info.components.g = VK_COMPONENT_SWIZZLE_IDENTITY; + create_info.components.b = VK_COMPONENT_SWIZZLE_IDENTITY; + create_info.components.a = VK_COMPONENT_SWIZZLE_IDENTITY; + + create_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; + create_info.subresourceRange.baseMipLevel = 0; + create_info.subresourceRange.levelCount = 1; + create_info.subresourceRange.baseArrayLayer = 0; + create_info.subresourceRange.layerCount = 1; + + if (vkCreateImageView(state->device, &create_info, NULL, &state->swapchain_image_views[i]) != VK_SUCCESS) { + printf("failed to create image view %x\n", i); + exit(1); + } + } + printf("created image views\n"); } void init_vulkan(ApplicationState* state) { @@ -478,6 +521,7 @@ void init_vulkan(ApplicationState* state) { pick_physical_device(state); create_logical_device(state); create_swap_chain(state); + create_image_views(state); } void main_loop(ApplicationState* state) { @@ -487,6 +531,9 @@ void main_loop(ApplicationState* state) { } void terminate(ApplicationState* state) { + for (int i = 0; i < state->swapchain_image_count; i++) { + vkDestroyImageView(state->device, state->swapchain_image_views[i], NULL); + } vkDestroySwapchainKHR(state->device, state->swapchain, NULL); vkDestroyDevice(state->device, NULL); vkDestroySurfaceKHR(state->instance, state->surface, NULL);