diff --git a/graphics/src/rse_vulkan_image.c b/graphics/src/rse_vulkan_image.c index 1e313a8e..2d40718c 100644 --- a/graphics/src/rse_vulkan_image.c +++ b/graphics/src/rse_vulkan_image.c @@ -1,6 +1,7 @@ #include "rse_vulkan_image.h" #include +#include "src/vulkan_pipeline.h" #include "utilities/file_utils.h" #include "utilities/logger.h" #include "utilities/rse_commons.h" @@ -43,6 +44,7 @@ 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; +struct rse_vulkan_image_t g_color_image; VkSampler g_sampler; @@ -105,7 +107,13 @@ static rse_err_t find_supported_format(const VkFormat* candidates, return VULKAN_ERROR_FORMAT_NOT_FOUND; } -static rse_err_t create_image(struct rse_vulkan_image_t* image, uint32_t width, uint32_t height, VkFormat format, VkImageUsageFlags usage, VmaAllocationCreateFlags allocationFlags) +static rse_err_t create_image(struct rse_vulkan_image_t* image, + uint32_t width, + uint32_t height, + VkFormat format, + VkSampleCountFlagBits maa_samples, + VkImageUsageFlags usage, + VmaAllocationCreateFlags allocationFlags) { VkExtent3D image_extent = {0}; VkImageCreateInfo image_info = {0}; @@ -123,7 +131,7 @@ static rse_err_t create_image(struct rse_vulkan_image_t* image, uint32_t width, 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.samples = maa_samples; image_info.tiling = VK_IMAGE_TILING_OPTIMAL; image_info.usage = usage; image_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE; //TODO: Change if used by more than one queue family @@ -183,10 +191,26 @@ static rse_err_t create_image_view(struct rse_vulkan_image_t* image, VkFormat fo return RSE_ERROR_NO_ERROR; } -// static rse_err_t create_color_resource() -// { -// return RSE_ERROR_NO_ERROR; -// } +static rse_err_t create_color_resource() +{ + rse_err_t status = RSE_ERROR_NO_ERROR; + VkFormat color_format = IMAGE_FORMAT; + VkSurfaceCapabilitiesKHR physical_device_surface_capabilities; + + vkGetPhysicalDeviceSurfaceCapabilitiesKHR(g_vulkan_state.physical_device, + g_vulkan_state.surface, + &physical_device_surface_capabilities); + + STATUS_CHECK(create_image(&g_color_image, + physical_device_surface_capabilities.currentExtent.width, + physical_device_surface_capabilities.currentExtent.height, + color_format, + VK_SAMPLE_COUNT_16_BIT, + VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, 0U)); + STATUS_CHECK(create_image_view(&g_color_image, color_format, VK_IMAGE_ASPECT_COLOR_BIT)); + + return status; +} static rse_err_t create_depth_resources() @@ -203,8 +227,11 @@ static rse_err_t create_depth_resources() STATUS_CHECK(create_image(&g_depth_image, physical_device_surface_capabilities.currentExtent.width, - physical_device_surface_capabilities.currentExtent.height, - depth_format, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, 0U)); + physical_device_surface_capabilities.currentExtent.height, + depth_format, + VK_SAMPLE_COUNT_16_BIT, + VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, + 0U)); STATUS_CHECK(create_image_view(&g_depth_image, depth_format, VK_IMAGE_ASPECT_DEPTH_BIT)); return status; @@ -273,6 +300,7 @@ static rse_err_t create_textured_image(uint32_t width, width, height, format, + VK_SAMPLE_COUNT_1_BIT, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT)); @@ -370,6 +398,7 @@ rse_err_t init_vulkan_images() STATUS_CHECK(create_sampler()); STATUS_CHECK(create_depth_resources()); + STATUS_CHECK(create_color_resource()); return status; } @@ -415,7 +444,9 @@ void rse_destroy_textures() } vkDestroyImageView(g_vulkan_state.device, g_depth_image.image_view, NULL); + vkDestroyImageView(g_vulkan_state.device, g_color_image.image_view, NULL); vmaDestroyImage(g_vulkan_state.allocator, g_depth_image.image, g_depth_image.allocation); + vmaDestroyImage(g_vulkan_state.allocator, g_color_image.image, g_color_image.allocation); } uint8_t image_exists(uint8_t image_id) { @@ -432,6 +463,10 @@ VkImageView rse_get_depth_image_view() return g_depth_image.image_view; } +VkImageView rse_get_color_image_view() +{ + return g_color_image.image_view; +} VkSampler rse_get_texture_sampler() { diff --git a/graphics/src/rse_vulkan_image.h b/graphics/src/rse_vulkan_image.h index 5dfe5be3..037cd977 100644 --- a/graphics/src/rse_vulkan_image.h +++ b/graphics/src/rse_vulkan_image.h @@ -76,10 +76,12 @@ uint8_t image_exists(uint8_t image_id); * * @return size_t Texture count */ -size_t get_textures_count(); +size_t get_textures_count(void); rse_err_t find_depth_format(VkFormat* found_format); -VkImageView rse_get_depth_image_view(); +VkImageView rse_get_depth_image_view(void); + +VkImageView rse_get_color_image_view(void); #endif /* RSE_TEXTURE_H */ diff --git a/graphics/src/vulkan_pipeline.c b/graphics/src/vulkan_pipeline.c index f811bddd..9432ae6b 100644 --- a/graphics/src/vulkan_pipeline.c +++ b/graphics/src/vulkan_pipeline.c @@ -4,6 +4,7 @@ #include "utilities/file_utils.h" #include "utilities/logger.h" #include "utilities/rse_errors_common.h" +#include "vulkan/vulkan_core.h" #include "vulkan_errors.h" #include "rse_vulkan_image.h" #include "vulkan_descriptors.h" @@ -21,7 +22,6 @@ enum shader_stage_t FRAGMENTS }; -#define IMAGE_FORMAT VK_FORMAT_B8G8R8A8_SRGB; uint32_t g_swapchain_images_count = 0U; @@ -194,9 +194,11 @@ static rse_err_t create_swapchain_image_views() static rse_err_t create_render_pass() { rse_err_t status = RSE_ERROR_NO_ERROR; - VkAttachmentDescription attachments[2] = {}; + VkAttachmentDescription attachments[3] = {}; VkAttachmentDescription color_attachment = {}; VkAttachmentReference color_attachment_ref = {}; + VkAttachmentDescription color_attachment_resolve = {}; + VkAttachmentReference color_attachment_resolve_ref = {}; VkSubpassDescription subpass = {}; VkRenderPassCreateInfo renderpass_info = {}; VkAttachmentDescription depth_attachment = {}; @@ -205,7 +207,7 @@ static rse_err_t create_render_pass() STATUS_CHECK(find_depth_format(&depth_attachment.format)); - depth_attachment.samples = VK_SAMPLE_COUNT_1_BIT; + depth_attachment.samples = VK_SAMPLE_COUNT_16_BIT; depth_attachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; depth_attachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; depth_attachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; @@ -218,24 +220,38 @@ static rse_err_t create_render_pass() color_attachment.flags = 0U; color_attachment.format = IMAGE_FORMAT; - color_attachment.samples = VK_SAMPLE_COUNT_1_BIT; + color_attachment.samples = VK_SAMPLE_COUNT_16_BIT; color_attachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; color_attachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE; color_attachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; color_attachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; color_attachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; - color_attachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; + color_attachment.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; color_attachment_ref.attachment = 0; color_attachment_ref.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; + color_attachment_resolve.format = IMAGE_FORMAT; + color_attachment_resolve.samples = VK_SAMPLE_COUNT_1_BIT; + color_attachment_resolve.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + color_attachment_resolve.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; + color_attachment_resolve.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + color_attachment_resolve.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; + color_attachment_resolve.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; + color_attachment_resolve.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; + + color_attachment_resolve_ref.attachment = 2; + color_attachment_resolve_ref.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; + subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; subpass.colorAttachmentCount = 1; subpass.pColorAttachments = &color_attachment_ref; subpass.pDepthStencilAttachment = &depth_attachment_ref; + subpass.pResolveAttachments = &color_attachment_resolve_ref; attachments[0] = color_attachment; attachments[1] = depth_attachment; + attachments[2] = color_attachment_resolve; dependency.srcSubpass = VK_SUBPASS_EXTERNAL; dependency.dstSubpass = 0; @@ -244,9 +260,8 @@ static rse_err_t create_render_pass() dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT; dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; - renderpass_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; - renderpass_info.attachmentCount = 2; + renderpass_info.attachmentCount = 3; renderpass_info.pAttachments = attachments; renderpass_info.subpassCount = 1; renderpass_info.pSubpasses = &subpass; @@ -595,7 +610,7 @@ static void fill_multisampling_state_info(VkPipelineMultisampleStateCreateInfo* multisampling->sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; multisampling->pNext = NULL; multisampling->flags = 0U; - multisampling->rasterizationSamples = VK_SAMPLE_COUNT_1_BIT; + multisampling->rasterizationSamples = VK_SAMPLE_COUNT_16_BIT; multisampling->sampleShadingEnable = VK_FALSE; multisampling->minSampleShading = 1.0f; multisampling->pSampleMask = NULL; @@ -734,12 +749,12 @@ static rse_err_t create_framebuffers() g_vulkan_state.swapchain_framebuffers = malloc(sizeof(VkFramebuffer) * g_swapchain_images_count); for (size_t i = 0; i < g_swapchain_images_count; i++) { - VkImageView attachments[] = {g_vulkan_state.swapchain_image_views[i], rse_get_depth_image_view()}; + VkImageView attachments[] = {rse_get_color_image_view(), rse_get_depth_image_view(), g_vulkan_state.swapchain_image_views[i]}; VkFramebufferCreateInfo framebufferInfo = {}; framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; framebufferInfo.renderPass = g_vulkan_state.render_pass; - framebufferInfo.attachmentCount = 2; + framebufferInfo.attachmentCount = 3; framebufferInfo.pAttachments = attachments; framebufferInfo.width = g_vulkan_state.swapchain_extent.width; framebufferInfo.height = g_vulkan_state.swapchain_extent.height; diff --git a/graphics/src/vulkan_pipeline.h b/graphics/src/vulkan_pipeline.h index 79819dbf..91fe5a0c 100644 --- a/graphics/src/vulkan_pipeline.h +++ b/graphics/src/vulkan_pipeline.h @@ -3,6 +3,7 @@ #include "utilities/rse_commons.h" +#define IMAGE_FORMAT VK_FORMAT_B8G8R8A8_SRGB; /** * @brief Create a graphics pipeline *