Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Assets/ASTC Encoder/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Assets/ASTC Encoder/Editor/AstcBenchmarkWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
210 changes: 210 additions & 0 deletions Assets/ASTC Encoder/Editor/AstcComputeShaderStripper.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Build-time ComputeShader variant stripper driven by the same Scripting Define Symbols
/// (<c>ASTC_STRIP_4X4</c>.. <c>ASTC_STRIP_12X12</c>, <c>ASTC_STRIP_HDR</c>,
/// <c>ASTC_STRIP_FAST</c>, <c>ASTC_STRIP_MED</c>, <c>ASTC_STRIP_FULL</c>)
/// that enforce C# compile-time enum checks and runtime CPU/GPU validation.
/// </summary>
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<string> 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<ShaderCompilerData> 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<string> 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;
}

/// <summary>
/// Determines whether a given ASTC encoder compute shader + kernel should be stripped
/// under the provided set of Scripting Define Symbols.
/// </summary>
public static bool ShouldStrip(string shaderName, string kernelName, ICollection<string> 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<string> GetActiveStripDefines(BuildTargetGroup group)
{
if (group == BuildTargetGroup.Unknown)
group = BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget);

string[] rawDefines = GetScriptingDefines(group);
var result = new HashSet<string>(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<string> stripDefinesToEnable)
{
if (group == BuildTargetGroup.Unknown)
group = BuildPipeline.GetBuildTargetGroup(EditorUserBuildSettings.activeBuildTarget);

var allDefines = new List<string>();
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<string>();
#else
string raw = PlayerSettings.GetScriptingDefineSymbolsForGroup(group);
return string.IsNullOrEmpty(raw)
? Array.Empty<string>()
: 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
}
}
}
11 changes: 11 additions & 0 deletions Assets/ASTC Encoder/Editor/AstcComputeShaderStripper.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading