Files
larp/shaders/replace/trace_shadow_rays.hlsli

667 lines
39 KiB
HLSL

// trace_shadow_rays.hlsli: the trace of the light shadow passes' CS_TraceShadowRays programs, shared by
// the replacements of _Full (07c93bfb77fecebb), _Eighth (96ccb14ef981fd85), _Quarter (0e64f14fae060d04),
// _Half (002e93a6cc2aa355) and FullAdaptive (dbdbf36298727135). Each program runs it twice per trace, on two
// copies of it: on the structure the light record's view-heap slot names, and on the pass's fixed view when
// that slot is 0 (dbdb has two traces, so four copies).
//
// What is shared and why it may be:
// - the walk is a host ray query with the console walk's flags and mask, the same in all twelve copies (the
// readings' site tables: flags 0x14, mask 0xD5, the opaque test live, a commit ends the walk), and its
// stop, the literal cap 0xFFFF in all twelve (shadow_rays_trace_at);
// - the candidate body is 07c9's translated code (its unmatched path, console 0x33E8-0x4220; the matched
// path 0x257C-0x33E0 is the same code, cdiff 633 of 640 instructions, apart from the reject and the dead
// test below). The 32-bit programs' bodies are the same loads, constants and samples (signature
// comparison 0.877 with 07c9's; 0e64 and 002e are 96cc's code). Where they differ, the host takes what
// this text does:
// - 96cc and 0e64 read the UV buffer's second-stream stride (0e64 also its base) from the first lane
// outside a waterfall loop (96cc 0x34B8, 0e64 0x346C/0x3470); 07c9's code, used here, reads each
// lane's own;
// - the 32-bit forms branch on the owner match for the whole wave (96cc 0x2A68-0x2A6C) and read the
// instance's fields at scalar addresses: on the console every lane in a body is at one instance (the
// top level is walked wave-coherently); on the host each lane has its own candidate, and the match,
// the reject and the instance's fields are the lane's own;
// - the console's waterfall loops over per-lane descriptors are the lane's own descriptor, indexed per
// lane (NonUniformResourceIndex).
//
// The names are the decompile's numbers of 07c9's translation, its descriptor-heap
// arrays put back as ResourceDescriptorHeap (README "Descriptors").
#include "view_heap_layout.hlsli"
struct BindlessCBV_1_1_1
{
float4 _m0[4096];
};
// 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 (the raw view of the title's graphics heap as the card reads it in place, at the view heap
// slot after the null slot that ends the title's slots) at the offset in its low 28 bits times 16; otherwise
// the dword is the slot of a raw view that begins at them. Row `row` of them (16 bytes).
float4 table_cbv(uint e, uint row)
{
bool placed = (e >> 31u) != 0u;
ByteAddressBuffer b = ResourceDescriptorHeap[min(placed ? HEAP_VIEW_SLOT : e, NULL_SLOT)];
return asfloat(b.Load4((placed ? (e << 4u) : 0u) + (row << 4u)));
}
// 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, t253 space1): 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);
uint shifted(uint slot, uint off) { uint s = view_shifts.Load<uint>(min(slot, 2097151u) * 4); uint b = off + s; return b < s ? off : b; }
ByteAddressBuffer heap_srv_raw(uint i) { ByteAddressBuffer r = ResourceDescriptorHeap[i]; return r; }
RWByteAddressBuffer heap_uav_raw(uint i) { RWByteAddressBuffer r = ResourceDescriptorHeap[i]; return r; }
Texture2D<float4> heap_tex2d(uint i) { Texture2D<float4> r = ResourceDescriptorHeap[i]; return r; }
Texture2DArray<float4> heap_tex2darray(uint i) { Texture2DArray<float4> r = ResourceDescriptorHeap[i]; return r; }
Buffer<float4> heap_buf(uint i) { Buffer<float4> r = ResourceDescriptorHeap[i]; return r; }
RWTexture2D<float> heap_rwtex2d_float(uint i) { RWTexture2D<float> r = ResourceDescriptorHeap[i]; return r; }
RWTexture2D<float4> heap_rwtex2d(uint i) { RWTexture2D<float4> r = ResourceDescriptorHeap[i]; return r; }
RWBuffer<float> heap_rwbuf_float(uint i) { RWBuffer<float> r = ResourceDescriptorHeap[i]; return r; }
uint2 spvTextureSize(Texture2D<float4> Tex, uint Level, out uint Param)
{
uint2 ret;
Tex.GetDimensions(Level, ret.x, ret.y, Param);
return ret;
}
// What the pass gives every candidate of one lane's ray (the program's registers, set before the walk).
struct ShadowRaysPass
{
uint records; // heap slot of the records R (slot 0 +0x280), clamped as the translation clamps it
uint palette; // heap slot of the palette of alpha test 2 (slot 0 +0x1C0)
uint key52; // owner key ((record +0x24 << 5) & 0xFFE00000) | (record +0x6C & 0x1FFFFF): 07c9 s52
uint key53; // (record +0x6C bit 23 ? 0x80000000 : 0) | bfe(record +0x8C, 3, 21): 07c9 s53
float lod_scale; // the LOD's factor of t: 07c9 v26
float3 zero_term; // the vector whose length enters the LOD times 0 (07c9 v19 v20 v21, the origin)
};
// A candidate as the body reads it: the query's candidate getters.
struct ShadowRaysCandidate
{
float t; // v50: CandidateTriangleRayT
float i; // v59, I / den: CandidateTriangleBarycentrics().x
float j; // v60, J / den: CandidateTriangleBarycentrics().y
uint contribution; // instance +52 & 0xFFFFFF: CandidateInstanceContributionToHitGroupIndex
uint geometry; // triangle +48 >> 8: CandidateGeometryIndex
uint instance_id; // instance +48 & 0xFFFFFF: CandidateInstanceID
uint primitive; // triangle +52 / +56 by the pointer's kind: CandidatePrimitiveIndex
};
// What the code after the walk reads (07c9 "Live at X"): the hit, t, I and J, and through the committed
// pointers the contribution, the geometry index and the primitive index; and the hit kind the commit writes
// (07c9 v37, 96cc v14: 0xFE front face, 0xFF back face, CommittedTriangleFrontFace), 0 when nothing is committed.
// And what the walk's stop leaves (shadow_rays_trace_at): `count`, the candidate loop's passes (the 32-bit
// programs' count register, which X tests > 0xFFFE: 96cc v38, 0e64 v36, 002e v31, dbdb v34 / v35), and `cap`,
// all ones where the loop stopped at the cap, else 0 (07c9 v53, which X tests == -1).
struct ShadowRaysHit
{
bool hit;
float t;
float i;
float j;
uint contribution;
uint geometry;
uint primitive;
uint kind;
uint count;
uint cap;
};
// The candidate body (07c9 C 0x2450, J 0x4224): 0xFFFFFFFF commits the candidate, 0 rejects it. `kind` is the
// hit kind the walk holds at its entry and `bound` the t the matched path compares with (below).
uint shadow_rays_body_at(ShadowRaysCandidate c, ShadowRaysPass p, SamplerState s0, uint kind, float bound)
{
uint _10174 = c.contribution; // instance +52 (0x2484)
uint _10176 = 16383u & _10174;
uint _10272 = c.geometry; // triangle +48 >> 8 (0x24E8; instance +60, 0x24B8, only its base)
uint _10278 = _10272 + _10176; // r (0x250C)
uint _10280 = ((16777215u & _10174) >> 20u) & 15u;
uint _10316 = _10278 * 116u;
uint _10329 = asuint(asfloat(heap_srv_raw(p.records).Load<uint>(shifted(p.records, (min(_10316, 2147483584u) >> 2u) * 4 + 0)))); // R[r] +0 (0x2524)
uint _10331 = 16777215u & c.instance_id; // instance +48 & 0xFFFFFF (0x2518)
uint _10336 = (((_10331 >> 7u) & 131071u) << 4u) | _10280;
// The owner match (0x2510-0x2574).
bool _10339 = (p.key53 & 2147483647u) == _10336;
bool _10340 = p.key52 == (((_10331 & 127u) << 21u) | _10336);
// Matched, with record +0x6C bit 23 (s53 bit 31): reject (0x2580-0x258C). Matched otherwise, the
// console also requires (hit kind v37 != 0xFE or !(v28 < t)) (0x259C-0x25BC). The hit kind is written in
// the walk only at a commit, after which the lane leaves it, so the test reads the hit kind the walk is
// entered with: 0 in the four programs above (v37 is 0 at E), where the test always passes
// (shadow_rays_body); a program whose walk is entered with an earlier trace's hit kind passes it here.
if ((_10339 || _10340) && (((p.key53 >> 31u) != 0u) || ((kind == 254u) && (bound < c.t))))
{
return 0u;
}
uint _11554 = _10176;
uint _11555 = _10278;
uint _11558 = _10329;
// Registers the body writes and the walk carries between candidates; nothing reads them after it.
uint _11559 = 0u;
uint _11560 = 0u;
// The decision (both paths): reject when R +0 & 0x3C000; commit when R +84 <= 0; else alpha test 1,
// and alpha test 2 when it passes and R +0 bit 18 is set.
uint _16154;
uint _16155;
uint _16156;
if (0u == (245760u & _11558))
{
uint _11569 = p.records;
uint _12612;
uint _12613;
uint _12614;
if (0.0f < asfloat(heap_srv_raw(_11569).Load<uint>(shifted(_11569, (min(((_11555 * 116u) + 84u), 2147483584u) >> 2u) * 4 + 0))))
{
uint _11674 = c.geometry; // triangle +48 >> 8, through instance +60: CandidateGeometryIndex
uint _11680 = p.records;
uint _11682 = (_11674 + _11554) * 116u;
uint _11689 = asuint(asfloat(heap_srv_raw(_11680).Load<uint>(shifted(_11680, (min(_11682, 2147483584u) >> 2u) * 4 + 0))));
uint _12607;
uint _12608;
uint _12609;
if (0u == (245760u & _11689))
{
uint _11798 = c.primitive; // triangle +52 / +56 by the pointer's kind (0x3538): CandidatePrimitiveIndex
uint _11804 = p.records;
precise float _11805 = 1.0f - c.i;
precise float _11829 = _11805 - c.j;
uint _11832 = min(min(((asuint(asfloat(heap_srv_raw(_11804).Load<uint>(shifted(_11804, (min((_11682 + 12u), 2147483584u) >> 2u) * 4 + 0)))) << 5u) >> 5u), TITLE_LIMIT), NULL_SLOT);
uint _11835 = min(0u, 2147483584u) >> 2u;
float2 _11842 = asfloat(uint2(heap_srv_raw(NonUniformResourceIndex(_11832)).Load<uint>(shifted(_11832, _11835 * 4 + 0)), heap_srv_raw(NonUniformResourceIndex(_11832)).Load<uint>(shifted(_11832, (_11835 + 1u) * 4 + 0))));
uint _11849 = ((asuint(_11842.x) << 4u) & 2097136u) | 12u;
uint _11857 = asuint(asfloat(heap_srv_raw(NonUniformResourceIndex(_11832)).Load<uint>(shifted(_11832, (min(((_11798 << 2u) + _11849), 2147483584u) >> 2u) * 4 + 0))));
uint _11862 = min((12u + (32752u & (_11857 >> 17u))), 2147483584u) >> 2u;
float2 _11869 = asfloat(uint2(heap_srv_raw(NonUniformResourceIndex(_11832)).Load<uint>(shifted(_11832, _11862 * 4 + 0)), heap_srv_raw(NonUniformResourceIndex(_11832)).Load<uint>(shifted(_11832, (_11862 + 1u) * 4 + 0))));
uint _11882 = (_11849 + (asuint(_11842.y) << 2u)) + ((((asuint(_11869.x) << 2u) & 65536u) | (65535u & asuint(_11869.y))) << 1u);
uint _11884 = _11882 + (254u & (_11857 << 1u));
uint _11891 = asuint(asfloat(heap_srv_raw(NonUniformResourceIndex(_11832)).Load<uint>(shifted(_11832, (min((2147483644u & _11884), 2147483584u) >> 2u) * 4 + 0))));
uint _11898 = _11882 + (254u & (_11857 >> 6u));
uint _11900 = (1u == (1u & (_11884 >> 1u))) ? (_11891 >> 16u) : (65535u & _11891);
uint _11907 = asuint(asfloat(heap_srv_raw(NonUniformResourceIndex(_11832)).Load<uint>(shifted(_11832, (min((2147483644u & _11898), 2147483584u) >> 2u) * 4 + 0))));
uint _11914 = _11882 + (254u & (_11857 >> 13u));
uint _11923 = asuint(asfloat(heap_srv_raw(NonUniformResourceIndex(_11832)).Load<uint>(shifted(_11832, (min((2147483644u & _11914), 2147483584u) >> 2u) * 4 + 0))));
uint _11932 = (1u == (1u & (_11914 >> 1u))) ? (_11923 >> 16u) : (65535u & _11923);
bool _11933 = 1u == (1u & (asuint(asfloat(heap_srv_raw(_11804).Load<uint>(shifted(_11804, (min((_11682 + 4u), 2147483584u) >> 2u) * 4 + 0)))) >> 6u));
uint _11938 = min(min(((asuint(asfloat(heap_srv_raw(_11804).Load<uint>(shifted(_11804, (min((_11682 + 16u), 2147483584u) >> 2u) * 4 + 0)))) << 5u) >> 5u), TITLE_LIMIT), NULL_SLOT);
uint _11944 = asuint(asfloat(heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, (12u >> 2u) * 4 + 0))));
uint _11946 = _11944 >> 3u;
uint _11947 = 15u & _11944;
uint _11948 = 33554428u & (_11944 >> 7u);
bool _11950 = 2u == _11947;
uint _11952 = ((_11933 ? _11932 : _11900) << 8u) >> 8u;
uint _11954 = ((60u & _11946) << 8u) >> 8u;
uint _11956 = (_11952 * _11954) + _11948;
uint _11970;
uint _11971;
if (_11950)
{
uint _11958 = min(_11956, 2147483584u) >> 2u;
float2 _11965 = asfloat(uint2(heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, _11958 * 4 + 0)), heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, (_11958 + 1u) * 4 + 0))));
_11970 = asuint(_11965.x);
_11971 = asuint(_11965.y);
}
else
{
_11970 = _11947;
_11971 = _11946;
}
uint _12001;
uint _12002;
uint _12003;
if (_11950)
{
_12001 = _11970;
_12002 = _11971;
_12003 = _11559;
}
else
{
float _11976 = asfloat(heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, (min(124u, 2147483584u) >> 2u) * 4 + 0)));
uint _11982 = asuint(asfloat(heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, (min(_11956, 2147483584u) >> 2u) * 4 + 0))));
precise float _11987 = float(int(uint(int((_11982 & 65535u) << 16u) >> int(16u)))) / 32767.0f;
float _11988 = max(_11987, -1.0f);
precise float _11995 = float(int(uint(int(((_11982 >> 16u) & 65535u) << 16u) >> int(16u)))) / 32767.0f;
precise float _11997 = _11976 * _11988;
precise float _11999 = _11976 * max(_11995, -1.0f);
_12001 = asuint(_11997);
_12002 = asuint(_11999);
_12003 = asuint(_11988);
}
uint _12005 = (((1u == (1u & (_11898 >> 1u))) ? (_11907 >> 16u) : (65535u & _11907)) << 8u) >> 8u;
uint _12007 = (_11954 * _12005) + _11948;
uint _12021;
uint _12022;
if (_11950)
{
uint _12009 = min(_12007, 2147483584u) >> 2u;
float2 _12016 = asfloat(uint2(heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, _12009 * 4 + 0)), heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, (_12009 + 1u) * 4 + 0))));
_12021 = asuint(_12016.x);
_12022 = asuint(_12016.y);
}
else
{
_12021 = _11956;
_12022 = _12003;
}
uint _12052;
uint _12053;
uint _12054;
if (_11950)
{
_12052 = _12021;
_12053 = _12022;
_12054 = _11560;
}
else
{
float _12027 = asfloat(heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, (min(124u, 2147483584u) >> 2u) * 4 + 0)));
uint _12033 = asuint(asfloat(heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, (min(_12007, 2147483584u) >> 2u) * 4 + 0))));
precise float _12038 = float(int(uint(int((_12033 & 65535u) << 16u) >> int(16u)))) / 32767.0f;
float _12039 = max(_12038, -1.0f);
precise float _12046 = float(int(uint(int(((_12033 >> 16u) & 65535u) << 16u) >> int(16u)))) / 32767.0f;
precise float _12048 = _12027 * _12039;
precise float _12050 = _12027 * max(_12046, -1.0f);
_12052 = asuint(_12048);
_12053 = asuint(_12050);
_12054 = asuint(_12039);
}
uint _12056 = ((_11933 ? _11900 : _11932) << 8u) >> 8u;
uint _12058 = (_11954 * _12056) + _11948;
uint _12072;
uint _12073;
if (_11950)
{
uint _12060 = min(_12058, 2147483584u) >> 2u;
float2 _12067 = asfloat(uint2(heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, _12060 * 4 + 0)), heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, (_12060 + 1u) * 4 + 0))));
_12072 = asuint(_12067.x);
_12073 = asuint(_12067.y);
}
else
{
_12072 = _12007;
_12073 = _12054;
}
uint _12102;
uint _12103;
if (_11950)
{
_12102 = _12072;
_12103 = _12073;
}
else
{
float _12078 = asfloat(heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, (min(124u, 2147483584u) >> 2u) * 4 + 0)));
uint _12084 = asuint(asfloat(heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, (min(_12058, 2147483584u) >> 2u) * 4 + 0))));
precise float _12089 = float(int(uint(int((_12084 & 65535u) << 16u) >> int(16u)))) / 32767.0f;
precise float _12096 = float(int(uint(int(((_12084 >> 16u) & 65535u) << 16u) >> int(16u)))) / 32767.0f;
precise float _12098 = _12078 * max(_12089, -1.0f);
precise float _12100 = _12078 * max(_12096, -1.0f);
_12102 = asuint(_12098);
_12103 = asuint(_12100);
}
uint _12109 = p.records;
float _12110 = p.zero_term.y;
precise float _12111 = _12110 * _12110;
precise float _12113 = c.i * asfloat(_12052);
precise float _12115 = c.i * asfloat(_12053);
float _12116 = p.zero_term.x;
precise float _12117 = _12116 * _12116;
precise float _12118 = _12117 + _12111;
precise float _12120 = _11829 * asfloat(_12001);
precise float _12121 = _12120 + _12113;
precise float _12123 = _11829 * asfloat(_12002);
precise float _12124 = _12123 + _12115;
float _12125 = p.zero_term.z;
precise float _12126 = _12125 * _12125;
precise float _12127 = _12126 + _12118;
precise float _12129 = c.j * asfloat(_12102);
precise float _12130 = _12129 + _12121;
precise float _12132 = c.j * asfloat(_12103);
precise float _12133 = _12132 + _12124;
uint _12134 = asuint(_12133);
precise float _12135 = 0.0f + p.lod_scale;
uint _12140 = min((_11682 + 68u), 2147483584u) >> 2u;
float4 _12153 = asfloat(uint4(heap_srv_raw(_12109).Load<uint>(shifted(_12109, _12140 * 4 + 0)), heap_srv_raw(_12109).Load<uint>(shifted(_12109, (_12140 + 1u) * 4 + 0)), heap_srv_raw(_12109).Load<uint>(shifted(_12109, (_12140 + 2u) * 4 + 0)), heap_srv_raw(_12109).Load<uint>(shifted(_12109, (_12140 + 3u) * 4 + 0))));
float _12157 = _12153.w;
uint _12165 = asuint(asfloat(heap_srv_raw(_12109).Load<uint>(shifted(_12109, (min((_11682 + 52u), 2147483584u) >> 2u) * 4 + 0))));
precise float _12172 = sqrt(_12127) * 0.0f;
precise float _12173 = _12172 + 0.0f;
precise float _12174 = _12135 * c.t;
precise float _12175 = _12174 + _12173;
precise float _12176 = _12153.x * _12130;
precise float _12177 = _12176 + _12153.z;
uint _12178 = _12165 << 5u;
precise float _12179 = _12153.y * _12133;
precise float _12180 = _12179 + _12157;
// The texture's size and levels (console 0x2D60-0x2E80, a waterfall loop over the lanes'
// descriptors): the lane's own texture, indexed per lane.
uint _12242;
uint2 _12192 = spvTextureSize(heap_tex2d(NonUniformResourceIndex(min(min((_12178 >> 5u), TITLE_LIMIT), NULL_SLOT))), 0u, _12242);
uint _12195 = _12192.x - 1u;
uint _12184 = ((_12195 << 30u) >> 30u) | ((((_12195 >> 2u) & 16383u) | ((_12192.y - 1u) << 14u)) << 2u);
uint _12229 = (((((_12192.x - 1u) >> 2u) & 16383u) | ((_12192.y - 1u) << 14u)) >> 14u) & 65535u;
uint _12247 = ((2415923116u | ((_12242 - 1u) << 16u)) >> 12u) & 15u;
uint _12250 = _12247 & 31u;
precise float _12253 = _12175 + _12175;
float _12254 = float(int((_12229 + 1u) >> _12250));
float _12256 = float(int((((_12184 << 16u) >> 16u) + 1u) >> _12250));
uint _12260 = asuint(_12256) | asuint(_12254);
precise float _12266 = 7.0f + log2(_12253);
float _12271 = ((asuint(_12266) & 2147483647u) == 0u) ? 0.0f : max(0.0f, _12266);
precise float _12273 = (-1.0f) + log2(((_12260 & 2147483647u) == 0u) ? asfloat(_12260) : min(_12256, _12254));
precise float _12274 = (-5.0f) + _12273;
float _12279 = ((asuint(_12274) & 2147483647u) == 0u) ? 0.0f : max(0.0f, _12274);
uint _12282 = asuint(_12271) | asuint(_12279);
float _12286 = ((_12282 & 2147483647u) == 0u) ? asfloat(_12282) : min(_12271, _12279);
float4 _12294 = heap_tex2d(NonUniformResourceIndex(min(min((_12178 >> 5u), TITLE_LIMIT), NULL_SLOT))).SampleLevel(s0, float2(_12177, _12180), _12286);
float _12296 = _12294.w;
bool _12299 = _12296 >= asfloat(heap_srv_raw(_12109).Load<uint>(shifted(_12109, (min((_11682 + 84u), 2147483584u) >> 2u) * 4 + 0)));
bool _12301 = 1u == (1u & (_11689 >> 18u));
uint _12602;
uint _12603;
uint _12604;
if (_12299 ? (!_12301) : true)
{
_12602 = uint(_12299);
_12603 = asuint(_12296);
_12604 = _12103;
}
else
{
uint _12310 = asuint(asfloat(heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, (min(4u, 2147483584u) >> 2u) * 4 + 0))));
uint _12315 = asuint(asfloat(heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, (16u >> 2u) * 4 + 0))));
uint _12339 = p.palette;
uint _12346 = p.records;
uint _12348 = _12315 >> 3u;
uint _12350 = 33554428u & (_12315 >> 7u);
uint _12351 = 60u & _12348;
bool _12352 = 2u == (15u & _12315);
uint _12354 = (_12351 << 8u) >> 8u;
uint _12356 = (_12354 * _11952) + _12350;
uint _12364 = asuint(asfloat(heap_srv_raw(_12346).Load<uint>(shifted(_12346, (min((_11682 + 56u), 2147483584u) >> 2u) * 4 + 0))));
precise float _12368 = 0.0039215688593685626983642578125f * float((asuint(asfloat(heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, (min((((((60u & (_12310 >> 3u)) << 8u) >> 8u) * _11952) + (33554428u & (_12310 >> 7u))), 2147483584u) >> 2u) * 4 + 0)))) >> 8u) & 255u);
precise float _12369 = 63.0f * _12368;
uint _12371 = uint(_12369) << 4u;
uint _12372 = _12364 + _12371;
uint _12376 = min((656u + _12372), 2147483584u) >> 2u;
float3 _12386 = asfloat(uint3(heap_srv_raw(_12339).Load<uint>(shifted(_12339, _12376 * 4 + 0)), heap_srv_raw(_12339).Load<uint>(shifted(_12339, (_12376 + 1u) * 4 + 0)), heap_srv_raw(_12339).Load<uint>(shifted(_12339, (_12376 + 2u) * 4 + 0))));
uint _12392 = min((528u + _12372), 2147483584u) >> 2u;
float3 _12402 = asfloat(uint3(heap_srv_raw(_12339).Load<uint>(shifted(_12339, _12392 * 4 + 0)), heap_srv_raw(_12339).Load<uint>(shifted(_12339, (_12392 + 1u) * 4 + 0)), heap_srv_raw(_12339).Load<uint>(shifted(_12339, (_12392 + 2u) * 4 + 0))));
float _12404 = _12402.y;
uint _12420;
uint _12421;
if (_12352)
{
uint _12408 = min(_12356, 2147483584u) >> 2u;
float2 _12415 = asfloat(uint2(heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, _12408 * 4 + 0)), heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, (_12408 + 1u) * 4 + 0))));
_12420 = asuint(_12415.x);
_12421 = asuint(_12415.y);
}
else
{
_12420 = _12364;
_12421 = _12348;
}
uint _12452;
uint _12453;
uint _12454;
uint _12455;
if (_12352)
{
_12452 = _12420;
_12453 = _12421;
_12454 = asuint(_12157);
_12455 = _12165;
}
else
{
float _12426 = asfloat(heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, (min(124u, 2147483584u) >> 2u) * 4 + 0)));
uint _12432 = asuint(asfloat(heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, (min(_12356, 2147483584u) >> 2u) * 4 + 0))));
precise float _12437 = float(int(uint(int((_12432 & 65535u) << 16u) >> int(16u)))) / 32767.0f;
float _12438 = max(_12437, -1.0f);
precise float _12445 = float(int(uint(int(((_12432 >> 16u) & 65535u) << 16u) >> int(16u)))) / 32767.0f;
float _12446 = max(_12445, -1.0f);
precise float _12448 = _12426 * _12438;
precise float _12450 = _12426 * _12446;
_12452 = asuint(_12448);
_12453 = asuint(_12450);
_12454 = asuint(_12438);
_12455 = asuint(_12446);
}
uint _12457 = (_12354 * _12005) + _12350;
uint _12471;
uint _12472;
if (_12352)
{
uint _12459 = min(_12457, 2147483584u) >> 2u;
float2 _12466 = asfloat(uint2(heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, _12459 * 4 + 0)), heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, (_12459 + 1u) * 4 + 0))));
_12471 = asuint(_12466.x);
_12472 = asuint(_12466.y);
}
else
{
_12471 = _12454;
_12472 = _12455;
}
uint _12501;
uint _12502;
if (_12352)
{
_12501 = _12471;
_12502 = _12472;
}
else
{
float _12477 = asfloat(heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, (min(124u, 2147483584u) >> 2u) * 4 + 0)));
uint _12483 = asuint(asfloat(heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, (min(_12457, 2147483584u) >> 2u) * 4 + 0))));
precise float _12488 = float(int(uint(int((_12483 & 65535u) << 16u) >> int(16u)))) / 32767.0f;
precise float _12495 = float(int(uint(int(((_12483 >> 16u) & 65535u) << 16u) >> int(16u)))) / 32767.0f;
precise float _12497 = _12477 * max(_12488, -1.0f);
precise float _12499 = _12477 * max(_12495, -1.0f);
_12501 = asuint(_12497);
_12502 = asuint(_12499);
}
uint _12504 = (_12354 * _12056) + _12350;
uint _12518;
uint _12519;
if (_12352)
{
uint _12506 = min(_12504, 2147483584u) >> 2u;
float2 _12513 = asfloat(uint2(heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, _12506 * 4 + 0)), heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, (_12506 + 1u) * 4 + 0))));
_12518 = asuint(_12513.x);
_12519 = asuint(_12513.y);
}
else
{
_12518 = _12350;
_12519 = _12351;
}
uint _12548;
uint _12549;
if (_12352)
{
_12548 = _12518;
_12549 = _12519;
}
else
{
float _12524 = asfloat(heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, (min(124u, 2147483584u) >> 2u) * 4 + 0)));
uint _12530 = asuint(asfloat(heap_srv_raw(NonUniformResourceIndex(_11938)).Load<uint>(shifted(_11938, (min(_12504, 2147483584u) >> 2u) * 4 + 0))));
precise float _12535 = float(int(uint(int((_12530 & 65535u) << 16u) >> int(16u)))) / 32767.0f;
precise float _12542 = float(int(uint(int(((_12530 >> 16u) & 65535u) << 16u) >> int(16u)))) / 32767.0f;
precise float _12544 = _12524 * max(_12535, -1.0f);
precise float _12546 = _12524 * max(_12542, -1.0f);
_12548 = asuint(_12544);
_12549 = asuint(_12546);
}
uint _12555 = p.palette;
precise float _12559 = c.i * asfloat(_12502);
precise float _12561 = _11829 * asfloat(_12453);
precise float _12562 = _12561 + _12559;
precise float _12564 = c.j * asfloat(_12549);
precise float _12565 = _12564 + _12562;
precise float _12573 = c.i * asfloat(_12501);
precise float _12575 = _11829 * asfloat(_12452);
precise float _12576 = _12575 + _12573;
precise float _12578 = c.j * asfloat(_12548);
precise float _12579 = _12578 + _12576;
precise float _12580 = _12402.x * _12579;
precise float _12581 = _12386.x * _12579;
precise float _12582 = _12404 * _12565;
precise float _12583 = _12582 + _12580;
precise float _12584 = _12386.y * _12565;
precise float _12585 = _12584 + _12581;
precise float _12586 = _12402.z + _12583;
precise float _12587 = _12386.z + _12585;
_12602 = uint(_12299 && (_12301 && (0.125f <= heap_tex2d(NonUniformResourceIndex(min(min(((asuint(asfloat(heap_srv_raw(_12555).Load<uint>(shifted(_12555, (min(((1936u + _12364) + _12371), 2147483584u) >> 2u) * 4 + 0)))) << 5u) >> 5u), TITLE_LIMIT), NULL_SLOT))).SampleLevel(s0, float2(_12586, _12587), _12286).w)));
_12603 = asuint(_12404);
_12604 = _12452;
}
_12607 = (0u != _12602) ? 4294967295u : 0u;
_12608 = _12603;
_12609 = _12604;
}
else
{
_12607 = 0u;
_12608 = _11559;
_12609 = _11560;
}
_12612 = uint(4294967295u == _12607);
_12613 = _12608;
_12614 = _12609;
}
else
{
_12612 = 1u;
_12613 = _11559;
_12614 = _11560;
}
_16154 = (0u != _12612) ? 4294967295u : 0u;
_16155 = _12613;
_16156 = _12614;
}
else
{
_16154 = 0u;
_16155 = _11559;
_16156 = _11560;
}
return _16154;
}
// The body of a walk entered with hit kind 0 (the four programs above): the matched path's test always passes.
uint shadow_rays_body(ShadowRaysCandidate c, ShadowRaysPass p, SamplerState s0)
{
return shadow_rays_body_at(c, p, s0, 0u, 0.0f);
}
// No hit: what X reads after a walk that commits nothing (t is t max, I, J and the pointers 0), and a count of
// 0 below the cap.
ShadowRaysHit shadow_rays_miss(float tmax)
{
ShadowRaysHit h;
h.hit = false;
h.t = tmax;
h.i = 0.0f;
h.j = 0.0f;
h.contribution = 0u;
h.geometry = 0u;
h.primitive = 0u;
h.kind = 0u;
h.count = 0u;
h.cap = 0u;
return h;
}
// One copy's walk (07c9 heap copy: E 0x1E08, W [0x1E00, 0x4398), X 0x4398; the fixed copy the same at
// +0x2A00) as a host ray query on `scene`.
// - Flags 0x14: ACCEPT_FIRST_HIT_AND_END_SEARCH (a commit, opaque or from the body, takes the lane out of
// the walk, 0x4388-0x4390) and CULL_BACK_FACING_TRIANGLES (cull multiplier 1, 0x232C; the instance's
// flags enter as D3D12 uses them). No opacity flag: the opaque test is live (triangle +48 bit 0 and the
// instance's flags, 0x2400-0x2444), so the host decides from the geometry's and the instance's flags,
// and only non-opaque candidates reach the body. Mask 0xD5 (instance +48 & 0xD5000000, 0x21B4).
// - The ray: [t min, t max) on the console (0x23D0, 0x23E0), [t min, t max] on the host; a candidate at
// exactly t max may differ. D3D12 takes no t min below 0: the host traces
// [max(t min, 0), t max], so a candidate in [t min, 0) is not found. A ray with a NaN, or with no
// range left, is not traced; the console's walk finds nothing in an empty range either.
// - The root load (header +48) is the constant 1: the root skip of the 32-bit form is never taken.
// - The stop: every walk of the five programs counts its passes and leaves at a count of 0xFFFF, a literal in
// the code, and X counts a capped lane as a hit. 07c9 counts the wave's passes (s19 += 1, then
// `s_cmpk_gt_u32 s19, 0xfffe` before the pass's node test: 0x1FC4/0x1FCC, fixed 0x49C4/0x49CC; the lanes
// still in the walk get v53 = -1 and leave it), and X tests v53 == -1 (0x43A0, 0x6DA0). The 32-bit forms
// count each lane's node visits (+1 at each top- and bottom-level pass) and end the lane after the pass
// when !(0xFFFF > count) (`v_cmp_gt_u32 0xffff, count`: 96cc v38 0x4794, fixed 0x722C; 0e64 v36 0x4744,
// 0x71E4; 002e v31 0x468C, 0x712C; dbdb trace 1 v34 0x4824, v35 0x7374, trace 2 v34 0xCC34, v35 0xF754),
// and X tests count > 0xFFFE (96cc 0x7334, 0x76E4; 0e64 0x72EC, 0x769C; 002e 0x7234, 0x75E4; dbdb 0x74A4,
// 0x788C, 0xF854, 0xFB7C). A capped lane has nothing committed (a commit ends the walk first), so X reads
// the committed pointers as 0: the console's hit path then reads the structure's header through them, which
// the host has not; the host gives what the query holds, the miss's values (t max; I, J, contribution,
// geometry, primitive and kind 0). Here the candidate loop counts its passes from 0, one per candidate,
// and before a candidate's body ends the query at count > 0xFFFE (the same test as 0xFFFF > count failing),
// with `count` and `cap` (all ones) in the result. A pass of the console's walk is a node visit and a pass
// of the candidate loop is a candidate, and a ray has at most as many candidates as node visits (07c9: as
// the wave has passes): so the host's stop never ends a ray the console would not have ended, and it may
// let one go on that the console would have ended.
// - The LDS stack and the walk's registers are gone with the walk.
// - `kind` and `bound`: the hit kind the walk is entered with and the matched path's bound (shadow_rays_body_at).
ShadowRaysHit shadow_rays_trace_at(RaytracingAccelerationStructure scene, float3 origin, float tmin, float3 direction,
float tmax, ShadowRaysPass p, SamplerState s0, uint kind, float bound)
{
ShadowRaysHit h = shadow_rays_miss(tmax);
float t0 = max(tmin, 0.0f);
if (!(t0 < tmax) || isnan(tmin) || any(isnan(origin)) || any(isnan(direction)))
{
return h;
}
RayQuery<RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH | RAY_FLAG_CULL_BACK_FACING_TRIANGLES> q;
RayDesc ray = { origin, t0, direction, tmax };
q.TraceRayInline(scene, RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH | RAY_FLAG_CULL_BACK_FACING_TRIANGLES, 0xD5u, ray);
while (q.Proceed())
{
// The stop (above): a capped lane leaves with nothing committed.
h.count += 1u;
if (h.count > 65534u)
{
h.cap = 4294967295u;
q.Abort();
break;
}
// A non-opaque candidate: the body (C 0x2450 to J 0x4224).
ShadowRaysCandidate c;
c.t = q.CandidateTriangleRayT();
float2 b = q.CandidateTriangleBarycentrics();
c.i = b.x;
c.j = b.y;
c.contribution = q.CandidateInstanceContributionToHitGroupIndex();
c.geometry = q.CandidateGeometryIndex();
c.instance_id = q.CandidateInstanceID();
c.primitive = q.CandidatePrimitiveIndex();
// J (0x4224): v65 == -1 commits; the commit ends the lane's walk.
if (shadow_rays_body_at(c, p, s0, kind, bound) == 4294967295u)
{
q.CommitNonOpaqueTriangleHit();
q.Abort();
}
}
if (q.CommittedStatus() == COMMITTED_TRIANGLE_HIT)
{
h.hit = true;
h.t = q.CommittedRayT();
float2 b = q.CommittedTriangleBarycentrics();
h.i = b.x;
h.j = b.y;
h.contribution = q.CommittedInstanceContributionToHitGroupIndex();
h.geometry = q.CommittedGeometryIndex();
h.primitive = q.CommittedPrimitiveIndex();
h.kind = q.CommittedTriangleFrontFace() ? 254u : 255u;
}
return h;
}
// A walk entered with hit kind 0 (the four programs above).
ShadowRaysHit shadow_rays_trace(RaytracingAccelerationStructure scene, float3 origin, float tmin, float3 direction,
float tmax, ShadowRaysPass p, SamplerState s0)
{
return shadow_rays_trace_at(scene, origin, tmin, direction, tmax, p, s0, 0u, 0.0f);
}