Browse Source

vulkan: improve sRGB support for textures

slime 2 years ago
parent
commit
2b074fe90e

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

@@ -940,14 +940,12 @@ PixelFormat Graphics::getSizedFormat(PixelFormat format, bool rendertarget, bool
 
 
 bool Graphics::isPixelFormatSupported(PixelFormat format, uint32 usage, bool sRGB)
 bool Graphics::isPixelFormatSupported(PixelFormat format, uint32 usage, bool sRGB)
 {
 {
-	(void)sRGB;	// fixme: sRGB
-
 	bool rendertarget = (usage & PIXELFORMATUSAGEFLAGS_RENDERTARGET) != 0;
 	bool rendertarget = (usage & PIXELFORMATUSAGEFLAGS_RENDERTARGET) != 0;
 	bool readable = (usage & PIXELFORMATUSAGEFLAGS_SAMPLE) != 0;
 	bool readable = (usage & PIXELFORMATUSAGEFLAGS_SAMPLE) != 0;
 
 
 	format = getSizedFormat(format, rendertarget, readable);
 	format = getSizedFormat(format, rendertarget, readable);
 
 
-	auto vulkanFormat = Vulkan::getTextureFormat(format);
+	auto vulkanFormat = Vulkan::getTextureFormat(format, sRGB);
 
 
 	VkFormatProperties formatProperties;
 	VkFormatProperties formatProperties;
 	vkGetPhysicalDeviceFormatProperties(physicalDevice, vulkanFormat.internalFormat, &formatProperties);
 	vkGetPhysicalDeviceFormatProperties(physicalDevice, vulkanFormat.internalFormat, &formatProperties);
@@ -2418,13 +2416,13 @@ void Graphics::setRenderPass(const RenderTargets &rts, int pixelw, int pixelh, b
 	RenderPassConfiguration renderPassConfiguration{};
 	RenderPassConfiguration renderPassConfiguration{};
 	for (const auto& color : rts.colors)
 	for (const auto& color : rts.colors)
 		renderPassConfiguration.colorAttachments.push_back({ 
 		renderPassConfiguration.colorAttachments.push_back({ 
-			Vulkan::getTextureFormat(color.texture->getPixelFormat()).internalFormat, 
+			Vulkan::getTextureFormat(color.texture->getPixelFormat(), isPixelFormatSRGB(color.texture->getPixelFormat())).internalFormat,
 			false, 
 			false, 
 			dynamic_cast<Texture*>(color.texture)->getMsaaSamples() });
 			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 = { 
 			renderPassConfiguration.staticData.depthAttachment = { 
-				Vulkan::getTextureFormat(rts.depthStencil.texture->getPixelFormat()).internalFormat, 
+				Vulkan::getTextureFormat(rts.depthStencil.texture->getPixelFormat(), false).internalFormat,
 				false,
 				false,
 				dynamic_cast<Texture*>(rts.depthStencil.texture)->getMsaaSamples() };
 				dynamic_cast<Texture*>(rts.depthStencil.texture)->getMsaaSamples() };
 
 

+ 3 - 3
src/modules/graphics/vulkan/Texture.cpp

@@ -55,7 +55,7 @@ bool Texture::loadVolatile()
 	else
 	else
 		imageAspect |= VK_IMAGE_ASPECT_COLOR_BIT;
 		imageAspect |= VK_IMAGE_ASPECT_COLOR_BIT;
 
 
-	auto vulkanFormat = Vulkan::getTextureFormat(format);
+	auto vulkanFormat = Vulkan::getTextureFormat(format, sRGB);
 
 
 	VkImageUsageFlags usageFlags =
 	VkImageUsageFlags usageFlags =
 		VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
 		VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
@@ -277,7 +277,7 @@ VkImageLayout Texture::getImageLayout() const
 
 
 void Texture::createTextureImageView()
 void Texture::createTextureImageView()
 {
 {
-	auto vulkanFormat = Vulkan::getTextureFormat(format);
+	auto vulkanFormat = Vulkan::getTextureFormat(format, sRGB);
 
 
 	VkImageViewCreateInfo viewInfo{};
 	VkImageViewCreateInfo viewInfo{};
 	viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
 	viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
@@ -349,7 +349,7 @@ void Texture::clear()
 
 
 VkClearColorValue Texture::getClearValue()
 VkClearColorValue Texture::getClearValue()
 {
 {
-	auto vulkanFormat = Vulkan::getTextureFormat(format);
+	auto vulkanFormat = Vulkan::getTextureFormat(format, sRGB);
 
 
 	VkClearColorValue clearColor{};
 	VkClearColorValue clearColor{};
 	switch (vulkanFormat.internalFormatRepresentation)
 	switch (vulkanFormat.internalFormatRepresentation)

+ 33 - 55
src/modules/graphics/vulkan/Vulkan.cpp

@@ -159,10 +159,16 @@ VkFormat Vulkan::getVulkanVertexFormat(DataFormat format)
 	}
 	}
 }
 }
 
 
-TextureFormat Vulkan::getTextureFormat(PixelFormat format)
+TextureFormat Vulkan::getTextureFormat(PixelFormat format, bool sRGB)
 {
 {
 	TextureFormat textureFormat{};
 	TextureFormat textureFormat{};
 
 
+	if (sRGB)
+		format = getSRGBPixelFormat(format);
+
+	if (!isPixelFormatCompressed(format) && !isPixelFormatSRGB(format))
+		sRGB = false;
+
 	switch (format)
 	switch (format)
 	{
 	{
 	case PIXELFORMAT_UNKNOWN:
 	case PIXELFORMAT_UNKNOWN:
@@ -331,13 +337,13 @@ TextureFormat Vulkan::getTextureFormat(PixelFormat format)
 		textureFormat.internalFormat = VK_FORMAT_D32_SFLOAT_S8_UINT;
 		textureFormat.internalFormat = VK_FORMAT_D32_SFLOAT_S8_UINT;
 		break;
 		break;
 	case PIXELFORMAT_DXT1_UNORM:
 	case PIXELFORMAT_DXT1_UNORM:
-		textureFormat.internalFormat = VK_FORMAT_BC1_RGBA_UNORM_BLOCK;
+		textureFormat.internalFormat = sRGB ? VK_FORMAT_BC1_RGBA_SRGB_BLOCK : VK_FORMAT_BC1_RGBA_UNORM_BLOCK;
 		break;
 		break;
 	case PIXELFORMAT_DXT3_UNORM:
 	case PIXELFORMAT_DXT3_UNORM:
-		textureFormat.internalFormat = VK_FORMAT_BC2_UNORM_BLOCK;
+		textureFormat.internalFormat = sRGB ? VK_FORMAT_BC2_SRGB_BLOCK : VK_FORMAT_BC2_UNORM_BLOCK;
 		break;
 		break;
 	case PIXELFORMAT_DXT5_UNORM:
 	case PIXELFORMAT_DXT5_UNORM:
-		textureFormat.internalFormat = VK_FORMAT_BC3_UNORM_BLOCK;
+		textureFormat.internalFormat = sRGB ? VK_FORMAT_BC3_SRGB_BLOCK : VK_FORMAT_BC3_UNORM_BLOCK;
 		break;
 		break;
 	case PIXELFORMAT_BC4_UNORM:
 	case PIXELFORMAT_BC4_UNORM:
 		textureFormat.internalFormat = VK_FORMAT_BC4_UNORM_BLOCK;
 		textureFormat.internalFormat = VK_FORMAT_BC4_UNORM_BLOCK;
@@ -358,31 +364,31 @@ TextureFormat Vulkan::getTextureFormat(PixelFormat format)
 		textureFormat.internalFormat = VK_FORMAT_BC6H_SFLOAT_BLOCK;
 		textureFormat.internalFormat = VK_FORMAT_BC6H_SFLOAT_BLOCK;
 		break;
 		break;
 	case PIXELFORMAT_BC7_UNORM:
 	case PIXELFORMAT_BC7_UNORM:
-		textureFormat.internalFormat = VK_FORMAT_BC7_UNORM_BLOCK;
+		textureFormat.internalFormat = sRGB ? VK_FORMAT_BC7_SRGB_BLOCK : VK_FORMAT_BC7_UNORM_BLOCK;
 		break;
 		break;
 	case PIXELFORMAT_PVR1_RGB2_UNORM:
 	case PIXELFORMAT_PVR1_RGB2_UNORM:
-		textureFormat.internalFormat = VK_FORMAT_PVRTC1_2BPP_SRGB_BLOCK_IMG;
+		textureFormat.internalFormat = sRGB ? VK_FORMAT_PVRTC1_2BPP_SRGB_BLOCK_IMG : VK_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG;
 		break;
 		break;
 	case PIXELFORMAT_PVR1_RGB4_UNORM:
 	case PIXELFORMAT_PVR1_RGB4_UNORM:
-		textureFormat.internalFormat = VK_FORMAT_PVRTC1_2BPP_SRGB_BLOCK_IMG;
+		textureFormat.internalFormat = sRGB ? VK_FORMAT_PVRTC1_4BPP_SRGB_BLOCK_IMG : VK_FORMAT_PVRTC1_4BPP_UNORM_BLOCK_IMG;
 		break;
 		break;
 	case PIXELFORMAT_PVR1_RGBA2_UNORM:
 	case PIXELFORMAT_PVR1_RGBA2_UNORM:
-		textureFormat.internalFormat = VK_FORMAT_PVRTC1_2BPP_SRGB_BLOCK_IMG;
+		textureFormat.internalFormat = sRGB ? VK_FORMAT_PVRTC1_2BPP_SRGB_BLOCK_IMG : VK_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG;
 		break;
 		break;
 	case PIXELFORMAT_PVR1_RGBA4_UNORM:
 	case PIXELFORMAT_PVR1_RGBA4_UNORM:
-		textureFormat.internalFormat = VK_FORMAT_PVRTC1_2BPP_SRGB_BLOCK_IMG;
+		textureFormat.internalFormat = sRGB ? VK_FORMAT_PVRTC1_4BPP_SRGB_BLOCK_IMG : VK_FORMAT_PVRTC1_4BPP_UNORM_BLOCK_IMG;
 		break;
 		break;
 	case PIXELFORMAT_ETC1_UNORM:
 	case PIXELFORMAT_ETC1_UNORM:
-		textureFormat.internalFormat = VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK;
+		textureFormat.internalFormat = sRGB ? VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK : VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK;
 		break;
 		break;
 	case PIXELFORMAT_ETC2_RGB_UNORM:
 	case PIXELFORMAT_ETC2_RGB_UNORM:
-		textureFormat.internalFormat = VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK;
+		textureFormat.internalFormat = sRGB ? VK_FORMAT_ETC2_R8G8B8_SRGB_BLOCK : VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK;
 		break;
 		break;
 	case PIXELFORMAT_ETC2_RGBA_UNORM:
 	case PIXELFORMAT_ETC2_RGBA_UNORM:
-		textureFormat.internalFormat = VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK;
+		textureFormat.internalFormat = sRGB ? VK_FORMAT_ETC2_R8G8B8A8_SRGB_BLOCK : VK_FORMAT_ETC2_R8G8B8A8_UNORM_BLOCK;
 		break;
 		break;
 	case PIXELFORMAT_ETC2_RGBA1_UNORM:
 	case PIXELFORMAT_ETC2_RGBA1_UNORM:
-		textureFormat.internalFormat = VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK;
+		textureFormat.internalFormat = sRGB ? VK_FORMAT_ETC2_R8G8B8A1_SRGB_BLOCK : VK_FORMAT_ETC2_R8G8B8A1_UNORM_BLOCK;
 		break;
 		break;
 	case PIXELFORMAT_EAC_R_UNORM:
 	case PIXELFORMAT_EAC_R_UNORM:
 		textureFormat.internalFormat = VK_FORMAT_EAC_R11_UNORM_BLOCK;
 		textureFormat.internalFormat = VK_FORMAT_EAC_R11_UNORM_BLOCK;
@@ -397,74 +403,46 @@ TextureFormat Vulkan::getTextureFormat(PixelFormat format)
 		textureFormat.internalFormat = VK_FORMAT_EAC_R11G11_SNORM_BLOCK;
 		textureFormat.internalFormat = VK_FORMAT_EAC_R11G11_SNORM_BLOCK;
 		break;
 		break;
 	case PIXELFORMAT_ASTC_4x4:
 	case PIXELFORMAT_ASTC_4x4:
-#ifdef VK_EXT_texture_compression_astc_hdr
-		textureFormat.internalFormat = VK_FORMAT_ASTC_4x4_SFLOAT_BLOCK_EXT;
-#endif
+		textureFormat.internalFormat = sRGB ? VK_FORMAT_ASTC_4x4_SRGB_BLOCK : VK_FORMAT_ASTC_4x4_UNORM_BLOCK;
 		break;
 		break;
 	case PIXELFORMAT_ASTC_5x4:
 	case PIXELFORMAT_ASTC_5x4:
-#ifdef VK_EXT_texture_compression_astc_hdr
-		textureFormat.internalFormat = VK_FORMAT_ASTC_5x4_SFLOAT_BLOCK_EXT;
-#endif
+		textureFormat.internalFormat = sRGB ? VK_FORMAT_ASTC_5x4_SRGB_BLOCK : VK_FORMAT_ASTC_5x4_UNORM_BLOCK;
 		break;
 		break;
 	case PIXELFORMAT_ASTC_5x5:
 	case PIXELFORMAT_ASTC_5x5:
-#ifdef VK_EXT_texture_compression_astc_hdr
-		textureFormat.internalFormat = VK_FORMAT_ASTC_5x5_SFLOAT_BLOCK_EXT;
-#endif
+		textureFormat.internalFormat = sRGB ? VK_FORMAT_ASTC_5x5_SRGB_BLOCK : VK_FORMAT_ASTC_5x5_UNORM_BLOCK;
 		break;
 		break;
 	case PIXELFORMAT_ASTC_6x5:
 	case PIXELFORMAT_ASTC_6x5:
-#ifdef VK_EXT_texture_compression_astc_hdr
-		textureFormat.internalFormat = VK_FORMAT_ASTC_6x5_SFLOAT_BLOCK_EXT;
-#endif
+		textureFormat.internalFormat = sRGB ? VK_FORMAT_ASTC_6x5_SRGB_BLOCK : VK_FORMAT_ASTC_6x5_UNORM_BLOCK;
 		break;
 		break;
 	case PIXELFORMAT_ASTC_6x6:
 	case PIXELFORMAT_ASTC_6x6:
-#ifdef VK_EXT_texture_compression_astc_hdr
-		textureFormat.internalFormat = VK_FORMAT_ASTC_6x6_SFLOAT_BLOCK_EXT;
-#endif
+		textureFormat.internalFormat = sRGB ? VK_FORMAT_ASTC_6x6_SRGB_BLOCK : VK_FORMAT_ASTC_6x6_UNORM_BLOCK;
 		break;
 		break;
 	case PIXELFORMAT_ASTC_8x5:
 	case PIXELFORMAT_ASTC_8x5:
-#ifdef VK_EXT_texture_compression_astc_hdr
-		textureFormat.internalFormat = VK_FORMAT_ASTC_8x5_SFLOAT_BLOCK_EXT;
-#endif
+		textureFormat.internalFormat = sRGB ? VK_FORMAT_ASTC_8x5_SRGB_BLOCK : VK_FORMAT_ASTC_8x5_UNORM_BLOCK;
 		break;
 		break;
 	case PIXELFORMAT_ASTC_8x6:
 	case PIXELFORMAT_ASTC_8x6:
-#ifdef VK_EXT_texture_compression_astc_hdr
-		textureFormat.internalFormat = VK_FORMAT_ASTC_8x6_SFLOAT_BLOCK_EXT;
-#endif
+		textureFormat.internalFormat = sRGB ? VK_FORMAT_ASTC_8x6_SRGB_BLOCK : VK_FORMAT_ASTC_8x6_UNORM_BLOCK;
 		break;
 		break;
 	case PIXELFORMAT_ASTC_8x8:
 	case PIXELFORMAT_ASTC_8x8:
-#ifdef VK_EXT_texture_compression_astc_hdr
-		textureFormat.internalFormat = VK_FORMAT_ASTC_8x8_SFLOAT_BLOCK_EXT;
-#endif
+		textureFormat.internalFormat = sRGB ? VK_FORMAT_ASTC_8x8_SRGB_BLOCK : VK_FORMAT_ASTC_8x8_UNORM_BLOCK;
 		break;
 		break;
 	case PIXELFORMAT_ASTC_10x5:
 	case PIXELFORMAT_ASTC_10x5:
-#ifdef VK_EXT_texture_compression_astc_hdr
-		textureFormat.internalFormat = VK_FORMAT_ASTC_10x5_SFLOAT_BLOCK_EXT;
-#endif
+		textureFormat.internalFormat = sRGB ? VK_FORMAT_ASTC_10x5_SRGB_BLOCK : VK_FORMAT_ASTC_10x5_UNORM_BLOCK;
 		break;
 		break;
 	case PIXELFORMAT_ASTC_10x6:
 	case PIXELFORMAT_ASTC_10x6:
-#ifdef VK_EXT_texture_compression_astc_hdr
-		textureFormat.internalFormat = VK_FORMAT_ASTC_10x6_SFLOAT_BLOCK_EXT;
-#endif
+		textureFormat.internalFormat = sRGB ? VK_FORMAT_ASTC_10x6_SRGB_BLOCK : VK_FORMAT_ASTC_10x6_UNORM_BLOCK;
 		break;
 		break;
 	case PIXELFORMAT_ASTC_10x8:
 	case PIXELFORMAT_ASTC_10x8:
-#ifdef VK_EXT_texture_compression_astc_hdr
-		textureFormat.internalFormat = VK_FORMAT_ASTC_10x8_SFLOAT_BLOCK_EXT;
-#endif
+		textureFormat.internalFormat = sRGB ? VK_FORMAT_ASTC_10x8_SRGB_BLOCK : VK_FORMAT_ASTC_10x8_UNORM_BLOCK;
 		break;
 		break;
 	case PIXELFORMAT_ASTC_10x10:
 	case PIXELFORMAT_ASTC_10x10:
-#ifdef VK_EXT_texture_compression_astc_hdr
-		textureFormat.internalFormat = VK_FORMAT_ASTC_10x10_SFLOAT_BLOCK_EXT;
-#endif
+		textureFormat.internalFormat = sRGB ? VK_FORMAT_ASTC_10x10_SRGB_BLOCK : VK_FORMAT_ASTC_10x10_UNORM_BLOCK;
 		break;
 		break;
 	case PIXELFORMAT_ASTC_12x10:
 	case PIXELFORMAT_ASTC_12x10:
-#ifdef VK_EXT_texture_compression_astc_hdr
-		textureFormat.internalFormat = VK_FORMAT_ASTC_12x10_SFLOAT_BLOCK_EXT;
-#endif
+		textureFormat.internalFormat = sRGB ? VK_FORMAT_ASTC_12x10_SRGB_BLOCK : VK_FORMAT_ASTC_12x10_UNORM_BLOCK;
 		break;
 		break;
 	case PIXELFORMAT_ASTC_12x12:
 	case PIXELFORMAT_ASTC_12x12:
-#ifdef VK_EXT_texture_compression_astc_hdr
-		textureFormat.internalFormat = VK_FORMAT_ASTC_12x12_SFLOAT_BLOCK_EXT;
-#endif
+		textureFormat.internalFormat = sRGB ? VK_FORMAT_ASTC_12x12_SRGB_BLOCK : VK_FORMAT_ASTC_12x12_UNORM_BLOCK;
 		break;
 		break;
 	default:
 	default:
 		throw love::Exception("unknown pixel format");
 		throw love::Exception("unknown pixel format");

+ 1 - 1
src/modules/graphics/vulkan/Vulkan.h

@@ -61,7 +61,7 @@ public:
 
 
 	static uint32_t getSupportedVulkanApiVersion(uint32_t suggested);
 	static uint32_t getSupportedVulkanApiVersion(uint32_t suggested);
 	static VkFormat getVulkanVertexFormat(DataFormat format);
 	static VkFormat getVulkanVertexFormat(DataFormat format);
-	static TextureFormat getTextureFormat(PixelFormat);
+	static TextureFormat getTextureFormat(PixelFormat, bool sRGB);
 	static std::string getVendorName(uint32_t vendorId);
 	static std::string getVendorName(uint32_t vendorId);
 	static std::string getVulkanApiVersion(uint32_t apiVersion);
 	static std::string getVulkanApiVersion(uint32_t apiVersion);
 	static VkPrimitiveTopology getPrimitiveTypeTopology(graphics::PrimitiveType);
 	static VkPrimitiveTopology getPrimitiveTypeTopology(graphics::PrimitiveType);