diff options
| author | Nico <nicolas.tassin23@gmail.com> | 2026-11-25 12:09:55 +0100 |
|---|---|---|
| committer | Nico <nicolas.tassin23@gmail.com> | 2026-11-25 12:09:55 +0100 |
| commit | 6ee7ef89b948a9942f5de0b61d1f6756f477d6d5 (patch) | |
| tree | 1121387dd0278137397c05d48ecbf6ffc3de4eef /runtime/shims/xapu_queue.cpp | |
| parent | 19559fec600f6e2899a5e96d6a44976ab07f3d65 (diff) | |
| parent | bcaa13dad35f560748bd9b6293cd46504fe288c6 (diff) | |
Merge branch 'main' of https://github.com/ubiquitarianism/leonida into xapu-connect-layouts
main added its own runtime/shims/xapu.cpp: the boot stack's earlier xapu.dll stub, which gta6rt does not link and
xapu_test checks. This branch's xapu.dll command queue moves to runtime/shims/xapu_queue.cpp so both stay: gta6rt,
xapu_chad_test, the cl.exe fallback list in scripts/build_runtime.ps1 and the references in xapu_chad.cpp,
xapu_chad_test.cpp and docs/audio_hal_exe_calls.md name the new file; main's xapu.cpp and xapu_test are unchanged.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GnGkjYHUWx92TkwvwCnAMe
Diffstat (limited to 'runtime/shims/xapu_queue.cpp')
| -rw-r--r-- | runtime/shims/xapu_queue.cpp | 361 |
1 files changed, 361 insertions, 0 deletions
diff --git a/runtime/shims/xapu_queue.cpp b/runtime/shims/xapu_queue.cpp new file mode 100644 index 000000000..3e69ce43c --- /dev/null +++ b/runtime/shims/xapu_queue.cpp | |||
| @@ -0,0 +1,361 @@ | |||
| 1 | // xapu.dll: the console's audio processing unit (APU) command queue. No PC has one, so this keeps the queue the title | ||
| 2 | // talks to - one result per accepted command, in command order, with the console's error codes - and decodes nothing: | ||
| 3 | // an Opus packet's result carries the frame count its table-of-contents byte gives and silence in its output. | ||
| 4 | // | ||
| 5 | // Read from the executable's call sites (counts in docs/runtime_imports.md, shapes in docs/audio_hal_exe_calls.md), | ||
| 6 | // statically, never run: | ||
| 7 | // XApuConnect(input24*, out16*, handle*) 1 site; success is SUCCEEDED(hr). The first qword of the 16-byte | ||
| 8 | // output is read later as the base of a CPU-visible buffer: per-stream | ||
| 9 | // input at base + slot * 0x280 and output at base + 0x14000 + | ||
| 10 | // slot * 0x1E00. The first argument is a 24-byte global of the title | ||
| 11 | // that other title code also addresses. | ||
| 12 | // XApuEnqueueCommand(handle, cmd*, 0) 4 sites; the return value is never read. cmd's first dword is a type | ||
| 13 | // (1, 2 and 3 seen), the second a stream slot. | ||
| 14 | // XApuGetResultCount(handle, 0) 1 site; 0 skips the loop that dequeues. | ||
| 15 | // XApuDequeueResult(handle, out*) 1 site, called count times into a zeroed 0x50-byte local; a negative | ||
| 16 | // HRESULT skips that result. The loop reads a type at +0, a stream slot | ||
| 17 | // at +4 and a dword at +0x34. Its per-slot state machine takes a type 1 | ||
| 18 | // result as "opened" and returns the slot to the title's free list only | ||
| 19 | // on a type 2 result, after the title enqueued a type 2 command. | ||
| 20 | // XApuDisconnect(handle, 0x20) 1 site; the return value is never read. | ||
| 21 | // | ||
| 22 | // Where the layouts come from. The signatures, structure layouts, command kinds, option bits and error codes below | ||
| 23 | // are facts recorded by Marvisak/VIctory (GPL-3.0-or-later, crates/victory-hle/src/modules/apu.rs at commit ffe310e), | ||
| 24 | // whose runtime runs the retail executable through these calls; see docs/related_projects.md. Nothing of its code is | ||
| 25 | // copied; this file is written from those facts. They agree with every shape above: the first argument of | ||
| 26 | // XApuConnect is an input, the dword the title reads at +0x34 of a result is its output byte count, and a type 1 | ||
| 27 | // (activate) or type 2 (deactivate) command gives a result of the same kind for the same stream. | ||
| 28 | // HRESULT XApuConnect(const ConnectInput*, ConnectOutput*, Connection** out) | ||
| 29 | // HRESULT XApuDisconnect(Connection*, uint32_t) the second argument is not read | ||
| 30 | // HRESULT XApuEnqueueCommand(Connection*, const void* command, uint32_t* accepted_so_far) may be null | ||
| 31 | // HRESULT XApuDequeueResult(Connection*, Result* out) 0x8AC81001 while no result is ready | ||
| 32 | // uint32_t XApuGetResultCount(Connection*, uint32_t* dequeued_so_far) may be null; returns the results ready | ||
| 33 | // A connection is for one kind of processing; kind 1 is Opus decoding to 32-bit float PCM at 48 kHz, and any other | ||
| 34 | // kind is E_NOTIMPL. Every command starts with the 24-byte CommandId. A command refused at once (unknown kind, | ||
| 35 | // stream out of range, or the stream already holding `commands_per_stream` results not dequeued) gives no result; | ||
| 36 | // every other command gives one, with its error, if any, in the result. | ||
| 37 | // | ||
| 38 | // What this does not do: decode. A process command's frame count comes from the Opus packet's table-of-contents byte | ||
| 39 | // (RFC 6716, section 3.1), clipped from first_frame, to frame_count and to what output_bytes holds, and that output | ||
| 40 | // range is written with zeros. So the title sees streams advance at the right rate and its error checks pass, and the | ||
| 41 | // audio stays silent until a decoder is wired in at fill_silence(). | ||
| 42 | #include <windows.h> | ||
| 43 | #include <intrin.h> | ||
| 44 | |||
| 45 | #include <cstddef> | ||
| 46 | #include <cstdint> | ||
| 47 | #include <cstdio> | ||
| 48 | #include <cstring> | ||
| 49 | #include <deque> | ||
| 50 | #include <mutex> | ||
| 51 | #include <new> | ||
| 52 | #include <set> | ||
| 53 | #include <string> | ||
| 54 | #include <vector> | ||
| 55 | |||
| 56 | #include "shims.h" | ||
| 57 | #include "startup_trace.h" | ||
| 58 | |||
| 59 | namespace { | ||
| 60 | |||
| 61 | constexpr HRESULT kNotImpl = static_cast<HRESULT>(rt::shims::kENotImpl); | ||
| 62 | constexpr HRESULT kQueueFull = static_cast<HRESULT>(0x8AC81000u); | ||
| 63 | constexpr HRESULT kNoResult = static_cast<HRESULT>(0x8AC81001u); | ||
| 64 | constexpr HRESULT kInvalidCommand = static_cast<HRESULT>(0x8AC81002u); | ||
| 65 | constexpr HRESULT kInvalidStream = static_cast<HRESULT>(0x8AC81003u); | ||
| 66 | constexpr HRESULT kAlreadyActive = static_cast<HRESULT>(0x8AC81005u); | ||
| 67 | constexpr HRESULT kBadPacket = static_cast<HRESULT>(0x8AC81008u); | ||
| 68 | |||
| 69 | constexpr uint32_t kDecodeOpus = 1; // ConnectInput::processing | ||
| 70 | |||
| 71 | constexpr uint32_t kActivate = 1; // CommandId::kind | ||
| 72 | constexpr uint32_t kDeactivate = 2; | ||
| 73 | constexpr uint32_t kProcess = 3; | ||
| 74 | constexpr uint32_t kReset = 5; // 4 is notify; neither carries anything to do while nothing is decoded | ||
| 75 | |||
| 76 | constexpr uint32_t kSignalWhenDone = 0x1; // CommandId::options: set the result event when the result is ready | ||
| 77 | constexpr uint32_t kResultEvent = 0x8; // ConnectInput::options: create an auto-reset event and hand it out | ||
| 78 | // ConnectInput::options 0x4 asks for deinterleaved output; silence is the same zeros either way. | ||
| 79 | |||
| 80 | constexpr uint32_t kMaxStreams = 500; | ||
| 81 | constexpr uint32_t kMaxCommandsPerStream = 16; | ||
| 82 | constexpr uint32_t kMaxFrames = 5760; // 120 ms at 48 kHz, the longest Opus packet (RFC 6716, section 3.2.5) | ||
| 83 | |||
| 84 | #pragma pack(push, 1) | ||
| 85 | struct ConnectInput { | ||
| 86 | uint32_t processing; | ||
| 87 | uint32_t streams; | ||
| 88 | uint32_t commands_per_stream; | ||
| 89 | uint32_t memory_bytes; | ||
| 90 | uint32_t frames_per_output; // 0 for decoding | ||
| 91 | uint32_t options; | ||
| 92 | }; | ||
| 93 | struct ConnectOutput { | ||
| 94 | void* memory; // zeroed, for the title's packets and output | ||
| 95 | HANDLE result_event; | ||
| 96 | }; | ||
| 97 | struct CommandId { | ||
| 98 | uint32_t kind; | ||
| 99 | uint32_t stream; | ||
| 100 | uint32_t sequence; | ||
| 101 | uint32_t options; | ||
| 102 | uint64_t context; | ||
| 103 | }; | ||
| 104 | struct ActivateCommand { | ||
| 105 | CommandId id; | ||
| 106 | uint32_t channels; // 1 or 2 | ||
| 107 | uint32_t init_bytes; | ||
| 108 | const void* init; | ||
| 109 | float start_pitch; | ||
| 110 | uint32_t processing_bytes; | ||
| 111 | void* processing; | ||
| 112 | }; | ||
| 113 | struct ProcessCommand { | ||
| 114 | CommandId id; | ||
| 115 | float pitch_ramp; | ||
| 116 | float target_pitch; | ||
| 117 | uint32_t first_frame; | ||
| 118 | uint32_t frame_count; | ||
| 119 | uint32_t input_bytes; | ||
| 120 | const uint8_t* input; | ||
| 121 | uint32_t output_bytes; | ||
| 122 | uint8_t* output; | ||
| 123 | }; | ||
| 124 | struct Result { | ||
| 125 | CommandId id; | ||
| 126 | uint32_t state; | ||
| 127 | uint64_t state_data; | ||
| 128 | uint64_t usec; | ||
| 129 | HRESULT error; | ||
| 130 | uint32_t frames; | ||
| 131 | uint32_t output_bytes; | ||
| 132 | uint8_t* output; | ||
| 133 | float pitch; | ||
| 134 | uint32_t input_bytes; | ||
| 135 | const uint8_t* input; | ||
| 136 | }; | ||
| 137 | #pragma pack(pop) | ||
| 138 | static_assert(sizeof(ConnectInput) == 24, "six dwords"); | ||
| 139 | static_assert(sizeof(ConnectOutput) == 16, "memory and event"); | ||
| 140 | static_assert(sizeof(CommandId) == 24, "kind, stream, sequence, options, context"); | ||
| 141 | static_assert(sizeof(ActivateCommand) == 56, "CommandId and the activate fields"); | ||
| 142 | static_assert(sizeof(ProcessCommand) == 64, "CommandId and the process fields"); | ||
| 143 | static_assert(sizeof(Result) == 0x50, "the result the title dequeues"); | ||
| 144 | static_assert(offsetof(Result, state) == 0x18 && offsetof(Result, error) == 0x2C && | ||
| 145 | offsetof(Result, output_bytes) == 0x34 && offsetof(Result, pitch) == 0x40 && | ||
| 146 | offsetof(Result, input) == 0x48, | ||
| 147 | "the offsets the title and the layout agree on"); | ||
| 148 | |||
| 149 | struct Stream { | ||
| 150 | bool active = false; | ||
| 151 | uint32_t channels = 0; | ||
| 152 | uint32_t pending = 0; // results queued and not dequeued | ||
| 153 | }; | ||
| 154 | |||
| 155 | struct Connection { | ||
| 156 | void* memory = nullptr; | ||
| 157 | HANDLE event = nullptr; | ||
| 158 | uint32_t commands_per_stream = 0; | ||
| 159 | std::mutex mutex; | ||
| 160 | std::vector<Stream> streams; | ||
| 161 | std::deque<Result> results; | ||
| 162 | uint32_t accepted = 0; | ||
| 163 | uint32_t dequeued = 0; | ||
| 164 | }; | ||
| 165 | |||
| 166 | // gta6rt --trace FILE: the first call of each function and the first XApuEnqueueCommand of each command kind, with | ||
| 167 | // the caller, so the first boot shows what the title sends without a line per call. | ||
| 168 | void note(const char* api, uint64_t value, const void* caller) { | ||
| 169 | if (!rt::startup_trace::enabled()) return; | ||
| 170 | static std::mutex mutex; | ||
| 171 | static std::set<std::pair<std::string, uint64_t>> seen; | ||
| 172 | { | ||
| 173 | const std::lock_guard lock(mutex); | ||
| 174 | if (!seen.insert({api, value}).second) return; | ||
| 175 | } | ||
| 176 | rt::startup_trace::event( | ||
| 177 | "audio_queue", std::string("\"api\":") + rt::startup_trace::quote(api) + | ||
| 178 | ",\"value\":" + rt::startup_trace::quote(rt::startup_trace::hex(value)) + ",\"caller\":" + | ||
| 179 | rt::startup_trace::quote(rt::startup_trace::where(reinterpret_cast<uint64_t>(caller)))); | ||
| 180 | } | ||
| 181 | |||
| 182 | // Samples per channel at 48 kHz in one Opus packet, from its table-of-contents byte and frame count code (RFC 6716, | ||
| 183 | // section 3.1); 0 for a packet that is not valid. | ||
| 184 | uint32_t opus_packet_frames(const uint8_t* packet, uint32_t bytes) { | ||
| 185 | if (!packet || bytes == 0) return 0; | ||
| 186 | static constexpr uint32_t kSilk[4] = {480, 960, 1920, 2880}; // 10, 20, 40, 60 ms | ||
| 187 | static constexpr uint32_t kHybrid[2] = {480, 960}; // 10, 20 ms | ||
| 188 | static constexpr uint32_t kCelt[4] = {120, 240, 480, 960}; // 2.5, 5, 10, 20 ms | ||
| 189 | const uint32_t config = packet[0] >> 3; | ||
| 190 | const uint32_t per_frame = config < 12 ? kSilk[config & 3] : config < 16 ? kHybrid[config & 1] : kCelt[config & 3]; | ||
| 191 | uint32_t count = 0; | ||
| 192 | switch (packet[0] & 3) { | ||
| 193 | case 0: count = 1; break; | ||
| 194 | case 1: | ||
| 195 | case 2: count = 2; break; | ||
| 196 | default: | ||
| 197 | if (bytes < 2) return 0; | ||
| 198 | count = packet[1] & 0x3F; | ||
| 199 | break; | ||
| 200 | } | ||
| 201 | if (count == 0 || count * per_frame > kMaxFrames) return 0; | ||
| 202 | return count * per_frame; | ||
| 203 | } | ||
| 204 | |||
| 205 | // Fills a process command's result. The decoder belongs here: today the frames are counted, not decoded. | ||
| 206 | void fill_silence(const Stream& stream, const ProcessCommand& c, Result& r) { | ||
| 207 | r.input = c.input; | ||
| 208 | r.input_bytes = c.input_bytes; | ||
| 209 | r.output = c.output; | ||
| 210 | if (!c.input || !c.output || c.input_bytes == 0) { | ||
| 211 | r.error = E_INVALIDARG; | ||
| 212 | return; | ||
| 213 | } | ||
| 214 | const uint32_t decoded = opus_packet_frames(c.input, c.input_bytes); | ||
| 215 | if (decoded == 0) { | ||
| 216 | r.error = kBadPacket; | ||
| 217 | return; | ||
| 218 | } | ||
| 219 | const uint32_t first = c.first_frame < decoded ? c.first_frame : decoded; | ||
| 220 | uint32_t frames = decoded - first; | ||
| 221 | if (frames > c.frame_count) frames = c.frame_count; | ||
| 222 | const uint32_t room = c.output_bytes / (4 * stream.channels); | ||
| 223 | if (frames > room) frames = room; | ||
| 224 | const uint32_t bytes = frames * stream.channels * 4; | ||
| 225 | std::memset(c.output, 0, bytes); // interleaved or not, silence is zeros | ||
| 226 | r.frames = frames; | ||
| 227 | r.output_bytes = bytes; | ||
| 228 | } | ||
| 229 | |||
| 230 | HRESULT __stdcall XApuConnect(const ConnectInput* input, ConnectOutput* output, Connection** out) { | ||
| 231 | note("XApuConnect", input ? input->processing : ~0ull, _ReturnAddress()); | ||
| 232 | if (!input || !output || !out) return E_INVALIDARG; | ||
| 233 | *out = nullptr; | ||
| 234 | ConnectInput in; | ||
| 235 | std::memcpy(&in, input, sizeof(in)); | ||
| 236 | if (in.streams == 0 || in.streams > kMaxStreams || in.commands_per_stream == 0 || | ||
| 237 | in.commands_per_stream > kMaxCommandsPerStream || in.memory_bytes == 0) | ||
| 238 | return E_INVALIDARG; | ||
| 239 | if (in.processing != kDecodeOpus) { | ||
| 240 | std::fprintf(stderr, "[rt] xapu: XApuConnect for processing kind %u: only Opus decoding (1); E_NOTIMPL\n", | ||
| 241 | in.processing); | ||
| 242 | return kNotImpl; | ||
| 243 | } | ||
| 244 | if (in.frames_per_output != 0) return E_INVALIDARG; | ||
| 245 | |||
| 246 | auto* c = new (std::nothrow) Connection; // a throwing new would unwind into the title | ||
| 247 | if (!c) return E_OUTOFMEMORY; | ||
| 248 | try { | ||
| 249 | c->streams.resize(in.streams); | ||
| 250 | } catch (...) { | ||
| 251 | delete c; | ||
| 252 | return E_OUTOFMEMORY; | ||
| 253 | } | ||
| 254 | c->memory = VirtualAlloc(nullptr, in.memory_bytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); // zeroed | ||
| 255 | if (!c->memory) { | ||
| 256 | delete c; | ||
| 257 | return E_OUTOFMEMORY; | ||
| 258 | } | ||
| 259 | if (in.options & kResultEvent) { | ||
| 260 | c->event = CreateEventW(nullptr, FALSE, FALSE, nullptr); // auto-reset | ||
| 261 | if (!c->event) { | ||
| 262 | const HRESULT hr = HRESULT_FROM_WIN32(GetLastError()); | ||
| 263 | VirtualFree(c->memory, 0, MEM_RELEASE); | ||
| 264 | delete c; | ||
| 265 | return hr; | ||
| 266 | } | ||
| 267 | } | ||
| 268 | c->commands_per_stream = in.commands_per_stream; | ||
| 269 | const ConnectOutput o{c->memory, c->event}; | ||
| 270 | std::memcpy(output, &o, sizeof(o)); | ||
| 271 | *out = c; | ||
| 272 | std::fprintf(stderr, "[rt] xapu: connected for Opus decoding, %u streams; packets are counted, not decoded\n", | ||
| 273 | in.streams); | ||
| 274 | return S_OK; | ||
| 275 | } | ||
| 276 | |||
| 277 | HRESULT __stdcall XApuDisconnect(Connection* c, uint32_t /*0x20 at the measured site*/) { | ||
| 278 | note("XApuDisconnect", 0, _ReturnAddress()); | ||
| 279 | if (!c) return E_INVALIDARG; | ||
| 280 | if (c->event) CloseHandle(c->event); | ||
| 281 | VirtualFree(c->memory, 0, MEM_RELEASE); | ||
| 282 | delete c; | ||
| 283 | return S_OK; | ||
| 284 | } | ||
| 285 | |||
| 286 | HRESULT __stdcall XApuEnqueueCommand(Connection* c, const void* command, uint32_t* accepted_so_far) { | ||
| 287 | if (!c || !command) return E_INVALIDARG; | ||
| 288 | CommandId id; | ||
| 289 | std::memcpy(&id, command, sizeof(id)); | ||
| 290 | note("XApuEnqueueCommand", id.kind, _ReturnAddress()); | ||
| 291 | if (id.kind < kActivate || id.kind > kReset) return kInvalidCommand; | ||
| 292 | |||
| 293 | const std::lock_guard lock(c->mutex); | ||
| 294 | if (id.stream >= c->streams.size()) return kInvalidStream; | ||
| 295 | Stream& stream = c->streams[id.stream]; | ||
| 296 | if (stream.pending >= c->commands_per_stream) return kQueueFull; | ||
| 297 | |||
| 298 | Result r{}; | ||
| 299 | r.id = id; | ||
| 300 | r.pitch = 1.0f; | ||
| 301 | if (id.kind == kActivate) { | ||
| 302 | ActivateCommand a; | ||
| 303 | std::memcpy(&a, command, sizeof(a)); | ||
| 304 | if (stream.active) { | ||
| 305 | r.error = kAlreadyActive; | ||
| 306 | } else if (a.channels < 1 || a.channels > 2) { | ||
| 307 | r.error = E_INVALIDARG; | ||
| 308 | } else { | ||
| 309 | stream.active = true; | ||
| 310 | stream.channels = a.channels; | ||
| 311 | } | ||
| 312 | } else if (id.kind == kDeactivate) { | ||
| 313 | stream.active = false; | ||
| 314 | } else if (id.kind == kProcess) { | ||
| 315 | ProcessCommand p; | ||
| 316 | std::memcpy(&p, command, sizeof(p)); | ||
| 317 | if (!stream.active) r.error = kInvalidStream; | ||
| 318 | else fill_silence(stream, p, r); | ||
| 319 | } | ||
| 320 | try { | ||
| 321 | c->results.push_back(r); | ||
| 322 | } catch (...) { // out of memory: refuse the command, nothing may unwind into the title | ||
| 323 | return E_OUTOFMEMORY; | ||
| 324 | } | ||
| 325 | ++stream.pending; | ||
| 326 | ++c->accepted; | ||
| 327 | if (accepted_so_far) *accepted_so_far = c->accepted; | ||
| 328 | if ((id.options & kSignalWhenDone) && c->event) SetEvent(c->event); | ||
| 329 | return S_OK; | ||
| 330 | } | ||
| 331 | |||
| 332 | HRESULT __stdcall XApuDequeueResult(Connection* c, Result* out) { | ||
| 333 | note("XApuDequeueResult", 0, _ReturnAddress()); | ||
| 334 | if (!c || !out) return E_INVALIDARG; | ||
| 335 | const std::lock_guard lock(c->mutex); | ||
| 336 | if (c->results.empty()) return kNoResult; | ||
| 337 | const Result r = c->results.front(); | ||
| 338 | c->results.pop_front(); | ||
| 339 | --c->streams[r.id.stream].pending; | ||
| 340 | ++c->dequeued; | ||
| 341 | std::memcpy(out, &r, sizeof(r)); | ||
| 342 | return S_OK; | ||
| 343 | } | ||
| 344 | |||
| 345 | uint32_t __stdcall XApuGetResultCount(Connection* c, uint32_t* dequeued_so_far) { | ||
| 346 | note("XApuGetResultCount", 0, _ReturnAddress()); | ||
| 347 | if (!c) return 0; | ||
| 348 | const std::lock_guard lock(c->mutex); | ||
| 349 | if (dequeued_so_far) *dequeued_so_far = c->dequeued; | ||
| 350 | return static_cast<uint32_t>(c->results.size()); | ||
| 351 | } | ||
| 352 | |||
| 353 | } // namespace | ||
| 354 | |||
| 355 | SHIM_TABLE(xapu, | ||
| 356 | {"xapu.dll", "XApuConnect", reinterpret_cast<void*>(&XApuConnect)}, | ||
| 357 | {"xapu.dll", "XApuDequeueResult", reinterpret_cast<void*>(&XApuDequeueResult)}, | ||
| 358 | {"xapu.dll", "XApuDisconnect", reinterpret_cast<void*>(&XApuDisconnect)}, | ||
| 359 | {"xapu.dll", "XApuEnqueueCommand", reinterpret_cast<void*>(&XApuEnqueueCommand)}, | ||
| 360 | {"xapu.dll", "XApuGetResultCount", reinterpret_cast<void*>(&XApuGetResultCount)}, | ||
| 361 | ); | ||