Skip to content
Closed
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
12 changes: 11 additions & 1 deletion docs/directstorage.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,17 @@ The first boot with the executable settles most of this: the dry-run report show
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.
are. **The decoder itself exists** (`runtime/src/bcpack.h`, library `gta6rt_bcpack`, RT-BCPACK-DECODER): a port
of `tools/bcpack.py`'s lane layer and of its BC1, BC3, BC4, BC5 and BC7 rebuilds, with the user's calibrated
history as an optional `History`. It has two policies: strict (the default), like `bcpack.decode`, refuses a chunk
that needs an unconfirmed history entry, an inferred BC7 signature or a BC7 block whose anchor / p-bit check fails;
lenient, for the request path, decodes such blocks anyway (from the zero state where the history is unknown, which
is all of BC7's built-in history today) and reports how many in `Result::guessed`, so a texture comes with a few
wrong blocks instead of missing. `bcpack_test` checks strict decoding against the Python decoder and lenient BC7
decoding against its provisional mode, on synthetic chunks (`runtime/test/bcpack_vectors.py`). Mode 6 (BC6H)
returns `unsupported_codec` until it is ported. What is left here is the wiring: call `bcpack::decode` from `execute_request` once destination
pages exist, since every BCPack request measured on the retail title (modes 1, 5 and 7, issue #14 comment
7135719755) also has the destination-pages bit.
- **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 Down
15 changes: 15 additions & 0 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 @@ -577,6 +584,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
818 changes: 818 additions & 0 deletions runtime/src/bcpack.cpp

Large diffs are not rendered by default.

89 changes: 89 additions & 0 deletions runtime/src/bcpack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// BCPack decoder for the DirectStorage worker: a port of tools/bcpack.py (docs/textures.md, "What BCPack is").
//
// BCPack is the console's entropy coder for BCn texture data: DirectStorage inflates a request (zlib), then decodes
// BCPack into BCn blocks, then swizzles (docs/directstorage.md). This file is the middle step only, portable (no
// windows.h), so it builds and tests on every OS. The lane layer is a line-by-line port of
// tools/bcpack_calibration/src/model.rs (itself a port of `_decode_lanes_raw`); the block rebuilds follow
// tools/bcpack_calibration/src/blocks.rs and `_bc1_blocks` .. `_bc5_blocks`, and `_bc7_check` / `_bc7_rebuild` for
// BC7 (with the structure of DadaCap's prototype port). The tables come from tools/bcpack_tables*.py through
// tools/bcpack_header.py (bcpack_tables.h).
//
// Two policies:
// - strict (the default), as `bcpack.decode`: a block that refers to a built-in history entry nobody has confirmed,
// a BC7 signature whose configuration is only inferred, or a BC7 block that fails its anchor / p-bit consistency
// check refuses the whole chunk. The test vectors hold this decoder to the Python one.
// - lenient, what a title needs: such a block is decoded anyway, from the zero state where the history is unknown
// (what `bcpack.decode(..., allow_provisional=True)` does for BC7, whose built-in history is all unknown), and
// counted in Result::guessed together with every block that predicts from it. The texture then has a few wrong
// blocks instead of missing.
//
// Done: BCPack modes 1 (BC1), 3 (BC3), 4 (BC4), 5 (BC5) and 7 (BC7). Not yet: 6 (BC6H), which returns
// Status::unsupported_codec.
#pragma once

#include <array>
#include <cstddef>
#include <cstdint>
#include <span>
#include <vector>

namespace leonida::bcpack {

enum class Status {
ok,
unsupported_codec, // not a BCPack mode this decoder rebuilds
bad_size, // the output is not a whole number of blocks of the mode
bad_stream, // the lane layer refuses the stream (corrupt, cut short, a missing model, ...)
trailing_data, // non-zero bytes after the last lane finished
lane1_nonzero, // lane 1 carries data (unexpected for BC1..BC5)
bad_symbol, // a symbol outside its lookup table
unconfirmed_history, // strict: a block needs a history entry or signature nobody has confirmed
bad_block, // BC7: a signature outside the table, or (strict) an anchor / p-bit inconsistency
};

const char* status_name(Status status);

// The lane widths of a BCPack mode (1, 3, 4, 5, 6 or 7); empty for another value.
std::span<const std::uint8_t> lane_widths(std::uint32_t mode);
// Bytes per block of a BCPack mode: 8 for BC1 and BC4, 16 for the others; 0 for another value.
std::uint32_t block_bytes(std::uint32_t mode);

// Entropy layer: the symbols of every lane, lane-major (`count` per lane), and the bytes of `in` it used.
struct Lanes {
std::vector<std::uint16_t> symbols;
std::size_t count = 0;
std::size_t used = 0;
std::uint16_t at(std::size_t lane, std::size_t i) const { return symbols[lane * count + i]; }
};
// `count` symbols per lane for `widths`; Status::bad_stream where tools/bcpack.py raises.
Status decode_lanes(std::span<const std::uint8_t> in, std::span<const std::uint8_t> widths, std::size_t count,
Lanes& out);

// Confirmed history entries from the user's calibrated tables (tools/bcpack_history_calib.py), to use besides the
// built-in ones: entry e of mode m at values[m][e * K .. e * K + K) with K = 6, 8, 2, 4 for modes 1, 3, 4, 5; -1 or a
// short vector means "not given". An entry given here wins over the built-in one (`_resolve_bc_history`'s merge).
// BC7's calibrated history is not read yet: its built-in entries are all unknown, so strict decoding refuses a BC7
// chunk that refers to one and lenient decoding takes the zero state.
struct History {
std::array<std::vector<std::int16_t>, 8> values;
};

struct Options {
const History* history = nullptr;
bool lenient = false;
};

struct Result {
Status status = Status::ok;
std::size_t guessed = 0; // lenient: blocks decoded from an unknown history entry, or that predict from one
};

// Decodes one BCPack chunk of `mode` into `out`, whose size is the decoded size (a whole number of blocks). On any
// status but ok, `out` is left in an unspecified state.
Result decode(std::span<const std::uint8_t> in, std::uint32_t mode, std::span<std::uint8_t> out,
const Options& options);
// The strict decode of `in` (Options{history}).
Status decode(std::span<const std::uint8_t> in, std::uint32_t mode, std::span<std::uint8_t> out,
const History* history = nullptr);

} // namespace leonida::bcpack
Loading