upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/shims/chad.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/shims/chad.cpp')
-rw-r--r--runtime/shims/chad.cpp220
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
36namespace {
37
38// ---- Handle types (opaque) ----
39
40struct 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};
50struct XDspStream {};
51
52// ---- Global constants ----
53
54constexpr uint32_t kMaxClients = 8;
55constexpr uint32_t kMaxStreamsPerClient = 256;
56constexpr uint32_t kMaxAggregateImpulseResponse = 128; // seconds
57
58// ---- Global state ----
59
60std::mutex g_chad_mutex;
61uint32_t g_client_count = 0;
62
63// ---- Startup trace helper ----
64
65void 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
85HRESULT __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
126HRESULT __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
172HRESULT __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
181HRESULT __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
190HRESULT __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
207HRESULT __stdcall XDspSubmitCommand(void*, void*) {
208 trace_call("XDspSubmitCommand", "");
209 return S_OK;
210}
211
212} // namespace
213
214SHIM_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);