Engine: native presets K and M in the D3D12 shim, wider game coverage

A standalone Vulkan inference engine (engine/) for DLSS presets K and M that
runs from weights extracted from the user's own nvngx_dlss.dll, with d4r's
own input, network and reconstruction shaders or the native HIP network
layers, recorded into the game's command list by the shim. Opt-in
([Engine] Enabled / D4R_ENGINE=1); anything uncovered falls back to the
CUDA/ZLUDA path with the reason logged.

Byte-identical to NGX's CUDA backend under the phase-capable reference
(1706x960 -> 2560x1440, native HIP network, eleven frames, RX 9070 XT):
- K render- and display-resolution motion vectors, HDR and LDR
- K game-supplied exposure and automatic exposure with any pre-exposure
  and exposure scale (one residual: effective exposure exactly 2 or 4)
- K and M with the DLSS indicator's inverted-axis parameters
- M with any exposure settings (NGX ignores them for M)
K packages need the current compile_k.py (marker D4RO0003).

Also: HIP shared buffers are pooled across feature recreation (ROCm leaks
destroyed imports), hipnet.bin must match the preset, M-only games do not
load the K model, install_d4r_runtime.sh stages k, k-ldr, m, m-ldr with
hip/, the activation table has a generator (gen_activation_m.py), and
package_release.sh can bundle the engine's shaders (never its weights).

Signed-off-by: woutermans <[email protected]>
This commit is contained in:
woutermans committed 2026-10-10 20:54:01 +02:00
1 parent 16cfc1f063
commit e0d1549a21
82 files changed
+13335 -96

No files matched your search

+3 -1
View File
@@ -18,7 +18,9 @@ ln -sfn "$VULKAN_INCLUDE/vk_video" "$ROOT/build/vulkan-include/vk_video"
PARAM_OBJECT="$ROOT/build/d4r_ngx_param_msvc.obj"
"$CLANG_CL" --target=x86_64-pc-windows-msvc /nologo /std:c++20 /O2 /c /GS- /GR- /EHs-c- /Zl \
"/Fo$PARAM_OBJECT" -- "$ROOT/tools/d4r_ngx_param_msvc.cpp"
"$MINGW_CXX" -std=c++20 -O2 -Wall -Wextra -Wno-missing-field-initializers -I"$ROOT/build/vulkan-include" -I"$ROOT/build/vulkan-staging" -shared -static -static-libgcc -static-libstdc++ \
# The engine (D4R_ENGINE) reaches Vulkan through a function table: MinGW has no Vulkan import library.
"$MINGW_CXX" -std=c++20 -O2 -Wall -Wextra -Wno-missing-field-initializers -DD4R_ENGINE_VK_TABLE -I"$ROOT/build/vulkan-include" -I"$ROOT/build/vulkan-staging" -shared -static -static-libgcc -static-libstdc++ \
"$ROOT/tools/d4r_nvngx_shim.cpp" "$ROOT/tools/d4r_ngx_param_host.cpp" "$PARAM_OBJECT" \
"$ROOT/engine/runtime.cpp" "$ROOT/engine/game.cpp" "$ROOT/engine/game_m.cpp" "$ROOT/engine/vk_dispatch.cpp" \
-Wl,--entry,d4r_dll_entry -o "$ROOT/build/d4r_nvngx.dll"
printf 'Built %s\n' "$ROOT/build/d4r_nvngx.dll"
+10
View File
@@ -156,6 +156,16 @@ def main(argv):
fail(f"[Interop] MarkerPollUs must be a number of microseconds, not {value!r}")
env["D4R_SHIM_MARKER_POLL_US"] = value
value = flag("Engine", "Enabled")
env["D4R_ENGINE"] = "1" if value else "0"
if path_value("Engine", "ModelDir"):
env["D4R_ENGINE_MODEL_DIR"] = path_value("Engine", "ModelDir")
network = get("Engine", "Network")
if network is not None and network not in ("auto", "hip", "vulkan"):
fail("[Engine] Network must be auto, hip or vulkan")
if network in ("hip", "vulkan"):
env["D4R_ENGINE_NETWORK"] = network
engine = path_value("Game", "EngineIni")
cvars = get("Game", "Cvars")
if engine and cvars:
+22
View File
@@ -7,6 +7,11 @@ set -euo pipefail
# bin/d4r_nvngx.dll D3D12-to-CUDA NGX core shim (OptiScaler NvngxPath target)
# ngx/_nvngx.dll official NGX core
# dlss/nvngx_dlss.dll official DLSS SR feature DLL (also copied into bin/)
# engine/k/, k-ldr/ models of preset K for the native engine ([Engine] in d4r.ini), when D4R_ENGINE_MODEL /
# D4R_ENGINE_MODEL_LDR name the folders engine/compile_k.py wrote (--ldr for the second)
# engine/m/, m-ldr/ models of preset M, when D4R_ENGINE_MODEL_M / D4R_ENGINE_MODEL_M_LDR name the folders
# engine/compile_m.py wrote
# Each model folder is copied whole, with the native HIP network's hipnet.bin and hip/ when built with --hip.
# usage: install_d4r_runtime.sh PATH_TO_NGX_CORE_DLL PATH_TO_NVNGX_DLSS_DLL
if [[ $# -ne 2 ]]; then
printf 'usage: %s PATH_TO_NGX_CORE_DLL PATH_TO_NVNGX_DLSS_DLL\n' "$0" >&2
@@ -25,5 +30,22 @@ copy_unless_same "$2" "$RUNTIME/dlss/nvngx_dlss.dll"
# The NGX core's CUDA init ignores the feature search paths it is given and
# scans the directory of the module that calls it, i.e. the shim's.
cp -f "$RUNTIME/dlss/nvngx_dlss.dll" "$RUNTIME/bin/nvngx_dlss.dll"
# K and M models; the -ldr variants serve games without the HDR flag
for variant in k k-ldr m m-ldr; do
case "$variant" in
k) source_dir="${D4R_ENGINE_MODEL:-}" required="weights.bin offsets.bin lut.bin input_k.spv output_k.spv exposure_k0.spv exposure_k1.spv" compiler=compile_k.py ;;
k-ldr) source_dir="${D4R_ENGINE_MODEL_LDR:-}" required="weights.bin offsets.bin lut.bin input_k.spv output_k.spv" compiler="compile_k.py --ldr" ;;
m) source_dir="${D4R_ENGINE_MODEL_M:-}" required="offsets.bin enc0_m.spv" compiler=compile_m.py ;;
m-ldr) source_dir="${D4R_ENGINE_MODEL_M_LDR:-}" required="offsets.bin enc0_m.spv" compiler="compile_m.py --ldr" ;;
esac
[[ -n "$source_dir" ]] || continue
for f in $required; do
[[ -f "$source_dir/$f" ]] || { printf '%s lacks %s (run engine/%s)\n' "$source_dir" "$f" "$compiler" >&2; exit 2; }
done
rm -rf "$RUNTIME/engine/$variant"
mkdir -p "$RUNTIME/engine"
cp -r "$source_dir" "$RUNTIME/engine/$variant"
printf 'Installed the native engine model in %s\n' "$RUNTIME/engine/$variant"
done
sha256sum "$RUNTIME"/ngx/_nvngx.dll "$RUNTIME"/dlss/nvngx_dlss.dll
printf 'Installed d4r runtime in %s\n' "$RUNTIME"
+17
View File
@@ -32,6 +32,10 @@
# Accuracy texture sets go in accuracy/<target>/, built with D4R_PREFER_ACCURACY=1.
# D4R_ZLUDA_EMIT d4r_emit from the patched ZLUDA build; required in full builds when an accuracy
# texture set or L's unfolded texture variants are not supplied in D4R_BUNDLE_TEX.
# D4R_BUNDLE_ENGINE optional: the native engine's preset K model (the folder engine/compile_k.py wrote),
# packaged as d4r/engine/k for d4r.ini [Engine]. Only d4r's shaders are taken from it: the
# weights and reconstruction table are values read from NVIDIA's DLSS and are never packaged,
# in any variant. Users produce them from their own nvngx_dlss.dll (engine/README.md).
# D4R_BUNDLE_NVIDIA=installer (default) ships install.sh instead of NVIDIA DLLs, retaining texture kernels.
# D4R_BUNDLE_NVIDIA=1 bundles both DLLs; =0 omits DLLs and texture kernels (nonvidia ZIP).
set -euo pipefail
@@ -119,6 +123,16 @@ if [[ "$VARIANT" != clean ]]; then
else
printf 'Put NVIDIA'"'"'s NGX runtime, _nvngx.dll, in this folder (see D4R_README.txt).\r\n' > "$STAGE/d4r/ngx/README.txt"
fi
if [[ -n "${D4R_BUNDLE_ENGINE:-}" ]]; then
for f in input_k.spv output_k.spv exposure_k0.spv exposure_k1.spv; do
[[ -f "$D4R_BUNDLE_ENGINE/$f" ]] || { echo "D4R_BUNDLE_ENGINE lacks $f (run engine/compile_k.py)" >&2; exit 2; }
done
mkdir -p "$STAGE/d4r/engine/k"
cp "$D4R_BUNDLE_ENGINE"/*.spv "$STAGE/d4r/engine/k/"
# d4r's own shader-revision marker; every other .bin in the model holds values read from NVIDIA's DLSS
[[ ! -f "$D4R_BUNDLE_ENGINE/direct_origins.bin" ]] || cp "$D4R_BUNDLE_ENGINE/direct_origins.bin" "$STAGE/d4r/engine/k/"
printf 'The native engine needs weights.bin, offsets.bin and lut.bin here (see D4R_README.txt).\r\n' > "$STAGE/d4r/engine/k/README.txt"
fi
IFS=: read -r -a DLLS <<< "$D4R_DLSS_DLLS"
# one folder per target; RDNA4 targets also get <target>-fp8 (native FP8 WMMA, d4r.ini NativeFp8), which the
# bridge serves instead when that setting is on
@@ -253,3 +267,6 @@ rm -f "$OUT/$NAME.zip"
(cd "$STAGE" && find . -type f | LC_ALL=C sort | sed 's|^\./||' | zip -q -X -9 "$OUT/$NAME.zip" -@)
(cd "$OUT" && sha256sum "$NAME.zip" > "$NAME.zip.sha256")
printf 'Built %s (%s)\n' "$OUT/$NAME.zip" "$(du -h "$OUT/$NAME.zip" | cut -f1)"
if [[ "$VARIANT" == full ]]; then
echo "This zip contains NVIDIA's DLLs and kernels built from NVIDIA's code. Do not publish or share it." >&2
fi
@@ -41,6 +41,8 @@ if [[ "${D4R_NO_CONFIG:-0}" != 1 ]]; then
eval "$D4R_CONFIG_EXPORTS"
fi
source "$ROOT/scripts/d4r_proton_env.sh"
# [Engine] ModelDir = auto: the model scripts/install_d4r_runtime.sh staged
export D4R_ENGINE_MODEL_DIR="${D4R_ENGINE_MODEL_DIR:-$D4R_RUNTIME_DIR/engine/k}"
PREFIX="$COMPAT_DIR/pfx"
UMU="$PREFIX/drive_c/windows/system32/umu"