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
40 changes: 40 additions & 0 deletions clean/dx12/kernel_sets.json
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,10 @@
"k_out_color",
"k_io.co"
],
[
"k_out_color_direct",
"k_io.co"
],
[
"k_out_pixel",
"k_io.co"
Expand All @@ -308,6 +312,14 @@
"k_out_pixel_c",
"k_io.co"
],
[
"k_out_pixel_c_direct",
"k_io.co"
],
[
"k_out_pixel_direct",
"k_io.co"
],
[
"k_stage2_d0_hidden",
"k_block.co"
Expand Down Expand Up @@ -380,9 +392,37 @@
"k_warm",
"k_block_wide.co"
],
[
"kx/k_in_base",
"k_io_exact.co"
],
[
"kx/k_in_base_direct",
"k_io_exact.co"
],
[
"kx/k_in_color",
"k_io_exact.co"
],
[
"kx/k_in_color_direct",
"k_io_exact.co"
],
[
"kx/k_in_reset",
"k_io_exact.co"
],
[
"kx/k_in_tiled",
"k_io_exact.co"
],
[
"kx/k_in_tiled_direct",
"k_io_exact.co"
],
[
"kx/k_in_tiled_uniform",
"k_io_exact.co"
]
]
}
101 changes: 85 additions & 16 deletions clean/dx12/tests/kernel_replay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ bool bind_direct(NativeD3D12Ext* ext, Case& c, const std::string& base) {
if (t.slot == "color") f.color = t.tex, f.color_format = (DXGI_FORMAT)t.format;
else if (t.slot == "depth") f.depth = t.tex, f.depth_format = (DXGI_FORMAT)t.format;
else if (t.slot == "motion") f.motion = t.tex, f.motion_format = (DXGI_FORMAT)t.format;
else if (t.slot == "exposure") f.exposure = t.tex, f.exposure_format = (DXGI_FORMAT)t.format;
else if (t.slot == "output") f.output = t.tex, f.output_format = (DXGI_FORMAT)t.format;
}
f.params = c.dparams;
Expand All @@ -275,6 +276,55 @@ bool bind_direct(NativeD3D12Ext* ext, Case& c, const std::string& base) {
return true;
}

// The three direct lines of a case or a chain (see the header comment); true when `w` is one of them (*bad set on error).
bool parse_direct_line(const std::string& w, std::istringstream& s, Case& c, bool* bad) {
*bad = false;
if (w == "direct") {
DirectTex t;
s >> t.slot >> t.format >> t.width >> t.height >> t.file;
if (t.slot != "color" && t.slot != "depth" && t.slot != "motion" && t.slot != "exposure" && t.slot != "output") {
std::printf("unknown direct slot %s\n", t.slot.c_str());
*bad = true;
}
c.direct.push_back(t);
return true;
}
if (w == "direct_params") {
s >> c.dparams.rx >> c.dparams.ry >> c.dparams.rw >> c.dparams.rh;
std::string tok;
int pos = 0;
while (s >> tok) { // [pi [rne16]] then key=value words: ms=<x>,<y> mo=<x>,<y> expo=<mode>
const size_t eq = tok.find('=');
if (eq == std::string::npos) {
if (pos == 0) c.dparams.color_pi = std::stof(tok);
else if (pos == 1) c.dparams.output_rne16 = (unsigned)std::stoul(tok);
++pos;
} else {
const std::string key = tok.substr(0, eq), val = tok.substr(eq + 1);
const size_t comma = val.find(',');
if (key == "ms" && comma != std::string::npos) {
c.dparams.msx = std::stof(val.substr(0, comma));
c.dparams.msy = std::stof(val.substr(comma + 1));
} else if (key == "mo" && comma != std::string::npos) {
c.dparams.mox = std::stof(val.substr(0, comma));
c.dparams.moy = std::stof(val.substr(comma + 1));
} else if (key == "expo") {
c.dparams.exposure_mode = (unsigned)std::stoul(val);
} else {
std::printf("unknown direct_params word %s\n", tok.c_str());
*bad = true;
}
}
}
return true;
}
if (w == "direct_out") {
s >> c.direct_out;
return true;
}
return false;
}

std::string dir_of(const std::string& path) {
const size_t s = path.find_last_of("/\\");
return s == std::string::npos ? std::string(".") : path.substr(0, s);
Expand Down Expand Up @@ -375,6 +425,7 @@ struct Chain {
std::vector<ChainStep> steps;
std::vector<std::pair<std::string, std::string>> saves; // (id, file)
int time_runs = 0;
Case dcase; // direct textures bound around the steps (rs=direct kernels): direct / direct_params / direct_out lines
};

int run_chain(Device* d, Chain& c, const std::string& base) {
Expand Down Expand Up @@ -484,7 +535,33 @@ int run_chain(Device* d, Chain& c, const std::string& base) {
if (!d->sync()) { std::printf("%s: sync: %s\n", c.name.c_str(), d->err().c_str()); return false; }
return true;
};
NativeD3D12Ext* dext = nullptr;
if (rc == 0 && !c.dcase.direct.empty()) {
c.dcase.name = c.name;
dext = d3d12_ext(d);
if (!dext || !bind_direct(dext, c.dcase, base)) {
std::string e;
if (dext) dext->sync(&e); // the upload pass may still be queued: the textures are released only after it ran
release_direct(c.dcase);
rc = 1;
}
}
if (rc == 0 && !run_steps()) rc = 1;
if (dext) { // the direct frame ends after the first run; an output texture is read back
std::string derr;
if (rc == 0 && (!dext->end_direct_frame(&derr) || !dext->sync(&derr))) {
std::printf("%s: direct: %s\n", c.name.c_str(), derr.c_str());
rc = 1;
}
if (rc == 0 && !c.dcase.direct_out.empty()) {
std::vector<uint8_t> texels;
if (!read_direct_output(dext, c.dcase, texels, derr) || !write_file(base + "/" + c.dcase.direct_out, texels)) {
std::printf("%s: direct output: %s\n", c.name.c_str(), derr.c_str());
rc = 1;
}
}
release_direct(c.dcase);
}
for (size_t i = 0; rc == 0 && i < c.saves.size(); ++i) {
ChainBuf* b = find(c.saves[i].first);
std::vector<uint8_t> data(b ? b->bytes : 0);
Expand Down Expand Up @@ -551,6 +628,10 @@ int main(int argc, char** argv) {
ch.saves.emplace_back(id, file);
} else if (w == "time") {
s >> ch.time_runs;
} else if (w == "direct" || w == "direct_params" || w == "direct_out") {
bool bad = false;
parse_direct_line(w, s, ch.dcase, &bad);
if (bad) return 2;
} else {
std::printf("unknown chain line: %s\n", line.c_str());
return 2;
Expand All @@ -565,22 +646,10 @@ int main(int argc, char** argv) {
c.modules.emplace_back(m, tag);
} else if (w == "kernel") {
s >> c.kernel >> c.grid >> c.block;
} else if (w == "direct") {
DirectTex t;
s >> t.slot >> t.format >> t.width >> t.height >> t.file;
if (t.slot != "color" && t.slot != "depth" && t.slot != "motion" && t.slot != "output") {
std::printf("unknown direct slot %s\n", t.slot.c_str());
return 2;
}
c.direct.push_back(t);
} else if (w == "direct_params") {
s >> c.dparams.rx >> c.dparams.ry >> c.dparams.rw >> c.dparams.rh;
float pi = 1.0f;
unsigned rne16 = 0;
if (s >> pi) c.dparams.color_pi = pi; // optional: color_pi, then output_rne16
if (s >> rne16) c.dparams.output_rne16 = rne16;
} else if (w == "direct_out") {
s >> c.direct_out;
} else if (w == "direct" || w == "direct_params" || w == "direct_out") {
bool bad = false;
parse_direct_line(w, s, c, &bad);
if (bad) return 2;
} else if (w == "arg") {
Arg a;
s >> a.kind;
Expand Down
59 changes: 59 additions & 0 deletions clean/fsr/tests/test_frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "../src/frontend.h"
#include "../src/param_map.h"
#include "wg_settings_io.h" // pure part: describe()
#include "../../runtime/interop/executor_select.h" // the network runtime's refused-frame policy (header only)

using namespace wgfsr;

Expand Down Expand Up @@ -143,6 +144,7 @@ static int g_failDispatchAt = -1;
static int g_notReadyUntil = 0; // FakeDispatch answers WG_ERROR_NOT_READY for frames below this count
static int g_parameterErrorAt = -1; // FakeDispatch answers WG_ERROR_PARAMETER for this frame (inputs it cannot take)
static int g_unsupportedFrom = -1; // FakeDispatch answers WG_ERROR_UNSUPPORTED from this frame on (frames it does not take)
static std::set<int> g_unsupportedAt; // FakeDispatch answers WG_ERROR_UNSUPPORTED at these frames only (refused frames)
static int g_inputErrorFrom = -1; // FakeDispatch answers WG_ERROR_INPUT from this frame on (inputs it never takes)
static std::set<int> g_inputErrorAt; // ...and at these frames (a menu or loading frame with an odd texture)
static int g_deviceErrorFrom = -1; // FakeDispatch answers WG_ERROR_DEVICE from this frame on (bug hunt F1)
Expand Down Expand Up @@ -171,6 +173,7 @@ static WgResult FakeDispatch(WgUpscaler*, const WgFrame* frame) {
g_frames.push_back(*frame);
if (int(g_frames.size()) - 1 == g_parameterErrorAt) return WG_ERROR_PARAMETER;
if (g_unsupportedFrom >= 0 && int(g_frames.size()) - 1 >= g_unsupportedFrom) return WG_ERROR_UNSUPPORTED;
if (g_unsupportedAt.count(int(g_frames.size()) - 1)) return WG_ERROR_UNSUPPORTED;
if (g_inputErrorFrom >= 0 && int(g_frames.size()) - 1 >= g_inputErrorFrom) return WG_ERROR_INPUT;
if (g_inputErrorAt.count(int(g_frames.size()) - 1)) return WG_ERROR_INPUT;
if (g_deviceErrorFrom >= 0 && int(g_frames.size()) - 1 >= g_deviceErrorFrom) return WG_ERROR_DEVICE;
Expand Down Expand Up @@ -343,6 +346,7 @@ static void Reset() {
g_notReadyUntil = 0;
g_parameterErrorAt = -1;
g_unsupportedFrom = -1;
g_unsupportedAt.clear();
g_inputErrorFrom = -1;
g_inputErrorAt.clear();
g_deviceErrorFrom = -1;
Expand Down Expand Up @@ -2388,6 +2392,60 @@ static void TestUnsupportedFrames() {
CHECK(fe->DestroyContext(&ctx, kMemCb) == FFX_API_RETURN_OK);
}

// REVIEW-215c: a refused frame of model K takes the same front-end path as model M's. The backend's answer comes from the
// real policy (wgi::refused_frame_action, executor_select.h: a refused K frame, and a refused M frame with hip_switch=off,
// are answered WG_ERROR_UNSUPPORTED, never a switch to HIP and never a bilinear fallback), the fake upscaler answers
// that for the refused frames, and the front end must give every frame an upscaler: AMD's FSR when it exists (it takes
// the context over at the first refusal, as for model M), else the stand-in for each refused frame (any number in a row,
// the context stays WrongGPU's and takes the next frame it can). No HIP runtime is ever started.
static void TestRefusedFrames() {
for (const bool model_k : {true, false}) { // model K, and model M as the control
const bool unsupported =
wgi::refused_frame_action(model_k, /*by_default=*/true, /*hip_switch=*/false) == wgi::RefusedAction::kNoSwitch;
CHECK(unsupported); // the backend's answer for a refused frame is WG_ERROR_UNSUPPORTED
const std::set<int> pattern = unsupported ? std::set<int>{1, 2, 3, 5, 6} : std::set<int>{};
// with AMD's FSR: the first refused frame hands the context to it; no later frame is lost
Reset();
g_resources[kOutput] = {dxgi::R16G16B16A16_FLOAT, 2560, 1440};
Config config;
config.hosted = true;
auto fe = MakeFrontEnd(config);
UpscaleCreate own(FFX_UPSCALE_ENABLE_HIGH_DYNAMIC_RANGE, /*withOverride=*/true);
own.version.versionId = kWrongGpuProviderVersionId;
ffxContext ctx = nullptr;
CHECK(fe->CreateContext(&ctx, &own.upscale.header, kMemCb) == FFX_API_RETURN_OK && fe->OwnContextCount() == 1);
g_unsupportedAt = pattern;
ffxDispatchDescUpscale d = MakeDispatch();
for (int i = 0; i < 8; ++i) CHECK(fe->Dispatch(&ctx, &d.header) == FFX_API_RETURN_OK); // every frame has an upscaler
CHECK(g_frames.size() == 2 && g_standInFrames.empty()); // frame 0 the network's, frame 1 refused: AMD from there on
CHECK(!g_amd.empty() && g_amd.back().fn == "dispatch" && g_statuses.back().state == UpscaleState::Amd);
CHECK(CountLog("HIP") == 0); // no HIP runtime on the way
CHECK(fe->DestroyContext(&ctx, kMemCb) == FFX_API_RETURN_OK);

// without AMD's FSR: each refused frame goes to the stand-in (five in a row, then a taken frame, then two more
// refused ones), every frame gets an upscaler, the network takes the frames between
Reset();
g_resources[kOutput] = {dxgi::R16G16B16A16_FLOAT, 2560, 1440};
g_originalAvailable = false;
fe = MakeFrontEnd(config);
ctx = nullptr;
CHECK(fe->CreateContext(&ctx, &own.upscale.header, kMemCb) == FFX_API_RETURN_OK && fe->OwnContextCount() == 1);
g_unsupportedAt = unsupported ? std::set<int>{1, 2, 3, 4, 5, 8, 9} : std::set<int>{};
const std::vector<UpscaleState> want = {UpscaleState::Network, UpscaleState::StandIn, UpscaleState::StandIn,
UpscaleState::StandIn, UpscaleState::StandIn, UpscaleState::StandIn,
UpscaleState::Network, UpscaleState::Network, UpscaleState::StandIn,
UpscaleState::StandIn, UpscaleState::Network};
for (size_t i = 0; i < want.size(); ++i) {
CHECK(fe->Dispatch(&ctx, &d.header) == FFX_API_RETURN_OK);
CHECK(g_statuses.back().state == want[i]);
}
CHECK(g_frames.size() == want.size() && g_standInFrames.size() == 7 && g_amd.empty());
CHECK(CountLog("HIP") == 0);
CHECK(CountLog("AMD FSR is not available for this context; the stand-in upscales the frames the upscaler does not take") == 1);
CHECK(fe->DestroyContext(&ctx, kMemCb) == FFX_API_RETURN_OK);
}
}

static void TestSplitBlocked() {
Reset();
g_resources[kOutput] = {dxgi::R16G16B16A16_FLOAT, 2560, 1440};
Expand Down Expand Up @@ -3434,6 +3492,7 @@ int main() {
#endif
TestRegistryEntries();
TestUnsupportedFrames();
TestRefusedFrames();
TestSplitBlocked();
TestRuntimeProviderName();
TestWithoutModelKItems();
Expand Down
18 changes: 18 additions & 0 deletions clean/k/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,24 @@ Model K ships in the same `wronggpu_runtime.dll` as model M; nothing changes for
Exposure (spec 42 draft 3 section 4): E_cur = the game's exposure texture or value, 1 if it passes none; the
auto-exposure value (spec 07, run when the game asks for it at creation, HDR only) is E_aux and acts only through
λ. `k_expo_rule=v051` (A/B only) restores the 0.5.1 rule (the measured value as E_cur and E_aux).
Direct3D 12 devices (`backend=2`, the hybrid device, and `backend=3`, the native device): the exact default path runs
on the D3D12 ports of `clean/k/dx12` (byte-identical to the HIP kernels, `clean/k/dx12/README.md`); a context whose
options leave that path (`network=fused16` / `fused8`, `k_fast=1`, `k_fast_in=1`, `k_in_exact=0`, `k_diag`) is
refused on the native device (those options run on the HIP backend only). The legacy exact input kernels
(`kx/k_in_color`, `kx/k_in_reset`, `kx/k_in_base`: a render size above 0.68 x the output up to 1.0, a jitter outside
[-0.5, 0.5], `input_tiled=0`) are ported too, so a native context covers every ratio and every jitter. An input-copy
frame (`raw_inputs`, the interop's HIP input copies) is refused on the D3D12 devices. On the native device `create`
first checks that
`dx12/manifest.json` has a port of every kernel the context can launch and refuses it, naming the missing kernels,
before the model is loaded. `k_in_uniforms=0` (diagnostic): the tiled input kernel that computes its frame values on the
GPU instead of taking the host's. `k_fill=<0..255>` (test hook, changes no value a frame writes): every buffer a frame writes and both history copies start with this byte after allocation, so that a test with a different byte per backend proves first-frame and reset-only writes. Latency options of the D3D12 devices that change no value: `k_color_at=<stage>`
(`k_out_color` runs unordered right after that stage; default: before the input stage, HIP's order) and `k_warm=<stage>` /
`off` (backend 3: `k_warm`, a cache warm-up of the weight blobs of the stages after it, launched unordered after that
stage, default `e0`; `k_warm_to`, `k_warm_line`, `k_warm_groups`). The D3D12 executor of the interop (the game's own
device) runs model K with the forms that read the game's textures and write the output texture (`_direct`, also
declared for the legacy input kernels). When it cannot take a context (a kernel without a port in the build, an option
off the exact path) it refuses K at create with one log line and the game keeps its own upscaler: model K has no HIP
route in a game (`executor=hip` selects the HIP executor as a diagnostic).

In game (Cyberpunk 2077 benchmark, 1440p Quality, RX 9070 XT; the project's measurements): model K
GPU time about 1.8 ms per frame (`fused`; 1.72 ms with `fused16`), the game at ~120 fps against ~117 fps with model M
Expand Down
Loading
Loading