Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clean/FORMATS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<even C>`; reported as `post_tf=<C>` 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
Expand Down
64 changes: 64 additions & 0 deletions clean/dx12/hlsl/wg_pidiv.hlsli
Original file line number Diff line number Diff line change
@@ -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;
}
24 changes: 24 additions & 0 deletions clean/dx12/kernel_sets.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
"emo/input_encoder_mo_direct",
"encoder_mo.co"
],
[
"emo/input_encoder_mo_pf",
"encoder_mo.co"
],
[
"expand_c96",
"swin.co"
Expand Down Expand Up @@ -68,6 +72,10 @@
"input_encoder_direct",
"encoder.co"
],
[
"input_encoder_pf",
"encoder.co"
],
[
"native_driver_probe",
"-"
Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down
16 changes: 15 additions & 1 deletion clean/dx12/kernels/encoder.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading