Skip to content
Merged
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
9 changes: 5 additions & 4 deletions clean/FORMATS.md
Original file line number Diff line number Diff line change
Expand Up @@ -492,12 +492,12 @@ the runtime is a shared D3D12 object except the three fences.

| Buffer | Size | Content |
|---|---|---|
| color | max R × 16 | `[rh][rw]` 4 × f32 (alpha 0), the game's color in the active region |
| color | max R × 16 | `[rh][rw]` 4 × f32, the game's color in the active region; the fourth value +0 (model M), or for model K the texel's alpha, unmodified (1 for a format without an alpha channel): model K's output alpha follows it |
| depth | max R × 4 | `[rh][rw]` f32, the game's depth as given |
| motion | max R × 8 | `[rh][rw]` 2 × f32, m = motion_scale · raw + motion_offset (render pixels); with `WG_CREATE_MOTION_OUTPUT_RES` the texel ⌊(x + ½)·mw/rw⌋, ⌊(y + ½)·mh/rh⌋ (our point resampling: what the network reads with `motion_outres=point`; with `original` it reads `motion_hi`) |
| motion_hi | O × 4 + 8, or none | only with `WG_CREATE_MOTION_OUTPUT_RES` (model M; with `motion_outres=point` only while a frame capture is armed): `[oh][ow]` 2 × FP16, the raw motion texels of the output-size region ("Motion vectors at output resolution"). It follows `out` in each slot; without it every offset is what it was |
| expo | 256 bytes | f32 at offset 0: texel (0, 0) channel R of the exposure texture (exposure mode texture) |
| out | O × 12 | `[oh][ow]` 3 × f32, the network's result (the runtime's `filter.out` format) |
| out | O × 12 (model K: O × 16) | `[oh][ow]` 3 × f32, the network's result (the runtime's `filter.out` format); model K: `[oh][ow]` 4 × f32, the fourth value its output alpha (`FrameIo.out_rgba32f`), which the output pass stores |

Fences (shared, `hipImportExternalSemaphore`, D3D12 fence): `ready` (the split request's done fence: the front end's
queue signals v after the first part of the game's list, which ends with the input conversion), `done` (HIP signals v
Expand All @@ -524,7 +524,7 @@ whole texture; otherwise the frame uses the input pass (logged). The network's f
0.309 → 0.251 ms, output 0.856 → 0.838 ms against the FP32 slot).
- Model K's compact input pass (default for `model=K` when no copy is made; `k_input=f32`: the FP32 input pass):
where the game's colour is RGBA16F or R11G11B10 and its motion RG16F or RGBA16F (FP16-representable values), the
pass `inputs16` writes dense RGBA16F colour (rw·8-byte rows, alpha 0), R32F depth and the raw RG16F motion texel
pass `inputs16` writes dense RGBA16F colour (rw·8-byte rows, the texel's alpha kept; 1 for R11G11B10), R32F depth and the raw RG16F motion texel
(rw·4-byte rows; at output size the texel under the render pixel's centre, as the FP32 pass samples it) into the
slot's buffers, and the network reads them as it reads the copies (motion_scale / motion_offset applied on load).
Same output bits as the FP32 pass (`test_interop_d3d12 hipsame`, render- and output-size motion); logged as
Expand Down Expand Up @@ -568,7 +568,8 @@ pre-exposure and exposure, then signals `done = v`. Values only grow.
3. Network enqueued (above). If it cannot be enqueued, the runtime's own compute queue waits for the previous `done`
value and signals `done = v` (and `pipe = v`), so the game's queue cannot wait forever; the dispatch returns an error.
4. Output pass into the re-opened list: `out` of slot v (inline) or of the previous network frame (pipelined; the
first frame: bilinear upscale of the color) → the output texture (alpha 1; sRGB formats encoded), output
first frame: bilinear upscale of the color) → the output texture (alpha 1; model K: the result's fourth value, in
the upscale the bilinear of the color's alpha; sRGB formats encoded), output
transitioned to UNORDERED_ACCESS if needed and back (else a UAV barrier). Shaders: HLSL compiled at create with
`d3dcompiler_47.dll` (cs_5_0); the result buffer is read through an R32_FLOAT typed UAV.

Expand Down
8 changes: 7 additions & 1 deletion clean/dx12/build_dx12.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,12 @@ def main(argv=None):
for fn in sorted(os.listdir(KERNEL_DIR)) if os.path.isdir(KERNEL_DIR) else []:
if fn.startswith("fused_") and fn.endswith("_gen.py"):
subprocess.run([sys.executable, os.path.join(KERNEL_DIR, fn)], check=True)
# Model K's FP constants and tables (clean/k/dx12/gen_k_fp_tables.py: computed from their series, not stored in the
# repository) into <out>/dx12/gen, the K ports' second include folder; only when a K kernel can be built
k_gen = os.path.join(dst, "gen")
if (a.model_k or only) and os.path.exists(os.path.join(K_DX12_DIR, "gen_k_fp_tables.py")):
subprocess.run([sys.executable, os.path.join(K_DX12_DIR, "gen_k_fp_tables.py"),
os.path.join(k_gen, "k_fp_tables.hlsli")], check=True)
jobs = [] # (source, declaration, output path) in declaration order (sorted file name, then line)
for src, k_source in source_files(a.model_k or bool(only)):
for d in kernel_decls(src):
Expand Down Expand Up @@ -410,7 +416,7 @@ def build_one(job):
if d["group"] != d["block"]:
defs["WG_LAUNCH_BLOCK"] = d["block"]
info = {}
compile_hlsl(src, d["entry"], out, defs, includes=(K_HLSL_DIR,) if k_source and os.path.isdir(K_HLSL_DIR) else (), asm_tool=asm,
compile_hlsl(src, d["entry"], out, defs, includes=(K_HLSL_DIR, k_gen) if k_source and os.path.isdir(K_HLSL_DIR) else (), asm_tool=asm,
keep_listing=out[:-5] + ".ll" if a.listing else None, info=info)
with open(out, "rb") as f:
sha = hashlib.sha256(f.read()).hexdigest()
Expand Down
4 changes: 4 additions & 0 deletions clean/dx12/kernel_sets.json
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@
[
"k_warm",
"k_block_wide.co"
],
[
"kx/k_in_tiled",
"k_io_exact.co"
]
]
}
9 changes: 5 additions & 4 deletions clean/k/FORMATS.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Sizes: `k_pack.stage_sections(st)["_size"]` = `model_k.cpp k_stage_blob_bytes`.
| luma history L | `[2⌈H⌉₁₆][2⌈W⌉₁₆]` FP16 | `k_out_pixel` | next frame's `k_out_pixel` |
| hidden history Hh | `[Bh][Bw][4]` FP16 | complete G2 frames: `k_stage2_d0_hidden` (FP32) or `k_stage2h_d0_hidden` ("fused16"); otherwise `k_out_hidden` | next frame's `k_in_base` or `k_in_tiled` |
| game colour / motion / depth | RGBA float32 / 2 × float32 / float32, row pitch in the launch arguments, subrect bases | the front end | both stages |
| game output | `KOutParams.omode`: 0 RGBA float32 `[H][opitch][4]` (alpha Ā); 1 RGBA16F rows of `opitch` texels (RGB rounded to nearest even, alpha 1: the interop's output copy); 2 dense RGB float32 `[H][opitch][3]` (the interop's output pass) | `k_out_pixel` | the front end |
| game output | `KOutParams.omode`: 0 RGBA float32 `[H][opitch][4]` (alpha Ā; the interop's output pass for model K, `FrameIo.out_rgba32f`); 1 RGBA16F rows of `opitch` texels (RGB rounded to nearest even, alpha Ā as it is: the interop's output copy); 2 dense RGB float32 `[H][opitch][3]` (no alpha: the runtime's own test buffer). Ā: the colour input's alpha of the frame through the 4.3 bilinear (the stored tap's fourth value RN16(alpha), the four taps and FP16 weights of Y / Co / Cg, RN16(w₀a₀) then three FP16 fused multiply-adds); spatial only, no exposure, no clamp | `k_out_pixel` | the front end |
| exposure scalars `xs` | float32 [4]: κ, ρ, λ, RN(1/κ) of spec 42 section 4 / spec 43 3.7 (`KFrameIn.xs`; null = the struct's κ, ρ, λ and `KOutUni.kinv`) | `k_expo_scalars` (runtime) | all I/O kernels |
| filter table | `[2048][8]` FP16, the frame's set (spec 43 section 7), generated on the host per level | host | `k_out_pixel` |
| 2^E table | `[65536]` FP16: h(2^E) for every binary16 bit pattern E | host (once) | `k_out_pixel` |
Expand All @@ -116,7 +116,7 @@ The per-frame runtime runs `KFrame`'s frame on its HIP stream (the interop's Fra
| exposure | `exposure_auto_diet` (`exposure.co`, spec 07, HDR only, when the game asked for auto-exposure at creation or the front end asks this frame; `exposure_auto_diet_c16` on the RGBA16F copy), `k_expo_scalars` (`k_io.co`) | E_cur, E_prev, E_aux of spec 42 draft 3 section 4 (the game's value or texel as E_cur, else 1; the measured value as E_aux), then `xs` |
| preparation | `k_out_color`, then `k_in_tiled` when supported; otherwise `k_in_color` plus `k_in_base`/`k_in_reset` | `KFrameIn.flags` = [HDR] from `WgRtConfig.hdr` (front end `WG_CREATE_HDR`), always [MVlo], [inv], reset |
| network | the eleven `k_stage_*` / `k_stagew<NG>_*` launches of `k_net.py` variant "fused" | window offsets `k_stage_offsets(f, net/window_offsets)`; the G2 d0 also emits hidden history and, by default, the compact pixel head instead of the head map (`k_stage2_d0_hp`, `k_stage2h_d0_hp`) |
| output stage | `k_out_pixel_c` (compact pixel head, default) or `k_out_pixel` (runtime option `k_head=full`) | game output omode 1 (FrameIo.out_half_pitch > 0) or 2 (interop dense RGB) |
| output stage | `k_out_pixel_c` (compact pixel head, default) or `k_out_pixel` (runtime option `k_head=full`) | game output omode 1 (FrameIo.out_half_pitch > 0), 0 (FrameIo.out_rgba32f: dense RGBA f32, the interop's output pass) or 2 (dense RGB f32, tests); the direct forms write the output texture (alpha Ā in every form that has one) |

`k_expo_scalars(st, src, measured, xs, KExpoArgs)` (one lane), spec 42 draft 3 section 4 (the host rule of
`clean/k/reference/exposure.py`): [HDR] E_cur = `value` (`src_mode` 0: the front end's exposure value, 1 when the
Expand All @@ -143,8 +143,9 @@ frame, raw), `exposure.measured` (the last auto-exposure value), `k.scalars` (xs
(network output, the 40-channel head map: only with the runtime option `k_head=full`; the default frame writes the
compact pixel head instead, buffer `k.Np`, and asking for `k.N` fails with a message naming the option; `k.Np` in
turn fails with `k_head=full`),
`k.Hc`, `k.L`, `k.Hh` (the copies the next frame reads), `filter.out` (own output: dense RGB f32,
not with external I/O), `input.color`, `input.depth`, `input.motion`. Timing marks (FrameIo.timing_base,
`k.Hc`, `k.L`, `k.Hh` (the copies the next frame reads), `filter.out` (own output: dense RGB f32, or dense RGBA f32
after a frame with `FrameIo.out_rgba32f`; not with external I/O), `input.color` (the fourth value is the colour's
alpha: `wg_rt_upload_inputs_rgba`; 1 after the RGB upload `wg_rt_upload_inputs`), `input.depth`, `input.motion`. Timing marks (FrameIo.timing_base,
`wg_rt_frame_io_timed`): exposure = first kernel → `k_out_color`; input = preparation → first neural stage;
network = eleven stages (including Hh in FP32 d0) → `k_out_pixel`; output = reconstruction and any standalone
hidden pass → the final empty timing marker. Full end-to-end comparisons retain all these intervals.
Expand Down
6 changes: 5 additions & 1 deletion clean/k/dx12/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ and starts filled with its run's own sentinel (HIP `0xA5`, D3D12 `0x5A`); the de
slack must keep its sentinel; special values are among the inputs; a negative control (the port given another input
than HIP) must be caught; Model M's `scale_rgb` runs through the same replay as a control. Output: counts and verdicts.

Families: `narrow` (the narrow network stages of `k_block.co`: `k_stage_e2`, `k_stage_e0`, `k_stage_e1`, `k_stage_d2`,
Families: `input` (the exact tiled input stage `kx/k_in_tiled` of `k_io_exact.co`: 16 flag sets of HDR, low-resolution
motion, inverted depth and reset, odd and tiny base grids down to 1 x 1, the exposure scalars from a device buffer as the
runtime passes them, infinity, huge values, zero and -0 in the colour, depth, motion and histories, NaN in a third of
the cases; the raw bytes are identical in every case; the port needs the adapter's binary64 fma and division (manifest
`fp64: ext`, dropped on an adapter without them), and flushes FP32 denormals); `narrow` (the narrow network stages of `k_block.co`: `k_stage_e2`, `k_stage_e0`, `k_stage_e1`, `k_stage_d2`,
`k_stage_d1`, `k_stage2_e0`, `k_stage2_e1`, `k_stage2_d1`, `k_stage2_d0_hp`, `k_stage2_d0_hidden`) on random stage weights
and inputs, the stage runner's launch plans run on HIP and replayed on D3D12 (`k_plan_bridge.py`); the inputs of every
third case carry special values (infinity, +-65504, zero, -0, subnormals) and of every third case NaN as well; the
Expand Down
108 changes: 108 additions & 0 deletions clean/k/dx12/gen_k_fp_tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# SPDX-License-Identifier: MIT
"""Generates k_fp_tables.hlsli: the constants of model K's exact FP32 / FP64 helpers (k_fp.h) as bit
patterns for the D3D12 port (k_fp.hlsli). WrongGPU's own code.

k_fp.h builds its tables at compile time (constexpr binary64 series KfExpTab / KfLogTab) and folds constants such as
1.0 / 23.0 or (float)(1.0 / 3.0). HLSL has no constexpr evaluation in binary64 and its float literals are parsed by
DXC, so the port takes every such value as bits computed here with the same IEEE operations in the same order (Python
floats are binary64 with round to nearest even; numpy.float32 rounds binary64 to binary32 to nearest even, as the C
casts do). clean/dx12/build_dx12.py runs it into <out>/dx12/gen (an include folder of the K ports), so the tables are
computed at every build and never stored in the repository: python clean/k/dx12/gen_k_fp_tables.py <output header>
"""
import os
import struct
import sys

import numpy as np



def f32(v):
return struct.unpack("<I", np.float32(v).tobytes())[0]


def f64(v):
b = struct.unpack("<Q", struct.pack("<d", v))[0]
return b & 0xFFFFFFFF, b >> 32


def kc_exp(x): # k_fp.h kc_exp, |x| <= 0.4
t, s = 1.0, 1.0
for k in range(1, 30):
t = t * x / k
s = s + t
return s


def kc_log(x): # k_fp.h kc_log, x in [0.5, 2]
s = (x - 1.0) / (x + 1.0)
s2 = s * s
t, r = s, 0.0
for k in range(40):
r = r + t / (2 * k + 1)
t = t * s2
return 2.0 * r


KC_LN2 = 0.69314718055994530942


def main(out_path):
exp_h, exp_l = [], []
for i in range(65): # KfExpTab: 2^(j/64), j = -32 .. 32
v = kc_exp((i - 32) / 64.0 * KC_LN2)
h = float(np.float32(v))
exp_h.append(f32(h))
exp_l.append(f32(v - h))
log_inv, log_h, log_l = [], [], []
for i in range(97): # KfLogTab: c_j = 1 + j/128, inv_j = RN32(1/c_j), -log2(inv_j) as hi + lo
c = 1.0 + (i - 32) / 128.0
inv = float(np.float32(1.0 / c))
log_inv.append(f32(inv))
v = -kc_log(inv) / KC_LN2
h = float(np.float32(v))
log_h.append(f32(h))
log_l.append(f32(v - h))

def arr(name, vals):
rows = [", ".join(f"0x{v:08X}u" for v in vals[i:i + 8]) for i in range(0, len(vals), 8)]
return f"static const uint {name}[{len(vals)}] = {{\n " + ",\n ".join(rows) + "\n};\n"

def dconst(name, v):
lo, hi = f64(v)
return f"#define {name} asdouble(0x{lo:08X}u, 0x{hi:08X}u) // {v!r}\n"

def fconst(name, v):
return f"#define {name} asfloat(0x{f32(v):08X}u) // {float(np.float32(v))!r}\n"

third = 1.0 / 3.0
third_h = float(np.float32(third))
inv_ln2 = 1.4426950408889634074
ln2 = 0.69314718055994530942
out = ["// Generated by clean/k/dx12/gen_k_fp_tables.py at build time: the constants of k_fp.h as bits. WrongGPU's own code.\n",
"#pragma once\n\n",
"// KfExpTab: 2^(j/64), j = -32 .. 32, hi + lo\n", arr("KF_EXP_H", exp_h), arr("KF_EXP_L", exp_l),
"// KfLogTab: inv_j = RN32(1 / (1 + j/128)), -log2(inv_j) as hi + lo, j = -32 .. 64\n",
arr("KF_LOG_INV", log_inv), arr("KF_LOG_H", log_h), arr("KF_LOG_L", log_l), "\n",
"// binary64 constants of kd_log_core / kd_log / kd_log2 / kd_exp2 (k_fp.h; folded there at compile time)\n",
dconst("KD_LN2", 0.6931471805599453094), dconst("KD_INV_LN2", 1.4426950408889634074),
dconst("KD_SQRT2", 1.4142135623730951), dconst("KD_INV23", 1.0 / 23.0)]
out.append("// 1 / (2k + 1), k = 0 .. 10 (kd_log_core's series)\n")
out.append("static const uint2 KD_INV_ODD[11] = {\n " + ",\n ".join(
"uint2(0x%08Xu, 0x%08Xu)" % f64(1.0 / (2 * k + 1)) for k in range(11)) + "\n};\n")
out.append("// 1 / k, k = 0 .. 16 (kd_exp2's series; entry 0 unused)\n")
out.append("static const uint2 KD_INV_K[17] = {\n uint2(0u, 0u),\n " + ",\n ".join(
"uint2(0x%08Xu, 0x%08Xu)" % f64(1.0 / k) for k in range(1, 17)) + "\n};\n\n")
out.append("// binary32 constants of kf2_log2 / kf2_exp2 / kf_ln (k_fp.h)\n")
out += [fconst("KF_THIRD_H", third), fconst("KF_THIRD_L", third - third_h),
fconst("KF_SIXTH", np.float32(1.0) / np.float32(6.0)), fconst("KF_1_120", np.float32(1.0) / np.float32(120.0)),
fconst("KF_1_24", np.float32(1.0) / np.float32(24.0)), fconst("KF_1_64", np.float32(1.0) / np.float32(64.0)),
fconst("KF2_INV_LN2_H", inv_ln2), fconst("KF2_INV_LN2_L", inv_ln2 - float(np.float32(inv_ln2))),
fconst("KF2_LN2_H", ln2), fconst("KF2_LN2_L", ln2 - float(np.float32(ln2)))]
os.makedirs(os.path.dirname(os.path.abspath(out_path)), exist_ok=True)
with open(out_path, "w", newline="\n") as f:
f.write("".join(out))


if __name__ == "__main__":
main(sys.argv[1])
Loading
Loading