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
8 changes: 7 additions & 1 deletion docs/directstorage.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,13 @@ 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 and BC5 rebuilds, strict like `bcpack.decode`, with
the user's calibrated history as an optional `History`. `bcpack_test` checks it against the Python decoder on
synthetic chunks (`runtime/test/bcpack_vectors.py`). Modes 6 and 7 (BC6H, BC7) return `unsupported_codec` until
they are 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
556 changes: 556 additions & 0 deletions runtime/src/bcpack.cpp

Large diffs are not rendered by default.

64 changes: 64 additions & 0 deletions runtime/src/bcpack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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`. Strict like `bcpack.decode` by default:
// a block that refers to a built-in history entry that is not confirmed is refused, never guessed. The tables come
// from tools/bcpack_tables.py through tools/bcpack_header.py (bcpack_tables.h).
//
// Done: BCPack modes 1 (BC1), 3 (BC3), 4 (BC4) and 5 (BC5). Not yet: 6 (BC6H) and 7 (BC7), which return
// 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, // a block refers to a built-in history entry that is not confirmed
};

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).
struct History {
std::array<std::vector<std::int16_t>, 8> values;
};

// 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.
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