Files
larp/shaders/replace/136986812880cead.compute.hlsl

583 lines
35 KiB
HLSL

// 136986812880cead, compute: CS_GatherDensityAreaForReadBack. A group is 4 x 4 x 4 cells of a grid; cell (x,
// y, z) = 4 * group + (lane & 3, (lane >> 2) & 3, lane >> 4) lies at org + stp * (x, y, z) (table 0 +0xca0 at
// 0x10 and 0x20; 0x40-0xa8). Per cell (0xb4-0x350): the ray from the eye (-C, C table 1 +0x60 at 0xc0) to the
// cell is cut by a sphere about (A, -k918) (A table 0 +0x100 at 600; k table 0 +0xca0) and by a second one
// (table 0 +0xc60 row 2, its distances times 1000, used when the cell is farther than table 1 +0x0's byte
// 56, 0x2b8) to a far distance, capped by k808. The march (0x358-0x13e4, cells with k418 < 1 and far >= 0.1)
// steps from 0.1 by (far - 0.1) / max(8, round((far - 0.1) / 8)), at least 1e-6, each step's sample at its
// middle: there four noise layers (table 0 +0x340 a coverage, +0x300 three noise pairs; one function here,
// `layer`, from bases 0xba0, 0xc10, 0xc80, 0xcf0), a 2-D map below table 0 +0x120's height (+0x320), two
// height terms and a 2-D map (+0x400), and the lights of the sample's cell of a 3-D grid (+0x740, two words
// of 32 bits a cell; light i: table 0 +0xc80 rows 0x5a3 + 9 i to 0x5ab + 9 i, its colour element 2 i + 1 of
// +0xa0's buffer) make an extinction per channel, and the transmittance (three channels, from 1) is
// multiplied by exp2(-log2(e) * step * extinction). A lane stops past the far distance, when all three
// channels are at or below k418, or after its 10000th step (0x1390-0x13d8). Then the same terms at the cell
// itself (0x13e8-0x1bbc).
//
// What it writes, the program's one store (0x1c8c, buffer_store_dwordx2 idxen): two dwords at byte ((z * 16 +
// y) * 16 + x) * 8 of table 1 +0x20's buffer, the title's CPU reads back. Each byte is a value
// saturated, times 255, rounded (v_cvt_pk_u8_f32). Word 0: byte 0 the largest over the three channels of
// saturate((T - k418) / (1 - k418)) of the march's transmittance (1 for a cell that does not march); byte 1
// the height term from +0x320 and k420-k438; byte 2 +0x400's term; byte 3 the largest of the four layers'
// densities at the cell. Word 1: those four densities. Here: the same two dwords, one store. What each value
// is (eye, sphere, fog, density) is inferred from its use; the arithmetic and the addresses are the
// console's.
//
// Written by hand from the console code (its disassembly; offsets are the
// console program's). The translation (made in the
// "local waves" form) and its decompile were 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); t252, t253
// space1; samplers s0-s3 (table 0 +0x340's, +0x300's, +0x320's, +0x400's); views from the heap typed as the
// translation's handles at the slots it reads, with its clamps (min 999999; a constant view per README
// "Descriptors"; the light grid a Texture3D<float4> read as .x's bits). The light loops' console code:
// 0xe14-0x10b0 and 0x10c8-0x1368.
//
// Each table slot is read from its list (t252) once, and each constant of table 0 +0xca0 once: the table's
// list does not change within a dispatch (larp-gpu draw.rs, table_lists), and the console reloads the
// constants (0x6e8, 0x7c0, 0x850, 0x908, ...) for want of registers. The constants it computes in vector
// registers and reads back by v_readfirstlane (0x104-0x5d4: differences and reciprocals of constants) and its
// spills through v37 (0x3f0-0x450, read at 0x928-0xa40) are plain values here.
//
// Lanes: the console runs the group as one wave of 64; here lane t is SV_GroupIndex t (numthreads 64, the
// translation's), so the translation's count of host waves is not here. What the lanes share is one thing a
// march pass: the ORs over the wave of the lanes' two mask words at the sample's cell (0xe20-0xe68,
// 0x10d4-0x111c: EXEC is the lanes inside the grid, the others give 0 at 0xe28 and 0x10dc; each lane then
// takes every light of the OR, 0xe90-0x10b0, 0x1140-0x1368), and whether the wave marches on (0x354 on the
// first pass, 0x13e4 of the pass before on the others). They are one meeting a pass (group words meet[pass %
// 3]): each host wave ORs in its part, one barrier, every thread reads; thread 0 zeroes the set of the next
// pass before the barrier, which no thread reads again (its last readers passed the barrier of the pass
// before), so no barrier follows the reads. The second word's load (0x10c8) comes before the first word's
// lights here: it reads the same cell (z + 1) of a texture the program does not write. The console's skips of
// the grid part (0xd9c, 0xdc0: no lane inside) are each lane's own test: what they skip holds the ORs, which
// here every thread reaches; with no lane inside, the ORs are 0 and no lane reads them. Two barriers: the
// zeroed first set, and the meeting.
//
// No guard of the translation's: the march stops on the console's own test, a lane's 10000th step (0x13d0
// s_cmpk_gt_i32 s63, 0x270f; s63 counts the wave's passes, and every lane marches from the first, so it is
// each lane's own count), where the translation ended the program unstored at a 4097th pass. Each light loop
// clears one bit of a word a pass and ends on its last bit (0x1064 s_xor, 0x10ac s_cmp_eq; 0x131c, 0x1364):
// 32 passes at most, so it runs per lane, inside the grid, with no guard.
//
// Bounds: the read of a table's list, none (((ud + K) >> 5) << 2 is below 2^29); a
// slot from a list, min 999999; the shift's index into t253, min 2097151 (the layer's root view); the raw
// views' constant offsets (table 0 +0x100 at 600 and 604, table 1 +0x0 at 56, table 0 +0x120 at 0x6dc to
// 0x6f0), neither the wrap test nor a bound (`raw_f`); a constant view's offset, min 2147483584 alone, as the
// translation has it; the colour's element, none (2 i + 1 with i < 64: firstbitlow of a word, plus 32 for the
// second, 0xe9c, 0x1150); the store's offset, from a cell whose z the dispatch alone bounds: the wrap test
// and min 2147483584 (`at`).
//
// Arithmetic as the translation gives it (each the console's): v_mul_legacy_f32 is mul_legacy (0 times
// anything is +0), v_mad_legacy_f32 mad_legacy, v_fma_f32 / v_fmac_f32 mad (v_mad_f32 at 0x162c-0x1634 a
// multiply and an add), v_min_f32 / v_max_f32 / v_min3 / v_max3 fmin_t / fmax_t (the select that orders -0
// below +0), the clamp modifier saturate, v_rndne round, v_exp exp2, v_rcp 1 / x, v_cvt_pk_u8_f32 to_u8;
// every float value is precise.
//
// The host's plain form where the listing shows the two give the same value:
// - a value times itself (the squares in every length and the light's bq * bq, sv * sv, e1 * e1, e2 * e2):
// equal for every operand;
// - fmax_t(8, x) (0x424), fmax_t(1e-6, x) (0x46c), fmax_t(0.01, x) and fmin_t(step, that) (0x5f0-0x5f8),
// fmin_t(t + step / 2, far) (0x604): one operand is not a zero (a literal; at least 0.01; t >= 0.1 and step
// > 0), so the select never takes;
// - the light's factor (0x102c-0x106c and 0x12e4-0x1324): 1/255 times three of 0 and 1, then times a byte: no
// zero of the other sign, infinity or NaN; 4 * c1 (0xf64, 0x1218): it differs only in a zero's sign, which
// the fma it feeds (4 * c1 * z + bq * bq, z never 0 nor NaN, bq * bq >= +0 or NaN) gives as +0 either way;
// - -log2(e) * step (0x1374) and its products with the extinction (0x1384-0x138c): step is finite and at
// least 1e-6 (0x46c, the march runs only with 0.1 <= far, so its span is finite or +inf, and +inf gives the
// step 1e-6), so they differ only in the sign of a zero, which exp2 (0x1398-0x13a0) reads as 1.
//
// Kept everywhere: precise; mad for the console's fused multiply-adds.
//
// Not established:
// - every other legacy multiply and min/max select keeps the translation's form: an operand is a title
// constant, a texture's value or one made from them, which the listing does not show finite, or not 0 where
// the other is infinite or NaN; the sign of a zero not shown unread;
// - v_sqrt, v_rsq, v_rcp, v_exp, v_rndne and v_cvt_pk_u8_f32 are the host's sqrt, rsqrt, 1 / x, exp2, round
// (to nearest even) and to_u8, which may differ from the console's in the last bit (the byte's rounding
// mode is the translation's reading); the console's fused multiply-adds are mad, which the host may round
// as a multiply and an add;
// - the 8 bytes a cell of the store's buffer are the translation's stride for the view; the console's
// idxen store takes the stride from the descriptor, which the listing does not show;
// - the light grid (table 0 +0x740) is a Texture3D<float4> whose .x is read as a word, as the translation
// types it; the console's image_load gives the word itself.
//
// 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]; };
ByteAddressBuffer table_lists : register(t252, space1);
ByteAddressBuffer view_shifts : register(t253, space1);
SamplerState samp0 : register(s0, space0); // table 0 +0x340's (0x69c: 0x180, 0xc00000, 0x14500000, 0)
SamplerState samp1 : register(s1, space0); // table 0 +0x300's (0x70c: 0, 0xc00000, 0x18500000, 0)
SamplerState samp2 : register(s2, space0); // table 0 +0x320's (0xbc8: 0x1b6, 0xc00000, 0x18500000, 0)
SamplerState samp3 : register(s3, space0); // table 0 +0x400's (0xc4c: 0, 0xc00000, 0x14500000, 0)
static const uint MAX_BYTE = 2147483584u;
uint table_entry(uint table, uint b) { return table_lists.Load(((table + b) >> 5u) << 2u); }
uint table_slot(uint table, uint b) { return min(table_entry(table, b), NULL_SLOT); }
// A raw view of the heap: its slot and the bytes its raw view begins before the console's.
struct View { uint slot; uint shift; };
View view_of(uint e)
{
View v;
v.slot = min(e, NULL_SLOT);
v.shift = view_shifts.Load(min(min(e, 2097151u) << 2u, MAX_BYTE));
return v;
}
uint at(View v, uint off) { uint b = off + v.shift; return min(b < v.shift ? off : b, MAX_BYTE); }
// A constant offset: neither the wrap test nor the bound.
float raw_f(View v, uint off) { ByteAddressBuffer r = ResourceDescriptorHeap[v.slot]; return asfloat(r.Load(off + v.shift)); }
// A table's constant view (README "Descriptors"): its slot and the byte its constants begin at.
struct Cbv { uint slot; uint base; };
Cbv cbv_of(uint e)
{
bool placed = (e >> 31u) != 0u;
Cbv c;
c.slot = min(placed ? HEAP_VIEW_SLOT : e, NULL_SLOT);
c.base = placed ? (e << 4u) : 0u;
return c;
}
uint cbv_u(Cbv c, uint off) { ByteAddressBuffer r = ResourceDescriptorHeap[c.slot]; return r.Load(min(c.base + off, MAX_BYTE)); }
float cbv_f(Cbv c, uint off) { return asfloat(cbv_u(c, off)); }
float3 cbv_f3(Cbv c, uint off) { ByteAddressBuffer r = ResourceDescriptorHeap[c.slot]; return asfloat(r.Load3(min(c.base + off, MAX_BYTE))); }
float4 cbv_f4(Cbv c, uint off) { ByteAddressBuffer r = ResourceDescriptorHeap[c.slot]; return asfloat(r.Load4(min(c.base + off, MAX_BYTE))); }
// The translation's float forms (the exact forms).
float mul_legacy(float a, float b) { precise float p = a * b; return (a == 0.0f || b == 0.0f) ? 0.0f : p; }
float mad_legacy(float a, float b, float c) { precise float r = mul_legacy(a, b) + c; return r; }
float fmin_t(float a, float b) { uint o = asuint(a) | asuint(b); return ((o & 0x7fffffffu) == 0u) ? asfloat(o) : min(a, b); }
float fmax_t(float a, float b) { uint o = asuint(a) | asuint(b); return ((o & 0x7fffffffu) == 0u) ? asfloat(asuint(a) & asuint(b)) : max(a, b); }
float sat(float x) { return saturate(x); }
uint to_u8(float x) { return uint(min(max(round(x), 0.0f), 255.0f)); }
// The march's one meeting a pass: set p % 3 holds, for pass p, the OR of the lanes' first and second
// mask words (0 from a lane outside the grid) and whether any lane is still marching. Each host wave ORs
// its own into it; thread 0 zeroes the set of pass p + 1 during pass p, after every reader of its pass
// p - 2 has passed the barrier of pass p - 1.
groupshared uint meet[3][3];
// The constants of table 0 +0xca0 the program reads (the console's s_buffer_loads of s[64:67]).
struct Consts
{
float3 org, stp; // 0x10, 0x20: the cell's position, origin and step
float s40, s41, s42; // 0x3e0, 0x3e4, 0x3e8
float s43; // 0x918
float thr; // 0x418: the transmittance at which the march stops
float s72, far_cap; // 0x804, 0x808
float s47; // 0x3f0
float4 r950; // 0x950
float3 w1s, w2s; // 0x7b0, 0x7c0
float k960, k964, k968; // 0x960, 0x964, 0x968
float3 w3s; // 0x7d0
float2 ofs848; // 0x848
float2 m210, m220; // 0x210, 0x220
float a7c, a9c, b83c; // 0xa7c, 0xa9c, 0x83c
float4 e420, e430; // 0x420, 0x430
float4 c380, c390, c3a0, c3b0; // 0x380 to 0x3bc
};
Consts load_consts(Cbv b)
{
Consts k;
k.org = cbv_f3(b, 0x10u); k.stp = cbv_f3(b, 0x20u);
k.s40 = cbv_f(b, 0x3e0u); k.s41 = cbv_f(b, 0x3e4u); k.s42 = cbv_f(b, 0x3e8u);
k.s43 = cbv_f(b, 0x918u);
k.thr = cbv_f(b, 0x418u);
k.s72 = cbv_f(b, 0x804u); k.far_cap = cbv_f(b, 0x808u);
k.s47 = cbv_f(b, 0x3f0u);
k.r950 = cbv_f4(b, 0x950u);
k.w1s = cbv_f3(b, 0x7b0u); k.w2s = cbv_f3(b, 0x7c0u);
k.k960 = cbv_f(b, 0x960u); k.k964 = cbv_f(b, 0x964u); k.k968 = cbv_f(b, 0x968u);
k.w3s = cbv_f3(b, 0x7d0u);
k.ofs848 = float2(cbv_f(b, 0x848u), cbv_f(b, 0x84cu));
k.m210 = float2(cbv_f(b, 0x210u), cbv_f(b, 0x214u));
k.m220 = float2(cbv_f(b, 0x220u), cbv_f(b, 0x224u));
k.a7c = cbv_f(b, 0xa7cu); k.a9c = cbv_f(b, 0xa9cu); k.b83c = cbv_f(b, 0x83cu);
k.e420 = cbv_f4(b, 0x420u); k.e430 = cbv_f4(b, 0x430u);
k.c380 = cbv_f4(b, 0x380u); k.c390 = cbv_f4(b, 0x390u); k.c3a0 = cbv_f4(b, 0x3a0u); k.c3b0 = cbv_f4(b, 0x3b0u);
return k;
}
// One of the four layers (0x848-0x8f4 and 0x1670-0x1750, base 0xba0 and 0xbe0; 0x900-0x9d0 and 0x1760-0x1840,
// 0xc10 and 0xc50; 0x9e0-0xa98 and 0x1850-0x1930, 0xc80 and 0xcc0; 0xaa8-0xb50 and 0x1940-0x1a20, 0xcf0 and
// 0xd30): `d` its density, `hn` the height in the layer's range, n1-n3 the three noise pairs.
float layer(Cbv b, uint base, uint wyb, float4 r950, float d, float hn, float2 n1, float2 n2, float2 n3)
{
float4 lo = cbv_f4(b, base), hi = cbv_f4(b, base + 16u);
float dscale = cbv_f(b, base + 32u);
float hadd = cbv_f(b, base + 40u), hmul = cbv_f(b, base + 44u);
float3 wx = cbv_f3(b, base + 48u);
float blo = cbv_f(b, base + 60u);
float3 wy = cbv_f3(b, wyb);
float bhi = cbv_f(b, wyb + 12u);
precise float hk = sat(mad(hn, hmul, hadd));
precise float lw_ = mad(hi.w - lo.w, hk, lo.w);
precise float sd = sat(mul_legacy(dscale, d));
precise float ny = mad(wy.y, n2.y, mul_legacy(wx.y, n2.x));
precise float ly = mad(hi.y - lo.y, hk, lo.y);
precise float p = mul_legacy(lw_, sd);
precise float nx = mad(wy.x, n1.y, mul_legacy(wx.x, n1.x));
precise float sum = mul_legacy(ny, ly);
precise float lx = mad(hi.x - lo.x, hk, lo.x);
precise float lz = mad(hi.z - lo.z, hk, lo.z);
sum = mad(nx, lx, sum);
precise float nz = mad(wy.z, n3.y, mul_legacy(wx.z, n3.x));
sum = mad(nz, lz, sum);
precise float bias = mad(bhi - blo, d, blo);
precise float w = fmax_t(0.0f, mul_legacy(p, bias + sum));
precise float k = sat(mad(abs(d - w), r950.y, r950.x));
k = mad(r950.w - r950.z, k, r950.z);
return mul_legacy(k, w);
}
// One light (0xe90-0x10a8, 0x1140-0x1360): light i of table 0 +0xc80's array (nine rows from row 0x5a3 + 9 i)
// and of table 0 +0xa0's colours (element 2 i + 1), at q, into the extinction s.
void light(Cbv e, Buffer<float4> colours, uint i, float3 q, inout float3 s)
{
uint r = 9u * i;
float3 col = colours.Load((i << 1u) | 1u).xyz; // i < 64: at most element 127
float3 lpos = cbv_f3(e, (r + 0x5a3u) << 4u);
uint fl = cbv_u(e, ((r + 0x5a4u) << 4u) | 12u);
uint mb = (r + 0x5a8u) << 4u;
float3 m0 = cbv_f3(e, mb), m1 = cbv_f3(e, mb + 16u), m2 = cbv_f3(e, mb + 32u);
float4 m3 = cbv_f4(e, mb + 48u);
precise float dx = q.x - lpos.x, dy = q.y - lpos.y, dz = q.z - lpos.z;
precise float c2 = mad(m2.z, dz, mad(m1.z, dy, mul_legacy(m0.z, dx)));
precise float c1 = mad(m2.y, dz, mad(m1.y, dy, mul_legacy(m0.y, dx)));
precise float c0 = mad(m2.x, dz, mad(m1.x, dy, mul_legacy(m0.x, dx)));
precise float z = (m3.z < 0.0f || m3.z > 0.0f) ? m3.z : asfloat(0x38d1b717u);
precise float bq = mad(-m3.z, c1, mad(m3.y, c2, 1.0f));
precise float disc = mad(4.0f * c1, z, bq * bq);
precise float sv = mul_legacy(sqrt(disc) - bq, 1.0f / (z + z));
precise float u2 = mul_legacy(c2, 1.0f / mad(sv, m3.z, 1.0f));
precise float wv = mad(m3.x, u2, mad(sv, m3.w, 1.0f));
precise float u0 = mul_legacy(c0, 1.0f / wv);
float fa, fb, fc;
if (((fl >> 2u) & 1u) == 1u) {
fa = abs(u0) < 1.0f ? 1.0f : 0.0f;
fb = abs(sv) < 1.0f ? 1.0f : 0.0f;
fc = abs(u2) < 1.0f ? 1.0f : 0.0f;
} else {
precise float rr = sqrt(mad(u0, u0, mad(u2, u2, sv * sv)));
fa = 1.0f > rr ? 1.0f : 0.0f;
fb = fa; fc = fa;
}
precise float f = asfloat(0x3b808081u) * fa * fb * fc;
f = f * float(cbv_u(e, ((r + 0x5a7u) << 4u) | 12u) & 0xffu);
precise float lx = mul_legacy(f, col.x), ly = mul_legacy(f, col.y), lz = mul_legacy(f, col.z);
if ((fl & 3u) != 0u) {
lx = mul_legacy(lx - s.x, f);
ly = mul_legacy(ly - s.y, f);
lz = mul_legacy(lz - s.z, f);
}
precise float3 ns = float3(s.x + lx, s.y + ly, s.z + lz);
s = ns;
}
[numthreads(64, 1, 1)]
void main(uint3 gid : SV_GroupID, uint lane : SV_GroupIndex)
{
if (lane == 0u) { meet[0][0] = 0u; meet[0][1] = 0u; meet[0][2] = 0u; }
uint table0 = ud[1].x, table1 = ud[1].y;
Cbv b = cbv_of(table_entry(table0, 0xca0u));
Cbv cc = cbv_of(table_entry(table1, 0x60u));
Cbv dcv = cbv_of(table_entry(table0, 0xc60u));
Cbv e = cbv_of(table_entry(table0, 0xc80u));
View va = view_of(table_entry(table0, 0x100u));
View vt1 = view_of(table_entry(table1, 0x0u));
View vf = view_of(table_entry(table0, 0x120u));
Consts k = load_consts(b);
Texture3D<float4> tex340 = ResourceDescriptorHeap[table_slot(table0, 0x340u)];
Texture3D<float4> tex300 = ResourceDescriptorHeap[table_slot(table0, 0x300u)];
Texture2D<float4> tex320 = ResourceDescriptorHeap[table_slot(table0, 0x320u)];
Texture2D<float4> tex400 = ResourceDescriptorHeap[table_slot(table0, 0x400u)];
Texture3D<float4> tex740 = ResourceDescriptorHeap[table_slot(table0, 0x740u)];
Buffer<float4> colours = ResourceDescriptorHeap[table_slot(table0, 0xa0u)];
// 0x40-0x9c: the cell (group 4 x 4 x 4) and its position.
uint gx = (gid.x << 2u) + (lane % 4u);
uint gy = (gid.y << 2u) + ((lane / 4u) % 4u);
uint gz = (gid.z << 2u) + (lane / 16u);
precise float px = mad(k.stp.x, float(gx), k.org.x);
precise float py = mad(k.stp.y, float(gy), k.org.y);
precise float pz = mad(k.stp.z, float(gz), k.org.z);
float ax = raw_f(va, 600u), ay = raw_f(va, 604u); // 0x6c
float3 cpos = cbv_f3(cc, 192u); // 0x90: s44-s46
float cx = cpos.x, cy = cpos.y, cz = cpos.z;
// 0xb4-0x1b8: the ray from the eye (-cpos) to the cell, against the sphere about (ax, ay, -s43).
precise float v22 = -cy - ay;
precise float v18 = -cx - ax;
precise float dx = -cx - px, dy = -cy - py, dz = -cz - pz;
precise float v19 = k.s43 - cz;
precise float len2 = mad(dz, dz, mad(dy, dy, dx * dx));
precise float rad = k.s41 + k.s43;
precise float c0 = mad(-rad, rad, mad(v19, v19, mad(v18, v18, v22 * v22)));
precise float irl = rsqrt(len2);
precise float dist = sqrt(len2);
precise float diry = mul_legacy(irl, -dy), dirx = mul_legacy(irl, -dx), dirz = mul_legacy(irl, -dz);
precise float qa = mad(dirz, dirz, mad(dirx, dirx, diry * diry));
precise float qb = mad(v19, dirz, mad(v18, dirx, mul_legacy(v22, diry))) * 2.0f;
precise float qa4 = mul_legacy(4.0f, qa);
bool a_zero = 0.0f == qa;
precise float disc = mad(qb, qb, -mul_legacy(qa4, c0));
float t0 = -1.0f, t1 = -1.0f;
if (!(a_zero || 0.0f > disc)) {
precise float sq = sqrt(disc);
precise float ri = 1.0f / (qa + qa);
t0 = mul_legacy(-qb - sq, ri);
t1 = mul_legacy(sq - qb, ri);
}
// 0x1c0-0x290: the second sphere (table 0 +0xc60, row 2), its distances times 1000.
float4 dsp = cbv_f4(dcv, 32u);
precise float ox = 0.0f - cx, oy = 0.0f - cy, oz = 0.0f - cz; // s84, s85, s86
precise float e19 = mad(0.001f, ox, -dsp.y);
precise float e21 = dsp.x + mad(0.001f, oz, -dsp.w);
precise float e23 = mad(0.001f, oy, -dsp.z);
precise float c2 = mad(-dsp.x, dsp.x, mad(e21, e21, mad(e19, e19, e23 * e23)));
precise float qb2 = mad(e21, dirz, mad(e19, dirx, mul_legacy(e23, diry))) * 2.0f;
precise float disc2 = mad(qb2, qb2, -mul_legacy(qa4, c2));
float t2a = -1000.0f, t2b = -1000.0f;
if (!(a_zero || 0.0f > disc2)) {
precise float ri = 1.0f / (qa + qa);
precise float sq = sqrt(disc2);
t2a = 1000.0f * mul_legacy(-qb2 - sq, ri);
t2b = 1000.0f * mul_legacy(sq - qb2, ri);
}
// 0x294-0x314: where the march ends.
float far = 0.0f;
bool first_sphere = true;
if (0.0f < t2a && 0.0f < t2b) {
if (raw_f(vt1, 56u) < dist) { // 0x2b8
far = fmin_t(fmin_t(dist, t2a), t2b);
first_sphere = false;
}
}
if (first_sphere) {
precise float tm = fmax_t(t0, t1);
far = (0.0f < t0 || 0.0f < t1) ? fmin_t(dist, tm) : 0.0f;
}
far = fmin_t(k.far_cap, far); // 0x338
float tx = 1.0f, ty = 1.0f, tz = 1.0f; // 0x328: the transmittance (v11, v12, v13)
bool enter = k.thr < 1.0f && 0.1f <= far; // 0x33c-0x350
{
// 0x358-0x5d8: the march's constants.
float r_s40 = 1.0f / k.s40; // s73
float r_a7c = 1.0f / (asfloat(0x33d6bf95u) + k.a7c); // s90
float s91 = asfloat(0xbd4ccccdu) + k.b83c; // s91
float2 egrid = float2(cbv_f(e, 0x5a20u), cbv_f(e, 0x5a24u)); // s82, s83
precise float s83 = 1.0f / mul_legacy(egrid.y, 0.5f);
precise float s92 = egrid.x + -1.0f;
float mf6dc = raw_f(vf, 0x6dcu); // 0xb9c
precise float span = asfloat(0xbdcccccdu) + far;
precise float nst = 1.0f / max(8.0f, round(mul_legacy(0.125f, span)));
precise float step = max(asfloat(0x358637bdu), mul_legacy(span, nst));
precise float t = asfloat(0x3dcccccdu);
bool active = enter;
int count = 0; // s63
GroupMemoryBarrierWithGroupSync(); // the zeroed set 0 before the first pass's ORs
[loop] for (uint pass = 0u;; pass++) {
precise float3 sg = float3(0.0f, 0.0f, 0.0f); // v19, v18, v17: the extinction
precise float qx = 0.0f, qy = 0.0f, qz = 0.0f;
precise float gxn = 0.0f, gyn = 0.0f, gzn = 0.0f;
if (active) {
// 0x5e8-0x664: the sample point and its height.
step = min(step, max(asfloat(0x3c23d70au), far - t));
precise float ts = min(mad(0.5f, step, t), far);
qy = mad(ts, diry, oy);
qx = mad(ts, dirx, ox);
qz = mad(ts, dirz, oz);
precise float hy = qy - ay, hx = qx - ax, hz = k.s43 + qz;
precise float h = sqrt(mad(hz, hz, mad(hx, hx, hy * hy))) - k.s43; // v24
precise float hn = sat(mul_legacy(h - k.s40, k.s42)); // v23
if (0.0f < hn && hn < 1.0f) {
// 0x670-0xb84: the four layers' density.
float4 cl = tex340.SampleLevel(samp0, float3(mad(k.s47, qx, 0.5f), mad(k.s47, qy, 0.5f), hn), 0.0f);
precise float acc = 0.0f;
if (0.0f < cl.x || 0.0f < cl.y || 0.0f < cl.z || 0.0f < cl.w) {
float3 w1 = float3(mad(k.k960, qx, k.w1s.x), mad(k.k960, qy, k.w1s.y), mad(k.k960, qz, k.w1s.z));
float3 w2 = float3(mad(k.k964, qx, k.w2s.x), mad(k.k964, qy, k.w2s.y), mad(k.k964, qz, k.w2s.z));
float4 s1 = tex300.SampleLevel(samp1, w1, 0.0f);
float4 s2 = tex300.SampleLevel(samp1, w2, 0.0f);
precise float ry = cy + qy, rx = cx + qx, rz = cz + qz;
precise float hk32 = sat(mul_legacy(32.0f, hn));
precise float4 dn = float4(mul_legacy(hk32, cl.x), mul_legacy(hk32, cl.y), mul_legacy(hk32, cl.z), mul_legacy(hk32, cl.w));
precise float cd = sqrt(mad(rz, rz, mad(rx, rx, ry * ry)));
float2 s3 = float2(0.5f, 0.5f);
if (cd < abs(k.s72)) {
float3 w3 = float3(mad(k.k968, qx, k.w3s.x), mad(k.k968, qy, k.w3s.y), mad(k.k968, qz, k.w3s.z));
s3 = tex300.SampleLevel(samp1, w3, 0.0f).xy;
}
precise float2 n3 = float2(mad(s3.x, 2.0f, -1.0f), mad(s3.y, 2.0f, -1.0f));
precise float2 n1 = float2(mad(s1.x, 2.0f, -1.0f), mad(s1.y, 2.0f, -1.0f));
precise float2 n2 = float2(mad(s2.x, 2.0f, -1.0f), mad(s2.y, 2.0f, -1.0f));
if (0.0f < dn.x) acc = fmax_t(acc, layer(b, 0xba0u, 0xbe0u, k.r950, dn.x, hn, n1, n2, n3));
if (0.0f < dn.y) acc = fmax_t(acc, layer(b, 0xc10u, 0xc50u, k.r950, dn.y, hn, n1, n2, n3));
if (0.0f < dn.z) acc = fmax_t(acc, layer(b, 0xc80u, 0xcc0u, k.r950, dn.z, hn, n1, n2, n3));
if (0.0f < dn.w) acc = fmax_t(acc, layer(b, 0xcf0u, 0xd30u, k.r950, dn.w, hn, n1, n2, n3));
}
precise float a = sat(fmax_t(acc, acc));
sg = float3(mul_legacy(k.c3b0.x, a), mul_legacy(k.c3b0.y, a), mul_legacy(k.c3b0.z, a));
}
// 0xb8c-0xc34: below table 0 +0x120's height, the 2-D map of table 0 +0x320.
if (mf6dc > qz) {
float2 tc = float2(mad(k.m210.x, qx, k.m220.x), mad(qy, -k.m210.y, 1.0f - k.m220.y));
float4 hs = tex320.SampleLevel(samp2, tc, 0.0f);
precise float hf = mul_legacy(sat(mul_legacy(hs.x - qz, r_a7c)), hs.z);
if (0.0f < hf) sg = float3(mad(k.c390.x, hf, sg.x), mad(k.c390.y, hf, sg.y), mad(k.c390.z, hf, sg.z));
}
// 0xc3c-0xd94: the height terms and table 0 +0x400's map.
precise float u = sat(mad_legacy(-h, r_s40, 1.0f));
float2 tc4 = float2(mad(mad(k.ofs848.x, u, qx), k.s47, 0.5f), mad(mad(k.ofs848.y, u, qy), k.s47, 0.5f));
float g = tex400.SampleLevel(samp3, tc4, 0.0f).x;
precise float e1 = sat(mad_legacy(-(h - k.e420.x), k.e420.y, 1.0f));
precise float e2 = sat(mad_legacy(-(h - k.e430.x), k.e430.y, 1.0f));
precise float ev = mad(k.e430.z, mul_legacy(e2 * e2, e2), mul_legacy(k.e420.z, mul_legacy(e1 * e1, e1)));
sg = float3(mad(k.c380.x, ev, sg.x), mad(k.c380.y, ev, sg.y), mad(k.c380.z, ev, sg.z));
precise float gw = 0.0f;
if (g < 0.0f || g > 0.0f) {
precise float kk = sat(mad_legacy(20.0f, -(hn - s91), 1.0f));
gw = mul_legacy(k.a9c, mul_legacy(kk, g));
}
precise float rx2 = cx + qx, ry2 = cy + qy, rz2 = cz + qz;
gxn = sat(mad(mul_legacy(s83, rx2), 0.5f, 0.5f));
gyn = sat(mad(mul_legacy(s83, ry2), 0.5f, 0.5f));
gzn = sat(mad(mul_legacy(s83, rz2), 0.5f, 0.5f));
sg = float3(mad(k.c3a0.x, gw, sg.x), mad(k.c3a0.y, gw, sg.y), mad(k.c3a0.z, gw, sg.z));
}
// 0xd98-0x1368: the lights of the cell of table 0 +0x740 (a 3-D grid of two mask words).
bool in1 = active && 0.0f < gxn && 0.0f < gyn && 0.0f < gzn && 1.0f > gxn && 1.0f > gyn && 1.0f > gzn;
uint m0 = 0u, m1 = 0u;
if (in1) {
precise float flx = fmin_t(s92, float(uint(mul_legacy(egrid.x, gxn))));
precise float fly = fmin_t(s92, float(uint(mul_legacy(egrid.x, gyn))));
precise float flz = fmin_t(s92, float(uint(mul_legacy(egrid.x, gzn)))) * 2.0f;
uint lx = uint(flx), ly = uint(fly), lz = uint(flz);
m0 = asuint(tex740.Load(int4(uint3(lx, ly, lz), 0)).x); // 0xe14
m1 = asuint(tex740.Load(int4(uint3(lx, ly, lz + 1u), 0)).x); // 0x10c8
}
// The meeting: the ORs of 0xe20-0xe68 and 0x10d4-0x111c, and the march's own test over the wave
// (0x354 on the first pass, 0x13e4 of the pass before on the others).
uint set = pass % 3u;
bool any_active = WaveActiveAnyTrue(active);
uint w0 = WaveActiveBitOr(m0), w1 = WaveActiveBitOr(m1);
if (WaveIsFirstLane()) {
if (w0 != 0u) InterlockedOr(meet[set][0], w0);
if (w1 != 0u) InterlockedOr(meet[set][1], w1);
if (any_active) InterlockedOr(meet[set][2], 1u);
}
if (lane == 0u) { uint z = (pass + 1u) % 3u; meet[z][0] = 0u; meet[z][1] = 0u; meet[z][2] = 0u; }
GroupMemoryBarrierWithGroupSync();
uint o0 = meet[set][0], o1 = meet[set][1];
if (meet[set][2] == 0u) break;
// The bits of each word, lowest first, each the light it names; a pass clears one bit and the
// last bit ends the loop (0x1064, 0x10ac; 0x131c, 0x1364).
if (in1) {
for (uint m = o0; m != 0u; m &= m - 1u) light(e, colours, firstbitlow(m), float3(qx, qy, qz), sg);
for (uint m = o1; m != 0u; m &= m - 1u) light(e, colours, firstbitlow(m) + 32u, float3(qx, qy, qz), sg);
}
// 0x1370-0x13e4: the step's transmittance, the next step, the stop.
if (active) {
precise float kx = asfloat(0xbfb8aa3bu) * step;
t = step + t;
count = count + 1;
ty = mul_legacy(exp2(kx * sg.y), ty);
tx = mul_legacy(exp2(kx * sg.x), tx);
tz = mul_legacy(exp2(kx * sg.z), tz);
bool stop = t > far || (k.thr >= ty && k.thr >= tx && k.thr >= tz) || count > 9999;
active = !stop;
}
}
}
// 0x13e8-0x14f0: the cell's own height and table 0 +0x340 there.
precise float hy0 = py - ay, hx0 = px - ax, hz0 = k.s43 + pz;
precise float h0 = sqrt(mad(hz0, hz0, mad(hx0, hx0, hy0 * hy0))) - k.s43; // v14
precise float hn0 = sat(mul_legacy(h0 - k.s40, k.s42)); // v8
float4 cl0 = tex340.SampleLevel(samp0, float3(mad(k.s47, px, 0.5f), mad(k.s47, py, 0.5f), hn0), 0.0f);
precise float rt = 1.0f / (1.0f - k.thr);
rt = ((asuint(rt) & 0x7fffffffu) == 0x7f800000u) ? asfloat(asuint(rt) & 0x7fffffffu) : rt; // 0x149c: an infinity made +inf
precise float ox_ = sat(mul_legacy(rt, tx - k.thr));
precise float oy_ = sat(mul_legacy(rt, ty - k.thr));
precise float oz_ = sat(mul_legacy(rt, tz - k.thr));
// 0x14f8-0x1a20: the four layers at the cell.
precise float r1 = 0.0f, r2 = 0.0f, r3 = 0.0f, r4 = 0.0f;
if (0.0f < cl0.x || 0.0f < cl0.y || 0.0f < cl0.z || 0.0f < cl0.w) {
float3 w1 = float3(mad(k.k960, px, k.w1s.x), mad(k.k960, py, k.w1s.y), mad(k.k960, pz, k.w1s.z));
float3 w2 = float3(mad(k.k964, px, k.w2s.x), mad(k.k964, py, k.w2s.y), mad(k.k964, pz, k.w2s.z));
float4 s1 = tex300.SampleLevel(samp1, w1, 0.0f);
float4 s2 = tex300.SampleLevel(samp1, w2, 0.0f);
precise float ry = cy + py, rx = cx + px, rz = cz + pz;
precise float hk32 = sat(mul_legacy(32.0f, hn0));
precise float4 dn = float4(mul_legacy(hk32, cl0.x), mul_legacy(hk32, cl0.y), mul_legacy(hk32, cl0.z), mul_legacy(hk32, cl0.w));
precise float cd = sqrt(mad(rz, rz, mad(rx, rx, ry * ry)));
precise float2 n3 = float2(0.0f, 0.0f);
if (cd < abs(k.s72)) {
float3 w3 = float3(mad(k.k968, px, k.w3s.x), mad(k.k968, py, k.w3s.y), mad(k.k968, pz, k.w3s.z));
float4 s3 = tex300.SampleLevel(samp1, w3, 0.0f);
precise float n3x = s3.x * 2.0f + -1.0f; // v_mad_f32
precise float n3y = s3.y * 2.0f + -1.0f;
n3 = float2(n3x, n3y);
}
precise float2 n1 = float2(mad(s1.x, 2.0f, -1.0f), mad(s1.y, 2.0f, -1.0f));
precise float2 n2 = float2(mad(s2.x, 2.0f, -1.0f), mad(s2.y, 2.0f, -1.0f));
if (0.0f < dn.x) r1 = layer(b, 0xba0u, 0xbe0u, k.r950, dn.x, hn0, n1, n2, n3);
if (0.0f < dn.y) r2 = layer(b, 0xc10u, 0xc50u, k.r950, dn.y, hn0, n1, n2, n3);
if (0.0f < dn.z) r3 = layer(b, 0xc80u, 0xcc0u, k.r950, dn.z, hn0, n1, n2, n3);
if (0.0f < dn.w) r4 = layer(b, 0xcf0u, 0xd30u, k.r950, dn.w, hn0, n1, n2, n3);
}
// 0x1a28-0x1bbc: the height fog terms at the cell.
precise float u0 = sat(mad_legacy(-h0, 1.0f / k.s40, 1.0f));
float2 f6e0 = float2(raw_f(vf, 0x6e0u), raw_f(vf, 0x6e4u));
float2 f6ec = float2(raw_f(vf, 0x6ecu), raw_f(vf, 0x6f0u));
float4 hs0 = tex320.SampleLevel(samp2, float2(mad(f6e0.x, px, f6ec.x), mad(f6e0.y, py, f6ec.y)), 0.0f);
float g0 = tex400.SampleLevel(samp3, float2(mad(mad(k.ofs848.x, u0, px), k.s47, 0.5f), mad(mad(k.ofs848.y, u0, py), k.s47, 0.5f)), 0.0f).x;
precise float r7 = 1.0f / (k.a7c + asfloat(0x33d6bf95u));
precise float e1 = sat(mad_legacy(-(pz - k.e420.x), k.e420.y, 1.0f));
precise float e2 = sat(mad_legacy(-(pz - k.e430.x), k.e430.y, 1.0f));
precise float e1c = mul_legacy(e1 * e1, e1);
precise float e2c = mul_legacy(e2 * e2, e2);
precise float fogb = sat(mul_legacy(hs0.x - pz, r7));
precise float dmax = sat(fmax_t(fmax_t(fmax_t(r1, r2), r3), r4));
precise float fog = mul_legacy(fogb, hs0.z);
fog = mad(k.e420.z, e1c, fog);
fog = sat(mad(e2c, k.e430.z, fog));
precise float gw0 = 0.0f;
if (g0 < 0.0f || g0 > 0.0f) {
precise float kk = sat(mad_legacy(20.0f, -(hn0 - (asfloat(0xbd4ccccdu) + k.b83c)), 1.0f));
gw0 = mul_legacy(k.a9c, mul_legacy(kk, g0));
}
// 0x1bc0-0x1c8c: two words of four bytes at cell (gx, gy, gz) of table 1 +0x20's buffer (8 bytes a cell).
precise float tr = sat(fmax_t(fmax_t(ox_, oy_), oz_));
precise float4 b0 = float4(mul_legacy(255.0f, tr), mul_legacy(255.0f, fog),
mul_legacy(255.0f, sat(fmax_t(gw0, gw0))), mul_legacy(255.0f, dmax));
precise float4 b1 = float4(mul_legacy(255.0f, sat(fmax_t(r1, r1))), mul_legacy(255.0f, sat(fmax_t(r2, r2))),
mul_legacy(255.0f, sat(fmax_t(r3, r3))), mul_legacy(255.0f, sat(fmax_t(r4, r4))));
uint w0 = to_u8(b0.x) | (to_u8(b0.y) << 8u) | (to_u8(b0.z) << 16u) | (to_u8(b0.w) << 24u);
uint w1 = to_u8(b1.x) | (to_u8(b1.y) << 8u) | (to_u8(b1.z) << 16u) | (to_u8(b1.w) << 24u);
View vo = view_of(table_entry(table1, 0x20u));
RWByteAddressBuffer outb = ResourceDescriptorHeap[vo.slot];
uint o = at(vo, ((((gz << 4u) + gy) << 4u) + gx) * 8u);
outb.Store2(o, uint2(w0, w1));
}