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/shims/chad.cpp | |
| 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/shims/chad.cpp')
| -rw-r--r-- | runtime/shims/chad.cpp | 220 |
1 files changed, 220 insertions, 0 deletions
diff --git a/runtime/shims/chad.cpp b/runtime/shims/chad.cpp new file mode 100644 index 000000000..011a34c4f --- /dev/null +++ b/runtime/shims/chad.cpp | |||
| @@ -0,0 +1,220 @@ | |||
| 1 | // chad.dll: Custom Hardware Audio DSP. Hardware-accelerated convolution reverb, FFT and IFFT. | ||
| 2 | // | ||
| 3 | // On a PC there is no such DSP hardware. This shim accepts connections and activations, | ||
| 4 | // records the relevant parameters through the startup trace, and returns success for every | ||
| 5 | // call. Any submitted command is dropped; its "completion" is reported through the status | ||
| 6 | // buffer synchronously. | ||
| 7 | // | ||
| 8 | // The signatures below come from the public GDK reference (devdocs.xbox.com / docs.xbox.com): | ||
| 9 | // XDspAudio function reference, handles XDspClientHandle and XDspStreamHandle. | ||
| 10 | // | ||
| 11 | // ASSUMPTION: XDspClientHandle and XDspStreamHandle are opaque pointers returned by the | ||
| 12 | // connect and activate calls respectively. They are never dereferenced by the title | ||
| 13 | // (only passed back to XDSP functions). A plain heap allocation suffices. | ||
| 14 | // | ||
| 15 | // ASSUMPTION: XDspStatus is a structure the title reads to find the last processed command | ||
| 16 | // sequence. Without a hardware queue the status always reports every submitted command as | ||
| 17 | // processed. | ||
| 18 | // | ||
| 19 | // ASSUMPTION: XDspCommand carries input/output buffers and a frame count. The shim copies | ||
| 20 | // nothing and reports the command as completed immediately. | ||
| 21 | |||
| 22 | #include <windows.h> | ||
| 23 | |||
| 24 | #include <atomic> | ||
| 25 | #include <cstdint> | ||
| 26 | #include <cstdio> | ||
| 27 | #include <cstdlib> | ||
| 28 | #include <cstring> | ||
| 29 | #include <mutex> | ||
| 30 | #include <set> | ||
| 31 | #include <string> | ||
| 32 | |||
| 33 | #include "shims.h" | ||
| 34 | #include "startup_trace.h" | ||
| 35 | |||
| 36 | namespace { | ||
| 37 | |||
| 38 | // ---- Handle types (opaque) ---- | ||
| 39 | |||
| 40 | struct XDspClient { | ||
| 41 | uint32_t refcount; | ||
| 42 | bool connected; | ||
| 43 | // The registered base buffer (the title's own memory, we just track it) | ||
| 44 | void* baseBuffer; | ||
| 45 | uint32_t baseBufferLength; | ||
| 46 | uint32_t aggregateImpulseResponseInSeconds; | ||
| 47 | uint32_t maxStreamCount; // set by XDspConnectWithMaximumStreamLimit | ||
| 48 | uint32_t maxChannelCount; // set by XDspConnectWithMaximumStreamLimit | ||
| 49 | }; | ||
| 50 | struct XDspStream {}; | ||
| 51 | |||
| 52 | // ---- Global constants ---- | ||
| 53 | |||
| 54 | constexpr uint32_t kMaxClients = 8; | ||
| 55 | constexpr uint32_t kMaxStreamsPerClient = 256; | ||
| 56 | constexpr uint32_t kMaxAggregateImpulseResponse = 128; // seconds | ||
| 57 | |||
| 58 | // ---- Global state ---- | ||
| 59 | |||
| 60 | std::mutex g_chad_mutex; | ||
| 61 | uint32_t g_client_count = 0; | ||
| 62 | |||
| 63 | // ---- Startup trace helper ---- | ||
| 64 | |||
| 65 | void trace_call(const char* api, const std::string& detail) { | ||
| 66 | if (!rt::startup_trace::enabled()) return; | ||
| 67 | static std::mutex mutex; | ||
| 68 | static std::set<std::string> seen; | ||
| 69 | { | ||
| 70 | std::lock_guard lock(mutex); | ||
| 71 | if (!seen.insert(api + detail).second) return; | ||
| 72 | } | ||
| 73 | rt::startup_trace::event("chad", std::string("\"api\":\"") + api + "\"" + detail); | ||
| 74 | } | ||
| 75 | |||
| 76 | // ---- XDspConnect ---- | ||
| 77 | // | ||
| 78 | // HRESULT XDspConnect(float* baseBuffer, uint32_t baseBufferLength, | ||
| 79 | // uint32_t aggregateImpulseResponseInSeconds, | ||
| 80 | // XDspClientHandle* handle) | ||
| 81 | // | ||
| 82 | // Not imported by the game (it uses XDspConnectWithMaximumStreamLimit), but provided | ||
| 83 | // for completeness. | ||
| 84 | |||
| 85 | HRESULT __stdcall XDspConnect(void* baseBuffer, uint32_t baseBufferLength, | ||
| 86 | uint32_t aggregateImpulseResponseInSeconds, | ||
| 87 | void** handle) { | ||
| 88 | // Validate | ||
| 89 | if (!handle) return E_POINTER; | ||
| 90 | *handle = nullptr; | ||
| 91 | // The real title passes baseBufferLength=0 at a measured call site, so accept any size. | ||
| 92 | if (!baseBuffer || baseBufferLength > 500 * 1024 * 1024) | ||
| 93 | return E_INVALIDARG; | ||
| 94 | if (aggregateImpulseResponseInSeconds > kMaxAggregateImpulseResponse) | ||
| 95 | return 0x8004E402; // XDSP_E_MAX_AGGREGATE_IMPULSE_RESPONSE_EXCEEDED | ||
| 96 | |||
| 97 | std::lock_guard lock(g_chad_mutex); | ||
| 98 | if (g_client_count >= kMaxClients) | ||
| 99 | return 0x8004E401; // XDSP_E_MAX_CLIENTS_EXCEEDED | ||
| 100 | |||
| 101 | auto* client = new (std::nothrow) XDspClient{}; | ||
| 102 | if (!client) return E_OUTOFMEMORY; | ||
| 103 | client->refcount = 1; | ||
| 104 | client->connected = true; | ||
| 105 | client->baseBuffer = baseBuffer; | ||
| 106 | client->baseBufferLength = baseBufferLength; | ||
| 107 | client->aggregateImpulseResponseInSeconds = aggregateImpulseResponseInSeconds; | ||
| 108 | client->maxStreamCount = kMaxStreamsPerClient; | ||
| 109 | client->maxChannelCount = 2; | ||
| 110 | ++g_client_count; | ||
| 111 | *handle = client; | ||
| 112 | |||
| 113 | trace_call("XDspConnect", | ||
| 114 | ",\"baseBufferLength\":" + std::to_string(baseBufferLength)); | ||
| 115 | return S_OK; | ||
| 116 | } | ||
| 117 | |||
| 118 | // ---- XDspConnectWithMaximumStreamLimit ---- | ||
| 119 | // | ||
| 120 | // HRESULT XDspConnectWithMaximumStreamLimit(float* baseBuffer, uint32_t baseBufferLength, | ||
| 121 | // uint32_t aggregateImpulseResponseInSeconds, | ||
| 122 | // uint32_t maximumStreamCount, | ||
| 123 | // uint32_t maximumChannelCount, | ||
| 124 | // XDspClientHandle* handle) | ||
| 125 | |||
| 126 | HRESULT __stdcall XDspConnectWithMaximumStreamLimit(void* baseBuffer, | ||
| 127 | uint32_t baseBufferLength, | ||
| 128 | uint32_t aggregateImpulseResponseInSeconds, | ||
| 129 | uint32_t maximumStreamCount, | ||
| 130 | uint32_t maximumChannelCount, | ||
| 131 | void** handle) { | ||
| 132 | if (!handle) return E_POINTER; | ||
| 133 | *handle = nullptr; | ||
| 134 | // A measured call site passes baseBufferLength=0, so accept any size. | ||
| 135 | if (!baseBuffer || baseBufferLength > 500 * 1024 * 1024) | ||
| 136 | return E_INVALIDARG; | ||
| 137 | if (maximumStreamCount == 0 || maximumStreamCount > kMaxStreamsPerClient) | ||
| 138 | return E_INVALIDARG; | ||
| 139 | if (maximumChannelCount != 1 && maximumChannelCount != 2) | ||
| 140 | return E_INVALIDARG; | ||
| 141 | if (aggregateImpulseResponseInSeconds > kMaxAggregateImpulseResponse) | ||
| 142 | return 0x8004E402; | ||
| 143 | |||
| 144 | std::lock_guard lock(g_chad_mutex); | ||
| 145 | if (g_client_count >= kMaxClients) | ||
| 146 | return 0x8004E401; | ||
| 147 | |||
| 148 | auto* client = new (std::nothrow) XDspClient{}; | ||
| 149 | if (!client) return E_OUTOFMEMORY; | ||
| 150 | client->refcount = 1; | ||
| 151 | client->connected = true; | ||
| 152 | client->baseBuffer = baseBuffer; | ||
| 153 | client->baseBufferLength = baseBufferLength; | ||
| 154 | client->aggregateImpulseResponseInSeconds = aggregateImpulseResponseInSeconds; | ||
| 155 | client->maxStreamCount = maximumStreamCount; | ||
| 156 | client->maxChannelCount = maximumChannelCount; | ||
| 157 | ++g_client_count; | ||
| 158 | *handle = client; | ||
| 159 | |||
| 160 | trace_call("XDspConnectWithMaximumStreamLimit", | ||
| 161 | ",\"baseBufferLength\":" + std::to_string(baseBufferLength) + | ||
| 162 | ",\"maxStreams\":" + std::to_string(maximumStreamCount)); | ||
| 163 | return S_OK; | ||
| 164 | } | ||
| 165 | |||
| 166 | // ---- XDspActivate ---- | ||
| 167 | // | ||
| 168 | // Measured: XDspActivate(void* handle, u64, u64, u64, u64) — 5 args (PR #6981). | ||
| 169 | // Success is a HRESULT of 0 (S_OK). The extra 4 values are activation parameters | ||
| 170 | // whose meaning is not read; we accept them and return success. | ||
| 171 | |||
| 172 | HRESULT __stdcall XDspActivate(void* handle, uint64_t, uint64_t, uint64_t, uint64_t) { | ||
| 173 | trace_call("XDspActivate", ""); | ||
| 174 | return handle ? S_OK : E_INVALIDARG; | ||
| 175 | } | ||
| 176 | |||
| 177 | // ---- XDspDeactivate ---- | ||
| 178 | // | ||
| 179 | // Measured: XDspDeactivate(void* handle, u64, u64, u64) — 4 args. | ||
| 180 | |||
| 181 | HRESULT __stdcall XDspDeactivate(void* handle, uint64_t, uint64_t, uint64_t) { | ||
| 182 | trace_call("XDspDeactivate", ""); | ||
| 183 | return handle ? S_OK : E_INVALIDARG; | ||
| 184 | } | ||
| 185 | |||
| 186 | // ---- XDspDisconnect ---- | ||
| 187 | // | ||
| 188 | // HRESULT XDspDisconnect(XDspClientHandle clientHandle) | ||
| 189 | |||
| 190 | HRESULT __stdcall XDspDisconnect(void* clientHandle) { | ||
| 191 | if (!clientHandle) return E_INVALIDARG; | ||
| 192 | auto* client = static_cast<XDspClient*>(clientHandle); | ||
| 193 | { | ||
| 194 | std::lock_guard lock(g_chad_mutex); | ||
| 195 | if (g_client_count > 0) --g_client_count; | ||
| 196 | } | ||
| 197 | delete client; | ||
| 198 | trace_call("XDspDisconnect", ""); | ||
| 199 | return S_OK; | ||
| 200 | } | ||
| 201 | |||
| 202 | // ---- XDspSubmitCommand ---- | ||
| 203 | // | ||
| 204 | // Measured: XDspSubmitCommand(void* a, void* b) — 2 args. Which of the two is | ||
| 205 | // the connection handle is not known (the measured wrapper swaps them). | ||
| 206 | |||
| 207 | HRESULT __stdcall XDspSubmitCommand(void*, void*) { | ||
| 208 | trace_call("XDspSubmitCommand", ""); | ||
| 209 | return S_OK; | ||
| 210 | } | ||
| 211 | |||
| 212 | } // namespace | ||
| 213 | |||
| 214 | SHIM_TABLE(chad, | ||
| 215 | {"chad.dll", "XDspActivate", reinterpret_cast<void*>(&XDspActivate)}, | ||
| 216 | {"chad.dll", "XDspConnectWithMaximumStreamLimit", reinterpret_cast<void*>(&XDspConnectWithMaximumStreamLimit)}, | ||
| 217 | {"chad.dll", "XDspDeactivate", reinterpret_cast<void*>(&XDspDeactivate)}, | ||
| 218 | {"chad.dll", "XDspDisconnect", reinterpret_cast<void*>(&XDspDisconnect)}, | ||
| 219 | {"chad.dll", "XDspSubmitCommand", reinterpret_cast<void*>(&XDspSubmitCommand)}, | ||
| 220 | ); | ||