From 40fbb328150d853d0d7a83ce447c3f008de38822 Mon Sep 17 00:00:00 2001 From: gramanas Date: Sun, 16 Jun 2024 22:20:07 +0300 Subject: Start integrating pds, fix warnings and add new test build object --- src/vksetup.h | 178 ++++++++++++++++++++++++++-------------------------------- 1 file changed, 81 insertions(+), 97 deletions(-) (limited to 'src/vksetup.h') diff --git a/src/vksetup.h b/src/vksetup.h index 5ba2b0b..8e14637 100644 --- a/src/vksetup.h +++ b/src/vksetup.h @@ -232,14 +232,15 @@ typedef struct vks_transition_image_layout_info /* Exported API */ VKSDEF void vks_create_vulkan_context (vks_context *vk); -VKSDEF void vks_create_buffer (const vks_context vk, const VkDeviceSize size, const VkBufferUsageFlags usage, const VkMemoryPropertyFlags properties, vks_buffer *buffer); -VKSDEF void vks_create_image (const vks_context vk, const uint32_t width, const uint32_t height, const uint32_t mipLevels, const VkFormat format, const VkImageTiling tiling, const VkImageUsageFlags usage, const VkMemoryPropertyFlags properties, const VkSampleCountFlagBits numSamples, vks_image *image); +VKSDEF void vks_destroy_vulkan_context (vks_context *vk); +VKSDEF void vks_create_buffer (const vks_context vk, const VkDeviceSize size, const VkBufferUsageFlags usage, vks_buffer *buffer); +VKSDEF void vks_create_image (const vks_context vk, const uint32_t width, const uint32_t height, const uint32_t mipLevels, const VkFormat format, const VkImageTiling tiling, const VkImageUsageFlags usage, const VkSampleCountFlagBits numSamples, vks_image *image); VKSDEF void vks_transition_image_layout (const vks_context vk, const vks_transition_image_layout_info *info); VKSDEF void vks_copy_buffer (const vks_context vk, const VkBuffer src_buffer, VkBuffer dst_buffer, const VkDeviceSize size); VKSDEF void vks_copy_buffer_to_image (const vks_context vk, const VkBuffer buffer, VkImage image, const uint32_t width, const uint32_t height); VKSDEF void vks_generate_mipmaps (const vks_context vk, VkImage image, const VkFormat imageFormat, const int32_t texWidth, const int32_t texHeight, const uint32_t mipLevels); VKSDEF VkImageView vks_create_image_view (const vks_context vk, VkImage image, VkFormat format, VkImageAspectFlags aspectFlags, uint32_t mipLevels); -VKSDEF VkFormat findDepthFormat(const vks_context vk); +VKSDEF VkFormat vks_find_depth_format (const vks_context vk); /* VKSDEF void vulkan_create_descriptor_set_layout(); */ /* VKSDEF void vulkan_create_graphics_pipeline(); */ /* VKSDEF void vulkan_create_command_pool(); */ @@ -295,26 +296,6 @@ _vks_check_validation_layer_support(const char* const validation_layers[], return layerFound; } -static uint32_t -_find_memory_type(const vks_context vk, - const uint32_t typeFilter, - const VkMemoryPropertyFlags properties) -{ - VkPhysicalDeviceMemoryProperties mem_properties; - vkGetPhysicalDeviceMemoryProperties(vk.physical_device, &mem_properties); - - for (uint32_t i = 0; i < mem_properties.memoryTypeCount; i++) { - if ((typeFilter & (1 << i)) && - (mem_properties.memoryTypes[i].propertyFlags & properties) == - properties) { - return i; - } - } - - vk_log(VK_ERROR, "failed to find suitable memory type!\n"); - return 9999; -} - VKSDEF VkCommandBuffer _vks_begin_single_time_commands(const vks_context vk) { @@ -564,10 +545,8 @@ _vulkan_find_queue_families(vks_context* vk, VkPhysicalDevice device) typedef struct _swap_chain_support_details { VkSurfaceCapabilitiesKHR capabilities; - VkSurfaceFormatKHR formats[100]; - uint32_t formatCount; - VkPresentModeKHR presentModes[100]; - uint32_t presentModeCount; + VkSurfaceFormatKHR *formats; + VkPresentModeKHR *presentModes; } _swap_chain_support_details; static _swap_chain_support_details @@ -580,22 +559,26 @@ _query_swap_chain_support(const vks_context* vk, VkPhysicalDevice device) vkGetPhysicalDeviceSurfaceCapabilitiesKHR( device, vk->surface, &details.capabilities); + uint32_t format_count, present_mode_count; + vkGetPhysicalDeviceSurfaceFormatsKHR( - device, vk->surface, &details.formatCount, NULL); + device, vk->surface, &format_count, NULL); - if (details.formatCount != 0) { - // todo alloc format arrray + if (format_count != 0) { + details.formats = pstack_create_ex(VkSurfaceFormatKHR, format_count); vkGetPhysicalDeviceSurfaceFormatsKHR( - device, vk->surface, &details.formatCount, details.formats); + device, vk->surface, &format_count, details.formats); + pstack_size_set(details.formats, format_count); } - + vkGetPhysicalDeviceSurfacePresentModesKHR( - device, vk->surface, &details.presentModeCount, NULL); + device, vk->surface, &present_mode_count, NULL); - if (details.presentModeCount != 0) { - // todo alloc presentModes array + if (present_mode_count != 0) { + details.presentModes = pstack_create_ex(VkPresentModeKHR, present_mode_count); vkGetPhysicalDeviceSurfacePresentModesKHR( - device, vk->surface, &details.presentModeCount, details.presentModes); + device, vk->surface, &present_mode_count, details.presentModes); + pstack_size_set(details.presentModes, present_mode_count); } return details; @@ -611,8 +594,11 @@ _vulkan_is_device_suitable(vks_context* vk, VkPhysicalDevice device) if (extensionsSupported) { _swap_chain_support_details swapChainSupport = _query_swap_chain_support(vk, device); - swapChainAdequate = !(swapChainSupport.formatCount == 0) && - !(swapChainSupport.presentModeCount == 0); + swapChainAdequate = !(pstack_size(swapChainSupport.formats) == 0) && + !(pstack_size(swapChainSupport.presentModes) == 0); + + pstack_destroy(swapChainSupport.formats); + pstack_destroy(swapChainSupport.presentModes); } VkPhysicalDeviceFeatures supportedFeatures; @@ -749,35 +735,6 @@ _vulkan_create_logical_device(vks_context* vk) vkGetDeviceQueue(vk->device, indices.presentFamily, 0, &vk->present_queue); } -static VkSurfaceFormatKHR -_chooseSwapSurfaceFormat(const VkSurfaceFormatKHR* availableFormats, - uint32_t formatCount) -{ - for (uint32_t i = 0; i < formatCount; i++) { - if (availableFormats[i].format == VK_FORMAT_B8G8R8A8_SRGB && - availableFormats[i].colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { - return availableFormats[i]; - } - } - - // if it fails pick the first one - return availableFormats[0]; -} - -static VkPresentModeKHR -_chooseSwapPresentMode(const VkPresentModeKHR* presentModes, - uint32_t presentModeCount) -{ - for (uint32_t i = 0; i < presentModeCount; i++) { - if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) { - return presentModes[i]; - } - } - - // if it fails pick the the FIFO one - return VK_PRESENT_MODE_FIFO_KHR; -} - static VkExtent2D _chooseSwapExtent(const vks_context* vk, const VkSurfaceCapabilitiesKHR* capabilities) @@ -817,10 +774,31 @@ _vulkan_create_swap_chain(vks_context* vk) _swap_chain_support_details swapChainSupport = _query_swap_chain_support(vk, vk->physical_device); - VkSurfaceFormatKHR surfaceFormat = _chooseSwapSurfaceFormat( - swapChainSupport.formats, swapChainSupport.formatCount); - VkPresentModeKHR presentMode = _chooseSwapPresentMode( - swapChainSupport.presentModes, swapChainSupport.presentModeCount); + VkSurfaceFormatKHR surfaceFormat; + while (!pstack_pop(swapChainSupport.formats, &surfaceFormat)) { + //for (uint32_t i = 0; i < formatCount; i++) { + if (surfaceFormat.format == VK_FORMAT_B8G8R8A8_SRGB && + surfaceFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { + break; + } + } + + pstack_destroy(swapChainSupport.formats); + + VkPresentModeKHR presentMode; + while (!pstack_pop(swapChainSupport.presentModes, &presentMode)) { + if (presentMode == VK_PRESENT_MODE_MAILBOX_KHR) { + break; + } + } + + if (pstack_size(swapChainSupport.presentModes) == 0 && + presentMode != VK_PRESENT_MODE_MAILBOX_KHR) { + presentMode = VK_PRESENT_MODE_FIFO_KHR; + } + + pstack_destroy(swapChainSupport.presentModes); + VkExtent2D extent = _chooseSwapExtent(vk, &swapChainSupport.capabilities); uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1; @@ -935,7 +913,7 @@ _findSupportedFormat(const vks_context vk, } VKSDEF VkFormat -findDepthFormat(const vks_context vk) +vks_find_depth_format(const vks_context vk) { VkFormat formats[] = { VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, @@ -949,11 +927,15 @@ findDepthFormat(const vks_context vk) VKSDEF void _vulkan_create_depth_resources(vks_context *vk) { - VkFormat depth_format = findDepthFormat(*vk); - vks_create_image(*vk, vk->swapchain.extent.width, vk->swapchain.extent.height, 1, - depth_format, VK_IMAGE_TILING_OPTIMAL, + VkFormat depth_format = vks_find_depth_format(*vk); + vks_create_image(*vk, + vk->swapchain.extent.width, + vk->swapchain.extent.height, + 1, + depth_format, + VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, - VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, vk->msaa_samples, + vk->msaa_samples, &vk->depth_image); vk->depth_image.view = vks_create_image_view(*vk, vk->depth_image.handle, depth_format, VK_IMAGE_ASPECT_DEPTH_BIT, 1); @@ -978,11 +960,15 @@ _vulkan_create_color_resources(vks_context *vk) { VkFormat colorFormat = vk->swapchain.image_format; - vks_create_image(*vk, vk->swapchain.extent.width, vk->swapchain.extent.height, 1, - colorFormat, VK_IMAGE_TILING_OPTIMAL, + vks_create_image(*vk, + vk->swapchain.extent.width, + vk->swapchain.extent.height, + 1, + colorFormat, + VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, - VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, vk->msaa_samples, + vk->msaa_samples, &vk->color_image); vk->color_image.view = vks_create_image_view(*vk, vk->color_image.handle, colorFormat, VK_IMAGE_ASPECT_COLOR_BIT, 1); } @@ -1068,6 +1054,21 @@ vks_create_vulkan_context(vks_context* vk) _vulkan_create_depth_resources(vk); } + +VKSDEF void +vks_destroy_vulkan_context(vks_context* vk) +{ + vks_cleanup_swapchain(*vk); + + vmaDestroyAllocator(vk->allocator); + + vkDestroyDevice(vk->device, NULL); + vkDestroySurfaceKHR(vk->instance, vk->surface, NULL); + + vkDestroyInstance(vk->instance, NULL); +} + + VKSDEF VkImageView vks_create_image_view(const vks_context vk, VkImage image, @@ -1247,7 +1248,6 @@ vks_transition_image_layout(const vks_context vk, const vks_transition_image_lay barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; barrier.image = info->image; - // barrier.subresourceRange.aspectMask = if (info->newLayout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL) { barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT; @@ -1285,7 +1285,6 @@ vks_create_image(const vks_context vk, const VkFormat format, const VkImageTiling tiling, const VkImageUsageFlags usage, - const VkMemoryPropertyFlags properties, const VkSampleCountFlagBits numSamples, vks_image* image) { @@ -1319,7 +1318,6 @@ VKSDEF void vks_create_buffer(const vks_context vk, const VkDeviceSize size, const VkBufferUsageFlags usage, - const VkMemoryPropertyFlags properties, vks_buffer* buffer) { VkBufferCreateInfo bufferInfo = { 0 }; @@ -1333,21 +1331,7 @@ vks_create_buffer(const vks_context vk, allocInfo.usage = VMA_MEMORY_USAGE_AUTO; allocInfo.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT; - //if (properties & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) - vmaCreateBuffer(vk.allocator, &bufferInfo, &allocInfo, &buffer->handle, &buffer->allocation, NULL); - - /* VkMemoryRequirements mem_requirements; */ - /* vkGetBufferMemoryRequirements(vk.device, buffer->handle, &mem_requirements); */ - - /* VkMemoryAllocateInfo allocInfo = { 0 }; */ - /* allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; */ - /* allocInfo.allocationSize = mem_requirements.size; */ - /* allocInfo.memoryTypeIndex = */ - /* _find_memory_type(vk, mem_requirements.memoryTypeBits, properties); */ - - /* // TODO: group allocations etc... (allocations limited by hardware) */ - /* VK_CHECK(vkAllocateMemory(vk.device, &allocInfo, NULL, &buffer->memory)); */ - /* VK_CHECK(vkBindBufferMemory(vk.device, buffer->handle, buffer->memory, 0)); */ } + #endif /* VKSETUP_IMPLEMENTATION */ -- cgit v1.2.3