Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions THIRD_PARTY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |

Expand Down
77 changes: 75 additions & 2 deletions crates/xcl/src/preflight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -48,8 +49,23 @@ pub struct Found {
pub dxc_toolchain: crate::gpu::recomp::dxc::Toolchain,
pub xgameruntime: bool,
pub gpu: Result<Gpu, String>,
/// the D3D12 / DXGI libraries of a Wine prefix; `None` on Windows
pub wine: Option<WineLibs>,
}

/// 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<bool>,
/// `dxgi.dll` is DXVK's
pub dxgi_is_dxvk: Option<bool>,
}

/// 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<String>, fix: Option<&str>) -> Check {
Check { level, what: what.into(), fix: fix.map(str::to_string) }
}
Expand Down Expand Up @@ -93,6 +109,22 @@ pub fn checks(f: &Found) -> Vec<Check> {
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
}

Expand Down Expand Up @@ -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<bool> {
use windows_sys::Win32::System::LibraryLoader::GetModuleHandleW;
use windows_sys::Win32::System::Memory::*;
let wide: Vec<u16> = 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<WineLibs> {
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<Check> {
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)
}
Expand Down Expand Up @@ -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 {
Expand Down
191 changes: 191 additions & 0 deletions docs/linux.md
Original file line number Diff line number Diff line change
@@ -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" <wine>/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 <game>` (`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/<user>/AppData/Local/XCL/<title>/` (`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)
48 changes: 48 additions & 0 deletions scripts/linux/build-opus.sh
Original file line number Diff line number Diff line change
@@ -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
Loading
Loading