Browse Source

Removing multisampling step

Multisampling is removed from ComputeAndGraphics file.
master
AliMehrabani 2 weeks ago
parent
commit
ad0769cef5
  1. 2
      VkTest.pro.user
  2. 86
      VulkanTutorial1.0/ComputeAndGraphics.cpp
  3. 12
      VulkanTutorial1.0/ComputeAndGraphics.h

2
VkTest.pro.user

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject> <!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.10.1, 2024-11-10T11:19:40. --> <!-- Written by QtCreator 4.10.1, 2024-11-10T17:06:43. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>

86
VulkanTutorial1.0/ComputeAndGraphics.cpp

@ -90,7 +90,6 @@ void ComputeAndGraphics::initVulkan()
createGraphicsPipeline(); createGraphicsPipeline();
// createComputePipeline(); // createComputePipeline();
createCommandPool(); createCommandPool();
createColorResources();
createDepthResources(); createDepthResources();
createFramebuffers(); createFramebuffers();
createTextureImage(); createTextureImage();
@ -260,27 +259,11 @@ void ComputeAndGraphics::chooseDevice(uint32_t deviceCount)
for (const auto& device : devices) { for (const auto& device : devices) {
if (isDeviceSuitable(device)) { if (isDeviceSuitable(device)) {
_physicalDevice = device; _physicalDevice = device;
_msaaSamples = getMaxUsableSampleCount();
break; break;
} }
} }
} }
VkSampleCountFlagBits ComputeAndGraphics::getMaxUsableSampleCount() {
VkPhysicalDeviceProperties physicalDeviceProperties;
vkGetPhysicalDeviceProperties(_physicalDevice, &physicalDeviceProperties);
VkSampleCountFlags counts = physicalDeviceProperties.limits.framebufferColorSampleCounts & physicalDeviceProperties.limits.framebufferDepthSampleCounts;
if (counts & VK_SAMPLE_COUNT_64_BIT) { return VK_SAMPLE_COUNT_64_BIT; }
if (counts & VK_SAMPLE_COUNT_32_BIT) { return VK_SAMPLE_COUNT_32_BIT; }
if (counts & VK_SAMPLE_COUNT_16_BIT) { return VK_SAMPLE_COUNT_16_BIT; }
if (counts & VK_SAMPLE_COUNT_8_BIT) { return VK_SAMPLE_COUNT_8_BIT; }
if (counts & VK_SAMPLE_COUNT_4_BIT) { return VK_SAMPLE_COUNT_4_BIT; }
if (counts & VK_SAMPLE_COUNT_2_BIT) { return VK_SAMPLE_COUNT_2_BIT; }
return VK_SAMPLE_COUNT_1_BIT;
}
bool ComputeAndGraphics::isDeviceSuitable(VkPhysicalDevice device) { bool ComputeAndGraphics::isDeviceSuitable(VkPhysicalDevice device) {
// VkPhysicalDeviceProperties deviceProperties; // VkPhysicalDeviceProperties deviceProperties;
// VkPhysicalDeviceFeatures deviceFeatures; // VkPhysicalDeviceFeatures deviceFeatures;
@ -554,20 +537,17 @@ void ComputeAndGraphics::createImageViews() {
} }
void ComputeAndGraphics::createRenderPass() { void ComputeAndGraphics::createRenderPass() {
VkAttachmentDescription colorAttachment = createColorAttachmentInfo(_swapChainImageFormat, _msaaSamples); VkAttachmentDescription colorAttachment = createColorAttachmentInfo(_swapChainImageFormat, VK_SAMPLE_COUNT_1_BIT);
VkAttachmentReference colorAttachmentRef = createAttachmentRefInfo(0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); VkAttachmentReference colorAttachmentRef = createAttachmentRefInfo(0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
VkAttachmentDescription depthAttachment = createDepthAttachmentInfo(findDepthFormat(), _msaaSamples); VkAttachmentDescription depthAttachment = createDepthAttachmentInfo(findDepthFormat(), VK_SAMPLE_COUNT_1_BIT);
VkAttachmentReference depthAttachmentRef = createAttachmentRefInfo(1, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); VkAttachmentReference depthAttachmentRef = createAttachmentRefInfo(1, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
VkAttachmentDescription colorAttachmentResolve = createColorAttachmentResolveInfo(_swapChainImageFormat, VK_SAMPLE_COUNT_1_BIT); VkSubpassDescription subpass = createSubpassInfo(&colorAttachmentRef, &depthAttachmentRef);
VkAttachmentReference colorAttachmentResolveRef = createAttachmentRefInfo(2, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
VkSubpassDescription subpass = createSubpassInfo(&colorAttachmentRef, &depthAttachmentRef, &colorAttachmentResolveRef);
VkSubpassDependency dependency = createSubpassDependencyInfo(); VkSubpassDependency dependency = createSubpassDependencyInfo();
std::vector<VkAttachmentDescription> attachments = {colorAttachment, depthAttachment, colorAttachmentResolve}; std::vector<VkAttachmentDescription> attachments = {colorAttachment, depthAttachment};
VkRenderPassCreateInfo renderPassInfo = createRenderPassInfo(attachments, &subpass, &dependency); VkRenderPassCreateInfo renderPassInfo = createRenderPassInfo(attachments, &subpass, &dependency);
if (vkCreateRenderPass(_device, &renderPassInfo, nullptr, &_renderPass) != VK_SUCCESS) { if (vkCreateRenderPass(_device, &renderPassInfo, nullptr, &_renderPass) != VK_SUCCESS) {
@ -575,17 +555,17 @@ void ComputeAndGraphics::createRenderPass() {
} }
} }
VkAttachmentDescription ComputeAndGraphics::createColorAttachmentInfo(VkFormat format, VkSampleCountFlagBits msaaSamples) VkAttachmentDescription ComputeAndGraphics::createColorAttachmentInfo(VkFormat format, VkSampleCountFlagBits /*msaaSamples*/)
{ {
VkAttachmentDescription colorAttachment{}; VkAttachmentDescription colorAttachment{};
colorAttachment.format = format; colorAttachment.format = format;
colorAttachment.samples = msaaSamples; colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE; colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
return colorAttachment; return colorAttachment;
} }
@ -599,11 +579,11 @@ VkAttachmentReference ComputeAndGraphics::createAttachmentRefInfo(uint32_t offse
return colorAttachmentRef; return colorAttachmentRef;
} }
VkAttachmentDescription ComputeAndGraphics::createDepthAttachmentInfo(VkFormat format, VkSampleCountFlagBits msaaSamples) VkAttachmentDescription ComputeAndGraphics::createDepthAttachmentInfo(VkFormat format, VkSampleCountFlagBits /*msaaSamples*/)
{ {
VkAttachmentDescription depthAttachment; VkAttachmentDescription depthAttachment;
depthAttachment.format = format; depthAttachment.format = format;
depthAttachment.samples = msaaSamples; depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
@ -614,30 +594,13 @@ VkAttachmentDescription ComputeAndGraphics::createDepthAttachmentInfo(VkFormat f
return depthAttachment; return depthAttachment;
} }
VkAttachmentDescription ComputeAndGraphics::createColorAttachmentResolveInfo(VkFormat format, VkSampleCountFlagBits msaaSamples) VkSubpassDescription ComputeAndGraphics::createSubpassInfo(VkAttachmentReference* colorAttachRef, VkAttachmentReference* depthAttachRef)
{
VkAttachmentDescription colorAttachmentResolve{};
colorAttachmentResolve.format = format;
colorAttachmentResolve.samples = msaaSamples;
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;
return colorAttachmentResolve;
}
VkSubpassDescription ComputeAndGraphics::createSubpassInfo(VkAttachmentReference* colorAttachRef, VkAttachmentReference* depthAttachRef
, VkAttachmentReference* colorAttachResolverRef)
{ {
VkSubpassDescription subpass{}; VkSubpassDescription subpass{};
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.colorAttachmentCount = 1; subpass.colorAttachmentCount = 1;
subpass.pColorAttachments = colorAttachRef; subpass.pColorAttachments = colorAttachRef;
subpass.pDepthStencilAttachment = depthAttachRef; subpass.pDepthStencilAttachment = depthAttachRef;
subpass.pResolveAttachments = colorAttachResolverRef;
return subpass; return subpass;
} }
@ -671,7 +634,6 @@ VkSubpassDependency ComputeAndGraphics::createSubpassDependencyInfo()
return dependency; return dependency;
} }
void ComputeAndGraphics::createDescriptorSetLayout() { void ComputeAndGraphics::createDescriptorSetLayout() {
VkDescriptorSetLayoutBinding uboLayoutBinding = createLayoutBindingInfo(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT); VkDescriptorSetLayoutBinding uboLayoutBinding = createLayoutBindingInfo(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT);
@ -859,7 +821,7 @@ VkPipelineMultisampleStateCreateInfo ComputeAndGraphics::createPipelineMultisamp
VkPipelineMultisampleStateCreateInfo multisampling{}; VkPipelineMultisampleStateCreateInfo multisampling{};
multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
multisampling.sampleShadingEnable = VK_FALSE; multisampling.sampleShadingEnable = VK_FALSE;
multisampling.rasterizationSamples = _msaaSamples; multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
multisampling.minSampleShading = 1.0f; // Optional multisampling.minSampleShading = 1.0f; // Optional
multisampling.pSampleMask = nullptr; // Optional multisampling.pSampleMask = nullptr; // Optional
multisampling.alphaToCoverageEnable = VK_FALSE; // Optional multisampling.alphaToCoverageEnable = VK_FALSE; // Optional
@ -981,9 +943,8 @@ void ComputeAndGraphics::createFramebuffers() {
for (size_t i = 0; i < _swapChainImageViews.size(); i++) { for (size_t i = 0; i < _swapChainImageViews.size(); i++) {
std::vector<VkImageView> attachments = { std::vector<VkImageView> attachments = {
_colorImageView, _swapChainImageViews[i],
_depthImageView, _depthImageView
_swapChainImageViews[i]
}; };
VkFramebufferCreateInfo framebufferInfo = createFramebufferInfo(attachments); VkFramebufferCreateInfo framebufferInfo = createFramebufferInfo(attachments);
@ -1021,19 +982,10 @@ void ComputeAndGraphics::createCommandPool() {
} }
} }
void ComputeAndGraphics::createColorResources() {
VkFormat colorFormat = _swapChainImageFormat;
createImage(_swapChainExtent.width, _swapChainExtent.height, 1, _msaaSamples, colorFormat, VK_IMAGE_TILING_OPTIMAL
, VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
, _colorImage, _colorImageMemory);
_colorImageView = createImageView(_colorImage, colorFormat, VK_IMAGE_ASPECT_COLOR_BIT, 1);
}
void ComputeAndGraphics::createDepthResources() { void ComputeAndGraphics::createDepthResources() {
VkFormat depthFormat = findDepthFormat(); VkFormat depthFormat = findDepthFormat();
createImage(_swapChainExtent.width, _swapChainExtent.height, 1, _msaaSamples, depthFormat, VK_IMAGE_TILING_OPTIMAL createImage(_swapChainExtent.width, _swapChainExtent.height, 1, VK_SAMPLE_COUNT_1_BIT, depthFormat, VK_IMAGE_TILING_OPTIMAL
, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, _depthImage, _depthImageMemory); , VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, _depthImage, _depthImageMemory);
_depthImageView = createImageView(_depthImage, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT, 1); _depthImageView = createImageView(_depthImage, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT, 1);
@ -1304,7 +1256,7 @@ void ComputeAndGraphics::setSrcAndDst(VkImageMemoryBarrier& barrier, VkPipelineS
} }
} }
void ComputeAndGraphics::createImage(uint32_t width, uint32_t height, uint32_t mipLevels, VkSampleCountFlagBits numSamples, VkFormat format void ComputeAndGraphics::createImage(uint32_t width, uint32_t height, uint32_t mipLevels, VkSampleCountFlagBits /*numSamples*/, VkFormat format
, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage& image , VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage& image
, VkDeviceMemory& imageMemory) , VkDeviceMemory& imageMemory)
{ {
@ -1321,7 +1273,7 @@ void ComputeAndGraphics::createImage(uint32_t width, uint32_t height, uint32_t m
imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
imageInfo.usage = usage; imageInfo.usage = usage;
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
imageInfo.samples = numSamples; imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
imageInfo.flags = 0; // Optional imageInfo.flags = 0; // Optional
if (vkCreateImage(_device, &imageInfo, nullptr, &image) != VK_SUCCESS) { if (vkCreateImage(_device, &imageInfo, nullptr, &image) != VK_SUCCESS) {
@ -1986,6 +1938,8 @@ VkSubmitInfo ComputeAndGraphics::createComputeSubmitInfo()
submitInfo.pCommandBuffers = &_computeCommandBuffers[currentFrame]; submitInfo.pCommandBuffers = &_computeCommandBuffers[currentFrame];
submitInfo.signalSemaphoreCount = 1; submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = &_computeFinishedSemaphores[currentFrame]; submitInfo.pSignalSemaphores = &_computeFinishedSemaphores[currentFrame];
return submitInfo;
} }
VkResult ComputeAndGraphics::graphicsSubmission() VkResult ComputeAndGraphics::graphicsSubmission()
@ -2208,7 +2162,6 @@ void ComputeAndGraphics::recreateSwapChain() {
createSwapChain(); createSwapChain();
createImageViews(); createImageViews();
createColorResources();
createDepthResources(); createDepthResources();
createFramebuffers(); createFramebuffers();
} }
@ -2216,9 +2169,6 @@ void ComputeAndGraphics::recreateSwapChain() {
void ComputeAndGraphics::cleanupSwapChain() { void ComputeAndGraphics::cleanupSwapChain() {
vkDestroyImageView(_device, _colorImageView, nullptr);
vkDestroyImage(_device, _colorImage, nullptr);
vkFreeMemory(_device, _colorImageMemory, nullptr);
vkDestroyImageView(_device, _depthImageView, nullptr); vkDestroyImageView(_device, _depthImageView, nullptr);
vkDestroyImage(_device, _depthImage, nullptr); vkDestroyImage(_device, _depthImage, nullptr);

12
VulkanTutorial1.0/ComputeAndGraphics.h

@ -225,9 +225,6 @@ private:
void generateMipmaps(VkImage image, VkFormat imageFormat, int32_t texWidth, int32_t texHeight, uint32_t mipLevels); void generateMipmaps(VkImage image, VkFormat imageFormat, int32_t texWidth, int32_t texHeight, uint32_t mipLevels);
VkSampleCountFlagBits getMaxUsableSampleCount();
void createColorResources();
void createShaderStorageBuffers(); void createShaderStorageBuffers();
void createComputePipeline(); void createComputePipeline();
void createComputeCommandBuffers(); void createComputeCommandBuffers();
@ -250,10 +247,8 @@ private:
VkAttachmentDescription createColorAttachmentInfo(VkFormat format, VkSampleCountFlagBits msaaSamples); VkAttachmentDescription createColorAttachmentInfo(VkFormat format, VkSampleCountFlagBits msaaSamples);
VkAttachmentDescription createDepthAttachmentInfo(VkFormat format, VkSampleCountFlagBits msaaSamples); VkAttachmentDescription createDepthAttachmentInfo(VkFormat format, VkSampleCountFlagBits msaaSamples);
VkAttachmentDescription createColorAttachmentResolveInfo(VkFormat format, VkSampleCountFlagBits msaaSamples);
VkAttachmentReference createAttachmentRefInfo(uint32_t offset, VkImageLayout layout); VkAttachmentReference createAttachmentRefInfo(uint32_t offset, VkImageLayout layout);
VkSubpassDescription createSubpassInfo(VkAttachmentReference* colorAttachRef, VkAttachmentReference* depthAttachRef VkSubpassDescription createSubpassInfo(VkAttachmentReference* colorAttachRef, VkAttachmentReference* depthAttachRef);
, VkAttachmentReference* colorAttachResolverRef);
VkRenderPassCreateInfo createRenderPassInfo(std::vector<VkAttachmentDescription>& attachments, VkSubpassDescription* subpass, VkSubpassDependency* dependency); VkRenderPassCreateInfo createRenderPassInfo(std::vector<VkAttachmentDescription>& attachments, VkSubpassDescription* subpass, VkSubpassDependency* dependency);
VkSubpassDependency createSubpassDependencyInfo(); VkSubpassDependency createSubpassDependencyInfo();
@ -406,11 +401,6 @@ private:
VkDeviceMemory _depthImageMemory; VkDeviceMemory _depthImageMemory;
VkImageView _depthImageView; VkImageView _depthImageView;
VkSampleCountFlagBits _msaaSamples = VK_SAMPLE_COUNT_1_BIT;
VkImage _colorImage;
VkDeviceMemory _colorImageMemory;
VkImageView _colorImageView;
std::vector<VkBuffer> _shaderStorageBuffers; std::vector<VkBuffer> _shaderStorageBuffers;
std::vector<VkDeviceMemory> _shaderStorageBuffersMemory; std::vector<VkDeviceMemory> _shaderStorageBuffersMemory;
VkPipelineLayout _computePipelineLayout; VkPipelineLayout _computePipelineLayout;

Loading…
Cancel
Save