This repository was archived by the owner on May 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathmain.cpp
More file actions
3566 lines (3399 loc) · 127 KB
/
Copy pathmain.cpp
File metadata and controls
3566 lines (3399 loc) · 127 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2022-2024, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <windows.h>
#include "version.h"
#include "module_processing.h"
#include "remix_api.h"
#include "util_bridge_assert.h"
#include "util_circularbuffer.h"
#include "util_commands.h"
#include "util_common.h"
#include "util_devicecommand.h"
#include "util_filesys.h"
#include "util_guid.h"
#include "util_hack_d3d_debug.h"
#include "util_messagechannel.h"
#include "util_modulecommand.h"
#include "util_process.h"
#include "util_remixapi.h"
#include "util_seh.h"
#include "util_semaphore.h"
#include "util_sharedheap.h"
#include "util_sharedmemory.h"
#include "util_texture_and_volume.h"
#include "util_version.h"
#include "log/log.h"
#include "config/config.h"
#include "config/global_options.h"
#include "server_options.h"
#include "../client/client_options.h"
#include "../tracy/tracy.hpp"
#include <iostream>
#include <d3d9.h>
#include <assert.h>
#include <map>
#include <atomic>
#include <array>
using namespace Commands;
using namespace bridge_util;
using namespace remixapi::util;
// NOTE: This extension is really useful for debugging the Bridge child process from the parent process:
// https://marketplace.visualstudio.com/items?itemName=vsdbgplat.MicrosoftChildProcessDebuggingPowerTool
#define SEND_OPTIONAL_SERVER_RESPONSE(hresult, uid) { \
if (GlobalOptions::getSendAllServerResponses()) { \
ServerMessage c(Commands::Bridge_Response, uid); \
c.send_data(hresult); \
} \
}
#define SEND_OPTIONAL_CREATE_FUNCTION_SERVER_RESPONSE(hresult, uid) { \
if (GlobalOptions::getSendCreateFunctionServerResponses() || GlobalOptions::getSendAllServerResponses()) { \
ServerMessage c(Commands::Bridge_Response, uid); \
c.send_data(hresult); \
} \
}
#define PULL(type, name) const auto& name = (type)DeviceBridge::get_data()
#define PULL_I(name) PULL(INT, name)
#define PULL_U(name) PULL(UINT, name)
#define PULL_D(name) PULL(DWORD, name)
#define PULL_H(name) PULL(HRESULT, name)
#define PULL_HND(name) \
PULL_U(name); \
assert(name != NULL)
#define PULL_DATA(size, name) \
uint32_t name##_len = DeviceBridge::get_data((void**)&name); \
assert(name##_len == 0 || size == name##_len)
#define PULL_OBJ(type, name) \
type* name = nullptr; \
PULL_DATA(sizeof(type), name)
#define CHECK_DATA_OFFSET (DeviceBridge::get_data_pos() == rpcHeader.dataOffset)
#define GET_HND(name) \
const auto& name = rpcHeader.pHandle; \
assert(name != NULL)
#define GET_HDR_VAL(name) \
const DWORD& name = rpcHeader.pHandle;
#define GET_RES(name, map) \
GET_HND(name##Handle); \
const auto& name = map[name##Handle]; \
assert(name != NULL)
// NOTE: MSDN states HWNDs are safe to cross x86-->x64 boundary, and that a truncating cast should be used:
// https://docs.microsoft.com/en-us/windows/win32/winprog64/interprocess-communication?redirectedfrom=MSDN
#define TRUNCATE_HANDLE(type, input) (type)(size_t)(input)
bool bDxvkModuleLoaded = false;
std::chrono::steady_clock::time_point gTimeStart;
// Shared memory and IPC channels
Guid gUniqueIdentifier;
NamedSemaphore* gpPresent = nullptr;
std::unique_ptr<MessageChannelServer> gpClientMessageChannel;
// D3D Library handle
typedef IDirect3D9* (WINAPI* D3DC9)(UINT);
typedef HRESULT(WINAPI* D3DC9Ex)(UINT, IDirect3D9Ex**);
HMODULE ghModule;
LPDIRECT3D9 gpD3D;
bool gOverwriteConditionAlreadyActive = false;
// Mapping between client and server pointer addresses
std::unordered_map<uint32_t, IDirect3DDevice9*> gpD3DDevices;
std::unordered_map<uint32_t, IDirect3DResource9*> gpD3DResources; // For Textures, Buffers, and Surfaces
std::unordered_map<uint32_t, IDirect3DVolume9*> gpD3DVolumes;
std::unordered_map<uint32_t, IDirect3DVertexDeclaration9*> gpD3DVertexDeclarations;
std::unordered_map<uint32_t, IDirect3DStateBlock9*> gpD3DStateBlocks;
std::unordered_map<uint32_t, IDirect3DVertexShader9*> gpD3DVertexShaders;
std::unordered_map<uint32_t, IDirect3DPixelShader9*> gpD3DPixelShaders;
std::unordered_map<uint32_t, IDirect3DSwapChain9*> gpD3DSwapChains;
std::unordered_map<uint32_t, IDirect3DQuery9*> gpD3DQuery;
std::unordered_map<uint32_t, void*> gMapRemixApi;
// Global state
bool gbBridgeRunning = true;
HANDLE hWait;
namespace {
template<typename SerializableT>
static void deserializeFromQueue(SerializableT& serializableT) {
static_assert(is_serializable_v<SerializableT>, "deserializeRemixApiT(...) may only be called with defined Serializable<T> types");
void* pSlzdData = nullptr;
const auto size = DeviceBridge::get_data(&pSlzdData);
SerializableT dslz(pSlzdData);
assert(size == dslz.size());
dslz.deserialize();
serializableT = std::move(dslz);
}
}
static inline void safeDestroy(IUnknown* obj, uint32_t x86handle) {
// Note: in DXVK the refcounts of non-standalone objects may go negative!
// We need to handle such objects appropriately, even though this is not
// the case in regular system D3D9.
#if defined(_DEBUG) && defined(VERBOSE)
if (obj) {
const LONG cnt = static_cast<LONG>(obj->Release());
if (cnt > 0) {
Logger::trace(format_string("Object [%p/%lx] refcount at destroy is %d > 1.",
obj, x86handle, cnt + 1));
}
}
#endif
while (obj && static_cast<LONG>(obj->Release()) > 0);
}
D3DPRESENT_PARAMETERS getPresParamFromRaw(const uint32_t* rawPresentationParameters) {
D3DPRESENT_PARAMETERS presParam;
// Set up presentation parameters. We can't just directly cast the structure because the hDeviceWindow
// handle is 4 bytes in the data coming in but 8 bytes in the x64 version of the struct.
presParam.BackBufferWidth = *reinterpret_cast<const UINT*>(rawPresentationParameters);
presParam.BackBufferHeight = *reinterpret_cast<const UINT*>(rawPresentationParameters + 1);
presParam.BackBufferFormat = *reinterpret_cast<const D3DFORMAT*>(rawPresentationParameters + 2);
presParam.BackBufferCount = *reinterpret_cast<const UINT*>(rawPresentationParameters + 3);
presParam.MultiSampleType = *reinterpret_cast<const D3DMULTISAMPLE_TYPE*>(rawPresentationParameters + 4);
presParam.MultiSampleQuality = *reinterpret_cast<const DWORD*>(rawPresentationParameters + 5);
presParam.SwapEffect = *reinterpret_cast<const D3DSWAPEFFECT*>(rawPresentationParameters + 6);
presParam.hDeviceWindow = *reinterpret_cast<const HWND*>(rawPresentationParameters + 7);
presParam.Windowed = *reinterpret_cast<const BOOL*>(rawPresentationParameters + 8);
presParam.EnableAutoDepthStencil = *reinterpret_cast<const BOOL*>(rawPresentationParameters + 9);
presParam.AutoDepthStencilFormat = *reinterpret_cast<const D3DFORMAT*>(rawPresentationParameters + 10);
presParam.Flags = *reinterpret_cast<const DWORD*>(rawPresentationParameters + 11);
presParam.FullScreen_RefreshRateInHz = *reinterpret_cast<const UINT*>(rawPresentationParameters + 12);
presParam.PresentationInterval = (UINT) * reinterpret_cast<const UINT*>(rawPresentationParameters + 13);
return presParam;
}
HRESULT ReturnSurfaceDataToClient(IDirect3DSurface9* pReturnSurfaceData, HRESULT hresult, UINT currentUID) {
// We send the HRESULT response back to the client even in case of failure
ServerMessage c(Commands::Bridge_Response, currentUID);
if (!SUCCEEDED(hresult)) {
c.send_data(hresult);
return hresult;
}
// Using surface desc to get width, height of the surface
D3DSURFACE_DESC pDesc;
hresult = pReturnSurfaceData->GetDesc(OUT & pDesc);
if (!SUCCEEDED(hresult)) {
c.send_data(hresult);
return hresult;
}
uint32_t width = pDesc.Width;
uint32_t height = pDesc.Height;
D3DFORMAT format = pDesc.Format;
// Obtaining raw buffer from the surface and we send this data to client
D3DLOCKED_RECT lockedRect;
hresult = pReturnSurfaceData->LockRect(OUT & lockedRect, NULL, IN D3DLOCK_READONLY);
if (!SUCCEEDED(hresult)) {
c.send_data(hresult);
return hresult;
}
// Sending raw surface buffer details to client
const uint32_t totalSize = bridge_util::calcTotalSizeOfRect(width, height, format);
const uint32_t rowSize = bridge_util::calcRowSize(width, format);
c.send_data(hresult);
c.send_data(width);
c.send_data(height);
c.send_data(format);
if (auto* blobPacketPtr = c.begin_data_blob(totalSize)) {
FOR_EACH_RECT_ROW(lockedRect, height, format, {
memcpy(blobPacketPtr, ptr, rowSize);
blobPacketPtr += rowSize;
});
c.end_data_blob();
}
hresult = pReturnSurfaceData->UnlockRect();
return hresult;
}
template<typename T>
static bool dumpLeakedObjects(const char* name, const T& map) {
if (!map.empty()) {
bridge_util::Logger::err(format_string("%zd objects discovered in %s map at "
"Direct3D module eviction:", map.size(), name));
for (auto& [handle, obj] : map) {
bridge_util::Logger::err(format_string("\t%x -> %p", handle, obj));
}
return true;
}
return false;
}
static bool dumpLeakedObjects() {
bool anyLeaked = false;
anyLeaked |= dumpLeakedObjects("Resource", gpD3DResources);
anyLeaked |= dumpLeakedObjects("Vertex Declaration", gpD3DVertexDeclarations);
anyLeaked |= dumpLeakedObjects("State Block", gpD3DStateBlocks);
anyLeaked |= dumpLeakedObjects("Vertex Shader", gpD3DVertexShaders);
anyLeaked |= dumpLeakedObjects("Pixel Shader", gpD3DPixelShaders);
anyLeaked |= dumpLeakedObjects("Swapchain", gpD3DSwapChains);
anyLeaked |= dumpLeakedObjects("Volume", gpD3DVolumes);
anyLeaked |= dumpLeakedObjects("Device", gpD3DDevices);
return anyLeaked;
}
void ProcessDeviceCommandQueue() {
// Loop until the client sends terminate instruction
bool done = false;
while (!done && DeviceBridge::waitForCommand() == Result::Success) {
ZoneScopedN("Process Command");
#ifdef LOG_SERVER_COMMAND_TIME
// Take a snapshot of the current tick count for profiling purposes
const auto start = GetTickCount64();
#endif
const Header rpcHeader = DeviceBridge::pop_front();
#ifdef _DEBUG
// If data batching is enabled and the data offset on the comamnd is different from
// our current offset we know there must be data to read, so we start a data batch
// read operation on the data queue buffer.
if (!CHECK_DATA_OFFSET) {
const auto result = DeviceBridge::begin_read_data();
assert(RESULT_SUCCESS(result));
}
#endif
{
ZoneScoped;
if (ZoneIsActive) {
const std::string commandStr = toString(rpcHeader.command);
ZoneName(commandStr.c_str(), commandStr.size());
}
PULL_U(currentUID);
#if defined(_DEBUG) || defined(DEBUGOPT)
if (GlobalOptions::getLogServerCommands()) {
Logger::info("Device Processing: " + toString(rpcHeader.command) + " UID: " + std::to_string(currentUID));
}
#endif
// The mother of all switch statements - every call in the D3D9 interface is mapped here...
switch (rpcHeader.command) {
case IDirect3D9Ex_CreateDeviceEx:
{
GET_HND(pHandle);
PULL_U(Adapter);
PULL(D3DDEVTYPE, DeviceType);
PULL(uint32_t, hFocusWindow);
PULL_D(BehaviorFlags);
D3DDISPLAYMODEEX* pFullscreenDisplayMode = nullptr;
PULL_DATA(sizeof(D3DDISPLAYMODEEX), pFullscreenDisplayMode);
uint32_t* rawPresentationParameters = nullptr;
DeviceBridge::get_data((void**) &rawPresentationParameters);
D3DPRESENT_PARAMETERS PresentationParameters = getPresParamFromRaw(rawPresentationParameters);
IDirect3DDevice9Ex* pD3DDevice = nullptr;
const auto hresult = ((IDirect3D9Ex*) gpD3D)->CreateDeviceEx(IN Adapter, IN DeviceType, IN TRUNCATE_HANDLE(HWND, hFocusWindow), IN BehaviorFlags, IN OUT & PresentationParameters, IN pFullscreenDisplayMode, OUT & pD3DDevice);
if (!SUCCEEDED(hresult)) {
std::stringstream ss;
ss << format_string("CreateDeviceEx() call failed with error code 0x%x", hresult) << std::endl;
Logger::err(ss.str());
} else {
Logger::info("Server side D3D9 DeviceEx created successfully!");
gpD3DDevices[pHandle] = pD3DDevice;
if(GlobalOptions::getExposeRemixApi()) {
remixapi::g_device = pD3DDevice;
remixapi::g_remix.dxvk_RegisterD3D9Device(remixapi::g_device);
}
}
// Send response back to the client
Logger::debug("Sending CreateDevice ack response back to client.");
{
ServerMessage c(Commands::Bridge_Response, currentUID);
c.send_data(hresult);
}
break;
}
case IDirect3D9Ex_CreateDevice:
{
GET_HND(pHandle);
PULL_U(Adapter);
PULL(D3DDEVTYPE, DeviceType);
PULL(uint32_t, hFocusWindow);
PULL_D(BehaviorFlags);
uint32_t* rawPresentationParameters = nullptr;
DeviceBridge::get_data((void**) &rawPresentationParameters);
D3DPRESENT_PARAMETERS PresentationParameters = getPresParamFromRaw(rawPresentationParameters);
IDirect3DDevice9* pD3DDevice = nullptr;
const auto hresult = gpD3D->CreateDevice(IN Adapter, IN DeviceType, IN TRUNCATE_HANDLE(HWND, hFocusWindow), IN BehaviorFlags, IN OUT & PresentationParameters, OUT & pD3DDevice);
if (!SUCCEEDED(hresult)) {
std::stringstream ss;
ss << format_string("CreateDevice() call failed with error code 0x%x", hresult) << std::endl;
Logger::err(ss.str());
} else {
Logger::info("Server side D3D9 Device created successfully!");
gpD3DDevices[pHandle] = (IDirect3DDevice9Ex*) pD3DDevice;
if(GlobalOptions::getExposeRemixApi()) {
remixapi::g_device = (IDirect3DDevice9Ex*) pD3DDevice;
remixapi::g_remix.dxvk_RegisterD3D9Device(remixapi::g_device);
}
}
// Send response back to the client
Logger::debug("Sending CreateDevice ack response back to client.");
{
ServerMessage c(Commands::Bridge_Response, currentUID);
c.send_data(hresult);
}
break;
}
case IDirect3DDevice9Ex_GetDisplayModeEx:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL_U(iSwapChain);
D3DDISPLAYMODEEX pMode;
D3DDISPLAYROTATION pRotation;
HRESULT hresult = ((IDirect3DDevice9Ex*) pD3DDevice)->GetDisplayModeEx(iSwapChain, &pMode, &pRotation);
{
ServerMessage c(Commands::Bridge_Response, currentUID);
c.send_data(hresult);
if (SUCCEEDED(hresult)) {
c.send_data(sizeof(D3DDISPLAYMODEEX), &pMode);
c.send_data(sizeof(D3DDISPLAYROTATION), &pRotation);
}
}
break;
}
case IDirect3DDevice9Ex_CreateRenderTargetEx:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL_U(Width);
PULL_U(Height);
PULL(D3DFORMAT, Format);
PULL(D3DMULTISAMPLE_TYPE, MultiSample);
PULL_D(MultisampleQuality);
PULL(BOOL, Lockable);
PULL(DWORD, Usage);
PULL_HND(pHandle);
LPDIRECT3DSURFACE9 pSurface;
const auto hresult = ((IDirect3DDevice9Ex*) pD3DDevice)->CreateRenderTargetEx(IN Width, IN Height, IN Format, IN MultiSample, IN MultisampleQuality, IN Lockable, OUT & pSurface, IN nullptr, IN Usage);
if (SUCCEEDED(hresult)) {
gpD3DResources[pHandle] = pSurface;
}
assert(SUCCEEDED(hresult));
SEND_OPTIONAL_CREATE_FUNCTION_SERVER_RESPONSE(hresult, currentUID);
break;
}
case IDirect3DDevice9Ex_CreateOffscreenPlainSurfaceEx:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL_U(Width);
PULL_U(Height);
PULL(D3DFORMAT, Format);
PULL(D3DPOOL, Pool);
PULL(DWORD, Usage);
PULL_HND(pHandle);
LPDIRECT3DSURFACE9 pSurface;
const auto hresult = ((IDirect3DDevice9Ex*) pD3DDevice)->CreateOffscreenPlainSurfaceEx(IN Width, IN Height, IN Format, IN Pool, OUT & pSurface, IN nullptr, IN Usage);
if (SUCCEEDED(hresult)) {
gpD3DResources[pHandle] = pSurface;
}
assert(SUCCEEDED(hresult));
SEND_OPTIONAL_CREATE_FUNCTION_SERVER_RESPONSE(hresult, currentUID);
break;
}
case IDirect3DDevice9Ex_CreateDepthStencilSurfaceEx:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL_U(Width);
PULL_U(Height);
PULL(D3DFORMAT, Format);
PULL(D3DMULTISAMPLE_TYPE, MultiSample);
PULL_D(MultisampleQuality);
PULL(BOOL, Discard);
PULL(DWORD, Usage);
PULL_HND(pHandle);
LPDIRECT3DSURFACE9 pSurface;
const auto hresult = ((IDirect3DDevice9Ex*) pD3DDevice)->CreateDepthStencilSurfaceEx(IN Width, IN Height, IN Format, IN MultiSample, IN MultisampleQuality, IN Discard, OUT & pSurface, IN nullptr, IN Usage);
if (SUCCEEDED(hresult)) {
gpD3DResources[pHandle] = pSurface;
}
assert(SUCCEEDED(hresult));
SEND_OPTIONAL_CREATE_FUNCTION_SERVER_RESPONSE(hresult, currentUID);
break;
}
/*
* IDirect3DDevice9 interface
*/
case IDirect3DDevice9Ex_LinkSwapchain:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL_HND(pClientSwapchain);
IDirect3DSwapChain9* pSwapChain = nullptr;
const auto hresult = pD3DDevice->GetSwapChain(0, &pSwapChain);
if (SUCCEEDED(hresult)) {
gpD3DSwapChains[pClientSwapchain] = pSwapChain;
}
break;
}
case IDirect3DDevice9Ex_LinkBackBuffer:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL(uint32_t, index);
PULL_HND(pSurfaceHandle);
IDirect3DSurface9* pBackbuffer = nullptr;
const auto hresult = pD3DDevice->GetBackBuffer(0, index, D3DBACKBUFFER_TYPE_MONO, &pBackbuffer);
if (SUCCEEDED(hresult)) {
gpD3DResources[pSurfaceHandle] = pBackbuffer;
}
assert(SUCCEEDED(hresult));
break;
}
case IDirect3DDevice9Ex_LinkAutoDepthStencil:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL_HND(pSurfaceHandle);
IDirect3DSurface9* pDepthStencil = nullptr;
const auto hresult = pD3DDevice->GetDepthStencilSurface(&pDepthStencil);
if (SUCCEEDED(hresult)) {
gpD3DResources[pSurfaceHandle] = pDepthStencil;
}
assert(SUCCEEDED(hresult));
break;
}
case IDirect3DDevice9Ex_QueryInterface:
break;
case IDirect3DDevice9Ex_AddRef:
break;
case IDirect3DDevice9Ex_Destroy:
{
GET_RES(pD3DDevice, gpD3DDevices);
safeDestroy(pD3DDevice, pD3DDeviceHandle);
gpD3DDevices.erase(pD3DDeviceHandle);
break;
}
case IDirect3DDevice9Ex_TestCooperativeLevel:
{
GET_RES(pD3DDevice, gpD3DDevices);
const auto hresult = pD3DDevice->TestCooperativeLevel();
assert(SUCCEEDED(hresult));
break;
}
case IDirect3DDevice9Ex_GetAvailableTextureMem:
{
GET_RES(pD3DDevice, gpD3DDevices);
const auto mem = pD3DDevice->GetAvailableTextureMem();
{
ServerMessage c(Commands::Bridge_Response, currentUID);
c.send_data(mem);
}
break;
}
case IDirect3DDevice9Ex_EvictManagedResources:
{
GET_RES(pD3DDevice, gpD3DDevices);
auto const hresult = pD3DDevice->EvictManagedResources();
assert(SUCCEEDED(hresult));
SEND_OPTIONAL_SERVER_RESPONSE(hresult, currentUID);
break;
}
case IDirect3DDevice9Ex_GetDirect3D:
{
GET_RES(pD3DDevice, gpD3DDevices);
IDirect3D9* pD3D = nullptr;
const auto hresult = pD3DDevice->GetDirect3D(OUT & pD3D);
assert(SUCCEEDED(hresult));
assert(gpD3D == pD3D); // The two pointers should be identical
break;
}
case IDirect3DDevice9Ex_GetDeviceCaps:
{
GET_RES(pD3DDevice, gpD3DDevices);
D3DCAPS9 pCaps;
const auto hresult = pD3DDevice->GetDeviceCaps(OUT & pCaps);
BRIDGE_ASSERT_LOG(SUCCEEDED(hresult), "Issue retrieving D3D9 device specific information");
{
ServerMessage c(Commands::Bridge_Response, currentUID);
c.send_data(hresult);
if (SUCCEEDED(hresult)) {
c.send_data(sizeof(D3DCAPS9), &pCaps);
}
}
break;
}
case IDirect3DDevice9Ex_GetDisplayMode:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL_U(iSwapChain);
D3DDISPLAYMODE pMode;
const auto hresult = pD3DDevice->GetDisplayMode(IN iSwapChain, OUT & pMode);
BRIDGE_ASSERT_LOG(SUCCEEDED(hresult), "Issue retrieving information about D3D9 display mode of the adapter");
{
ServerMessage c(Commands::Bridge_Response, currentUID);
c.send_data(hresult);
if (SUCCEEDED(hresult)) {
c.send_data(sizeof(D3DDISPLAYMODE), &pMode);
}
}
break;
}
case IDirect3DDevice9Ex_GetCreationParameters:
break;
case IDirect3DDevice9Ex_SetCursorProperties:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL(UINT, XHotSpot);
PULL(UINT, YHotSpot);
PULL_U(pHandle);
IDirect3DSurface9* pCursorBitmap = nullptr;
if (pHandle != NULL) {
pCursorBitmap = (IDirect3DSurface9*) gpD3DResources[pHandle];
}
const auto hresult = pD3DDevice->SetCursorProperties(XHotSpot, YHotSpot, pCursorBitmap);
SEND_OPTIONAL_SERVER_RESPONSE(hresult, currentUID);
break;
}
case IDirect3DDevice9Ex_SetCursorPosition:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL(int, X);
PULL(int, Y);
PULL(DWORD, Flags);
pD3DDevice->SetCursorPosition(X, Y, Flags);
break;
}
case IDirect3DDevice9Ex_ShowCursor:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL(BOOL, bShow);
const BOOL prevShow = pD3DDevice->ShowCursor(bShow);
{
ServerMessage c(Commands::Bridge_Response, currentUID);
c.send_data(prevShow);
}
break;
}
case IDirect3DDevice9Ex_CreateAdditionalSwapChain:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL_HND(pHandle);
uint32_t* rawPresentationParameters = nullptr;
DeviceBridge::get_data((void**) &rawPresentationParameters);
D3DPRESENT_PARAMETERS PresentationParameters = getPresParamFromRaw(rawPresentationParameters);
IDirect3DSwapChain9* pSwapChain = nullptr;
const auto hresult = pD3DDevice->CreateAdditionalSwapChain(&PresentationParameters, &pSwapChain);
if (SUCCEEDED(hresult)) {
gpD3DSwapChains[pHandle] = pSwapChain;
}
SEND_OPTIONAL_CREATE_FUNCTION_SERVER_RESPONSE(hresult, currentUID);
break;
}
case IDirect3DDevice9Ex_GetSwapChain:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL_U(iSwapChain);
IDirect3DSwapChain9* pSwapChain = nullptr;
const auto hresult = pD3DDevice->GetSwapChain(iSwapChain, &pSwapChain);
assert(SUCCEEDED(hresult));
assert(pSwapChain != nullptr);
break;
}
case IDirect3DDevice9Ex_GetNumberOfSwapChains:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL_U(orig_cnt);
const auto cnt = pD3DDevice->GetNumberOfSwapChains();
assert(orig_cnt == cnt);
break;
}
case IDirect3DDevice9Ex_Reset:
{
GET_RES(pD3DDevice, gpD3DDevices);
uint32_t* rawPresentationParameters = nullptr;
DeviceBridge::get_data((void**) &rawPresentationParameters);
D3DPRESENT_PARAMETERS PresentationParameters = getPresParamFromRaw(rawPresentationParameters);
if (!PresentationParameters.Windowed && !bDxvkModuleLoaded) {
bridge_util::Logger::err("Fullscreen is not yet supported for non-DXVK uses of the bridge. This is not recoverable. Exiting.");
done = true;
}
//Release implicit swapchain
IDirect3DSwapChain9* pSwapChain = nullptr;
pD3DDevice->GetSwapChain(0, &pSwapChain);
pSwapChain->Release();
const auto hresult = pD3DDevice->Reset(&PresentationParameters);
assert(SUCCEEDED(hresult));
SEND_OPTIONAL_SERVER_RESPONSE(hresult, currentUID);
break;
}
case IDirect3DDevice9Ex_ResetEx:
{
GET_RES(pD3DDevice, gpD3DDevices);
uint32_t* rawPresentationParameters = nullptr;
DeviceBridge::get_data((void**) &rawPresentationParameters);
D3DDISPLAYMODEEX* pFullscreenDisplayMode = nullptr;
PULL_DATA(sizeof(D3DDISPLAYMODEEX), pFullscreenDisplayMode);
D3DPRESENT_PARAMETERS PresentationParameters = getPresParamFromRaw(rawPresentationParameters);
if (!PresentationParameters.Windowed && !bDxvkModuleLoaded) {
bridge_util::Logger::err("Fullscreen is not yet supported for non-DXVK uses of the bridge. This is not recoverable. Exiting.");
done = true;
}
//Release implicit swapchain
IDirect3DSwapChain9* pSwapChain = nullptr;
pD3DDevice->GetSwapChain(0, &pSwapChain);
pSwapChain->Release();
const auto hresult = ((IDirect3DDevice9Ex*) pD3DDevice)->ResetEx(&PresentationParameters, pFullscreenDisplayMode);
assert(SUCCEEDED(hresult));
SEND_OPTIONAL_SERVER_RESPONSE(hresult, currentUID);
break;
}
case IDirect3DDevice9Ex_Present:
{
FrameMark;
#ifdef ENABLE_PRESENT_SEMAPHORE_TRACE
Logger::trace("Server side Present call received, releasing semaphore...");
#endif
GET_RES(pD3DDevice, gpD3DDevices);
PULL_OBJ(RECT, pSourceRect);
PULL_OBJ(RECT, pDestRect);
PULL(uint32_t, hDestWindowOverride);
PULL_OBJ(RGNDATA, pDirtyRegion);
HWND hwnd = TRUNCATE_HANDLE(HWND, hDestWindowOverride);
const auto hresult = pD3DDevice->Present(pSourceRect, pDestRect, hwnd, pDirtyRegion);
if (!SUCCEEDED(hresult)) {
std::stringstream ss;
ss << "Present() failed! Check all logs for reported errors.";
Logger::err(ss.str());
}
// If we're syncing with the client on Present() then trigger the semaphore now
if (GlobalOptions::getPresentSemaphoreEnabled()) {
gpPresent->release();
#ifdef ENABLE_PRESENT_SEMAPHORE_TRACE
Logger::trace("Present semaphore released successfully.");
#endif
}
break;
}
case IDirect3DDevice9Ex_GetBackBuffer:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL(uint32_t, iSwapChain);
PULL(uint32_t, iBackBuffer);
PULL_HND(pSurfaceHandle);
IDirect3DSurface9* pBackbuffer = nullptr;
const auto hresult = pD3DDevice->GetBackBuffer(iSwapChain, iBackBuffer, D3DBACKBUFFER_TYPE_MONO, &pBackbuffer);
assert(SUCCEEDED(hresult));
if (SUCCEEDED(hresult)) {
gpD3DResources[pSurfaceHandle] = pBackbuffer;
}
break;
}
case IDirect3DDevice9Ex_GetRasterStatus:
break;
case IDirect3DDevice9Ex_SetDialogBoxMode:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL(BOOL, bEnableDialogs);
const auto hresult = pD3DDevice->SetDialogBoxMode(bEnableDialogs);
SEND_OPTIONAL_SERVER_RESPONSE(hresult, currentUID);
assert(SUCCEEDED(hresult));
break;
}
case IDirect3DDevice9Ex_SetGammaRamp:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL_U(iSwapChain);
PULL_D(Flags);
PULL_OBJ(D3DGAMMARAMP, pRamp);
pD3DDevice->SetGammaRamp(iSwapChain, Flags, pRamp);
break;
}
case IDirect3DDevice9Ex_GetGammaRamp:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL_U(iSwapChain);
D3DGAMMARAMP pRamp;
pD3DDevice->GetGammaRamp(iSwapChain, &pRamp);
break;
}
case IDirect3DDevice9Ex_CreateTexture:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL_U(Width);
PULL_U(Height);
PULL_U(Levels);
PULL_D(Usage);
PULL(D3DFORMAT, Format);
PULL(D3DPOOL, Pool);
PULL_HND(pHandle);
LPDIRECT3DTEXTURE9 pTexture;
const auto hresult = pD3DDevice->CreateTexture(IN Width, IN Height, IN Levels, IN Usage, IN Format, IN Pool, OUT & pTexture, IN nullptr);
if (SUCCEEDED(hresult)) {
gpD3DResources[pHandle] = pTexture;
}
assert(SUCCEEDED(hresult));
SEND_OPTIONAL_CREATE_FUNCTION_SERVER_RESPONSE(hresult, currentUID);
break;
}
case IDirect3DDevice9Ex_CreateVolumeTexture:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL_U(Width);
PULL_U(Height);
PULL_U(Depth);
PULL_U(Levels);
PULL_D(Usage);
PULL(D3DFORMAT, Format);
PULL(D3DPOOL, Pool);
PULL_HND(pHandle);
LPDIRECT3DVOLUMETEXTURE9 pVolumeTexture;
const auto hresult = pD3DDevice->CreateVolumeTexture(IN Width, IN Height, IN Depth, IN Levels, IN Usage, IN Format, IN Pool, OUT & pVolumeTexture, IN nullptr);
if (SUCCEEDED(hresult)) {
gpD3DResources[pHandle] = pVolumeTexture;
}
assert(SUCCEEDED(hresult));
SEND_OPTIONAL_CREATE_FUNCTION_SERVER_RESPONSE(hresult, currentUID);
break;
}
case IDirect3DDevice9Ex_CreateCubeTexture:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL_U(EdgeLength);
PULL_U(Levels);
PULL_D(Usage);
PULL(D3DFORMAT, Format);
PULL(D3DPOOL, Pool);
PULL_HND(pHandle);
LPDIRECT3DCUBETEXTURE9 pCubeTexture;
const auto hresult = pD3DDevice->CreateCubeTexture(IN EdgeLength, IN Levels, IN Usage, IN Format, IN Pool, OUT & pCubeTexture, IN nullptr);
if (SUCCEEDED(hresult)) {
gpD3DResources[pHandle] = pCubeTexture;
}
assert(SUCCEEDED(hresult));
SEND_OPTIONAL_CREATE_FUNCTION_SERVER_RESPONSE(hresult, currentUID);
break;
}
case IDirect3DDevice9Ex_CreateVertexBuffer:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL_U(Length);
PULL_D(Usage);
PULL_D(FVF);
PULL(D3DPOOL, Pool);
PULL_HND(pHandle);
LPDIRECT3DVERTEXBUFFER9 pVertexBuffer;
const auto hresult = pD3DDevice->CreateVertexBuffer(IN Length, IN Usage, IN FVF, IN Pool, OUT & pVertexBuffer, IN nullptr);
if (SUCCEEDED(hresult)) {
gpD3DResources[pHandle] = pVertexBuffer;
}
assert(SUCCEEDED(hresult));
SEND_OPTIONAL_CREATE_FUNCTION_SERVER_RESPONSE(hresult, currentUID);
break;
}
case IDirect3DDevice9Ex_CreateIndexBuffer:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL_U(Length);
PULL_D(Usage);
PULL(D3DFORMAT, Format);
PULL(D3DPOOL, Pool);
PULL_HND(pHandle);
LPDIRECT3DINDEXBUFFER9 pIndexBuffer;
const auto hresult = pD3DDevice->CreateIndexBuffer(IN Length, IN Usage, IN Format, IN Pool, OUT & pIndexBuffer, IN nullptr);
if (SUCCEEDED(hresult)) {
gpD3DResources[pHandle] = pIndexBuffer;
}
assert(SUCCEEDED(hresult));
SEND_OPTIONAL_CREATE_FUNCTION_SERVER_RESPONSE(hresult, currentUID);
break;
}
case IDirect3DDevice9Ex_CreateRenderTarget:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL_U(Width);
PULL_U(Height);
PULL(D3DFORMAT, Format);
PULL(D3DMULTISAMPLE_TYPE, MultiSample);
PULL_D(MultisampleQuality);
PULL(BOOL, Lockable);
PULL_HND(pHandle);
LPDIRECT3DSURFACE9 pSurface;
const auto hresult = pD3DDevice->CreateRenderTarget(IN Width, IN Height, IN Format, IN MultiSample, IN MultisampleQuality, IN Lockable, OUT & pSurface, IN nullptr);
if (SUCCEEDED(hresult)) {
gpD3DResources[pHandle] = pSurface;
}
assert(SUCCEEDED(hresult));
SEND_OPTIONAL_CREATE_FUNCTION_SERVER_RESPONSE(hresult, currentUID);
break;
}
case IDirect3DDevice9Ex_CreateDepthStencilSurface:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL_U(Width);
PULL_U(Height);
PULL(D3DFORMAT, Format);
PULL(D3DMULTISAMPLE_TYPE, MultiSample);
PULL_D(MultisampleQuality);
PULL(BOOL, Discard);
PULL_HND(pHandle);
LPDIRECT3DSURFACE9 pSurface;
const auto hresult = pD3DDevice->CreateDepthStencilSurface(IN Width, IN Height, IN Format, IN MultiSample, IN MultisampleQuality, IN Discard, OUT & pSurface, IN nullptr);
if (SUCCEEDED(hresult)) {
gpD3DResources[pHandle] = pSurface;
}
assert(SUCCEEDED(hresult));
SEND_OPTIONAL_CREATE_FUNCTION_SERVER_RESPONSE(hresult, currentUID);
break;
}
case IDirect3DDevice9Ex_UpdateSurface:
{
HRESULT hresult = D3DERR_INVALIDCALL;
GET_RES(pD3DDevice, gpD3DDevices);
PULL_HND(pSourceHandle);
PULL_OBJ(RECT, pSourceRect);
PULL_HND(pDestHandle);
PULL_OBJ(POINT, pDestPoint);
const auto& pSourceSurface = (IDirect3DSurface9*) gpD3DResources[pSourceHandle];
assert(pSourceSurface != nullptr);
const auto& pDestinationSurface = (IDirect3DSurface9*) gpD3DResources[pDestHandle];
assert(pDestinationSurface != nullptr);
if (pSourceSurface != nullptr && pDestinationSurface != nullptr) {
hresult = pD3DDevice->UpdateSurface(IN pSourceSurface, IN pSourceRect, IN pDestinationSurface, IN pDestPoint);
assert(SUCCEEDED(hresult));
}
SEND_OPTIONAL_SERVER_RESPONSE(hresult, currentUID);
break;
}
case IDirect3DDevice9Ex_UpdateTexture:
{
HRESULT hresult = D3DERR_INVALIDCALL;
GET_RES(pD3DDevice, gpD3DDevices);
PULL_HND(pSourceTextureHandle);
PULL_HND(pDestinationTextureHandle);
const auto& pSourceTexture = (IDirect3DBaseTexture9*) gpD3DResources[pSourceTextureHandle];
assert(pSourceTexture != nullptr);
const auto& pDestinationTexture = (IDirect3DBaseTexture9*) gpD3DResources[pDestinationTextureHandle];
assert(pDestinationTexture != nullptr);
if (pSourceTexture != nullptr && pDestinationTexture != nullptr) {
hresult = pD3DDevice->UpdateTexture(IN pSourceTexture, IN pDestinationTexture);
assert(SUCCEEDED(hresult));
}
SEND_OPTIONAL_SERVER_RESPONSE(hresult, currentUID);
break;
}
case IDirect3DDevice9Ex_GetRenderTargetData:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL_HND(pRenderTargetHandle);
PULL_HND(pDestSurfaceHandle);
const auto& pRenderTarget = (IDirect3DSurface9*) gpD3DResources[pRenderTargetHandle];
const auto& pDestSurface = (IDirect3DSurface9*) gpD3DResources[pDestSurfaceHandle];
auto hresult = pD3DDevice->GetRenderTargetData(IN pRenderTarget, IN pDestSurface);
hresult = ReturnSurfaceDataToClient(pDestSurface, hresult, currentUID);
assert(SUCCEEDED(hresult));
break;
}
case IDirect3DDevice9Ex_GetFrontBufferData:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL(uint32_t, iSwapChain);
PULL_HND(pDestSurfaceHandle);
const auto& pDestSurface = (IDirect3DSurface9*) gpD3DResources[pDestSurfaceHandle];
IDirect3DSurface9* pBackbuffer = nullptr;
auto hresult = pD3DDevice->GetFrontBufferData(IN iSwapChain, IN pDestSurface);
hresult = ReturnSurfaceDataToClient(pDestSurface, hresult, currentUID);
assert(SUCCEEDED(hresult));
break;
}
case IDirect3DDevice9Ex_StretchRect:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL_HND(pSourceHandle);
PULL_OBJ(RECT, pSourceRect);
PULL_HND(pDestHandle);
PULL_OBJ(RECT, pDestRect);
PULL(D3DTEXTUREFILTERTYPE, Filter);
const auto& pSourceSurface = (IDirect3DSurface9*) gpD3DResources[pSourceHandle];
const auto& pDestSurface = (IDirect3DSurface9*) gpD3DResources[pDestHandle];
const auto hresult = pD3DDevice->StretchRect(IN pSourceSurface, IN pSourceRect, IN pDestSurface, IN pDestRect, IN Filter);
assert(SUCCEEDED(hresult));
SEND_OPTIONAL_SERVER_RESPONSE(hresult, currentUID);
break;
}
case IDirect3DDevice9Ex_ColorFill:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL_HND(pHandle);
PULL_OBJ(RECT, pRect);
PULL_OBJ(D3DCOLOR, color);
const auto& pSurface = (IDirect3DSurface9*) gpD3DResources[pHandle];
const auto hresult = pD3DDevice->ColorFill(IN pSurface, IN pRect, IN * color);
assert(SUCCEEDED(hresult));
SEND_OPTIONAL_SERVER_RESPONSE(hresult, currentUID);
break;
}
case IDirect3DDevice9Ex_CreateOffscreenPlainSurface:
{
GET_RES(pD3DDevice, gpD3DDevices);
PULL_U(Width);
PULL_U(Height);
PULL(D3DFORMAT, Format);
PULL(D3DPOOL, Pool);
PULL_HND(pHandle);
LPDIRECT3DSURFACE9 pSurface;
const auto hresult = pD3DDevice->CreateOffscreenPlainSurface(IN Width, IN Height, IN Format, IN Pool, OUT & pSurface, IN nullptr);
if (SUCCEEDED(hresult)) {
gpD3DResources[pHandle] = pSurface;
}
assert(SUCCEEDED(hresult));
SEND_OPTIONAL_CREATE_FUNCTION_SERVER_RESPONSE(hresult, currentUID);
break;
}
case IDirect3DDevice9Ex_SetRenderTarget:
{
HRESULT hresult = D3DERR_INVALIDCALL;
GET_RES(pD3DDevice, gpD3DDevices);
PULL_D(RenderTargetIndex);
PULL_U(pHandle);