Skip to content
63 changes: 50 additions & 13 deletions docs/directstorage.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,14 @@ The first boot with the executable settles most of this: the dry-run report show
(`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. BCPack mode above 7, swizzle mode above 4 (`_INVALID_BCPACK_MODE`, `_INVALID_SWIZZLE_MODE`), then BCPack or
swizzle, with or without zlib, then physical pages (`E_NOTIMPL`, logged once: later PRs);
5. an `IntermediateSize` (`_INVALID_INTERMEDIATE_SIZE`: it is the size between the inflate and the BCPack decode,
so only a zlib + BCPack request has one), a `DestinationSize` other than `SourceSize` without zlib, or not
larger than `SourceSize` with it (`_INVALID_DESTINATION_SIZE`: the rule `dstorageerr.h` gives for that code,
and what `DSTORAGE_REQUEST` says of `DestinationSize`), more than 1 GiB in total, then a buffer over 32 MiB
4. BCPack mode above 7, swizzle mode above 4 (`_INVALID_BCPACK_MODE`, `_INVALID_SWIZZLE_MODE`), then swizzle,
BCPack mode 2 (BC2 has no BCPack form) and physical pages (`E_NOTIMPL`, logged once: later PRs);
5. an `IntermediateSize` on any but a zlib + BCPack request, or none on one (`_INVALID_INTERMEDIATE_SIZE`: it is the
size between the inflate and the BCPack decode), an `IntermediateSize` over 32 MiB (`_REQUEST_TOO_LARGE`), a
`DestinationSize` other than `SourceSize` without decompression, not larger than `SourceSize` with zlib alone, or
0 with BCPack (`_INVALID_DESTINATION_SIZE`: the rule `dstorageerr.h` gives for that code, and what
`DSTORAGE_REQUEST` says of `DestinationSize`; a BCPack chunk's size against its blocks depends on the texture),
more than 1 GiB in total, then a buffer over 32 MiB
(`_REQUEST_TOO_LARGE` for both: `DSTORAGE_REQUEST` allows a buffer over 32 MiB only with 64 KiB or 2 MiB pages,
which are not implemented yet; the source is never larger than the destination here, so the destination's
limit covers it), then a zlib file request whose `FileOffset` is not 16-byte aligned (`_INVALID_FILE_OFFSET`,
Expand Down Expand Up @@ -132,6 +134,38 @@ The first boot with the executable settles most of this: the dry-run report show

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.
- **BCPack** (`BcpackMode` 1, 3, 4, 5, 6 or 7, with or without `ZlibDecompress`): the source is read into a staging
buffer, inflated first to exactly `IntermediateSize` bytes when the request is zlib too (the same inflater and
failure codes as above), then decoded into `DestinationSize` bytes of BCn blocks by the runtime's BCPack decoder
(`runtime/src/bcpack.h`, library `gta6rt_bcpack`, a port of `tools/bcpack.py` for all six modes). The request path
decodes leniently: a block that rests on built-in history nobody has measured (all of BC6H's and BC7's) is rebuilt
from the zero state, as `bcpack.decode(..., allow_provisional=True)` does, and the request still succeeds, with a
line logged once, so a texture has a few wrong blocks instead of missing. A chunk the decoder refuses (a corrupt
stream, non-zero bytes after it, a destination that is not whole blocks, a BC6H mode or BC7 signature outside its
table) fails with `E_DSTORAGE_DECOMPRESSION_ERROR` and leaves the destination as it was. Decoded BCPack requests count
in the decompression statistics like zlib ones. `bcpack_test` checks the decoder against the Python one on
synthetic chunks (`runtime/test/bcpack_vectors.py`): strict decoding on all of them, lenient BC6H and BC7 decoding
against Python's provisional mode. Swizzle is not applied (no request measured on the retail title asks for it).

**The calibrated BC6H/BC7 history.** The repository carries no built-in BC6H or BC7 history: it has to be
calibrated from the user's own copy (`tools/bcpack_history_calib.py`, the `--history` JSON of `tools/bcpack.py`).
`python -I tools/bcpack_history_sidecar.py <history.json | folder> bcpack_history.bin` turns that JSON into a
60,936-byte "BCPHIST1" sidecar (the same loaders as `tools/bcpack.py`, so the same merging and mid-range fill; the
layout of the #984 integration branch's profile). The request path reads it once, at its first BCPack decode: the
file `LEONIDA_BCPACK_HISTORY` names, or else `bcpack_history.bin` next to the runtime's executable; with one, a block
on a calibrated entry decodes from that entry instead of from zeros, and only entries that are still unknown count as
guessed. A file that is not a BCPHIST1 sidecar is ignored with a log line. The sidecar comes from the user's data:
it stays on their machine. `bcpack_test` decodes with a synthetic sidecar against `tools/bcpack.py` with the same
history, strict and provisional, and `dstorage_test` reads one through `LEONIDA_BCPACK_HISTORY`.

**The calibrated BC1/BC3/BC4/BC5 history.** The repository's tables confirm 252 BC1 entries but none for BC3, 2 for
BC4 and 5 for BC5, so most BC3/BC4/BC5 blocks that predict from the built-in history are guessed without a
calibration. When the history JSON has a `bc1` / `bc3` / `bc4` / `bc5` section (`load_bc_history`), the tool appends
a "BC15HIS1" section (71,192 bytes in all; without one the file keeps the 60,936-byte profile size): per mode a given
flag, the section's `replace` flag and its confirmed entries. The runtime merges them as `_resolve_bc_history` does:
an entry given there wins over the built-in one, and with `replace` the built-in table is not read. Provisional
entries are not written, as for the built-in tables. `bcpack_test` checks 100 BC1-5 chunks against
`tools/bcpack.py` with the same synthetic history (BC1 replacing its table, BC4 and BC5 merged, BC3 not given).
- **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.
Expand All @@ -154,11 +188,10 @@ The first boot with the executable settles most of this: the dry-run report show

## Not done yet (later PRs)

- **BCPack** (`BcpackMode`; `tools/bcpack.py` is the reference decoder), then **swizzle** (`SwizzleMode`). The
decode order is DEFLATE, BCPack, swizzle; with both zlib and BCPack `IntermediateSize` is the size between them
(the inflate's output, the BCPack decoder's input), and `FileOffset` must be 16-byte aligned. Each becomes a stage
after `inflate_request`'s inflate and before its destination write, so the request checks above stay where they
are.
- **Swizzle** (`SwizzleMode`): the decode order is DEFLATE, BCPack, swizzle, so it becomes a stage after the BCPack
decode and before the destination write. No request measured on the retail title asks for it.
- **BCPack requests on the retail title**: every one measured (modes 1, 5 and 7, issue #14 comment 7135719755) also
has the destination-pages bit, so they reach the decoder once destination pages are served (RT-DSTORAGE-DEST-PAGES).
- **Physical pages** (`SourceIsPhysicalPages`, `DestinationIsPhysicalPages`): arrays of 64 KiB page numbers from
`XMemAllocatePhysicalPages`; needs the `xmem` shim to translate a page number to the address it mapped.
- **GPU memory**: the console API has no GPU destinations of its own (memory is unified, so a destination is an
Expand All @@ -182,8 +215,12 @@ fixed-Huffman block) from text the test rebuilds, and stored-block streams the t
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 `IntermediateSize`, zlib with
BCPack or swizzle (`E_NOTIMPL`), an unreadable source and an unwritable destination each get their code, a failed
stream leaves its destination untouched, and the decompression statistics count them.
swizzle (`E_NOTIMPL`), zlib with BCPack whose stream is longer than its `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. BCPack requests use chunks of `runtime/test/bcpack_vectors.h` from memory: BC1 and BC7 chunks
decode to the Python decoder's blocks, a BC7 chunk on unknown built-in history to its provisional blocks, a BC1 chunk
inside a stored zlib stream to the same blocks; a corrupt chunk, a destination that is not whole blocks, a zlib +
BCPack request without an `IntermediateSize` and a BCPack request with no destination size each get their code.

## Sources and licences

Expand Down
21 changes: 19 additions & 2 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ add_executable(root_signature_test
test/root_signature_test.cpp
)
target_link_libraries(root_signature_test PRIVATE gta6rt_root_signature)
# Portable BCPack decoder for the DirectStorage worker (a port of tools/bcpack.py; no windows.h), with the table
# header tools/bcpack_header.py writes. Its test is in the BUILD_TESTING block below.
add_library(gta6rt_bcpack STATIC src/bcpack.cpp)
target_include_directories(gta6rt_bcpack PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
if(MSVC)
target_compile_options(gta6rt_bcpack PRIVATE /W4 /permissive- /utf-8)
endif()
# AVX, OSXSAVE and XCR0 YMM state from synthetic CPUID/XCR0 words, plus one call on the real host.
add_executable(host_cpu_test test/host_cpu_test.cpp src/host_cpu.cpp)
target_include_directories(host_cpu_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
Expand Down Expand Up @@ -188,7 +195,7 @@ if(WIN32 AND LEONIDA_BUILD_WINDOWS_RUNTIME)
# scripts/build_runtime.ps1.
set(GTA6RT_STACK 8388608)
target_link_options(gta6rt PRIVATE /OPT:NOREF ${GTA6RT_HOST_BASE} /STACK:${GTA6RT_STACK})
target_link_libraries(gta6rt PRIVATE gta6rt_d3d12_xs_rootsig gta6rt_xtaskqueue psapi)
target_link_libraries(gta6rt PRIVATE gta6rt_d3d12_xs_rootsig gta6rt_xtaskqueue psapi gta6rt_bcpack)

# Root-signature forwarding test: needs a real PC d3d12.lib to serialize the synthetic descriptors.
# find_library searches %LIB% (a Developer Prompt), not the Windows SDK the Visual Studio generator links
Expand Down Expand Up @@ -311,8 +318,10 @@ if(WIN32 AND LEONIDA_BUILD_WINDOWS_RUNTIME)
)
target_include_directories(dstorage_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/shims)
target_compile_definitions(dstorage_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
target_compile_options(dstorage_test PRIVATE /W4 /permissive- /utf-8)
target_compile_options(dstorage_test PRIVATE /W4 /permissive- /utf-8 /bigobj)
target_link_options(dstorage_test PRIVATE /OPT:NOREF)
# BCPack requests decode through the runtime's decoder (src/bcpack.h); the test reuses its synthetic vectors.
target_link_libraries(dstorage_test PRIVATE gta6rt_bcpack)

# xapu.dll and chad.dll, the APU and audio DSP command queues, resolved through the registry like xmem_test.
add_executable(xapu_chad_test
Expand Down Expand Up @@ -577,6 +586,14 @@ if(BUILD_TESTING)
add_executable(gfx10_tiling_test test/gfx10_tiling_test.cpp)
add_test(NAME gfx10_tiling COMMAND gfx10_tiling_test)

# The BCPack decoder against tools/bcpack.py's answers for synthetic chunks (test/bcpack_vectors.h).
add_executable(bcpack_test test/bcpack_test.cpp)
target_link_libraries(bcpack_test PRIVATE gta6rt_bcpack)
if(MSVC)
target_compile_options(bcpack_test PRIVATE /W4 /permissive- /utf-8 /bigobj)
endif()
add_test(NAME bcpack COMMAND bcpack_test)

add_executable(retire_tickets_test test/retire_tickets_test.cpp)
add_test(NAME retire_tickets COMMAND retire_tickets_test)
# VulkanContext::buffer_at's VA index (runtime/src/buffer_va_index.h): pure stdlib, no Vulkan instance.
Expand Down
Loading
Loading