From eb89831d1471e78413145ca7159c72b42d33b376 Mon Sep 17 00:00:00 2001 From: tylermurphy534 Date: Mon, 26 Sep 2022 00:06:51 -0400 Subject: MSAA --- engine/xe_swap_chain.cpp | 105 ++++++++++++++++++++++++----------------------- 1 file changed, 54 insertions(+), 51 deletions(-) (limited to 'engine/xe_swap_chain.cpp') diff --git a/engine/xe_swap_chain.cpp b/engine/xe_swap_chain.cpp index 7044226..67248f4 100755 --- a/engine/xe_swap_chain.cpp +++ b/engine/xe_swap_chain.cpp @@ -20,6 +20,7 @@ void SwapChain::init() { createSwapChain(); createImageViews(); createRenderPass(); + createColorResources(); createDepthResources(); createFramebuffers(); createSyncObjects(); @@ -37,6 +38,12 @@ SwapChain::~SwapChain() { swapChain = nullptr; } + for (int i = 0; i < colorImages.size(); i++) { + vkDestroyImageView(device.device(), colorImageViews[i], nullptr); + vkDestroyImage(device.device(), colorImages[i], nullptr); + vkFreeMemory(device.device(), colorImageMemorys[i], nullptr); + } + for (int i = 0; i < depthImages.size(); i++) { vkDestroyImageView(device.device(), depthImageViews[i], nullptr); vkDestroyImage(device.device(), depthImages[i], nullptr); @@ -202,9 +209,24 @@ void SwapChain::createImageViews() { } void SwapChain::createRenderPass() { + + VkAttachmentDescription colorAttachment = {}; + colorAttachment.format = getSwapChainImageFormat(); + colorAttachment.samples = device.getSamples(); + colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; + colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE; + colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; + colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; + colorAttachment.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; + + VkAttachmentReference colorAttachmentRef = {}; + colorAttachmentRef.attachment = 0; + colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; + VkAttachmentDescription depthAttachment{}; depthAttachment.format = findDepthFormat(); - depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT; + depthAttachment.samples = device.getSamples(); depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; @@ -216,25 +238,26 @@ void SwapChain::createRenderPass() { depthAttachmentRef.attachment = 1; depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; - VkAttachmentDescription colorAttachment = {}; - colorAttachment.format = getSwapChainImageFormat(); - colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT; - colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; - colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE; - colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; - colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; - colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; - colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; + VkAttachmentDescription colorAttachmentResolve{}; + colorAttachmentResolve.format = getSwapChainImageFormat(); + colorAttachmentResolve.samples = VK_SAMPLE_COUNT_1_BIT; + colorAttachmentResolve.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + colorAttachmentResolve.storeOp = VK_ATTACHMENT_STORE_OP_STORE; + colorAttachmentResolve.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + colorAttachmentResolve.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; + colorAttachmentResolve.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; + colorAttachmentResolve.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; - VkAttachmentReference colorAttachmentRef = {}; - colorAttachmentRef.attachment = 0; - colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; + VkAttachmentReference colorAttachmentResolveRef{}; + colorAttachmentResolveRef.attachment = 2; + colorAttachmentResolveRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; VkSubpassDescription subpass = {}; subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; subpass.colorAttachmentCount = 1; subpass.pColorAttachments = &colorAttachmentRef; subpass.pDepthStencilAttachment = &depthAttachmentRef; + subpass.pResolveAttachments = &colorAttachmentResolveRef; VkSubpassDependency dependency = {}; dependency.srcSubpass = VK_SUBPASS_EXTERNAL; @@ -247,7 +270,7 @@ void SwapChain::createRenderPass() { dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; - std::array attachments = {colorAttachment, depthAttachment}; + std::array attachments = {colorAttachment, depthAttachment, colorAttachmentResolve}; VkRenderPassCreateInfo renderPassInfo = {}; renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; renderPassInfo.attachmentCount = static_cast(attachments.size()); @@ -265,7 +288,7 @@ void SwapChain::createRenderPass() { void SwapChain::createFramebuffers() { swapChainFramebuffers.resize(imageCount()); for (size_t i = 0; i < imageCount(); i++) { - std::array attachments = {swapChainImageViews[i], depthImageViews[i]}; + std::array attachments = {colorImageViews[i], depthImageViews[i], swapChainImageViews[i]}; VkExtent2D swapChainExtent = getSwapChainExtent(); VkFramebufferCreateInfo framebufferInfo = {}; @@ -287,6 +310,20 @@ void SwapChain::createFramebuffers() { } } +void SwapChain::createColorResources() { + VkFormat colorFormat = getSwapChainImageFormat(); + VkExtent2D swapChainExtent = getSwapChainExtent(); + + colorImages.resize(imageCount()); + colorImageMemorys.resize(imageCount()); + colorImageViews.resize(imageCount()); + + for (int i = 0; i < colorImages.size(); i++) { + Image::createImage(device, swapChainExtent.width, swapChainExtent.height, 1, device.getSamples(), colorFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, colorImages[i], colorImageMemorys[i]); + colorImageViews[i] = Image::createImageView(device, colorImages[i], colorFormat, VK_IMAGE_ASPECT_COLOR_BIT, 1); + } +} + void SwapChain::createDepthResources() { VkFormat depthFormat = findDepthFormat(); swapChainDepthFormat = depthFormat; @@ -297,42 +334,8 @@ void SwapChain::createDepthResources() { depthImageViews.resize(imageCount()); for (int i = 0; i < depthImages.size(); i++) { - VkImageCreateInfo imageInfo{}; - imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; - imageInfo.imageType = VK_IMAGE_TYPE_2D; - imageInfo.extent.width = swapChainExtent.width; - imageInfo.extent.height = swapChainExtent.height; - imageInfo.extent.depth = 1; - imageInfo.mipLevels = 1; - imageInfo.arrayLayers = 1; - imageInfo.format = depthFormat; - imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL; - imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; - imageInfo.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; - imageInfo.samples = VK_SAMPLE_COUNT_1_BIT; - imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; - imageInfo.flags = 0; - - device.createImageWithInfo( - imageInfo, - VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, - depthImages[i], - depthImageMemorys[i]); - - VkImageViewCreateInfo viewInfo{}; - viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; - viewInfo.image = depthImages[i]; - viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; - viewInfo.format = depthFormat; - viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT; - viewInfo.subresourceRange.baseMipLevel = 0; - viewInfo.subresourceRange.levelCount = 1; - viewInfo.subresourceRange.baseArrayLayer = 0; - viewInfo.subresourceRange.layerCount = 1; - - if (vkCreateImageView(device.device(), &viewInfo, nullptr, &depthImageViews[i]) != VK_SUCCESS) { - throw std::runtime_error("failed to create texture image view!"); - } + Image::createImage(device, swapChainExtent.width, swapChainExtent.height, 1, device.getSamples(), depthFormat, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, depthImages[i], depthImageMemorys[i]); + depthImageViews[i] = Image::createImageView(device, depthImages[i], depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT, 1); } } -- cgit v1.2.3-freya