forked from TheElixZammuto/moonlight-xbox
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpch.h
More file actions
254 lines (223 loc) · 6.4 KB
/
pch.h
File metadata and controls
254 lines (223 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#pragma once
// Use the C++ standard templated min/max
#define NOMINMAX
#include <wrl.h>
#include <wrl/client.h>
#include <dxgi1_6.h>
#include <dxgi1_4.h>
#include <dxgi1_3.h>
#include <d3d11_4.h>
#include <d3d11_3.h>
#include <d2d1_3.h>
#include <d2d1effects_2.h>
#include <dwrite_3.h>
#include <wincodec.h>
#include <DirectXColors.h>
#include <DirectXMath.h>
#include <algorithm>
#include <memory>
#include <mutex>
#include <string>
#include <cctype>
#include <agile.h>
#include <concrt.h>
#include <ppltasks.h>
#include <collection.h>
#include <gamingdeviceinformation.h>
#include "App.xaml.h"
#define IMGUI_USER_CONFIG "Common\imconfig.moonlight.h"
#include <imgui.h>
#include <imgui_impl_uwp.h>
#include <imgui_impl_dx11.h>
#ifdef _DEBUG
#include <dxgidebug.h>
#endif
// Helper for dispatching code to the UI thread
#define DISPATCH_UI(LAMBDA) \
Windows::ApplicationModel::Core::CoreApplication::MainView->CoreWindow \
->Dispatcher \
->RunAsync( \
Windows::UI::Core::CoreDispatcherPriority::Normal, \
ref new Windows::UI::Core::DispatchedHandler(LAMBDA) \
)
// Helper for dispatching code to the threadpool
#define DISPATCH_THREADPOOL(LAMBDA) \
Windows::System::Threading::ThreadPool::RunAsync( \
ref new Windows::System::Threading::WorkItemHandler([=](Windows::Foundation::IAsyncAction^) { LAMBDA(); }) \
)
// Time helpers
static inline int64_t QpcFreq() {
static int64_t f = [] {
LARGE_INTEGER li{};
QueryPerformanceFrequency(&li);
return li.QuadPart;
}();
return f;
}
static inline int64_t QpcNow() {
LARGE_INTEGER li{};
QueryPerformanceCounter(&li);
return li.QuadPart;
}
static inline int64_t UsToQpc(int64_t us) {
const int64_t f = QpcFreq();
return (us / INT64_C(1000000)) * f +
(us % INT64_C(1000000)) * f / INT64_C(1000000);
}
static inline int64_t QpcToUs(int64_t qpc) {
const int64_t f = QpcFreq();
int64_t q = qpc / f;
int64_t r = qpc % f;
if (r < 0) {
--q;
r += f;
}
return q * INT64_C(1000000) + (r * INT64_C(1000000)) / f;
}
static inline double QpcToMsD(double qpc) {
return qpc * 1000.0 / (double)QpcFreq();
}
static inline double QpcToMs(int64_t qpc) {
return QpcToMsD(static_cast<double>(qpc));
}
static inline int64_t MsToQpc(double ms) {
const double us_d = ms * 1000.0;
const int64_t us = static_cast<int64_t>(us_d >= 0.0 ? us_d + 0.5 : us_d - 0.5);
return UsToQpc(us);
}
// Log something only once, safe to use in hot areas of the code
#define CONCAT(a, b) CONCAT2(a, b)
#define CONCAT2(a, b) a##b
#define LogOnce(fmt, ...) \
do { \
static std::once_flag CONCAT(_onceFlag_, __LINE__); \
std::call_once(CONCAT(_onceFlag_, __LINE__), [&] { \
Utils::Logf(fmt, ##__VA_ARGS__); \
}); \
} while (0)
// Frame queue debugging, uncomment FRAME_QUEUE_VERBOSE
// Note: When FQLog is enabled, the spam is intense, so it only logs data for a short time
#if !defined(NDEBUG)
//#define FRAME_QUEUE_VERBOSE
#endif
#ifdef FRAME_QUEUE_VERBOSE
#define FQLog(fmt, ...) \
moonlight_xbox_dx::Utils::Logf("[%lu] " fmt, ::GetCurrentThreadId(), ##__VA_ARGS__)
#else
# ifdef FRAME_QUEUE_VERBOSE_LIMITED
#include <atomic>
static std::atomic<int> g_fqlog_counter{0};
#define FQLog(fmt, ...) \
if (++g_fqlog_counter > 200 && g_fqlog_counter < 1000) \
moonlight_xbox_dx::Utils::Logf("[%lu] " fmt, ::GetCurrentThreadId(), ##__VA_ARGS__)
# else
#if defined(_MSC_VER)
#define FQLog(...) __noop
#else
#define FQLog(fmt, ...) do {} while(0)
#endif
# endif
#endif
// Xbox helpers
// Any Xbox
static inline bool IsXbox()
{
static bool f = []{
GAMING_DEVICE_MODEL_INFORMATION info;
if (FAILED(GetGamingDeviceModelInformation(&info))) {
return false;
}
if (info.vendorId == GAMING_DEVICE_VENDOR_ID_MICROSOFT) {
return true;
}
return false;
}();
return f;
}
// Xbox Series S/X
static inline bool IsXboxSeries()
{
static bool f = []{
GAMING_DEVICE_MODEL_INFORMATION info;
if (FAILED(GetGamingDeviceModelInformation(&info))) {
return false;
}
if (info.vendorId == GAMING_DEVICE_VENDOR_ID_MICROSOFT) {
if ( info.deviceId == GAMING_DEVICE_DEVICE_ID_XBOX_SERIES_S
|| info.deviceId == GAMING_DEVICE_DEVICE_ID_XBOX_SERIES_X
|| info.deviceId == GAMING_DEVICE_DEVICE_ID_XBOX_SERIES_X_DEVKIT
) {
return true;
}
}
return false;
}();
return f;
}
// Xbox One/One X
static inline bool IsXboxOne()
{
static bool f = []{
GAMING_DEVICE_MODEL_INFORMATION info;
if (FAILED(GetGamingDeviceModelInformation(&info))) {
return false;
}
if (info.vendorId == GAMING_DEVICE_VENDOR_ID_MICROSOFT) {
if ( info.deviceId == GAMING_DEVICE_DEVICE_ID_XBOX_ONE
|| info.deviceId == GAMING_DEVICE_DEVICE_ID_XBOX_ONE_S
|| info.deviceId == GAMING_DEVICE_DEVICE_ID_XBOX_ONE_X
|| info.deviceId == GAMING_DEVICE_DEVICE_ID_XBOX_ONE_X_DEVKIT
) {
return true;
}
}
return false;
}();
return f;
}
// Xbox VCR/One S
static inline bool IsXboxOneVCR()
{
static bool f = []{
GAMING_DEVICE_MODEL_INFORMATION info;
if (FAILED(GetGamingDeviceModelInformation(&info))) {
return false;
}
if (info.vendorId == GAMING_DEVICE_VENDOR_ID_MICROSOFT) {
if (info.deviceId == GAMING_DEVICE_DEVICE_ID_XBOX_ONE) {
return true;
}
}
return false;
}();
return f;
}
// Sleep until approximately targetQpc, then busy-wait to land precisely.
// sleepSlackUs: how early (in microseconds) to stop sleeping and start spinning
static inline void SleepUntilQpc(int64_t targetQpc, int64_t sleepSlackUs = 1000) {
const int64_t f = QpcFreq();
const int64_t slack = UsToQpc(sleepSlackUs);
for (;;) {
const int64_t now = QpcNow();
const int64_t remaining = targetQpc - now;
if (remaining <= 0) break;
if (remaining > slack) {
DWORD ms = (DWORD)(((remaining - slack) * 1000 + f / 2) / f);
if (ms > 0) Sleep(ms);
continue;
}
YieldProcessor();
}
}
static inline const char *LiGetFormattedStageName(int status) {
const char *stageName = ::LiGetStageName(status);
if (!stageName) {
return nullptr;
}
thread_local std::string s;
s = stageName;
if (!s.empty()) {
s[0] = static_cast<char>(std::toupper(static_cast<unsigned char>(s[0])));
}
return s.c_str();
}