Add depth attachment

This commit is contained in:
2026-05-25 23:25:55 +01:00
parent 2283400a93
commit 401cab7da0
+71 -3
View File
@@ -31,12 +31,14 @@ enum ExitCode {
EXIT_CODE_NO_PHYSICAL_DEVICE_SUPPORT, EXIT_CODE_NO_PHYSICAL_DEVICE_SUPPORT,
EXIT_CODE_NO_QUEUE_FAMILIES, EXIT_CODE_NO_QUEUE_FAMILIES,
EXIT_CODE_NO_SUITABLE_QUEUE_FAMILY, EXIT_CODE_NO_SUITABLE_QUEUE_FAMILY,
EXIT_CODE_NO_SUITABLE_DEPTH_FORMAT,
}; };
typedef VkPhysicalDevice *VkPhysicalDeviceArray; typedef VkPhysicalDevice *VkPhysicalDeviceArray;
typedef VkQueueFamilyProperties2 *VkQueueFamilyProperties2Array; typedef VkQueueFamilyProperties2 *VkQueueFamilyProperties2Array;
typedef VkImage *VkImageArray; typedef VkImage *VkImageArray;
typedef VkImageView *VkImageViewArray; typedef VkImageView *VkImageViewArray;
typedef VkFormat *VkFormatArray;
wapp_intern inline void check(VkResult result); wapp_intern inline void check(VkResult result);
wapp_intern inline void check_swapchain(VkResult result); wapp_intern inline void check_swapchain(VkResult result);
@@ -54,11 +56,20 @@ wapp_intern VkDevice device = VK_NULL_HANDLE;
wapp_intern VkQueue queue = VK_NULL_HANDLE; wapp_intern VkQueue queue = VK_NULL_HANDLE;
wapp_intern VmaAllocator allocator = VK_NULL_HANDLE; wapp_intern VmaAllocator allocator = VK_NULL_HANDLE;
wapp_intern glm::ivec2 window_size = {}; wapp_intern glm::ivec2 window_size = {};
// NOTE (Abdelrahman): The following three variables are better queried from the physical device
// to find the different supported values. For the purposes of this tutorial, we're using defaults
// that are guaranteed by the spec to be available on any valid implementation.
wapp_intern VkFormat image_format = VK_FORMAT_B8G8R8A8_SRGB; wapp_intern VkFormat image_format = VK_FORMAT_B8G8R8A8_SRGB;
wapp_intern VkColorSpaceKHR colorspace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
wapp_intern VkPresentModeKHR present_mode = VK_PRESENT_MODE_FIFO_KHR;
wapp_intern VkSwapchainKHR swapchain = VK_NULL_HANDLE; wapp_intern VkSwapchainKHR swapchain = VK_NULL_HANDLE;
wapp_intern u32 swapchain_image_count = 0; wapp_intern u32 swapchain_image_count = 0;
wapp_intern VkImageArray swapchain_images = nullptr; wapp_intern VkImageArray swapchain_images = nullptr;
wapp_intern VkImageViewArray swapchain_views = nullptr; wapp_intern VkImageViewArray swapchain_views = nullptr;
wapp_intern VkFormat depth_format = VK_FORMAT_UNDEFINED;
wapp_intern VkImage depth_image = VK_NULL_HANDLE;
wapp_intern VkImageView depth_view = VK_NULL_HANDLE;
wapp_intern VmaAllocation depth_allocation = VK_NULL_HANDLE;
int main() { int main() {
// {{{ Initialisation // {{{ Initialisation
@@ -272,7 +283,8 @@ int main() {
vk_functions.vkCreateImage = vkCreateImage; vk_functions.vkCreateImage = vkCreateImage;
VmaAllocatorCreateInfo allocator_create_info = {}; VmaAllocatorCreateInfo allocator_create_info = {};
allocator_create_info.flags = VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT; allocator_create_info.flags = VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT |
VMA_ALLOCATOR_CREATE_EXT_MEMORY_PRIORITY_BIT;
allocator_create_info.instance = instance; allocator_create_info.instance = instance;
allocator_create_info.physicalDevice = physical_device; allocator_create_info.physicalDevice = physical_device;
allocator_create_info.device = device; allocator_create_info.device = device;
@@ -298,7 +310,7 @@ int main() {
swapchain_create_info.surface = surface; swapchain_create_info.surface = surface;
swapchain_create_info.minImageCount = surface_caps.minImageCount; swapchain_create_info.minImageCount = surface_caps.minImageCount;
swapchain_create_info.imageFormat = image_format; swapchain_create_info.imageFormat = image_format;
swapchain_create_info.imageColorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR; swapchain_create_info.imageColorSpace = colorspace;
swapchain_create_info.imageExtent = swapchain_extent; swapchain_create_info.imageExtent = swapchain_extent;
swapchain_create_info.imageArrayLayers = 1; swapchain_create_info.imageArrayLayers = 1;
swapchain_create_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; swapchain_create_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
@@ -307,7 +319,7 @@ int main() {
swapchain_create_info.pQueueFamilyIndices = &queue_family_index; swapchain_create_info.pQueueFamilyIndices = &queue_family_index;
swapchain_create_info.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; swapchain_create_info.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
swapchain_create_info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; swapchain_create_info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
swapchain_create_info.presentMode = VK_PRESENT_MODE_FIFO_KHR; swapchain_create_info.presentMode = present_mode;
check(vkCreateSwapchainKHR(device, &swapchain_create_info, nullptr, &swapchain)); check(vkCreateSwapchainKHR(device, &swapchain_create_info, nullptr, &swapchain));
// }}} // }}}
@@ -341,6 +353,60 @@ int main() {
// }}} // }}}
// }}} // }}}
// {{{ Depth Attachment Setup
// {{{ Check Supported Depth Formats
// NOTE (Abdelrahman): Favour 24-bit format for performance
VkFormatArray depth_formats = wapp_array(VkFormat, VK_FORMAT_D24_UNORM_S8_UINT, VK_FORMAT_D32_SFLOAT_S8_UINT);
for (u32 i = 0; i < wapp_array_count(depth_formats); ++i) {
VkFormatProperties2 format_properties = { VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2 };
vkGetPhysicalDeviceFormatProperties2(physical_device, depth_formats[i], &format_properties);
if (format_properties.formatProperties.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) {
depth_format = depth_formats[i];
break;
}
}
check(depth_format != VK_FORMAT_UNDEFINED, EXIT_CODE_NO_SUITABLE_DEPTH_FORMAT);
// }}}
// {{{ Create Depth Image
VkImageCreateInfo depth_image_create_info = {};
depth_image_create_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
depth_image_create_info.imageType = VK_IMAGE_TYPE_2D;
depth_image_create_info.format = depth_format;
depth_image_create_info.extent = {swapchain_extent.width, swapchain_extent.height, 1};
depth_image_create_info.mipLevels = 1;
depth_image_create_info.arrayLayers = 1;
depth_image_create_info.samples = VK_SAMPLE_COUNT_1_BIT;
depth_image_create_info.tiling = VK_IMAGE_TILING_OPTIMAL;
depth_image_create_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
depth_image_create_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
depth_image_create_info.queueFamilyIndexCount = 1;
depth_image_create_info.pQueueFamilyIndices = &queue_family_index;
depth_image_create_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
VmaAllocationCreateInfo depth_alloc_create_info = {};
depth_alloc_create_info.flags = VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
depth_alloc_create_info.usage = VMA_MEMORY_USAGE_AUTO;
check(vmaCreateImage(allocator, &depth_image_create_info, &depth_alloc_create_info, &depth_image,
&depth_allocation, nullptr));
// }}}
// {{{ Create Depth Image View
VkImageViewCreateInfo depth_view_create_info = {};
depth_view_create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
depth_view_create_info.image = depth_image;
depth_view_create_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
depth_view_create_info.format = depth_format;
depth_view_create_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
depth_view_create_info.subresourceRange.levelCount = 1;
depth_view_create_info.subresourceRange.layerCount = 1;
check(vkCreateImageView(device, &depth_view_create_info, nullptr, &depth_view));
// }}}
// }}}
// {{{ Render Loop // {{{ Render Loop
SDL_Event event = {}; SDL_Event event = {};
while (running) { while (running) {
@@ -360,6 +426,8 @@ int main() {
// }}} // }}}
// {{{ Cleanup // {{{ Cleanup
vkDestroyImageView(device, depth_view, nullptr);
vmaDestroyImage(allocator, depth_image, depth_allocation);
for (u32 i = 0; i < swapchain_image_count; ++i) { for (u32 i = 0; i < swapchain_image_count; ++i) {
vkDestroyImageView(device, swapchain_views[i], nullptr); vkDestroyImageView(device, swapchain_views[i], nullptr);
} }