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
18 changes: 13 additions & 5 deletions clean/k/runtime/runtime_k.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,17 +593,22 @@ bool KRuntime::create(const WgRtConfig& c, Device* dev, AllocFn alloc_fn, bool e
!alloc(&Hh_[p], (size_t)Bw_ * Bh_ * 4 * 2, true))
return false;
const int ny = std::max(256, max_rh_ / 2);
// native_direct: no dense inputs (the kernels read the game's textures) and no legacy input-stage maps (cpre, cpre2:
// the tiled input stage, the only one with direct forms, reads neither)
if (!(native_direct_ || (alloc(&color_, R * 16) && alloc(&depth_, R * 4) && alloc(&motion_, R * 8) &&
alloc(&cpre_, R * 16) && alloc(&cpre2_, R * 8))) ||
!alloc(&ctap_, R * 8) || !alloc(&ctapT_, R * 2) ||
// native_direct: no dense inputs (the kernels read the game's textures). The legacy input stage's maps (cpre, cpre2)
// are needed by every context: its direct forms (kx/k_in_color_direct, then kx/k_in_reset or kx/k_in_base_direct) run
// on a frame outside the tiled guard (a render size above 0.68 x the output, a jitter outside [-0.5, 0.5],
// input_tiled=0) and write and read them
if (!(native_direct_ || (alloc(&color_, R * 16) && alloc(&depth_, R * 4) && alloc(&motion_, R * 8))) ||
!alloc(&cpre_, R * 16) || !alloc(&cpre2_, R * 8) || !alloc(&ctap_, R * 8) || !alloc(&ctapT_, R * 2) ||
// exp2tab: read only by the host emulator's k_out_pixel (the GPU uses v_exp_f16); a valid, zeroed buffer
!alloc(&exp2tab_, 65536 * 2, true) || !alloc(&tables_, 8 * kTableBytes) ||
!alloc(&expo_, 32, true) || !alloc(&expo_partials_, 4 * (size_t)cdiv(ny, 4)) || !alloc(&expo_computed_, 4, true) ||
!alloc(&expo_sync_, 4, true) || !alloc(&xs_, 16, true) || !alloc(&kexpo_, 16, true) ||
(!warm_at_.empty() && !alloc(&warm_dummy_, 16, true)))
return false;
// every buffer an input-stage kernel of this context can be handed is allocated: a null pointer would be packed as
// arena offset 0 (clean/runtime/struct_args.h) and alias the arena's first allocation
if (!cpre_ || !cpre2_ || !ctap_ || !ctapT_ || (!native_direct_ && (!color_ || !depth_ || !motion_)))
return fail("model K: an input-stage buffer is not allocated (it would be packed as arena offset 0)");
// the own output buffer (tests): dense RGB f32, dense RGBA f32 (FrameIo.out_rgba32f) or RGBA16F rows that fit
if (!external_io_ && !alloc(&out_, (size_t)W_ * H_ * 16, true)) return false;
if (fill_ >= 0) { // k_fill (test hook): the sentinel in everything a frame writes, before the first frame
Expand Down Expand Up @@ -1198,6 +1203,9 @@ bool KRuntime::frame(const WgRtFrame& fr, const FrameIo* io) {
++tiled_launches_;
} else {
const std::string color_name = std::string(in_exact_ ? "kx/k_in_color" : "k_in_color") + (native_direct_ ? "_direct" : "");
// the legacy input kernels write and read cpre / cpre2: never launched with a null one (packed as arena offset 0)
if (!ip.cpre || !ip.cpre2)
return fail("model K: " + color_name + " would get a null cpre / cpre2 (arena offset 0); the frame is refused");
if (!launch(color_name.c_str(), cdiv((long long)rw * rh, kThreads), kThreads, &ip)) return false;
++legacy_color_launches_;
// native_direct: k_in_color / k_in_base read the game's textures (their "_direct" forms); k_in_reset reads buffers only
Expand Down
80 changes: 80 additions & 0 deletions clean/k/tests/test_k_direct_legacy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# SPDX-License-Identifier: MIT
"""GPU/D3D12: Model K on the native direct route, frames on the legacy input path, byte for byte against the exact
reference.

The legacy input kernels (kx/k_in_color_direct, then kx/k_in_reset or kx/k_in_base_direct) run on a frame outside the
tiled guard: a render size above 0.68 x the output (DLAA), a jitter outside [-0.5, 0.5], or input_tiled=0. They write
and read the intermediates cpre / cpre2, which a direct context must allocate like a buffer context (a null pointer is
packed as arena offset 0, clean/runtime/struct_args.h, and the frame would overwrite the arena's first allocation).
test_interop_d3d12.exe dx12same runs the same 6 frames (resets at frames 1 and 5) through executor=hip (the exact
reference on the buffer route) and executor=dx12 (the native direct route: "inputs read directly 1") and compares the
raw output texture bytes: every frame must be identical. The tiled Quality case is the control.

Set WG_K_GPU_BUILD (a developer runtime build: executor=hip is a diagnostic key) and WG_K_INTEROP_EXE
(test_interop_d3d12.exe); run under the shared GPU lock. Only synthetic parameters (random_network_params).

Run: python -m unittest clean.k.tests.test_k_direct_legacy
"""
import os
from pathlib import Path
import re
import subprocess
import tempfile
import unittest

from clean.k.reference.network import random_network_params
from clean.k.tests.test_k_runtime_gpu import gpu_build, write_k_model

FRAMES = 6
CASES = [ # (label, render w, h, output w, h, extra options, extra environment)
("DLAA", 1280, 720, 1280, 720, "", {}),
("jitter outside the guard, Quality", 853, 480, 1280, 720, "", {"WG_TEST_JITTER_OUT": "1"}),
("input_tiled=0, Quality", 853, 480, 1280, 720, "input_tiled=0", {}),
("control: tiled Quality", 853, 480, 1280, 720, "", {}),
]
ROW = re.compile(r"frame (\d+): (\d+) of (\d+) bytes differ")


class TestKDirectLegacy(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.exe = os.environ.get("WG_K_INTEROP_EXE")
if not cls.exe:
raise unittest.SkipTest("set WG_K_INTEROP_EXE to the clean native interop test")
cls.build = Path(gpu_build()).resolve()
cls.tmp = tempfile.TemporaryDirectory(prefix="k-direct-legacy-")
cls.model, cls.map = write_k_model(os.path.join(cls.tmp.name, "model"), random_network_params(11))

@classmethod
def tearDownClass(cls):
cls.tmp.cleanup()

def run_case(self, rw, rh, ow, oh, extra, env_extra):
opts = ";".join(["model=K", f"kernels_dir={self.build}", f"model_dir={self.tmp.name}",
f"model_k_dir={Path(self.model).parent}", f"model_k_map={self.map}"] + ([extra] if extra else []))
env = {k: v for k, v in os.environ.items() if not k.startswith("WG_") or k == "WG_K_INTEROP_EXE"}
env.update(WG_TEST_RENDER_WIDTH=str(rw), WG_TEST_RENDER_HEIGHT=str(rh), WG_TEST_OUTPUT_WIDTH=str(ow),
WG_TEST_OUTPUT_HEIGHT=str(oh), **env_extra)
p = subprocess.run([self.exe, str(self.build / "wronggpu_runtime.dll"), "dx12same", opts, str(FRAMES)],
capture_output=True, text=True, timeout=170, env=env)
out = p.stdout + p.stderr
if "executor=hip ignored: diagnostic key, release build" in out:
self.skipTest("the exact reference (executor=hip) needs a developer build")
return p.returncode, out

def test_direct_legacy_frames_match_the_reference(self):
for label, rw, rh, ow, oh, extra, env_extra in CASES:
with self.subTest(case=label):
rc, out = self.run_case(rw, rh, ow, oh, extra, env_extra)
self.assertIn("inputs read directly 1", out, out[-2000:]) # the native direct route
rows = [(int(f), int(d), int(n)) for f, d, n in ROW.findall(out)]
print("%s: %s" % (label, ", ".join("frame %d %d/%d" % r for r in rows)))
self.assertEqual([f for f, _, _ in rows], list(range(1, FRAMES + 1)), out[-2000:])
self.assertEqual([d for _, d, _ in rows], [0] * FRAMES, "%s: output bytes differ from the exact reference"
% label)
self.assertNotIn("null cpre", out)
self.assertEqual(rc, 0, out[-2000:])


if __name__ == "__main__":
unittest.main()
4 changes: 4 additions & 0 deletions clean/runtime/interop/tests/test_interop_d3d12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// test_interop_d3d12.exe <runtime> dx12same "<common options>" [frames] [outsrv]
// executor=hip against executor=dx12 (the network on this test's D3D12 device, no HIP) through the full interop:
// the output textures byte for byte; the common options name the synthetic model (see case_dx12_same).
// dx12same: WG_TEST_JITTER_OUT=1 gives every frame a jitter outside [-0.5, 0.5] (model K's legacy input path).
// hipbench / dx12same environment: WG_TEST_RENDER_WIDTH / _HEIGHT, WG_TEST_OUTPUT_WIDTH / _HEIGHT (sizes),
// WG_TEST_TEX_PAD=n (textures n texels wider than the render size), WG_TEST_MOTION_OUTRES=1 (motion at output size,
// WG_CREATE_MOTION_OUTPUT_RES), WG_TEST_MOTIONFMT=rg16f|rg32f, WG_TEST_PI (pre-exposure).
Expand Down Expand Up @@ -1996,6 +1997,9 @@ bool case_dx12_same(Gpu& g, const char* dll, const std::string& common, int fram
f.motion_offset[0] = 0.25f, f.motion_offset[1] = 0.0f;
const float jit[4][2] = {{0.0f, -1.0f / 6}, {-0.25f, 1.0f / 6}, {0.25f, -7.0f / 18}, {-0.375f, -1.0f / 18}};
f.jitter[0] = jit[t % 4][0], f.jitter[1] = jit[t % 4][1];
// WG_TEST_JITTER_OUT=1 (this test program only): a jitter outside [-0.5, 0.5] on every frame (model K's legacy
// input path at any render size)
if (std::getenv("WG_TEST_JITTER_OUT")) f.jitter[0] = 0.75f, f.jitter[1] = -0.8f;
f.exposure_mode = t % 3 == 0 ? WG_EXPOSURE_COMPUTE : t % 3 == 1 ? WG_EXPOSURE_VALUE : WG_EXPOSURE_TEXTURE;
f.exposure_value = 0.75f + (float)(t % 24) / 24.0f;
// WG_TEST_PI: the odd frames' pre-exposure (default 1; the even frames: 1.375)
Expand Down
14 changes: 14 additions & 0 deletions clean/tests/check_no_dev_diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ def check_object_names_in(data):
b"_tmo.dxil", b"l_hyb_probe", b'_hyb_mut"', b'_hyb_tmo"', b'_hyb_dump"') if t in data]


# Switches of the test programs (never in a release artifact: a file that names one is a test build)
TEST_HARNESS_STRINGS = (b"WG_TEST_JITTER_OUT",)


def test_harness_strings_in(data):
return [t.decode("ascii") for t in TEST_HARNESS_STRINGS if t in data]


def dev_only_strings_in(data):
return [t.decode("ascii") for t in DEV_ONLY_STRINGS if t in data]

Expand All @@ -85,6 +93,9 @@ def check_bytes(data):
objs = check_object_names_in(data)
if objs:
return False, "carries check-only code objects (%r)" % objs[0]
harness = test_harness_strings_in(data)
if harness:
return False, "carries a test program's switch (%r)" % harness[0]
return True, "dev-diagnostics=0"


Expand Down Expand Up @@ -159,6 +170,9 @@ def check_artifact(name, data):
carried = dev_only_strings_in(data)
if carried:
return False, "carries developer-diagnostic code (%r)" % carried[0], False
harness = test_harness_strings_in(data)
if harness:
return False, "carries a test program's switch (%r)" % harness[0], False
return True, "dev-diagnostics=0" if zero else "nothing to check", zero


Expand Down
14 changes: 14 additions & 0 deletions clean/tests/test_dev_diagnostics_off.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,20 @@ def test_the_package_check_refuses_a_test_only_d3d12_manifest(self):
res = chk.check_path(os.path.join(d, "folder"))
self.assertTrue(any(not ok for _, ok, _, _ in res), res)

def test_a_test_program_switch_is_refused(self):
"""WG_TEST_JITTER_OUT (test_interop_d3d12.exe's dx12same switch) exists only in the test program: a runtime library
or any payload or package file naming it is refused."""
self.assertFalse(chk.check_bytes(b"MZ" + MARK0 + b" WG_TEST_JITTER_OUT ")[0])
ok, text, _ = chk.check_artifact("test_interop_d3d12.exe", b"MZ ... WG_TEST_JITTER_OUT ...")
self.assertFalse(ok)
self.assertIn("test program", text)
self.assertTrue(chk.check_artifact("readme.txt", b"plain text")[0])
lib = self._release()
r, _ = self._pack({"wronggpu_runtime.dll": lib, "tools/harness.exe": b"MZ WG_TEST_JITTER_OUT"},
{"wronggpu_runtime.dll": manifest_for("wronggpu_runtime.dll", lib)})
self.assertNotEqual(r.returncode, 0)
self.assertIn("test program", r.stderr + r.stdout)

def test_no_manifest_refuses_the_payload(self):
r, out = self._pack({"wronggpu_runtime.dll": self._release()})
self.assertNotEqual(r.returncode, 0)
Expand Down
Loading