优化 MSAA

This commit is contained in:
ubuntu1804 2025-11-10 21:51:11 +08:00
parent 2f16c0b5e4
commit d82fc95584
2 changed files with 53 additions and 8 deletions

View File

@ -225,11 +225,34 @@ uint32_t VulkanRenderer::getMaxUsableSampleCount()
VkSampleCountFlags counts = physicalDeviceProperties.limits.framebufferColorSampleCounts &
physicalDeviceProperties.limits.framebufferDepthSampleCounts;
// Try to use 4x MSAA as a good balance between quality and performance
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; }
std::cout << "Available MSAA sample counts: ";
if (counts & VK_SAMPLE_COUNT_64_BIT) std::cout << "64 ";
if (counts & VK_SAMPLE_COUNT_32_BIT) std::cout << "32 ";
if (counts & VK_SAMPLE_COUNT_16_BIT) std::cout << "16 ";
if (counts & VK_SAMPLE_COUNT_8_BIT) std::cout << "8 ";
if (counts & VK_SAMPLE_COUNT_4_BIT) std::cout << "4 ";
if (counts & VK_SAMPLE_COUNT_2_BIT) std::cout << "2 ";
std::cout << std::endl;
// Prefer higher sample counts for better quality
if (counts & VK_SAMPLE_COUNT_16_BIT) {
std::cout << "Selected: 16x MSAA" << std::endl;
return VK_SAMPLE_COUNT_16_BIT;
}
if (counts & VK_SAMPLE_COUNT_8_BIT) {
std::cout << "Selected: 8x MSAA" << std::endl;
return VK_SAMPLE_COUNT_8_BIT;
}
if (counts & VK_SAMPLE_COUNT_4_BIT) {
std::cout << "Selected: 4x MSAA" << std::endl;
return VK_SAMPLE_COUNT_4_BIT;
}
if (counts & VK_SAMPLE_COUNT_2_BIT) {
std::cout << "Selected: 2x MSAA" << std::endl;
return VK_SAMPLE_COUNT_2_BIT;
}
std::cout << "No MSAA support available" << std::endl;
return VK_SAMPLE_COUNT_1_BIT;
}
@ -935,7 +958,8 @@ bool VulkanRenderer::createBackgroundPipeline()
// Multisampling
VkPipelineMultisampleStateCreateInfo multisampling = {};
multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
multisampling.sampleShadingEnable = VK_FALSE;
multisampling.sampleShadingEnable = VK_TRUE; // Enable sample shading for better quality
multisampling.minSampleShading = 0.5f; // Minimum fraction for sample shading
multisampling.rasterizationSamples = static_cast<VkSampleCountFlagBits>(m_msaaSamples);
// Color blending
@ -1101,7 +1125,8 @@ bool VulkanRenderer::createGeometryPipeline()
VkPipelineMultisampleStateCreateInfo multisampling = {};
multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
multisampling.sampleShadingEnable = VK_FALSE;
multisampling.sampleShadingEnable = VK_TRUE; // Enable sample shading for better quality
multisampling.minSampleShading = 0.5f; // Minimum fraction for sample shading
multisampling.rasterizationSamples = static_cast<VkSampleCountFlagBits>(m_msaaSamples);
// Enable alpha blending
@ -1262,7 +1287,8 @@ bool VulkanRenderer::createLinePipeline()
VkPipelineMultisampleStateCreateInfo multisampling = {};
multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
multisampling.sampleShadingEnable = VK_FALSE;
multisampling.sampleShadingEnable = VK_TRUE; // Enable sample shading for smoother lines
multisampling.minSampleShading = 0.75f; // Higher quality for lines
multisampling.rasterizationSamples = static_cast<VkSampleCountFlagBits>(m_msaaSamples);
// Enable alpha blending
@ -1441,7 +1467,8 @@ bool VulkanRenderer::createTextPipeline()
VkPipelineMultisampleStateCreateInfo multisampling = {};
multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
multisampling.sampleShadingEnable = VK_FALSE;
multisampling.sampleShadingEnable = VK_TRUE; // Enable sample shading for smoother text
multisampling.minSampleShading = 1.0f; // Maximum quality for text rendering
multisampling.rasterizationSamples = static_cast<VkSampleCountFlagBits>(m_msaaSamples);
// Enable alpha blending for text

View File

@ -359,8 +359,26 @@ bool VulkanWidget::createDevice()
queueCreateInfo.queueCount = 1;
queueCreateInfo.pQueuePriorities = &queuePriority;
// Query supported features first
VkPhysicalDeviceFeatures supportedFeatures;
vkGetPhysicalDeviceFeatures(m_physicalDevice, &supportedFeatures);
VkPhysicalDeviceFeatures deviceFeatures = {};
// Enable sample rate shading for better MSAA quality
if (supportedFeatures.sampleRateShading) {
deviceFeatures.sampleRateShading = VK_TRUE;
qDebug() << "Sample rate shading feature enabled";
} else {
qDebug() << "Sample rate shading not supported by device";
}
// Enable wide lines if supported (for smoother line rendering)
if (supportedFeatures.wideLines) {
deviceFeatures.wideLines = VK_TRUE;
qDebug() << "Wide lines feature enabled";
}
std::vector<const char*> deviceExtensions = getRequiredDeviceExtensions();
VkDeviceCreateInfo createInfo = {};