Add depth buffering

This commit is contained in:
Piotr Krygier committed 2023-10-03 13:22:24 +02:00
1 parent 548ed57969
commit d328e38761
9 files changed
+323 -39

No files matched your search

+138
View File
@@ -37,8 +37,11 @@ struct rse_vulkan_image_t
extern struct rse_vulkan_state_t g_vulkan_state;
struct rse_vulkan_image_t g_texture_images[RSE_MAX_IMAGE_COUNT];
struct rse_vulkan_image_t g_depth_image;
VkSampler g_sampler;
static uint8_t create_sampler()
{
VkSamplerCreateInfo create_info = {0};
@@ -73,6 +76,117 @@ static uint8_t create_sampler()
return VULKAN_ERROR_NO_ERROR;
}
// static uint8_t has_stencil_component(VkFormat format) {
// return format == VK_FORMAT_D32_SFLOAT_S8_UINT || format == VK_FORMAT_D24_UNORM_S8_UINT;
// }
static VkFormat find_supported_format(const VkFormat* candidates,
size_t candidates_count,
VkImageTiling tiling,
VkFormatFeatureFlags features)
{
size_t i = 0U;
for (i = 0U; i < candidates_count; ++i) {
VkFormatProperties props;
vkGetPhysicalDeviceFormatProperties(g_vulkan_state.physical_device, candidates[i], &props);
if (tiling == VK_IMAGE_TILING_LINEAR && (props.linearTilingFeatures & features) == features) {
return candidates[i];
} else if (tiling == VK_IMAGE_TILING_OPTIMAL && (props.optimalTilingFeatures & features) == features) {
return candidates[i];
}
}
LOGE("Failed to find correct format");
//FIXME: Add error handling
return 0;
}
static uint8_t create_depth_resources()
{
uint8_t status = VULKAN_ERROR_NO_ERROR;
VkFormat depth_format = {0};
VkExtent3D image_extent = {0};
VkImageCreateInfo image_info = {0};
VmaAllocationCreateInfo alloc_create_info = {0};
VkImageViewCreateInfo image_view_create_info = {0};
VkSurfaceCapabilitiesKHR physical_device_surface_capabilities;
depth_format = find_depth_format();
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(g_vulkan_state.physical_device,
g_vulkan_state.surface,
&physical_device_surface_capabilities);
/*TODO: Remove boilerplate code */
image_extent.width = physical_device_surface_capabilities.currentExtent.width;
image_extent.height = physical_device_surface_capabilities.currentExtent.height;
image_extent.depth = 1U;
image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
image_info.pNext = NULL;
image_info.flags = 0U;
image_info.imageType = VK_IMAGE_TYPE_2D;
image_info.format = depth_format;
image_info.extent = image_extent;
image_info.mipLevels = 1U;
image_info.arrayLayers = 1U;
image_info.samples = VK_SAMPLE_COUNT_1_BIT; //TODO: Enable multisampling
image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
image_info.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
image_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE; //TODO: Change if used by more than one queue family
image_info.queueFamilyIndexCount = 0U;
image_info.pQueueFamilyIndices = NULL;
image_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
alloc_create_info.flags = 0U;
alloc_create_info.usage = VMA_MEMORY_USAGE_AUTO;
alloc_create_info.requiredFlags = 0U;
alloc_create_info.preferredFlags = 0U;
alloc_create_info.memoryTypeBits = 0U;
alloc_create_info.pool = NULL;
alloc_create_info.pUserData = NULL;
alloc_create_info.priority = 1.0f;
if (VK_SUCCESS != vmaCreateImage(g_vulkan_state.allocator,
&image_info,
&alloc_create_info,
&g_depth_image.image,
&g_depth_image.allocation,
&g_depth_image.allocation_info)) {
LOGF("Failed to create image");
return -1;
}
image_view_create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
image_view_create_info.pNext = NULL;
image_view_create_info.flags = 0U;
image_view_create_info.image = g_depth_image.image;
image_view_create_info.viewType = VK_IMAGE_VIEW_TYPE_2D; /* 2D Texture */
image_view_create_info.format = depth_format;
image_view_create_info.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
image_view_create_info.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
image_view_create_info.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
image_view_create_info.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
image_view_create_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
image_view_create_info.subresourceRange.baseMipLevel = 0U;
image_view_create_info.subresourceRange.levelCount = 1U;
image_view_create_info.subresourceRange.baseArrayLayer = 0U;
image_view_create_info.subresourceRange.layerCount = 1U;
if (VK_SUCCESS != vkCreateImageView(g_vulkan_state.device,
&image_view_create_info,
NULL,
&g_depth_image.image_view)) {
LOGF(vulkan_error_messages[VULKAN_SWAPCHAIN_IMVIEW_CREATION_FAILED]);
return VULKAN_ERROR_SWAPCHAIN_IMVIEW_CREATION_FAILED;
}
return status;
}
static VkDeviceSize format_to_pixel_size(VkFormat format)
{
switch (format)
@@ -278,6 +392,7 @@ uint8_t init_vulkan_images()
// memset(g_texture_images, 0, sizeof(struct rse_vulkan_image_t) * RSE_MAX_IMAGE_COUNT);
STATUS_CHECK(create_sampler());
STATUS_CHECK(create_depth_resources());
return status;
}
@@ -323,6 +438,9 @@ void rse_destroy_textures()
vmaDestroyImage(g_vulkan_state.allocator, g_texture_images[i].image, g_texture_images[i].allocation);
}
}
vkDestroyImageView(g_vulkan_state.device, g_depth_image.image_view, NULL);
vmaDestroyImage(g_vulkan_state.allocator, g_depth_image.image, g_depth_image.allocation);
}
int8_t image_exists(uint8_t image_id) {
@@ -334,6 +452,12 @@ VkImageView rse_get_image_view(size_t image_id)
return g_texture_images[image_id].image_view;
}
VkImageView rse_get_depth_image_view()
{
return g_depth_image.image_view;
}
VkSampler rse_get_texture_sampler()
{
return g_sampler;
@@ -351,4 +475,18 @@ size_t get_textures_count()
}
return count;
}
VkFormat find_depth_format()
{
#define formats_count 3
VkFormat formats[formats_count] = {VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT};
return find_supported_format(
formats,
formats_count,
VK_IMAGE_TILING_OPTIMAL,
VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT
);
#undef formats_count
}