From 769ca3ddd60fd4c06d02966e44d467c45266b3c6 Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Fri, 13 Nov 2026 15:41:39 -0500 Subject: [PATCH 1/3] Front end: tests for keeping upscalers replaced by an output resize (fail before the fix) TestOutputResize expects the old upscaler to stay after a resize; TestResizeKeepsRetired resizes twice with a stand-in present and expects nothing destroyed before the context goes, everything destroyed with it, an immediate destroy while the device is lost, and the replaced upscaler destroyed when a context is parked. Against the current front end 6 checks fail: the resize destroys the old upscaler and stand-in at once (issue #58). Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> --- clean/fsr/tests/test_frontend.cpp | 69 ++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/clean/fsr/tests/test_frontend.cpp b/clean/fsr/tests/test_frontend.cpp index 9975a429..2d5f239a 100644 --- a/clean/fsr/tests/test_frontend.cpp +++ b/clean/fsr/tests/test_frontend.cpp @@ -609,7 +609,7 @@ static void TestOutputResize() { d.upscaleSize = {1920, 1080}; CHECK(fe->Dispatch(&ctx, &d.header) == FFX_API_RETURN_OK); CHECK(g_createCalls == 2 && g_createInfo.output_width == 1920 && g_createInfo.output_height == 1080); - CHECK(g_destroyed == 1); // the old upscaler + CHECK(g_destroyed == 0); // issue #58: the old upscaler is kept (the game's lists may still use it) CHECK(g_frames.size() == 2 && g_frames[1].output_width == 1920 && g_frames[1].output_height == 1080 && g_frames[1].reset == 2); CHECK(CountLog("output size 1920x1080 (the upscaler was created for 2560x1440") == 1); CHECK(fe->Dispatch(&ctx, &d.header) == FFX_API_RETURN_OK); @@ -626,10 +626,76 @@ static void TestOutputResize() { } CHECK(g_createCalls == 1 + 8); CHECK(CountLog("the upscaler is not created again for this context") == 1); + CHECK(g_destroyed == 0); // the 8 replaced upscalers are all kept until the context goes CHECK(fe->DestroyContext(&ctx, kMemCb) == FFX_API_RETURN_OK && fe->OwnContextCount() == 0); CHECK(g_destroyed == 9); } +// Issue #58: an upscaler (and its stand-in) replaced by an output resize may still be used by command lists the game +// recorded before (the copy into the output after the split, or a whole list not yet submitted). The front end has no +// fence of its own; its completion boundary is the context's destroy, so the replaced ones are destroyed only then, or +// at once when the device is lost (nothing of it runs any more). +static void TestResizeKeepsRetired() { + Reset(); + auto fe = MakeFrontEnd(Config{}); + UpscaleCreate c(FFX_UPSCALE_ENABLE_HIGH_DYNAMIC_RANGE); + c.upscale.maxRenderSize = {2560, 1440}; + ffxContext ctx = nullptr; + CHECK(fe->CreateContext(&ctx, &c.upscale.header, kMemCb) == FFX_API_RETURN_OK && ctx); + ffxDispatchDescUpscale d = MakeDispatch(); + CHECK(fe->Dispatch(&ctx, &d.header) == FFX_API_RETURN_OK); + // a frame the upscaler cannot take: the stand-in is made and upscales it + ffxDispatchDescUpscale bad = MakeDispatch(); + void* const smallColor = reinterpret_cast(uintptr_t(0xC058)); + g_resources[smallColor] = {dxgi::R16G16B16A16_FLOAT, 1280, 720}; + bad.color = Res(smallColor, FFX_API_SURFACE_FORMAT_R16G16B16A16_FLOAT, 1280, 720, FFX_API_RESOURCE_STATE_COMPUTE_READ); + CHECK(fe->Dispatch(&ctx, &bad.header) == FFX_API_RETURN_OK && g_standInFrames.size() == 1); + d.upscaleSize = {1920, 1080}; // first resize: the old upscaler and the stand-in are kept, not destroyed + CHECK(fe->Dispatch(&ctx, &d.header) == FFX_API_RETURN_OK && g_createCalls == 2); + CHECK(g_destroyed == 0 && g_standInDestroyed == 0); + CHECK(CountLog("the old upscaler and its stand-in are kept until the context is destroyed") == 1); + d.upscaleSize = {1600, 900}; // second resize: the first replaced upscaler is still kept + CHECK(fe->Dispatch(&ctx, &d.header) == FFX_API_RETURN_OK && g_createCalls == 3); + CHECK(g_destroyed == 0 && g_standInDestroyed == 0); + CHECK(fe->DestroyContext(&ctx, kMemCb) == FFX_API_RETURN_OK && fe->OwnContextCount() == 0); + CHECK(g_destroyed == 3 && g_standInDestroyed == 1); // both replaced upscalers, the current one and the stand-in + + // the device is lost: nothing of it runs any more, the replaced upscaler is destroyed at once + Reset(); + g_useTimer = true; // (the fake platform has deviceLost only with the timer) + Config noPark; + noPark.contextParkMs = 0; + fe = MakeFrontEnd(noPark); + ctx = nullptr; + CHECK(fe->CreateContext(&ctx, &c.upscale.header, kMemCb) == FFX_API_RETURN_OK && ctx); + d.upscaleSize = {0, 0}; + CHECK(fe->Dispatch(&ctx, &d.header) == FFX_API_RETURN_OK); + g_deviceLost = true; + d.upscaleSize = {1920, 1080}; + fe->Dispatch(&ctx, &d.header); + CHECK(g_createCalls == 2 && g_destroyed == 1); + g_deviceLost = false; + CHECK(fe->DestroyContext(&ctx, kMemCb) == FFX_API_RETURN_OK && g_destroyed == 2); + + // a parked context (destroyed with a working upscaler) destroys its replaced upscalers at the destroy + Reset(); + Config parkCfg; + parkCfg.contextParkMs = 1; + g_useTimer = true; + fe = MakeFrontEnd(parkCfg); + ctx = nullptr; + CHECK(fe->CreateContext(&ctx, &c.upscale.header, nullptr) == FFX_API_RETURN_OK && ctx); + d.upscaleSize = {0, 0}; + CHECK(fe->Dispatch(&ctx, &d.header) == FFX_API_RETURN_OK); + d.upscaleSize = {1920, 1080}; + CHECK(fe->Dispatch(&ctx, &d.header) == FFX_API_RETURN_OK && g_destroyed == 0); + CHECK(fe->DestroyContext(&ctx, nullptr) == FFX_API_RETURN_OK && fe->HasParked()); + CHECK(g_destroyed == 1); // the replaced one; the current one is parked + std::this_thread::sleep_for(std::chrono::milliseconds(30)); + CHECK(g_timerFn() == 0 && !fe->HasParked() && g_destroyed == 2); + g_useTimer = false; +} + // Bug hunt F1 (a): an upscaler that failed for good, without AMD's FSR to take the context over, is not called again: // the stand-in (or an error) for the later frames, logged at 1, 2, 3, 10... frames, not two ERROR lines per frame. static void TestBrokenNotCalledAgain() { @@ -3047,6 +3113,7 @@ int main() { TestCustomSlotFailedCreate(); TestCustomSlotInputRefused(); TestOutputResize(); + TestResizeKeepsRetired(); TestBrokenNotCalledAgain(); TestHostReentryStopped(); TestCustomSlotDestroyPath(); From 4149c52df8d9b8c22d46d8b1c665f26f5c105ff3 Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Fri, 13 Nov 2026 15:41:39 -0500 Subject: [PATCH 2/3] Front end: keep upscalers replaced by an output resize until the context is destroyed Fixes issue #58. ResizeForFrame no longer destroys the old upscaler and stand-in on the game's recording thread while command lists recorded before may still use them (the copy into the output after the split, or a list not yet submitted). The front end has no fence of its own; its completion boundary is the context's destroy (as for the AMD handoff in Fail). The replaced ones are kept on the context and destroyed by DestroyContext, parked or not, or at once while the device is lost. The resize line in the log says which. This also removes the stall of the backend's destroy (the old network's last frame) from the resize. At most kMaxResizes upscalers and their stand-ins are kept per context. Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> --- clean/fsr/src/frontend.cpp | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/clean/fsr/src/frontend.cpp b/clean/fsr/src/frontend.cpp index 604d6fdb..f488697b 100644 --- a/clean/fsr/src/frontend.cpp +++ b/clean/fsr/src/frontend.cpp @@ -60,6 +60,19 @@ struct FrontEnd::UpscaleContext { uint64_t brokenFrames = 0; // bug hunt F1: frames since the upscaler failed for good (it is not called again) uint32_t resizes = 0; // bug hunt H: upscalers created again for the frames' output size (kMaxResizes) 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 + // (the copy into the output after the split, or a whole list not yet submitted); the front end has no fence of its + // own, so they are destroyed only when the context is destroyed (its completion boundary, as for Fail's handoff), + // or at once while the device is lost. At most kMaxResizes upscalers (and their stand-ins) per context. + struct Replaced { + const WgUpscalerBackend* backend; + WgUpscaler* upscaler; + }; + std::vector replaced; + void DestroyReplaced() { + for (const Replaced& r : replaced) r.backend->destroy(r.upscaler); + replaced.clear(); + } std::mutex mutex; // one call at a time per context // This object (parked with its upscaler, stand-in, create info and options: the upscaler holds pointers into it) @@ -984,19 +997,31 @@ void FrontEnd::ResizeForFrame(UpscaleContext& u, WgFrame& frame, const std::stri WgResultText(r) + "; frames of that size are refused (the stand-in or AMD's FSR takes them)"); return; } - u.backend->destroy(u.upscaler); + // Issue #58: the old upscaler (and stand-in) may still be used by command lists the game recorded before this + // dispatch: kept until the context is destroyed, not destroyed here (that also took the old network's last frame of + // GPU time on the game's recording thread). Only a lost device runs nothing any more: then they go at once. + const bool lost = platform_.deviceLost && platform_.deviceLost(u.device); + const bool hadStandIn = u.fallback && u.fallbackBackend; + auto retire = [&](const WgUpscalerBackend* backend, WgUpscaler* upscaler) { + if (lost) backend->destroy(upscaler); + else u.replaced.push_back({backend, upscaler}); + }; + retire(u.backend, u.upscaler); u.upscaler = fresh; u.createInfo = info; - if (u.fallback && u.fallbackBackend) { // the stand-in is made again for the new size when it is needed - u.fallbackBackend->destroy(u.fallback); + if (hadStandIn) { // the stand-in is made again for the new size when it is needed + retire(u.fallbackBackend, u.fallback); u.fallback = nullptr; } ++u.resizes; frame.reset = 2; // a new upscaler: no history u.resetNext = u.restartNext = false; + const std::string old = hadStandIn ? "the old upscaler and its stand-in are" : "the old upscaler is"; Log(tag + " note U" + std::to_string(u.id) + ": output size " + now + " (the upscaler was created for " + was + ", the context's maximum is " + std::to_string(u.create.maxUpscaleSize.width) + "x" + std::to_string(u.create.maxUpscaleSize.height) + - "): the upscaler is created again for it; the context stays"); + "): the upscaler is created again for it; the context stays; " + + (lost ? old + " destroyed now (the device is lost)" + : old + " kept until the context is destroyed (the game's command lists may still use them)")); } std::string FrontEnd::FailureKey(const WgUpscalerBackend* backend, const UpscaleContext& u) const { @@ -1118,6 +1143,7 @@ ffxReturnCode_t FrontEnd::DestroyContext(ffxContext* context, const ffxAllocatio bool switched = false; // it could have been parked, but another upscaler context is in use { std::lock_guard lock(u->mutex); + u->DestroyReplaced(); // issue #58: the destroy is their completion boundary, parked or not pending = std::move(u->pending); // A working upscaler is parked for the next context with the same parameters (frontend.h), with its // stand-in; AMD's contexts always go now. From 8bd5f24526341ae716ae31e13a8c974c76eb6746 Mon Sep 17 00:00:00 2001 From: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> Date: Sat, 14 Nov 2026 12:14:43 -0500 Subject: [PATCH 3/3] Front end: bound the upscalers kept by output resizes to one per context (#58) A kept replaced upscaler holds its GPU memory until the context is destroyed. Measured on the real backend (RX 9070 XT, D3D12 executor): one Model M upscaler is 933 MiB at 4K Quality and 419 MiB at 1440p Quality, so the old limit of 8 kept ones meant 9 live upscalers and 8.1 GiB of growth at 4K Quality (3.6 GiB at 1440p; Model L 4K Quality 12.6 GiB). The limit is now Config::maxOutputResizes, default 1: a kept upscaler holds 0.91 GiB at 4K Quality. A further size change creates nothing (the log says so once; frames of that size are refused as before). The tests that need several kept upscalers raise the limit; a new test covers the default. Adds the by-hand test mode resize_real to test_dll_d3d12 (real runtime, process video memory by DXGI QueryVideoMemoryInfo and the runtime's own allocation total per step, in-flight lists, device removal and memory after the destroy). Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com> --- clean/fsr/src/config.h | 5 + clean/fsr/src/frontend.cpp | 14 +- clean/fsr/tests/test_dll_d3d12.cpp | 213 ++++++++++++++++++++++++++++- clean/fsr/tests/test_frontend.cpp | 39 +++++- 4 files changed, 258 insertions(+), 13 deletions(-) diff --git a/clean/fsr/src/config.h b/clean/fsr/src/config.h index 68ed13b7..54b76ce2 100644 --- a/clean/fsr/src/config.h +++ b/clean/fsr/src/config.h @@ -40,6 +40,11 @@ struct Config { // device, sizes, flags, options and model within that time takes it over instead of creating one (games that // re-create their upscaler at every step of a dynamic resolution). One parked upscaler at most; 0: never. uint32_t contextParkMs = 3000; + // Output-size changes (ResizeForFrame) per context. Each one creates an upscaler again and keeps the replaced one + // until the context is destroyed (issue #58), so this bounds the GPU memory that replaced upscalers hold: one + // Model M upscaler is about 0.9 GiB at 4K output (Quality), so 1 keeps that at or under about 1 GiB. Not a + // setting: no key in wronggpu.ini; tests set it. + uint32_t maxOutputResizes = 1; // Stand-in options, passed to the upscaler as "key=value;..." (see StandInOptions in standin_d3d12.cpp). std::string view = "upscale"; // upscale | warp | warpdiff | motion | depth | jitter diff --git a/clean/fsr/src/frontend.cpp b/clean/fsr/src/frontend.cpp index d815e5e1..ace7a686 100644 --- a/clean/fsr/src/frontend.cpp +++ b/clean/fsr/src/frontend.cpp @@ -58,12 +58,12 @@ 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) - uint32_t resizes = 0; // bug hunt H: upscalers created again for the frames' output size (kMaxResizes) + 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 // (the copy into the output after the split, or a whole list not yet submitted); the front end has no fence of its // own, so they are destroyed only when the context is destroyed (its completion boundary, as for Fail's handoff), - // or at once while the device is lost. At most kMaxResizes upscalers (and their stand-ins) per context. + // or at once while the device is lost. At most Config::maxOutputResizes upscalers (and their stand-ins) per context. struct Replaced { const WgUpscalerBackend* backend; WgUpscaler* upscaler; @@ -982,10 +982,11 @@ ffxReturnCode_t FrontEnd::BrokenFrame(UpscaleContext& u, WgFrame& frame, const s // output size is fixed at create: every such frame was refused with WG_ERROR_PARAMETER). Within the context's // maxUpscaleSize the upscaler is created again for the frame's size, here on the game's thread (as a create without // AMD's FSR to upscale meanwhile); the outer FidelityFX context stays, the new upscaler starts without history. At most -// kMaxResizes times per context (a game that changes the size every few frames gets the old behaviour after that), and -// never again after a failed create (the old upscaler stays). +// Config::maxOutputResizes times per context (a game that changes the size more often gets the old behaviour after that; +// each kept replaced upscaler holds its GPU memory until the context is destroyed), and never again after a failed create +// (the old upscaler stays). void FrontEnd::ResizeForFrame(UpscaleContext& u, WgFrame& frame, const std::string& tag) { - constexpr uint32_t kMaxResizes = 8; + const uint32_t kMaxResizes = config_.maxOutputResizes; if (!u.backend || !u.upscaler || u.pending || u.broken || u.resizeFailed) return; const uint32_t w = frame.output_width, h = frame.output_height; if ((w == u.createInfo.output_width && h == u.createInfo.output_height) || !w || !h) return; @@ -995,7 +996,8 @@ void FrontEnd::ResizeForFrame(UpscaleContext& u, WgFrame& frame, const std::stri if (u.resizes >= kMaxResizes) { if (u.resizes++ == kMaxResizes) Log(tag + " note U" + std::to_string(u.id) + ": output size " + now + " again after " + std::to_string(kMaxResizes) + - " sizes; the upscaler is not created again for this context (frames of another size are refused)"); + (kMaxResizes == 1 ? " size change" : " size changes") + + "; the upscaler is not created again for this context (frames of another size are refused)"); return; } WgCreateInfo info = u.createInfo; diff --git a/clean/fsr/tests/test_dll_d3d12.cpp b/clean/fsr/tests/test_dll_d3d12.cpp index ed9efeff..72869757 100644 --- a/clean/fsr/tests/test_dll_d3d12.cpp +++ b/clean/fsr/tests/test_dll_d3d12.cpp @@ -41,7 +41,8 @@ // network_bad a runtime whose tables lack functions is refused, with the reason; AMD upscales // network_cyberpunk_fail Cyberpunk's FSR 3.0 path: the runtime fails on frame 1, AMD's upscaler takes over // By hand, with the real runtime (not ctests): park_real (a game's dynamic resolution), switch_real (a player -// switching upscalers in an upscaler mod's menu; GPU memory after every step; see RunSwitchReal). +// switching upscalers in an upscaler mod's menu; GPU memory after every step; see RunSwitchReal), resize_real (a game +// changing its output size: the upscalers kept until the context is destroyed, GPU memory per step; see RunResizeReal). // With MSVC, the D3D12 debug layer is on when installed (WG_TEST_D3D12_DEBUG=0 turns it off); its errors fail the test. #define WIN32_LEAN_AND_MEAN #define NOMINMAX @@ -1942,6 +1943,194 @@ void RunSwitchReal(Api& api, Gpu& gpu, int cycles, int frames, int settleMs) { } } +// resize_real (run by hand, not a ctest; issue #58, PR #85; needs the real wronggpu_runtime.dll with its kernels and a +// model in the test's folder): a context created for the largest output size whose frames then ask for other output +// sizes, one new size per step. The front end creates the upscaler again for each size and keeps the replaced ones +// until the context is destroyed (at most kMaxResizes of them). Per step: this process's video memory (DXGI +// QueryVideoMemoryInfo, local segment, the process only) and the front end's own query of the current upscaler's GPU +// memory (the runtime's allocation total). At the end the context is destroyed, and the memory is read again. +// Environment: WG_TEST_RESIZE_W / _H (the context's maximum output size, default 3840 x 2160), WG_TEST_RESIZE_RATIO +// (render size = output / ratio in percent: 150 = Quality, 100 = native; default 150), WG_TEST_RESIZE_STEPS (new output +// sizes after the first, default 10), WG_TEST_RESIZE_CYCLES (contexts created, resized and destroyed one after the +// other, default 1), WG_TEST_RESIZE_INFLIGHT (1: at every step the list for the previous size is recorded first and +// submitted last, the list for the new size in between: the game's command lists that use the replaced upscaler), +// WG_TEST_RESIZE_SETTLE_MS (wait before a memory reading, default 1500), WG_TEST_RUNTIME_OPTIONS (runtime_options). +int EnvInt(const char* name, int fallback) { + char text[32] = ""; + return GetEnvironmentVariableA(name, text, sizeof text) ? std::atoi(text) : fallback; +} + +void RunResizeReal(Api& api, Gpu& gpu) { + const UINT W = UINT(EnvInt("WG_TEST_RESIZE_W", 3840)), H = UINT(EnvInt("WG_TEST_RESIZE_H", 2160)); + const int ratio = EnvInt("WG_TEST_RESIZE_RATIO", 150), steps = EnvInt("WG_TEST_RESIZE_STEPS", 10); + const int cycles = EnvInt("WG_TEST_RESIZE_CYCLES", 1), settleMs = EnvInt("WG_TEST_RESIZE_SETTLE_MS", 1500); + const bool inflight = EnvInt("WG_TEST_RESIZE_INFLIGHT", 0) != 0; + const UINT RW = (W * 100 / UINT(ratio)) & ~1u, RH = (H * 100 / UINT(ratio)) & ~1u; + struct Size { + UINT w, h; + }; + std::vector sizes; // every size differs, all close to the maximum (the worst case for the kept upscalers) + for (int k = 0; k <= steps; ++k) sizes.push_back({W - 16u * UINT(k), H - 8u * UINT(k)}); + std::printf("resize_real: context maximum output %ux%u, render %ux%u (ratio %d %%), %d new sizes, %d cycle(s), in-flight lists: %s\n", W, + H, RW, RH, ratio, steps, cycles, inflight ? "yes" : "no"); + ProcessGpuMemory pm; + if (!pm.Init(gpu.device)) std::printf("resize_real: note: DXGI does not report the process's video memory\n"); + { + DXGI_ADAPTER_DESC1 ad{}; + IDXGIFactory4* f = nullptr; + IDXGIAdapter1* a = nullptr; + HMODULE dxgi = LoadLibraryW(L"dxgi.dll"); + typedef HRESULT(WINAPI * PfnFactory)(REFIID, void**); + auto mk = dxgi ? reinterpret_cast(reinterpret_cast(GetProcAddress(dxgi, "CreateDXGIFactory1"))) : nullptr; + if (mk && SUCCEEDED(mk(__uuidof(IDXGIFactory4), reinterpret_cast(&f))) && f && + SUCCEEDED(f->EnumAdapterByLuid(gpu.device->GetAdapterLuid(), __uuidof(IDXGIAdapter1), reinterpret_cast(&a))) && a && + SUCCEEDED(a->GetDesc1(&ad))) + std::printf("resize_real: adapter %ls, dedicated video memory %.0f MiB\n", ad.Description, double(ad.DedicatedVideoMemory) / 1048576.0); + } + const std::vector one(size_t(RW) * RH * 4, HalfOf(0.5f)); + ID3D12Resource* color = gpu.Texture(DXGI_FORMAT_R16G16B16A16_FLOAT, RW, RH, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COPY_DEST); + ID3D12Resource* depth = gpu.Texture(DXGI_FORMAT_R32_FLOAT, RW, RH, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COPY_DEST); + ID3D12Resource* motion = gpu.Texture(DXGI_FORMAT_R16G16_FLOAT, RW, RH, D3D12_RESOURCE_FLAG_NONE, D3D12_RESOURCE_STATE_COPY_DEST); + CHECK(color && depth && motion); + if (!color || !depth || !motion) return; + { + std::vector z(size_t(RW) * RH); + for (UINT y = 0; y < RH; ++y) + for (UINT x = 0; x < RW; ++x) z[size_t(y) * RW + x] = 0.02f + 0.9f * float((x / 64 + y / 64) % 11) / 11.0f; + std::vector px(size_t(RW) * RH * 4), mv(size_t(RW) * RH * 2, 0); + for (UINT y = 0; y < RH; ++y) + for (UINT x = 0; x < RW; ++x) { + uint16_t* p = &px[(size_t(y) * RW + x) * 4]; + p[0] = HalfOf(((x / 16 + y / 16) & 1) ? 0.6f : 0.15f); + p[1] = HalfOf(0.1f + 0.8f * float(y % 193) / 193.0f); + p[2] = HalfOf(0.05f + 1.5f * float((x + y) % 61) / 61.0f); + p[3] = HalfOf(1.0f); + } + gpu.Begin(); + gpu.Upload(color, px.data(), 8, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE); + gpu.Upload(depth, z.data(), 4, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE); + gpu.Upload(motion, mv.data(), 4, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE); + CHECK(gpu.SubmitAndWait()); + } + std::vector outs; // the game's output texture of every size, made before any measurement + for (const Size& s : sizes) { + outs.push_back(gpu.Texture(DXGI_FORMAT_R16G16B16A16_FLOAT, s.w, s.h, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_UNORDERED_ACCESS)); + CHECK(outs.back()); + if (!outs.back()) return; + } + auto create = [&]() { + ffxCreateBackendDX12Desc backend{}; + backend.header.type = FFX_API_CREATE_CONTEXT_DESC_TYPE_BACKEND_DX12; + backend.device = gpu.device; + ffxCreateContextDescUpscale c{}; + c.header.type = FFX_API_CREATE_CONTEXT_DESC_TYPE_UPSCALE; + c.header.pNext = &backend.header; + c.flags = FFX_UPSCALE_ENABLE_HIGH_DYNAMIC_RANGE | FFX_UPSCALE_ENABLE_DEPTH_INVERTED; + c.maxRenderSize = {RW, RH}; + c.maxUpscaleSize = {W, H}; + ffxContext ctx = nullptr; + CHECK(api.create(&ctx, &c.header, nullptr) == FFX_API_RETURN_OK && ctx); + return ctx; + }; + static const float kJitter[4][2] = {{0.0f, -1.0f / 6}, {-0.25f, 1.0f / 6}, {0.25f, -7.0f / 18}, {-0.375f, -1.0f / 18}}; + ffxContext ctx = nullptr; + int frameNo = 0; + auto record = [&](ID3D12CommandAllocator* alloc, ID3D12GraphicsCommandList* list, int k) { + alloc->Reset(); + list->Reset(alloc, nullptr); + ffxDispatchDescUpscale d{}; + d.header.type = FFX_API_DISPATCH_DESC_TYPE_UPSCALE; + d.commandList = list; + d.color = Resource(color, FFX_API_SURFACE_FORMAT_R16G16B16A16_FLOAT, RW, RH, FFX_API_RESOURCE_STATE_COMPUTE_READ); + d.depth = Resource(depth, FFX_API_SURFACE_FORMAT_R32_FLOAT, RW, RH, FFX_API_RESOURCE_STATE_COMPUTE_READ); + d.motionVectors = Resource(motion, FFX_API_SURFACE_FORMAT_R16G16_FLOAT, RW, RH, FFX_API_RESOURCE_STATE_COMPUTE_READ); + d.output = Resource(outs[size_t(k)], FFX_API_SURFACE_FORMAT_R16G16B16A16_FLOAT, sizes[size_t(k)].w, sizes[size_t(k)].h, + FFX_API_RESOURCE_STATE_UNORDERED_ACCESS); + d.jitterOffset = {kJitter[frameNo & 3][0], kJitter[frameNo & 3][1]}; + d.motionVectorScale = {float(RW), float(RH)}; + d.renderSize = {RW, RH}; + d.upscaleSize = {sizes[size_t(k)].w, sizes[size_t(k)].h}; + d.preExposure = 1.0f; + d.frameTimeDelta = 16.7f; + d.cameraNear = 100000.0f; + d.cameraFar = 0.1f; + d.cameraFovAngleVertical = 1.0472f; + ++frameNo; + CHECK(api.dispatch(&ctx, &d.header) == FFX_API_RETURN_OK); + }; + auto submit = [&](ID3D12GraphicsCommandList* list) { + CHECK(SUCCEEDED(list->Close())); + ID3D12CommandList* lists[] = {list}; + gpu.queue->ExecuteCommandLists(1, lists); + }; + auto frame = [&](int k) { + record(gpu.allocator, gpu.list, k); + submit(gpu.list); + CHECK(gpu.Wait()); + }; + auto removed = [&](const char* when) { + const HRESULT hr = gpu.device->GetDeviceRemovedReason(); + if (hr != S_OK) std::printf("resize_real: DEVICE REMOVED %s: 0x%08lx\n", when, static_cast(hr)); + CHECK(hr == S_OK); + }; + auto nonLocalMiB = [&]() { + DXGI_QUERY_VIDEO_MEMORY_INFO info{}; + if (!pm.adapter || FAILED(static_cast(pm.adapter)->QueryVideoMemoryInfo(0, DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL, &info))) return -1.0; + return double(info.CurrentUsage) / 1048576.0; + }; + auto reported = [&]() { // the front end's query: the runtime's allocation total of the current upscaler + ffxQueryDescUpscaleGetGPUMemoryUsage memory{}; + FfxApiEffectMemoryUsage usage{}; + memory.header.type = FFX_API_QUERY_DESC_TYPE_UPSCALE_GPU_MEMORY_USAGE; + memory.gpuMemoryUsageUpscaler = &usage; + return api.query(&ctx, &memory.header) == FFX_API_RETURN_OK ? double(usage.totalUsageInBytes) / 1048576.0 : -1.0; + }; + auto settled = [&]() { + Sleep(DWORD(settleMs)); + return pm.ProcessMiB(); + }; + const double base = settled(); + std::printf("resize_real: before any context: process video memory %.0f MiB local, %.0f MiB non-local (textures made)\n", base, nonLocalMiB()); + double firstEnd = 0; + for (int c = 0; c < cycles; ++c) { + ctx = create(); + if (!ctx) return; + for (int f = 0; f < 3; ++f) frame(0); + removed("after the first frames"); + const double first = settled(); + std::printf("resize_real: cycle %d step 0 output %ux%u: process %.0f MiB (+%.0f over before), runtime reports %.0f MiB for this upscaler\n", c + 1, + sizes[0].w, sizes[0].h, first, first - base, reported()); + double previous = first, peak = first; + for (int k = 1; k <= steps; ++k) { + if (inflight) { // the earlier list (previous size) recorded first, not yet submitted; the resize happens in the second + record(gpu.allocator, gpu.list, k - 1); + record(gpu.allocatorB, gpu.listB, k); + submit(gpu.listB); + submit(gpu.list); + CHECK(gpu.Wait()); + } else { + frame(k); + } + frame(k); + frame(k); + removed("after a resize"); + const double m = settled(); + peak = std::max(peak, m); + std::printf("resize_real: cycle %d step %2d output %ux%u: process %.0f MiB (+%.0f over before, %+.0f over the previous step), runtime reports %.0f MiB\n", + c + 1, k, sizes[size_t(k)].w, sizes[size_t(k)].h, m, m - base, m - previous, reported()); + previous = m; + } + std::printf("resize_real: cycle %d peak process video memory %.0f MiB (+%.0f over before any context)\n", c + 1, peak, peak - base); + CHECK(api.destroy(&ctx, nullptr) == FFX_API_RETURN_OK && !ctx); + removed("after the destroy"); + const double end = settled(); + std::printf("resize_real: cycle %d context destroyed: process %.0f MiB (+%.0f over before any context; %+.0f over the end of cycle 1)\n", c + 1, end, + end - base, end - (c ? firstEnd : end)); + if (c == 0) firstEnd = end; + } + removed("at the end"); +} + int main(int argc, char** argv) { const std::string mode = argc > 1 ? argv[1] : "upscale"; if (mode == "switch_real") g_dllName = L"wronggpu_ffx_dx12.dll"; @@ -1967,9 +2156,10 @@ int main(int argc, char** argv) { SetEnvironmentVariableA("WG_FAKE_RUNTIME", fake); if (mode == "network_park") settings += "context_park_ms = 300\n"; } - if (mode == "park_real") { + if (mode == "park_real" || mode == "resize_real") { char park[32] = "3000", options[2048] = ""; GetEnvironmentVariableA("WG_TEST_PARK_MS", park, sizeof park); + if (mode == "resize_real") std::strcpy(park, "0"); // no parked upscaler: the memory is what the context holds GetEnvironmentVariableA("WG_TEST_RUNTIME_OPTIONS", options, sizeof options); settings = std::string("upscaler = network\ncontext_park_ms = ") + park + "\n"; if (options[0]) settings += std::string("runtime_options = ") + options + "\n"; @@ -1986,7 +2176,7 @@ int main(int argc, char** argv) { if (mode.rfind("view_", 0) == 0) settings += "view = " + mode.substr(5) + "\n"; // The checks below count per-frame trace lines (dispatches, submits, split releases) of every frame: log them all // (the release default logs only the first frames of a context). - if (mode != "park_real" && mode != "switch_real") settings += "log_dispatches = 1\n"; + if (mode != "park_real" && mode != "switch_real" && mode != "resize_real") settings += "log_dispatches = 1\n"; WriteFileText(ini, settings); Api api; @@ -2021,6 +2211,7 @@ int main(int argc, char** argv) { else if (mode == "network_cyberpunk_fail") RunNetworkFsr3(gpu); else if (mode == "network_park") RunNetworkPark(api, gpu); else if (mode == "park_real") RunParkReal(api, gpu); + else if (mode == "resize_real") RunResizeReal(api, gpu); else if (mode == "switch_real") RunSwitchReal(api, gpu, argc > 2 ? std::atoi(argv[2]) : 6, argc > 3 ? std::atoi(argv[3]) : 20, argc > 4 ? std::atoi(argv[4]) : 3500); else if (mode.rfind("network_", 0) == 0) RunNetwork(api, gpu, mode); @@ -2067,7 +2258,7 @@ int main(int argc, char** argv) { } const bool trace = mode.rfind("hooks_trace", 0) == 0; const bool toAmd = mode == "network_missing" || mode == "network_create_fail" || mode == "network_bad"; // no WrongGPU frame - const bool parks = mode == "network_park" || mode == "park_real" || mode == "switch_real"; // several contexts + const bool parks = mode == "network_park" || mode == "park_real" || mode == "switch_real" || mode == "resize_real"; // several contexts if (mode != "forward" && mode != "amd" && mode != "fsr30_log" && !trace && !toAmd && !parks) CHECK(Count(log, "WrongGPU frame: frame=0 ") == 1); if (mode == "network_park") { CHECK(Count(log, "fake runtime created") == 1); // the second context took the first one's upscaler over @@ -2093,6 +2284,20 @@ int main(int argc, char** argv) { std::printf("park_real: log: %s\n", log.substr(a + 1, b - a - 1).c_str()); } } + if (mode == "resize_real") { + std::printf("resize_real: log: %zu upscalers created, %zu created again for a new output size, %zu old upscalers kept until the context is destroyed, " + "%zu not created again after the limit, %zu errors\n", + Count(log, "network runtime ready"), Count(log, "the upscaler is created again for it"), Count(log, "kept until the context is destroyed"), + Count(log, "is not created again for this context"), Count(log, "ERROR")); + size_t at = 0; + int shown = 0; + while ((at = log.find("GPU memory ", at)) != std::string::npos && shown < 24) { + const size_t a = log.rfind('\n', at), b = log.find('\n', at); + std::printf("resize_real: log: %s\n", log.substr(a + 1, b - a - 1).c_str()); + at = b == std::string::npos ? log.size() : b; + ++shown; + } + } if (mode == "switch_real") { std::printf("switch_real: log: %zu upscalers created, %zu took a parked one over, %zu parked, %zu parked ones destroyed, %zu new " "arenas, %zu arenas kept from an earlier context, %zu arenas released, %zu AMD contexts, %zu errors\n", diff --git a/clean/fsr/tests/test_frontend.cpp b/clean/fsr/tests/test_frontend.cpp index 81756369..3bff71db 100644 --- a/clean/fsr/tests/test_frontend.cpp +++ b/clean/fsr/tests/test_frontend.cpp @@ -597,10 +597,13 @@ static void TestOwnUpscalerPath() { // Bug hunt H: a frame whose output size (upscaleSize) is smaller than the create's maxUpscaleSize: the upscaler is created // again for that size (the context stays), its first frame without history; the same size again creates nothing; a -// size over the maximum is not; after 8 sizes no more creates. +// size over the maximum is not; after Config::maxOutputResizes sizes no more creates (the tests raise it to 8; the +// default, 1, is TestResizeCapDefault). static void TestOutputResize() { Reset(); - auto fe = MakeFrontEnd(Config{}); + Config eight; + eight.maxOutputResizes = 8; + auto fe = MakeFrontEnd(eight); UpscaleCreate c(FFX_UPSCALE_ENABLE_HIGH_DYNAMIC_RANGE); ffxContext ctx = nullptr; CHECK(fe->CreateContext(&ctx, &c.upscale.header, kMemCb) == FFX_API_RETURN_OK && ctx); @@ -639,7 +642,9 @@ static void TestOutputResize() { // at once when the device is lost (nothing of it runs any more). static void TestResizeKeepsRetired() { Reset(); - auto fe = MakeFrontEnd(Config{}); + Config eight; + eight.maxOutputResizes = 8; // (the default of 1 allows only one kept upscaler: TestResizeCapDefault) + auto fe = MakeFrontEnd(eight); UpscaleCreate c(FFX_UPSCALE_ENABLE_HIGH_DYNAMIC_RANGE); c.upscale.maxRenderSize = {2560, 1440}; ffxContext ctx = nullptr; @@ -700,6 +705,33 @@ static void TestResizeKeepsRetired() { // Bug hunt F1 (a): an upscaler that failed for good, without AMD's FSR to take the context over, is not called again: // the stand-in (or an error) for the later frames, logged at 1, 2, 3, 10... frames, not two ERROR lines per frame. +// The default bound (Config::maxOutputResizes = 1, from the GPU memory measured for PR #85: one Model M upscaler is about +// 0.9 GiB at 4K output): one output-size change creates an upscaler again and keeps the old one until the context is +// destroyed; the next size change creates nothing (the kept one is not replaced, nothing else is destroyed), and the +// log says so once. +static void TestResizeCapDefault() { + Reset(); + Config defaults; + CHECK(defaults.maxOutputResizes == 1); + auto fe = MakeFrontEnd(defaults); + UpscaleCreate c(FFX_UPSCALE_ENABLE_HIGH_DYNAMIC_RANGE); + ffxContext ctx = nullptr; + CHECK(fe->CreateContext(&ctx, &c.upscale.header, kMemCb) == FFX_API_RETURN_OK && ctx); + ffxDispatchDescUpscale d = MakeDispatch(); + CHECK(fe->Dispatch(&ctx, &d.header) == FFX_API_RETURN_OK); + d.upscaleSize = {1920, 1080}; + CHECK(fe->Dispatch(&ctx, &d.header) == FFX_API_RETURN_OK && g_createCalls == 2 && g_destroyed == 0); + d.upscaleSize = {1600, 900}; // the second size change: over the bound + fe->Dispatch(&ctx, &d.header); + d.upscaleSize = {1280, 720}; + fe->Dispatch(&ctx, &d.header); + CHECK(g_createCalls == 2 && g_destroyed == 0); + CHECK(CountLog("the upscaler is not created again for this context") == 1); + CHECK(CountLog("again after 1 size change;") == 1); + CHECK(fe->DestroyContext(&ctx, kMemCb) == FFX_API_RETURN_OK && fe->OwnContextCount() == 0); + CHECK(g_destroyed == 2); // the kept one and the current one +} + static void TestBrokenNotCalledAgain() { Reset(); g_originalAvailable = false; @@ -3388,6 +3420,7 @@ int main() { TestCustomSlotInputRefused(); TestOutputResize(); TestResizeKeepsRetired(); + TestResizeCapDefault(); TestBrokenNotCalledAgain(); TestHostReentryStopped(); TestCustomSlotDestroyPath();