Files
mandy/docs/directstorage.md
Moriz82 273b3bdf9d [integration] Linux/Proton and native Windows: boot through the story load (RT-PROTON-STORY-LOAD)
One squashed commit of a fork that was developed on a Linux/Proton machine and
tested on two native Windows machines. On all of them the retail 1.0.2.1
package now launches, shows its launch movie, first-run pages and landing
prompt, runs the story load to its end with the loading slideshow drawing its
artwork, and enters the world phase. A first world frame is not verified yet.
docs/linux_proton.md has the setup, a healthy boot's timeline, the machines
and where it stops.

Runtime: frames in the title's own window; DirectStorage pinned reads,
page-list scatter/gather, BCPack and unshuffle with a locally generated
predictor file; pooled, device-local and deferred image backing for streamed
and placed textures; enhanced barriers into LAYOUT_UNDEFINED; a bindless table
that degrades instead of refusing a submission; a descriptor hash finalizer
(off until a library carries it); directly indexed heap offsets for compute
dispatches and VS/PS/GS draws; title buffer windows outside a fragmented
arena; opt-in device-local copies of streamed buffer views; write-fault
retries under the tracker lock, shared trackers and write-watched title memory
for native Windows; device-loss, compute-input and memory probes.

Translator: loop bounds at structured exits, the eight FidelityFX programs
that use a packed-dot form the disassembler skips, buffer bounds modes per
view kind, a compaction option, the hash finalizer option.

Tools and scripts: Linux boot scripts (Wine/Proton, no network), a Windows
boot script, the paired compute translation script.

No game data. AI-assisted (Claude Code and Codex agents), reviewed and booted
by people on the machines named in docs/linux_proton.md.

Co-authored-by: Plaaasma <47513553+Plaaasma@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-24 11:42:12 -06:00

25 KiB
Raw Blame History

DirectStorage

The console title reads its streamed data through Xbox DirectStorage: DStorageGetFactory hands out a factory, the factory opens files and creates queues, and a queue reads file or memory ranges into memory and reports completion through status arrays, ID3D12Fence signals and events. runtime/shims/dstorage.cpp serves that API on the PC: files open through the runtime's GAME: mapping and write overlay, and a worker thread per queue does the reads and inflates zlib requests. This page records which DLL and ABI the console title is expected to use, how sure that is, and what the shim does.

Which DLL and which ABI: the evidence

The game executable is not in the package available here, so its import table is unmeasured (runtime_imports.md). What is known:

Evidence Source What it says
None of the 16 shipped DLLs imports DirectStorage runtime_imports.md (6 Xbox-specific imports, all xmem/d3d12_xs), textures.md "The shipped binaries" (no DStorage import, export or string) DirectStorage, if used, is called by the executable itself
The texture chunks carry the DSTORAGE_BCPACK_MODE values 1-7 and mix zlib and BCPack chunks textures.md "What BCPack is" The data is laid out for console DirectStorage's decompression (DEFLATE, then BCPack, then swizzle)
A sibling console layer's export inventory names dstorage_xs.dll as console DirectStorage d3d12_vulkan.md §7 The console module name, measured elsewhere, not here
A retail title run by a sibling layer has xcrdapi in its console-only library set d3d12_vulkan.md §7 Only the name. Nothing in this repository says what xcrdapi.dll exports
The GDK reference: DStorageGetFactory is in header dstorage_xs.h, library dstorage_xs.lib; the interface pages give library dstorage.lib; all say "Xbox Series consoles" GDK DStorage reference The console API has its own names (IDStorageFactoryX, IDStorageQueueX, IDStorageFileX, IDStorageStatusArrayX) and structures that differ from the PC API
registry.cpp classified dstorage.dll/dstorage_xs.dll DStorageGetFactory as returning an HRESULT runtime/shims/registry.cpp It was an unimplemented import; xcrdapi.dll was not named

Chosen ABI: the console one (the GDK's IDStorageFactoryX2/IDStorageQueueX2/IDStorageFileX/ IDStorageStatusArrayX and its DSTORAGE_REQUEST), served as DStorageGetFactory under all three names:

Module Confidence Why
dstorage_xs.dll medium The reference's dstorage_xs.lib and the sibling inventory agree; not measured in this title
xcrdapi.dll low Only a name in a library list. Owning the module also means a title's LoadLibrary("xcrdapi.dll") gets the shims' pseudo-handle, and GetProcAddress for any other name returns NULL (logged). Static imports of other xcrdapi names are unchanged
dstorage.dll low The interface pages name dstorage.lib. Every caller of gta6rt's import table is a console build, so this name serves the console ABI too, not the PC one

The PC DirectStorage ABI (dstorage.h: IDStorageFactory with DSTORAGE_REQUEST source and destination unions, ID3D12Resource destinations) is not served. A caller that asks for a PC interface id gets E_NOINTERFACE and a log line, never an object of the wrong shape. The one exception is IDStorageStatusArray, whose two methods are the console interface's in the same order (console D3D12 binaries reuse the PC SDK's ids), so a status array accepts its id.

Owning a module has a run-time side: LoadLibrary of xcrdapi.dll or dstorage.dll (or dstorage_xs.dll) gets the shims' pseudo-handle even when the package ships its own copy of that DLL, so the shim shadows a packaged copy at run time (library_loader.cpp asks the registry before the bundled loader). None of the 16 shipped DLLs is one of these, so nothing is shadowed today.

What is not public, and the choice made

Unknown Choice Confidence Where to change it
Vtable order of the console interfaces (the reference lists methods alphabetically) The PC header's order for the methods both APIs have; console-only methods where the overview lists them: factory CreateQueue, OpenFile, CreateStatusArray, SetCpuAffinity, SetDebugFlags, SetStagingBufferSize, then GetStats (X1), CreateQueue1 (X2); queue EnqueueRequest, EnqueueStatus, EnqueueSignal, Submit, CancelRequestsWithTag, Close, GetErrorEvent, RetrieveErrorRecord, Query, then EnqueueSetEvent (X1), Query1 (X2); status array IsComplete, GetHResult; file Close, GetHandle queue, status array: medium; file: medium; factory: low (the SetCpuAffinity slot is a guess) runtime/shims/dstorage_xs_abi.h, one declaration per interface
Interface ids of the X interfaces A creating call (DStorageGetFactory, CreateQueue*, OpenFile, CreateStatusArray) accepts an id it has not seen for the kind of object it hands out and logs it once ([rt] dstorage: ... asked for {...}: serving the ... interface), so the first boot records them. It refuses an id seen for another kind, a PC DirectStorage id, and the system COM interfaces (the {xxxxxxxx-0000-0000-C000-000000000046} range with IMarshal, IClientSecurity, ..., and IAgileObject, INoMarshal, IInspectable and a few more). QueryInterface follows d3d12_vulkan.md's rule (an unknown id is E_NOINTERFACE, logged): it accepts only IUnknown, the declared IDStorageStatusArray id for a status array, and the ids a creating call accepted for that kind of object n/a (a policy, not a fact) IidPolicy in runtime/shims/dstorage.cpp
DSTORAGE_MIN_QUEUE_CAPACITY, DSTORAGE_MAX_QUEUE_CAPACITY, DSTORAGE_REQUEST_MAX_NAME The PC header's values: 0x80, 0x2000, 64 medium dstorage_xs_abi.h
E_DSTORAGE_* values The PC dstorageerr.h values (FACILITY_GAME, 0x8924xxxx). That header also defines the console-only failures (zlib, BCPack, swizzle, XVD), so the space is shared. E_DSTORAGE_DECOMPRESSION_ERROR (0x89240030) is on the public constants page; E_DSTORAGE_ZLIB_BAD_HEADER, _ZLIB_BAD_DATA and _ZLIB_PARITY_FAIL (0x89240031-33) have no public description, and their names and values come from the header-generated bindings of the direct-storage crate (Sources). Which failure the console reports with each zlib code is inferred from the names medium-high (values); medium (zlib codes' meaning) dstorage_xs_abi.h
Struct layouts The compiler's x64 layout of the reference's declarations, pinned with static_assert (DSTORAGE_REQUEST 72 bytes, DSTORAGE_QUEUE_DESC 24, DSTORAGE_QUEUE_DESC1 32); DSTORAGE_REQUEST_OPTIONS is decoded from AsUINT64 by explicit bit positions (zlib bit 0, BCPack 1-4, swizzle 5-20, destination pages 21, source type 22, source pages 23, reserved 24-63) high for member order and types, medium for the bit allocation (MSVC allocates bit-fields from bit 0 up) dstorage_xs_abi.h

The first boot with the executable settles most of this: the dry-run report shows which module it imports DStorageGetFactory from, and the log lines give the interface ids.

What the shim does

  • Factory: one object for the process, never freed (its reference count does not drop below one). SetDebugFlags and SetCpuAffinity are stored only (a console core mask means nothing on the host). SetStagingBufferSize accepts 0, 16, 20, 24, 28 and 32 MiB, fails with E_DSTORAGE_INVALID_STAGING_BUFFER_SIZE otherwise and with E_DSTORAGE_STAGING_BUFFER_LOCKED while a queue or file exists. This records the console setting. Host transform scratch is allocated on demand. GetStats reports the counts the shim keeps (queues per priority, requests, bytes, files, errors, zlib inflates: TotalDecompressionRequestCount grows when a zlib request's inflate starts, which is after its checks passed and its source was read, PendingDecompressionRequestCount is the inflates running, and DecompressionErrors the streams that failed). As DSTORAGE_PRIORITY_STATS defines them, TotalRequestCount and the byte totals grow when a request is submitted (not when it is enqueued), and PendingRequestCount counts the requests the worker is processing; status and fence entries are counted when they are enqueued. A bad argument (a null pointer, an invalid queue desc or staging size) is logged once per method and kind, so a wrong vtable guess shows up in the log as arguments that make no sense for the method.

  • Files: OpenFile goes through the registry's kernel32.dll!CreateFileW, the shim in file_system.cpp, so a GAME: path reads the overlay copy first, then the package, and a path that climbs out is refused (a host path is passed through). Read access, FILE_SHARE_READ, as the overview says; a failed open returns the Win32 error as an HRESULT. GetHandle returns a handle of its own (ReOpenFile, read access, FILE_SHARE_READ) that the caller closes; it has its own file position, which the shim's reads do not move. Close waits for the submitted requests that read the file; a request processed after it fails with E_DSTORAGE_FILE_NOT_OPEN. A request holds a reference on its file, so the last Release cannot close a file a request still reads.

  • Queues: capacity 0x80..0x2000, priority LOW..REALTIME, a memory-sourced queue must be REALTIME (E_DSTORAGE_INVALID_QUEUE_CAPACITY, _INVALID_QUEUE_PRIORITY, _INVALID_MEMORY_QUEUE_PRIORITY). A queue holds capacity - 1 entries; Query reports the empty slots and the entries left before the automatic submit, which happens when the entries enqueued since the last submit reach half the capacity (unless DSTORAGE_QUEUE_OPTIONS::AutoSubmitDisabled). An enqueue into a full queue waits for space, as the reference says; with auto-submit disabled only a Submit from another thread frees it. One worker thread per queue runs the submitted entries in order, so every notification follows all the entries before it.

  • Requests: copied at enqueue. A file request reads SourceSize bytes at FileOffset into Destination; a memory request copies Source to Destination (memmove, so an overlapping pair is safe); a zlib request inflates those SourceSize bytes into Destination (below). Checked in this order when the worker reaches the request:

    1. cancelled: a success, whatever else is wrong with it;
    2. found at enqueue: a null request pointer (E_INVALIDARG) or a file object the shim did not make (E_DSTORAGE_INVALID_FILE_HANDLE);
    3. reserved option bits (E_DSTORAGE_RESERVED_FIELDS), then the source type against the queue's (E_DSTORAGE_INVALID_SOURCE_TYPE);
    4. a BCPack mode above 7 or swizzle mode above 4. BC2 is reserved here for a future decoder (E_NOTIMPL);
    5. a nonzero IntermediateSize is required only for combined zlib and BCPack. Compressed requests require a larger output than source, complete BC blocks and complete swizzle groups. Raw requests require equal source and destination sizes. File offsets used for decompression must be 16-byte aligned. The total memory bound is 1 GiB, with ordinary buffers limited to 32 MiB apiece. Page lists can exceed the 32 MiB bound;
    6. a null destination, a null source (memory) or a null file (E_INVALIDARG), a closed file (_FILE_NOT_OPEN);
    7. a zero size: a success that touches no memory (an assumption: the reference does not say);
    8. a range past the end of the file (E_DSTORAGE_END_OF_FILE), then a destination or source the process cannot access (E_DSTORAGE_ACCESS_VIOLATION; the worker survives it). This needs asynchronous exceptions under clang-cl, so runtime/CMakeLists.txt builds dstorage.cpp with /EHa there; MSVC always catches it.

    Step 7 does not apply to a zlib request (its DestinationSize is never 0 after step 5); its stream fails as described next.

  • zlib (ZlibDecompress alone): the source, SourceSize bytes of the file or of memory, is read into a staging buffer and inflated there by the runtime's RFC 1950 inflater (leonida::zlib_inflate in runtime/src/shader_library.h, the one the shader library uses for XBCH blobs). The result is copied to Destination only when it is exactly DestinationSize bytes, so a stream that fails leaves the destination as it was, and a memory source inside its own destination (in-place loading) inflates correctly. For ordinary destinations, step 5 also means a stream of stored blocks only, which is larger than what it holds, is refused with _INVALID_DESTINATION_SIZE before it is read, as the documented rule implies. The stream is a zlib stream: the two header bytes, the deflate blocks (stored, fixed and dynamic Huffman), the Adler-32 of the result. Bytes after the Adler-32 are not read, so a SourceSize rounded up over padding works (an assumption: the reference does not say). The game's own data agrees with RFC 1950: the texture pools' codec-8 chunks, laid out for this decompression (textures.md), inflate with Python's zlib.decompress, which checks the header and the Adler-32 (tools/ttpf_mips.py). A failure:

    Stream HRESULT
    header not deflate, a window over 32 KiB, a wrong check value, or a preset dictionary E_DSTORAGE_ZLIB_BAD_HEADER (0x89240031)
    a reserved block type, an invalid code, a distance before the start, or a truncated stream E_DSTORAGE_ZLIB_BAD_DATA (0x89240032)
    Adler-32 mismatch E_DSTORAGE_ZLIB_PARITY_FAIL (0x89240033)
    inflates to more or fewer bytes than DestinationSize E_DSTORAGE_DECOMPRESSION_ERROR (0x89240030; no code names a size mismatch, so the generic one)

    The inflater decodes one bit at a time, on the queue's worker: correct, not fast. A faster decoder (table lookup) can replace it behind zlib_inflate without touching the request path.

  • Cancel: CancelRequestsWithTag(mask, value) marks every request enqueued before the call and not yet started whose (CancellationTag & mask) == value. A cancelled request completes as a success without reading and is not counted as a failure, as the overview and the reference say, even when it also had an error found at enqueue.

  • Notifications: EnqueueStatus sets the entry to E_PENDING at once and, when reached, to the first failure since the previous status or signal (else S_OK); IsComplete is GetHResult != E_PENDING; an index past the array is E_DSTORAGE_INDEX_BOUND, a foreign or null array E_DSTORAGE_INVALID_STATUS_ARRAY. EnqueueSignal calls ID3D12Fence::Signal (PC slot 10, which the d3d12_xs fence proxies forward) on the worker thread; a null fence is E_DSTORAGE_INVALID_FENCE. EnqueueSetEvent duplicates the handle at once and sets the duplicate, then closes it, so an event the title closes in the meantime (and a handle value reused for another object) is never the one set; a null or invalid handle is recorded as a failure. Each notification holds a reference on its array or fence until it has run.

  • Errors: a failure goes into the status entry, the queue's error record (failure count and the first failure since the last RetrieveErrorRecord, with the file name the title opened) and sets the error event. GetErrorEvent returns a duplicate handle (the reference says the caller closes it). RetrieveErrorRecord clears the record and resets the event; with nothing recorded it returns DSTORAGE_COMMAND_TYPE_NONE. A failed SetEvent is recorded with command type 3 (the PC API's EVENT; the console enum stops at 2).

  • Close and release: Close submits what is enqueued, waits until the worker has run it all and stops the worker; later enqueues are dropped and recorded as E_DSTORAGE_QUEUE_CLOSED. The last Release closes the same way, so a queue released with requests in flight finishes them first.

Streamed texture requests

  • Two adjacent file-backed transforms can read and decode concurrently into private buffers. A queue has one ordered publisher. Mutable memory inputs, page-list resolution, status slots, events, fences, and overlapping destinations retain their enqueue order. At most two helpers run across all queues and each assisted request has a 16 MiB eligibility limit. A shared 64 MiB window covers private source, intermediate, output and page-number buffers across queues. Each transform releases its previous input and each request releases the final buffer at retirement. A valid request larger than the window runs alone, bounded by the ABI maximum of two 1 GiB payloads. Raw buffer reads use no private staging. With tracing enabled, ds-staging reports the process peak reservation and return to zero. The figure excludes title destinations, allocator overhead and fixed decoder stacks. LEONIDA_DSTORAGE_WORKERS=1 restores serial execution. Inflate expands LZ matches in bulk and batches Adler-32 reductions. LEONIDA_DSTORAGE_FAST_INFLATE=0 selects the original bytewise path. BCPack indexes the entropy model by the high slot byte. LEONIDA_DSTORAGE_FAST_BCPACK=0 selects the scalar bucket search.

  • With LEONIDA_DSTORAGE_TRACE=1, tools/dstorage_census.py <trace> totals retirements, errors and bytes by shape, plus microseconds spent reading, inflating, decoding BC blocks, unshuffling, publishing and notifying. --from-file story000 starts the measurement at the first story archive request and counts every subsequent retirement across queues. Stage times can overlap. Each failure reports its process-wide count for that HRESULT even with request tracing disabled.

  • The worker inflates zlib to IntermediateSize when BCPack follows, rebuilds standard BC blocks, then applies the optional DirectStorage macro-unshuffle. Each stage validates its entire output before a destination write. Typed zlib errors propagate through the combined path.

  • runtime/src/bcpack.cpp ports the interleaved rANS and strict block rebuilders from tools/bcpack.py for BC1, BC3, BC4, BC5, BC6H and BC7. The predictor window holds 256 blocks. The build generates the lookup tables from the existing codec modules. Damaged streams and missing predictors or inconsistent blocks exceeding the reference tolerance give E_DSTORAGE_DECOMPRESSION_ERROR. BC2 has no reference decoder. The runtime accepts the reference's fitted BC1/3/4/5 predictors for streamed art. LEONIDA_DSTORAGE_FITTED=0 requires confirmed entries. Fitted blocks can differ from console output. The runtime stage of make_game.py generates <work>/shaders/bcpack/history.bcph locally. The loader finds it relative to the shader folder selected by LEONIDA_SHADERS or the packaged shader_path.txt. LEONIDA_DSTORAGE_HISTORY overrides the path; a value of 0 disables external history. The load diagnostic reports BC6H row coverage and BC7 endpoint/index field coverage. Standalone generation:

    python3 -I tools/build_dstorage_history.py <game-root> --out <work>/shaders/bcpack/history.bcph
    

    The fitter follows the physical block offset of packed 2D mips, including those that start past the first 256 blocks of a stream. It combines adjacent mips from the same layer with full-stream index anchor and shared-p-bit constraints. A shared p-bit can relate low red endpoint bits even when its sibling mip supplies only the leading bits. These local fits preserve the recorded precision and exact reference bits. A profile can still lack endpoint values or exceed the reference consistency tolerance. Such a request reports a decompression error before publication.

  • The four swizzle modes interleave stripes from request-wide component planes into 16 KiB (mode 1) or 32 KiB (modes 2–4) groups. The enum names give the successive stripe widths. The shipped BC4 sample separates its endpoint planes from indices across the inflated source. The title's compute shader does the within-group shuffle and the stored blocks already follow the texture layout. Microsoft texture shuffling sample

  • Physical destinations and memory sources use 64 KiB pages with byte offsets in the first page. The worker reads the page list after submit, resolves and commits every page before copying, and holds xmem's map lock through the transfer. A successful scatter notifies the texture refill tracker at each mapped VA. Ordinary GPU alias destinations use the paired CPU view. Raw file writes into title memory go through the shared kernel-write helper, which pins each kernel write, commits first-touch pages on an access error and retries once. The superseded LEONIDA_DSTORAGE_PIN_FILE_READS switch is retired.

  • LEONIDA_DSTORAGE_EXTENDED=0 restores the old refusal for page lists, BCPack and nonzero swizzle. LEONIDA_DSTORAGE_TRACE=1 logs each retired request, worker timestamp, options, source file/range, sizes, destination, cancellation and HRESULT. Queue pointers connect those lines to status slots, fences and events. Failures always log the full request. An access violation also records the CPU mapping and protection. With startup tracing enabled, dstorage_failure events share its clock with title exceptions. Pool fill lines report whether a successful texture upload used any nonzero detiled bytes.

Tests

runtime/test/dstorage_test.cpp (ctest dstorage) runs on synthetic files in %TEMP% through the registry entries under all three module names: GAME: reads with an overlay copy winning, offsets, zero size, a range past the end, cancellation, auto-submit, a full queue, status arrays, fences (a CPU fence object that checks the data is in place when it is signalled), events, error records, file and queue close with requests in flight, a destination the process cannot write, four threads enqueueing at once, the interface-id policy (unknown and system ids refused by QueryInterface, learned by creating calls), a GetHandle handle with its own file position, an event closed (and its handle value reused) before it is set, and cancelled requests that also had an enqueue error. zlib requests use synthetic streams only: two made with Python's zlib (level 9: one dynamic-Huffman block; level 1: one fixed-Huffman block) from text the test rebuilds, and stored-block streams the test builds, one of them a stored block followed by the dynamic one. They are read from a file and from memory (also in place); a bad header, a preset dictionary, a reserved block type, a truncated stream, an Adler-32 mismatch, a stream shorter and one longer than DestinationSize, a stored stream larger than its result, an unaligned offset, an invalid IntermediateSize, an unreadable source and an unwritable destination each get their code, a failed stream leaves its destination untouched, and the decompression statistics count them.

The extended tests encode hand-built lane symbols with tests/test_bcpack.py. All six modes are compared byte-for-byte with the Python decoder through the queue, both bare and zlib-wrapped. Additional cases cover truncation, all four swizzle patterns across three groups, an invalid second page, late page-list updates, a decode crossing two reversed pages, both CPU refill ranges, gather, and event/status ordering.

Sources and licences

Source Facts used Licence / use
GDK DStorage reference and its function, interface, structure and enumeration pages Names, method signatures, structure members, enum values, requirements rows Microsoft Learn Terms of Use; facts paraphrased, no text copied
DirectStorage overview (GDK) Queue, auto-submit, cancellation, notification order, file sharing, decode order Same terms; paraphrased
dstorage.h, dstorageerr.h in the public Microsoft.Direct3D.DirectStorage 1.3.0 NuGet package PC method order, interface ids (to refuse them), capacity and name-length constants, E_DSTORAGE_* values Read for facts only; not vendored. runtime/shims/dstorage_xs_abi.h is Leonida's own declaration
DirectStorage constants (Win32 reference) E_DSTORAGE_DECOMPRESSION_ERROR = 0x89240030; E_DSTORAGE_INVALID_DESTINATION_SIZE: equal to the length without decompression, larger with it; E_DSTORAGE_INVALID_FILE_OFFSET is the offset invalid for a decompression mode Microsoft Learn Terms of Use; paraphrased
direct-storage crate 0.5.1 (bindings generated by windows-bindgen from the DirectStorage headers; 0.2.0's errors.rs has the same values) E_DSTORAGE_ZLIB_BAD_HEADER 0x89240031, E_DSTORAGE_ZLIB_BAD_DATA 0x89240032, E_DSTORAGE_ZLIB_PARITY_FAIL 0x89240033 (names and values only) MIT/Apache-2.0; two constants' names and values, no code copied
RFC 1950 and RFC 1951 The zlib stream and DEFLATE formats zlib_inflate decodes Public specifications