diff --git a/.github/workflows/wronggpu-edition-tests.yml b/.github/workflows/wronggpu-edition-tests.yml new file mode 100644 index 0000000000..8d19e2a94f --- /dev/null +++ b/.github/workflows/wronggpu-edition-tests.yml @@ -0,0 +1,16 @@ +name: WrongGPU edition tests +on: [push, pull_request] +jobs: + host-event: + name: Version box and WrongGPU's list order + runs-on: windows-latest + steps: + - uses: actions/checkout@v4.2.2 + - uses: ilammy/msvc-dev-cmd@v1.13.0 + with: + arch: x64 + - name: Build and run wronggpu-edition-tests/host_event_test.cpp + run: | + $PSNativeCommandUseErrorActionPreference = $true + cl /nologo /EHsc /std:c++17 /W4 /WX /I OptiScaler/misc wronggpu-edition-tests/host_event_test.cpp /Fe:host_event_test.exe + ./host_event_test.exe diff --git a/OptiScaler/include/wronggpu/wg_model_entries.h b/OptiScaler/include/wronggpu/wg_model_entries.h new file mode 100644 index 0000000000..b0966641e2 --- /dev/null +++ b/OptiScaler/include/wronggpu/wg_model_entries.h @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: MIT +#pragma once +// The ONE definition of where WrongGPU's model entries are in the list of upscaler versions the front end gives a host +// (FFX_API_QUERY_DESC_TYPE_GET_VERSIONS: frontend.cpp ListVersions, from the registry's SR model rows). A host such as +// OptiScaler stores the player's entry as its position in that list (UpscalerIndex; OptiScaler shows the list sorted, but +// the index it stores is the position in the list the DLL returned), so this order is what the launcher writes: +// Model M first, then Model K (a build with Model K), then Model L (a build with Model L). +// The launcher (profile.h: kModelKEntryIndex / kModelLEntryIndex) and the front end use it; test_frontend +// (TestModelEntryOrder) fails when the list ListVersions returns and this order disagree, for each build's K / L flags. +// The index is for display only: which model runs is the model choice record's (clean/fsr/src/model_choice_record.h). + +namespace wgentries { + +// The position of `model`'s entry ('M', 'K', 'L') in a build with or without Model K and Model L; -1 when the build does +// not list that model. +constexpr int ModelEntryIndex(char model, bool withK, bool withL) { + return model == 'M' ? 0 : model == 'K' ? (withK ? 1 : -1) : model == 'L' ? (withL ? (withK ? 2 : 1) : -1) : -1; +} + +// The same as the decimal text an ini holds ("0", "1", "2"; nullptr for -1). +constexpr const char* IndexText(int index) { return index == 0 ? "0" : index == 1 ? "1" : index == 2 ? "2" : nullptr; } + +} // namespace wgentries diff --git a/OptiScaler/menu/menu_common.cpp b/OptiScaler/menu/menu_common.cpp index 508ca5ea7b..93363559a2 100644 --- a/OptiScaler/menu/menu_common.cpp +++ b/OptiScaler/menu/menu_common.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -1073,6 +1074,29 @@ static bool IsOn12(Upscaler upscaler) } } +// WrongGPU edition: the player selected version `versionId` in the version box. WrongGPU's FFX dll exports +// WrongGpuHostSelectedV1 (WrongGPU's include/wronggpu_host_event.h) and records a selection of one of its models as the +// player's model choice, so it is called before the backend change recreates the upscaler. Only a selection made here +// calls it, never an index read from the ini. A dll without the export is not called (0). What the box then does is +// misc/WrongGpuHostEvent.h's OnVersionSelected: 1 recreate (also for the entry already selected), 0 as before, negative +// not saved (nothing changes; the player is told to try again). +static int32_t NotifyHostSelected(HMODULE module, uint64_t versionId) +{ + const int32_t result = wronggpu_edition::CallHostSelected( + [module]() -> wronggpu_edition::PfnHostSelected + { + return module != nullptr ? reinterpret_cast( + GetProcAddress(module, "WrongGpuHostSelectedV1")) + : nullptr; + }, + versionId); + + if (result != 0) + LOG_INFO("WrongGPU: the player selected version {:X}, result {}", versionId, result); + + return result; +} + // Name without the w/Dx12 suffix, that one is shown as a tag in the list instead static std::string ShortUpscalerName(Upscaler upscaler, API api, int ffxIndex) { @@ -3821,6 +3845,18 @@ void MenuCommon::RenderActiveUpscalerSettings(RenderMenuContext& ctx) ImGui::Spacing(); const int ffxIndex = upscalerIndex.value_or_default(); + + // WrongGPU edition: WrongGPU's model entries must be where misc/wg_model_entries.h says (the index this box + // stores is a position in this list); a mismatch is logged once per list + static const void* checkedList = nullptr; + if (checkedList != versions) + { + checkedList = versions; + if (!wronggpu_edition::ListOrderMatches(versions->names)) + LOG_WARN( + "WrongGPU: the version list's model entries are not in the order wg_model_entries.h defines"); + } + // A custom upscaler's versions carry its own names (no "FSR " in front) and the box carries its name const char* prefix = customName.empty() ? "FSR " : ""; auto currentName = ffxIndex >= 0 && ffxIndex < versions->names.size() @@ -3834,10 +3870,34 @@ void MenuCommon::RenderActiveUpscalerSettings(RenderMenuContext& ctx) { for (auto n : FfxVersionsNewestFirst(*versions)) { - if (ImGui::Selectable(StrFmt("%s%s##%d", prefix, versions->names[n], n).c_str(), ffxIndex == n) && - ffxIndex != n) + if (!ImGui::Selectable(StrFmt("%s%s##%d", prefix, versions->names[n], n).c_str(), ffxIndex == n)) + continue; + + // WrongGPU edition: the selection goes to the upscaler's dll first (NotifyHostSelected), then + // OnVersionSelected decides (misc/WrongGpuHostEvent.h): 1 recreate, also when the entry was already + // selected (the model that ran may have been chosen elsewhere: WrongGPU's launcher or menu plugin); + // not saved: nothing changes, not even the index; 0: as before, a new entry recreates the upscaler. + HMODULE module = customUpscaler != nullptr ? customUpscaler->module.dll + : state.api == API::DX12 || currentBackend == Upscaler::FFX_on12 + ? FfxApiProxy::UpscalerModule(versions->ids[n]) + : nullptr; + const auto action = + wronggpu_edition::OnVersionSelected(ffxIndex, n, NotifyHostSelected(module, versions->ids[n])); + + if (action.notSaved) { + ImGuiToast notification { ImGuiToastType::Warning, 10000 }; + notification.setTitle("WrongGPU model choice"); + notification.setContent( + "Not saved, try again. The model that runs did not change. See WrongGPU's log."); + ImGui::InsertNotification(notification); + } + + if (action.setIndex) upscalerIndex = n; + + if (action.recreate) + { state.newBackend = currentBackend; MARK_ALL_BACKENDS_CHANGED(); } diff --git a/OptiScaler/misc/WrongGpuHostEvent.h b/OptiScaler/misc/WrongGpuHostEvent.h new file mode 100644 index 0000000000..c710069282 --- /dev/null +++ b/OptiScaler/misc/WrongGpuHostEvent.h @@ -0,0 +1,105 @@ +#pragma once + +// WrongGPU edition: what the version box does with the player's selection when the upscaler's dll is WrongGPU's FFX dll +// (WRONGGPU-EDITION.md item 6). Kept free of ImGui and Windows so wronggpu-edition-tests/host_event_test.cpp can test +// it. +// +// include/wronggpu/wg_model_entries.h is a byte-identical copy of WrongGPU's clean/wgshared/include/wg_model_entries.h +// (the order of WrongGPU's model entries in the list its dll returns); WrongGPU's CI checks that the copies match. + +#include "../include/wronggpu/wg_model_entries.h" + +#include +#include +#include + +namespace wronggpu_edition +{ + +// The order the copied header defines, as this edition expects it. +static_assert(wgentries::ModelEntryIndex('M', true, true) == 0, "Model M is the first WrongGPU entry"); +static_assert(wgentries::ModelEntryIndex('K', true, true) == 1, "Model K follows Model M"); +static_assert(wgentries::ModelEntryIndex('L', true, true) == 2, "Model L follows Model K"); +static_assert(wgentries::ModelEntryIndex('L', false, true) == 1, "without Model K, Model L follows Model M"); +static_assert(wgentries::ModelEntryIndex('K', false, true) == -1, "a build without Model K lists none"); + +// WrongGpuHostSelectedV1's results (WrongGPU's include/wronggpu_host_event.h). +constexpr int32_t kRecorded = 1; // the selected model runs from now on: recreate, also for the entry already selected +constexpr int32_t kNothingChanged = 0; + +// What the version box does after the player selected entry `selected` while `current` is selected, given WrongGPU's +// result (0 for a dll without the export). +struct VersionBoxAction +{ + bool setIndex = false; // store `selected` as the index + bool recreate = false; // recreate the upscaler (the backend change) + bool notSaved = false; // tell the player "Not saved, try again" +}; + +inline VersionBoxAction OnVersionSelected(int current, int selected, int32_t result) +{ + VersionBoxAction a; + + if (result < 0) + { + a.notSaved = true; // nothing changes: no new index, no recreate + return a; + } + + if (current != selected || result == kRecorded) + { + a.setIndex = true; + a.recreate = true; + } + + return a; +} + +// Calls the dll's WrongGpuHostSelectedV1 when `resolve` finds it (a function returning the export's address or +// nullptr), else returns 0. +using PfnHostSelected = int32_t (*)(uint64_t versionId); + +template int32_t CallHostSelected(Resolve&& resolve, uint64_t versionId) +{ + const PfnHostSelected fn = resolve(); + return fn != nullptr ? fn(versionId) : kNothingChanged; +} + +// The model of a listed version name ("WrongGPU Model K##3.1.0" -> 'K'), else 0. +inline char ModelOfName(const char* name) +{ + static const char kPrefix[] = "WrongGPU Model "; + const size_t n = sizeof(kPrefix) - 1; + + if (name == nullptr || std::strncmp(name, kPrefix, n) != 0) + return 0; + + const char m = name[n]; + return (m == 'M' || m == 'K' || m == 'L') && (name[n + 1] == 0 || name[n + 1] == '#') ? m : 0; +} + +// True when WrongGPU's model entries in a version list (the names the dll returned, in its order: the index stored in +// OptiScaler.ini is a position in this list) are at the positions wg_model_entries.h defines. A list without WrongGPU's +// entries is true (not WrongGPU's dll). +inline bool ListOrderMatches(const std::vector& names) +{ + bool hasK = false, hasL = false; + + for (auto name : names) + { + hasK = hasK || ModelOfName(name) == 'K'; + hasL = hasL || ModelOfName(name) == 'L'; + } + + for (size_t i = 0; i < names.size(); i++) + { + const char m = ModelOfName(names[i]); + + if (m != 0 && wgentries::ModelEntryIndex(m, hasK, hasL) != static_cast(i)) + return false; + } + + return true; +} + +} // namespace wronggpu_edition diff --git a/WRONGGPU-EDITION.md b/WRONGGPU-EDITION.md index 8df26885d8..b0442d75a1 100644 --- a/WRONGGPU-EDITION.md +++ b/WRONGGPU-EDITION.md @@ -29,5 +29,18 @@ This branch is OptiScaler with a few changes for the WrongGPU upscaler. It is bu 5. **Diagnostics, off by default.** `[Hotfix] DredDiagnostics=true` (D3D12 DRED breadcrumbs and page-fault data, dumped once when the device is removed) and `[Hotfix] NullRootViewFix=true` (null root views logged and replaced by a zero buffer). +6. **The player's selection in the version box is told to the upscaler.** When the player selects a version in + the version box, the upscaler's DLL is told first, through `WrongGpuHostSelectedV1(versionId)`, if it + exports it (WrongGPU's FFX DLL does, and records a selection of one of its models as the player's model + choice). The result decides (`OptiScaler/misc/WrongGpuHostEvent.h`, `OnVersionSelected`): 1, the selected + model runs from now on: the upscaler is recreated, also when the entry was already selected (the model that + ran may have been chosen elsewhere, in WrongGPU's launcher or menu plugin); 0, nothing changed for WrongGPU: + as before, a new entry recreates the upscaler; negative, not saved: nothing changes (no new index, no + recreate) and a notification says "Not saved, try again". The call is made only for a selection in the + menu, never for the index read from `OptiScaler.ini`. A DLL without the export is never called. The order of + WrongGPU's model entries in the list its DLL returns (the index this box stores is a position in that list) + is WrongGPU's `wg_model_entries.h`, copied byte for byte to `OptiScaler/include/wronggpu/` (WrongGPU's CI + checks the copy); a list in another order is logged. `wronggpu-edition-tests/host_event_test.cpp` tests both, + built and run by the `WrongGPU edition tests` workflow. Nothing else is changed. WrongGPU itself is a separate program; it is not part of this repository. diff --git a/wronggpu-edition-tests/host_event_test.cpp b/wronggpu-edition-tests/host_event_test.cpp new file mode 100644 index 0000000000..a19ce37c7b --- /dev/null +++ b/wronggpu-edition-tests/host_event_test.cpp @@ -0,0 +1,72 @@ +// WrongGPU edition: tests of OptiScaler/misc/WrongGpuHostEvent.h (what the version box does with WrongGPU's result, and +// the list order of WrongGPU's model entries against the copied wg_model_entries.h). Built and run by +// .github/workflows/wronggpu-edition-tests.yml: cl /EHsc /std:c++17 /I OptiScaler/misc host_event_test.cpp + +#include "WrongGpuHostEvent.h" + +#include + +static int g_checks = 0, g_failures = 0; +#define CHECK(c) \ + do \ + { \ + ++g_checks; \ + if (!(c)) \ + { \ + ++g_failures; \ + std::printf("FAIL line %d: %s\n", __LINE__, #c); \ + } \ + } while (0) + +using namespace wronggpu_edition; + +static int32_t g_lastVersion = 0; +static int32_t FakeHostSelected(uint64_t versionId) +{ + g_lastVersion = static_cast(versionId); + return versionId == 7 ? 1 : versionId == 9 ? -1 : 0; +} + +int main() +{ + // 1: this model runs from now on: recreate, also when the entry was already selected + auto a = OnVersionSelected(2, 2, 1); + CHECK(a.setIndex && a.recreate && !a.notSaved); + a = OnVersionSelected(0, 2, 1); + CHECK(a.setIndex && a.recreate && !a.notSaved); + // 0: as before: a new entry recreates, the entry already selected does nothing + a = OnVersionSelected(0, 2, 0); + CHECK(a.setIndex && a.recreate && !a.notSaved); + a = OnVersionSelected(2, 2, 0); + CHECK(!a.setIndex && !a.recreate && !a.notSaved); + // negative: not saved: nothing changes (no new index, no recreate), the player is told + for (int32_t r : { -1, -2, -3 }) + { + a = OnVersionSelected(0, 2, r); + CHECK(!a.setIndex && !a.recreate && a.notSaved); + a = OnVersionSelected(2, 2, r); + CHECK(!a.setIndex && !a.recreate && a.notSaved); + } + + // the export: called when found, 0 without it + CHECK(CallHostSelected([]() -> PfnHostSelected { return nullptr; }, 7) == 0); + CHECK(CallHostSelected([]() -> PfnHostSelected { return &FakeHostSelected; }, 7) == 1 && g_lastVersion == 7); + CHECK(CallHostSelected([]() -> PfnHostSelected { return &FakeHostSelected; }, 9) == -1 && g_lastVersion == 9); + + // the names WrongGPU's dll lists + CHECK(ModelOfName("WrongGPU Model K##3.1.0") == 'K' && ModelOfName("WrongGPU Model M") == 'M'); + CHECK(ModelOfName("WrongGPU Model Q") == 0 && ModelOfName("FSR 3.1.4") == 0 && ModelOfName(nullptr) == 0); + CHECK(ModelOfName("WrongGPU Model LX") == 0); + + // the list order against wg_model_entries.h: M, then K, then L, as each build lists them (AMD's versions follow) + CHECK(ListOrderMatches({ "WrongGPU Model M##3.1.0", "WrongGPU Model K##3.1.0", "WrongGPU Model L##3.1.0", "3.1.5" })); + CHECK(ListOrderMatches({ "WrongGPU Model M##3.1.0", "WrongGPU Model L##3.1.0", "3.1.5", "2.3.4" })); + CHECK(ListOrderMatches({ "WrongGPU Model M##3.1.0", "WrongGPU Model K##3.1.0" })); + CHECK(ListOrderMatches({ "FSR 3.1.4", "FSR 2.3.4" })); // not WrongGPU's dll + CHECK(!ListOrderMatches({ "WrongGPU Model K##3.1.0", "WrongGPU Model M##3.1.0" })); + CHECK(!ListOrderMatches({ "WrongGPU Model M##3.1.0", "WrongGPU Model L##3.1.0", "WrongGPU Model K##3.1.0" })); + CHECK(!ListOrderMatches({ "3.1.5", "WrongGPU Model M##3.1.0" })); + + std::printf("%d checks, %d failures\n", g_checks, g_failures); + return g_failures ? 1 : 0; +}