From 53a2ebbd17e21d1ab44beebacfd2dfe84b19ad5f Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Wed, 20 Dec 2023 15:12:12 +0100 Subject: [PATCH] Add more ASTC texture formats with larger block sizes --- .../Engine/Graphics/PixelFormatExtensions.cpp | 76 ++++++++++++++----- Source/Engine/Graphics/RenderTools.cpp | 6 ++ .../Engine/Graphics/Textures/GPUTexture.cpp | 9 ++- .../Vulkan/RenderToolsVulkan.cpp | 8 +- .../Tools/TextureTool/TextureTool.astc.cpp | 6 +- 5 files changed, 80 insertions(+), 25 deletions(-) diff --git a/Source/Engine/Graphics/PixelFormatExtensions.cpp b/Source/Engine/Graphics/PixelFormatExtensions.cpp index 4431870ee..2f3aa3931 100644 --- a/Source/Engine/Graphics/PixelFormatExtensions.cpp +++ b/Source/Engine/Graphics/PixelFormatExtensions.cpp @@ -29,6 +29,21 @@ void PixelFormatExtensions::Init() PixelFormat::R8_Typeless, PixelFormat::R8_UInt, PixelFormat::R8_UNorm, + PixelFormat::BC2_Typeless, + PixelFormat::BC2_UNorm, + PixelFormat::BC2_UNorm_sRGB, + PixelFormat::BC3_Typeless, + PixelFormat::BC3_UNorm, + PixelFormat::BC3_UNorm_sRGB, + PixelFormat::BC5_SNorm, + PixelFormat::BC5_Typeless, + PixelFormat::BC5_UNorm, + PixelFormat::BC6H_Sf16, + PixelFormat::BC6H_Typeless, + PixelFormat::BC6H_Uf16, + PixelFormat::BC7_Typeless, + PixelFormat::BC7_UNorm, + PixelFormat::BC7_UNorm_sRGB, PixelFormat::ASTC_4x4_UNorm, PixelFormat::ASTC_4x4_UNorm_sRGB, }; @@ -134,25 +149,6 @@ void PixelFormatExtensions::Init() PixelFormat::BC4_UNorm, }; InitFormat(formats8, 4); - - PixelFormat formats9[] = { - PixelFormat::BC2_Typeless, - PixelFormat::BC2_UNorm, - PixelFormat::BC2_UNorm_sRGB, - PixelFormat::BC3_Typeless, - PixelFormat::BC3_UNorm, - PixelFormat::BC3_UNorm_sRGB, - PixelFormat::BC5_SNorm, - PixelFormat::BC5_Typeless, - PixelFormat::BC5_UNorm, - PixelFormat::BC6H_Sf16, - PixelFormat::BC6H_Typeless, - PixelFormat::BC6H_Uf16, - PixelFormat::BC7_Typeless, - PixelFormat::BC7_UNorm, - PixelFormat::BC7_UNorm_sRGB, - }; - InitFormat(formats9, 8); } int32 PixelFormatExtensions::SizeInBits(PixelFormat format) @@ -315,6 +311,12 @@ bool PixelFormatExtensions::IsCompressed(const PixelFormat format) case PixelFormat::BC7_UNorm_sRGB: case PixelFormat::ASTC_4x4_UNorm: case PixelFormat::ASTC_4x4_UNorm_sRGB: + case PixelFormat::ASTC_6x6_UNorm: + case PixelFormat::ASTC_6x6_UNorm_sRGB: + case PixelFormat::ASTC_8x8_UNorm: + case PixelFormat::ASTC_8x8_UNorm_sRGB: + case PixelFormat::ASTC_10x10_UNorm: + case PixelFormat::ASTC_10x10_UNorm_sRGB: return true; default: return false; @@ -358,6 +360,12 @@ bool PixelFormatExtensions::IsCompressedASTC(PixelFormat format) { case PixelFormat::ASTC_4x4_UNorm: case PixelFormat::ASTC_4x4_UNorm_sRGB: + case PixelFormat::ASTC_6x6_UNorm: + case PixelFormat::ASTC_6x6_UNorm_sRGB: + case PixelFormat::ASTC_8x8_UNorm: + case PixelFormat::ASTC_8x8_UNorm_sRGB: + case PixelFormat::ASTC_10x10_UNorm: + case PixelFormat::ASTC_10x10_UNorm_sRGB: return true; default: return false; @@ -391,6 +399,9 @@ bool PixelFormatExtensions::IsSRGB(const PixelFormat format) case PixelFormat::B8G8R8X8_UNorm_sRGB: case PixelFormat::BC7_UNorm_sRGB: case PixelFormat::ASTC_4x4_UNorm_sRGB: + case PixelFormat::ASTC_6x6_UNorm_sRGB: + case PixelFormat::ASTC_8x8_UNorm_sRGB: + case PixelFormat::ASTC_10x10_UNorm_sRGB: return true; default: return false; @@ -580,6 +591,12 @@ int32 PixelFormatExtensions::ComputeComponentsCount(const PixelFormat format) case PixelFormat::B8G8R8X8_UNorm_sRGB: case PixelFormat::ASTC_4x4_UNorm: case PixelFormat::ASTC_4x4_UNorm_sRGB: + case PixelFormat::ASTC_6x6_UNorm: + case PixelFormat::ASTC_6x6_UNorm_sRGB: + case PixelFormat::ASTC_8x8_UNorm: + case PixelFormat::ASTC_8x8_UNorm_sRGB: + case PixelFormat::ASTC_10x10_UNorm: + case PixelFormat::ASTC_10x10_UNorm_sRGB: return 4; case PixelFormat::R32G32B32_Typeless: case PixelFormat::R32G32B32_Float: @@ -669,6 +686,15 @@ int32 PixelFormatExtensions::ComputeBlockSize(PixelFormat format) case PixelFormat::ASTC_4x4_UNorm: case PixelFormat::ASTC_4x4_UNorm_sRGB: return 4; + case PixelFormat::ASTC_6x6_UNorm: + case PixelFormat::ASTC_6x6_UNorm_sRGB: + return 6; + case PixelFormat::ASTC_8x8_UNorm: + case PixelFormat::ASTC_8x8_UNorm_sRGB: + return 8; + case PixelFormat::ASTC_10x10_UNorm: + case PixelFormat::ASTC_10x10_UNorm_sRGB: + return 10; default: return 1; } @@ -694,6 +720,12 @@ PixelFormat PixelFormatExtensions::TosRGB(const PixelFormat format) return PixelFormat::BC7_UNorm_sRGB; case PixelFormat::ASTC_4x4_UNorm: return PixelFormat::ASTC_4x4_UNorm_sRGB; + case PixelFormat::ASTC_6x6_UNorm: + return PixelFormat::ASTC_6x6_UNorm_sRGB; + case PixelFormat::ASTC_8x8_UNorm: + return PixelFormat::ASTC_8x8_UNorm_sRGB; + case PixelFormat::ASTC_10x10_UNorm: + return PixelFormat::ASTC_10x10_UNorm_sRGB; default: return format; } @@ -719,6 +751,12 @@ PixelFormat PixelFormatExtensions::ToNonsRGB(const PixelFormat format) return PixelFormat::BC7_UNorm; case PixelFormat::ASTC_4x4_UNorm_sRGB: return PixelFormat::ASTC_4x4_UNorm; + case PixelFormat::ASTC_6x6_UNorm_sRGB: + return PixelFormat::ASTC_6x6_UNorm; + case PixelFormat::ASTC_8x8_UNorm_sRGB: + return PixelFormat::ASTC_8x8_UNorm; + case PixelFormat::ASTC_10x10_UNorm_sRGB: + return PixelFormat::ASTC_10x10_UNorm; default: return format; } diff --git a/Source/Engine/Graphics/RenderTools.cpp b/Source/Engine/Graphics/RenderTools.cpp index 5fc6c7d65..66e17896d 100644 --- a/Source/Engine/Graphics/RenderTools.cpp +++ b/Source/Engine/Graphics/RenderTools.cpp @@ -331,6 +331,12 @@ void RenderTools::ComputePitch(PixelFormat format, int32 width, int32 height, ui break; case PixelFormat::ASTC_4x4_UNorm: case PixelFormat::ASTC_4x4_UNorm_sRGB: + case PixelFormat::ASTC_6x6_UNorm: + case PixelFormat::ASTC_6x6_UNorm_sRGB: + case PixelFormat::ASTC_8x8_UNorm: + case PixelFormat::ASTC_8x8_UNorm_sRGB: + case PixelFormat::ASTC_10x10_UNorm: + case PixelFormat::ASTC_10x10_UNorm_sRGB: { const int32 blockSize = PixelFormatExtensions::ComputeBlockSize(format); uint32 nbw = Math::Max(1, Math::DivideAndRoundUp(width, blockSize)); diff --git a/Source/Engine/Graphics/Textures/GPUTexture.cpp b/Source/Engine/Graphics/Textures/GPUTexture.cpp index 403f545ba..4d43e20ec 100644 --- a/Source/Engine/Graphics/Textures/GPUTexture.cpp +++ b/Source/Engine/Graphics/Textures/GPUTexture.cpp @@ -337,14 +337,12 @@ int32 GPUTexture::ComputeBufferOffset(int32 subresource, int32 rowAlign, int32 s int32 GPUTexture::ComputeBufferTotalSize(int32 rowAlign, int32 sliceAlign) const { int32 result = 0; - for (int32 mipLevel = 0; mipLevel < MipLevels(); mipLevel++) { const int32 slicePitch = ComputeSlicePitch(mipLevel, rowAlign); const int32 depth = CalculateMipSize(Depth(), mipLevel); result += Math::AlignUp(slicePitch * depth, sliceAlign); } - return result * ArraySize(); } @@ -355,8 +353,11 @@ int32 GPUTexture::ComputeSlicePitch(int32 mipLevel, int32 rowAlign) const int32 GPUTexture::ComputeRowPitch(int32 mipLevel, int32 rowAlign) const { - const int32 formatSize = PixelFormatExtensions::SizeInBytes(Format()); - return Math::AlignUp(CalculateMipSize(Width(), mipLevel) * formatSize, rowAlign); + int32 mipWidth = CalculateMipSize(Width(), mipLevel); + int32 mipHeight = CalculateMipSize(Height(), mipLevel); + uint32 rowPitch, slicePitch; + RenderTools::ComputePitch(Format(), mipWidth, mipHeight, rowPitch, slicePitch); + return Math::AlignUp(rowPitch, rowAlign); } bool GPUTexture::Init(const GPUTextureDescription& desc) diff --git a/Source/Engine/GraphicsDevice/Vulkan/RenderToolsVulkan.cpp b/Source/Engine/GraphicsDevice/Vulkan/RenderToolsVulkan.cpp index 3a5a91e5e..f9c9237f6 100644 --- a/Source/Engine/GraphicsDevice/Vulkan/RenderToolsVulkan.cpp +++ b/Source/Engine/GraphicsDevice/Vulkan/RenderToolsVulkan.cpp @@ -6,7 +6,7 @@ #include "Engine/Core/Types/StringBuilder.h" #include "Engine/Core/Log.h" -VkFormat RenderToolsVulkan::PixelFormatToVkFormat[102] = +VkFormat RenderToolsVulkan::PixelFormatToVkFormat[108] = { VK_FORMAT_UNDEFINED, VK_FORMAT_R32G32B32A32_SFLOAT, @@ -110,6 +110,12 @@ VkFormat RenderToolsVulkan::PixelFormatToVkFormat[102] = VK_FORMAT_BC7_SRGB_BLOCK, VK_FORMAT_ASTC_4x4_UNORM_BLOCK, VK_FORMAT_ASTC_4x4_SRGB_BLOCK, + VK_FORMAT_ASTC_6x6_UNORM_BLOCK, + VK_FORMAT_ASTC_6x6_SRGB_BLOCK, + VK_FORMAT_ASTC_8x8_UNORM_BLOCK, + VK_FORMAT_ASTC_8x8_SRGB_BLOCK, + VK_FORMAT_ASTC_10x10_UNORM_BLOCK, + VK_FORMAT_ASTC_10x10_SRGB_BLOCK, }; VkBlendFactor RenderToolsVulkan::BlendToVkBlendFactor[20] = diff --git a/Source/Engine/Tools/TextureTool/TextureTool.astc.cpp b/Source/Engine/Tools/TextureTool/TextureTool.astc.cpp index 3dcd8bb72..3eceb837e 100644 --- a/Source/Engine/Tools/TextureTool/TextureTool.astc.cpp +++ b/Source/Engine/Tools/TextureTool/TextureTool.astc.cpp @@ -8,6 +8,7 @@ #include "Engine/Core/Math/Color32.h" #include "Engine/Graphics/Textures/TextureData.h" #include "Engine/Graphics/PixelFormatExtensions.h" +#include "Engine/Graphics/RenderTools.h" #include bool TextureTool::ConvertAstc(TextureData& dst, const TextureData& src, const PixelFormat dstFormat) @@ -87,7 +88,10 @@ bool TextureTool::ConvertAstc(TextureData& dst, const TextureData& src, const Pi auto mipHeight = Math::Max(textureData->Height >> mipIndex, 1); auto blocksWidth = Math::Max(Math::DivideAndRoundUp(mipWidth, blockSize), 1); auto blocksHeight = Math::Max(Math::DivideAndRoundUp(mipHeight, blockSize), 1); - ASSERT(srcMip.RowPitch == mipWidth * PixelFormatExtensions::SizeInBytes(textureData->Format)); + uint32 mipRowPitch, mipSlicePitch; + RenderTools::ComputePitch(textureData->Format, mipWidth, mipHeight, mipRowPitch, mipSlicePitch); + ASSERT(srcMip.RowPitch == mipRowPitch); + ASSERT(srcMip.DepthPitch == mipSlicePitch); ASSERT(srcMip.Lines == mipHeight); // Allocate memory