mirror of
https://github.com/gamingwithhomie/larp.git
synced 2026-12-06 07:38:36 +00:00
218 lines
13 KiB
HLSL
218 lines
13 KiB
HLSL
// d72397c79be4bd1a, compute: CS_CombineFacingCountWithNearPlaneContribution_neither_BoundsCheck, the underwater
|
|
// mask's combine for the 16x16 tiles of the list it is given, with a test of each pixel against the facing
|
|
// count texture's size (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 console code is
|
|
// 07fb4c723c4241ed's but for that test.
|
|
//
|
|
// The tile: the word e at element 2 c + g of table +0x40's typed buffer (0x44-0x58; 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 (0x6c); its x at (e << 4) & 0xffff0, its row at e >> 16 (0x90, 0x98, 0xa0). The pixel: x = that x +
|
|
// tid.x (0xa4), y = row * 16 + tid.y (0xac).
|
|
//
|
|
// The test (0xa8-0xf4): a pixel with x >= w or y >= h (w, h the size of table +0x20's texture) loads
|
|
// nothing and adds no bit; it skips to the barrier (0xf4 to 0x2b8). The others, per pixel (0xf8-0x2b0):
|
|
// the facing count, the dword of table +0x20's texture at (x, y) (0xf8: 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 (0x10c); n == 0 gives t = 0 (0x118, 0x11c). Else the
|
|
// point (0x124-0x1a4): u = 2 x / w - 1, v = 1 - 2 y / h, p = r3.w * (r0.x u + r0.y v + r0.z, r1..., r2...)
|
|
// + r3.xyz, rows r0-r3 of table +0xc0's constants (0x154); then volume k = 1, 2, ... (0x13c, 0x1b8) at byte
|
|
// 44 k, eleven dwords d0-d10 (0x1bc-0x1cc), five planes with f16 coefficients and f32 offsets
|
|
// (0x1d8-0x248):
|
|
// 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, 0x224-0x24c); a pixel inside
|
|
// ends with t = 1 (0x250), and the loop ends after volume k when k >= n (0x264, 0x268); then t = 0 (0x26c).
|
|
//
|
|
// The words (0x284-0x320): LDS dwords tid.x (t) and tid.x + 16 (the mask) are cleared by the threads with
|
|
// tid.y == 0 (0x7c), a barrier (0x8c), each pixel inside the texture ORs t << tid.y into dword tid.x and
|
|
// ((low + t) != high) << tid.y into dword tid.x + 16 (0x2a8, 0x2b0), a barrier (0x2c0), 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) (0x310, 0x320; 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 (0x6c): the lane's own value. The load (0x58) 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 (0x13c, 0x1b8), the
|
|
// loop goes back to 0x1b8 while k < n (0x264 s_cmp_ge_u32, 0x268), and it is entered only with n != 0
|
|
// (0x118), 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, 0x100, 0x124, 0x2dc, 0x2e4), table +0x0's shift with it.
|
|
// - the size test made again in the mask bit, inside the branch that made it (0xf4): there it holds.
|
|
// - the barriers' order of all memory: each orders group-shared memory alone (0x88 s_waitcnt lgkmcnt(0),
|
|
// 0x8c; 0x2bc, 0x2c0). 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 (0x10c) and the shift at most 12.
|
|
// - the legacy multiplies' test for a zero operand (0x148, 0x14c): 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 (0x1c0, 0x1cc) 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 (0x140, 0x144) 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-0x58: 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
|
|
Texture2D<float4> facing_tex = ResourceDescriptorHeap[min(table_entry(0x20u), NULL_SLOT)]; // 0x50
|
|
uint tile = asuint(tiles[min((c << 1u) + g, MAX_ELEMENT)].x); // 0x58
|
|
bool first_row = tid.y == 0u; // 0x60
|
|
|
|
if (first_row) // 0x70-0x84
|
|
{
|
|
words[tid.x] = 0u;
|
|
words[tid.x + 16u] = 0u;
|
|
}
|
|
GroupMemoryBarrierWithGroupSync(); // 0x88, 0x8c
|
|
|
|
uint row = tile >> 16u; // 0xa0
|
|
uint x = ((tile << 4u) & 0xffff0u) + tid.x; // 0x90, 0x98, 0xa4
|
|
uint y = (row << 4u) + tid.y; // 0xac
|
|
uint w, h, levels;
|
|
facing_tex.GetDimensions(0u, w, h, levels);
|
|
w = ((w - 1u) & 0xffffu) + 1u; // 0x94, 0xa8, 0xb4, 0xc0, 0xcc, 0xd8
|
|
h = ((h - 1u) & 0xffffu) + 1u; // 0xb8, 0xc8, 0xdc
|
|
|
|
if (w > x && h > y) // 0xe0-0xf4
|
|
{
|
|
uint facing = asuint(facing_tex.Load(int3(uint2(x, y), 0)).x); // 0xf8
|
|
uint ve = table_entry(0x0u); // 0x100
|
|
ByteAddressBuffer vols = ResourceDescriptorHeap[min(ve, NULL_SLOT)];
|
|
uint vs = view_shifts.Load(min(ve, 2097151u) << 2u);
|
|
uint n = vols.Load(vs); // 0x10c
|
|
|
|
uint t;
|
|
if (n == 0u) // 0x118
|
|
{
|
|
t = 0u; // 0x278
|
|
}
|
|
else
|
|
{
|
|
float fy = float(y); // 0x134
|
|
float fx = float(x); // 0x138
|
|
precise float rh = 1.0f / float(h); // 0x12c, 0x140
|
|
precise float rw = 1.0f / float(w); // 0x130, 0x144
|
|
uint cm = table_entry(0xc0u); // 0x124
|
|
ByteAddressBuffer mv = cbv_view(cm);
|
|
uint mb = cbv_base(cm);
|
|
float4 r0 = asfloat(mv.Load4(min(mb, MAX_BYTE))); // 0x154
|
|
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; // 0x148
|
|
precise float sx = fx * rw; // 0x14c
|
|
float v = mad(sy, -2.0f, 1.0f); // 0x15c
|
|
float u = mad(sx, 2.0f, -1.0f); // 0x164
|
|
float px = mad(r3.w, mad(r0.x, u, mad(r0.y, v, r0.z)), r3.x); // 0x170-0x1a4
|
|
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; // 0x26c
|
|
for (uint k = 1u;; ++k) // 0x13c, 0x1b8
|
|
{
|
|
uint o = k * 44u; // 0x1bc
|
|
uint d0 = shifted_load(vols, vs, o); // 0x1c0
|
|
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)); // 0x1cc
|
|
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))); // 0x1d8, 0x1f8, 0x210
|
|
float pb = mad(f16tof32(d0), px, mad(f16tof32(d1), py, mad(f16tof32(d2), pz, d4))); // 0x1e0, 0x200, 0x218
|
|
float pc = mad(f16tof32(d5 >> 16u), px, mad(f16tof32(d5), py, d6)); // 0x1e8, 0x208
|
|
float pd = mad(f16tof32(d7 >> 16u), px, mad(f16tof32(d7), py, d8)); // 0x1f0, 0x228
|
|
float pe = mad(f16tof32(d9 >> 16u), px, mad(f16tof32(d9), py, d10)); // 0x230, 0x23c
|
|
if (int(asuint(pb) & asuint(pa) & asuint(pc) & asuint(pd) & asuint(pe)) < 0) // 0x224-0x24c
|
|
{
|
|
t = 1u; // 0x250
|
|
break;
|
|
}
|
|
if (k >= n) // 0x264, 0x268
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 0x284-0x2b0
|
|
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); // 0x2a8
|
|
InterlockedOr(words[tid.x], t << s); // 0x2b0
|
|
}
|
|
GroupMemoryBarrierWithGroupSync(); // 0x2bc, 0x2c0
|
|
|
|
if (first_row) // 0x2c4
|
|
{
|
|
RWTexture2D<float4> mask_target = ResourceDescriptorHeap[min(table_entry(0x80u), NULL_SLOT)]; // 0x2dc
|
|
RWTexture2D<float4> term_target = ResourceDescriptorHeap[min(table_entry(0x60u), NULL_SLOT)]; // 0x2e4
|
|
uint term_word = words[tid.x]; // 0x2f0
|
|
uint mask_word = words[tid.x + 16u]; // 0x2f0
|
|
mask_target[uint2(x, row)] = asfloat(mask_word).xxxx; // 0x310
|
|
term_target[uint2(x, row)] = asfloat(term_word).xxxx; // 0x320
|
|
}
|
|
}
|