From 2b074fe90e56d2da35f970473a5d872f6b72c4c4 Mon Sep 17 00:00:00 2001 From: slime Date: Tue, 20 Sep 2022 19:04:56 -0300 Subject: [PATCH] vulkan: improve sRGB support for textures --- src/modules/graphics/vulkan/Graphics.cpp | 8 +-- src/modules/graphics/vulkan/Texture.cpp | 6 +- src/modules/graphics/vulkan/Vulkan.cpp | 88 +++++++++--------------- src/modules/graphics/vulkan/Vulkan.h | 2 +- 4 files changed, 40 insertions(+), 64 deletions(-) diff --git a/src/modules/graphics/vulkan/Graphics.cpp b/src/modules/graphics/vulkan/Graphics.cpp index c000e09ff..42687353d 100644 --- a/src/modules/graphics/vulkan/Graphics.cpp +++ b/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) { - (void)sRGB; // fixme: sRGB - bool rendertarget = (usage & PIXELFORMATUSAGEFLAGS_RENDERTARGET) != 0; bool readable = (usage & PIXELFORMATUSAGEFLAGS_SAMPLE) != 0; format = getSizedFormat(format, rendertarget, readable); - auto vulkanFormat = Vulkan::getTextureFormat(format); + auto vulkanFormat = Vulkan::getTextureFormat(format, sRGB); VkFormatProperties formatProperties; vkGetPhysicalDeviceFormatProperties(physicalDevice, vulkanFormat.internalFormat, &formatProperties); @@ -2418,13 +2416,13 @@ void Graphics::setRenderPass(const RenderTargets &rts, int pixelw, int pixelh, b RenderPassConfiguration renderPassConfiguration{}; for (const auto& color : rts.colors) renderPassConfiguration.colorAttachments.push_back({ - Vulkan::getTextureFormat(color.texture->getPixelFormat()).internalFormat, + Vulkan::getTextureFormat(color.texture->getPixelFormat(), isPixelFormatSRGB(color.texture->getPixelFormat())).internalFormat, false, dynamic_cast(color.texture)->getMsaaSamples() }); if (rts.depthStencil.texture != nullptr) if (rts.depthStencil.texture != nullptr) renderPassConfiguration.staticData.depthAttachment = { - Vulkan::getTextureFormat(rts.depthStencil.texture->getPixelFormat()).internalFormat, + Vulkan::getTextureFormat(rts.depthStencil.texture->getPixelFormat(), false).internalFormat, false, dynamic_cast(rts.depthStencil.texture)->getMsaaSamples() }; diff --git a/src/modules/graphics/vulkan/Texture.cpp b/src/modules/graphics/vulkan/Texture.cpp index 74e21829a..fedf4c102 100644 --- a/src/modules/graphics/vulkan/Texture.cpp +++ b/src/modules/graphics/vulkan/Texture.cpp @@ -55,7 +55,7 @@ bool Texture::loadVolatile() else imageAspect |= VK_IMAGE_ASPECT_COLOR_BIT; - auto vulkanFormat = Vulkan::getTextureFormat(format); + auto vulkanFormat = Vulkan::getTextureFormat(format, sRGB); VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | @@ -277,7 +277,7 @@ VkImageLayout Texture::getImageLayout() const void Texture::createTextureImageView() { - auto vulkanFormat = Vulkan::getTextureFormat(format); + auto vulkanFormat = Vulkan::getTextureFormat(format, sRGB); VkImageViewCreateInfo viewInfo{}; viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; @@ -349,7 +349,7 @@ void Texture::clear() VkClearColorValue Texture::getClearValue() { - auto vulkanFormat = Vulkan::getTextureFormat(format); + auto vulkanFormat = Vulkan::getTextureFormat(format, sRGB); VkClearColorValue clearColor{}; switch (vulkanFormat.internalFormatRepresentation) diff --git a/src/modules/graphics/vulkan/Vulkan.cpp b/src/modules/graphics/vulkan/Vulkan.cpp index 6660d5f02..633299c83 100644 --- a/src/modules/graphics/vulkan/Vulkan.cpp +++ b/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{}; + if (sRGB) + format = getSRGBPixelFormat(format); + + if (!isPixelFormatCompressed(format) && !isPixelFormatSRGB(format)) + sRGB = false; + switch (format) { case PIXELFORMAT_UNKNOWN: @@ -331,13 +337,13 @@ TextureFormat Vulkan::getTextureFormat(PixelFormat format) textureFormat.internalFormat = VK_FORMAT_D32_SFLOAT_S8_UINT; break; 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; 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; 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; case PIXELFORMAT_BC4_UNORM: textureFormat.internalFormat = VK_FORMAT_BC4_UNORM_BLOCK; @@ -358,31 +364,31 @@ TextureFormat Vulkan::getTextureFormat(PixelFormat format) textureFormat.internalFormat = VK_FORMAT_BC6H_SFLOAT_BLOCK; break; 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; 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; 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; 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; 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; 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; 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; 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; 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; case PIXELFORMAT_EAC_R_UNORM: 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; break; 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; 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; 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; 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; 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; 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; 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; 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; 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; 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; 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; 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; 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; 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; default: throw love::Exception("unknown pixel format"); diff --git a/src/modules/graphics/vulkan/Vulkan.h b/src/modules/graphics/vulkan/Vulkan.h index 98fdb1a29..b57cd39d6 100644 --- a/src/modules/graphics/vulkan/Vulkan.h +++ b/src/modules/graphics/vulkan/Vulkan.h @@ -61,7 +61,7 @@ public: static uint32_t getSupportedVulkanApiVersion(uint32_t suggested); 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 getVulkanApiVersion(uint32_t apiVersion); static VkPrimitiveTopology getPrimitiveTypeTopology(graphics::PrimitiveType);