Files
larp/shaders/replace/b96880482a17f632.compute.hlsl

333 lines
18 KiB
HLSL

// b96880482a17f632, compute: CS_ProjDecalReduceClassification_Vectorized. One group per tile of 8 x 8
// pixels in the list of tiles the classification left (table 1 +0x60, a word a tile): for each pixel,
// the triangle the visibility texture (table 1 +0x20) names is rebuilt from its object's index and
// vertex buffers (descriptors in the view heap), its position and normal interpolated at the pixel's
// barycentrics, and the projected decals of the pixel's decal mask (128 bits, in the record at table 0
// +0x500) are tested one by one: a decal whose box holds the position (a unit sphere after the
// decal's scale) and whose direction faces the normal is a hit. The tile's four mask words (table 1
// +0x40) are written all ones, and a tile in which no pixel has a hit is taken out of the list (its
// word written -1). What each value is (visibility texel, object, decal, box) is inferred from how the
// code uses it; the arithmetic and the addresses are the console's.
//
// Written by hand from the console code (its disassembly; offsets below are the console program's),
// for host waves of any width; the translation's DXIL
// was the cross-check. The interface is the translation's (README "The one rule"): user data b0
// space0, 8 dwords (4: table 0, 5: table 1, 7: Other(260), the first group of this part of the
// dispatch); t252 and t253 space1; views from the heap typed as the translation's handles, at the slots
// it reads, with its clamps. t255 (strides) is in the interface and read by neither.
//
// What the console's one wave of 64 shares, and what is left of it here:
// - 0x60-0x68: the tile's word, read by the first lane; -1 ends the group. Every thread reads the same
// word and leaves on its own: no barrier.
// - 0xd8-0xdc: a wave with no pixel inside the target ends. The tile's first pixel (thread 0) is its
// lowest x and y, so that is "thread 0 is outside", which every thread computes: no barrier.
// - 0x88 / 0x16dc-0x16e8: threads 0-3 write -1 to group memory words 0-3, nothing else writes them, and
// each reads back its own word and stores it to the tile's mask. No exchange: each of those threads
// (that is inside the target) stores 0xFFFFFFFF.
// - The waterfall loops (v_readfirstlane_b32 / s_cbranch_execnz, 0x260-0x1394) serve one descriptor per
// pass to the lanes that share it: here each thread reads through its own descriptor
// (NonUniformResourceIndex). No exchange.
// - 0x16a0-0x16b8: group memory word 4 zeroed, every thread inside the target adds its hit (0 or 1),
// the sum read back; 0x16f8-0x171c: thread 0, with the sum 0, stores -1 to the tile's word. The one
// real dependence between threads. Here each thread writes its hit to its own word of `pixel_hit_of`
// (all 64 write, so no word needs clearing first), one GroupMemoryBarrierWithGroupSync, and thread 0
// ORs the 64 words. That barrier is the program's only one; a group that leaves at either test above
// never reaches it.
//
// Arithmetic as the console does it: v_mul_legacy_f32 is mul_legacy (0 times anything is 0),
// v_fma_f32 / v_fmac_f32 is mad, v_mad_legacy_f32 is mul_legacy then an add, the clamp modifier is
// saturate, v_rcp_f32 is 1 / x, v_cvt_i32_f32 is int(); every float value is precise. The stride of each
// title buffer (48, 8, 12, 1472, 420, 16, 4 bytes) is the console's, as the translation takes it from the
// program's resource list. The clamps are the translation's for this host's view heap of 1,000,000
// slots.
//
// Not reproduced, as in the translation: the console buffer views' bound on an element index (idxen
// against num_records; here a raw view's size bounds the bytes); v_rcp_f32 and v_cvt_i32_f32 are the
// host's 1 / x and float-to-int, which may differ from the console's in the last bit and out of range.
//
// Names are from what the console code does with each value; what the title calls them is not known.
#include "view_heap_layout.hlsli"
cbuffer UserData : register(b0, space0) { uint4 ud[2]; };
// The lists of host view slots view tables are read through (README "Descriptors").
ByteAddressBuffer table_lists : register(t252, space1);
// The bytes (0, 4, 8 or 12) by which the raw view at each host view heap slot begins before the
// console's view (larp_shader::emit::SLOT_SHIFTS): added to every byte offset through a view from
// the heap, except where the sum passes 2^32, as the translation does.
ByteAddressBuffer view_shifts : register(t253, space1);
static const uint TITLE_SLOTS_END = TITLE_LIMIT; // the null slot that ends the title's slots
static const uint MAX_BYTE = 2147483584u; // the translation's bound on a raw byte offset
static const uint MAX_ELEMENT = 134217727u; // the translation's bound on a typed element index
// The view slot of the descriptor at byte `b` of a table (`table` its user-data dword).
uint table_entry(uint table, uint b) { return table_lists.Load(min(((table + b) >> 5u) << 2u, MAX_BYTE)); }
uint table_slot(uint table, uint b) { return min(table_entry(table, b), NULL_SLOT); }
// The view slot of the descriptor the console loads from the resource heap at byte `b` (s[6:7] + b).
uint heap_slot(uint b) { return min(min(b >> 5u, TITLE_SLOTS_END), NULL_SLOT); }
uint shifted(uint slot, uint off) { uint s = view_shifts.Load(min(slot, 2097151u) * 4u); uint b = off + s; return b < s ? off : b; }
uint at(uint slot, uint off) { return min(shifted(slot, off), MAX_BYTE); }
ByteAddressBuffer srv(uint i) { ByteAddressBuffer r = ResourceDescriptorHeap[i]; return r; }
ByteAddressBuffer srv_nu(uint i) { ByteAddressBuffer r = ResourceDescriptorHeap[NonUniformResourceIndex(i)]; return r; }
RWByteAddressBuffer uav(uint i) { RWByteAddressBuffer r = ResourceDescriptorHeap[i]; return r; }
Buffer<float4> typed_nu(uint i) { Buffer<float4> r = ResourceDescriptorHeap[NonUniformResourceIndex(i)]; return r; }
Texture2D<float4> tex(uint i) { Texture2D<float4> r = ResourceDescriptorHeap[i]; return r; }
// A table's constant view, by its dword of the table's list (t252): with the top bit set its constants
// lie in the heap view at the offset in its low bits times 16; otherwise the dword is the slot of a raw
// view that begins at them. Row `row` of them (16 bytes).
uint4 table_cbv(uint e, uint row)
{
bool placed = (e >> 31u) != 0u;
ByteAddressBuffer b = ResourceDescriptorHeap[min(placed ? HEAP_VIEW_SLOT : e, NULL_SLOT)];
return b.Load4((placed ? (e << 4u) : 0u) + (row << 4u));
}
float mul_legacy(float a, float b) { precise float p = a * b; return (a == 0.0f || b == 0.0f) ? 0.0f : p; }
// The descriptors of table 0 (user-data dword 4) and table 1 (dword 5) the program reads, and the
// constants. Group-uniform.
struct Bindings
{
uint objects; // table 0 +0xd80: 12 bytes an object (UAV): index descriptor, vertex descriptor, fields
uint instances; // table 0 +0x560: 48 bytes an instance; the dword at 32 holds the key
uint key_views; // table 0 +0x3c0: 8 bytes a key; the dword at 0 is a view, -1 none
uint views; // table 0 +0x500: 1472 bytes a view
uint decals; // table 0 +0x220: 420 bytes a decal
uint decal_boxes; // table 0 +0x760: texture, texel (decal, view) the decal's packed position
uint4 c1; // the constants' row 1: x and y the bit fields of the object's fields word
};
// The three vertex indices of triangle `tri` of the object whose index descriptor is `w0`.
uint3 triangle_indices(uint w0, uint tri)
{
uint s = heap_slot(w0 << 5u);
if (int(w0) >= 0) {
// 0x250-0x320: a typed index buffer, three entries a triangle.
Buffer<float4> ib = typed_nu(s);
uint i = 3u * tri; // v_mul_u32_u24; tri < 2^19
return uint3(asuint(ib.Load(min(i, MAX_ELEMENT)).x), asuint(ib.Load(min(i + 1u, MAX_ELEMENT)).x),
asuint(ib.Load(min(i + 2u, MAX_ELEMENT)).x));
}
// 0x328-0x600: a packed buffer: a header, a dword per triangle (its group and three 7-bit
// offsets), per group a base, and 16-bit indices.
ByteAddressBuffer b = srv_nu(s);
uint e = b.Load(at(s, (((b.Load(at(s, 0u)) << 4u) & 0x1ffff0u) | 12u) + (tri << 2u)));
uint2 h0 = b.Load2(at(s, 0u));
uint2 h1 = b.Load2(at(s, ((e >> 17u) & 0x7ff0u) + 12u));
uint base = (((h0.x << 4u) & 0x1ffff0u) | 12u) + (h0.y << 2u) + ((((h1.x << 2u) & 0x10000u) | (h1.y & 0xffffu)) << 1u);
uint3 off = base + uint3((e << 1u) & 0xfeu, (e >> 6u) & 0xfeu, (e >> 13u) & 0xfeu);
uint3 idx;
[unroll] for (uint k = 0u; k < 3u; k++) {
uint w = b.Load(at(s, off[k] & 0x7ffffffcu));
idx[k] = ((off[k] >> 1u) & 1u) == 1u ? (w >> 16u) : (w & 0xffffu);
}
return idx;
}
// The vertex layout of the object whose vertex descriptor is `w1`: two words at bytes 0 (positions)
// and 8 (normals), each a format in bits 0-4, a stride ((w >> 3) & 60) and an offset ((w >> 7) &
// 0x1fffffc); for quantised positions a base (96), an axis order (108) and a scale (112).
struct Vertices
{
uint slot;
uint pos_word, nrm_word;
float3 qbase; uint qorder; float3 qscale;
};
// 0x660-0x860 (vertex 0; 0xb14-0xd18, 0xf7c-0x1188 the others): format 3, three floats; format 2, 64
// bits quantised (24, 24, 16), each a mad of the base and scale, the axes ordered by `qorder`, then
// reversed; any other format, zero.
float3 vertex_position(Vertices v, uint i)
{
uint fmt = v.pos_word & 31u;
uint off = ((v.pos_word >> 3u) & 60u) * i + ((v.pos_word >> 7u) & 0x1fffffcu);
ByteAddressBuffer b = srv_nu(v.slot);
if (fmt == 3u) return asfloat(b.Load3(at(v.slot, off)));
if (fmt != 2u) return float3(0.0f, 0.0f, 0.0f);
uint2 r = b.Load2(at(v.slot, off));
precise float x = mad(float(r.x >> 8u), v.qscale.x, v.qbase.x);
precise float y = mad(float((r.x & 0xffu) | ((r.y & 0xffffu) << 8u)), v.qscale.y, v.qbase.y); // v_perm_b32 0x0c060500
precise float z = mad(float(r.y >> 16u), v.qscale.z, v.qbase.z);
precise float3 p = float3(x, y, z);
if (v.qorder == 1u) p = float3(x, z, y);
else if (v.qorder != 0u) p = float3(z, y, x);
return p.zyx;
}
// 0x8a8-0x9b8: octahedral decode of x, y in [-1, 1], normalised.
float3 octahedral(precise float x, precise float y)
{
precise float z = (1.0f - abs(x)) - abs(y);
precise float t = saturate(-z);
precise float y2 = ((0.0f <= y) ? -t : t) + y;
precise float x2 = ((0.0f <= x) ? -t : t) + x;
precise float l = mad(z, z, mad(x2, x2, mul_legacy(y2, y2)));
precise float r = rsqrt(l);
return float3(mul_legacy(x2, r), mul_legacy(y2, r), mul_legacy(r, z));
}
// 0x8a8-0xb0c (vertex 0; 0xd20-0xf74, 0x1190-0x13e4 the others): format 1, octahedral from 10:10 bits
// (the dword negative) or 8:8; format 7, three 10-bit signed-normalised; any other format, zero.
float3 vertex_normal(Vertices v, uint i)
{
uint fmt = v.nrm_word & 31u;
if (fmt != 1u && fmt != 7u) return float3(0.0f, 0.0f, 0.0f);
uint off = ((v.nrm_word >> 3u) & 60u) * i + ((v.nrm_word >> 7u) & 0x1fffffcu);
uint n = srv_nu(v.slot).Load(at(v.slot, off));
const float k10 = asfloat(0x3b002008u); // 2 / 1023
const float k8 = asfloat(0x3c008081u); // 2 / 255
if (fmt == 7u)
return float3(mad(k10, float(n & 0x3ffu), -1.0f), mad(k10, float((n >> 10u) & 0x3ffu), -1.0f),
mad(k10, float((n >> 20u) & 0x3ffu), -1.0f));
if (int(n) < 0)
return octahedral(mad(k10, float(n & 0x3ffu), -1.0f), mad(k10, float((n >> 10u) & 0x3ffu), -1.0f));
return octahedral(mad(k8, float(n & 0xffu), -1.0f), mad(k8, float((n >> 8u) & 0xffu), -1.0f));
}
// Whether a decal of the pixel's mask holds the pixel (0xec-0x1690): the console's v8 at 0x1698.
bool pixel_hit(Bindings g, uint px, uint py, uint tile_texture)
{
// 0xec: the visibility texel: x the triangle (19 bits), y the instance (high 16) and object + 1
// (low 16), z and w two barycentrics (24 bits each).
uint4 vis = asuint(tex(tile_texture).Load(int3(px, py, 0)));
if (vis.y == 0u) return false;
// 0x110-0x1b8.
uint obj = (vis.y & 0xffffu) - 1u;
uint key = srv(g.instances).Load(at(g.instances, (vis.y >> 16u) * 48u + 32u)) & 0xffffu;
uint fields = uav(g.objects).Load(at(g.objects, obj * 12u + 8u));
uint view = srv(g.key_views).Load(at(g.key_views, key * 8u));
uint mask_set = (fields & g.c1.x) >> (firstbitlow(g.c1.x) & 31u);
if (!(key != 0u && view != 0xffffffffu && mask_set < 64u)) return false;
// 0x1bc-0x238.
uint w0 = uav(g.objects).Load(at(g.objects, obj * 12u));
uint w1 = uav(g.objects).Load(at(g.objects, obj * 12u + 4u));
const float k24 = asfloat(0x33800001u);
precise float bz = float(vis.z & 0xffffffu);
precise float bw = float(vis.w & 0xffffffu);
precise float b1 = saturate(mul_legacy(k24, bw)); // v4
precise float b0 = saturate(mul_legacy(k24, bz)); // v6
precise float b2 = saturate(mul_legacy(-bz, k24) + 1.0f) - b1; // v3: v_mad_legacy_f32 clamp, then v_sub
uint decal_part = (fields & g.c1.y) >> (firstbitlow(g.c1.y) & 31u);
uint3 idx = triangle_indices(w0, vis.x & 0x7ffffu);
Vertices v;
v.slot = heap_slot(w1 << 5u);
ByteAddressBuffer vb = srv_nu(v.slot);
v.pos_word = vb.Load(at(v.slot, 0u));
v.nrm_word = vb.Load(at(v.slot, 8u));
v.qbase = 0.0f; v.qorder = 0u; v.qscale = 0.0f;
if ((v.pos_word & 31u) == 2u) {
uint4 q = vb.Load4(at(v.slot, 96u));
v.qbase = asfloat(q.xyz); v.qorder = q.w;
v.qscale = asfloat(vb.Load3(at(v.slot, 112u)));
}
// 0x13ec-0x1490: position and normal at the pixel.
float3 p0 = vertex_position(v, idx.x), p1 = vertex_position(v, idx.y), p2 = vertex_position(v, idx.z);
float3 n0 = vertex_normal(v, idx.x), n1 = vertex_normal(v, idx.y), n2 = vertex_normal(v, idx.z);
precise float3 pos, nrm;
[unroll] for (uint c = 0u; c < 3u; c++) {
pos[c] = mad(p2[c], b2, mad(p1[c], b1, mul_legacy(p0[c], b0)));
nrm[c] = mad(n2[c], b2, mad(n1[c], b1, mul_legacy(n0[c], b0)));
}
// 0x13ec-0x14cc: the view's record: the decal mask chosen by mask_set (448), the origin (128) and
// extent (144) of the boxes' packed positions, the first decal of the mask (160).
uint vr = view * 1472u;
ByteAddressBuffer views = srv(g.views);
uint4 mask = views.Load4(at(g.views, vr + (mask_set << 4u) + 448u));
float3 origin = asfloat(views.Load3(at(g.views, vr + 128u)));
float3 extent = asfloat(views.Load3(at(g.views, vr + 144u)));
uint first_decal = views.Load(at(g.views, vr + 160u));
precise float3 rel = pos - origin;
precise float3 cell = float3(mul_legacy(extent.x, asfloat(0x3a001002u)), // 1 / 2047
mul_legacy(extent.y, asfloat(0x3a802008u)), // 1 / 1023
mul_legacy(extent.z, asfloat(0x3a001002u)));
uint part = 0xc0u + 0x5cu * decal_part;
// 0x14d8-0x1680: each set bit of the mask, lowest first; the first hit ends it.
ByteAddressBuffer decals = srv(g.decals);
Texture2D<float4> boxes = tex(g.decal_boxes);
[loop] for (uint word = 0u; word < 4u; word++) {
uint bits = word == 3u ? mask.w : word == 2u ? mask.z : word == 1u ? mask.y : mask.x; // 0x164c-0x1674
[loop] while (bits != 0u) {
uint bit = firstbitlow(bits);
bits ^= 1u << bit;
uint r = (bit + first_decal + (word << 5u)) * 420u;
int bx = int(asfloat(decals.Load(at(g.decals, r))));
uint half_word = decals.Load(at(g.decals, r + part));
precise float scale = asfloat(decals.Load(at(g.decals, r + 12u)));
precise float inv = 1.0f / mad(f16tof32(half_word), scale, 0.0f); // v_fma_mix_f32, v_rcp_f32
uint box = asuint(boxes.Load(int3(bx, view, 0)).x);
precise float dy = mul_legacy(mad(float((box >> 11u) & 0x3ffu) + 0.5f, -cell.y, rel.y), inv);
precise float dx = mul_legacy(mad(float(box >> 21u) + 0.5f, -cell.x, rel.x), inv);
precise float dz = mul_legacy(mad(float(box & 0x7ffu) + 0.5f, -cell.z, rel.z), inv);
precise float d2 = mad(dz, dz, mad(dx, dx, mul_legacy(dy, dy)));
if (1.0f > d2) {
float3 dir = asfloat(decals.Load3(at(g.decals, r + 16u)));
precise float facing = mad(dir.z, nrm.z, mad(dir.x, nrm.x, mul_legacy(dir.y, nrm.y)));
precise bool faces = !(0.0f >= facing); // v_cmp_nge_f32: a NaN is a hit
if (faces) return true;
}
}
}
return false;
}
groupshared uint pixel_hit_of[64];
[numthreads(64, 1, 1)]
void main(uint3 group_id : SV_GroupID, uint t : SV_GroupIndex)
{
uint table0 = ud[1].x, table1 = ud[1].y;
// 0x1c-0x68: the tile's word, at the constants' z * w - 1 - group in the list.
uint4 c0 = table_cbv(table_entry(table1, 128u), 0u);
uint tiles = table_slot(table1, 96u);
uint tile = c0.w * c0.z + ~(group_id.x + ud[1].w);
uint word = uav(tiles).Load(at(tiles, tile * 4u));
if (word == 0xffffffffu) return;
// 0x6c-0xdc: the pixel; the group ends when no pixel of the tile is inside the target (c0.x, c0.y).
uint x0 = (word << 3u) & 0x7fff8u, y0 = ((word >> 16u) & 0xffffu) << 3u;
if (c0.y <= y0 || c0.x <= x0) return;
uint px = (t & 7u) | x0, py = y0 + (t >> 3u);
bool inside = !(c0.y <= py || c0.x <= px);
bool hit = false;
if (inside) {
Bindings g;
g.objects = table_slot(table0, 3456u);
g.instances = table_slot(table0, 1376u);
g.key_views = table_slot(table0, 960u);
g.views = table_slot(table0, 1280u);
g.decals = table_slot(table0, 544u);
g.decal_boxes = table_slot(table0, 1888u);
g.c1 = table_cbv(table_entry(table1, 128u), 1u);
hit = pixel_hit(g, px, py, table_slot(table1, 32u));
// 0x16c0-0x16e8: the tile's mask, all ones.
if (t < 4u) {
uint masks = table_slot(table1, 64u);
uav(masks).Store(at(masks, tile * 16u + (t << 2u)), 0xffffffffu);
}
}
// 0x16a0-0x171c: a tile with no hit leaves the list.
pixel_hit_of[t] = hit ? 1u : 0u;
GroupMemoryBarrierWithGroupSync();
if (t == 0u) {
uint any_hit = 0u;
[unroll] for (uint i = 0u; i < 64u; i++) any_hit |= pixel_hit_of[i];
if (any_hit == 0u) uav(tiles).Store(at(tiles, tile * 4u), 0xffffffffu);
}
}