|
|
@ -90,7 +90,6 @@ void ComputeAndGraphics::initVulkan() |
|
|
|
createGraphicsPipeline(); |
|
|
|
// createComputePipeline();
|
|
|
|
createCommandPool(); |
|
|
|
createColorResources(); |
|
|
|
createDepthResources(); |
|
|
|
createFramebuffers(); |
|
|
|
createTextureImage(); |
|
|
@ -260,27 +259,11 @@ void ComputeAndGraphics::chooseDevice(uint32_t deviceCount) |
|
|
|
for (const auto& device : devices) { |
|
|
|
if (isDeviceSuitable(device)) { |
|
|
|
_physicalDevice = device; |
|
|
|
_msaaSamples = getMaxUsableSampleCount(); |
|
|
|
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) { |
|
|
|
// VkPhysicalDeviceProperties deviceProperties;
|
|
|
|
// VkPhysicalDeviceFeatures deviceFeatures;
|
|
|
@ -554,20 +537,17 @@ void ComputeAndGraphics::createImageViews() { |
|
|
|
} |
|
|
|
|
|
|
|
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); |
|
|
|
|
|
|
|
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); |
|
|
|
|
|
|
|
VkAttachmentDescription colorAttachmentResolve = createColorAttachmentResolveInfo(_swapChainImageFormat, VK_SAMPLE_COUNT_1_BIT); |
|
|
|
VkAttachmentReference colorAttachmentResolveRef = createAttachmentRefInfo(2, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); |
|
|
|
|
|
|
|
VkSubpassDescription subpass = createSubpassInfo(&colorAttachmentRef, &depthAttachmentRef, &colorAttachmentResolveRef); |
|
|
|
VkSubpassDescription subpass = createSubpassInfo(&colorAttachmentRef, &depthAttachmentRef); |
|
|
|
|
|
|
|
VkSubpassDependency dependency = createSubpassDependencyInfo(); |
|
|
|
|
|
|
|
std::vector<VkAttachmentDescription> attachments = {colorAttachment, depthAttachment, colorAttachmentResolve}; |
|
|
|
std::vector<VkAttachmentDescription> attachments = {colorAttachment, depthAttachment}; |
|
|
|
VkRenderPassCreateInfo renderPassInfo = createRenderPassInfo(attachments, &subpass, &dependency); |
|
|
|
|
|
|
|
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{}; |
|
|
|
colorAttachment.format = format; |
|
|
|
colorAttachment.samples = msaaSamples; |
|
|
|
colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT; |
|
|
|
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; |
|
|
|
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE; |
|
|
|
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; |
|
|
|
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
|
|
|
colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
|
|
|
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; |
|
|
|
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; |
|
|
|
|
|
|
|
return colorAttachment; |
|
|
|
} |
|
|
@ -599,11 +579,11 @@ VkAttachmentReference ComputeAndGraphics::createAttachmentRefInfo(uint32_t offse |
|
|
|
return colorAttachmentRef; |
|
|
|
} |
|
|
|
|
|
|
|
VkAttachmentDescription ComputeAndGraphics::createDepthAttachmentInfo(VkFormat format, VkSampleCountFlagBits msaaSamples) |
|
|
|
VkAttachmentDescription ComputeAndGraphics::createDepthAttachmentInfo(VkFormat format, VkSampleCountFlagBits /*msaaSamples*/) |
|
|
|
{ |
|
|
|
VkAttachmentDescription depthAttachment; |
|
|
|
depthAttachment.format = format; |
|
|
|
depthAttachment.samples = msaaSamples; |
|
|
|
depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT; |
|
|
|
depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; |
|
|
|
depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; |
|
|
|
depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; |
|
|
@ -614,30 +594,13 @@ VkAttachmentDescription ComputeAndGraphics::createDepthAttachmentInfo(VkFormat f |
|
|
|
return depthAttachment; |
|
|
|
} |
|
|
|
|
|
|
|
VkAttachmentDescription ComputeAndGraphics::createColorAttachmentResolveInfo(VkFormat format, VkSampleCountFlagBits msaaSamples) |
|
|
|
{ |
|
|
|
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 ComputeAndGraphics::createSubpassInfo(VkAttachmentReference* colorAttachRef, VkAttachmentReference* depthAttachRef) |
|
|
|
{ |
|
|
|
VkSubpassDescription subpass{}; |
|
|
|
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; |
|
|
|
subpass.colorAttachmentCount = 1; |
|
|
|
subpass.pColorAttachments = colorAttachRef; |
|
|
|
subpass.pDepthStencilAttachment = depthAttachRef; |
|
|
|
subpass.pResolveAttachments = colorAttachResolverRef; |
|
|
|
|
|
|
|
return subpass; |
|
|
|
} |
|
|
@ -671,7 +634,6 @@ VkSubpassDependency ComputeAndGraphics::createSubpassDependencyInfo() |
|
|
|
return dependency; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void ComputeAndGraphics::createDescriptorSetLayout() { |
|
|
|
VkDescriptorSetLayoutBinding uboLayoutBinding = createLayoutBindingInfo(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT); |
|
|
|
|
|
|
@ -859,7 +821,7 @@ VkPipelineMultisampleStateCreateInfo ComputeAndGraphics::createPipelineMultisamp |
|
|
|
VkPipelineMultisampleStateCreateInfo multisampling{}; |
|
|
|
multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; |
|
|
|
multisampling.sampleShadingEnable = VK_FALSE; |
|
|
|
multisampling.rasterizationSamples = _msaaSamples; |
|
|
|
multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT; |
|
|
|
multisampling.minSampleShading = 1.0f; // Optional
|
|
|
|
multisampling.pSampleMask = nullptr; // Optional
|
|
|
|
multisampling.alphaToCoverageEnable = VK_FALSE; // Optional
|
|
|
@ -981,9 +943,8 @@ void ComputeAndGraphics::createFramebuffers() { |
|
|
|
|
|
|
|
for (size_t i = 0; i < _swapChainImageViews.size(); i++) { |
|
|
|
std::vector<VkImageView> attachments = { |
|
|
|
_colorImageView, |
|
|
|
_depthImageView, |
|
|
|
_swapChainImageViews[i] |
|
|
|
_swapChainImageViews[i], |
|
|
|
_depthImageView |
|
|
|
}; |
|
|
|
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() { |
|
|
|
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); |
|
|
|
_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 |
|
|
|
, 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.usage = usage; |
|
|
|
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; |
|
|
|
imageInfo.samples = numSamples; |
|
|
|
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT; |
|
|
|
imageInfo.flags = 0; // Optional
|
|
|
|
|
|
|
|
if (vkCreateImage(_device, &imageInfo, nullptr, &image) != VK_SUCCESS) { |
|
|
@ -1986,6 +1938,8 @@ VkSubmitInfo ComputeAndGraphics::createComputeSubmitInfo() |
|
|
|
submitInfo.pCommandBuffers = &_computeCommandBuffers[currentFrame]; |
|
|
|
submitInfo.signalSemaphoreCount = 1; |
|
|
|
submitInfo.pSignalSemaphores = &_computeFinishedSemaphores[currentFrame]; |
|
|
|
|
|
|
|
return submitInfo; |
|
|
|
} |
|
|
|
|
|
|
|
VkResult ComputeAndGraphics::graphicsSubmission() |
|
|
@ -2208,7 +2162,6 @@ void ComputeAndGraphics::recreateSwapChain() { |
|
|
|
|
|
|
|
createSwapChain(); |
|
|
|
createImageViews(); |
|
|
|
createColorResources(); |
|
|
|
createDepthResources(); |
|
|
|
createFramebuffers(); |
|
|
|
} |
|
|
@ -2216,9 +2169,6 @@ void ComputeAndGraphics::recreateSwapChain() { |
|
|
|
|
|
|
|
|
|
|
|
void ComputeAndGraphics::cleanupSwapChain() { |
|
|
|
vkDestroyImageView(_device, _colorImageView, nullptr); |
|
|
|
vkDestroyImage(_device, _colorImage, nullptr); |
|
|
|
vkFreeMemory(_device, _colorImageMemory, nullptr); |
|
|
|
|
|
|
|
vkDestroyImageView(_device, _depthImageView, nullptr); |
|
|
|
vkDestroyImage(_device, _depthImage, nullptr); |
|
|
|