Merge pull request #476 from jelly013/track-d/445-guest-shader-bridge

[M2-D] dxbc-accc: reach Windows guests as bx_shader_bridge.dll (#445)
This commit is contained in:
Ateş authored and GitHub committed 2026-11-26 23:40:30 +03:00
commit 52e786664d
4 files changed
+464

No files matched your search

+1
View File
@@ -10,6 +10,7 @@ Guest execution uses original, purpose-built test programs. Commercial Xbox game
remains unsupported.
### Added
- `dxbc-accc` now reaches Windows guests as `bx_shader_bridge.dll`, the x86-64 Windows build of the same crate (#445). It exports exactly the three `bx_shader_bridge.h` entry points and needs no MinGW runtime DLL. `shader-transpiler/crates/dxbc-accc/guest/build.sh` builds the DLL and checks its exports and imports. It also builds `check_bridge.exe`, a C guest program compiled against the frozen header, which sends a real DXC SM 6.6 blob and eight synthetic containers (DXIL with and without trailing bytes, `ACCC`/`FSPX` v1 and v0, dual, legacy `SHEX`, malformed) through the DLL loaded by full path. It checks classification (exact chunk sizes, root signature flag, header digest, declared size), prepare (only the declared container is copied), free, and prints a DXIL vs. `ACCC` count. The build script always uses `<repo>/target` and rustup's toolchain, and checks the whole Export Address Table and an import allowlist. On one Apple M5 Pro with CrossOver 26.3, in a prefix and a D3DMetal bottle, it reported `dxil=3 accc_only=2 dual=1 legacy=1 invalid=2` and passed. Synthetic containers only. Staging the DLL in `build-shims`, an `abi/defs` export list, and the `d3d12_xs` PSO hook (#466) are separate changes.
- `xgameruntime.dll!QueryApiImpl` now serves offline `XGameSave` and `XPackage` as COM `IUnknown` objects under BetterXen synthetic keys (#468, not yet closed). A title linked against Microsoft's `xgameruntime.lib` does not request these keys. Every synthetic key and COM slot order now lives in `shims/xgameruntime/synthetic_keys.h`, the single place to find or replace them. Signatures, structure layouts, `E_GS_*` values and container-name rules follow the public Microsoft Learn reference.
- **XGameSave:** provider init, containers, updates, blob writes, submit and read (both asynchronous), enumeration and the close calls. Saves are separated by SCID: each container is one file under `C:\bx_pls\<hash>\saves\<SCID hash>\`, replaced atomically on submit. A named mutex per SCID directory serializes access across threads and processes. Damaged files (truncated, trailing bytes, duplicate or NUL-bearing names) read as `ERROR_FILE_CORRUPT`, and enumeration skips them and unreadable files with a log line. Offline choices where the reference is silent (16 MiB including blob names, 256-byte blob names, no sync, retry after a failed submit) are listed in `game_save.cpp`.
- **XPackage:** the running executable's directory stands in for the installed package: a stable `BXPKG…` identifier, `XPackageKind::Game` for it, and `XPackageMount` / mount-path queries that return that directory.
@@ -0,0 +1,59 @@
# dxbc-accc
Classifies D3D shader containers (`DXBC` with `DXIL`, legacy `SHDR`/`SHEX`, and `ACCC` chunks carrying BetterXen's synthetic `FSPX` table) and exposes the C ABI in [`abi/include/bx_shader_bridge.h`](../../../abi/include/bx_shader_bridge.h): `bx_shader_inspect`, `bx_shader_prepare_for_d3dmetal` and `bx_shader_free_buffer`.
## Guest access: `bx_shader_bridge.dll` (#445)
The shims that see shader bytecode (`d3d12_xs.dll`) are x86-64 Windows PE32+ DLLs running inside CrossOver. They cannot load the arm64 macOS library that `cargo build` produces on the host. The bridge reaches guests as a **Windows build of this same crate**, with no second implementation:
- **Build:** `cargo build --target x86_64-pc-windows-gnu` turns the crate's `cdylib` into a PE32+ DLL, published as `bx_shader_bridge.dll`. Its Export Address Table holds exactly the three header functions as direct RVAs. It imports only Windows system DLLs that Wine provides, so no MinGW runtime DLL. `guest/build.sh` enforces both. The allowlist is `KERNEL32`, `ntdll`, `bcryptprimitives`, `USERENV`, `WS2_32` and the `api-ms-win-crt-*` / `api-ms-win-core-*` sets.
- **Callers:** a shim loads the bridge at run time with `GetProcAddress`, so a missing bridge is a logged condition, not a load failure of the shim.
- **Full path:** it must load the bridge by **full path from its own directory**. That is `GetModuleFileNameW` of the shim's own module (for example via `GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, ...)`), plus `bx_shader_bridge.dll`, loaded with `LoadLibraryExW(..., LOAD_WITH_ALTERED_SEARCH_PATH)`.
- **Why not the bare name:** a bare `LoadLibraryW(L"bx_shader_bridge.dll")` searches the guest EXE's directory first under the [standard search order](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order). It can therefore miss a bridge staged next to the shims, or pick up a different copy.
- **Staging:** the bridge is meant to be staged in the same directory as the shims that call it.
- **Why not a host tool over captured logs:** the M2 gate needs the decision at `Create*PipelineState` time inside the guest. That means passing DXIL through and rejecting ACCC-only containers. An offline host tool could count shaders but could not act on them.
### Build and check
```sh
rustup target add x86_64-pc-windows-gnu # once
shader-transpiler/crates/dxbc-accc/guest/build.sh
target/debug/bx-cli run --prefix /tmp/bx-shader --shims target/bx-shader-bridge \
--exe target/bx-shader-bridge/check_bridge.exe
```
`guest/build.sh` writes `bx_shader_bridge.dll` and `check_bridge.exe` to `target/bx-shader-bridge/`. It always builds into, and copies from, `<repo>/target`, whatever `CARGO_TARGET_DIR` says, so it cannot package a stale DLL. It resolves `cargo` and `rustc` through rustup, checks that this `rustc` has `std` for the Windows target, and prints which `rustc` it used. It fails if the Export Address Table holds anything other than the three entry points as direct RVAs (an ordinal-only entry or a forwarder fails it), or if any import is outside the allowlist above.
[`guest/check_bridge.c`](guest/check_bridge.c) is a C guest program built against the frozen header. It checks the struct layout with `_Static_assert`, loads the DLL by full path from its own directory, and sends nine containers through the DLL:
- a real DXC SM 6.6 compute blob (from [`d3dmetal-harness`](../d3dmetal-harness/README.md));
- synthetic DXIL pixel SM 6.0 with `RTS0`, once as is and once followed by 16 bytes past its declared size;
- `ACCC` with an `FSPX` v1 stage section, and `ACCC` with an `FSPX` v0 stub;
- `DXIL` + `ACCC`;
- legacy `SHEX`;
- two malformed blobs.
The program prints one `BXSHADER` line per container and a count line, and checks:
- **Classification:**
- kind, stage and shader model;
- the declared container size;
- the exact `DXIL` and `ACCC` chunk sizes and the root signature flag, each derived from the program's own chunk-table walk;
- the digest, compared with header bytes 4..20 for every container, the real one included.
- **Prepare:** exactly the declared container is copied, never the trailing bytes. ACCC-only and legacy are `BX_SHADER_ERR_UNSUPPORTED`. Malformed input is `BX_SHADER_ERR_INVALID_BLOB`, with output cleared.
- **Free and argument errors:** freeing returned buffers, and null arguments at the DLL boundary.
Recorded run (one Apple M5 Pro, macOS 27.2.1, CrossOver 26.3 `26.3.0.39832`, both a fresh prefix and a D3DMetal bottle):
```text
BXSHADER counts dxil=3 accc_only=2 dual=1 legacy=1 unknown=0 invalid=2
PASS: check_bridge (9 containers through bx_shader_bridge.dll)
```
This is a synthetic, single-host result. It is not a measurement of any title's shader corpus, and not Xbox hardware.
Each of these faults compiled into the DLL fails the check: `dxil_chunk_size` clamped to 1, `accc_chunk_size` multiplied, the digest zeroed, `has_root_signature` set to 7, and the DXIL and ACCC kind numbers swapped. The last one also fails when it is rebuilt with `CARGO_TARGET_DIR` set elsewhere.
### Not done here (other owners)
- **`abi/defs/bx_shader_bridge.def` (`[ABI]`):** the frozen export list for the DLL. Linking with it would also set the export directory's internal name, which is `dxbc_accc.dll` today. Wine loads by file name, so this does not affect loading.
- **Staging in `scripts/build-shims.sh` (Track A):** building the DLL there and verifying it with the other shims, so `build-shims/bin` ships it. That would also make CI build it; today nothing does, and `rustup target add x86_64-pc-windows-gnu` is a manual step.
- **Calling it from `d3d12_xs` at PSO creation, and JSONL telemetry (#466):** today `bx_shader_prepare_for_d3dmetal` reports an ACCC-only container on the guest's stderr (`bx-shader event=prepare status=unsupported …`), not through `bx_log_emit`.
+108
View File
@@ -0,0 +1,108 @@
#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Builds bx_shader_bridge.dll (this crate for x86_64-pc-windows-gnu) and the guest check
# check_bridge.exe into one directory (default: <repo>/target/bx-shader-bridge), then checks
# the DLL: its Export Address Table holds exactly the three bx_shader_bridge.h entry points as
# direct RVAs (no extra named or ordinal-only entries, no forwarders), and every imported DLL
# is a system DLL Wine provides. Run the check under CrossOver afterwards, for example:
# target/debug/bx-cli run --prefix /tmp/bx-shader --shims target/bx-shader-bridge \
# --exe target/bx-shader-bridge/check_bridge.exe
#
# Needs: rustup with the x86_64-pc-windows-gnu target, x86_64-w64-mingw32-gcc / -objdump
# (mingw-w64), and python3.
set -eu
crate=$(cd "$(dirname "$0")/.." && pwd)
root=$(cd "$crate/../../.." && pwd)
out=${1:-$root/target/bx-shader-bridge}
target=x86_64-pc-windows-gnu
cc=${BX_MINGW_CC:-x86_64-w64-mingw32-gcc}
objdump=${BX_MINGW_OBJDUMP:-x86_64-w64-mingw32-objdump}
# Always build here and copy from here, whatever CARGO_TARGET_DIR or build.target-dir say,
# so a stale DLL left elsewhere can never be packaged.
target_dir=$root/target
for tool in "$cc" "$objdump" python3; do
command -v "$tool" >/dev/null 2>&1 || { echo "build.sh: $tool not found" >&2; exit 1; }
done
# Use one toolchain for both the target check and the build. With rustup, resolve cargo and
# rustc through it (a cargo earlier on PATH, e.g. Homebrew's, has no Windows std).
if command -v rustup >/dev/null 2>&1; then
cargo=$(cd "$root" && rustup which cargo)
rustc=$(cd "$root" && rustup which rustc)
else
cargo=$(command -v cargo) || { echo "build.sh: cargo not found" >&2; exit 1; }
rustc=$(command -v rustc) || { echo "build.sh: rustc not found" >&2; exit 1; }
fi
libdir=$("$rustc" --print target-libdir --target "$target" 2>/dev/null || true)
if [ -z "$libdir" ] || ! ls "$libdir"/libstd-*.rlib >/dev/null 2>&1; then
echo "build.sh: $rustc ($("$rustc" --version)) has no std for $target" >&2
echo "build.sh: install it with: rustup target add $target" >&2
exit 1
fi
echo "build.sh: using $("$rustc" --version) at $rustc"
RUSTC=$rustc CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER=$cc \
"$cargo" build --locked --release -p dxbc-accc --target "$target" \
--target-dir "$target_dir" --manifest-path "$root/Cargo.toml"
mkdir -p "$out"
cp "$target_dir/$target/release/dxbc_accc.dll" "$out/bx_shader_bridge.dll"
dll=$out/bx_shader_bridge.dll
# Same reading of `objdump -p` as scripts/build-shims.sh: every populated Export Address Table
# slot must be a direct RVA owned by one of the expected names.
"$objdump" -p "$dll" | python3 -I -c '
import re, sys
expected = {"bx_shader_inspect", "bx_shader_prepare_for_d3dmetal", "bx_shader_free_buffer"}
# The Windows-provided system DLLs a Rust std cdylib imports; anything else is an error.
allowed = {"kernel32.dll", "ntdll.dll", "bcryptprimitives.dll", "userenv.dll", "ws2_32.dll"}
allowed_prefixes = ("api-ms-win-crt-", "api-ms-win-core-")
eat, names, imports, mode = {}, {}, [], None
for line in sys.stdin:
line = line.rstrip("\n")
m = re.match(r"^\s*DLL Name:\s*(\S+)", line)
if m:
imports.append(m.group(1))
if line.startswith("Export Address Table --"):
mode = "eat"; continue
if line.startswith("[Ordinal/Name Pointer] Table --"):
mode = "names"; continue
if not line.strip():
mode = None; continue
if mode == "eat":
m = re.match(r"^\s*\[\s*\d+\]\s*\+base\[\s*(\d+)\]\s+[0-9a-fA-F]+\s+(Export|Forwarder) RVA", line)
if m:
eat[int(m.group(1))] = m.group(2)
elif mode == "names":
m = re.match(r"^\s*\[\s*\d+\]\s*\+base\[\s*(\d+)\]\s+[0-9a-fA-F]+\s+(\S+)", line)
if m:
names[m.group(2)] = int(m.group(1))
errors = []
if set(names) != expected:
errors.append("named exports %s, want %s" % (sorted(names), sorted(expected)))
owned = set(names.values())
for ordinal, kind in sorted(eat.items()):
if ordinal not in owned:
errors.append("export @%d has no name (ordinal-only or stray entry)" % ordinal)
if kind != "Export":
errors.append("export @%d is a forwarder" % ordinal)
for name, ordinal in names.items():
if ordinal not in eat:
errors.append("%s @%d missing from the Export Address Table" % (name, ordinal))
for dll in imports:
low = dll.lower()
if low not in allowed and not low.startswith(allowed_prefixes):
errors.append("unexpected import %s (only Wine-provided system DLLs are allowed)" % dll)
if errors:
sys.exit("build.sh: bx_shader_bridge.dll: " + "; ".join(errors))
print("build.sh: exports %s; imports %s" % (" ".join(sorted(names)), " ".join(imports)))
'
"$cc" -std=c11 -O2 -Wall -Wextra -Werror -static \
-I"$root/abi/include" -I"$crate/../d3dmetal-harness/probe/shaders" \
-o "$out/check_bridge.exe" "$crate/guest/check_bridge.c"
echo "built $dll"
echo "built $out/check_bridge.exe"
@@ -0,0 +1,296 @@
/* SPDX-License-Identifier: GPL-3.0-or-later
*
* Guest-side check for bx_shader_bridge.dll, the x86-64 Windows build of this crate (#445).
*
* Runs as a PE32+ guest under CrossOver/Wine, loads the DLL by full path from its own directory,
* and drives the three entry points from abi/include/bx_shader_bridge.h with synthetic
* containers plus one real DXC SM 6.6 compute blob. It counts the classifications and checks
* them, and the prepare/free behaviour, against expectations written here from the DXBC
* layout and the frozen header, not from the Rust implementation.
*
* Output: "BXSHADER" lines with each blob's classification, one "BXSHADER counts" line, then
* PASS or FAIL. Exit code 0 on success, 1 on a failed check, 2 if the DLL cannot be loaded.
*/
#include <windows.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include "bx_shader_bridge.h"
#include "fill_cs66.h" /* g_fill_cs66: DXC 1.9.2609 output, see d3dmetal-harness */
/* The Rust side asserts the same layout; agreeing here proves both sides read one struct. */
_Static_assert(sizeof(bx_shader_kind_t) == 4, "C enum must be 4 bytes");
_Static_assert(sizeof(bx_shader_inspection_t) == 48, "bx_shader_inspection_t size");
_Static_assert(offsetof(bx_shader_inspection_t, container_size) == 16, "container_size offset");
_Static_assert(offsetof(bx_shader_inspection_t, digest_hash) == 32, "digest_hash offset");
typedef bx_shader_status_t (*inspect_fn)(const uint8_t*, size_t, bx_shader_inspection_t*);
typedef bx_shader_status_t (*prepare_fn)(const uint8_t*, size_t, uint8_t**, size_t*);
typedef void (*free_fn)(uint8_t*, size_t);
static int failures;
#define CHECK(expr) \
do { \
if (!(expr)) { \
fprintf(stderr, "FAIL: check_bridge line %d: %s\n", __LINE__, #expr); \
++failures; \
} \
} while (0)
/* --- Synthetic DXBC containers --- */
static void put32(uint8_t* at, uint32_t value) {
at[0] = (uint8_t)value;
at[1] = (uint8_t)(value >> 8);
at[2] = (uint8_t)(value >> 16);
at[3] = (uint8_t)(value >> 24);
}
typedef struct {
const char* fourcc;
const uint8_t* data;
uint32_t size;
} chunk_t;
/* DXBC layout: "DXBC", 16-byte digest, u32 version 1, u32 total size, u32 chunk count, then
* one u32 offset per chunk; each chunk is fourcc, u32 size, payload. */
static uint32_t build_dxbc(uint8_t* out, size_t cap, const chunk_t* chunks, uint32_t count) {
uint32_t at = 32 + 4 * count;
for (uint32_t i = 0; i < count; ++i) at += 8 + chunks[i].size;
if (at > cap) abort();
memcpy(out, "DXBC", 4);
memset(out + 4, 0x5A, 16);
put32(out + 20, 1);
put32(out + 24, at);
put32(out + 28, count);
uint32_t offset = 32 + 4 * count;
for (uint32_t i = 0; i < count; ++i) {
put32(out + 32 + 4 * i, offset);
memcpy(out + offset, chunks[i].fourcc, 4);
put32(out + offset + 4, chunks[i].size);
memcpy(out + offset + 8, chunks[i].data, chunks[i].size);
offset += 8 + chunks[i].size;
}
return at;
}
/* A 28-byte DxilProgramHeader: program version word (kind << 16 | major << 4 | minor), size
* in dwords (7), "DXIL", DXIL version 1.0, bitcode offset 16, bitcode size 4, 4 bitcode bytes. */
static void dxil_program(uint8_t out[28], uint32_t kind, uint32_t major, uint32_t minor) {
memset(out, 0, 28);
put32(out, kind << 16 | major << 4 | minor);
put32(out + 4, 7);
memcpy(out + 8, "DXIL", 4);
put32(out + 12, 0x0100);
put32(out + 16, 16);
put32(out + 20, 4);
put32(out + 24, 0xDEC011D1u);
}
/* Size of the first chunk named fourcc in a well-formed container, 0 if absent. Written from
* the DXBC layout, independently of the Rust parser. */
static uint32_t get32(const uint8_t* at) {
return (uint32_t)at[0] | (uint32_t)at[1] << 8 | (uint32_t)at[2] << 16 | (uint32_t)at[3] << 24;
}
static int chunk(const uint8_t* bytes, size_t len, const char* fourcc, uint32_t* size) {
if (len < 32 || memcmp(bytes, "DXBC", 4) != 0) return 0;
const uint32_t count = get32(bytes + 28);
for (uint32_t i = 0; i < count && 36 + 4 * (size_t)i <= len; ++i) {
const uint32_t offset = get32(bytes + 32 + 4 * i);
if (offset + 8 <= len && memcmp(bytes + offset, fourcc, 4) == 0) {
*size = get32(bytes + offset + 4);
return 1;
}
}
return 0;
}
/* --- Cases --- */
typedef struct {
const char* name;
const uint8_t* bytes;
size_t len;
bx_shader_status_t inspect_status;
bx_shader_kind_t kind;
bx_shader_stage_t stage;
uint32_t sm_major, sm_minor;
uint32_t container_size; /* the container's declared size, which may be less than len */
bx_shader_status_t prepare_status;
} case_t;
static const char* kind_name(bx_shader_kind_t kind) {
switch (kind) {
case BX_SHADER_DXBC_DXIL: return "dxil";
case BX_SHADER_DXBC_LEGACY: return "legacy";
case BX_SHADER_DXBC_ACCC: return "accc_only";
case BX_SHADER_DXBC_DUAL: return "dual";
default: return "unknown";
}
}
int main(void) {
/* By full path beside this EXE, as a caller should (see the crate README). */
wchar_t path[MAX_PATH];
const DWORD path_len = GetModuleFileNameW(NULL, path, MAX_PATH);
wchar_t* slash = path_len && path_len < MAX_PATH ? wcsrchr(path, L'\\') : NULL;
if (!slash || (size_t)(slash + 1 - path) + 21 > MAX_PATH) {
fprintf(stderr, "FAIL: check_bridge: executable path too long\n");
return 2;
}
wcscpy(slash + 1, L"bx_shader_bridge.dll");
HMODULE dll = LoadLibraryExW(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
if (!dll) {
fprintf(stderr, "FAIL: check_bridge: bx_shader_bridge.dll not loaded (error %lu)\n", GetLastError());
return 2;
}
inspect_fn inspect = (inspect_fn)(void (*)(void))GetProcAddress(dll, "bx_shader_inspect");
prepare_fn prepare = (prepare_fn)(void (*)(void))GetProcAddress(dll, "bx_shader_prepare_for_d3dmetal");
free_fn release = (free_fn)(void (*)(void))GetProcAddress(dll, "bx_shader_free_buffer");
if (!inspect || !prepare || !release) {
fprintf(stderr, "FAIL: check_bridge: missing export\n");
return 2;
}
uint8_t dxil_cs66[28], dxil_ps60[28];
dxil_program(dxil_cs66, 5, 6, 6); /* DXIL program kind 5 = compute */
dxil_program(dxil_ps60, 0, 6, 0); /* kind 0 = pixel */
const uint8_t rts0[4] = {1, 2, 3, 4};
/* FSPX v1 (BetterXen's synthetic layout): header "FSPX", version 1, 1 section, flags 0;
* descriptor {id 1 (stage config), offset 28, size 12}; body {stage 6 compute, SM 6.0}. */
uint8_t fspx_v1[40] = {0};
memcpy(fspx_v1, "FSPX", 4);
put32(fspx_v1 + 4, 1);
put32(fspx_v1 + 8, 1);
put32(fspx_v1 + 16, 1);
put32(fspx_v1 + 20, 28);
put32(fspx_v1 + 24, 12);
put32(fspx_v1 + 28, BX_STAGE_COMPUTE);
put32(fspx_v1 + 32, 6);
put32(fspx_v1 + 36, 0);
/* FSPX version 0: magic-only stub, no sections. */
uint8_t fspx_v0[16] = {'F', 'S', 'P', 'X'};
/* SM 5.0 compute SHEX token (program type 5 << 16 | 5 << 4 | 0) and a length dword. */
uint8_t shex[8];
put32(shex, 5u << 16 | 5u << 4);
put32(shex + 4, 2);
static uint8_t ps60[256], accc_v1[256], accc_v0[256], dual[256], legacy[256];
const chunk_t ps60_chunks[] = {{"RTS0", rts0, 4}, {"DXIL", dxil_ps60, 28}};
const chunk_t accc_v1_chunks[] = {{"ACCC", fspx_v1, sizeof(fspx_v1)}};
const chunk_t accc_v0_chunks[] = {{"ACCC", fspx_v0, sizeof(fspx_v0)}};
const chunk_t dual_chunks[] = {{"DXIL", dxil_cs66, 28}, {"ACCC", fspx_v0, sizeof(fspx_v0)}};
const chunk_t legacy_chunks[] = {{"SHEX", shex, sizeof(shex)}};
const uint32_t ps60_len = build_dxbc(ps60, sizeof(ps60), ps60_chunks, 2);
const uint32_t accc_v1_len = build_dxbc(accc_v1, sizeof(accc_v1), accc_v1_chunks, 1);
const uint32_t accc_v0_len = build_dxbc(accc_v0, sizeof(accc_v0), accc_v0_chunks, 1);
const uint32_t dual_len = build_dxbc(dual, sizeof(dual), dual_chunks, 2);
const uint32_t legacy_len = build_dxbc(legacy, sizeof(legacy), legacy_chunks, 1);
/* The pixel shader again, followed by 16 bytes that are not part of the container. */
static uint8_t padded[256 + 16];
memcpy(padded, ps60, ps60_len);
memset(padded + ps60_len, 0xEE, 16);
static const uint8_t truncated[20] = {'D', 'X', 'B', 'C'};
static const uint8_t not_dxbc[64] = {'X', 'X', 'X', 'X'};
const case_t cases[] = {
{"dxc_cs_6_6", g_fill_cs66, sizeof(g_fill_cs66), BX_SHADER_OK, BX_SHADER_DXBC_DXIL, BX_STAGE_COMPUTE, 6, 6,
sizeof(g_fill_cs66), BX_SHADER_OK},
{"synthetic_ps_6_0", ps60, ps60_len, BX_SHADER_OK, BX_SHADER_DXBC_DXIL, BX_STAGE_PIXEL, 6, 0, ps60_len,
BX_SHADER_OK},
{"trailing_bytes", padded, ps60_len + 16, BX_SHADER_OK, BX_SHADER_DXBC_DXIL, BX_STAGE_PIXEL, 6, 0, ps60_len,
BX_SHADER_OK},
{"accc_fspx_v1", accc_v1, accc_v1_len, BX_SHADER_OK, BX_SHADER_DXBC_ACCC, BX_STAGE_COMPUTE, 6, 0, accc_v1_len,
BX_SHADER_ERR_UNSUPPORTED},
{"accc_fspx_v0", accc_v0, accc_v0_len, BX_SHADER_OK, BX_SHADER_DXBC_ACCC, BX_STAGE_UNKNOWN, 0, 0, accc_v0_len,
BX_SHADER_ERR_UNSUPPORTED},
{"dual_dxil_accc", dual, dual_len, BX_SHADER_OK, BX_SHADER_DXBC_DUAL, BX_STAGE_COMPUTE, 6, 6, dual_len,
BX_SHADER_OK},
{"legacy_shex_5_0", legacy, legacy_len, BX_SHADER_OK, BX_SHADER_DXBC_LEGACY, BX_STAGE_COMPUTE, 5, 0,
legacy_len, BX_SHADER_ERR_UNSUPPORTED},
{"truncated", truncated, sizeof(truncated), BX_SHADER_ERR_INVALID_BLOB, BX_SHADER_UNKNOWN, BX_STAGE_UNKNOWN,
0, 0, 0, BX_SHADER_ERR_INVALID_BLOB},
{"not_dxbc", not_dxbc, sizeof(not_dxbc), BX_SHADER_ERR_INVALID_BLOB, BX_SHADER_UNKNOWN, BX_STAGE_UNKNOWN, 0,
0, 0, BX_SHADER_ERR_INVALID_BLOB},
};
const size_t count = sizeof(cases) / sizeof(cases[0]);
unsigned tally[5] = {0};
unsigned invalid = 0;
for (size_t i = 0; i < count; ++i) {
const case_t* c = &cases[i];
bx_shader_inspection_t info;
memset(&info, 0xEE, sizeof(info));
const bx_shader_status_t status = inspect(c->bytes, c->len, &info);
printf("BXSHADER %s status=%d kind=%s stage=%d sm=%u.%u container_size=%u dxil=%u accc=%u rts0=%u\n",
c->name, (int)status, kind_name(info.kind), (int)info.stage, info.shader_model_major,
info.shader_model_minor, info.container_size, info.dxil_chunk_size, info.accc_chunk_size,
info.has_root_signature);
CHECK(status == c->inspect_status);
if (status == BX_SHADER_OK) {
if ((unsigned)info.kind < 5) ++tally[info.kind];
CHECK(info.kind == c->kind);
CHECK(info.stage == c->stage);
CHECK(info.shader_model_major == c->sm_major && info.shader_model_minor == c->sm_minor);
CHECK(info.container_size == c->container_size);
/* Exact chunk sizes and the root signature flag, from this file's own chunk walk. */
uint32_t want_dxil = 0, want_accc = 0, rts0_size = 0;
chunk(c->bytes, c->len, "DXIL", &want_dxil);
chunk(c->bytes, c->len, "ACCC", &want_accc);
const uint32_t want_rts0 = (uint32_t)chunk(c->bytes, c->len, "RTS0", &rts0_size);
CHECK(info.dxil_chunk_size == want_dxil);
CHECK(info.accc_chunk_size == want_accc);
CHECK(info.has_root_signature == want_rts0);
CHECK((want_dxil != 0) == (c->kind == BX_SHADER_DXBC_DXIL || c->kind == BX_SHADER_DXBC_DUAL));
CHECK((want_accc != 0) == (c->kind == BX_SHADER_DXBC_ACCC || c->kind == BX_SHADER_DXBC_DUAL));
/* The digest is the container header's bytes 4..20, for every case. */
CHECK(memcmp(info.digest_hash, c->bytes + 4, 16) == 0);
} else {
++invalid;
const bx_shader_inspection_t zero = {0};
CHECK(memcmp(&info, &zero, sizeof(info)) == 0); /* cleared on failure */
}
uint8_t* copy = (uint8_t*)(uintptr_t)1;
size_t copy_len = 1;
const bx_shader_status_t prepared = prepare(c->bytes, c->len, &copy, &copy_len);
CHECK(prepared == c->prepare_status);
if (prepared == BX_SHADER_OK) {
/* Only the declared container is copied, never bytes past it. */
CHECK(copy && copy_len == c->container_size && memcmp(copy, c->bytes, c->container_size) == 0);
release(copy, copy_len);
} else {
CHECK(copy == NULL && copy_len == 0);
}
}
/* Argument checks at the DLL boundary. */
bx_shader_inspection_t info;
CHECK(inspect(g_fill_cs66, sizeof(g_fill_cs66), NULL) == BX_SHADER_ERR_INVALID_BLOB);
CHECK(inspect(NULL, 16, &info) == BX_SHADER_ERR_INVALID_BLOB);
uint8_t* copy = NULL;
CHECK(prepare(g_fill_cs66, sizeof(g_fill_cs66), NULL, NULL) == BX_SHADER_ERR_INVALID_BLOB);
CHECK(prepare(NULL, 16, &copy, &(size_t){0}) == BX_SHADER_ERR_INVALID_BLOB && copy == NULL);
release(NULL, 0);
printf("BXSHADER counts dxil=%u accc_only=%u dual=%u legacy=%u unknown=%u invalid=%u\n",
tally[BX_SHADER_DXBC_DXIL], tally[BX_SHADER_DXBC_ACCC], tally[BX_SHADER_DXBC_DUAL],
tally[BX_SHADER_DXBC_LEGACY], tally[BX_SHADER_UNKNOWN], invalid);
CHECK(tally[BX_SHADER_DXBC_DXIL] == 3 && tally[BX_SHADER_DXBC_ACCC] == 2 && tally[BX_SHADER_DXBC_DUAL] == 1);
CHECK(tally[BX_SHADER_DXBC_LEGACY] == 1 && tally[BX_SHADER_UNKNOWN] == 0 && invalid == 2);
if (failures) {
fprintf(stderr, "FAIL: check_bridge (%d failed checks)\n", failures);
return 1;
}
printf("PASS: check_bridge (%u containers through bx_shader_bridge.dll)\n", (unsigned)count);
return 0;
}