flowchart TD
A["Unity Texture2D / RenderTexture / Raw Pixels"] --> B["Unified Entry (Encoder.cs)"]
B -->|GPU Backend| G1["AstcGpuBackend.cs"]
G1 -->|Optional| G0["AstcGpuMipGenerator.compute (Odd-NPOT + Normal Renorm)"]
G0 --> G2["AstcGpuEncoder.compute (Tableless HLSL)"]
G1 --> G2
G2 -->|Hardware ASTC CopyTexture| G3["Zero-Readback -> ASTC Texture2D"]
G2 -->|Buffer / AsyncGPUReadback| G4["AstcRaw (Unmanaged Buffer)"]
B -->|CPU Backend| C1["AstcCpuBackend.cs + AstcContextPool.cs"]
C1 --> C2["NativeAPI P/Invoke (astc-encoder v5.7.1)"]
C2 --> G4
G4 --> H1["Texture2D.LoadRawTextureData + Apply"]
G4 --> H2["AstcKtxContainer (.ktx) / AstcHeader (.astc)"]
Located under Assets/ASTC Encoder/Runtime/Gpu/ and Runtime/Resources/:
- Tableless Pure-ALU Encoding (
AstcGpuBise.hlsl): Performs BISE quantization, unquantization, and 128-bit block packing using integer ALU instructions with no large lookup tables, keeping constant-buffer and L1 cache footprints minimal across mobile tile-based GPUs (Metal / Vulkan / GLES 3.1+) and desktop GPUs (D3D11 / D3D12). - Per-Block Kernel Specialization (
AstcGpuEncoder.compute/AstcGpuCore.hlsl):- Separate
ASTC_FAST_ONLYkernels (Fastest/Fast) and Full kernels (Medium–Exhaustive) to avoid register bloat in real-time modes. - Per-block
ASTC_MAX_GRIDsizing (4x4→ 16,5x5→ 25,>=6x6→ 36) to cut VGPR usage. [numthreads(8, 4, 1)]occupancy tuning for>=8x8blocks.- On-chip packed
RGBA8pixel cache for<=6x6LDR blocks.
- Separate
- High-Quality Compression Without Redundant Scans (
AstcGpuCore.hlsl/AstcGpuColor.hlsl):- Lossless Void-Extent fast path for constant blocks.
- Zero-
pow()sqrtsRGB approximation (0/2568-bit round-trip error). - Row-level early-exit candidate pruning.
- Same-grid continuous weight projection reuse across quantization candidates, combined 1P error + worst-channel accumulation (eliminating worst-channel rescans), merged 3-pass 2-Partition dual-subset endpoint fitting, and loop-hoisted
AstcHash52(AstcPreparePartition2). - Isotropic and anisotropic weight grids (
6x4/4x6/8x4/4x8), dynamic-CCS Dual-Plane for both RGB (ORM/MRAO) and RGBA, post-quantization ideal endpoint refit, dedicatedCEM 4+ tangent-space angular-error Dual-Plane for Normal maps, and HDRCEM 11/14/15high-precision delta modes.
- Zero-Readback Mipmap + CopyTexture Pipeline (
AstcGpuMipGenerator.compute/AstcGpuBackend.cs):- Generates the full mipmap chain (handling odd-NPOT edge texels and tangent-space normal renormalization) and encodes every mip into ASTC inside a single
CommandBuffer. - Uses
CommandBuffer.CopyTextureon block coordinates (ceil(mipW / bx) × ceil(mipH / by)) for zero-CPU-readback uploads across both aligned and non-multiple dimensions, including full mipmap tails. - When CPU access is requested (
CompressTextureAsync/EncodeToASTCAsync/EncodeToKTXAsync), uses LOH-freeAsyncGPUReadback.
- Generates the full mipmap chain (handling odd-NPOT edge texels and tangent-space normal renormalization) and encodes every mip into ASTC inside a single
Located under Assets/ASTC Encoder/Runtime/Cpu/ and Runtime/NativeAPI/:
- Runtime ISA Dispatch (
astcenc_api.cs):- Windows x64: Probes AVX2 via
IsProcessorFeaturePresent(40)at startup and bindsastcenc-avx2-shared.dll, falling back seamlessly toastcenc-sse4.1-shared.dll. - Android: Dispatches
libastcenc-neon-shared.so(arm64-v8a) vslibastcenc-none-shared.so(armeabi-v7a) based onIntPtr.Size == 8. - macOS: Universal dylib (
x86_64SSE4.1 +x86_64hAVX2 +arm64NEON). - iOS: Static NEON library (
__Internal).
- Windows x64: Probes AVX2 via
- Context Pooling &
parentContextSharing (AstcContextPool.cs/AstcContext.cs):- Thread-safe static pool eliminates per-call
astcenc_context_alloc/freeoverhead across all static CPU APIs. - Shares read-only block lookup tables via
astcenc 5.xparentContextperBlockSize, with safe deferred parent disposal onEncoder.ReleaseCpuContextPool()or application quit. - Supports multi-core
Parallel.Foror optional Unity Job System (IJobParallelFor),CancellationTokencancellation (astcenc_compress_cancel), and guaranteedastcenc_compress_reset.
- Thread-safe static pool eliminates per-call
- Zero-Copy & Low-Memory Pixel Transfer (
ImageExtensions.cs/RawTextureData.cs):- 4-channel
RGBA8/BGRA8/RGBA16F/RGBA32FpassNativeSliceread-only pointers directly in synchronous mode (zero copy). - Asynchronous mode takes a single compact
UnsafeUtility.MemCpysnapshot on the main thread and expands/compresses one mip level at a time on background threads (up to 8× lower peak memory for 1/2/3-channel and 16-bit formats). - 1/2/3-channel and 16-bit UNorm formats expand via
unsafepointer loops with zero managed heap allocations.
- 4-channel
The repository includes five CI workflows under .github/workflows/:
verify-shaders-and-code.yml: Manually dispatched (workflow_dispatch) workflow that validates HLSL/Compute shader syntax across all block sizes and verifies Unity.metafile completeness and GUID uniqueness.build-native-plugins.yml: Builds all nativeastc-encoderbinaries fromThirdParty/astc-encoder(Windows x64 AVX2 + SSE4.1, macOS Universal, iOS ARM64 +astcenc.xcframework, Androidarm64-v8a/armeabi-v7awith symbol stripping and 16 KB page alignment, Linux x64 SSE4.1) and verifies exported symbols. Automatically runs on non-masterpushes touching the submodule or workflow file, and can be manually dispatched (commit_binaries: truecommits updated binaries and Linux.metafiles via Git LFS).- When upgrading
ThirdParty/astc-encoder, verify struct layouts and signatures inRuntime/NativeAPI/againstThirdParty/astc-encoder/Source/astcenc.h.
- When upgrading
unity-multiplatform-e2e.yml: Manually dispatched (workflow_dispatch) workflow that runs Unity EditMode tests (Aperture.Astcenc.Tests) and builds/executes end-to-end smoke tests across Linux x64, Windows x64, macOS Universal, Android ARM64 (emulator), and iOS ARM64 (Xcode build & link) on demand.performance-benchmarks.yml: Manually dispatched (workflow_dispatch) workflow that runs cross-platform native CPU SIMD benchmarks and Unity Standalone Player GPU/CPU benchmarks, optionally syncing results intoREADME.md,README_CN.md,docs/Benchmarks.md, anddocs/benchmarks/baseline.json.release-package.yml: Automatically triggered when adevelop→masterpull request is merged (also supports manualworkflow_dispatch). Verifies resolved LFS native plugins, creates thev<version>Git tag frompackage.json, packages both a UPM tarball (com.aperture.astcenc-<version>.tgz) and a direct Assets.ziparchive with SHA-256 checksums, extracts notes fromCHANGELOG.md, and publishes a GitHub Release.