Browse Source

vulkan: implement texture msaa

niki 2 years ago
parent
commit
4e59b563a4

+ 24 - 20
src/modules/graphics/vulkan/Graphics.cpp

@@ -198,9 +198,8 @@ void Graphics::discard(const std::vector<bool>& colorbuffers, bool depthstencil)
 	else
 	else
 	{
 	{
 		RenderPassConfiguration renderPassConfiguration{};
 		RenderPassConfiguration renderPassConfiguration{};
-		renderPassConfiguration.colorAttachments.push_back({ swapChainImageFormat, colorbuffers[0] });
-		renderPassConfiguration.staticData.depthAttachment = { findDepthFormat(), depthstencil };
-		renderPassConfiguration.staticData.msaaSamples = msaaSamples;
+		renderPassConfiguration.colorAttachments.push_back({ swapChainImageFormat, colorbuffers[0], msaaSamples });
+		renderPassConfiguration.staticData.depthAttachment = { findDepthFormat(), depthstencil, msaaSamples };
 		if (msaaSamples & VK_SAMPLE_COUNT_1_BIT)
 		if (msaaSamples & VK_SAMPLE_COUNT_1_BIT)
 			renderPassConfiguration.staticData.resolve = false;
 			renderPassConfiguration.staticData.resolve = false;
 		else
 		else
@@ -1192,7 +1191,7 @@ void Graphics::pickPhysicalDevice()
 	vkGetPhysicalDeviceProperties(physicalDevice, &properties);
 	vkGetPhysicalDeviceProperties(physicalDevice, &properties);
 	minUniformBufferOffsetAlignment = properties.limits.minUniformBufferOffsetAlignment;
 	minUniformBufferOffsetAlignment = properties.limits.minUniformBufferOffsetAlignment;
 
 
-	getMaxUsableSampleCount();
+	msaaSamples = getMsaaCount(requestedMsaa);
 }
 }
 
 
 bool Graphics::checkDeviceExtensionSupport(VkPhysicalDevice device)
 bool Graphics::checkDeviceExtensionSupport(VkPhysicalDevice device)
@@ -1717,9 +1716,8 @@ void Graphics::createImageViews()
 void Graphics::createDefaultRenderPass()
 void Graphics::createDefaultRenderPass()
 {
 {
 	RenderPassConfiguration renderPassConfiguration{};
 	RenderPassConfiguration renderPassConfiguration{};
-	renderPassConfiguration.colorAttachments.push_back({ swapChainImageFormat, false });
-	renderPassConfiguration.staticData.msaaSamples = msaaSamples;
-	renderPassConfiguration.staticData.depthAttachment = { findDepthFormat(), false };
+	renderPassConfiguration.colorAttachments.push_back({ swapChainImageFormat, false, msaaSamples });
+	renderPassConfiguration.staticData.depthAttachment = { findDepthFormat(), false, msaaSamples };
 	if (msaaSamples & VK_SAMPLE_COUNT_1_BIT)
 	if (msaaSamples & VK_SAMPLE_COUNT_1_BIT)
 		renderPassConfiguration.staticData.resolve = false;
 		renderPassConfiguration.staticData.resolve = false;
 	else
 	else
@@ -1824,7 +1822,7 @@ VkRenderPass Graphics::createRenderPass(RenderPassConfiguration &configuration)
 
 
 		VkAttachmentDescription colorDescription{};
 		VkAttachmentDescription colorDescription{};
 		colorDescription.format = colorAttachment.format;
 		colorDescription.format = colorAttachment.format;
-		colorDescription.samples = configuration.staticData.msaaSamples;
+		colorDescription.samples = colorAttachment.msaaSamples;
 		if (colorAttachment.discard)
 		if (colorAttachment.discard)
 			colorDescription.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
 			colorDescription.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
 		else
 		else
@@ -1849,7 +1847,7 @@ VkRenderPass Graphics::createRenderPass(RenderPassConfiguration &configuration)
 
 
 		VkAttachmentDescription depthStencilAttachment{};
 		VkAttachmentDescription depthStencilAttachment{};
 		depthStencilAttachment.format = configuration.staticData.depthAttachment.format;
 		depthStencilAttachment.format = configuration.staticData.depthAttachment.format;
-		depthStencilAttachment.samples = configuration.staticData.msaaSamples;
+		depthStencilAttachment.samples = configuration.staticData.depthAttachment.msaaSamples;
 		if (configuration.staticData.depthAttachment.discard)
 		if (configuration.staticData.depthAttachment.discard)
 			depthStencilAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
 			depthStencilAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
 		else
 		else
@@ -2096,11 +2094,17 @@ void Graphics::setRenderPass(const RenderTargets &rts, int pixelw, int pixelh, b
 	// fixme: hasSRGBtexture
 	// fixme: hasSRGBtexture
 	// fixme: msaaSamples
 	// fixme: msaaSamples
 	RenderPassConfiguration renderPassConfiguration{};
 	RenderPassConfiguration renderPassConfiguration{};
-	for (const auto &color : rts.colors)
-		renderPassConfiguration.colorAttachments.push_back({ Vulkan::getTextureFormat(color.texture->getPixelFormat()).internalFormat, false });
+	for (const auto& color : rts.colors)
+		renderPassConfiguration.colorAttachments.push_back({ 
+			Vulkan::getTextureFormat(color.texture->getPixelFormat()).internalFormat, 
+			false, 
+			dynamic_cast<Texture*>(color.texture)->getMsaaSamples() });
 	if (rts.depthStencil.texture != nullptr)
 	if (rts.depthStencil.texture != nullptr)
 		if (rts.depthStencil.texture != nullptr)
 		if (rts.depthStencil.texture != nullptr)
-			renderPassConfiguration.staticData.depthAttachment = { Vulkan::getTextureFormat(rts.depthStencil.texture->getPixelFormat()).internalFormat, false };
+			renderPassConfiguration.staticData.depthAttachment = { 
+				Vulkan::getTextureFormat(rts.depthStencil.texture->getPixelFormat()).internalFormat, 
+				false,
+				dynamic_cast<Texture*>(rts.depthStencil.texture)->getMsaaSamples() };
 
 
 	FramebufferConfiguration configuration{};
 	FramebufferConfiguration configuration{};
 
 
@@ -2409,7 +2413,7 @@ void Graphics::ensureGraphicsPipelineConfiguration(GraphicsPipelineConfiguration
 	}
 	}
 }
 }
 
 
-void Graphics::getMaxUsableSampleCount()
+VkSampleCountFlagBits Graphics::getMsaaCount(int requestedMsaa) const
 {
 {
 	VkPhysicalDeviceProperties physicalDeviceProperties;
 	VkPhysicalDeviceProperties physicalDeviceProperties;
 	vkGetPhysicalDeviceProperties(physicalDevice, &physicalDeviceProperties);
 	vkGetPhysicalDeviceProperties(physicalDevice, &physicalDeviceProperties);
@@ -2417,19 +2421,19 @@ void Graphics::getMaxUsableSampleCount()
 	VkSampleCountFlags counts = physicalDeviceProperties.limits.framebufferColorSampleCounts & physicalDeviceProperties.limits.framebufferDepthSampleCounts;
 	VkSampleCountFlags counts = physicalDeviceProperties.limits.framebufferColorSampleCounts & physicalDeviceProperties.limits.framebufferDepthSampleCounts;
 
 
 	if (counts & VK_SAMPLE_COUNT_64_BIT && requestedMsaa >= 64)
 	if (counts & VK_SAMPLE_COUNT_64_BIT && requestedMsaa >= 64)
-		msaaSamples = VK_SAMPLE_COUNT_64_BIT;
+		return VK_SAMPLE_COUNT_64_BIT;
 	else if (counts & VK_SAMPLE_COUNT_32_BIT && requestedMsaa >= 32)
 	else if (counts & VK_SAMPLE_COUNT_32_BIT && requestedMsaa >= 32)
-		msaaSamples = VK_SAMPLE_COUNT_32_BIT;
+		return VK_SAMPLE_COUNT_32_BIT;
 	else if (counts & VK_SAMPLE_COUNT_16_BIT && requestedMsaa >= 16)
 	else if (counts & VK_SAMPLE_COUNT_16_BIT && requestedMsaa >= 16)
-		msaaSamples = VK_SAMPLE_COUNT_16_BIT;
+		return VK_SAMPLE_COUNT_16_BIT;
 	else if (counts & VK_SAMPLE_COUNT_8_BIT && requestedMsaa >= 8)
 	else if (counts & VK_SAMPLE_COUNT_8_BIT && requestedMsaa >= 8)
-		msaaSamples = VK_SAMPLE_COUNT_8_BIT;
+		return VK_SAMPLE_COUNT_8_BIT;
 	else if (counts & VK_SAMPLE_COUNT_4_BIT && requestedMsaa >= 4)
 	else if (counts & VK_SAMPLE_COUNT_4_BIT && requestedMsaa >= 4)
-		msaaSamples = VK_SAMPLE_COUNT_4_BIT;
+		return VK_SAMPLE_COUNT_4_BIT;
 	else if (counts & VK_SAMPLE_COUNT_2_BIT && requestedMsaa >= 2)
 	else if (counts & VK_SAMPLE_COUNT_2_BIT && requestedMsaa >= 2)
-		msaaSamples = VK_SAMPLE_COUNT_2_BIT;
+		return VK_SAMPLE_COUNT_2_BIT;
 	else
 	else
-		msaaSamples = VK_SAMPLE_COUNT_1_BIT;
+		return VK_SAMPLE_COUNT_1_BIT;
 }
 }
 
 
 void Graphics::createColorResources()
 void Graphics::createColorResources()

+ 5 - 3
src/modules/graphics/vulkan/Graphics.h

@@ -31,10 +31,13 @@ struct RenderPassAttachment
 {
 {
 	VkFormat format = VK_FORMAT_UNDEFINED;
 	VkFormat format = VK_FORMAT_UNDEFINED;
 	bool discard = true;
 	bool discard = true;
+	VkSampleCountFlagBits msaaSamples = VK_SAMPLE_COUNT_1_BIT;
 
 
 	bool operator==(const RenderPassAttachment &attachment) const
 	bool operator==(const RenderPassAttachment &attachment) const
 	{
 	{
-		return format == attachment.format && discard == attachment.discard;
+		return format == attachment.format && 
+			discard == attachment.discard && 
+			msaaSamples == attachment.msaaSamples;
 	}
 	}
 };
 };
 
 
@@ -44,7 +47,6 @@ struct RenderPassConfiguration
 
 
 	struct StaticRenderPassConfiguration
 	struct StaticRenderPassConfiguration
 	{
 	{
-		VkSampleCountFlagBits msaaSamples = VK_SAMPLE_COUNT_1_BIT;
 		RenderPassAttachment depthAttachment;
 		RenderPassAttachment depthAttachment;
 		bool resolve = false;
 		bool resolve = false;
 	} staticData;
 	} staticData;
@@ -290,6 +292,7 @@ public:
 	std::set<Shader*> &getUsedShadersInFrame();
 	std::set<Shader*> &getUsedShadersInFrame();
 	graphics::Shader::BuiltinUniformData getCurrentBuiltinUniformData();
 	graphics::Shader::BuiltinUniformData getCurrentBuiltinUniformData();
 	const OptionalDeviceFeatures &getEnabledOptionalDeviceExtensions() const;
 	const OptionalDeviceFeatures &getEnabledOptionalDeviceExtensions() const;
+	VkSampleCountFlagBits getMsaaCount(int requestedMsaa) const;
 
 
 protected:
 protected:
 	graphics::ShaderStage *newShaderStageInternal(ShaderStageType stage, const std::string &cachekey, const std::string &source, bool gles) override;
 	graphics::ShaderStage *newShaderStageInternal(ShaderStageType stage, const std::string &cachekey, const std::string &source, bool gles) override;
@@ -304,7 +307,6 @@ private:
 	void createVulkanInstance();
 	void createVulkanInstance();
 	bool checkValidationSupport();
 	bool checkValidationSupport();
 	void pickPhysicalDevice();
 	void pickPhysicalDevice();
-	void getMaxUsableSampleCount();
 	int rateDeviceSuitability(VkPhysicalDevice device);
 	int rateDeviceSuitability(VkPhysicalDevice device);
 	QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device);
 	QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device);
 	void createLogicalDevice();
 	void createLogicalDevice();

+ 9 - 2
src/modules/graphics/vulkan/Texture.cpp

@@ -55,6 +55,8 @@ bool Texture::loadVolatile()
 		layerCount = 6;
 		layerCount = 6;
 		createFlags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
 		createFlags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
 	}
 	}
+
+	msaaSamples = vgfx->getMsaaCount(requestedMSAA);
 	
 	
 	VkImageCreateInfo imageInfo{};
 	VkImageCreateInfo imageInfo{};
 	imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
 	imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
@@ -70,7 +72,7 @@ bool Texture::loadVolatile()
 	imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
 	imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
 	imageInfo.usage = usageFlags;
 	imageInfo.usage = usageFlags;
 	imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
 	imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
-	imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
+	imageInfo.samples = msaaSamples;
 
 
 	VmaAllocationCreateInfo imageAllocationCreateInfo{};
 	VmaAllocationCreateInfo imageAllocationCreateInfo{};
 
 
@@ -195,9 +197,14 @@ VkImageView Texture::getRenderTargetView(int mip, int layer)
 	return renderTargetImageViews.at(mip).at(layer);
 	return renderTargetImageViews.at(mip).at(layer);
 }
 }
 
 
+VkSampleCountFlagBits Texture::getMsaaSamples() const
+{
+	return msaaSamples;
+}
+
 int Texture::getMSAA() const
 int Texture::getMSAA() const
 {
 {
-	return 0;
+	return static_cast<int>(msaaSamples);
 }
 }
 
 
 ptrdiff_t Texture::getHandle() const
 ptrdiff_t Texture::getHandle() const

+ 2 - 0
src/modules/graphics/vulkan/Texture.h

@@ -37,6 +37,7 @@ public:
 	ptrdiff_t getSamplerHandle() const override;
 	ptrdiff_t getSamplerHandle() const override;
 
 
 	VkImageView getRenderTargetView(int mip, int layer);
 	VkImageView getRenderTargetView(int mip, int layer);
+	VkSampleCountFlagBits getMsaaSamples() const;
 
 
 	void uploadByteData(PixelFormat pixelformat, const void *data, size_t size, int level, int slice, const Rect &r) override;
 	void uploadByteData(PixelFormat pixelformat, const void *data, size_t size, int level, int slice, const Rect &r) override;
 
 
@@ -62,6 +63,7 @@ private:
 	VkSampler textureSampler = VK_NULL_HANDLE;
 	VkSampler textureSampler = VK_NULL_HANDLE;
 	Slices slices;
 	Slices slices;
 	int layerCount = 0;
 	int layerCount = 0;
+	VkSampleCountFlagBits msaaSamples = VK_SAMPLE_COUNT_1_BIT;
 };
 };
 
 
 } // vulkan
 } // vulkan