Multisampling

This commit is contained in:
Mars 2024-10-11 21:14:36 -04:00
parent 8bb2fafe01
commit 7dafdfffa6
Signed by untrusted user: pupbrained
GPG key ID: 874E22DF2F9DFCB5

View file

@ -101,6 +101,7 @@ class VulkanApp {
vk::UniqueSurfaceKHR mSurface; vk::UniqueSurfaceKHR mSurface;
vk::PhysicalDevice mPhysicalDevice; vk::PhysicalDevice mPhysicalDevice;
vk::SampleCountFlagBits mMsaaSamples = vk::SampleCountFlagBits::e1;
vk::UniqueDevice mDevice; vk::UniqueDevice mDevice;
vk::Queue mGraphicsQueue; vk::Queue mGraphicsQueue;
@ -120,6 +121,10 @@ class VulkanApp {
vk::UniqueCommandPool mCommandPool; vk::UniqueCommandPool mCommandPool;
vk::UniqueImage mColorImage;
vk::UniqueDeviceMemory mColorImageMemory;
vk::UniqueImageView mColorImageView;
vk::UniqueImage mDepthImage; vk::UniqueImage mDepthImage;
vk::UniqueDeviceMemory mDepthImageMemory; vk::UniqueDeviceMemory mDepthImageMemory;
vk::UniqueImageView mDepthImageView; vk::UniqueImageView mDepthImageView;
@ -219,6 +224,7 @@ class VulkanApp {
createDescriptorSetLayout(); createDescriptorSetLayout();
createGraphicsPipeline(); createGraphicsPipeline();
createCommandPool(); createCommandPool();
createColorResources();
createDepthResources(); createDepthResources();
createFramebuffers(); createFramebuffers();
createTextureImage(); createTextureImage();
@ -265,6 +271,7 @@ class VulkanApp {
createSwapChain(); createSwapChain();
createImageViews(); createImageViews();
createColorResources();
createDepthResources(); createDepthResources();
createFramebuffers(); createFramebuffers();
} }
@ -350,6 +357,7 @@ class VulkanApp {
if (isDeviceSuitable(device)) { if (isDeviceSuitable(device)) {
mPhysicalDevice = device; mPhysicalDevice = device;
mMsaaSamples = getMaxUsableSampleCount();
break; break;
} }
} }
@ -444,19 +452,17 @@ class VulkanApp {
} }
fn createRenderPass() -> void { fn createRenderPass() -> void {
vk::AttachmentDescription colorAttachment { vk::AttachmentDescription colorAttachment { .format = mSwapChainImageFormat,
.format = mSwapChainImageFormat, .samples = mMsaaSamples,
.samples = vk::SampleCountFlagBits::e1,
.loadOp = vk::AttachmentLoadOp::eClear, .loadOp = vk::AttachmentLoadOp::eClear,
.storeOp = vk::AttachmentStoreOp::eStore, .storeOp = vk::AttachmentStoreOp::eStore,
.stencilLoadOp = vk::AttachmentLoadOp::eDontCare, .stencilLoadOp = vk::AttachmentLoadOp::eDontCare,
.stencilStoreOp = vk::AttachmentStoreOp::eDontCare, .stencilStoreOp = vk::AttachmentStoreOp::eDontCare,
.initialLayout = vk::ImageLayout::eUndefined, .initialLayout = vk::ImageLayout::eUndefined,
.finalLayout = vk::ImageLayout::ePresentSrcKHR, .finalLayout = vk::ImageLayout::eColorAttachmentOptimal };
};
vk::AttachmentDescription depthAttachment { .format = findDepthFormat(), vk::AttachmentDescription depthAttachment { .format = findDepthFormat(),
.samples = vk::SampleCountFlagBits::e1, .samples = mMsaaSamples,
.loadOp = vk::AttachmentLoadOp::eClear, .loadOp = vk::AttachmentLoadOp::eClear,
.storeOp = vk::AttachmentStoreOp::eDontCare, .storeOp = vk::AttachmentStoreOp::eDontCare,
.stencilLoadOp = vk::AttachmentLoadOp::eDontCare, .stencilLoadOp = vk::AttachmentLoadOp::eDontCare,
@ -465,17 +471,28 @@ class VulkanApp {
.finalLayout = .finalLayout =
vk::ImageLayout::eDepthStencilAttachmentOptimal }; vk::ImageLayout::eDepthStencilAttachmentOptimal };
vk::AttachmentReference colorAttachmentRef { vk::AttachmentDescription colorAttachmentResolve { .format = mSwapChainImageFormat,
.attachment = 0, .samples = vk::SampleCountFlagBits::e1,
.layout = vk::ImageLayout::eColorAttachmentOptimal, .loadOp = vk::AttachmentLoadOp::eDontCare,
}; .storeOp = vk::AttachmentStoreOp::eStore,
.stencilLoadOp = vk::AttachmentLoadOp::eDontCare,
.stencilStoreOp = vk::AttachmentStoreOp::eDontCare,
.initialLayout = vk::ImageLayout::eUndefined,
.finalLayout = vk::ImageLayout::ePresentSrcKHR };
vk::AttachmentReference colorAttachmentRef { .attachment = 0,
.layout = vk::ImageLayout::eColorAttachmentOptimal };
vk::AttachmentReference depthAttachmentRef { .attachment = 1, vk::AttachmentReference depthAttachmentRef { .attachment = 1,
.layout = vk::ImageLayout::eDepthStencilAttachmentOptimal }; .layout = vk::ImageLayout::eDepthStencilAttachmentOptimal };
vk::AttachmentReference colorAttachmentResolveRef { .attachment = 2,
.layout = vk::ImageLayout::eColorAttachmentOptimal };
vk::SubpassDescription subpass { .pipelineBindPoint = vk::PipelineBindPoint::eGraphics, vk::SubpassDescription subpass { .pipelineBindPoint = vk::PipelineBindPoint::eGraphics,
.colorAttachmentCount = 1, .colorAttachmentCount = 1,
.pColorAttachments = &colorAttachmentRef, .pColorAttachments = &colorAttachmentRef,
.pResolveAttachments = &colorAttachmentResolveRef,
.pDepthStencilAttachment = &depthAttachmentRef }; .pDepthStencilAttachment = &depthAttachmentRef };
vk::SubpassDependency dependency { .srcSubpass = vk::SubpassExternal, vk::SubpassDependency dependency { .srcSubpass = vk::SubpassExternal,
@ -488,7 +505,9 @@ class VulkanApp {
.dstAccessMask = vk::AccessFlagBits::eColorAttachmentWrite | .dstAccessMask = vk::AccessFlagBits::eColorAttachmentWrite |
vk::AccessFlagBits::eDepthStencilAttachmentWrite }; vk::AccessFlagBits::eDepthStencilAttachmentWrite };
std::array<vk::AttachmentDescription, 2> attachments = { colorAttachment, depthAttachment }; std::array<vk::AttachmentDescription, 3> attachments = { colorAttachment,
depthAttachment,
colorAttachmentResolve };
vk::RenderPassCreateInfo renderPassInfo { .attachmentCount = static_cast<u32>(attachments.size()), vk::RenderPassCreateInfo renderPassInfo { .attachmentCount = static_cast<u32>(attachments.size()),
.pAttachments = attachments.data(), .pAttachments = attachments.data(),
@ -569,8 +588,7 @@ class VulkanApp {
.depthBiasEnable = vk::False, .depthBiasEnable = vk::False,
.lineWidth = 1.0F }; .lineWidth = 1.0F };
vk::PipelineMultisampleStateCreateInfo multisampling { .rasterizationSamples = vk::PipelineMultisampleStateCreateInfo multisampling { .rasterizationSamples = mMsaaSamples,
vk::SampleCountFlagBits::e1,
.sampleShadingEnable = vk::False }; .sampleShadingEnable = vk::False };
vk::PipelineDepthStencilStateCreateInfo depthStencil { .depthTestEnable = vk::True, vk::PipelineDepthStencilStateCreateInfo depthStencil { .depthTestEnable = vk::True,
@ -634,7 +652,9 @@ class VulkanApp {
mSwapChainFramebuffers.resize(mSwapChainImageViews.size()); mSwapChainFramebuffers.resize(mSwapChainImageViews.size());
for (usize i = 0; i < mSwapChainImageViews.size(); i++) { for (usize i = 0; i < mSwapChainImageViews.size(); i++) {
std::array<vk::ImageView, 2> attachments = { mSwapChainImageViews[i].get(), mDepthImageView.get() }; std::array<vk::ImageView, 3> attachments = { mColorImageView.get(),
mDepthImageView.get(),
mSwapChainImageViews[i].get() };
vk::FramebufferCreateInfo framebufferInfo { .renderPass = mRenderPass.get(), vk::FramebufferCreateInfo framebufferInfo { .renderPass = mRenderPass.get(),
.attachmentCount = static_cast<u32>(attachments.size()), .attachmentCount = static_cast<u32>(attachments.size()),
@ -658,6 +678,25 @@ class VulkanApp {
mCommandPool = mDevice->createCommandPoolUnique(poolInfo); mCommandPool = mDevice->createCommandPoolUnique(poolInfo);
} }
fn createColorResources() -> void {
vk::Format colorFormat = mSwapChainImageFormat;
createImage(
mSwapChainExtent.width,
mSwapChainExtent.height,
1,
mMsaaSamples,
colorFormat,
vk::ImageTiling::eOptimal,
vk::ImageUsageFlagBits::eTransientAttachment | vk::ImageUsageFlagBits::eColorAttachment,
vk::MemoryPropertyFlagBits::eDeviceLocal,
mColorImage,
mColorImageMemory
);
mColorImageView = createImageView(mColorImage.get(), colorFormat, vk::ImageAspectFlagBits::eColor, 1);
}
fn createDepthResources() -> void { fn createDepthResources() -> void {
vk::Format depthFormat = findDepthFormat(); vk::Format depthFormat = findDepthFormat();
@ -665,6 +704,7 @@ class VulkanApp {
mSwapChainExtent.width, mSwapChainExtent.width,
mSwapChainExtent.height, mSwapChainExtent.height,
1, 1,
mMsaaSamples,
depthFormat, depthFormat,
vk::ImageTiling::eOptimal, vk::ImageTiling::eOptimal,
vk::ImageUsageFlagBits::eDepthStencilAttachment, vk::ImageUsageFlagBits::eDepthStencilAttachment,
@ -736,6 +776,7 @@ class VulkanApp {
static_cast<u32>(texWidth), static_cast<u32>(texWidth),
static_cast<u32>(texHeight), static_cast<u32>(texHeight),
mMipLevels, mMipLevels,
vk::SampleCountFlagBits::e1,
vk::Format::eR8G8B8A8Srgb, vk::Format::eR8G8B8A8Srgb,
vk::ImageTiling::eOptimal, vk::ImageTiling::eOptimal,
vk::ImageUsageFlagBits::eTransferSrc | vk::ImageUsageFlagBits::eTransferDst | vk::ImageUsageFlagBits::eTransferSrc | vk::ImageUsageFlagBits::eTransferDst |
@ -856,6 +897,28 @@ class VulkanApp {
endSingleTimeCommands(std::move(commandBuffer)); endSingleTimeCommands(std::move(commandBuffer));
} }
fn getMaxUsableSampleCount() -> vk::SampleCountFlagBits {
vk::PhysicalDeviceProperties physicalDeviceProperties = mPhysicalDevice.getProperties();
vk::SampleCountFlags counts = physicalDeviceProperties.limits.framebufferColorSampleCounts &
physicalDeviceProperties.limits.framebufferDepthSampleCounts;
if (counts & vk::SampleCountFlagBits::e64)
return vk::SampleCountFlagBits::e64;
if (counts & vk::SampleCountFlagBits::e32)
return vk::SampleCountFlagBits::e32;
if (counts & vk::SampleCountFlagBits::e16)
return vk::SampleCountFlagBits::e16;
if (counts & vk::SampleCountFlagBits::e8)
return vk::SampleCountFlagBits::e8;
if (counts & vk::SampleCountFlagBits::e4)
return vk::SampleCountFlagBits::e4;
if (counts & vk::SampleCountFlagBits::e2)
return vk::SampleCountFlagBits::e2;
return vk::SampleCountFlagBits::e1;
}
fn createTextureImageView() -> void { fn createTextureImageView() -> void {
mTextureImageView = createImageView( mTextureImageView = createImageView(
mTextureImage.get(), vk::Format::eR8G8B8A8Srgb, vk::ImageAspectFlagBits::eColor, mMipLevels mTextureImage.get(), vk::Format::eR8G8B8A8Srgb, vk::ImageAspectFlagBits::eColor, mMipLevels
@ -905,6 +968,7 @@ class VulkanApp {
u32 width, u32 width,
u32 height, u32 height,
u32 mipLevels, u32 mipLevels,
vk::SampleCountFlagBits numSamples,
vk::Format format, vk::Format format,
vk::ImageTiling tiling, vk::ImageTiling tiling,
vk::ImageUsageFlags usage, vk::ImageUsageFlags usage,
@ -918,7 +982,7 @@ class VulkanApp {
.extent = { .width = width, .height = height, .depth = 1 }, .extent = { .width = width, .height = height, .depth = 1 },
.mipLevels = mipLevels, .mipLevels = mipLevels,
.arrayLayers = 1, .arrayLayers = 1,
.samples = vk::SampleCountFlagBits::e1, .samples = numSamples,
.tiling = tiling, .tiling = tiling,
.usage = usage, .usage = usage,
.sharingMode = vk::SharingMode::eExclusive, .sharingMode = vk::SharingMode::eExclusive,