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 docs/experiments/native-fixed-mesh57.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ Set `X2W_SHADER_CACHE` to the request cache and `X2W_MESH57_CAPTURE_STATE=1` to

Native preparation reads a separately reviewed `<variant>.mesh57-state` profile from the cache. The stand-in lists the cache's top-level file names once per process, at the first profile lookup (`src/startup_stubs/cache_index.h`), and opens a profile only when that listing has its name. A profile written while the process runs is read by the next process. Its versioned Windows x64 header is defined by `Mesh57Profile` in `src/startup_stubs/mesh57_profile.h`. The header supplies the MS entry layout and desktop fixed-function state. The file ends with the normalized descriptor, register blocks, serialized root and both guest programs. The stand-in compares every byte before building a PSO. The fixture authors this profile from its own known inputs. A captured register block alone does not establish the desktop rasterizer state or MS entry layout.

The `.mesh57-state` name uses a stable profile key. `stable_mesh57_profile_descriptor` first recognizes the same 0x300 layout as `normalize_mesh57_descriptor`: the `0x04010000` markers at `0x11c` and `0x27c`, the zero and non-zero length words, and the ten required record pointers near the section pointer at `0x38`. Any other 0x300 descriptor is returned byte-for-byte, so its unclassified bytes still distinguish profiles. In a recognized layout the optional pointers `0x198`, `0x1b8`, `0x1d8` and `0x2f8` are made relative when near the section. The four 32-bit values at `0x188`, `0x1a8`, `0x1c8` and `0x2e8` keep only their distance from the first valid value, so a common base shift keeps the key and a changed spacing does not; small values such as `0x40`/`0x80` remain metadata. Finally the qwords at `0x38`, `0x78`, `0x120`, `0x128`, `0x160`, `0x168`, `0x178`, `0x198`, `0x1b8`, `0x1d8`, `0x280`, `0x288`, `0x2c0`, `0x2c8`, `0x2d8` and `0x2f8` and the dwords at `0x110`, `0x270` and `0x1f4` are cleared; `0x188`/`0x1a8`/`0x1c8`/`0x2e8` are never cleared. The `.s57` archive keeps its stream-key variant name and the full normalized descriptor; the profile name, lookup and byte comparison use this stable form. `normalize_mesh57_descriptor` and its stream-key test are unchanged. The fixture covers a common second-base shift, a spacing change, unclassified bytes under an unknown marker, metadata in a second-base slot and idempotence.

To serialize reviewed desktop and entry state, run
`python -B tools/startup/mesh57_profile.py ARCHIVE REVIEWED.json OUTPUT_DIR`.
Keep the three request companions beside the archive. The
Expand Down
1 change: 1 addition & 0 deletions docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -1865,6 +1865,7 @@ ctest --preset dev --no-tests=error -R '^startup_hook_(window|overlap)$' -V
| `X2W_D3D12_RELEASE_RETIRED=1` | opt-in, effective with `X2W_D3D12_RENDER=1` only (the list handlers that record holds exist only there): frees the desktop object of a released committed 2D texture of one narrow class once no recorded list, submission or release-time fence refers to it ([details](#d3d12-resource-budget)). Unset, nothing changes |
| `X2W_D3D12_INLINE_DESCRIPTORS=1` | opt-in: leaves byte +0x18 of the guest device at 0, so the title writes constant buffer views and copies CBV/SRV/UAV descriptors itself instead of calling device slots 17 and 24 (sampler copies still arrive through slot 24); with `X2W_D3D12_RELEASE_RETIRED=1`, a retired resource is then freed once its release fences pass ([details](#d3d12-inline-descriptors)). Unset, nothing changes |
| `X2W_D3D12_NO_VISIBLE_HEAP_MIRROR=1` | negative control only: title view and sampler heaps created shader-visible get no CPU-only mirror. By default every view, sampler and copy the stand-in writes into a shader-visible range goes to the mirror first and is copied from there, and copies and `ClearUnorderedAccessView*` read a visible descriptor from the mirror, because desktop D3D12 reads those only from CPU-only heaps (debug layer message 654; a driver fault on a GTX 1060, #3558). The switch restores the direct read |
| `X2W_MESH57_CAPTURE_STATE=1` | opt-in: archives each slot 57 fixed mesh variant as `req_<variant>.s57`; a published `.mesh57-state` uses the stable profile key for a recognized 0x300 layout (optional record pointers relative, the common `+0x188`-base values shifted, remaining relocations cleared; an unknown layout keeps every byte), so its name can differ from the `.s57` stream-key variant |
| `X2W_D3D12_RELEASE_RETIRED_VIEWS=1` | legacy diagnostic. Logs that clearing live slots is disabled and enables the existing retirement breakdown when base retirement and budgets are on ([details](#d3d12-resource-budget)) |
| `X2W_D3D12_AGILITY=<dir>` | opt-in: the stand-ins select the Agility SDK `D3D12Core.dll` in `<dir>` before the first D3D12 device of the process (Windows Developer Mode only; `<dir>` on the executable's drive). Unset, the in-box runtime stays and nothing is read ([details](#d3d12-agility-sdk-runtime)) |
| `X2W_D3D12_RETIRE_REASONS=1` | diagnostic only, effective with `X2W_D3D12_RELEASE_RETIRED=1` and `X2W_D3D12_BUDGET=1`: adds one `x2w-retire held:` line to the heartbeat that counts final-released resources whose desktop object is still alive, by reason ([details](#d3d12-resource-budget)). Unset, nothing is recorded |
Expand Down
62 changes: 62 additions & 0 deletions src/startup_stubs/mesh57_profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,68 @@ inline std::vector<unsigned char> normalize_mesh57_descriptor(std::vector<unsign
std::memset(desc.data() + offset, 0, sizeof(allocation));
return desc;
}

// The profile key must survive a different process allocation of the same fixed pipeline. It is
// only defined for the extended layout normalize_mesh57_descriptor recognizes; any other 0x300
// descriptor is returned byte-for-byte. Recognized optional record pointers are made relative,
// the second base at +0x188/+0x1a8/+0x1c8/+0x2e8 keeps only its spacing, and the remaining
// pointer/relocation fields are cleared.
inline std::vector<unsigned char> stable_mesh57_profile_descriptor(std::vector<unsigned char> desc)
{
if (desc.size() != 0x300)
return desc;
const auto word = [&](std::size_t offset) {
std::uint32_t value;
std::memcpy(&value, desc.data() + offset, sizeof(value));
return value;
};
const auto address = [&](std::size_t offset) {
std::uint64_t value;
std::memcpy(&value, desc.data() + offset, sizeof(value));
return value;
};
const auto put = [&](std::size_t offset, std::uint64_t value) {
std::memcpy(desc.data() + offset, &value, sizeof(value));
};
const auto is_pointer = [](std::uint64_t value) {
return value >= 0x10000 && value < 0x800000000000ull;
};
const auto section = address(0x38);
if (address(0x48) || address(0x50) || !address(0x30) || !address(0x70) ||
word(0x11c) != 0x04010000 || word(0x27c) != 0x04010000 || !is_pointer(section))
return desc;
const auto near_section = [&](std::uint64_t value) {
return is_pointer(value) && (value >= section ? value - section : section - value) <= INT32_MAX;
};
constexpr std::size_t required_pointers[]{
0x38, 0x78, 0x120, 0x128, 0x168, 0x178, 0x280, 0x288, 0x2c8, 0x2d8
};
for (const auto offset : required_pointers)
if (!near_section(address(offset)))
return desc;
constexpr std::size_t optional_pointers[]{0x198, 0x1b8, 0x1d8, 0x2f8};
for (const auto offset : optional_pointers)
if (near_section(address(offset)))
put(offset, address(offset) - section);
std::uint64_t second_base = 0;
for (const auto offset : {0x188, 0x1a8, 0x1c8, 0x2e8}) {
const auto value = address(offset);
if (value < 0x10000 || value >> 32)
continue;
if (!second_base) second_base = value;
put(offset, value - second_base);
}
constexpr std::size_t qwords[]{
0x00, 0x08, 0x18, 0x28, 0x38, 0x68, 0x78, 0x120, 0x128, 0x160,
0x168, 0x178, 0x198, 0x1b8, 0x1d8, 0x280, 0x288, 0x2c0, 0x2c8, 0x2d8, 0x2f8
};
constexpr std::size_t dwords[]{0x110, 0x270, 0x1f4};
for (const auto offset : qwords)
std::memset(desc.data() + offset, 0, sizeof(std::uint64_t));
for (const auto offset : dwords)
std::memset(desc.data() + offset, 0, sizeof(std::uint32_t));
return desc;
}
// A reviewed fixed-state/entry record. The header is followed by the exact normalized fixed
// descriptor, the two register blocks, creation root blob, guest MS and guest PS, in this order.
// Descriptor normalization removes the relocations recognized in each fixed layout.
Expand Down
14 changes: 12 additions & 2 deletions src/startup_stubs/native_mesh57.inl
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,8 @@ void prepare_native_mesh57(Wrapper* pipeline)
constexpr char revision[] = "x2w-startup-mesh-57-4";
const u64 key = fnv_part(root_blob, fnv_part(ps_bytes, fnv_part(ms_bytes, fnv_bytes(revision, sizeof(revision) - 1))));
const auto normalized = x2w_startup::normalize_mesh57_descriptor(info->desc);
const auto variant = fnv_part(info->state_b, fnv_part(info->state_a, fnv_part(normalized, key)));
const auto stable = x2w_startup::stable_mesh57_profile_descriptor(normalized);
const auto variant = fnv_part(info->state_b, fnv_part(info->state_a, fnv_part(stable, key)));
if (!cache_path(path, L"", variant, L".mesh57-state"))
return note_mesh57_outcome(Mesh57Outcome::profile_absent);
InterlockedIncrement(&mesh57_profile_lookups);
Expand Down Expand Up @@ -496,8 +497,17 @@ void prepare_native_mesh57(Wrapper* pipeline)
if (!size)
return;
std::string copy(size, 0);
if (!file.read(copy.data(), size) || std::memcmp(copy.data(), bytes, size))
if (!file.read(copy.data(), size))
throw profile_error(Mesh57Outcome::profile_input_content, std::string("profile input bytes differ: ") + name + " content");
if (part == 0) {
std::vector<unsigned char> stored(copy.begin(), copy.end());
if (x2w_startup::stable_mesh57_profile_descriptor(stored) != stable)
throw profile_error(Mesh57Outcome::profile_input_content,
std::string("profile input bytes differ: ") + name + " content");
} else if (std::memcmp(copy.data(), bytes, size)) {
throw profile_error(Mesh57Outcome::profile_input_content,
std::string("profile input bytes differ: ") + name + " content");
}
};
match(0, "normalized descriptor", normalized.data(), normalized.size());
match(1, "state_a", info->state_a.data(), info->state_a.size());
Expand Down
59 changes: 59 additions & 0 deletions tests/mesh57_profile_draft_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ def check_census(folder):
def main():
compiler = Path(sys.argv[1]) if len(sys.argv) > 1 else None
dxc = Path(sys.argv[2]) if len(sys.argv) > 2 else None
stable_key_checks()
check_classification()
check_depth_decode()
check_blend_words()
Expand All @@ -431,5 +432,63 @@ def main():
print("slot 57 profile draft, review, depth-only and refusal checks passed")


def stable_key_checks():
def build(section, allocation):
desc = bytearray(0x300)
offsets = (0x38, 0x78, 0x120, 0x128, 0x168, 0x178, 0x198, 0x1d8,
0x280, 0x288, 0x2c8, 0x2d8, 0x2f8)
for index, offset in enumerate(offsets):
struct.pack_into("<Q", desc, offset, section + index * 16)
struct.pack_into("<Q", desc, 0x78, section - 16)
struct.pack_into("<I", desc, 0x110, (section + 8 * 16) & 0xFFFFFFFF)
struct.pack_into("<I", desc, 0x270, (section + 16) & 0xFFFFFFFF)
struct.pack_into("<Q", desc, 0x30, 512)
struct.pack_into("<Q", desc, 0x70, 1024)
struct.pack_into("<I", desc, 0x11c, 0x04010000)
struct.pack_into("<I", desc, 0x27c, 0x04010000)
struct.pack_into("<Q", desc, 0x160, allocation)
struct.pack_into("<Q", desc, 0x2c0, allocation)
return desc

def key(desc):
value = 0x12345678
for part in (mesh57_profile.stable_mesh57_profile_descriptor(desc), b"\x01\x02\x03\x04", b"\x05\x06"):
value = mesh57_profile.fnv(part, mesh57_profile.fnv(struct.pack("<Q", len(part)), value))
return value

original = build(0x10020000, 0x30030000)
stable = mesh57_profile.stable_mesh57_profile_descriptor
assert stable(original) == stable(stable(original))

unknown = bytearray(original)
struct.pack_into("<I", unknown, 0x11c, 0x04020000)
unknown_40 = bytearray(unknown)
struct.pack_into("<Q", unknown_40, 0x1b8, 0x40)
unknown_80 = bytearray(unknown)
struct.pack_into("<Q", unknown_80, 0x1b8, 0x80)
assert stable(unknown_40) == bytes(unknown_40) and stable(unknown_80) == bytes(unknown_80)
assert stable(unknown_40) != stable(unknown_80)
assert key(unknown_40) != key(unknown_80)

def second_base(section, base, spacing):
desc = build(section, 0x30030000)
for offset, value in ((0x188, base), (0x1a8, base + 0x400), (0x1c8, base + 0x700),
(0x2e8, base + spacing)):
struct.pack_into("<Q", desc, offset, value)
return desc

shifted = second_base(0x10020000, 0x86abaf00, 0x2f100)
shifted_again = second_base(0x140050000, 0x1a0baf00, 0x2f100)
respaced = second_base(0x10020000, 0x86abaf00, 0x2f200)
assert key(shifted) == key(shifted_again)
assert key(shifted) != key(respaced)

metadata_40 = bytearray(original)
struct.pack_into("<Q", metadata_40, 0x188, 0x40)
metadata_80 = bytearray(original)
struct.pack_into("<Q", metadata_80, 0x188, 0x80)
assert key(metadata_40) != key(metadata_80)


if __name__ == "__main__":
main()
6 changes: 5 additions & 1 deletion tests/native_direct_mesh_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import sys
import tempfile

sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tools" / "startup"))
import mesh57_profile


def triangle():
return [
Expand Down Expand Up @@ -212,7 +215,8 @@ def checked_pixel_entry(folder, compiler, sdk):
for offset, value in ((384, 3), (388, 1), (408, 1), (436, 8), (480, 28), (512, 1),
(520, 1), (528, 0xffffffff)):
struct.pack_into("<I", header, offset, value)
profile = folder / f"{variant:016x}.mesh57-state"
stable_variant = mesh57_profile.profile_variant(archive)
profile = folder / f"{stable_variant:016x}.mesh57-state"
blob = header + struct.pack("<3I", 0, 0, 64) + b"".join(parts)
check_profile_writer(folder, archive, profile, blob)
profile.write_bytes(blob)
Expand Down
Loading
Loading