mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-19 21:39:58 -04:00
Import the base tree (MIT, see LICENSE)
This commit is contained in:
429
runtime/shaders/astc_decode.comp
Normal file
429
runtime/shaders/astc_decode.comp
Normal file
@@ -0,0 +1,429 @@
|
||||
// ASTC LDR 2D decode to RGBA8 on the GPU, one invocation per texel. Ported from runtime/tex_astc.c (itself from LAS
|
||||
// Recomp runtime/nvn/astc.c @ da35b83a, MIT, Copyright (c) 2026 Peiikai), so the output is the same bytes as
|
||||
// astc_decode_ldr and astcenc's 8-bit decode: endpoints expand to 16 bits (linear v * 257, sRGB v << 8 | 0x80),
|
||||
// texel = (e0 * (64 - w) + e1 * w + 32) >> 6, top 8 bits. Error blocks are magenta; HDR endpoint modes give magenta
|
||||
// endpoints for their partition.
|
||||
//
|
||||
// A workgroup of 64 lanes takes up to four blocks of one block row (main below): every lane decodes its block's header,
|
||||
// the block's lanes share the integer-sequence decode of its weights and colour values through shared memory, and
|
||||
// each lane then interpolates its own texels.
|
||||
// Compiled to runtime/astc_decode_spv.h by tools/build_shaders.py; vk_astc.c records the dispatch.
|
||||
#version 450
|
||||
|
||||
layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
|
||||
|
||||
layout(std430, set = 0, binding = 0) readonly buffer Blocks { uvec4 blk[]; };
|
||||
layout(std430, set = 0, binding = 1) writeonly buffer Texels { uint px[]; };
|
||||
// uint 0..11: byte offsets of the weight unquantization rows (QUANT_2..QUANT_32), 12..27: of the colour rows
|
||||
// (QUANT_6..QUANT_256 minus one); the bytes follow (vk_astc.c astc_tables).
|
||||
layout(std430, set = 0, binding = 2) readonly buffer Tables { uint tab[]; };
|
||||
|
||||
layout(push_constant) uniform Params {
|
||||
uint in_base; // first block of slice 0 (uvec4 index into blk)
|
||||
uint out_base; // first texel of slice 0 (index into px)
|
||||
uint width, height;
|
||||
uint bw, bh; // block footprint
|
||||
uint bx; // blocks per row
|
||||
uint bslice; // blocks per slice
|
||||
uint srgb;
|
||||
} pc;
|
||||
|
||||
uint tab_byte(uint i) { return (tab[i >> 2] >> ((i & 3u) * 8u)) & 0xFFu; }
|
||||
|
||||
// n (<= 32) bits at pos; bits past 127 read as zero.
|
||||
uint rbits(uvec4 b, int pos, int n) {
|
||||
if (n == 0 || pos >= 128) return 0u;
|
||||
int wi = pos >> 5, sh = pos & 31;
|
||||
uint v = b[wi] >> uint(sh);
|
||||
if (sh != 0 && wi < 3) v |= b[wi + 1] << uint(32 - sh);
|
||||
return n >= 32 ? v : v & ((1u << uint(n)) - 1u);
|
||||
}
|
||||
|
||||
// Quantization levels QUANT_2 .. QUANT_256 (0..20): bits, trits, quints of tex_astc.c q_bits / q_trits / q_quints.
|
||||
int q_bits(int q) {
|
||||
if (q < 2) return q == 0 ? 1 : 0;
|
||||
int g = (q - 2) / 3, r = (q - 2) - 3 * g;
|
||||
return r == 0 ? g + 2 : r == 1 ? g : g + 1;
|
||||
}
|
||||
bool q_trit(int q) { return q == 1 || (q >= 2 && (q - 2) % 3 == 2); }
|
||||
bool q_quint(int q) { return q >= 2 && (q - 2) % 3 == 1; }
|
||||
|
||||
int ise_bitcount(int count, int q) {
|
||||
int n = count * q_bits(q);
|
||||
if (q_trit(q)) n += (8 * count + 4) / 5;
|
||||
if (q_quint(q)) n += (7 * count + 2) / 3;
|
||||
return n;
|
||||
}
|
||||
|
||||
int trit_at(uint T, int k) {
|
||||
uint C;
|
||||
int t0, t1, t2, t3, t4;
|
||||
if (((T >> 2) & 7u) == 7u) {
|
||||
C = ((T >> 5) & 7u) << 2 | (T & 3u);
|
||||
t4 = 2; t3 = 2;
|
||||
} else {
|
||||
C = T & 0x1Fu;
|
||||
if (((T >> 5) & 3u) == 3u) { t4 = 2; t3 = int((T >> 7) & 1u); }
|
||||
else { t4 = int((T >> 7) & 1u); t3 = int((T >> 5) & 3u); }
|
||||
}
|
||||
if ((C & 3u) == 3u) {
|
||||
t2 = 2; t1 = int((C >> 4) & 1u);
|
||||
t0 = int(((C >> 3) & 1u) << 1 | (((C >> 2) & 1u) & ~(C >> 3) & 1u));
|
||||
} else if (((C >> 2) & 3u) == 3u) {
|
||||
t2 = 2; t1 = 2; t0 = int(C & 3u);
|
||||
} else {
|
||||
t2 = int((C >> 4) & 1u); t1 = int((C >> 2) & 3u);
|
||||
t0 = int(((C >> 1) & 1u) << 1 | ((C & 1u) & ~(C >> 1) & 1u));
|
||||
}
|
||||
return k == 0 ? t0 : k == 1 ? t1 : k == 2 ? t2 : k == 3 ? t3 : t4;
|
||||
}
|
||||
|
||||
int quint_at(uint Q, int k) {
|
||||
int q0, q1, q2;
|
||||
if (((Q >> 1) & 3u) == 3u && ((Q >> 5) & 3u) == 0u) {
|
||||
uint b0 = Q & 1u;
|
||||
q2 = int(b0 << 2 | (((Q >> 4) & 1u) & ~b0 & 1u) << 1 | (((Q >> 3) & 1u) & ~b0 & 1u));
|
||||
q1 = 4; q0 = 4;
|
||||
} else {
|
||||
uint C;
|
||||
if (((Q >> 1) & 3u) == 3u) { q2 = 4; C = ((Q >> 3) & 3u) << 3 | (~(Q >> 5) & 3u) << 1 | (Q & 1u); }
|
||||
else { q2 = int((Q >> 5) & 3u); C = Q & 0x1Fu; }
|
||||
if ((C & 7u) == 5u) { q1 = 4; q0 = int((C >> 3) & 3u); }
|
||||
else { q1 = int((C >> 3) & 3u); q0 = int(C & 7u); }
|
||||
}
|
||||
return k == 0 ? q0 : k == 1 ? q1 : q2;
|
||||
}
|
||||
|
||||
// Raw ISE value idx (trit/quint << bits | bits) of a sequence of count values at bit start; tex_astc.c ise_decode
|
||||
// reads the sequence front to back, this reads one value's group only. Bits of values past count are not read.
|
||||
uint ise_at(uvec4 b, int start, int count, int q, int idx) {
|
||||
int nb = q_bits(q);
|
||||
if (q_trit(q)) {
|
||||
// value k of a group: nb bits at k * nb + ts[k], then tb[k] trit bits; ts = {0, 2, 4, 5, 7}, tb = {2, 2, 1, 2, 1}
|
||||
int g = idx / 5, k = idx - 5 * g, gs = start + g * (5 * nb + 8);
|
||||
uint m = rbits(b, gs + k * nb + (k * 7 + 2) / 4, nb);
|
||||
uint T = 0u;
|
||||
for (int j = 0; j < 5; j++) {
|
||||
int ts = (j * 7 + 2) / 4, tb = j == 2 || j == 4 ? 1 : 2;
|
||||
if (5 * g + j < count) T |= rbits(b, gs + (j + 1) * nb + ts, tb) << uint(ts);
|
||||
}
|
||||
return uint(trit_at(T, k)) << uint(nb) | m;
|
||||
}
|
||||
if (q_quint(q)) {
|
||||
// qs = {0, 3, 5}, qb = {3, 2, 2}
|
||||
int g = idx / 3, k = idx - 3 * g, gs = start + g * (3 * nb + 7);
|
||||
uint m = rbits(b, gs + k * nb + (k == 0 ? 0 : k == 1 ? 3 : 5), nb);
|
||||
uint Q = 0u;
|
||||
for (int j = 0; j < 3; j++) {
|
||||
int qs = j == 0 ? 0 : j == 1 ? 3 : 5, qb = j == 0 ? 3 : 2;
|
||||
if (3 * g + j < count) Q |= rbits(b, gs + (j + 1) * nb + qs, qb) << uint(qs);
|
||||
}
|
||||
return uint(quint_at(Q, k)) << uint(nb) | m;
|
||||
}
|
||||
return rbits(b, start + idx * nb, nb);
|
||||
}
|
||||
|
||||
uint hash52(uint p) {
|
||||
p ^= p >> 15; p -= p << 17; p += p << 7; p += p << 4;
|
||||
p ^= p >> 5; p += p << 16; p ^= p >> 7; p ^= p >> 3;
|
||||
p ^= p << 6; p ^= p >> 17;
|
||||
return p;
|
||||
}
|
||||
|
||||
int part_of(int seed, int count, int x, int y) {
|
||||
seed += (count - 1) * 1024;
|
||||
uint rnum = hash52(uint(seed));
|
||||
int sh1, sh2;
|
||||
if ((seed & 1) != 0) { sh1 = (seed & 2) != 0 ? 4 : 5; sh2 = count == 3 ? 6 : 5; }
|
||||
else { sh1 = count == 3 ? 6 : 5; sh2 = (seed & 2) != 0 ? 4 : 5; }
|
||||
int s[8];
|
||||
for (int i = 0; i < 8; i++) {
|
||||
int v = int((rnum >> uint(4 * i)) & 0xFu);
|
||||
s[i] = (v * v) >> ((i & 1) != 0 ? sh2 : sh1);
|
||||
}
|
||||
int a = int((uint(s[0] * x + s[1] * y) + (rnum >> 14)) & 0x3Fu);
|
||||
int b = int((uint(s[2] * x + s[3] * y) + (rnum >> 10)) & 0x3Fu);
|
||||
int c = count >= 3 ? int((uint(s[4] * x + s[5] * y) + (rnum >> 6)) & 0x3Fu) : 0;
|
||||
int d = count >= 4 ? int((uint(s[6] * x + s[7] * y) + (rnum >> 2)) & 0x3Fu) : 0;
|
||||
if (a >= b && a >= c && a >= d) return 0;
|
||||
if (b >= c && b >= d) return 1;
|
||||
if (c >= d) return 2;
|
||||
return 3;
|
||||
}
|
||||
|
||||
int clamp255(int v) { return clamp(v, 0, 255); }
|
||||
|
||||
// v: the partition's unquantized colour values. False for an HDR mode.
|
||||
bool unpack_endpoints(int cem, int v[8], out ivec4 e0, out ivec4 e1) {
|
||||
e0 = ivec4(0); e1 = ivec4(0);
|
||||
switch (cem) {
|
||||
case 0:
|
||||
e0 = ivec4(v[0], v[0], v[0], 255); e1 = ivec4(v[1], v[1], v[1], 255);
|
||||
return true;
|
||||
case 1: {
|
||||
int l0 = v[0] >> 2 | (v[1] & 0xC0);
|
||||
int l1 = min(l0 + (v[1] & 0x3F), 255);
|
||||
e0 = ivec4(l0, l0, l0, 255); e1 = ivec4(l1, l1, l1, 255);
|
||||
return true;
|
||||
}
|
||||
case 4:
|
||||
e0 = ivec4(v[0], v[0], v[0], v[2]); e1 = ivec4(v[1], v[1], v[1], v[3]);
|
||||
return true;
|
||||
case 5: {
|
||||
int l0 = v[0], l1 = v[1], a0 = v[2], a1 = v[3];
|
||||
l0 |= (l1 & 0x80) << 1; a0 |= (a1 & 0x80) << 1;
|
||||
l1 &= 0x7F; a1 &= 0x7F;
|
||||
if ((l1 & 0x40) != 0) l1 -= 0x80;
|
||||
if ((a1 & 0x40) != 0) a1 -= 0x80;
|
||||
l0 >>= 1; a0 >>= 1; l1 >>= 1; a1 >>= 1;
|
||||
l1 = clamp255(l1 + l0); a1 = clamp255(a1 + a0);
|
||||
e0 = ivec4(l0, l0, l0, a0); e1 = ivec4(l1, l1, l1, a1);
|
||||
return true;
|
||||
}
|
||||
case 6:
|
||||
case 10: {
|
||||
int s = v[3];
|
||||
e1 = ivec4(v[0], v[1], v[2], 255);
|
||||
e0 = ivec4(v[0] * s >> 8, v[1] * s >> 8, v[2] * s >> 8, 255);
|
||||
if (cem == 10) { e0.w = v[4]; e1.w = v[5]; }
|
||||
return true;
|
||||
}
|
||||
case 8:
|
||||
case 12: {
|
||||
ivec4 a = ivec4(v[0], v[2], v[4], cem == 12 ? v[6] : 0);
|
||||
ivec4 b = ivec4(v[1], v[3], v[5], cem == 12 ? v[7] : 0);
|
||||
if (a.x + a.y + a.z > b.x + b.y + b.z) {
|
||||
e0 = b; e1 = a;
|
||||
e0.x = (e0.x + e0.z) >> 1; e0.y = (e0.y + e0.z) >> 1;
|
||||
e1.x = (e1.x + e1.z) >> 1; e1.y = (e1.y + e1.z) >> 1;
|
||||
} else {
|
||||
e0 = a; e1 = b;
|
||||
}
|
||||
if (cem == 8) { e0.w = 255; e1.w = 255; }
|
||||
return true;
|
||||
}
|
||||
case 9:
|
||||
case 13: {
|
||||
ivec4 a = ivec4(0), b = ivec4(0);
|
||||
for (int c = 0; c < 4; c++) {
|
||||
if (c == 3 && cem != 13) break;
|
||||
int base = v[2 * c], delta = v[2 * c + 1];
|
||||
a[c] = base >> 1 | (delta & 0x80);
|
||||
int d = (delta >> 1) & 0x3F;
|
||||
if ((d & 0x20) != 0) d -= 0x40;
|
||||
b[c] = d;
|
||||
}
|
||||
int sum = b.x + b.y + b.z;
|
||||
b += a;
|
||||
if (sum < 0) {
|
||||
e0 = b; e1 = a;
|
||||
e0.x = (e0.x + e0.z) >> 1; e0.y = (e0.y + e0.z) >> 1;
|
||||
e1.x = (e1.x + e1.z) >> 1; e1.y = (e1.y + e1.z) >> 1;
|
||||
} else {
|
||||
e0 = a; e1 = b;
|
||||
}
|
||||
e0 = clamp(e0, ivec4(0), ivec4(255)); e1 = clamp(e1, ivec4(0), ivec4(255));
|
||||
if (cem == 9) { e0.w = 255; e1.w = 255; }
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const uint MAGENTA = 0xFFFF00FFu;
|
||||
|
||||
// What every texel of a block needs from its header (tex_astc.c astc_decode_block up to the weight infill).
|
||||
struct Hdr {
|
||||
uint konst; // the colour of a void-extent or error block
|
||||
bool solid; // konst is the whole block
|
||||
int wx, wy, dual, quant, wtotal, pcount, cem0, cem1, cem2, cem3, n0, n1, n2, ccount, cq, ccs;
|
||||
};
|
||||
|
||||
Hdr header(uvec4 b, int bw, int bh) {
|
||||
Hdr H;
|
||||
H.solid = true; H.konst = MAGENTA;
|
||||
H.wx = 0; H.wy = 0; H.dual = 0; H.quant = 0; H.wtotal = 0; H.pcount = 1;
|
||||
H.cem0 = 0; H.cem1 = 0; H.cem2 = 0; H.cem3 = 0; H.n0 = 0; H.n1 = 0; H.n2 = 0; H.ccount = 0; H.cq = 20; H.ccs = -1;
|
||||
uint mode = b.x & 0x7FFu;
|
||||
if ((mode & 0x1FFu) == 0x1FCu) { // void extent
|
||||
if ((mode & 0x200u) != 0u || ((b.x >> 10) & 3u) != 3u) return H;
|
||||
uint s0 = rbits(b, 12, 13), s1 = rbits(b, 25, 13), t0 = rbits(b, 38, 13), t1 = rbits(b, 51, 13);
|
||||
bool all_ones = s0 == 0x1FFFu && s1 == 0x1FFFu && t0 == 0x1FFFu && t1 == 0x1FFFu;
|
||||
if ((s0 >= s1 || t0 >= t1) && !all_ones) return H;
|
||||
H.konst = (b.z >> 8 & 0xFFu) | (b.z >> 24) << 8 | (b.w >> 8 & 0xFFu) << 16 | (b.w >> 24) << 24;
|
||||
return H;
|
||||
}
|
||||
|
||||
// block mode (tex_astc.c block_mode_2d)
|
||||
uint m = mode;
|
||||
uint r = (m >> 4) & 1u, h = (m >> 9) & 1u, d = (m >> 10) & 1u, a = (m >> 5) & 3u;
|
||||
int wx, wy;
|
||||
if ((m & 3u) != 0u) {
|
||||
r |= (m & 3u) << 1;
|
||||
uint bb = (m >> 7) & 3u;
|
||||
uint sel = (m >> 2) & 3u;
|
||||
if (sel == 0u) { wx = int(bb) + 4; wy = int(a) + 2; }
|
||||
else if (sel == 1u) { wx = int(bb) + 8; wy = int(a) + 2; }
|
||||
else if (sel == 2u) { wx = int(a) + 2; wy = int(bb) + 8; }
|
||||
else {
|
||||
bb &= 1u;
|
||||
if ((m & 0x100u) != 0u) { wx = int(bb) + 2; wy = int(a) + 2; }
|
||||
else { wx = int(a) + 2; wy = int(bb) + 6; }
|
||||
}
|
||||
} else {
|
||||
r |= ((m >> 2) & 3u) << 1;
|
||||
if (((m >> 2) & 3u) == 0u) return H;
|
||||
uint bb = (m >> 9) & 3u;
|
||||
uint sel = (m >> 7) & 3u;
|
||||
if (sel == 0u) { wx = 12; wy = int(a) + 2; }
|
||||
else if (sel == 1u) { wx = int(a) + 2; wy = 12; }
|
||||
else if (sel == 2u) { wx = int(a) + 6; wy = int(bb) + 6; d = 0u; h = 0u; }
|
||||
else {
|
||||
if (a == 0u) { wx = 6; wy = 10; }
|
||||
else if (a == 1u) { wx = 10; wy = 6; }
|
||||
else return H;
|
||||
}
|
||||
}
|
||||
int dual = int(d);
|
||||
int wtotal = (wx * wy) << dual;
|
||||
int quant = int(r) - 2 + 6 * int(h);
|
||||
int wbits = ise_bitcount(wtotal, quant);
|
||||
if (wtotal > 64 || wbits < 24 || wbits > 96 || wx > bw || wy > bh) return H;
|
||||
int pcount = int((b.x >> 11) & 3u) + 1;
|
||||
if (dual != 0 && pcount == 4) return H;
|
||||
|
||||
// colour endpoint modes
|
||||
int cem0, cem1 = 0, cem2 = 0, cem3 = 0;
|
||||
int below = 128 - wbits, high = 0;
|
||||
if (pcount == 1) {
|
||||
cem0 = int((b.x >> 13) & 0xFu);
|
||||
} else {
|
||||
high = 3 * pcount - 4;
|
||||
below -= high;
|
||||
uint enc = ((b.x >> 23) & 0x3Fu) | rbits(b, below, high) << 6;
|
||||
uint base = enc & 3u;
|
||||
if (base == 0u) {
|
||||
cem0 = cem1 = cem2 = cem3 = int((enc >> 2) & 0xFu);
|
||||
below += high;
|
||||
high = 0;
|
||||
} else {
|
||||
base--;
|
||||
cem0 = int((((enc >> 2) & 1u) + base) << 2 | ((enc >> uint(2 + pcount)) & 3u));
|
||||
cem1 = int((((enc >> 3) & 1u) + base) << 2 | ((enc >> uint(4 + pcount)) & 3u));
|
||||
cem2 = int((((enc >> 4) & 1u) + base) << 2 | ((enc >> uint(6 + pcount)) & 3u));
|
||||
cem3 = int((((enc >> 5) & 1u) + base) << 2 | ((enc >> uint(8 + pcount)) & 3u));
|
||||
}
|
||||
}
|
||||
int n0 = ((cem0 >> 2) + 1) * 2, n1 = pcount > 1 ? ((cem1 >> 2) + 1) * 2 : 0;
|
||||
int n2 = pcount > 2 ? ((cem2 >> 2) + 1) * 2 : 0, n3 = pcount > 3 ? ((cem3 >> 2) + 1) * 2 : 0;
|
||||
int ccount = n0 + n1 + n2 + n3;
|
||||
if (ccount > 18) return H;
|
||||
int cbits = max((pcount == 1 ? 111 : 99) - wbits - high - (dual != 0 ? 2 : 0), 0);
|
||||
int cq = -1;
|
||||
for (int q = 20; q >= 0; q--) {
|
||||
if (ise_bitcount(ccount, q) <= cbits) { cq = q; break; }
|
||||
}
|
||||
if (cq < 4) return H; // QUANT_6
|
||||
|
||||
H.solid = false;
|
||||
H.wx = wx; H.wy = wy; H.dual = dual; H.quant = quant; H.wtotal = wtotal; H.pcount = pcount;
|
||||
H.cem0 = cem0; H.cem1 = cem1; H.cem2 = cem2; H.cem3 = cem3; H.n0 = n0; H.n1 = n1; H.n2 = n2;
|
||||
H.ccount = ccount; H.cq = cq;
|
||||
H.ccs = dual != 0 ? int(rbits(b, below - 2, 2)) : -1;
|
||||
return H;
|
||||
}
|
||||
|
||||
// A workgroup decodes BPW blocks of one block row when a block has 64 texels or fewer (64 / texels of them), else one
|
||||
// block: S lanes per block (its texels, at most 64). The lanes of a block decode its weights and colour values into
|
||||
// shared memory, the first pcount lanes then unpack the endpoints, and each lane writes its texels.
|
||||
const int MAXB = 4; // 4x4: 4 blocks of 16 lanes
|
||||
shared uint s_w[MAXB][64]; // unquantized weights 0..64 (dual plane: interleaved as in the bit stream)
|
||||
shared int s_cv[MAXB][18]; // unquantized colour values
|
||||
shared uvec2 s_e0[MAXB][4], s_e1[MAXB][4]; // 16-bit endpoints, two channels per uint
|
||||
|
||||
uvec2 pack16(ivec4 e) { return uvec2(uint(e.x) | uint(e.y) << 16, uint(e.z) | uint(e.w) << 16); }
|
||||
int ch16(uvec2 p, int c) { return int(((c < 2 ? p.x : p.y) >> (16 * (c & 1))) & 0xFFFFu); }
|
||||
|
||||
void main() {
|
||||
int bw = int(pc.bw), bh = int(pc.bh), G = bw * bh;
|
||||
int bpw = G <= 64 ? 64 / G : 1, S = G <= 64 ? G : 64;
|
||||
int l = int(gl_LocalInvocationID.x), lb = l / S, lane = l - lb * S;
|
||||
bool live = lb < bpw;
|
||||
int bxi = int(gl_WorkGroupID.x) * bpw + lb, byi = int(gl_WorkGroupID.y);
|
||||
live = live && bxi < int(pc.bx);
|
||||
lb = min(lb, MAXB - 1);
|
||||
uvec4 b = live ? blk[pc.in_base + gl_WorkGroupID.z * pc.bslice + uint(byi) * pc.bx + uint(bxi)] : uvec4(0u);
|
||||
Hdr H = header(b, bw, bh);
|
||||
bool work = live && !H.solid;
|
||||
|
||||
if (work) {
|
||||
uvec4 rev = uvec4(bitfieldReverse(b.w), bitfieldReverse(b.z), bitfieldReverse(b.y), bitfieldReverse(b.x));
|
||||
uint wrow = tab[H.quant];
|
||||
for (int j = lane; j < H.wtotal; j += S) s_w[lb][j] = tab_byte(wrow + ise_at(rev, 0, H.wtotal, H.quant, j));
|
||||
uint crow = H.cq == 20 ? 0u : tab[12 + H.cq - 4];
|
||||
int cstart = H.pcount == 1 ? 17 : 29;
|
||||
for (int j = lane; j < H.ccount; j += S) {
|
||||
uint raw = ise_at(b, cstart, H.ccount, H.cq, j);
|
||||
s_cv[lb][j] = H.cq == 20 ? int(raw) : int(tab_byte(crow + raw));
|
||||
}
|
||||
}
|
||||
barrier();
|
||||
if (work && lane < H.pcount) {
|
||||
int p = lane;
|
||||
int cem = p == 0 ? H.cem0 : p == 1 ? H.cem1 : p == 2 ? H.cem2 : H.cem3;
|
||||
int k = p == 0 ? 0 : p == 1 ? H.n0 : p == 2 ? H.n0 + H.n1 : H.n0 + H.n1 + H.n2;
|
||||
int n = ((cem >> 2) + 1) * 2;
|
||||
int v[8];
|
||||
for (int j = 0; j < 8; j++) {
|
||||
v[j] = 0;
|
||||
if (j < n) v[j] = s_cv[lb][k + j];
|
||||
}
|
||||
ivec4 e0, e1;
|
||||
if (!unpack_endpoints(cem, v, e0, e1)) { e0 = ivec4(0xFF, 0, 0xFF, 0xFF); e1 = e0; }
|
||||
if (pc.srgb != 0u) { e0 = e0 << 8 | 0x80; e1 = e1 << 8 | 0x80; }
|
||||
else { e0 *= 257; e1 *= 257; }
|
||||
s_e0[lb][p] = pack16(e0);
|
||||
s_e1[lb][p] = pack16(e1);
|
||||
}
|
||||
barrier();
|
||||
if (!live) return;
|
||||
|
||||
bool small = G < 32;
|
||||
int seed = int((b.x >> 13) & 0x3FFu);
|
||||
int sx = int((1024 + bw / 2) / (bw - 1)), sy = int((1024 + bh / 2) / (bh - 1));
|
||||
for (int t = lane; t < G; t += S) {
|
||||
int x = t % bw, y = t / bw;
|
||||
uint gx = uint(bxi * bw + x), gy = uint(byi * bh + y);
|
||||
if (gx >= pc.width || gy >= pc.height) continue;
|
||||
uint o = H.konst;
|
||||
if (!H.solid) {
|
||||
int p = H.pcount > 1 ? part_of(seed, H.pcount, small ? x << 1 : x, small ? y << 1 : y) : 0;
|
||||
int vx = (sx * x * (H.wx - 1) + 32) >> 6, vy = (sy * y * (H.wy - 1) + 32) >> 6;
|
||||
int fs = vx & 0xF, ft = vy & 0xF;
|
||||
int i0 = (vy >> 4) * H.wx + (vx >> 4);
|
||||
int w11 = (fs * ft + 8) >> 4, w10 = fs - w11, w01 = ft - w11, w00 = 16 - fs - ft + w11;
|
||||
int wcount = H.wx * H.wy;
|
||||
int w[2];
|
||||
for (int pl = 0; pl < 2; pl++) {
|
||||
w[pl] = 0;
|
||||
if (pl > H.dual) continue;
|
||||
int acc = 0;
|
||||
for (int c = 0; c < 4; c++) {
|
||||
int idx = i0 + (c == 0 ? 0 : c == 1 ? 1 : c == 2 ? H.wx : H.wx + 1);
|
||||
int f = c == 0 ? w00 : c == 1 ? w10 : c == 2 ? w01 : w11;
|
||||
if (f == 0 || idx >= wcount) continue;
|
||||
acc += int(s_w[lb][H.dual != 0 ? 2 * idx + pl : idx]) * f;
|
||||
}
|
||||
w[pl] = (acc + 8) >> 4;
|
||||
}
|
||||
uvec2 q0 = s_e0[lb][p], q1 = s_e1[lb][p];
|
||||
o = 0u;
|
||||
for (int c = 0; c < 4; c++) {
|
||||
int wc = c == H.ccs ? w[1] : w[0];
|
||||
o |= uint((ch16(q0, c) * (64 - wc) + ch16(q1, c) * wc + 32) >> 14) << uint(8 * c);
|
||||
}
|
||||
}
|
||||
px[pc.out_base + (gl_WorkGroupID.z * pc.height + gy) * pc.width + gx] = o;
|
||||
}
|
||||
}
|
||||
89
runtime/shaders/bc3_encode.comp
Normal file
89
runtime/shaders/bc3_encode.comp
Normal file
@@ -0,0 +1,89 @@
|
||||
// RGBA8 texels to BC3 blocks on the GPU, one invocation per 4x4 block: the second pass of the ASTC decode when
|
||||
// NX2NAVI_ASTC_BC is on (vk_astc.c), so a decoded ASTC texture takes 8 bits per texel in video memory instead of 32.
|
||||
// Texels past the right or bottom edge repeat the last column or row.
|
||||
//
|
||||
// Colour: the BC1 four-colour block of the box around the block's colours, its diagonal turned to the sign of the
|
||||
// colours' covariance and inset by 1/16 of the range (the bounding-box method of stb_dxt and of the hardware's BC1 recompression),
|
||||
// then each texel takes the nearest of the four palette entries along that axis. Alpha: the eight-value BC3 alpha
|
||||
// block between the block's smallest and largest alpha. The bytes are what the texels hold, so an sRGB image keeps
|
||||
// sRGB-encoded values, as BC3_SRGB expects.
|
||||
// Compiled to runtime/bc3_encode_spv.h by tools/build_shaders.py; vk_astc.c records the dispatch.
|
||||
#version 450
|
||||
|
||||
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
|
||||
|
||||
layout(std430, set = 0, binding = 0) readonly buffer Texels { uint px[]; };
|
||||
layout(std430, set = 0, binding = 1) writeonly buffer Blocks { uvec4 blk[]; };
|
||||
|
||||
layout(push_constant) uniform Params {
|
||||
uint in_base; // first texel of slice 0 (index into px)
|
||||
uint out_base; // first block of slice 0 (uvec4 index into blk)
|
||||
uint width, height;
|
||||
uint bx; // blocks per row
|
||||
uint bslice; // blocks per slice
|
||||
} pc;
|
||||
|
||||
vec4 texel(uint x, uint y, uint z) {
|
||||
x = min(x, pc.width - 1u), y = min(y, pc.height - 1u);
|
||||
return unpackUnorm4x8(px[pc.in_base + (z * pc.height + y) * pc.width + x]) * 255.0;
|
||||
}
|
||||
|
||||
uint to565(vec3 c) {
|
||||
uvec3 q = uvec3(round(clamp(c, 0.0, 255.0) * vec3(31.0, 63.0, 31.0) / 255.0));
|
||||
return q.r << 11 | q.g << 5 | q.b;
|
||||
}
|
||||
|
||||
vec3 from565(uint v) {
|
||||
return vec3(float(v >> 11 & 31u) * 255.0 / 31.0, float(v >> 5 & 63u) * 255.0 / 63.0, float(v & 31u) * 255.0 / 31.0);
|
||||
}
|
||||
|
||||
void main() {
|
||||
uvec3 id = gl_GlobalInvocationID;
|
||||
uint by = (pc.height + 3u) / 4u;
|
||||
if (id.x >= pc.bx || id.y >= by) return;
|
||||
vec4 t[16];
|
||||
vec4 lo = vec4(255.0), hi = vec4(0.0), mean = vec4(0.0);
|
||||
for (uint i = 0u; i < 16u; i++) {
|
||||
t[i] = texel(id.x * 4u + (i & 3u), id.y * 4u + (i >> 2), id.z);
|
||||
lo = min(lo, t[i]), hi = max(hi, t[i]), mean += t[i];
|
||||
}
|
||||
mean /= 16.0;
|
||||
|
||||
// alpha: a0 > a1 selects the eight-value mode; index 0 is a0, 1 is a1, 2..7 the six steps from a0 to a1
|
||||
uint a0 = uint(hi.a), a1 = uint(lo.a);
|
||||
uvec2 ab = uvec2(a0 | a1 << 8, 0u);
|
||||
if (a0 > a1) {
|
||||
float range = float(a0 - a1);
|
||||
for (uint i = 0u; i < 16u; i++) {
|
||||
uint s = uint(clamp(round((float(a0) - t[i].a) * 7.0 / range), 0.0, 7.0)); // 0 = a0 .. 7 = a1
|
||||
uint code = s == 0u ? 0u : s == 7u ? 1u : s + 1u;
|
||||
uint bit = 16u + 3u * i;
|
||||
if (bit < 32u) ab.x |= code << bit;
|
||||
if (bit + 3u > 32u) ab.y |= code >> (32u - min(bit, 32u)) << (bit > 32u ? bit - 32u : 0u);
|
||||
}
|
||||
}
|
||||
|
||||
// colour: the box diagonal along the covariance of red and blue with green
|
||||
vec3 cmin = lo.rgb, cmax = hi.rgb;
|
||||
float crg = 0.0, cbg = 0.0;
|
||||
for (uint i = 0u; i < 16u; i++) {
|
||||
vec3 d = t[i].rgb - mean.rgb;
|
||||
crg += d.r * d.g, cbg += d.b * d.g;
|
||||
}
|
||||
if (crg < 0.0) { float s = cmin.r; cmin.r = cmax.r; cmax.r = s; }
|
||||
if (cbg < 0.0) { float s = cmin.b; cmin.b = cmax.b; cmax.b = s; }
|
||||
vec3 inset = (cmax - cmin) / 16.0;
|
||||
uint c0 = to565(cmax - inset), c1 = to565(cmin + inset);
|
||||
uint idx = 0u;
|
||||
if (c0 != c1) {
|
||||
if (c0 < c1) { uint s = c0; c0 = c1; c1 = s; } // c0 > c1: the four-colour mode
|
||||
vec3 e0 = from565(c0), e1 = from565(c1), axis = e1 - e0;
|
||||
float len2 = max(dot(axis, axis), 1e-6);
|
||||
for (uint i = 0u; i < 16u; i++) {
|
||||
uint s = uint(clamp(round(dot(t[i].rgb - e0, axis) / len2 * 3.0), 0.0, 3.0)); // 0 = e0 .. 3 = e1
|
||||
uint code = s == 0u ? 0u : s == 3u ? 1u : s + 1u; // palette order e0, e1, 2/3 e0 + 1/3 e1, 1/3 e0 + 2/3 e1
|
||||
idx |= code << (2u * i);
|
||||
}
|
||||
}
|
||||
blk[pc.out_base + id.z * pc.bslice + id.y * pc.bx + id.x] = uvec4(ab.x, ab.y, c0 | c1 << 16, idx);
|
||||
}
|
||||
23
runtime/shaders/color_grade.comp
Normal file
23
runtime/shaders/color_grade.comp
Normal file
@@ -0,0 +1,23 @@
|
||||
#version 450
|
||||
// Colour adjustment on the present (runtime/vk_color.c): the game's picture, crop rectangle at offset, into an RGBA16F
|
||||
// image the FSR 1 pass or the blit then scales to the window. Brightness, contrast around mid-grey, saturation against
|
||||
// Rec. 709 luma, then a power curve: the order and ranges of STS78721's colour profiles (issue #136).
|
||||
layout(local_size_x = 8, local_size_y = 8) in;
|
||||
layout(binding = 0) uniform sampler2D src;
|
||||
layout(binding = 1, rgba16f) uniform writeonly image2D dst;
|
||||
layout(push_constant) uniform P {
|
||||
ivec2 offset; // the crop rectangle's corner in src
|
||||
ivec2 size; // its size, the size of dst
|
||||
vec4 adj; // brightness (added), contrast, saturation, gamma (exponent)
|
||||
} pc;
|
||||
|
||||
void main() {
|
||||
ivec2 p = ivec2(gl_GlobalInvocationID.xy);
|
||||
if (any(greaterThanEqual(p, pc.size))) return;
|
||||
vec3 c = texelFetch(src, p + pc.offset, 0).rgb;
|
||||
c += pc.adj.x;
|
||||
c = (c - 0.5) * pc.adj.y + 0.5;
|
||||
c = mix(vec3(dot(c, vec3(0.2126, 0.7152, 0.0722))), c, pc.adj.z);
|
||||
c = pow(max(c, vec3(0.0)), vec3(pc.adj.w));
|
||||
imageStore(dst, p, vec4(clamp(c, 0.0, 1.0), 1.0));
|
||||
}
|
||||
67
runtime/shaders/contact_shadow.comp
Normal file
67
runtime/shaders/contact_shadow.comp
Normal file
@@ -0,0 +1,67 @@
|
||||
#version 450
|
||||
// Screen-space contact shadows (runtime/vk_contact.c). Each pixel of the game's lit scene colour marches a short way
|
||||
// toward the sun through the linear depth target; when the ray passes behind something close in front of it, the pixel
|
||||
// is in a contact shadow the shadow maps are too coarse to show, and its colour is darkened.
|
||||
layout(local_size_x = 8, local_size_y = 8) in;
|
||||
|
||||
layout(binding = 0) uniform sampler2D lin_depth; // the game's linear depth: view distance / far
|
||||
layout(binding = 1, r11f_g11f_b10f) uniform image2D scene; // the lit scene colour, darkened in place
|
||||
|
||||
layout(push_constant) uniform Params {
|
||||
vec4 proj; // projection x scale, y scale, far plane, strength (0..1)
|
||||
vec4 light; // view-space direction toward the sun (xyz), march length in metres at 10 m (w)
|
||||
vec4 size; // width, height, 1/width, 1/height
|
||||
vec4 opt; // x: debug (1: write the shadow factor as grey)
|
||||
} pc;
|
||||
|
||||
vec3 view_pos(vec2 uv, float z) { // z: distance in front of the camera
|
||||
vec2 ndc = uv * 2.0 - 1.0;
|
||||
return vec3(ndc.x * z / pc.proj.x, -ndc.y * z / pc.proj.y, -z);
|
||||
}
|
||||
|
||||
vec2 to_uv(vec3 p) {
|
||||
float z = -p.z;
|
||||
return vec2(0.5 + 0.5 * p.x * pc.proj.x / z, 0.5 - 0.5 * p.y * pc.proj.y / z);
|
||||
}
|
||||
|
||||
float ign(vec2 px) { // interleaved gradient noise: a per-pixel start offset that hides the step pattern
|
||||
return fract(52.9829189 * fract(dot(px, vec2(0.06711056, 0.00583715))));
|
||||
}
|
||||
|
||||
void main() {
|
||||
ivec2 px = ivec2(gl_GlobalInvocationID.xy);
|
||||
if (px.x >= int(pc.size.x) || px.y >= int(pc.size.y)) return;
|
||||
float d = texelFetch(lin_depth, px, 0).x;
|
||||
if (d <= 0.0 || d >= 0.999) return; // nothing drawn, or the sky
|
||||
float z = d * pc.proj.z;
|
||||
if (z > 80.0) return;
|
||||
vec2 uv = (vec2(px) + 0.5) * pc.size.zw;
|
||||
vec3 p = view_pos(uv, z);
|
||||
// the march grows with distance, as one pixel covers more ground; thickness keeps thin blades from hiding the sky
|
||||
float len = pc.light.w * clamp(z / 10.0, 0.4, 3.0);
|
||||
float thick = 0.15 + z * 0.01;
|
||||
const int STEPS = 16;
|
||||
float step_len = len / float(STEPS), t = step_len * (0.25 + ign(vec2(px)));
|
||||
float occ = 0.0;
|
||||
for (int i = 0; i < STEPS; i++, t += step_len) {
|
||||
vec3 q = p + pc.light.xyz * t;
|
||||
if (q.z >= -0.05) break;
|
||||
vec2 quv = to_uv(q);
|
||||
if (any(lessThan(quv, vec2(0.0))) || any(greaterThan(quv, vec2(1.0)))) break;
|
||||
float sz = texelFetch(lin_depth, ivec2(quv * pc.size.xy), 0).x * pc.proj.z;
|
||||
float delta = -q.z - sz; // how far the scene at that pixel is in front of the ray point
|
||||
if (delta > 0.02 + z * 0.002 && delta < thick) {
|
||||
occ = 1.0 - float(i) / float(STEPS); // a hit close to the pixel shadows more than one at the end
|
||||
break;
|
||||
}
|
||||
}
|
||||
float fade = 1.0 - smoothstep(40.0, 80.0, z);
|
||||
float k = 1.0 - pc.proj.w * occ * fade;
|
||||
if (pc.opt.x > 0.5) {
|
||||
imageStore(scene, px, vec4(k, k, k, 1.0));
|
||||
return;
|
||||
}
|
||||
if (k >= 0.999) return;
|
||||
vec4 c = imageLoad(scene, px);
|
||||
imageStore(scene, px, vec4(c.rgb * k, c.a));
|
||||
}
|
||||
63
runtime/shaders/dlaa_mv.comp
Normal file
63
runtime/shaders/dlaa_mv.comp
Normal file
@@ -0,0 +1,63 @@
|
||||
#version 450
|
||||
// Camera motion vectors for DLAA (vk_dlaa.c mv_pass): each pixel's depth is taken back to the world through this
|
||||
// frame's view-projection and forward through last frame's; the vector is last frame's window position minus this
|
||||
// frame's, in render pixels, without the jitter (DLSS's default convention). Objects that move on their own get only
|
||||
// the camera's motion.
|
||||
// The camera block is NX2navi's scene uniform block (bound as c4 in the scene's vertex shaders): vec4 rows 3-6 are the
|
||||
// view-projection, rows of a row-major 4x4 with OpenGL clip depth (-w..w). The replay's viewports have negative
|
||||
// heights, so window y grows downwards from ndc y = +1, and depth is z_ndc * 0.5 + 0.5.
|
||||
// With pc.bias > 0 the pass also writes DLSS's bias mask (vk_dlaa.c, "characters"): G-buffer target 0 carries an object
|
||||
// class in green, above 64/255 for characters, creatures and what they hold (Link 253, others 125, held items 160;
|
||||
// terrain 0, foliage 32). A pixel gets pc.bias when a character covers it now, or covered the place its surface came
|
||||
// from last frame (the previous class mask, read through the motion vector, 3x3): there the history holds a moving
|
||||
// character that the camera-only vector does not follow, which is the trail. chars_out keeps this frame's mask for the
|
||||
// next; chars_prev is last frame's (pc.prev_ok 0: none).
|
||||
layout(local_size_x = 8, local_size_y = 8) in;
|
||||
layout(binding = 0) uniform sampler2D depth_tex;
|
||||
layout(binding = 1, rg16f) uniform writeonly image2D mv_out;
|
||||
layout(binding = 2, std140) uniform Cur { vec4 r[16]; } cur;
|
||||
layout(binding = 3, std140) uniform Prev { vec4 r[16]; } prev;
|
||||
layout(binding = 4) uniform sampler2D ids_tex;
|
||||
layout(binding = 5, r8) uniform writeonly image2D mask_out;
|
||||
layout(binding = 6, r8) uniform readonly image2D chars_prev;
|
||||
layout(binding = 7, r8) uniform writeonly image2D chars_out;
|
||||
layout(push_constant) uniform P {
|
||||
vec2 size; // render size in pixels
|
||||
vec2 jitter; // this frame's jitter, pixels, as the scene was drawn (window x right, y down)
|
||||
float bias; // the mask value for character pixels; 0: no mask (bindings 4-7 unused)
|
||||
float prev_ok; // 1: chars_prev holds last frame's class mask at this size
|
||||
} pc;
|
||||
|
||||
mat4 vp(vec4 a, vec4 b, vec4 c, vec4 d) { return transpose(mat4(a, b, c, d)); }
|
||||
|
||||
void main() {
|
||||
ivec2 p = ivec2(gl_GlobalInvocationID.xy);
|
||||
if (p.x >= int(pc.size.x) || p.y >= int(pc.size.y)) return;
|
||||
float d = texelFetch(depth_tex, p, 0).r;
|
||||
vec2 win = vec2(p) + 0.5 - pc.jitter; // where this pixel's surface sits without the jitter
|
||||
vec4 ndc = vec4(win.x / pc.size.x * 2.0 - 1.0, 1.0 - win.y / pc.size.y * 2.0, d * 2.0 - 1.0, 1.0);
|
||||
mat4 c = vp(cur.r[3], cur.r[4], cur.r[5], cur.r[6]);
|
||||
mat4 q = vp(prev.r[3], prev.r[4], prev.r[5], prev.r[6]);
|
||||
vec4 world = inverse(c) * ndc;
|
||||
vec2 mv = vec2(0.0);
|
||||
if (abs(world.w) > 1e-12) {
|
||||
vec4 clip = q * (world / world.w);
|
||||
if (clip.w > 1e-6) {
|
||||
vec2 pn = clip.xy / clip.w;
|
||||
vec2 pwin = vec2((pn.x * 0.5 + 0.5) * pc.size.x, (0.5 - pn.y * 0.5) * pc.size.y);
|
||||
mv = pwin - win;
|
||||
}
|
||||
}
|
||||
imageStore(mv_out, p, vec4(mv, 0.0, 0.0));
|
||||
if (pc.bias <= 0.0) return;
|
||||
float ch = texelFetch(ids_tex, p, 0).g > 64.0 / 255.0 ? 1.0 : 0.0;
|
||||
imageStore(chars_out, p, vec4(ch));
|
||||
float was = 0.0;
|
||||
if (pc.prev_ok > 0.5) {
|
||||
ivec2 q = ivec2(floor(win + mv)), hi = ivec2(pc.size) - 1;
|
||||
for (int dy = -1; dy <= 1; dy++)
|
||||
for (int dx = -1; dx <= 1; dx++)
|
||||
was = max(was, imageLoad(chars_prev, clamp(q + ivec2(dx, dy), ivec2(0), hi)).r);
|
||||
}
|
||||
imageStore(mask_out, p, vec4(max(ch, was) * pc.bias));
|
||||
}
|
||||
19
runtime/shaders/ffx/LICENSE.txt
Normal file
19
runtime/shaders/ffx/LICENSE.txt
Normal file
@@ -0,0 +1,19 @@
|
||||
Copyright (c) 2021 Advanced Micro Devices, Inc. All rights reserved.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
2656
runtime/shaders/ffx/ffx_a.h
Normal file
2656
runtime/shaders/ffx/ffx_a.h
Normal file
File diff suppressed because it is too large
Load Diff
1199
runtime/shaders/ffx/ffx_fsr1.h
Normal file
1199
runtime/shaders/ffx/ffx_fsr1.h
Normal file
File diff suppressed because it is too large
Load Diff
46
runtime/shaders/fsr_easu.comp
Normal file
46
runtime/shaders/fsr_easu.comp
Normal file
@@ -0,0 +1,46 @@
|
||||
// AMD FidelityFX Super Resolution 1 edge-adaptive upscale (EASU) of the game's output to the swapchain size
|
||||
// (runtime/vk_fsr.c). ffx/ffx_a.h and ffx/ffx_fsr1.h are AMD's headers, unmodified (MIT, ffx/LICENSE.txt). Each
|
||||
// invocation writes four pixels of a 16x16 tile, as AMD's sample does; the constants come from FsrEasuConOffset, so the
|
||||
// game's crop rectangle can start anywhere in its image.
|
||||
#version 450
|
||||
#extension GL_GOOGLE_include_directive : require
|
||||
|
||||
layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
|
||||
|
||||
layout(set = 0, binding = 0) uniform sampler2D src;
|
||||
layout(set = 0, binding = 1, rgba16f) uniform writeonly image2D dst;
|
||||
|
||||
layout(push_constant) uniform Params {
|
||||
vec4 view; // crop width, crop height, source image width, source image height
|
||||
vec4 out_; // output width, output height, crop x, crop y
|
||||
} pc;
|
||||
|
||||
#define A_GPU 1
|
||||
#define A_GLSL 1
|
||||
#include "ffx/ffx_a.h"
|
||||
#define FSR_EASU_F 1
|
||||
AF4 FsrEasuRF(AF2 p) { return textureGather(src, p, 0); }
|
||||
AF4 FsrEasuGF(AF2 p) { return textureGather(src, p, 1); }
|
||||
AF4 FsrEasuBF(AF2 p) { return textureGather(src, p, 2); }
|
||||
#include "ffx/ffx_fsr1.h"
|
||||
|
||||
void put(AU2 p, AU4 c0, AU4 c1, AU4 c2, AU4 c3) {
|
||||
if (p.x >= uint(pc.out_.x) || p.y >= uint(pc.out_.y)) return;
|
||||
AF3 c;
|
||||
FsrEasuF(c, p, c0, c1, c2, c3);
|
||||
imageStore(dst, ivec2(p), vec4(c, 1.0));
|
||||
}
|
||||
|
||||
void main() {
|
||||
AU4 c0, c1, c2, c3;
|
||||
FsrEasuConOffset(c0, c1, c2, c3, pc.view.x, pc.view.y, pc.view.z, pc.view.w, pc.out_.x, pc.out_.y, pc.out_.z,
|
||||
pc.out_.w);
|
||||
AU2 p = ARmp8x8(gl_LocalInvocationID.x) + AU2(gl_WorkGroupID.x << 4u, gl_WorkGroupID.y << 4u);
|
||||
put(p, c0, c1, c2, c3);
|
||||
p.x += 8u;
|
||||
put(p, c0, c1, c2, c3);
|
||||
p.y += 8u;
|
||||
put(p, c0, c1, c2, c3);
|
||||
p.x -= 8u;
|
||||
put(p, c0, c1, c2, c3);
|
||||
}
|
||||
41
runtime/shaders/fsr_rcas.comp
Normal file
41
runtime/shaders/fsr_rcas.comp
Normal file
@@ -0,0 +1,41 @@
|
||||
// AMD FidelityFX Super Resolution 1 robust contrast-adaptive sharpening (RCAS) of the EASU result
|
||||
// (runtime/vk_fsr.c). ffx/ffx_a.h and ffx/ffx_fsr1.h are AMD's headers, unmodified (MIT, ffx/LICENSE.txt).
|
||||
#version 450
|
||||
#extension GL_GOOGLE_include_directive : require
|
||||
|
||||
layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
|
||||
|
||||
layout(set = 0, binding = 0) uniform sampler2D src;
|
||||
layout(set = 0, binding = 1, rgba16f) uniform writeonly image2D dst;
|
||||
|
||||
layout(push_constant) uniform Params {
|
||||
vec4 size; // width, height, sharpness in stops (0 = most), unused
|
||||
} pc;
|
||||
|
||||
#define A_GPU 1
|
||||
#define A_GLSL 1
|
||||
#include "ffx/ffx_a.h"
|
||||
#define FSR_RCAS_F 1
|
||||
AF4 FsrRcasLoadF(ASU2 p) { return texelFetch(src, p, 0); }
|
||||
void FsrRcasInputF(inout AF1 r, inout AF1 g, inout AF1 b) {}
|
||||
#include "ffx/ffx_fsr1.h"
|
||||
|
||||
void put(AU2 p, AU4 con) {
|
||||
if (p.x >= uint(pc.size.x) || p.y >= uint(pc.size.y)) return;
|
||||
AF3 c;
|
||||
FsrRcasF(c.r, c.g, c.b, p, con);
|
||||
imageStore(dst, ivec2(p), vec4(c, 1.0));
|
||||
}
|
||||
|
||||
void main() {
|
||||
AU4 con;
|
||||
FsrRcasCon(con, pc.size.z);
|
||||
AU2 p = ARmp8x8(gl_LocalInvocationID.x) + AU2(gl_WorkGroupID.x << 4u, gl_WorkGroupID.y << 4u);
|
||||
put(p, con);
|
||||
p.x += 8u;
|
||||
put(p, con);
|
||||
p.y += 8u;
|
||||
put(p, con);
|
||||
p.x -= 8u;
|
||||
put(p, con);
|
||||
}
|
||||
34
runtime/shaders/hdr_encode.comp
Normal file
34
runtime/shaders/hdr_encode.comp
Normal file
@@ -0,0 +1,34 @@
|
||||
#version 450
|
||||
// HDR10 encode on the present (runtime/vk_hdr.c vk_hdr_encode): the game's picture, crop rectangle at offset, linear
|
||||
// Rec. 709 with the HUD's white at 1.0 (an sRGB view decodes the 8-bit window texture), into an RGBA16F image of PQ
|
||||
// code values in Rec. 2020 that FSR 1 or the blit then scale into the HDR10 swapchain. p.x: nits of 1.0, p.y: the
|
||||
// display's peak in nits, p.z: 1 when the source holds sRGB-encoded values in a UNORM format.
|
||||
// PQ: SMPTE ST 2084; the Rec. 709 to Rec. 2020 matrix: ITU-R BT.2087.
|
||||
layout(local_size_x = 8, local_size_y = 8) in;
|
||||
layout(binding = 0) uniform sampler2D src;
|
||||
layout(binding = 1, rgba16f) uniform writeonly image2D dst;
|
||||
layout(push_constant) uniform P {
|
||||
ivec2 offset;
|
||||
ivec2 size;
|
||||
vec4 p;
|
||||
} pc;
|
||||
|
||||
vec3 srgb_decode(vec3 c) {
|
||||
return mix(c / 12.92, pow((c + 0.055) / 1.055, vec3(2.4)), greaterThan(c, vec3(0.04045)));
|
||||
}
|
||||
|
||||
vec3 pq(vec3 nits) {
|
||||
const float m1 = 0.1593017578125, m2 = 78.84375, c1 = 0.8359375, c2 = 18.8515625, c3 = 18.6875;
|
||||
vec3 y = pow(clamp(nits / 10000.0, 0.0, 1.0), vec3(m1));
|
||||
return pow((c1 + c2 * y) / (1.0 + c3 * y), vec3(m2));
|
||||
}
|
||||
|
||||
void main() {
|
||||
ivec2 at = ivec2(gl_GlobalInvocationID.xy);
|
||||
if (any(greaterThanEqual(at, pc.size))) return;
|
||||
vec3 c = max(texelFetch(src, at + pc.offset, 0).rgb, vec3(0.0));
|
||||
if (pc.p.z > 0.5) c = srgb_decode(clamp(c, 0.0, 1.0));
|
||||
const mat3 to2020 = mat3(0.6274, 0.0691, 0.0164, 0.3293, 0.9195, 0.0880, 0.0433, 0.0114, 0.8956);
|
||||
vec3 nits = min(max(to2020 * c, vec3(0.0)) * pc.p.x, vec3(pc.p.y));
|
||||
imageStore(dst, at, vec4(pq(nits), 1.0));
|
||||
}
|
||||
8
runtime/shaders/hdr_fullscreen.vert
Normal file
8
runtime/shaders/hdr_fullscreen.vert
Normal file
@@ -0,0 +1,8 @@
|
||||
#version 450
|
||||
// One triangle over the whole target, for hdr_ui.frag (runtime/vk_hdr.c vk_hdr_ui_compose).
|
||||
layout(location = 0) out vec2 uv;
|
||||
|
||||
void main() {
|
||||
uv = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2);
|
||||
gl_Position = vec4(uv * 2.0 - 1.0, 0.0, 1.0);
|
||||
}
|
||||
74
runtime/shaders/hdr_tonemap.frag
Normal file
74
runtime/shaders/hdr_tonemap.frag
Normal file
@@ -0,0 +1,74 @@
|
||||
#version 450
|
||||
// The game's tonemap pass for HDR output (runtime/vk_hdr.c vk_hdr_shader_swap puts it in place of the translated
|
||||
// shader when NX2NAVI_HDR=1). Same inputs as the game's: fp_t_tcb_8 (bloom, clamped to 10000 as the game does) plus
|
||||
// fp_t_tcb_A (the scene), weights in c1[0] and the saturation grade in c3[0]. The game maps luminance and each channel
|
||||
// with 1 - e^-x, blends the two by the luminance curve squared, clamps to 0..1 and grades saturation. Here the curve is
|
||||
// the game's up to PIVOT and a straight line with its slope above, so mid-greys look as in SDR and highlights keep
|
||||
// going; the grade works on the colour divided by its largest channel above 1; a roll-off on the largest channel stops
|
||||
// at the display's peak. The output is relative to the HUD's white (1.0), which the present maps to NX2NAVI_HDR_UI_NITS.
|
||||
// Hue correction (hdr.z, NX2NAVI_HDR_HUE): the game's SDR clamp turns bright saturated colours towards white (a torch flame
|
||||
// clips red, then green, and reads yellow-white, the colour its emitted light was tuned to); the extended curve keeps
|
||||
// their ratios, so they stay saturated. From the picture's white upwards the colour moves towards the game's own SDR
|
||||
// result at the HDR luminance, by up to hdr.z (RenoDX's NX2navi mod has the same control).
|
||||
// Approach after RenoDX's NX2navi mod by souperman9 and Musa (github.com/clshortfuse/renodx, src/games/nx2navi, MIT): its
|
||||
// "extended vanilla" curve.
|
||||
// Bindings are those of the game's translated shader: vk_hdr.c rewrites them from the original's names.
|
||||
layout(location = 0) in vec4 uv;
|
||||
layout(location = 0) out vec4 color;
|
||||
|
||||
layout(set = 0, binding = 0, std140) uniform SupportBuffer {
|
||||
vec4 pad[90];
|
||||
vec4 hdr; // byte 1440 (vk_exec_draw.c support_fill): game white / HUD white, peak / game white, hue correction
|
||||
} support_buffer;
|
||||
layout(set = 0, binding = 1) uniform sampler2D fp_t_tcb_8;
|
||||
layout(set = 0, binding = 2) uniform sampler2D fp_t_tcb_A;
|
||||
layout(set = 0, binding = 3, std140) uniform C1 { vec4 v[4096]; } fp_c1;
|
||||
layout(set = 0, binding = 4, std140) uniform C3 { vec4 v[4096]; } fp_c3;
|
||||
|
||||
const float PIVOT = 0.18;
|
||||
|
||||
float curve(float x) { // the game's 1 - e^-x below PIVOT, its tangent there above
|
||||
float y0 = 1.0 - exp(-PIVOT);
|
||||
return x <= PIVOT ? 1.0 - exp(-x) : y0 + exp(-PIVOT) * (x - PIVOT);
|
||||
}
|
||||
|
||||
vec3 curve3(vec3 x) { return vec3(curve(x.r), curve(x.g), curve(x.b)); }
|
||||
|
||||
float maxc(vec3 c) { return max(c.r, max(c.g, c.b)); }
|
||||
|
||||
void main() {
|
||||
vec3 x = min(texture(fp_t_tcb_8, uv.xy).rgb, vec3(10000.0)) + texture(fp_t_tcb_A, uv.xy).rgb;
|
||||
vec4 w = fp_c1.v[0], g = fp_c3.v[0];
|
||||
float game = support_buffer.hdr.x > 0.0 ? support_buffer.hdr.x : 1.0;
|
||||
float peak = support_buffer.hdr.y > 1.0 ? support_buffer.hdr.y : 1.0;
|
||||
|
||||
float lum = max(x.r * 0.2989 + x.g * w.x + x.b * w.y, 0.0);
|
||||
float sdr = 1.0 - exp(-lum);
|
||||
vec3 by_lum = lum > 0.0 ? x * (curve(lum) / lum) : x;
|
||||
vec3 c = max(mix(by_lum, curve3(x), sdr * sdr), vec3(0.0));
|
||||
|
||||
// the game's saturation grade, on the colour scaled into 0..1 by its largest channel
|
||||
float s = max(maxc(c), 1.0);
|
||||
vec3 n = c / s;
|
||||
float m = maxc(n);
|
||||
float d = n.r * 0.6666667 + (n.g + n.b) * w.z - 1.0;
|
||||
float grade = g.y * (1.0 - d * d) + g.x;
|
||||
c = s * ((n - m) * grade + m);
|
||||
|
||||
// the game's own SDR result: the same blend clamped to 0..1, graded
|
||||
vec3 sdr_c = clamp(mix(lum > 0.0 ? x * (sdr / lum) : x, vec3(1.0) - exp(-x), sdr * sdr), 0.0, 1.0);
|
||||
float sm = maxc(sdr_c), sd = sdr_c.r * 0.6666667 + (sdr_c.g + sdr_c.b) * w.z - 1.0;
|
||||
sdr_c = (sdr_c - sm) * (g.y * (1.0 - sd * sd) + g.x) + sm;
|
||||
float hue = clamp(support_buffer.hdr.z, 0.0, 1.0);
|
||||
const vec3 Y = vec3(0.2126, 0.7152, 0.0722); // Rec. 709 luminance
|
||||
float yc = dot(c, Y), ys = dot(sdr_c, Y);
|
||||
if (hue > 0.0 && ys > 1e-5) c = mix(c, max(sdr_c, vec3(0.0)) * (yc / ys), hue * smoothstep(0.5, 1.5, maxc(c)));
|
||||
|
||||
// roll-off of the largest channel from half the peak to the peak
|
||||
float top = maxc(c), knee = 0.5 * peak;
|
||||
if (top > knee) {
|
||||
float r = peak - knee;
|
||||
c *= (knee + r * (1.0 - exp(-(top - knee) / r))) / top;
|
||||
}
|
||||
color = vec4(max(c, vec3(0.0)) * game, 0.0);
|
||||
}
|
||||
27
runtime/shaders/hdr_ui.frag
Normal file
27
runtime/shaders/hdr_ui.frag
Normal file
@@ -0,0 +1,27 @@
|
||||
#version 450
|
||||
// The in-game menu on an HDR10 swapchain (runtime/vk_hdr.c vk_hdr_ui_compose): ImGui drew it into an RGBA8 image,
|
||||
// premultiplied sRGB colour over a clear 0; this turns each pixel back into its colour, puts its white at p.x nits and
|
||||
// writes the PQ code with its coverage, which the pipeline blends over the picture (in PQ code values, as the menu
|
||||
// blends in sRGB code values on an SDR swapchain). PQ: SMPTE ST 2084; Rec. 709 to Rec. 2020: ITU-R BT.2087.
|
||||
layout(location = 0) in vec2 uv;
|
||||
layout(location = 0) out vec4 color;
|
||||
layout(binding = 0) uniform sampler2D ui;
|
||||
layout(push_constant) uniform P { vec4 p; } pc;
|
||||
|
||||
vec3 srgb_decode(vec3 c) {
|
||||
return mix(c / 12.92, pow((c + 0.055) / 1.055, vec3(2.4)), greaterThan(c, vec3(0.04045)));
|
||||
}
|
||||
|
||||
vec3 pq(vec3 nits) {
|
||||
const float m1 = 0.1593017578125, m2 = 78.84375, c1 = 0.8359375, c2 = 18.8515625, c3 = 18.6875;
|
||||
vec3 y = pow(clamp(nits / 10000.0, 0.0, 1.0), vec3(m1));
|
||||
return pow((c1 + c2 * y) / (1.0 + c3 * y), vec3(m2));
|
||||
}
|
||||
|
||||
void main() {
|
||||
vec4 u = texture(ui, uv);
|
||||
if (u.a <= 0.0) discard;
|
||||
vec3 c = srgb_decode(clamp(u.rgb / u.a, 0.0, 1.0));
|
||||
const mat3 to2020 = mat3(0.6274, 0.0691, 0.0164, 0.3293, 0.9195, 0.0880, 0.0433, 0.0114, 0.8956);
|
||||
color = vec4(pq(max(to2020 * c, vec3(0.0)) * pc.p.x), u.a);
|
||||
}
|
||||
27
runtime/shaders/wb_merge.comp
Normal file
27
runtime/shaders/wb_merge.comp
Normal file
@@ -0,0 +1,27 @@
|
||||
#version 450
|
||||
// Write-back merge of a VRAM mirror (vk_memory.c, wb_merge_record): copies a 32-bit word of what the GPU left in the
|
||||
// mirror into the pool's imported buffer (guest memory) only where it differs from the snapshot taken before the
|
||||
// submission's GPU work, so words the GPU did not change keep what the CPU wrote meanwhile.
|
||||
// One invocation per word of the listed ranges; ranges are sorted by their first invocation (x: word offset in the
|
||||
// mirror and imported bindings, y: word offset in the snapshot binding, z: words, w: first invocation).
|
||||
layout(local_size_x = 64) in;
|
||||
layout(std430, set = 0, binding = 0) readonly buffer Post { uint post[]; };
|
||||
layout(std430, set = 0, binding = 1) readonly buffer Pre { uint pre[]; };
|
||||
layout(std430, set = 0, binding = 2) buffer Dst { uint dst[]; };
|
||||
layout(std430, set = 0, binding = 3) readonly buffer Ranges { uvec4 rng[]; };
|
||||
layout(push_constant) uniform Push { uint nranges, total; } pc;
|
||||
|
||||
void main() {
|
||||
uint i = gl_GlobalInvocationID.x;
|
||||
if (i >= pc.total) return;
|
||||
uint lo = 0, hi = pc.nranges;
|
||||
while (hi - lo > 1) {
|
||||
uint m = (lo + hi) >> 1;
|
||||
if (rng[m].w <= i) lo = m;
|
||||
else hi = m;
|
||||
}
|
||||
uvec4 r = rng[lo];
|
||||
uint k = i - r.w;
|
||||
uint v = post[r.x + k];
|
||||
if (v != pre[r.y + k]) dst[r.x + k] = v;
|
||||
}
|
||||
Reference in New Issue
Block a user