diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-24 20:26:00 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-24 20:26:00 -0500 |
| commit | 154e4ec034902deffa4b89b2cbca9a949f38baec (patch) | |
| tree | 1b2ef5656de42c172291bc6009efa4c510650815 /runtime/src/bcpack_stream.h | |
| parent | eb6a08cd359d98b5777e70d54d0ae8a75879495a (diff) | |
| parent | 8a242efebf823aeb23c72d17c3d666daf0bbd3e2 (diff) | |
Merge the #984 result (main + RT-PROTON-STORY-LOAD) into #939
The base is 8a242efeb, the merge of main and #984. 68 files conflicted; they were resolved in seven groups by
one rule: the same change in two forms keeps the base's form, everything only #939 has is kept, and where the
two solve one problem in two ways the base's is the frame and #939's plugs into it.
A, the Xbox proxy (d3d12_xs_device.cpp/.h, d3d12_xs.cpp, d3d12_xs_pso_stream.cpp/.h, d3d12_xs_alloc_size.h,
d3d12_xs_desc_clean.h, device_proxy_test.cpp, queue_proxy_test.cpp): the base's files with #939's additions
ported in. Device slot 57 asks #939's NativeGraphicsFactory first and falls back to the base's
proxy_create_graphics_pipeline_from_native_code on E_NOTIMPL. Kept from #939: the providers (texture lease,
raw texture view, resource by address, constant view, descriptor mapping, depth-stencil UAV, texture pages),
the title-backed descriptor mirror, slot 13's feature answers, GPU labels on list slots 71/72. Dropped: #939's
duplicates of the boot ladder routes, its fallback window, its slot 133 stand-in.
A2 (xs_proxy_mock_test.cpp): the base's test with #939's slot 70 raw-buffer case; presentation cases follow
#939's rule that a device which ever had a handler has no queue fallback.
B1, the Vulkan device (d3d12_vulkan_device.cpp, d3d12_com.cpp/.h, shader_library.h, queue_families.h,
pipeline_cache_file.h, startup_trace.h): one lazy-pipeline mechanism, the base's; #939's pending_base_compile
and first_use_backend_pipelines dropped, prepare_on_bind and cold_graphics_compile kept. create_texture keeps
the base's signature; #939's deferred backing is folded into the base's placed-memory binder. #939's quad
lists as triangle indices.
B2, the D3D12 tests (d3d12_existing_heap, a8_dimensions, pipeline_cache_seed, title_va, rect_list,
shader_library, loader_tls_index0, draw, compute, xmem_placed, shader_library_corpus): the base's tests where
both carry the same case, #939's cases added (rebound tables, linear texture map, large vertex batch,
executable shaders, default A8 views).
C, the backend (vulkan_replay.h, vulkan_backend.h, vulkan_bindless.h, vulkan_scratch.h, vulkan_present.h,
rect_list_geometry.h, vk_test_device.h, vk_pipeline_check.cpp, vk_store_test.cpp, bindless_sizing_test.cpp,
replay_texture_barrier_test.cpp): the base's prepare_draw and lazy compute, its #1055 release rule and its
rectangle generator; #939's rectangle variants, GPU quad index expansion, 1D/3D and depth-stencil UAVs, the
label fence on the base's submit.
D, memory, services, presentation (xmem.cpp/.h, xg_xs.cpp, xg_xs_test.cpp, title_va_alias.h and its test,
main.cpp, frame_pacer.h, xbox_present.cpp/.h; xgameruntime_offline.cpp for one comment): the base's xmem with
#939's label hooks, file-backed sections and pin_mapped_data; both texture computers in xg_xs.cpp, the base's
answering first; --first-run-ui kept without host speech in its profile.
E, build, tools, docs (runtime/CMakeLists.txt, make_game.py, tools/rdna2spirv.py, tools/shader_disasm.py,
scripts, tests, docs; docs/README.md and tests/test_exe_shaders_stage.py beyond the conflicts): the base's
v_dot4 decode, #281's disassembler form and tests/semantic/test_fspx_dot4_op_sel_disasm.py dropped; the
base's executable-shader step with #939's cache fix; 366 registered tests.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/bcpack_stream.h')
| -rw-r--r-- | runtime/src/bcpack_stream.h | 222 |
1 files changed, 222 insertions, 0 deletions
diff --git a/runtime/src/bcpack_stream.h b/runtime/src/bcpack_stream.h new file mode 100644 index 000000000..787686929 --- /dev/null +++ b/runtime/src/bcpack_stream.h | |||
| @@ -0,0 +1,222 @@ | |||
| 1 | // Shared rANS block stream used by the runtime decoder and the offline history fitter. | ||
| 2 | #pragma once | ||
| 3 | #include <algorithm> | ||
| 4 | #include <array> | ||
| 5 | #include <bit> | ||
| 6 | #include <cstdint> | ||
| 7 | #include <span> | ||
| 8 | |||
| 9 | namespace leonida::bcpack::detail { | ||
| 10 | using namespace std; | ||
| 11 | struct Seed { | ||
| 12 | int16_t values[8]; | ||
| 13 | bool fitted; | ||
| 14 | }; | ||
| 15 | struct BC6Mode { | ||
| 16 | int code, transformed, base, delta[3], regions, first, count; | ||
| 17 | }; | ||
| 18 | #include "bcpack_tables.inc" | ||
| 19 | |||
| 20 | using Symbols = array<uint16_t, 16>; | ||
| 21 | constexpr uint32_t home = 1u << 24; | ||
| 22 | |||
| 23 | struct Model { | ||
| 24 | uint32_t base[11]{}; | ||
| 25 | const uint32_t *frequency{}; | ||
| 26 | int width{}; | ||
| 27 | array<uint8_t, 256> first{}; | ||
| 28 | uint64_t reciprocal[9]{}; | ||
| 29 | void init(int w, int q) { | ||
| 30 | width = w; | ||
| 31 | frequency = freq[w][q]; | ||
| 32 | for (int p = 1; p <= w; ++p) { | ||
| 33 | base[p] = base[p - 1] + (frequency[p - 1] << max(0, p - 2)); | ||
| 34 | reciprocal[p] = ((uint64_t{1} << 32) + frequency[p] - 1) / frequency[p]; | ||
| 35 | } | ||
| 36 | unsigned p = 0; | ||
| 37 | for (unsigned window = 0; window < first.size(); ++window) { | ||
| 38 | while (p < unsigned(width) && base[p + 1] <= (window << 8)) ++p; | ||
| 39 | first[window] = uint8_t(p); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | int symbol(uint32_t state, bool fast) const { | ||
| 43 | const uint32_t cf = state & 65535; | ||
| 44 | if (fast) { | ||
| 45 | unsigned p = first[cf >> 8]; | ||
| 46 | while (p < unsigned(width) && cf >= base[p + 1]) ++p; | ||
| 47 | if (!p) return 0; | ||
| 48 | const auto quotient = uint32_t(((cf - base[p]) * reciprocal[p]) >> 32); | ||
| 49 | return int((1u << (p - 1)) + quotient); | ||
| 50 | } | ||
| 51 | int p = width; | ||
| 52 | while (p && cf < base[p]) --p; | ||
| 53 | return p ? (1 << (p - 1)) + (cf - base[p]) / frequency[p] : 0; | ||
| 54 | } | ||
| 55 | uint32_t advance(uint32_t state, unsigned sym) const { | ||
| 56 | const int p = bit_width(sym); | ||
| 57 | uint32_t f = frequency[p], | ||
| 58 | c = base[p] + f * (sym - (p ? 1u << (p - 1) : 0u)); | ||
| 59 | if (!c) { | ||
| 60 | --f; | ||
| 61 | ++c; | ||
| 62 | } | ||
| 63 | return f * (state >> 16) + (state & 65535) - c; | ||
| 64 | } | ||
| 65 | }; | ||
| 66 | |||
| 67 | // VIctory's GPL-3.0 BCPack decoder uses the high slot byte to locate a bucket. | ||
| 68 | // Build this accelerator solely from Leonida's own rANS frequencies. | ||
| 69 | inline const Model& model_for(int w, int q) { | ||
| 70 | static const auto models = [] { | ||
| 71 | array<array<Model, 16>, 8> result{}; | ||
| 72 | for (int width = 1; width <= 8; ++width) | ||
| 73 | for (int quality = 0; quality < 16; ++quality) | ||
| 74 | result[width - 1][quality].init(width, quality); | ||
| 75 | return result; | ||
| 76 | }(); | ||
| 77 | return models[w - 1][q]; | ||
| 78 | } | ||
| 79 | |||
| 80 | enum class Phase { header, length, long_length, symbols }; | ||
| 81 | struct Lane { | ||
| 82 | Phase phase{Phase::header}; | ||
| 83 | const Model* model{}; | ||
| 84 | uint32_t state{}; | ||
| 85 | int bytes{}, byte{}, zeros{}, symbol{}, slot{}; | ||
| 86 | size_t remaining{}; | ||
| 87 | bool flush{}, extended{}, buffered{}, held{}, full{}, done{}; | ||
| 88 | |||
| 89 | bool step(int width, span<const uint8_t> &input, int &budget, bool fast) { | ||
| 90 | if (done || (held && full)) | ||
| 91 | return true; | ||
| 92 | if (!buffered && (phase != Phase::symbols || bytes)) { | ||
| 93 | if (!budget) | ||
| 94 | return true; | ||
| 95 | if (input.empty()) | ||
| 96 | return false; | ||
| 97 | --budget; | ||
| 98 | byte = input.front(); | ||
| 99 | input = input.subspan(1); | ||
| 100 | buffered = true; | ||
| 101 | } | ||
| 102 | if (held) { | ||
| 103 | state = model->advance(state, symbol); | ||
| 104 | slot = symbol; | ||
| 105 | full = true; | ||
| 106 | held = false; | ||
| 107 | } | ||
| 108 | if (phase == Phase::header) { | ||
| 109 | model = &model_for(width - (byte >> 6), byte & 15); | ||
| 110 | flush = (byte & 16) != 0; | ||
| 111 | extended = (byte & 32) != 0; | ||
| 112 | zeros = 0; | ||
| 113 | buffered = false; | ||
| 114 | phase = Phase::length; | ||
| 115 | } else if (phase == Phase::length || phase == Phase::long_length) { | ||
| 116 | if (phase == Phase::long_length) { | ||
| 117 | bytes += (byte << 8) + 257; | ||
| 118 | phase = Phase::symbols; | ||
| 119 | } else { | ||
| 120 | bytes = byte + !extended; | ||
| 121 | phase = extended ? Phase::long_length : Phase::symbols; | ||
| 122 | } | ||
| 123 | buffered = false; | ||
| 124 | } else { | ||
| 125 | if (state < home) { | ||
| 126 | if (bytes) { | ||
| 127 | state = (state << 8) | byte; | ||
| 128 | buffered = false; | ||
| 129 | --bytes; | ||
| 130 | } else if (!flush) { | ||
| 131 | done = remaining == 0; | ||
| 132 | phase = Phase::header; | ||
| 133 | return true; | ||
| 134 | } | ||
| 135 | if (!bytes && flush && state < home) { | ||
| 136 | if (!state || zeros >= 8) | ||
| 137 | return false; | ||
| 138 | while (state < home && zeros < 8) { | ||
| 139 | state <<= 8; | ||
| 140 | ++zeros; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | } | ||
| 144 | held = state > home && remaining; | ||
| 145 | if (held) { | ||
| 146 | symbol = model->symbol(state, fast); | ||
| 147 | if (symbol >= (1 << model->width)) | ||
| 148 | return false; | ||
| 149 | --remaining; | ||
| 150 | } | ||
| 151 | if (!bytes && flush && state == home) { | ||
| 152 | done = !remaining; | ||
| 153 | phase = Phase::header; | ||
| 154 | state = 0; | ||
| 155 | } else if (!bytes && !flush && !remaining && !held) { | ||
| 156 | done = true; | ||
| 157 | } else if (state == home || (state > home && !held)) { | ||
| 158 | return false; | ||
| 159 | } | ||
| 160 | } | ||
| 161 | return true; | ||
| 162 | } | ||
| 163 | }; | ||
| 164 | |||
| 165 | inline int unzig(int value) { return value & 1 ? -((value + 1) >> 1) : value >> 1; } | ||
| 166 | inline int interleave(int a, int b, int bits = 4) { | ||
| 167 | int v = 0; | ||
| 168 | for (int k = 0; k < bits; ++k) | ||
| 169 | v |= (((a >> k) & 1) | (((b >> k) & 1) << 1)) << (2 * k); | ||
| 170 | return v; | ||
| 171 | } | ||
| 172 | |||
| 173 | struct BC7Format { | ||
| 174 | int subsets, color, alpha, pbits, index, partition; | ||
| 175 | }; | ||
| 176 | constexpr BC7Format bc7_formats[] = {{3, 4, 0, 1, 3, 4}, {2, 6, 0, 2, 3, 6}, | ||
| 177 | {3, 5, 0, 0, 2, 6}, {2, 7, 0, 1, 2, 6}, | ||
| 178 | {1, 5, 6, 0, 3, 0}, {1, 7, 8, 0, 2, 0}, | ||
| 179 | {1, 7, 7, 1, 4, 0}, {2, 5, 5, 1, 2, 6}}; | ||
| 180 | |||
| 181 | |||
| 182 | template<class Block> | ||
| 183 | bool visit_blocks(unsigned codec, span<const uint8_t> source, size_t size, Block block, bool fast = true) { | ||
| 184 | const bool single = codec == 1 || codec == 4; | ||
| 185 | const unsigned count = codec >= 6 ? 16 : 12; | ||
| 186 | const size_t elements = size / 8; | ||
| 187 | array<Lane, 16> lanes{}; | ||
| 188 | for (auto &lane : lanes) lane.remaining = elements; | ||
| 189 | Symbols first{}; | ||
| 190 | size_t emitted = 0; | ||
| 191 | while (elements) { | ||
| 192 | bool ready = true, done = true; | ||
| 193 | int budget = 8; | ||
| 194 | for (unsigned l = 0; l < count; ++l) { | ||
| 195 | auto &lane = lanes[l]; | ||
| 196 | if (!lane.step(widths[codec][l], source, budget, fast)) | ||
| 197 | return false; | ||
| 198 | ready &= lane.full; | ||
| 199 | done &= lane.done; | ||
| 200 | } | ||
| 201 | if (ready) { | ||
| 202 | if (emitted == elements) | ||
| 203 | return false; | ||
| 204 | Symbols tuple{}; | ||
| 205 | for (unsigned l = 0; l < count; ++l) { | ||
| 206 | tuple[l] = uint16_t(lanes[l].slot); | ||
| 207 | lanes[l].full = false; | ||
| 208 | } | ||
| 209 | if (single || emitted % 2) { | ||
| 210 | const size_t i = single ? emitted : emitted / 2; | ||
| 211 | if (!block(i, single ? tuple : first, tuple)) return false; | ||
| 212 | } else { | ||
| 213 | first = tuple; | ||
| 214 | } | ||
| 215 | ++emitted; | ||
| 216 | } | ||
| 217 | if (done) | ||
| 218 | break; | ||
| 219 | } | ||
| 220 | return emitted == elements && ranges::all_of(source, [](uint8_t v) { return v == 0; }); | ||
| 221 | } | ||
| 222 | } // namespace leonida::bcpack::detail | ||