Skip to content
Draft
15 changes: 14 additions & 1 deletion clean/fsr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,20 @@ OptiScaler folder ships `amd_fidelityfx_upscaler_dx12.dll`; OptiScaler.ini `[Lib
- **Frames the upscaler does not take:** `WG_ERROR_UNSUPPORTED` from the upscaler's dispatch (the network runtime with
`hip_switch=off`, its default since 2026-11-04: frames above the render size checked at create) hands the context to
AMD's FSR at once;
without AMD's FSR the stand-in upscales such frames.
without AMD's FSR the stand-in upscales such frames. This is one contract for every model (K refused frames take exactly
model M's path), and what a frame gets is explicit (B8): when AMD's FSR (missing, not created, or its dispatch failed) and
the stand-in (not made, or its dispatch failed) have both failed, the error goes back to the game with ONE line naming both
failures per context (also after an earlier success or a takeover); the count is logged when the context is destroyed. AMD's
own failures are logged once per context and counted, and AMD's per-frame context create for a bad frame is retried at the
1st, 2nd, 3rd, 10th, 100th ... frame. A frame the game flags as non-linear (sRGB / PQ) is AMD's alone: it never reaches the
stand-in, on any path (AMD missing, failing, the takeover at the bad-frame threshold, a context already handed to AMD).
`tests/test_frontend.cpp` covers each failure case for models M, K and L over a long run (CI).
`tests/test_refusal_integration.cpp` is a release check: it drives refused frames through the real network backend (the
runtime DLL's `Context`, with the scripted executor `executor=dx12-test`) and the real front end on a D3D12 device (WARP is
enough), with and without AMD's FSR, the stand-in working and not, for K and M, and checks that no HIP executor was ever
made (`wgDiagHipExecutorsMade`, an export for that test only). It needs the runtime DLL of a build (so it is not in the CI),
and fails without one; its build command is in the file's header, and the release script under `tools/release` builds it
from that source.
- **Model location:** the front end looks for the converted model (`model.wgcm`, Model K in `K\`) in `[fsr] model_dir`
of `wronggpu.ini` (relative: from the data folder), then `<game exe folder>\WrongGPU\model`, and passes the first
folder that has it to the runtime as `model_dir=`; otherwise the runtime's own default (`%LOCALAPPDATA%\WrongGPU\model`,
Expand Down
167 changes: 126 additions & 41 deletions clean/fsr/src/frontend.cpp

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions clean/fsr/src/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,18 @@ class FrontEnd {
std::string FailureKey(const WgUpscalerBackend* backend, const UpscaleContext& u) const;
void ResizeForFrame(UpscaleContext& u, WgFrame& frame, const std::string& tag); // bug hunt H
ffxReturnCode_t BrokenFrame(UpscaleContext& u, WgFrame& frame, const std::string& tag, uint64_t frameIndex); // bug hunt F1
// B8, the refused-frame contract: what a frame gets is explicit. When AMD's FSR and the stand-in have both failed for a frame,
// the error goes back with ONE line naming both failures (per context, also after an earlier success or a takeover; the count
// is logged when the context is destroyed), never silently. A non-linear sRGB / PQ frame is AMD's alone: it never reaches the
// stand-in, on any path. NoteAmdFailure: AMD's failures, logged once per context, counted. NoteBothFailed: the one line.
// StandInFrame: the stand-in upscales `frame` (false: why not, in *why). AfterAmdFailure: AMD's dispatch failed for this frame
// (rc): the stand-in (unless `perceptual`, or in OptiScaler's custom slot, whose host upscales on an error), else rc.
void NoteAmdFailure(UpscaleContext& u, const std::string& tag, uint64_t frameIndex, ffxReturnCode_t rc);
void NoteBothFailed(UpscaleContext& u, const std::string& tag, uint64_t frameIndex, const std::string& amdWhy,
const std::string& standInWhy);
bool StandInFrame(UpscaleContext& u, WgFrame& frame, std::string* why);
ffxReturnCode_t AfterAmdFailure(UpscaleContext& u, const std::string& tag, uint64_t frameIndex, WgFrame* frame,
ffxReturnCode_t rc, bool perceptual, const std::string& noFrameWhy = std::string());
void Fail(UpscaleContext& u, const std::string& why, uint64_t call, bool error = true);
// Platform::status for one upscale dispatch of `u` (frame: the mapped frame when WrongGPU upscaled it, else null).
// amdReason: why AMD's FSR upscaled this one frame of a context that stays WrongGPU's (state Amd).
Expand Down
151 changes: 150 additions & 1 deletion clean/fsr/tests/test_frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,17 @@ static const WgUpscalerBackend* g_useBackend = &g_backend;
static std::vector<WgFrame> g_standInFrames;
static WgCreateInfo g_standInCreate{};
static int g_standInDestroyed = 0;
static bool g_standInCreateFails = false; // B8: the stand-in cannot be made
static bool g_standInDispatchFails = false; // B8: the stand-in's dispatch fails
static WgResult StandInCreate(const WgCreateInfo* info, WgUpscaler** out) {
g_standInCreate = *info;
if (g_standInCreateFails) return WG_ERROR_DEVICE;
*out = reinterpret_cast<WgUpscaler*>(new FakeUpscaler{2});
return WG_OK;
}
static WgResult StandInDispatch(WgUpscaler*, const WgFrame* frame) {
g_standInFrames.push_back(*frame);
return WG_OK;
return g_standInDispatchFails ? WG_ERROR_DEVICE : WG_OK;
}
static void StandInDestroy(WgUpscaler* u) {
++g_standInDestroyed;
Expand Down Expand Up @@ -361,6 +364,7 @@ static void Reset() {
g_createGate = true;
g_useBackend = &g_backend;
g_standInFrames.clear();
g_standInCreateFails = g_standInDispatchFails = false;
g_standInCreate = WgCreateInfo{};
g_standInDestroyed = 0;
g_servicesAsked.clear();
Expand Down Expand Up @@ -1880,6 +1884,11 @@ static void TestFailedFrameProviderTruth() {
if (failure == 2) g_amdDispatchResult = FFX_API_RETURN_ERROR_RUNTIME_ERROR;
auto dispatch = MakeDispatch();
if (failure == 3) dispatch.flags = FFX_UPSCALE_FLAG_NON_LINEAR_COLOR_SRGB;
if (failure == 2) {
// B8: AMD's dispatch fails after the upscaler failed: the stand-in upscales the frame (it was an error before)
CHECK(fe->Dispatch(&ctx, &dispatch.header) == FFX_API_RETURN_OK);
CHECK(!g_statuses.empty() && g_statuses.back().state == UpscaleState::StandIn);
} else {
CHECK(fe->Dispatch(&ctx, &dispatch.header) != FFX_API_RETURN_OK);
CHECK(!g_statuses.empty());
if (!g_statuses.empty()) {
Expand All @@ -1888,6 +1897,7 @@ static void TestFailedFrameProviderTruth() {
status.state != UpscaleState::StandIn && status.state != UpscaleState::Compare);
CHECK(!status.reason.empty());
}
}
CHECK(fe->DestroyContext(&ctx, kMemCb) == FFX_API_RETURN_OK);
}
for (bool comparison : {false, true}) {
Expand Down Expand Up @@ -2446,6 +2456,144 @@ static void TestRefusedFrames() {
}
}

// B8 (REVIEW-215d P1, shrunk by REVIEW-228b): what a frame the upscaler refuses gets is explicit. The upscaler answers
// WG_ERROR_UNSUPPORTED for frames 1 .. 300; AMD's FSR is missing, cannot be created or fails; the stand-in cannot be made or
// fails: the frame returns the error with ONE line naming both failures (per context, not per frame), the count is logged when the
// context is destroyed. Every model (the contract is shared). An answer another upscaler can give stays an OK frame.
static void TestRefusedFrameContract() {
const uint64_t models[] = {kWrongGpuProviderVersionId, kWrongGpuKProviderVersionId, kWrongGpuLProviderVersionId};
const ffxReturnCode_t amdBad = FFX_API_RETURN_ERROR_RUNTIME_ERROR;
const ffxReturnCode_t ok = FFX_API_RETURN_OK;
for (const uint64_t model : models) {
struct Case {
const char* name;
bool amd;
ffxReturnCode_t amdResult;
ffxReturnCode_t amdCreate;
bool standInCreateFails, standInDispatchFails, customSlot;
const char* amdWhy; // what the one line says about AMD
const char* standInWhy; // ... and about the stand-in
};
const Case cases[] = {
{"no AMD, stand-in create fails", false, ok, ok, true, false, false, "is not available", "could not be created"},
{"no AMD, stand-in dispatch fails", false, ok, ok, false, true, false, "is not available", "dispatch failed"},
{"AMD create fails, stand-in ok", true, ok, FFX_API_RETURN_NO_PROVIDER, false, false, false, nullptr, nullptr},
{"AMD create fails, stand-in fails", true, ok, FFX_API_RETURN_NO_PROVIDER, true, false, false, "is not available", "could not be created"},
{"AMD dispatch fails, stand-in ok", true, amdBad, ok, false, false, false, nullptr, nullptr},
{"AMD dispatch fails, stand-in create fails", true, amdBad, ok, true, false, false, "dispatch failed (ERROR_RUNTIME_ERROR)", "could not be created"},
{"AMD dispatch fails, stand-in dispatch fails", true, amdBad, ok, false, true, false, "dispatch failed (ERROR_RUNTIME_ERROR)", "dispatch failed"},
{"custom slot, no AMD, stand-in fails", false, ok, ok, true, false, true, "is not available", "could not be created"},
};
for (const Case& c : cases) {
Reset();
g_resources[kOutput] = {dxgi::R16G16B16A16_FLOAT, 2560, 1440};
g_originalAvailable = c.amd;
g_amdDispatchResult = c.amdResult;
g_amdCreateResult = c.amdCreate;
g_standInCreateFails = c.standInCreateFails;
g_standInDispatchFails = c.standInDispatchFails;
Config config;
config.hosted = true;
config.customSlot = c.customSlot;
auto fe = MakeFrontEnd(config);
UpscaleCreate own(FFX_UPSCALE_ENABLE_HIGH_DYNAMIC_RANGE, /*withOverride=*/true);
own.version.versionId = model;
ffxContext ctx = nullptr;
CHECK(fe->CreateContext(&ctx, &own.upscale.header, kMemCb) == FFX_API_RETURN_OK && fe->OwnContextCount() == 1);
ffxDispatchDescUpscale d = MakeDispatch();
CHECK(fe->Dispatch(&ctx, &d.header) == FFX_API_RETURN_OK && g_statuses.back().state == UpscaleState::Network);
const int kFrames = 300; // a long run: the logs must not grow with it
std::set<int> refused;
for (int i = 1; i <= kFrames; ++i) refused.insert(i);
g_unsupportedAt = refused;
std::vector<ffxReturnCode_t> rcs;
for (int i = 0; i < kFrames; ++i) rcs.push_back(fe->Dispatch(&ctx, &d.header));
const bool answered = !c.standInCreateFails && !c.standInDispatchFails; // the stand-in took every frame
const bool amdFailing = c.amd && c.amdResult != ok;
for (ffxReturnCode_t rc : rcs) CHECK(rc == (answered ? ok : (amdFailing ? amdBad : FFX_API_RETURN_ERROR_RUNTIME_ERROR)));
if (answered) {
CHECK(g_standInFrames.size() == (size_t)kFrames);
CHECK(CountLog("no upscaler could take this frame") == 0);
} else {
// one line naming both failures, not one a frame
CHECK(CountLog("no upscaler could take this frame") == 1);
CHECK(CountLog(std::string("AMD FSR ").append(c.amdWhy).append("; the stand-in ").append(c.standInWhy).c_str()) == 1);
}
CHECK(CountLog("its later failures in this context are counted") <= 1); // AMD's failure: once per context
CHECK(g_log.size() < 60); // the logs do not grow with the run
const size_t lines = g_log.size();
CHECK(fe->DestroyContext(&ctx, kMemCb) == FFX_API_RETURN_OK);
if (!answered || amdFailing) CHECK(CountLog("fallback counts:") == 1 && g_log.size() > lines); // the count, logged at the context's end
if (g_failures) {
std::printf("case: %s (%zu log lines)\n", c.name, g_log.size());
for (size_t k = 0; k < g_log.size() && k < 60; ++k) std::printf(" log: %.200s\n", g_log[k].c_str());
return;
}
}
}

// a failure after an earlier success is reported too: no AMD, the stand-in takes frames 1-2, then it fails
{
Reset();
g_resources[kOutput] = {dxgi::R16G16B16A16_FLOAT, 2560, 1440};
g_originalAvailable = false;
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);
ffxDispatchDescUpscale d = MakeDispatch();
CHECK(fe->Dispatch(&ctx, &d.header) == FFX_API_RETURN_OK);
g_unsupportedAt = {1, 2, 3, 4};
CHECK(fe->Dispatch(&ctx, &d.header) == FFX_API_RETURN_OK && fe->Dispatch(&ctx, &d.header) == FFX_API_RETURN_OK);
CHECK(g_standInFrames.size() == 2 && CountLog("no upscaler could take this frame") == 0);
g_standInDispatchFails = true;
CHECK(fe->Dispatch(&ctx, &d.header) != FFX_API_RETURN_OK);
CHECK(CountLog("no upscaler could take this frame") == 1 && CountLog("the stand-in dispatch failed") == 1); // after two successes
CHECK(fe->Dispatch(&ctx, &d.header) != FFX_API_RETURN_OK && CountLog("no upscaler could take this frame") == 1);
CHECK(fe->DestroyContext(&ctx, kMemCb) == FFX_API_RETURN_OK);
}

// a frame the game flags as non-linear (sRGB / PQ) is not a refused frame: it is AMD's alone and never reaches the stand-in,
// on any path: AMD missing, AMD failing from the start, the takeover at the bad-frame threshold, and a context already handed
// to AMD whose AMD dispatch starts failing
const uint32_t flags[] = {FFX_UPSCALE_FLAG_NON_LINEAR_COLOR_SRGB, FFX_UPSCALE_FLAG_NON_LINEAR_COLOR_PQ};
for (const uint32_t flag : flags) {
for (const int path : {0, 1, 2}) { // 0 AMD missing, 1 AMD failing from the start (40 frames: the takeover threshold too)
Reset();
g_resources[kOutput] = {dxgi::R16G16B16A16_FLOAT, 2560, 1440};
g_originalAvailable = path != 0;
g_amdDispatchResult = path == 1 ? FFX_API_RETURN_ERROR_RUNTIME_ERROR : FFX_API_RETURN_OK;
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);
ffxDispatchDescUpscale d = MakeDispatch();
if (path == 2) {
// an ordinary refused frame hands the context to AMD (its dispatch works), then AMD starts failing
CHECK(fe->Dispatch(&ctx, &d.header) == FFX_API_RETURN_OK);
g_unsupportedAt = {1};
CHECK(fe->Dispatch(&ctx, &d.header) == FFX_API_RETURN_OK && g_statuses.back().state == UpscaleState::Amd);
g_amdDispatchResult = FFX_API_RETURN_ERROR_RUNTIME_ERROR;
}
ffxDispatchDescUpscale p = d;
p.flags |= flag;
for (int i = 0; i < 40; ++i) CHECK(fe->Dispatch(&ctx, &p.header) != FFX_API_RETURN_OK);
CHECK(g_standInFrames.empty()); // perceptual input never reaches the stand-in
CHECK(CountLog("no upscaler could take this frame") == 1 && CountLog("the stand-in is not used for non-linear (sRGB / PQ) input") == 1);
if (path == 2) { // and an ordinary frame of the same context still gets the stand-in
CHECK(fe->Dispatch(&ctx, &d.header) == FFX_API_RETURN_OK && g_standInFrames.size() == 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 @@ -3493,6 +3641,7 @@ int main() {
TestRegistryEntries();
TestUnsupportedFrames();
TestRefusedFrames();
TestRefusedFrameContract();
TestSplitBlocked();
TestRuntimeProviderName();
TestWithoutModelKItems();
Expand Down
Loading
Loading