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
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
52 changes: 39 additions & 13 deletions clean/k/kernels/k_io.hip
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@ enum { KIO_HDR = 1, KIO_MVLO = 2, KIO_INV = 4, KIO_RESET = 8, KIO_C16 = 16, KIO_
// pitch mpitch texels) holding the game's raw values r, the motion is m = ms r + mo (FP32 multiply, then add: the
// interop's conversion, clean/kernels/frame.hip input_convert). Both read the interop's input copies (or its compact
// input pass) directly; the values equal those of the FP32 inputs the conversion would have written.
// Game-output formats of k_out_pixel (KOutParams.omode): RGBA float32 [H][opitch] (the spec's output, tests); RGBA16F
// rows of opitch texels with RGB rounded to nearest even and alpha 1 (the interop's output copy); dense RGB float32
// [H][opitch] (the interop's output pass, which writes alpha 1 itself).
// Game-output formats of k_out_pixel (KOutParams.omode): RGBA float32 [H][opitch] (the spec's output, tests, and the
// interop's output pass for model K: FrameIo.out_rgba32f); RGBA16F rows of opitch texels with RGB rounded to nearest
// even (the interop's output copy); dense RGB float32 [H][opitch] (the runtime's own test buffer: no fourth value).
// Game output alpha (issue #26): the colour input's alpha of the current frame, spatial only: cbar[3], the fourth
// channel of the 4.3 bilinear (the stored tap's A = RN16 of the colour texel's fourth value as the game supplies it, the
// four taps and FP16 weights of Y / Co / Cg, RN16(w0 a0) then three FP16 fused multiply-adds). No history, nothing
// from the network, no exposure, no clamp. RGBA16F rows store it as it is, RGBA float32 as its exact widening. The
// D3D12 ports (k_out.hlsl) write the same.
enum { KIO_OUT_RGBA32F = 0, KIO_OUT_RGBA16F = 1, KIO_OUT_RGB32F = 2 };

// Frame inputs shared by both stages (must match KFrameIn in clean/k/runner/k_io.py).
Expand Down Expand Up @@ -79,7 +84,7 @@ struct KOutParams {
h16* Hcout;
h16* Hhout; // [Bh][Bw][4]
int opitch;
int omode; // KIO_OUT_RGBA32F (default), KIO_OUT_RGBA16F (alpha 1), KIO_OUT_RGB32F (runtime interop)
int omode; // KIO_OUT_RGBA32F (default), KIO_OUT_RGBA16F (both: alpha cbar[3]), KIO_OUT_RGB32F (no alpha)
h16* ctap; // k_out_color -> k_out_pixel: stored tap (Y, Co, Cg, A) of spec 43 4.2 per render pixel [rh][rw][4]
h16* ctapT; // the same: filter-tap value T of spec 43 5.3 per render pixel [rh][rw]
KOutUni u; // frame-uniform values (host)
Expand Down Expand Up @@ -153,12 +158,18 @@ WG_FN v4f kio_texel(const KFrameIn& F, int x, int y) {
WG_FN v4f kio_color(const KFrameIn& F, int ix, int iy) {
return kio_texel(F, kio_tap(ix, 0, F.cbx, F.rw), kio_tap(iy, 0, F.cby, F.rh));
}
// motion texel (x, y) of the texture (subrect base included by the caller): FP32 pairs, or KIO_M16's ms r + mo
// motion texel (x, y) of the texture (subrect base included by the caller): FP32 pairs, or KIO_M16's raw FP16 value r
// KIO_M16 (the interop's raw-buffer route: input copies or model K's compact input pass, ms / mo in KFrameIn rmsx ..,
// WgRtFrame's scale 1): the raw FP16 value r, the scale folded into the motion factor and the offset RN(mo / ms) in raw
// units, as for the direct forms (issue #26; before: ms r + mo per texel, then the factor of scale 1).
// kio_m16_sp / kio_m16_mo: the factor with the scale folded in (the host's expression with s = sx ms) and the offset.
WG_FN float kio_m16_sp(float s, float ms, float den) { return (s * ms) / den; }
WG_FN float kio_m16_mo(float o, float mo, float ms) { return mo == 0.0f ? o : o + mo / ms; }
WG_FN void kio_motion(const KFrameIn& F, int x, int y, float* mvx, float* mvy) {
if (F.flags & KIO_M16) {
const h16* r = kio_ptr((const h16*)F.motion, ((u32)y * F.mpitch + x) * (u32)F.mch);
*mvx = F.rmsx * (float)r[0] + F.rmox;
*mvy = F.rmsy * (float)r[1] + F.rmoy;
*mvx = (float)r[0];
*mvy = (float)r[1];
return;
}
const float* m = kio_ptr(F.motion, ((u32)y * F.mpitch + x) * 2u);
Expand Down Expand Up @@ -421,8 +432,8 @@ WG_FN void ko_motion(const KFrameIn& F, int x, int y, float* mvx, float* mvy) {
if (F.flags & KIO_M16) {
const u32 i = F.mch == 2 ? t << 1 : (F.mch == 4 ? t << 2 : t * (u32)F.mch);
const h16* r = kio_ptr((const h16*)F.motion, i);
*mvx = F.rmsx * (float)r[0] + F.rmox;
*mvy = F.rmsy * (float)r[1] + F.rmoy;
*mvx = (float)r[0]; // KIO_M16: the raw value (kio_m16_sp / kio_m16_mo apply ms / mo at the use)
*mvy = (float)r[1];
return;
}
const float* m = kio_ptr(F.motion, t * 2u);
Expand Down Expand Up @@ -801,7 +812,14 @@ WG_FN void ko_pixel(KOutParams P0) {
}
spx = U.spx; // [MVlo]: sx / rho_x, else sx
spy = U.spy;
const float wx = kf_fma(spx, mvx + F.mox, px), wy = kf_fma(spy, mvy + F.moy, py);
float ofx = F.mox, ofy = F.moy;
if (F.flags & KIO_M16) { // KIO_M16: the scale folded into the factor (out_uniforms' spx with s = sx ms), the offset
spx = mvlo ? kio_m16_sp(F.sx, F.rmsx, U.rhox) : F.sx * F.rmsx;
spy = mvlo ? kio_m16_sp(F.sy, F.rmsy, U.rhoy) : F.sy * F.rmsy;
ofx = kio_m16_mo(F.mox, F.rmox, F.rmsx);
ofy = kio_m16_mo(F.moy, F.rmoy, F.rmsy);
}
const float wx = kf_fma(spx, mvx + ofx, px), wy = kf_fma(spy, mvy + ofy, py);

// 3.4 validity, 3.5 colour history
bool valid = !(F.flags & KIO_RESET) && wx * invW >= 0.0f && wx * invW <= 1.0f && wy * invH >= 0.0f &&
Expand Down Expand Up @@ -1281,8 +1299,8 @@ WG_FN void ko_pixel(KOutParams P0) {
Cgg = kinv * (Cgo * d);
}
const float R = (Yg + Cog) - Cgg, G = Cgg + kf_fma(Cog, 0.0f, Yg), B = (Yg - Cog) - Cgg;
if (P.omode == KIO_OUT_RGBA16F) { // RGB rounded once to FP16 (nearest even), alpha 1
const v4h o = {kh_round(R), kh_round(G), kh_round(B), (h16)1.0f};
if (P.omode == KIO_OUT_RGBA16F) { // RGB rounded once to FP16 (nearest even), alpha cbar[3] (an FP16 value) as it is
const v4h o = {kh_round(R), kh_round(G), kh_round(B), cbar[3]};
*(v4h*)kio_ptr((h16*)P.out, ((u32)y * P.opitch + x) * 4u) = o;
} else if (P.omode == KIO_OUT_RGB32F) {
float* o = kio_ptr(P.out, ((u32)y * P.opitch + x) * 3u);
Expand Down Expand Up @@ -1436,7 +1454,15 @@ WG_FN KiFine ki_fine(const KInParams& P, const KiHost& k, int fx, int fy) {
const int mx = (int)kf_R(kf_fma(px, k.mp_x, -0.5f)), my = (int)kf_R(kf_fma(py, k.mp_y, -0.5f));
kio_motion(F, kio_tap(mx, 0, F.mbx, F.W), kio_tap(my, 0, F.mby, F.H), &mvx, &mvy);
}
const float mx = k.sp_x * (mvx + F.mox), my = k.sp_y * (mvy + F.moy);
float spx = k.sp_x, spy = k.sp_y, ofx = F.mox, ofy = F.moy;
if (F.flags & KIO_M16) { // KIO_M16: the scale folded into the factor (ki_host's sp_x with s = sx ms), the offset
const bool mvlo = (F.flags & KIO_MVLO) != 0;
spx = kio_m16_sp(F.sx, F.rmsx, (1.0f / (float)F.W) * (float)(mvlo ? F.rw : F.W));
spy = kio_m16_sp(F.sy, F.rmsy, (1.0f / (float)F.H) * (float)(mvlo ? F.rh : F.H));
ofx = kio_m16_mo(F.mox, F.rmox, F.rmsx);
ofy = kio_m16_mo(F.moy, F.rmoy, F.rmsy);
}
const float mx = spx * (mvx + ofx), my = spy * (mvy + ofy);
const float wx = px + mx, wy = py + my;
KiFine r;
r.valid = wx * k.xi_x >= 0.0f && wx * k.xi_x <= 1.0f && wy * k.xi_y >= 0.0f && wy * k.xi_y <= 1.0f;
Expand Down
Loading
Loading