diff --git a/README.md b/README.md index aae5a3fe1..8c1eb2e82 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,8 @@ next to `xcl.exe`, directly next to `xcl.exe`, in `%LOCALAPPDATA%\XCL\D3D12\`, o size of 1.9's for translated compute programs and took an NVIDIA driver 1.4–2.2 times as long to build pipelines from ([#156](https://github.com/ivall/xcl/issues/156)). `xcl run` prints the DXC it loaded, and `report.json` has it under `dxc`. +- **Linux:** cross-build for `x86_64-pc-windows-gnu` and run under Wine + vkd3d-proton: setup, scripts + and known state in [`docs/linux.md`](docs/linux.md). - **GPU:** developed on AMD RDNA4. The console's geometry and NGG programs run as a compute pass plus a mesh pass, so a GPU without mesh shaders draws none of them. On GPUs with 32-lane waves only (NVIDIA) console wave64 compute, geometry and mesh programs run as two 32-lane halves that exchange diff --git a/THIRD_PARTY.md b/THIRD_PARTY.md index e2f7ec0d0..9c7288023 100644 --- a/THIRD_PARTY.md +++ b/THIRD_PARTY.md @@ -17,6 +17,7 @@ carry a note in their header about its origin and the date of changes (GPL-2.0 | docs.xbox.com | documentation | signatures, enums, structs written independently | reference | | Rust crates (`Cargo.lock`) | MIT / BSD-2 / Zlib / Unlicense (or with an MIT alternative) | build dependencies | statically linked | | [libopus](https://opus-codec.org/) (`opus.dll`) | BSD-3-Clause | Opus decoding where Windows has no `MSOpusDecoder.dll` (Wine): loaded at runtime from `%XCL_OPUS_DIR%` / next to xcl.exe (`hle/opus.rs`) | **not** distributed with XCL | +| [vkd3d-proton](https://github.com/HansKristian-Work/vkd3d-proton) | LGPL-2.1 | `scripts/linux/vkd3d-proton-xcl.patch`: a 6-line patch to its `command.c` (Linux / Wine setup, `docs/linux.md`); the patch only, vkd3d-proton itself is built by the user and **not** distributed with XCL | patch applied to the user's own checkout | | DXC (`dxcompiler.dll`, `dxil.dll`) from the Windows SDK | NCSA (dxcompiler) / Microsoft (dxil) | HLSL → DXIL compilation at runtime | loaded from the user's SDK, **not** distributed with XCL | | Windows system libraries (d3d12, dxgi, xaudio2_9, mfplat, MSOpusDecoder, ole32 …) | system | target APIs | GPL-2.0 "system library" exception | diff --git a/crates/xcl/src/preflight.rs b/crates/xcl/src/preflight.rs index 230a29bf5..0473b3d0e 100644 --- a/crates/xcl/src/preflight.rs +++ b/crates/xcl/src/preflight.rs @@ -2,6 +2,7 @@ //! system XGameRuntime and the GPU the game will get (Shader Model 6.6, mesh shaders, wave //! width). Without one of them the game still starts, but draws nothing or part of the //! picture, so each missing piece is printed with what to do, and kept in `report.json`. +//! Under Wine it also looks at the D3D12 / DXGI libraries the process got (see `docs/linux.md`). use serde::{Deserialize, Serialize}; @@ -48,8 +49,23 @@ pub struct Found { pub dxc_toolchain: crate::gpu::recomp::dxc::Toolchain, pub xgameruntime: bool, pub gpu: Result, + /// the D3D12 / DXGI libraries of a Wine prefix; `None` on Windows + pub wine: Option, } +/// What the libraries loaded under Wine are, found by looking into their images. `None`: that +/// library is not loaded, nothing to say. +#[derive(Debug, Clone, Copy, Default)] +pub struct WineLibs { + /// `d3d12core.dll` holds vkd3d-proton's vtable-identity assertions + pub vkd3d_asserts_vtables: Option, + /// `dxgi.dll` is DXVK's + pub dxgi_is_dxvk: Option, +} + +/// The assertion that stops vkd3d-proton on the objects XCL adopts (upstream issue #241). +const VKD3D_VTABLE_ASSERT: &str = "iface->lpVtbl == &d3d12_heap_vtbl"; + fn check(level: Level, what: impl Into, fix: Option<&str>) -> Check { Check { level, what: what.into(), fix: fix.map(str::to_string) } } @@ -93,6 +109,22 @@ pub fn checks(f: &Found) -> Vec { if !f.xgameruntime { v.push(check(Level::Warning, "no System32\\xgameruntime.dll: dialogs, text entry, game invites and game streaming are missing (XCL's own task queues, async and networking are used)", Some("install Gaming Services (Microsoft Store)"))); } + if let Some(w) = &f.wine { + if w.vkd3d_asserts_vtables == Some(true) { + v.push(check( + Level::Error, + format!("this vkd3d-proton stops on the objects XCL hands to D3D12 (its d3d12core.dll holds the assertion `{VKD3D_VTABLE_ASSERT}`, upstream issue #241): the game aborts as soon as it uses one of them"), + Some("use the vkd3d-proton built by scripts/linux/build-vkd3d-proton.sh (patched, assertions off) – see docs/linux.md"), + )); + } + if w.dxgi_is_dxvk == Some(false) { + v.push(check( + Level::Warning, + "dxgi.dll is Wine's, not DXVK's: presenting crashes in wined3d (upstream issue #175)", + Some("copy DXVK's dxgi.dll, d3d11.dll and d3d10core.dll into the prefix's System32 and run with WINEDLLOVERRIDES=\"d3d12,d3d12core,dxgi,d3d11,d3d10core=n,b\" – see docs/linux.md"), + )); + } + } v } @@ -153,13 +185,54 @@ fn xgameruntime_present() -> bool { std::path::Path::new(&root).join("System32").join("xgameruntime.dll").is_file() } +/// Whether the image of the loaded module `name` holds `needle`; `None` when it is not loaded. +/// Only committed, readable pages of the module's own allocation are read. +fn image_contains(name: &str, needle: &[u8]) -> Option { + use windows_sys::Win32::System::LibraryLoader::GetModuleHandleW; + use windows_sys::Win32::System::Memory::*; + let wide: Vec = name.encode_utf16().chain(std::iter::once(0)).collect(); + // SAFETY: `wide` is NUL-terminated; the regions read are the ones VirtualQuery reports as + // committed and readable, inside the allocation of the module's base address. + unsafe { + let base = GetModuleHandleW(wide.as_ptr()) as *const u8; + if base.is_null() { + return None; + } + let readable = PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY; + let mut at = base; + loop { + let mut mbi: MEMORY_BASIC_INFORMATION = std::mem::zeroed(); + if VirtualQuery(at as _, &mut mbi, std::mem::size_of_val(&mbi)) == 0 || mbi.AllocationBase as *const u8 != base { + return Some(false); + } + if mbi.State == MEM_COMMIT && mbi.Protect & readable != 0 && mbi.Protect & (PAGE_GUARD | PAGE_NOACCESS) == 0 { + let region = std::slice::from_raw_parts(mbi.BaseAddress as *const u8, mbi.RegionSize); + if region.windows(needle.len()).any(|w| w == needle) { + return Some(true); + } + } + at = (mbi.BaseAddress as *const u8).add(mbi.RegionSize); + } + } +} + +/// The libraries the process has by now (after the GPU probe has loaded them), under Wine. +fn wine_libs() -> Option { + crate::util::under_wine().then(|| WineLibs { + vkd3d_asserts_vtables: image_contains("d3d12core.dll", VKD3D_VTABLE_ASSERT.as_bytes()), + dxgi_is_dxvk: image_contains("dxgi.dll", b"DXVK"), + }) +} + /// Check this PC for a run with the adapter `setting`. pub fn run(setting: &str) -> Vec { + let gpu = probe_gpu(setting); let found = Found { dxc: crate::gpu::recomp::dxc::location().map(|p| p.display().to_string()), dxc_toolchain: crate::gpu::recomp::dxc::toolchain(), xgameruntime: xgameruntime_present(), - gpu: probe_gpu(setting), + gpu, + wine: wine_libs(), }; checks(&found) } @@ -190,7 +263,7 @@ mod tests { } fn found(gpu: Gpu) -> Found { - Found { dxc: Ok(r"C:\dxc".into()), dxc_toolchain: dxc(1, 9), xgameruntime: true, gpu: Ok(gpu) } + Found { dxc: Ok(r"C:\dxc".into()), dxc_toolchain: dxc(1, 9), xgameruntime: true, gpu: Ok(gpu), wine: None } } fn dxc(major: u32, minor: u32) -> crate::gpu::recomp::dxc::Toolchain { diff --git a/docs/linux.md b/docs/linux.md new file mode 100644 index 000000000..a645ed9b1 --- /dev/null +++ b/docs/linux.md @@ -0,0 +1,191 @@ +# Running XCL on Linux + +XCL is a Windows program. On Linux it is cross-compiled for `x86_64-pc-windows-gnu` and `xcl.exe` runs +under Wine, which sends D3D12 to the host's Vulkan driver through vkd3d-proton. This works, with the +setup below, up to the first frames of a retail game; the picture is still black (see +[Known state](#known-state)). Requests: [#154](https://github.com/ivall/xcl/issues/154) (Linux), +[#175](https://github.com/ivall/xcl/issues/175) (what Wine and vkd3d-proton need), +[#241](https://github.com/ivall/xcl/issues/241) (the vkd3d-proton assertion). + +Checked on Ubuntu 26.04 (kernel 7.0), an NVIDIA RTX 4080 SUPER, Proton Experimental 11.0, vkd3d-proton v3.1, +DXVK 3.1.1. The helper scripts are in [`scripts/linux/`](../scripts/linux). + +## Prerequisites + +- Rust with the target: `rustup target add x86_64-pc-windows-gnu` +- mingw-w64 (Ubuntu: `gcc-mingw-w64-x86-64-posix g++-mingw-w64-x86-64-posix`; if `x86_64-w64-mingw32-gcc` + uses win32 threads, `CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER=x86_64-w64-mingw32-gcc-posix` picks the + POSIX one) +- To build vkd3d-proton: `git meson ninja glslang-tools mingw-w64-tools` (the last one has `widl`) +- Steam with **Proton Experimental** installed (the Wine; see below) +- A Vulkan driver with the extensions vkd3d-proton needs, and a GPU with Shader Model 6.6 and mesh + shaders, as on Windows (README) +- Linux 6.7 or newer (`userfaultfd` write-protect, see [Wine](#wine)) +- An audio device, even a silent one (see [Audio device](#audio-device)) + +## Build + +```sh +cargo build --release --workspace --target x86_64-pc-windows-gnu # target/x86_64-pc-windows-gnu/release/xcl.exe +``` + +`cargo build --workspace --all-targets --locked --target x86_64-pc-windows-gnu` builds the tests too. +The test binaries are run under Wine the same way as `xcl.exe`, with the setup below. + +## DXC + +XCL compiles shaders with Microsoft's DXC, as on Windows. Take `dxcompiler.dll` and `dxil.dll` (`bin/x64`) from +a [DirectXShaderCompiler release](https://github.com/microsoft/DirectXShaderCompiler/releases) (1.8 or +newer) and put them next to `xcl.exe`, or point `XCL_DXC_DIR` at their folder. `run.sh` takes a Linux path +there and converts it. + +## Opus (the sound of GTA) + +GTA plays its sound through the console's audio processor, not XAudio2: with #294 its Opus packets go to the emulated +decoder (xapu, `hle/acphal.rs`), whose PCM is played on the PC through XAudio2 (`hle/xaudio2.rs`). On Windows the decoder +is the system's `MSOpusDecoder.dll`. **Wine / Proton has none** (only `mfplat.dll`), so XCL loads libopus +(BSD-3-Clause) from an `opus.dll` at run time, like DXC. Without either, the decoder answers silence and the log says +`xapu: no Opus decoder`. Build it with mingw-w64 (`cmake`, `make`, `curl`; downloads and checks the official release): + +```sh +scripts/linux/build-opus.sh # -> ~/.local/share/xcl-runtime/opus/opus.dll +``` + +and point `XCL_OPUS_DIR` at its folder (a Windows path: `Z:\home\you\.local\share\xcl-runtime\opus`), or copy it next to +`xcl.exe`. `xcl.log` says which decoder is used (`Opus: decoder = libopus (…)`); `XCL_TRACE_APU=1` logs what a title sends +to xapu / acphal. + +## Wine + +XCL needs a Wine that tracks `MEM_WRITE_WATCH` memory with the kernel (`userfaultfd`, Wine's +`USE_UFFD_WRITEWATCH`). Without it vkd3d-proton cannot import XCL's unified memory: it logs +`Failed to import host memory, hr 0x8007000e`, allocates a copy of its own and still returns success, so +the CPU and the GPU each see their own memory and the GPU reads zeros (#175). + +- **Proton Experimental's Wine** (`files/bin/wine`) has it: its `lib/wine/x86_64-unix/ntdll.so` contains + "Using kernel write watches". This is the Wine the scripts use. +- WineHQ's `wine-staging` packages for current Ubuntu have it too (11.21 on Ubuntu 24.04, #257). +- Ubuntu's own `wine` package (10.0 here) does not, nor do builds made against kernel headers older than + 6.7, such as portable builds made in an old chroot (Kron4ek's). Check any other Wine with + `grep -a "Using kernel write watches" /lib/wine/x86_64-unix/ntdll.so`. +- Proton's `proton` script is not used: it failed here, copying `nvngx_update.exe`. The scripts call its + `wine` directly, with a prefix of their own. +- A container needs `userfaultfd` allowed (Docker's default seccomp profile blocks it). + `vm.unprivileged_userfaultfd` does not matter: Wine passes `UFFD_USER_MODE_ONLY`. + +## vkd3d-proton + +Stock vkd3d-proton (the releases, Proton's, winetricks') cannot run XCL. XCL gives D3D12 objects it +creates its own extended vtable (`hle/d3d12x/real.rs`, `adopt`), and vkd3d-proton checks the vtable's identity: + +- 11 `assert(iface->lpVtbl == &d3d12_..._vtbl)` in `vkd3d_private.h`. The first one a title reaches is the + heap's (`iface->lpVtbl == &d3d12_heap_vtbl`, [#241](https://github.com/ivall/xcl/issues/241)). +- `d3d12_command_allocator_from_iface` and `d3d12_command_list_from_iface` return NULL for those + objects, so `CreateCommandList` fails with `E_INVALIDARG`. +- Shared fences are told from plain ones by their vtable: run with `VKD3D_CONFIG=ignore_shared_fence`. + +The build in `scripts/linux/` is v3.1 with a 6-line patch for the two `from_iface` functions +([`vkd3d-proton-xcl.patch`](../scripts/linux/vkd3d-proton-xcl.patch)) and `-Db_ndebug=true`, which compiles +the assertions out. Its `dxil-spirv` submodule is moved to a newer commit (`DXIL_SPIRV_REF`, default +`53aac418`): v3.1's loops without end or overflows the stack on 20 of the retail title's compute programs +during world loading ([#611](https://github.com/ivall/xcl/issues/611)), the newer one converts all of them: + +```sh +scripts/linux/build-vkd3d-proton.sh ~/xcl-vkd3d # -> ~/xcl-vkd3d/vkd3d-proton-xcl/{d3d12,d3d12core}.dll +``` + +This removes checks for every application: use it for XCL only, in XCL's prefix. It is a workaround; the +real fix is XCL not touching the host's objects (proxies, [#175](https://github.com/ivall/xcl/issues/175)). + +## DXVK + +Wine's own `dxgi.dll` crashes in wined3d when XCL presents (#175). `setup-prefix.sh` copies DXVK's `dxgi.dll`, +`d3d11.dll` and `d3d10core.dll` from Proton (`files/lib/wine/dxvk/x86_64-windows/`). DXVK reports an +NVIDIA GPU as AMD by default; `DXVK_CONFIG='dxgi.hideNvidiaGpu = False'` keeps XCL's NVIDIA paths. + +## Audio device + +A retail game needs an audio endpoint. With no sound device at all (a container, a headless machine) its +audio engine's device setup fails and the game deadlocks about 30 s in (#257). A silent ALSA default device +is enough, in `~/.asoundrc`: + +``` +pcm.!default { + type null +} +``` + +A desktop with sound does not need it. + +## Prefix + +```sh +PROTON="$HOME/.steam/steam/steamapps/common/Proton - Experimental" +scripts/linux/setup-prefix.sh "$PROTON" ~/xcl-prefix ~/xcl-vkd3d/vkd3d-proton-xcl +``` + +It creates the prefix with Proton's Wine and copies into its `System32`: Proton's `libvkd3d-*.dll` +(without them `xcl.exe` does not start: `d3dcompiler_47` → wined3d → `libvkd3d-shader-1.dll`), DXVK's +three DLLs and the vkd3d-proton build. Only the prefix is written to. + +## Running + +```sh +scripts/linux/run.sh "$PROTON" ~/xcl-prefix /path/to/unpacked/game +``` + +`run.sh` runs `xcl.exe run ` (`XCL_EXE` picks another `xcl.exe`) with: + +| Variable | Value | +|---|---| +| `WINEPREFIX` | the prefix | +| `WINEDLLOVERRIDES` | `d3d12,d3d12core,dxgi,d3d11,d3d10core=n,b`: the prefix's DLLs, not Wine's | +| `VKD3D_CONFIG` | `ignore_shared_fence` unless set | +| `XCL_NETWORK` | `0` unless set. A retail game's crash reporter sends minidumps out; leave it off | +| `XCL_DXC_DIR` | converted to a Windows path if set | +| `WINEDEBUG` | `-all` unless set | + +Arguments after the game folder go to `xcl run`. The title's data and logs are in the prefix: +`drive_c/users//AppData/Local/XCL//` (`logs/xcl.log`, `logs/report.json`). A run left behind +is stopped with `WINEPREFIX=~/xcl-prefix "$PROTON/files/bin/wineserver" -k`. + +## Regression runs + +`scripts/smoke.ps1` (the regression run CONTRIBUTING.md asks of a PR that changes the runtime) needs +Windows. `scripts/linux/smoke.sh` is the same run under Wine: it runs the title for a fixed time with +`XCL_RUN_SECONDS`, reads XCL's log and prints the same `xcl-smoke` block, once from `main` and once +from the PR's head commit: + +```sh +scripts/linux/smoke.sh "$PROTON" ~/xcl-prefix /path/to/unpacked/game 60 +``` + +Say in the PR that the blocks come from `smoke.sh` under Wine. The `gpu:` line comes from XCL's log. +`SMOKE_GRACE` (default 120 s, as `smoke.ps1`) is how long past the run's time it may take before it is +killed: under Wine XCL's end takes about 15 s after `XCL_RUN_SECONDS` (its end watchdog), and +`PR checks` refuses a block that says `killed`. + +## Start-up check + +Under Wine `xcl run` also checks the libraries it got and prints the result with the other start-up checks +(and keeps it in `report.json`): + +- **ERROR** when the loaded `d3d12core.dll` holds the string `iface->lpVtbl == &d3d12_heap_vtbl`: this + vkd3d-proton stops on XCL's objects (#241). Use the build above. +- **WARNING** when the loaded `dxgi.dll` is not DXVK's: presenting crashes in wined3d. + +Nothing changes on Windows. + +## Known state + +- A retail game (GTA VI 1.0.2.1) starts, compiles its pipelines and runs at about 30 fps with the setup + above. **The picture stays black.** This is known upstream and not something this kit changes: on NVIDIA + under Wine the menu's element draws collapse onto one diagonal line + ([#175](https://github.com/ivall/xcl/issues/175), comments); the retail black image on memory the GPU does + not see is [#186](https://github.com/ivall/xcl/issues/186). +- The vkd3d-proton build is a workaround. The correct fix is proxies for the host's objects, so vkd3d-proton + stays untouched (proposed in #175, not implemented). +- Some tests differ under Wine (error codes, timings; #175). + +Links: [#154](https://github.com/ivall/xcl/issues/154) · [#175](https://github.com/ivall/xcl/issues/175) · +[#186](https://github.com/ivall/xcl/issues/186) · [#241](https://github.com/ivall/xcl/issues/241) diff --git a/scripts/linux/build-opus.sh b/scripts/linux/build-opus.sh new file mode 100755 index 000000000..54172f009 --- /dev/null +++ b/scripts/linux/build-opus.sh @@ -0,0 +1,48 @@ +#!/bin/bash +# Builds libopus (the Opus decoder, BSD-3-Clause) for Windows with mingw-w64: the opus.dll XCL loads at run +# time when Windows' own decoder (MSOpusDecoder.dll) is missing, which is the case under Wine / Proton. +# XCL does not distribute it (like DXC); see docs/linux.md. +# +# usage: build-opus.sh [output dir] default: ~/.local/share/xcl-runtime/opus +# -> <output dir>/opus.dll point XCL_OPUS_DIR at it (Windows path: Z:\home\you\...) or copy it +# next to xcl.exe +# needs: cmake, make, curl, sha256sum, x86_64-w64-mingw32-gcc +set -euo pipefail + +VERSION=1.5.2 +SHA256=65c1d2f78b9f2fb20082c38cbe47c951ad5839345876e46941612ee87f9a7ce1 # downloads.xiph.org/releases/opus/SHA256SUMS.txt +OUT=${1:-$HOME/.local/share/xcl-runtime/opus} + +for t in cmake make curl sha256sum x86_64-w64-mingw32-gcc; do + command -v "$t" >/dev/null || { echo "missing: $t" >&2; exit 1; } +done + +mkdir -p "$OUT/dl" +TARBALL="$OUT/dl/opus-$VERSION.tar.gz" +if [ ! -f "$TARBALL" ]; then + curl -fsSL -o "$TARBALL" "https://downloads.xiph.org/releases/opus/opus-$VERSION.tar.gz" +fi +echo "$SHA256 $TARBALL" | sha256sum -c - >&2 + +SRC="$OUT/src" +rm -rf "$SRC" && mkdir -p "$SRC" +tar -xzf "$TARBALL" -C "$SRC" --strip-components=1 + +cmake -S "$SRC" -B "$SRC/build" \ + -DCMAKE_SYSTEM_NAME=Windows \ + -DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc \ + -DCMAKE_BUILD_TYPE=Release \ + -DBUILD_SHARED_LIBS=ON \ + -DOPUS_BUILD_PROGRAMS=OFF -DOPUS_BUILD_TESTING=OFF \ + -DOPUS_INSTALL_PKG_CONFIG_MODULE=OFF -DOPUS_INSTALL_CMAKE_CONFIG_MODULE=OFF \ + -DOPUS_STACK_PROTECTOR=OFF -DOPUS_FORTIFY_SOURCE=OFF \ + -DCMAKE_C_FLAGS="-fno-stack-protector -U_FORTIFY_SOURCE" \ + -DCMAKE_SHARED_LIBRARY_PREFIX="" \ + -DCMAKE_SHARED_LINKER_FLAGS="-static-libgcc" +cmake --build "$SRC/build" -j"$(nproc)" + +DLL=$(find "$SRC/build" -maxdepth 1 -name '*opus*.dll' | head -1) +[ -n "$DLL" ] || { echo "no DLL built" >&2; exit 1; } +cp "$DLL" "$OUT/opus.dll" +echo "built: $OUT/opus.dll (libopus $VERSION)" >&2 +x86_64-w64-mingw32-objdump -p "$OUT/opus.dll" | grep -E "DLL Name|opus_decoder_create|opus_decode$" >&2 || true diff --git a/scripts/linux/build-vkd3d-proton.sh b/scripts/linux/build-vkd3d-proton.sh new file mode 100755 index 000000000..2159782a9 --- /dev/null +++ b/scripts/linux/build-vkd3d-proton.sh @@ -0,0 +1,77 @@ +#!/usr/bin/env bash +# Build vkd3d-proton v3.1 for XCL: its dxil-spirv submodule at a newer commit, the XCL patch +# applied, assertions compiled out. +# See docs/linux.md ("vkd3d-proton"). +# +# usage: build-vkd3d-proton.sh <work-dir> +# Clones vkd3d-proton into <work-dir>/vkd3d-proton, builds there, and leaves +# d3d12.dll and d3d12core.dll in <work-dir>/vkd3d-proton-xcl/ (what setup-prefix.sh takes). +# Nothing is written outside <work-dir>. Needs: git, meson, ninja, glslangValidator +# (glslang-tools), x86_64-w64-mingw32-gcc / -g++ and -widl (mingw-w64, mingw-w64-tools). +# env: VKD3D_PROTON_REPO the repository to clone (default: upstream) +# VKD3D_PROTON_REF the tag to build (default: v3.1, the one the patch is made for) +# DXIL_SPIRV_REF the dxil-spirv commit to build it with (default: 53aac41815f8; +# empty: the one the tag has) +set -euo pipefail + +if [ $# -ne 1 ]; then + echo "usage: $0 <work-dir>" >&2 + exit 2 +fi +here=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +patch=$here/vkd3d-proton-xcl.patch +repo=${VKD3D_PROTON_REPO:-https://github.com/HansKristian-Work/vkd3d-proton} +ref=${VKD3D_PROTON_REF:-v3.1} +dxs_ref=${DXIL_SPIRV_REF-53aac41815f8203dff4e081969438ed5063dbff3} + +for tool in git meson ninja glslangValidator x86_64-w64-mingw32-gcc x86_64-w64-mingw32-g++ x86_64-w64-mingw32-widl; do + command -v "$tool" >/dev/null || { echo "missing: $tool (see docs/linux.md, Prerequisites)" >&2; exit 1; } +done + +mkdir -p "$1" +work=$(cd "$1" && pwd) +src=$work/vkd3d-proton +out=$work/vkd3d-proton-xcl + +if [ ! -d "$src/.git" ]; then + git clone --branch "$ref" --recurse-submodules "$repo" "$src" +fi +cd "$src" + +# apply_once <dir> <patch>: the patch applied in <dir> unless it is already +apply_once() { + if git -C "$1" apply --check "$2" 2>/dev/null; then + git -C "$1" apply "$2" + elif git -C "$1" apply --check --reverse "$2" 2>/dev/null; then + echo "$(basename "$2") already applied" + else + echo "$2 does not apply to $1 (is it vkd3d-proton $ref?)" >&2 + exit 1 + fi +} +apply_once "$src" "$patch" + +# v3.1's dxil-spirv (8b48f0e) cannot structure 20 of a retail title's compute programs: it loops +# without end (tens of GB within minutes) or recurses until the stack runs out, and the title's +# GPU watchdog ends it during world loading (#611). Its master has fixed all of them since. +if [ -n "$dxs_ref" ]; then + dxs=$src/subprojects/dxil-spirv + if [ "$(git -C "$dxs" rev-parse HEAD)" != "$(git -C "$dxs" rev-parse --verify --quiet "$dxs_ref^{commit}" || true)" ]; then + git -C "$dxs" fetch --quiet origin + git -C "$dxs" checkout --quiet --detach "$dxs_ref" + git -C "$dxs" submodule update --init --recursive --quiet + fi + echo "dxil-spirv: $(git -C "$dxs" log -1 --format='%h %s')" +fi + +# b_ndebug: the vtable-identity assertions the objects XCL adopts would trip are compiled out +if [ -d build ]; then + meson setup --reconfigure --cross-file build-win64.txt --buildtype release -Db_ndebug=true --strip build +else + meson setup --cross-file build-win64.txt --buildtype release -Db_ndebug=true --strip build +fi +ninja -C build + +mkdir -p "$out" +cp build/libs/d3d12/d3d12.dll build/libs/d3d12core/d3d12core.dll "$out/" +echo "built: $out/{d3d12.dll,d3d12core.dll}" diff --git a/scripts/linux/run.sh b/scripts/linux/run.sh new file mode 100755 index 000000000..4d0446af1 --- /dev/null +++ b/scripts/linux/run.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash +# Run a game with xcl.exe under Proton's Wine. See docs/linux.md ("Running"). +# +# usage: run.sh <proton-dir> <prefix> <game-dir> [xcl run arguments...] +# <proton-dir> Proton Experimental's folder (it holds files/bin/wine) +# <prefix> a prefix made by setup-prefix.sh +# <game-dir> the unpacked game's folder +# env: XCL_EXE xcl.exe to run (default: target/x86_64-pc-windows-gnu/release/xcl.exe) +# XCL_DXC_DIR a Linux folder with dxcompiler.dll and dxil.dll (default: next to xcl.exe) +# XCL_NETWORK 0 unless set: a retail game's crash reporter uploads minidumps +# VKD3D_CONFIG ignore_shared_fence unless set; WINEDEBUG -all unless set +# Any other XCL_* variable is passed on to xcl.exe as it is. Nothing is written but the +# prefix (and what xcl.exe writes there). Stop a run left behind with +# WINEPREFIX=<prefix> <proton-dir>/files/bin/wineserver -k +set -euo pipefail + +if [ $# -lt 3 ]; then + echo "usage: $0 <proton-dir> <prefix> <game-dir> [xcl run arguments...]" >&2 + exit 2 +fi +proton=$1 +prefix=$2 +game=$3 +shift 3 +files=$proton/files +[ -x "$files/bin/wine" ] || files=$proton +[ -x "$files/bin/wine" ] || { echo "no files/bin/wine in $proton" >&2; exit 1; } +[ -d "$prefix/drive_c" ] || { echo "$prefix is not a prefix (setup-prefix.sh makes one)" >&2; exit 1; } +[ -d "$game" ] || { echo "no game folder $game" >&2; exit 1; } + +here=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +exe=${XCL_EXE:-$here/../../target/x86_64-pc-windows-gnu/release/xcl.exe} +[ -f "$exe" ] || { echo "no xcl.exe at $exe (see docs/linux.md, Build; XCL_EXE overrides)" >&2; exit 1; } +game=$(cd "$game" && pwd) + +export WINEPREFIX=$prefix +export WINEDEBUG=${WINEDEBUG:--all} +export WINEDLLOVERRIDES="d3d12,d3d12core,dxgi,d3d11,d3d10core=n,b" +export VKD3D_CONFIG=${VKD3D_CONFIG:-ignore_shared_fence} +export XCL_NETWORK=${XCL_NETWORK:-0} +if [ -n "${XCL_DXC_DIR:-}" ]; then + XCL_DXC_DIR=$("$files/bin/wine" winepath -w "$XCL_DXC_DIR" 2>/dev/null | tr -d '\r') + export XCL_DXC_DIR +fi + +exec "$files/bin/wine" "$exe" run "$game" "$@" diff --git a/scripts/linux/setup-prefix.sh b/scripts/linux/setup-prefix.sh new file mode 100755 index 000000000..018ba9454 --- /dev/null +++ b/scripts/linux/setup-prefix.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash +# Make a Wine prefix XCL can run in: Proton's own libvkd3d, DXVK's DXGI / D3D11 and a +# vkd3d-proton for D3D12. See docs/linux.md ("Prefix"). +# +# usage: setup-prefix.sh <proton-dir> <prefix> <vkd3d-proton-dir> +# <proton-dir> Proton Experimental's folder (it holds files/bin/wine), e.g. +# "$HOME/.steam/steam/steamapps/common/Proton - Experimental" +# <prefix> the prefix to create or update +# <vkd3d-proton-dir> a folder with d3d12.dll and d3d12core.dll: the output of +# build-vkd3d-proton.sh (<work-dir>/vkd3d-proton-xcl) +# Only <prefix> is written to. +set -euo pipefail + +if [ $# -ne 3 ]; then + echo "usage: $0 <proton-dir> <prefix> <vkd3d-proton-dir>" >&2 + exit 2 +fi +proton=$1 +vkd3d=$3 +files=$proton/files +[ -x "$files/bin/wine" ] || files=$proton # a bare "files" folder works too +[ -x "$files/bin/wine" ] || { echo "no files/bin/wine in $proton" >&2; exit 1; } +for f in d3d12.dll d3d12core.dll; do + [ -f "$vkd3d/$f" ] || { echo "no $f in $vkd3d" >&2; exit 1; } +done +mkdir -p "$2" +prefix=$(cd "$2" && pwd) + +export WINEPREFIX=$prefix WINEDEBUG=-all +export WINEDLLOVERRIDES="mscoree,mshtml=" # no Mono / Gecko download prompts +"$files/bin/wine" wineboot --init +"$files/bin/wineserver" --wait + +sys32=$prefix/drive_c/windows/system32 +[ -d "$sys32" ] || { echo "wineboot did not create $sys32" >&2; exit 1; } + +# without these, xcl.exe does not start: d3dcompiler_47 -> wined3d -> libvkd3d-shader-1.dll +cp "$files"/lib/vkd3d/x86_64-windows/libvkd3d-*.dll "$sys32/" +# with Wine's own dxgi, presenting crashes in wined3d (upstream issue #175) +for f in dxgi.dll d3d11.dll d3d10core.dll; do + cp "$files/lib/wine/dxvk/x86_64-windows/$f" "$sys32/" +done +cp "$vkd3d/d3d12.dll" "$vkd3d/d3d12core.dll" "$sys32/" + +"$files/bin/wineserver" --kill 2>/dev/null || true +echo "prefix ready: $prefix" diff --git a/scripts/linux/smoke.sh b/scripts/linux/smoke.sh new file mode 100755 index 000000000..dd8fcf6b1 --- /dev/null +++ b/scripts/linux/smoke.sh @@ -0,0 +1,92 @@ +#!/usr/bin/env bash +# scripts/smoke.ps1 under Wine: a regression run of a title for a pull request. It runs the title +# for a fixed time (XCL_RUN_SECONDS), reads XCL's log and prints the same `xcl-smoke` block as +# smoke.ps1, to paste into the PR's "Regression run" section – once built from main, once from the +# PR's head commit (CONTRIBUTING.md, "What a PR needs to be merged"). Say in the PR that the blocks +# come from this script under Wine; see docs/linux.md. +# +# usage: smoke.sh <proton-dir> <prefix> <game-dir> [seconds (default 60)] +# env: XCL_EXE (default: target/x86_64-pc-windows-gnu/release/xcl.exe); as run.sh: XCL_DXC_DIR, +# VKD3D_CONFIG, WINEDEBUG; any other XCL_* variable is passed on. SMOKE_GRACE: seconds +# the run may take past <seconds> before it is killed (default 120, as smoke.ps1). Networking is off +# (XCL_NETWORK=0). Under Wine XCL's end takes about 15 s after XCL_RUN_SECONDS (its end watchdog), +# so a grace under 20 s gets the run killed and the block says so. +set -euo pipefail + +if [ $# -lt 3 ]; then + echo "usage: $0 <proton-dir> <prefix> <game-dir> [seconds]" >&2 + exit 2 +fi +proton=$1 +prefix=$2 +game=$3 +seconds=${4:-60} +files=$proton/files +[ -x "$files/bin/wine" ] || files=$proton +[ -x "$files/bin/wine" ] || { echo "no files/bin/wine in $proton" >&2; exit 1; } +[ -d "$prefix/drive_c" ] || { echo "$prefix is not a prefix (setup-prefix.sh makes one)" >&2; exit 1; } +[ -d "$game" ] || { echo "no game folder $game" >&2; exit 1; } + +here=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +root=$(cd "$here/../.." && pwd) +exe=${XCL_EXE:-$root/target/x86_64-pc-windows-gnu/release/xcl.exe} +[ -f "$exe" ] || { echo "no xcl.exe at $exe - build it first (docs/linux.md, Build)" >&2; exit 1; } +game=$(cd "$game" && pwd) +commit=$(git -C "$root" rev-parse HEAD) +dirty=$(git -C "$root" status --porcelain --untracked-files=no -- crates Cargo.toml Cargo.lock) + +export WINEPREFIX=$prefix +export WINEDEBUG=${WINEDEBUG:--all} +export WINEDLLOVERRIDES="d3d12,d3d12core,dxgi,d3d11,d3d10core=n,b" +export VKD3D_CONFIG=${VKD3D_CONFIG:-ignore_shared_fence} +export XCL_NETWORK=0 XCL_RUN_SECONDS=$seconds XCL_ACTIVITY=1 +if [ -n "${XCL_DXC_DIR:-}" ]; then + XCL_DXC_DIR=$("$files/bin/wine" winepath -w "$XCL_DXC_DIR" 2>/dev/null | tr -d '\r') + export XCL_DXC_DIR +fi + +out=$(mktemp) +trap 'rm -f "$out"' EXIT +code=0 +timeout -k 10 $((seconds + ${SMOKE_GRACE:-120})) "$files/bin/wine" "$exe" run "$game" >"$out" 2>/dev/null </dev/null || code=$? +if [ "$code" -eq 124 ] || [ "$code" -eq 137 ]; then + exitcode='killed (no exit)' +else + exitcode=$(printf '0x%x' "$code") +fi + +winlog=$(tr -d '\r' <"$out" | sed -n 's/^Log:[[:space:]]*//p' | tail -n 1) +if [ -z "$winlog" ]; then + # a timed run (XCL_RUN_SECONDS) can end before the launcher prints its summary under Wine: + # the log is in the title's data folder that it printed at the start + data=$(tr -d '\r' <"$out" | sed -n 's/^[[:space:]]*data:[[:space:]]*//p' | head -n 1) + [ -n "$data" ] && winlog="$data\\logs\\xcl.log" +fi +[ -n "$winlog" ] || { echo "XCL printed no log path; its output:" >&2; cat "$out" >&2; exit 1; } +log=$("$files/bin/wine" winepath -u "$winlog" 2>/dev/null | tr -d '\r') +[ -f "$log" ] || { echo "no log at $log ($winlog)" >&2; exit 1; } + +frames=$(grep -oE '\(([0-9]+) frames shown, ([0-9]+) PresentX calls\)' "$log" | tail -n 1 | + sed -E 's/\(([0-9]+) frames shown, ([0-9]+) PresentX calls\)/\1 (PresentX \2)/') +[ -n "$frames" ] || frames='none logged (the run did not reach its end)' +presents=$(grep -oE 'activity \(2 s\): [0-9]+ presents' "$log" | grep -oE '[0-9]+ presents' | + grep -oE '^[0-9]+' | tail -n 3 | paste -sd' ' -) +peak=$(grep -oE 'memory: [0-9]+ MB committed' "$log" | grep -oE '[0-9]+' | sort -n | tail -n 1) +removals=$(grep -c 'device removed' "$log" || true) +crashes=$(grep -cE 'unhandled exception|process ends with status' "$log" || true) +gpu=$(grep -oE "backend 'real' – .+ \([0-9]+ MiB VRAM\)" "$log" | head -n 1 | + sed -E "s/^backend 'real' – (.+) \([0-9]+ MiB VRAM\)$/\1/") + +echo '```' +echo 'xcl-smoke v1' +echo "commit: $commit$([ -n "$dirty" ] && echo ' (uncommitted changes)')" +echo "gpu: ${gpu:-unknown}" +echo "title: $(basename "$game")" +echo "seconds: $seconds" +echo "exit: $exitcode" +echo "frames shown: $frames" +echo "presents per 2 s (last 3): $presents" +echo "device removals: $removals" +echo "crashes: $crashes" +echo "peak commit: ${peak:-n/a} MB" +echo '```' diff --git a/scripts/linux/vkd3d-proton-xcl.patch b/scripts/linux/vkd3d-proton-xcl.patch new file mode 100644 index 000000000..5905add60 --- /dev/null +++ b/scripts/linux/vkd3d-proton-xcl.patch @@ -0,0 +1,24 @@ +diff --git a/libs/vkd3d/command.c b/libs/vkd3d/command.c +index c6f6ee8..c397ca7 100644 +--- a/libs/vkd3d/command.c ++++ b/libs/vkd3d/command.c +@@ -3352,7 +3352,8 @@ static bool d3d12_command_allocator_allocate_query_from_heap_type(struct d3d12_c + + static struct d3d12_command_allocator *d3d12_command_allocator_from_iface(ID3D12CommandAllocator *iface) + { +- if (!iface || iface->lpVtbl != &d3d12_command_allocator_vtbl) ++ /* XCL: objects it adopts carry an extended vtable; their layout is ours. */ ++ if (!iface) + return NULL; + + return impl_from_ID3D12CommandAllocator(iface); +@@ -23182,7 +23183,8 @@ HRESULT d3d12_command_list_create(struct d3d12_device *device, + + struct d3d12_command_list *d3d12_command_list_from_iface(ID3D12CommandList *iface) + { +- bool is_valid = false; ++ /* XCL: objects it adopts carry an extended vtable; their layout is ours. */ ++ bool is_valid = true; + if (!iface) + return NULL; +