diff --git a/clean/fsr/README.md b/clean/fsr/README.md index 278cff08..ab693ab7 100644 --- a/clean/fsr/README.md +++ b/clean/fsr/README.md @@ -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 `\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`, diff --git a/clean/fsr/src/frontend.cpp b/clean/fsr/src/frontend.cpp index ace7a686..84de5585 100644 --- a/clean/fsr/src/frontend.cpp +++ b/clean/fsr/src/frontend.cpp @@ -58,6 +58,9 @@ struct FrontEnd::UpscaleContext { bool amdLast = false; // the last frame was one of AMD's FSR for such a frame (AMD's history continues) bool broken = false; // the upscaler failed for good: it is not parked uint64_t brokenFrames = 0; // bug hunt F1: frames since the upscaler failed for good (it is not called again) + uint64_t amdFailures = 0; // B8: AMD FSR dispatches of this context that failed (logged once, counted) + uint64_t bothFailed = 0; // B8: frames that neither AMD's FSR nor the stand-in could upscale (logged once, counted) + uint64_t amdCreateTries = 0; // B8: per-frame attempts to make AMD's context for a bad frame (made at the 1st, 2nd, 3rd, 10th ...) uint32_t resizes = 0; // bug hunt H: upscalers created again for the frames' output size (Config::maxOutputResizes) bool resizeFailed = false; // ... and such a create failed: no more attempts for this context // Issue #58: upscalers and stand-ins a resize replaced. Command lists the game recorded before may still use them @@ -948,6 +951,69 @@ bool FrontEnd::ActivateSplit(UpscaleContext& u, const WgUpscalerBackend* backend return false; } +// B8: AMD's failures are logged once per context (the first), counted; the counts are logged when the context is destroyed. +void FrontEnd::NoteAmdFailure(UpscaleContext& u, const std::string& tag, uint64_t frameIndex, ffxReturnCode_t rc) { + if (++u.amdFailures == 1) + Log(tag + " WARNING U" + std::to_string(u.id) + ": frame " + std::to_string(frameIndex) + ": AMD FSR dispatch failed (" + RcName(rc) + + "); its later failures in this context are counted, not logged"); +} + +// B8: the one line when neither AMD's FSR nor the stand-in could upscale a frame: it names both failures; the error goes back to +// the game for this frame and the later such frames of the context (counted, the count is logged at the context's end). +void FrontEnd::NoteBothFailed(UpscaleContext& u, const std::string& tag, uint64_t frameIndex, const std::string& amdWhy, + const std::string& standInWhy) { + if (++u.bothFailed == 1) + Log(tag + " ERROR U" + std::to_string(u.id) + ": frame " + std::to_string(frameIndex) + ": no upscaler could take this frame: AMD FSR " + + amdWhy + "; the stand-in " + standInWhy + + "; the error goes back to the game for this frame and the later such frames of this context (counted, logged once)"); +} + +// B8: the stand-in upscales `frame` (cut to its textures); false with the reason in *why. +bool FrontEnd::StandInFrame(UpscaleContext& u, WgFrame& frame, std::string* why) { + if (!UsableForStandIn(frame)) { + *why = "cannot take this frame (no command list, colour texture or output texture)"; + return false; + } + if (!u.fallbackBackend && platform_.standIn) u.fallbackBackend = platform_.standIn(config_); + if (!u.fallbackBackend || u.fallbackBackend == u.backend) { + *why = "is not available"; + return false; + } + if (!u.fallback) { + WgCreateInfo info = u.createInfo; + AdaptCreateInfo(u.fallbackBackend, nullptr, info); + if (u.fallbackBackend->create(&info, &u.fallback) != WG_OK) { + u.fallback = nullptr; + *why = "could not be created"; + return false; + } + } + AdaptFrame(u.fallbackBackend, frame); + const WgResult r = u.fallbackBackend->dispatch(u.fallback, &frame); + if (r != WG_OK) { + *why = std::string("dispatch failed (") + WgResultText(r) + ")"; + return false; + } + u.resetNext = true; + if (platform_.afterDispatch) platform_.afterDispatch(frame, true); + Report(u, UpscaleState::StandIn, &frame); + return true; +} + +// B8: AMD's FSR was asked for this frame and its dispatch failed (rc). +ffxReturnCode_t FrontEnd::AfterAmdFailure(UpscaleContext& u, const std::string& tag, uint64_t frameIndex, WgFrame* frame, ffxReturnCode_t rc, + bool perceptual, const std::string& noFrameWhy) { + NoteAmdFailure(u, tag, frameIndex, rc); + if (config_.customSlot) return rc; // the host upscales on the error + std::string why; + if (perceptual) why = "is not used for non-linear (sRGB / PQ) input"; + else if (!frame) why = noFrameWhy.empty() ? "has no mapped frame" : "has no mapped frame (" + noFrameWhy + ")"; + else if (StandInFrame(u, *frame, &why)) return FFX_API_RETURN_OK; + NoteBothFailed(u, tag, frameIndex, "dispatch failed (" + std::string(RcName(rc)) + ")", why); + Report(u, UpscaleState::Failed, nullptr, "AMD FSR dispatch failed (" + std::string(RcName(rc)) + "); the stand-in " + why); + return rc; +} + // Bug hunt F1 (a): the upscaler failed for good (u.broken) and AMD's FSR did not take the context over (none, or // fallback_to_amd = 0): it is not called again (each call would fail and log again, every frame). In OptiScaler's custom // slot the error goes back to the host, which makes a new context (Dispatch's run of failures decides when that stops); @@ -1192,6 +1258,9 @@ ffxReturnCode_t FrontEnd::DestroyContext(ffxContext* context, const ffxAllocatio if (park) u->amd = nullptr; } } + if (u->bothFailed || u->amdFailures) + Log(tag + " note U" + std::to_string(u->id) + ": fallback counts: AMD FSR failed " + std::to_string(u->amdFailures) + " time(s), " + + std::to_string(u->bothFailed) + " frame(s) could be upscaled by neither AMD FSR nor the stand-in (the error went back to the game)"); Log(tag + " -> " + RcName(rc) + " (U" + std::to_string(u->id) + " destroyed after " + std::to_string(u->dispatches) + " dispatches" + (pending && !pending->Done() ? "; its upscaler is still starting and is destroyed when it has" : "") + (park ? "; its upscaler is parked for " + std::to_string(config_.contextParkMs) + @@ -1582,9 +1651,11 @@ ffxReturnCode_t FrontEnd::DispatchUpscale(UpscaleContext& u, const ffxDispatchDe if (u.mode == UpscaleContext::Mode::Amd) { const ffxReturnCode_t rc = u.amdApi->dispatch(&u.amd, &d.header); Log(tag + " -> " + RcName(rc) + " (forwarded to AMD)"); - Report(u, rc == FFX_API_RETURN_OK ? UpscaleState::Amd : UpscaleState::Failed, nullptr, - "AMD FSR dispatch failed: " + std::string(RcName(rc))); - return rc; + if (rc == FFX_API_RETURN_OK) { + Report(u, UpscaleState::Amd, nullptr, "AMD FSR dispatch failed: " + std::string(RcName(rc))); + return rc; + } + return AfterAmdFailure(u, tag, frameIndex, &frame, rc, false); // B8 } Log(tag + " note U" + std::to_string(u.id) + ": AMD FSR is not available for this context; the stand-in upscales its frames"); } @@ -1631,9 +1702,11 @@ ffxReturnCode_t FrontEnd::DispatchUpscale(UpscaleContext& u, const ffxDispatchDe amd.reset = true; // AMD's history starts here const ffxReturnCode_t rc = u.amdApi->dispatch(&u.amd, &amd.header); Log(tag + " -> " + RcName(rc) + " (forwarded to AMD)"); - Report(u, rc == FFX_API_RETURN_OK ? UpscaleState::Amd : UpscaleState::Failed, nullptr, - "AMD FSR dispatch failed: " + std::string(RcName(rc))); - return rc; + if (rc == FFX_API_RETURN_OK) { + Report(u, UpscaleState::Amd, nullptr, "AMD FSR dispatch failed: " + std::string(RcName(rc))); + return rc; + } + return AfterAmdFailure(u, tag, frameIndex, &frame, rc, false); // B8 } Log(tag + " note U" + std::to_string(u.id) + ": AMD FSR is not available for this context; the stand-in upscales the frames the upscaler does not take"); } @@ -1673,9 +1746,11 @@ ffxReturnCode_t FrontEnd::DispatchUpscale(UpscaleContext& u, const ffxDispatchDe if (u.mode == UpscaleContext::Mode::Amd) { const ffxReturnCode_t rc = u.amdApi->dispatch(&u.amd, &d.header); Log(tag + " -> " + RcName(rc) + " (forwarded to AMD after the failure)"); - Report(u, rc == FFX_API_RETURN_OK ? UpscaleState::Amd : UpscaleState::Failed, nullptr, - u.failReason + "; AMD FSR dispatch failed: " + RcName(rc)); - return rc; + if (rc == FFX_API_RETURN_OK) { + Report(u, UpscaleState::Amd, nullptr, u.failReason + "; AMD FSR dispatch failed: " + RcName(rc)); + return rc; + } + return AfterAmdFailure(u, tag, frameIndex, mapped ? &frame : nullptr, rc, false, mapped ? std::string() : problem); // B8 } Report(u, UpscaleState::Failed, nullptr, u.failReason + "; AMD fallback is unavailable"); Log(tag + " -> ERROR_RUNTIME_ERROR"); @@ -1697,52 +1772,47 @@ ffxReturnCode_t FrontEnd::BadFrame(UpscaleContext& u, const ffxDispatchDescUpsca if (!u.amdLast) amd.reset = true; const ffxReturnCode_t rc = u.amdApi->dispatch(&u.amd, &amd.header); Log(tag + " -> " + RcName(rc) + " (forwarded to AMD)"); - Report(u, rc == FFX_API_RETURN_OK ? UpscaleState::Amd : UpscaleState::Failed, nullptr, - u.failReason + "; AMD FSR dispatch failed: " + RcName(rc)); - return rc; + if (rc == FFX_API_RETURN_OK) { + Report(u, UpscaleState::Amd, nullptr, u.failReason + "; AMD FSR dispatch failed: " + RcName(rc)); + return rc; + } + return AfterAmdFailure(u, tag, frameIndex, frame, rc, amdOnly); // B8: a perceptual frame never reaches the stand-in } } u.resetNext = true; // this frame goes past the upscaler: its next one starts without history - // 1. The stand-in, on the frame as it is or cut to its textures. - if (!amdOnly && frame && UsableForStandIn(*frame)) { - if (!u.fallbackBackend && platform_.standIn) u.fallbackBackend = platform_.standIn(config_); - if (u.fallbackBackend && u.fallbackBackend != u.backend) { - if (!u.fallback) { - WgCreateInfo info = u.createInfo; - AdaptCreateInfo(u.fallbackBackend, nullptr, info); - if (u.fallbackBackend->create(&info, &u.fallback) != WG_OK) u.fallback = nullptr; - } - AdaptFrame(u.fallbackBackend, *frame); - if (u.fallback && u.fallbackBackend->dispatch(u.fallback, frame) == WG_OK) { - if (run == 1) - Log(tag + " note U" + std::to_string(u.id) + ": " + what + ": the stand-in upscales this frame (" + - std::to_string(frame->render_width) + "x" + std::to_string(frame->render_height) + "); the context stays WrongGPU's (AMD FSR takes it over after " + - std::to_string(kBadFrameRun) + " such frames in a row)"); - u.amdLast = false; - if (platform_.afterDispatch) platform_.afterDispatch(*frame, true); - Report(u, UpscaleState::StandIn, frame); - return FFX_API_RETURN_OK; - } + // 1. The stand-in, on the frame as it is or cut to its textures (not for non-linear input: that is AMD's alone). + std::string standWhy = amdOnly ? "is not used for non-linear (sRGB / PQ) input" : "has no mapped frame"; + if (!amdOnly && frame) { + if (StandInFrame(u, *frame, &standWhy)) { + if (run == 1) + Log(tag + " note U" + std::to_string(u.id) + ": " + what + ": the stand-in upscales this frame (" + + std::to_string(frame->render_width) + "x" + std::to_string(frame->render_height) + "); the context stays WrongGPU's (AMD FSR takes it over after " + + std::to_string(kBadFrameRun) + " such frames in a row)"); + u.amdLast = false; + return FFX_API_RETURN_OK; } } // 2. AMD's FSR, for this frame only: the context made from the saved create parameters, kept for later ones. - if (config_.fallbackToAmd && CreateAmdFromSaved(u, call, false) == FFX_API_RETURN_OK) { + if (config_.fallbackToAmd && (u.amd || LogPoint(++u.amdCreateTries)) && CreateAmdFromSaved(u, call, false) == FFX_API_RETURN_OK) { ffxDispatchDescUpscale amd = d; if (!u.amdLast) amd.reset = true; // AMD's history is old, or never started u.amdLast = true; const ffxReturnCode_t rc = u.amdApi->dispatch(&u.amd, &amd.header); - if (run == 1 || rc != FFX_API_RETURN_OK) + if (rc != FFX_API_RETURN_OK) + NoteAmdFailure(u, tag, frameIndex, rc); // B8: once per context, counted + else if (run == 1) Log(tag + " note U" + std::to_string(u.id) + ": " + what + ": AMD FSR upscales this frame -> " + RcName(rc) + "; the context stays WrongGPU's (AMD FSR takes it over after " + std::to_string(kBadFrameRun) + " such frames in a row)"); + if (rc != FFX_API_RETURN_OK) { + u.amdLast = false; + NoteBothFailed(u, tag, frameIndex, "dispatch failed (" + std::string(RcName(rc)) + ")", standWhy); // B8: the stand-in came first + } Report(u, rc == FFX_API_RETURN_OK ? UpscaleState::Amd : UpscaleState::Failed, nullptr, what + "; AMD FSR dispatch failed: " + RcName(rc), what); return rc; } - // 3. Nobody: the frame fails, the context stays. - if (run == 1 || ShouldLogDispatch(frameIndex)) - Log(tag + (amdOnly ? " note U" : " ERROR U") + std::to_string(u.id) + ": " + what + ": no upscaler for this frame (" + - (config_.fallbackToAmd ? "AMD FSR is not available" : "fallback_to_amd = 0") + ") -> " + - (result == WG_ERROR_PARAMETER ? "ERROR_PARAMETER" : "ERROR_RUNTIME_ERROR")); + // 3. Nobody: the frame fails, the context stays. B8: one line naming both failures (AMD's FSR was never reached here). + NoteBothFailed(u, tag, frameIndex, config_.fallbackToAmd ? "is not available" : "is off (fallback_to_amd = 0)", standWhy); Report(u, UpscaleState::Failed, nullptr, what + "; AMD fallback is unavailable"); return result == WG_ERROR_PARAMETER ? FFX_API_RETURN_ERROR_PARAMETER : FFX_API_RETURN_ERROR_RUNTIME_ERROR; } @@ -1874,10 +1944,25 @@ ffxReturnCode_t FrontEnd::Dispatch(ffxContext* context, const ffxDispatchDescHea std::lock_guard lock(u->mutex); ++u->dispatches; const ffxReturnCode_t rc = original->dispatch(&u->amd, desc); - if (log || rc != FFX_API_RETURN_OK) Log(tag + " -> " + RcName(rc) + " (forwarded to AMD)"); - if (desc->type == FFX_API_DISPATCH_DESC_TYPE_UPSCALE) + const bool takenOver = u->upscaler != nullptr; // WrongGPU's upscaler is kept, unused: the context was handed to AMD + if (log || (rc != FFX_API_RETURN_OK && !takenOver)) Log(tag + " -> " + RcName(rc) + " (forwarded to AMD)"); + if (desc->type == FFX_API_DISPATCH_DESC_TYPE_UPSCALE) { + if (rc != FFX_API_RETURN_OK && takenOver) { + // B8: a context WrongGPU handed to AMD's FSR whose AMD dispatch fails: the stand-in for an ordinary frame, never for + // a non-linear (sRGB / PQ) one; both failing is the one line naming both (a context that was AMD's from the start + // is the game's own FSR and keeps AMD's answer) + const auto& ud = *reinterpret_cast(desc); + const bool perceptual = (ud.flags & (FFX_UPSCALE_FLAG_NON_LINEAR_COLOR_SRGB | FFX_UPSCALE_FLAG_NON_LINEAR_COLOR_PQ)) != 0; + const uint64_t frameIndex = u->dispatches - 1; + WgFrame frame{}; + std::string problem, notes; + const bool mapped = !perceptual && MapDispatch(ud, u->create, config_, platform_.resourceInfo, u->map, frameIndex, frame, + problem, notes); + return AfterAmdFailure(*u, tag, frameIndex, mapped ? &frame : nullptr, rc, perceptual, mapped ? std::string() : problem); + } Report(*u, rc == FFX_API_RETURN_OK ? UpscaleState::Amd : UpscaleState::Failed, nullptr, "AMD FSR dispatch failed: " + std::string(RcName(rc))); + } return rc; } const ffxReturnCode_t rc = original->dispatch(context, desc); diff --git a/clean/fsr/src/frontend.h b/clean/fsr/src/frontend.h index e33a9b74..322a4667 100644 --- a/clean/fsr/src/frontend.h +++ b/clean/fsr/src/frontend.h @@ -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). diff --git a/clean/fsr/tests/test_frontend.cpp b/clean/fsr/tests/test_frontend.cpp index 2db49e57..d750421a 100644 --- a/clean/fsr/tests/test_frontend.cpp +++ b/clean/fsr/tests/test_frontend.cpp @@ -197,14 +197,17 @@ static const WgUpscalerBackend* g_useBackend = &g_backend; static std::vector 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(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; @@ -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(); @@ -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()) { @@ -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}) { @@ -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 refused; + for (int i = 1; i <= kFrames; ++i) refused.insert(i); + g_unsupportedAt = refused; + std::vector 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}; @@ -3493,6 +3641,7 @@ int main() { TestRegistryEntries(); TestUnsupportedFrames(); TestRefusedFrames(); + TestRefusedFrameContract(); TestSplitBlocked(); TestRuntimeProviderName(); TestWithoutModelKItems(); diff --git a/clean/fsr/tests/test_refusal_integration.cpp b/clean/fsr/tests/test_refusal_integration.cpp new file mode 100644 index 00000000..38781bda --- /dev/null +++ b/clean/fsr/tests/test_refusal_integration.cpp @@ -0,0 +1,225 @@ +// SPDX-License-Identifier: MIT +// B8 (REVIEW-215d P2): the front end and the REAL network backend (the runtime DLL's Context, the same code a game runs) with a +// controllable executor (executor=dx12-test: the D3D12 route, scripted frame by frame), driving refused frames for model K and, as +// the control, model M; with and without AMD's FSR; repeated; with the stand-in working and with it failing. In every scenario +// the refusal comes from the backend's own code path (backend.cpp dispatch_record, refused_frame_action), never a counter, and +// HIP is never started: the runtime DLL's counter of HIP executors made stays 0 (wgDiagHipExecutorsMade, exported for this test +// only, not part of the public header), no amdhip64 module is loaded in this process, and the backend never logs the HIP runtime's +// start. +// +// Windows, a D3D12 device (WARP is enough, no GPU slot needed; a GPU through gpu_slot also works) and the runtime DLL of a build. +// It is a release check, not a CI test (CI has no runtime DLL): the release lead's script under tools/release builds it from this +// source and runs it with the DLL of the build; without the DLL it FAILS (exit 2), a gate that runs it cannot pass by skipping +// (--skip-if-missing is for a developer's loop). The front end's fixtures are test_frontend.cpp's, included below. Build, from +// the repository root, with the registry header generated (python clean/wgshared/tools/gen_registry.py --out-dir ) and the +// runtime DLL built (python clean/runner/build.py --rocm --out --runtime-only): +// clang++ -std=c++17 -O1 -D_CRT_SECURE_NO_WARNINGS -I /include -I clean/wgshared/include -I clean/fsr/include +// -I clean/third_party/fidelityfx-sdk-1.1.4 clean/fsr/tests/test_refusal_integration.cpp +// clean/fsr/src/{frontend,param_map,ffx_describe,config,fsr30_hook,fsr30_map,amd_compare,async_create}.cpp +// -ld3d12 -ldxgi -o test_refusal_integration.exe +// test_refusal_integration.exe /wronggpu_runtime.dll +#define main wg_frontend_unit_main +#include "test_frontend.cpp" +#undef main + +#define NOMINMAX +#include +#include +#include + +#include +#include + +namespace { + +template +void Release(T*& p) { + if (p) p->Release(); + p = nullptr; +} + +struct Gpu { + ID3D12Device* device = nullptr; + ID3D12CommandQueue* queue = nullptr; + ID3D12CommandAllocator* alloc = nullptr; + ID3D12GraphicsCommandList* list = nullptr; + ID3D12Resource *color = nullptr, *depth = nullptr, *motion = nullptr, *output = nullptr; +}; + +constexpr UINT kRenderW = 1706, kRenderH = 960, kOutW = 2560, kOutH = 1440; + +ID3D12Resource* MakeTexture(ID3D12Device* dev, DXGI_FORMAT fmt, UINT w, UINT h, D3D12_RESOURCE_FLAGS flags, + D3D12_RESOURCE_STATES state) { + D3D12_HEAP_PROPERTIES hp = {}; + hp.Type = D3D12_HEAP_TYPE_DEFAULT; + D3D12_RESOURCE_DESC d = {}; + d.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + d.Width = w; + d.Height = h; + d.DepthOrArraySize = 1; + d.MipLevels = 1; + d.Format = fmt; + d.SampleDesc.Count = 1; + d.Flags = flags; + ID3D12Resource* r = nullptr; + if (FAILED(dev->CreateCommittedResource(&hp, D3D12_HEAP_FLAG_NONE, &d, state, nullptr, IID_PPV_ARGS(&r)))) return nullptr; + return r; +} + +bool MakeGpu(Gpu& g) { + IDXGIFactory4* f = nullptr; + IDXGIAdapter* warp = nullptr; + if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&f))) || FAILED(f->EnumWarpAdapter(IID_PPV_ARGS(&warp)))) return false; + const HRESULT hr = D3D12CreateDevice(warp, D3D_FEATURE_LEVEL_12_0, IID_PPV_ARGS(&g.device)); + warp->Release(); + f->Release(); + if (FAILED(hr)) return false; + D3D12_COMMAND_QUEUE_DESC qd = {}; + qd.Type = D3D12_COMMAND_LIST_TYPE_DIRECT; + if (FAILED(g.device->CreateCommandQueue(&qd, IID_PPV_ARGS(&g.queue))) || + FAILED(g.device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&g.alloc))) || + FAILED(g.device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, g.alloc, nullptr, IID_PPV_ARGS(&g.list)))) + return false; + const D3D12_RESOURCE_STATES srv = D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE; + g.color = MakeTexture(g.device, DXGI_FORMAT_R16G16B16A16_FLOAT, kRenderW, kRenderH, D3D12_RESOURCE_FLAG_NONE, srv); + g.depth = MakeTexture(g.device, DXGI_FORMAT_R32_FLOAT, kRenderW, kRenderH, D3D12_RESOURCE_FLAG_NONE, srv); + g.motion = MakeTexture(g.device, DXGI_FORMAT_R16G16_FLOAT, kRenderW, kRenderH, D3D12_RESOURCE_FLAG_NONE, + D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE | srv); + g.output = MakeTexture(g.device, DXGI_FORMAT_R16G16B16A16_FLOAT, kOutW, kOutH, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, + D3D12_RESOURCE_STATE_UNORDERED_ACCESS); + return g.color && g.depth && g.motion && g.output; +} + +void RealRefusals(const char* dllPath) { + HMODULE dll = LoadLibraryA(dllPath); + std::printf("runtime DLL %s\n", dll ? "loaded" : "NOT loaded"); + std::fflush(stdout); + CHECK(dll != nullptr); + if (!dll) return; + auto get = reinterpret_cast(reinterpret_cast(GetProcAddress(dll, "wgGetUpscalerBackend"))); + const WgUpscalerBackend* real = get ? get(WG_UPSCALER_INTERFACE_VERSION) : nullptr; + CHECK(real != nullptr); + if (!real) return; + Gpu g; + CHECK(MakeGpu(g)); + std::printf("WARP device and textures: %s\n", g.output ? "ready" : "FAILED"); + std::fflush(stdout); + if (!g.output) return; + using HipMadeFn = uint32_t (*)(); + const auto hipMade = reinterpret_cast(reinterpret_cast(GetProcAddress(dll, "wgDiagHipExecutorsMade"))); + CHECK(hipMade != nullptr); // the runtime DLL counts the HIP executors it makes (a diagnostic export) + CHECK(GetModuleHandleA("amdhip64_7.dll") == nullptr && GetModuleHandleA("amdhip64.dll") == nullptr); + + struct Scenario { + const char* name; + bool amd, standInCreateFails; + const char* script; + int frames; + }; + // frame 0 is taken; the refused frames follow; 'o' after them is taken again (the context stays usable, no counter) + const Scenario scenarios[] = { + {"AMD available", true, false, "orrrrr", 6}, + {"no AMD, stand-in works, five refusals in a row then taken", false, false, "orrrrroo", 8}, + {"no AMD, stand-in cannot be made: AMD and the stand-in both failed, the error goes back", false, true, "orrroo", 6}, + }; + for (const bool model_k : {true, false}) { // model K, and model M as the control + for (const Scenario& sc : scenarios) { + std::printf("scenario: %s, %s\n", model_k ? "model K" : "model M", sc.name); + std::fflush(stdout); + const int failuresBefore = g_failures; + Reset(); + g_originalAvailable = sc.amd; + g_standInCreateFails = sc.standInCreateFails; + g_useBackend = real; + g_resources[g.color] = {dxgi::R16G16B16A16_FLOAT, kRenderW, kRenderH}; + g_resources[g.depth] = {dxgi::R32_FLOAT, kRenderW, kRenderH}; + g_resources[g.motion] = {dxgi::R16G16_FLOAT, kRenderW, kRenderH}; + g_resources[g.output] = {dxgi::R16G16B16A16_FLOAT, kOutW, kOutH}; + const std::string options = std::string("executor=dx12-test;test_script=") + sc.script + (model_k ? ";model=K" : ""); + _putenv_s("WG_RUNTIME_OPTIONS", options.c_str()); + Config config; + config.hosted = true; + auto fe = MakeFrontEnd(config); + UpscaleCreate own(FFX_UPSCALE_ENABLE_HIGH_DYNAMIC_RANGE, /*withOverride=*/true); + own.version.versionId = model_k ? kWrongGpuKProviderVersionId : kWrongGpuProviderVersionId; + own.backend.device = g.device; + ffxContext ctx = nullptr; + const std::string tag = std::string(model_k ? "model K, " : "model M, ") + sc.name; + CHECK(fe->CreateContext(&ctx, &own.upscale.header, kMemCb) == FFX_API_RETURN_OK && fe->OwnContextCount() == 1); + CHECK(CountLog("network runtime ready: executor dx12-scripted") == 1); + ffxDispatchDescUpscale d = MakeDispatch(); + d.commandList = g.list; + d.color = Res(g.color, FFX_API_SURFACE_FORMAT_R16G16B16A16_FLOAT, kRenderW, kRenderH, FFX_API_RESOURCE_STATE_COMPUTE_READ); + d.depth = Res(g.depth, FFX_API_SURFACE_FORMAT_R32_FLOAT, kRenderW, kRenderH, FFX_API_RESOURCE_STATE_COMPUTE_READ); + d.motionVectors = Res(g.motion, FFX_API_SURFACE_FORMAT_R16G16_FLOAT, kRenderW, kRenderH, FFX_API_RESOURCE_STATE_PIXEL_COMPUTE_READ); + d.output = Res(g.output, FFX_API_SURFACE_FORMAT_R16G16B16A16_FLOAT, kOutW, kOutH, FFX_API_RESOURCE_STATE_UNORDERED_ACCESS); + d.renderSize = {kRenderW, kRenderH}; + d.upscaleSize = {kOutW, kOutH}; + g.alloc->Reset(); + g.list->Reset(g.alloc, nullptr); + std::vector rc; + std::vector state; + for (int i = 0; i < sc.frames; ++i) { + rc.push_back(fe->Dispatch(&ctx, &d.header)); + state.push_back(g_statuses.empty() ? UpscaleState::Failed : g_statuses.back().state); + } + g.list->Close(); + CHECK(rc[0] == FFX_API_RETURN_OK && state[0] == UpscaleState::Network); // frame 0 is the real backend's + // the refusals came from the real backend's code path (dispatch_record): K's and M's own line, once, not a counter + CHECK(CountLog(model_k ? "model K: the D3D12 executor does not take this frame" + : "executor dx12 (default on RDNA 4) does not take this frame") == 1); + if (sc.amd) { + // the first refused frame hands the context to AMD's FSR for good: the rest are AMD's + for (int i = 0; i < sc.frames; ++i) CHECK(rc[i] == FFX_API_RETURN_OK); + CHECK(state[1] == UpscaleState::Amd && !g_amd.empty() && g_amd.back().fn == "dispatch"); + for (int i = 2; i < sc.frames; ++i) CHECK(state[i] == UpscaleState::Amd); + CHECK(CountLog("WrongGPU frame: frame=") == 2 && g_standInFrames.empty()); // the backend was asked for frames 0 and 1 only + } else if (!sc.standInCreateFails) { + // the stand-in takes each refused frame (five in a row included); the context stays WrongGPU's: the taken frames + // after the refusals are the real backend's again + for (int i = 0; i < sc.frames; ++i) CHECK(rc[i] == FFX_API_RETURN_OK); + for (int i = 1; i <= 5; ++i) CHECK(state[i] == UpscaleState::StandIn); + CHECK(state[6] == UpscaleState::Network && state[7] == UpscaleState::Network); + CHECK(g_standInFrames.size() == 5 && g_amd.empty()); + } else { + // neither AMD nor the stand-in: the refused frames (1-3) return the error with ONE line naming both failures; the + // frames the backend takes afterwards (4, 5) are fine again + for (int i = 1; i <= 3; ++i) CHECK(rc[i] != FFX_API_RETURN_OK && state[i] == UpscaleState::Failed); + CHECK(rc[4] == FFX_API_RETURN_OK && rc[5] == FFX_API_RETURN_OK); + CHECK(CountLog("no upscaler could take this frame: AMD FSR is not available; the stand-in could not be created") == 1); + CHECK(g_amd.empty() && g_standInFrames.empty()); + } + // HIP never started: no HIP runtime line, no amdhip64 module, no HIP executor constructed + CHECK(CountLog("about to start the HIP runtime") == 0); + CHECK(GetModuleHandleA("amdhip64_7.dll") == nullptr && GetModuleHandleA("amdhip64.dll") == nullptr); + CHECK(hipMade && hipMade() == 0); + if (g_failures != failuresBefore) { // the log of a failed scenario + std::lock_guard lock(g_logMutex); + for (const std::string& line : g_log) std::printf(" log: %s\n", line.c_str()); + } + CHECK(fe->DestroyContext(&ctx, kMemCb) == FFX_API_RETURN_OK); + std::printf(" %s: states", tag.c_str()); + for (UpscaleState s : state) std::printf(" %d", (int)s); + std::printf(", checks so far %d, failures %d\n", g_checks, g_failures); + } + } + _putenv_s("WG_RUNTIME_OPTIONS", ""); + Release(g.color); + Release(g.depth); + Release(g.motion); + Release(g.output); +} + +} // namespace + +int main(int argc, char** argv) { + const bool skipMissing = argc > 1 && std::strcmp(argv[1], "--skip-if-missing") == 0; + const char* dll = argc > 1 && !skipMissing ? argv[1] : std::getenv("WG_TEST_RUNTIME_DLL"); + if (!dll || !*dll) { + std::printf("%s: no runtime DLL (argument or WG_TEST_RUNTIME_DLL)\n", skipMissing ? "SKIP" : "FAIL"); + return skipMissing ? 0 : 2; + } + RealRefusals(dll); + std::printf("%d checks, %d failures\n", g_checks, g_failures); + return g_failures ? 1 : 0; +} diff --git a/clean/runtime/interop/backend.cpp b/clean/runtime/interop/backend.cpp index 31486b09..37ed0f99 100644 --- a/clean/runtime/interop/backend.cpp +++ b/clean/runtime/interop/backend.cpp @@ -44,6 +44,10 @@ struct WgUpscaler; namespace wgi { +// HIP executors made by this process's contexts (wgDiagHipExecutorsMade): the integration test of the refused-frame contract +// checks that none was made. +std::atomic g_hip_executors_made{0}; + namespace { // the converter maps and model K's folder, from the model table (clean/runtime/model_select.h) @@ -168,6 +172,7 @@ struct Options { int test_view = 0; // d3d12-test executor: 0 color, 1 depth, 2 motion bool test_handles = false; // d3d12-test executor: make shared objects with NT handles (sharing paths) bool test_keep = false; // d3d12-test executor with test_handles: keep the arena for later contexts (pool) + std::string test_script; // dx12-test executor: one character per frame (executor.h make_scripted_dx12_executor) bool timing_stages = true; // timing_stages=0: keep D3D12 full-span timestamps only bool timing_log = false; // one timing summary line every 300 dispatches bool watchdog = false; // diagnostics: report a dispatch or network frame stuck for more than 3 s @@ -221,6 +226,7 @@ Options parse_options(const char* text) { else if (key == "model_format") o.model_format = value; else if (key == "test_handles") o.test_handles = value == "1"; else if (key == "test_keep") o.test_keep = value == "1"; + else if (key == "test_script") o.test_script = value; else if (key == "share_path") o.share_path = value; else if (key == "timing_log") o.timing_log = value == "1"; else if (key == "timing_stages") o.timing_stages = value != "0"; @@ -600,6 +606,7 @@ class Context { say(WG_LOG_WARN, "WG_RUNTIME_OPTIONS is longer than 4095 characters and is ignored"); } Options o = parse_options(text.c_str()); + bool test_model_k = false; // dx12-test: the model the options name (no configure_hip there) { // the executor (executor_select.h): the option, else the default (dx12 for model M on RDNA 4); one log line why ExecutorRequest q; q.executor = o.executor; @@ -609,6 +616,7 @@ class Context { std::string merr; // model K only (Model L runs on the D3D12 executor in builds with it: runtime.cpp, native D3D12 backend 3) q.model_k = wgrt::model_from_options(probe, &model, &merr) && model == wgrt::ModelId::K; + test_model_k = q.model_k; q.adapter_known = adapter_info(device_, &q.adapter, &q.vendor_id, &q.software); q.sparkle_guard = o.sparkle_guard > 0.0f; q.capture = !o.capture_dir.empty(); @@ -632,7 +640,7 @@ class Context { split_ = split; share_path_opt_ = o.share_path; input_copy_opt_ = o.input_copy; - if (!split && o.executor != "dx12") + if (!split && o.executor != "dx12" && o.executor != "dx12-test") return fail(WG_ERROR_UNSUPPORTED, "the front end offers no command-list split (interface version 2)"); if (!o.unknown.empty()) say(WG_LOG_INFO, "options passed on to the network runtime: " + o.unknown); if (o.motion_outres != "original" && o.motion_outres != "point") @@ -645,6 +653,10 @@ class Context { bool dx12 = false; if (o.executor == "d3d12-test") { exec_ = make_test_executor(o.test_view, o.test_handles, o.test_keep); + } else if (o.executor == "dx12-test") { +#ifndef WG_NO_MODEL_K + model_k_ = test_model_k; +#endif } else if (o.executor == "hip" || o.executor == "dx12") { if (!configure_hip(o, &err)) return fail(WG_ERROR, err); } else { @@ -659,15 +671,17 @@ class Context { // the game's command list. Paths without D3D12 ports, and the diagnostics that need HIP, got the HIP executor // above (choose_executor). (Motion at output resolution: the D3D12 ports of the emo / pmo kernels read the game's // motion texture with motion_outres=original; with point the executor's D3D12 pass resamples it.) - dx12 = o.executor == "dx12"; + const bool dx12_test = o.executor == "dx12-test"; + dx12 = o.executor == "dx12" || dx12_test; + if (dx12_test) dx12_default_ = true; // the scripted executor stands in for the default D3D12 executor if (dx12) { - exec_ = make_d3d12_executor(rt_cfg_); + exec_ = dx12_test ? make_scripted_dx12_executor(o.test_script) : make_d3d12_executor(rt_cfg_); exec_->set_log_sink([this](bool warning, const std::string& m) { say(warning ? WG_LOG_WARN : WG_LOG_INFO, "D3D12 executor: " + m); }); no_slots_ = true; // its kernels read the game's textures: no slot buffers (fences and a 128 KB flag arena only) if (!exec_->prepare(&err) || !exec_->prepare_device(device_, &err) || - !make_shared_objects(false, "arena", &err) || !exec_->init(device_, set_, &err)) { + (!dx12_test && !make_shared_objects(false, "arena", &err)) || !exec_->init(device_, set_, &err)) { // model K: no HIP route in a game; the one error line below is the whole report, the game keeps its own upscaler if (model_k_) return fail(WG_ERROR_UNSUPPORTED, dx12_refused_line_k(dx12_default_, err)); say(WG_LOG_WARN, dx12_refused_line(dx12_default_, err)); @@ -687,6 +701,7 @@ class Context { } if (!dx12 && !exec_) { if (!split) return fail(WG_ERROR_UNSUPPORTED, "the front end offers no command-list split (interface version 2)"); + ++g_hip_executors_made; exec_ = make_hip_executor(rt_cfg_, adapter_); } if (!dx12) { @@ -1204,6 +1219,7 @@ class Context { if (!split_) { err = "the front end offers no command-list split (interface version 2)"; } else { + ++g_hip_executors_made; hip = make_hip_executor(rt_cfg_, adapter_); hip->set_log_sink([this](bool warning, const std::string& m) { say(warning ? WG_LOG_WARN : WG_LOG_INFO, "destroy: " + m); @@ -2215,6 +2231,10 @@ const WgUpscalerBackend kBackend = {sizeof(WgUpscalerBackend), WG_UPSCALER_INTER } // namespace +// A diagnostic for tests of the refused-frame contract: how many HIP executors this process's contexts have made (none in a +// game: HIP is an explicit diagnostic only). +extern "C" __declspec(dllexport) uint32_t wgDiagHipExecutorsMade() { return wgi::g_hip_executors_made.load(); } + // Interface version 2 only: the backend needs the front end's command-list split. extern "C" __declspec(dllexport) const WgUpscalerBackend* wgGetUpscalerBackend(uint32_t interface_version) { return interface_version == WG_UPSCALER_INTERFACE_VERSION ? &kBackend : nullptr; diff --git a/clean/runtime/interop/executor.h b/clean/runtime/interop/executor.h index c9a0d881..f8be0694 100644 --- a/clean/runtime/interop/executor.h +++ b/clean/runtime/interop/executor.h @@ -231,6 +231,11 @@ std::unique_ptr make_hip_executor(const WgRtConfig& cfg, const std::st // handles: behave like an executor that imports the shared objects (tests of the sharing paths). // keep (with handles): also like one whose arena import is kept for later contexts (tests of the arena pool). std::unique_ptr make_test_executor(int view, bool handles, bool keep = false); +// executor=dx12-test (tests of the backend's frame paths, no GPU work): an executor on the D3D12 route (records into the game's +// list, no slots, no fences) that answers each frame as `script` says, one character per frame, the last repeated: 'o' the frame +// is taken (nothing is recorded, the output counts as written), 'r' refused ("frame not taken (nothing recorded)", as the D3D12 +// executor refuses a render size or a missing output texture), 'f' a failure inside the network. +std::unique_ptr make_scripted_dx12_executor(const std::string& script); // executor=dx12: cfg as for the HIP executor (its backend field is ignored: the pure D3D12 device on the game's device). std::unique_ptr make_d3d12_executor(const WgRtConfig& cfg); // The D3D12 executor's output route of its last frame (one phrase for the log), empty for other executors. diff --git a/clean/runtime/interop/executor_select.h b/clean/runtime/interop/executor_select.h index 5dc379bd..c5c84115 100644 --- a/clean/runtime/interop/executor_select.h +++ b/clean/runtime/interop/executor_select.h @@ -63,7 +63,7 @@ inline std::string k_sparkle_off_line() { inline ExecutorChoice choose_executor(const ExecutorRequest& r) { ExecutorChoice c; const std::string& e = r.executor; - if (e == "d3d12-test") { + if (e == "d3d12-test" || e == "dx12-test") { // the offline test executors (dx12-test: the D3D12 route, scripted) c.executor = e; return c; } diff --git a/clean/runtime/interop/executor_test.cpp b/clean/runtime/interop/executor_test.cpp index 48913b67..105c22ae 100644 --- a/clean/runtime/interop/executor_test.cpp +++ b/clean/runtime/interop/executor_test.cpp @@ -4,6 +4,7 @@ // `out`, and signals `done`. view 0: color times the exposure (so the exposure plumbing is visible); 1: depth (d, d, // d); 2: motion in render pixels (mx, my, 0). WrongGPU's own code. #include +#include #include #include "executor.h" @@ -293,4 +294,50 @@ std::unique_ptr make_test_executor(int view, bool handles, bool keep) return std::unique_ptr(new TestExecutor(view, handles, keep)); } +// ---- executor=dx12-test: the scripted executor on the D3D12 route (executor.h make_scripted_dx12_executor) ---------------- + +namespace { + +class ScriptedDx12Executor final : public Executor { + public: + explicit ScriptedDx12Executor(std::string script) : script_(script.empty() ? "o" : std::move(script)) {} + const char* name() const override { return "dx12-scripted"; } + bool needs_handles() const override { return false; } + bool init(ID3D12Device*, const SharedSet&, std::string*) override { return true; } + bool submit(const Job&, bool*, std::string* err) override { + *err = "the scripted executor records into the game's list"; + return false; + } + bool records_into_list() const override { return true; } + bool record(const Job& job, ID3D12GraphicsCommandList*, bool* output_written, std::string* err) override { + const size_t k = job.value ? (size_t)(job.value - 1) : 0; + const char c = script_[k < script_.size() ? k : script_.size() - 1]; + const std::string n = std::to_string((unsigned long long)job.value); + *output_written = false; + if (c == 'r') { + *err = "frame not taken (nothing recorded): scripted refusal of frame " + n; + return false; + } + completed_ = job.value; + if (c == 'f') { + *err = "scripted failure of frame " + n + " inside the network"; + return false; + } + *output_written = true; + return true; + } + uint64_t completed() const override { return completed_; } + void drain() override {} + + private: + std::string script_; + uint64_t completed_ = 0; +}; + +} // namespace + +std::unique_ptr make_scripted_dx12_executor(const std::string& script) { + return std::unique_ptr(new ScriptedDx12Executor(script)); +} + } // namespace wgi