diff --git a/data/shaders/d3d12/README.md b/data/shaders/d3d12/README.md index da35ffef..d2c7dfb5 100644 --- a/data/shaders/d3d12/README.md +++ b/data/shaders/d3d12/README.md @@ -51,11 +51,53 @@ file and rewrites the matching `.inc` under `src/common/d3d12/embedded_dxbc/`. Both the `.hlsl` and the regenerated `.inc` must be committed together so the two stay in sync. -`fxc.exe` ships with the Windows 10 SDK and is **not** invoked by the core -build. On Linux, run the script under Wine - it will look for `fxc.exe` in -the standard SDK install path under `$WINEPREFIX/drive_c/Program Files (x86)/Windows Kits/10/bin//x64/fxc.exe`, -or you can pass `--fxc PATH` explicitly. The `--wine PATH` flag lets you -point at a specific wine binary if `wine` isn't on `PATH`. +### Microsoft `fxc.exe` is required + +The regen script will refuse to write `.inc` files produced by any compiler +other than Microsoft's `fxc.exe`. The check parses the DXBC `RDEF` chunk and +matches the creator string against a Microsoft-prefix allowlist. + +The reason is empirical: Wine ships its own `d3dcompiler_47.dll` +implementation (vkd3d-shader), and that compiler's output, while a valid +DXBC container, doesn't pass the Microsoft D3D12 runtime's +`CreateGraphicsPipelineState` validation for non-trivial shaders. The +failure mode is `E_FAIL` (0x80004005) at PSO-creation time with no +useful diagnostic from `Log_ErrorPrintf`. Commit cd971cd shipped a +vkd3d-shader-compiled `vram_copy_ps.inc` and broke the entire D3D12 +backend on launch; da20f5b reverts it. The script's creator-string +validator exists to prevent that mistake from recurring. + +### Installing Microsoft `fxc.exe` + +**Windows**: Install the Windows 10 or 11 SDK from Microsoft. `fxc.exe` +ends up at +`C:\Program Files (x86)\Windows Kits\10\bin\\x64\fxc.exe` (or +the same path under `\Program Files\` if the 64-bit SDK is installed). +The regen script auto-detects it there. + +**Linux**: Microsoft's `fxc.exe` is a Windows x86 PE binary and runs +fine under Wine - the trick is just to get the SDK installed inside a +Wine prefix. Two options: + +* Run the Windows SDK installer (`winsdksetup.exe` from Microsoft) under + `wine` and select the Windows SDK Signing Tools / SDK component that + ships `fxc.exe`. The default install puts it at + `/drive_c/Program Files (x86)/Windows Kits/10/bin//x64/fxc.exe`, + which the regen script picks up automatically. +* Or extract `fxc.exe` + `d3dcompiler_47.dll` directly from the SDK MSI + using `msiextract` from `msitools`, drop both in any directory, and + pass `--fxc PATH/TO/fxc.exe` to the regen script. Both files must + live in the same directory so Wine's loader finds Microsoft's + `d3dcompiler_47.dll` before falling back to its bundled + vkd3d-shader version. + +The script searches for `fxc.exe` in: `$WINEPREFIX` (if set), `~/.wine`, +and any subdirectory under `~/.local/share/wineprefixes/` that has a +`drive_c` tree. Set `WINEPREFIX` to your prefix path if it lives +elsewhere. + +The `--wine PATH` flag lets you point at a specific wine binary if +`wine` isn't on `$PATH`. ## Filename convention diff --git a/tools/regen_d3d12_dxbc.py b/tools/regen_d3d12_dxbc.py index a3b45681..109a3ac6 100755 --- a/tools/regen_d3d12_dxbc.py +++ b/tools/regen_d3d12_dxbc.py @@ -60,9 +60,21 @@ TEMPLATE_VARIANTS = {} def find_fxc(explicit_fxc, explicit_wine): - # On Windows, look for fxc.exe in the Windows 10 SDK install location - # first, then PATH. On Linux, require Wine + explicit --fxc pointing at - # the Windows fxc.exe install. + # On Windows, look for fxc.exe in the Windows 10 / 11 SDK install + # location first, then PATH. On Linux, look in any Wine prefix + # we can find that has the SDK installed; only fall back to the + # --fxc override when the user explicitly points us at one. + # + # Microsoft's actual fxc.exe is required - Wine's bundled + # d3dcompiler_47.dll (which is the vkd3d-shader reimplementation) + # produces DXBC that doesn't pass Microsoft D3D12 PSO validation + # for non-trivial shaders. cd971cd / da20f5b chronicles a + # vram_copy_ps pre-bake attempt that failed with + # CreateGraphicsPipelineState E_FAIL because the .inc was built + # with vkd3d-shader. The check_microsoft_creator() validator + # below rejects vkd3d-compiled output post-hoc, but it's better + # to find Microsoft's fxc.exe up-front and skip the wasted + # compile time entirely. See data/shaders/d3d12/README.md. if explicit_fxc: fxc = Path(explicit_fxc) if not fxc.exists(): @@ -77,17 +89,32 @@ def find_fxc(explicit_fxc, explicit_wine): if path: return [path] - # Try the common Windows 10 SDK install location even when not on - # Windows (in case the user has the SDK mounted under Wine prefix). + # SDK auto-detection. The Windows 10 / 11 SDK installs fxc.exe at + # //x64/fxc.exe. Try the standard install + # locations on Windows AND under every plausible Wine prefix on + # Linux. The /Program Files/ and /Program Files (x86)/ split + # depends on whether the SDK was installed as 32-bit or 64-bit; + # check both. Wine prefix discovery covers $WINEPREFIX (if set), + # the default ~/.wine, and any prefixes under ~/.local/share/ + # wineprefixes/ that PlayOnLinux and lutris-style installs use. sdk_roots = [ Path("C:/Program Files (x86)/Windows Kits/10/bin"), Path("C:/Program Files/Windows Kits/10/bin"), ] - # Add Wine-prefix style path if we have one. - wine_prefix = os.environ.get("WINEPREFIX") - if wine_prefix: - sdk_roots.append(Path(wine_prefix) / "drive_c" / "Program Files (x86)" / "Windows Kits" / "10" / "bin") - sdk_roots.append(Path(wine_prefix) / "drive_c" / "Program Files" / "Windows Kits" / "10" / "bin") + wine_prefixes = [] + if "WINEPREFIX" in os.environ: + wine_prefixes.append(Path(os.environ["WINEPREFIX"])) + default_prefix = Path.home() / ".wine" + if default_prefix.exists() and default_prefix not in wine_prefixes: + wine_prefixes.append(default_prefix) + extra_prefix_dir = Path.home() / ".local" / "share" / "wineprefixes" + if extra_prefix_dir.exists(): + for child in extra_prefix_dir.iterdir(): + if child.is_dir() and (child / "drive_c").exists(): + wine_prefixes.append(child) + for prefix in wine_prefixes: + sdk_roots.append(prefix / "drive_c" / "Program Files (x86)" / "Windows Kits" / "10" / "bin") + sdk_roots.append(prefix / "drive_c" / "Program Files" / "Windows Kits" / "10" / "bin") for root in sdk_roots: if not root.exists(): @@ -102,13 +129,101 @@ def find_fxc(explicit_fxc, explicit_wine): [explicit_wine or "wine", str(candidate)]) sys.stderr.write( - "error: fxc.exe not found. Install the Windows 10 SDK (or a newer\n" - " Windows SDK that still ships fxc.exe), or pass --fxc PATH\n" - " pointing at a working fxc.exe binary. On Linux pass --wine\n" - " PATH if `wine` isn't on PATH.\n") + "error: fxc.exe not found. The Microsoft Windows 10/11 SDK ships\n" + " it under /bin//x64/fxc.exe. See\n" + " data/shaders/d3d12/README.md for installation steps,\n" + " including how to set up the SDK under a Wine prefix\n" + " for Linux contributors. If you have fxc.exe elsewhere,\n" + " pass --fxc PATH. On Linux pass --wine PATH if `wine`\n" + " isn't on PATH.\n" + "\n" + " Wine's bundled d3dcompiler_47.dll (vkd3d-shader\n" + " reimplementation) is NOT a substitute - its DXBC output\n" + " fails Microsoft D3D12 PSO validation. See da20f5b for\n" + " a worked example.\n") sys.exit(1) +def extract_rdef_creator(data): + # Inspect the DXBC RDEF chunk and pull out the compiler-creator + # string. The DXBC container is: + # [0:4] magic "DXBC" + # [4:20] 16-byte content hash + # [20:24] container version + # [24:28] total size + # [28:32] chunk count + # [32:..] uint32 offsets, one per chunk + # Each chunk header is 8 bytes (4-byte magic + 4-byte size) and + # is followed by chunk-type-specific payload. The RDEF chunk + # body lays out as: + # [0:4] constant_buffer_count + # [4:8] constant_buffer_offset + # [8:12] resource_binding_count + # [12:16] resource_binding_offset + # [16:20] shader_version (program_type | major | minor) + # [20:24] compiler_flags + # [24:28] creator_offset (relative to body_start) + # Offsets in the RDEF body are relative to the START of the + # chunk body, NOT the start of the file - that's the same + # convention DXBC uses internally for SGN tables. + chunk_count_bytes = data[28:32] + if len(chunk_count_bytes) != 4: + return None + chunk_count = struct.unpack(" len(data): + continue + magic = data[off:off + 4] + if magic != b"RDEF": + continue + body_start = off + 8 + if body_start + 28 > len(data): + return None + creator_offset = struct.unpack(" "vram_read_ps"; only [A-Za-z0-9_] survive. name = re.sub(r"[^A-Za-z0-9_]", "_", stem) @@ -152,6 +267,10 @@ def compile_one(fxc_cmd, hlsl_path, target, defines=None): f"error: {hlsl_path.name} produced output with bad magic " f"(0x{magic:08x}, expected 0x43425844)\n") sys.exit(1) + # Reject non-Microsoft compilers. The check parses the RDEF chunk + # for the creator string; Wine's bundled d3dcompiler_47.dll + # (vkd3d-shader) gets refused here. + check_microsoft_creator(data, hlsl_path) return data