From d82fc9558496663d7e688dbd5e8a348b36f5d33b Mon Sep 17 00:00:00 2001 From: ubuntu1804 Date: Mon, 10 Nov 2025 21:51:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20MSAA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/vulkanrenderer.cpp | 43 ++++++++++++++++++++++++++++++++++-------- src/vulkanwidget.cpp | 18 ++++++++++++++++++ 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/src/vulkanrenderer.cpp b/src/vulkanrenderer.cpp index 549b88a..44a2b3d 100644 --- a/src/vulkanrenderer.cpp +++ b/src/vulkanrenderer.cpp @@ -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(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(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(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(m_msaaSamples); // Enable alpha blending for text diff --git a/src/vulkanwidget.cpp b/src/vulkanwidget.cpp index 5ae208a..0f223b0 100644 --- a/src/vulkanwidget.cpp +++ b/src/vulkanwidget.cpp @@ -359,7 +359,25 @@ 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 deviceExtensions = getRequiredDeviceExtensions();