From 51267aded3fc2edb1bdb83fa207f8efdbdbbe1dd Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Mon, 23 Nov 2026 23:03:20 -0500 Subject: [PATCH 1/5] Model M (D3D12): hoist 8-byte base alignment masks in post_tf (-41.3 us on Windows AMD DX12, byte-identical) --- clean/dx12/kernels/post.hlsl | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/clean/dx12/kernels/post.hlsl b/clean/dx12/kernels/post.hlsl index ef349ecd..933de445 100644 --- a/clean/dx12/kernels/post.hlsl +++ b/clean/dx12/kernels/post.hlsl @@ -286,10 +286,8 @@ TexF tex_features(uint dw) // r2_features of the texel at dword dw return f; } -// 8-byte arena loads with their alignment made visible to the driver: RWByteAddressBuffer.Load2 / Load4 at a computed -// offset is known to be 4-byte aligned only, so each became two / four global_load_b32. The history (hist_prev) and the -// A8 features are the runtime's own allocations (256-byte aligned) and every offset formed on them here is a multiple of -// 8 bytes, so clearing the address's low 3 bits changes no address; the driver then issues global_load_b64. +// 8-byte arena loads: mask the 256-byte-aligned base pointers once at the scalar level (keeping 8-byte alignment +// provable for RADV/ACO without emitting per-load v_and_b32 or blocking immediate offset folding on AMD DX12). uint2 ld64_a8(uint a) { return wg_mem.Load2(a & ~7u); } uint4 ld128_a8(uint a) { return wg_mem.Load4(a & ~7u); } @@ -307,7 +305,7 @@ TfRaw tf_fetch(int idx, int col, int row PD_P) #else t.c = wg_ld4f(color, ui * 16u); #endif - uint2 d = ld64_a8(A8 + ui * 8u); + uint2 d = wg_mem.Load2((A8 & ~7u) + ui * 8u); t.d0 = d.x; t.d1 = d.y; t.c01 = wg_ld32(C4, ui * 4u); @@ -467,26 +465,27 @@ float3 history_cr_span(float qx, float qy) // lshl_add + one mad_u24 (iy in [1, P_dh - 3] and pitch = 8 P_dw < 2^24, so the 24-bit product is the full one) // (the constant opaque, g_z: the driver would otherwise split it again into its parts, one add each) #if WG_POST_CUTS - uint b0 = mul24((uint)iy, pitch) + ((uint)ix * 8u + ((hist_prev - 8u - pitch) ^ g_z)); + uint b0 = (mul24((uint)iy, pitch) + ((uint)ix * 8u + (((hist_prev & ~7u) - 8u - pitch) ^ g_z))) & ~7u; #else - uint b0 = hist_prev + (uint)(ix - 1) * 8u + mul24((uint)(iy - 1), pitch); + uint b0 = (hist_prev & ~7u) + (uint)(ix - 1) * 8u + mul24((uint)(iy - 1), pitch); #endif #if defined(OP_POST_MUTANT) && OP_POST_MUTANT == 4 // test-only mutant (byte check must catch it): history base +8 b0 += 8u; #endif const uint b1 = b0 + pitch, b2 = b0 + 2u * pitch, b3 = b0 + 3u * pitch; - const uint4 a0 = ld128_a8(b0 + 8u), a10 = ld128_a8(b1), a11 = ld128_a8(b1 + 16u); - const uint4 a20 = ld128_a8(b2), a21 = ld128_a8(b2 + 16u), a3 = ld128_a8(b3 + 8u); + const uint4 a0 = wg_mem.Load4(b0 + 8u), a10 = wg_mem.Load4(b1), a11 = wg_mem.Load4(b1 + 16u); + const uint4 a20 = wg_mem.Load4(b2), a21 = wg_mem.Load4(b2 + 16u), a3 = wg_mem.Load4(b3 + 8u); return cr_sum(a0.xy, a0.zw, a10.xy, a10.zw, a11.xy, a11.zw, a20.xy, a20.zw, a21.xy, a21.zw, a3.xy, a3.zw, cr_weights(tx - fx0), cr_weights(ty - fy0)); } + const uint hp8 = hist_prev & ~7u; const uint ro0 = mul24((uint)clampi(iy - 1, 0, P_dh - 1), pitch), ro1 = mul24((uint)clampi(iy, 0, P_dh - 1), pitch); const uint ro2 = mul24((uint)clampi(iy + 1, 0, P_dh - 1), pitch), ro3 = mul24((uint)clampi(iy + 2, 0, P_dh - 1), pitch); - const uint c0 = hist_prev + (uint)clampi(ix - 1, 0, P_dw - 1) * 8u, c1 = hist_prev + (uint)clampi(ix, 0, P_dw - 1) * 8u; - const uint c2 = hist_prev + (uint)clampi(ix + 1, 0, P_dw - 1) * 8u, c3 = hist_prev + (uint)clampi(ix + 2, 0, P_dw - 1) * 8u; - return cr_sum(ld64_a8(ro0 + c1), ld64_a8(ro0 + c2), ld64_a8(ro1 + c0), ld64_a8(ro1 + c1), - ld64_a8(ro1 + c2), ld64_a8(ro1 + c3), ld64_a8(ro2 + c0), ld64_a8(ro2 + c1), - ld64_a8(ro2 + c2), ld64_a8(ro2 + c3), ld64_a8(ro3 + c1), ld64_a8(ro3 + c2), + const uint c0 = hp8 + (uint)clampi(ix - 1, 0, P_dw - 1) * 8u, c1 = hp8 + (uint)clampi(ix, 0, P_dw - 1) * 8u; + const uint c2 = hp8 + (uint)clampi(ix + 1, 0, P_dw - 1) * 8u, c3 = hp8 + (uint)clampi(ix + 2, 0, P_dw - 1) * 8u; + return cr_sum(wg_mem.Load2(ro0 + c1), wg_mem.Load2(ro0 + c2), wg_mem.Load2(ro1 + c0), wg_mem.Load2(ro1 + c1), + wg_mem.Load2(ro1 + c2), wg_mem.Load2(ro1 + c3), wg_mem.Load2(ro2 + c0), wg_mem.Load2(ro2 + c1), + wg_mem.Load2(ro2 + c2), wg_mem.Load2(ro2 + c3), wg_mem.Load2(ro3 + c1), wg_mem.Load2(ro3 + c2), cr_weights(tx - fx0), cr_weights(ty - fy0)); } From b28679f3dc6f98eeef776c142ce6019cf8d5a8f9 Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:15:05 -0500 Subject: [PATCH 2/5] Model M (D3D12): fold scale_rgb into the post_tf store when the pre-exposure is not 1 (-0.11 ms at pi = 2, byte-identical) On the staged native D3D12 route with a pre-exposure other than 1, the post_tf kernels (post_tf_f32_p12_diet_vsit, post_tf4_dl_*, pmo/post_tf_*_mo) now apply the scale_rgb multiply in their own store (native_scale's values, including the exact repair of a flushed tiny product), so the separate scale_rgb launch is gone. pi = 1, raw / direct / Model L routes and the interop output copy are untouched. WG_SCALE_FOLD=0 (runtime option scale_fold=0) brings the launch back. Windows AMD DX12 (RX 9070 XT, 1706x960 -> 2560x1440, 8 interleaved ABBA rounds, medians): pi = 2.0 frame span 2333 -> 2222 us (-111 us), kernel profile sum 2318 -> 2202 us (scale_rgb 128 -> 0 us, post_tf +10 us); pi = 1.0 unchanged (span -2 us, profile sum -2.5 us; post_tf keeps 85 VGPRs, 30336 B LDS). Output, history and every named buffer are byte-identical to the unfolded sequence and to v0.6.5 for pi in 1, 2, 0.7, 1e-3, 1e4, 1e38 and a mixed-pi stream, with +-0, denormal, huge, Inf and NaN colour pixels, on the post_tf, post_tf4, native-AA and motion-at-output-resolution routes. --- clean/FORMATS.md | 1 + clean/dx12/kernels/post.hlsl | 36 ++++++++++++++++- clean/runtime/runtime.cpp | 20 ++++++++-- clean/tests/runtime_frame_trace.cpp | 6 ++- clean/tests/test_runtime_frame_trace.py | 52 +++++++++++++++++++++++++ 5 files changed, 109 insertions(+), 6 deletions(-) diff --git a/clean/FORMATS.md b/clean/FORMATS.md index 75d6af0d..9a6829f9 100644 --- a/clean/FORMATS.md +++ b/clean/FORMATS.md @@ -422,6 +422,7 @@ frame. | post (fused) | `post_kernel` | `WG_POST_KERNEL` | — | `post_fused*`, `post_fused3*`: reconstruction + filter in one launch (no display image; history 6 or 4 bytes, `_o16` RGBA16F output — refused for an external output unless `FrameIo.out_rgba16f`, and with pre-exposure ≠ 1) | | filter | `filter_kernel` | `WG_FILTER_KERNEL` | `post_filter_c1_s4_band` (with the banded default; else `post_filter_c1_s4`) | `post_filter[_c1][_s4\|_s16]` | | post (true fusion) | runtime options `post_tf`, `post_tf_cpairs` | `WG_POST_TF=0` (or `WG_RUNTIME_OPTIONS=post_tf=0`) | on: frames of the default banded pack12 path (post kernels in wave64, default filter) that `tf_supported` takes run one `post_tf_p12` / `post_tf_dl_p12` launch (interop output copy: RGBA16F rows, π folded in) or `post_tf_f32_p12` / `post_tf_dl_f32_p12` (3 × f32, then `scale_rgb` when π ≠ 1); C = 68 output row pairs per workgroup, 36 for the last dispatch round | `post_tf=0`: the banded sequence; `post_tf_cpairs=`; reported as `post_tf=` and `post_last` | +| scale_rgb fold (native D3D12) | runtime option `scale_fold` | `WG_SCALE_FOLD=0` (or `WG_RUNTIME_OPTIONS=scale_fold=0`) | on: staged buffers (not raw / direct / Model L / interop output copy) on the `post_tf_f32_p12_diet_vsit` / `post_tf4_dl_*` / `pmo/post_tf_*_mo` route with π ≠ 1: `scale_rgb` is done by the post kernel's store (its π argument; `native_scale`'s values), one launch fewer, the same bytes | `scale_fold=0`: `scale_rgb` after the post | Both spec 06 kernels take an extra last argument `expo`: a pointer to the exposure pair. When it is not null (the runtime), they derive e, e_prev, e/e_prev and 0.01/e from it on the GPU (0 → 1, the quotients via binary64 then diff --git a/clean/dx12/kernels/post.hlsl b/clean/dx12/kernels/post.hlsl index 933de445..f5e870b9 100644 --- a/clean/dx12/kernels/post.hlsl +++ b/clean/dx12/kernels/post.hlsl @@ -257,6 +257,30 @@ float hw_hi(uint t, float w) { return mad(f16tof32(t >> 16), w, asfloat(g_z)); } // post.hip framebuffer_half: v clamped to +-65504 by compares (a NaN passes), then (_Float16) (v_cvt_f16_f32, RNE: the // driver's conversion, cvt3_f16). float fb_clamp(float v) { return select(v > 65504.0f, 65504.0f, select(v < -65504.0f, -65504.0f, v)); } +#if POST_OUT == 0 +// native_scale (native_helpers.hlsl: the staged route's scale_rgb) of one value: the product, plus the exact repair of a +// tiny product that the driver flushes to zero although its RNE result is the smallest normal (needs a zero product of a +// normal a, so only such lanes run it; every other lane's result is the product itself) +float m1_scale_ref(float a, float b) +{ + const float r = a * b; + const uint ua = asuint(a), ub = asuint(b); + const uint ea = (ua >> 23u) & 255u, eb = (ub >> 23u) & 255u; + if ((asuint(r) & 0x7fffffffu) == 0u && ea != 0u && eb != 0u && ea + eb == 127u) { + const uint64_t product = (uint64_t)((ua & 0x7fffffu) | 0x800000u) * (uint64_t)((ub & 0x7fffffu) | 0x800000u); + const uint64_t normal = (uint64_t)1u << 47u; + if (product >= normal - ((uint64_t)1u << 23u) && product < normal) + return asfloat(((ua ^ ub) & 0x80000000u) | 0x00800000u); + } + return r; +} +float m1_scale(float a, float b) +{ + float r = a * b; + [branch] if (and(abs(r) == 0.0f, abs(a) >= asfloat(0x00800000u))) r = m1_scale_ref(a, b); + return r; +} +#endif #if POST_OUT == 3 // The direct form's stored value. RGBA16F output with output_copy (nd_output_rne16): the HIP output-copy bytes, // framebuffer_half (clamp, RNE16; NaN passes), since typed RGBA16F stores round toward zero on this driver; the typed @@ -1423,7 +1447,17 @@ WG_KERNEL(post_tf_p12_diet_vsit) } #if POST_OUT == 0 const uint o = mul24((uint)y, (uint)ow * 12u) + mul24((uint)xg, 12u); - wg_mem.Store3(out_p + o, uint3(asuint(vo[0] * dec_scale), asuint(vo[1] * dec_scale), asuint(vo[2] * dec_scale))); + [branch] if (pi_arg == 1.0f) { + wg_mem.Store3(out_p + o, uint3(asuint(vo[0] * dec_scale), asuint(vo[1] * dec_scale), asuint(vo[2] * dec_scale))); + } else { + // The runtime's scale_rgb folded in (native D3D12 only; pi_arg = the frame's pre-exposure, 1 otherwise): + // native_scale(v dec_scale, pi) per channel, the stored FP32 value of the staged route. mul_nz keeps the + // first product's rounding (the driver would regroup v (dec_scale pi)); the repair of a flushed tiny + // product runs only on the lanes whose product is zero (as native_direct_scale). + const float3 res = float3(mul_nz(vo[0], dec_scale), mul_nz(vo[1], dec_scale), mul_nz(vo[2], dec_scale)); + const float3 so = float3(m1_scale(res.x, pi_arg), m1_scale(res.y, pi_arg), m1_scale(res.z, pi_arg)); + wg_mem.Store3(out_p + o, uint3(asuint(so.x), asuint(so.y), asuint(so.z))); + } #else // framebuffer_half((v dec_scale) pi): clamp to +-65504 (NaN passes), round to FP16 to nearest even; alpha 1. opq: // the driver would otherwise regroup the two uniform factors into v (dec_scale pi) diff --git a/clean/runtime/runtime.cpp b/clean/runtime/runtime.cpp index c693e191..4d819b72 100644 --- a/clean/runtime/runtime.cpp +++ b/clean/runtime/runtime.cpp @@ -484,6 +484,9 @@ struct Runtime { // True fusion (post_tf*, runtime option post_tf / WG_POST_TF, default 1): frames of the default banded pack12 path // whose size post_tf takes run one post_tf launch instead of the banded reconstruction + filter sequence. bool post_tf = true; + // Staged native D3D12 route (backend 3, buffer resources) with a pre-exposure other than 1: the output scale (scale_rgb) is + // done by the true-fusion post kernel's store (WG_SCALE_FOLD / option scale_fold, default on): the same bytes, one launch fewer + bool scale_fold = true; bool output_quality9 = false; // private Model M final-resampling quality profile; histories/weights unchanged // output_filter=quality5: the 5-tap output filter with WrongGPU's quality5 weights. The post kernels come from // post_w64__q5.co (post.hip built with WG_LZ_PROFILE=1: the same kernels with other filter constants), so every @@ -1428,6 +1431,7 @@ struct Runtime { if (k == "stream_priority") high_priority = v == "high"; if (k == "post_bands") post_bands = std::max(1, std::atoi(v.c_str())); if (k == "post_tf") post_tf = !(v == "0" || v == "off" || v == "false"); + if (k == "scale_fold") scale_fold = !(v == "0" || v == "off" || v == "false"); if (k == "output_filter") { if (v != "default" && v != "original" && v != "analytic" && v != "quality5" && v != "quality9") return fail("output_filter must be original, analytic, quality5 or quality9 (or default; Model M)"); @@ -1466,6 +1470,7 @@ struct Runtime { if (const char* nb = std::getenv("WG_POST_BANDS")) post_bands = std::max(1, std::atoi(nb)); if (const char* sr = std::getenv("WG_SWIN_RESIDENT")) swin_resident = std::max(0, std::atoi(sr)); if (const char* t = std::getenv("WG_POST_TF")) post_tf = !(std::string(t) == "0" || std::string(t) == "off"); + if (const char* t = std::getenv("WG_SCALE_FOLD")) scale_fold = !(std::string(t) == "0" || std::string(t) == "off"); // The output filter (spec 06 draft 4, spec 10 section 5 step 6): the original's weights from the model file by // default, the analytic set when the model file has none (noted for the log) or when it is selected. output_quality9 = output_filter_opt == "quality9"; @@ -2930,6 +2935,11 @@ struct Runtime { p.e_prev = e; p.ratio = 1.0f; DevPtr w = wdev["post"]; + // scale_fold: the staged true-fusion post (the HLSL ports of post_tf_f32_p12_diet_vsit, post_tf4_dl_*, pmo/post_tf_*_mo) + // applies the scale_rgb multiply itself when its pi argument is not 1. Never raw / direct / Model L / interop output copy. + const bool fold_scale = + scale_fold && d3d12_native && !native_direct_io && !raw && !model_l && hp <= 0 && pi != 1.0f && + (tf_mo || (tf_kernel && (tf_name == "post_tf_f32_p12_diet_vsit" || tf_name == "post_tf4_dl_f32_p12_diet_vsit"))); if (post_fused) { // reconstruction + output filter in one launch, straight into the output buffer if (!fused_supported(rw, rh, dw, dh)) return fail(post_kernel + ": render size " + std::to_string(rw) + "x" + std::to_string(rh) + @@ -2950,7 +2960,8 @@ struct Runtime { } else if (tf_mo) { // reconstruction + output filter in one kernel, motion at output resolution int ow_i = ow, oh_i = oh, hpitch = hp > 0 ? hp : 12 * ow, cp = post_tf_cpairs, k1 = 0, c2 = 0; const int nch = tf_chunks(ow, oh, cp, &k1, &c2); - float dec_scale = (float)(0.01 / (fr.exposure == 0.0f ? 1.0 : (double)fr.exposure)), p_h = hp > 0 ? pi : 1.0f; + float dec_scale = (float)(0.01 / (fr.exposure == 0.0f ? 1.0 : (double)fr.exposure)), + p_h = hp > 0 || fold_scale ? pi : 1.0f; post_last = tf_mo4 ? "post_tf4_dl_f32_p12_mo" : hp > 0 ? "post_tf_p12_mo" : "post_tf_f32_p12_mo"; if (native_direct_io) post_last += "_direct"; // typed colour input and output texture (dx12/kernels/post.hlsl) filter_last = "fused"; @@ -2964,7 +2975,7 @@ struct Runtime { &expo, &ow_i, &oh_i, &dec_scale, &p_h, &hpitch, &cp, &k1, &c2, &mop}))) return false; // spec 10 draft 2: the filter's result times pi (folded in with the copy; direct shader stores include it) - if (!native_direct_io && hp <= 0 && pi != 1.0f) { + if (!native_direct_io && hp <= 0 && pi != 1.0f && !fold_scale) { int n3 = 3 * ow * oh; float p_ = pi; if (!launch("scale_rgb", cdiv(n3, 256), 256, {&outp, &p_, &n3})) return false; @@ -2977,7 +2988,8 @@ struct Runtime { } else if (tf_kernel) { // reconstruction + output filter in one kernel (kernels/post.hip, post_tf*) int ow_i = ow, oh_i = oh, hpitch = hp > 0 ? hp : 12 * ow, cp = post_tf_cpairs, k1 = 0, c2 = 0; const int nch = tf_chunks(ow, oh, cp, &k1, &c2); - float dec_scale = (float)(0.01 / (fr.exposure == 0.0f ? 1.0 : (double)fr.exposure)), p_h = hp > 0 ? pi : 1.0f; + float dec_scale = (float)(0.01 / (fr.exposure == 0.0f ? 1.0 : (double)fr.exposure)), + p_h = hp > 0 || fold_scale ? pi : 1.0f; post_last = tf_kernel; filter_last = "fused"; #ifdef WG_DEV_DIAGNOSTICS @@ -2989,7 +3001,7 @@ struct Runtime { with_taps({&cnet, &A, &B, &C, &hist[cur], &mnear[nxt], &mcenter, &mfar, &w, &hist[nxt], &reduced, &outp, &p, &expo, &ow_i, &oh_i, &dec_scale, &p_h, &hpitch, &cp, &k1, &c2}))) return false; - if (!native_direct_io && hp <= 0 && pi != 1.0f) { // direct shader stores already include this final multiply + if (!native_direct_io && hp <= 0 && pi != 1.0f && !fold_scale) { // direct / folded stores already include this multiply int n3 = 3 * ow * oh; float p_ = pi; if (!launch("scale_rgb", cdiv(n3, 256), 256, {&outp, &p_, &n3})) return false; diff --git a/clean/tests/runtime_frame_trace.cpp b/clean/tests/runtime_frame_trace.cpp index bf28c222..295d2ad2 100644 --- a/clean/tests/runtime_frame_trace.cpp +++ b/clean/tests/runtime_frame_trace.cpp @@ -15,6 +15,7 @@ // --geo== a Swin / GEMM launch-geometry override in WgRtConfig (nt, gemm_threads, swin_threads, wpw64, // wpw96, wpw128, wpg0 .. wpg4 = block_wpg[k]); repeatable // --hdr=<0|1> WgRtConfig.hdr (default 0) +// --pre-exposure= WgRtFrame.pre_exposure of every frame (default 1) // --guard-timeout= the frame guard (Device::guard_begin, the hybrid bottleneck's wait timeout): in that frame // the bottleneck "times out" (at the guard's second reading of that frame, the words a timed-out wait // writes are set first: sync words 12 and 15 + 1). The device models the guard: "guard_begin @@ -357,6 +358,7 @@ int main(int argc, char** argv) { std::string options, map_path, fail; bool fail_hyb = false; int guard_timeout = -1; + float pre_exposure = 1.0f; std::vector> sizes; std::set resets; for (int a = 8; a < argc; ++a) { @@ -377,6 +379,8 @@ int main(int argc, char** argv) { for (const std::string& f : split(s.substr(8), ',')) resets.insert(std::atoi(f.c_str())); } else if (s.compare(0, 6, "--hdr=") == 0) { cfg.hdr = std::atoi(s.c_str() + 6); + } else if (s.compare(0, 15, "--pre-exposure=") == 0) { + pre_exposure = (float)std::atof(s.c_str() + 15); } else if (s.compare(0, 7, "--fail=") == 0) { fail = s.substr(7); } else if (s == "--fail-hyb-tables") { @@ -432,7 +436,7 @@ int main(int argc, char** argv) { fr.rh = rh; fr.mv_scale[0] = fr.mv_scale[1] = 1.0f; fr.exposure = 1.0f; - fr.pre_exposure = 1.0f; + fr.pre_exposure = pre_exposure; fr.reset = resets.count(f) ? 1 : 0; const bool ok = runtime_frame(rt, fr, nullptr); WgRtFrameInfo info = WgRtFrameInfo(); diff --git a/clean/tests/test_runtime_frame_trace.py b/clean/tests/test_runtime_frame_trace.py index 34db0858..4174e1af 100644 --- a/clean/tests/test_runtime_frame_trace.py +++ b/clean/tests/test_runtime_frame_trace.py @@ -261,5 +261,57 @@ def test_model_l_refused(self): self.assertTrue(lines[0].startswith("ERR create: ") and "Model L" in lines[0], lines[0]) +class NativeScaleFold(unittest.TestCase): + """Native D3D12 (backend 3, the stand-in), staged buffers, pre-exposure != 1: the output scale (scale_rgb) is done by the + true-fusion post kernel's store (its pi argument), not launched; the colour is still divided by color_divide. + WG_SCALE_FOLD=0 brings the scale_rgb launch back (A/B, same bytes).""" + + COS = ("dec1_dec0__pack12.co", "post_w64.co", "post_w64__orig.co", "swin.co", "encoder.co", "exposure.co", "frame.co", + "post.co") + + def frames(self, pre_exposure, env=None): + build = tempfile.mkdtemp(prefix="wg_ftb_") + try: + os.makedirs(os.path.join(build, "dx12")) + with open(os.path.join(build, "dx12", "manifest.json"), "w") as f: + f.write('{"kernels": [%s]}' % ", ".join('{"name": "x", "co": "%s"}' % c for c in self.COS)) + e = {k: v for k, v in os.environ.items() if not k.upper().startswith("WG_")} + e.update(env or {}) + r = subprocess.run([trace_exe(), build, model_dir(), "64", "64", "96", "96", "2", "", "--backend=3", + "--pre-exposure=%s" % pre_exposure], capture_output=True, text=True, env=e, timeout=600) + finally: + shutil.rmtree(build, ignore_errors=True) + self.assertEqual(r.returncode, 0, r.stdout + r.stderr) + t = Trace(r.stdout.splitlines()) + self.assertEqual(len(t.frames), 2, r.stdout) + for f in t.frames: + self.assertIsNone(f["error"]) + return t.frames + + @staticmethod + def names(frame): + return [ln.kernel for ln in frame["launches"]] + + def test_folded_by_default(self): + for fr in self.frames("2"): + names = self.names(fr) + self.assertIn("color_divide", names) + self.assertNotIn("scale_rgb", names) + self.assertEqual(names[-1], "w64/post_tf_f32_p12_diet_vsit") + + def test_pre_exposure_one_changes_nothing(self): + for fr in self.frames("1"): + names = self.names(fr) + self.assertNotIn("color_divide", names) + self.assertNotIn("scale_rgb", names) + self.assertEqual(names[-1], "w64/post_tf_f32_p12_diet_vsit") + + def test_switch_brings_the_launch_back(self): + for fr in self.frames("2", {"WG_SCALE_FOLD": "0"}): # scale_rgb after the post + names = self.names(fr) + self.assertIn("color_divide", names) + self.assertEqual(names[-2:], ["w64/post_tf_f32_p12_diet_vsit", "scale_rgb"]) + + if __name__ == "__main__": unittest.main() From 7b0f6a58a66b24322b3d45a998a97213642f605e Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Tue, 24 Nov 2026 19:16:02 -0500 Subject: [PATCH 3/5] Model M (D3D12): Wave 1 bit-exact perf levers (T1.1/T1.2/T1.3/T1.5/T1.8) Implement five bit-exact Model M D3D12 optimizations for gfx1201: - T1.1: hoist scalar alignment mask in post_tf history_cr_span (4 -> 0 v_and_b32 -8) and drop WG_LD_A16 per-VGPR masking on swin_block_c64_nc (17 -> 0 v_and_b32 -16) and dec1_dec0 (11 -> 0) while preserving all buffer_load_b128. - T1.2: port all 7 POST_OUT==3 bit-exact optimizations to POST_OUT==0 (pd_stage_fetch hoisting, unrolled gather + gather_zero_opq, 2-chain rh_code, 2x QuadReadAcrossX display LDS store + k==0 hsum skip, hoisted h_src/h_dst + pd_vsplit, WG_PT_SMALL/WG_PT_INF, and pi==1 vs pi!=1 body specialization). - T1.3: add 256 KB FP16 decode LUT (f16lut_build_dec) for staged POST_OUT==0 (output_filter=original), overlapped with input_encoder via unordered_next(), toggleable with WG_M_POST_LUT=0. - T1.5: drop input_encoder from 103 -> 95 VGPRs (0 scratch, 10 KB lane-private LDS -> 4 WGs/CU) via A-fragment streaming and per-lane LDS parking, plus WaveActiveAnyTrue(ec > 32752.0f) guard on the second wg_log2. - T1.8: overlap color_divide with exposure_auto_diet via unordered_next() when pi != 1, and add dedicated _pf variants (input_encoder_pf, emo/input_encoder_mo_pf, post_tf*_pf) folding color_divide with zero impact on pi == 1 DXILs, toggleable with WG_M_PF_FOLD=0. --- clean/dx12/hlsl/wg_f16lut.hlsli | 27 +- clean/dx12/hlsl/wg_pidiv.hlsli | 64 ++++ clean/dx12/kernel_sets.json | 460 +++++++++++++++++++++++++++ clean/dx12/kernels/dec1_dec0.hlsl | 2 +- clean/dx12/kernels/encoder.hlsl | 112 +++++-- clean/dx12/kernels/exposure.hlsl | 22 ++ clean/dx12/kernels/post.hlsl | 134 ++++++-- clean/dx12/kernels/swin_c64w2.hlsl | 2 +- clean/runtime/runtime.cpp | 140 ++++---- clean/tests/test_post_tf_align.py | 4 +- clean/tests/test_post_tf_geometry.py | 3 +- 11 files changed, 852 insertions(+), 118 deletions(-) create mode 100644 clean/dx12/hlsl/wg_pidiv.hlsli diff --git a/clean/dx12/hlsl/wg_f16lut.hlsli b/clean/dx12/hlsl/wg_f16lut.hlsli index 6a40372b..6236d3cd 100644 --- a/clean/dx12/hlsl/wg_f16lut.hlsli +++ b/clean/dx12/hlsl/wg_f16lut.hlsli @@ -41,6 +41,31 @@ float f16lut_poly(float qc) return (t * y); } +// Exact scale with normal-result underflow repair (same bits as native_direct_scale / m1_scale). +#if !defined(WG_DIRECT_TEXTURES) +float f16lut_scale_ref(float a, float b) +{ + const float r = a * b; + const uint ua = asuint(a), ub = asuint(b); + const uint ea = (ua >> 23u) & 255u, eb = (ub >> 23u) & 255u; + if ((asuint(r) & 0x7fffffffu) == 0u && ea != 0u && eb != 0u && ea + eb == 127u) { + const uint64_t product = (uint64_t)((ua & 0x7fffffu) | 0x800000u) * (uint64_t)((ub & 0x7fffffu) | 0x800000u); + const uint64_t normal = (uint64_t)1u << 47u; + if (product >= normal - ((uint64_t)1u << 23u) && product < normal) + return asfloat(((ua ^ ub) & 0x80000000u) | 0x00800000u); + } + return r; +} +float f16lut_scale(float a, float b) +{ + float r = a * b; + [branch] if (and(abs(r) == 0.0f, abs(a) >= asfloat(0x00800000u))) r = f16lut_scale_ref(a, b); + return r; +} +#else +float f16lut_scale(float a, float b) { return native_direct_scale(a, b); } +#endif + // The output value of FP16 bits r (post.hlsl's vertical pass after lz5): z is an opaque zero (mul_nz's addend -0 the // driver cannot drop). For finite dec_scale and pi no value is NaN (v is finite and positive), so the software RNE of // the clamped value (wg_f2h) has the bits of the post's driver conversion (v_cvt_f16_f32). @@ -50,7 +75,7 @@ float f16lut_out(uint r, float dec_scale, float pi, bool rne16, uint z) float v = exp2(q * kF16lutDecK) - 1.0f; v = q < kF16lutPolyMax ? f16lut_poly(q) : v; float x = mad(v, dec_scale, asfloat(z | 0x80000000u)); - if (pi != 1.0f) x = native_direct_scale(x, pi); + if (pi != 1.0f) x = f16lut_scale(x, pi); if (rne16) { const float c = select(x > 65504.0f, 65504.0f, select(x < -65504.0f, -65504.0f, x)); x = f16tof32(wg_f2h(c)); diff --git a/clean/dx12/hlsl/wg_pidiv.hlsli b/clean/dx12/hlsl/wg_pidiv.hlsli new file mode 100644 index 00000000..458b8d77 --- /dev/null +++ b/clean/dx12/hlsl/wg_pidiv.hlsli @@ -0,0 +1,64 @@ +// SPDX-License-Identifier: MIT +// The pre-exposure division of the staged native D3D12 route, done by the dedicated _pf kernels (input_encoder_pf, +// emo/input_encoder_mo_pf, and the post_tf _pf buffer forms) when pi != 1 instead of a separate color_divide launch. +// Include after wg_platform.hlsli. +// +// Per channel the value is native_color_div (native_helpers.hlsl: wg_div plus the exact repair of the one tiny quotient +// that rounds to the smallest normal): the staged route's color_divide bits. Like native_direct.hlsli's +// native_direct_color_div3 (the direct route's per-frame form, same proof: clean/runtime/native_direct_pi.h) three +// operations on the lanes whose |a| lies in the window [lo, hi) (or is zero / an FP32 denormal), the reference division on +// the others. The window is native_direct_pi_window(pi) of the host, computed here from the pre-exposure's bits (the host +// folds the division only for a pi whose window is valid: hi != 0): y = RN(1 / pi) is the kernel's own correctly rounded +// quotient, lo = 2^max(-79, B - 125), hi = 2^(B + 127) (+inf for B > 0), B the unbiased exponent of pi. Alpha is not touched +// by the caller (color_divide copies it as raw bits). +#pragma once + +// native_color_div (native_helpers.hlsl) +float pidiv_ref(float a, float b) +{ + const float r = wg_div(a, b); + const uint ua = asuint(a), ub = asuint(b); + const uint ea = (ua >> 23u) & 255u, eb = (ub >> 23u) & 255u; + if ((asuint(r) & 0x7fffffffu) == 0u && ea != 0u && eb != 255u && eb == ea + 127u && + (ua & 0x7fffffu) == 0x7fffffu && (ub & 0x7fffffu) == 0u) + return asfloat(((ua ^ ub) & 0x80000000u) | 0x00800000u); + return r; +} + +struct PiDiv { float b, y, lo, hi; }; // pi, RN(1 / pi), the window's bounds (|a| in [lo, hi) takes the fast form) + +PiDiv pidiv_make(uint bits) +{ + PiDiv d; + d.b = asfloat(bits); + d.y = wg_div(1.0f, d.b); + const uint eb = (bits >> 23u) & 255u; + const bool ok = and(bits < 0x80000000u, and(eb >= 1u, eb <= 252u)); + const int B = (int)eb - 127; + const int L = max(B - 125, -79); + d.lo = asfloat((uint)(L + 127) << 23u); + d.hi = ok ? (B > 0 ? asfloat(0x7f800000u) : asfloat((uint)(B + 254) << 23u)) : 0.0f; + return d; +} + +// c / pi per channel. z: a zero the compiler cannot see (the loop count, so that the three exact-fallback copies stay one +// rolled loop; the caller xors the result with it to keep the removed store's rounding boundary) +float3 pidiv3(float3 c, PiDiv d, uint z) +{ + const float3 q0 = c * d.y; + const float3 rn = mad(d.b, q0, -c); + float3 q = mad(-rn, d.y, q0); + const float3 m = abs(c); + const bool3 fast = and(m < d.hi, or(m >= d.lo, m < asfloat(0x00800000u))); + [branch] if (!all(fast)) { + float3 a = c; + uint3 f = uint3(fast); + [loop] for (uint i = 0u; i < 3u + z; ++i) { // channel i in .x; rotated after each + const float r = f.x != 0u ? q.x : pidiv_ref(a.x, d.b); + q = float3(q.y, q.z, r); + a = float3(a.y, a.z, a.x); + f = uint3(f.y, f.z, f.x); + } + } + return q; +} diff --git a/clean/dx12/kernel_sets.json b/clean/dx12/kernel_sets.json index b13ec118..c0e90e7b 100644 --- a/clean/dx12/kernel_sets.json +++ b/clean/dx12/kernel_sets.json @@ -36,6 +36,10 @@ "emo/input_encoder_mo_direct", "encoder_mo.co" ], + [ + "emo/input_encoder_mo_pf", + "encoder_mo.co" + ], [ "expand_c96", "swin.co" @@ -56,6 +60,10 @@ "exposure_pair_diet_direct", "exposure.co" ], + [ + "f16lut_build_dec", + "-" + ], [ "f16lut_build_direct", "-" @@ -68,6 +76,10 @@ "input_encoder_direct", "encoder.co" ], + [ + "input_encoder_pf", + "encoder.co" + ], [ "native_driver_probe", "-" @@ -92,6 +104,10 @@ "pmo/post_tf4_dl_f32_p12_mo_direct", "post_mo_w64__orig.co" ], + [ + "pmo/post_tf4_dl_f32_p12_mo_pf", + "post_mo_w64__orig.co" + ], [ "pmo/post_tf_f32_p12_mo", "post_mo_w64__orig.co" @@ -100,6 +116,10 @@ "pmo/post_tf_f32_p12_mo_direct", "post_mo_w64__orig.co" ], + [ + "pmo/post_tf_f32_p12_mo_pf", + "post_mo_w64__orig.co" + ], [ "scale_rgb", "frame.co" @@ -164,6 +184,10 @@ "w64/post_tf4_dl_f32_p12_diet_vsit_direct", "post_w64__orig.co" ], + [ + "w64/post_tf4_dl_f32_p12_diet_vsit_pf", + "post_w64__orig.co" + ], [ "w64/post_tf_f32_p12_diet_vsit", "post_w64.co" @@ -180,6 +204,10 @@ "w64/post_tf_f32_p12_diet_vsit_direct", "post_w64__orig.co" ], + [ + "w64/post_tf_f32_p12_diet_vsit_pf", + "post_w64__orig.co" + ], [ "w64/post_tf_p12_diet_vsit", "post_w64__orig.co" @@ -190,14 +218,94 @@ ] ], "model_l": [ + [ + "bneck/swin_bneck6_mb_nc", + "swin__bmegahg.co" + ], + [ + "bneck/swin_bneck6_mbo_nc", + "swin__bmegahg.co" + ], + [ + "bneck/swin_bneck6_nc", + "swin__bmegahg.co" + ], + [ + "color_divide", + "frame.co" + ], + [ + "d1p12/dec0_q7", + "dec1_dec0__pack12.co" + ], + [ + "d1p12/swin_block_c64_add_dec0_nc", + "dec1_dec0__pack12.co" + ], + [ + "d1p12/swin_block_c64_add_dec0_xd_nc", + "dec1_dec0__pack12.co" + ], + [ + "emo/input_encoder_mo", + "encoder_mo.co" + ], + [ + "emo/input_encoder_mo_direct", + "encoder_mo.co" + ], + [ + "emo/input_encoder_mo_pf", + "encoder_mo.co" + ], [ "expand_c128_hyb", "-" ], + [ + "expand_c96", + "swin.co" + ], [ "expand_c96_hyb", "-" ], + [ + "exposure_auto_diet", + "exposure.co" + ], + [ + "exposure_auto_diet_direct", + "exposure.co" + ], + [ + "exposure_pair_diet", + "exposure.co" + ], + [ + "exposure_pair_diet_direct", + "exposure.co" + ], + [ + "f16lut_build_dec", + "-" + ], + [ + "f16lut_build_direct", + "-" + ], + [ + "input_encoder", + "encoder.co" + ], + [ + "input_encoder_direct", + "encoder.co" + ], + [ + "input_encoder_pf", + "encoder.co" + ], [ "l_coef", "l_ops.co" @@ -370,14 +478,58 @@ "lev/input_encoder_q20", "-" ], + [ + "native_driver_probe", + "-" + ], + [ + "native_fill", + "-" + ], + [ + "patch_merge_c64", + "swin.co" + ], [ "patch_merge_c64_hyb", "-" ], + [ + "patch_merge_c96", + "swin.co" + ], [ "patch_merge_c96_hyb", "-" ], + [ + "pmo/post_tf4_dl_f32_p12_mo", + "post_mo_w64__orig.co" + ], + [ + "pmo/post_tf4_dl_f32_p12_mo_direct", + "post_mo_w64__orig.co" + ], + [ + "pmo/post_tf4_dl_f32_p12_mo_pf", + "post_mo_w64__orig.co" + ], + [ + "pmo/post_tf_f32_p12_mo", + "post_mo_w64__orig.co" + ], + [ + "pmo/post_tf_f32_p12_mo_direct", + "post_mo_w64__orig.co" + ], + [ + "pmo/post_tf_f32_p12_mo_pf", + "post_mo_w64__orig.co" + ], + [ + "scale_rgb", + "frame.co" + ], [ "swin_block_c128_hyb", "-" @@ -394,20 +546,192 @@ "swin_block_c64_hyb", "-" ], + [ + "swin_block_c64_nc", + "swin.co" + ], + [ + "swin_block_c96_add_ex_nc", + "swin.co" + ], [ "swin_block_c96_add_hyb", "-" ], + [ + "swin_block_c96_add_xe_nc", + "swin.co" + ], + [ + "swin_block_c96_add_xe_rev_nc", + "swin.co" + ], + [ + "swin_block_c96_add_xe_xd_nc", + "swin.co" + ], + [ + "swin_block_c96_add_xe_xo_nc", + "swin.co" + ], [ "swin_block_c96_hyb", "-" ], + [ + "swin_block_c96_nc", + "swin.co" + ], + [ + "swin_block_c96_pm_nc", + "swin.co" + ], + [ + "swin_block_c96_pm_rev_nc", + "swin.co" + ], [ "swin_bneck6_hyb", "-" + ], + [ + "timing_barrier", + "frame.co" + ], + [ + "w64/post_filter_c1_s4", + "post_w64.co" + ], + [ + "w64/post_tf4_dl_f32_p12_diet_vsit", + "post_w64.co" + ], + [ + "w64/post_tf4_dl_f32_p12_diet_vsit", + "post_w64__orig.co" + ], + [ + "w64/post_tf4_dl_f32_p12_diet_vsit_direct", + "post_w64.co" + ], + [ + "w64/post_tf4_dl_f32_p12_diet_vsit_direct", + "post_w64__orig.co" + ], + [ + "w64/post_tf4_dl_f32_p12_diet_vsit_pf", + "post_w64__orig.co" + ], + [ + "w64/post_tf_f32_p12_diet_vsit", + "post_w64.co" + ], + [ + "w64/post_tf_f32_p12_diet_vsit", + "post_w64__orig.co" + ], + [ + "w64/post_tf_f32_p12_diet_vsit_direct", + "post_w64.co" + ], + [ + "w64/post_tf_f32_p12_diet_vsit_direct", + "post_w64__orig.co" + ], + [ + "w64/post_tf_f32_p12_diet_vsit_pf", + "post_w64__orig.co" + ], + [ + "w64/post_tf_p12_diet_vsit", + "post_w64__orig.co" + ], + [ + "w64/post_tf_p12_diet_vsit_tex", + "post_w64__orig.co" ] ], "model_k": [ + [ + "bneck/swin_bneck6_mb_nc", + "swin__bmegahg.co" + ], + [ + "bneck/swin_bneck6_mbo_nc", + "swin__bmegahg.co" + ], + [ + "bneck/swin_bneck6_nc", + "swin__bmegahg.co" + ], + [ + "color_divide", + "frame.co" + ], + [ + "d1p12/dec0_q7", + "dec1_dec0__pack12.co" + ], + [ + "d1p12/swin_block_c64_add_dec0_nc", + "dec1_dec0__pack12.co" + ], + [ + "d1p12/swin_block_c64_add_dec0_xd_nc", + "dec1_dec0__pack12.co" + ], + [ + "emo/input_encoder_mo", + "encoder_mo.co" + ], + [ + "emo/input_encoder_mo_direct", + "encoder_mo.co" + ], + [ + "emo/input_encoder_mo_pf", + "encoder_mo.co" + ], + [ + "expand_c96", + "swin.co" + ], + [ + "exposure_auto_diet", + "exposure.co" + ], + [ + "exposure_auto_diet_direct", + "exposure.co" + ], + [ + "exposure_pair_diet", + "exposure.co" + ], + [ + "exposure_pair_diet_direct", + "exposure.co" + ], + [ + "f16lut_build_dec", + "-" + ], + [ + "f16lut_build_direct", + "-" + ], + [ + "input_encoder", + "encoder.co" + ], + [ + "input_encoder_direct", + "encoder.co" + ], + [ + "input_encoder_pf", + "encoder.co" + ], [ "k_expo_scalars", "k_io.co" @@ -651,6 +975,142 @@ [ "kxs/k_in_tiled_uniform", "k_io_exact.co" + ], + [ + "native_driver_probe", + "-" + ], + [ + "native_fill", + "-" + ], + [ + "patch_merge_c64", + "swin.co" + ], + [ + "patch_merge_c96", + "swin.co" + ], + [ + "pmo/post_tf4_dl_f32_p12_mo", + "post_mo_w64__orig.co" + ], + [ + "pmo/post_tf4_dl_f32_p12_mo_direct", + "post_mo_w64__orig.co" + ], + [ + "pmo/post_tf4_dl_f32_p12_mo_pf", + "post_mo_w64__orig.co" + ], + [ + "pmo/post_tf_f32_p12_mo", + "post_mo_w64__orig.co" + ], + [ + "pmo/post_tf_f32_p12_mo_direct", + "post_mo_w64__orig.co" + ], + [ + "pmo/post_tf_f32_p12_mo_pf", + "post_mo_w64__orig.co" + ], + [ + "scale_rgb", + "frame.co" + ], + [ + "swin_block_c64_nc", + "swin.co" + ], + [ + "swin_block_c96_add_ex_nc", + "swin.co" + ], + [ + "swin_block_c96_add_xe_nc", + "swin.co" + ], + [ + "swin_block_c96_add_xe_rev_nc", + "swin.co" + ], + [ + "swin_block_c96_add_xe_xd_nc", + "swin.co" + ], + [ + "swin_block_c96_add_xe_xo_nc", + "swin.co" + ], + [ + "swin_block_c96_nc", + "swin.co" + ], + [ + "swin_block_c96_pm_nc", + "swin.co" + ], + [ + "swin_block_c96_pm_rev_nc", + "swin.co" + ], + [ + "timing_barrier", + "frame.co" + ], + [ + "w64/post_filter_c1_s4", + "post_w64.co" + ], + [ + "w64/post_tf4_dl_f32_p12_diet_vsit", + "post_w64.co" + ], + [ + "w64/post_tf4_dl_f32_p12_diet_vsit", + "post_w64__orig.co" + ], + [ + "w64/post_tf4_dl_f32_p12_diet_vsit_direct", + "post_w64.co" + ], + [ + "w64/post_tf4_dl_f32_p12_diet_vsit_direct", + "post_w64__orig.co" + ], + [ + "w64/post_tf4_dl_f32_p12_diet_vsit_pf", + "post_w64__orig.co" + ], + [ + "w64/post_tf_f32_p12_diet_vsit", + "post_w64.co" + ], + [ + "w64/post_tf_f32_p12_diet_vsit", + "post_w64__orig.co" + ], + [ + "w64/post_tf_f32_p12_diet_vsit_direct", + "post_w64.co" + ], + [ + "w64/post_tf_f32_p12_diet_vsit_direct", + "post_w64__orig.co" + ], + [ + "w64/post_tf_f32_p12_diet_vsit_pf", + "post_w64__orig.co" + ], + [ + "w64/post_tf_p12_diet_vsit", + "post_w64__orig.co" + ], + [ + "w64/post_tf_p12_diet_vsit_tex", + "post_w64__orig.co" ] ] } diff --git a/clean/dx12/kernels/dec1_dec0.hlsl b/clean/dx12/kernels/dec1_dec0.hlsl index e08e1709..909782b7 100644 --- a/clean/dx12/kernels/dec1_dec0.hlsl +++ b/clean/dx12/kernels/dec1_dec0.hlsl @@ -21,7 +21,7 @@ // both pixels of every lane and only the stores are conditional). dec0's FP16 latent update is done in FP32 with an FP16 // rounding after each step, not in half arithmetic (the driver contracts half mul + add into v_fma_mix_f32). // Bit-identical to the HIP kernel on the synthetic frames (clean/dx12/compare.py, both cases, every buffer). -// @wg-kernel d1p12/swin_block_c64_add_dec0_nc co=dec1_dec0__pack12.co block=256 wave=32 args=p,p,p,u,u,u,u,p,p,p,p,p,p,p,b56 defines=WG_GELU_T5=0,WG_LD_A16=1 +// @wg-kernel d1p12/swin_block_c64_add_dec0_nc co=dec1_dec0__pack12.co block=256 wave=32 args=p,p,p,u,u,u,u,p,p,p,p,p,p,p,b56 defines=WG_GELU_T5=0 // // DEC0_ONLY (port d1p12/dec0_q7; Model M on the FP16-form shared stages, WG_M_FORM=q7, where dec1 runs as the q7 block // l_q7_block_c64_dec and stores its map): the same kernel without the block. The epilogue's input o[0..3] (lane (t, hh): diff --git a/clean/dx12/kernels/encoder.hlsl b/clean/dx12/kernels/encoder.hlsl index a5e0fd18..2a8492d1 100644 --- a/clean/dx12/kernels/encoder.hlsl +++ b/clean/dx12/kernels/encoder.hlsl @@ -16,6 +16,7 @@ // projection into "tok < ntok" (its only effect is that store); here every lane runs it (the WMMA needs the whole wave) // and only the store is conditional: the same bytes. // @wg-kernel input_encoder co=encoder.co block=256 wave=32 args=p,p,p,p,p,p,p,p,p,p,p,p,b116 +// @wg-kernel input_encoder_pf co=encoder.co entry=input_encoder block=256 wave=32 args=p,p,p,p,p,p,p,p,p,p,p,p,b116 defines=WG_STAGED_PIDIV=1 // @wg-kernel input_encoder_direct co=encoder.co entry=input_encoder block=256 wave=32 args=p,p,p,p,p,p,p,p,p,p,p,p,b116 rs=direct defines=WG_DIRECT_TEXTURES=1 // // Motion at output resolution (WG_ENC_MO; HIP: input_encoder_mo of kernels/encoder_mo.hip, code object encoder_mo.co @@ -25,6 +26,7 @@ // kernel without depth, then WgEncMoParams (mw, mh, pitch, wide, kmx, kmy) after WgEncParams. Buffer form: the raw // motion buffer (wg_motion_hi.hlsli); direct form: the game's motion texture (its `motion` pointer is not read). // @wg-kernel emo/input_encoder_mo co=encoder_mo.co entry=input_encoder block=256 wave=32 args=p,p,p,p,p,p,p,p,p,p,p,b116,b24 defines=WG_ENC_MO=1 +// @wg-kernel emo/input_encoder_mo_pf co=encoder_mo.co entry=input_encoder block=256 wave=32 args=p,p,p,p,p,p,p,p,p,p,p,b116,b24 defines=WG_ENC_MO=1,WG_STAGED_PIDIV=1 // @wg-kernel emo/input_encoder_mo_direct co=encoder_mo.co entry=input_encoder block=256 wave=32 args=p,p,p,p,p,p,p,p,p,p,p,b116,b24 rs=direct defines=WG_DIRECT_TEXTURES=1,WG_ENC_MO=1 // // Model L's value kernel (WG_ENC_VALUES_OUT; HIP: encoder.hip built with WG_ENC_VALUES_OUT=1 as l_enc_values.co, issue @@ -65,6 +67,9 @@ #if defined(WG_ENC_MO) #include "wg_motion_hi.hlsli" #endif +#if defined(WG_STAGED_PIDIV) +#include "wg_pidiv.hlsli" +#endif WG_ARGS_BEGIN #if defined(WG_ENC_MO) @@ -182,7 +187,10 @@ void pixel_values(int mode, Uni U, int x, int y, float4 c, float2 mn, float2 mf, [unroll] for (uint ch = 0; ch < 3; ++ch) { float ec = U.e * c[ch]; float r = kG * wg_log2(wg_fmax(mad(100.0f, wg_fmin(ec, 32752.0f), 1.0f), k1em4)); - float g = ec > 32752.0f ? kG * wg_log2(mad(100.0f, ec, 1.0f)) : r; + float g = r; + [branch] if (WaveActiveAnyTrue(ec > 32752.0f)) { + g = ec > 32752.0f ? kG * wg_log2(mad(100.0f, ec, 1.0f)) : r; + } bool nan = ec != ec; v[ch] = nan ? 0.0f : r; gc[ch] = nan ? 0.0f : g; @@ -318,6 +326,12 @@ float2 enc_mo_raw(int x, uint ty) } #endif +#if !WG_ENC_VALUES_OUT +// Per-lane private scratch in LDS (10 KB for 256 lanes, bank-conflict-free) so slot 1 inputs and slot 0 outputs +// do not stay live in VGPRs across pixel_values, keeping input_encoder <= 96 VGPRs (4 WGs/CU on gfx1201, 0 scratch). +groupshared uint enc_lds[10 * WG_BLOCK]; +#endif + void encoder_main() { uint tid = wg_lane_id(); @@ -516,24 +530,67 @@ void encoder_main() } // Slot 0, then slot 1: the 20 values -> 5 words of FP8, values 8..15 swapped with the partner lane (lane ^ 16) +#if defined(WG_STAGED_PIDIV) + const PiDiv pd = pidiv_make((uint)P_pad0); +#endif uint q[2][5], x8[2][2]; +#if !WG_ENC_VALUES_OUT + // Park slot 1's inputs in per-lane LDS during slot 0's pixel_values (no barrier needed: lane-private) + enc_lds[0u * WG_BLOCK + tid] = asuint(cs[1].x); + enc_lds[1u * WG_BLOCK + tid] = asuint(cs[1].y); + enc_lds[2u * WG_BLOCK + tid] = asuint(cs[1].z); + enc_lds[3u * WG_BLOCK + tid] = asuint(mn[1].x); + enc_lds[4u * WG_BLOCK + tid] = asuint(mn[1].y); + enc_lds[5u * WG_BLOCK + tid] = asuint(mf[1].x); + enc_lds[6u * WG_BLOCK + tid] = asuint(mf[1].y); + enc_lds[7u * WG_BLOCK + tid] = asuint(mc[1].x); + enc_lds[8u * WG_BLOCK + tid] = asuint(mc[1].y); + enc_lds[9u * WG_BLOCK + tid] = xs[1]; +#endif [unroll] for (uint dx2 = 0; dx2 < 2; ++dx2) { +#if !WG_ENC_VALUES_OUT + float4 c; + float2 mn_c, mf_c, mc_c; + uint x_c; + if (dx2 == 0u) { + c = cs[0]; mn_c = mn[0]; mf_c = mf[0]; mc_c = mc[0]; x_c = xs[0]; + } else { + c = float4(asfloat(enc_lds[0u * WG_BLOCK + tid]), asfloat(enc_lds[1u * WG_BLOCK + tid]), + asfloat(enc_lds[2u * WG_BLOCK + tid]), 0.0f); + mn_c = float2(asfloat(enc_lds[3u * WG_BLOCK + tid]), asfloat(enc_lds[4u * WG_BLOCK + tid])); + mf_c = float2(asfloat(enc_lds[5u * WG_BLOCK + tid]), asfloat(enc_lds[6u * WG_BLOCK + tid])); + mc_c = float2(asfloat(enc_lds[7u * WG_BLOCK + tid]), asfloat(enc_lds[8u * WG_BLOCK + tid])); + x_c = enc_lds[9u * WG_BLOCK + tid]; + // Park slot 0's quantized outputs in LDS while slot 1 runs pixel_values + enc_lds[0u * WG_BLOCK + tid] = q[0][0]; + enc_lds[1u * WG_BLOCK + tid] = q[0][1]; + enc_lds[2u * WG_BLOCK + tid] = q[0][4]; + enc_lds[3u * WG_BLOCK + tid] = x8[0][0]; + enc_lds[4u * WG_BLOCK + tid] = x8[0][1]; + } +#else float4 c = cs[dx2]; + float2 mn_c = mn[dx2], mf_c = mf[dx2], mc_c = mc[dx2]; + uint x_c = xs[dx2]; +#endif +#if defined(WG_STAGED_PIDIV) + c = float4(asfloat(asuint(pidiv3(c.xyz, pd, (uint)P_rw >> 31u)) ^ ((uint)P_rw >> 31u)), c.w); +#endif float v[20]; if (U.mode == kHistGeneral) // uniform - pixel_values(kHistGeneral, U, (int)xs[dx2], y, c, mn[dx2], mf[dx2], mc[dx2], v + pixel_values(kHistGeneral, U, (int)x_c, y, c, mn_c, mf_c, mc_c, v #if defined(WG_DIRECT_TEXTURES) , lut, lt[dx2] #endif ); else if (U.mode == kHistSame) - pixel_values(kHistSame, U, (int)xs[dx2], y, c, mn[dx2], mf[dx2], mc[dx2], v + pixel_values(kHistSame, U, (int)x_c, y, c, mn_c, mf_c, mc_c, v #if defined(WG_DIRECT_TEXTURES) , lut, lt[dx2] #endif ); else - pixel_values(kHistRaw, U, (int)xs[dx2], y, c, mn[dx2], mf[dx2], mc[dx2], v + pixel_values(kHistRaw, U, (int)x_c, y, c, mn_c, mf_c, mc_c, v #if defined(WG_DIRECT_TEXTURES) , lut, lt[dx2] #endif @@ -546,6 +603,13 @@ void encoder_main() x8[dx2][1] = wg_xor16_u(qq[3]); #endif } +#if !WG_ENC_VALUES_OUT + q[0][0] = enc_lds[0u * WG_BLOCK + tid]; + q[0][1] = enc_lds[1u * WG_BLOCK + tid]; + q[0][4] = enc_lds[2u * WG_BLOCK + tid]; + x8[0][0] = enc_lds[3u * WG_BLOCK + tid]; + x8[0][1] = enc_lds[4u * WG_BLOCK + tid]; +#endif #if WG_ENC_VALUES_OUT // pixel (X0 + dx, Y) of the halo map: 20 value bytes and 12 zero bytes @@ -574,34 +638,46 @@ void encoder_main() bf[2 * p + 1] = uint2(own ? q[d][4] : 0u, 0u); } - // Two output tiles at a time; every A fragment and bias of a pair is loaded before its first WMMA - uint o[8]; + // Two output tiles at a time; stream A fragments per p2 step to keep VGPRs <= 96 (4 WGs/CU, 0 spills) [unroll] for (uint pr = 0; pr < 2; ++pr) { v8f acc[2]; - uint2 A[2][8]; + uint2 A[2][2]; [unroll] for (uint j = 0; j < 2; ++j) { acc[j] = wg_load8f(w, kOffBias + hh * 32u + (2u * pr + j) * 64u); - [unroll] for (uint kb = 0; kb < 8; ++kb) - A[j][kb] = wg_ld64(w, kOffFrags + ((2u * pr + j) * 8u + kb) * 256u + lane * 8u); + A[j][0] = wg_ld64(w, kOffFrags + ((2u * pr + j) * 8u + 0u) * 256u + lane * 8u); + A[j][1] = wg_ld64(w, kOffFrags + ((2u * pr + j) * 8u + 1u) * 256u + lane * 8u); } [unroll] for (uint p2 = 0; p2 < 4; ++p2) { + uint2 An[2][2]; + if (p2 < 3u) { + [unroll] for (uint jn = 0; jn < 2; ++jn) { + An[jn][0] = wg_ld64(w, kOffFrags + ((2u * pr + jn) * 8u + 2u * p2 + 2u) * 256u + lane * 8u); + An[jn][1] = wg_ld64(w, kOffFrags + ((2u * pr + jn) * 8u + 2u * p2 + 3u) * 256u + lane * 8u); + } + } [unroll] for (uint j2 = 0; j2 < 2; ++j2) { - acc[j2] = wg_wmma_fp8(A[j2][2 * p2], bf[2 * p2], acc[j2]); - acc[j2] = wg_wmma_fp8(A[j2][2 * p2 + 1], bf[2 * p2 + 1], acc[j2]); + acc[j2] = wg_wmma_fp8(A[j2][0], bf[2 * p2], acc[j2]); + acc[j2] = wg_wmma_fp8(A[j2][1], bf[2 * p2 + 1], acc[j2]); acc[j2] = round_f16(acc[j2]); } + if (p2 < 3u) { + [unroll] for (uint jc = 0; jc < 2; ++jc) { + A[jc][0] = An[jc][0]; + A[jc][1] = An[jc][1]; + } + } } + uint o4[4]; [unroll] for (uint j3 = 0; j3 < 2; ++j3) { [unroll] for (uint i = 0; i < 8; ++i) acc[j3].v[i] = wg_fmin(wg_fmax(acc[j3].v[i], -6.28125f), 6.28125f); uint2 qo = wg_pack_q8_nc_ags(acc[j3]); // within +-6.28125: the bytes of wg_pack_q8 - o[4 * pr + 2 * j3] = qo.x; - o[4 * pr + 2 * j3 + 1] = qo.y; + o4[2 * j3] = qo.x; + o4[2 * j3 + 1] = qo.y; + } + if (tok < ntok) { + uint fo = (uint)tok * 64u + hh * 32u + pr * 16u; + wg_st128(features, fo, uint4(o4[0], o4[1], o4[2], o4[3])); } - } - if (tok < ntok) { - uint fo = (uint)tok * 64u + hh * 32u; - wg_st128(features, fo, uint4(o[0], o[1], o[2], o[3])); - wg_st128(features, fo + 16u, uint4(o[4], o[5], o[6], o[7])); } #endif } diff --git a/clean/dx12/kernels/exposure.hlsl b/clean/dx12/kernels/exposure.hlsl index 9cb36833..34ede02d 100644 --- a/clean/dx12/kernels/exposure.hlsl +++ b/clean/dx12/kernels/exposure.hlsl @@ -11,6 +11,7 @@ // @wg-kernel exposure_auto_diet co=exposure.co block=256 wave=32 args=p,p,p,p,p,b36,u // @wg-kernel exposure_auto_diet_direct co=exposure.co entry=exposure_auto_diet block=256 wave=32 args=p,p,p,p,p,b36,u rs=direct defines=WG_DIRECT_TEXTURES=1 // @wg-kernel f16lut_build_direct co=- block=256 wave=32 args=p,p rs=direct defines=WG_DIRECT_TEXTURES=1,F16LUT_BUILD=1 +// @wg-kernel f16lut_build_dec co=- block=256 wave=32 args=p,p,u,u defines=F16LUT_BUILD_DEC=1 #include "wg_platform.hlsli" #if defined(WG_DIRECT_TEXTURES) #include "native_direct.hlsli" @@ -33,6 +34,27 @@ WG_KERNEL(f16lut_build_direct) if (and(tab != 0u, i < 65536u)) f16lut_build(tab, i, wg_ldf(ex, 0u), wg_ldf(ex, 16u), nd_color_pi, nd_output_rne16, nd_rw >> 31u); } +#elif defined(F16LUT_BUILD_DEC) +// Staged path (POST_OUT == 0) per-frame 64K FP16 decode table (hlsl/wg_f16lut.hlsli, 256 KB): skips rebuild when the +// header already matches (dec_scale, pi_arg, 0). +#include "wg_f16lut.hlsli" +WG_ARGS_BEGIN + uint tab, ex; + float dec_scale_arg, pi_arg; +WG_ARGS_END +WG_KERNEL(f16lut_build_dec) +{ + WG_SETUP(); + const uint i = wg_block_id() * WG_BLOCK + wg_lane_id(); + if (or(tab == 0u, i >= 65536u)) return; + const float dec_scale = ex != 0u ? wg_ldf(ex, 16u) : dec_scale_arg; + if (f16lut_valid(tab, dec_scale, pi_arg, 0u)) return; + wg_mem.Store(tab + WG_F16LUT_DEC + 4u * i, asuint(f16lut_out(i, dec_scale, pi_arg, false, wg_grid_size() >> 31u))); + if (i == 0u) { + const bool finite = and((asuint(dec_scale) & 0x7f800000u) != 0x7f800000u, (asuint(pi_arg) & 0x7f800000u) != 0x7f800000u); + wg_mem.Store4(tab, finite ? uint4(WG_F16LUT_MAGIC, asuint(dec_scale), asuint(pi_arg), 0u) : uint4(0u, 0u, 0u, 0u)); + } +} #else WG_ARGS_BEGIN diff --git a/clean/dx12/kernels/post.hlsl b/clean/dx12/kernels/post.hlsl index f5e870b9..3b0992c6 100644 --- a/clean/dx12/kernels/post.hlsl +++ b/clean/dx12/kernels/post.hlsl @@ -24,6 +24,7 @@ // forms 1/2 it does not clamp to FP16 or explicitly round to half. The UAV format supplies the final format conversion. // Form 3 uses native_direct.hlsli's immutable frame b1 and texture table (rs=direct). // @wg-kernel w64/post_tf_f32_p12_diet_vsit co=post_w64__orig.co entry=post_tf_f32_p12_diet_vsit block=512 wave=64 args=p,p,p,p,p,p,p,p,p,p,p,p,b64,p,u,u,u,u,u,u,u,u,u,u,u,u,u +// @wg-kernel w64/post_tf_f32_p12_diet_vsit_pf co=post_w64__orig.co entry=post_tf_f32_p12_diet_vsit block=512 wave=64 args=p,p,p,p,p,p,p,p,p,p,p,p,b64,p,u,u,u,u,u,u,u,u,u,u,u,u,u defines=WG_STAGED_PIDIV=1 // @wg-kernel w64/post_tf_p12_diet_vsit co=post_w64__orig.co entry=post_tf_p12_diet_vsit block=512 wave=64 args=p,p,p,p,p,p,p,p,p,p,p,p,b64,p,u,u,u,u,u,u,u,u,u,u,u,u,u defines=POST_OUT=1 // @wg-kernel w64/post_tf_p12_diet_vsit_tex co=post_w64__orig.co rs=tex entry=post_tf_p12_diet_vsit block=512 wave=64 args=p,p,p,p,p,p,p,p,p,p,p,p,b64,p,u,u,u,u,u,u,u,u,u,u,u,u,u defines=POST_OUT=2 // @wg-kernel w64/post_tf_f32_p12_diet_vsit_direct co=post_w64__orig.co rs=direct entry=post_tf_f32_p12_diet_vsit_direct block=512 wave=64 args=p,p,p,p,p,p,p,p,p,p,p,p,b64,p,u,u,u,u,u,u,u,u,u,u,u,u,u defines=WG_DIRECT_TEXTURES=1,POST_OUT=3 @@ -38,6 +39,7 @@ // bx, by, idw, idh) after c2. Buffer form: the raw motion buffer (wg_motion_hi.hlsli); direct form: the game's motion // texture (its `motion` pointer is not read). // @wg-kernel pmo/post_tf_f32_p12_mo co=post_mo_w64__orig.co entry=post_tf_f32_p12_diet_vsit block=512 wave=64 args=p,p,p,p,p,p,p,p,p,p,p,b64,p,u,u,u,u,u,u,u,u,b40,u,u,u,u,u defines=WG_POST_MO=1 +// @wg-kernel pmo/post_tf_f32_p12_mo_pf co=post_mo_w64__orig.co entry=post_tf_f32_p12_diet_vsit block=512 wave=64 args=p,p,p,p,p,p,p,p,p,p,p,b64,p,u,u,u,u,u,u,u,u,b40,u,u,u,u,u defines=WG_POST_MO=1,WG_STAGED_PIDIV=1 // @wg-kernel pmo/post_tf_f32_p12_mo_direct co=post_mo_w64__orig.co rs=direct entry=post_tf_f32_p12_diet_vsit_direct block=512 wave=64 args=p,p,p,p,p,p,p,p,p,p,p,b64,p,u,u,u,u,u,u,u,u,b40,u,u,u,u,u defines=WG_DIRECT_TEXTURES=1,POST_OUT=3,WG_POST_MO=1 // // Native AA and ratios between 1.0 and 1.5 (Brief 15; D3D12 only, no HIP kernel of its own): the same body with NW = 4 @@ -47,8 +49,10 @@ // LDS); runtime.cpp tf4_supported is the host bound (the step footprint of NW = 4 fits 448). LDS: 448 x 48 + 20 x 480 = // 31104 bytes. The kernels above keep their DXIL (the defaults below). // @wg-kernel w64/post_tf4_dl_f32_p12_diet_vsit co=post_w64__orig.co entry=post_tf_f32_p12_diet_vsit block=256 wave=64 args=p,p,p,p,p,p,p,p,p,p,p,p,b64,p,u,u,u,u,u,u,u,u,u,u,u,u,u defines=WG_TF_NW=4,WG_TF_CAP=448 +// @wg-kernel w64/post_tf4_dl_f32_p12_diet_vsit_pf co=post_w64__orig.co entry=post_tf_f32_p12_diet_vsit block=256 wave=64 args=p,p,p,p,p,p,p,p,p,p,p,p,b64,p,u,u,u,u,u,u,u,u,u,u,u,u,u defines=WG_TF_NW=4,WG_TF_CAP=448,WG_STAGED_PIDIV=1 // @wg-kernel w64/post_tf4_dl_f32_p12_diet_vsit_direct co=post_w64__orig.co rs=direct entry=post_tf_f32_p12_diet_vsit_direct block=256 wave=64 args=p,p,p,p,p,p,p,p,p,p,p,p,b64,p,u,u,u,u,u,u,u,u,u,u,u,u,u defines=WG_DIRECT_TEXTURES=1,POST_OUT=3,WG_TF_NW=4,WG_TF_CAP=448 // @wg-kernel pmo/post_tf4_dl_f32_p12_mo co=post_mo_w64__orig.co entry=post_tf_f32_p12_diet_vsit block=256 wave=64 args=p,p,p,p,p,p,p,p,p,p,p,b64,p,u,u,u,u,u,u,u,u,b40,u,u,u,u,u defines=WG_POST_MO=1,WG_TF_NW=4,WG_TF_CAP=448 +// @wg-kernel pmo/post_tf4_dl_f32_p12_mo_pf co=post_mo_w64__orig.co entry=post_tf_f32_p12_diet_vsit block=256 wave=64 args=p,p,p,p,p,p,p,p,p,p,p,b64,p,u,u,u,u,u,u,u,u,b40,u,u,u,u,u defines=WG_POST_MO=1,WG_TF_NW=4,WG_TF_CAP=448,WG_STAGED_PIDIV=1 // @wg-kernel pmo/post_tf4_dl_f32_p12_mo_direct co=post_mo_w64__orig.co rs=direct entry=post_tf_f32_p12_diet_vsit_direct block=256 wave=64 args=p,p,p,p,p,p,p,p,p,p,p,b64,p,u,u,u,u,u,u,u,u,b40,u,u,u,u,u defines=WG_DIRECT_TEXTURES=1,POST_OUT=3,WG_POST_MO=1,WG_TF_NW=4,WG_TF_CAP=448 // // Analytic output filter (WG_LZ_ANALYTIC = 1; HIP: post_w64.co, post.hip built with -mwavefrontsize64 and the default @@ -68,16 +72,22 @@ #if WG_LZ_ANALYTIC && ((POST_OUT != 0 && POST_OUT != 3) || defined(WG_POST_MO)) #error "analytic output filter: forms 0 and 3 (direct) only" #endif +#ifndef POST_OUT +#define POST_OUT 0 +#endif #if defined(WG_DIRECT_TEXTURES) #include "native_direct.hlsli" #include "wg_f16lut.hlsli" +#elif POST_OUT == 0 && !WG_LZ_ANALYTIC +#include "wg_f16lut.hlsli" #endif -#ifndef POST_OUT -#define POST_OUT 0 +#if defined(WG_STAGED_PIDIV) +#include "wg_pidiv.hlsli" #endif // WG_POST_CUTS: the exact instruction cuts below (history base fold, shared horizontal min / max) in the forms whose // bytes the checks cover (0: 3 x f32 rows, 3: direct); the RGBA16F row forms 1 / 2 keep their previous code #define WG_POST_CUTS (POST_OUT == 0 || POST_OUT == 3) +#define WG_POST_OPT (POST_OUT == 0 || POST_OUT == 3) #if POST_OUT == 2 RWTexture2D wg_tex_out : register(u3); // the output texture (RGBA16F), written through a typed UAV #endif @@ -185,6 +195,9 @@ float unif(float x) { return asfloat(WaveReadLaneFirst(asuint(x))); } #if POST_OUT == 3 #define PD_P , const bool pd_rc, const bool pd_pi1 #define PD_A , pd_rc, pd_pi1 +#elif POST_OUT == 0 && defined(WG_STAGED_PIDIV) +#define PD_P , PiDiv pd_div +#define PD_A , pd_div #else #define PD_P #define PD_A @@ -193,10 +206,10 @@ float unif(float x) { return asfloat(WaveReadLaneFirst(asuint(x))); } #if WG_WAVE == 64 // wave32: wg_platform.hlsli includes it #include "wg_ags.hlsli" #endif -#if POST_OUT == 3 && !defined(WG_NO_PT_SMALL) +#if WG_POST_OPT && !defined(WG_NO_PT_SMALL) #define WG_PT_SMALL 1 // post_posttr.hlsli: Dec's polynomial flag by one minimum #endif -#if POST_OUT == 3 && !defined(WG_NO_PT_INF) +#if WG_POST_OPT && !defined(WG_NO_PT_INF) #define WG_PT_INF 1 // post_posttr.hlsli: blend's infinity flag by one maximum #endif @@ -343,8 +356,8 @@ TfRaw tf_fetch(int idx, int col, int row PD_P) return t; } -#if POST_OUT == 3 -// The direct form's staging fetch of texel i = mini(lane, ilast) of footprint rows r0 ..., columns c0 ... (ncol per +#if WG_POST_OPT +// Staging fetch of texel i = mini(lane, ilast) of footprint rows r0 ..., columns c0 ... (ncol per // row): (row, column) = (st_r, st_col) on lane < ilast + 1 (the lane's own texel, fixed per lane), else those of texel // ilast (wave-uniform); the same texel and clamps as post_body's other forms TfRaw pd_stage_fetch(int lane, int ilast, int st_r, int st_col, float inv_ncol_u, int r0, int c0, int ncol PD_P) @@ -360,7 +373,7 @@ TfRaw pd_stage_fetch(int lane, int ilast, int st_r, int st_col, float inv_ncol_u } #endif -#if WG_TF_TWO && POST_OUT != 3 +#if WG_TF_TWO && !WG_POST_OPT // The buffer forms' second staged texel of the lane: i = mini(lane + kThreads, nstage - 1), the first texel's formula TfRaw tf_stage2(int lane, int nstage, float inv_ncol, int r0, int c0, int ncol PD_P) { @@ -383,6 +396,9 @@ void tf_put(uint i, TfRaw r, float e PD_P) // reciprocal form (3 operations per channel, its own exact fallback lanes), as pd_normalize float4 c = r.c; if (pd_div_on(pd_rc, pd_pi1)) c.xyz = pd_xor(pd_div3(r.c.xyz)); +#elif POST_OUT == 0 && defined(WG_STAGED_PIDIV) + float4 c = r.c; + c.xyz = asfloat(asuint(pidiv3(r.c.xyz, pd_div, g_z)) ^ g_z); #else const float4 c = r.c; #endif @@ -489,7 +505,7 @@ float3 history_cr_span(float qx, float qy) // lshl_add + one mad_u24 (iy in [1, P_dh - 3] and pitch = 8 P_dw < 2^24, so the 24-bit product is the full one) // (the constant opaque, g_z: the driver would otherwise split it again into its parts, one add each) #if WG_POST_CUTS - uint b0 = (mul24((uint)iy, pitch) + ((uint)ix * 8u + (((hist_prev & ~7u) - 8u - pitch) ^ g_z))) & ~7u; + uint b0 = mul24((uint)iy, pitch) + (uint)ix * 8u + ((((hist_prev & ~7u) - 8u - pitch) ^ g_z) & ~7u); #else uint b0 = (hist_prev & ~7u) + (uint)(ix - 1) * 8u + mul24((uint)(iy - 1), pitch); #endif @@ -522,6 +538,9 @@ float3 fallback_rgb(int fx, int fy PD_P) const float4 c = pd_normalize(pd_raw_color(mul24((uint)fy, (uint)P_rw) + (uint)fx, fx, fy, pd_rc), pd_rc, pd_pi1); #else float4 c = wg_ld4f(color, mul24((uint)fy, (uint)P_rw * 16u) + (uint)fx * 16u); +#if POST_OUT == 0 && defined(WG_STAGED_PIDIV) + c.xyz = asfloat(asuint(pidiv3(c.xyz, pd_div, g_z)) ^ g_z); +#endif #endif return float3(enc_e(opq(g_e_prev * c.x)), enc_e(opq(g_e_prev * c.y)), enc_e(opq(g_e_prev * c.z))); } @@ -847,12 +866,16 @@ void tf_row_sync() #endif } -#if POST_OUT == 3 -// One vertical item of the direct form (post_body's step 3): output pixel (xg, y), ring column xo, its first ring row +#if WG_POST_OPT +// One vertical item (post_body's step 3): output pixel (xg, y), ring column xo, its first ring row // urel = 3 pr + 2 dy past the pass's first row pair, and the lane phase's filter weights wa = (w0, w4), wb = (w1, w3) as // FP16 bit pairs. post_body calls it with per-lane values fixed for the walk on the first pass (it = lane). void pd_vitem(int y, int xg, int xo, int urel, uint wa, uint wb, int p_next, bool interior, int s0, int r_first, bool lut, - float dec_scale PD_P) + float dec_scale +#if POST_OUT == 0 + , const bool p0_pi1 +#endif + PD_P) { if (or(y >= oh, xg >= ow)) return; uint t[5][3]; @@ -888,13 +911,23 @@ void pd_vitem(int y, int xg, int xo, int urel, uint wa, uint wb, int p_next, boo } #else const half w0 = hbox(wa), w4 = hbox(wa >> 16), w1 = hbox(wb), w3 = hbox(wb >> 16); - if (lut) { // the output values T[r] (wg_f16lut.hlsli, lev-f16lut d7149e2): the chain below, looked up + if (lut) { // the output values T[r] (wg_f16lut.hlsli): the chain below, looked up +#if POST_OUT == 3 float3 res; [unroll] for (int c = 0; c < 3; ++c) { const uint r = lz5_original(t[0][c], t[1][c], t[2][c], t[3][c], t[4][c], w0, w1, hbox(lz_w2), w3, w4); res[c] = asfloat(wg_ld32(color, WG_F16LUT_DEC + (r << 2))); } native_direct_output[uint2((uint)xg, (uint)y)] = float4(res, 1.0f); +#else + uint3 res; + [unroll] for (int c = 0; c < 3; ++c) { + const uint r = lz5_original(t[0][c], t[1][c], t[2][c], t[3][c], t[4][c], w0, w1, hbox(lz_w2), w3, w4); + res[c] = wg_ld32(B_unused, WG_F16LUT_DEC + (r << 2)); + } + const uint o = mul24((uint)y, (uint)ow * 12u) + mul24((uint)xg, 12u); + wg_mem.Store3(out_p + o, res); +#endif return; } float vo[3], qv[3]; @@ -909,6 +942,7 @@ void pd_vitem(int y, int xg, int xo, int urel, uint wa, uint wb, int p_next, boo if (WaveActiveAnyTrue(vsmall)) { // Dec's polynomial only when some lane needs it (post.hip's branches) [unroll] for (int c = 0; c < 3; ++c) vo[c] = qv[c] < kDecPolyMax ? dec_m1_poly(qv[c]) : vo[c]; } +#if POST_OUT == 3 // Match the staged FP32 output, optional scale_rgb, then Converter's typed output. opq preserves the // first pass's rounding boundary: the driver must not regroup vo * (dec_scale * nd_color_pi). const float3 result = float3(mul_nz(vo[0], dec_scale), mul_nz(vo[1], dec_scale), mul_nz(vo[2], dec_scale)); @@ -925,6 +959,16 @@ void pd_vitem(int y, int xg, int xo, int urel, uint wa, uint wb, int p_next, boo if (zr) native_direct_output[uint2((uint)xg, (uint)y)] = float4(ex, 1.0f); } } +#else + const uint o = mul24((uint)y, (uint)ow * 12u) + mul24((uint)xg, 12u); + if (p0_pi1) { + wg_mem.Store3(out_p + o, uint3(asuint(vo[0] * dec_scale), asuint(vo[1] * dec_scale), asuint(vo[2] * dec_scale))); + } else { + const float3 res = float3(mul_nz(vo[0], dec_scale), mul_nz(vo[1], dec_scale), mul_nz(vo[2], dec_scale)); + const float3 so = float3(m1_scale(res.x, pi_arg), m1_scale(res.y, pi_arg), m1_scale(res.z, pi_arg)); + wg_mem.Store3(out_p + o, uint3(asuint(so.x), asuint(so.y), asuint(so.z))); + } +#endif } // item it's (y - 2 p_next, xo, urel, wa, wb) (post_body's decomposition of an item index) @@ -952,9 +996,8 @@ void pd_vsplit(int it, out int yrel, out int xo, out int urel, out uint wa, out void post_body(const bool pd_rc, const bool pd_pi1) // the direct form's body (its kernel at the end of the file) { #elif POST_OUT == 0 -WG_KERNEL(post_tf_f32_p12_diet_vsit) +void post_body(const bool p0_pi1) // form 0's body (its kernel at the end of the file) { - WG_SETUP(); #else WG_KERNEL(post_tf_p12_diet_vsit) { @@ -962,10 +1005,13 @@ WG_KERNEL(post_tf_p12_diet_vsit) #endif if (P_dw < 4) return; g_z = (uint)ow >> 31; // 0 (ow > 0), unknown to the compiler +#if POST_OUT == 0 && defined(WG_STAGED_PIDIV) + const PiDiv pd_div = pidiv_make((uint)P_pad0); +#endif g_e = P_e; g_e_prev = P_e_prev; g_ratio = P_ratio; -#if POST_OUT != 3 +#if !WG_POST_OPT float dec_scale = dec_scale_arg; if (expo != 0u) { // kDiet: ratio and dec_scale precomputed in the exposure pair float e0 = unif(wg_ldf(expo, 0u)), e1 = unif(wg_ldf(expo, 4u)); @@ -1010,8 +1056,8 @@ WG_KERNEL(post_tf_p12_diet_vsit) nstage = ncol * nrow < kTfCap ? ncol * nrow : kTfCap; } TfRaw raw; -#if POST_OUT == 3 - // Direct form: texel i = mini(lane, nstage - 1) as below, but its footprint row and clamped column are fixed per lane +#if WG_POST_OPT + // texel i = mini(lane, nstage - 1) as below, but its footprint row and clamped column are fixed per lane // for lane < nstage (opaque: computed once) and wave-uniform for the other lanes (texel nstage - 1: scalar ALU) const float inv_ncol_u = unif(inv_ncol); const int st_r = (int)(((float)lane + 0.5f) * inv_ncol); @@ -1035,8 +1081,8 @@ WG_KERNEL(post_tf_p12_diet_vsit) TfRaw raw2 = tf_stage2(lane, nstage, inv_ncol, r0, c0, ncol PD_A); #endif #endif -#if POST_OUT == 3 - // Direct form: the per-launch values after the first staging loads (lev-f16lut 1a9daed: with the exposure-pair loads +#if WG_POST_OPT + // per-launch values after the first staging loads (lev-f16lut 1a9daed: with the exposure-pair loads // first, every wave waited for them before issuing any texel load) float dec_scale = dec_scale_arg; if (expo != 0u) { // kDiet: ratio and dec_scale precomputed in the exposure pair @@ -1046,11 +1092,13 @@ WG_KERNEL(post_tf_p12_diet_vsit) g_ratio = unif(wg_ldf(expo, 12u)); dec_scale = unif(wg_ldf(expo, 16u)); } - // the output table of this frame (wg_f16lut.hlsli; color: the table, 0 = none), wave-uniform + // the output table of this frame (wg_f16lut.hlsli; color in form 3, B_unused in form 0; 0 = none), wave-uniform #if WG_LZ_ANALYTIC const bool lut = false; // the table is indexed by an FP16 filter result (the original's); the analytic one is FP32 -#else +#elif POST_OUT == 3 const bool lut = WaveReadLaneFirst(f16lut_valid(color, dec_scale, nd_color_pi, nd_output_rne16) ? 1u : 0u) != 0u; +#else + const bool lut = WaveReadLaneFirst(f16lut_valid(B_unused, dec_scale, p0_pi1 ? 1.0f : pi_arg, 0u) ? 1u : 0u) != 0u; #endif #endif @@ -1069,7 +1117,7 @@ WG_KERNEL(post_tf_p12_diet_vsit) const uint2 mo_dims = uint2(0u, 0u); #endif -#if POST_OUT == 3 +#if WG_POST_OPT // the horizontal pass's lane roles (row hk, channel hc, cell hg) and its scratch offsets: fixed per lane const int hk_l = wl >= 30 ? 1 : 0, hrem_l = wl - 30 * hk_l, hc_l = hrem_l >= 20 ? 2 : (hrem_l >= 10 ? 1 : 0), hg_l = hrem_l - 10 * hc_l; @@ -1151,7 +1199,7 @@ WG_KERNEL(post_tf_p12_diet_vsit) // order): each texel's features are read from LDS once for both rows. Row y0 + 1's sums wait in gnext. Gather gk; if (k == 0) { -#if POST_OUT == 3 +#if WG_POST_OPT // unrolled: the first texel starts the sums from zeros as operands (no zero moves) and the loop's // overhead goes (78 VGPRs). The colour sums start from an opaque +0 (gather_zero_opq): with a // literal 0 the driver turns fma(w, ec, +0) into w * ec (a -0 product would stay -0). @@ -1197,7 +1245,7 @@ WG_KERNEL(post_tf_p12_diet_vsit) wg_st64(hist_out, mul24((uint)y, (uint)P_dw * 8u) + (uint)x * 8u, he); // display values (FP16) of the 64 columns -> scratch row slot + k: lanes 2m, 2m + 1 share a dword const uint srow = scr + (uint)k * kRowDw; -#if POST_OUT == 3 +#if WG_POST_OPT { // the partner's two dwords (c0 | c1 << 16, c2), then the even lane's three packed dwords (the same bits) const uint nx = QuadReadAcrossX(de.x), ny = QuadReadAcrossX(de.y); const uint d0 = (de.x & 0xffffu) | (nx << 16), d1 = (de.x >> 16) | (nx & 0xffff0000u); @@ -1222,7 +1270,7 @@ WG_KERNEL(post_tf_p12_diet_vsit) #endif } // reduced history: mean of H^ over the 2 x 2 block, the partner column in lane ^ 1 -#if POST_OUT == 3 +#if WG_POST_OPT // two code chains for the three channels: chain a is channel 0 on the even lane and channel 1 on the odd lane // (its sum hsum_o[1] + hsum_e[1]: the even lane's hsum_e[1] + hsum_o[1] commuted, equal but for a NaN's // payload, and every NaN m gives the code of enc_arg = 1e-4), chain b channel 2; the odd lane's code a comes @@ -1258,7 +1306,7 @@ WG_KERNEL(post_tf_p12_diet_vsit) r0 = floor_i(pos_y(ys) - 0.5f) - 1; int nrow = floor_i(pos_y(yl) - 0.5f) + 2 - r0 + 1; nstage = ncol * nrow < kTfCap ? ncol * nrow : kTfCap; -#if POST_OUT == 3 +#if WG_POST_OPT raw = pd_stage_fetch(lane, nstage - 1 < 0 ? 0 : nstage - 1, st_rx, st_col, inv_ncol_u, r0, c0, ncol PD_A); #if WG_TF_TWO raw2 = pd_stage_fetch(lane2, nstage - 1 < 0 ? 0 : nstage - 1, st_rx2, st_col2, inv_ncol_u, r0, c0, ncol PD_A); @@ -1276,7 +1324,7 @@ WG_KERNEL(post_tf_p12_diet_vsit) // horizontal pass of the wave's two rows: lane (row hk, channel hc, cell hg), output columns 4 hg ... 4 hg + 3 tf_row_sync(); -#if POST_OUT == 3 +#if WG_POST_OPT const bool hact = and(wact, h_lane); const int hk = hk_l; #else @@ -1285,13 +1333,13 @@ WG_KERNEL(post_tf_p12_diet_vsit) const int hk = hl >= 30 ? 1 : 0, hrem = hl - 30 * hk, hc = hrem >= 20 ? 2 : (hrem >= 10 ? 1 : 0), hg = hrem - 10 * hc; #endif -#if POST_OUT == 3 +#if WG_POST_OPT uint hv[4]; // set and used on the hact lanes only #else uint hv[4] = { 0u, 0u, 0u, 0u }; #endif if (hact) { -#if POST_OUT == 3 +#if WG_POST_OPT const uint src = scr + h_src; #else const uint src = scr + (uint)hk * kRowDw + (uint)hc * 32u + (uint)hg * 3u; @@ -1354,7 +1402,7 @@ WG_KERNEL(post_tf_p12_diet_vsit) } tf_row_sync(); if (hact) { -#if POST_OUT == 3 +#if WG_POST_OPT const uint dst = scr + h_dst; #else const uint dst = scr + (uint)hk * kRowDw + (uint)hc * (uint)kTfOutX + (uint)hg * 4u; @@ -1373,15 +1421,23 @@ WG_KERNEL(post_tf_p12_diet_vsit) const int nitems = (p_rdy - p_next) * 2 * kTfOutX; const bool interior = and(3 * p_next - 2 >= 0, 3 * p_rdy + 1 <= P_dh - 1); const int s0 = interior ? (3 * p_next - 2 - r_first) % kRing : 0; -#if POST_OUT == 3 +#if WG_POST_OPT if (lane < nitems) - pd_vitem(2 * p_next + v_yrel, v_xg, v_xo, v_urel, v_wa, v_wb, p_next, interior, s0, r_first, lut, dec_scale PD_A); + pd_vitem(2 * p_next + v_yrel, v_xg, v_xo, v_urel, v_wa, v_wb, p_next, interior, s0, r_first, lut, dec_scale +#if POST_OUT == 0 + , p0_pi1 +#endif + PD_A); [loop] for (int it = lane + kThreads; it < nitems; it += kThreads) { int yrel, xo, urel; uint wa, wb; pd_vsplit(it, yrel, xo, urel, wa, wb); pd_vitem(2 * p_next + yrel, strip * kTfOutX + xo, xo, urel, wa, wb, p_next, interior, s0, r_first, lut, - dec_scale PD_A); + dec_scale +#if POST_OUT == 0 + , p0_pi1 +#endif + PD_A); } #else #if POST_OUT == 0 @@ -1489,4 +1545,16 @@ WG_KERNEL(post_tf_f32_p12_diet_vsit_direct) else if (rc) post_body(true, false); else post_body(false, false); } +#elif POST_OUT == 0 +WG_KERNEL(post_tf_f32_p12_diet_vsit) +{ + WG_SETUP(); +#if defined(WG_STAGED_PIDIV) + post_body(false); +#else + // launch-uniform: the pi == 1 copy omits m1_scale completely + [branch] if (pi_arg == 1.0f) post_body(true); + else post_body(false); +#endif +} #endif diff --git a/clean/dx12/kernels/swin_c64w2.hlsl b/clean/dx12/kernels/swin_c64w2.hlsl index 1769335a..4ba1a5ae 100644 --- a/clean/dx12/kernels/swin_c64w2.hlsl +++ b/clean/dx12/kernels/swin_c64w2.hlsl @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT // swin_block_c64_nc (enc1's Swin block) with TWO windows per wave in 128-lane workgroups (tracks lev3-swin, r3-c64w2). // -// @wg-kernel swin_block_c64_nc co=swin.co block=256 group=128 gscale=1 wave=32 args=p,p,p,p,u,u,u,u defines=SW_C=64,SW_HEADS=2,SW_MLP=256,SW_ADD=0,SW_SAFE=1,SW_ECL=0,SW_W2=1,WG_LD_A16=1 +// @wg-kernel swin_block_c64_nc co=swin.co block=256 group=128 gscale=1 wave=32 args=p,p,p,p,u,u,u,u defines=SW_C=64,SW_HEADS=2,SW_MLP=256,SW_ADD=0,SW_SAFE=1,SW_ECL=0,SW_W2=1 // Model L's dec1 block swin_block_c64_add_ex_nc (swin_c64ex.hlsl) runs this kernel too (SW_W2_NAME), with the block // input x + xadd (SW_ADD) and the fused expand (SW_ECL: swin_block.hlsl's fused_expand with the 4 waves; each wave then // reads its two windows' records). diff --git a/clean/runtime/runtime.cpp b/clean/runtime/runtime.cpp index 4d819b72..1c469b2d 100644 --- a/clean/runtime/runtime.cpp +++ b/clean/runtime/runtime.cpp @@ -28,6 +28,7 @@ #include "model.h" #include "model_select.h" #include "runtime_core.h" +#include "native_direct_pi.h" #include "wg_runtime.h" #ifdef WG_DEV_DIAGNOSTICS #include "post_tf_align.h" @@ -487,6 +488,9 @@ struct Runtime { // Staged native D3D12 route (backend 3, buffer resources) with a pre-exposure other than 1: the output scale (scale_rgb) is // done by the true-fusion post kernel's store (WG_SCALE_FOLD / option scale_fold, default on): the same bytes, one launch fewer bool scale_fold = true; + // Staged native D3D12 route (POST_OUT == 0, output_filter=original): per-frame 256 KB FP16 decode LUT (WG_M_POST_LUT, + // default on) and dedicated pi != 1 _pf kernels folding color_divide (WG_M_PF_FOLD, default on; pi == 1 DXIL untouched). + bool m_post_lut = true, m_pf_fold = true; bool output_quality9 = false; // private Model M final-resampling quality profile; histories/weights unchanged // output_filter=quality5: the 5-tap output filter with WrongGPU's quality5 weights. The post kernels come from // post_w64__q5.co (post.hip built with WG_LZ_PROFILE=1: the same kernels with other filter constants), so every @@ -564,6 +568,7 @@ struct Runtime { // not read a colour buffer (they read the game's texture), so it is passed as their colour pointer. DevPtr f16lut = 0; static constexpr size_t kF16LutBytes = 256 + 65536 * 12; + static constexpr size_t kF16LutDecBytes = 256 + 65536 * 4; DevPtr mnear[2] = {0, 0}, mfar = 0, mcenter = 0, latent[2] = {0, 0}, reduced = 0; DevPtr skip1 = 0, merged1 = 0, skip2 = 0, merged2 = 0, bout[6] = {0}, e2 = 0, out2 = 0, e1 = 0, out1 = 0; DevPtr A = 0, B = 0, C = 0, hist[2] = {0, 0}, display = 0, out = 0, color_net = 0; @@ -1432,6 +1437,8 @@ struct Runtime { if (k == "post_bands") post_bands = std::max(1, std::atoi(v.c_str())); if (k == "post_tf") post_tf = !(v == "0" || v == "off" || v == "false"); if (k == "scale_fold") scale_fold = !(v == "0" || v == "off" || v == "false"); + if (k == "m_post_lut") m_post_lut = !(v == "0" || v == "off" || v == "false"); + if (k == "m_pf_fold") m_pf_fold = !(v == "0" || v == "off" || v == "false"); if (k == "output_filter") { if (v != "default" && v != "original" && v != "analytic" && v != "quality5" && v != "quality9") return fail("output_filter must be original, analytic, quality5 or quality9 (or default; Model M)"); @@ -1471,6 +1478,8 @@ struct Runtime { if (const char* sr = std::getenv("WG_SWIN_RESIDENT")) swin_resident = std::max(0, std::atoi(sr)); if (const char* t = std::getenv("WG_POST_TF")) post_tf = !(std::string(t) == "0" || std::string(t) == "off"); if (const char* t = std::getenv("WG_SCALE_FOLD")) scale_fold = !(std::string(t) == "0" || std::string(t) == "off"); + if (const char* t = std::getenv("WG_M_POST_LUT")) m_post_lut = !(std::string(t) == "0" || std::string(t) == "off"); + if (const char* t = std::getenv("WG_M_PF_FOLD")) m_pf_fold = !(std::string(t) == "0" || std::string(t) == "off"); // The output filter (spec 06 draft 4, spec 10 section 5 step 6): the original's weights from the model file by // default, the analytic set when the model file has none (noted for the log) or when it is selected. output_quality9 = output_filter_opt == "quality9"; @@ -1819,7 +1828,9 @@ struct Runtime { (native_direct_io || (alloc(&depth, R * 4) && alloc(&motion, R * 8) && alloc(&color_net, R * 16))) && (!own_direct || alloc(&color_net, R * 16)) && alloc(&expo, 32) && - (!native_direct_io || (alloc(&f16lut, kF16LutBytes) && (dev->memset(f16lut, 0, 256) || dfail()))) && + (!(native_direct_io || (d3d12_native && !model_l)) || + (alloc(&f16lut, native_direct_io ? kF16LutBytes : kF16LutDecBytes) && + (dev->memset(f16lut, 0, 256) || dfail()))) && alloc(&expo_partials, 4 * (size_t)cdiv(ny, 4)) && alloc(&expo_computed, 4) && alloc(&expo_sync, 4) && (dev->memset(expo_sync, 0, 4) || dfail()) && alloc(&feat0, t2 * 64) && alloc(&mnear[0], R * 4) && alloc(&mnear[1], R * 4) && alloc(&mfar, R * 4) && alloc(&mcenter, R * 4) && @@ -2549,6 +2560,51 @@ struct Runtime { } const bool mo = mhi != 0; mo_last = mo; + // The post route of this frame (decided here, before color_divide / exposure, so staged native D3D12 can decide + // whether to fold color_divide into dedicated _pf kernels and whether to build the 256 KB FP16 decode LUT). + const int hp = io ? io->out_half_pitch : 0; + const bool band_now = post_band && recon2_supported(rw, rh, dw, dh) && + (hp <= 0 || filter_kernel == "post_filter_c1_s4_band") && !mo; + const bool tf_mo_route = mo && post_band && (hp <= 0 || filter_kernel == "post_filter_c1_s4_band") && post_tf && + post_diet && post_kernel == "post_recon2_r_band_p12" && filter_kernel == "post_filter_c1_s4_band"; + const bool tf_mo4 = tf_mo_route && d3d12_native && hp <= 0 && tf4_supported(rw, rh, dw, dh) && + (post_tf4_force || !tf_supported(rw, rh, dw, dh, kTfCap)); + const bool tf_mo = tf_mo_route && (tf_mo4 || tf_supported(rw, rh, dw, dh, kTfCap)); + std::string tf_name; + const char* tf_kernel = nullptr; + int tf_block = 512; + if (band_now && post_tf && (p12 && recon2_supported(rw, rh, dw, dh)) && post_wave == 64 && + post_kernel == "post_recon2_r_band_p12" && filter_kernel == "post_filter_c1_s4_band") { + const bool tf4 = d3d12_native && hp <= 0 && post_diet && tf_vdiet && post_sel && post_idx && post_trim && + tf4_supported(rw, rh, dw, dh) && (post_tf4_force || !tf_supported(rw, rh, dw, dh, kTfCap)); + if (tf4) { + tf_name = std::string("post_tf4_dl_f32_p12_diet_vsit") + (native_direct_io ? "_direct" : ""); + tf_kernel = tf_name.c_str(); + tf_block = 256; + } else if (tf_supported(rw, rh, dw, dh, kTfCap)) { + if (post_diet) { + std::string sfx = std::string(tf_vdiet ? "v" : "") + (post_sel ? "s" : "") + (post_idx ? "i" : ""); + if (post_trim && sfx == "vsi") sfx += "t"; + tf_name = std::string(hp > 0 ? "post_tf_p12_diet" : "post_tf_f32_p12_diet") + (sfx.empty() ? "" : "_" + sfx); + if (native_direct_io) tf_name += "_direct"; + tf_kernel = tf_name.c_str(); + } else { + tf_kernel = hp > 0 ? "post_tf_p12" : "post_tf_f32_p12"; + } + } else if (tf_supported(rw, rh, dw, dh, kTfDlCap)) { + tf_kernel = hp > 0 ? "post_tf_dl_p12" : "post_tf_dl_f32_p12"; + } + } + const bool staged_tf_any = + d3d12_native && !native_direct_io && !raw && !model_l && hp <= 0 && + (tf_mo || (tf_kernel && (tf_name == "post_tf_f32_p12_diet_vsit" || tf_name == "post_tf4_dl_f32_p12_diet_vsit"))); + const bool fold_scale = scale_fold && staged_tf_any && pi != 1.0f; + const bool fold_pidiv = m_pf_fold && staged_tf_any && output_original && pi != 1.0f && + wgrt::native_direct_pi_window(pi).hi != 0u; + const bool use_post_lut = m_post_lut && staged_tf_any && output_original && f16lut != 0; + int pidiv_bits = 0; + if (fold_pidiv) std::memcpy(&pidiv_bits, &pi, 4); + const int mode = io && io->exposure_mode >= 0 ? io->exposure_mode : (cfg.auto_exposure ? 2 : 0); if (raw) { // interop input copy: the game's formats -> dense color / pi, depth, motion in render pixels if (!depth || !motion) return fail("raw inputs need the runtime's own depth / motion buffers"); DevPtr c16 = cin, d32 = din, m16 = min_; @@ -2562,16 +2618,16 @@ struct Runtime { cnet = cin = color_net; // exposure then reads color / pi with 1 / pi = 1 (see FrameIo) din = depth; min_ = motion; - } else if (!native_direct_io && pi != 1.0f) { + } else if (!native_direct_io && pi != 1.0f && !fold_pidiv) { int npix = rw * rh; float p_ = pi; if (!launch("color_divide", cdiv(npix, 256), 256, {&cin, &color_net, &p_, &npix})) return false; + if (mode == 2) dev->unordered_next(); // exposure reads cin, writes expo*; disjoint from color_net cnet = color_net; } if (native_direct_io) cnet = f16lut; // the direct kernels' colour pointer: the output table (wg_f16lut.hlsli) // 1. exposure (spec 10 section 5.1, spec 07); pair state [e, e_prev, last e] on the GPU - const int mode = io && io->exposure_mode >= 0 ? io->exposure_mode : (cfg.auto_exposure ? 2 : 0); int use_computed = mode != 0 ? 1 : 0, first = frames == 0 ? 1 : 0; float value = fr.exposure; DevPtr esrc = mode == 1 ? io->expo_src : expo_computed; @@ -2608,6 +2664,13 @@ struct Runtime { // the direct path's 64K tables of this frame (dx12/hlsl/wg_f16lut_in.hlsli, wg_f16lut.hlsli): one lane per FP16 pattern if (native_direct_io && !launch("f16lut_build_direct", 256, 256, {&f16lut, &expo})) return false; + // staged native D3D12 (POST_OUT == 0, output_filter=original): 256 KB FP16 decode LUT, overlapped with input_encoder + if (use_post_lut) { + float dec_scale_lut = (float)(0.01 / (fr.exposure == 0.0f ? 1.0 : (double)fr.exposure)); + float pi_lut = fold_scale ? pi : 1.0f; + if (!launch("f16lut_build_dec", 256, 256, {&f16lut, &expo, &dec_scale_lut, &pi_lut})) return false; + dev->unordered_next(); + } // 2. input encoder (spec 03) { @@ -2640,6 +2703,7 @@ struct Runtime { p.idh = (float)(1.0 / dh); p.irw = (float)(1.0 / rw); p.irh = (float)(1.0 / rh); + p.pad0 = pidiv_bits; DevPtr w = wdev["enc0"]; const int ntok = (fh / 2) * (fw / 2); if (model_l) { @@ -2651,11 +2715,14 @@ struct Runtime { p.sy = mop.sy; p.bx = mop.bx; p.by = mop.by; - if (!launch(native_direct_io ? "emo/input_encoder_mo_direct" : "emo/input_encoder_mo", cdiv(ntok, 128), 256, + if (!launch(native_direct_io ? "emo/input_encoder_mo_direct" + : (fold_pidiv ? "emo/input_encoder_mo_pf" : "emo/input_encoder_mo"), + cdiv(ntok, 128), 256, {&cnet, &mhi, &reduced, &latent[cur], &mnear[cur], &feat0, &mnear[nxt], &mfar, &mcenter, &expo, &w, &p, &emop})) return false; - } else if (!launch(native_direct_io ? "input_encoder_direct" : "input_encoder", + } else if (!launch(native_direct_io ? "input_encoder_direct" + : (fold_pidiv ? "input_encoder_pf" : "input_encoder"), cdiv(ntok, 128), 256, {&cnet, &din, &min_, &reduced, &latent[cur], &mnear[cur], &feat0, &mnear[nxt], &mfar, &mcenter, &expo, &w, &p})) @@ -2845,7 +2912,6 @@ struct Runtime { } // interop output copy (FrameIo.out_half_pitch > 0): the output is RGBA16F rows of that pitch with pi folded in - const int hp = io ? io->out_half_pitch : 0; if (post_fused && hp > 0 && !(out_f16 && pi == 1.0f && hp == 8 * ow)) return fail("output copy (RGBA16F rows) with post kernel " + post_kernel + ": needs a fused _o16 kernel, pre-exposure 1 and rows of 8 ow bytes"); @@ -2860,52 +2926,6 @@ struct Runtime { (hp > 0 ? hp == 8 * ow : (!io || !io->out || io->out_rgba16f)) && pi == 1.0f && recon2_supported(rw, rh, dw, dh); frame_out_f16 = out_f16 || recon_out16 || hp > 0; - // the banded kernels need the footprint of post_recon2_r; other frames: the unbanded kernel (post_recon where it - // does not fit, 8-byte history) + the unbanded default filter - // (and write 3 x f32: not with the output copy) - // With the interop output copy (hp > 0) the default banded filter's RGBA16F-rows form post_filter_c1_s4_h_band - // (pi folded in, any pitch) keeps the path banded; bit-identical to the unbanded copy filters. - // (motion at output resolution: the true-fusion kernel post_tf_*_mo where the default one would run, otherwise - // post_recon2_r_p12_mo and the unbanded filter; kernels/post_mo.hip) - const bool band_now = post_band && recon2_supported(rw, rh, dw, dh) && - (hp <= 0 || filter_kernel == "post_filter_c1_s4_band") && !mo; - const bool tf_mo_route = mo && post_band && (hp <= 0 || filter_kernel == "post_filter_c1_s4_band") && post_tf && - post_diet && post_kernel == "post_recon2_r_band_p12" && filter_kernel == "post_filter_c1_s4_band"; - // (native D3D12, f32 / direct forms: pmo/post_tf4_dl_* where the cap-352 kernel does not fit, or post_tf4=force) - const bool tf_mo4 = tf_mo_route && d3d12_native && hp <= 0 && tf4_supported(rw, rh, dw, dh) && - (post_tf4_force || !tf_supported(rw, rh, dw, dh, kTfCap)); - const bool tf_mo = tf_mo_route && (tf_mo4 || tf_supported(rw, rh, dw, dh, kTfCap)); - // True fusion: a frame of the default banded pack12 path (post kernels in wave64) whose size post_tf takes runs - // post_tf_p12 (Quality ... Ultra Performance) or post_tf_dl_p12 (native AA): bit-identical to the banded - // sequence (runner/post_exact.py), one launch, the display image is not written. Output as the banded path: RGBA16F - // rows with pi folded in for the interop output copy (hp > 0), otherwise 3 x f32 (then scale_rgb when pi != 1). - std::string tf_name; // post_tf_*_diet[_] (tf_kernel points into it) - const char* tf_kernel = nullptr; - int tf_block = 512; // post_tf4_dl_*: 256 - if (band_now && post_tf && p12_frame && post_wave == 64 && post_kernel == "post_recon2_r_band_p12" && - filter_kernel == "post_filter_c1_s4_band") { - // Native D3D12 (Brief 15): post_tf4_dl_f32_p12_diet_vsit[_direct] where the cap-352 kernel does not fit (native AA - // and every ratio between 1.0 and 1.5), or every frame it takes with post_tf4=force; f32 / direct forms only - const bool tf4 = d3d12_native && hp <= 0 && post_diet && tf_vdiet && post_sel && post_idx && post_trim && - tf4_supported(rw, rh, dw, dh) && (post_tf4_force || !tf_supported(rw, rh, dw, dh, kTfCap)); - if (tf4) { - tf_name = std::string("post_tf4_dl_f32_p12_diet_vsit") + (native_direct_io ? "_direct" : ""); - tf_kernel = tf_name.c_str(); - tf_block = 256; - } else if (tf_supported(rw, rh, dw, dh, kTfCap)) { - if (post_diet) { // the _diet kernel with the suffix of its options (kernels/post.hip WG_TF_KERNEL_DIET_X) - std::string sfx = std::string(tf_vdiet ? "v" : "") + (post_sel ? "s" : "") + (post_idx ? "i" : ""); - if (post_trim && sfx == "vsi") sfx += "t"; // post_trim: only the default kernel has the t form - tf_name = std::string(hp > 0 ? "post_tf_p12_diet" : "post_tf_f32_p12_diet") + (sfx.empty() ? "" : "_" + sfx); - if (native_direct_io) tf_name += "_direct"; - tf_kernel = tf_name.c_str(); - } else { - tf_kernel = hp > 0 ? "post_tf_p12" : "post_tf_f32_p12"; - } - } else if (tf_supported(rw, rh, dw, dh, kTfDlCap)) { - tf_kernel = hp > 0 ? "post_tf_dl_p12" : "post_tf_dl_f32_p12"; - } - } if (!mark(3)) return false; // l_post=own on the direct-resource route: Model L's reconstruction reads the colour from a buffer, so the game's // texture is staged into color_net first, with the staged route's values (native_direct_normalized_color: the @@ -2934,12 +2954,9 @@ struct Runtime { p.e = e; p.e_prev = e; p.ratio = 1.0f; + p.pad0 = pidiv_bits; DevPtr w = wdev["post"]; - // scale_fold: the staged true-fusion post (the HLSL ports of post_tf_f32_p12_diet_vsit, post_tf4_dl_*, pmo/post_tf_*_mo) - // applies the scale_rgb multiply itself when its pi argument is not 1. Never raw / direct / Model L / interop output copy. - const bool fold_scale = - scale_fold && d3d12_native && !native_direct_io && !raw && !model_l && hp <= 0 && pi != 1.0f && - (tf_mo || (tf_kernel && (tf_name == "post_tf_f32_p12_diet_vsit" || tf_name == "post_tf4_dl_f32_p12_diet_vsit"))); + DevPtr post_b = use_post_lut ? f16lut : (d3d12_native && !native_direct_io && hp <= 0 && !model_l ? (DevPtr)0 : B); if (post_fused) { // reconstruction + output filter in one launch, straight into the output buffer if (!fused_supported(rw, rh, dw, dh)) return fail(post_kernel + ": render size " + std::to_string(rw) + "x" + std::to_string(rh) + @@ -2964,6 +2981,7 @@ struct Runtime { p_h = hp > 0 || fold_scale ? pi : 1.0f; post_last = tf_mo4 ? "post_tf4_dl_f32_p12_mo" : hp > 0 ? "post_tf_p12_mo" : "post_tf_f32_p12_mo"; if (native_direct_io) post_last += "_direct"; // typed colour input and output texture (dx12/kernels/post.hlsl) + else if (fold_pidiv) post_last += "_pf"; filter_last = "fused"; #ifdef WG_DEV_DIAGNOSTICS if (const char* bad = post_tf_misaligned(A, hist[cur])) @@ -2971,7 +2989,7 @@ struct Runtime { " pointer is not 8-byte aligned (the D3D12 port reads it with 8-byte loads, dx12/kernels/post.hlsl)"); #endif if (!launch(("pmo/" + post_last).c_str(), cdiv(ow, 40) * nch, tf_mo4 ? 256 : 512, - with_taps({&cnet, &A, &B, &C, &hist[cur], &mnear[nxt], &mhi, &w, &hist[nxt], &reduced, &outp, &p, + with_taps({&cnet, &A, &post_b, &C, &hist[cur], &mnear[nxt], &mhi, &w, &hist[nxt], &reduced, &outp, &p, &expo, &ow_i, &oh_i, &dec_scale, &p_h, &hpitch, &cp, &k1, &c2, &mop}))) return false; // spec 10 draft 2: the filter's result times pi (folded in with the copy; direct shader stores include it) @@ -2990,15 +3008,15 @@ struct Runtime { const int nch = tf_chunks(ow, oh, cp, &k1, &c2); float dec_scale = (float)(0.01 / (fr.exposure == 0.0f ? 1.0 : (double)fr.exposure)), p_h = hp > 0 || fold_scale ? pi : 1.0f; - post_last = tf_kernel; + post_last = fold_pidiv ? (std::string(tf_kernel) + "_pf") : std::string(tf_kernel); filter_last = "fused"; #ifdef WG_DEV_DIAGNOSTICS if (const char* bad = post_tf_misaligned(A, hist[cur])) return fail(std::string("post_tf: the ") + bad + " pointer is not 8-byte aligned (the D3D12 port reads it with 8-byte loads, dx12/kernels/post.hlsl)"); #endif - if (!launch((post_pre + tf_kernel).c_str(), cdiv(ow, 40) * nch, tf_block, - with_taps({&cnet, &A, &B, &C, &hist[cur], &mnear[nxt], &mcenter, &mfar, &w, &hist[nxt], &reduced, + if (!launch((post_pre + post_last).c_str(), cdiv(ow, 40) * nch, tf_block, + with_taps({&cnet, &A, &post_b, &C, &hist[cur], &mnear[nxt], &mcenter, &mfar, &w, &hist[nxt], &reduced, &outp, &p, &expo, &ow_i, &oh_i, &dec_scale, &p_h, &hpitch, &cp, &k1, &c2}))) return false; if (!native_direct_io && hp <= 0 && pi != 1.0f && !fold_scale) { // direct / folded stores already include this multiply diff --git a/clean/tests/test_post_tf_align.py b/clean/tests/test_post_tf_align.py index eb527835..59abb64c 100644 --- a/clean/tests/test_post_tf_align.py +++ b/clean/tests/test_post_tf_align.py @@ -77,14 +77,14 @@ def test_runtime_wiring(self): self.assertTrue(any(b.start(1) <= c < b.end(1) for b in blocks), "a check outside WG_DEV_DIAGNOSTICS") self.assertTrue(any('#include "post_tf_align.h"' in b.group(1) for b in blocks), "the header outside the macro") self.assertNotIn('#include "post_tf_align.h"', re.sub(re.escape(DEV) + r"\n.*?\n#endif", "", src, flags=re.S)) - for launch in (r'launch\(\("pmo/" \+ post_last\)', r"launch\(\(post_pre \+ tf_kernel\)"): + for launch in (r'launch\(\("pmo/" \+ post_last\)', r"launch\(\(post_pre \+ post_last\)\.c_str\(\), cdiv\(ow, 40\)"): m = re.search(launch, src) self.assertIsNotNone(m, launch) before = src[max(0, m.start() - 600):m.start()] self.assertIn("post_tf_misaligned(A, hist[cur])", before, "no check right before " + launch) self.assertRegex(before, r"return fail\(.*\n?.*is not 8-byte aligned") call = src[m.start():m.start() + 300] - self.assertIn("&cnet, &A, &B, &C, &hist[cur]", call, "the launch no longer passes A and hist[cur] as checked") + self.assertIn("&cnet, &A, &post_b, &C, &hist[cur]", call, "the launch no longer passes A and hist[cur] as checked") if __name__ == "__main__": diff --git a/clean/tests/test_post_tf_geometry.py b/clean/tests/test_post_tf_geometry.py index 4f1b2355..0818616a 100644 --- a/clean/tests/test_post_tf_geometry.py +++ b/clean/tests/test_post_tf_geometry.py @@ -172,7 +172,8 @@ def test_nw4_declarations_agree(self): hlsl = open(os.path.join(clean, "dx12", "kernels", "post.hlsl"), encoding="utf-8").read() decl = re.findall(r"(?m)^// @wg-kernel (\S*post_tf4_dl\S*) .*$", hlsl) self.assertEqual(sorted(set(decl)), ["pmo/post_tf4_dl_f32_p12_mo", "pmo/post_tf4_dl_f32_p12_mo_direct", - "w64/post_tf4_dl_f32_p12_diet_vsit", "w64/post_tf4_dl_f32_p12_diet_vsit_direct"]) + "pmo/post_tf4_dl_f32_p12_mo_pf", "w64/post_tf4_dl_f32_p12_diet_vsit", + "w64/post_tf4_dl_f32_p12_diet_vsit_direct", "w64/post_tf4_dl_f32_p12_diet_vsit_pf"]) for line in re.findall(r"(?m)^// @wg-kernel \S*post_tf4_dl\S* .*$", hlsl): self.assertIn(" block=256 ", line) self.assertIn("WG_TF_NW=4,WG_TF_CAP=%d" % PS.TF4_DL_CAP, line) From 5ceffd95ddfbe11d3e6dc2a6975a562f074eaeee Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Tue, 24 Nov 2026 20:55:51 -0500 Subject: [PATCH 4/5] Model M (D3D12): Wave 1.5 - fix the post_tf VGPR cliff, drop the f16lut decode table and the encoder LDS (bit-exact) Follow-up to the Wave 1 levers (7b0f6a5), from the Wave 1 GPU A/B: T1.1 and T1.8 won, T1.2 / _pf regressed post_tf only because its VGPRs went 85 -> 96 (NW = 8: 2 workgroups per CU -> 1). - post_tf / post_tf_pf: one body per kernel (no inlined pi == 1 / pi != 1 copies). post_tf keeps b28679f's run-time pi == 1 branch at the store; _pf takes PiDiv + m1_scale unconditionally. The T1.2 cuts are now one switch each (WG_PO_*); the three per-lane hoists (staging row/column, horizontal roles, vertical first item) are left out of the NW = 8 kernels and the horizontal one of the NW = 4 kernels, everything else stays (exposure loads after the first staging loads, unrolled gather, QuadReadAcrossX display store, 2-chain reduced code, WG_PT_SMALL / WG_PT_INF, history base mask). RGA gfx1201, 0 spills: post_tf 96 -> 76, post_tf_pf 96 -> 77, post_tf4_dl 108 -> 83, post_tf4_dl_pf 90 -> 85, no v_and_b32 -8. Direct (form 3) kernels are unchanged. - T1.3 removed (f16lut_build_dec, the decode-table path of post.hlsl, wg_f16lut.hlsli / exposure.hlsl back to b28679f, runtime m_post_lut): it cost 5.5 us of build and saved nothing at pi = 1. - T1.5 reverted: input_encoder is back to b28679f's register-only A load (0 B LDS, 0 scratch); the WaveActiveAnyTrue(ec > 32752) guard and the _pf kernels stay. - kernel_sets.json regenerated (107 Model M / L kernels). --- clean/dx12/hlsl/wg_f16lut.hlsli | 27 +- clean/dx12/kernel_sets.json | 436 ------------------------------ clean/dx12/kernels/encoder.hlsl | 96 ++----- clean/dx12/kernels/exposure.hlsl | 22 -- clean/dx12/kernels/post.hlsl | 146 ++++++---- clean/runtime/runtime.cpp | 26 +- clean/tests/test_post_tf_align.py | 2 +- 7 files changed, 111 insertions(+), 644 deletions(-) diff --git a/clean/dx12/hlsl/wg_f16lut.hlsli b/clean/dx12/hlsl/wg_f16lut.hlsli index 6236d3cd..6a40372b 100644 --- a/clean/dx12/hlsl/wg_f16lut.hlsli +++ b/clean/dx12/hlsl/wg_f16lut.hlsli @@ -41,31 +41,6 @@ float f16lut_poly(float qc) return (t * y); } -// Exact scale with normal-result underflow repair (same bits as native_direct_scale / m1_scale). -#if !defined(WG_DIRECT_TEXTURES) -float f16lut_scale_ref(float a, float b) -{ - const float r = a * b; - const uint ua = asuint(a), ub = asuint(b); - const uint ea = (ua >> 23u) & 255u, eb = (ub >> 23u) & 255u; - if ((asuint(r) & 0x7fffffffu) == 0u && ea != 0u && eb != 0u && ea + eb == 127u) { - const uint64_t product = (uint64_t)((ua & 0x7fffffu) | 0x800000u) * (uint64_t)((ub & 0x7fffffu) | 0x800000u); - const uint64_t normal = (uint64_t)1u << 47u; - if (product >= normal - ((uint64_t)1u << 23u) && product < normal) - return asfloat(((ua ^ ub) & 0x80000000u) | 0x00800000u); - } - return r; -} -float f16lut_scale(float a, float b) -{ - float r = a * b; - [branch] if (and(abs(r) == 0.0f, abs(a) >= asfloat(0x00800000u))) r = f16lut_scale_ref(a, b); - return r; -} -#else -float f16lut_scale(float a, float b) { return native_direct_scale(a, b); } -#endif - // The output value of FP16 bits r (post.hlsl's vertical pass after lz5): z is an opaque zero (mul_nz's addend -0 the // driver cannot drop). For finite dec_scale and pi no value is NaN (v is finite and positive), so the software RNE of // the clamped value (wg_f2h) has the bits of the post's driver conversion (v_cvt_f16_f32). @@ -75,7 +50,7 @@ float f16lut_out(uint r, float dec_scale, float pi, bool rne16, uint z) float v = exp2(q * kF16lutDecK) - 1.0f; v = q < kF16lutPolyMax ? f16lut_poly(q) : v; float x = mad(v, dec_scale, asfloat(z | 0x80000000u)); - if (pi != 1.0f) x = f16lut_scale(x, pi); + if (pi != 1.0f) x = native_direct_scale(x, pi); if (rne16) { const float c = select(x > 65504.0f, 65504.0f, select(x < -65504.0f, -65504.0f, x)); x = f16tof32(wg_f2h(c)); diff --git a/clean/dx12/kernel_sets.json b/clean/dx12/kernel_sets.json index c0e90e7b..26f5fdc5 100644 --- a/clean/dx12/kernel_sets.json +++ b/clean/dx12/kernel_sets.json @@ -60,10 +60,6 @@ "exposure_pair_diet_direct", "exposure.co" ], - [ - "f16lut_build_dec", - "-" - ], [ "f16lut_build_direct", "-" @@ -218,94 +214,14 @@ ] ], "model_l": [ - [ - "bneck/swin_bneck6_mb_nc", - "swin__bmegahg.co" - ], - [ - "bneck/swin_bneck6_mbo_nc", - "swin__bmegahg.co" - ], - [ - "bneck/swin_bneck6_nc", - "swin__bmegahg.co" - ], - [ - "color_divide", - "frame.co" - ], - [ - "d1p12/dec0_q7", - "dec1_dec0__pack12.co" - ], - [ - "d1p12/swin_block_c64_add_dec0_nc", - "dec1_dec0__pack12.co" - ], - [ - "d1p12/swin_block_c64_add_dec0_xd_nc", - "dec1_dec0__pack12.co" - ], - [ - "emo/input_encoder_mo", - "encoder_mo.co" - ], - [ - "emo/input_encoder_mo_direct", - "encoder_mo.co" - ], - [ - "emo/input_encoder_mo_pf", - "encoder_mo.co" - ], [ "expand_c128_hyb", "-" ], - [ - "expand_c96", - "swin.co" - ], [ "expand_c96_hyb", "-" ], - [ - "exposure_auto_diet", - "exposure.co" - ], - [ - "exposure_auto_diet_direct", - "exposure.co" - ], - [ - "exposure_pair_diet", - "exposure.co" - ], - [ - "exposure_pair_diet_direct", - "exposure.co" - ], - [ - "f16lut_build_dec", - "-" - ], - [ - "f16lut_build_direct", - "-" - ], - [ - "input_encoder", - "encoder.co" - ], - [ - "input_encoder_direct", - "encoder.co" - ], - [ - "input_encoder_pf", - "encoder.co" - ], [ "l_coef", "l_ops.co" @@ -478,58 +394,14 @@ "lev/input_encoder_q20", "-" ], - [ - "native_driver_probe", - "-" - ], - [ - "native_fill", - "-" - ], - [ - "patch_merge_c64", - "swin.co" - ], [ "patch_merge_c64_hyb", "-" ], - [ - "patch_merge_c96", - "swin.co" - ], [ "patch_merge_c96_hyb", "-" ], - [ - "pmo/post_tf4_dl_f32_p12_mo", - "post_mo_w64__orig.co" - ], - [ - "pmo/post_tf4_dl_f32_p12_mo_direct", - "post_mo_w64__orig.co" - ], - [ - "pmo/post_tf4_dl_f32_p12_mo_pf", - "post_mo_w64__orig.co" - ], - [ - "pmo/post_tf_f32_p12_mo", - "post_mo_w64__orig.co" - ], - [ - "pmo/post_tf_f32_p12_mo_direct", - "post_mo_w64__orig.co" - ], - [ - "pmo/post_tf_f32_p12_mo_pf", - "post_mo_w64__orig.co" - ], - [ - "scale_rgb", - "frame.co" - ], [ "swin_block_c128_hyb", "-" @@ -546,192 +418,20 @@ "swin_block_c64_hyb", "-" ], - [ - "swin_block_c64_nc", - "swin.co" - ], - [ - "swin_block_c96_add_ex_nc", - "swin.co" - ], [ "swin_block_c96_add_hyb", "-" ], - [ - "swin_block_c96_add_xe_nc", - "swin.co" - ], - [ - "swin_block_c96_add_xe_rev_nc", - "swin.co" - ], - [ - "swin_block_c96_add_xe_xd_nc", - "swin.co" - ], - [ - "swin_block_c96_add_xe_xo_nc", - "swin.co" - ], [ "swin_block_c96_hyb", "-" ], - [ - "swin_block_c96_nc", - "swin.co" - ], - [ - "swin_block_c96_pm_nc", - "swin.co" - ], - [ - "swin_block_c96_pm_rev_nc", - "swin.co" - ], [ "swin_bneck6_hyb", "-" - ], - [ - "timing_barrier", - "frame.co" - ], - [ - "w64/post_filter_c1_s4", - "post_w64.co" - ], - [ - "w64/post_tf4_dl_f32_p12_diet_vsit", - "post_w64.co" - ], - [ - "w64/post_tf4_dl_f32_p12_diet_vsit", - "post_w64__orig.co" - ], - [ - "w64/post_tf4_dl_f32_p12_diet_vsit_direct", - "post_w64.co" - ], - [ - "w64/post_tf4_dl_f32_p12_diet_vsit_direct", - "post_w64__orig.co" - ], - [ - "w64/post_tf4_dl_f32_p12_diet_vsit_pf", - "post_w64__orig.co" - ], - [ - "w64/post_tf_f32_p12_diet_vsit", - "post_w64.co" - ], - [ - "w64/post_tf_f32_p12_diet_vsit", - "post_w64__orig.co" - ], - [ - "w64/post_tf_f32_p12_diet_vsit_direct", - "post_w64.co" - ], - [ - "w64/post_tf_f32_p12_diet_vsit_direct", - "post_w64__orig.co" - ], - [ - "w64/post_tf_f32_p12_diet_vsit_pf", - "post_w64__orig.co" - ], - [ - "w64/post_tf_p12_diet_vsit", - "post_w64__orig.co" - ], - [ - "w64/post_tf_p12_diet_vsit_tex", - "post_w64__orig.co" ] ], "model_k": [ - [ - "bneck/swin_bneck6_mb_nc", - "swin__bmegahg.co" - ], - [ - "bneck/swin_bneck6_mbo_nc", - "swin__bmegahg.co" - ], - [ - "bneck/swin_bneck6_nc", - "swin__bmegahg.co" - ], - [ - "color_divide", - "frame.co" - ], - [ - "d1p12/dec0_q7", - "dec1_dec0__pack12.co" - ], - [ - "d1p12/swin_block_c64_add_dec0_nc", - "dec1_dec0__pack12.co" - ], - [ - "d1p12/swin_block_c64_add_dec0_xd_nc", - "dec1_dec0__pack12.co" - ], - [ - "emo/input_encoder_mo", - "encoder_mo.co" - ], - [ - "emo/input_encoder_mo_direct", - "encoder_mo.co" - ], - [ - "emo/input_encoder_mo_pf", - "encoder_mo.co" - ], - [ - "expand_c96", - "swin.co" - ], - [ - "exposure_auto_diet", - "exposure.co" - ], - [ - "exposure_auto_diet_direct", - "exposure.co" - ], - [ - "exposure_pair_diet", - "exposure.co" - ], - [ - "exposure_pair_diet_direct", - "exposure.co" - ], - [ - "f16lut_build_dec", - "-" - ], - [ - "f16lut_build_direct", - "-" - ], - [ - "input_encoder", - "encoder.co" - ], - [ - "input_encoder_direct", - "encoder.co" - ], - [ - "input_encoder_pf", - "encoder.co" - ], [ "k_expo_scalars", "k_io.co" @@ -975,142 +675,6 @@ [ "kxs/k_in_tiled_uniform", "k_io_exact.co" - ], - [ - "native_driver_probe", - "-" - ], - [ - "native_fill", - "-" - ], - [ - "patch_merge_c64", - "swin.co" - ], - [ - "patch_merge_c96", - "swin.co" - ], - [ - "pmo/post_tf4_dl_f32_p12_mo", - "post_mo_w64__orig.co" - ], - [ - "pmo/post_tf4_dl_f32_p12_mo_direct", - "post_mo_w64__orig.co" - ], - [ - "pmo/post_tf4_dl_f32_p12_mo_pf", - "post_mo_w64__orig.co" - ], - [ - "pmo/post_tf_f32_p12_mo", - "post_mo_w64__orig.co" - ], - [ - "pmo/post_tf_f32_p12_mo_direct", - "post_mo_w64__orig.co" - ], - [ - "pmo/post_tf_f32_p12_mo_pf", - "post_mo_w64__orig.co" - ], - [ - "scale_rgb", - "frame.co" - ], - [ - "swin_block_c64_nc", - "swin.co" - ], - [ - "swin_block_c96_add_ex_nc", - "swin.co" - ], - [ - "swin_block_c96_add_xe_nc", - "swin.co" - ], - [ - "swin_block_c96_add_xe_rev_nc", - "swin.co" - ], - [ - "swin_block_c96_add_xe_xd_nc", - "swin.co" - ], - [ - "swin_block_c96_add_xe_xo_nc", - "swin.co" - ], - [ - "swin_block_c96_nc", - "swin.co" - ], - [ - "swin_block_c96_pm_nc", - "swin.co" - ], - [ - "swin_block_c96_pm_rev_nc", - "swin.co" - ], - [ - "timing_barrier", - "frame.co" - ], - [ - "w64/post_filter_c1_s4", - "post_w64.co" - ], - [ - "w64/post_tf4_dl_f32_p12_diet_vsit", - "post_w64.co" - ], - [ - "w64/post_tf4_dl_f32_p12_diet_vsit", - "post_w64__orig.co" - ], - [ - "w64/post_tf4_dl_f32_p12_diet_vsit_direct", - "post_w64.co" - ], - [ - "w64/post_tf4_dl_f32_p12_diet_vsit_direct", - "post_w64__orig.co" - ], - [ - "w64/post_tf4_dl_f32_p12_diet_vsit_pf", - "post_w64__orig.co" - ], - [ - "w64/post_tf_f32_p12_diet_vsit", - "post_w64.co" - ], - [ - "w64/post_tf_f32_p12_diet_vsit", - "post_w64__orig.co" - ], - [ - "w64/post_tf_f32_p12_diet_vsit_direct", - "post_w64.co" - ], - [ - "w64/post_tf_f32_p12_diet_vsit_direct", - "post_w64__orig.co" - ], - [ - "w64/post_tf_f32_p12_diet_vsit_pf", - "post_w64__orig.co" - ], - [ - "w64/post_tf_p12_diet_vsit", - "post_w64__orig.co" - ], - [ - "w64/post_tf_p12_diet_vsit_tex", - "post_w64__orig.co" ] ] } diff --git a/clean/dx12/kernels/encoder.hlsl b/clean/dx12/kernels/encoder.hlsl index 2a8492d1..744bf4a5 100644 --- a/clean/dx12/kernels/encoder.hlsl +++ b/clean/dx12/kernels/encoder.hlsl @@ -326,12 +326,6 @@ float2 enc_mo_raw(int x, uint ty) } #endif -#if !WG_ENC_VALUES_OUT -// Per-lane private scratch in LDS (10 KB for 256 lanes, bank-conflict-free) so slot 1 inputs and slot 0 outputs -// do not stay live in VGPRs across pixel_values, keeping input_encoder <= 96 VGPRs (4 WGs/CU on gfx1201, 0 scratch). -groupshared uint enc_lds[10 * WG_BLOCK]; -#endif - void encoder_main() { uint tid = wg_lane_id(); @@ -534,63 +528,26 @@ void encoder_main() const PiDiv pd = pidiv_make((uint)P_pad0); #endif uint q[2][5], x8[2][2]; -#if !WG_ENC_VALUES_OUT - // Park slot 1's inputs in per-lane LDS during slot 0's pixel_values (no barrier needed: lane-private) - enc_lds[0u * WG_BLOCK + tid] = asuint(cs[1].x); - enc_lds[1u * WG_BLOCK + tid] = asuint(cs[1].y); - enc_lds[2u * WG_BLOCK + tid] = asuint(cs[1].z); - enc_lds[3u * WG_BLOCK + tid] = asuint(mn[1].x); - enc_lds[4u * WG_BLOCK + tid] = asuint(mn[1].y); - enc_lds[5u * WG_BLOCK + tid] = asuint(mf[1].x); - enc_lds[6u * WG_BLOCK + tid] = asuint(mf[1].y); - enc_lds[7u * WG_BLOCK + tid] = asuint(mc[1].x); - enc_lds[8u * WG_BLOCK + tid] = asuint(mc[1].y); - enc_lds[9u * WG_BLOCK + tid] = xs[1]; -#endif [unroll] for (uint dx2 = 0; dx2 < 2; ++dx2) { -#if !WG_ENC_VALUES_OUT - float4 c; - float2 mn_c, mf_c, mc_c; - uint x_c; - if (dx2 == 0u) { - c = cs[0]; mn_c = mn[0]; mf_c = mf[0]; mc_c = mc[0]; x_c = xs[0]; - } else { - c = float4(asfloat(enc_lds[0u * WG_BLOCK + tid]), asfloat(enc_lds[1u * WG_BLOCK + tid]), - asfloat(enc_lds[2u * WG_BLOCK + tid]), 0.0f); - mn_c = float2(asfloat(enc_lds[3u * WG_BLOCK + tid]), asfloat(enc_lds[4u * WG_BLOCK + tid])); - mf_c = float2(asfloat(enc_lds[5u * WG_BLOCK + tid]), asfloat(enc_lds[6u * WG_BLOCK + tid])); - mc_c = float2(asfloat(enc_lds[7u * WG_BLOCK + tid]), asfloat(enc_lds[8u * WG_BLOCK + tid])); - x_c = enc_lds[9u * WG_BLOCK + tid]; - // Park slot 0's quantized outputs in LDS while slot 1 runs pixel_values - enc_lds[0u * WG_BLOCK + tid] = q[0][0]; - enc_lds[1u * WG_BLOCK + tid] = q[0][1]; - enc_lds[2u * WG_BLOCK + tid] = q[0][4]; - enc_lds[3u * WG_BLOCK + tid] = x8[0][0]; - enc_lds[4u * WG_BLOCK + tid] = x8[0][1]; - } -#else float4 c = cs[dx2]; - float2 mn_c = mn[dx2], mf_c = mf[dx2], mc_c = mc[dx2]; - uint x_c = xs[dx2]; -#endif #if defined(WG_STAGED_PIDIV) c = float4(asfloat(asuint(pidiv3(c.xyz, pd, (uint)P_rw >> 31u)) ^ ((uint)P_rw >> 31u)), c.w); #endif float v[20]; if (U.mode == kHistGeneral) // uniform - pixel_values(kHistGeneral, U, (int)x_c, y, c, mn_c, mf_c, mc_c, v + pixel_values(kHistGeneral, U, (int)xs[dx2], y, c, mn[dx2], mf[dx2], mc[dx2], v #if defined(WG_DIRECT_TEXTURES) , lut, lt[dx2] #endif ); else if (U.mode == kHistSame) - pixel_values(kHistSame, U, (int)x_c, y, c, mn_c, mf_c, mc_c, v + pixel_values(kHistSame, U, (int)xs[dx2], y, c, mn[dx2], mf[dx2], mc[dx2], v #if defined(WG_DIRECT_TEXTURES) , lut, lt[dx2] #endif ); else - pixel_values(kHistRaw, U, (int)x_c, y, c, mn_c, mf_c, mc_c, v + pixel_values(kHistRaw, U, (int)xs[dx2], y, c, mn[dx2], mf[dx2], mc[dx2], v #if defined(WG_DIRECT_TEXTURES) , lut, lt[dx2] #endif @@ -603,13 +560,6 @@ void encoder_main() x8[dx2][1] = wg_xor16_u(qq[3]); #endif } -#if !WG_ENC_VALUES_OUT - q[0][0] = enc_lds[0u * WG_BLOCK + tid]; - q[0][1] = enc_lds[1u * WG_BLOCK + tid]; - q[0][4] = enc_lds[2u * WG_BLOCK + tid]; - x8[0][0] = enc_lds[3u * WG_BLOCK + tid]; - x8[0][1] = enc_lds[4u * WG_BLOCK + tid]; -#endif #if WG_ENC_VALUES_OUT // pixel (X0 + dx, Y) of the halo map: 20 value bytes and 12 zero bytes @@ -638,47 +588,35 @@ void encoder_main() bf[2 * p + 1] = uint2(own ? q[d][4] : 0u, 0u); } - // Two output tiles at a time; stream A fragments per p2 step to keep VGPRs <= 96 (4 WGs/CU, 0 spills) + // Two output tiles at a time; every A fragment and bias of a pair is loaded before its first WMMA + uint o[8]; [unroll] for (uint pr = 0; pr < 2; ++pr) { v8f acc[2]; - uint2 A[2][2]; + uint2 A[2][8]; [unroll] for (uint j = 0; j < 2; ++j) { acc[j] = wg_load8f(w, kOffBias + hh * 32u + (2u * pr + j) * 64u); - A[j][0] = wg_ld64(w, kOffFrags + ((2u * pr + j) * 8u + 0u) * 256u + lane * 8u); - A[j][1] = wg_ld64(w, kOffFrags + ((2u * pr + j) * 8u + 1u) * 256u + lane * 8u); + [unroll] for (uint kb = 0; kb < 8; ++kb) + A[j][kb] = wg_ld64(w, kOffFrags + ((2u * pr + j) * 8u + kb) * 256u + lane * 8u); } [unroll] for (uint p2 = 0; p2 < 4; ++p2) { - uint2 An[2][2]; - if (p2 < 3u) { - [unroll] for (uint jn = 0; jn < 2; ++jn) { - An[jn][0] = wg_ld64(w, kOffFrags + ((2u * pr + jn) * 8u + 2u * p2 + 2u) * 256u + lane * 8u); - An[jn][1] = wg_ld64(w, kOffFrags + ((2u * pr + jn) * 8u + 2u * p2 + 3u) * 256u + lane * 8u); - } - } [unroll] for (uint j2 = 0; j2 < 2; ++j2) { - acc[j2] = wg_wmma_fp8(A[j2][0], bf[2 * p2], acc[j2]); - acc[j2] = wg_wmma_fp8(A[j2][1], bf[2 * p2 + 1], acc[j2]); + acc[j2] = wg_wmma_fp8(A[j2][2 * p2], bf[2 * p2], acc[j2]); + acc[j2] = wg_wmma_fp8(A[j2][2 * p2 + 1], bf[2 * p2 + 1], acc[j2]); acc[j2] = round_f16(acc[j2]); } - if (p2 < 3u) { - [unroll] for (uint jc = 0; jc < 2; ++jc) { - A[jc][0] = An[jc][0]; - A[jc][1] = An[jc][1]; - } - } } - uint o4[4]; [unroll] for (uint j3 = 0; j3 < 2; ++j3) { [unroll] for (uint i = 0; i < 8; ++i) acc[j3].v[i] = wg_fmin(wg_fmax(acc[j3].v[i], -6.28125f), 6.28125f); uint2 qo = wg_pack_q8_nc_ags(acc[j3]); // within +-6.28125: the bytes of wg_pack_q8 - o4[2 * j3] = qo.x; - o4[2 * j3 + 1] = qo.y; - } - if (tok < ntok) { - uint fo = (uint)tok * 64u + hh * 32u + pr * 16u; - wg_st128(features, fo, uint4(o4[0], o4[1], o4[2], o4[3])); + o[4 * pr + 2 * j3] = qo.x; + o[4 * pr + 2 * j3 + 1] = qo.y; } } + if (tok < ntok) { + uint fo = (uint)tok * 64u + hh * 32u; + wg_st128(features, fo, uint4(o[0], o[1], o[2], o[3])); + wg_st128(features, fo + 16u, uint4(o[4], o[5], o[6], o[7])); + } #endif } diff --git a/clean/dx12/kernels/exposure.hlsl b/clean/dx12/kernels/exposure.hlsl index 34ede02d..9cb36833 100644 --- a/clean/dx12/kernels/exposure.hlsl +++ b/clean/dx12/kernels/exposure.hlsl @@ -11,7 +11,6 @@ // @wg-kernel exposure_auto_diet co=exposure.co block=256 wave=32 args=p,p,p,p,p,b36,u // @wg-kernel exposure_auto_diet_direct co=exposure.co entry=exposure_auto_diet block=256 wave=32 args=p,p,p,p,p,b36,u rs=direct defines=WG_DIRECT_TEXTURES=1 // @wg-kernel f16lut_build_direct co=- block=256 wave=32 args=p,p rs=direct defines=WG_DIRECT_TEXTURES=1,F16LUT_BUILD=1 -// @wg-kernel f16lut_build_dec co=- block=256 wave=32 args=p,p,u,u defines=F16LUT_BUILD_DEC=1 #include "wg_platform.hlsli" #if defined(WG_DIRECT_TEXTURES) #include "native_direct.hlsli" @@ -34,27 +33,6 @@ WG_KERNEL(f16lut_build_direct) if (and(tab != 0u, i < 65536u)) f16lut_build(tab, i, wg_ldf(ex, 0u), wg_ldf(ex, 16u), nd_color_pi, nd_output_rne16, nd_rw >> 31u); } -#elif defined(F16LUT_BUILD_DEC) -// Staged path (POST_OUT == 0) per-frame 64K FP16 decode table (hlsl/wg_f16lut.hlsli, 256 KB): skips rebuild when the -// header already matches (dec_scale, pi_arg, 0). -#include "wg_f16lut.hlsli" -WG_ARGS_BEGIN - uint tab, ex; - float dec_scale_arg, pi_arg; -WG_ARGS_END -WG_KERNEL(f16lut_build_dec) -{ - WG_SETUP(); - const uint i = wg_block_id() * WG_BLOCK + wg_lane_id(); - if (or(tab == 0u, i >= 65536u)) return; - const float dec_scale = ex != 0u ? wg_ldf(ex, 16u) : dec_scale_arg; - if (f16lut_valid(tab, dec_scale, pi_arg, 0u)) return; - wg_mem.Store(tab + WG_F16LUT_DEC + 4u * i, asuint(f16lut_out(i, dec_scale, pi_arg, false, wg_grid_size() >> 31u))); - if (i == 0u) { - const bool finite = and((asuint(dec_scale) & 0x7f800000u) != 0x7f800000u, (asuint(pi_arg) & 0x7f800000u) != 0x7f800000u); - wg_mem.Store4(tab, finite ? uint4(WG_F16LUT_MAGIC, asuint(dec_scale), asuint(pi_arg), 0u) : uint4(0u, 0u, 0u, 0u)); - } -} #else WG_ARGS_BEGIN diff --git a/clean/dx12/kernels/post.hlsl b/clean/dx12/kernels/post.hlsl index 3b0992c6..ef825060 100644 --- a/clean/dx12/kernels/post.hlsl +++ b/clean/dx12/kernels/post.hlsl @@ -78,8 +78,6 @@ #if defined(WG_DIRECT_TEXTURES) #include "native_direct.hlsli" #include "wg_f16lut.hlsli" -#elif POST_OUT == 0 && !WG_LZ_ANALYTIC -#include "wg_f16lut.hlsli" #endif #if defined(WG_STAGED_PIDIV) #include "wg_pidiv.hlsli" @@ -88,6 +86,52 @@ // bytes the checks cover (0: 3 x f32 rows, 3: direct); the RGBA16F row forms 1 / 2 keep their previous code #define WG_POST_CUTS (POST_OUT == 0 || POST_OUT == 3) #define WG_POST_OPT (POST_OUT == 0 || POST_OUT == 3) +// The WG_POST_OPT instruction cuts, one switch each (experiments: -DWG_PO_x=0). Form 3 (direct) always takes all of them. +#if POST_OUT == 3 +#define WG_PO_STAGE 1 // staging fetch with per-lane fixed texel row / column (pd_stage_fetch) +#define WG_PO_EXPO 1 // the exposure-pair loads after the first staging loads +#define WG_PO_HH 1 // the horizontal pass's lane roles and scratch offsets fixed per lane +#define WG_PO_VV 1 // the vertical pass's first item fixed per lane (pd_vsplit once) +#define WG_PO_GATH 1 // unrolled gather +#define WG_PO_QUAD 1 // display values: partner dwords by QuadReadAcrossX, three packed dwords +#define WG_PO_RH 1 // reduced history: two code chains +#else +// Staged form 0 (POST_OUT == 0; RGA, gfx1201): the three per-lane hoists (STAGE: staging row / column, HH: horizontal +// roles and offsets, VV: the vertical first item) each keep a few VGPRs live for the whole walk. With all of them +// post_tf is 96 VGPRs (1 workgroup per CU of 8 waves64; 85 keeps 2), so the 512-lane kernels (NW = 8) leave them out +// and keep the rest (76 VGPRs; _pf 77). The NW = 4 kernels (two staged texels per lane) keep STAGE and VV, leave out HH +// (83 VGPRs; _pf 85). +#if WG_TF_NW == 4 +#define WG_PO_STAGE_D 1 +#define WG_PO_HH_D 0 +#define WG_PO_VV_D 1 +#else +#define WG_PO_STAGE_D 0 +#define WG_PO_HH_D 0 +#define WG_PO_VV_D 0 +#endif +#ifndef WG_PO_STAGE +#define WG_PO_STAGE (WG_POST_OPT && WG_PO_STAGE_D) +#endif +#ifndef WG_PO_EXPO +#define WG_PO_EXPO WG_POST_OPT +#endif +#ifndef WG_PO_HH +#define WG_PO_HH (WG_POST_OPT && WG_PO_HH_D) +#endif +#ifndef WG_PO_VV +#define WG_PO_VV (WG_POST_OPT && WG_PO_VV_D) +#endif +#ifndef WG_PO_GATH +#define WG_PO_GATH WG_POST_OPT +#endif +#ifndef WG_PO_QUAD +#define WG_PO_QUAD WG_POST_OPT +#endif +#ifndef WG_PO_RH +#define WG_PO_RH WG_POST_OPT +#endif +#endif #if POST_OUT == 2 RWTexture2D wg_tex_out : register(u3); // the output texture (RGBA16F), written through a typed UAV #endif @@ -373,7 +417,7 @@ TfRaw pd_stage_fetch(int lane, int ilast, int st_r, int st_col, float inv_ncol_u } #endif -#if WG_TF_TWO && !WG_POST_OPT +#if WG_TF_TWO && !WG_PO_STAGE // The buffer forms' second staged texel of the lane: i = mini(lane + kThreads, nstage - 1), the first texel's formula TfRaw tf_stage2(int lane, int nstage, float inv_ncol, int r0, int c0, int ncol PD_P) { @@ -871,11 +915,7 @@ void tf_row_sync() // urel = 3 pr + 2 dy past the pass's first row pair, and the lane phase's filter weights wa = (w0, w4), wb = (w1, w3) as // FP16 bit pairs. post_body calls it with per-lane values fixed for the walk on the first pass (it = lane). void pd_vitem(int y, int xg, int xo, int urel, uint wa, uint wb, int p_next, bool interior, int s0, int r_first, bool lut, - float dec_scale -#if POST_OUT == 0 - , const bool p0_pi1 -#endif - PD_P) + float dec_scale PD_P) { if (or(y >= oh, xg >= ow)) return; uint t[5][3]; @@ -911,25 +951,17 @@ void pd_vitem(int y, int xg, int xo, int urel, uint wa, uint wb, int p_next, boo } #else const half w0 = hbox(wa), w4 = hbox(wa >> 16), w1 = hbox(wb), w3 = hbox(wb >> 16); - if (lut) { // the output values T[r] (wg_f16lut.hlsli): the chain below, looked up #if POST_OUT == 3 + if (lut) { // the output values T[r] (wg_f16lut.hlsli, lev-f16lut d7149e2): the chain below, looked up float3 res; [unroll] for (int c = 0; c < 3; ++c) { const uint r = lz5_original(t[0][c], t[1][c], t[2][c], t[3][c], t[4][c], w0, w1, hbox(lz_w2), w3, w4); res[c] = asfloat(wg_ld32(color, WG_F16LUT_DEC + (r << 2))); } native_direct_output[uint2((uint)xg, (uint)y)] = float4(res, 1.0f); -#else - uint3 res; - [unroll] for (int c = 0; c < 3; ++c) { - const uint r = lz5_original(t[0][c], t[1][c], t[2][c], t[3][c], t[4][c], w0, w1, hbox(lz_w2), w3, w4); - res[c] = wg_ld32(B_unused, WG_F16LUT_DEC + (r << 2)); - } - const uint o = mul24((uint)y, (uint)ow * 12u) + mul24((uint)xg, 12u); - wg_mem.Store3(out_p + o, res); -#endif return; } +#endif float vo[3], qv[3]; bool vsmall = false; [unroll] for (int c = 0; c < 3; ++c) { @@ -961,14 +993,22 @@ void pd_vitem(int y, int xg, int xo, int urel, uint wa, uint wb, int p_next, boo } #else const uint o = mul24((uint)y, (uint)ow * 12u) + mul24((uint)xg, 12u); - if (p0_pi1) { +#if defined(WG_STAGED_PIDIV) + { // _pf kernels run only when pi != 1: the scale_rgb fold is unconditional (no pi == 1 copy of the store) + const float3 res = float3(mul_nz(vo[0], dec_scale), mul_nz(vo[1], dec_scale), mul_nz(vo[2], dec_scale)); + const float3 so = float3(m1_scale(res.x, pi_arg), m1_scale(res.y, pi_arg), m1_scale(res.z, pi_arg)); + wg_mem.Store3(out_p + o, uint3(asuint(so.x), asuint(so.y), asuint(so.z))); + } +#else + [branch] if (pi_arg == 1.0f) { wg_mem.Store3(out_p + o, uint3(asuint(vo[0] * dec_scale), asuint(vo[1] * dec_scale), asuint(vo[2] * dec_scale))); - } else { + } else { // the runtime's scale_rgb folded in (m_pf_fold = 0, or a pi whose division window is invalid) const float3 res = float3(mul_nz(vo[0], dec_scale), mul_nz(vo[1], dec_scale), mul_nz(vo[2], dec_scale)); const float3 so = float3(m1_scale(res.x, pi_arg), m1_scale(res.y, pi_arg), m1_scale(res.z, pi_arg)); wg_mem.Store3(out_p + o, uint3(asuint(so.x), asuint(so.y), asuint(so.z))); } #endif +#endif } // item it's (y - 2 p_next, xo, urel, wa, wb) (post_body's decomposition of an item index) @@ -996,8 +1036,9 @@ void pd_vsplit(int it, out int yrel, out int xo, out int urel, out uint wa, out void post_body(const bool pd_rc, const bool pd_pi1) // the direct form's body (its kernel at the end of the file) { #elif POST_OUT == 0 -void post_body(const bool p0_pi1) // form 0's body (its kernel at the end of the file) +WG_KERNEL(post_tf_f32_p12_diet_vsit) { + WG_SETUP(); #else WG_KERNEL(post_tf_p12_diet_vsit) { @@ -1011,7 +1052,7 @@ WG_KERNEL(post_tf_p12_diet_vsit) g_e = P_e; g_e_prev = P_e_prev; g_ratio = P_ratio; -#if !WG_POST_OPT +#if !WG_PO_EXPO float dec_scale = dec_scale_arg; if (expo != 0u) { // kDiet: ratio and dec_scale precomputed in the exposure pair float e0 = unif(wg_ldf(expo, 0u)), e1 = unif(wg_ldf(expo, 4u)); @@ -1056,7 +1097,7 @@ WG_KERNEL(post_tf_p12_diet_vsit) nstage = ncol * nrow < kTfCap ? ncol * nrow : kTfCap; } TfRaw raw; -#if WG_POST_OPT +#if WG_PO_STAGE // texel i = mini(lane, nstage - 1) as below, but its footprint row and clamped column are fixed per lane // for lane < nstage (opaque: computed once) and wave-uniform for the other lanes (texel nstage - 1: scalar ALU) const float inv_ncol_u = unif(inv_ncol); @@ -1081,7 +1122,7 @@ WG_KERNEL(post_tf_p12_diet_vsit) TfRaw raw2 = tf_stage2(lane, nstage, inv_ncol, r0, c0, ncol PD_A); #endif #endif -#if WG_POST_OPT +#if WG_PO_EXPO // per-launch values after the first staging loads (lev-f16lut 1a9daed: with the exposure-pair loads // first, every wave waited for them before issuing any texel load) float dec_scale = dec_scale_arg; @@ -1092,14 +1133,17 @@ WG_KERNEL(post_tf_p12_diet_vsit) g_ratio = unif(wg_ldf(expo, 12u)); dec_scale = unif(wg_ldf(expo, 16u)); } - // the output table of this frame (wg_f16lut.hlsli; color in form 3, B_unused in form 0; 0 = none), wave-uniform +#if POST_OUT == 3 + // the output table of this frame (wg_f16lut.hlsli; color: the table, 0 = none), wave-uniform #if WG_LZ_ANALYTIC const bool lut = false; // the table is indexed by an FP16 filter result (the original's); the analytic one is FP32 -#elif POST_OUT == 3 - const bool lut = WaveReadLaneFirst(f16lut_valid(color, dec_scale, nd_color_pi, nd_output_rne16) ? 1u : 0u) != 0u; #else - const bool lut = WaveReadLaneFirst(f16lut_valid(B_unused, dec_scale, p0_pi1 ? 1.0f : pi_arg, 0u) ? 1u : 0u) != 0u; + const bool lut = WaveReadLaneFirst(f16lut_valid(color, dec_scale, nd_color_pi, nd_output_rne16) ? 1u : 0u) != 0u; +#endif #endif +#endif +#if POST_OUT != 3 + const bool lut = false; #endif // this lane's D column (fixed over the walk) @@ -1117,7 +1161,7 @@ WG_KERNEL(post_tf_p12_diet_vsit) const uint2 mo_dims = uint2(0u, 0u); #endif -#if WG_POST_OPT +#if WG_PO_HH // the horizontal pass's lane roles (row hk, channel hc, cell hg) and its scratch offsets: fixed per lane const int hk_l = wl >= 30 ? 1 : 0, hrem_l = wl - 30 * hk_l, hc_l = hrem_l >= 20 ? 2 : (hrem_l >= 10 ? 1 : 0), hg_l = hrem_l - 10 * hc_l; @@ -1125,6 +1169,8 @@ WG_KERNEL(post_tf_p12_diet_vsit) const uint h_src = ((uint)hk_l * kRowDw + (uint)hc_l * 32u + (uint)hg_l * 3u) ^ g_z; const uint h_dst = ((uint)hk_l * kRowDw + (uint)hc_l * (uint)kTfOutX + (uint)hg_l * 4u) ^ g_z; const bool h_lane = wl < 60; +#endif +#if WG_PO_VV // the vertical pass's first item of each step is it = lane: its values, fixed for the walk (opaque: computed once) int v_yrel, v_xo, v_urel; uint v_wa, v_wb; @@ -1199,7 +1245,7 @@ WG_KERNEL(post_tf_p12_diet_vsit) // order): each texel's features are read from LDS once for both rows. Row y0 + 1's sums wait in gnext. Gather gk; if (k == 0) { -#if WG_POST_OPT +#if WG_PO_GATH // unrolled: the first texel starts the sums from zeros as operands (no zero moves) and the loop's // overhead goes (78 VGPRs). The colour sums start from an opaque +0 (gather_zero_opq): with a // literal 0 the driver turns fma(w, ec, +0) into w * ec (a -0 product would stay -0). @@ -1245,7 +1291,7 @@ WG_KERNEL(post_tf_p12_diet_vsit) wg_st64(hist_out, mul24((uint)y, (uint)P_dw * 8u) + (uint)x * 8u, he); // display values (FP16) of the 64 columns -> scratch row slot + k: lanes 2m, 2m + 1 share a dword const uint srow = scr + (uint)k * kRowDw; -#if WG_POST_OPT +#if WG_PO_QUAD { // the partner's two dwords (c0 | c1 << 16, c2), then the even lane's three packed dwords (the same bits) const uint nx = QuadReadAcrossX(de.x), ny = QuadReadAcrossX(de.y); const uint d0 = (de.x & 0xffffu) | (nx << 16), d1 = (de.x >> 16) | (nx & 0xffff0000u); @@ -1270,7 +1316,7 @@ WG_KERNEL(post_tf_p12_diet_vsit) #endif } // reduced history: mean of H^ over the 2 x 2 block, the partner column in lane ^ 1 -#if WG_POST_OPT +#if WG_PO_RH // two code chains for the three channels: chain a is channel 0 on the even lane and channel 1 on the odd lane // (its sum hsum_o[1] + hsum_e[1]: the even lane's hsum_e[1] + hsum_o[1] commuted, equal but for a NaN's // payload, and every NaN m gives the code of enc_arg = 1e-4), chain b channel 2; the odd lane's code a comes @@ -1306,7 +1352,7 @@ WG_KERNEL(post_tf_p12_diet_vsit) r0 = floor_i(pos_y(ys) - 0.5f) - 1; int nrow = floor_i(pos_y(yl) - 0.5f) + 2 - r0 + 1; nstage = ncol * nrow < kTfCap ? ncol * nrow : kTfCap; -#if WG_POST_OPT +#if WG_PO_STAGE raw = pd_stage_fetch(lane, nstage - 1 < 0 ? 0 : nstage - 1, st_rx, st_col, inv_ncol_u, r0, c0, ncol PD_A); #if WG_TF_TWO raw2 = pd_stage_fetch(lane2, nstage - 1 < 0 ? 0 : nstage - 1, st_rx2, st_col2, inv_ncol_u, r0, c0, ncol PD_A); @@ -1324,7 +1370,7 @@ WG_KERNEL(post_tf_p12_diet_vsit) // horizontal pass of the wave's two rows: lane (row hk, channel hc, cell hg), output columns 4 hg ... 4 hg + 3 tf_row_sync(); -#if WG_POST_OPT +#if WG_PO_HH const bool hact = and(wact, h_lane); const int hk = hk_l; #else @@ -1333,13 +1379,13 @@ WG_KERNEL(post_tf_p12_diet_vsit) const int hk = hl >= 30 ? 1 : 0, hrem = hl - 30 * hk, hc = hrem >= 20 ? 2 : (hrem >= 10 ? 1 : 0), hg = hrem - 10 * hc; #endif -#if WG_POST_OPT +#if WG_PO_HH uint hv[4]; // set and used on the hact lanes only #else uint hv[4] = { 0u, 0u, 0u, 0u }; #endif if (hact) { -#if WG_POST_OPT +#if WG_PO_HH const uint src = scr + h_src; #else const uint src = scr + (uint)hk * kRowDw + (uint)hc * 32u + (uint)hg * 3u; @@ -1402,7 +1448,7 @@ WG_KERNEL(post_tf_p12_diet_vsit) } tf_row_sync(); if (hact) { -#if WG_POST_OPT +#if WG_PO_HH const uint dst = scr + h_dst; #else const uint dst = scr + (uint)hk * kRowDw + (uint)hc * (uint)kTfOutX + (uint)hg * 4u; @@ -1421,23 +1467,15 @@ WG_KERNEL(post_tf_p12_diet_vsit) const int nitems = (p_rdy - p_next) * 2 * kTfOutX; const bool interior = and(3 * p_next - 2 >= 0, 3 * p_rdy + 1 <= P_dh - 1); const int s0 = interior ? (3 * p_next - 2 - r_first) % kRing : 0; -#if WG_POST_OPT +#if WG_PO_VV if (lane < nitems) - pd_vitem(2 * p_next + v_yrel, v_xg, v_xo, v_urel, v_wa, v_wb, p_next, interior, s0, r_first, lut, dec_scale -#if POST_OUT == 0 - , p0_pi1 -#endif - PD_A); + pd_vitem(2 * p_next + v_yrel, v_xg, v_xo, v_urel, v_wa, v_wb, p_next, interior, s0, r_first, lut, dec_scale PD_A); [loop] for (int it = lane + kThreads; it < nitems; it += kThreads) { int yrel, xo, urel; uint wa, wb; pd_vsplit(it, yrel, xo, urel, wa, wb); pd_vitem(2 * p_next + yrel, strip * kTfOutX + xo, xo, urel, wa, wb, p_next, interior, s0, r_first, lut, - dec_scale -#if POST_OUT == 0 - , p0_pi1 -#endif - PD_A); + dec_scale PD_A); } #else #if POST_OUT == 0 @@ -1545,16 +1583,4 @@ WG_KERNEL(post_tf_f32_p12_diet_vsit_direct) else if (rc) post_body(true, false); else post_body(false, false); } -#elif POST_OUT == 0 -WG_KERNEL(post_tf_f32_p12_diet_vsit) -{ - WG_SETUP(); -#if defined(WG_STAGED_PIDIV) - post_body(false); -#else - // launch-uniform: the pi == 1 copy omits m1_scale completely - [branch] if (pi_arg == 1.0f) post_body(true); - else post_body(false); -#endif -} #endif diff --git a/clean/runtime/runtime.cpp b/clean/runtime/runtime.cpp index 1c469b2d..64764eab 100644 --- a/clean/runtime/runtime.cpp +++ b/clean/runtime/runtime.cpp @@ -488,9 +488,9 @@ struct Runtime { // Staged native D3D12 route (backend 3, buffer resources) with a pre-exposure other than 1: the output scale (scale_rgb) is // done by the true-fusion post kernel's store (WG_SCALE_FOLD / option scale_fold, default on): the same bytes, one launch fewer bool scale_fold = true; - // Staged native D3D12 route (POST_OUT == 0, output_filter=original): per-frame 256 KB FP16 decode LUT (WG_M_POST_LUT, - // default on) and dedicated pi != 1 _pf kernels folding color_divide (WG_M_PF_FOLD, default on; pi == 1 DXIL untouched). - bool m_post_lut = true, m_pf_fold = true; + // Staged native D3D12 route: dedicated pi != 1 _pf kernels fold color_divide into input_encoder and post_tf + // (WG_M_PF_FOLD, default on; the pi == 1 kernels are unchanged). + bool m_pf_fold = true; bool output_quality9 = false; // private Model M final-resampling quality profile; histories/weights unchanged // output_filter=quality5: the 5-tap output filter with WrongGPU's quality5 weights. The post kernels come from // post_w64__q5.co (post.hip built with WG_LZ_PROFILE=1: the same kernels with other filter constants), so every @@ -568,7 +568,6 @@ struct Runtime { // not read a colour buffer (they read the game's texture), so it is passed as their colour pointer. DevPtr f16lut = 0; static constexpr size_t kF16LutBytes = 256 + 65536 * 12; - static constexpr size_t kF16LutDecBytes = 256 + 65536 * 4; DevPtr mnear[2] = {0, 0}, mfar = 0, mcenter = 0, latent[2] = {0, 0}, reduced = 0; DevPtr skip1 = 0, merged1 = 0, skip2 = 0, merged2 = 0, bout[6] = {0}, e2 = 0, out2 = 0, e1 = 0, out1 = 0; DevPtr A = 0, B = 0, C = 0, hist[2] = {0, 0}, display = 0, out = 0, color_net = 0; @@ -1437,7 +1436,6 @@ struct Runtime { if (k == "post_bands") post_bands = std::max(1, std::atoi(v.c_str())); if (k == "post_tf") post_tf = !(v == "0" || v == "off" || v == "false"); if (k == "scale_fold") scale_fold = !(v == "0" || v == "off" || v == "false"); - if (k == "m_post_lut") m_post_lut = !(v == "0" || v == "off" || v == "false"); if (k == "m_pf_fold") m_pf_fold = !(v == "0" || v == "off" || v == "false"); if (k == "output_filter") { if (v != "default" && v != "original" && v != "analytic" && v != "quality5" && v != "quality9") @@ -1478,7 +1476,6 @@ struct Runtime { if (const char* sr = std::getenv("WG_SWIN_RESIDENT")) swin_resident = std::max(0, std::atoi(sr)); if (const char* t = std::getenv("WG_POST_TF")) post_tf = !(std::string(t) == "0" || std::string(t) == "off"); if (const char* t = std::getenv("WG_SCALE_FOLD")) scale_fold = !(std::string(t) == "0" || std::string(t) == "off"); - if (const char* t = std::getenv("WG_M_POST_LUT")) m_post_lut = !(std::string(t) == "0" || std::string(t) == "off"); if (const char* t = std::getenv("WG_M_PF_FOLD")) m_pf_fold = !(std::string(t) == "0" || std::string(t) == "off"); // The output filter (spec 06 draft 4, spec 10 section 5 step 6): the original's weights from the model file by // default, the analytic set when the model file has none (noted for the log) or when it is selected. @@ -1828,9 +1825,7 @@ struct Runtime { (native_direct_io || (alloc(&depth, R * 4) && alloc(&motion, R * 8) && alloc(&color_net, R * 16))) && (!own_direct || alloc(&color_net, R * 16)) && alloc(&expo, 32) && - (!(native_direct_io || (d3d12_native && !model_l)) || - (alloc(&f16lut, native_direct_io ? kF16LutBytes : kF16LutDecBytes) && - (dev->memset(f16lut, 0, 256) || dfail()))) && + (!native_direct_io || (alloc(&f16lut, kF16LutBytes) && (dev->memset(f16lut, 0, 256) || dfail()))) && alloc(&expo_partials, 4 * (size_t)cdiv(ny, 4)) && alloc(&expo_computed, 4) && alloc(&expo_sync, 4) && (dev->memset(expo_sync, 0, 4) || dfail()) && alloc(&feat0, t2 * 64) && alloc(&mnear[0], R * 4) && alloc(&mnear[1], R * 4) && alloc(&mfar, R * 4) && alloc(&mcenter, R * 4) && @@ -2601,7 +2596,6 @@ struct Runtime { const bool fold_scale = scale_fold && staged_tf_any && pi != 1.0f; const bool fold_pidiv = m_pf_fold && staged_tf_any && output_original && pi != 1.0f && wgrt::native_direct_pi_window(pi).hi != 0u; - const bool use_post_lut = m_post_lut && staged_tf_any && output_original && f16lut != 0; int pidiv_bits = 0; if (fold_pidiv) std::memcpy(&pidiv_bits, &pi, 4); const int mode = io && io->exposure_mode >= 0 ? io->exposure_mode : (cfg.auto_exposure ? 2 : 0); @@ -2664,13 +2658,6 @@ struct Runtime { // the direct path's 64K tables of this frame (dx12/hlsl/wg_f16lut_in.hlsli, wg_f16lut.hlsli): one lane per FP16 pattern if (native_direct_io && !launch("f16lut_build_direct", 256, 256, {&f16lut, &expo})) return false; - // staged native D3D12 (POST_OUT == 0, output_filter=original): 256 KB FP16 decode LUT, overlapped with input_encoder - if (use_post_lut) { - float dec_scale_lut = (float)(0.01 / (fr.exposure == 0.0f ? 1.0 : (double)fr.exposure)); - float pi_lut = fold_scale ? pi : 1.0f; - if (!launch("f16lut_build_dec", 256, 256, {&f16lut, &expo, &dec_scale_lut, &pi_lut})) return false; - dev->unordered_next(); - } // 2. input encoder (spec 03) { @@ -2956,7 +2943,6 @@ struct Runtime { p.ratio = 1.0f; p.pad0 = pidiv_bits; DevPtr w = wdev["post"]; - DevPtr post_b = use_post_lut ? f16lut : (d3d12_native && !native_direct_io && hp <= 0 && !model_l ? (DevPtr)0 : B); if (post_fused) { // reconstruction + output filter in one launch, straight into the output buffer if (!fused_supported(rw, rh, dw, dh)) return fail(post_kernel + ": render size " + std::to_string(rw) + "x" + std::to_string(rh) + @@ -2989,7 +2975,7 @@ struct Runtime { " pointer is not 8-byte aligned (the D3D12 port reads it with 8-byte loads, dx12/kernels/post.hlsl)"); #endif if (!launch(("pmo/" + post_last).c_str(), cdiv(ow, 40) * nch, tf_mo4 ? 256 : 512, - with_taps({&cnet, &A, &post_b, &C, &hist[cur], &mnear[nxt], &mhi, &w, &hist[nxt], &reduced, &outp, &p, + with_taps({&cnet, &A, &B, &C, &hist[cur], &mnear[nxt], &mhi, &w, &hist[nxt], &reduced, &outp, &p, &expo, &ow_i, &oh_i, &dec_scale, &p_h, &hpitch, &cp, &k1, &c2, &mop}))) return false; // spec 10 draft 2: the filter's result times pi (folded in with the copy; direct shader stores include it) @@ -3016,7 +3002,7 @@ struct Runtime { " pointer is not 8-byte aligned (the D3D12 port reads it with 8-byte loads, dx12/kernels/post.hlsl)"); #endif if (!launch((post_pre + post_last).c_str(), cdiv(ow, 40) * nch, tf_block, - with_taps({&cnet, &A, &post_b, &C, &hist[cur], &mnear[nxt], &mcenter, &mfar, &w, &hist[nxt], &reduced, + with_taps({&cnet, &A, &B, &C, &hist[cur], &mnear[nxt], &mcenter, &mfar, &w, &hist[nxt], &reduced, &outp, &p, &expo, &ow_i, &oh_i, &dec_scale, &p_h, &hpitch, &cp, &k1, &c2}))) return false; if (!native_direct_io && hp <= 0 && pi != 1.0f && !fold_scale) { // direct / folded stores already include this multiply diff --git a/clean/tests/test_post_tf_align.py b/clean/tests/test_post_tf_align.py index 59abb64c..68656ae5 100644 --- a/clean/tests/test_post_tf_align.py +++ b/clean/tests/test_post_tf_align.py @@ -84,7 +84,7 @@ def test_runtime_wiring(self): self.assertIn("post_tf_misaligned(A, hist[cur])", before, "no check right before " + launch) self.assertRegex(before, r"return fail\(.*\n?.*is not 8-byte aligned") call = src[m.start():m.start() + 300] - self.assertIn("&cnet, &A, &post_b, &C, &hist[cur]", call, "the launch no longer passes A and hist[cur] as checked") + self.assertIn("&cnet, &A, &B, &C, &hist[cur]", call, "the launch no longer passes A and hist[cur] as checked") if __name__ == "__main__": From d2dee9f9fddd7125341c490b6665d2f7b4bacf77 Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Wed, 25 Nov 2026 09:30:54 -0500 Subject: [PATCH 5/5] Model M (D3D12): restore WG_LD_A16 on the two Swin kernels, make the history pitch visibly 8-aligned (byte-identical) RADV review of #296: whole-frame GPU time was +0.087 ms against main because 7b0f6a5 dropped ,WG_LD_A16=1 from the @wg-kernel lines of swin_block_c64_nc (+0.024 ms, 4921 -> 5441 instructions on ACO) and d1p12/swin_block_c64_add_dec0_nc (+0.061 ms, 4122 -> 4578), which #294 had added for -101.6 us on RADV. Both lines have the flag again; no other alignment define differs from main. post.hlsl history_cr_span: the 24-bit multiplicand pitch & 0xffffff becomes pitch & 0xfffff8 (pitch is a multiple of 8, so the value is the same bits), a mask with the low three bits clear, so the product with it is visibly a multiple of 8 to a compiler that reads masks and not known bits. RGA gfx1201: post_tf 76 VGPRs / 4212 instructions and post_tf_pf 77 / 5337, both unchanged (same code, one scalar and moved), still no v_and_b32 -8. --- clean/dx12/kernels/dec1_dec0.hlsl | 2 +- clean/dx12/kernels/post.hlsl | 12 ++++++++---- clean/dx12/kernels/swin_c64w2.hlsl | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/clean/dx12/kernels/dec1_dec0.hlsl b/clean/dx12/kernels/dec1_dec0.hlsl index 909782b7..e08e1709 100644 --- a/clean/dx12/kernels/dec1_dec0.hlsl +++ b/clean/dx12/kernels/dec1_dec0.hlsl @@ -21,7 +21,7 @@ // both pixels of every lane and only the stores are conditional). dec0's FP16 latent update is done in FP32 with an FP16 // rounding after each step, not in half arithmetic (the driver contracts half mul + add into v_fma_mix_f32). // Bit-identical to the HIP kernel on the synthetic frames (clean/dx12/compare.py, both cases, every buffer). -// @wg-kernel d1p12/swin_block_c64_add_dec0_nc co=dec1_dec0__pack12.co block=256 wave=32 args=p,p,p,u,u,u,u,p,p,p,p,p,p,p,b56 defines=WG_GELU_T5=0 +// @wg-kernel d1p12/swin_block_c64_add_dec0_nc co=dec1_dec0__pack12.co block=256 wave=32 args=p,p,p,u,u,u,u,p,p,p,p,p,p,p,b56 defines=WG_GELU_T5=0,WG_LD_A16=1 // // DEC0_ONLY (port d1p12/dec0_q7; Model M on the FP16-form shared stages, WG_M_FORM=q7, where dec1 runs as the q7 block // l_q7_block_c64_dec and stores its map): the same kernel without the block. The epilogue's input o[0..3] (lane (t, hh): diff --git a/clean/dx12/kernels/post.hlsl b/clean/dx12/kernels/post.hlsl index ef825060..79647f28 100644 --- a/clean/dx12/kernels/post.hlsl +++ b/clean/dx12/kernels/post.hlsl @@ -544,14 +544,18 @@ float3 history_cr_span(float qx, float qy) const float fx0 = floor(tx), fy0 = floor(ty); const int ix = (int)fx0, iy = (int)fy0; const uint pitch = (uint)P_dw * 8u; + // pitch & 0xfffff8 == pitch & 0xffffff (pitch is a multiple of 8): the same 24-bit multiplicand, but a mask whose + // low three bits are clear, so that a product with it is visibly a multiple of 8 to the driver (the Linux compiler + // otherwise cannot prove 8-byte alignment of the history addresses and splits the 16-byte loads) + const uint pitch24 = pitch & 0xfffff8u; [branch] if (WaveActiveAllTrue(and((uint)(ix - 1) <= (uint)(P_dw - 4), (uint)(iy - 1) <= (uint)(P_dh - 4)))) { // O1: hist_prev + (ix - 1) 8 + (iy - 1) pitch == ix 8 + iy pitch + (hist_prev - 8 - pitch) mod 2^32, one // lshl_add + one mad_u24 (iy in [1, P_dh - 3] and pitch = 8 P_dw < 2^24, so the 24-bit product is the full one) // (the constant opaque, g_z: the driver would otherwise split it again into its parts, one add each) #if WG_POST_CUTS - uint b0 = mul24((uint)iy, pitch) + (uint)ix * 8u + ((((hist_prev & ~7u) - 8u - pitch) ^ g_z) & ~7u); + uint b0 = mul24((uint)iy, pitch24) + (uint)ix * 8u + ((((hist_prev & ~7u) - 8u - pitch) ^ g_z) & ~7u); #else - uint b0 = (hist_prev & ~7u) + (uint)(ix - 1) * 8u + mul24((uint)(iy - 1), pitch); + uint b0 = (hist_prev & ~7u) + (uint)(ix - 1) * 8u + mul24((uint)(iy - 1), pitch24); #endif #if defined(OP_POST_MUTANT) && OP_POST_MUTANT == 4 // test-only mutant (byte check must catch it): history base +8 b0 += 8u; @@ -563,8 +567,8 @@ float3 history_cr_span(float qx, float qy) cr_weights(tx - fx0), cr_weights(ty - fy0)); } const uint hp8 = hist_prev & ~7u; - const uint ro0 = mul24((uint)clampi(iy - 1, 0, P_dh - 1), pitch), ro1 = mul24((uint)clampi(iy, 0, P_dh - 1), pitch); - const uint ro2 = mul24((uint)clampi(iy + 1, 0, P_dh - 1), pitch), ro3 = mul24((uint)clampi(iy + 2, 0, P_dh - 1), pitch); + const uint ro0 = mul24((uint)clampi(iy - 1, 0, P_dh - 1), pitch24), ro1 = mul24((uint)clampi(iy, 0, P_dh - 1), pitch24); + const uint ro2 = mul24((uint)clampi(iy + 1, 0, P_dh - 1), pitch24), ro3 = mul24((uint)clampi(iy + 2, 0, P_dh - 1), pitch24); const uint c0 = hp8 + (uint)clampi(ix - 1, 0, P_dw - 1) * 8u, c1 = hp8 + (uint)clampi(ix, 0, P_dw - 1) * 8u; const uint c2 = hp8 + (uint)clampi(ix + 1, 0, P_dw - 1) * 8u, c3 = hp8 + (uint)clampi(ix + 2, 0, P_dw - 1) * 8u; return cr_sum(wg_mem.Load2(ro0 + c1), wg_mem.Load2(ro0 + c2), wg_mem.Load2(ro1 + c0), wg_mem.Load2(ro1 + c1), diff --git a/clean/dx12/kernels/swin_c64w2.hlsl b/clean/dx12/kernels/swin_c64w2.hlsl index 4ba1a5ae..1769335a 100644 --- a/clean/dx12/kernels/swin_c64w2.hlsl +++ b/clean/dx12/kernels/swin_c64w2.hlsl @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT // swin_block_c64_nc (enc1's Swin block) with TWO windows per wave in 128-lane workgroups (tracks lev3-swin, r3-c64w2). // -// @wg-kernel swin_block_c64_nc co=swin.co block=256 group=128 gscale=1 wave=32 args=p,p,p,p,u,u,u,u defines=SW_C=64,SW_HEADS=2,SW_MLP=256,SW_ADD=0,SW_SAFE=1,SW_ECL=0,SW_W2=1 +// @wg-kernel swin_block_c64_nc co=swin.co block=256 group=128 gscale=1 wave=32 args=p,p,p,p,u,u,u,u defines=SW_C=64,SW_HEADS=2,SW_MLP=256,SW_ADD=0,SW_SAFE=1,SW_ECL=0,SW_W2=1,WG_LD_A16=1 // Model L's dec1 block swin_block_c64_add_ex_nc (swin_c64ex.hlsl) runs this kernel too (SW_W2_NAME), with the block // input x + xadd (SW_ADD) and the fused expand (SW_ECL: swin_block.hlsl's fused_expand with the 4 waves; each wave then // reads its two windows' records).