From 3a066d610f381d22e9f732044a759c667015aab1 Mon Sep 17 00:00:00 2001 From: FrozenKresnik <80754402+Overflowu@users.noreply.github.com> Date: Tue, 1 Dec 2026 11:40:30 +0000 Subject: [PATCH 1/9] fix(runtime): check SystemInfo.IsFormatSupported in CanCreateAstcTexture even in Editor --- Assets/ASTC Encoder/Runtime/Encoder.cs | 3 --- Assets/Scripts/Sample.cs | 19 ++++++++++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Assets/ASTC Encoder/Runtime/Encoder.cs b/Assets/ASTC Encoder/Runtime/Encoder.cs index 6c7794c..5a13776 100644 --- a/Assets/ASTC Encoder/Runtime/Encoder.cs +++ b/Assets/ASTC Encoder/Runtime/Encoder.cs @@ -147,9 +147,6 @@ public static GraphicsFormat GetTargetAstcFormat(GraphicsFormat sourceFormat, Bl internal static bool CanCreateAstcTexture(GraphicsFormat format) { - if (Application.isEditor) - return true; - #if UNITY_2023_2_OR_NEWER return SystemInfo.IsFormatSupported(format, GraphicsFormatUsage.Sample); #else diff --git a/Assets/Scripts/Sample.cs b/Assets/Scripts/Sample.cs index 77488ac..c73688d 100644 --- a/Assets/Scripts/Sample.cs +++ b/Assets/Scripts/Sample.cs @@ -20,12 +20,29 @@ void Start() if (inputTexture == null) return; + bool includeMips = inputTexture.mipmapCount > 1; outputTexture = inputTexture.CompressToTexture( TextureType.Default, blockSize, quality, - includeMipmaps: inputTexture.mipmapCount > 1, + includeMipmaps: includeMips, backend: backend); + if (outputTexture == null) + { + // Host GPU does not support hardware ASTC sampling (e.g., Windows desktop Editor): + // encode to KTX and load with automatic CPU software-decompression fallback for preview. + var options = AstcCompressOptions.Default; + options.TextureType = TextureType.Default; + options.BlockSize = blockSize; + options.Quality = quality; + options.IncludeMipmaps = includeMips; + options.Backend = backend; + byte[] ktxBytes = inputTexture.EncodeToKTX(options); + if (ktxBytes != null) + { + outputTexture = Encoder.LoadFromKTX(ktxBytes, makeNoLongerReadable: false, textureName: inputTexture.name); + } + } m_OwnsOutputTexture = outputTexture != null; byte[] fileData = inputTexture.EncodeToASTC(TextureType.Default, blockSize, quality, backend: backend); From 45e3379ebdf5b0ecba6046df2ee76ca13963f4ed Mon Sep 17 00:00:00 2001 From: FrozenKresnik <80754402+Overflowu@users.noreply.github.com> Date: Tue, 1 Dec 2026 11:45:58 +0000 Subject: [PATCH 2/9] chore(sample): keep Sample.cs minimal without KTX fallback --- Assets/Scripts/Sample.cs | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/Assets/Scripts/Sample.cs b/Assets/Scripts/Sample.cs index c73688d..77488ac 100644 --- a/Assets/Scripts/Sample.cs +++ b/Assets/Scripts/Sample.cs @@ -20,29 +20,12 @@ void Start() if (inputTexture == null) return; - bool includeMips = inputTexture.mipmapCount > 1; outputTexture = inputTexture.CompressToTexture( TextureType.Default, blockSize, quality, - includeMipmaps: includeMips, + includeMipmaps: inputTexture.mipmapCount > 1, backend: backend); - if (outputTexture == null) - { - // Host GPU does not support hardware ASTC sampling (e.g., Windows desktop Editor): - // encode to KTX and load with automatic CPU software-decompression fallback for preview. - var options = AstcCompressOptions.Default; - options.TextureType = TextureType.Default; - options.BlockSize = blockSize; - options.Quality = quality; - options.IncludeMipmaps = includeMips; - options.Backend = backend; - byte[] ktxBytes = inputTexture.EncodeToKTX(options); - if (ktxBytes != null) - { - outputTexture = Encoder.LoadFromKTX(ktxBytes, makeNoLongerReadable: false, textureName: inputTexture.name); - } - } m_OwnsOutputTexture = outputTexture != null; byte[] fileData = inputTexture.EncodeToASTC(TextureType.Default, blockSize, quality, backend: backend); From 100189eb9d3075d85e44fe4f96218bcbec8f0a49 Mon Sep 17 00:00:00 2001 From: FrozenKresnik <80754402+Overflowu@users.noreply.github.com> Date: Tue, 1 Dec 2026 11:51:08 +0000 Subject: [PATCH 3/9] fix(shader): initialize unquantOut in AstcQuantizeWeight to silence D3D11 FXC warning --- Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl | 1 + 1 file changed, 1 insertion(+) diff --git a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl index 71be3b7..64f9464 100644 --- a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl +++ b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl @@ -393,6 +393,7 @@ uint AstcQuantizePow2Weight(float w01, uint maxK, uint b, out uint unquantOut) // Quantizes a normalized weight w01 in [0, 1] to the exact nearest BISE code and returns unquantized [0, 64]. uint AstcQuantizeWeight(float w01, uint q, out uint unquantOut) { + unquantOut = 0u; uint rangeVal, b, t, qu; AstcGetQuantParams(q, rangeVal, b, t, qu); From 55f7a4513ee939ca729304b38e534d856ddad889 Mon Sep 17 00:00:00 2001 From: FrozenKresnik <80754402+Overflowu@users.noreply.github.com> Date: Tue, 1 Dec 2026 12:03:06 +0000 Subject: [PATCH 4/9] perf(shader): split encoder into 12 per-block LDR/HDR shaders, unify texture output + CopyTexToBuf, drop editor_sync_compilation --- .github/workflows/verify-shaders-and-code.yml | 16 +- .../Runtime/Gpu/AstcGpuBackend.cs | 304 +++++++++--------- .../Runtime/Gpu/Shaders/AstcGpuEntry.hlsl | 37 +++ .../Gpu/Shaders/AstcGpuEntry.hlsl.meta | 7 + .../Resources/AstcGpuBufferCopy.compute | 18 ++ ...te.meta => AstcGpuBufferCopy.compute.meta} | 2 +- .../Runtime/Resources/AstcGpuEncoder.compute | 138 -------- .../Resources/AstcGpuEncoder_10x10.compute | 9 + .../AstcGpuEncoder_10x10.compute.meta | 8 + .../AstcGpuEncoder_10x10_Hdr.compute | 9 + .../AstcGpuEncoder_10x10_Hdr.compute.meta | 8 + .../Resources/AstcGpuEncoder_12x12.compute | 9 + .../AstcGpuEncoder_12x12.compute.meta | 8 + .../AstcGpuEncoder_12x12_Hdr.compute | 9 + .../AstcGpuEncoder_12x12_Hdr.compute.meta | 8 + .../Resources/AstcGpuEncoder_4x4.compute | 9 + .../Resources/AstcGpuEncoder_4x4.compute.meta | 8 + .../Resources/AstcGpuEncoder_4x4_Hdr.compute | 9 + .../AstcGpuEncoder_4x4_Hdr.compute.meta | 8 + .../Resources/AstcGpuEncoder_5x5.compute | 9 + .../Resources/AstcGpuEncoder_5x5.compute.meta | 8 + .../Resources/AstcGpuEncoder_5x5_Hdr.compute | 9 + .../AstcGpuEncoder_5x5_Hdr.compute.meta | 8 + .../Resources/AstcGpuEncoder_6x6.compute | 9 + .../Resources/AstcGpuEncoder_6x6.compute.meta | 8 + .../Resources/AstcGpuEncoder_6x6_Hdr.compute | 9 + .../AstcGpuEncoder_6x6_Hdr.compute.meta | 8 + .../Resources/AstcGpuEncoder_8x8.compute | 9 + .../Resources/AstcGpuEncoder_8x8.compute.meta | 8 + .../Resources/AstcGpuEncoder_8x8_Hdr.compute | 9 + .../AstcGpuEncoder_8x8_Hdr.compute.meta | 8 + 31 files changed, 429 insertions(+), 297 deletions(-) create mode 100644 Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuEntry.hlsl create mode 100644 Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuEntry.hlsl.meta create mode 100644 Assets/ASTC Encoder/Runtime/Resources/AstcGpuBufferCopy.compute rename Assets/ASTC Encoder/Runtime/Resources/{AstcGpuEncoder.compute.meta => AstcGpuBufferCopy.compute.meta} (79%) delete mode 100644 Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder.compute create mode 100644 Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_10x10.compute create mode 100644 Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_10x10.compute.meta create mode 100644 Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_10x10_Hdr.compute create mode 100644 Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_10x10_Hdr.compute.meta create mode 100644 Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_12x12.compute create mode 100644 Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_12x12.compute.meta create mode 100644 Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_12x12_Hdr.compute create mode 100644 Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_12x12_Hdr.compute.meta create mode 100644 Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_4x4.compute create mode 100644 Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_4x4.compute.meta create mode 100644 Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_4x4_Hdr.compute create mode 100644 Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_4x4_Hdr.compute.meta create mode 100644 Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_5x5.compute create mode 100644 Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_5x5.compute.meta create mode 100644 Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_5x5_Hdr.compute create mode 100644 Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_5x5_Hdr.compute.meta create mode 100644 Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_6x6.compute create mode 100644 Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_6x6.compute.meta create mode 100644 Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_6x6_Hdr.compute create mode 100644 Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_6x6_Hdr.compute.meta create mode 100644 Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_8x8.compute create mode 100644 Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_8x8.compute.meta create mode 100644 Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_8x8_Hdr.compute create mode 100644 Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_8x8_Hdr.compute.meta diff --git a/.github/workflows/verify-shaders-and-code.yml b/.github/workflows/verify-shaders-and-code.yml index 20803f7..257b5a7 100644 --- a/.github/workflows/verify-shaders-and-code.yml +++ b/.github/workflows/verify-shaders-and-code.yml @@ -14,15 +14,19 @@ jobs: - name: Checkout repository uses: actions/checkout@v4 - - name: Preprocess all HLSL block-size and Fast-only variants + - name: Preprocess all HLSL block-size, quality, and LDR/HDR variants run: | set -e - SHADERS="Assets/ASTC Encoder/Runtime/Gpu/Shaders" - for bx in 4 5 6 8 10 12; do - clang -E -P -x c -I"$SHADERS" -DBLOCK_X=$bx -DBLOCK_Y=$bx "$SHADERS/AstcGpuCore.hlsl" > /dev/null - clang -E -P -x c -I"$SHADERS" -DBLOCK_X=$bx -DBLOCK_Y=$bx -DASTC_FAST_ONLY=1 "$SHADERS/AstcGpuCore.hlsl" > /dev/null + RES="Assets/ASTC Encoder/Runtime/Resources" + for f in "$RES"/AstcGpuEncoder_*.compute; do + EXTRA="" + case "$f" in *_Hdr.compute) EXTRA="-DASTC_HDR_VARIANT=1" ;; esac + clang -E -P -x c $EXTRA "$f" > /dev/null + clang -E -P -x c $EXTRA -DASTC_NO_2P=1 "$f" > /dev/null + clang -E -P -x c $EXTRA -DASTC_FAST_ONLY=1 "$f" > /dev/null done - clang -E -P -x c "Assets/ASTC Encoder/Runtime/Resources/AstcGpuMipGenerator.compute" > /dev/null + clang -E -P -x c "$RES/AstcGpuBufferCopy.compute" > /dev/null + clang -E -P -x c "$RES/AstcGpuMipGenerator.compute" > /dev/null echo "All HLSL shader variants preprocessed cleanly!" - name: Verify Unity .meta files and GUID uniqueness diff --git a/Assets/ASTC Encoder/Runtime/Gpu/AstcGpuBackend.cs b/Assets/ASTC Encoder/Runtime/Gpu/AstcGpuBackend.cs index c064711..7cd47e2 100644 --- a/Assets/ASTC Encoder/Runtime/Gpu/AstcGpuBackend.cs +++ b/Assets/ASTC Encoder/Runtime/Gpu/AstcGpuBackend.cs @@ -13,10 +13,30 @@ namespace Aperture.Astcenc.Runtime { internal static class AstcGpuBackend { - private const string k_ShaderResourcePath = "AstcGpuEncoder"; + private const string k_CopyShaderResourcePath = "AstcGpuBufferCopy"; private const string k_MipShaderResourcePath = "AstcGpuMipGenerator"; private const int k_BytesPerBlock = 16; + private static readonly string[] s_LdrShaderNames = + { + "AstcGpuEncoder_4x4", + "AstcGpuEncoder_5x5", + "AstcGpuEncoder_6x6", + "AstcGpuEncoder_8x8", + "AstcGpuEncoder_10x10", + "AstcGpuEncoder_12x12", + }; + + private static readonly string[] s_HdrShaderNames = + { + "AstcGpuEncoder_4x4_Hdr", + "AstcGpuEncoder_5x5_Hdr", + "AstcGpuEncoder_6x6_Hdr", + "AstcGpuEncoder_8x8_Hdr", + "AstcGpuEncoder_10x10_Hdr", + "AstcGpuEncoder_12x12_Hdr", + }; + private const uint k_ModeLdrRgb = 0u; private const uint k_ModeLdrRgba = 1u; private const uint k_ModeLdrNormal = 2u; @@ -25,6 +45,7 @@ internal static class AstcGpuBackend private static readonly int s_PropSourceTex = Shader.PropertyToID("_AstcSourceTex"); private static readonly int s_PropDestTex = Shader.PropertyToID("_AstcDestTex"); + private static readonly int s_PropSrcBlockTex = Shader.PropertyToID("_AstcSrcBlockTex"); private static readonly int s_PropDestBuffer = Shader.PropertyToID("_AstcDestBuffer"); private static readonly int s_PropSrcWidth = Shader.PropertyToID("_AstcSrcWidth"); private static readonly int s_PropSrcHeight = Shader.PropertyToID("_AstcSrcHeight"); @@ -48,15 +69,17 @@ internal static class AstcGpuBackend private static readonly int s_PropMipIsAlphaWeighted = Shader.PropertyToID("_MipIsAlphaWeighted"); private static readonly int s_PropMipIsHdr = Shader.PropertyToID("_MipIsHdr"); - private static ComputeShader s_Shader; - private static bool s_ShaderLoadAttempted; - private static LocalKeyword s_KwHdrVariant; - private static int[] s_TexKernels; - private static int[] s_BufKernels; - private static int[] s_TexMedKernels; - private static int[] s_BufMedKernels; - private static int[] s_TexFastKernels; - private static int[] s_BufFastKernels; + private static readonly ComputeShader[] s_LdrShaders = new ComputeShader[6]; + private static readonly bool[] s_LdrLoadAttempted = new bool[6]; + private static readonly int[,] s_LdrKernels = new int[6, 3]; + + private static readonly ComputeShader[] s_HdrShaders = new ComputeShader[6]; + private static readonly bool[] s_HdrLoadAttempted = new bool[6]; + private static readonly int[,] s_HdrKernels = new int[6, 3]; + + private static ComputeShader s_CopyShader; + private static bool s_CopyShaderLoadAttempted; + private static int s_CopyKernel; private static ComputeShader s_MipShader; private static bool s_MipShaderLoadAttempted; @@ -96,7 +119,7 @@ internal static bool IsSupported if (!IsLoadStoreSupported(GraphicsFormat.R32G32B32A32_UInt)) return false; - return EnsureShaderLoaded(); + return EnsureCopyShaderLoaded(); } } @@ -295,18 +318,61 @@ private static int GetDispatchGroupsY(int blockCountY, BlockSize blockSize) : (blockCountY + 7) >> 3; } - private static int GetTexKernel(int slot, uint qualityTier) + private static bool EnsureCopyShaderLoaded() { - if (qualityTier == 0u) return s_TexFastKernels[slot]; - if (qualityTier == 1u) return s_TexMedKernels[slot]; - return s_TexKernels[slot]; + if (s_CopyShader != null) + return true; + + if (s_CopyShaderLoadAttempted) + return false; + + s_CopyShaderLoadAttempted = true; + s_CopyShader = Resources.Load(k_CopyShaderResourcePath); + if (s_CopyShader == null) + { + Debug.LogError($"Failed to load ComputeShader from Resources/{k_CopyShaderResourcePath}.compute."); + return false; + } + + s_CopyKernel = s_CopyShader.FindKernel("CopyTexToBuf"); + return true; } - private static int GetBufKernel(int slot, uint qualityTier) + private static bool TryGetEncoderShader(int slot, bool isHdr, uint qualityTier, out ComputeShader shader, out int kernel) { - if (qualityTier == 0u) return s_BufFastKernels[slot]; - if (qualityTier == 1u) return s_BufMedKernels[slot]; - return s_BufKernels[slot]; + shader = null; + kernel = -1; + if (slot < 0 || slot >= 6) + return false; + + ComputeShader[] shaders = isHdr ? s_HdrShaders : s_LdrShaders; + bool[] attempted = isHdr ? s_HdrLoadAttempted : s_LdrLoadAttempted; + int[,] kernels = isHdr ? s_HdrKernels : s_LdrKernels; + + if (shaders[slot] == null) + { + if (attempted[slot]) + return false; + + attempted[slot] = true; + string resourcePath = isHdr ? s_HdrShaderNames[slot] : s_LdrShaderNames[slot]; + ComputeShader loaded = Resources.Load(resourcePath); + if (loaded == null) + { + Debug.LogError($"Failed to load ComputeShader from Resources/{resourcePath}.compute."); + return false; + } + + shaders[slot] = loaded; + kernels[slot, 0] = loaded.FindKernel("EncodeFast"); + kernels[slot, 1] = loaded.FindKernel("EncodeMed"); + kernels[slot, 2] = loaded.FindKernel("EncodeFull"); + } + + shader = shaders[slot]; + int tierIdx = qualityTier == 0u ? 0 : (qualityTier == 1u ? 1 : 2); + kernel = kernels[slot, tierIdx]; + return true; } private static uint ResolveShaderMode(GraphicsFormat sourceFormat, TextureType textureType, out bool isSRGB, out bool isHdr) @@ -329,87 +395,6 @@ private static uint ResolveShaderMode(GraphicsFormat sourceFormat, TextureType t return hasAlpha ? k_ModeLdrRgba : k_ModeLdrRgb; } - private static bool EnsureShaderLoaded() - { - if (s_Shader != null) - return true; - - if (s_ShaderLoadAttempted) - return false; - - s_ShaderLoadAttempted = true; - s_Shader = Resources.Load(k_ShaderResourcePath); - if (s_Shader == null) - { - Debug.LogError($"Failed to load ComputeShader from Resources/{k_ShaderResourcePath}.compute."); - return false; - } - - s_KwHdrVariant = new LocalKeyword(s_Shader, "ASTC_HDR_VARIANT"); - - s_TexKernels = new int[6] - { - s_Shader.FindKernel("EncodeTex4x4"), - s_Shader.FindKernel("EncodeTex5x5"), - s_Shader.FindKernel("EncodeTex6x6"), - s_Shader.FindKernel("EncodeTex8x8"), - s_Shader.FindKernel("EncodeTex10x10"), - s_Shader.FindKernel("EncodeTex12x12"), - }; - - s_BufKernels = new int[6] - { - s_Shader.FindKernel("EncodeBuf4x4"), - s_Shader.FindKernel("EncodeBuf5x5"), - s_Shader.FindKernel("EncodeBuf6x6"), - s_Shader.FindKernel("EncodeBuf8x8"), - s_Shader.FindKernel("EncodeBuf10x10"), - s_Shader.FindKernel("EncodeBuf12x12"), - }; - - s_TexMedKernels = new int[6] - { - s_Shader.FindKernel("EncodeTex4x4Med"), - s_Shader.FindKernel("EncodeTex5x5Med"), - s_Shader.FindKernel("EncodeTex6x6Med"), - s_Shader.FindKernel("EncodeTex8x8Med"), - s_Shader.FindKernel("EncodeTex10x10Med"), - s_Shader.FindKernel("EncodeTex12x12Med"), - }; - - s_BufMedKernels = new int[6] - { - s_Shader.FindKernel("EncodeBuf4x4Med"), - s_Shader.FindKernel("EncodeBuf5x5Med"), - s_Shader.FindKernel("EncodeBuf6x6Med"), - s_Shader.FindKernel("EncodeBuf8x8Med"), - s_Shader.FindKernel("EncodeBuf10x10Med"), - s_Shader.FindKernel("EncodeBuf12x12Med"), - }; - - s_TexFastKernels = new int[6] - { - s_Shader.FindKernel("EncodeTex4x4Fast"), - s_Shader.FindKernel("EncodeTex5x5Fast"), - s_Shader.FindKernel("EncodeTex6x6Fast"), - s_Shader.FindKernel("EncodeTex8x8Fast"), - s_Shader.FindKernel("EncodeTex10x10Fast"), - s_Shader.FindKernel("EncodeTex12x12Fast"), - }; - - s_BufFastKernels = new int[6] - { - s_Shader.FindKernel("EncodeBuf4x4Fast"), - s_Shader.FindKernel("EncodeBuf5x5Fast"), - s_Shader.FindKernel("EncodeBuf6x6Fast"), - s_Shader.FindKernel("EncodeBuf8x8Fast"), - s_Shader.FindKernel("EncodeBuf10x10Fast"), - s_Shader.FindKernel("EncodeBuf12x12Fast"), - }; - - return true; - } - private static RenderTextureDescriptor CreateBlockRTDescriptor(int blockW, int blockH) { return new RenderTextureDescriptor(blockW, blockH, GraphicsFormat.R32G32B32A32_UInt, 0) @@ -441,23 +426,27 @@ internal static void Preload(BlockSize blockSize) for (int variant = 0; variant < 2; variant++) { - uint mode = variant == 0 ? k_ModeHdrRgb : k_ModeLdrRgb; + bool isHdr = variant == 0; + uint mode = isHdr ? k_ModeHdrRgb : k_ModeLdrRgb; for (uint tier = 0u; tier <= 2u; tier++) { - SetCommonUniforms(s_Shader, 4, 4, 0, 1, 1, 0, mode, false, false, tier); + if (!TryGetEncoderShader(slot, isHdr, tier, out ComputeShader shader, out int kernel)) + continue; - int texKernel = GetTexKernel(slot, tier); - s_Shader.SetTexture(texKernel, s_PropSourceTex, dummySrc); - s_Shader.SetTexture(texKernel, s_PropDestTex, rt); - s_Shader.Dispatch(texKernel, 1, 1, 1); - - int bufKernel = GetBufKernel(slot, tier); - s_Shader.SetTexture(bufKernel, s_PropSourceTex, dummySrc); - s_Shader.SetBuffer(bufKernel, s_PropDestBuffer, buf); - s_Shader.Dispatch(bufKernel, 1, 1, 1); + SetCommonUniforms(shader, 4, 4, 0, 1, 1, mode, false, false, tier); + shader.SetTexture(kernel, s_PropSourceTex, dummySrc); + shader.SetTexture(kernel, s_PropDestTex, rt); + shader.Dispatch(kernel, 1, 1, 1); } } + s_CopyShader.SetTexture(s_CopyKernel, s_PropSrcBlockTex, rt); + s_CopyShader.SetBuffer(s_CopyKernel, s_PropDestBuffer, buf); + s_CopyShader.SetInt(s_PropBlockCountX, 1); + s_CopyShader.SetInt(s_PropBlockCountY, 1); + s_CopyShader.SetInt(s_PropBufferBlockOffset, 0); + s_CopyShader.Dispatch(s_CopyKernel, 1, 1, 1); + if (EnsureMipShaderLoaded()) { RenderTexture mipRt = RenderTexture.GetTemporary(CreateMipRTDescriptor(2, 2, useFloat16: false)); @@ -677,15 +666,12 @@ internal static void ReleaseCache() } s_BufferPool.Clear(); - s_Shader = null; - s_ShaderLoadAttempted = false; - s_KwHdrVariant = default; - s_TexKernels = null; - s_BufKernels = null; - s_TexMedKernels = null; - s_BufMedKernels = null; - s_TexFastKernels = null; - s_BufFastKernels = null; + Array.Clear(s_LdrShaders, 0, s_LdrShaders.Length); + Array.Clear(s_LdrLoadAttempted, 0, s_LdrLoadAttempted.Length); + Array.Clear(s_HdrShaders, 0, s_HdrShaders.Length); + Array.Clear(s_HdrLoadAttempted, 0, s_HdrLoadAttempted.Length); + s_CopyShader = null; + s_CopyShaderLoadAttempted = false; s_MipShader = null; s_MipShaderLoadAttempted = false; } @@ -693,17 +679,14 @@ internal static void ReleaseCache() private static void SetCommonUniforms( ComputeShader shader, int srcWidth, int srcHeight, int mipLevel, - int blockCountX, int blockCountY, int bufferBlockOffset, + int blockCountX, int blockCountY, uint mode, bool isSRGB, bool useAlphaWeight, uint qualityTier) { - bool isHdrMode = mode == k_ModeHdrRgb || mode == k_ModeHdrRgba; - shader.SetKeyword(s_KwHdrVariant, isHdrMode); shader.SetInt(s_PropSrcWidth, srcWidth); shader.SetInt(s_PropSrcHeight, srcHeight); shader.SetInt(s_PropMipLevel, mipLevel); shader.SetInt(s_PropBlockCountX, blockCountX); shader.SetInt(s_PropBlockCountY, blockCountY); - shader.SetInt(s_PropBufferBlockOffset, bufferBlockOffset); shader.SetInt(s_PropMode, (int)mode); shader.SetInt(s_PropIsSRGB, isSRGB ? 1 : 0); shader.SetInt(s_PropUseAlphaWeight, useAlphaWeight ? 1 : 0); @@ -714,17 +697,14 @@ private static void SetCommonUniforms( CommandBuffer cmd, ComputeShader shader, int srcWidth, int srcHeight, int mipLevel, - int blockCountX, int blockCountY, int bufferBlockOffset, + int blockCountX, int blockCountY, uint mode, bool isSRGB, bool useAlphaWeight, uint qualityTier) { - bool isHdrMode = mode == k_ModeHdrRgb || mode == k_ModeHdrRgba; - cmd.SetKeyword(shader, s_KwHdrVariant, isHdrMode); cmd.SetComputeIntParam(shader, s_PropSrcWidth, srcWidth); cmd.SetComputeIntParam(shader, s_PropSrcHeight, srcHeight); cmd.SetComputeIntParam(shader, s_PropMipLevel, mipLevel); cmd.SetComputeIntParam(shader, s_PropBlockCountX, blockCountX); cmd.SetComputeIntParam(shader, s_PropBlockCountY, blockCountY); - cmd.SetComputeIntParam(shader, s_PropBufferBlockOffset, bufferBlockOffset); cmd.SetComputeIntParam(shader, s_PropMode, (int)mode); cmd.SetComputeIntParam(shader, s_PropIsSRGB, isSRGB ? 1 : 0); cmd.SetComputeIntParam(shader, s_PropUseAlphaWeight, useAlphaWeight ? 1 : 0); @@ -771,7 +751,8 @@ private static ComputeBuffer DispatchAllMipsToBuffer( uint mode = ResolveShaderMode(source.graphicsFormat, textureType, out bool isSRGB, out bool isHdr); uint qualityTier = MapQualityTier(quality); - int kernel = GetBufKernel(slot, qualityTier); + if (!TryGetEncoderShader(slot, isHdr, qualityTier, out ComputeShader shader, out int kernel)) + return null; bool useFloat16Mip = (isHdr || isSRGB) && textureType != TextureType.Normal; int mipKernel = textureType == TextureType.Normal ? s_MipKernelNormal : s_MipKernelColor; @@ -779,6 +760,7 @@ private static ComputeBuffer DispatchAllMipsToBuffer( ComputeBuffer buffer = AcquireBlockBuffer(totalBlocks); RenderTexture prevMipRT = null; RenderTexture currMipRT = null; + RenderTexture blockRT = null; try { int blockOffset = 0; @@ -828,13 +810,26 @@ private static ComputeBuffer DispatchAllMipsToBuffer( encodeIsSRGB = isSRGB && textureType != TextureType.Normal; } - s_Shader.SetTexture(kernel, s_PropSourceTex, encodeSrc); - s_Shader.SetBuffer(kernel, s_PropDestBuffer, buffer); - SetCommonUniforms(s_Shader, mipW, mipH, encodeSrcMip, blockCountX, blockCountY, blockOffset, mode, encodeIsSRGB, useAlphaWeight, qualityTier); + blockRT = RenderTexture.GetTemporary(CreateBlockRTDescriptor(blockCountX, blockCountY)); + blockRT.Create(); + + shader.SetTexture(kernel, s_PropSourceTex, encodeSrc); + shader.SetTexture(kernel, s_PropDestTex, blockRT); + SetCommonUniforms(shader, mipW, mipH, encodeSrcMip, blockCountX, blockCountY, mode, encodeIsSRGB, useAlphaWeight, qualityTier); int groupsX = (blockCountX + 7) >> 3; int groupsY = GetDispatchGroupsY(blockCountY, blockSize); - s_Shader.Dispatch(kernel, groupsX, groupsY, 1); + shader.Dispatch(kernel, groupsX, groupsY, 1); + + s_CopyShader.SetTexture(s_CopyKernel, s_PropSrcBlockTex, blockRT); + s_CopyShader.SetBuffer(s_CopyKernel, s_PropDestBuffer, buffer); + s_CopyShader.SetInt(s_PropBlockCountX, blockCountX); + s_CopyShader.SetInt(s_PropBlockCountY, blockCountY); + s_CopyShader.SetInt(s_PropBufferBlockOffset, blockOffset); + s_CopyShader.Dispatch(s_CopyKernel, groupsX, (blockCountY + 7) >> 3, 1); + + RenderTexture.ReleaseTemporary(blockRT); + blockRT = null; blockOffset += blockCountX * blockCountY; } @@ -849,6 +844,8 @@ private static ComputeBuffer DispatchAllMipsToBuffer( } finally { + if (blockRT != null) + RenderTexture.ReleaseTemporary(blockRT); if (currMipRT != null) RenderTexture.ReleaseTemporary(currMipRT); if (prevMipRT != null) @@ -1142,9 +1139,10 @@ internal static void Encode( if (destY < 0 || (destY % (int)by) != 0 || destY + blockCountY * (int)by > alignedDstH) throw new ArgumentOutOfRangeException(nameof(destY), destY, $"destY must be a non-negative multiple of {by} and fit within destination mip height {dstMipH}."); - uint mode = ResolveShaderMode(source.graphicsFormat, textureType, out bool isSRGB, out _); + uint mode = ResolveShaderMode(source.graphicsFormat, textureType, out bool isSRGB, out bool isHdr); uint qualityTier = MapQualityTier(quality); - int kernel = GetTexKernel(slot, qualityTier); + if (!TryGetEncoderShader(slot, isHdr, qualityTier, out ComputeShader shader, out int kernel)) + throw new InvalidOperationException($"Failed to load GPU ASTC encoder shader for block size {blockSize} (HDR={isHdr})."); // Temp RT lifetime is owned by the CommandBuffer, so it stays valid however late the buffer executes. var tempRT = new RenderTargetIdentifier(s_PropTempBlocksRT); @@ -1163,20 +1161,20 @@ internal static void Encode( var tempSliceRT = new RenderTargetIdentifier(s_PropTempSliceRT); cmd.GetTemporaryRT(s_PropTempSliceRT, sliceDesc, FilterMode.Point); cmd.CopyTexture(source, sourceSlice, sourceMip, 0, 0, mipW, mipH, tempSliceRT, 0, 0, 0, 0); - cmd.SetComputeTextureParam(s_Shader, kernel, s_PropSourceTex, tempSliceRT); - SetCommonUniforms(cmd, s_Shader, mipW, mipH, 0, blockCountX, blockCountY, 0, mode, isSRGB, useAlphaWeight, qualityTier); + cmd.SetComputeTextureParam(shader, kernel, s_PropSourceTex, tempSliceRT); + SetCommonUniforms(cmd, shader, mipW, mipH, 0, blockCountX, blockCountY, mode, isSRGB, useAlphaWeight, qualityTier); } else { - cmd.SetComputeTextureParam(s_Shader, kernel, s_PropSourceTex, source); - SetCommonUniforms(cmd, s_Shader, mipW, mipH, sourceMip, blockCountX, blockCountY, 0, mode, isSRGB, useAlphaWeight, qualityTier); + cmd.SetComputeTextureParam(shader, kernel, s_PropSourceTex, source); + SetCommonUniforms(cmd, shader, mipW, mipH, sourceMip, blockCountX, blockCountY, mode, isSRGB, useAlphaWeight, qualityTier); } - cmd.SetComputeTextureParam(s_Shader, kernel, s_PropDestTex, tempRT); + cmd.SetComputeTextureParam(shader, kernel, s_PropDestTex, tempRT); int groupsX = (blockCountX + 7) >> 3; int groupsY = GetDispatchGroupsY(blockCountY, blockSize); - cmd.DispatchCompute(s_Shader, kernel, groupsX, groupsY, 1); + cmd.DispatchCompute(shader, kernel, groupsX, groupsY, 1); cmd.CopyTexture(tempRT, 0, 0, 0, 0, blockCountX, blockCountY, destination, destSlice, destMip, destX, destY); cmd.ReleaseTemporaryRT(s_PropTempBlocksRT); @@ -1327,7 +1325,8 @@ internal static void RecordDirectCompressToTexture( uint mode = ResolveShaderMode(source.graphicsFormat, textureType, out bool isSRGB, out bool isHdr); uint qualityTier = MapQualityTier(quality); - int kernel = GetTexKernel(slot, qualityTier); + if (!TryGetEncoderShader(slot, isHdr, qualityTier, out ComputeShader shader, out int kernel)) + throw new InvalidOperationException($"Failed to load GPU ASTC encoder shader for block size {blockSize} (HDR={isHdr})."); bool useFloat16Mip = (isHdr || isSRGB) && textureType != TextureType.Normal; int mipKernel = textureType == TextureType.Normal ? s_MipKernelNormal : s_MipKernelColor; @@ -1373,10 +1372,10 @@ internal static void RecordDirectCompressToTexture( bool encodeIsSRGB = isSRGB && textureType != TextureType.Normal; cmd.GetTemporaryRT(s_PropTempBlocksRT, CreateBlockRTDescriptor(blockCountX, blockCountY), FilterMode.Point); - cmd.SetComputeTextureParam(s_Shader, kernel, s_PropSourceTex, currMipRT); - cmd.SetComputeTextureParam(s_Shader, kernel, s_PropDestTex, tempBlocksRT); - SetCommonUniforms(cmd, s_Shader, mipW, mipH, 0, blockCountX, blockCountY, 0, mode, encodeIsSRGB, useAlphaWeight, qualityTier); - cmd.DispatchCompute(s_Shader, kernel, (blockCountX + 7) >> 3, GetDispatchGroupsY(blockCountY, blockSize), 1); + cmd.SetComputeTextureParam(shader, kernel, s_PropSourceTex, currMipRT); + cmd.SetComputeTextureParam(shader, kernel, s_PropDestTex, tempBlocksRT); + SetCommonUniforms(cmd, shader, mipW, mipH, 0, blockCountX, blockCountY, mode, encodeIsSRGB, useAlphaWeight, qualityTier); + cmd.DispatchCompute(shader, kernel, (blockCountX + 7) >> 3, GetDispatchGroupsY(blockCountY, blockSize), 1); cmd.CopyTexture(tempBlocksRT, 0, 0, 0, 0, blockCountX, blockCountY, dst, 0, mip, 0, 0); cmd.ReleaseTemporaryRT(s_PropTempBlocksRT); @@ -1607,9 +1606,10 @@ internal static bool CompressInPlace( && Encoder.GetBlockSize(blockSize, out uint bx, out uint by, out _)) { int slot = BlockSizeToSlot(blockSize); - uint mode = ResolveShaderMode(inputTexture.graphicsFormat, textureType, out bool isSRGB, out _); + uint mode = ResolveShaderMode(inputTexture.graphicsFormat, textureType, out bool isSRGB, out bool isHdr); uint qualityTier = MapQualityTier(quality); - int kernel = GetTexKernel(slot, qualityTier); + if (!TryGetEncoderShader(slot, isHdr, qualityTier, out ComputeShader shader, out int kernel)) + return false; var mipRTs = new RenderTexture[mipCount]; try @@ -1625,10 +1625,10 @@ internal static bool CompressInPlace( rt.Create(); mipRTs[mip] = rt; - s_Shader.SetTexture(kernel, s_PropSourceTex, inputTexture); - s_Shader.SetTexture(kernel, s_PropDestTex, rt); - SetCommonUniforms(s_Shader, mipW, mipH, mip, blockW, blockH, 0, mode, isSRGB, useAlphaWeight, qualityTier); - s_Shader.Dispatch(kernel, (blockW + 7) >> 3, GetDispatchGroupsY(blockH, blockSize), 1); + shader.SetTexture(kernel, s_PropSourceTex, inputTexture); + shader.SetTexture(kernel, s_PropDestTex, rt); + SetCommonUniforms(shader, mipW, mipH, mip, blockW, blockH, mode, isSRGB, useAlphaWeight, qualityTier); + shader.Dispatch(kernel, (blockW + 7) >> 3, GetDispatchGroupsY(blockH, blockSize), 1); } if (!inputTexture.Reinitialize(width, height, astcFormat, mipCount > 1)) diff --git a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuEntry.hlsl b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuEntry.hlsl new file mode 100644 index 0000000..a1e407c --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuEntry.hlsl @@ -0,0 +1,37 @@ +// Shared compute shader entry points included by per-block AstcGpuEncoder_*.compute files. +// Each .compute file defines BLOCK_X, BLOCK_Y, ASTC_THREADS_Y (8 or 4), and optionally ASTC_HDR_VARIANT. +#ifndef ASTC_GPU_ENTRY_HLSL +#define ASTC_GPU_ENTRY_HLSL + +Texture2D _AstcSourceTex; +RWTexture2D _AstcDestTex; + +uint _AstcSrcWidth; +uint _AstcSrcHeight; +uint _AstcMipLevel; +uint _AstcBlockCountX; +uint _AstcBlockCountY; +uint _AstcMode; +uint _AstcIsSRGB; +uint _AstcUseAlphaWeight; +uint _AstcQuality; + +#include "AstcGpuCore.hlsl" + +void AstcDispatchToTexture(uint3 id) +{ + if (id.x >= _AstcBlockCountX || id.y >= _AstcBlockCountY) + return; + _AstcDestTex[id.xy] = AstcEncodeBlock(id.x, id.y); +} + +[numthreads(8, ASTC_THREADS_Y, 1)] +void EncodeFull(uint3 id : SV_DispatchThreadID) { AstcDispatchToTexture(id); } + +[numthreads(8, ASTC_THREADS_Y, 1)] +void EncodeMed(uint3 id : SV_DispatchThreadID) { AstcDispatchToTexture(id); } + +[numthreads(8, ASTC_THREADS_Y, 1)] +void EncodeFast(uint3 id : SV_DispatchThreadID) { AstcDispatchToTexture(id); } + +#endif // ASTC_GPU_ENTRY_HLSL diff --git a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuEntry.hlsl.meta b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuEntry.hlsl.meta new file mode 100644 index 0000000..50ef0f2 --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuEntry.hlsl.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: c2f3fa8acabd06a2a524153db14d4a0f +ShaderIncludeImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuBufferCopy.compute b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuBufferCopy.compute new file mode 100644 index 0000000..1683bb5 --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuBufferCopy.compute @@ -0,0 +1,18 @@ +#pragma kernel CopyTexToBuf + +Texture2D _AstcSrcBlockTex; +RWStructuredBuffer _AstcDestBuffer; + +uint _AstcBlockCountX; +uint _AstcBlockCountY; +uint _AstcBufferBlockOffset; + +[numthreads(8, 8, 1)] +void CopyTexToBuf(uint3 id : SV_DispatchThreadID) +{ + if (id.x >= _AstcBlockCountX || id.y >= _AstcBlockCountY) + return; + + uint outIdx = _AstcBufferBlockOffset + id.y * _AstcBlockCountX + id.x; + _AstcDestBuffer[outIdx] = _AstcSrcBlockTex.Load(int3(id.xy, 0)); +} diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder.compute.meta b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuBufferCopy.compute.meta similarity index 79% rename from Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder.compute.meta rename to Assets/ASTC Encoder/Runtime/Resources/AstcGpuBufferCopy.compute.meta index 3484763..0392e71 100644 --- a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder.compute.meta +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuBufferCopy.compute.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 5b4a4d38cbb10f5b64370264638c4454 +guid: ce16aef6b8be625305859287e3e5ec04 ComputeShaderImporter: externalObjects: {} preprocessorOverride: 0 diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder.compute b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder.compute deleted file mode 100644 index c82cbc0..0000000 --- a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder.compute +++ /dev/null @@ -1,138 +0,0 @@ -#pragma editor_sync_compilation -#pragma multi_compile_local _ ASTC_HDR_VARIANT - -#pragma kernel EncodeTex4x4 ASTC_BLOCK_4X4 -#pragma kernel EncodeTex5x5 ASTC_BLOCK_5X5 -#pragma kernel EncodeTex6x6 ASTC_BLOCK_6X6 -#pragma kernel EncodeTex8x8 ASTC_BLOCK_8X8 -#pragma kernel EncodeTex10x10 ASTC_BLOCK_10X10 -#pragma kernel EncodeTex12x12 ASTC_BLOCK_12X12 - -#pragma kernel EncodeBuf4x4 ASTC_BLOCK_4X4 -#pragma kernel EncodeBuf5x5 ASTC_BLOCK_5X5 -#pragma kernel EncodeBuf6x6 ASTC_BLOCK_6X6 -#pragma kernel EncodeBuf8x8 ASTC_BLOCK_8X8 -#pragma kernel EncodeBuf10x10 ASTC_BLOCK_10X10 -#pragma kernel EncodeBuf12x12 ASTC_BLOCK_12X12 - -#pragma kernel EncodeTex4x4Med ASTC_BLOCK_4X4 ASTC_NO_2P -#pragma kernel EncodeTex5x5Med ASTC_BLOCK_5X5 ASTC_NO_2P -#pragma kernel EncodeTex6x6Med ASTC_BLOCK_6X6 ASTC_NO_2P -#pragma kernel EncodeTex8x8Med ASTC_BLOCK_8X8 ASTC_NO_2P -#pragma kernel EncodeTex10x10Med ASTC_BLOCK_10X10 ASTC_NO_2P -#pragma kernel EncodeTex12x12Med ASTC_BLOCK_12X12 ASTC_NO_2P - -#pragma kernel EncodeBuf4x4Med ASTC_BLOCK_4X4 ASTC_NO_2P -#pragma kernel EncodeBuf5x5Med ASTC_BLOCK_5X5 ASTC_NO_2P -#pragma kernel EncodeBuf6x6Med ASTC_BLOCK_6X6 ASTC_NO_2P -#pragma kernel EncodeBuf8x8Med ASTC_BLOCK_8X8 ASTC_NO_2P -#pragma kernel EncodeBuf10x10Med ASTC_BLOCK_10X10 ASTC_NO_2P -#pragma kernel EncodeBuf12x12Med ASTC_BLOCK_12X12 ASTC_NO_2P - -#pragma kernel EncodeTex4x4Fast ASTC_BLOCK_4X4 ASTC_FAST_ONLY -#pragma kernel EncodeTex5x5Fast ASTC_BLOCK_5X5 ASTC_FAST_ONLY -#pragma kernel EncodeTex6x6Fast ASTC_BLOCK_6X6 ASTC_FAST_ONLY -#pragma kernel EncodeTex8x8Fast ASTC_BLOCK_8X8 ASTC_FAST_ONLY -#pragma kernel EncodeTex10x10Fast ASTC_BLOCK_10X10 ASTC_FAST_ONLY -#pragma kernel EncodeTex12x12Fast ASTC_BLOCK_12X12 ASTC_FAST_ONLY - -#pragma kernel EncodeBuf4x4Fast ASTC_BLOCK_4X4 ASTC_FAST_ONLY -#pragma kernel EncodeBuf5x5Fast ASTC_BLOCK_5X5 ASTC_FAST_ONLY -#pragma kernel EncodeBuf6x6Fast ASTC_BLOCK_6X6 ASTC_FAST_ONLY -#pragma kernel EncodeBuf8x8Fast ASTC_BLOCK_8X8 ASTC_FAST_ONLY -#pragma kernel EncodeBuf10x10Fast ASTC_BLOCK_10X10 ASTC_FAST_ONLY -#pragma kernel EncodeBuf12x12Fast ASTC_BLOCK_12X12 ASTC_FAST_ONLY - -#if defined(ASTC_BLOCK_5X5) - #define BLOCK_X 5 - #define BLOCK_Y 5 -#elif defined(ASTC_BLOCK_6X6) - #define BLOCK_X 6 - #define BLOCK_Y 6 -#elif defined(ASTC_BLOCK_8X8) - #define BLOCK_X 8 - #define BLOCK_Y 8 -#elif defined(ASTC_BLOCK_10X10) - #define BLOCK_X 10 - #define BLOCK_Y 10 -#elif defined(ASTC_BLOCK_12X12) - #define BLOCK_X 12 - #define BLOCK_Y 12 -#else - #define BLOCK_X 4 - #define BLOCK_Y 4 -#endif - -Texture2D _AstcSourceTex; -RWTexture2D _AstcDestTex; -RWStructuredBuffer _AstcDestBuffer; - -uint _AstcSrcWidth; -uint _AstcSrcHeight; -uint _AstcMipLevel; -uint _AstcBlockCountX; -uint _AstcBlockCountY; -uint _AstcBufferBlockOffset; -uint _AstcMode; -uint _AstcIsSRGB; -uint _AstcUseAlphaWeight; -uint _AstcQuality; - -#include "../Gpu/Shaders/AstcGpuCore.hlsl" - -void DispatchToTexture(uint3 id) -{ - if (id.x >= _AstcBlockCountX || id.y >= _AstcBlockCountY) - return; - _AstcDestTex[id.xy] = AstcEncodeBlock(id.x, id.y); -} - -void DispatchToBuffer(uint3 id) -{ - if (id.x >= _AstcBlockCountX || id.y >= _AstcBlockCountY) - return; - uint outIdx = _AstcBufferBlockOffset + id.y * _AstcBlockCountX + id.x; - _AstcDestBuffer[outIdx] = AstcEncodeBlock(id.x, id.y); -} - -[numthreads(8, 8, 1)] void EncodeTex4x4(uint3 id : SV_DispatchThreadID) { DispatchToTexture(id); } -[numthreads(8, 8, 1)] void EncodeTex5x5(uint3 id : SV_DispatchThreadID) { DispatchToTexture(id); } -[numthreads(8, 8, 1)] void EncodeTex6x6(uint3 id : SV_DispatchThreadID) { DispatchToTexture(id); } -[numthreads(8, 4, 1)] void EncodeTex8x8(uint3 id : SV_DispatchThreadID) { DispatchToTexture(id); } -[numthreads(8, 4, 1)] void EncodeTex10x10(uint3 id : SV_DispatchThreadID) { DispatchToTexture(id); } -[numthreads(8, 4, 1)] void EncodeTex12x12(uint3 id : SV_DispatchThreadID) { DispatchToTexture(id); } - -[numthreads(8, 8, 1)] void EncodeBuf4x4(uint3 id : SV_DispatchThreadID) { DispatchToBuffer(id); } -[numthreads(8, 8, 1)] void EncodeBuf5x5(uint3 id : SV_DispatchThreadID) { DispatchToBuffer(id); } -[numthreads(8, 8, 1)] void EncodeBuf6x6(uint3 id : SV_DispatchThreadID) { DispatchToBuffer(id); } -[numthreads(8, 4, 1)] void EncodeBuf8x8(uint3 id : SV_DispatchThreadID) { DispatchToBuffer(id); } -[numthreads(8, 4, 1)] void EncodeBuf10x10(uint3 id : SV_DispatchThreadID) { DispatchToBuffer(id); } -[numthreads(8, 4, 1)] void EncodeBuf12x12(uint3 id : SV_DispatchThreadID) { DispatchToBuffer(id); } - -[numthreads(8, 8, 1)] void EncodeTex4x4Med(uint3 id : SV_DispatchThreadID) { DispatchToTexture(id); } -[numthreads(8, 8, 1)] void EncodeTex5x5Med(uint3 id : SV_DispatchThreadID) { DispatchToTexture(id); } -[numthreads(8, 8, 1)] void EncodeTex6x6Med(uint3 id : SV_DispatchThreadID) { DispatchToTexture(id); } -[numthreads(8, 4, 1)] void EncodeTex8x8Med(uint3 id : SV_DispatchThreadID) { DispatchToTexture(id); } -[numthreads(8, 4, 1)] void EncodeTex10x10Med(uint3 id : SV_DispatchThreadID) { DispatchToTexture(id); } -[numthreads(8, 4, 1)] void EncodeTex12x12Med(uint3 id : SV_DispatchThreadID) { DispatchToTexture(id); } - -[numthreads(8, 8, 1)] void EncodeBuf4x4Med(uint3 id : SV_DispatchThreadID) { DispatchToBuffer(id); } -[numthreads(8, 8, 1)] void EncodeBuf5x5Med(uint3 id : SV_DispatchThreadID) { DispatchToBuffer(id); } -[numthreads(8, 8, 1)] void EncodeBuf6x6Med(uint3 id : SV_DispatchThreadID) { DispatchToBuffer(id); } -[numthreads(8, 4, 1)] void EncodeBuf8x8Med(uint3 id : SV_DispatchThreadID) { DispatchToBuffer(id); } -[numthreads(8, 4, 1)] void EncodeBuf10x10Med(uint3 id : SV_DispatchThreadID) { DispatchToBuffer(id); } -[numthreads(8, 4, 1)] void EncodeBuf12x12Med(uint3 id : SV_DispatchThreadID) { DispatchToBuffer(id); } - -[numthreads(8, 8, 1)] void EncodeTex4x4Fast(uint3 id : SV_DispatchThreadID) { DispatchToTexture(id); } -[numthreads(8, 8, 1)] void EncodeTex5x5Fast(uint3 id : SV_DispatchThreadID) { DispatchToTexture(id); } -[numthreads(8, 8, 1)] void EncodeTex6x6Fast(uint3 id : SV_DispatchThreadID) { DispatchToTexture(id); } -[numthreads(8, 4, 1)] void EncodeTex8x8Fast(uint3 id : SV_DispatchThreadID) { DispatchToTexture(id); } -[numthreads(8, 4, 1)] void EncodeTex10x10Fast(uint3 id : SV_DispatchThreadID) { DispatchToTexture(id); } -[numthreads(8, 4, 1)] void EncodeTex12x12Fast(uint3 id : SV_DispatchThreadID) { DispatchToTexture(id); } - -[numthreads(8, 8, 1)] void EncodeBuf4x4Fast(uint3 id : SV_DispatchThreadID) { DispatchToBuffer(id); } -[numthreads(8, 8, 1)] void EncodeBuf5x5Fast(uint3 id : SV_DispatchThreadID) { DispatchToBuffer(id); } -[numthreads(8, 8, 1)] void EncodeBuf6x6Fast(uint3 id : SV_DispatchThreadID) { DispatchToBuffer(id); } -[numthreads(8, 4, 1)] void EncodeBuf8x8Fast(uint3 id : SV_DispatchThreadID) { DispatchToBuffer(id); } -[numthreads(8, 4, 1)] void EncodeBuf10x10Fast(uint3 id : SV_DispatchThreadID) { DispatchToBuffer(id); } -[numthreads(8, 4, 1)] void EncodeBuf12x12Fast(uint3 id : SV_DispatchThreadID) { DispatchToBuffer(id); } diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_10x10.compute b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_10x10.compute new file mode 100644 index 0000000..941692b --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_10x10.compute @@ -0,0 +1,9 @@ +#pragma kernel EncodeFull +#pragma kernel EncodeMed ASTC_NO_2P +#pragma kernel EncodeFast ASTC_FAST_ONLY + +#define BLOCK_X 10 +#define BLOCK_Y 10 +#define ASTC_THREADS_Y 4 + +#include "../Gpu/Shaders/AstcGpuEntry.hlsl" diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_10x10.compute.meta b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_10x10.compute.meta new file mode 100644 index 0000000..7c185f5 --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_10x10.compute.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 7cb929b37b6a594466589aadeb1c280d +ComputeShaderImporter: + externalObjects: {} + preprocessorOverride: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_10x10_Hdr.compute b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_10x10_Hdr.compute new file mode 100644 index 0000000..5a89947 --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_10x10_Hdr.compute @@ -0,0 +1,9 @@ +#pragma kernel EncodeFull ASTC_HDR_VARIANT +#pragma kernel EncodeMed ASTC_HDR_VARIANT ASTC_NO_2P +#pragma kernel EncodeFast ASTC_HDR_VARIANT ASTC_FAST_ONLY + +#define BLOCK_X 10 +#define BLOCK_Y 10 +#define ASTC_THREADS_Y 4 + +#include "../Gpu/Shaders/AstcGpuEntry.hlsl" diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_10x10_Hdr.compute.meta b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_10x10_Hdr.compute.meta new file mode 100644 index 0000000..7cd8578 --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_10x10_Hdr.compute.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 1e7617bfb5ab620a57c53b76751b39ec +ComputeShaderImporter: + externalObjects: {} + preprocessorOverride: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_12x12.compute b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_12x12.compute new file mode 100644 index 0000000..60848af --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_12x12.compute @@ -0,0 +1,9 @@ +#pragma kernel EncodeFull +#pragma kernel EncodeMed ASTC_NO_2P +#pragma kernel EncodeFast ASTC_FAST_ONLY + +#define BLOCK_X 12 +#define BLOCK_Y 12 +#define ASTC_THREADS_Y 4 + +#include "../Gpu/Shaders/AstcGpuEntry.hlsl" diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_12x12.compute.meta b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_12x12.compute.meta new file mode 100644 index 0000000..744e6b9 --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_12x12.compute.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e0de69b98b9a0c06c81cc9cf774de655 +ComputeShaderImporter: + externalObjects: {} + preprocessorOverride: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_12x12_Hdr.compute b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_12x12_Hdr.compute new file mode 100644 index 0000000..b7c7924 --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_12x12_Hdr.compute @@ -0,0 +1,9 @@ +#pragma kernel EncodeFull ASTC_HDR_VARIANT +#pragma kernel EncodeMed ASTC_HDR_VARIANT ASTC_NO_2P +#pragma kernel EncodeFast ASTC_HDR_VARIANT ASTC_FAST_ONLY + +#define BLOCK_X 12 +#define BLOCK_Y 12 +#define ASTC_THREADS_Y 4 + +#include "../Gpu/Shaders/AstcGpuEntry.hlsl" diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_12x12_Hdr.compute.meta b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_12x12_Hdr.compute.meta new file mode 100644 index 0000000..f0299b4 --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_12x12_Hdr.compute.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: d327376fddf5fbcc91b8ee5dd1472c61 +ComputeShaderImporter: + externalObjects: {} + preprocessorOverride: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_4x4.compute b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_4x4.compute new file mode 100644 index 0000000..b91cd13 --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_4x4.compute @@ -0,0 +1,9 @@ +#pragma kernel EncodeFull +#pragma kernel EncodeMed ASTC_NO_2P +#pragma kernel EncodeFast ASTC_FAST_ONLY + +#define BLOCK_X 4 +#define BLOCK_Y 4 +#define ASTC_THREADS_Y 8 + +#include "../Gpu/Shaders/AstcGpuEntry.hlsl" diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_4x4.compute.meta b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_4x4.compute.meta new file mode 100644 index 0000000..27e9e96 --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_4x4.compute.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: f83311884bd1f91caf937ec64e88ccc3 +ComputeShaderImporter: + externalObjects: {} + preprocessorOverride: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_4x4_Hdr.compute b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_4x4_Hdr.compute new file mode 100644 index 0000000..5a35e09 --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_4x4_Hdr.compute @@ -0,0 +1,9 @@ +#pragma kernel EncodeFull ASTC_HDR_VARIANT +#pragma kernel EncodeMed ASTC_HDR_VARIANT ASTC_NO_2P +#pragma kernel EncodeFast ASTC_HDR_VARIANT ASTC_FAST_ONLY + +#define BLOCK_X 4 +#define BLOCK_Y 4 +#define ASTC_THREADS_Y 8 + +#include "../Gpu/Shaders/AstcGpuEntry.hlsl" diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_4x4_Hdr.compute.meta b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_4x4_Hdr.compute.meta new file mode 100644 index 0000000..ee45a86 --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_4x4_Hdr.compute.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: d666a15107f4c17b3f2e3e810dc72936 +ComputeShaderImporter: + externalObjects: {} + preprocessorOverride: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_5x5.compute b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_5x5.compute new file mode 100644 index 0000000..09a33df --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_5x5.compute @@ -0,0 +1,9 @@ +#pragma kernel EncodeFull +#pragma kernel EncodeMed ASTC_NO_2P +#pragma kernel EncodeFast ASTC_FAST_ONLY + +#define BLOCK_X 5 +#define BLOCK_Y 5 +#define ASTC_THREADS_Y 8 + +#include "../Gpu/Shaders/AstcGpuEntry.hlsl" diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_5x5.compute.meta b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_5x5.compute.meta new file mode 100644 index 0000000..3f725b9 --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_5x5.compute.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 4ee8b255be5aec3c729b2b185a197f1d +ComputeShaderImporter: + externalObjects: {} + preprocessorOverride: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_5x5_Hdr.compute b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_5x5_Hdr.compute new file mode 100644 index 0000000..6a13c57 --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_5x5_Hdr.compute @@ -0,0 +1,9 @@ +#pragma kernel EncodeFull ASTC_HDR_VARIANT +#pragma kernel EncodeMed ASTC_HDR_VARIANT ASTC_NO_2P +#pragma kernel EncodeFast ASTC_HDR_VARIANT ASTC_FAST_ONLY + +#define BLOCK_X 5 +#define BLOCK_Y 5 +#define ASTC_THREADS_Y 8 + +#include "../Gpu/Shaders/AstcGpuEntry.hlsl" diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_5x5_Hdr.compute.meta b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_5x5_Hdr.compute.meta new file mode 100644 index 0000000..a4b1657 --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_5x5_Hdr.compute.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 8c92cfb5b3480d301de78f7110cf0153 +ComputeShaderImporter: + externalObjects: {} + preprocessorOverride: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_6x6.compute b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_6x6.compute new file mode 100644 index 0000000..68a94e9 --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_6x6.compute @@ -0,0 +1,9 @@ +#pragma kernel EncodeFull +#pragma kernel EncodeMed ASTC_NO_2P +#pragma kernel EncodeFast ASTC_FAST_ONLY + +#define BLOCK_X 6 +#define BLOCK_Y 6 +#define ASTC_THREADS_Y 8 + +#include "../Gpu/Shaders/AstcGpuEntry.hlsl" diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_6x6.compute.meta b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_6x6.compute.meta new file mode 100644 index 0000000..68262de --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_6x6.compute.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: c9849dc767dbe1f80320ac7dae243cb8 +ComputeShaderImporter: + externalObjects: {} + preprocessorOverride: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_6x6_Hdr.compute b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_6x6_Hdr.compute new file mode 100644 index 0000000..c8e4ef4 --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_6x6_Hdr.compute @@ -0,0 +1,9 @@ +#pragma kernel EncodeFull ASTC_HDR_VARIANT +#pragma kernel EncodeMed ASTC_HDR_VARIANT ASTC_NO_2P +#pragma kernel EncodeFast ASTC_HDR_VARIANT ASTC_FAST_ONLY + +#define BLOCK_X 6 +#define BLOCK_Y 6 +#define ASTC_THREADS_Y 8 + +#include "../Gpu/Shaders/AstcGpuEntry.hlsl" diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_6x6_Hdr.compute.meta b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_6x6_Hdr.compute.meta new file mode 100644 index 0000000..848cddb --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_6x6_Hdr.compute.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 3581e368874ed5228719ddde389f9acc +ComputeShaderImporter: + externalObjects: {} + preprocessorOverride: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_8x8.compute b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_8x8.compute new file mode 100644 index 0000000..c2c02ed --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_8x8.compute @@ -0,0 +1,9 @@ +#pragma kernel EncodeFull +#pragma kernel EncodeMed ASTC_NO_2P +#pragma kernel EncodeFast ASTC_FAST_ONLY + +#define BLOCK_X 8 +#define BLOCK_Y 8 +#define ASTC_THREADS_Y 4 + +#include "../Gpu/Shaders/AstcGpuEntry.hlsl" diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_8x8.compute.meta b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_8x8.compute.meta new file mode 100644 index 0000000..717bf2d --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_8x8.compute.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: bad505eb82ea6568b943e0c50a8b3650 +ComputeShaderImporter: + externalObjects: {} + preprocessorOverride: 0 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_8x8_Hdr.compute b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_8x8_Hdr.compute new file mode 100644 index 0000000..98db18c --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_8x8_Hdr.compute @@ -0,0 +1,9 @@ +#pragma kernel EncodeFull ASTC_HDR_VARIANT +#pragma kernel EncodeMed ASTC_HDR_VARIANT ASTC_NO_2P +#pragma kernel EncodeFast ASTC_HDR_VARIANT ASTC_FAST_ONLY + +#define BLOCK_X 8 +#define BLOCK_Y 8 +#define ASTC_THREADS_Y 4 + +#include "../Gpu/Shaders/AstcGpuEntry.hlsl" diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_8x8_Hdr.compute.meta b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_8x8_Hdr.compute.meta new file mode 100644 index 0000000..0dec6e9 --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuEncoder_8x8_Hdr.compute.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: a6fb5e1413fa1dcc573dfb7495453f24 +ComputeShaderImporter: + externalObjects: {} + preprocessorOverride: 0 + userData: + assetBundleName: + assetBundleVariant: From 100214521139d86a2b61d44048d5bca78cfb7092 Mon Sep 17 00:00:00 2001 From: FrozenKresnik <80754402+Overflowu@users.noreply.github.com> Date: Tue, 1 Dec 2026 13:23:52 +0000 Subject: [PATCH 5/9] perf(gpu): eliminate duplicate HLSL inlining and reduce indexable temp arrays --- .../Runtime/Gpu/Shaders/AstcGpuColor.hlsl | 35 +- .../Runtime/Gpu/Shaders/AstcGpuCore.hlsl | 627 +++++++++--------- 2 files changed, 337 insertions(+), 325 deletions(-) diff --git a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuColor.hlsl b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuColor.hlsl index 19f5f44..068b219 100644 --- a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuColor.hlsl +++ b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuColor.hlsl @@ -529,23 +529,26 @@ void AstcComputeInfillTap( } // Evaluates the exact ASTC hardware interpolated weight [0, 64] at texel (x, y) -// from a local unquantized grid array gridUq[36] without passing the array by value. -#define ASTC_EVAL_INFILL(gridUq, tx, ty, bx, by, gw, gh, outWeight64) \ - if ((gw) == (bx) && (gh) == (by)) \ - { \ - (outWeight64) = (gridUq)[(ty) * (gw) + (tx)]; \ - } \ - else \ - { \ - uint _js, _jt; \ - uint4 _tw; \ - AstcComputeInfillTap((tx), (ty), (bx), (by), (gw), (gh), _js, _jt, _tw); \ - uint _idx00 = _jt * (gw) + _js; \ - uint _p00 = (gridUq)[_idx00]; \ - uint _p10 = (_tw.y > 0u) ? (gridUq)[_idx00 + 1u] : 0u; \ - uint _p01 = (_tw.z > 0u) ? (gridUq)[_idx00 + (gw)] : 0u; \ - uint _p11 = (_tw.w > 0u) ? (gridUq)[_idx00 + (gw) + 1u] : 0u; \ +// from a packed unquantized grid array gridUq[36] (plane 0 in bits 0..7, plane 1 in bits 8..15). +#define ASTC_EVAL_INFILL_SHIFT(gridUq, shift, tx, ty, bx, by, gw, gh, outWeight64) \ + if ((gw) == (bx) && (gh) == (by)) \ + { \ + (outWeight64) = ((gridUq)[(ty) * (gw) + (tx)] >> (shift)) & 0xFFu; \ + } \ + else \ + { \ + uint _js, _jt; \ + uint4 _tw; \ + AstcComputeInfillTap((tx), (ty), (bx), (by), (gw), (gh), _js, _jt, _tw); \ + uint _idx00 = _jt * (gw) + _js; \ + uint _p00 = ((gridUq)[_idx00] >> (shift)) & 0xFFu; \ + uint _p10 = (_tw.y > 0u) ? (((gridUq)[_idx00 + 1u] >> (shift)) & 0xFFu) : 0u; \ + uint _p01 = (_tw.z > 0u) ? (((gridUq)[_idx00 + (gw)] >> (shift)) & 0xFFu) : 0u; \ + uint _p11 = (_tw.w > 0u) ? (((gridUq)[_idx00 + (gw) + 1u] >> (shift)) & 0xFFu) : 0u; \ (outWeight64) = (_p00 * _tw.x + _p10 * _tw.y + _p01 * _tw.z + _p11 * _tw.w + 8u) >> 4u; \ } +#define ASTC_EVAL_INFILL(gridUq, tx, ty, bx, by, gw, gh, outWeight64) \ + ASTC_EVAL_INFILL_SHIFT(gridUq, 0u, tx, ty, bx, by, gw, gh, outWeight64) + #endif // ASTC_GPU_COLOR_HLSL diff --git a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl index 3dc00fc..646db76 100644 --- a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl +++ b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl @@ -45,17 +45,14 @@ struct AstcCandidate #endif }; -#if BLOCK_X <= 4 -#define ASTC_MAX_GRID 16 -#define ASTC_MAX_GRID_U 16u -#elif BLOCK_X <= 5 -#define ASTC_MAX_GRID 25 -#define ASTC_MAX_GRID_U 25u +#if (BLOCK_X <= 5) && (BLOCK_Y <= 5) +#define ASTC_MAX_GRID (BLOCK_X * BLOCK_Y) #else -#define ASTC_MAX_GRID 36 -#define ASTC_MAX_GRID_U 36u +#define ASTC_MAX_GRID 36 #endif +#define ASTC_MAX_BLOCK (BLOCK_X * BLOCK_Y) + #if ASTC_ENABLE_2P #define ASTC_MAX_EP_CODES 16 #else @@ -67,21 +64,18 @@ struct AstcCandidate static float g_AstcGrid0[ASTC_MAX_GRID]; static float g_AstcGrid1[ASTC_MAX_GRID]; #if !defined(ASTC_FAST_ONLY) -static uint g_AstcGridUq0[ASTC_MAX_GRID]; -static uint g_AstcGridUq1[ASTC_MAX_GRID]; -static uint g_AstcCandEpCodes[ASTC_MAX_EP_CODES]; +// Plane 0 unquantized weight [0..64] in bits 0..7; Plane 1 in bits 8..15. +static uint g_AstcGridUq[ASTC_MAX_GRID]; +static uint g_AstcBestEpCodes[ASTC_MAX_EP_CODES]; #endif static uint g_AstcEpCodes[ASTC_MAX_EP_CODES]; -static uint g_AstcEpVals[8]; -static uint g_AstcEpCArr[8]; -static uint g_AstcEpUqArr[8]; // On-chip per-thread working-pixel cache: -// - LDR variants pack RGBA8 into a single uint (64..576 bytes per thread, 50% smaller than uint2). -// - HDR variants pack 4x16-bit LNS into uint2 (128..1152 bytes per thread). +// - LDR variants pack RGBA8 into a single uint (64 bytes for 4x4, 576 bytes for 12x12). +// - HDR variants pack 4x16-bit LNS into uint2 (128 bytes for 4x4, 1152 bytes for 12x12). #define ASTC_ENABLE_PIXEL_CACHE 1 #if defined(ASTC_HDR_VARIANT) -static uint2 g_AstcPackedBlock[BLOCK_X * BLOCK_Y]; +static uint2 g_AstcPackedBlock[ASTC_MAX_BLOCK]; uint2 AstcPackRgba16(float4 v) { @@ -89,7 +83,7 @@ uint2 AstcPackRgba16(float4 v) return uint2(u.x | (u.y << 16u), u.z | (u.w << 16u)); } #else -static uint g_AstcPackedBlock[BLOCK_X * BLOCK_Y]; +static uint g_AstcPackedBlock[ASTC_MAX_BLOCK]; uint AstcPackRgba8(uint4 u) { @@ -262,63 +256,46 @@ void AstcQuantizeEndpoints( swapped = false; #if !defined(ASTC_HDR_VARIANT) - if (effMode == ASTC_EFF_LDR_NORMAL) - { - g_AstcEpVals[0] = (uint)clamp(floor(e0.x + 0.5), 0.0, 255.0); - g_AstcEpVals[1] = (uint)clamp(floor(e1.x + 0.5), 0.0, 255.0); - g_AstcEpVals[2] = (uint)clamp(floor(e0.w + 0.5), 0.0, 255.0); - g_AstcEpVals[3] = (uint)clamp(floor(e1.w + 0.5), 0.0, 255.0); - - [loop] for (uint i = 0u; i < 4u; ++i) - { - uint uqTmp; - g_AstcEpCodes[codeOffset + i] = AstcQuantizeColor(g_AstcEpVals[i], epQuant, uqTmp); - g_AstcEpUqArr[i] = uqTmp; - } + uint4 q0 = (uint4)clamp(floor(e0 + 0.5), 0.0, 255.0); + uint4 q1 = (uint4)clamp(floor(e1 + 0.5), 0.0, 255.0); + uint cnt = (effMode == ASTC_EFF_LDR_NORMAL) ? 4u : ((effMode == ASTC_EFF_LDR_RGBA) ? 8u : 6u); + uint4 u0 = uint4(0u, 0u, 0u, 255u); + uint4 u1 = uint4(0u, 0u, 0u, 255u); - uqE0 = float4((float)g_AstcEpUqArr[0], 0.0, 0.0, (float)g_AstcEpUqArr[2]); - uqE1 = float4((float)g_AstcEpUqArr[1], 0.0, 0.0, (float)g_AstcEpUqArr[3]); - return; - } - - g_AstcEpVals[0] = (uint)clamp(floor(e0.r + 0.5), 0.0, 255.0); - g_AstcEpVals[1] = (uint)clamp(floor(e1.r + 0.5), 0.0, 255.0); - g_AstcEpVals[2] = (uint)clamp(floor(e0.g + 0.5), 0.0, 255.0); - g_AstcEpVals[3] = (uint)clamp(floor(e1.g + 0.5), 0.0, 255.0); - g_AstcEpVals[4] = (uint)clamp(floor(e0.b + 0.5), 0.0, 255.0); - g_AstcEpVals[5] = (uint)clamp(floor(e1.b + 0.5), 0.0, 255.0); - g_AstcEpVals[6] = (uint)clamp(floor(e0.a + 0.5), 0.0, 255.0); - g_AstcEpVals[7] = (uint)clamp(floor(e1.a + 0.5), 0.0, 255.0); - - [loop] for (uint z = 0u; z < 8u; ++z) { g_AstcEpCArr[z] = 0u; g_AstcEpUqArr[z] = 255u; } - - uint cnt = (effMode == ASTC_EFF_LDR_RGBA) ? 8u : 6u; [loop] for (uint i = 0u; i < cnt; ++i) { + uint ch = (effMode == ASTC_EFF_LDR_NORMAL) ? ((i < 2u) ? 0u : 3u) : (i >> 1u); + uint val = ((i & 1u) == 0u) + ? ((ch == 0u) ? q0.x : ((ch == 1u) ? q0.y : ((ch == 2u) ? q0.z : q0.w))) + : ((ch == 0u) ? q1.x : ((ch == 1u) ? q1.y : ((ch == 2u) ? q1.z : q1.w))); uint uqTmp; - g_AstcEpCArr[i] = AstcQuantizeColor(g_AstcEpVals[i], epQuant, uqTmp); - g_AstcEpUqArr[i] = uqTmp; + g_AstcEpCodes[codeOffset + i] = AstcQuantizeColor(val, epQuant, uqTmp); + if ((i & 1u) == 0u) + { + if (ch == 0u) u0.x = uqTmp; else if (ch == 1u) u0.y = uqTmp; else if (ch == 2u) u0.z = uqTmp; else u0.w = uqTmp; + } + else + { + if (ch == 0u) u1.x = uqTmp; else if (ch == 1u) u1.y = uqTmp; else if (ch == 2u) u1.z = uqTmp; else u1.w = uqTmp; + } } // Khronos ASTC spec: CEM 8/12 triggers blue-contraction & endpoint swap whenever (r1+g1+b1) < (r0+g0+b0). // Guarantee s1 >= s0 so hardware always decodes direct endpoints without unexpected contraction/inversion. - if ((g_AstcEpUqArr[0] + g_AstcEpUqArr[2] + g_AstcEpUqArr[4]) > (g_AstcEpUqArr[1] + g_AstcEpUqArr[3] + g_AstcEpUqArr[5])) + if (effMode != ASTC_EFF_LDR_NORMAL && (u0.x + u0.y + u0.z) > (u1.x + u1.y + u1.z)) { swapped = true; - [loop] for (uint s = 0u; s < 8u; s += 2u) + uint4 tu = u0; u0 = u1; u1 = tu; + [loop] for (uint s = 0u; s < cnt; s += 2u) { - uint tc = g_AstcEpCArr[s]; g_AstcEpCArr[s] = g_AstcEpCArr[s + 1u]; g_AstcEpCArr[s + 1u] = tc; - uint tu = g_AstcEpUqArr[s]; g_AstcEpUqArr[s] = g_AstcEpUqArr[s + 1u]; g_AstcEpUqArr[s + 1u] = tu; + uint tc = g_AstcEpCodes[codeOffset + s]; + g_AstcEpCodes[codeOffset + s] = g_AstcEpCodes[codeOffset + s + 1u]; + g_AstcEpCodes[codeOffset + s + 1u] = tc; } } - [loop] for (uint k = 0u; k < cnt; ++k) - { - g_AstcEpCodes[codeOffset + k] = g_AstcEpCArr[k]; - } - - uqE0 = float4((float)g_AstcEpUqArr[0], (float)g_AstcEpUqArr[2], (float)g_AstcEpUqArr[4], (float)g_AstcEpUqArr[6]); - uqE1 = float4((float)g_AstcEpUqArr[1], (float)g_AstcEpUqArr[3], (float)g_AstcEpUqArr[5], (float)g_AstcEpUqArr[7]); + uqE0 = (float4)u0; + uqE1 = (float4)u1; #else // HDR RGB / HDR RGB + LDR Alpha / HDR RGBA // Orient endpoints so e1 holds the major RGB component, enabling CEM 11 high-precision delta modes. @@ -883,11 +860,13 @@ void AstcComputeWeightGrid2P( #endif // ASTC_ENABLE_2P // Unified exact hardware-reconstructed candidate error evaluator for 1P (stage=0), Dual-Plane (stage=1), and 2P (stage=2). +// When skipWeightQuant is true, reuses the already-quantized g_AstcGridUq weights (for endpoint refit evaluation). float AstcEvalCandidateError( uint baseX, uint baseY, uint effMode, uint stage, uint ccs, + bool skipWeightQuant, #if ASTC_ENABLE_2P AstcPart2Params p2, #endif @@ -898,7 +877,12 @@ float AstcEvalCandidateError( uint gridW, uint gridH, uint wQuant, uint epQuant, float bestError, - out float4 outChanErr) + out float4 outChanErr, + out float4 outUq0_p0, out float4 outUq1_p0 +#if ASTC_ENABLE_2P + , out float4 outUq0_p1, out float4 outUq1_p1 +#endif +) { bool isDual = (stage == 1u); float4 uq0_p0 = 0.0, uq1_p0 = 0.0; @@ -919,27 +903,36 @@ float AstcEvalCandidateError( if (pi == 0u) { uq0_p0 = uq0; uq1_p0 = uq1; swap0 = sw; } else { uq0_p1 = uq0; uq1_p1 = uq1; swap1 = sw; } } + outUq0_p1 = uq0_p1; + outUq1_p1 = uq1_p1; #else AstcQuantizeEndpoints(e0_p0, e1_p0, effMode, epQuant, 0u, uq0_p0, uq1_p0, swap0); #endif + outUq0_p0 = uq0_p0; + outUq1_p0 = uq1_p0; - uint gridCount = gridW * gridH; - [loop] for (uint k = 0u; k < gridCount; ++k) + if (!skipWeightQuant) { - uint uq0; - AstcQuantizeWeight(g_AstcGrid0[k], wQuant, uq0); - g_AstcGridUq0[k] = uq0; - if (isDual) + uint gridCount = gridW * gridH; + [loop] for (uint k = 0u; k < gridCount; ++k) { - uint uq1; - AstcQuantizeWeight(g_AstcGrid1[k], wQuant, uq1); - g_AstcGridUq1[k] = uq1; + uint uq0; + AstcQuantizeWeight(g_AstcGrid0[k], wQuant, uq0); + uint packedUq = uq0; + if (isDual) + { + uint uq1; + AstcQuantizeWeight(g_AstcGrid1[k], wQuant, uq1); + packedUq |= (uq1 << 8u); + } + g_AstcGridUq[k] = packedUq; } } float4 mask = AstcChannelMaskEx(effMode, 4u); float totalErr = 0.0; outChanErr = 0.0; + bool applySwap0 = !skipWeightQuant && swap0; [loop] for (uint y = 0u; y < BLOCK_Y; ++y) { @@ -948,14 +941,14 @@ float AstcEvalCandidateError( float imp; float4 p = AstcFetchWorkingPixel(baseX, baseY, x, y, effMode, imp); uint w0; - ASTC_EVAL_INFILL(g_AstcGridUq0, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w0); + ASTC_EVAL_INFILL(g_AstcGridUq, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w0); float4 recon; #if ASTC_ENABLE_2P if (isTwoP) { uint part = AstcEvalPartition2(p2, x, y); - uint effW0 = ((part == 0u) ? swap0 : swap1) ? (64u - w0) : w0; + uint effW0 = ((part == 0u) ? applySwap0 : swap1) ? (64u - w0) : w0; recon = (part == 0u) ? lerp(uq0_p0, uq1_p0, (float)effW0 * (1.0 / 64.0)) : lerp(uq0_p1, uq1_p1, (float)effW0 * (1.0 / 64.0)); @@ -963,13 +956,13 @@ float AstcEvalCandidateError( else #endif { - uint effW0 = swap0 ? (64u - w0) : w0; + uint effW0 = applySwap0 ? (64u - w0) : w0; recon = lerp(uq0_p0, uq1_p0, (float)effW0 * (1.0 / 64.0)); if (isDual) { uint w1; - ASTC_EVAL_INFILL(g_AstcGridUq1, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w1); - uint effW1 = swap0 ? (64u - w1) : w1; + ASTC_EVAL_INFILL_SHIFT(g_AstcGridUq, 8u, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w1); + uint effW1 = applySwap0 ? (64u - w1) : w1; float p1Val = lerp(AstcGetChannel(uq0_p0, ccs), AstcGetChannel(uq1_p0, ccs), (float)effW1 * (1.0 / 64.0)); recon = AstcSetChannel(recon, ccs, p1Val); } @@ -988,7 +981,7 @@ float AstcEvalCandidateError( float4 diff = (p - recon) * mask; float4 chImp = AstcGetChannelImp(effMode, imp); float4 sq = diff * diff * chImp; - if (stage == 0u) outChanErr += sq; + if (!skipWeightQuant && stage == 0u) outChanErr += sq; totalErr += sq.r + sq.g + sq.b + sq.a; } } @@ -1113,18 +1106,20 @@ void AstcFitBothPartitions2P( } #endif // ASTC_ENABLE_2P -// Unified post-quantization ideal endpoint refit for 1P single-plane and Dual-Plane blocks. -void AstcRefitEndpoints( +// Solves the post-quantization least-squares endpoint refit for 1P single-plane and Dual-Plane blocks. +// Returns true if endpoints changed; caller evaluates refit error through the shared AstcEvalCandidateError site. +bool AstcSolveRefitEndpoints( uint baseX, uint baseY, uint effMode, bool isDualPlane, uint ccs, uint gridW, uint gridH, - uint epQuant, - float4 curUqE0, float4 curUqE1) + float4 curUqE0, float4 curUqE1, + out float origErr, + out float4 refitE0, out float4 refitE1) { float4 mask = AstcChannelMaskEx(effMode, 4u); - float origErr = 0.0; + origErr = 0.0; float a00_p0 = 0.0, a01_p0 = 0.0, a11_p0 = 0.0; float a00_p0a = 0.0, a01_p0a = 0.0, a11_p0a = 0.0; @@ -1140,7 +1135,7 @@ void AstcRefitEndpoints( float4 p = AstcFetchWorkingPixel(baseX, baseY, x, y, effMode, imp); float4 chImp = AstcGetChannelImp(effMode, imp); uint w0_64; - ASTC_EVAL_INFILL(g_AstcGridUq0, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w0_64); + ASTC_EVAL_INFILL(g_AstcGridUq, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w0_64); float w0 = (float)w0_64 * (1.0 / 64.0); float omw0 = 1.0 - w0; @@ -1149,7 +1144,7 @@ void AstcRefitEndpoints( if (isDualPlane) { uint w1_64; - ASTC_EVAL_INFILL(g_AstcGridUq1, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w1_64); + ASTC_EVAL_INFILL_SHIFT(g_AstcGridUq, 8u, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w1_64); w1 = (float)w1_64 * (1.0 / 64.0); recon = AstcSetChannel(recon, ccs, lerp(AstcGetChannel(curUqE0, ccs), AstcGetChannel(curUqE1, ccs), w1)); } @@ -1194,8 +1189,8 @@ void AstcRefitEndpoints( } } - float4 refitE0 = curUqE0; - float4 refitE1 = curUqE1; + refitE0 = curUqE0; + refitE1 = curUqE1; bool changed = false; float detP0 = a00_p0 * a11_p0 - a01_p0 * a01_p0; @@ -1228,56 +1223,13 @@ void AstcRefitEndpoints( changed = true; } - if (!changed) return; - - [loop] for (uint sv = 0u; sv < 8u; ++sv) g_AstcCandEpCodes[sv] = g_AstcEpCodes[sv]; - float4 uq0, uq1; - bool dummySwapped; - AstcQuantizeEndpoints(refitE0, refitE1, effMode, epQuant, 0u, uq0, uq1, dummySwapped); - - float refitErr = 0.0; - [loop] for (uint y2 = 0u; y2 < BLOCK_Y; ++y2) - { - [loop] for (uint x2 = 0u; x2 < BLOCK_X; ++x2) - { - float imp; - float4 p = AstcFetchWorkingPixel(baseX, baseY, x2, y2, effMode, imp); - float4 chImp = AstcGetChannelImp(effMode, imp); - uint w0_64; - ASTC_EVAL_INFILL(g_AstcGridUq0, x2, y2, BLOCK_X, BLOCK_Y, gridW, gridH, w0_64); - float4 recon = lerp(uq0, uq1, (float)w0_64 * (1.0 / 64.0)); - if (isDualPlane) - { - uint w1_64; - ASTC_EVAL_INFILL(g_AstcGridUq1, x2, y2, BLOCK_X, BLOCK_Y, gridW, gridH, w1_64); - recon = AstcSetChannel(recon, ccs, lerp(AstcGetChannel(uq0, ccs), AstcGetChannel(uq1, ccs), (float)w1_64 * (1.0 / 64.0))); - } - - if (isDualPlane && effMode == ASTC_EFF_LDR_NORMAL) - { - float2 nOrig = float2(p.w, p.x) * (2.0 / 255.0) - 1.0; - float zOrig = sqrt(saturate(1.0 - dot(nOrig, nOrig))); - float2 nRec = float2(recon.a, recon.r) * (2.0 / 255.0) - 1.0; - float zRec = sqrt(saturate(1.0 - dot(nRec, nRec))); - refitErr += max(0.0, 1.0 - (dot(nOrig, nRec) + zOrig * zRec)); - } - else - { - float4 diff = (p - recon) * mask; - refitErr += dot(diff * diff, chImp); - } - } - } - - if (refitErr >= origErr) - { - [loop] for (uint ei = 0u; ei < 8u; ++ei) g_AstcEpCodes[ei] = g_AstcCandEpCodes[ei]; - } + return changed; } #endif // !ASTC_FAST_ONLY -// Assembles the final winning AstcCandidate into a 128-bit physical ASTC block. +#if defined(ASTC_FAST_ONLY) +// Assembles the final winning AstcCandidate into a 128-bit physical ASTC block (Fast path). uint4 AstcAssembleBlock(uint baseX, uint baseY, uint effMode, AstcCandidate c) { uint epPerPart = AstcGetEpCountForEffMode(effMode); @@ -1287,28 +1239,12 @@ uint4 AstcAssembleBlock(uint baseX, uint baseY, uint effMode, AstcCandidate c) uint totalWeights = gridCount; float4 uqE0 = 0.0, uqE1 = 0.0; -#if ASTC_ENABLE_2P - float4 uq0_p1 = 0.0, uq1_p1 = 0.0; - uint partCnt = (c.isTwoPart != 0u) ? 2u : 1u; - [loop] for (uint pi = 0u; pi < partCnt; ++pi) - { - float4 te0 = (pi == 0u) ? c.e0 : c.e0_p1; - float4 te1 = (pi == 0u) ? c.e1 : c.e1_p1; - float4 tu0, tu1; - bool dummySwapped; - AstcQuantizeEndpoints(te0, te1, effMode, c.epQuant, pi * epPerPart, tu0, tu1, dummySwapped); - if (pi == 0u) { uqE0 = tu0; uqE1 = tu1; } - else { uq0_p1 = tu0; uq1_p1 = tu1; } - } -#else bool dummySwapped; AstcQuantizeEndpoints(c.e0, c.e1, effMode, c.epQuant, 0u, uqE0, uqE1, dummySwapped); -#endif uint4 block = uint4(0u, 0u, 0u, 0u); bool weightsPackedDirect = false; -#if defined(ASTC_FAST_ONLY) // Fast direct MSB-descending weight packing for 1-plane exact-grid power-of-2 weight modes: // eliminates g_AstcGrid0, g_AstcBiseVals, AstcEncodeBise (weights), and AstcReverse128. if (c.isDualPlane == 0u && c.gridW == BLOCK_X && c.gridH == BLOCK_Y) @@ -1358,64 +1294,34 @@ uint4 AstcAssembleBlock(uint baseX, uint baseY, uint effMode, AstcCandidate c) weightsPackedDirect = true; } } -#endif if (!weightsPackedDirect) { -#if ASTC_ENABLE_2P - if (c.isTwoPart != 0u) + bool isDual = (c.isDualPlane != 0u); + uint exclCh = isDual ? c.dualPlaneChannel : 4u; + AstcComputeWeightGrid1P(baseX, baseY, effMode, exclCh, uqE0, uqE1, c.gridW, c.gridH); + if (isDual) { - AstcComputeWeightGrid2P(baseX, baseY, effMode, c.partSeed, uqE0, uqE1, uq0_p1, uq1_p1, c.gridW, c.gridH); - [loop] for (uint k = 0u; k < gridCount; ++k) - { - uint dummyUq; - g_AstcBiseVals[k] = AstcQuantizeWeight(g_AstcGrid0[k], c.wQuant, dummyUq); - } + AstcComputePlane1WeightGrid(baseX, baseY, effMode, c.dualPlaneChannel, AstcGetChannel(uqE0, c.dualPlaneChannel), AstcGetChannel(uqE1, c.dualPlaneChannel), c.gridW, c.gridH); } - else -#endif + + [loop] for (uint k = 0u; k < gridCount; ++k) { - bool isDual = (c.isDualPlane != 0u); - uint exclCh = isDual ? c.dualPlaneChannel : 4u; - AstcComputeWeightGrid1P(baseX, baseY, effMode, exclCh, uqE0, uqE1, c.gridW, c.gridH); + uint uq0; + uint code0 = AstcQuantizeWeight(g_AstcGrid0[k], c.wQuant, uq0); if (isDual) { - AstcComputePlane1WeightGrid(baseX, baseY, effMode, c.dualPlaneChannel, AstcGetChannel(uqE0, c.dualPlaneChannel), AstcGetChannel(uqE1, c.dualPlaneChannel), c.gridW, c.gridH); + uint uq1; + g_AstcBiseVals[2u * k] = code0; + g_AstcBiseVals[2u * k + 1u] = AstcQuantizeWeight(g_AstcGrid1[k], c.wQuant, uq1); } - - [loop] for (uint k = 0u; k < gridCount; ++k) - { - uint uq0; - uint code0 = AstcQuantizeWeight(g_AstcGrid0[k], c.wQuant, uq0); -#if !defined(ASTC_FAST_ONLY) - g_AstcGridUq0[k] = uq0; -#endif - if (isDual) - { - uint uq1; - g_AstcBiseVals[2u * k] = code0; - g_AstcBiseVals[2u * k + 1u] = AstcQuantizeWeight(g_AstcGrid1[k], c.wQuant, uq1); -#if !defined(ASTC_FAST_ONLY) - g_AstcGridUq1[k] = uq1; -#endif - } - else - { - g_AstcBiseVals[k] = code0; - } - } - if (isDual) totalWeights = gridCount * 2u; - -#if !defined(ASTC_FAST_ONLY) - if (_AstcQuality >= 1u) + else { - AstcRefitEndpoints(baseX, baseY, effMode, isDual, c.dualPlaneChannel, c.gridW, c.gridH, c.epQuant, uqE0, uqE1); + g_AstcBiseVals[k] = code0; } -#endif } + if (isDual) totalWeights = gridCount * 2u; - // Fast direct MSB-descending weight packing when weight quant is pure power-of-2 (1..4 bits); - // otherwise pack via BISE at bit 0 and reverse all 128 bits. uint wRange2, wBits2, wTrits2, wQuints2; AstcGetQuantParams(c.wQuant, wRange2, wBits2, wTrits2, wQuints2); if (wTrits2 == 0u && wQuints2 == 0u && wBits2 >= 1u && wBits2 <= 4u) @@ -1434,6 +1340,44 @@ uint4 AstcAssembleBlock(uint baseX, uint baseY, uint effMode, AstcCandidate c) } } + AstcWriteBits128(block, 0u, 11u, c.blockMode); + AstcWriteBits128(block, 11u, 2u, 0u); + AstcWriteBits128(block, 13u, 4u, cem); + if (c.isDualPlane != 0u) + { + uint weightBits = AstcGetBiseBitCount(totalWeights, c.wQuant); + AstcWriteBits128(block, 128u - weightBits - 2u, 2u, c.dualPlaneChannel); + } + [loop] for (uint ei = 0u; ei < epPerPart; ++ei) g_AstcBiseVals[ei] = g_AstcEpCodes[ei]; + AstcEncodeBise(block, 17u, c.epQuant, epPerPart); + + return block; +} +#else +// Packs pre-computed weights (g_AstcBiseVals) and endpoints (g_AstcBestEpCodes) into a 128-bit ASTC block. +uint4 AstcPackFinalBlock(uint effMode, AstcCandidate c, uint totalWeights) +{ + uint epPerPart = AstcGetEpCountForEffMode(effMode); + uint cem = AstcGetCemForEffMode(effMode); + uint4 block = uint4(0u, 0u, 0u, 0u); + + uint wRange2, wBits2, wTrits2, wQuints2; + AstcGetQuantParams(c.wQuant, wRange2, wBits2, wTrits2, wQuints2); + if (wTrits2 == 0u && wQuints2 == 0u && wBits2 >= 1u && wBits2 <= 4u) + { + [loop] for (uint wi = 0u; wi < totalWeights; ++wi) + { + uint revW = AstcReverseSmallBits(g_AstcBiseVals[wi], wBits2); + AstcWriteBits128(block, 128u - (wi + 1u) * wBits2, wBits2, revW); + } + } + else + { + uint4 wBuf = uint4(0u, 0u, 0u, 0u); + AstcEncodeBise(wBuf, 0u, c.wQuant, totalWeights); + block = AstcReverse128(wBuf); + } + // Header: BlockMode (11 bits) + PartitionCount-1 (2 bits) AstcWriteBits128(block, 0u, 11u, c.blockMode); uint epStartBit = 17u; @@ -1459,17 +1403,16 @@ uint4 AstcAssembleBlock(uint baseX, uint baseY, uint effMode, AstcCandidate c) AstcWriteBits128(block, 128u - weightBits - 2u, 2u, c.dualPlaneChannel); } } - [loop] for (uint ei = 0u; ei < epTotalCount; ++ei) g_AstcBiseVals[ei] = g_AstcEpCodes[ei]; + [loop] for (uint ei = 0u; ei < epTotalCount; ++ei) g_AstcBiseVals[ei] = g_AstcBestEpCodes[ei]; AstcEncodeBise(block, epStartBit, c.epQuant, epTotalCount); return block; } -#if !defined(ASTC_FAST_ONLY) - void AstcTryCandidate( uint baseX, uint baseY, uint effMode, uint stage, uint ccs, + bool isRefitPass, float refitThreshold, #if ASTC_ENABLE_2P uint partSeed, AstcPart2Params p2, #endif @@ -1483,35 +1426,49 @@ void AstcTryCandidate( inout float4 bestChanErr) { float4 chanErr; + float4 uq0_p0, uq1_p0; + float errLimit = isRefitPass ? refitThreshold : best.error; #if ASTC_ENABLE_2P - float err = AstcEvalCandidateError(baseX, baseY, effMode, stage, ccs, p2, e0_p0, e1_p0, e0_p1, e1_p1, gw, gh, wQ, epQ, best.error, chanErr); + float4 uq0_p1, uq1_p1; + float err = AstcEvalCandidateError(baseX, baseY, effMode, stage, ccs, isRefitPass, p2, e0_p0, e1_p0, e0_p1, e1_p1, gw, gh, wQ, epQ, errLimit, chanErr, uq0_p0, uq1_p0, uq0_p1, uq1_p1); #else - float err = AstcEvalCandidateError(baseX, baseY, effMode, stage, ccs, e0_p0, e1_p0, gw, gh, wQ, epQ, best.error, chanErr); + float err = AstcEvalCandidateError(baseX, baseY, effMode, stage, ccs, isRefitPass, e0_p0, e1_p0, gw, gh, wQ, epQ, errLimit, chanErr, uq0_p0, uq1_p0); #endif - if (err < best.error) + if (err < errLimit) { - best.blockMode = bm; - best.gridW = gw; - best.gridH = gh; - best.wQuant = wQ; - best.epQuant = epQ; - best.isDualPlane = (stage == 1u) ? 1u : 0u; - best.dualPlaneChannel = (stage == 1u) ? ccs : 3u; - best.error = err; - best.e0 = e0_p0; - best.e1 = e1_p0; + uint epPerPart = AstcGetEpCountForEffMode(effMode); #if ASTC_ENABLE_2P - best.isTwoPart = (stage == 2u) ? 1u : 0u; - best.partSeed = (stage == 2u) ? partSeed : 0u; - if (stage == 2u) - { - best.e0_p1 = e0_p1; - best.e1_p1 = e1_p1; - } + uint epTotal = (!isRefitPass && stage == 2u) ? (epPerPart * 2u) : epPerPart; +#else + uint epTotal = epPerPart; #endif - if (stage == 0u) + [loop] for (uint ei = 0u; ei < epTotal; ++ei) g_AstcBestEpCodes[ei] = g_AstcEpCodes[ei]; + + if (!isRefitPass) { - bestChanErr = chanErr; + best.blockMode = bm; + best.gridW = gw; + best.gridH = gh; + best.wQuant = wQ; + best.epQuant = epQ; + best.isDualPlane = (stage == 1u) ? 1u : 0u; + best.dualPlaneChannel = (stage == 1u) ? ccs : 3u; + best.error = err; + best.e0 = uq0_p0; + best.e1 = uq1_p0; +#if ASTC_ENABLE_2P + best.isTwoPart = (stage == 2u) ? 1u : 0u; + best.partSeed = (stage == 2u) ? partSeed : 0u; + if (stage == 2u) + { + best.e0_p1 = uq0_p1; + best.e1_p1 = uq1_p1; + } +#endif + if (stage == 0u) + { + bestChanErr = chanErr; + } } } } @@ -1841,16 +1798,8 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) #endif #if defined(ASTC_FAST_ONLY) - uint refineIters = 0u; -#elif defined(ASTC_NO_2P) - uint refineIters = (!isNormal) ? 1u : 0u; -#else - uint refineIters = (!isNormal && _AstcQuality >= 1u) ? ((_AstcQuality >= 2u) ? 2u : 1u) : 0u; -#endif float4 e0, e1; - AstcFitSubsetEndpoints(baseX, baseY, effMode, isNormal ? 3u : 4u, 0u, 2u, refineIters, hasPreStats, preMeanP, preMinC, preMaxC, e0, e1); - -#if defined(ASTC_FAST_ONLY) + AstcFitSubsetEndpoints(baseX, baseY, effMode, isNormal ? 3u : 4u, 0u, 2u, 0u, hasPreStats, preMeanP, preMinC, preMaxC, e0, e1); best.e0 = e0; best.e1 = e1; if (isNormal) @@ -1882,45 +1831,15 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) best.blockMode = 0x0E2u; best.gridW = 5u; best.gridH = 5u; best.wQuant = ASTC_QUANT_4; best.epQuant = ASTC_QUANT_192; #endif } + return AstcAssembleBlock(baseX, baseY, effMode, best); #else -#if !defined(ASTC_NO_2P) - if (_AstcQuality == 0u) + uint finalTotalWeights = 16u; { - best.e0 = e0; - best.e1 = e1; - if (isNormal) - { - best.blockMode = 0x452u; best.gridW = 4u; best.gridH = 4u; best.wQuant = ASTC_QUANT_5; best.epQuant = ASTC_QUANT_256; - best.isDualPlane = 1u; best.dualPlaneChannel = 3u; - } - else if (!hasAlpha) - { -#if BLOCK_X == 4 - best.blockMode = 0x242u; best.gridW = 4u; best.gridH = 4u; best.wQuant = ASTC_QUANT_16; best.epQuant = ASTC_QUANT_192; -#elif BLOCK_X == 5 - best.blockMode = 0x0F3u; best.gridW = 5u; best.gridH = 5u; best.wQuant = ASTC_QUANT_8; best.epQuant = ASTC_QUANT_64; -#elif BLOCK_X == 6 - best.blockMode = 0x108u; best.gridW = 6u; best.gridH = 6u; best.wQuant = ASTC_QUANT_4; best.epQuant = ASTC_QUANT_80; -#else - best.blockMode = 0x0E3u; best.gridW = 5u; best.gridH = 5u; best.wQuant = ASTC_QUANT_6; best.epQuant = ASTC_QUANT_192; -#endif - } - else - { -#if BLOCK_X == 4 - best.blockMode = 0x053u; best.gridW = 4u; best.gridH = 4u; best.wQuant = ASTC_QUANT_8; best.epQuant = ASTC_QUANT_192; -#elif BLOCK_X == 5 - best.blockMode = 0x0E2u; best.gridW = 5u; best.gridH = 5u; best.wQuant = ASTC_QUANT_4; best.epQuant = ASTC_QUANT_192; -#elif BLOCK_X == 6 - best.blockMode = 0x108u; best.gridW = 6u; best.gridH = 6u; best.wQuant = ASTC_QUANT_4; best.epQuant = ASTC_QUANT_24; +#if defined(ASTC_NO_2P) + uint refineIters = (!isNormal) ? 1u : 0u; #else - best.blockMode = 0x0E2u; best.gridW = 5u; best.gridH = 5u; best.wQuant = ASTC_QUANT_4; best.epQuant = ASTC_QUANT_192; -#endif - } - } - else + uint refineIters = (!isNormal && _AstcQuality >= 1u) ? ((_AstcQuality >= 2u) ? 2u : 1u) : 0u; #endif - { float4 bestChanErr = 0.0; #if ASTC_ENABLE_2P AstcPart2Params p2Params; @@ -1934,87 +1853,118 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) float earlyOutErr = (hasAlpha ? 4.0 : 3.0) * (255.0 * 255.0 * 1e-5) * (float)(BLOCK_X * BLOCK_Y); #endif - // Unified multi-stage candidate search: stage 0 = 1P, stage 1 = Dual-Plane, stage 2 = 2P (High quality). - // Consolidates AstcTryCandidate / AstcEvalCandidateError / AstcComputeWeightGrid1P to a single inlined site. + // Unified multi-stage candidate search + finalization pass: + // stage 0 = 1P, stage 1 = Dual-Plane, [stage 2 = 2P], stage == maxStage = final weight quant & endpoint refit. + // Consolidates AstcFitSubsetEndpoints, AstcQuantizeEndpoints, AstcEvalCandidateError, and weight-grid functions + // to single inlined sites to minimize FXC/DXC/HLSLcc compile time and VGPR pressure. #if ASTC_ENABLE_2P const uint maxStage = 3u; #else const uint maxStage = 2u; #endif - [loop] for (uint stage = 0u; stage < maxStage; ++stage) + [loop] for (uint stage = 0u; stage <= maxStage; ++stage) { - if (stage == 0u && isNormal) continue; + bool isFinalPass = (stage == maxStage); + if (!isFinalPass) + { + if (stage == 0u && isNormal) continue; #if defined(ASTC_NO_2P) && !defined(ASTC_HDR_VARIANT) - if (stage == 1u && canEarlyOut && best.error <= earlyOutErr) break; + if (stage == 1u && canEarlyOut && best.error <= earlyOutErr) continue; #endif #if ASTC_ENABLE_2P - if (stage == 2u && (isNormal || _AstcQuality < 2u)) continue; + if (stage == 2u && (isNormal || _AstcQuality < 2u)) continue; #endif + } + uint effStage = stage; uint stageCcs = 4u; - float4 sE0_p0 = e0, sE1_p0 = e1; + float4 sE0_p0 = 0.0, sE1_p0 = 0.0; #if ASTC_ENABLE_2P uint stageSeed = 0u; float4 sE0_p1 = 0.0, sE1_p1 = 0.0; #endif - uint candCount = 0u; + uint candCount = 1u; - if (stage == 0u) + if (isFinalPass) + { +#if ASTC_ENABLE_2P + if (best.isTwoPart != 0u) + { + effStage = 2u; + stageSeed = best.partSeed; + sE0_p0 = best.e0; sE1_p0 = best.e1; + sE0_p1 = best.e0_p1; sE1_p1 = best.e1_p1; + } + else +#endif + { + effStage = (best.isDualPlane != 0u) ? 1u : 0u; + stageCcs = (best.isDualPlane != 0u) ? best.dualPlaneChannel : 4u; + sE0_p0 = best.e0; sE1_p0 = best.e1; + } + } + else if (stage < 2u) { + uint fitRefine = refineIters; + if (stage == 0u) + { + stageCcs = 4u; #if defined(ASTC_NO_2P) #if BLOCK_X >= 6 - candCount = 4u; + candCount = 4u; #elif BLOCK_X >= 5 - candCount = 3u; + candCount = 3u; #else - candCount = 2u; + candCount = 2u; #endif #else - if (!hasAlpha) - { + if (!hasAlpha) + { #if BLOCK_X >= 8 - candCount = 12u; + candCount = 12u; #elif BLOCK_X >= 6 - candCount = 10u; + candCount = 10u; #elif BLOCK_X >= 5 - candCount = 6u; + candCount = 6u; #else - candCount = 3u; + candCount = 3u; #endif - } - else - { + } + else + { #if BLOCK_X >= 6 - candCount = 8u; + candCount = 8u; #elif BLOCK_X >= 5 - candCount = 5u; + candCount = 5u; #else - candCount = 3u; + candCount = 3u; #endif - } + } #endif - } - else if (stage == 1u) - { - stageCcs = 3u; - if (!isNormal) - { - stageCcs = 0u; - float maxChErr = bestChanErr.r; - if (bestChanErr.g > maxChErr) { maxChErr = bestChanErr.g; stageCcs = 1u; } - if (bestChanErr.b > maxChErr) { maxChErr = bestChanErr.b; stageCcs = 2u; } - if (hasAlpha && bestChanErr.a > maxChErr) { stageCcs = 3u; } - AstcFitSubsetEndpoints(baseX, baseY, effMode, stageCcs, 0u, 2u, 1u, hasPreStats, preMeanP, preMinC, preMaxC, sE0_p0, sE1_p0); } + else + { + stageCcs = 3u; + fitRefine = isNormal ? 0u : 1u; + if (!isNormal) + { + stageCcs = 0u; + float maxChErr = bestChanErr.r; + if (bestChanErr.g > maxChErr) { maxChErr = bestChanErr.g; stageCcs = 1u; } + if (bestChanErr.b > maxChErr) { maxChErr = bestChanErr.b; stageCcs = 2u; } + if (hasAlpha && bestChanErr.a > maxChErr) { stageCcs = 3u; } + } #if BLOCK_X >= 5 #if defined(ASTC_NO_2P) - candCount = isNormal ? 4u : 2u; + candCount = isNormal ? 4u : 2u; #else - candCount = isNormal ? 4u : 3u; + candCount = isNormal ? 4u : 3u; #endif #else - candCount = 2u; + candCount = 2u; #endif + } + AstcFitSubsetEndpoints(baseX, baseY, effMode, stageCcs, 0u, 2u, fitRefine, hasPreStats, preMeanP, preMinC, preMaxC, sE0_p0, sE1_p0); } #if ASTC_ENABLE_2P else @@ -2035,7 +1985,11 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) [loop] for (uint ci = 0u; ci < candCount; ++ci) { uint bm, gw, gh, wQ, epQ; - if (stage == 0u) + if (isFinalPass) + { + bm = best.blockMode; gw = best.gridW; gh = best.gridH; wQ = best.wQuant; epQ = best.epQuant; + } + else if (stage == 0u) { #if defined(ASTC_NO_2P) if (!hasAlpha) @@ -2085,27 +2039,47 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) { if (ci == 0u) { bm = 0x251u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_12; epQ = ASTC_QUANT_256; } else if (ci == 1u) { bm = 0x242u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_16; epQ = ASTC_QUANT_192; } +#if BLOCK_X >= 5 else if (ci == 2u) { bm = 0x252u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_20; epQ = ASTC_QUANT_96; } else if (ci == 3u) { bm = 0x0F2u; gw = 5u; gh = 5u; wQ = ASTC_QUANT_5; epQ = ASTC_QUANT_256; } else if (ci == 4u) { bm = 0x0E3u; gw = 5u; gh = 5u; wQ = ASTC_QUANT_6; epQ = ASTC_QUANT_192; } +#if BLOCK_X >= 6 else if (ci == 5u) { bm = 0x0F3u; gw = 5u; gh = 5u; wQ = ASTC_QUANT_8; epQ = ASTC_QUANT_64; } else if (ci == 6u) { bm = 0x114u; gw = 6u; gh = 6u; wQ = ASTC_QUANT_3; epQ = ASTC_QUANT_256; } else if (ci == 7u) { bm = 0x108u; gw = 6u; gh = 6u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_80; } else if (ci == 8u) { bm = 0x143u; gw = 6u; gh = 4u; wQ = ASTC_QUANT_6; epQ = ASTC_QUANT_256; } +#if BLOCK_X >= 8 else if (ci == 9u) { bm = 0x04Fu; gw = 4u; gh = 6u; wQ = ASTC_QUANT_6; epQ = ASTC_QUANT_256; } else if (ci == 10u){ bm = 0x046u; gw = 8u; gh = 4u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_192; } else { bm = 0x04Au; gw = 4u; gh = 8u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_192; } +#else + else { bm = 0x04Fu; gw = 4u; gh = 6u; wQ = ASTC_QUANT_6; epQ = ASTC_QUANT_256; } +#endif +#else + else { bm = 0x0F3u; gw = 5u; gh = 5u; wQ = ASTC_QUANT_8; epQ = ASTC_QUANT_64; } +#endif +#else + else { bm = 0x252u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_20; epQ = ASTC_QUANT_96; } +#endif } else { if (ci == 0u) { bm = 0x043u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_6; epQ = ASTC_QUANT_256; } else if (ci == 1u) { bm = 0x053u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_8; epQ = ASTC_QUANT_192; } +#if BLOCK_X >= 5 else if (ci == 2u) { bm = 0x251u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_12; epQ = ASTC_QUANT_96; } else if (ci == 3u) { bm = 0x0E2u; gw = 5u; gh = 5u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_192; } +#if BLOCK_X >= 6 else if (ci == 4u) { bm = 0x0F2u; gw = 5u; gh = 5u; wQ = ASTC_QUANT_5; epQ = ASTC_QUANT_80; } else if (ci == 5u) { bm = 0x114u; gw = 6u; gh = 6u; wQ = ASTC_QUANT_3; epQ = ASTC_QUANT_96; } else if (ci == 6u) { bm = 0x142u; gw = 6u; gh = 4u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_192; } else { bm = 0x04Eu; gw = 4u; gh = 6u; wQ = ASTC_QUANT_4; epQ = ASTC_QUANT_192; } +#else + else { bm = 0x0F2u; gw = 5u; gh = 5u; wQ = ASTC_QUANT_5; epQ = ASTC_QUANT_80; } +#endif +#else + else { bm = 0x251u; gw = 4u; gh = 4u; wQ = ASTC_QUANT_12; epQ = ASTC_QUANT_96; } +#endif } #endif } @@ -2158,7 +2132,7 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) if (gw != cachedGW || gh != cachedGH) { #if ASTC_ENABLE_2P - if (stage == 2u) + if (effStage == 2u) { AstcComputeWeightGrid2P(baseX, baseY, effMode, stageSeed, sE0_p0, sE1_p0, sE0_p1, sE1_p1, gw, gh); } @@ -2166,7 +2140,7 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) #endif { AstcComputeWeightGrid1P(baseX, baseY, effMode, stageCcs, sE0_p0, sE1_p0, gw, gh); - if (stage == 1u) + if (effStage == 1u) { AstcComputePlane1WeightGrid(baseX, baseY, effMode, stageCcs, AstcGetChannel(sE0_p0, stageCcs), AstcGetChannel(sE1_p0, stageCcs), gw, gh); } @@ -2175,17 +2149,52 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) cachedGH = gh; } + bool runEval = !isFinalPass; + float refitOrigErr = 0.0; + float4 evalE0 = sE0_p0, evalE1 = sE1_p0; + + if (isFinalPass) + { + uint gridCount = gw * gh; + bool isDualFinal = (effStage == 1u); + [loop] for (uint k = 0u; k < gridCount; ++k) + { + uint uq0; + uint code0 = AstcQuantizeWeight(g_AstcGrid0[k], wQ, uq0); + if (isDualFinal) + { + uint uq1; + g_AstcBiseVals[2u * k] = code0; + g_AstcBiseVals[2u * k + 1u] = AstcQuantizeWeight(g_AstcGrid1[k], wQ, uq1); + g_AstcGridUq[k] = uq0 | (uq1 << 8u); + } + else + { + g_AstcBiseVals[k] = code0; + g_AstcGridUq[k] = uq0; + } + } + finalTotalWeights = isDualFinal ? (gridCount * 2u) : gridCount; + + if (effStage < 2u && _AstcQuality >= 1u) + { + runEval = AstcSolveRefitEndpoints(baseX, baseY, effMode, isDualFinal, stageCcs, gw, gh, best.e0, best.e1, refitOrigErr, evalE0, evalE1); + } + } + + if (runEval) + { #if ASTC_ENABLE_2P - AstcTryCandidate(baseX, baseY, effMode, stage, stageCcs, stageSeed, p2Params, sE0_p0, sE1_p0, sE0_p1, sE1_p1, bm, gw, gh, wQ, epQ, best, bestChanErr); + AstcTryCandidate(baseX, baseY, effMode, effStage, stageCcs, isFinalPass, refitOrigErr, stageSeed, p2Params, evalE0, evalE1, sE0_p1, sE1_p1, bm, gw, gh, wQ, epQ, best, bestChanErr); #else - AstcTryCandidate(baseX, baseY, effMode, stage, stageCcs, sE0_p0, sE1_p0, bm, gw, gh, wQ, epQ, best, bestChanErr); + AstcTryCandidate(baseX, baseY, effMode, effStage, stageCcs, isFinalPass, refitOrigErr, evalE0, evalE1, bm, gw, gh, wQ, epQ, best, bestChanErr); #endif + } } } } + return AstcPackFinalBlock(effMode, best, finalTotalWeights); #endif // !ASTC_FAST_ONLY - - return AstcAssembleBlock(baseX, baseY, effMode, best); } #endif // ASTC_GPU_CORE_HLSL From 49e19ed856bf853c2d9652c9cba04d3fefbe1969 Mon Sep 17 00:00:00 2001 From: FrozenKresnik <80754402+Overflowu@users.noreply.github.com> Date: Tue, 1 Dec 2026 13:56:52 +0000 Subject: [PATCH 6/9] perf(gpu): pack indexable temp arrays, collapse BISE/weight calls, and fix shader warnings --- .../Runtime/Gpu/Shaders/AstcGpuBise.hlsl | 270 ++++++++------- .../Runtime/Gpu/Shaders/AstcGpuColor.hlsl | 97 +++--- .../Runtime/Gpu/Shaders/AstcGpuCore.hlsl | 325 ++++++++++-------- .../Resources/AstcGpuMipGenerator.compute | 2 - 4 files changed, 379 insertions(+), 315 deletions(-) diff --git a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl index 64f9464..7f24a2b 100644 --- a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl +++ b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl @@ -100,35 +100,48 @@ uint AstcPack5Trits(uint t0, uint t1, uint t2, uint t3, uint t4) C = (t2 << 4u) | (t1 << 2u) | t0; } + uint res; if (t4 == 2u) { - return (t3 == 2u) + res = (t3 == 2u) ? (((C >> 2u) << 5u) | (7u << 2u) | (C & 3u)) : ((t3 << 7u) | (3u << 5u) | C); } - return (t4 << 7u) | (t3 << 5u) | C; + else + { + res = (t4 << 7u) | (t3 << 5u) | C; + } + return res; } // Pure-ALU BISE 3-quint packer -> 7-bit integer (exact match to Khronos ASTC spec). uint AstcPack3Quints(uint q0, uint q1, uint q2) { + uint res; if (q2 == 4u) { if (q1 == 4u) { - return (q0 == 4u) ? 7u : ((1u << 5u) | (q0 << 3u) | 7u); + res = (q0 == 4u) ? 7u : ((1u << 5u) | (q0 << 3u) | 7u); + } + else if (q0 == 4u) + { + res = (1u << 5u) | (q1 << 3u) | 6u; } - if (q0 == 4u) + else { - return (1u << 5u) | (q1 << 3u) | 6u; + res = ((3u - (q0 >> 1u)) << 5u) | (q1 << 3u) | 6u | (q0 & 1u); } - return ((3u - (q0 >> 1u)) << 5u) | (q1 << 3u) | 6u | (q0 & 1u); } - if (q1 == 4u) + else if (q1 == 4u) + { + res = (q0 == 4u) ? ((q2 << 3u) | 6u) : ((q2 << 5u) | (q0 << 3u) | 5u); + } + else { - return (q0 == 4u) ? ((q2 << 3u) | 6u) : ((q2 << 5u) | (q0 << 3u) | 5u); + res = (q2 << 5u) | (q1 << 3u) | q0; } - return (q2 << 5u) | (q1 << 3u) | q0; + return res; } // Maps a monotonically ordered quantized level k in [0, range-1] to the ASTC BISE code. @@ -137,27 +150,27 @@ uint AstcLinearToCode(uint k, uint q) { uint rangeVal, b, t, qu; AstcGetQuantParams(q, rangeVal, b, t, qu); - if ((t == 0u && qu == 0u) || b == 0u) + uint res = k; + if (!((t == 0u && qu == 0u) || b == 0u)) { - return k; - } + uint halfR = rangeVal >> 1u; + uint val = (k < halfR) ? k : ((rangeVal - 1u) - k); + uint oddBit = (k < halfR) ? 0u : 1u; - uint halfR = rangeVal >> 1u; - uint val = (k < halfR) ? k : ((rangeVal - 1u) - k); - uint oddBit = (k < halfR) ? 0u : 1u; - - uint m, d; - if (t != 0u) - { - m = (val * 0xAAABu) >> 17u; - d = val - m * 3u; - } - else - { - m = (val * 0xCCCDu) >> 18u; - d = val - m * 5u; + uint m, d; + if (t != 0u) + { + m = (val * 0xAAABu) >> 17u; + d = val - m * 3u; + } + else + { + m = (val * 0xCCCDu) >> 18u; + d = val - m * 5u; + } + res = (d << b) | (m << 1u) | oddBit; } - return (d << b) | (m << 1u) | oddBit; + return res; } // Exact Khronos ASTC color unquantization: BISE code v -> [0, 255]. @@ -165,6 +178,7 @@ uint AstcUnquantizeColor(uint v, uint q) { uint rangeVal, b, t, qu; AstcGetQuantParams(q, rangeVal, b, t, qu); + uint res; if (t == 0u && qu == 0u) { @@ -177,38 +191,41 @@ uint AstcUnquantizeColor(uint v, uint q) { r = (b == 3u) ? ((v << 5u) | (v << 2u) | (v >> 1u)) : ((b == 2u) ? (v * 85u) : (v * 255u)); } - return r & 0xFFu; - } - - uint D = v >> b; - uint low = v & ((1u << b) - 1u); - uint A = ((low & 1u) != 0u) ? 0x1FFu : 0u; - uint m = low >> 1u; - - uint C = 0u; - uint B = 0u; - if (t != 0u) - { - C = (b == 1u) ? 204u : ((b == 2u) ? 93u : ((b == 3u) ? 44u : ((b == 4u) ? 22u : ((b == 5u) ? 11u : 5u)))); - B = (b == 1u) ? 0u - : (b == 2u) ? ((m << 8u) | (m << 4u) | (m << 2u) | (m << 1u)) - : (b == 3u) ? ((m << 7u) | (m << 2u) | m) - : (b == 4u) ? ((m << 6u) | m) - : (b == 5u) ? ((m << 5u) | (m >> 2u)) - : ((m << 4u) | (m >> 4u)); + res = r & 0xFFu; } else { - C = (b == 1u) ? 113u : ((b == 2u) ? 54u : ((b == 3u) ? 26u : ((b == 4u) ? 13u : 6u))); - B = (b == 1u) ? 0u - : (b == 2u) ? ((m << 8u) | (m << 3u) | (m << 2u)) - : (b == 3u) ? ((m << 7u) | (m << 1u) | (m >> 1u)) - : (b == 4u) ? ((m << 6u) | (m >> 1u)) - : ((m << 5u) | (m >> 3u)); - } + uint D = v >> b; + uint low = v & ((1u << b) - 1u); + uint A = ((low & 1u) != 0u) ? 0x1FFu : 0u; + uint m = low >> 1u; - uint T = (D * C + B) ^ A; - return (A & 0x80u) | (T >> 2u); + uint C = 0u; + uint B = 0u; + if (t != 0u) + { + C = (b == 1u) ? 204u : ((b == 2u) ? 93u : ((b == 3u) ? 44u : ((b == 4u) ? 22u : ((b == 5u) ? 11u : 5u)))); + B = (b == 1u) ? 0u + : (b == 2u) ? ((m << 8u) | (m << 4u) | (m << 2u) | (m << 1u)) + : (b == 3u) ? ((m << 7u) | (m << 2u) | m) + : (b == 4u) ? ((m << 6u) | m) + : (b == 5u) ? ((m << 5u) | (m >> 2u)) + : ((m << 4u) | (m >> 4u)); + } + else + { + C = (b == 1u) ? 113u : ((b == 2u) ? 54u : ((b == 3u) ? 26u : ((b == 4u) ? 13u : 6u))); + B = (b == 1u) ? 0u + : (b == 2u) ? ((m << 8u) | (m << 3u) | (m << 2u)) + : (b == 3u) ? ((m << 7u) | (m << 1u) | (m >> 1u)) + : (b == 4u) ? ((m << 6u) | (m >> 1u)) + : ((m << 5u) | (m >> 3u)); + } + + uint T = (D * C + B) ^ A; + res = (A & 0x80u) | (T >> 2u); + } + return res; } uint AstcUnquantizePow2Weight(uint v, uint b) @@ -285,50 +302,58 @@ uint AstcUnquantizeWeight(uint v, uint q) // When min128 is true, constrains the unquantized value to >= 128 (HDR modeval/majcomp == 3 direct endpoints). uint AstcQuantizeColorRange(uint c8, uint q, bool min128, out uint unquantOut) { + unquantOut = 0u; uint rangeVal, b, t, qu; AstcGetQuantParams(q, rangeVal, b, t, qu); if (min128) c8 = max(c8, 128u); + + uint resultCode; if (q == ASTC_QUANT_256) { unquantOut = c8; - return c8; + resultCode = c8; } - - int minK = min128 ? (int)(rangeVal >> 1u) : 0; - int maxK = (int)rangeVal - 1; - int kEst = clamp((int)floor((float)c8 * (float)maxK / 255.0 + 0.5), minK, maxK); - - // O(1) fast path for pure power-of-2 quantization ranges (no trit/quint search loop or integer div/mod). - if (t == 0u && qu == 0u) + else { - uint v = (uint)kEst; - uint r = v << (8u - b); - if (b < 8u) r |= v >> max((int)b * 2 - 8, 0); - if (b <= 3u) + int minK = min128 ? (int)(rangeVal >> 1u) : 0; + int maxK = (int)rangeVal - 1; + int kEst = clamp((int)floor((float)c8 * (float)maxK / 255.0 + 0.5), minK, maxK); + + // O(1) fast path for pure power-of-2 quantization ranges (no trit/quint search loop or integer div/mod). + if (t == 0u && qu == 0u) { - r = (b == 3u) ? ((v << 5u) | (v << 2u) | (v >> 1u)) : ((b == 2u) ? (v * 85u) : (v * 255u)); + uint v = (uint)kEst; + uint r = v << (8u - b); + if (b < 8u) r |= v >> max((int)b * 2 - 8, 0); + if (b <= 3u) + { + r = (b == 3u) ? ((v << 5u) | (v << 2u) | (v >> 1u)) : ((b == 2u) ? (v * 85u) : (v * 255u)); + } + unquantOut = r & 0xFFu; + resultCode = v; } - unquantOut = r & 0xFFu; - return v; - } + else + { + uint bestCode = 0u; + uint bestUq = 0u; + int bestErr = 10000; - uint bestCode = 0u; - uint bestUq = 0u; - int bestErr = 10000; + [loop] for (int step = 0; step < 3; ++step) + { + int dk = (step == 0) ? 0 : ((step == 1) ? -1 : 1); + int k = kEst + dk; + if (k < minK || k > maxK) continue; + uint code = AstcLinearToCode((uint)k, q); + uint uq = AstcUnquantizeColor(code, q); + int err = abs((int)uq - (int)c8); + if (err < bestErr) { bestErr = err; bestCode = code; bestUq = uq; } + } - [loop] for (int step = 0; step < 3; ++step) - { - int dk = (step == 0) ? 0 : ((step == 1) ? -1 : 1); - int k = kEst + dk; - if (k < minK || k > maxK) continue; - uint code = AstcLinearToCode((uint)k, q); - uint uq = AstcUnquantizeColor(code, q); - int err = abs((int)uq - (int)c8); - if (err < bestErr) { bestErr = err; bestCode = code; bestUq = uq; } + unquantOut = bestUq; + resultCode = bestCode; + } } - - unquantOut = bestUq; - return bestCode; + return resultCode; } uint AstcQuantizeColor(uint c8, uint q, out uint unquantOut) @@ -344,10 +369,10 @@ uint AstcQuantizeColorMin128(uint c8, uint q, out uint unquantOut) // Reverses the lowest b bits (b in 1..4) of a power-of-2 weight code for direct MSB-descending packing. uint AstcReverseSmallBits(uint v, uint b) { - if (b == 2u) return ((v & 1u) << 1u) | (v >> 1u); - if (b == 3u) return ((v & 1u) << 2u) | (v & 2u) | (v >> 2u); - if (b == 4u) return ((v & 1u) << 3u) | ((v & 2u) << 1u) | ((v & 4u) >> 1u) | (v >> 3u); - return v & 1u; + return (b == 2u) ? (((v & 1u) << 1u) | (v >> 1u)) + : (b == 3u) ? (((v & 1u) << 2u) | (v & 2u) | (v >> 2u)) + : (b == 4u) ? (((v & 1u) << 3u) | ((v & 2u) << 1u) | ((v & 4u) >> 1u) | (v >> 3u)) + : (v & 1u); } // Compact 56-entry packed LUT for all non-power-of-2 ASTC weight quant modes (QUANT_3/5/6/10/12/20). @@ -398,35 +423,40 @@ uint AstcQuantizeWeight(float w01, uint q, out uint unquantOut) AstcGetQuantParams(q, rangeVal, b, t, qu); uint maxK = rangeVal - 1u; + uint resultCode; if (t == 0u && qu == 0u) { - return AstcQuantizePow2Weight(w01, maxK, b, unquantOut); + resultCode = AstcQuantizePow2Weight(w01, maxK, b, unquantOut); } - float sw = saturate(w01); - uint kEst = min((uint)floor(sw * (float)maxK + 0.5), maxK); - - // O(1) exact LUT path for all trit/quint weight ranges (QUANT_3/5/6/10/12/20): - // because unquantized weights are strictly monotonic, checking kEst and 1 neighbor in the - // direction of (w64 - uqEst) guarantees the exact nearest BISE code with zero loops. - uint lutBase = (q == ASTC_QUANT_3) ? 0u - : (q == ASTC_QUANT_5) ? 3u - : (q == ASTC_QUANT_6) ? 8u - : (q == ASTC_QUANT_10) ? 14u - : (q == ASTC_QUANT_12) ? 24u - : 36u; - - uint e0 = kAstcWeightLut[lutBase + kEst]; - uint uq0 = e0 >> 8u; - float w64 = sw * 64.0; - float diff0 = w64 - (float)uq0; - - uint kNbr = AstcWeightNeighbor(kEst, maxK, diff0); - uint e1 = kAstcWeightLut[lutBase + kNbr]; - uint uq1 = e1 >> 8u; - - uint bestEntry = (abs(w64 - (float)uq1) < abs(diff0)) ? e1 : e0; - unquantOut = bestEntry >> 8u; - return bestEntry & 0xFFu; + else + { + float sw = saturate(w01); + uint kEst = min((uint)floor(sw * (float)maxK + 0.5), maxK); + + // O(1) exact LUT path for all trit/quint weight ranges (QUANT_3/5/6/10/12/20): + // because unquantized weights are strictly monotonic, checking kEst and 1 neighbor in the + // direction of (w64 - uqEst) guarantees the exact nearest BISE code with zero loops. + uint lutBase = (q == ASTC_QUANT_3) ? 0u + : (q == ASTC_QUANT_5) ? 3u + : (q == ASTC_QUANT_6) ? 8u + : (q == ASTC_QUANT_10) ? 14u + : (q == ASTC_QUANT_12) ? 24u + : 36u; + + uint e0 = kAstcWeightLut[lutBase + kEst]; + uint uq0 = e0 >> 8u; + float w64 = sw * 64.0; + float diff0 = w64 - (float)uq0; + + uint kNbr = AstcWeightNeighbor(kEst, maxK, diff0); + uint e1 = kAstcWeightLut[lutBase + kNbr]; + uint uq1 = e1 >> 8u; + + uint bestEntry = (abs(w64 - (float)uq1) < abs(diff0)) ? e1 : e0; + unquantOut = bestEntry >> 8u; + resultCode = bestEntry & 0xFFu; + } + return resultCode; } // Writes up to 24 bits into a 128-bit block (little-endian uint4), branchless. @@ -469,7 +499,13 @@ uint4 AstcReverse128(uint4 v) } // Shared per-thread BISE symbol scratch buffer (eliminates array-by-value copies in FXC/HLSLcc/DXC). -static uint g_AstcBiseVals[64]; +// Max weights per block: 4x4 dual-plane = 32; 5x5+ dual-plane = 50 (all 1P grids <= 36). +#if (BLOCK_X == 4) && (BLOCK_Y == 4) +#define ASTC_MAX_BISE_VALS 32 +#else +#define ASTC_MAX_BISE_VALS 50 +#endif +static uint g_AstcBiseVals[ASTC_MAX_BISE_VALS]; // Encodes up to 64 BISE symbols from g_AstcBiseVals[0..count-1] into a 128-bit block starting at startBit. void AstcEncodeBise(inout uint4 dst, uint startBit, uint q, uint count) diff --git a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuColor.hlsl b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuColor.hlsl index 068b219..39476e6 100644 --- a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuColor.hlsl +++ b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuColor.hlsl @@ -66,22 +66,22 @@ uint AstcFloatToHalfU16(float x) // ASTC HDR Endpoint Quantization (CEM 11 / CEM 14 / CEM 15) // ============================================================================ -// Shared per-thread scratch arrays for HDR endpoint quantization (avoids per-call-site indexableTemp explosion in FXC). -static uint g_AstcHdrCodes[6]; -static uint g_AstcHdrRawV[6]; -static uint g_AstcHdrCandCodes[6]; -static uint g_AstcHdrUqV[6]; +#if defined(ASTC_HDR_VARIANT) +// Shared per-thread scratch array for HDR endpoint quantization (x=Codes, y=RawV, z=CandCodes, w=UqV). +static uint4 g_AstcHdrWork[6]; // Exact Khronos ASTC Spec CEM 11 (HDR RGB Direct) hardware unpacker. -// Reads 6 unquantized 8-bit endpoint values from g_AstcHdrUqV[0..5], returns hardware-reconstructed LNS endpoints in [0, 65535]. +// Reads 6 unquantized 8-bit endpoint values from g_AstcHdrWork[0..5].w, returns hardware-reconstructed LNS endpoints in [0, 65535]. void AstcUnpackHdrRgb(out float3 outE0Lns, out float3 outE1Lns) { - uint v0 = g_AstcHdrUqV[0]; - uint v1 = g_AstcHdrUqV[1]; - uint v2 = g_AstcHdrUqV[2]; - uint v3 = g_AstcHdrUqV[3]; - uint v4 = g_AstcHdrUqV[4]; - uint v5 = g_AstcHdrUqV[5]; + outE0Lns = 0.0; + outE1Lns = 0.0; + uint v0 = g_AstcHdrWork[0].w; + uint v1 = g_AstcHdrWork[1].w; + uint v2 = g_AstcHdrWork[2].w; + uint v3 = g_AstcHdrWork[3].w; + uint v4 = g_AstcHdrWork[4].w; + uint v5 = g_AstcHdrWork[5].w; uint majcomp = ((v4 & 0x80u) >> 7u) | (((v5 & 0x80u) >> 7u) << 1u); if (majcomp == 3u) @@ -162,7 +162,7 @@ void AstcUnpackHdrRgb(out float3 outE0Lns, out float3 outE1Lns) outE1Lns = float3((float)(r1 << 4), (float)(g1 << 4), (float)(bl1 << 4)); } -// Packs CEM 11 delta mode fields into 6 8-bit endpoint values in g_AstcHdrRawV[0..5]. +// Packs CEM 11 delta mode fields into 6 8-bit endpoint values in g_AstcHdrWork[0..5].y. void AstcPackHdrRgbDeltaMode(uint majcomp, uint modeval, uint a, uint b0, uint b1, uint c, uint d0, uint d1) { uint v0 = a & 0xFFu; @@ -227,11 +227,11 @@ void AstcPackHdrRgbDeltaMode(uint majcomp, uint modeval, uint a, uint b0, uint b v4 |= (bit2 << 6u) | (bit4 << 5u); v5 |= (bit3 << 6u) | (bit5 << 5u); - g_AstcHdrRawV[0] = v0; g_AstcHdrRawV[1] = v1; g_AstcHdrRawV[2] = v2; - g_AstcHdrRawV[3] = v3; g_AstcHdrRawV[4] = v4; g_AstcHdrRawV[5] = v5; + g_AstcHdrWork[0].y = v0; g_AstcHdrWork[1].y = v1; g_AstcHdrWork[2].y = v2; + g_AstcHdrWork[3].y = v3; g_AstcHdrWork[4].y = v4; g_AstcHdrWork[5].y = v5; } -// Encodes HDR RGB endpoints e0, e1 (in LNS [0, 65535]) into g_AstcHdrCodes[0..5] +// Encodes HDR RGB endpoints e0, e1 (in LNS [0, 65535]) into g_AstcHdrWork[0..5].x // using CEM_HDR_RGB_DIRECT (11): tries high-precision majcomp 0/1/2 delta modes (up to 12-bit) // and compares exact hardware-reconstructed LNS MSE against majcomp == 3 direct mode. void AstcQuantizeHdrRgbEndpoints( @@ -242,22 +242,22 @@ void AstcQuantizeHdrRgbEndpoints( out float3 uq1Lns) { // Baseline: majcomp == 3 direct mode (always valid) - g_AstcHdrRawV[0] = (uint)clamp(floor(e0Lns.r / 256.0 + 0.5), 0.0, 255.0); - g_AstcHdrRawV[1] = (uint)clamp(floor(e1Lns.r / 256.0 + 0.5), 0.0, 255.0); - g_AstcHdrRawV[2] = (uint)clamp(floor(e0Lns.g / 256.0 + 0.5), 0.0, 255.0); - g_AstcHdrRawV[3] = (uint)clamp(floor(e1Lns.g / 256.0 + 0.5), 0.0, 255.0); - g_AstcHdrRawV[4] = 128u + (uint)clamp(floor(e0Lns.b / 512.0 + 0.5), 0.0, 127.0); - g_AstcHdrRawV[5] = 128u + (uint)clamp(floor(e1Lns.b / 512.0 + 0.5), 0.0, 127.0); + g_AstcHdrWork[0].y = (uint)clamp(floor(e0Lns.r / 256.0 + 0.5), 0.0, 255.0); + g_AstcHdrWork[1].y = (uint)clamp(floor(e1Lns.r / 256.0 + 0.5), 0.0, 255.0); + g_AstcHdrWork[2].y = (uint)clamp(floor(e0Lns.g / 256.0 + 0.5), 0.0, 255.0); + g_AstcHdrWork[3].y = (uint)clamp(floor(e1Lns.g / 256.0 + 0.5), 0.0, 255.0); + g_AstcHdrWork[4].y = 128u + (uint)clamp(floor(e0Lns.b / 512.0 + 0.5), 0.0, 127.0); + g_AstcHdrWork[5].y = 128u + (uint)clamp(floor(e1Lns.b / 512.0 + 0.5), 0.0, 127.0); [loop] for (uint bi = 0u; bi < 6u; ++bi) { uint uqTmp; - g_AstcHdrCodes[bi] = AstcQuantizeColorRange(g_AstcHdrRawV[bi], epQuant, bi >= 4u, uqTmp); - g_AstcHdrUqV[bi] = uqTmp; + g_AstcHdrWork[bi].x = AstcQuantizeColorRange(g_AstcHdrWork[bi].y, epQuant, bi >= 4u, uqTmp); + g_AstcHdrWork[bi].w = uqTmp; } - uq0Lns = float3((float)(g_AstcHdrUqV[0] << 8u), (float)(g_AstcHdrUqV[2] << 8u), (float)((g_AstcHdrUqV[4] & 0x7Fu) << 9u)); - uq1Lns = float3((float)(g_AstcHdrUqV[1] << 8u), (float)(g_AstcHdrUqV[3] << 8u), (float)((g_AstcHdrUqV[5] & 0x7Fu) << 9u)); + uq0Lns = float3((float)(g_AstcHdrWork[0].w << 8u), (float)(g_AstcHdrWork[2].w << 8u), (float)((g_AstcHdrWork[4].w & 0x7Fu) << 9u)); + uq1Lns = float3((float)(g_AstcHdrWork[1].w << 8u), (float)(g_AstcHdrWork[3].w << 8u), (float)((g_AstcHdrWork[5].w & 0x7Fu) << 9u)); float3 d0 = uq0Lns - e0Lns; float3 d1 = uq1Lns - e1Lns; @@ -335,8 +335,8 @@ void AstcQuantizeHdrRgbEndpoints( [loop] for (uint ci = 0u; ci < 6u; ++ci) { uint uqTmp; - g_AstcHdrCandCodes[ci] = AstcQuantizeColor(g_AstcHdrRawV[ci], epQuant, uqTmp); - g_AstcHdrUqV[ci] = uqTmp; + g_AstcHdrWork[ci].z = AstcQuantizeColor(g_AstcHdrWork[ci].y, epQuant, uqTmp); + g_AstcHdrWork[ci].w = uqTmp; } float3 cand0, cand1; @@ -346,7 +346,7 @@ void AstcQuantizeHdrRgbEndpoints( float candErr = dot(cd0, cd0) + dot(cd1, cd1); if (candErr < bestErr) { - [loop] for (uint cj = 0u; cj < 6u; ++cj) g_AstcHdrCodes[cj] = g_AstcHdrCandCodes[cj]; + [loop] for (uint cj = 0u; cj < 6u; ++cj) g_AstcHdrWork[cj].x = g_AstcHdrWork[cj].z; uq0Lns = cand0; uq1Lns = cand1; } @@ -354,6 +354,7 @@ void AstcQuantizeHdrRgbEndpoints( } } } +#endif // ASTC_HDR_VARIANT // Encodes HDR Alpha endpoints a0, a1 (in LNS [0, 65535]) for CEM_HDR_RGBA_DIRECT (15) // using direct modeval == 3 (bit 7 == 1 on both endpoints). @@ -529,26 +530,26 @@ void AstcComputeInfillTap( } // Evaluates the exact ASTC hardware interpolated weight [0, 64] at texel (x, y) -// from a packed unquantized grid array gridUq[36] (plane 0 in bits 0..7, plane 1 in bits 8..15). -#define ASTC_EVAL_INFILL_SHIFT(gridUq, shift, tx, ty, bx, by, gw, gh, outWeight64) \ - if ((gw) == (bx) && (gh) == (by)) \ - { \ - (outWeight64) = ((gridUq)[(ty) * (gw) + (tx)] >> (shift)) & 0xFFu; \ - } \ - else \ - { \ - uint _js, _jt; \ - uint4 _tw; \ - AstcComputeInfillTap((tx), (ty), (bx), (by), (gw), (gh), _js, _jt, _tw); \ - uint _idx00 = _jt * (gw) + _js; \ - uint _p00 = ((gridUq)[_idx00] >> (shift)) & 0xFFu; \ - uint _p10 = (_tw.y > 0u) ? (((gridUq)[_idx00 + 1u] >> (shift)) & 0xFFu) : 0u; \ - uint _p01 = (_tw.z > 0u) ? (((gridUq)[_idx00 + (gw)] >> (shift)) & 0xFFu) : 0u; \ - uint _p11 = (_tw.w > 0u) ? (((gridUq)[_idx00 + (gw) + 1u] >> (shift)) & 0xFFu) : 0u; \ - (outWeight64) = (_p00 * _tw.x + _p10 * _tw.y + _p01 * _tw.z + _p11 * _tw.w + 8u) >> 4u; \ +// from the packed .z component of g_AstcGrid (plane 0 in bits 0..7, plane 1 in bits 8..15). +#define ASTC_EVAL_INFILL_SHIFT(gridArr, shift, tx, ty, bx, by, gw, gh, outWeight64) \ + if ((gw) == (bx) && (gh) == (by)) \ + { \ + (outWeight64) = ((gridArr)[(ty) * (gw) + (tx)].z >> (shift)) & 0xFFu; \ + } \ + else \ + { \ + uint _js, _jt; \ + uint4 _tw; \ + AstcComputeInfillTap((tx), (ty), (bx), (by), (gw), (gh), _js, _jt, _tw); \ + uint _idx00 = _jt * (gw) + _js; \ + uint _p00 = ((gridArr)[_idx00].z >> (shift)) & 0xFFu; \ + uint _p10 = (_tw.y > 0u) ? (((gridArr)[_idx00 + 1u].z >> (shift)) & 0xFFu) : 0u; \ + uint _p01 = (_tw.z > 0u) ? (((gridArr)[_idx00 + (gw)].z >> (shift)) & 0xFFu) : 0u; \ + uint _p11 = (_tw.w > 0u) ? (((gridArr)[_idx00 + (gw) + 1u].z >> (shift)) & 0xFFu) : 0u; \ + (outWeight64) = (_p00 * _tw.x + _p10 * _tw.y + _p01 * _tw.z + _p11 * _tw.w + 8u) >> 4u; \ } -#define ASTC_EVAL_INFILL(gridUq, tx, ty, bx, by, gw, gh, outWeight64) \ - ASTC_EVAL_INFILL_SHIFT(gridUq, 0u, tx, ty, bx, by, gw, gh, outWeight64) +#define ASTC_EVAL_INFILL(gridArr, tx, ty, bx, by, gw, gh, outWeight64) \ + ASTC_EVAL_INFILL_SHIFT(gridArr, 0u, tx, ty, bx, by, gw, gh, outWeight64) #endif // ASTC_GPU_COLOR_HLSL diff --git a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl index 646db76..8990a3c 100644 --- a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl +++ b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl @@ -59,16 +59,18 @@ struct AstcCandidate #define ASTC_MAX_EP_CODES 8 #endif -// Shared per-thread scratch arrays: eliminates per-call-site local indexableTemp arrays -// and array-by-value copies across functions (prevents FXC/HLSLcc/DXC compile timeouts). -static float g_AstcGrid0[ASTC_MAX_GRID]; -static float g_AstcGrid1[ASTC_MAX_GRID]; -#if !defined(ASTC_FAST_ONLY) -// Plane 0 unquantized weight [0..64] in bits 0..7; Plane 1 in bits 8..15. -static uint g_AstcGridUq[ASTC_MAX_GRID]; -static uint g_AstcBestEpCodes[ASTC_MAX_EP_CODES]; -#endif +// Shared per-thread scratch arrays: packed into vector arrays to minimize FXC/DXC indexableTemp slots (eliminates X4714). +// g_AstcGrid: .x = asuint(plane0 float), .y = asuint(plane1 float), .z = packed unquantized uq0 | (uq1 << 8). +#if defined(ASTC_FAST_ONLY) +static uint2 g_AstcGrid[ASTC_MAX_GRID]; static uint g_AstcEpCodes[ASTC_MAX_EP_CODES]; +#define ASTC_EP_CUR(i) g_AstcEpCodes[(i)] +#else +static uint3 g_AstcGrid[ASTC_MAX_GRID]; +static uint2 g_AstcEpCodes[ASTC_MAX_EP_CODES]; +#define ASTC_EP_CUR(i) g_AstcEpCodes[(i)].x +#define ASTC_EP_BEST(i) g_AstcEpCodes[(i)].y +#endif // On-chip per-thread working-pixel cache: // - LDR variants pack RGBA8 into a single uint (64 bytes for 4x4, 576 bytes for 12x12). @@ -142,13 +144,13 @@ float4 AstcClampWorkingColor(float4 c, uint effMode) uint AstcGetCemForEffMode(uint effMode) { #if defined(ASTC_HDR_VARIANT) - if (effMode == ASTC_EFF_HDR_RGB) return ASTC_CEM_HDR_RGB_DIRECT; - if (effMode == ASTC_EFF_HDR_RGB_LDR_ALPHA) return ASTC_CEM_HDR_RGB_LDR_ALPHA; - return ASTC_CEM_HDR_RGBA_DIRECT; + return (effMode == ASTC_EFF_HDR_RGB) ? ASTC_CEM_HDR_RGB_DIRECT + : (effMode == ASTC_EFF_HDR_RGB_LDR_ALPHA) ? ASTC_CEM_HDR_RGB_LDR_ALPHA + : ASTC_CEM_HDR_RGBA_DIRECT; #else - if (effMode == ASTC_EFF_LDR_NORMAL) return ASTC_CEM_LDR_LUM_ALPHA_DIRECT; - if (effMode == ASTC_EFF_LDR_RGB) return ASTC_CEM_LDR_RGB_DIRECT; - return ASTC_CEM_LDR_RGBA_DIRECT; + return (effMode == ASTC_EFF_LDR_NORMAL) ? ASTC_CEM_LDR_LUM_ALPHA_DIRECT + : (effMode == ASTC_EFF_LDR_RGB) ? ASTC_CEM_LDR_RGB_DIRECT + : ASTC_CEM_LDR_RGBA_DIRECT; #endif } @@ -157,18 +159,18 @@ uint AstcGetEpCountForEffMode(uint effMode) #if defined(ASTC_HDR_VARIANT) return (effMode == ASTC_EFF_HDR_RGB) ? 6u : 8u; #else - if (effMode == ASTC_EFF_LDR_NORMAL) return 4u; - if (effMode == ASTC_EFF_LDR_RGB) return 6u; - return 8u; + return (effMode == ASTC_EFF_LDR_NORMAL) ? 4u + : (effMode == ASTC_EFF_LDR_RGB) ? 6u + : 8u; #endif } float AstcGetChannel(float4 v, uint ch) { - if (ch == 0u) return v.r; - if (ch == 1u) return v.g; - if (ch == 2u) return v.b; - return v.a; + return (ch == 0u) ? v.r + : (ch == 1u) ? v.g + : (ch == 2u) ? v.b + : v.a; } float4 AstcSetChannel(float4 v, uint ch, float val) @@ -269,7 +271,7 @@ void AstcQuantizeEndpoints( ? ((ch == 0u) ? q0.x : ((ch == 1u) ? q0.y : ((ch == 2u) ? q0.z : q0.w))) : ((ch == 0u) ? q1.x : ((ch == 1u) ? q1.y : ((ch == 2u) ? q1.z : q1.w))); uint uqTmp; - g_AstcEpCodes[codeOffset + i] = AstcQuantizeColor(val, epQuant, uqTmp); + ASTC_EP_CUR(codeOffset + i) = AstcQuantizeColor(val, epQuant, uqTmp); if ((i & 1u) == 0u) { if (ch == 0u) u0.x = uqTmp; else if (ch == 1u) u0.y = uqTmp; else if (ch == 2u) u0.z = uqTmp; else u0.w = uqTmp; @@ -288,9 +290,9 @@ void AstcQuantizeEndpoints( uint4 tu = u0; u0 = u1; u1 = tu; [loop] for (uint s = 0u; s < cnt; s += 2u) { - uint tc = g_AstcEpCodes[codeOffset + s]; - g_AstcEpCodes[codeOffset + s] = g_AstcEpCodes[codeOffset + s + 1u]; - g_AstcEpCodes[codeOffset + s + 1u] = tc; + uint tc = ASTC_EP_CUR(codeOffset + s); + ASTC_EP_CUR(codeOffset + s) = ASTC_EP_CUR(codeOffset + s + 1u); + ASTC_EP_CUR(codeOffset + s + 1u) = tc; } } @@ -311,7 +313,7 @@ void AstcQuantizeEndpoints( AstcQuantizeHdrRgbEndpoints(e0.rgb, e1.rgb, epQuant, uqRgb0, uqRgb1); [loop] for (uint hi = 0u; hi < 6u; ++hi) { - g_AstcEpCodes[codeOffset + hi] = g_AstcHdrCodes[hi]; + ASTC_EP_CUR(codeOffset + hi) = g_AstcHdrWork[hi].x; } uqE0 = float4(uqRgb0, 65535.0); @@ -330,7 +332,7 @@ void AstcQuantizeEndpoints( [loop] for (uint ai = 0u; ai < 2u; ++ai) { uint uqTmp; - g_AstcEpCodes[codeOffset + 6u + ai] = AstcQuantizeColorRange((ai == 0u) ? aIn0 : aIn1, epQuant, isHdrA, uqTmp); + ASTC_EP_CUR(codeOffset + 6u + ai) = AstcQuantizeColorRange((ai == 0u) ? aIn0 : aIn1, epQuant, isHdrA, uqTmp); if (ai == 0u) uqA0 = uqTmp; else uqA1 = uqTmp; } uqE0.a = isHdrA ? (float)((uqA0 & 0x7Fu) << 9u) : (float)(uqA0 * 257u); @@ -355,6 +357,8 @@ void AstcFitSubsetEndpoints( out float4 outE0, out float4 outE1) { + outE0 = 0.0; + outE1 = 0.0; float4 mask = AstcChannelMaskEx(effMode, excludedChannel); #if ASTC_ENABLE_2P bool smallBlock = (BLOCK_X * BLOCK_Y) < 32u; @@ -480,7 +484,10 @@ void AstcFitSubsetEndpoints( #if !defined(ASTC_FAST_ONLY) // Optional least-squares endpoint refinement with separate RGB (imp) and Alpha (1.0) normal equations - [loop] for (uint iter = 0u; iter < refineIters; ++iter) +#if !defined(ASTC_NO_2P) + [loop] +#endif + for (uint iter = 0u; iter < refineIters; ++iter) { float4 dir = (e1 - e0) * mask; float dirLenSq = dot(dir, dir); @@ -611,7 +618,7 @@ void AstcComputeWeightGrid1P( { t = saturate(dot(p, scaledDir) + projBias); } - g_AstcGrid0[rowOff + x] = t; + g_AstcGrid[rowOff + x].x = asuint(t); } } return; @@ -658,7 +665,7 @@ void AstcComputeWeightGrid1P( den += fw; } } - g_AstcGrid0[gRow + gx] = (den > 1e-6) ? saturate(num / den) : 0.0; + g_AstcGrid[gRow + gx].x = asuint((den > 1e-6) ? saturate(num / den) : 0.0); } } } @@ -690,7 +697,7 @@ void AstcComputePlane1WeightGrid( float imp; float4 p = AstcFetchWorkingPixel(baseX, baseY, x, y, effMode, imp); float pv = AstcGetChannel(p, plane1Channel); - g_AstcGrid1[rowOff + x] = saturate((pv - c0) * invDelta); + g_AstcGrid[rowOff + x].y = asuint(saturate((pv - c0) * invDelta)); } } return; @@ -726,7 +733,7 @@ void AstcComputePlane1WeightGrid( den += fw; } } - g_AstcGrid1[gRow + gx] = (den > 1e-6) ? saturate(num / den) : 0.0; + g_AstcGrid[gRow + gx].y = asuint((den > 1e-6) ? saturate(num / den) : 0.0); } } } @@ -748,8 +755,8 @@ void AstcComputeWeightGrid2P( float4 dir1 = (e1_p1 - e0_p1) * mask; float lenSq0 = dot(dir0, dir0); float lenSq1 = dot(dir1, dir1); - float4 scaledDir0 = dir0 * ((lenSq0 > 1e-6) ? (1.0 / lenSq0) : 0.0); - float4 scaledDir1 = dir1 * ((lenSq1 > 1e-6) ? (1.0 / lenSq1) : 0.0); + float4 scaledDir0 = dir0 * ((lenSq0 > 1e-6) ? (1.0 / max(lenSq0, 1e-6)) : 0.0); + float4 scaledDir1 = dir1 * ((lenSq1 > 1e-6) ? (1.0 / max(lenSq1, 1e-6)) : 0.0); float projBias0 = -dot(e0_p0, scaledDir0); float projBias1 = -dot(e0_p1, scaledDir1); @@ -781,13 +788,13 @@ void AstcComputeWeightGrid2P( { float4 wDir0 = dir0 * chImp; float denom0 = dot(wDir0, dir0); - t = (denom0 > 1e-6) ? saturate(dot((p - e0_p0) * mask, wDir0) / denom0) : 0.0; + t = (denom0 > 1e-6) ? saturate(dot((p - e0_p0) * mask, wDir0) / max(denom0, 1e-6)) : 0.0; } else { float4 wDir1 = dir1 * chImp; float denom1 = dot(wDir1, dir1); - t = (denom1 > 1e-6) ? saturate(dot((p - e0_p1) * mask, wDir1) / denom1) : 0.0; + t = (denom1 > 1e-6) ? saturate(dot((p - e0_p1) * mask, wDir1) / max(denom1, 1e-6)) : 0.0; } } else @@ -796,7 +803,7 @@ void AstcComputeWeightGrid2P( ? saturate(dot(p, scaledDir0) + projBias0) : saturate(dot(p, scaledDir1) + projBias1); } - g_AstcGrid0[rowOff + x] = t; + g_AstcGrid[rowOff + x].x = asuint(t); } } return; @@ -834,13 +841,13 @@ void AstcComputeWeightGrid2P( { float4 wDir0 = dir0 * chImp; float denom0 = dot(wDir0, dir0); - t = (denom0 > 1e-6) ? saturate(dot((p - e0_p0) * mask, wDir0) / denom0) : 0.0; + t = (denom0 > 1e-6) ? saturate(dot((p - e0_p0) * mask, wDir0) / max(denom0, 1e-6)) : 0.0; } else { float4 wDir1 = dir1 * chImp; float denom1 = dot(wDir1, dir1); - t = (denom1 > 1e-6) ? saturate(dot((p - e0_p1) * mask, wDir1) / denom1) : 0.0; + t = (denom1 > 1e-6) ? saturate(dot((p - e0_p1) * mask, wDir1) / max(denom1, 1e-6)) : 0.0; } } else @@ -853,7 +860,7 @@ void AstcComputeWeightGrid2P( den += tap; } } - g_AstcGrid0[gRow + gx] = (den > 1e-6) ? saturate(num / den) : 0.0; + g_AstcGrid[gRow + gx].x = asuint((den > 1e-6) ? saturate(num / den) : 0.0); } } } @@ -914,18 +921,18 @@ float AstcEvalCandidateError( if (!skipWeightQuant) { uint gridCount = gridW * gridH; + uint planes = isDual ? 2u : 1u; [loop] for (uint k = 0u; k < gridCount; ++k) { - uint uq0; - AstcQuantizeWeight(g_AstcGrid0[k], wQuant, uq0); - uint packedUq = uq0; - if (isDual) + uint packedUq = 0u; + [loop] for (uint pl = 0u; pl < planes; ++pl) { - uint uq1; - AstcQuantizeWeight(g_AstcGrid1[k], wQuant, uq1); - packedUq |= (uq1 << 8u); + float wf = asfloat((pl == 0u) ? g_AstcGrid[k].x : g_AstcGrid[k].y); + uint uqP; + AstcQuantizeWeight(wf, wQuant, uqP); + packedUq |= (uqP << (pl * 8u)); } - g_AstcGridUq[k] = packedUq; + g_AstcGrid[k].z = packedUq; } } @@ -941,7 +948,7 @@ float AstcEvalCandidateError( float imp; float4 p = AstcFetchWorkingPixel(baseX, baseY, x, y, effMode, imp); uint w0; - ASTC_EVAL_INFILL(g_AstcGridUq, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w0); + ASTC_EVAL_INFILL(g_AstcGrid, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w0); float4 recon; #if ASTC_ENABLE_2P @@ -961,7 +968,7 @@ float AstcEvalCandidateError( if (isDual) { uint w1; - ASTC_EVAL_INFILL_SHIFT(g_AstcGridUq, 8u, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w1); + ASTC_EVAL_INFILL_SHIFT(g_AstcGrid, 8u, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w1); uint effW1 = applySwap0 ? (64u - w1) : w1; float p1Val = lerp(AstcGetChannel(uq0_p0, ccs), AstcGetChannel(uq1_p0, ccs), (float)effW1 * (1.0 / 64.0)); recon = AstcSetChannel(recon, ccs, p1Val); @@ -1135,7 +1142,7 @@ bool AstcSolveRefitEndpoints( float4 p = AstcFetchWorkingPixel(baseX, baseY, x, y, effMode, imp); float4 chImp = AstcGetChannelImp(effMode, imp); uint w0_64; - ASTC_EVAL_INFILL(g_AstcGridUq, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w0_64); + ASTC_EVAL_INFILL(g_AstcGrid, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w0_64); float w0 = (float)w0_64 * (1.0 / 64.0); float omw0 = 1.0 - w0; @@ -1144,7 +1151,7 @@ bool AstcSolveRefitEndpoints( if (isDualPlane) { uint w1_64; - ASTC_EVAL_INFILL_SHIFT(g_AstcGridUq, 8u, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w1_64); + ASTC_EVAL_INFILL_SHIFT(g_AstcGrid, 8u, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w1_64); w1 = (float)w1_64 * (1.0 / 64.0); recon = AstcSetChannel(recon, ccs, lerp(AstcGetChannel(curUqE0, ccs), AstcGetChannel(curUqE1, ccs), w1)); } @@ -1305,19 +1312,14 @@ uint4 AstcAssembleBlock(uint baseX, uint baseY, uint effMode, AstcCandidate c) AstcComputePlane1WeightGrid(baseX, baseY, effMode, c.dualPlaneChannel, AstcGetChannel(uqE0, c.dualPlaneChannel), AstcGetChannel(uqE1, c.dualPlaneChannel), c.gridW, c.gridH); } + uint planes = isDual ? 2u : 1u; [loop] for (uint k = 0u; k < gridCount; ++k) { - uint uq0; - uint code0 = AstcQuantizeWeight(g_AstcGrid0[k], c.wQuant, uq0); - if (isDual) + [loop] for (uint pl = 0u; pl < planes; ++pl) { - uint uq1; - g_AstcBiseVals[2u * k] = code0; - g_AstcBiseVals[2u * k + 1u] = AstcQuantizeWeight(g_AstcGrid1[k], c.wQuant, uq1); - } - else - { - g_AstcBiseVals[k] = code0; + float wf = asfloat((pl == 0u) ? g_AstcGrid[k].x : g_AstcGrid[k].y); + uint dummyUq; + g_AstcBiseVals[isDual ? (2u * k + pl) : k] = AstcQuantizeWeight(wf, c.wQuant, dummyUq); } } if (isDual) totalWeights = gridCount * 2u; @@ -1331,30 +1333,44 @@ uint4 AstcAssembleBlock(uint baseX, uint baseY, uint effMode, AstcCandidate c) uint revW = AstcReverseSmallBits(g_AstcBiseVals[wi], wBits2); AstcWriteBits128(block, 128u - (wi + 1u) * wBits2, wBits2, revW); } - } - else - { - uint4 wBuf = uint4(0u, 0u, 0u, 0u); - AstcEncodeBise(wBuf, 0u, c.wQuant, totalWeights); - block = AstcReverse128(wBuf); + weightsPackedDirect = true; } } - AstcWriteBits128(block, 0u, 11u, c.blockMode); - AstcWriteBits128(block, 11u, 2u, 0u); - AstcWriteBits128(block, 13u, 4u, cem); - if (c.isDualPlane != 0u) + [loop] for (uint bisePass = 0u; bisePass < 2u; ++bisePass) { - uint weightBits = AstcGetBiseBitCount(totalWeights, c.wQuant); - AstcWriteBits128(block, 128u - weightBits - 2u, 2u, c.dualPlaneChannel); + if (bisePass == 0u && weightsPackedDirect) continue; + + uint4 dstBuf = uint4(0u, 0u, 0u, 0u); + uint startBit = 0u; + uint biseQ = c.wQuant; + uint biseCnt = totalWeights; + + if (bisePass == 1u) + { + AstcWriteBits128(block, 0u, 11u, c.blockMode); + AstcWriteBits128(block, 11u, 2u, 0u); + AstcWriteBits128(block, 13u, 4u, cem); + if (c.isDualPlane != 0u) + { + uint weightBits = AstcGetBiseBitCount(totalWeights, c.wQuant); + AstcWriteBits128(block, 128u - weightBits - 2u, 2u, c.dualPlaneChannel); + } + [loop] for (uint ei = 0u; ei < epPerPart; ++ei) g_AstcBiseVals[ei] = ASTC_EP_CUR(ei); + dstBuf = block; + startBit = 17u; + biseQ = c.epQuant; + biseCnt = epPerPart; + } + + AstcEncodeBise(dstBuf, startBit, biseQ, biseCnt); + block = (bisePass == 0u) ? AstcReverse128(dstBuf) : dstBuf; } - [loop] for (uint ei = 0u; ei < epPerPart; ++ei) g_AstcBiseVals[ei] = g_AstcEpCodes[ei]; - AstcEncodeBise(block, 17u, c.epQuant, epPerPart); return block; } #else -// Packs pre-computed weights (g_AstcBiseVals) and endpoints (g_AstcBestEpCodes) into a 128-bit ASTC block. +// Packs pre-computed weights (g_AstcBiseVals) and endpoints (ASTC_EP_BEST) into a 128-bit ASTC block. uint4 AstcPackFinalBlock(uint effMode, AstcCandidate c, uint totalWeights) { uint epPerPart = AstcGetEpCountForEffMode(effMode); @@ -1363,7 +1379,8 @@ uint4 AstcPackFinalBlock(uint effMode, AstcCandidate c, uint totalWeights) uint wRange2, wBits2, wTrits2, wQuints2; AstcGetQuantParams(c.wQuant, wRange2, wBits2, wTrits2, wQuints2); - if (wTrits2 == 0u && wQuints2 == 0u && wBits2 >= 1u && wBits2 <= 4u) + bool directPo2Weights = (wTrits2 == 0u && wQuints2 == 0u && wBits2 >= 1u && wBits2 <= 4u); + if (directPo2Weights) { [loop] for (uint wi = 0u; wi < totalWeights; ++wi) { @@ -1371,40 +1388,52 @@ uint4 AstcPackFinalBlock(uint effMode, AstcCandidate c, uint totalWeights) AstcWriteBits128(block, 128u - (wi + 1u) * wBits2, wBits2, revW); } } - else + + // Single AstcEncodeBise call site: pass 0 packs non-Po2 weights (reversed), pass 1 writes header and packs endpoints. + [loop] for (uint bisePass = 0u; bisePass < 2u; ++bisePass) { - uint4 wBuf = uint4(0u, 0u, 0u, 0u); - AstcEncodeBise(wBuf, 0u, c.wQuant, totalWeights); - block = AstcReverse128(wBuf); - } + if (bisePass == 0u && directPo2Weights) continue; + + uint4 dstBuf = uint4(0u, 0u, 0u, 0u); + uint startBit = 0u; + uint biseQ = c.wQuant; + uint biseCnt = totalWeights; - // Header: BlockMode (11 bits) + PartitionCount-1 (2 bits) - AstcWriteBits128(block, 0u, 11u, c.blockMode); - uint epStartBit = 17u; - uint epTotalCount = epPerPart; + if (bisePass == 1u) + { + // Header: BlockMode (11 bits) + PartitionCount-1 (2 bits) + AstcWriteBits128(block, 0u, 11u, c.blockMode); + startBit = 17u; + biseCnt = epPerPart; + biseQ = c.epQuant; #if ASTC_ENABLE_2P - if (c.isTwoPart != 0u) - { - AstcWriteBits128(block, 11u, 2u, 1u); - AstcWriteBits128(block, 13u, 10u, c.partSeed); - // Matched CEM for both partitions: low 2 bits = 00, upper 4 bits = cem - AstcWriteBits128(block, 23u, 6u, cem << 2u); - epStartBit = 29u; - epTotalCount = epPerPart * 2u; - } - else + if (c.isTwoPart != 0u) + { + AstcWriteBits128(block, 11u, 2u, 1u); + AstcWriteBits128(block, 13u, 10u, c.partSeed); + // Matched CEM for both partitions: low 2 bits = 00, upper 4 bits = cem + AstcWriteBits128(block, 23u, 6u, cem << 2u); + startBit = 29u; + biseCnt = epPerPart * 2u; + } + else #endif - { - AstcWriteBits128(block, 11u, 2u, 0u); - AstcWriteBits128(block, 13u, 4u, cem); - if (c.isDualPlane != 0u) - { - uint weightBits = AstcGetBiseBitCount(totalWeights, c.wQuant); - AstcWriteBits128(block, 128u - weightBits - 2u, 2u, c.dualPlaneChannel); + { + AstcWriteBits128(block, 11u, 2u, 0u); + AstcWriteBits128(block, 13u, 4u, cem); + if (c.isDualPlane != 0u) + { + uint weightBits = AstcGetBiseBitCount(totalWeights, c.wQuant); + AstcWriteBits128(block, 128u - weightBits - 2u, 2u, c.dualPlaneChannel); + } + } + [loop] for (uint ei = 0u; ei < biseCnt; ++ei) g_AstcBiseVals[ei] = ASTC_EP_BEST(ei); + dstBuf = block; } + + AstcEncodeBise(dstBuf, startBit, biseQ, biseCnt); + block = (bisePass == 0u) ? AstcReverse128(dstBuf) : dstBuf; } - [loop] for (uint ei = 0u; ei < epTotalCount; ++ei) g_AstcBiseVals[ei] = g_AstcBestEpCodes[ei]; - AstcEncodeBise(block, epStartBit, c.epQuant, epTotalCount); return block; } @@ -1442,7 +1471,7 @@ void AstcTryCandidate( #else uint epTotal = epPerPart; #endif - [loop] for (uint ei = 0u; ei < epTotal; ++ei) g_AstcBestEpCodes[ei] = g_AstcEpCodes[ei]; + [loop] for (uint ei = 0u; ei < epTotal; ++ei) ASTC_EP_BEST(ei) = ASTC_EP_CUR(ei); if (!isRefitPass) { @@ -1475,24 +1504,15 @@ void AstcTryCandidate( #if ASTC_ENABLE_2P // Curated 16 geometrically diverse ASTC 2-partition seeds (vertical, horizontal, diagonals, corners). +static const uint kAstcCuratedSeeds[16] = +{ + 16u, 28u, 31u, 78u, 17u, 39u, 116u, 72u, + 107u, 43u, 110u, 252u, 61u, 76u, 210u, 137u +}; + uint AstcGetCuratedSeed(uint idx) { - if (idx == 0u) return 16u; - if (idx == 1u) return 28u; - if (idx == 2u) return 31u; - if (idx == 3u) return 78u; - if (idx == 4u) return 17u; - if (idx == 5u) return 39u; - if (idx == 6u) return 116u; - if (idx == 7u) return 72u; - if (idx == 8u) return 107u; - if (idx == 9u) return 43u; - if (idx == 10u) return 110u; - if (idx == 11u) return 252u; - if (idx == 12u) return 61u; - if (idx == 13u) return 76u; - if (idx == 14u) return 210u; - return 137u; + return kAstcCuratedSeeds[idx & 15u]; } // Fast heuristic to select the best 2-partition seed by minimizing per-channel weighted intra-partition variance. @@ -1592,6 +1612,8 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) float4 preMinC = 0.0; float4 preMaxC = 0.0; bool hasPreStats = true; + bool isVoid = false; + uint4 resultBlock = uint4(0u, 0u, 0u, 0u); #if !defined(ASTC_HDR_VARIANT) // ------------------------------------------------------------------------ @@ -1652,7 +1674,8 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) effMode = ASTC_EFF_LDR_NORMAL; if (min8.x == max8.x && min8.w == max8.w) { - return AstcEncodeVoidExtentLDR(uint4(first8.x, first8.x, first8.x, first8.w) * 257u); + resultBlock = AstcEncodeVoidExtentLDR(uint4(first8.x, first8.x, first8.x, first8.w) * 257u); + isVoid = true; } } else if (isRgbOnlyMode || min8.a == 255u) @@ -1661,7 +1684,8 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) effMode = ASTC_EFF_LDR_RGB; if (min8.r == max8.r && min8.g == max8.g && min8.b == max8.b) { - return AstcEncodeVoidExtentLDR(uint4(first8.rgb, 255u) * 257u); + resultBlock = AstcEncodeVoidExtentLDR(uint4(first8.rgb, 255u) * 257u); + isVoid = true; } } else @@ -1669,13 +1693,17 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) effMode = ASTC_EFF_LDR_RGBA; if (min8.r == max8.r && min8.g == max8.g && min8.b == max8.b && min8.a == max8.a) { - return AstcEncodeVoidExtentLDR(first8 * 257u); + resultBlock = AstcEncodeVoidExtentLDR(first8 * 257u); + isVoid = true; } } - preMeanP = sumP / max(sumW, 1e-6); - preMinC = (float4)min8; - preMaxC = (float4)max8; + if (!isVoid) + { + preMeanP = sumP / max(sumW, 1e-6); + preMinC = (float4)min8; + preMaxC = (float4)max8; + } #else // ------------------------------------------------------------------------ // Step 1 + 2 (HDR): Single-pass texture load -> LNS16 cache + Void-Extent + block stats @@ -1739,10 +1767,11 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) AstcFloatToHalfU16(rawFirst.g), AstcFloatToHalfU16(rawFirst.b), (effMode == ASTC_EFF_HDR_RGB) ? 0x3C00u : AstcFloatToHalfU16(rawFirst.a)); - return AstcEncodeVoidExtentHDR(f16); + resultBlock = AstcEncodeVoidExtentHDR(f16); + isVoid = true; } - if (effMode == ASTC_EFF_HDR_RGB_LDR_ALPHA) + if (!isVoid && effMode == ASTC_EFF_HDR_RGB_LDR_ALPHA) { // Scale LDR alpha [0,1] into [0,65535] to match CEM 14 working space. [loop] for (uint cy = 0u; cy < BLOCK_Y; ++cy) @@ -1760,7 +1789,7 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) } hasPreStats = false; } - else + else if (!isVoid) { preMeanP = sumLns * (1.0 / (float)(BLOCK_X * BLOCK_Y)); preMinC = minLns; @@ -1771,6 +1800,8 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) // ------------------------------------------------------------------------ // Step 3: Endpoint Fitting & Mode Selection // ------------------------------------------------------------------------ + if (!isVoid) + { AstcCandidate best; best.blockMode = 0x251u; best.gridW = 4u; @@ -1831,7 +1862,7 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) best.blockMode = 0x0E2u; best.gridW = 5u; best.gridH = 5u; best.wQuant = ASTC_QUANT_4; best.epQuant = ASTC_QUANT_192; #endif } - return AstcAssembleBlock(baseX, baseY, effMode, best); + resultBlock = AstcAssembleBlock(baseX, baseY, effMode, best); #else uint finalTotalWeights = 16u; { @@ -1984,7 +2015,7 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) uint cachedGW = 0u, cachedGH = 0u; [loop] for (uint ci = 0u; ci < candCount; ++ci) { - uint bm, gw, gh, wQ, epQ; + uint bm = 0u, gw = 4u, gh = 4u, wQ = 0u, epQ = 0u; if (isFinalPass) { bm = best.blockMode; gw = best.gridW; gh = best.gridH; wQ = best.wQuant; epQ = best.epQuant; @@ -2157,22 +2188,18 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) { uint gridCount = gw * gh; bool isDualFinal = (effStage == 1u); + uint planes = isDualFinal ? 2u : 1u; [loop] for (uint k = 0u; k < gridCount; ++k) { - uint uq0; - uint code0 = AstcQuantizeWeight(g_AstcGrid0[k], wQ, uq0); - if (isDualFinal) - { - uint uq1; - g_AstcBiseVals[2u * k] = code0; - g_AstcBiseVals[2u * k + 1u] = AstcQuantizeWeight(g_AstcGrid1[k], wQ, uq1); - g_AstcGridUq[k] = uq0 | (uq1 << 8u); - } - else + uint packedUq = 0u; + [loop] for (uint pl = 0u; pl < planes; ++pl) { - g_AstcBiseVals[k] = code0; - g_AstcGridUq[k] = uq0; + float wf = asfloat((pl == 0u) ? g_AstcGrid[k].x : g_AstcGrid[k].y); + uint uqP; + g_AstcBiseVals[isDualFinal ? (2u * k + pl) : k] = AstcQuantizeWeight(wf, wQ, uqP); + packedUq |= (uqP << (pl * 8u)); } + g_AstcGrid[k].z = packedUq; } finalTotalWeights = isDualFinal ? (gridCount * 2u) : gridCount; @@ -2193,8 +2220,10 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) } } } - return AstcPackFinalBlock(effMode, best, finalTotalWeights); + resultBlock = AstcPackFinalBlock(effMode, best, finalTotalWeights); #endif // !ASTC_FAST_ONLY + } + return resultBlock; } #endif // ASTC_GPU_CORE_HLSL diff --git a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuMipGenerator.compute b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuMipGenerator.compute index 737cd94..18307a5 100644 --- a/Assets/ASTC Encoder/Runtime/Resources/AstcGpuMipGenerator.compute +++ b/Assets/ASTC Encoder/Runtime/Resources/AstcGpuMipGenerator.compute @@ -1,5 +1,3 @@ -#pragma editor_sync_compilation - #pragma kernel DownsampleColor #pragma kernel DownsampleNormal From 2512a1305b0286416c11e29efd1ac73852034279 Mon Sep 17 00:00:00 2001 From: FrozenKresnik <80754402+Overflowu@users.noreply.github.com> Date: Tue, 1 Dec 2026 14:25:10 +0000 Subject: [PATCH 7/9] perf(gpu): absorb BISE scratch into g_AstcGrid.w to eliminate X4714 and remaining loop warnings --- .../Runtime/Gpu/Shaders/AstcGpuBise.hlsl | 59 ++++--- .../Runtime/Gpu/Shaders/AstcGpuCore.hlsl | 150 +++++++++--------- 2 files changed, 115 insertions(+), 94 deletions(-) diff --git a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl index 7f24a2b..51dbbb5 100644 --- a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl +++ b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl @@ -498,16 +498,39 @@ uint4 AstcReverse128(uint4 v) AstcReverseBits32(v.x)); } -// Shared per-thread BISE symbol scratch buffer (eliminates array-by-value copies in FXC/HLSLcc/DXC). -// Max weights per block: 4x4 dual-plane = 32; 5x5+ dual-plane = 50 (all 1P grids <= 36). -#if (BLOCK_X == 4) && (BLOCK_Y == 4) -#define ASTC_MAX_BISE_VALS 32 +// Unified per-thread weight grid + BISE symbol scratch array (eliminates separate g_AstcBiseVals and X4714). +// - Non-Fast: uint4[ASTC_MAX_GRID]: .x = asuint(w0), .y = asuint(w1), .z = packed uq0|(uq1<<8), .w = 2x 8-bit BISE symbols. +// - Fast: uint3[ASTC_MAX_GRID]: .x = asuint(w0), .y = asuint(w1), .z = 2x 8-bit BISE symbols. +#if (BLOCK_X <= 5) && (BLOCK_Y <= 5) +#define ASTC_MAX_GRID (BLOCK_X * BLOCK_Y) #else -#define ASTC_MAX_BISE_VALS 50 +#define ASTC_MAX_GRID 36 #endif -static uint g_AstcBiseVals[ASTC_MAX_BISE_VALS]; -// Encodes up to 64 BISE symbols from g_AstcBiseVals[0..count-1] into a 128-bit block starting at startBit. +#if defined(ASTC_FAST_ONLY) +static uint3 g_AstcGrid[ASTC_MAX_GRID]; +#define ASTC_BISE_WORD(slot) g_AstcGrid[(slot)].z +#else +static uint4 g_AstcGrid[ASTC_MAX_GRID]; +#define ASTC_BISE_WORD(slot) g_AstcGrid[(slot)].w +#endif + +uint AstcGetBiseVal(uint idx) +{ + uint w = ASTC_BISE_WORD(idx >> 1u); + return ((idx & 1u) == 0u) ? (w & 0xFFu) : ((w >> 8u) & 0xFFu); +} + +void AstcSetBiseVal(uint idx, uint val) +{ + uint slot = idx >> 1u; + uint w = ASTC_BISE_WORD(slot); + ASTC_BISE_WORD(slot) = ((idx & 1u) == 0u) + ? ((w & 0xFF00u) | (val & 0xFFu)) + : ((w & 0x00FFu) | ((val & 0xFFu) << 8u)); +} + +// Encodes up to 2*ASTC_MAX_GRID BISE symbols packed in g_AstcGrid into a 128-bit block starting at startBit. void AstcEncodeBise(inout uint4 dst, uint startBit, uint q, uint count) { uint rangeVal, b, t, qu; @@ -518,7 +541,7 @@ void AstcEncodeBise(inout uint4 dst, uint startBit, uint q, uint count) { [loop] for (uint i = 0u; i < count; ++i) { - AstcWriteBits128(dst, bitPos, b, g_AstcBiseVals[i]); + AstcWriteBits128(dst, bitPos, b, AstcGetBiseVal(i)); bitPos += b; } } @@ -527,11 +550,11 @@ void AstcEncodeBise(inout uint4 dst, uint startBit, uint q, uint count) uint lowMask = (b > 0u) ? ((1u << b) - 1u) : 0u; [loop] for (uint i = 0u; i < count; i += 5u) { - uint v0 = g_AstcBiseVals[i]; - uint v1 = (i + 1u < count) ? g_AstcBiseVals[i + 1u] : 0u; - uint v2 = (i + 2u < count) ? g_AstcBiseVals[i + 2u] : 0u; - uint v3 = (i + 3u < count) ? g_AstcBiseVals[i + 3u] : 0u; - uint v4 = (i + 4u < count) ? g_AstcBiseVals[i + 4u] : 0u; + uint v0 = AstcGetBiseVal(i); + uint v1 = (i + 1u < count) ? AstcGetBiseVal(i + 1u) : 0u; + uint v2 = (i + 2u < count) ? AstcGetBiseVal(i + 2u) : 0u; + uint v3 = (i + 3u < count) ? AstcGetBiseVal(i + 3u) : 0u; + uint v4 = (i + 4u < count) ? AstcGetBiseVal(i + 4u) : 0u; uint T = AstcPack5Trits(v0 >> b, v1 >> b, v2 >> b, v3 >> b, v4 >> b); @@ -539,7 +562,7 @@ void AstcEncodeBise(inout uint4 dst, uint startBit, uint q, uint count) [loop] for (uint j = 0u; j < 5u; ++j) { if (i + j >= count) break; - uint vj = g_AstcBiseVals[i + j]; + uint vj = AstcGetBiseVal(i + j); uint tBits = (j == 2u || j == 4u) ? 1u : 2u; uint tMask = (tBits == 1u) ? 1u : 3u; AstcWriteBits128(dst, bitPos, b, vj & lowMask); bitPos += b; @@ -553,9 +576,9 @@ void AstcEncodeBise(inout uint4 dst, uint startBit, uint q, uint count) uint lowMask = (b > 0u) ? ((1u << b) - 1u) : 0u; [loop] for (uint i = 0u; i < count; i += 3u) { - uint v0 = g_AstcBiseVals[i]; - uint v1 = (i + 1u < count) ? g_AstcBiseVals[i + 1u] : 0u; - uint v2 = (i + 2u < count) ? g_AstcBiseVals[i + 2u] : 0u; + uint v0 = AstcGetBiseVal(i); + uint v1 = (i + 1u < count) ? AstcGetBiseVal(i + 1u) : 0u; + uint v2 = (i + 2u < count) ? AstcGetBiseVal(i + 2u) : 0u; uint Q = AstcPack3Quints(v0 >> b, v1 >> b, v2 >> b); @@ -563,7 +586,7 @@ void AstcEncodeBise(inout uint4 dst, uint startBit, uint q, uint count) [loop] for (uint j = 0u; j < 3u; ++j) { if (i + j >= count) break; - uint vj = g_AstcBiseVals[i + j]; + uint vj = AstcGetBiseVal(i + j); uint qBits = (j == 0u) ? 3u : 2u; uint qMask = (qBits == 3u) ? 7u : 3u; AstcWriteBits128(dst, bitPos, b, vj & lowMask); bitPos += b; diff --git a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl index 8990a3c..e989c7e 100644 --- a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl +++ b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl @@ -45,12 +45,6 @@ struct AstcCandidate #endif }; -#if (BLOCK_X <= 5) && (BLOCK_Y <= 5) -#define ASTC_MAX_GRID (BLOCK_X * BLOCK_Y) -#else -#define ASTC_MAX_GRID 36 -#endif - #define ASTC_MAX_BLOCK (BLOCK_X * BLOCK_Y) #if ASTC_ENABLE_2P @@ -59,14 +53,11 @@ struct AstcCandidate #define ASTC_MAX_EP_CODES 8 #endif -// Shared per-thread scratch arrays: packed into vector arrays to minimize FXC/DXC indexableTemp slots (eliminates X4714). -// g_AstcGrid: .x = asuint(plane0 float), .y = asuint(plane1 float), .z = packed unquantized uq0 | (uq1 << 8). +// Shared per-thread endpoint code scratch array (g_AstcGrid is declared in AstcGpuBise.hlsl). #if defined(ASTC_FAST_ONLY) -static uint2 g_AstcGrid[ASTC_MAX_GRID]; static uint g_AstcEpCodes[ASTC_MAX_EP_CODES]; #define ASTC_EP_CUR(i) g_AstcEpCodes[(i)] #else -static uint3 g_AstcGrid[ASTC_MAX_GRID]; static uint2 g_AstcEpCodes[ASTC_MAX_EP_CODES]; #define ASTC_EP_CUR(i) g_AstcEpCodes[(i)].x #define ASTC_EP_BEST(i) g_AstcEpCodes[(i)].y @@ -484,80 +475,82 @@ void AstcFitSubsetEndpoints( #if !defined(ASTC_FAST_ONLY) // Optional least-squares endpoint refinement with separate RGB (imp) and Alpha (1.0) normal equations -#if !defined(ASTC_NO_2P) - [loop] +#if defined(ASTC_NO_2P) + if (refineIters > 0u) +#else + [loop] for (uint iter = 0u; iter < refineIters; ++iter) #endif - for (uint iter = 0u; iter < refineIters; ++iter) { float4 dir = (e1 - e0) * mask; float dirLenSq = dot(dir, dir); - if (dirLenSq <= 1e-6) break; - - bool useAlphaW = (effMode == ASTC_EFF_LDR_RGBA && _AstcUseAlphaWeight != 0u); - float4 scaledDir = dir / dirLenSq; - float projBias = -dot(e0, scaledDir); + if (dirLenSq > 1e-6) + { + bool useAlphaW = (effMode == ASTC_EFF_LDR_RGBA && _AstcUseAlphaWeight != 0u); + float4 scaledDir = dir / max(dirLenSq, 1e-6); + float projBias = -dot(e0, scaledDir); - float a00_rgb = 0.0, a01_rgb = 0.0, a11_rgb = 0.0; - float a00_a = 0.0, a01_a = 0.0, a11_a = 0.0; - float4 b0 = 0.0, b1 = 0.0; + float a00_rgb = 0.0, a01_rgb = 0.0, a11_rgb = 0.0; + float a00_a = 0.0, a01_a = 0.0, a11_a = 0.0; + float4 b0 = 0.0, b1 = 0.0; - [loop] for (uint yr = 0u; yr < BLOCK_Y; ++yr) - { - [loop] for (uint xr = 0u; xr < BLOCK_X; ++xr) + [loop] for (uint yr = 0u; yr < BLOCK_Y; ++yr) { + [loop] for (uint xr = 0u; xr < BLOCK_X; ++xr) + { #if ASTC_ENABLE_2P - if (partFilter < 2u && AstcSelectPartition2(partSeed, xr, yr, smallBlock) != partFilter) - continue; + if (partFilter < 2u && AstcSelectPartition2(partSeed, xr, yr, smallBlock) != partFilter) + continue; #endif - float imp; - float4 p = AstcFetchWorkingPixel(baseX, baseY, xr, yr, effMode, imp); - float4 chImp = AstcGetChannelImp(effMode, imp); - float w; - if (useAlphaW) - { - float4 wDir = dir * chImp; - float denom = dot(wDir, dir); - w = (denom > 1e-6) ? saturate(dot((p - e0) * mask, wDir) / denom) : 0.0; - } - else - { - w = saturate(dot(p, scaledDir) + projBias); - } - float omw = 1.0 - w; + float imp; + float4 p = AstcFetchWorkingPixel(baseX, baseY, xr, yr, effMode, imp); + float4 chImp = AstcGetChannelImp(effMode, imp); + float w; + if (useAlphaW) + { + float4 wDir = dir * chImp; + float denom = dot(wDir, dir); + w = (denom > 1e-6) ? saturate(dot((p - e0) * mask, wDir) / max(denom, 1e-6)) : 0.0; + } + else + { + w = saturate(dot(p, scaledDir) + projBias); + } + float omw = 1.0 - w; - float wRgb = chImp.x; - a00_rgb += wRgb * omw * omw; - a01_rgb += wRgb * w * omw; - a11_rgb += wRgb * w * w; + float wRgb = chImp.x; + a00_rgb += wRgb * omw * omw; + a01_rgb += wRgb * w * omw; + a11_rgb += wRgb * w * w; - a00_a += omw * omw; - a01_a += w * omw; - a11_a += w * w; + a00_a += omw * omw; + a01_a += w * omw; + a11_a += w * w; - b0 += p * (chImp * omw); - b1 += p * (chImp * w); + b0 += p * (chImp * omw); + b1 += p * (chImp * w); + } } - } - float detRgb = a00_rgb * a11_rgb - a01_rgb * a01_rgb; - if (detRgb > 1e-4) - { - float invDetRgb = 1.0 / detRgb; - float3 newE0Rgb = (a11_rgb * b0.rgb - a01_rgb * b1.rgb) * invDetRgb; - float3 newE1Rgb = (a00_rgb * b1.rgb - a01_rgb * b0.rgb) * invDetRgb; - e0.rgb = lerp(e0.rgb, AstcClampWorkingColor(float4(newE0Rgb, 0.0), effMode).rgb, mask.rgb); - e1.rgb = lerp(e1.rgb, AstcClampWorkingColor(float4(newE1Rgb, 0.0), effMode).rgb, mask.rgb); - } + float detRgb = a00_rgb * a11_rgb - a01_rgb * a01_rgb; + if (detRgb > 1e-4) + { + float invDetRgb = 1.0 / detRgb; + float3 newE0Rgb = (a11_rgb * b0.rgb - a01_rgb * b1.rgb) * invDetRgb; + float3 newE1Rgb = (a00_rgb * b1.rgb - a01_rgb * b0.rgb) * invDetRgb; + e0.rgb = lerp(e0.rgb, AstcClampWorkingColor(float4(newE0Rgb, 0.0), effMode).rgb, mask.rgb); + e1.rgb = lerp(e1.rgb, AstcClampWorkingColor(float4(newE1Rgb, 0.0), effMode).rgb, mask.rgb); + } - float detA = a00_a * a11_a - a01_a * a01_a; - if (mask.a > 0.0 && detA > 1e-4) - { - float invDetA = 1.0 / detA; - float newE0A = (a11_a * b0.a - a01_a * b1.a) * invDetA; - float newE1A = (a00_a * b1.a - a01_a * b0.a) * invDetA; - e0.a = AstcClampWorkingColor(float4(0.0, 0.0, 0.0, newE0A), effMode).a; - e1.a = AstcClampWorkingColor(float4(0.0, 0.0, 0.0, newE1A), effMode).a; + float detA = a00_a * a11_a - a01_a * a01_a; + if (mask.a > 0.0 && detA > 1e-4) + { + float invDetA = 1.0 / detA; + float newE0A = (a11_a * b0.a - a01_a * b1.a) * invDetA; + float newE1A = (a00_a * b1.a - a01_a * b0.a) * invDetA; + e0.a = AstcClampWorkingColor(float4(0.0, 0.0, 0.0, newE0A), effMode).a; + e1.a = AstcClampWorkingColor(float4(0.0, 0.0, 0.0, newE1A), effMode).a; + } } } #endif @@ -1312,15 +1305,20 @@ uint4 AstcAssembleBlock(uint baseX, uint baseY, uint effMode, AstcCandidate c) AstcComputePlane1WeightGrid(baseX, baseY, effMode, c.dualPlaneChannel, AstcGetChannel(uqE0, c.dualPlaneChannel), AstcGetChannel(uqE1, c.dualPlaneChannel), c.gridW, c.gridH); } - uint planes = isDual ? 2u : 1u; [loop] for (uint k = 0u; k < gridCount; ++k) { +#if defined(ASTC_HDR_VARIANT) + uint dummyUq; + AstcSetBiseVal(k, AstcQuantizeWeight(asfloat(g_AstcGrid[k].x), c.wQuant, dummyUq)); +#else + uint planes = isDual ? 2u : 1u; [loop] for (uint pl = 0u; pl < planes; ++pl) { float wf = asfloat((pl == 0u) ? g_AstcGrid[k].x : g_AstcGrid[k].y); uint dummyUq; - g_AstcBiseVals[isDual ? (2u * k + pl) : k] = AstcQuantizeWeight(wf, c.wQuant, dummyUq); + AstcSetBiseVal(isDual ? (2u * k + pl) : k, AstcQuantizeWeight(wf, c.wQuant, dummyUq)); } +#endif } if (isDual) totalWeights = gridCount * 2u; @@ -1330,7 +1328,7 @@ uint4 AstcAssembleBlock(uint baseX, uint baseY, uint effMode, AstcCandidate c) { [loop] for (uint wi = 0u; wi < totalWeights; ++wi) { - uint revW = AstcReverseSmallBits(g_AstcBiseVals[wi], wBits2); + uint revW = AstcReverseSmallBits(AstcGetBiseVal(wi), wBits2); AstcWriteBits128(block, 128u - (wi + 1u) * wBits2, wBits2, revW); } weightsPackedDirect = true; @@ -1356,7 +1354,7 @@ uint4 AstcAssembleBlock(uint baseX, uint baseY, uint effMode, AstcCandidate c) uint weightBits = AstcGetBiseBitCount(totalWeights, c.wQuant); AstcWriteBits128(block, 128u - weightBits - 2u, 2u, c.dualPlaneChannel); } - [loop] for (uint ei = 0u; ei < epPerPart; ++ei) g_AstcBiseVals[ei] = ASTC_EP_CUR(ei); + [loop] for (uint ei = 0u; ei < epPerPart; ++ei) AstcSetBiseVal(ei, ASTC_EP_CUR(ei)); dstBuf = block; startBit = 17u; biseQ = c.epQuant; @@ -1370,7 +1368,7 @@ uint4 AstcAssembleBlock(uint baseX, uint baseY, uint effMode, AstcCandidate c) return block; } #else -// Packs pre-computed weights (g_AstcBiseVals) and endpoints (ASTC_EP_BEST) into a 128-bit ASTC block. +// Packs pre-computed weights (packed in g_AstcGrid.w) and endpoints (ASTC_EP_BEST) into a 128-bit ASTC block. uint4 AstcPackFinalBlock(uint effMode, AstcCandidate c, uint totalWeights) { uint epPerPart = AstcGetEpCountForEffMode(effMode); @@ -1384,7 +1382,7 @@ uint4 AstcPackFinalBlock(uint effMode, AstcCandidate c, uint totalWeights) { [loop] for (uint wi = 0u; wi < totalWeights; ++wi) { - uint revW = AstcReverseSmallBits(g_AstcBiseVals[wi], wBits2); + uint revW = AstcReverseSmallBits(AstcGetBiseVal(wi), wBits2); AstcWriteBits128(block, 128u - (wi + 1u) * wBits2, wBits2, revW); } } @@ -1427,7 +1425,7 @@ uint4 AstcPackFinalBlock(uint effMode, AstcCandidate c, uint totalWeights) AstcWriteBits128(block, 128u - weightBits - 2u, 2u, c.dualPlaneChannel); } } - [loop] for (uint ei = 0u; ei < biseCnt; ++ei) g_AstcBiseVals[ei] = ASTC_EP_BEST(ei); + [loop] for (uint ei = 0u; ei < biseCnt; ++ei) AstcSetBiseVal(ei, ASTC_EP_BEST(ei)); dstBuf = block; } @@ -2196,7 +2194,7 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) { float wf = asfloat((pl == 0u) ? g_AstcGrid[k].x : g_AstcGrid[k].y); uint uqP; - g_AstcBiseVals[isDualFinal ? (2u * k + pl) : k] = AstcQuantizeWeight(wf, wQ, uqP); + AstcSetBiseVal(isDualFinal ? (2u * k + pl) : k, AstcQuantizeWeight(wf, wQ, uqP)); packedUq |= (uqP << (pl * 8u)); } g_AstcGrid[k].z = packedUq; From eb2f3f804b9258760490cfe536672f235e00a375 Mon Sep 17 00:00:00 2001 From: FrozenKresnik <80754402+Overflowu@users.noreply.github.com> Date: Tue, 1 Dec 2026 14:47:16 +0000 Subject: [PATCH 8/9] perf(gpu): pair HDR pixel cache into uint4 slots to eliminate final X4714 warning --- .../Runtime/Gpu/Shaders/AstcGpuCore.hlsl | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl index e989c7e..07a46c1 100644 --- a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl +++ b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl @@ -68,7 +68,8 @@ static uint2 g_AstcEpCodes[ASTC_MAX_EP_CODES]; // - HDR variants pack 4x16-bit LNS into uint2 (128 bytes for 4x4, 1152 bytes for 12x12). #define ASTC_ENABLE_PIXEL_CACHE 1 #if defined(ASTC_HDR_VARIANT) -static uint2 g_AstcPackedBlock[ASTC_MAX_BLOCK]; +// Pack 2 HDR LNS16 pixels per uint4 slot (.xy = even index, .zw = odd index) to halve indexable temp slots. +static uint4 g_AstcPackedBlock[(ASTC_MAX_BLOCK + 1) / 2]; uint2 AstcPackRgba16(float4 v) { @@ -89,7 +90,9 @@ float4 AstcFetchWorkingPixel(uint baseX, uint baseY, uint x, uint y, uint effMod { impWeight = 1.0; #if defined(ASTC_HDR_VARIANT) - uint2 packed = g_AstcPackedBlock[y * BLOCK_X + x]; + uint idx = y * BLOCK_X + x; + uint4 pair = g_AstcPackedBlock[idx >> 1u]; + uint2 packed = ((idx & 1u) == 0u) ? pair.xy : pair.zw; return float4( (float)(packed.x & 0xFFFFu), (float)(packed.x >> 16u), @@ -1735,7 +1738,10 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) float4 lns = AstcFloatToLns4(r); if (isHdrRgbOnly) lns.a = 65535.0; - g_AstcPackedBlock[rowOff + x] = AstcPackRgba16(lns); + uint pIdx = rowOff + x; + uint2 pk16 = AstcPackRgba16(lns); + if ((pIdx & 1u) == 0u) g_AstcPackedBlock[pIdx >> 1u].xy = pk16; + else g_AstcPackedBlock[pIdx >> 1u].zw = pk16; sumLns += lns; minLns = min(minLns, lns); maxLns = max(maxLns, lns); @@ -1781,8 +1787,10 @@ uint4 AstcEncodeBlock(uint blockXIdx, uint blockYIdx) uint px = min(baseX + cx, _AstcSrcWidth - 1u); float ra = saturate(_AstcSourceTex.Load(int3(px, py, _AstcMipLevel)).a); uint a16 = (uint)clamp(floor(ra * 65535.0 + 0.5), 0.0, 65535.0); - uint2 pk = g_AstcPackedBlock[rowOff + cx]; - g_AstcPackedBlock[rowOff + cx] = uint2(pk.x, (pk.y & 0xFFFFu) | (a16 << 16u)); + uint cIdx = rowOff + cx; + uint cSlot = cIdx >> 1u; + if ((cIdx & 1u) == 0u) g_AstcPackedBlock[cSlot].y = (g_AstcPackedBlock[cSlot].y & 0xFFFFu) | (a16 << 16u); + else g_AstcPackedBlock[cSlot].w = (g_AstcPackedBlock[cSlot].w & 0xFFFFu) | (a16 << 16u); } } hasPreStats = false; From d61b6173309cc12d4c15fdba7a0e938e3f273546 Mon Sep 17 00:00:00 2001 From: FrozenKresnik <80754402+Overflowu@users.noreply.github.com> Date: Tue, 1 Dec 2026 15:10:53 +0000 Subject: [PATCH 9/9] perf(gpu): unify HDR color quantization pass and pair dual-plane infill tap evaluation --- .../Runtime/Gpu/Shaders/AstcGpuColor.hlsl | 279 +++++++++--------- .../Runtime/Gpu/Shaders/AstcGpuCore.hlsl | 15 +- 2 files changed, 138 insertions(+), 156 deletions(-) diff --git a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuColor.hlsl b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuColor.hlsl index 39476e6..9dbf817 100644 --- a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuColor.hlsl +++ b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuColor.hlsl @@ -241,143 +241,128 @@ void AstcQuantizeHdrRgbEndpoints( out float3 uq0Lns, out float3 uq1Lns) { - // Baseline: majcomp == 3 direct mode (always valid) - g_AstcHdrWork[0].y = (uint)clamp(floor(e0Lns.r / 256.0 + 0.5), 0.0, 255.0); - g_AstcHdrWork[1].y = (uint)clamp(floor(e1Lns.r / 256.0 + 0.5), 0.0, 255.0); - g_AstcHdrWork[2].y = (uint)clamp(floor(e0Lns.g / 256.0 + 0.5), 0.0, 255.0); - g_AstcHdrWork[3].y = (uint)clamp(floor(e1Lns.g / 256.0 + 0.5), 0.0, 255.0); - g_AstcHdrWork[4].y = 128u + (uint)clamp(floor(e0Lns.b / 512.0 + 0.5), 0.0, 127.0); - g_AstcHdrWork[5].y = 128u + (uint)clamp(floor(e1Lns.b / 512.0 + 0.5), 0.0, 127.0); - - [loop] for (uint bi = 0u; bi < 6u; ++bi) + uq0Lns = 0.0; + uq1Lns = 0.0; + float bestErr = 1e30; + + // Pass 0: baseline majcomp == 3 direct mode (always valid). + // Pass 1: high-precision majcomp 0/1/2 delta mode (if e1 holds major component and a mode fits). + // Shares a single AstcQuantizeColorRange call site to halve HDR endpoint quantization IR. + [loop] for (uint hPass = 0u; hPass < 2u; ++hPass) { - uint uqTmp; - g_AstcHdrWork[bi].x = AstcQuantizeColorRange(g_AstcHdrWork[bi].y, epQuant, bi >= 4u, uqTmp); - g_AstcHdrWork[bi].w = uqTmp; - } - - uq0Lns = float3((float)(g_AstcHdrWork[0].w << 8u), (float)(g_AstcHdrWork[2].w << 8u), (float)((g_AstcHdrWork[4].w & 0x7Fu) << 9u)); - uq1Lns = float3((float)(g_AstcHdrWork[1].w << 8u), (float)(g_AstcHdrWork[3].w << 8u), (float)((g_AstcHdrWork[5].w & 0x7Fu) << 9u)); - - float3 d0 = uq0Lns - e0Lns; - float3 d1 = uq1Lns - e1Lns; - float bestErr = dot(d0, d0) + dot(d1, d1); - - // High-precision majcomp 0/1/2 delta mode requires e1 to hold the overall major component. - int3 c0_12 = int3( - clamp((int)floor(e0Lns.r / 16.0 + 0.5), 0, 4095), - clamp((int)floor(e0Lns.g / 16.0 + 0.5), 0, 4095), - clamp((int)floor(e0Lns.b / 16.0 + 0.5), 0, 4095)); - int3 c1_12 = int3( - clamp((int)floor(e1Lns.r / 16.0 + 0.5), 0, 4095), - clamp((int)floor(e1Lns.g / 16.0 + 0.5), 0, 4095), - clamp((int)floor(e1Lns.b / 16.0 + 0.5), 0, 4095)); - - int max0 = max(c0_12.r, max(c0_12.g, c0_12.b)); - int max1 = max(c1_12.r, max(c1_12.g, c1_12.b)); - if (max1 < max0) - { - return; - } - - uint majcomp = 0u; - int R0 = c0_12.r, G0 = c0_12.g, B0 = c0_12.b; - int R1 = c1_12.r, G1 = c1_12.g, B1 = c1_12.b; - if (c1_12.g >= c1_12.r && c1_12.g >= c1_12.b) - { - majcomp = 1u; - R0 = c0_12.g; G0 = c0_12.r; B0 = c0_12.b; - R1 = c1_12.g; G1 = c1_12.r; B1 = c1_12.b; - } - else if (c1_12.b > c1_12.r && c1_12.b > c1_12.g) - { - majcomp = 2u; - R0 = c0_12.b; G0 = c0_12.g; B0 = c0_12.r; - R1 = c1_12.b; G1 = c1_12.g; B1 = c1_12.r; - } - - int A_12 = R1; - int B0_12 = R1 - G1; - int B1_12 = R1 - B1; - int C_12 = R1 - R0; - int D0_12 = (R0 - G0) - B0_12; - int D1_12 = (R0 - B0) - B1_12; - - // Try finest resolution modes first (modeval 7..0) and pick the first that fits. - [loop] for (uint mi = 0u; mi < 8u; ++mi) - { - uint mv = 7u - mi; - uint sh = (mv >> 1u) ^ 3u; - uint bb = (mv == 1u || mv == 4u) ? 8u : ((mv == 0u || mv == 3u || mv == 6u) ? 7u : 6u); - uint cb = (mv == 5u) ? 8u : ((mv == 2u || mv == 3u || mv == 6u || mv == 7u) ? 7u : 6u); - uint db = (mv == 0u || mv == 2u) ? 7u : ((mv == 4u || mv == 6u) ? 5u : 6u); - - int halfStep = (sh > 0u) ? (int)(1u << (sh - 1u)) : 0; - int aS = min((int)((1u << (12u - sh)) - 1u), (A_12 + halfStep) >> (int)sh); - int b0S = (B0_12 + halfStep) >> (int)sh; - int b1S = (B1_12 + halfStep) >> (int)sh; - int cS = (C_12 + halfStep) >> (int)sh; - float invStep = 1.0 / (float)(1u << sh); - int d0S = (int)floor((float)D0_12 * invStep + 0.5); - int d1S = (int)floor((float)D1_12 * invStep + 0.5); - - int dMin = -(int)(1u << (db - 1u)); - int dMax = (int)(1u << (db - 1u)) - 1; - if (b0S >= 0 && b0S < (int)(1u << bb) - && b1S >= 0 && b1S < (int)(1u << bb) - && cS >= 0 && cS < (int)(1u << cb) - && d0S >= dMin && d0S <= dMax - && d1S >= dMin && d1S <= dMax) + if (hPass == 0u) { - uint dMask = (1u << db) - 1u; - AstcPackHdrRgbDeltaMode(majcomp, mv, (uint)aS, (uint)b0S, (uint)b1S, (uint)cS, ((uint)d0S) & dMask, ((uint)d1S) & dMask); - - [loop] for (uint ci = 0u; ci < 6u; ++ci) + g_AstcHdrWork[0].y = (uint)clamp(floor(e0Lns.r / 256.0 + 0.5), 0.0, 255.0); + g_AstcHdrWork[1].y = (uint)clamp(floor(e1Lns.r / 256.0 + 0.5), 0.0, 255.0); + g_AstcHdrWork[2].y = (uint)clamp(floor(e0Lns.g / 256.0 + 0.5), 0.0, 255.0); + g_AstcHdrWork[3].y = (uint)clamp(floor(e1Lns.g / 256.0 + 0.5), 0.0, 255.0); + g_AstcHdrWork[4].y = 128u + (uint)clamp(floor(e0Lns.b / 512.0 + 0.5), 0.0, 127.0); + g_AstcHdrWork[5].y = 128u + (uint)clamp(floor(e1Lns.b / 512.0 + 0.5), 0.0, 127.0); + } + else + { + int3 c0_12 = int3( + clamp((int)floor(e0Lns.r / 16.0 + 0.5), 0, 4095), + clamp((int)floor(e0Lns.g / 16.0 + 0.5), 0, 4095), + clamp((int)floor(e0Lns.b / 16.0 + 0.5), 0, 4095)); + int3 c1_12 = int3( + clamp((int)floor(e1Lns.r / 16.0 + 0.5), 0, 4095), + clamp((int)floor(e1Lns.g / 16.0 + 0.5), 0, 4095), + clamp((int)floor(e1Lns.b / 16.0 + 0.5), 0, 4095)); + + int max0 = max(c0_12.r, max(c0_12.g, c0_12.b)); + int max1 = max(c1_12.r, max(c1_12.g, c1_12.b)); + if (max1 < max0) break; + + uint majcomp = 0u; + int R0 = c0_12.r, G0 = c0_12.g, B0 = c0_12.b; + int R1 = c1_12.r, G1 = c1_12.g, B1 = c1_12.b; + if (c1_12.g >= c1_12.r && c1_12.g >= c1_12.b) + { + majcomp = 1u; + R0 = c0_12.g; G0 = c0_12.r; B0 = c0_12.b; + R1 = c1_12.g; G1 = c1_12.r; B1 = c1_12.b; + } + else if (c1_12.b > c1_12.r && c1_12.b > c1_12.g) { - uint uqTmp; - g_AstcHdrWork[ci].z = AstcQuantizeColor(g_AstcHdrWork[ci].y, epQuant, uqTmp); - g_AstcHdrWork[ci].w = uqTmp; + majcomp = 2u; + R0 = c0_12.b; G0 = c0_12.g; B0 = c0_12.r; + R1 = c1_12.b; G1 = c1_12.g; B1 = c1_12.r; } - float3 cand0, cand1; - AstcUnpackHdrRgb(cand0, cand1); - float3 cd0 = cand0 - e0Lns; - float3 cd1 = cand1 - e1Lns; - float candErr = dot(cd0, cd0) + dot(cd1, cd1); - if (candErr < bestErr) + int A_12 = R1; + int B0_12 = R1 - G1; + int B1_12 = R1 - B1; + int C_12 = R1 - R0; + int D0_12 = (R0 - G0) - B0_12; + int D1_12 = (R0 - B0) - B1_12; + + bool foundMode = false; + [loop] for (uint mi = 0u; mi < 8u; ++mi) { - [loop] for (uint cj = 0u; cj < 6u; ++cj) g_AstcHdrWork[cj].x = g_AstcHdrWork[cj].z; - uq0Lns = cand0; - uq1Lns = cand1; + uint mv = 7u - mi; + uint sh = (mv >> 1u) ^ 3u; + uint bb = (mv == 1u || mv == 4u) ? 8u : ((mv == 0u || mv == 3u || mv == 6u) ? 7u : 6u); + uint cb = (mv == 5u) ? 8u : ((mv == 2u || mv == 3u || mv == 6u || mv == 7u) ? 7u : 6u); + uint db = (mv == 0u || mv == 2u) ? 7u : ((mv == 4u || mv == 6u) ? 5u : 6u); + + int halfStep = (sh > 0u) ? (int)(1u << (sh - 1u)) : 0; + int aS = min((int)((1u << (12u - sh)) - 1u), (A_12 + halfStep) >> (int)sh); + int b0S = (B0_12 + halfStep) >> (int)sh; + int b1S = (B1_12 + halfStep) >> (int)sh; + int cS = (C_12 + halfStep) >> (int)sh; + float invStep = 1.0 / (float)(1u << sh); + int d0S = (int)floor((float)D0_12 * invStep + 0.5); + int d1S = (int)floor((float)D1_12 * invStep + 0.5); + + int dMin = -(int)(1u << (db - 1u)); + int dMax = (int)(1u << (db - 1u)) - 1; + if (b0S >= 0 && b0S < (int)(1u << bb) + && b1S >= 0 && b1S < (int)(1u << bb) + && cS >= 0 && cS < (int)(1u << cb) + && d0S >= dMin && d0S <= dMax + && d1S >= dMin && d1S <= dMax) + { + uint dMask = (1u << db) - 1u; + AstcPackHdrRgbDeltaMode(majcomp, mv, (uint)aS, (uint)b0S, (uint)b1S, (uint)cS, ((uint)d0S) & dMask, ((uint)d1S) & dMask); + foundMode = true; + break; + } } - break; + if (!foundMode) break; } - } -} -#endif // ASTC_HDR_VARIANT -// Encodes HDR Alpha endpoints a0, a1 (in LNS [0, 65535]) for CEM_HDR_RGBA_DIRECT (15) -// using direct modeval == 3 (bit 7 == 1 on both endpoints). -void AstcQuantizeHdrAlphaEndpoints( - float a0Lns, - float a1Lns, - uint epQuant, - out uint code0, - out uint code1, - out float uqA0Lns, - out float uqA1Lns) -{ - uint a0 = 128u + (uint)clamp(floor(a0Lns / 512.0 + 0.5), 0.0, 127.0); - uint a1 = 128u + (uint)clamp(floor(a1Lns / 512.0 + 0.5), 0.0, 127.0); + [loop] for (uint qi = 0u; qi < 6u; ++qi) + { + uint uqTmp; + g_AstcHdrWork[qi].z = AstcQuantizeColorRange(g_AstcHdrWork[qi].y, epQuant, (hPass == 0u) && (qi >= 4u), uqTmp); + g_AstcHdrWork[qi].w = uqTmp; + } - uint uq0, uq1; - code0 = AstcQuantizeColorMin128(a0, epQuant, uq0); - code1 = AstcQuantizeColorMin128(a1, epQuant, uq1); + float3 cand0, cand1; + if (hPass == 0u) + { + cand0 = float3((float)(g_AstcHdrWork[0].w << 8u), (float)(g_AstcHdrWork[2].w << 8u), (float)((g_AstcHdrWork[4].w & 0x7Fu) << 9u)); + cand1 = float3((float)(g_AstcHdrWork[1].w << 8u), (float)(g_AstcHdrWork[3].w << 8u), (float)((g_AstcHdrWork[5].w & 0x7Fu) << 9u)); + } + else + { + AstcUnpackHdrRgb(cand0, cand1); + } - uqA0Lns = (float)((uq0 & 0x7Fu) << 9u); - uqA1Lns = (float)((uq1 & 0x7Fu) << 9u); + float3 cd0 = cand0 - e0Lns; + float3 cd1 = cand1 - e1Lns; + float candErr = dot(cd0, cd0) + dot(cd1, cd1); + if (hPass == 0u || candErr < bestErr) + { + bestErr = candErr; + [loop] for (uint cj = 0u; cj < 6u; ++cj) g_AstcHdrWork[cj].x = g_AstcHdrWork[cj].z; + uq0Lns = cand0; + uq1Lns = cand1; + } + } } - +#endif // ASTC_HDR_VARIANT #endif // ASTC_HDR_VARIANT #if !defined(ASTC_FAST_ONLY) && !defined(ASTC_NO_2P) @@ -529,27 +514,29 @@ void AstcComputeInfillTap( tapWeights = AstcInfillWeights(gs, gt); } -// Evaluates the exact ASTC hardware interpolated weight [0, 64] at texel (x, y) -// from the packed .z component of g_AstcGrid (plane 0 in bits 0..7, plane 1 in bits 8..15). -#define ASTC_EVAL_INFILL_SHIFT(gridArr, shift, tx, ty, bx, by, gw, gh, outWeight64) \ - if ((gw) == (bx) && (gh) == (by)) \ - { \ - (outWeight64) = ((gridArr)[(ty) * (gw) + (tx)].z >> (shift)) & 0xFFu; \ - } \ - else \ - { \ - uint _js, _jt; \ - uint4 _tw; \ - AstcComputeInfillTap((tx), (ty), (bx), (by), (gw), (gh), _js, _jt, _tw); \ - uint _idx00 = _jt * (gw) + _js; \ - uint _p00 = ((gridArr)[_idx00].z >> (shift)) & 0xFFu; \ - uint _p10 = (_tw.y > 0u) ? (((gridArr)[_idx00 + 1u].z >> (shift)) & 0xFFu) : 0u; \ - uint _p01 = (_tw.z > 0u) ? (((gridArr)[_idx00 + (gw)].z >> (shift)) & 0xFFu) : 0u; \ - uint _p11 = (_tw.w > 0u) ? (((gridArr)[_idx00 + (gw) + 1u].z >> (shift)) & 0xFFu) : 0u; \ - (outWeight64) = (_p00 * _tw.x + _p10 * _tw.y + _p01 * _tw.z + _p11 * _tw.w + 8u) >> 4u; \ +// Evaluates exact ASTC hardware interpolated weights [0, 64] for plane 0 (and plane 1 when isDual) +// at texel (x, y) in a single bilinear tap + single 4-tap grid load (halves both compile-time IR and dual-plane runtime ALU). +#define ASTC_EVAL_INFILL_PAIR(gridArr, isDual, tx, ty, bx, by, gw, gh, outW0, outW1) \ + if ((gw) == (bx) && (gh) == (by)) \ + { \ + uint _pk = (gridArr)[(ty) * (gw) + (tx)].z; \ + (outW0) = _pk & 0xFFu; \ + (outW1) = (isDual) ? ((_pk >> 8u) & 0xFFu) : (outW0); \ + } \ + else \ + { \ + uint _js, _jt; \ + uint4 _tw; \ + AstcComputeInfillTap((tx), (ty), (bx), (by), (gw), (gh), _js, _jt, _tw); \ + uint _idx00 = _jt * (gw) + _js; \ + uint _v00 = (gridArr)[_idx00].z; \ + uint _v10 = (_tw.y > 0u) ? (gridArr)[_idx00 + 1u].z : 0u; \ + uint _v01 = (_tw.z > 0u) ? (gridArr)[_idx00 + (gw)].z : 0u; \ + uint _v11 = (_tw.w > 0u) ? (gridArr)[_idx00 + (gw) + 1u].z : 0u; \ + (outW0) = ((_v00 & 0xFFu) * _tw.x + (_v10 & 0xFFu) * _tw.y + (_v01 & 0xFFu) * _tw.z + (_v11 & 0xFFu) * _tw.w + 8u) >> 4u; \ + (outW1) = (isDual) \ + ? ((((_v00 >> 8u) & 0xFFu) * _tw.x + ((_v10 >> 8u) & 0xFFu) * _tw.y + ((_v01 >> 8u) & 0xFFu) * _tw.z + ((_v11 >> 8u) & 0xFFu) * _tw.w + 8u) >> 4u) \ + : (outW0); \ } -#define ASTC_EVAL_INFILL(gridArr, tx, ty, bx, by, gw, gh, outWeight64) \ - ASTC_EVAL_INFILL_SHIFT(gridArr, 0u, tx, ty, bx, by, gw, gh, outWeight64) - #endif // ASTC_GPU_COLOR_HLSL diff --git a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl index 07a46c1..10442fd 100644 --- a/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl +++ b/Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuCore.hlsl @@ -943,8 +943,8 @@ float AstcEvalCandidateError( { float imp; float4 p = AstcFetchWorkingPixel(baseX, baseY, x, y, effMode, imp); - uint w0; - ASTC_EVAL_INFILL(g_AstcGrid, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w0); + uint w0, w1; + ASTC_EVAL_INFILL_PAIR(g_AstcGrid, isDual, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w0, w1); float4 recon; #if ASTC_ENABLE_2P @@ -963,8 +963,6 @@ float AstcEvalCandidateError( recon = lerp(uq0_p0, uq1_p0, (float)effW0 * (1.0 / 64.0)); if (isDual) { - uint w1; - ASTC_EVAL_INFILL_SHIFT(g_AstcGrid, 8u, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w1); uint effW1 = applySwap0 ? (64u - w1) : w1; float p1Val = lerp(AstcGetChannel(uq0_p0, ccs), AstcGetChannel(uq1_p0, ccs), (float)effW1 * (1.0 / 64.0)); recon = AstcSetChannel(recon, ccs, p1Val); @@ -1137,18 +1135,15 @@ bool AstcSolveRefitEndpoints( float imp; float4 p = AstcFetchWorkingPixel(baseX, baseY, x, y, effMode, imp); float4 chImp = AstcGetChannelImp(effMode, imp); - uint w0_64; - ASTC_EVAL_INFILL(g_AstcGrid, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w0_64); + uint w0_64, w1_64; + ASTC_EVAL_INFILL_PAIR(g_AstcGrid, isDualPlane, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w0_64, w1_64); float w0 = (float)w0_64 * (1.0 / 64.0); float omw0 = 1.0 - w0; float4 recon = lerp(curUqE0, curUqE1, w0); - float w1 = w0; + float w1 = (float)w1_64 * (1.0 / 64.0); if (isDualPlane) { - uint w1_64; - ASTC_EVAL_INFILL_SHIFT(g_AstcGrid, 8u, x, y, BLOCK_X, BLOCK_Y, gridW, gridH, w1_64); - w1 = (float)w1_64 * (1.0 / 64.0); recon = AstcSetChannel(recon, ccs, lerp(AstcGetChannel(curUqE0, ccs), AstcGetChannel(curUqE1, ccs), w1)); }