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
9 changes: 9 additions & 0 deletions Assets/ASTC Encoder/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

All notable changes to `com.aperture.astcenc` (Runtime ASTC Encoder for Unity) are documented in this file.

## [2.3.1] - 2026-11-29

### Changed & Optimized
- **GPU Compute Encoder Throughput (P0 / P1 / Medium)**:
- **Single-Pass Pixel Cache & LDR/HDR Shader Split**: Replaced multi-pass texture loads with a single-pass compact `uint` RGBA8 LDR pixel cache and split `ASTC_HDR_VARIANT` via `LocalKeyword` to reduce register pressure on LDR paths.
- **O(1) BISE Quantization & Direct Weight Packing**: Added division-free `AstcLinearToCode`, 56-entry O(1) `kAstcWeightLut`, Gather tent-kernel weight decimation, direct MSB-descending power-of-2 weight packing (skipping 128-bit reversal), and `6x6` exact-grid Fast mode (`0x108`).
- **ComputeBuffer Pooling**: Pooled intermediate GPU block buffers (`AcquireBlockBuffer` / `ReturnBlockBuffer`) with exact-byte async readbacks in `AstcGpuBackend`.
- **Dedicated Medium Kernels (`ASTC_NO_2P`)**: Added 12 `EncodeTex*Med` / `EncodeBuf*Med` kernels stripping 2-Partition code and registers at compile time, paired with pruned Stage 0/1 candidates, 1-pass endpoint refinement, and a conservative ~50 dB PSNR early-out (cutting 1024px Medium GPU latency by 28%–35% on Apple Silicon Metal with negligible quality impact).

## [2.3.0] - 2026-11-28

### Added
Expand Down
213 changes: 173 additions & 40 deletions Assets/ASTC Encoder/Runtime/Gpu/AstcGpuBackend.cs

Large diffs are not rendered by default.

147 changes: 114 additions & 33 deletions Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuBise.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ uint AstcPack3Quints(uint q0, uint q1, uint q2)
}

// Maps a monotonically ordered quantized level k in [0, range-1] to the ASTC BISE code.
// Uses exact reciprocal multiplies for /3 and /5 (valid for all k < 256) to avoid GPU integer div/mod.
uint AstcLinearToCode(uint k, uint q)
{
uint rangeVal, b, t, qu;
Expand All @@ -142,20 +143,21 @@ uint AstcLinearToCode(uint k, uint q)
}

uint halfR = rangeVal >> 1u;
uint baseVal = (t != 0u) ? 3u : 5u;
if (k < halfR)
uint val = (k < halfR) ? k : ((rangeVal - 1u) - k);
uint oddBit = (k < halfR) ? 0u : 1u;

uint m, d;
if (t != 0u)
{
uint d = k % baseVal;
uint m = k / baseVal;
return (d << b) | (m << 1u);
m = (val * 0xAAABu) >> 17u;
d = val - m * 3u;
}
else
{
uint rk = (rangeVal - 1u) - k;
uint d = rk % baseVal;
uint m = rk / baseVal;
return (d << b) | (m << 1u) | 1u;
m = (val * 0xCCCDu) >> 18u;
d = val - m * 5u;
}
return (d << b) | (m << 1u) | oddBit;
}

// Exact Khronos ASTC color unquantization: BISE code v -> [0, 255].
Expand Down Expand Up @@ -209,6 +211,17 @@ uint AstcUnquantizeColor(uint v, uint q)
return (A & 0x80u) | (T >> 2u);
}

uint AstcUnquantizePow2Weight(uint v, uint b)
{
uint t;
if (b == 1u) t = (v != 0u) ? 63u : 0u;
else if (b == 2u) t = v * 21u;
else if (b == 3u) t = (v << 3u) | v;
else if (b == 4u) t = (v << 2u) | (v >> 2u);
else t = (v << 1u) | (v >> 4u);
return t + (t > 32u ? 1u : 0u);
}

// Exact Khronos ASTC weight unquantization: BISE code v -> [0, 64].
uint AstcUnquantizeWeight(uint v, uint q)
{
Expand All @@ -218,11 +231,7 @@ uint AstcUnquantizeWeight(uint v, uint q)

if (t == 0u && qu == 0u)
{
if (b == 1u) T = (v != 0u) ? 63u : 0u;
else if (b == 2u) T = v * 21u;
else if (b == 3u) T = (v << 3u) | v;
else if (b == 4u) T = (v << 2u) | (v >> 2u);
else T = (v << 1u) | (v >> 4u);
return AstcUnquantizePow2Weight(v, b);
}
else if (t != 0u)
{
Expand Down Expand Up @@ -289,6 +298,20 @@ uint AstcQuantizeColorRange(uint c8, uint q, bool min128, out uint unquantOut)
int maxK = (int)rangeVal - 1;
int kEst = clamp((int)floor((float)c8 * (float)maxK / 255.0 + 0.5), minK, maxK);

// O(1) fast path for pure power-of-2 quantization ranges (no trit/quint search loop or integer div/mod).
if (t == 0u && qu == 0u)
{
uint v = (uint)kEst;
uint r = v << (8u - b);
if (b < 8u) r |= v >> max((int)b * 2 - 8, 0);
if (b <= 3u)
{
r = (b == 3u) ? ((v << 5u) | (v << 2u) | (v >> 1u)) : ((b == 2u) ? (v * 85u) : (v * 255u));
}
unquantOut = r & 0xFFu;
return v;
}

uint bestCode = 0u;
uint bestUq = 0u;
int bestErr = 10000;
Expand Down Expand Up @@ -318,33 +341,91 @@ uint AstcQuantizeColorMin128(uint c8, uint q, out uint unquantOut)
return AstcQuantizeColorRange(c8, q, true, unquantOut);
}

// Reverses the lowest b bits (b in 1..4) of a power-of-2 weight code for direct MSB-descending packing.
uint AstcReverseSmallBits(uint v, uint b)
{
if (b == 2u) return ((v & 1u) << 1u) | (v >> 1u);
if (b == 3u) return ((v & 1u) << 2u) | (v & 2u) | (v >> 2u);
if (b == 4u) return ((v & 1u) << 3u) | ((v & 2u) << 1u) | ((v & 4u) >> 1u) | (v >> 3u);
return v & 1u;
}

// Compact 56-entry packed LUT for all non-power-of-2 ASTC weight quant modes (QUANT_3/5/6/10/12/20).
// Each entry packs (code | (unquantizedWeight64 << 8)) in monotonic order of unquantized weight.
static const uint kAstcWeightLut[56] =
{
// QUANT_3 (offset 0, range 3)
0x0000u, 0x2001u, 0x4002u,
// QUANT_5 (offset 3, range 5)
0x0000u, 0x1001u, 0x2002u, 0x3003u, 0x4004u,
// QUANT_6 (offset 8, range 6)
0x0000u, 0x0C02u, 0x1904u, 0x2705u, 0x3403u, 0x4001u,
// QUANT_10 (offset 14, range 10)
0x0000u, 0x0702u, 0x0E04u, 0x1506u, 0x1C08u, 0x2409u, 0x2B07u, 0x3205u, 0x3903u, 0x4001u,
// QUANT_12 (offset 24, range 12)
0x0000u, 0x0504u, 0x0B08u, 0x1102u, 0x1706u, 0x1C0Au, 0x240Bu, 0x2907u, 0x2F03u, 0x3509u, 0x3B05u, 0x4001u,
// QUANT_20 (offset 36, range 20)
0x0000u, 0x0304u, 0x0608u, 0x090Cu, 0x0D10u, 0x1002u, 0x1306u, 0x170Au, 0x1A0Eu, 0x1D12u,
0x2313u, 0x260Fu, 0x290Bu, 0x2D07u, 0x3003u, 0x3311u, 0x370Du, 0x3A09u, 0x3D05u, 0x4001u
};

uint AstcWeightNeighbor(uint k, uint maxK, float error)
{
return (error > 0.0) ? min(k + 1u, maxK) : max(k, 1u) - 1u;
}

uint AstcQuantizePow2Weight(float w01, uint maxK, uint b, out uint unquantOut)
{
float sw = saturate(w01);
uint k = min((uint)floor(sw * (float)maxK + 0.5), maxK);
uint uq = AstcUnquantizePow2Weight(k, b);
float target = sw * 64.0;
float error = target - (float)uq;

// Uniform rounding locates the candidates, but decoded weights are not uniformly spaced.
uint kNbr = AstcWeightNeighbor(k, maxK, error);
uint uqNbr = AstcUnquantizePow2Weight(kNbr, b);
bool useNeighbor = abs(target - (float)uqNbr) < abs(error);
unquantOut = useNeighbor ? uqNbr : uq;
return useNeighbor ? kNbr : k;
}

// Quantizes a normalized weight w01 in [0, 1] to the exact nearest BISE code and returns unquantized [0, 64].
uint AstcQuantizeWeight(float w01, uint q, out uint unquantOut)
{
uint rangeVal, b, t, qu;
AstcGetQuantParams(q, rangeVal, b, t, qu);

float w64 = saturate(w01) * 64.0;
int maxK = (int)rangeVal - 1;
int kEst = clamp((int)floor(saturate(w01) * (float)maxK + 0.5), 0, maxK);

uint bestCode = 0u;
uint bestUq = 0u;
float bestErr = 1e30;

[loop] for (int step = 0; step < 3; ++step)
uint maxK = rangeVal - 1u;
if (t == 0u && qu == 0u)
{
int dk = (step == 0) ? 0 : ((step == 1) ? -1 : 1);
int k = kEst + dk;
if (k < 0 || k > maxK) continue;
uint code = AstcLinearToCode((uint)k, q);
uint uq = AstcUnquantizeWeight(code, q);
float err = abs((float)uq - w64);
if (err < bestErr) { bestErr = err; bestCode = code; bestUq = uq; }
return AstcQuantizePow2Weight(w01, maxK, b, unquantOut);
}

unquantOut = bestUq;
return bestCode;
float sw = saturate(w01);
uint kEst = min((uint)floor(sw * (float)maxK + 0.5), maxK);

// O(1) exact LUT path for all trit/quint weight ranges (QUANT_3/5/6/10/12/20):
// because unquantized weights are strictly monotonic, checking kEst and 1 neighbor in the
// direction of (w64 - uqEst) guarantees the exact nearest BISE code with zero loops.
uint lutBase = (q == ASTC_QUANT_3) ? 0u
: (q == ASTC_QUANT_5) ? 3u
: (q == ASTC_QUANT_6) ? 8u
: (q == ASTC_QUANT_10) ? 14u
: (q == ASTC_QUANT_12) ? 24u
: 36u;

uint e0 = kAstcWeightLut[lutBase + kEst];
uint uq0 = e0 >> 8u;
float w64 = sw * 64.0;
float diff0 = w64 - (float)uq0;

uint kNbr = AstcWeightNeighbor(kEst, maxK, diff0);
uint e1 = kAstcWeightLut[lutBase + kNbr];
uint uq1 = e1 >> 8u;

uint bestEntry = (abs(w64 - (float)uq1) < abs(diff0)) ? e1 : e0;
unquantOut = bestEntry >> 8u;
return bestEntry & 0xFFu;
}

// Writes up to 24 bits into a 128-bit block (little-endian uint4), branchless.
Expand Down
64 changes: 48 additions & 16 deletions Assets/ASTC Encoder/Runtime/Gpu/Shaders/AstcGpuColor.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ float3 AstcLinearToSRGB(float3 rgb)
return lerp(hi, c * 12.92, float3(c <= 0.0031308));
}

#if defined(ASTC_HDR_VARIANT)

// Exact Khronos ASTC float4 -> 16-bit LNS [0, 65535] conversion (branchless vectorized).
// Negative values and NaNs clamp to 0; values >= 65536 clamp to 65535.
float4 AstcFloatToLns4(float4 c)
Expand Down Expand Up @@ -375,6 +377,10 @@ void AstcQuantizeHdrAlphaEndpoints(
uqA1Lns = (float)((uq1 & 0x7Fu) << 9u);
}

#endif // ASTC_HDR_VARIANT

#if !defined(ASTC_FAST_ONLY) && !defined(ASTC_NO_2P)

// ============================================================================
// ASTC 2-Partition Procedural Hash (Khronos Spec Section 22.21)
// ============================================================================
Expand Down Expand Up @@ -462,10 +468,49 @@ uint AstcSelectPartition2(uint seed, uint x, uint y, bool smallBlock)
return AstcEvalPartition2(AstcPreparePartition2(seed, smallBlock), x, y);
}

#endif // !ASTC_FAST_ONLY

// ============================================================================
// ASTC Hardware Bilinear Weight Grid Infill (Khronos Spec Section 22.17)
// ============================================================================

uint AstcInfillScale(uint blockSize, uint gridSize)
{
return ((1024u + (blockSize >> 1u)) / (blockSize - 1u)) * (gridSize - 1u);
}

uint AstcInfillCoord(uint texel, uint scale)
{
return (texel * scale + 32u) >> 6u;
}

uint4 AstcInfillWeights(uint s, uint t)
{
uint fs = s & 15u;
uint ft = t & 15u;
uint w11 = (fs * ft + 8u) >> 4u;
return uint4(16u - fs - ft + w11, fs - w11, ft - w11, w11);
}

// Invert the rounded infill coordinate for the two cells adjacent to this grid node.
uint2 AstcGatherSpan(uint node, uint scale, uint blockSize)
{
uint lo = ((max(node, 1u) - 1u) << 10u) + 32u;
uint hi = ((node + 1u) << 10u) - 33u;
return uint2(node == 0u ? 0u : (lo + scale - 1u) / scale,
min(blockSize - 1u, hi / scale));
}

uint AstcGatherTap(uint s, uint t, uint gx, uint gy)
{
uint4 w = AstcInfillWeights(s, t);
bool right = (gx != (s >> 4u));
bool bottom = (gy != (t >> 4u));
uint row0 = right ? w.y : w.x;
uint row1 = right ? w.w : w.z;
return bottom ? row1 : row0;
}

// Computes bilinear grid coordinates (js, jt) and 4 integer weights (w00, w10, w01, w11)
// summing to 16 for texel (x, y) in a (blockX, blockY) block using a (gridW, gridH) weight grid.
void AstcComputeInfillTap(
Expand All @@ -475,25 +520,12 @@ void AstcComputeInfillTap(
out uint js, out uint jt,
out uint4 tapWeights)
{
uint ds = (1024u + (blockX >> 1u)) / (blockX - 1u);
uint dt = (1024u + (blockY >> 1u)) / (blockY - 1u);
uint cs = ds * x;
uint ct = dt * y;

uint gs = (cs * (gridW - 1u) + 32u) >> 6u;
uint gt = (ct * (gridH - 1u) + 32u) >> 6u;
uint gs = AstcInfillCoord(x, AstcInfillScale(blockX, gridW));
uint gt = AstcInfillCoord(y, AstcInfillScale(blockY, gridH));

js = gs >> 4u;
jt = gt >> 4u;
uint fs = gs & 15u;
uint ft = gt & 15u;

uint w11 = (fs * ft + 8u) >> 4u;
uint w10 = fs - w11;
uint w01 = ft - w11;
uint w00 = 16u - fs - ft + w11;

tapWeights = uint4(w00, w10, w01, w11);
tapWeights = AstcInfillWeights(gs, gt);
}

// Evaluates the exact ASTC hardware interpolated weight [0, 64] at texel (x, y)
Expand Down
Loading
Loading