From ae5fa9619bb19d6a7f53be6cb157e0f8ee156b16 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Thu, 14 Dec 2023 18:23:17 +0100 Subject: [PATCH] Remove `TextureUtils` --- .../Engine/ContentImporters/ImportTexture.cpp | 1 - .../Engine/Graphics/Textures/TextureUtils.h | 61 ------------------- .../TextureTool/TextureTool.DirectXTex.cpp | 21 ++----- .../Engine/Tools/TextureTool/TextureTool.cpp | 48 +++++++++++++++ Source/Engine/Tools/TextureTool/TextureTool.h | 4 +- .../Tools/TextureTool/TextureTool.stb.cpp | 3 +- 6 files changed, 55 insertions(+), 83 deletions(-) delete mode 100644 Source/Engine/Graphics/Textures/TextureUtils.h diff --git a/Source/Engine/ContentImporters/ImportTexture.cpp b/Source/Engine/ContentImporters/ImportTexture.cpp index 51b93420c..ebc7e514c 100644 --- a/Source/Engine/ContentImporters/ImportTexture.cpp +++ b/Source/Engine/ContentImporters/ImportTexture.cpp @@ -8,7 +8,6 @@ #include "Engine/Serialization/MemoryWriteStream.h" #include "Engine/Serialization/MemoryReadStream.h" #include "Engine/Graphics/Textures/TextureData.h" -#include "Engine/Graphics/Textures/TextureUtils.h" #include "Engine/Graphics/PixelFormatExtensions.h" #include "Engine/Content/Storage/ContentStorageManager.h" #include "Engine/ContentImporters/ImportIES.h" diff --git a/Source/Engine/Graphics/Textures/TextureUtils.h b/Source/Engine/Graphics/Textures/TextureUtils.h deleted file mode 100644 index b021d27fe..000000000 --- a/Source/Engine/Graphics/Textures/TextureUtils.h +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. - -#pragma once - -#include "Engine/Core/Types/BaseTypes.h" -#include "Types.h" - -/// -/// Texture utilities class -/// -class TextureUtils -{ -public: - static PixelFormat ToPixelFormat(const TextureFormatType format, int32 width, int32 height, bool canCompress) - { - const bool canUseBlockCompression = width % 4 == 0 && height % 4 == 0; - if (canCompress && canUseBlockCompression) - { - switch (format) - { - case TextureFormatType::ColorRGB: - return PixelFormat::BC1_UNorm; - case TextureFormatType::ColorRGBA: - return PixelFormat::BC3_UNorm; - case TextureFormatType::NormalMap: - return PixelFormat::BC5_UNorm; - case TextureFormatType::GrayScale: - return PixelFormat::BC4_UNorm; - case TextureFormatType::HdrRGBA: - return PixelFormat::BC7_UNorm; - case TextureFormatType::HdrRGB: -#if PLATFORM_LINUX - // TODO: support BC6H compression for Linux Editor - return PixelFormat::BC7_UNorm; -#else - return PixelFormat::BC6H_Uf16; -#endif - default: - return PixelFormat::Unknown; - } - } - - switch (format) - { - case TextureFormatType::ColorRGB: - return PixelFormat::R8G8B8A8_UNorm; - case TextureFormatType::ColorRGBA: - return PixelFormat::R8G8B8A8_UNorm; - case TextureFormatType::NormalMap: - return PixelFormat::R16G16_UNorm; - case TextureFormatType::GrayScale: - return PixelFormat::R8_UNorm; - case TextureFormatType::HdrRGBA: - return PixelFormat::R16G16B16A16_Float; - case TextureFormatType::HdrRGB: - return PixelFormat::R11G11B10_Float; - default: - return PixelFormat::Unknown; - } - } -}; diff --git a/Source/Engine/Tools/TextureTool/TextureTool.DirectXTex.cpp b/Source/Engine/Tools/TextureTool/TextureTool.DirectXTex.cpp index 17929bed0..fced3423d 100644 --- a/Source/Engine/Tools/TextureTool/TextureTool.DirectXTex.cpp +++ b/Source/Engine/Tools/TextureTool/TextureTool.DirectXTex.cpp @@ -10,7 +10,6 @@ #include "Engine/Platform/ConditionVariable.h" #include "Engine/Graphics/RenderTools.h" #include "Engine/Graphics/Async/GPUTask.h" -#include "Engine/Graphics/Textures/TextureUtils.h" #include "Engine/Graphics/Textures/TextureData.h" #include "Engine/Graphics/PixelFormatExtensions.h" #if USE_EDITOR @@ -318,7 +317,7 @@ bool TextureTool::ImportTextureDirectXTex(ImageType type, const StringView& path textureData.Width = (int32)meta.width; textureData.Height = (int32)meta.height; textureData.Depth = (int32)meta.depth; - textureData.Format = ToPixelFormat(meta.format); + textureData.Format = ::ToPixelFormat(meta.format); textureData.Items.Resize(1); textureData.Items.Resize((int32)meta.arraySize); for (int32 arrayIndex = 0; arrayIndex < (int32)meta.arraySize; arrayIndex++) @@ -598,7 +597,7 @@ bool TextureTool::ImportTextureDirectXTex(ImageType type, const StringView& path float alphaThreshold = 0.3f; bool isPowerOfTwo = Math::IsPowerOfTwo(width) && Math::IsPowerOfTwo(height); DXGI_FORMAT sourceDxgiFormat = currentImage->GetMetadata().format; - PixelFormat targetFormat = TextureUtils::ToPixelFormat(options.Type, width, height, options.Compress); + PixelFormat targetFormat = TextureTool::ToPixelFormat(options.Type, width, height, options.Compress); if (options.sRGB) targetFormat = PixelFormatExtensions::TosRGB(targetFormat); DXGI_FORMAT targetDxgiFormat = ToDxgiFormat(targetFormat); @@ -638,14 +637,13 @@ bool TextureTool::ImportTextureDirectXTex(ImageType type, const StringView& path // Keep image in the current compressed format (artist choice) so we don't have to run the slow mipmap generation keepAsIs = true; targetDxgiFormat = sourceDxgiFormat; - targetFormat = ToPixelFormat(currentImage->GetMetadata().format); + targetFormat = ::ToPixelFormat(currentImage->GetMetadata().format); } // Decompress if texture is compressed (next steps need decompressed input data, for eg. mip maps generation or format changing) if (!keepAsIs && DirectX::IsCompressed(sourceDxgiFormat)) { auto& tmpImg = GET_TMP_IMG(); - sourceDxgiFormat = DXGI_FORMAT_R16G16B16A16_FLOAT; result = Decompress(currentImage->GetImages(), currentImage->GetImageCount(), currentImage->GetMetadata(), sourceDxgiFormat, tmpImg); if (FAILED(result)) @@ -653,14 +651,13 @@ bool TextureTool::ImportTextureDirectXTex(ImageType type, const StringView& path errorMsg = String::Format(TEXT("Cannot decompress texture, error: {0:x}"), static_cast(result)); return true; } - SET_CURRENT_IMG(tmpImg); } // Fix sRGB problem if (!keepAsIs && DirectX::IsSRGB(sourceDxgiFormat)) { - sourceDxgiFormat = ToDxgiFormat(PixelFormatExtensions::ToNonsRGB(ToPixelFormat(sourceDxgiFormat))); + sourceDxgiFormat = ToDxgiFormat(PixelFormatExtensions::ToNonsRGB(::ToPixelFormat(sourceDxgiFormat))); ((DirectX::TexMetadata&)currentImage->GetMetadata()).format = sourceDxgiFormat; for (size_t i = 0; i < currentImage->GetImageCount(); i++) ((DirectX::Image*)currentImage->GetImages())[i].format = sourceDxgiFormat; @@ -670,12 +667,10 @@ bool TextureTool::ImportTextureDirectXTex(ImageType type, const StringView& path if (!keepAsIs && DirectX::HasAlpha(sourceDxgiFormat) && options.Type == TextureFormatType::ColorRGB && options.Compress) { auto& tmpImg = GET_TMP_IMG(); - result = TransformImage(currentImage->GetImages(), currentImage->GetImageCount(), currentImage->GetMetadata(), [](DirectX::XMVECTOR* outPixels, const DirectX::XMVECTOR* inPixels, size_t width, size_t y) { UNREFERENCED_PARAMETER(y); - for (size_t j = 0; j < width; j++) { outPixels[j] = DirectX::XMVectorSelect(DirectX::g_XMOne, inPixels[j], DirectX::g_XMSelect1110); @@ -686,8 +681,6 @@ bool TextureTool::ImportTextureDirectXTex(ImageType type, const StringView& path errorMsg = String::Format(TEXT("Cannot transform texture to remove unwanted alpha channel, error: {0:x}"), static_cast(result)); return true; } - - // Use converted image SET_CURRENT_IMG(tmpImg); } @@ -695,7 +688,6 @@ bool TextureTool::ImportTextureDirectXTex(ImageType type, const StringView& path if (!keepAsIs && options.FlipY) { auto& tmpImg = GET_TMP_IMG(); - DWORD flags = DirectX::TEX_FR_FLIP_VERTICAL; result = FlipRotate(currentImage->GetImages(), currentImage->GetImageCount(), currentImage->GetMetadata(), flags, tmpImg); if (FAILED(result)) @@ -703,8 +695,6 @@ bool TextureTool::ImportTextureDirectXTex(ImageType type, const StringView& path errorMsg = String::Format(TEXT("Cannot rotate/flip texture, error: {0:x}"), static_cast(result)); return true; } - - // Use converted image SET_CURRENT_IMG(tmpImg); } @@ -712,7 +702,6 @@ bool TextureTool::ImportTextureDirectXTex(ImageType type, const StringView& path if (!keepAsIs && options.InvertGreenChannel) { auto& timage = GET_TMP_IMG(); - result = TransformImage(currentImage->GetImages(), currentImage->GetImageCount(), currentImage->GetMetadata(), [&](DirectX::XMVECTOR* outPixels, const DirectX::XMVECTOR* inPixels, size_t w, size_t y) { @@ -734,7 +723,6 @@ bool TextureTool::ImportTextureDirectXTex(ImageType type, const StringView& path errorMsg = String::Format(TEXT("Cannot invert green channel in texture, error: {0:x}"), static_cast(result)); return true; } - // Use converted image SET_CURRENT_IMG(timage); } @@ -757,7 +745,6 @@ bool TextureTool::ImportTextureDirectXTex(ImageType type, const StringView& path errorMsg = String::Format(TEXT("Cannot generate texture mip maps chain, error: {1:x}"), *path, static_cast(result)); return true; } - SET_CURRENT_IMG(tmpImg); } diff --git a/Source/Engine/Tools/TextureTool/TextureTool.cpp b/Source/Engine/Tools/TextureTool/TextureTool.cpp index c64134e46..c2f5c62dc 100644 --- a/Source/Engine/Tools/TextureTool/TextureTool.cpp +++ b/Source/Engine/Tools/TextureTool/TextureTool.cpp @@ -679,6 +679,54 @@ Color TextureTool::SampleLinear(const PixelFormatSampler* sampler, const Float2& return Color::Lerp(Color::Lerp(v00, v01, uvFraction.X), Color::Lerp(v10, v11, uvFraction.X), uvFraction.Y); } +PixelFormat TextureTool::ToPixelFormat(TextureFormatType format, int32 width, int32 height, bool canCompress) +{ + const bool canUseBlockCompression = width % 4 == 0 && height % 4 == 0; + if (canCompress && canUseBlockCompression) + { + switch (format) + { + case TextureFormatType::ColorRGB: + return PixelFormat::BC1_UNorm; + case TextureFormatType::ColorRGBA: + return PixelFormat::BC3_UNorm; + case TextureFormatType::NormalMap: + return PixelFormat::BC5_UNorm; + case TextureFormatType::GrayScale: + return PixelFormat::BC4_UNorm; + case TextureFormatType::HdrRGBA: + return PixelFormat::BC7_UNorm; + case TextureFormatType::HdrRGB: +#if PLATFORM_LINUX + // TODO: support BC6H compression for Linux Editor + return PixelFormat::BC7_UNorm; +#else + return PixelFormat::BC6H_Uf16; +#endif + default: + return PixelFormat::Unknown; + } + } + + switch (format) + { + case TextureFormatType::ColorRGB: + return PixelFormat::R8G8B8A8_UNorm; + case TextureFormatType::ColorRGBA: + return PixelFormat::R8G8B8A8_UNorm; + case TextureFormatType::NormalMap: + return PixelFormat::R16G16_UNorm; + case TextureFormatType::GrayScale: + return PixelFormat::R8_UNorm; + case TextureFormatType::HdrRGBA: + return PixelFormat::R16G16B16A16_Float; + case TextureFormatType::HdrRGB: + return PixelFormat::R11G11B10_Float; + default: + return PixelFormat::Unknown; + } +} + bool TextureTool::GetImageType(const StringView& path, ImageType& type) { const auto extension = FileSystem::GetExtension(path).ToLower(); diff --git a/Source/Engine/Tools/TextureTool/TextureTool.h b/Source/Engine/Tools/TextureTool/TextureTool.h index 351167b3c..08d2e77e7 100644 --- a/Source/Engine/Tools/TextureTool/TextureTool.h +++ b/Source/Engine/Tools/TextureTool/TextureTool.h @@ -110,14 +110,12 @@ API_CLASS(Namespace="FlaxEngine.Tools", Static) class FLAXENGINE_API TextureTool public: #if USE_EDITOR - /// /// Checks whenever the given texture file contains alpha channel data with values different than solid fill of 1 (non fully opaque). /// /// The file path. /// True if has alpha channel, otherwise false. static bool HasAlpha(const StringView& path); - #endif /// @@ -240,6 +238,8 @@ public: /// The sampled color (linear). static Color SampleLinear(const PixelFormatSampler* sampler, const Float2& uv, const void* data, const Int2& size, int32 rowPitch); + static PixelFormat ToPixelFormat(TextureFormatType format, int32 width, int32 height, bool canCompress); + private: enum class ImageType { diff --git a/Source/Engine/Tools/TextureTool/TextureTool.stb.cpp b/Source/Engine/Tools/TextureTool/TextureTool.stb.cpp index 9fd7f554b..97fe8c198 100644 --- a/Source/Engine/Tools/TextureTool/TextureTool.stb.cpp +++ b/Source/Engine/Tools/TextureTool/TextureTool.stb.cpp @@ -9,7 +9,6 @@ #include "Engine/Serialization/FileWriteStream.h" #include "Engine/Graphics/RenderTools.h" #include "Engine/Graphics/Textures/TextureData.h" -#include "Engine/Graphics/Textures/TextureUtils.h" #include "Engine/Graphics/PixelFormatExtensions.h" #include "Engine/Platform/File.h" @@ -459,7 +458,7 @@ bool TextureTool::ImportTextureStb(ImageType type, const StringView& path, Textu // Cache data float alphaThreshold = 0.3f; bool isPowerOfTwo = Math::IsPowerOfTwo(width) && Math::IsPowerOfTwo(height); - PixelFormat targetFormat = TextureUtils::ToPixelFormat(options.Type, width, height, options.Compress); + PixelFormat targetFormat = ToPixelFormat(options.Type, width, height, options.Compress); if (options.sRGB) targetFormat = PixelFormatExtensions::TosRGB(targetFormat);