From e9d5acd888ae77008cb62810bb36f1e89aeea6ee Mon Sep 17 00:00:00 2001 From: DarkShadow Date: Sat, 12 Oct 2024 00:32:52 +0300 Subject: [PATCH] Improve sRGB displaying, fix auto compression for sRGB textures --- .../TelltaleTextureTool/Converter.cs | 2 +- .../GUI/Image/ImageData.cs | 2 +- .../Graphics/TextureManager.cs | 62 ++++++++++++------- 3 files changed, 41 insertions(+), 25 deletions(-) diff --git a/TelltaleTextureTool/TelltaleTextureTool/Converter.cs b/TelltaleTextureTool/TelltaleTextureTool/Converter.cs index 7fff21d..2da9ea7 100644 --- a/TelltaleTextureTool/TelltaleTextureTool/Converter.cs +++ b/TelltaleTextureTool/TelltaleTextureTool/Converter.cs @@ -248,7 +248,7 @@ public static void ConvertTextureFromOthersToD3Dtx(string sourceFilePath, string options.IsSRGB = true; } - texture.TransformTexture(options, true, false); + texture.TransformTexture(options, true, true); // Get the image texture.GetDDSInformation(out D3DTXMetadata metadata, out ImageSection[] sections, flags); diff --git a/TelltaleTextureTool/TelltaleTextureTool/GUI/Image/ImageData.cs b/TelltaleTextureTool/TelltaleTextureTool/GUI/Image/ImageData.cs index e8b0966..8e82dcf 100644 --- a/TelltaleTextureTool/TelltaleTextureTool/GUI/Image/ImageData.cs +++ b/TelltaleTextureTool/TelltaleTextureTool/GUI/Image/ImageData.cs @@ -70,7 +70,7 @@ public void ApplyEffects(ImageAdvancedOptions options) { try { - DDSImage.TransformTexture(options, false, true); + DDSImage.TransformTexture(options, true, false); DDSImage.GetBounds(out uint maxMip, out uint maxFace); MaxMip = maxMip; diff --git a/TelltaleTextureTool/TelltaleTextureTool/Graphics/TextureManager.cs b/TelltaleTextureTool/TelltaleTextureTool/Graphics/TextureManager.cs index b59e2a7..8c83acb 100644 --- a/TelltaleTextureTool/TelltaleTextureTool/Graphics/TextureManager.cs +++ b/TelltaleTextureTool/TelltaleTextureTool/Graphics/TextureManager.cs @@ -499,7 +499,7 @@ public byte[] GetSectionPixelData(uint mip, uint face) { ScratchImage newDestImage = DirectXTex.CreateScratchImage(); - DirectXTex.Decompress(destImage.GetImage(0, 0, 0), (int)DXGIFormat.R8G8B8A8_UNORM, ref newDestImage).ThrowIf(); + DirectXTex.Decompress(destImage.GetImage(0, 0, 0), (int)DXGIFormat.UNKNOWN, ref newDestImage).ThrowIf(); destImage.Release(); destImage = newDestImage; @@ -509,7 +509,10 @@ public byte[] GetSectionPixelData(uint mip, uint face) { ScratchImage newDestImage = DirectXTex.CreateScratchImage(); - DirectXTex.Convert(destImage.GetImage(0, 0, 0), (int)DXGIFormat.R8G8B8A8_UNORM, TexFilterFlags.Default, 0.5f, ref newDestImage).ThrowIf(); + if (DirectXTex.IsSRGB(destImage.GetMetadata().Format)) + DirectXTex.Convert(destImage.GetImage(0, 0, 0), (int)DXGIFormat.R8G8B8A8_UNORM, TexFilterFlags.SrgbOut, 0.5f, ref newDestImage).ThrowIf(); + else + DirectXTex.Convert(destImage.GetImage(0, 0, 0), (int)DXGIFormat.R8G8B8A8_UNORM, TexFilterFlags.Default, 0.5f, ref newDestImage).ThrowIf(); destImage.Release(); destImage = newDestImage; @@ -577,7 +580,18 @@ public void Compress(DXGIFormat format = DXGIFormat.UNKNOWN) { TexMetadata originalMetadata = Image.GetMetadata(); - DirectXTex.Compress2(Image.GetImages(), Image.GetImageCount(), ref originalMetadata, (int)format, TexCompressFlags.Default, 0.5f, ref transformedImage).ThrowIf(); + TexCompressFlags flags = TexCompressFlags.Default; + + if (DirectXTex.IsSRGB(Image.GetMetadata().Format)) + { + flags |= TexCompressFlags.SrgbOut; + } + + if (DirectXTex.IsSRGB((int)format)){ + flags |= TexCompressFlags.SrgbIn; + } + + DirectXTex.Compress2(Image.GetImages(), Image.GetImageCount(), ref originalMetadata, (int)format, flags, 0.5f, ref transformedImage).ThrowIf(); Image.Release(); Image = transformedImage; @@ -731,7 +745,7 @@ public void TransformTexture(ImageAdvancedOptions options, bool keepOriginal = f Deswizzle(options.PlatformType); } - Decompress(DXGIFormat.R8G8B8A8_UNORM); + Decompress(); if (options.EnableNormalMap) { @@ -769,32 +783,35 @@ public void TransformTexture(ImageAdvancedOptions options, bool keepOriginal = f } } - if (options.EnableAutomaticCompression) + if (convertingOnly) { - if (options.EnableNormalMap && options.IsTelltaleXYNormalMap) - { - Compress(DXGIFormat.BC5_UNORM); - } - else if (OriginalImage.IsAlphaAllOpaque()) + if (options.EnableAutomaticCompression) { - if (options.IsSRGB) + if (options.EnableNormalMap && options.IsTelltaleXYNormalMap) { - Compress(DXGIFormat.BC1_UNORM_SRGB); + Compress(DXGIFormat.BC5_UNORM); } - else + else if (OriginalImage.IsAlphaAllOpaque()) { - Compress(DXGIFormat.BC1_UNORM); - } - } - else - { - if (options.IsSRGB) - { - Compress(DXGIFormat.BC3_UNORM_SRGB); + if (options.IsSRGB) + { + Compress(DXGIFormat.BC1_UNORM_SRGB); + } + else + { + Compress(DXGIFormat.BC1_UNORM); + } } else { - Compress(DXGIFormat.BC3_UNORM); + if (options.IsSRGB) + { + Compress(DXGIFormat.BC3_UNORM_SRGB); + } + else + { + Compress(DXGIFormat.BC3_UNORM); + } } } } @@ -803,7 +820,6 @@ public void TransformTexture(ImageAdvancedOptions options, bool keepOriginal = f Compress((DXGIFormat)OriginalImage.GetMetadata().Format); } - if (options.EnableSwizzle && options.IsSwizzle) { Swizzle(options.PlatformType);