mirror of
https://github.com/gamingwithhomie/larp.git
synced 2026-12-06 07:38:36 +00:00
210 lines
13 KiB
HLSL
210 lines
13 KiB
HLSL
// 07fb4c723c4241ed, compute: CS_CombineFacingCountWithNearPlaneContribution_neither, the underwater mask's
|
|
// combine for the 16x16 tiles of the list it is given (pass_water_underwater_mask_build_b_q 0x14291D3A0).
|
|
// One group a tile, one thread a pixel (numthreads 16, 16, 1; tid the thread's place in its group).
|
|
//
|
|
// The tile: the word e at element 2 c + g of table +0x40's typed buffer (0x44-0x60; c the dword at byte 0x18
|
|
// of table +0xa0's constants, 0x30; g the group, s6, plus the first group of this part), the same in every
|
|
// lane (0x74); its x at (e << 4) & 0xffff0, its row at e >> 16 (0x98-0xa0). The pixel: x = that x + tid.x
|
|
// (0xb0), y = row * 16 + tid.y (0xa8).
|
|
//
|
|
// Per pixel (0xb4-0x264): the facing count, the dword of table +0x20's texture at (x, y) (0xbc: low and
|
|
// high 16 bits), and the near-plane term t: 1 if the pixel's near-plane point lies inside one of the volumes
|
|
// of table +0x0's buffer, else 0. The buffer's dword 0 is the count n (0xb4); n == 0 gives t = 0 (0xc8,
|
|
// 0xcc). Else the point (0xd4-0x188): u = 2 x / w - 1, v = 1 - 2 y / h (w, h the texture's size,
|
|
// 0xdc-0x148), p = r3.w * (r0.x u + r0.y v + r0.z, r1..., r2...) + r3.xyz, rows r0-r3 of table +0xc0's
|
|
// constants (0x130); then volume k = 1, 2, ... (0x194, 0x198) at byte 44 k, eleven dwords d0-d10
|
|
// (0x19c-0x1ac), five planes with f16 coefficients and f32 offsets (0x1b8-0x228):
|
|
// A = d0.hi x + d1.hi y + d2.hi z + d3 B = d0.lo x + d1.lo y + d2.lo z + d4
|
|
// C = d5.hi x + d5.lo y + d6 D = d7.hi x + d7.lo y + d8 E = d9.hi x + d9.lo y + d10
|
|
// inside when the AND of the five as integers is negative (every sign bit set, 0x204-0x22c); a pixel inside
|
|
// ends with t = 1 (0x230), and the loop ends after volume k when k >= n (0x244, 0x248); then t = 0 (0x24c).
|
|
//
|
|
// The words (0x264-0x2f0): LDS dwords tid.x (t) and tid.x + 16 (the mask) are cleared by the threads with
|
|
// tid.y == 0 (0x84), a barrier (0x94), each thread ORs t << tid.y into dword tid.x and
|
|
// ((low + t) != high) << tid.y into dword tid.x + 16 (0x288, 0x290), a barrier (0x29c), and the threads
|
|
// with tid.y == 0 store dword tid.x + 16 to table +0x80's target and dword tid.x to table +0x60's, both at
|
|
// (x, row) (0x2f0, 0x2f8; R16_UINT targets in the frames read: the low 16 bits are kept).
|
|
//
|
|
// Made from the translation, decompiled, its bindings restored and its names given from what the console code
|
|
// does with each value (its disassembly; offsets are the console
|
|
// program's). The texture's size is the translation's: it rebuilds the console descriptor's width, height
|
|
// and base level from GetDimensions, which comes to ((w - 1) & 0xffff) + 1, ((h - 1) & 0xffff) + 1 and a
|
|
// base level of 0 (the rebuilt word 0x90000FAC | (levels - 1) << 16 has bits 12-15 zero), written so here.
|
|
//
|
|
// What the translation had and this file does not:
|
|
// - the first-lane read of the tile (0x74): the lane's own value. The load (0x60) is addressed from scalars
|
|
// alone (its index 0x4c from c and the group, its descriptor 0x38, c from a scalar load 0x30), and
|
|
// nothing between is written under a lane's condition.
|
|
// - the guard of 4,096 passes on the volume loop. The loop's own stop: k counts from 1 (0x194, 0x198), the
|
|
// loop goes back to 0x198 while k < n (0x244 s_cmp_ge_u32, 0x248), and it is entered only with n != 0
|
|
// (0xc8), so it ends after at most n passes, at the last volume, unless the pixel is inside one before.
|
|
// - each slot read from its list at every use: read once, where the console loads the descriptor (0x24,
|
|
// 0x38, 0x50, 0x58, 0xd4, 0x2c0, 0x2c8), table +0x0's shift with it.
|
|
// - the barriers' order of all memory: each orders group-shared memory alone (0x90 s_waitcnt lgkmcnt(0),
|
|
// 0x94; 0x298, 0x29c). No thread reads a buffer or texture another writes; the program's only writes to
|
|
// memory are the two stores after the last barrier.
|
|
// - bounds: the read of a table's list, none; the shift's index into t253, min
|
|
// 2097151 alone (the second, 2147483584, is redundant after it); the count's offset, none: the console's
|
|
// is the literal 0 (0xb4) and the shift at most 12.
|
|
// - the legacy multiplies' test for a zero operand (0x138, 0x13c): a plain multiply. Neither operand can be
|
|
// infinite or NaN (y and x are integers below 2^32; 1 / h and 1 / w, h and w from 1 to 65536, are finite
|
|
// and above 0), and a zero product is +0 either way.
|
|
// Bounds kept: a slot from a list, min 999999; the volume dwords' offsets, 44 k + 4 i for k up to n (a dword
|
|
// of the buffer, unbounded in the listing), the wrap test and min 2147483584; the tile list's element,
|
|
// 2 c + g (0x44, 0x48; c a constant the listing does not bound), min 134217727; the constant views'
|
|
// offsets, min 2147483584 alone, as the translation has it.
|
|
//
|
|
// The interface is the translation's: user data b0 space0, 8 dwords (5: the table, root parameter 1; 6: the
|
|
// first group, Other(260)); t252, t253 space1; views from the heap at the slots the lists give, typed as
|
|
// the translation's handles (+0x0 a raw SRV, +0x20 Texture2D<float4>, +0x40 RWBuffer<float4>, +0x60 and
|
|
// +0x80 RWTexture2D<float4>, +0xa0 and +0xc0 constant views read as raw bytes, README "Descriptors").
|
|
//
|
|
// Not established:
|
|
// - n, and so the length of the loop: nothing in the program bounds it. A volume past the end of the
|
|
// buffer reads 0 here (past the raw view) and holds no pixel (no plane of 0 is negative); what the
|
|
// console's s_buffer_load gives there (0x1a0, 0x1ac) is its descriptor's, which the listing does not show.
|
|
// - the bounds kept above, for want of a bound in the listing.
|
|
// - the reciprocals: the console's v_rcp_iflag_f32 (0x120, 0x128) is an approximation; here, as in the
|
|
// translation, a division.
|
|
// - the console's multiply-adds are fused (v_fma_f32, v_fmac_f32, v_fma_mix_f32); `mad` is the
|
|
// translation's, and whether the host fuses it is the driver's.
|
|
// - the targets are written as the translation types them, RWTexture2D<float4>, the word as the bits of x:
|
|
// that the bits reach the R16_UINT texel unchanged rests on the format of the host's view.
|
|
|
|
#include "view_heap_layout.hlsli"
|
|
|
|
cbuffer UserData : register(b0, space0) { uint4 ud[2]; };
|
|
|
|
ByteAddressBuffer table_lists : register(t252, space1);
|
|
ByteAddressBuffer view_shifts : register(t253, space1);
|
|
|
|
static const uint MAX_BYTE = 2147483584u;
|
|
static const uint MAX_ELEMENT = 134217727u;
|
|
|
|
groupshared uint words[32];
|
|
|
|
// The heap slot in the list entry for byte b of the table (ud[1].y, user-data dword 5).
|
|
uint table_entry(uint b) { return table_lists.Load(((ud[1].y + b) >> 5u) << 2u); }
|
|
|
|
// A constant view whose list entry is e: the raw view its constants are read through, and their first byte
|
|
// there (README "Descriptors").
|
|
ByteAddressBuffer cbv_view(uint e) { return ResourceDescriptorHeap[min((e >> 31u) != 0u ? HEAP_VIEW_SLOT : e, NULL_SLOT)]; }
|
|
uint cbv_base(uint e) { return (e >> 31u) != 0u ? e << 4u : 0u; }
|
|
|
|
// The dword at byte `off` of the title's view, through its raw view beginning `shift` bytes before it.
|
|
uint shifted_load(ByteAddressBuffer v, uint shift, uint off)
|
|
{
|
|
uint at = off + shift;
|
|
return v.Load(min(at < shift ? off : at, MAX_BYTE));
|
|
}
|
|
|
|
[numthreads(16, 16, 1)]
|
|
void main(uint3 group : SV_GroupID, uint3 tid : SV_GroupThreadID)
|
|
{
|
|
uint g = group.x + ud[1].z; // s6
|
|
|
|
// 0x24-0x60: the tile.
|
|
uint ce = table_entry(0xa0u); // 0x24
|
|
uint c = cbv_view(ce).Load4(min(cbv_base(ce) + 16u, MAX_BYTE)).z; // 0x30
|
|
RWBuffer<float4> tiles = ResourceDescriptorHeap[min(table_entry(0x40u), NULL_SLOT)]; // 0x38
|
|
uint ve = table_entry(0x0u); // 0x50
|
|
Texture2D<float4> facing_tex = ResourceDescriptorHeap[min(table_entry(0x20u), NULL_SLOT)]; // 0x58
|
|
uint tile = asuint(tiles[min((c << 1u) + g, MAX_ELEMENT)].x); // 0x60
|
|
bool first_row = tid.y == 0u; // 0x68
|
|
|
|
if (first_row) // 0x78-0x8c
|
|
{
|
|
words[tid.x] = 0u;
|
|
words[tid.x + 16u] = 0u;
|
|
}
|
|
GroupMemoryBarrierWithGroupSync(); // 0x90, 0x94
|
|
|
|
uint row = tile >> 16u; // 0x9c
|
|
uint y = (row << 4u) + tid.y; // 0xa8
|
|
uint x = ((tile << 4u) & 0xffff0u) + tid.x; // 0x98, 0xa0, 0xb0
|
|
|
|
ByteAddressBuffer vols = ResourceDescriptorHeap[min(ve, NULL_SLOT)];
|
|
uint vs = view_shifts.Load(min(ve, 2097151u) << 2u);
|
|
uint n = vols.Load(vs); // 0xb4
|
|
uint facing = asuint(facing_tex.Load(int3(uint2(x, y), 0)).x); // 0xbc
|
|
|
|
uint t;
|
|
if (n == 0u) // 0xc8
|
|
{
|
|
t = 0u; // 0x258
|
|
}
|
|
else
|
|
{
|
|
uint w, h, levels;
|
|
facing_tex.GetDimensions(0u, w, h, levels);
|
|
w = ((w - 1u) & 0xffffu) + 1u; // 0xdc, 0xe8, 0xf8, 0x100, 0x10c, 0x114
|
|
h = ((h - 1u) & 0xffffu) + 1u; // 0xe0, 0xf4, 0xfc
|
|
float fy = float(y); // 0x110
|
|
float fx = float(x); // 0x118
|
|
precise float rh = 1.0f / float(h); // 0x120
|
|
precise float rw = 1.0f / float(w); // 0x128
|
|
uint cm = table_entry(0xc0u); // 0xd4
|
|
ByteAddressBuffer mv = cbv_view(cm);
|
|
uint mb = cbv_base(cm);
|
|
float4 r0 = asfloat(mv.Load4(min(mb, MAX_BYTE))); // 0x130
|
|
float4 r1 = asfloat(mv.Load4(min(mb + 16u, MAX_BYTE)));
|
|
float4 r2 = asfloat(mv.Load4(min(mb + 32u, MAX_BYTE)));
|
|
float4 r3 = asfloat(mv.Load4(min(mb + 48u, MAX_BYTE)));
|
|
precise float sy = fy * rh; // 0x138
|
|
precise float sx = fx * rw; // 0x13c
|
|
float v = mad(sy, -2.0f, 1.0f); // 0x140
|
|
float u = mad(sx, 2.0f, -1.0f); // 0x148
|
|
float px = mad(r3.w, mad(r0.x, u, mad(r0.y, v, r0.z)), r3.x); // 0x154-0x188
|
|
float py = mad(r3.w, mad(r1.x, u, mad(r1.y, v, r1.z)), r3.y);
|
|
float pz = mad(r3.w, mad(r2.x, u, mad(r2.y, v, r2.z)), r3.z);
|
|
|
|
t = 0u; // 0x24c
|
|
for (uint k = 1u;; ++k) // 0x194, 0x198
|
|
{
|
|
uint o = k * 44u; // 0x19c
|
|
uint d0 = shifted_load(vols, vs, o); // 0x1a0
|
|
uint d1 = shifted_load(vols, vs, o + 4u);
|
|
uint d2 = shifted_load(vols, vs, o + 8u);
|
|
float d3 = asfloat(shifted_load(vols, vs, o + 12u));
|
|
float d4 = asfloat(shifted_load(vols, vs, o + 16u));
|
|
uint d5 = shifted_load(vols, vs, o + 20u);
|
|
float d6 = asfloat(shifted_load(vols, vs, o + 24u));
|
|
uint d7 = shifted_load(vols, vs, o + 28u);
|
|
float d8 = asfloat(shifted_load(vols, vs, o + 32u)); // 0x1ac
|
|
uint d9 = shifted_load(vols, vs, o + 36u);
|
|
float d10 = asfloat(shifted_load(vols, vs, o + 40u));
|
|
float pa = mad(f16tof32(d0 >> 16u), px, mad(f16tof32(d1 >> 16u), py, mad(f16tof32(d2 >> 16u), pz, d3))); // 0x1b8, 0x1d8, 0x1f0
|
|
float pb = mad(f16tof32(d0), px, mad(f16tof32(d1), py, mad(f16tof32(d2), pz, d4))); // 0x1c0, 0x1e0, 0x1f8
|
|
float pc = mad(f16tof32(d5 >> 16u), px, mad(f16tof32(d5), py, d6)); // 0x1c8, 0x1e8
|
|
float pd = mad(f16tof32(d7 >> 16u), px, mad(f16tof32(d7), py, d8)); // 0x1d0, 0x208
|
|
float pe = mad(f16tof32(d9 >> 16u), px, mad(f16tof32(d9), py, d10)); // 0x210, 0x21c
|
|
if (int(asuint(pb) & asuint(pa) & asuint(pc) & asuint(pd) & asuint(pe)) < 0) // 0x204-0x22c
|
|
{
|
|
t = 1u; // 0x230
|
|
break;
|
|
}
|
|
if (k >= n) // 0x244, 0x248
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 0x264-0x290
|
|
uint high = facing >> 16u;
|
|
uint low = (facing << 16u) >> 16u;
|
|
uint bit = ((t + low) != high) ? 1u : 0u;
|
|
uint s = tid.y & 31u;
|
|
InterlockedOr(words[tid.x + 16u], bit << s); // 0x288
|
|
InterlockedOr(words[tid.x], t << s); // 0x290
|
|
GroupMemoryBarrierWithGroupSync(); // 0x298, 0x29c
|
|
|
|
if (first_row) // 0x2a0
|
|
{
|
|
uint mask_word = words[tid.x + 16u]; // 0x2b0
|
|
uint term_word = words[tid.x]; // 0x2b8
|
|
RWTexture2D<float4> mask_target = ResourceDescriptorHeap[min(table_entry(0x80u), NULL_SLOT)]; // 0x2c0
|
|
RWTexture2D<float4> term_target = ResourceDescriptorHeap[min(table_entry(0x60u), NULL_SLOT)]; // 0x2c8
|
|
mask_target[uint2(x, row)] = asfloat(mask_word).xxxx; // 0x2f0
|
|
term_target[uint2(x, row)] = asfloat(term_word).xxxx; // 0x2f8
|
|
}
|
|
}
|