Skip to content
Closed
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
41 changes: 34 additions & 7 deletions d3d12/crates/leonida-gpu/src/compose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ use windows::core::{PCSTR, s};
use crate::host::Host;

const SOURCE: &str = r#"
#define RS "DescriptorTable(SRV(t0, numDescriptors=2), visibility=SHADER_VISIBILITY_PIXEL), StaticSampler(s0, filter=FILTER_MIN_MAG_MIP_LINEAR, addressU=TEXTURE_ADDRESS_CLAMP, addressV=TEXTURE_ADDRESS_CLAMP)"
Texture2D<float4> scene : register(t0);
Texture2D<float4> over : register(t1);
SamplerState smooth_ : register(s0);
struct V { float4 at : SV_Position; float2 uv : TEXCOORD0; };
[RootSignature(RS)]
V vs(uint id : SV_VertexID) {
V v;
v.uv = float2((id << 1) & 2, id & 2);
Expand Down Expand Up @@ -58,7 +56,6 @@ float clean1(float x) {
float3 clean(float3 x) {
return float3(clean1(x.r), clean1(x.g), clean1(x.b));
}
[RootSignature(RS)]
float4 ps(V v) : SV_Target {
float3 s = sharpened(v.uv);
float4 o = over.SampleLevel(smooth_, v.uv, 0);
Expand Down Expand Up @@ -137,11 +134,41 @@ impl Composer {
);
// SAFETY: the blobs' own bytes, alive through the calls.
unsafe {
let bytes = |b: &ID3DBlob| {
std::slice::from_raw_parts(b.GetBufferPointer().cast::<u8>(), b.GetBufferSize())
};
let device = &host.device;
let root: ID3D12RootSignature = device.CreateRootSignature(0, bytes(&vs))?;
let ranges = [D3D12_DESCRIPTOR_RANGE1 {
RangeType: D3D12_DESCRIPTOR_RANGE_TYPE_SRV,
NumDescriptors: 2,
BaseShaderRegister: 0,
RegisterSpace: 0,
Flags: D3D12_DESCRIPTOR_RANGE_FLAG_DATA_STATIC_WHILE_SET_AT_EXECUTE,
OffsetInDescriptorsFromTableStart: D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND,
}];
let parameters = [D3D12_ROOT_PARAMETER1 {
ParameterType: D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE,
Anonymous: D3D12_ROOT_PARAMETER1_0 {
DescriptorTable: D3D12_ROOT_DESCRIPTOR_TABLE1 {
NumDescriptorRanges: ranges.len() as u32,
pDescriptorRanges: ranges.as_ptr(),
},
},
ShaderVisibility: D3D12_SHADER_VISIBILITY_PIXEL,
}];
let samplers = [D3D12_STATIC_SAMPLER_DESC {
Filter: D3D12_FILTER_MIN_MAG_MIP_LINEAR,
AddressU: D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
AddressV: D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
AddressW: D3D12_TEXTURE_ADDRESS_MODE_WRAP,
MipLODBias: 0.0,
MaxAnisotropy: 16,
ComparisonFunc: D3D12_COMPARISON_FUNC_LESS_EQUAL,
BorderColor: D3D12_STATIC_BORDER_COLOR_OPAQUE_WHITE,
MinLOD: 0.0,
MaxLOD: f32::MAX,
ShaderRegister: 0,
RegisterSpace: 0,
ShaderVisibility: D3D12_SHADER_VISIBILITY_ALL,
}];
let root = crate::host::root_signature(device, &parameters, &samplers)?;
let code = |b: &ID3DBlob| D3D12_SHADER_BYTECODE {
pShaderBytecode: b.GetBufferPointer(),
BytecodeLength: b.GetBufferSize(),
Expand Down
27 changes: 27 additions & 0 deletions d3d12/crates/leonida-gpu/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1686,6 +1686,33 @@ pub(crate) fn compile(
Ok(bytes.to_vec())
}

pub(crate) fn root_signature(
device: &ID3D12Device,
parameters: &[D3D12_ROOT_PARAMETER1],
samplers: &[D3D12_STATIC_SAMPLER_DESC],
) -> windows::core::Result<ID3D12RootSignature> {
use windows::Win32::Graphics::Direct3D::ID3DBlob;
let desc = D3D12_VERSIONED_ROOT_SIGNATURE_DESC {
Version: D3D_ROOT_SIGNATURE_VERSION_1_1,
Anonymous: D3D12_VERSIONED_ROOT_SIGNATURE_DESC_0 {
Desc_1_1: D3D12_ROOT_SIGNATURE_DESC1 {
NumParameters: parameters.len() as u32,
pParameters: if parameters.is_empty() { std::ptr::null() } else { parameters.as_ptr() },
NumStaticSamplers: samplers.len() as u32,
pStaticSamplers: if samplers.is_empty() { std::ptr::null() } else { samplers.as_ptr() },
Flags: D3D12_ROOT_SIGNATURE_FLAG_NONE,
},
},
};
let mut blob: Option<ID3DBlob> = None;
unsafe {
D3D12SerializeVersionedRootSignature(&desc, &mut blob, None)?;
let blob = blob.ok_or_else(windows::core::Error::empty)?;
let bytes = std::slice::from_raw_parts(blob.GetBufferPointer().cast::<u8>(), blob.GetBufferSize());
device.CreateRootSignature(0, bytes)
}
}

impl PlanePass {
/// The root signature and pipeline for a twin of 16-bit steps (`steps`)
/// or of floats and the depth format `format`.
Expand Down
42 changes: 29 additions & 13 deletions d3d12/crates/leonida-gpu/src/host/trainer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,16 @@ use leonida_menu::paint::{LEFT, TOP};
use super::{Host, HostList, transition};

const SOURCE: &str = r#"
#define RS "RootConstants(num32BitConstants=8, b0), DescriptorTable(SRV(t0))"
cbuffer Place : register(b0) { float2 target; float2 origin; float2 size; float band_top; float band_bottom; };
Texture2D<float4> menu : register(t0);
struct V { float4 pos : SV_Position; };
[RootSignature(RS)]
V vs(uint id : SV_VertexID) {
float2 corner = float2(id & 1, id >> 1);
float2 px = origin + corner * size;
V v;
v.pos = float4(px / target * float2(2.0, -2.0) + float2(-1.0, 1.0), 0.0, 1.0);
return v;
}
[RootSignature(RS)]
float4 ps(V v) : SV_Target {
float2 p = v.pos.xy - origin;
int layer = (p.y >= band_top && p.y < band_bottom) ? 1 : 0;
Expand Down Expand Up @@ -105,13 +102,6 @@ fn compile(
code.ok_or_else(windows::core::Error::empty)
}

fn bytes(blob: &ID3DBlob) -> &[u8] {
// SAFETY: the blob's own bytes, alive as long as it is.
unsafe {
std::slice::from_raw_parts(blob.GetBufferPointer().cast::<u8>(), blob.GetBufferSize())
}
}

/// The pipeline for one target format.
struct Pipeline {
root: ID3D12RootSignature,
Expand All @@ -122,10 +112,36 @@ struct Pipeline {
fn pipeline(device: &ID3D12Device, format: DXGI_FORMAT) -> windows::core::Result<Pipeline> {
let vs = compile(windows::core::s!("vs"), windows::core::s!("vs_5_1"))?;
let ps = compile(windows::core::s!("ps"), windows::core::s!("ps_5_1"))?;
// SAFETY: the blobs' own bytes (the root signature is inside them);
// plain object creation.
// SAFETY: the blobs' own bytes; plain object creation.
unsafe {
let root: ID3D12RootSignature = device.CreateRootSignature(0, bytes(&vs))?;
let ranges = [D3D12_DESCRIPTOR_RANGE1 {
RangeType: D3D12_DESCRIPTOR_RANGE_TYPE_SRV,
NumDescriptors: 1,
BaseShaderRegister: 0,
RegisterSpace: 0,
Flags: D3D12_DESCRIPTOR_RANGE_FLAG_DATA_STATIC_WHILE_SET_AT_EXECUTE,
OffsetInDescriptorsFromTableStart: D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND,
}];
let parameters = [
D3D12_ROOT_PARAMETER1 {
ParameterType: D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS,
Anonymous: D3D12_ROOT_PARAMETER1_0 {
Constants: D3D12_ROOT_CONSTANTS { ShaderRegister: 0, RegisterSpace: 0, Num32BitValues: 8 },
},
ShaderVisibility: D3D12_SHADER_VISIBILITY_ALL,
},
D3D12_ROOT_PARAMETER1 {
ParameterType: D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE,
Anonymous: D3D12_ROOT_PARAMETER1_0 {
DescriptorTable: D3D12_ROOT_DESCRIPTOR_TABLE1 {
NumDescriptorRanges: ranges.len() as u32,
pDescriptorRanges: ranges.as_ptr(),
},
},
ShaderVisibility: D3D12_SHADER_VISIBILITY_ALL,
},
];
let root = super::root_signature(device, &parameters, &[])?;
let mut blend = D3D12_BLEND_DESC::default();
blend.RenderTarget[0] = D3D12_RENDER_TARGET_BLEND_DESC {
BlendEnable: true.into(),
Expand Down
3 changes: 2 additions & 1 deletion docs/boot.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ and the one line that names the cause is
`CreateRootSignature`.

Copying the DLLs a Proton prefix installs is not enough on its own: Wine keeps preferring its own copy next to
them. Name them in the overrides.
them. Name them in the overrides. For the D3D12 runtime (`leonida.exe`), `scripts/play-linux.sh` does both itself,
under a Proton or the system's Wine ([linux.md](linux.md#the-d3d12-runtime-leonidaexe-under-wine)).

```bash
GE=$HOME/GE-Proton11-13-x86_64/files/lib/wine
Expand Down
35 changes: 24 additions & 11 deletions docs/launcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,24 @@ Standard library only (`tools/leonida_launcher.py`).

## Pages

- **Play**: PLAY runs `powershell -NoProfile -ExecutionPolicy Bypass -File scripts\play-d3d12.ps1 -Game <folder>
-Data <folder>` plus the switches chosen in Settings (on Linux `scripts/play-linux.sh <folder>/game_scarlett_master.exe`,
with `--series-x` if chosen). The script's output goes into the log box. The state line reads a few of the script's
own messages: Starting, Building the runtime, Preparing shaders, Running, Exited with code N (a guess from the text;
the log is the truth). Only one thing runs at a time: while it runs PLAY is replaced by Stop, which, after a
confirmation, ends the process and everything it started (`taskkill /T /F`; on Linux the process group).
- **Play**: PLAY runs `powershell -NoProfile -ExecutionPolicy Bypass -File scripts\play-d3d12.ps1 -Game <folder> -Data
<folder>` plus the switches chosen in Settings (on Linux `scripts/play-linux.sh <folder>/game_scarlett_master.exe`
with `--series-x` and `--log` if chosen; without a usable Proton or Wine, PLAY opens Settings). The script's output
goes into the log box. The state line reads a few of the script's own messages: Starting, Building the runtime,
Preparing shaders, Running, Exited with code N (a guess from the text; the log is the truth). Only one thing runs at a
time: while it runs PLAY is replaced by Stop, which, after a confirmation, ends the process and everything it started
(`taskkill /T /F`; on Linux the process group).
- **Settings**: the game folder (a picker, and a check that `game_scarlett_master.exe` is in it), the data folder
(empty = the script's default), the mods folder, and the options of `play-d3d12.ps1`: DLSS (off is `-NoDlss`),
(empty = the script's default), the mods folder and the options of `play-d3d12.ps1`: DLSS (off is `-NoDlss`),
`-RayTracing`, `-SmoothMotion`, shader preparation (off is `-NoPrecompile`), `-SeriesX`, `-Quality`, `-Resolution`
(default = none), `-Capture`, `-Log`. On Linux only the game folder and Series X are used.
(default = none), `-Capture`, `-Log`. Log to a file (`-Log`, on Linux `--log`) writes the runtime's error output to
`play-<MMdd-HHmmss>.log` in the logs folder and keeps the five newest.
- **Settings on Linux**: only the game folder, Proton or Wine, Series X and Log to a file are used. The Proton or Wine
list starts with "Automatic" (the default: the script's own pick, named beside it), then the usable Proton builds
(highest version first) and "System Wine" when `wine` is on `PATH`; a named entry is stored as that folder. Browse
picks another Proton folder. A line below the list appears only when a folder picked with Browse cannot be used.
How the script picks and runs a Proton or Wine:
[linux.md](linux.md#the-d3d12-runtime-leonidaexe-under-wine).
- **Mods**: the `mods\*\mod.json` of the mods folder (this repository's `mods` unless changed), with the enabled flag
written back as the mod manager does it (`tools/leonida_mods.py`); a button opens that manager.
- **Saves**: the D3D12 runtime's saves under the data folder, `<data>\data\7952987A\saves\<user id>\<configuration>\<Name>~<FNV-1a 64
Expand All @@ -35,8 +43,9 @@ Standard library only (`tools/leonida_launcher.py`).
export and import a single blob, and a read-only hex and strings viewer. All of them are refused while the game
is running.
- **Tools**: fetch NVIDIA's DLSS files (`d3d12\Get-NVIDIA-DLSS.cmd`, after a confirmation: about 70 MB from NVIDIA's
GitHub), rebuild the runtime (`d3d12\scripts\build-nvidia.ps1`), open the logs folder
(`%LOCALAPPDATA%\Leonida\logs`) and the screenshots folder. The two scripts are Windows only.
GitHub), rebuild the runtime (`d3d12\scripts\build-nvidia.ps1`; on Linux `scripts/build-d3d12-linux.sh` after
accepting Microsoft's SDK licence, see [linux.md](linux.md)), open the logs folder (`%LOCALAPPDATA%\Leonida\logs`,
on Linux `~/.local/share/leonida/logs`) and the screenshots folder. The DLSS download is Windows only.
- **About**: what the port is, that it runs your own copy, the credits (see `d3d12/README.md`) and the licence.

## Settings file
Expand All @@ -48,7 +57,11 @@ window. Unknown or mistyped keys are ignored.

`python -I tests/test_leonida_saves.py` does the same for the Saves page on fake save trees. `python -I tests/test_leonida_launcher.py` covers the parts without a window: reading and writing the settings, the
command line each option produces (checked against the switch names in `scripts/play-d3d12.ps1`), the mod list and
the newest-picture search.
the newest-picture search, and the Proton or Wine setting. `python -I tests/test_play_linux.py` runs
`scripts/play-linux.sh` against Proton folders and a Wine the test creates itself and checks which one it picks,
where DXVK's `dxgi.dll` comes from, how it sets up the prefix and where it writes its logs.
`python -I tests/test_build_d3d12_linux.py` runs `scripts/build-d3d12-linux.sh` with stand-in tools and checks
where it builds `leonida.exe` and when it fetches VKD3D-Proton and DXVK again.

## Saves: what the bytes are, and what is not edited

Expand Down
55 changes: 55 additions & 0 deletions docs/linux.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,61 @@ unless you add the `x86_64-pc-windows-msvc` Rust target or pass a prebuilt stati
`-DLEONIDA_XMA2_DECODER_LIB=<path>`, built yourself from this checkout's `runtime/xma_decoder` with the command in
[runtime/xma_decoder/README.md](../runtime/xma_decoder/README.md).

### The D3D12 runtime (`leonida.exe`) under Wine

`scripts/play-linux.sh` and PLAY in the launcher start `leonida.exe`, the D3D12 runtime in `d3d12/`. `./Build.sh`
does not build it. On Linux:

```sh
cargo install --locked cargo-xwin # once; also needs clang and lld
XWIN_ACCEPT_LICENSE=1 sh scripts/build-d3d12-linux.sh # leonida.exe, VKD3D-Proton, DXVK's dxgi.dll, d3dcompiler_47
sh scripts/play-linux.sh /path/to/game_scarlett_master.exe
```

cargo-xwin downloads Microsoft's CRT and Windows SDK on its first run. `XWIN_ACCEPT_LICENSE=1` accepts their licence;
without it the script stops before downloading. The runtime lands in `d3d12/target/x86_64-pc-windows-msvc/diag/`,
VKD3D-Proton in `d3d12/target/vkd3d-proton/` (fetching it needs Python 3.14 or newer), DXVK's `dxgi.dll` in
`d3d12/target/dxvk/` (`scripts/fetch_dxvk.py`, which fetches it on its own too). The fetch scripts write
`SOURCE.txt` last, and the build script fetches again while a folder lacks one of its files. `play-linux.sh` looks for
`leonida.exe` beside itself, then under `d3d12/target/`. `LEONIDA_RUNTIME` and `LEONIDA_VKD3D` select other files.

The script takes `LEONIDA_PROTON` (a Proton folder or its `files/bin/wine`), else `LEONIDA_WINE` (a Wine program, such
as `wine`), else the usable Proton build with the highest version, else `wine` on `PATH`. Every folder with
`files/bin/wine` counts as a Proton build, whatever its name, in Steam's `compatibilitytools.d` and `steamapps/common`
(all libraries, Flatpak and Snap too), `STEAM_EXTRA_COMPAT_TOOLS_PATHS` and the system's `compatibilitytools.d`; it is
usable when it also has `files/lib/wine` or `files/lib64/wine`; others are left out. Builds are ranked by the numbers
in their version. `--find-proton` prints the Proton or Wine the script would use, `--list-protons` every usable build;
neither changes anything.

The game needs DXVK's `dxgi.dll`: with Wine's own (on `wined3d`) it crashes when it creates its swap chain, and Wine's
own `d3d12.dll` (vkd3d) creates no device for it. The `dxgi.dll` comes from `LEONIDA_DXVK` (a folder with a 64-bit
`dxgi.dll`) when it is set, else from the Proton; if it has none, or under the system's Wine, from the package (`dxvk/`
beside the script, then `d3d12/target/dxvk/`), else the distribution's DXVK (under `/usr/share/dxvk` or
`/usr/lib/dxvk`), else another Proton. Without any, the script stops and names `fetch_dxvk.py`. It copies that dxgi.dll
and the bundled VKD3D-Proton into its own prefix (`LEONIDA_WINEPREFIX`, made and updated with the Wine in use) and sets
`WINEDLLOVERRIDES=d3d12,d3d12core,dxgi=n`, after any overrides already set, since Wine keeps the last one for a DLL
([boot.md](boot.md#under-wine-on-linux)). Under a Proton it puts the build's
libraries first in `WINEDLLPATH` and `LD_LIBRARY_PATH`, as Proton's own `proton` script does. VKD3D-Proton's shader
cache goes to `<LEONIDA_CACHE_DIR>/vkd3d-proton`. With `VKD3D_SHADER_CACHE_PATH=0` it keeps none (the same as
`VKD3D_CONFIG=pipeline_library_app_cache`), and the title's D3D12 pipeline library then holds the SPIR-V and the
driver's pipeline cache.

Wine's own `d3dcompiler_47.dll` rejects the runtime's indirect-argument clamp (`E5005`, `InterlockedOr` on a
`RWByteAddressBuffer`), and then every indirect draw is dropped: the intro shows, gameplay and cutscenes have no 3D.
`play-linux.sh` installs Microsoft's 64-bit `d3dcompiler_47.dll` into its prefix (`d3dcompiler_47=n`), from
`LEONIDA_D3DCOMPILER`, else `d3dcompiler/` beside the script, else `d3d12/target/d3dcompiler/`, else fetched there by
`scripts/fetch_d3dcompiler.py` (pinned NuGet package and SHA-256). Without it the script warns and starts anyway.

VKD3D-Proton releases without dxil-spirv's new structurizer turn some of the title's compute shaders into invalid
SPIR-V, and the Vulkan driver crashes on them (`vkCreateComputePipelines`, exception `0xc0000005`). A `d3d12core.dll`
counts as having it when it names the `legacy_structurizer` option (VKD3D-Proton `v3.1-22` to `v3.1-96`) or when its
build is `v3.1-22` or later: `v3.1-97` removed the legacy structurizer and with it that name. The build is read as
`v3.1-97-g<commit>` from the DLL or the `version` file beside it, else as `vkd3d-1.1-<n>-g<commit>`, the form Proton
writes in that file (`v3.1` is `vkd3d-1.1-5686`), else from the release number the DLL carries (3.2 on). When the
bundled `d3d12core.dll` lacks it, `play-linux.sh` takes the newest such VKD3D-Proton among the Proton in use and the
other Protons found, the Proton in use first among equal builds, also when `LEONIDA_PROTON` chose the Proton. If none
has it, the script warns and uses the bundled one. `LEONIDA_VKD3D` selects a VKD3D-Proton and skips this check.

### Why there is no native Linux `gta6rt`

A native (ELF) loader is not a matter of build flags:
Expand Down
Loading