From 4bb9a126196c6d3717ab0e2c5891c0546b564e4c Mon Sep 17 00:00:00 2001 From: FrozenKresnik <80754402+Overflowu@users.noreply.github.com> Date: Tue, 1 Dec 2026 16:50:46 +0000 Subject: [PATCH 1/3] feat: add unified CPU/GPU ASTC feature and compute shader variant stripping --- .../Editor/AstcBenchmarkWindow.cs | 4 +- .../Editor/AstcComputeShaderStripper.cs | 210 ++++++++++++++ .../Editor/AstcComputeShaderStripper.cs.meta | 11 + .../Editor/AstcPerformanceBenchmarkRunner.cs | 75 ++--- .../Editor/AstcStrippingSettingsWindow.cs | 259 ++++++++++++++++++ .../AstcStrippingSettingsWindow.cs.meta | 11 + .../Runtime/AstcCompressOptions.cs | 12 +- Assets/ASTC Encoder/Runtime/AstcContext.cs | 9 +- Assets/ASTC Encoder/Runtime/AstcHeader.cs | 12 +- .../ASTC Encoder/Runtime/AstcKtxContainer.cs | 66 ++--- .../ASTC Encoder/Runtime/AstcStripConfig.cs | 227 +++++++++++++++ .../Runtime/AstcStripConfig.cs.meta | 11 + Assets/ASTC Encoder/Runtime/BlockSize.cs | 37 ++- .../ASTC Encoder/Runtime/CompressQuality.cs | 33 ++- .../Runtime/Cpu/AstcCpuBackend.cs | 12 +- Assets/ASTC Encoder/Runtime/Encoder.cs | 76 ++--- .../Runtime/Gpu/AstcGpuBackend.cs | 202 +++++++++++--- .../Tests/Editor/AstcEncoderTests.cs | 85 ++++++ 18 files changed, 1170 insertions(+), 182 deletions(-) create mode 100644 Assets/ASTC Encoder/Editor/AstcComputeShaderStripper.cs create mode 100644 Assets/ASTC Encoder/Editor/AstcComputeShaderStripper.cs.meta create mode 100644 Assets/ASTC Encoder/Editor/AstcStrippingSettingsWindow.cs create mode 100644 Assets/ASTC Encoder/Editor/AstcStrippingSettingsWindow.cs.meta create mode 100644 Assets/ASTC Encoder/Runtime/AstcStripConfig.cs create mode 100644 Assets/ASTC Encoder/Runtime/AstcStripConfig.cs.meta diff --git a/Assets/ASTC Encoder/Editor/AstcBenchmarkWindow.cs b/Assets/ASTC Encoder/Editor/AstcBenchmarkWindow.cs index fadfa33..fe17c13 100644 --- a/Assets/ASTC Encoder/Editor/AstcBenchmarkWindow.cs +++ b/Assets/ASTC Encoder/Editor/AstcBenchmarkWindow.cs @@ -56,8 +56,8 @@ public void Dispose() private Texture2D m_SourceTexture; private TextureType m_TextureType = TextureType.Default; - private BlockSize m_BlockSize = BlockSize.ASTC_6x6; - private CompressQuality m_Quality = CompressQuality.Fast; + private BlockSize m_BlockSize = AstcCompressOptions.Default.BlockSize; + private CompressQuality m_Quality = AstcCompressOptions.Default.Quality; private bool m_UseAlphaWeight; private bool m_UsePerceptual; private bool m_GenerateMipmaps; diff --git a/Assets/ASTC Encoder/Editor/AstcComputeShaderStripper.cs b/Assets/ASTC Encoder/Editor/AstcComputeShaderStripper.cs new file mode 100644 index 0000000..95dbfe8 --- /dev/null +++ b/Assets/ASTC Encoder/Editor/AstcComputeShaderStripper.cs @@ -0,0 +1,210 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Aperture.Astcenc.Runtime; +using UnityEditor; +using UnityEditor.Build; +using UnityEditor.Build.Reporting; +using UnityEditor.Rendering; +using UnityEngine; + +namespace Aperture.Astcenc.Editor +{ + /// + /// Build-time ComputeShader variant stripper driven by the same Scripting Define Symbols + /// (ASTC_STRIP_4X4.. ASTC_STRIP_12X12, ASTC_STRIP_HDR, + /// ASTC_STRIP_FAST, ASTC_STRIP_MED, ASTC_STRIP_FULL) + /// that enforce C# compile-time enum checks and runtime CPU/GPU validation. + /// + public sealed class AstcComputeShaderStripper : IPreprocessComputeShaders, IPreprocessBuildWithReport, IPostprocessBuildWithReport + { + private const string k_EncoderShaderPrefix = "AstcGpuEncoder_"; + private const string k_HdrSuffix = "_Hdr"; + + internal static readonly string[] AllStripDefines = + { + AstcStripConfig.DefineStrip4x4, + AstcStripConfig.DefineStrip5x5, + AstcStripConfig.DefineStrip6x6, + AstcStripConfig.DefineStrip8x8, + AstcStripConfig.DefineStrip10x10, + AstcStripConfig.DefineStrip12x12, + AstcStripConfig.DefineStripHdr, + AstcStripConfig.DefineStripFast, + AstcStripConfig.DefineStripMed, + AstcStripConfig.DefineStripFull, + }; + + private static HashSet s_ActiveStripDefines; + private static int s_StrippedVariantCount; + private static int s_KeptVariantCount; + + public int callbackOrder => 0; + + public void OnPreprocessBuild(BuildReport report) + { + BuildTargetGroup group = report != null + ? report.summary.platformGroup + : EditorUserBuildSettings.selectedBuildTargetGroup; + + s_ActiveStripDefines = GetActiveStripDefines(group); + s_StrippedVariantCount = 0; + s_KeptVariantCount = 0; + } + + public void OnProcessComputeShader(ComputeShader shader, string kernelName, IList data) + { + if (shader == null || data == null || data.Count == 0) + return; + + string shaderName = shader.name; + if (string.IsNullOrEmpty(shaderName) || !shaderName.StartsWith(k_EncoderShaderPrefix, StringComparison.Ordinal)) + return; + + HashSet defines = s_ActiveStripDefines ?? GetActiveStripDefines(EditorUserBuildSettings.selectedBuildTargetGroup); + if (ShouldStrip(shaderName, kernelName, defines)) + { + s_StrippedVariantCount += data.Count; + data.Clear(); + } + else + { + s_KeptVariantCount += data.Count; + } + } + + public void OnPostprocessBuild(BuildReport report) + { + if (s_StrippedVariantCount > 0) + { + string activeList = s_ActiveStripDefines != null && s_ActiveStripDefines.Count > 0 + ? string.Join(", ", s_ActiveStripDefines.OrderBy(d => d, StringComparer.Ordinal)) + : "none"; + Debug.Log($"[ASTC Encoder] Stripped {s_StrippedVariantCount} compute shader variant(s), kept {s_KeptVariantCount} (defines: {activeList})."); + } + + s_ActiveStripDefines = null; + s_StrippedVariantCount = 0; + s_KeptVariantCount = 0; + } + + /// + /// Determines whether a given ASTC encoder compute shader + kernel should be stripped + /// under the provided set of Scripting Define Symbols. + /// + public static bool ShouldStrip(string shaderName, string kernelName, ICollection activeDefines) + { + if (activeDefines == null || activeDefines.Count == 0) + return false; + + if (string.IsNullOrEmpty(shaderName) || !shaderName.StartsWith(k_EncoderShaderPrefix, StringComparison.Ordinal)) + return false; + + bool isHdr = shaderName.EndsWith(k_HdrSuffix, StringComparison.Ordinal); + if (isHdr && activeDefines.Contains(AstcStripConfig.DefineStripHdr)) + return true; + + int suffixStart = k_EncoderShaderPrefix.Length; + int suffixLen = shaderName.Length - suffixStart - (isHdr ? k_HdrSuffix.Length : 0); + if (suffixLen <= 0) + return false; + + string blockTag = shaderName.Substring(suffixStart, suffixLen); + string blockDefine = blockTag switch + { + "4x4" => AstcStripConfig.DefineStrip4x4, + "5x5" => AstcStripConfig.DefineStrip5x5, + "6x6" => AstcStripConfig.DefineStrip6x6, + "8x8" => AstcStripConfig.DefineStrip8x8, + "10x10" => AstcStripConfig.DefineStrip10x10, + "12x12" => AstcStripConfig.DefineStrip12x12, + _ => null, + }; + + if (blockDefine != null && activeDefines.Contains(blockDefine)) + return true; + + string qualityDefine = kernelName switch + { + "EncodeFast" => AstcStripConfig.DefineStripFast, + "EncodeMed" => AstcStripConfig.DefineStripMed, + "EncodeFull" => AstcStripConfig.DefineStripFull, + _ => null, + }; + + return qualityDefine != null && activeDefines.Contains(qualityDefine); + } + + internal static HashSet GetActiveStripDefines(BuildTargetGroup group) + { + if (group == BuildTargetGroup.Unknown) + group = BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget); + + string[] rawDefines = GetScriptingDefines(group); + var result = new HashSet(StringComparer.Ordinal); + for (int i = 0; i < rawDefines.Length; i++) + { + string sym = rawDefines[i]?.Trim(); + if (!string.IsNullOrEmpty(sym) && Array.IndexOf(AllStripDefines, sym) >= 0) + { + result.Add(sym); + } + } + return result; + } + + internal static void SetStripDefines(BuildTargetGroup group, HashSet stripDefinesToEnable) + { + if (group == BuildTargetGroup.Unknown) + group = BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget); + + var allDefines = new List(); + string[] existing = GetScriptingDefines(group); + for (int i = 0; i < existing.Length; i++) + { + string sym = existing[i]?.Trim(); + if (string.IsNullOrEmpty(sym)) + continue; + if (Array.IndexOf(AllStripDefines, sym) >= 0) + continue; + if (!allDefines.Contains(sym)) + allDefines.Add(sym); + } + + for (int i = 0; i < AllStripDefines.Length; i++) + { + string sym = AllStripDefines[i]; + if (stripDefinesToEnable != null && stripDefinesToEnable.Contains(sym)) + { + allDefines.Add(sym); + } + } + + SetScriptingDefines(group, allDefines.ToArray()); + } + + private static string[] GetScriptingDefines(BuildTargetGroup group) + { +#if UNITY_2021_2_OR_NEWER + NamedBuildTarget namedTarget = NamedBuildTarget.FromBuildTargetGroup(group); + PlayerSettings.GetScriptingDefineSymbols(namedTarget, out string[] defines); + return defines ?? Array.Empty(); +#else + string raw = PlayerSettings.GetScriptingDefineSymbolsForGroup(group); + return string.IsNullOrEmpty(raw) + ? Array.Empty() + : raw.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); +#endif + } + + private static void SetScriptingDefines(BuildTargetGroup group, string[] defines) + { +#if UNITY_2021_2_OR_NEWER + NamedBuildTarget namedTarget = NamedBuildTarget.FromBuildTargetGroup(group); + PlayerSettings.SetScriptingDefineSymbols(namedTarget, defines); +#else + PlayerSettings.SetScriptingDefineSymbolsForGroup(group, string.Join(";", defines)); +#endif + } + } +} diff --git a/Assets/ASTC Encoder/Editor/AstcComputeShaderStripper.cs.meta b/Assets/ASTC Encoder/Editor/AstcComputeShaderStripper.cs.meta new file mode 100644 index 0000000..1f3f820 --- /dev/null +++ b/Assets/ASTC Encoder/Editor/AstcComputeShaderStripper.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c75ea8fa8fbe434a981e20d760b78c9c +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/ASTC Encoder/Editor/AstcPerformanceBenchmarkRunner.cs b/Assets/ASTC Encoder/Editor/AstcPerformanceBenchmarkRunner.cs index 339b521..c4f4483 100644 --- a/Assets/ASTC Encoder/Editor/AstcPerformanceBenchmarkRunner.cs +++ b/Assets/ASTC Encoder/Editor/AstcPerformanceBenchmarkRunner.cs @@ -170,6 +170,15 @@ public static void RunAllFromCli() Debug.Log($"[ASTC Benchmark CLI] Completed {report.Results.Count} benchmarks. Output: {exportDir}\n{FormatMarkdownTable(report)}"); } + // Raw enum values so Editor benchmark code compiles cleanly even when ASTC_STRIP_* defines mark public members obsolete. + private const BlockSize k_Block4x4 = (BlockSize)1; + private const BlockSize k_Block6x6 = (BlockSize)3; + private const BlockSize k_Block8x8 = (BlockSize)4; + private const BlockSize k_Block12x12 = (BlockSize)6; + private const CompressQuality k_QualityFast = (CompressQuality)1; + private const CompressQuality k_QualityMedium = (CompressQuality)2; + private const CompressQuality k_QualityThorough = (CompressQuality)3; + public static AstcBenchmarkReport CreateEmptyReport() { return new AstcBenchmarkReport @@ -219,7 +228,7 @@ public static List RunGpuEncodingSuite(int warmup = 2, int if (!Encoder.IsGpuSupported) return results; - BlockSize[] blockSizes = { BlockSize.ASTC_4x4, BlockSize.ASTC_6x6, BlockSize.ASTC_8x8, BlockSize.ASTC_12x12 }; + BlockSize[] blockSizes = { k_Block4x4, k_Block6x6, k_Block8x8, k_Block12x12 }; foreach (BlockSize bs in blockSizes) { Encoder.PreloadGpu(bs); @@ -234,7 +243,7 @@ public static List RunGpuEncodingSuite(int warmup = 2, int try { - CompressQuality[] qualities512 = { CompressQuality.Fast, CompressQuality.Medium, CompressQuality.Thorough }; + CompressQuality[] qualities512 = { k_QualityFast, k_QualityMedium, k_QualityThorough }; foreach (BlockSize bs in blockSizes) { foreach (CompressQuality q in qualities512) @@ -264,7 +273,7 @@ public static List RunGpuEncodingSuite(int warmup = 2, int } // 2. 1024x1024 RGBA8 (Fast / Medium across 4x4, 6x6, 8x8, 12x12) - CompressQuality[] qualities1024 = { CompressQuality.Fast, CompressQuality.Medium }; + CompressQuality[] qualities1024 = { k_QualityFast, k_QualityMedium }; foreach (BlockSize bs in blockSizes) { foreach (CompressQuality q in qualities1024) @@ -297,8 +306,8 @@ public static List RunGpuEncodingSuite(int warmup = 2, int { var srgbOpt = AstcCompressOptions.Default; srgbOpt.Backend = EncodeBackend.Gpu; - srgbOpt.BlockSize = BlockSize.ASTC_6x6; - srgbOpt.Quality = CompressQuality.Fast; + srgbOpt.BlockSize = k_Block6x6; + srgbOpt.Quality = k_QualityFast; srgbOpt.IncludeMipmaps = false; results.Add(AstcBenchmarkSampler.Measure( @@ -319,8 +328,8 @@ public static List RunGpuEncodingSuite(int warmup = 2, int var normOpt = AstcCompressOptions.NormalMap; normOpt.Backend = EncodeBackend.Gpu; - normOpt.BlockSize = BlockSize.ASTC_6x6; - normOpt.Quality = CompressQuality.Medium; + normOpt.BlockSize = k_Block6x6; + normOpt.Quality = k_QualityMedium; normOpt.IncludeMipmaps = false; results.Add(AstcBenchmarkSampler.Measure( @@ -341,8 +350,8 @@ public static List RunGpuEncodingSuite(int warmup = 2, int var hdrOpt = AstcCompressOptions.Default; hdrOpt.Backend = EncodeBackend.Gpu; - hdrOpt.BlockSize = BlockSize.ASTC_6x6; - hdrOpt.Quality = CompressQuality.Fast; + hdrOpt.BlockSize = k_Block6x6; + hdrOpt.Quality = k_QualityFast; hdrOpt.IncludeMipmaps = false; results.Add(AstcBenchmarkSampler.Measure( @@ -363,8 +372,8 @@ public static List RunGpuEncodingSuite(int warmup = 2, int var mipOpt = AstcCompressOptions.Default; mipOpt.Backend = EncodeBackend.Gpu; - mipOpt.BlockSize = BlockSize.ASTC_6x6; - mipOpt.Quality = CompressQuality.Fast; + mipOpt.BlockSize = k_Block6x6; + mipOpt.Quality = k_QualityFast; mipOpt.IncludeMipmaps = true; mipOpt.GenerateMipmaps = true; @@ -389,8 +398,8 @@ public static List RunGpuEncodingSuite(int warmup = 2, int // Direct CopyTexture vs Readback fallback for CompressToTexture (aligned 4x4 mip0 vs non-aligned 6x6 mip0) var direct4x4Opt = AstcCompressOptions.Default; direct4x4Opt.Backend = EncodeBackend.Gpu; - direct4x4Opt.BlockSize = BlockSize.ASTC_4x4; - direct4x4Opt.Quality = CompressQuality.Fast; + direct4x4Opt.BlockSize = k_Block4x4; + direct4x4Opt.Quality = k_QualityFast; direct4x4Opt.IncludeMipmaps = false; results.Add(AstcBenchmarkSampler.Measure( @@ -410,12 +419,12 @@ public static List RunGpuEncodingSuite(int warmup = 2, int if (outTex != null) UnityEngine.Object.DestroyImmediate(outTex); return ok; }, - notes: Encoder.SupportsGpuDirectCopy(BlockSize.ASTC_4x4) ? "DirectCopy eligible" : "Readback fallback (no HW ASTC)")); + notes: Encoder.SupportsGpuDirectCopy(k_Block4x4) ? "DirectCopy eligible" : "Readback fallback (no HW ASTC)")); var fallback6x6Opt = AstcCompressOptions.Default; fallback6x6Opt.Backend = EncodeBackend.Gpu; - fallback6x6Opt.BlockSize = BlockSize.ASTC_6x6; - fallback6x6Opt.Quality = CompressQuality.Fast; + fallback6x6Opt.BlockSize = k_Block6x6; + fallback6x6Opt.Quality = k_QualityFast; fallback6x6Opt.IncludeMipmaps = false; results.Add(AstcBenchmarkSampler.Measure( @@ -459,8 +468,8 @@ public static List RunCpuEncodingSuite(int warmup = 1, int Texture2D tex512 = CreateProceduralColorTexture(512, 512, srgb: false, withAlpha: true, withMipmaps: false); try { - BlockSize[] blockSizes = { BlockSize.ASTC_4x4, BlockSize.ASTC_6x6, BlockSize.ASTC_8x8, BlockSize.ASTC_12x12 }; - CompressQuality[] qualities = { CompressQuality.Fast, CompressQuality.Medium }; + BlockSize[] blockSizes = { k_Block4x4, k_Block6x6, k_Block8x8, k_Block12x12 }; + CompressQuality[] qualities = { k_QualityFast, k_QualityMedium }; int autoThreads = Math.Max(1, Math.Min(Environment.ProcessorCount, 4)); // 1. Cold (per-call AstcContext.Create) vs Warm (reused AstcContext) @@ -528,8 +537,8 @@ public static List RunCpuEncodingSuite(int warmup = 1, int using AstcContext ctx = AstcContext.Create( tex512.graphicsFormat, TextureType.Default, - BlockSize.ASTC_6x6, - CompressQuality.Fast, + k_Block6x6, + k_QualityFast, threadCount: tc, useUnityJobSystem: false); @@ -557,8 +566,8 @@ public static List RunCpuEncodingSuite(int warmup = 1, int using AstcContext jobCtx = AstcContext.Create( tex512.graphicsFormat, TextureType.Default, - BlockSize.ASTC_6x6, - CompressQuality.Fast, + k_Block6x6, + k_QualityFast, threadCount: autoThreads, useUnityJobSystem: true); @@ -610,8 +619,8 @@ public static List RunCpuRawPixelsSuite(int warmup = 1, in var opt = AstcCompressOptions.Default; opt.Backend = EncodeBackend.Cpu; - opt.BlockSize = BlockSize.ASTC_6x6; - opt.Quality = CompressQuality.Fast; + opt.BlockSize = k_Block6x6; + opt.Quality = k_QualityFast; opt.ThreadCount = 0; results.Add(AstcBenchmarkSampler.Measure( @@ -662,7 +671,7 @@ public static List RunDecompressionSuite(int warmup = 1, i Texture2D tex512 = CreateProceduralColorTexture(w, h, srgb: false, withAlpha: true, withMipmaps: false); try { - BlockSize[] blockSizes = { BlockSize.ASTC_4x4, BlockSize.ASTC_6x6, BlockSize.ASTC_8x8, BlockSize.ASTC_12x12 }; + BlockSize[] blockSizes = { k_Block4x4, k_Block6x6, k_Block8x8, k_Block12x12 }; int multiThreads = Math.Max(1, Math.Min(Environment.ProcessorCount, 4)); foreach (BlockSize bs in blockSizes) @@ -670,7 +679,7 @@ public static List RunDecompressionSuite(int warmup = 1, i var opt = AstcCompressOptions.Default; opt.Backend = EncodeBackend.Cpu; opt.BlockSize = bs; - opt.Quality = CompressQuality.Fast; + opt.Quality = k_QualityFast; opt.IncludeMipmaps = false; using AstcRaw encoded = tex512.CompressTexture(opt); @@ -737,8 +746,8 @@ public static List RunContainerAndCacheSuite(int warmup = { var opt = AstcCompressOptions.Default; opt.Backend = Encoder.IsGpuSupported ? EncodeBackend.Gpu : EncodeBackend.Cpu; - opt.BlockSize = BlockSize.ASTC_6x6; - opt.Quality = CompressQuality.Fast; + opt.BlockSize = k_Block6x6; + opt.Quality = k_QualityFast; opt.IncludeMipmaps = true; using AstcRaw raw = tex512Mip.CompressTexture(opt); @@ -838,8 +847,8 @@ public static List RunQueueBatchSuite(int warmup = 1, int var cpuOpt = AstcCompressOptions.Default; cpuOpt.Backend = EncodeBackend.Cpu; - cpuOpt.BlockSize = BlockSize.ASTC_6x6; - cpuOpt.Quality = CompressQuality.Fast; + cpuOpt.BlockSize = k_Block6x6; + cpuOpt.Quality = k_QualityFast; cpuOpt.IncludeMipmaps = false; results.Add(AstcBenchmarkSampler.Measure( @@ -878,7 +887,7 @@ public static List RunQueueBatchSuite(int warmup = 1, int if (Encoder.IsGpuSupported) { - Encoder.PreloadGpu(BlockSize.ASTC_4x4); + Encoder.PreloadGpu(k_Block4x4); using var gpuQueue = new AstcCompressionQueue { MaxGpuTexelsPerFrame = totalPixels @@ -886,8 +895,8 @@ public static List RunQueueBatchSuite(int warmup = 1, int var gpuOpt = AstcCompressOptions.Default; gpuOpt.Backend = EncodeBackend.Gpu; - gpuOpt.BlockSize = BlockSize.ASTC_4x4; - gpuOpt.Quality = CompressQuality.Fast; + gpuOpt.BlockSize = k_Block4x4; + gpuOpt.Quality = k_QualityFast; gpuOpt.IncludeMipmaps = false; results.Add(AstcBenchmarkSampler.Measure( diff --git a/Assets/ASTC Encoder/Editor/AstcStrippingSettingsWindow.cs b/Assets/ASTC Encoder/Editor/AstcStrippingSettingsWindow.cs new file mode 100644 index 0000000..0ca75f2 --- /dev/null +++ b/Assets/ASTC Encoder/Editor/AstcStrippingSettingsWindow.cs @@ -0,0 +1,259 @@ +using System; +using System.Collections.Generic; +using Aperture.Astcenc.Runtime; +using UnityEditor; +using UnityEngine; + +namespace Aperture.Astcenc.Editor +{ + /// + /// Editor window for configuring unified CPU + GPU ASTC feature and compute shader variant stripping + /// via PlayerSettings Scripting Define Symbols (ASTC_STRIP_*). + /// + public sealed class AstcStrippingSettingsWindow : EditorWindow + { + private static readonly (string Label, string Define)[] s_BlockSizeOptions = + { + ("ASTC 4x4 (8.00 bpp)", AstcStripConfig.DefineStrip4x4), + ("ASTC 5x5 (5.12 bpp)", AstcStripConfig.DefineStrip5x5), + ("ASTC 6x6 (3.56 bpp)", AstcStripConfig.DefineStrip6x6), + ("ASTC 8x8 (2.00 bpp)", AstcStripConfig.DefineStrip8x8), + ("ASTC 10x10 (1.28 bpp)", AstcStripConfig.DefineStrip10x10), + ("ASTC 12x12 (0.89 bpp)", AstcStripConfig.DefineStrip12x12), + }; + + private static readonly (string Label, string Define)[] s_QualityOptions = + { + ("Fast (Fastest / Fast — EncodeFast)", AstcStripConfig.DefineStripFast), + ("Medium (Medium — EncodeMed)", AstcStripConfig.DefineStripMed), + ("Full (Thorough / VeryThorough / Exhaustive — EncodeFull)", AstcStripConfig.DefineStripFull), + }; + + private BuildTargetGroup m_TargetGroup; + private readonly HashSet m_PendingStripDefines = new HashSet(StringComparer.Ordinal); + private readonly HashSet m_AppliedStripDefines = new HashSet(StringComparer.Ordinal); + private Vector2 m_ScrollPos; + + [MenuItem("Window/ASTC Encoder/Shader & Feature Stripping")] + public static void OpenWindow() + { + var window = GetWindow("ASTC Stripping"); + window.minSize = new Vector2(460f, 480f); + window.Show(); + } + + private void OnEnable() + { + ReloadFromPlayerSettings(); + } + + private void ReloadFromPlayerSettings() + { + m_TargetGroup = EditorUserBuildSettings.selectedBuildTargetGroup; + if (m_TargetGroup == BuildTargetGroup.Unknown) + m_TargetGroup = BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget); + + HashSet active = AstcComputeShaderStripper.GetActiveStripDefines(m_TargetGroup); + m_AppliedStripDefines.Clear(); + m_PendingStripDefines.Clear(); + foreach (string def in active) + { + m_AppliedStripDefines.Add(def); + m_PendingStripDefines.Add(def); + } + } + + private bool HasUnsavedChanges() + { + return !m_PendingStripDefines.SetEquals(m_AppliedStripDefines); + } + + private void OnGUI() + { + BuildTargetGroup currentGroup = EditorUserBuildSettings.selectedBuildTargetGroup; + if (currentGroup == BuildTargetGroup.Unknown) + currentGroup = BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget); + if (currentGroup != m_TargetGroup) + { + ReloadFromPlayerSettings(); + } + + m_ScrollPos = EditorGUILayout.BeginScrollView(m_ScrollPos); + + EditorGUILayout.LabelField("ASTC Encoder — Unified CPU + GPU Feature & Shader Stripping", EditorStyles.boldLabel); + EditorGUILayout.HelpBox( + "Controls Scripting Define Symbols (ASTC_STRIP_*) for the active build target.\n" + + "• C# Compile Time: Stripped BlockSize / CompressQuality enum values become compile errors (CS0619).\n" + + "• Runtime (CPU & GPU): Both AstcContext (CPU) and AstcGpuBackend (GPU) enforce the exact same rules.\n" + + "• Build Time (GPU): Stripped compute shader files and kernels are excluded via IPreprocessComputeShaders.", + MessageType.Info); + + EditorGUILayout.Space(4f); + EditorGUILayout.LabelField("Active Build Target Group", m_TargetGroup.ToString(), EditorStyles.boldLabel); + + int enabledBlocks = 0; + for (int i = 0; i < s_BlockSizeOptions.Length; i++) + { + if (!m_PendingStripDefines.Contains(s_BlockSizeOptions[i].Define)) + enabledBlocks++; + } + + int enabledRanges = m_PendingStripDefines.Contains(AstcStripConfig.DefineStripHdr) ? 1 : 2; + + int enabledQualities = 0; + for (int i = 0; i < s_QualityOptions.Length; i++) + { + if (!m_PendingStripDefines.Contains(s_QualityOptions[i].Define)) + enabledQualities++; + } + + int activeVariants = enabledBlocks * enabledRanges * enabledQualities; + int totalVariants = 6 * 2 * 3; + int strippedVariants = totalVariants - activeVariants; + float reductionPct = (strippedVariants * 100f) / totalVariants; + + using (new EditorGUILayout.VerticalScope("box")) + { + EditorGUILayout.LabelField( + $"GPU Compute Shader Variants: {activeVariants} / {totalVariants} enabled ({strippedVariants} stripped, -{reductionPct:F0}%)", + EditorStyles.boldLabel); + } + + EditorGUILayout.Space(4f); + + // Quick Presets + using (new EditorGUILayout.HorizontalScope()) + { + if (GUILayout.Button("Enable All (36/36)")) + { + m_PendingStripDefines.Clear(); + } + + if (GUILayout.Button("Mobile Common (6/36)")) + { + m_PendingStripDefines.Clear(); + m_PendingStripDefines.Add(AstcStripConfig.DefineStrip5x5); + m_PendingStripDefines.Add(AstcStripConfig.DefineStrip10x10); + m_PendingStripDefines.Add(AstcStripConfig.DefineStrip12x12); + m_PendingStripDefines.Add(AstcStripConfig.DefineStripHdr); + m_PendingStripDefines.Add(AstcStripConfig.DefineStripFull); + } + + if (GUILayout.Button("6x6 LDR Fast Only (1/36)")) + { + m_PendingStripDefines.Clear(); + m_PendingStripDefines.Add(AstcStripConfig.DefineStrip4x4); + m_PendingStripDefines.Add(AstcStripConfig.DefineStrip5x5); + m_PendingStripDefines.Add(AstcStripConfig.DefineStrip8x8); + m_PendingStripDefines.Add(AstcStripConfig.DefineStrip10x10); + m_PendingStripDefines.Add(AstcStripConfig.DefineStrip12x12); + m_PendingStripDefines.Add(AstcStripConfig.DefineStripHdr); + m_PendingStripDefines.Add(AstcStripConfig.DefineStripMed); + m_PendingStripDefines.Add(AstcStripConfig.DefineStripFull); + } + } + + EditorGUILayout.Space(6f); + + // 1. Block Sizes + using (new EditorGUILayout.VerticalScope("box")) + { + EditorGUILayout.LabelField("Block Sizes (CPU + GPU)", EditorStyles.boldLabel); + for (int i = 0; i < s_BlockSizeOptions.Length; i++) + { + DrawFeatureToggle(s_BlockSizeOptions[i].Label, s_BlockSizeOptions[i].Define); + } + } + + EditorGUILayout.Space(4f); + + // 2. Dynamic Range + using (new EditorGUILayout.VerticalScope("box")) + { + EditorGUILayout.LabelField("Dynamic Range (CPU + GPU)", EditorStyles.boldLabel); + EditorGUI.BeginDisabledGroup(true); + EditorGUILayout.ToggleLeft("LDR (sRGB / UNorm — always enabled)", true); + EditorGUI.EndDisabledGroup(); + DrawFeatureToggle("HDR (Half / Float — *_Hdr shaders & ASTCENC_PRF_HDR)", AstcStripConfig.DefineStripHdr); + } + + EditorGUILayout.Space(4f); + + // 3. Quality Tiers + using (new EditorGUILayout.VerticalScope("box")) + { + EditorGUILayout.LabelField("Quality Tiers (CPU + GPU)", EditorStyles.boldLabel); + for (int i = 0; i < s_QualityOptions.Length; i++) + { + DrawFeatureToggle(s_QualityOptions[i].Label, s_QualityOptions[i].Define); + } + } + + if (enabledBlocks == 0 || enabledQualities == 0) + { + EditorGUILayout.HelpBox( + "Warning: At least one Block Size and one Quality Tier must remain enabled for ASTC encoding to work.", + MessageType.Warning); + } + + EditorGUILayout.Space(6f); + + // Preview active defines + var activeList = new List(); + for (int i = 0; i < AstcComputeShaderStripper.AllStripDefines.Length; i++) + { + string def = AstcComputeShaderStripper.AllStripDefines[i]; + if (m_PendingStripDefines.Contains(def)) + activeList.Add(def); + } + + EditorGUILayout.LabelField("Scripting Define Symbols to Apply", EditorStyles.miniBoldLabel); + using (new EditorGUI.DisabledScope(true)) + { + EditorGUILayout.TextArea(activeList.Count > 0 ? string.Join(";", activeList) : "(none — all features enabled)"); + } + + EditorGUILayout.Space(6f); + + bool dirty = HasUnsavedChanges(); + using (new EditorGUILayout.HorizontalScope()) + { + using (new EditorGUI.DisabledScope(!dirty)) + { + if (GUILayout.Button("Apply to Scripting Define Symbols", GUILayout.Height(28f))) + { + AstcComputeShaderStripper.SetStripDefines(m_TargetGroup, m_PendingStripDefines); + ReloadFromPlayerSettings(); + Debug.Log($"[ASTC Encoder] Updated stripping defines for {m_TargetGroup}: {(activeList.Count > 0 ? string.Join(", ", activeList) : "none")}"); + } + + if (GUILayout.Button("Revert", GUILayout.Width(80f), GUILayout.Height(28f))) + { + ReloadFromPlayerSettings(); + } + } + } + + EditorGUILayout.EndScrollView(); + } + + private void DrawFeatureToggle(string label, string stripDefine) + { + bool currentlyEnabled = !m_PendingStripDefines.Contains(stripDefine); + using (new EditorGUILayout.HorizontalScope()) + { + bool nextEnabled = EditorGUILayout.ToggleLeft(label, currentlyEnabled); + GUILayout.FlexibleSpace(); + EditorGUILayout.LabelField(nextEnabled ? "" : stripDefine, EditorStyles.miniLabel, GUILayout.Width(130f)); + + if (nextEnabled != currentlyEnabled) + { + if (nextEnabled) + m_PendingStripDefines.Remove(stripDefine); + else + m_PendingStripDefines.Add(stripDefine); + } + } + } + } +} diff --git a/Assets/ASTC Encoder/Editor/AstcStrippingSettingsWindow.cs.meta b/Assets/ASTC Encoder/Editor/AstcStrippingSettingsWindow.cs.meta new file mode 100644 index 0000000..13b6a41 --- /dev/null +++ b/Assets/ASTC Encoder/Editor/AstcStrippingSettingsWindow.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 43836de7ed9f4c9380dab12ebf1b53f8 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/ASTC Encoder/Runtime/AstcCompressOptions.cs b/Assets/ASTC Encoder/Runtime/AstcCompressOptions.cs index a3d2e68..f6e52b2 100644 --- a/Assets/ASTC Encoder/Runtime/AstcCompressOptions.cs +++ b/Assets/ASTC Encoder/Runtime/AstcCompressOptions.cs @@ -67,8 +67,8 @@ public struct AstcCompressOptions public static AstcCompressOptions Default => new AstcCompressOptions { TextureType = TextureType.Default, - BlockSize = BlockSize.ASTC_6x6, - Quality = CompressQuality.Fast, + BlockSize = AstcEnumValues.DefaultBlockSize, + Quality = AstcEnumValues.DefaultFastQuality, Backend = EncodeBackend.Auto, ThreadCount = 0, UseAlphaWeight = false, @@ -84,8 +84,8 @@ public struct AstcCompressOptions public static AstcCompressOptions NormalMap => new AstcCompressOptions { TextureType = TextureType.Normal, - BlockSize = BlockSize.ASTC_6x6, - Quality = CompressQuality.Fast, + BlockSize = AstcEnumValues.DefaultBlockSize, + Quality = AstcEnumValues.DefaultFastQuality, Backend = EncodeBackend.Auto, ThreadCount = 0, UseAlphaWeight = false, @@ -101,8 +101,8 @@ public struct AstcCompressOptions public static AstcCompressOptions Transparent => new AstcCompressOptions { TextureType = TextureType.Default, - BlockSize = BlockSize.ASTC_6x6, - Quality = CompressQuality.Fast, + BlockSize = AstcEnumValues.DefaultBlockSize, + Quality = AstcEnumValues.DefaultFastQuality, Backend = EncodeBackend.Auto, ThreadCount = 0, UseAlphaWeight = true, diff --git a/Assets/ASTC Encoder/Runtime/AstcContext.cs b/Assets/ASTC Encoder/Runtime/AstcContext.cs index e013358..ec05998 100644 --- a/Assets/ASTC Encoder/Runtime/AstcContext.cs +++ b/Assets/ASTC Encoder/Runtime/AstcContext.cs @@ -121,6 +121,12 @@ public static AstcContext Create( return null; } + astcenc_profile profile = AstcCpuBackend.GetColorProfile(graphicsFormat, textureType); + if (!AstcStripConfig.ValidateEncodingArgsWithLog(blockSize, quality, profile == astcenc_profile.ASTCENC_PRF_HDR)) + { + return null; + } + if (!Encoder.GetBlockSize(blockSize, out uint blockX, out uint blockY, out uint blockZ)) { Debug.LogError("Unsupported ASTC block size: " + blockSize); @@ -144,7 +150,6 @@ public static AstcContext Create( : threadCount; float qualityValue = AstcCpuBackend.GetQualityValue(quality); - astcenc_profile profile = AstcCpuBackend.GetColorProfile(graphicsFormat, textureType); CodecFlags flags = AstcCpuBackend.GetFlags(textureType, graphicsFormat, useAlphaWeight, usePerceptual); astcenc_config config = new astcenc_config(); @@ -220,7 +225,7 @@ public static AstcContext CreateDecompressor( return null; } - return new AstcContext(context, profile, blockSize, blockX, blockY, CompressQuality.Fastest, flags, resolvedThreads); + return new AstcContext(context, profile, blockSize, blockX, blockY, AstcEnumValues.QualityFastest, flags, resolvedThreads); } public void ThrowIfDisposed() diff --git a/Assets/ASTC Encoder/Runtime/AstcHeader.cs b/Assets/ASTC Encoder/Runtime/AstcHeader.cs index 41a6c23..ccf4df5 100644 --- a/Assets/ASTC Encoder/Runtime/AstcHeader.cs +++ b/Assets/ASTC Encoder/Runtime/AstcHeader.cs @@ -110,12 +110,12 @@ public static bool TryMapBlockSize(uint dimX, uint dimY, uint dimZ, out BlockSiz switch (dimX) { - case 4: blockSize = BlockSize.ASTC_4x4; return true; - case 5: blockSize = BlockSize.ASTC_5x5; return true; - case 6: blockSize = BlockSize.ASTC_6x6; return true; - case 8: blockSize = BlockSize.ASTC_8x8; return true; - case 10: blockSize = BlockSize.ASTC_10x10; return true; - case 12: blockSize = BlockSize.ASTC_12x12; return true; + case 4: blockSize = AstcEnumValues.Block4x4; return true; + case 5: blockSize = AstcEnumValues.Block5x5; return true; + case 6: blockSize = AstcEnumValues.Block6x6; return true; + case 8: blockSize = AstcEnumValues.Block8x8; return true; + case 10: blockSize = AstcEnumValues.Block10x10; return true; + case 12: blockSize = AstcEnumValues.Block12x12; return true; default: return false; } } diff --git a/Assets/ASTC Encoder/Runtime/AstcKtxContainer.cs b/Assets/ASTC Encoder/Runtime/AstcKtxContainer.cs index 29d3fc2..93fddc4 100644 --- a/Assets/ASTC Encoder/Runtime/AstcKtxContainer.cs +++ b/Assets/ASTC Encoder/Runtime/AstcKtxContainer.cs @@ -371,26 +371,26 @@ private static bool TryMapGraphicsFormatToGl(GraphicsFormat format, out uint glI isHdr = false; switch (format) { - case GraphicsFormat.RGBA_ASTC4X4_UNorm: glInternal = k_GlAstc4x4Rgba; blockSize = BlockSize.ASTC_4x4; return true; - case GraphicsFormat.RGBA_ASTC5X5_UNorm: glInternal = k_GlAstc5x5Rgba; blockSize = BlockSize.ASTC_5x5; return true; - case GraphicsFormat.RGBA_ASTC6X6_UNorm: glInternal = k_GlAstc6x6Rgba; blockSize = BlockSize.ASTC_6x6; return true; - case GraphicsFormat.RGBA_ASTC8X8_UNorm: glInternal = k_GlAstc8x8Rgba; blockSize = BlockSize.ASTC_8x8; return true; - case GraphicsFormat.RGBA_ASTC10X10_UNorm: glInternal = k_GlAstc10x10Rgba; blockSize = BlockSize.ASTC_10x10; return true; - case GraphicsFormat.RGBA_ASTC12X12_UNorm: glInternal = k_GlAstc12x12Rgba; blockSize = BlockSize.ASTC_12x12; return true; - - case GraphicsFormat.RGBA_ASTC4X4_SRGB: glInternal = k_GlAstc4x4Srgb; blockSize = BlockSize.ASTC_4x4; return true; - case GraphicsFormat.RGBA_ASTC5X5_SRGB: glInternal = k_GlAstc5x5Srgb; blockSize = BlockSize.ASTC_5x5; return true; - case GraphicsFormat.RGBA_ASTC6X6_SRGB: glInternal = k_GlAstc6x6Srgb; blockSize = BlockSize.ASTC_6x6; return true; - case GraphicsFormat.RGBA_ASTC8X8_SRGB: glInternal = k_GlAstc8x8Srgb; blockSize = BlockSize.ASTC_8x8; return true; - case GraphicsFormat.RGBA_ASTC10X10_SRGB: glInternal = k_GlAstc10x10Srgb; blockSize = BlockSize.ASTC_10x10; return true; - case GraphicsFormat.RGBA_ASTC12X12_SRGB: glInternal = k_GlAstc12x12Srgb; blockSize = BlockSize.ASTC_12x12; return true; - - case GraphicsFormat.RGBA_ASTC4X4_UFloat: glInternal = k_GlAstc4x4Rgba; blockSize = BlockSize.ASTC_4x4; isHdr = true; return true; - case GraphicsFormat.RGBA_ASTC5X5_UFloat: glInternal = k_GlAstc5x5Rgba; blockSize = BlockSize.ASTC_5x5; isHdr = true; return true; - case GraphicsFormat.RGBA_ASTC6X6_UFloat: glInternal = k_GlAstc6x6Rgba; blockSize = BlockSize.ASTC_6x6; isHdr = true; return true; - case GraphicsFormat.RGBA_ASTC8X8_UFloat: glInternal = k_GlAstc8x8Rgba; blockSize = BlockSize.ASTC_8x8; isHdr = true; return true; - case GraphicsFormat.RGBA_ASTC10X10_UFloat: glInternal = k_GlAstc10x10Rgba; blockSize = BlockSize.ASTC_10x10; isHdr = true; return true; - case GraphicsFormat.RGBA_ASTC12X12_UFloat: glInternal = k_GlAstc12x12Rgba; blockSize = BlockSize.ASTC_12x12; isHdr = true; return true; + case GraphicsFormat.RGBA_ASTC4X4_UNorm: glInternal = k_GlAstc4x4Rgba; blockSize = AstcEnumValues.Block4x4; return true; + case GraphicsFormat.RGBA_ASTC5X5_UNorm: glInternal = k_GlAstc5x5Rgba; blockSize = AstcEnumValues.Block5x5; return true; + case GraphicsFormat.RGBA_ASTC6X6_UNorm: glInternal = k_GlAstc6x6Rgba; blockSize = AstcEnumValues.Block6x6; return true; + case GraphicsFormat.RGBA_ASTC8X8_UNorm: glInternal = k_GlAstc8x8Rgba; blockSize = AstcEnumValues.Block8x8; return true; + case GraphicsFormat.RGBA_ASTC10X10_UNorm: glInternal = k_GlAstc10x10Rgba; blockSize = AstcEnumValues.Block10x10; return true; + case GraphicsFormat.RGBA_ASTC12X12_UNorm: glInternal = k_GlAstc12x12Rgba; blockSize = AstcEnumValues.Block12x12; return true; + + case GraphicsFormat.RGBA_ASTC4X4_SRGB: glInternal = k_GlAstc4x4Srgb; blockSize = AstcEnumValues.Block4x4; return true; + case GraphicsFormat.RGBA_ASTC5X5_SRGB: glInternal = k_GlAstc5x5Srgb; blockSize = AstcEnumValues.Block5x5; return true; + case GraphicsFormat.RGBA_ASTC6X6_SRGB: glInternal = k_GlAstc6x6Srgb; blockSize = AstcEnumValues.Block6x6; return true; + case GraphicsFormat.RGBA_ASTC8X8_SRGB: glInternal = k_GlAstc8x8Srgb; blockSize = AstcEnumValues.Block8x8; return true; + case GraphicsFormat.RGBA_ASTC10X10_SRGB: glInternal = k_GlAstc10x10Srgb; blockSize = AstcEnumValues.Block10x10; return true; + case GraphicsFormat.RGBA_ASTC12X12_SRGB: glInternal = k_GlAstc12x12Srgb; blockSize = AstcEnumValues.Block12x12; return true; + + case GraphicsFormat.RGBA_ASTC4X4_UFloat: glInternal = k_GlAstc4x4Rgba; blockSize = AstcEnumValues.Block4x4; isHdr = true; return true; + case GraphicsFormat.RGBA_ASTC5X5_UFloat: glInternal = k_GlAstc5x5Rgba; blockSize = AstcEnumValues.Block5x5; isHdr = true; return true; + case GraphicsFormat.RGBA_ASTC6X6_UFloat: glInternal = k_GlAstc6x6Rgba; blockSize = AstcEnumValues.Block6x6; isHdr = true; return true; + case GraphicsFormat.RGBA_ASTC8X8_UFloat: glInternal = k_GlAstc8x8Rgba; blockSize = AstcEnumValues.Block8x8; isHdr = true; return true; + case GraphicsFormat.RGBA_ASTC10X10_UFloat: glInternal = k_GlAstc10x10Rgba; blockSize = AstcEnumValues.Block10x10; isHdr = true; return true; + case GraphicsFormat.RGBA_ASTC12X12_UFloat: glInternal = k_GlAstc12x12Rgba; blockSize = AstcEnumValues.Block12x12; isHdr = true; return true; default: glInternal = 0; @@ -403,19 +403,19 @@ private static bool TryMapGlToGraphicsFormat(uint glInternal, bool isHdr, out Bl { switch (glInternal) { - case k_GlAstc4x4Rgba: blockSize = BlockSize.ASTC_4x4; format = isHdr ? GraphicsFormat.RGBA_ASTC4X4_UFloat : GraphicsFormat.RGBA_ASTC4X4_UNorm; return true; - case k_GlAstc5x5Rgba: blockSize = BlockSize.ASTC_5x5; format = isHdr ? GraphicsFormat.RGBA_ASTC5X5_UFloat : GraphicsFormat.RGBA_ASTC5X5_UNorm; return true; - case k_GlAstc6x6Rgba: blockSize = BlockSize.ASTC_6x6; format = isHdr ? GraphicsFormat.RGBA_ASTC6X6_UFloat : GraphicsFormat.RGBA_ASTC6X6_UNorm; return true; - case k_GlAstc8x8Rgba: blockSize = BlockSize.ASTC_8x8; format = isHdr ? GraphicsFormat.RGBA_ASTC8X8_UFloat : GraphicsFormat.RGBA_ASTC8X8_UNorm; return true; - case k_GlAstc10x10Rgba: blockSize = BlockSize.ASTC_10x10; format = isHdr ? GraphicsFormat.RGBA_ASTC10X10_UFloat : GraphicsFormat.RGBA_ASTC10X10_UNorm; return true; - case k_GlAstc12x12Rgba: blockSize = BlockSize.ASTC_12x12; format = isHdr ? GraphicsFormat.RGBA_ASTC12X12_UFloat : GraphicsFormat.RGBA_ASTC12X12_UNorm; return true; - - case k_GlAstc4x4Srgb: blockSize = BlockSize.ASTC_4x4; format = GraphicsFormat.RGBA_ASTC4X4_SRGB; return true; - case k_GlAstc5x5Srgb: blockSize = BlockSize.ASTC_5x5; format = GraphicsFormat.RGBA_ASTC5X5_SRGB; return true; - case k_GlAstc6x6Srgb: blockSize = BlockSize.ASTC_6x6; format = GraphicsFormat.RGBA_ASTC6X6_SRGB; return true; - case k_GlAstc8x8Srgb: blockSize = BlockSize.ASTC_8x8; format = GraphicsFormat.RGBA_ASTC8X8_SRGB; return true; - case k_GlAstc10x10Srgb: blockSize = BlockSize.ASTC_10x10; format = GraphicsFormat.RGBA_ASTC10X10_SRGB; return true; - case k_GlAstc12x12Srgb: blockSize = BlockSize.ASTC_12x12; format = GraphicsFormat.RGBA_ASTC12X12_SRGB; return true; + case k_GlAstc4x4Rgba: blockSize = AstcEnumValues.Block4x4; format = isHdr ? GraphicsFormat.RGBA_ASTC4X4_UFloat : GraphicsFormat.RGBA_ASTC4X4_UNorm; return true; + case k_GlAstc5x5Rgba: blockSize = AstcEnumValues.Block5x5; format = isHdr ? GraphicsFormat.RGBA_ASTC5X5_UFloat : GraphicsFormat.RGBA_ASTC5X5_UNorm; return true; + case k_GlAstc6x6Rgba: blockSize = AstcEnumValues.Block6x6; format = isHdr ? GraphicsFormat.RGBA_ASTC6X6_UFloat : GraphicsFormat.RGBA_ASTC6X6_UNorm; return true; + case k_GlAstc8x8Rgba: blockSize = AstcEnumValues.Block8x8; format = isHdr ? GraphicsFormat.RGBA_ASTC8X8_UFloat : GraphicsFormat.RGBA_ASTC8X8_UNorm; return true; + case k_GlAstc10x10Rgba: blockSize = AstcEnumValues.Block10x10; format = isHdr ? GraphicsFormat.RGBA_ASTC10X10_UFloat : GraphicsFormat.RGBA_ASTC10X10_UNorm; return true; + case k_GlAstc12x12Rgba: blockSize = AstcEnumValues.Block12x12; format = isHdr ? GraphicsFormat.RGBA_ASTC12X12_UFloat : GraphicsFormat.RGBA_ASTC12X12_UNorm; return true; + + case k_GlAstc4x4Srgb: blockSize = AstcEnumValues.Block4x4; format = GraphicsFormat.RGBA_ASTC4X4_SRGB; return true; + case k_GlAstc5x5Srgb: blockSize = AstcEnumValues.Block5x5; format = GraphicsFormat.RGBA_ASTC5X5_SRGB; return true; + case k_GlAstc6x6Srgb: blockSize = AstcEnumValues.Block6x6; format = GraphicsFormat.RGBA_ASTC6X6_SRGB; return true; + case k_GlAstc8x8Srgb: blockSize = AstcEnumValues.Block8x8; format = GraphicsFormat.RGBA_ASTC8X8_SRGB; return true; + case k_GlAstc10x10Srgb: blockSize = AstcEnumValues.Block10x10; format = GraphicsFormat.RGBA_ASTC10X10_SRGB; return true; + case k_GlAstc12x12Srgb: blockSize = AstcEnumValues.Block12x12; format = GraphicsFormat.RGBA_ASTC12X12_SRGB; return true; default: blockSize = BlockSize.ASTC_None; diff --git a/Assets/ASTC Encoder/Runtime/AstcStripConfig.cs b/Assets/ASTC Encoder/Runtime/AstcStripConfig.cs new file mode 100644 index 0000000..d71425c --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/AstcStripConfig.cs @@ -0,0 +1,227 @@ +using UnityEngine; + +namespace Aperture.Astcenc.Runtime +{ + /// + /// Internal raw enum constants so runtime internals can reference all enum values cleanly + /// without triggering CS0619 when any ASTC_STRIP_* define marks a public enum member obsolete. + /// + internal static class AstcEnumValues + { + internal const BlockSize Block4x4 = (BlockSize)1; + internal const BlockSize Block5x5 = (BlockSize)2; + internal const BlockSize Block6x6 = (BlockSize)3; + internal const BlockSize Block8x8 = (BlockSize)4; + internal const BlockSize Block10x10 = (BlockSize)5; + internal const BlockSize Block12x12 = (BlockSize)6; + + internal const CompressQuality QualityFastest = (CompressQuality)0; + internal const CompressQuality QualityFast = (CompressQuality)1; + internal const CompressQuality QualityMedium = (CompressQuality)2; + internal const CompressQuality QualityThorough = (CompressQuality)3; + internal const CompressQuality QualityVeryThorough = (CompressQuality)4; + internal const CompressQuality QualityExhaustive = (CompressQuality)5; + + // Default parameter fallbacks: pick the first non-stripped value so public API signatures + // always compile cleanly under any combination of ASTC_STRIP_* defines. +#if !ASTC_STRIP_6X6 + internal const BlockSize DefaultBlockSize = Block6x6; +#elif !ASTC_STRIP_4X4 + internal const BlockSize DefaultBlockSize = Block4x4; +#elif !ASTC_STRIP_5X5 + internal const BlockSize DefaultBlockSize = Block5x5; +#elif !ASTC_STRIP_8X8 + internal const BlockSize DefaultBlockSize = Block8x8; +#elif !ASTC_STRIP_10X10 + internal const BlockSize DefaultBlockSize = Block10x10; +#elif !ASTC_STRIP_12X12 + internal const BlockSize DefaultBlockSize = Block12x12; +#else + internal const BlockSize DefaultBlockSize = BlockSize.ASTC_None; +#endif + +#if !ASTC_STRIP_FAST + internal const CompressQuality DefaultFastQuality = QualityFast; +#elif !ASTC_STRIP_MED + internal const CompressQuality DefaultFastQuality = QualityMedium; +#elif !ASTC_STRIP_FULL + internal const CompressQuality DefaultFastQuality = QualityThorough; +#else + internal const CompressQuality DefaultFastQuality = QualityFast; +#endif + +#if !ASTC_STRIP_MED + internal const CompressQuality DefaultMediumQuality = QualityMedium; +#elif !ASTC_STRIP_FAST + internal const CompressQuality DefaultMediumQuality = QualityFast; +#elif !ASTC_STRIP_FULL + internal const CompressQuality DefaultMediumQuality = QualityThorough; +#else + internal const CompressQuality DefaultMediumQuality = QualityMedium; +#endif + } + + /// + /// Unified CPU + GPU feature stripping configuration driven by Scripting Define Symbols + /// (ASTC_STRIP_4X4.. ASTC_STRIP_12X12, ASTC_STRIP_HDR, + /// ASTC_STRIP_FAST, ASTC_STRIP_MED, ASTC_STRIP_FULL). + /// The same defines control C# compile-time enum checks, runtime CPU/GPU validation, + /// and build-time ComputeShader variant stripping. + /// + public static class AstcStripConfig + { + public const string DefineStrip4x4 = "ASTC_STRIP_4X4"; + public const string DefineStrip5x5 = "ASTC_STRIP_5X5"; + public const string DefineStrip6x6 = "ASTC_STRIP_6X6"; + public const string DefineStrip8x8 = "ASTC_STRIP_8X8"; + public const string DefineStrip10x10 = "ASTC_STRIP_10X10"; + public const string DefineStrip12x12 = "ASTC_STRIP_12X12"; + public const string DefineStripHdr = "ASTC_STRIP_HDR"; + public const string DefineStripFast = "ASTC_STRIP_FAST"; + public const string DefineStripMed = "ASTC_STRIP_MED"; + public const string DefineStripFull = "ASTC_STRIP_FULL"; + +#if ASTC_STRIP_HDR + public const bool IsHdrEnabled = false; +#else + public const bool IsHdrEnabled = true; +#endif + +#if ASTC_STRIP_FAST + public const bool IsFastQualityEnabled = false; +#else + public const bool IsFastQualityEnabled = true; +#endif + +#if ASTC_STRIP_MED + public const bool IsMediumQualityEnabled = false; +#else + public const bool IsMediumQualityEnabled = true; +#endif + +#if ASTC_STRIP_FULL + public const bool IsFullQualityEnabled = false; +#else + public const bool IsFullQualityEnabled = true; +#endif + + public static bool IsBlockSizeEnabled(BlockSize blockSize) + { + switch (blockSize) + { +#if !ASTC_STRIP_4X4 + case AstcEnumValues.Block4x4: return true; +#endif +#if !ASTC_STRIP_5X5 + case AstcEnumValues.Block5x5: return true; +#endif +#if !ASTC_STRIP_6X6 + case AstcEnumValues.Block6x6: return true; +#endif +#if !ASTC_STRIP_8X8 + case AstcEnumValues.Block8x8: return true; +#endif +#if !ASTC_STRIP_10X10 + case AstcEnumValues.Block10x10: return true; +#endif +#if !ASTC_STRIP_12X12 + case AstcEnumValues.Block12x12: return true; +#endif + default: return false; + } + } + + public static bool IsQualityEnabled(CompressQuality quality) + { + switch (quality) + { + case AstcEnumValues.QualityFastest: + case AstcEnumValues.QualityFast: + return IsFastQualityEnabled; + case AstcEnumValues.QualityMedium: + return IsMediumQualityEnabled; + case AstcEnumValues.QualityThorough: + case AstcEnumValues.QualityVeryThorough: + case AstcEnumValues.QualityExhaustive: + return IsFullQualityEnabled; + default: + return false; + } + } + + public static bool IsQualityTierEnabled(uint tier) + { + return tier == 0u ? IsFastQualityEnabled + : tier == 1u ? IsMediumQualityEnabled + : IsFullQualityEnabled; + } + + internal static string GetStripDefineForBlockSize(BlockSize blockSize) + { + return blockSize switch + { + AstcEnumValues.Block4x4 => DefineStrip4x4, + AstcEnumValues.Block5x5 => DefineStrip5x5, + AstcEnumValues.Block6x6 => DefineStrip6x6, + AstcEnumValues.Block8x8 => DefineStrip8x8, + AstcEnumValues.Block10x10 => DefineStrip10x10, + AstcEnumValues.Block12x12 => DefineStrip12x12, + _ => null, + }; + } + + internal static string GetStripDefineForQuality(CompressQuality quality) + { + return quality switch + { + AstcEnumValues.QualityFastest => DefineStripFast, + AstcEnumValues.QualityFast => DefineStripFast, + AstcEnumValues.QualityMedium => DefineStripMed, + AstcEnumValues.QualityThorough => DefineStripFull, + AstcEnumValues.QualityVeryThorough => DefineStripFull, + AstcEnumValues.QualityExhaustive => DefineStripFull, + _ => null, + }; + } + + internal static bool ValidateEncodingArgs(BlockSize blockSize, CompressQuality quality, bool isHdr, out string error) + { + if (!IsBlockSizeEnabled(blockSize)) + { + string def = GetStripDefineForBlockSize(blockSize); + error = def != null + ? $"ASTC block size '{blockSize}' is disabled by Scripting Define Symbol '{def}'." + : $"Unsupported ASTC block size: '{blockSize}'."; + return false; + } + + if (!IsQualityEnabled(quality)) + { + string def = GetStripDefineForQuality(quality); + error = def != null + ? $"ASTC quality '{quality}' is disabled by Scripting Define Symbol '{def}'." + : $"Unsupported ASTC quality: '{quality}'."; + return false; + } + + if (isHdr && !IsHdrEnabled) + { + error = $"ASTC HDR encoding is disabled by Scripting Define Symbol '{DefineStripHdr}'."; + return false; + } + + error = null; + return true; + } + + internal static bool ValidateEncodingArgsWithLog(BlockSize blockSize, CompressQuality quality, bool isHdr) + { + if (!ValidateEncodingArgs(blockSize, quality, isHdr, out string error)) + { + Debug.LogError(error); + return false; + } + return true; + } + } +} diff --git a/Assets/ASTC Encoder/Runtime/AstcStripConfig.cs.meta b/Assets/ASTC Encoder/Runtime/AstcStripConfig.cs.meta new file mode 100644 index 0000000..dff74a9 --- /dev/null +++ b/Assets/ASTC Encoder/Runtime/AstcStripConfig.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7d483ee2bfa4403f90d33786be90621e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/ASTC Encoder/Runtime/BlockSize.cs b/Assets/ASTC Encoder/Runtime/BlockSize.cs index edf76fd..966fbef 100644 --- a/Assets/ASTC Encoder/Runtime/BlockSize.cs +++ b/Assets/ASTC Encoder/Runtime/BlockSize.cs @@ -1,25 +1,44 @@ +using System; namespace Aperture.Astcenc.Runtime { public enum BlockSize { - ASTC_None, - //2D block sizes - ASTC_4x4, + ASTC_None = 0, + // 2D block sizes +#if ASTC_STRIP_4X4 + [Obsolete("BlockSize.ASTC_4x4 is disabled by ASTC_STRIP_4X4.", error: true)] +#endif + ASTC_4x4 = 1, //ASTC_5x4, - ASTC_5x5, +#if ASTC_STRIP_5X5 + [Obsolete("BlockSize.ASTC_5x5 is disabled by ASTC_STRIP_5X5.", error: true)] +#endif + ASTC_5x5 = 2, //ASTC_6x5, - ASTC_6x6, +#if ASTC_STRIP_6X6 + [Obsolete("BlockSize.ASTC_6x6 is disabled by ASTC_STRIP_6X6.", error: true)] +#endif + ASTC_6x6 = 3, //ASTC_8x5, //ASTC_8x6, //ASTC_10x5, //ASTC_10x6, - ASTC_8x8, +#if ASTC_STRIP_8X8 + [Obsolete("BlockSize.ASTC_8x8 is disabled by ASTC_STRIP_8X8.", error: true)] +#endif + ASTC_8x8 = 4, //ASTC_10x8, - ASTC_10x10, +#if ASTC_STRIP_10X10 + [Obsolete("BlockSize.ASTC_10x10 is disabled by ASTC_STRIP_10X10.", error: true)] +#endif + ASTC_10x10 = 5, //ASTC_12x10, - ASTC_12x12, - //3D block sizes +#if ASTC_STRIP_12X12 + [Obsolete("BlockSize.ASTC_12x12 is disabled by ASTC_STRIP_12X12.", error: true)] +#endif + ASTC_12x12 = 6, + // 3D block sizes //ASTC_3x3x3, //ASTC_4x3x3, //ASTC_4x4x3, diff --git a/Assets/ASTC Encoder/Runtime/CompressQuality.cs b/Assets/ASTC Encoder/Runtime/CompressQuality.cs index 6da513c..35eed91 100644 --- a/Assets/ASTC Encoder/Runtime/CompressQuality.cs +++ b/Assets/ASTC Encoder/Runtime/CompressQuality.cs @@ -1,13 +1,32 @@ +using System; + namespace Aperture.Astcenc.Runtime { - public enum CompressQuality { - Fastest, - Fast, - Medium, - Thorough, - VeryThorough, - Exhaustive, +#if ASTC_STRIP_FAST + [Obsolete("CompressQuality.Fastest is disabled by ASTC_STRIP_FAST.", error: true)] +#endif + Fastest = 0, +#if ASTC_STRIP_FAST + [Obsolete("CompressQuality.Fast is disabled by ASTC_STRIP_FAST.", error: true)] +#endif + Fast = 1, +#if ASTC_STRIP_MED + [Obsolete("CompressQuality.Medium is disabled by ASTC_STRIP_MED.", error: true)] +#endif + Medium = 2, +#if ASTC_STRIP_FULL + [Obsolete("CompressQuality.Thorough is disabled by ASTC_STRIP_FULL.", error: true)] +#endif + Thorough = 3, +#if ASTC_STRIP_FULL + [Obsolete("CompressQuality.VeryThorough is disabled by ASTC_STRIP_FULL.", error: true)] +#endif + VeryThorough = 4, +#if ASTC_STRIP_FULL + [Obsolete("CompressQuality.Exhaustive is disabled by ASTC_STRIP_FULL.", error: true)] +#endif + Exhaustive = 5, } } \ No newline at end of file diff --git a/Assets/ASTC Encoder/Runtime/Cpu/AstcCpuBackend.cs b/Assets/ASTC Encoder/Runtime/Cpu/AstcCpuBackend.cs index 58d10ba..11440f4 100644 --- a/Assets/ASTC Encoder/Runtime/Cpu/AstcCpuBackend.cs +++ b/Assets/ASTC Encoder/Runtime/Cpu/AstcCpuBackend.cs @@ -68,12 +68,12 @@ internal static float GetQualityValue(CompressQuality quality) { return quality switch { - CompressQuality.Fastest => 0.0f, - CompressQuality.Fast => 10.0f, - CompressQuality.Medium => 60.0f, - CompressQuality.Thorough => 98.0f, - CompressQuality.VeryThorough => 99.0f, - CompressQuality.Exhaustive => 100.0f, + AstcEnumValues.QualityFastest => 0.0f, + AstcEnumValues.QualityFast => 10.0f, + AstcEnumValues.QualityMedium => 60.0f, + AstcEnumValues.QualityThorough => 98.0f, + AstcEnumValues.QualityVeryThorough => 99.0f, + AstcEnumValues.QualityExhaustive => 100.0f, _ => throw new ArgumentException("Unknown type of a CompressQuality:", nameof(quality)), }; } diff --git a/Assets/ASTC Encoder/Runtime/Encoder.cs b/Assets/ASTC Encoder/Runtime/Encoder.cs index 5a13776..63ea5e9 100644 --- a/Assets/ASTC Encoder/Runtime/Encoder.cs +++ b/Assets/ASTC Encoder/Runtime/Encoder.cs @@ -14,27 +14,27 @@ internal static bool GetBlockSize(BlockSize size, out uint x, out uint y, out ui { switch (size) { - case BlockSize.ASTC_4x4: + case AstcEnumValues.Block4x4: x = y = 4; z = 1; return true; - case BlockSize.ASTC_5x5: + case AstcEnumValues.Block5x5: x = y = 5; z = 1; return true; - case BlockSize.ASTC_6x6: + case AstcEnumValues.Block6x6: x = y = 6; z = 1; return true; - case BlockSize.ASTC_8x8: + case AstcEnumValues.Block8x8: x = y = 8; z = 1; return true; - case BlockSize.ASTC_10x10: + case AstcEnumValues.Block10x10: x = y = 10; z = 1; return true; - case BlockSize.ASTC_12x12: + case AstcEnumValues.Block12x12: x = y = 12; z = 1; return true; @@ -61,17 +61,17 @@ internal static GraphicsFormat FitAstcGraphicsFormat(GraphicsFormat graphicsForm { switch (blockSize) { - case BlockSize.ASTC_4x4: + case AstcEnumValues.Block4x4: return GraphicsFormat.RGBA_ASTC4X4_SRGB; - case BlockSize.ASTC_5x5: + case AstcEnumValues.Block5x5: return GraphicsFormat.RGBA_ASTC5X5_SRGB; - case BlockSize.ASTC_6x6: + case AstcEnumValues.Block6x6: return GraphicsFormat.RGBA_ASTC6X6_SRGB; - case BlockSize.ASTC_8x8: + case AstcEnumValues.Block8x8: return GraphicsFormat.RGBA_ASTC8X8_SRGB; - case BlockSize.ASTC_10x10: + case AstcEnumValues.Block10x10: return GraphicsFormat.RGBA_ASTC10X10_SRGB; - case BlockSize.ASTC_12x12: + case AstcEnumValues.Block12x12: return GraphicsFormat.RGBA_ASTC12X12_SRGB; } } @@ -89,17 +89,17 @@ internal static GraphicsFormat FitAstcGraphicsFormat(GraphicsFormat graphicsForm case GraphicsFormat.R16G16B16A16_UNorm: switch (blockSize) { - case BlockSize.ASTC_4x4: + case AstcEnumValues.Block4x4: return GraphicsFormat.RGBA_ASTC4X4_UNorm; - case BlockSize.ASTC_5x5: + case AstcEnumValues.Block5x5: return GraphicsFormat.RGBA_ASTC5X5_UNorm; - case BlockSize.ASTC_6x6: + case AstcEnumValues.Block6x6: return GraphicsFormat.RGBA_ASTC6X6_UNorm; - case BlockSize.ASTC_8x8: + case AstcEnumValues.Block8x8: return GraphicsFormat.RGBA_ASTC8X8_UNorm; - case BlockSize.ASTC_10x10: + case AstcEnumValues.Block10x10: return GraphicsFormat.RGBA_ASTC10X10_UNorm; - case BlockSize.ASTC_12x12: + case AstcEnumValues.Block12x12: return GraphicsFormat.RGBA_ASTC12X12_UNorm; } break; @@ -118,17 +118,17 @@ internal static GraphicsFormat FitAstcGraphicsFormat(GraphicsFormat graphicsForm } switch (blockSize) { - case BlockSize.ASTC_4x4: + case AstcEnumValues.Block4x4: return GraphicsFormat.RGBA_ASTC4X4_UFloat; - case BlockSize.ASTC_5x5: + case AstcEnumValues.Block5x5: return GraphicsFormat.RGBA_ASTC5X5_UFloat; - case BlockSize.ASTC_6x6: + case AstcEnumValues.Block6x6: return GraphicsFormat.RGBA_ASTC6X6_UFloat; - case BlockSize.ASTC_8x8: + case AstcEnumValues.Block8x8: return GraphicsFormat.RGBA_ASTC8X8_UFloat; - case BlockSize.ASTC_10x10: + case AstcEnumValues.Block10x10: return GraphicsFormat.RGBA_ASTC10X10_UFloat; - case BlockSize.ASTC_12x12: + case AstcEnumValues.Block12x12: return GraphicsFormat.RGBA_ASTC12X12_UFloat; } break; @@ -177,7 +177,7 @@ public static bool SupportsGpuDirectCopy(BlockSize blockSize, bool srgb = false, return AstcGpuBackend.SupportsDirectCopy(blockSize, srgb, isHdr); } - public static void PreloadGpu(BlockSize blockSize = BlockSize.ASTC_6x6) + public static void PreloadGpu(BlockSize blockSize = AstcEnumValues.DefaultBlockSize) { AstcGpuBackend.Preload(blockSize); } @@ -271,7 +271,7 @@ public static AstcRaw CompressTexture(this Texture inputTexture, AstcCompressOpt /// /// Compresses a RenderTexture on the GPU and returns raw ASTC bytes. Call Dispose() on the returned AstcRaw after use. /// - public static AstcRaw CompressTexture(this RenderTexture inputTexture, TextureType textureType = TextureType.Default, BlockSize blockSize = BlockSize.ASTC_6x6, CompressQuality quality = CompressQuality.Fast, bool includeMipmaps = true, bool useAlphaWeight = false) + public static AstcRaw CompressTexture(this RenderTexture inputTexture, TextureType textureType = TextureType.Default, BlockSize blockSize = AstcEnumValues.DefaultBlockSize, CompressQuality quality = AstcEnumValues.DefaultFastQuality, bool includeMipmaps = true, bool useAlphaWeight = false) { return AstcGpuBackend.CompressTextureSync(inputTexture, textureType, blockSize, quality, includeMipmaps, useAlphaWeight); } @@ -280,7 +280,7 @@ public static AstcRaw CompressTexture(this RenderTexture inputTexture, TextureTy /// Compresses a Texture2D asynchronously on GPU AsyncGPUReadback or CPU background thread(s) depending on backend. /// You should call Dispose() on the returned AstcRaw after use. /// - public static async Task CompressTextureAsync(this Texture2D inputTexture, TextureType textureType = TextureType.Default, BlockSize blockSize = BlockSize.ASTC_6x6, CompressQuality quality = CompressQuality.Fast, int threadCount = 0, bool useAlphaWeight = false, CancellationToken cancellationToken = default, EncodeBackend backend = EncodeBackend.Auto) + public static async Task CompressTextureAsync(this Texture2D inputTexture, TextureType textureType = TextureType.Default, BlockSize blockSize = AstcEnumValues.DefaultBlockSize, CompressQuality quality = AstcEnumValues.DefaultFastQuality, int threadCount = 0, bool useAlphaWeight = false, CancellationToken cancellationToken = default, EncodeBackend backend = EncodeBackend.Auto) { if (cancellationToken.IsCancellationRequested) { @@ -334,7 +334,7 @@ public static async Task CompressTextureAsync(this Texture inputTexture /// Compresses a RenderTexture asynchronously on the GPU using AsyncGPUReadback without stalling the main thread. /// Call Dispose() on the returned AstcRaw after use. /// - public static Task CompressTextureAsync(this RenderTexture inputTexture, TextureType textureType = TextureType.Default, BlockSize blockSize = BlockSize.ASTC_6x6, CompressQuality quality = CompressQuality.Fast, bool includeMipmaps = true, bool useAlphaWeight = false, CancellationToken cancellationToken = default) + public static Task CompressTextureAsync(this RenderTexture inputTexture, TextureType textureType = TextureType.Default, BlockSize blockSize = AstcEnumValues.DefaultBlockSize, CompressQuality quality = AstcEnumValues.DefaultFastQuality, bool includeMipmaps = true, bool useAlphaWeight = false, CancellationToken cancellationToken = default) { return AstcGpuBackend.CompressTextureAsync(inputTexture, textureType, blockSize, quality, includeMipmaps, useAlphaWeight, cancellationToken); } @@ -362,7 +362,7 @@ public static void LoadRawTextureData(this Texture2D inputTexture, AstcRaw raw) /// /// Compress texture in-place at runtime to ASTC format. /// - public static bool Compress(this Texture2D inputTexture, TextureType textureType = TextureType.Default, BlockSize blockSize = BlockSize.ASTC_6x6, CompressQuality quality = CompressQuality.Fast, bool makeNoLongerReadable = true, int threadCount = 1, bool useAlphaWeight = false, EncodeBackend backend = EncodeBackend.Auto) + public static bool Compress(this Texture2D inputTexture, TextureType textureType = TextureType.Default, BlockSize blockSize = AstcEnumValues.DefaultBlockSize, CompressQuality quality = AstcEnumValues.DefaultFastQuality, bool makeNoLongerReadable = true, int threadCount = 1, bool useAlphaWeight = false, EncodeBackend backend = EncodeBackend.Auto) { if (ShouldUseGpu(inputTexture, backend)) { @@ -402,8 +402,8 @@ public static bool Compress(this Texture2D inputTexture, AstcCompressOptions opt public static Texture2D CompressToTexture( this Texture source, TextureType textureType = TextureType.Default, - BlockSize blockSize = BlockSize.ASTC_6x6, - CompressQuality quality = CompressQuality.Fast, + BlockSize blockSize = AstcEnumValues.DefaultBlockSize, + CompressQuality quality = AstcEnumValues.DefaultFastQuality, bool includeMipmaps = true, bool useAlphaWeight = false, EncodeBackend backend = EncodeBackend.Auto) @@ -465,8 +465,8 @@ public static Texture2D CompressToTexture(this Texture source, AstcCompressOptio public static async Task CompressToTextureAsync( this Texture source, TextureType textureType = TextureType.Default, - BlockSize blockSize = BlockSize.ASTC_6x6, - CompressQuality quality = CompressQuality.Fast, + BlockSize blockSize = AstcEnumValues.DefaultBlockSize, + CompressQuality quality = AstcEnumValues.DefaultFastQuality, bool includeMipmaps = true, bool useAlphaWeight = false, int threadCount = 0, @@ -548,8 +548,8 @@ public static void Encode( Texture source, Texture destination, TextureType textureType = TextureType.Default, - BlockSize blockSize = BlockSize.ASTC_6x6, - CompressQuality quality = CompressQuality.Fast, + BlockSize blockSize = AstcEnumValues.DefaultBlockSize, + CompressQuality quality = AstcEnumValues.DefaultFastQuality, int sourceMip = 0, int destMip = 0, int destX = 0, @@ -755,7 +755,7 @@ public static unsafe Task CompressRawPixelsAsync( /// /// Encode any 2D Texture (Texture2D or RenderTexture) to ASTC file bytes with a 16-byte .astc header (mip 0 only). /// - public static byte[] EncodeToASTC(this Texture inputTexture, TextureType textureType = TextureType.Default, BlockSize blockSize = BlockSize.ASTC_6x6, CompressQuality quality = CompressQuality.Medium, int threadCount = 1, bool useAlphaWeight = false, EncodeBackend backend = EncodeBackend.Auto) + public static byte[] EncodeToASTC(this Texture inputTexture, TextureType textureType = TextureType.Default, BlockSize blockSize = AstcEnumValues.DefaultBlockSize, CompressQuality quality = AstcEnumValues.DefaultMediumQuality, int threadCount = 1, bool useAlphaWeight = false, EncodeBackend backend = EncodeBackend.Auto) { if (inputTexture == null) { @@ -832,8 +832,8 @@ public static byte[] EncodeToASTC(this Texture inputTexture, AstcCompressOptions public static async Task EncodeToASTCAsync( this Texture inputTexture, TextureType textureType = TextureType.Default, - BlockSize blockSize = BlockSize.ASTC_6x6, - CompressQuality quality = CompressQuality.Medium, + BlockSize blockSize = AstcEnumValues.DefaultBlockSize, + CompressQuality quality = AstcEnumValues.DefaultMediumQuality, int threadCount = 0, bool useAlphaWeight = false, CancellationToken cancellationToken = default, diff --git a/Assets/ASTC Encoder/Runtime/Gpu/AstcGpuBackend.cs b/Assets/ASTC Encoder/Runtime/Gpu/AstcGpuBackend.cs index 7cd47e2..550b1f3 100644 --- a/Assets/ASTC Encoder/Runtime/Gpu/AstcGpuBackend.cs +++ b/Assets/ASTC Encoder/Runtime/Gpu/AstcGpuBackend.cs @@ -19,22 +19,70 @@ internal static class AstcGpuBackend private static readonly string[] s_LdrShaderNames = { +#if ASTC_STRIP_4X4 + null, +#else "AstcGpuEncoder_4x4", +#endif +#if ASTC_STRIP_5X5 + null, +#else "AstcGpuEncoder_5x5", +#endif +#if ASTC_STRIP_6X6 + null, +#else "AstcGpuEncoder_6x6", +#endif +#if ASTC_STRIP_8X8 + null, +#else "AstcGpuEncoder_8x8", +#endif +#if ASTC_STRIP_10X10 + null, +#else "AstcGpuEncoder_10x10", +#endif +#if ASTC_STRIP_12X12 + null, +#else "AstcGpuEncoder_12x12", +#endif }; private static readonly string[] s_HdrShaderNames = { +#if ASTC_STRIP_HDR || ASTC_STRIP_4X4 + null, +#else "AstcGpuEncoder_4x4_Hdr", +#endif +#if ASTC_STRIP_HDR || ASTC_STRIP_5X5 + null, +#else "AstcGpuEncoder_5x5_Hdr", +#endif +#if ASTC_STRIP_HDR || ASTC_STRIP_6X6 + null, +#else "AstcGpuEncoder_6x6_Hdr", +#endif +#if ASTC_STRIP_HDR || ASTC_STRIP_8X8 + null, +#else "AstcGpuEncoder_8x8_Hdr", +#endif +#if ASTC_STRIP_HDR || ASTC_STRIP_10X10 + null, +#else "AstcGpuEncoder_10x10_Hdr", +#endif +#if ASTC_STRIP_HDR || ASTC_STRIP_12X12 + null, +#else "AstcGpuEncoder_12x12_Hdr", +#endif }; private const uint k_ModeLdrRgb = 0u; @@ -247,12 +295,12 @@ internal static GraphicsFormat GetAstcGraphicsFormat(BlockSize blockSize, bool s { return blockSize switch { - BlockSize.ASTC_4x4 => GraphicsFormat.RGBA_ASTC4X4_UFloat, - BlockSize.ASTC_5x5 => GraphicsFormat.RGBA_ASTC5X5_UFloat, - BlockSize.ASTC_6x6 => GraphicsFormat.RGBA_ASTC6X6_UFloat, - BlockSize.ASTC_8x8 => GraphicsFormat.RGBA_ASTC8X8_UFloat, - BlockSize.ASTC_10x10 => GraphicsFormat.RGBA_ASTC10X10_UFloat, - BlockSize.ASTC_12x12 => GraphicsFormat.RGBA_ASTC12X12_UFloat, + AstcEnumValues.Block4x4 => GraphicsFormat.RGBA_ASTC4X4_UFloat, + AstcEnumValues.Block5x5 => GraphicsFormat.RGBA_ASTC5X5_UFloat, + AstcEnumValues.Block6x6 => GraphicsFormat.RGBA_ASTC6X6_UFloat, + AstcEnumValues.Block8x8 => GraphicsFormat.RGBA_ASTC8X8_UFloat, + AstcEnumValues.Block10x10 => GraphicsFormat.RGBA_ASTC10X10_UFloat, + AstcEnumValues.Block12x12 => GraphicsFormat.RGBA_ASTC12X12_UFloat, _ => throw new ArgumentException($"Unsupported ASTC block size: {blockSize}", nameof(blockSize)), }; } @@ -261,24 +309,24 @@ internal static GraphicsFormat GetAstcGraphicsFormat(BlockSize blockSize, bool s { return blockSize switch { - BlockSize.ASTC_4x4 => GraphicsFormat.RGBA_ASTC4X4_SRGB, - BlockSize.ASTC_5x5 => GraphicsFormat.RGBA_ASTC5X5_SRGB, - BlockSize.ASTC_6x6 => GraphicsFormat.RGBA_ASTC6X6_SRGB, - BlockSize.ASTC_8x8 => GraphicsFormat.RGBA_ASTC8X8_SRGB, - BlockSize.ASTC_10x10 => GraphicsFormat.RGBA_ASTC10X10_SRGB, - BlockSize.ASTC_12x12 => GraphicsFormat.RGBA_ASTC12X12_SRGB, + AstcEnumValues.Block4x4 => GraphicsFormat.RGBA_ASTC4X4_SRGB, + AstcEnumValues.Block5x5 => GraphicsFormat.RGBA_ASTC5X5_SRGB, + AstcEnumValues.Block6x6 => GraphicsFormat.RGBA_ASTC6X6_SRGB, + AstcEnumValues.Block8x8 => GraphicsFormat.RGBA_ASTC8X8_SRGB, + AstcEnumValues.Block10x10 => GraphicsFormat.RGBA_ASTC10X10_SRGB, + AstcEnumValues.Block12x12 => GraphicsFormat.RGBA_ASTC12X12_SRGB, _ => throw new ArgumentException($"Unsupported ASTC block size: {blockSize}", nameof(blockSize)), }; } return blockSize switch { - BlockSize.ASTC_4x4 => GraphicsFormat.RGBA_ASTC4X4_UNorm, - BlockSize.ASTC_5x5 => GraphicsFormat.RGBA_ASTC5X5_UNorm, - BlockSize.ASTC_6x6 => GraphicsFormat.RGBA_ASTC6X6_UNorm, - BlockSize.ASTC_8x8 => GraphicsFormat.RGBA_ASTC8X8_UNorm, - BlockSize.ASTC_10x10 => GraphicsFormat.RGBA_ASTC10X10_UNorm, - BlockSize.ASTC_12x12 => GraphicsFormat.RGBA_ASTC12X12_UNorm, + AstcEnumValues.Block4x4 => GraphicsFormat.RGBA_ASTC4X4_UNorm, + AstcEnumValues.Block5x5 => GraphicsFormat.RGBA_ASTC5X5_UNorm, + AstcEnumValues.Block6x6 => GraphicsFormat.RGBA_ASTC6X6_UNorm, + AstcEnumValues.Block8x8 => GraphicsFormat.RGBA_ASTC8X8_UNorm, + AstcEnumValues.Block10x10 => GraphicsFormat.RGBA_ASTC10X10_UNorm, + AstcEnumValues.Block12x12 => GraphicsFormat.RGBA_ASTC12X12_UNorm, _ => throw new ArgumentException($"Unsupported ASTC block size: {blockSize}", nameof(blockSize)), }; } @@ -287,33 +335,47 @@ private static int BlockSizeToSlot(BlockSize blockSize) { return blockSize switch { - BlockSize.ASTC_4x4 => 0, - BlockSize.ASTC_5x5 => 1, - BlockSize.ASTC_6x6 => 2, - BlockSize.ASTC_8x8 => 3, - BlockSize.ASTC_10x10 => 4, - BlockSize.ASTC_12x12 => 5, + AstcEnumValues.Block4x4 => 0, + AstcEnumValues.Block5x5 => 1, + AstcEnumValues.Block6x6 => 2, + AstcEnumValues.Block8x8 => 3, + AstcEnumValues.Block10x10 => 4, + AstcEnumValues.Block12x12 => 5, _ => -1, }; } + private static BlockSize SlotToBlockSize(int slot) + { + return slot switch + { + 0 => AstcEnumValues.Block4x4, + 1 => AstcEnumValues.Block5x5, + 2 => AstcEnumValues.Block6x6, + 3 => AstcEnumValues.Block8x8, + 4 => AstcEnumValues.Block10x10, + 5 => AstcEnumValues.Block12x12, + _ => BlockSize.ASTC_None, + }; + } + private static uint MapQualityTier(CompressQuality quality) { return quality switch { - CompressQuality.Fastest => 0u, - CompressQuality.Fast => 0u, - CompressQuality.Medium => 1u, - CompressQuality.Thorough => 2u, - CompressQuality.VeryThorough => 2u, - CompressQuality.Exhaustive => 2u, + AstcEnumValues.QualityFastest => 0u, + AstcEnumValues.QualityFast => 0u, + AstcEnumValues.QualityMedium => 1u, + AstcEnumValues.QualityThorough => 2u, + AstcEnumValues.QualityVeryThorough => 2u, + AstcEnumValues.QualityExhaustive => 2u, _ => 0u, }; } private static int GetDispatchGroupsY(int blockCountY, BlockSize blockSize) { - return (blockSize == BlockSize.ASTC_8x8 || blockSize == BlockSize.ASTC_10x10 || blockSize == BlockSize.ASTC_12x12) + return (blockSize == AstcEnumValues.Block8x8 || blockSize == AstcEnumValues.Block10x10 || blockSize == AstcEnumValues.Block12x12) ? (blockCountY + 3) >> 2 : (blockCountY + 7) >> 3; } @@ -345,6 +407,29 @@ private static bool TryGetEncoderShader(int slot, bool isHdr, uint qualityTier, if (slot < 0 || slot >= 6) return false; + BlockSize blockSize = SlotToBlockSize(slot); + if (!AstcStripConfig.IsBlockSizeEnabled(blockSize)) + { + string def = AstcStripConfig.GetStripDefineForBlockSize(blockSize); + Debug.LogError($"ASTC block size '{blockSize}' is disabled by Scripting Define Symbol '{def}'."); + return false; + } + + if (isHdr && !AstcStripConfig.IsHdrEnabled) + { + Debug.LogError($"ASTC HDR encoding is disabled by Scripting Define Symbol '{AstcStripConfig.DefineStripHdr}'."); + return false; + } + + if (!AstcStripConfig.IsQualityTierEnabled(qualityTier)) + { + string def = qualityTier == 0u ? AstcStripConfig.DefineStripFast + : qualityTier == 1u ? AstcStripConfig.DefineStripMed + : AstcStripConfig.DefineStripFull; + Debug.LogError($"ASTC quality tier {qualityTier} is disabled by Scripting Define Symbol '{def}'."); + return false; + } + ComputeShader[] shaders = isHdr ? s_HdrShaders : s_LdrShaders; bool[] attempted = isHdr ? s_HdrLoadAttempted : s_LdrLoadAttempted; int[,] kernels = isHdr ? s_HdrKernels : s_LdrKernels; @@ -356,6 +441,9 @@ private static bool TryGetEncoderShader(int slot, bool isHdr, uint qualityTier, attempted[slot] = true; string resourcePath = isHdr ? s_HdrShaderNames[slot] : s_LdrShaderNames[slot]; + if (string.IsNullOrEmpty(resourcePath)) + return false; + ComputeShader loaded = Resources.Load(resourcePath); if (loaded == null) { @@ -364,15 +452,15 @@ private static bool TryGetEncoderShader(int slot, bool isHdr, uint qualityTier, } shaders[slot] = loaded; - kernels[slot, 0] = loaded.FindKernel("EncodeFast"); - kernels[slot, 1] = loaded.FindKernel("EncodeMed"); - kernels[slot, 2] = loaded.FindKernel("EncodeFull"); + kernels[slot, 0] = AstcStripConfig.IsFastQualityEnabled ? loaded.FindKernel("EncodeFast") : -1; + kernels[slot, 1] = AstcStripConfig.IsMediumQualityEnabled ? loaded.FindKernel("EncodeMed") : -1; + kernels[slot, 2] = AstcStripConfig.IsFullQualityEnabled ? loaded.FindKernel("EncodeFull") : -1; } shader = shaders[slot]; int tierIdx = qualityTier == 0u ? 0 : (qualityTier == 1u ? 1 : 2); kernel = kernels[slot, tierIdx]; - return true; + return kernel >= 0; } private static uint ResolveShaderMode(GraphicsFormat sourceFormat, TextureType textureType, out bool isSRGB, out bool isHdr) @@ -411,6 +499,14 @@ internal static void Preload(BlockSize blockSize) if (!IsSupported) return; + if (!AstcStripConfig.IsBlockSizeEnabled(blockSize)) + { + string def = AstcStripConfig.GetStripDefineForBlockSize(blockSize); + if (def != null) + Debug.LogError($"ASTC block size '{blockSize}' is disabled by Scripting Define Symbol '{def}'."); + return; + } + int slot = BlockSizeToSlot(blockSize); if (slot < 0) return; @@ -427,9 +523,15 @@ internal static void Preload(BlockSize blockSize) for (int variant = 0; variant < 2; variant++) { bool isHdr = variant == 0; + if (isHdr && !AstcStripConfig.IsHdrEnabled) + continue; + uint mode = isHdr ? k_ModeHdrRgb : k_ModeLdrRgb; for (uint tier = 0u; tier <= 2u; tier++) { + if (!AstcStripConfig.IsQualityTierEnabled(tier)) + continue; + if (!TryGetEncoderShader(slot, isHdr, tier, out ComputeShader shader, out int kernel)) continue; @@ -750,6 +852,9 @@ private static ComputeBuffer DispatchAllMipsToBuffer( return null; uint mode = ResolveShaderMode(source.graphicsFormat, textureType, out bool isSRGB, out bool isHdr); + if (!AstcStripConfig.ValidateEncodingArgsWithLog(blockSize, quality, isHdr)) + return null; + uint qualityTier = MapQualityTier(quality); if (!TryGetEncoderShader(slot, isHdr, qualityTier, out ComputeShader shader, out int kernel)) return null; @@ -1140,6 +1245,9 @@ internal static void Encode( 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 bool isHdr); + if (!AstcStripConfig.ValidateEncodingArgs(blockSize, quality, isHdr, out string stripError)) + throw new NotSupportedException(stripError); + uint qualityTier = MapQualityTier(quality); 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})."); @@ -1198,7 +1306,10 @@ internal static Cubemap CompressCubemap( return null; } - GraphicsFormat astcFormat = ResolveTargetAstcFormat(source.graphicsFormat, textureType, blockSize, out _, out _); + GraphicsFormat astcFormat = ResolveTargetAstcFormat(source.graphicsFormat, textureType, blockSize, out _, out bool isHdr); + if (!AstcStripConfig.ValidateEncodingArgsWithLog(blockSize, quality, isHdr)) + return null; + int mipCount = includeMipmaps ? Math.Max(1, source.mipmapCount) : 1; if (!CanUseDirectCopy(source.width, source.height, mipCount, blockSize, astcFormat)) { @@ -1255,7 +1366,9 @@ internal static Texture2DArray CompressTexture2DArray( return null; } - GraphicsFormat astcFormat = ResolveTargetAstcFormat(source.graphicsFormat, textureType, blockSize, out _, out _); + GraphicsFormat astcFormat = ResolveTargetAstcFormat(source.graphicsFormat, textureType, blockSize, out _, out bool isHdr); + if (!AstcStripConfig.ValidateEncodingArgsWithLog(blockSize, quality, isHdr)) + return null; int mipCount = includeMipmaps ? Math.Max(1, source.mipmapCount) : 1; if (!CanUseDirectCopy(source.width, source.height, mipCount, blockSize, astcFormat)) { @@ -1425,7 +1538,10 @@ internal static Texture2D CompressToTexture( return null; } - GraphicsFormat astcFormat = ResolveTargetAstcFormat(source.graphicsFormat, textureType, blockSize, out _, out _); + GraphicsFormat astcFormat = ResolveTargetAstcFormat(source.graphicsFormat, textureType, blockSize, out _, out bool isHdr); + if (!AstcStripConfig.ValidateEncodingArgsWithLog(blockSize, quality, isHdr)) + return null; + if (!Encoder.CanCreateAstcTexture(astcFormat)) { Debug.LogError($"ASTC texture format '{astcFormat}' is not supported on this device. Use CompressTexture or EncodeToASTC for raw ASTC data."); @@ -1514,7 +1630,10 @@ internal static async Task CompressToTextureAsync( return null; } - GraphicsFormat astcFormat = ResolveTargetAstcFormat(source.graphicsFormat, textureType, blockSize, out _, out _); + GraphicsFormat astcFormat = ResolveTargetAstcFormat(source.graphicsFormat, textureType, blockSize, out _, out bool isHdr); + if (!AstcStripConfig.ValidateEncodingArgsWithLog(blockSize, quality, isHdr)) + return null; + if (!Encoder.CanCreateAstcTexture(astcFormat)) { Debug.LogError($"ASTC texture format '{astcFormat}' is not supported on this device. Use CompressTextureAsync or EncodeToASTCAsync for raw ASTC data."); @@ -1582,7 +1701,10 @@ internal static bool CompressInPlace( return false; } - GraphicsFormat astcFormat = ResolveTargetAstcFormat(inputTexture.graphicsFormat, textureType, blockSize, out _, out _); + GraphicsFormat astcFormat = ResolveTargetAstcFormat(inputTexture.graphicsFormat, textureType, blockSize, out _, out bool isHdr); + if (!AstcStripConfig.ValidateEncodingArgsWithLog(blockSize, quality, isHdr)) + return false; + if (!Encoder.CanCreateAstcTexture(astcFormat)) { Debug.LogError($"ASTC texture format '{astcFormat}' is not supported on this device. Use CompressTexture or EncodeToASTC for raw ASTC data."); diff --git a/Assets/ASTC Encoder/Tests/Editor/AstcEncoderTests.cs b/Assets/ASTC Encoder/Tests/Editor/AstcEncoderTests.cs index 680096c..24e7e7f 100644 --- a/Assets/ASTC Encoder/Tests/Editor/AstcEncoderTests.cs +++ b/Assets/ASTC Encoder/Tests/Editor/AstcEncoderTests.cs @@ -450,5 +450,90 @@ private static double ComputeMeanNormalAngularErrorDeg(Color[] refPixels, byte[] } return count > 0 ? degSum / count : 0.0; } + + [Test] + public void StripConfig_DefaultBuild_AllValidBlockSizesHdrAndQualitiesEnabled() + { + Assert.IsFalse(AstcStripConfig.IsBlockSizeEnabled(BlockSize.ASTC_None)); + foreach (BlockSize bs in s_AllBlockSizes) + { + Assert.IsTrue(AstcStripConfig.IsBlockSizeEnabled(bs), $"Expected {bs} enabled by default."); + } + + Assert.IsTrue(AstcStripConfig.IsHdrEnabled); + Assert.IsTrue(AstcStripConfig.IsFastQualityEnabled); + Assert.IsTrue(AstcStripConfig.IsMediumQualityEnabled); + Assert.IsTrue(AstcStripConfig.IsFullQualityEnabled); + + foreach (CompressQuality q in Enum.GetValues(typeof(CompressQuality))) + { + Assert.IsTrue(AstcStripConfig.IsQualityEnabled(q), $"Expected {q} enabled by default."); + } + } + + [Test] + public void ComputeShaderStripper_StripsMatchingBlockSizesHdrAndQualities() + { + string[] blocks = { "4x4", "5x5", "6x6", "8x8", "10x10", "12x12" }; + string[] kernels = { "EncodeFast", "EncodeMed", "EncodeFull" }; + + int CountKept(System.Collections.Generic.HashSet defines) + { + int kept = 0; + foreach (string b in blocks) + { + foreach (bool hdr in new[] { false, true }) + { + string shaderName = hdr ? $"AstcGpuEncoder_{b}_Hdr" : $"AstcGpuEncoder_{b}"; + foreach (string k in kernels) + { + if (!Aperture.Astcenc.Editor.AstcComputeShaderStripper.ShouldStrip(shaderName, k, defines)) + kept++; + } + } + } + return kept; + } + + // Utility shaders must never be stripped even if all strip defines are set + var allDefines = new System.Collections.Generic.HashSet + { + AstcStripConfig.DefineStrip4x4, AstcStripConfig.DefineStrip5x5, AstcStripConfig.DefineStrip6x6, + AstcStripConfig.DefineStrip8x8, AstcStripConfig.DefineStrip10x10, AstcStripConfig.DefineStrip12x12, + AstcStripConfig.DefineStripHdr, AstcStripConfig.DefineStripFast, AstcStripConfig.DefineStripMed, AstcStripConfig.DefineStripFull + }; + Assert.IsFalse(Aperture.Astcenc.Editor.AstcComputeShaderStripper.ShouldStrip("AstcGpuBufferCopy", "CopyTexToBuf", allDefines)); + Assert.IsFalse(Aperture.Astcenc.Editor.AstcComputeShaderStripper.ShouldStrip("AstcGpuMipGenerator", "DownsampleColor", allDefines)); + Assert.AreEqual(0, CountKept(allDefines)); + + // No defines: all 36 variants kept + Assert.AreEqual(36, CountKept(new System.Collections.Generic.HashSet())); + + // HDR only stripped: 18 LDR variants kept + Assert.AreEqual(18, CountKept(new System.Collections.Generic.HashSet { AstcStripConfig.DefineStripHdr })); + + // Quality Full stripped: 24 variants kept + Assert.AreEqual(24, CountKept(new System.Collections.Generic.HashSet { AstcStripConfig.DefineStripFull })); + + // Mobile Common (keep 4x4, 6x6, 8x8 LDR Fast+Med): 6 variants kept + var mobileCommon = new System.Collections.Generic.HashSet + { + AstcStripConfig.DefineStrip5x5, AstcStripConfig.DefineStrip10x10, AstcStripConfig.DefineStrip12x12, + AstcStripConfig.DefineStripHdr, AstcStripConfig.DefineStripFull + }; + Assert.AreEqual(6, CountKept(mobileCommon)); + + // 6x6 LDR Fast Only: 1 variant kept + var only6x6Fast = new System.Collections.Generic.HashSet + { + AstcStripConfig.DefineStrip4x4, AstcStripConfig.DefineStrip5x5, AstcStripConfig.DefineStrip8x8, + AstcStripConfig.DefineStrip10x10, AstcStripConfig.DefineStrip12x12, + AstcStripConfig.DefineStripHdr, AstcStripConfig.DefineStripMed, AstcStripConfig.DefineStripFull + }; + Assert.AreEqual(1, CountKept(only6x6Fast)); + Assert.IsFalse(Aperture.Astcenc.Editor.AstcComputeShaderStripper.ShouldStrip("AstcGpuEncoder_6x6", "EncodeFast", only6x6Fast)); + Assert.IsTrue(Aperture.Astcenc.Editor.AstcComputeShaderStripper.ShouldStrip("AstcGpuEncoder_6x6", "EncodeMed", only6x6Fast)); + Assert.IsTrue(Aperture.Astcenc.Editor.AstcComputeShaderStripper.ShouldStrip("AstcGpuEncoder_6x6_Hdr", "EncodeFast", only6x6Fast)); + } } } From e93b2976fb479a35dec35fa8faaee717f34a2477 Mon Sep 17 00:00:00 2001 From: FrozenKresnik <80754402+Overflowu@users.noreply.github.com> Date: Tue, 1 Dec 2026 16:52:52 +0000 Subject: [PATCH 2/3] chore(release): bump version to 2.3.2 --- Assets/ASTC Encoder/CHANGELOG.md | 20 ++++++++++++++++++++ Assets/ASTC Encoder/package.json | 2 +- README.md | 6 +++--- README_CN.md | 6 +++--- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/Assets/ASTC Encoder/CHANGELOG.md b/Assets/ASTC Encoder/CHANGELOG.md index bd4220c..b93c32e 100644 --- a/Assets/ASTC Encoder/CHANGELOG.md +++ b/Assets/ASTC Encoder/CHANGELOG.md @@ -2,6 +2,26 @@ All notable changes to `com.aperture.astcenc` (Runtime ASTC Encoder for Unity) are documented in this file. +## [2.3.2] - 2026-12-01 + +### Added +- **Unified CPU + GPU Feature & Shader Variant Stripping (`ASTC_STRIP_*`)**: + - Added Scripting Define Symbols across all three dimensions — Block Size (`ASTC_STRIP_4X4`..`ASTC_STRIP_12X12`), HDR (`ASTC_STRIP_HDR`), and Quality Tier (`ASTC_STRIP_FAST`, `ASTC_STRIP_MED`, `ASTC_STRIP_FULL`) — defaulting to all enabled when undefined. + - **C# Compile-Time Enforcement**: Stripped `BlockSize` and `CompressQuality` enum members are marked `[Obsolete(..., error: true)]` with stable explicit integer values so invalid calls fail at compile time (`CS0619`). + - **Unified Runtime Validation (`AstcStripConfig`)**: Both CPU (`AstcContext.Create`) and GPU (`AstcGpuBackend`) enforce identical stripping rules, with public runtime query helpers (`IsBlockSizeEnabled`, `IsQualityEnabled`, `IsHdrEnabled`). + - **Build-Time Compute Shader Stripper (`AstcComputeShaderStripper`)**: Implements `IPreprocessComputeShaders` to strip unused `AstcGpuEncoder_*` shaders and `EncodeFast` / `EncodeMed` / `EncodeFull` kernels during builds. + - **Editor Settings Window (`Window → ASTC Encoder → Shader & Feature Stripping`)**: Interactive UI with live variant count (`X / 36`), quick presets, and one-click Scripting Define Symbol management. + +### Changed & Optimized +- **5x–6.5x Faster GPU Compute Shader Compilation with Zero Warnings**: + - Split the monolithic GPU compute encoder into 12 per-block LDR/HDR shaders (`AstcGpuEncoder_{4x4..12x12}{,_Hdr}.compute`) with unified texture output + `AstcGpuBufferCopy.compute`, removing `#pragma editor_sync_compilation`. + - Eliminated duplicate HLSL inlining, packed indexable temporary arrays, absorbed BISE scratch into `g_AstcGrid.w`, paired HDR pixel cache into `uint4` slots, unified HDR color quantization, and paired dual-plane infill tap evaluation. + - Reduced full multi-platform build shader compile times dramatically (Win64 `24m59s → 5m01s`, iOS `37m18s → 5m42s`, macOS `32m28s → 7m02s`, Linux64 `56m56s → 8m29s`, Android `55m50s → 8m57s`) with **0 shader warnings** (including 0 `X4714`) across all 5 platforms and zero runtime performance regression. + +### Fixed +- Checked `SystemInfo.IsFormatSupported` in `Encoder.CanCreateAstcTexture` in the Unity Editor as well, preventing invalid ASTC texture creation on Editor GPUs lacking hardware ASTC support. +- Initialized `unquantOut` in `AstcQuantizeWeight` to eliminate D3D11 FXC uninitialized-output warning. + ## [2.3.1] - 2026-11-29 ### Changed & Optimized diff --git a/Assets/ASTC Encoder/package.json b/Assets/ASTC Encoder/package.json index 8ed58f1..f2057d7 100644 --- a/Assets/ASTC Encoder/package.json +++ b/Assets/ASTC Encoder/package.json @@ -1,7 +1,7 @@ { "name": "com.aperture.astcenc", "displayName": "Runtime ASTC Encoder", - "version": "2.3.1", + "version": "2.3.2", "unity": "2021.3", "keywords": ["Texture", "ASTC", "Runtime", "astcenc", "GPU", "ComputeShader", "KTX"], "description": "High-performance runtime ASTC texture compression (GPU Compute + native ARM astc-encoder CPU), GPU mipmap generation, KTX v1 disk caching, and Editor benchmarking for Unity.", diff --git a/README.md b/README.md index 855afa6..04d74f2 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ [![Unity Multi-Platform E2E](https://github.com/Overflowu/Runtime-ASTC-Encoder-For-Unity/actions/workflows/unity-multiplatform-e2e.yml/badge.svg)](https://github.com/Overflowu/Runtime-ASTC-Encoder-For-Unity/actions/workflows/unity-multiplatform-e2e.yml) [![Build Native Plugins](https://github.com/Overflowu/Runtime-ASTC-Encoder-For-Unity/actions/workflows/build-native-plugins.yml/badge.svg)](https://github.com/Overflowu/Runtime-ASTC-Encoder-For-Unity/actions/workflows/build-native-plugins.yml) ![Unity 2021.3+](https://img.shields.io/badge/Unity-2021.3%2B-black?logo=unity) -![UPM v2.3.1](https://img.shields.io/badge/UPM-v2.3.1-blue) +![UPM v2.3.2](https://img.shields.io/badge/UPM-v2.3.2-blue) ![astc-encoder v5.7.1](https://img.shields.io/badge/astc--encoder-v5.7.1-informational) [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](Assets/ASTC%20Encoder/LICENSE.md) @@ -45,10 +45,10 @@ A production-ready **runtime ASTC texture compression package** for Unity (`com. In Unity, open **Window → Package Manager → `+` → Add package from git URL...**: ```text -https://github.com/Overflowu/Runtime-ASTC-Encoder-For-Unity.git?path=/Assets/ASTC%20Encoder#v2.3.1 +https://github.com/Overflowu/Runtime-ASTC-Encoder-For-Unity.git?path=/Assets/ASTC%20Encoder#v2.3.2 ``` -*(Omit `#v2.3.1` to track the latest `master` branch.)* +*(Omit `#v2.3.2` to track the latest `master` branch.)* ### Option 2: Local Clone / Direct Copy diff --git a/README_CN.md b/README_CN.md index 6045ee4..a050282 100644 --- a/README_CN.md +++ b/README_CN.md @@ -6,7 +6,7 @@ [![Unity Multi-Platform E2E](https://github.com/Overflowu/Runtime-ASTC-Encoder-For-Unity/actions/workflows/unity-multiplatform-e2e.yml/badge.svg)](https://github.com/Overflowu/Runtime-ASTC-Encoder-For-Unity/actions/workflows/unity-multiplatform-e2e.yml) [![Build Native Plugins](https://github.com/Overflowu/Runtime-ASTC-Encoder-For-Unity/actions/workflows/build-native-plugins.yml/badge.svg)](https://github.com/Overflowu/Runtime-ASTC-Encoder-For-Unity/actions/workflows/build-native-plugins.yml) ![Unity 2021.3+](https://img.shields.io/badge/Unity-2021.3%2B-black?logo=unity) -![UPM v2.3.1](https://img.shields.io/badge/UPM-v2.3.1-blue) +![UPM v2.3.2](https://img.shields.io/badge/UPM-v2.3.2-blue) ![astc-encoder v5.7.1](https://img.shields.io/badge/astc--encoder-v5.7.1-informational) [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](Assets/ASTC%20Encoder/LICENSE.md) @@ -45,10 +45,10 @@ 在 Unity 中打开 **Window → Package Manager → `+` → Add package from git URL...**: ```text -https://github.com/Overflowu/Runtime-ASTC-Encoder-For-Unity.git?path=/Assets/ASTC%20Encoder#v2.3.1 +https://github.com/Overflowu/Runtime-ASTC-Encoder-For-Unity.git?path=/Assets/ASTC%20Encoder#v2.3.2 ``` -*(去掉 `#v2.3.1` 可直接跟踪 `master` 分支最新提交。)* +*(去掉 `#v2.3.2` 可直接跟踪 `master` 分支最新提交。)* ### 方式二:本地克隆 / 直接拷贝 From 79714c2e3daf514e0026792837a97da798100cf1 Mon Sep 17 00:00:00 2001 From: FrozenKresnik <80754402+Overflowu@users.noreply.github.com> Date: Tue, 1 Dec 2026 16:58:20 +0000 Subject: [PATCH 3/3] fix: avoid shadowed isHdr local variable in AstcGpuBackend.CompressInPlace --- Assets/ASTC Encoder/Runtime/Gpu/AstcGpuBackend.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/ASTC Encoder/Runtime/Gpu/AstcGpuBackend.cs b/Assets/ASTC Encoder/Runtime/Gpu/AstcGpuBackend.cs index 550b1f3..fb4e517 100644 --- a/Assets/ASTC Encoder/Runtime/Gpu/AstcGpuBackend.cs +++ b/Assets/ASTC Encoder/Runtime/Gpu/AstcGpuBackend.cs @@ -1728,7 +1728,7 @@ 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 bool isHdr); + uint mode = ResolveShaderMode(inputTexture.graphicsFormat, textureType, out bool isSRGB, out isHdr); uint qualityTier = MapQualityTier(quality); if (!TryGetEncoderShader(slot, isHdr, qualityTier, out ComputeShader shader, out int kernel)) return false;