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/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..26f5fdc5 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" @@ -68,6 +72,10 @@ "input_encoder_direct", "encoder.co" ], + [ + "input_encoder_pf", + "encoder.co" + ], [ "native_driver_probe", "-" @@ -92,6 +100,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 +112,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 +180,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 +200,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" diff --git a/clean/dx12/kernels/encoder.hlsl b/clean/dx12/kernels/encoder.hlsl index a5e0fd18..744bf4a5 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; @@ -516,9 +524,15 @@ 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]; [unroll] for (uint dx2 = 0; dx2 < 2; ++dx2) { float4 c = cs[dx2]; +#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 diff --git a/clean/dx12/kernels/post.hlsl b/clean/dx12/kernels/post.hlsl index ef349ecd..79647f28 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,66 @@ #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" #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) +// 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 @@ -185,6 +239,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 +250,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 @@ -257,6 +314,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 @@ -286,10 +367,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 +386,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); @@ -321,8 +400,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) @@ -338,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 && POST_OUT != 3 +#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) { @@ -361,6 +440,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 @@ -462,31 +544,36 @@ 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 - 8u - pitch) ^ g_z)); + uint b0 = mul24((uint)iy, pitch24) + (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), pitch24); #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 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 hp8 = hist_prev & ~7u; + 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), + 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)); } @@ -499,6 +586,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))); } @@ -824,8 +914,8 @@ 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, @@ -865,6 +955,7 @@ 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 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) { @@ -874,6 +965,7 @@ void pd_vitem(int y, int xg, int xo, int urel, uint wa, uint wb, int p_next, boo native_direct_output[uint2((uint)xg, (uint)y)] = float4(res, 1.0f); return; } +#endif float vo[3], qv[3]; bool vsmall = false; [unroll] for (int c = 0; c < 3; ++c) { @@ -886,6 +978,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)); @@ -902,6 +995,24 @@ 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 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 { // 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) @@ -939,10 +1050,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_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)); @@ -987,8 +1101,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_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); const int st_r = (int)(((float)lane + 0.5f) * inv_ncol); @@ -1012,8 +1126,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_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; if (expo != 0u) { // kDiet: ratio and dec_scale precomputed in the exposure pair @@ -1023,12 +1137,17 @@ WG_KERNEL(post_tf_p12_diet_vsit) g_ratio = unif(wg_ldf(expo, 12u)); dec_scale = unif(wg_ldf(expo, 16u)); } +#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 #else 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) @@ -1046,7 +1165,7 @@ WG_KERNEL(post_tf_p12_diet_vsit) const uint2 mo_dims = uint2(0u, 0u); #endif -#if POST_OUT == 3 +#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; @@ -1054,6 +1173,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; @@ -1128,7 +1249,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_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). @@ -1174,7 +1295,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_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); @@ -1199,7 +1320,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_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 @@ -1235,7 +1356,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_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); @@ -1253,7 +1374,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_PO_HH const bool hact = and(wact, h_lane); const int hk = hk_l; #else @@ -1262,13 +1383,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_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 POST_OUT == 3 +#if WG_PO_HH const uint src = scr + h_src; #else const uint src = scr + (uint)hk * kRowDw + (uint)hc * 32u + (uint)hg * 3u; @@ -1331,7 +1452,7 @@ WG_KERNEL(post_tf_p12_diet_vsit) } tf_row_sync(); if (hact) { -#if POST_OUT == 3 +#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; @@ -1350,7 +1471,7 @@ 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_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 PD_A); [loop] for (int it = lane + kThreads; it < nitems; it += kThreads) { @@ -1424,7 +1545,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..64764eab 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" @@ -484,6 +485,12 @@ 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; + // 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 @@ -1428,6 +1435,8 @@ 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 == "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)"); @@ -1466,6 +1475,8 @@ 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"); + 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"; @@ -2544,6 +2555,50 @@ 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; + 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_; @@ -2557,16 +2612,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; @@ -2635,6 +2690,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) { @@ -2646,11 +2702,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})) @@ -2840,7 +2899,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"); @@ -2855,52 +2913,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 @@ -2929,6 +2941,7 @@ struct Runtime { p.e = e; p.e_prev = e; p.ratio = 1.0f; + p.pad0 = pidiv_bits; DevPtr w = wdev["post"]; if (post_fused) { // reconstruction + output filter in one launch, straight into the output buffer if (!fused_supported(rw, rh, dw, dh)) @@ -2950,9 +2963,11 @@ 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) + else if (fold_pidiv) post_last += "_pf"; filter_last = "fused"; #ifdef WG_DEV_DIAGNOSTICS if (const char* bad = post_tf_misaligned(A, hist[cur])) @@ -2964,7 +2979,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,19 +2992,20 @@ 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; - post_last = tf_kernel; + 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 = 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, + if (!launch((post_pre + post_last).c_str(), cdiv(ow, 40) * nch, tf_block, 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_post_tf_align.py b/clean/tests/test_post_tf_align.py index eb527835..68656ae5 100644 --- a/clean/tests/test_post_tf_align.py +++ b/clean/tests/test_post_tf_align.py @@ -77,7 +77,7 @@ 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()] 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) 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()