diff --git a/clean/rr_f/runtime/src/rr_runtime.cpp b/clean/rr_f/runtime/src/rr_runtime.cpp index 9c039346..497bcc7f 100644 --- a/clean/rr_f/runtime/src/rr_runtime.cpp +++ b/clean/rr_f/runtime/src/rr_runtime.cpp @@ -1300,6 +1300,8 @@ uint32_t set_device_locked(ID3D12Device* given) { log_line(std::string(wgrt::needs_rx9000_sentence()) + " (" + who + "): Ray Reconstruction is not available; the game keeps its own denoiser"); return WG_E_ADAPTER_NOT_SUPPORTED; } + const std::string wine = wgrt::wine_fallback_line(id); // Wine: how the adapter was identified (#248) + if (!wine.empty()) log_line(wine); rt.adapter_name = id.dxgi_known ? id.description : who; } D3D12_COMMAND_QUEUE_DESC qd = {}; diff --git a/clean/runtime/interop/backend.cpp b/clean/runtime/interop/backend.cpp index 297bc76c..43aff736 100644 --- a/clean/runtime/interop/backend.cpp +++ b/clean/runtime/interop/backend.cpp @@ -606,12 +606,16 @@ class Context { q.kmt_known = ident.kmt_known; q.kmt_vendor = ident.kmt_vendor; q.kmt_device = ident.kmt_device; + q.wine_fallback = ident.wine_fallback; + q.wine_vendor = ident.wine_vendor; + q.wine_device = ident.wine_device; } q.sparkle_guard = o.sparkle_guard > 0.0f; q.capture = !o.capture_dir.empty(); q.hip_switch = o.hip_switch; const ExecutorChoice ch = choose_executor(q); if (ch.executor.empty()) return fail(WG_ERROR_PARAMETER, "unknown executor " + o.executor); + if (!ch.adapter_note.empty()) say(WG_LOG_INFO, ch.adapter_note); // Wine: how the adapter was identified (#248) if (!ch.why.empty()) say(ch.warn ? WG_LOG_WARN : WG_LOG_INFO, ch.why); // R1: a release build ignores the diagnostic keys (executor=hip, capture_dir): one line each, the context stays on D3D12 for (const std::string& line : ch.ignored) say(WG_LOG_WARN, line); diff --git a/clean/runtime/interop/executor_select.h b/clean/runtime/interop/executor_select.h index e73b9898..4a089dbf 100644 --- a/clean/runtime/interop/executor_select.h +++ b/clean/runtime/interop/executor_select.h @@ -52,6 +52,8 @@ struct ExecutorRequest { std::string adapter; // that DXGI description bool kmt_known = false; // the kernel-mode driver's PCI ids were read (they decide first) uint32_t kmt_vendor = 0, kmt_device = 0; + bool wine_fallback = false; // Wine only: the driver's query is not implemented there; the system DXGI's ids (#248) + uint32_t wine_vendor = 0, wine_device = 0; bool sparkle_guard = false; // option sparkle_guard on bool capture = false; // option capture_dir set std::string hip_switch; // the 0.6.0 option hip_switch (ignored for every model: hip_switch_note) @@ -66,6 +68,7 @@ struct ExecutorChoice { bool sparkle_off = false; // the sparkle guard (HIP only) is turned off, whatever the model; the context stays on D3D12 std::string why; // the one log line about the choice; empty: nothing to log here std::string hip_note; // the one line when the option hip_switch asks for a move to HIP (ignored, any model) + std::string adapter_note; // the one line when the Wine fallback identified the adapter (rdna4.h, #248); else empty std::vector ignored; // release build: one warning line per diagnostic key that was ignored (executor=hip, // capture_dir); the context stays on D3D12 (the backend also drops capture_dir) }; @@ -106,6 +109,9 @@ inline wgrt::AdapterIdentity adapter_identity(const ExecutorRequest& r) { id.kmt_known = r.kmt_known; id.kmt_vendor = r.kmt_vendor; id.kmt_device = r.kmt_device; + id.wine_fallback = r.wine_fallback; + id.wine_vendor = r.wine_vendor; + id.wine_device = r.wine_device; return id; } @@ -117,6 +123,7 @@ inline ExecutorChoice choose_executor(const ExecutorRequest& r, bool devRequeste const bool dev = kDevDiagnostics && devRequested; ExecutorChoice c; c.hip_note = hip_switch_note(r.hip_switch); // the same for every model: the option is ignored + c.adapter_note = wgrt::wine_fallback_line(adapter_identity(r)); std::string e = r.executor; bool capture = r.capture; if (!dev) { // R1: a release build never starts HIP, whatever the game's files say diff --git a/clean/runtime/interop/tests/test_executor_select.cpp b/clean/runtime/interop/tests/test_executor_select.cpp index a9e199a5..06b17916 100644 --- a/clean/runtime/interop/tests/test_executor_select.cpp +++ b/clean/runtime/interop/tests/test_executor_select.cpp @@ -223,6 +223,127 @@ int main() { expect("RR: the game's RX 9000 (spoofed name) is accepted", wgrt::rdna4_identity(id)); } + // ---- Wine and Proton (#248): the driver's query is not implemented there; the system DXGI's ids under the same rule --- + { + struct WineFake { + int32_t status = 0; // the driver query's answer + uint32_t kvendor = 0x1002, kdevice = 0x7550; // its ids when it answers 0 + bool wine = false; // ntdll exports wine_get_version + bool dxgi_answers = true; + uint32_t dvendor = 0x1002, ddevice = 0x7550; // the system DXGI's ids + int asked_query = 0, asked_wine = 0, asked_dxgi_ids = 0; + wgrt::AdapterSources sources() { + wgrt::AdapterSources s; + s.find_adapter = [](const wgrt::AdapterLuid&, wgrt::AdapterRecord* out) { + out->vendor = 0x1002; + out->description = "AMD Radeon RX 9070 XT"; + return true; + }; + s.driver_query = [this](const wgrt::AdapterLuid&, uint32_t* v, uint32_t* d) -> int32_t { + ++asked_query; + if (status == 0) { + *v = kvendor; + *d = kdevice; + } + return status; + }; + s.under_wine = [this]() { + ++asked_wine; + return wine; + }; + s.dxgi_ids = [this](const wgrt::AdapterLuid&, uint32_t* v, uint32_t* d) { + ++asked_dxgi_ids; + if (!dxgi_answers) return false; + *v = dvendor; + *d = ddevice; + return true; + }; + return s; + } + }; + const wgrt::AdapterLuid luid; + auto choice = [](const wgrt::AdapterIdentity& id) { + ExecutorRequest r = request_for(id); + r.wine_fallback = id.wine_fallback; + r.wine_vendor = id.wine_vendor; + r.wine_device = id.wine_device; + return choose_executor(r); + }; + + WineFake f; + f.status = wgrt::kStatusNotImplemented; + f.wine = true; + wgrt::AdapterIdentity id = wgrt::lookup_adapter(luid, f.sources()); + ExecutorChoice c = choice(id); + expect("Wine: query not implemented + Wine + the system DXGI's 1002:7550: RDNA 4, the D3D12 executor", + !id.kmt_known && id.wine_fallback && wgrt::rdna4_identity(id) && c.executor == "dx12" && c.by_default); + expect("Wine: the one line says the Wine fallback identified the adapter, with the ids", + has(c.adapter_note, "Wine: the driver's adapter id query is not implemented") && + has(c.adapter_note, "PCI 1002:7550 (Wine: the system DXGI's ids)") && + c.adapter_note == wgrt::wine_fallback_line(id) && c.why.empty()); + for (uint32_t dev : {0x7550u, 0x7551u, 0x7590u, 0x7591u}) { + f.ddevice = dev; + expect("Wine: every RDNA 4 id of the list is accepted", wgrt::rdna4_identity(wgrt::lookup_adapter(luid, f.sources()))); + } + f.dvendor = 0x8086; + f.ddevice = 0x56A0; + id = wgrt::lookup_adapter(luid, f.sources()); + c = choice(id); + expect("Wine: another vendor's ids are refused (the front end's upscaler), no Wine line", + id.wine_fallback && !wgrt::rdna4_identity(id) && c.executor == "fsr" && c.adapter_note.empty() && + has(c.why, "PCI 8086:56A0")); + f.dvendor = 0x10DE; + f.ddevice = 0x7550; + id = wgrt::lookup_adapter(luid, f.sources()); + expect("Wine: an RDNA 4 device id under another vendor's id (spoofed) is refused", + !wgrt::rdna4_identity(id) && choice(id).executor == "fsr"); + f.dvendor = 0x1002; + f.ddevice = 0x744C; + id = wgrt::lookup_adapter(luid, f.sources()); + expect("Wine: an AMD board outside the list is refused, and said so", + !wgrt::rdna4_identity(id) && has(wgrt::describe_identity(id), "outside the RDNA 4 id list")); + f.ddevice = 0x7550; + f.dxgi_answers = false; + id = wgrt::lookup_adapter(luid, f.sources()); + expect("Wine: the system DXGI not answering: not identified, refused (fail closed)", + !id.wine_fallback && !wgrt::rdna4_identity(id) && choice(id).executor == "fsr"); + f.dxgi_answers = true; + + // outside Wine (Windows): the fallback is never asked + WineFake w; + w.status = wgrt::kStatusNotImplemented; + w.wine = false; + id = wgrt::lookup_adapter(luid, w.sources()); + expect("not under Wine: query not implemented is refused, and the system DXGI's ids are never asked", + !id.wine_fallback && !wgrt::rdna4_identity(id) && w.asked_wine == 1 && w.asked_dxgi_ids == 0 && + choice(id).executor == "fsr"); + for (int32_t st : {wgrt::kStatusUnsuccessful, (int32_t)-1073741811 /* 0xC000000D */, (int32_t)-1073741790 /* 0xC0000022 */}) { + WineFake o; + o.status = st; + o.wine = true; + id = wgrt::lookup_adapter(luid, o.sources()); + expect("another status from the query is refused, under Wine too: neither Wine nor the system DXGI's ids are asked", + !id.wine_fallback && !id.kmt_known && !wgrt::rdna4_identity(id) && o.asked_wine == 0 && o.asked_dxgi_ids == 0); + } + WineFake ok; // Windows: the driver's ids are read; nothing else is asked, whatever the system DXGI would say + ok.dvendor = 0x10DE; + id = wgrt::lookup_adapter(luid, ok.sources()); + c = choice(id); + expect("the driver's ids read: they decide, the fallback is never asked, no Wine line", + id.kmt_known && !id.wine_fallback && wgrt::rdna4_identity(id) && ok.asked_wine == 0 && ok.asked_dxgi_ids == 0 && + c.executor == "dx12" && c.adapter_note.empty()); + ok.kdevice = 0x744C; + ok.ddevice = 0x7550; + ok.wine = true; + id = wgrt::lookup_adapter(luid, ok.sources()); + expect("the driver's ids read under Wine (a future Wine): RDNA 3 ids are refused, the DXGI ids never override them", + !wgrt::rdna4_identity(id) && ok.asked_dxgi_ids == 0); + // the Ray Reconstruction runtime: the same lookup, rule and line (rr_runtime.cpp) + id = wgrt::lookup_adapter(luid, f.sources()); + expect("RR: the Wine fallback's RX 9000 is accepted, with the one line", + wgrt::rdna4_identity(id) && !wgrt::wine_fallback_line(id).empty()); + } + // ---- the default rule on real boards, every model ----------------------------------------------------------------- for (bool model_k : {false, true}) { const char* tag = model_k ? " (model K)" : " (model M or L)"; diff --git a/clean/runtime/rdna4.h b/clean/runtime/rdna4.h index 3e86efee..9fecb75d 100644 --- a/clean/runtime/rdna4.h +++ b/clean/runtime/rdna4.h @@ -14,7 +14,13 @@ // the list below shows up in a log and can be added. (The AMD AGS library's family query would be a second, family-level // source; it is not part of this project's build, see the notes of the R1 pull request.) // -// Header only and portable: the lookup takes its two sources as functions (AdapterSources), so that tests run on any host +// Wine and Proton (#248): Wine does not implement the driver's id query (D3DKMTQueryAdapterInfo type 31 answers +// STATUS_NOT_IMPLEMENTED), so under Wine the rule above would refuse every board. Only in that case, the query answering +// exactly STATUS_NOT_IMPLEMENTED in a process that runs under Wine (ntdll exports wine_get_version), the system DXGI's +// vendor and device id of the adapter with the same LUID stand in for the driver's, against the same vendor and the same +// id list. Any other failure, and the same status outside Wine, fails closed as before. Windows never takes this path. +// +// Header only and portable: the lookup takes its sources as functions (AdapterSources), so that tests run on any host // with fakes; rdna4_win.h supplies the system's. #pragma once @@ -42,6 +48,7 @@ struct AdapterLuid { struct AdapterRecord { AdapterLuid luid; uint32_t vendor = 0; + uint32_t device = 0; // DXGI's DeviceId (read only for the Wine fallback) bool software = false; std::string description; }; @@ -54,12 +61,27 @@ struct AdapterIdentity { std::string description; bool kmt_known = false; // the kernel-mode driver's ids were read: the only evidence that decides uint32_t kmt_vendor = 0, kmt_device = 0; + // Wine only: the driver's query answered STATUS_NOT_IMPLEMENTED under Wine, and the system DXGI's ids were read instead + bool wine_fallback = false; + uint32_t wine_vendor = 0, wine_device = 0; }; -// The two sources (injectable; rdna4_win.h: the system's own DXGI by its full path, and D3DKMTQueryAdapterInfo). +// NTSTATUS 0xC0000002, STATUS_NOT_IMPLEMENTED: Wine's answer to the driver's id query. +constexpr int32_t kStatusNotImplemented = -1073741822; +// NTSTATUS 0xC0000001, STATUS_UNSUCCESSFUL: what rdna4_win.h's query reports when it could not ask at all (no thunks, the +// adapter not opened), so that only the query's own STATUS_NOT_IMPLEMENTED can lead to the Wine fallback. +constexpr int32_t kStatusUnsuccessful = -1073741823; + +// The sources (injectable; rdna4_win.h: the system's own DXGI by its full path, and D3DKMTQueryAdapterInfo). struct AdapterSources { std::function find_adapter; // system DXGI, by LUID std::function driver_ids; // kernel-mode driver, by LUID + // The driver's query with its own status (0: the ids were read); when set, it is asked instead of driver_ids. + std::function driver_query; + // The Wine fallback's two sources, asked only when driver_query answered kStatusNotImplemented: whether the process runs + // under Wine, then the system DXGI's vendor and device id of the adapter with this LUID. + std::function under_wine; + std::function dxgi_ids; }; // The adapter with this LUID (the game device's): the same LUID goes to both sources, nothing is guessed; what a source @@ -73,13 +95,32 @@ inline AdapterIdentity lookup_adapter(const AdapterLuid& luid, const AdapterSour id.software = rec.software; id.description = rec.description; } - if (s.driver_ids) id.kmt_known = s.driver_ids(luid, &id.kmt_vendor, &id.kmt_device); + if (s.driver_query) { + uint32_t v = 0, d = 0; + const int32_t status = s.driver_query(luid, &v, &d); + if (status == 0) { + id.kmt_known = true; + id.kmt_vendor = v; + id.kmt_device = d; + } else if (status == kStatusNotImplemented && s.under_wine && s.under_wine() && s.dxgi_ids) { + uint32_t wv = 0, wd = 0; + if (s.dxgi_ids(luid, &wv, &wd)) { + id.wine_fallback = true; + id.wine_vendor = wv; + id.wine_device = wd; + } + } + } else if (s.driver_ids) { + id.kmt_known = s.driver_ids(luid, &id.kmt_vendor, &id.kmt_device); + } return id; } // An RDNA 4 Radeon RX 9000-series board: the driver's vendor and device id, nothing else; unknown ids are not RDNA 4. +// Under Wine, when the driver's query is not implemented there, the system DXGI's ids under the same rule (#248). inline bool rdna4_identity(const AdapterIdentity& a) { - return a.kmt_known && a.kmt_vendor == 0x1002 && rdna4_device_id(a.kmt_device); + if (a.kmt_known) return a.kmt_vendor == 0x1002 && rdna4_device_id(a.kmt_device); + return a.wine_fallback && a.wine_vendor == 0x1002 && rdna4_device_id(a.wine_device); } // The adapter as a log line shows it: the system DXGI's description when it has one, the driver's ids when known, and the @@ -90,12 +131,24 @@ inline std::string describe_identity(const AdapterIdentity& a) { std::string s; if (a.dxgi_known) s = "adapter " + (a.description.empty() ? std::string("(no name)") : a.description); if (a.kmt_known) s += (s.empty() ? "adapter " : ", ") + std::string(ids); + const bool wine = !a.kmt_known && a.wine_fallback; + if (wine) { + std::snprintf(ids, sizeof ids, "PCI %04X:%04X", (unsigned)a.wine_vendor, (unsigned)a.wine_device); + s += (s.empty() ? "adapter " : ", ") + std::string(ids) + " (Wine: the system DXGI's ids)"; + } if (s.empty()) s = "adapter not identified"; - if (a.kmt_known && a.kmt_vendor == 0x1002 && !rdna4_device_id(a.kmt_device)) - s += "; an AMD board outside the RDNA 4 id list"; + const uint32_t vendor = wine ? a.wine_vendor : a.kmt_vendor, device = wine ? a.wine_device : a.kmt_device; + if ((a.kmt_known || wine) && vendor == 0x1002 && !rdna4_device_id(device)) s += "; an AMD board outside the RDNA 4 id list"; return s; } +// The one line when an adapter was accepted by the Wine fallback (#248); empty otherwise. +inline std::string wine_fallback_line(const AdapterIdentity& a) { + if (a.kmt_known || !a.wine_fallback || !rdna4_identity(a)) return std::string(); + return "Wine: the driver's adapter id query is not implemented; the adapter was identified from the system DXGI's ids (" + + describe_identity(a) + ")"; +} + // The sentence every refusal of a GPU that is not RDNA 4 (or not identified) starts with: the runtime's log line when it // hands the game to AMD FSR, the Ray Reconstruction runtime's, and the launcher's (clean/installer wg_core // NeedsRx9000Sentence is the same words). diff --git a/clean/runtime/rdna4_win.h b/clean/runtime/rdna4_win.h index d92edcd9..79f7b307 100644 --- a/clean/runtime/rdna4_win.h +++ b/clean/runtime/rdna4_win.h @@ -12,6 +12,7 @@ // - the system's own DXGI, \dxgi.dll loaded by its full path (a bare "dxgi.dll", with or without // LOAD_LIBRARY_SEARCH_SYSTEM32, returns a proxy that is already loaded under that name), enumerated by the same LUID: // its description is shown in log lines, it never decides. +// Under Wine only, where the driver's query answers STATUS_NOT_IMPLEMENTED, the system DXGI's ids stand in (rdna4.h, #248). #pragma once #include @@ -64,22 +65,23 @@ inline LUID to_win_luid(const AdapterLuid& l) { return r; } -// The kernel-mode driver's PCI vendor and device id of the adapter with this LUID. False when the query is not -// available (Wine without the thunks, a refused call). -inline bool driver_device_ids(const AdapterLuid& luid, uint32_t* vendor, uint32_t* device) { +// The kernel-mode driver's PCI vendor and device id of the adapter with this LUID, and the query's own status: 0 when the +// ids were read, the query's NTSTATUS when it refused (Wine: STATUS_NOT_IMPLEMENTED), kStatusUnsuccessful when it could not +// be asked at all (no thunks, the adapter not opened). +inline int32_t driver_query_status(const AdapterLuid& luid, uint32_t* vendor, uint32_t* device) { using OpenFn = LONG(WINAPI*)(kmt::OpenAdapterFromLuid*); using QueryFn = LONG(WINAPI*)(kmt::QueryAdapterInfo*); using CloseFn = LONG(WINAPI*)(kmt::CloseAdapter*); HMODULE gdi = GetModuleHandleW(L"gdi32.dll"); if (!gdi) gdi = LoadLibraryExW(L"gdi32.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); - if (!gdi) return false; + if (!gdi) return kStatusUnsuccessful; auto open = reinterpret_cast(reinterpret_cast(GetProcAddress(gdi, "D3DKMTOpenAdapterFromLuid"))); auto query = reinterpret_cast(reinterpret_cast(GetProcAddress(gdi, "D3DKMTQueryAdapterInfo"))); auto close = reinterpret_cast(reinterpret_cast(GetProcAddress(gdi, "D3DKMTCloseAdapter"))); - if (!open || !query || !close) return false; + if (!open || !query || !close) return kStatusUnsuccessful; kmt::OpenAdapterFromLuid o = {}; o.AdapterLuid = to_win_luid(luid); - if (open(&o) != 0) return false; + if (open(&o) != 0) return kStatusUnsuccessful; kmt::QueryDeviceIds ids = {}; kmt::QueryAdapterInfo q = {}; q.hAdapter = o.hAdapter; @@ -90,10 +92,21 @@ inline bool driver_device_ids(const AdapterLuid& luid, uint32_t* vendor, uint32_ kmt::CloseAdapter c = {}; c.hAdapter = o.hAdapter; close(&c); - if (st != 0) return false; + if (st != 0) return static_cast(st); *vendor = ids.Ids.VendorID; *device = ids.Ids.DeviceID; - return true; + return 0; +} + +// The same as a yes or no: the ids were read. +inline bool driver_device_ids(const AdapterLuid& luid, uint32_t* vendor, uint32_t* device) { + return driver_query_status(luid, vendor, device) == 0; +} + +// The process runs under Wine (Proton included): Wine's ntdll exports wine_get_version, Windows' never does. +inline bool running_under_wine() { + HMODULE ntdll = GetModuleHandleW(L"ntdll.dll"); + return ntdll && GetProcAddress(ntdll, "wine_get_version") != nullptr; } inline std::string wide_to_utf8(const wchar_t* w) { @@ -129,6 +142,7 @@ inline bool system_dxgi_adapter(const AdapterLuid& luid, AdapterRecord* out) { ok = true; out->luid = to_luid(d.AdapterLuid); out->vendor = d.VendorId; + out->device = d.DeviceId; out->software = (d.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) != 0; out->description = wide_to_utf8(d.Description); } @@ -139,10 +153,21 @@ inline bool system_dxgi_adapter(const AdapterLuid& luid, AdapterRecord* out) { return ok; } +// The system DXGI's vendor and device id of the adapter with this LUID: the Wine fallback's source only (rdna4.h). +inline bool system_dxgi_ids(const AdapterLuid& luid, uint32_t* vendor, uint32_t* device) { + AdapterRecord r; + if (!system_dxgi_adapter(luid, &r)) return false; + *vendor = r.vendor; + *device = r.device; + return true; +} + inline AdapterSources system_sources() { AdapterSources s; s.find_adapter = system_dxgi_adapter; - s.driver_ids = driver_device_ids; + s.driver_query = driver_query_status; + s.under_wine = running_under_wine; + s.dxgi_ids = system_dxgi_ids; return s; } diff --git a/clean/tests/test_executor_select.py b/clean/tests/test_executor_select.py index a9baabf2..4496888c 100644 --- a/clean/tests/test_executor_select.py +++ b/clean/tests/test_executor_select.py @@ -52,7 +52,7 @@ def _build(exe, defines=()): raise unittest.SkipTest("no host C++ compiler (CXX, clang++, g++ or MSVC cl)") objdir = os.path.join(os.path.dirname(exe), "obj") os.makedirs(objdir, exist_ok=True) - cl = 'cl /nologo /std:c++17 /O1 /EHsc /utf-8 /D_CRT_SECURE_NO_WARNINGS %s "%s" /Fo"%s\\\\" /Fe"%s"' % ( + cl = 'cl /nologo /std:c++17 /permissive- /O1 /EHsc /utf-8 /D_CRT_SECURE_NO_WARNINGS %s "%s" /Fo"%s\\\\" /Fe"%s"' % ( " ".join("/D" + d for d in defines), os.path.join(ROOT, TEST), objdir, exe) bat = exe + ".bat" with open(bat, "w") as f: