// SPDX-FileCopyrightText: Copyright 2026 Douglas F // SPDX-License-Identifier: GPL-3.0-or-later // // NOT a Nintendo file, and nothing in it comes from the game. Triforce's own BC7 encoder, compiled to // bc7_encoder_comp_spv.h by glslangValidator -V --target-env vulkan1.1 --vn BC7_ENCODER_COMP_SPV. // // BC7 (mode 6) encoder for the Vulkan back end's big ASTC textures (vk_texture.cpp). // // The same block a BC3 texture gets, at the same 16 bytes: the 4x4 texels are fitted along their // principal colour axis (a covariance power iteration), both endpoints are quantized to mode 6's // 7 bits plus a p-bit, and each texel takes the nearest of the 16 interpolants. It is the CPU // encoder of the WebGPU back end (gpu/render/rx_bc7.cpp, RX_ASTC_BC7) as a compute shader: BC7's // mode 6 keeps gradients and alpha far better than BC3 at the same 8 bits a texel. // // Binding 0: the RGBA8 scratch (one layer per dispatch z). // Binding 1: the output buffer, 16 bytes a block, layer-major (as the copies into the image expect). // Push constants: the same BcPush as the BC3 encoder; bc3 is unused. #version 450 layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; layout(binding = 0, rgba8) uniform image2DArray src; layout(binding = 1) buffer Out { uint words[]; }; layout(push_constant) uniform Push { uint blocks_x; uint blocks_y; uint width; uint height; uint bc3; uint out_offset; } pc; const int kWeights[16] = {0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64}; struct Fit { int q[2][4]; int p[2]; int index[16]; int error; }; struct Quant { int q[4]; int p; }; // 7 bits plus the p-bit nearest to e[4] (0..255), the two candidates tried as the CPU encoder does. Quant quantize(const float e[4]) { Quant out_q; int best = 0x7fffffff; for (int pb = 0; pb < 2; ++pb) { int err = 0; int t[4]; for (int c = 0; c < 4; ++c) { t[c] = clamp(int((e[c] - float(pb)) * 0.5 + 0.5), 0, 127); const float d = e[c] - float(t[c] * 2 + pb); err += int(d * d * 16.0); } if (err < best) { best = err; out_q.p = pb; for (int c = 0; c < 4; ++c) out_q.q[c] = t[c]; } } return out_q; } // Each texel's nearest of the 16 interpolants of the two endpoints. void choose_indices(const int px[16][4], inout Fit f) { int pal[16][4]; for (int k = 0; k < 16; ++k) { for (int c = 0; c < 4; ++c) { const int a = f.q[0][c] * 2 + f.p[0]; const int b = f.q[1][c] * 2 + f.p[1]; pal[k][c] = ((64 - kWeights[k]) * a + kWeights[k] * b + 32) >> 6; } } f.error = 0; for (int i = 0; i < 16; ++i) { int best = 0; int best_err = 0x7fffffff; for (int k = 0; k < 16; ++k) { int e = 0; for (int c = 0; c < 4; ++c) { const int d = px[i][c] - pal[k][c]; e += d * d; } if (e < best_err) { best_err = e; best = k; } } f.index[i] = best; f.error += best_err; } } Fit fit_endpoints(const int px[16][4], const float e0[4], const float e1[4]) { Fit f; const Quant q0 = quantize(e0); const Quant q1 = quantize(e1); for (int c = 0; c < 4; ++c) { f.q[0][c] = q0.q[c]; f.q[1][c] = q1.q[c]; } f.p[0] = q0.p; f.p[1] = q1.p; choose_indices(px, f); return f; } // The block's 128 bits, little-endian as the image's blocks are: mode 6, 7-bit endpoints with // their p-bits, then one 3-bit index (that the mode shares with a 4-bit one) and 15 4-bit indices. void put(inout uint w[4], inout int pos, uint v, int n) { for (int i = 0; i < n; ++i) { if (((v >> i) & 1u) != 0u) { const int p = pos + i; w[p >> 5] |= 1u << (p & 31); } } pos += n; } uvec4 encode_block(const int px[16][4]) { float mean[4] = {0.0, 0.0, 0.0, 0.0}; for (int i = 0; i < 16; ++i) for (int c = 0; c < 4; ++c) mean[c] += float(px[i][c]); for (int c = 0; c < 4; ++c) mean[c] *= 1.0 / 16.0; float cov[4][4]; for (int a0 = 0; a0 < 4; ++a0) for (int b0 = 0; b0 < 4; ++b0) cov[a0][b0] = 0.0; for (int i = 0; i < 16; ++i) { float d[4]; for (int c = 0; c < 4; ++c) d[c] = float(px[i][c]) - mean[c]; for (int a = 0; a < 4; ++a) for (int b = 0; b < 4; ++b) cov[a][b] += d[a] * d[b]; } float axis[4] = {cov[0][0], cov[1][1], cov[2][2], cov[3][3]}; bool restarted = false; for (int it = 0; it < 8; ++it) { float n[4] = {0.0, 0.0, 0.0, 0.0}; for (int a = 0; a < 4; ++a) for (int b = 0; b < 4; ++b) n[a] += cov[a][b] * axis[b]; float m = 0.0; for (int a = 0; a < 4; ++a) m = max(m, abs(n[a])); if (m < 1e-6) { // (the diagonal can be exactly orthogonal to the principal axis: R and G anticorrelated with equal // variance, B and A constant. Restart once from the covariance row with the largest norm.) if (restarted) break; int row = 0; float best = 0.0; for (int a = 0; a < 4; ++a) { float l = 0.0; for (int b = 0; b < 4; ++b) l += cov[a][b] * cov[a][b]; if (l > best) best = l, row = a; } if (best < 1e-12) break; for (int a = 0; a < 4; ++a) axis[a] = cov[row][a]; restarted = true; continue; } for (int a = 0; a < 4; ++a) axis[a] = n[a] / m; } float len = 0.0; for (int c = 0; c < 4; ++c) len += axis[c] * axis[c]; len = sqrt(len); float tmin = 0.0, tmax = 0.0; if (len > 1e-6) { for (int c = 0; c < 4; ++c) axis[c] /= len; tmin = 1e30; tmax = -1e30; for (int i = 0; i < 16; ++i) { float t = 0.0; for (int c = 0; c < 4; ++c) t += (float(px[i][c]) - mean[c]) * axis[c]; tmin = min(tmin, t); tmax = max(tmax, t); } } float e0[4], e1[4]; for (int c = 0; c < 4; ++c) { e0[c] = clamp(mean[c] + axis[c] * tmin, 0.0, 255.0); e1[c] = clamp(mean[c] + axis[c] * tmax, 0.0, 255.0); } Fit best = fit_endpoints(px, e0, e1); if (best.error > 64) { float aa = 0.0, ab = 0.0, bb = 0.0; float ax[4] = {0.0, 0.0, 0.0, 0.0}, bx[4] = {0.0, 0.0, 0.0, 0.0}; for (int i = 0; i < 16; ++i) { const float b = float(kWeights[best.index[i]]) / 64.0; const float a = 1.0 - b; aa += a * a; ab += a * b; bb += b * b; for (int c = 0; c < 4; ++c) { ax[c] += a * float(px[i][c]); bx[c] += b * float(px[i][c]); } } const float det = aa * bb - ab * ab; if (abs(det) > 1e-6) { float r0[4], r1[4]; for (int c = 0; c < 4; ++c) { r0[c] = clamp((bb * ax[c] - ab * bx[c]) / det, 0.0, 255.0); r1[c] = clamp((aa * bx[c] - ab * ax[c]) / det, 0.0, 255.0); } const Fit refit = fit_endpoints(px, r0, r1); if (refit.error < best.error) best = refit; } } // Mode 6's first index is 3 bits: an index 8 or above there reads as the other endpoint first. if ((best.index[0] & 8) != 0) { int tq[4]; for (int c = 0; c < 4; ++c) { tq[c] = best.q[0][c]; best.q[0][c] = best.q[1][c]; best.q[1][c] = tq[c]; } const int tp = best.p[0]; best.p[0] = best.p[1]; best.p[1] = tp; for (int i = 0; i < 16; ++i) best.index[i] = 15 - best.index[i]; } uint w[4] = {0u, 0u, 0u, 0u}; int pos = 0; put(w, pos, 1u << 6, 7); for (int c = 0; c < 4; ++c) { put(w, pos, uint(best.q[0][c]), 7); put(w, pos, uint(best.q[1][c]), 7); } put(w, pos, uint(best.p[0]), 1); put(w, pos, uint(best.p[1]), 1); put(w, pos, uint(best.index[0]), 3); for (int i = 1; i < 16; ++i) put(w, pos, uint(best.index[i]), 4); return uvec4(w[0], w[1], w[2], w[3]); } void main() { const uvec3 gid = gl_GlobalInvocationID; if (gid.x >= pc.blocks_x || gid.y >= pc.blocks_y) return; int px[16][4]; for (int y = 0; y < 4; ++y) { const int sy = min(int(gid.y) * 4 + y, int(pc.height) - 1); for (int x = 0; x < 4; ++x) { const int sx = min(int(gid.x) * 4 + x, int(pc.width) - 1); const vec4 p = imageLoad(src, ivec3(sx, sy, int(gid.z))); for (int c = 0; c < 4; ++c) px[y * 4 + x][c] = int(clamp(p[c] * 255.0 + 0.5, 0.0, 255.0)); } } const uvec4 w = encode_block(px); const uint base = (pc.out_offset >> 2) + ((gid.z * pc.blocks_y + gid.y) * pc.blocks_x + gid.x) * 4u; words[base + 0u] = w.x; words[base + 1u] = w.y; words[base + 2u] = w.z; words[base + 3u] = w.w; }