Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
bcedb06
Frame generation (D3D12): tiled/fused pre-net2, batched FP16 conversi…
phantoslayer21 Nov 24, 2026
a8150db
FG proxy tests: use static_assert for constant gate checks (fixes MSV…
phantoslayer21 Nov 24, 2026
4cf4086
FG swapchain hook: silence MSVC /W4 /WX C4127 on mutant 6
phantoslayer21 Nov 24, 2026
bcf27ef
Frame generation (D3D12): the output kernels write the presenter slot…
phantoslayer21 Nov 24, 2026
426a434
Frame generation (D3D12): the Full blend writes net2's merged input i…
phantoslayer21 Nov 24, 2026
ff06d00
Frame generation (D3D12): Full blend converts NaN lanes as before; Di…
phantoslayer21 Nov 24, 2026
d894510
Frame generation (D3D12): per-launch profile and multi-queue timing, …
phantoslayer21 Nov 24, 2026
153d351
Frame generation (D3D12): fused and retiled kernels (fine pass + hole…
phantoslayer21 Nov 24, 2026
92c5c2f
Frame generation (D3D12): the benchmark takes --fuse 0|1 beside the o…
phantoslayer21 Nov 24, 2026
55923d5
Frame generation (D3D12): Wave 1B fusions (net1 cells in LDS, net2 re…
phantoslayer21 Nov 24, 2026
1b7deb7
Frame generation (D3D12): one arena alias and one driver-channel buff…
phantoslayer21 Nov 24, 2026
9188c88
Frame generation (D3D12): Wave 1B defaults to the two levers that win…
phantoslayer21 Nov 25, 2026
133e87e
Frame generation (D3D12): Wave 2 levers A15-A18, all bit-exact and sw…
phantoslayer21 Nov 25, 2026
2be02a0
Frame generation (D3D12): the default fuse2 mask is 593 (17 + A15 fil…
phantoslayer21 Nov 25, 2026
4bef024
Frame generation (D3D12): CI follow-up - Wave 2 test file in LF, K so…
phantoslayer21 Nov 25, 2026
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
14 changes: 14 additions & 0 deletions clean/dx12/hlsl/fg_fp16.hlsli
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: MIT
#pragma once

static uint fg_fp16_zero;
#define FG_FP16_SETUP() (WG_SETUP(), fg_fp16_zero = wg_grid_ >> 31)

// Driver-channel FP32 to FP16 RNE. The opaque zero keeps the driver from folding the producing
// FP32 operation into a mixed-precision instruction and losing its separate FP32 rounding.
uint fg_f2h(float value)
{
return wg_ags_call(0x1cu, 0u, 1u, asuint(value) ^ fg_fp16_zero, 0u) & 0xffffu;
}

uint fg_pack_h2(float x, float y) { return fg_f2h(x) | (fg_f2h(y) << 16); }
79 changes: 71 additions & 8 deletions clean/dx12/hlsl/fg_full.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// fg_out_pull.hlsl, fg_out_push.hlsl, fg_distortion.hlsl), with the bits of clean/fg/reference/stages_full.py.
//
// - Images live in the arena, row-major: 4 x float16 = 8 bytes per texel (r | g << 16, b | a << 16), 2 x float16 = 4
// bytes, 1 x float16 = 2 bytes, 2 x float32 = 8 bytes; float16 is written with wg_f2h (nearest even).
// bytes, 1 x float16 = 2 bytes, 2 x float32 = 8 bytes; float16 is written with fg_f2h (nearest even).
// - The reference's approximate division is wg_div (IEEE); float -> int is (int) (truncate, saturate, NaN -> 0).
// - Bilinear reads by hand (fgf_tap / fgf_mix): i = floor(t), f = t - i, taps i and i + 1 clamped to the image, weights
// (1 - fx)(1 - fy), fx (1 - fy), fy (1 - fx), fx fy, value fma(w11, c11, fma(w01, c01, fma(w00, c00, w10 c10))).
Expand All @@ -13,16 +13,41 @@
// 1, 2, 4, ... sums pairs in x, then y, at every scale (the reference's block_sums order).
// The including file includes wg_platform.hlsli first and starts its kernel with FGF_SETUP().
#pragma once
#include "fg_fp16.hlsli"

static uint fgf_z_; // an opaque zero: wg_grid_ >> 31 (a grid never has 2^31 workgroups)
#define FGF_SETUP() (WG_SETUP(), fgf_z_ = wg_grid_ >> 31)
#define FGF_SETUP() (FG_FP16_SETUP(), fgf_z_ = wg_grid_ >> 31)

float fgf_opq(float x) { return asfloat(asuint(x) ^ fgf_z_); }
float3 fgf_opq3(float3 x) { return asfloat(asuint(x) ^ fgf_z_); }
float fgf_zero() { return asfloat(fgf_z_); }
float fgf_sat(float x) { return min(max(x, 0.0f), 1.0f); }
float3 fgf_sat3(float3 x) { return min(max(x, 0.0f), 1.0f); }

// The refinement in wg_div without exponent normalization. Use it for a positive
// normal divisor and finite operands in the range needed by the Full path.
float fgf_div_reciprocal(float b)
{
const float y = rcp(b);
return mad(mad(-b, y, 1.0f), y, y);
}

float3 fgf_div_normal3(float3 a, float b, float y)
{
float3 q = a * y;
q = mad(mad(-b, q, a), y, q);
return mad(mad(-b, q, a), y, q);
}

float fgf_pixel_centre(int i, int n)
{
const float a = (float)i + 0.5f, b = (float)n;
const float y = fgf_div_reciprocal(b);
float q = a * y;
q = mad(mad(-b, q, a), y, q);
return fgf_sat(mad(mad(-b, q, a), y, q));
}

// sat((i + 0.5) / n): the normalized centre of pixel i of an n-pixel axis
float fgf_centre(int i, int n) { return fgf_sat(wg_div((float)i + 0.5f, (float)n)); }

Expand All @@ -40,9 +65,23 @@ float4 fgf_ld4h(uint p, int i)
}
float2 fgf_ld2h(uint p, int i) { const uint d = wg_ld32(p, (uint)i * 4u); return float2(wg_h2f_lo(d), wg_h2f_hi(d)); }
float fgf_ld1h(uint p, int i) { return wg_h2f(wg_ld16(p, (uint)i * 2u)); }
void fgf_st4h(uint p, int i, float4 v) { wg_st64(p, (uint)i * 8u, uint2(wg_pack_h2(v.x, v.y), wg_pack_h2(v.z, v.w))); }
void fgf_st2h(uint p, int i, float2 v) { wg_st32(p, (uint)i * 4u, wg_pack_h2(v.x, v.y)); }
void fgf_st1h(uint p, int i, float v) { wg_st16(p, (uint)i * 2u, wg_f2h(v)); }
void fgf_st4h(uint p, int i, float4 v) { wg_st64(p, (uint)i * 8u, uint2(fg_pack_h2(v.x, v.y), fg_pack_h2(v.z, v.w))); }
void fgf_st2h(uint p, int i, float2 v) { wg_st32(p, (uint)i * 4u, fg_pack_h2(v.x, v.y)); }
void fgf_st1h(uint p, int i, float v) { wg_st16(p, (uint)i * 2u, fg_f2h(v)); }

// Adjacent Morton lanes cover an aligned horizontal pair when the row pitch is even.
// Call with the full wave active, including lanes outside the frame.
void fgf_store_morton_flag(uint p, int x, int y, int w, int h, bool flag)
{
const uint bits = flag ? 0x3c00u : 0u;
if ((w & 1) == 0 && (p & 3u) == 0u) {
const uint other = QuadReadAcrossX(bits);
if ((x & 1) == 0 && x < w && y < h)
wg_st32(p, (uint)(y * w + x) * 2u, bits | (other << 16));
} else if (x < w && y < h) {
wg_st16(p, (uint)(y * w + x) * 2u, bits);
}
}

// ---- bilinear reads ---------------------------------------------------------------------------------------------------
struct FgfTap { int i00, i10, i01, i11; float w00, w10, w01, w11; };
Expand All @@ -53,9 +92,9 @@ FgfTap fgf_tap(float tx, float ty, int W, int H)
FgfTap t;
const float x0f = floor(tx), y0f = floor(ty);
const float fx = tx - x0f, fy = ty - y0f;
const int xi = (int)x0f, yi = (int)y0f;
const int x0 = clamp(xi, 0, W - 1), x1 = clamp(xi, -1, W - 2) + 1;
const int y0 = clamp(yi, 0, H - 1), y1 = clamp(yi, -1, H - 2) + 1;
const int xi = clamp((int)x0f, -1, W - 1), yi = clamp((int)y0f, -1, H - 1);
const int x0 = max(xi, 0), x1 = min(xi + 1, W - 1);
const int y0 = max(yi, 0), y1 = min(yi + 1, H - 1);
t.i00 = y0 * W + x0; t.i10 = y0 * W + x1; t.i01 = y1 * W + x0; t.i11 = y1 * W + x1;
const float gx = 1.0f - fx, gy = 1.0f - fy;
t.w00 = fgf_opq(gx * gy);
Expand All @@ -77,8 +116,32 @@ float2 fgf_mix2(FgfTap t, float2 c00, float2 c10, float2 c01, float2 c11)
return mad(t.w11, c11, mad(t.w01, c01, mad(t.w00, c00, t.w10 * c10)));
}

#ifndef FG_BIL128
#define FG_BIL128 0
#endif
#if FG_BIL128
// A16: the two texels of a tap row are adjacent (i10 == i00 + 1) or the same texel (i10 == i00: the frame's left / right
// border clamps x1 to x0), so one load at i00 covers both: 16 bytes for 4 x float16 texels (8 for 2 x float16), the second
// texel taken from it unless the border made it the first. The same four texel values reach fgf_mix4 / fgf_mix2, so the
// result is bit for bit the 4-load version's. (Reads one texel past i00 at the frame's last texel: robust buffer access.)
float4 fgf_unpack4h(uint2 d) { return float4(wg_h2f_lo(d.x), wg_h2f_hi(d.x), wg_h2f_lo(d.y), wg_h2f_hi(d.y)); }
float4 fgf_bil4h(uint p, FgfTap t)
{
const uint4 a = wg_ld128(p, (uint)t.i00 * 8u), b = wg_ld128(p, (uint)t.i01 * 8u);
const uint2 c10 = select(t.i10 == t.i00, a.xy, a.zw), c11 = select(t.i11 == t.i01, b.xy, b.zw);
return fgf_mix4(t, fgf_unpack4h(a.xy), fgf_unpack4h(c10), fgf_unpack4h(b.xy), fgf_unpack4h(c11));
}
float2 fgf_unpack2h(uint d) { return float2(wg_h2f_lo(d), wg_h2f_hi(d)); }
float2 fgf_bil2h(uint p, FgfTap t)
{
const uint2 a = wg_ld64(p, (uint)t.i00 * 4u), b = wg_ld64(p, (uint)t.i01 * 4u);
const uint c10 = select(t.i10 == t.i00, a.x, a.y), c11 = select(t.i11 == t.i01, b.x, b.y);
return fgf_mix2(t, fgf_unpack2h(a.x), fgf_unpack2h(c10), fgf_unpack2h(b.x), fgf_unpack2h(c11));
}
#else
float4 fgf_bil4h(uint p, FgfTap t) { return fgf_mix4(t, fgf_ld4h(p, t.i00), fgf_ld4h(p, t.i10), fgf_ld4h(p, t.i01), fgf_ld4h(p, t.i11)); }
float2 fgf_bil2h(uint p, FgfTap t) { return fgf_mix2(t, fgf_ld2h(p, t.i00), fgf_ld2h(p, t.i10), fgf_ld2h(p, t.i01), fgf_ld2h(p, t.i11)); }
#endif

// ---- output pyramid -----------------------------------------------------------------------------------------------------
// (sum r, sum g, sum b, sum w) -> (r / max(sum w, 1e-12), ..., sum w / n)
Expand Down
30 changes: 30 additions & 0 deletions clean/dx12/hlsl/fg_full_up.hlsli
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-License-Identifier: MIT
// Frame generation (issue #96): the output pyramid's 9/3/3/1 upsample on four already-loaded taps, for the kernels whose taps
// do not come from the arena (fg_out_push.hlsl FGU_TAIL: LDS; FGU_L1: texels pushed on the fly). It is the second half of
// fgf_up (fg_full.hlsli), line for line: clean/fg/tests/test_fg_turbo_v2.py compares the two texts. Include after
// fg_full.hlsli.
#pragma once

// fgf_up's taps: the element indices (iP, iX, iY, iD) of the coarse level (wc x hc) around fine element (qx, qy)
void fgf_up_idx(int wc, int hc, int qx, int qy, out int iP, out int iX, out int iY, out int iD)
{
const int px = qx >> 1, py = qy >> 1;
const int nx = clamp(px + ((qx & 1) != 0 ? 1 : -1), 0, wc - 1);
const int ny = clamp(py + ((qy & 1) != 0 ? 1 : -1), 0, hc - 1);
iP = py * wc + px;
iX = py * wc + nx;
iY = ny * wc + px;
iD = ny * wc + nx;
}

// fgf_up's arithmetic from the four taps' float values (eP: own column and row, eX: neighbour column, eY: neighbour row,
// eD: both)
float4 fgf_up_taps(float4 eP, float4 eX, float4 eY, float4 eD)
{
const float z = fgf_zero();
const float ws = mad(eD.w, 0.0625f, mad(eY.w, 0.1875f, mad(eX.w, 0.1875f, mad(eP.w, 0.5625f, z))));
const float3 vs = mad(fgf_opq3(eD.xyz * eD.w), 0.0625f, mad(fgf_opq3(eY.xyz * eY.w), 0.1875f,
mad(fgf_opq3(eX.xyz * eX.w), 0.1875f, mad(fgf_opq3(eP.xyz * eP.w), 0.5625f, z))));
const float den = min(max(ws, 1e-12f), 1.0f);
return float4(wg_div(vs.x, den), wg_div(vs.y, den), wg_div(vs.z, den), ws);
}
59 changes: 52 additions & 7 deletions clean/dx12/hlsl/fg_lite_common.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
// The driver's compiler regroups chains of operations with constant or uniform terms (clean/dx12/README.md "Driver
// exceptions"); fg_opq() passes a value through an integer xor with a zero it cannot see, which cuts such a chain.
#pragma once
#include "fg_fp16.hlsli"

#ifndef FG_BIL128
#define FG_BIL128 0
#endif
static uint fg_z_; // 0 at run time (the grid size is below 2^31), unknown to the compiler
#define FG_SETUP() (WG_SETUP(), fg_z_ = wg_grid_ >> 31)
#define FG_SETUP() (FG_FP16_SETUP(), fg_z_ = wg_grid_ >> 31)

float fg_opq(float x) { return asfloat(asuint(x) ^ fg_z_); }
// a * b exactly as one rounded product, as an fma with an opaque -0: neither regrouped nor fused
Expand All @@ -39,6 +43,18 @@ float fg_sq(float x, float y) { return mad(x, x, fg_mul(y, y)); }
// u = sat((i + 0.5) / n), approximate division
float fg_centre(uint i, uint n) { return fg_sat(fg_mul((float)i + 0.5f, fg_rcp((float)n))); }

// Remap the existing 256-thread linear dispatch when the frame fits 32 x 8 tiles.
uint2 fg_lite_tile_pixel(uint idx, uint w, uint h)
{
if ((w & 31u) == 0u && (h & 7u) == 0u) {
const uint tile = wg_block_id(), tiles_x = w >> 5u, lane = wg_lane_id();
const uint ty = tile / tiles_x;
return uint2(((tile - ty * tiles_x) << 5u) + (lane & 31u), (ty << 3u) + (lane >> 5u));
}
const uint y = idx / w;
return uint2(idx - y * w, y);
}

// 2^y: the coefficients are stages_common.EX2_COEFFS (this project's least-squares fit of 2^f on [0, 1])
float fg_ex2(float y)
{
Expand Down Expand Up @@ -90,17 +106,17 @@ float4 fg_texel(uint base, uint kind, uint idx)
return float4(asfloat(wg_ld32(base, idx * 4u)), 0.0f, 0.0f, 0.0f);
}

void fg_st_h1(uint base, uint idx, float a) { wg_st16(base, idx * 2u, wg_f2h(a)); }
void fg_st_h2(uint base, uint idx, float a, float b) { wg_st32(base, idx * 4u, wg_f2h(a) | (wg_f2h(b) << 16)); }
void fg_st_h1(uint base, uint idx, float a) { wg_st16(base, idx * 2u, fg_f2h(a)); }
void fg_st_h2(uint base, uint idx, float a, float b) { wg_st32(base, idx * 4u, fg_pack_h2(a, b)); }
void fg_st_h3(uint base, uint idx, float3 a)
{
wg_st16(base, idx * 6u, wg_f2h(a.x));
wg_st16(base, idx * 6u + 2u, wg_f2h(a.y));
wg_st16(base, idx * 6u + 4u, wg_f2h(a.z));
wg_st16(base, idx * 6u, fg_f2h(a.x));
wg_st16(base, idx * 6u + 2u, fg_f2h(a.y));
wg_st16(base, idx * 6u + 4u, fg_f2h(a.z));
}
void fg_st_h4(uint base, uint idx, float4 a)
{
wg_st64(base, idx * 8u, uint2(wg_f2h(a.x) | (wg_f2h(a.y) << 16), wg_f2h(a.z) | (wg_f2h(a.w) << 16)));
wg_st64(base, idx * 8u, uint2(fg_pack_h2(a.x, a.y), fg_pack_h2(a.z, a.w)));
}

// integer read at (x, y), clamped
Expand All @@ -123,8 +139,37 @@ float4 fg_read_linear(uint base, uint kind, int w, int h, float X, float Y)
float ax = px - fx, ay = py - fy;
int rx = clamp((int)fx, -1, w - 1), ry = clamp((int)fy, -1, h - 1);
int x0 = max(rx, 0), x1 = min(rx + 1, w - 1), y0 = max(ry, 0), y1 = min(ry + 1, h - 1);
#if FG_BIL128
// A16 (kinds 4 and 2): x1 is x0 + 1 or x0 itself (the border clamp), so one load per row covers both texels
// (16 bytes for float16 x 4, 8 for float16 x 2) and the same four texel values reach the blend below.
float4 t00, t10, t01, t11;
if (kind == 4u)
{
const uint4 a = wg_ld128(base, (uint)(y0 * w + x0) * 8u), b = wg_ld128(base, (uint)(y1 * w + x0) * 8u);
const uint2 a1 = select(x1 == x0, a.xy, a.zw), b1 = select(x1 == x0, b.xy, b.zw);
t00 = float4(f16tof32(a.x), f16tof32(a.x >> 16), f16tof32(a.y), f16tof32(a.y >> 16));
t10 = float4(f16tof32(a1.x), f16tof32(a1.x >> 16), f16tof32(a1.y), f16tof32(a1.y >> 16));
t01 = float4(f16tof32(b.x), f16tof32(b.x >> 16), f16tof32(b.y), f16tof32(b.y >> 16));
t11 = float4(f16tof32(b1.x), f16tof32(b1.x >> 16), f16tof32(b1.y), f16tof32(b1.y >> 16));
}
else if (kind == 2u)
{
const uint2 a = wg_ld64(base, (uint)(y0 * w + x0) * 4u), b = wg_ld64(base, (uint)(y1 * w + x0) * 4u);
const uint a1 = select(x1 == x0, a.x, a.y), b1 = select(x1 == x0, b.x, b.y);
t00 = float4(f16tof32(a.x), f16tof32(a.x >> 16), 0.0f, 0.0f);
t10 = float4(f16tof32(a1), f16tof32(a1 >> 16), 0.0f, 0.0f);
t01 = float4(f16tof32(b.x), f16tof32(b.x >> 16), 0.0f, 0.0f);
t11 = float4(f16tof32(b1), f16tof32(b1 >> 16), 0.0f, 0.0f);
}
else
{
t00 = fg_texel(base, kind, (uint)(y0 * w + x0)); t10 = fg_texel(base, kind, (uint)(y0 * w + x1));
t01 = fg_texel(base, kind, (uint)(y1 * w + x0)); t11 = fg_texel(base, kind, (uint)(y1 * w + x1));
}
#else
float4 t00 = fg_texel(base, kind, (uint)(y0 * w + x0)), t10 = fg_texel(base, kind, (uint)(y0 * w + x1));
float4 t01 = fg_texel(base, kind, (uint)(y1 * w + x0)), t11 = fg_texel(base, kind, (uint)(y1 * w + x1));
#endif
float4 r0 = mad(ax.xxxx, t10 - t00, t00);
float4 r1 = mad(ax.xxxx, t11 - t01, t01);
return mad(ay.xxxx, r1 - r0, r0);
Expand Down
51 changes: 51 additions & 0 deletions clean/dx12/hlsl/fg_net1_pixel.hlsli
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-License-Identifier: MIT
// Frame generation (issue #96): the ten cell values of one net1 image pixel, for fg_mconv2_mid_cells (which builds net1_01's
// staged cells in LDS, fg_mconv.hlsl FG_CELLS). It is fg_lite_net1_input's lane work (fg_lite_net1io.hlsl FG_K == 3, left as
// it was so that its DXIL stays byte-identical), line for line: clean/fg/tests/test_fg_turbo_v2.py compares the two
// texts. The including file defines the argument block
// (ld, dc, dp, fin, mv, eimg, hc, hp, qp, mimg, qc, om: arena offsets; wd, hd, w1, h1: the net1 grid and the scatter grid)
// and includes wg_platform.hlsli and fg_lite_common.hlsli first.
#pragma once

float fg_warp(float3 a, float3 b)
{
const float3 d = abs(a - b);
return fg_sat(fg_adiv(d.z + fg_opq(d.x + d.y), 3.0f));
}

// The values of image pixel (x, y), all zero when not active or outside the image (x in [0, wd), y in [0, hd)). With store_side the pixel
// also writes its Qc = (v6, v2) and Om = sat((E Hp) Hc) texels.
void fg_net1_pixel(int x, int y, bool active, bool store_side, out float vals[10])
{
[unroll] for (int i = 0; i < 10; ++i) vals[i] = 0.0f;
if (active && x >= 0 && x < (int)wd && y >= 0 && y < (int)hd)
{
const uint pi = (uint)y * wd + (uint)x;
const float u = fg_centre((uint)x, wd), v = fg_centre((uint)y, hd);
const float4 dcv = fg_texel(dc, 4u, pi), dpv = fg_texel(dp, 4u, pi);
const float4 f = fg_read_nearest(fin, 2u, (int)w1, (int)h1, u, v);
const float su = u + fg_adiv(f.x, (float)w1), sv = v + fg_adiv(f.y, (float)h1);
const float v2 = fg_warp(fg_read_linear(dc, 4u, (int)wd, (int)hd, su, sv).xyz, dpv.xyz);
const float4 m = fg_read_nearest(mv, 5u, (int)w1, (int)h1, u, v);
const float v6 = fg_warp(fg_read_linear(dp, 4u, (int)wd, (int)hd, u + m.x, v + m.y).xyz, dcv.xyz);
const float e = fg_read_nearest(eimg, 1u, (int)w1, (int)h1, u, v).x;
const float hcv = fg_read_nearest(hc, 1u, (int)wd, (int)hd, u, v).x;
const float hpv = fg_read_nearest(hp, 1u, (int)wd, (int)hd, u, v).x;
const float mm = fg_texel(mimg, 1u, pi).x;
vals[0] = fg_texel(ld, 1u, pi).x;
vals[1] = fg_luma(dpv.xyz);
vals[2] = v2;
vals[3] = fg_texel(qp, 2u, pi).x;
vals[4] = e;
vals[5] = fg_luma(dcv.xyz);
vals[6] = v6;
vals[7] = hcv;
vals[8] = mm;
vals[9] = 1.0f - mm;
if (store_side)
{
fg_st_h2(qc, pi, v6, v2);
fg_st_h1(om, pi, fg_sat(fg_mul(fg_mul(e, hpv), hcv)));
}
}
}
Loading
Loading