|
@ -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); |
|
|