-
Notifications
You must be signed in to change notification settings - Fork 83
/
d3d8to9_device.cpp
2354 lines (1988 loc) · 77.8 KB
/
d3d8to9_device.cpp
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) 2015 Patrick Mours. All rights reserved.
* License: https://github.com/crosire/d3d8to9#license
*/
#include "d3dx9.hpp"
#include "d3d8to9.hpp"
#include <regex>
#include <assert.h>
struct VertexShaderInfo
{
IDirect3DVertexShader9 *Shader = nullptr;
IDirect3DVertexDeclaration9 *Declaration = nullptr;
};
Direct3DDevice8::Direct3DDevice8(Direct3D8 *d3d, IDirect3DDevice9 *ProxyInterface, DWORD BehaviorFlags, BOOL EnableZBufferDiscarding) :
D3D(d3d), ProxyInterface(ProxyInterface), ZBufferDiscarding(EnableZBufferDiscarding)
{
ProxyAddressLookupTable = new AddressLookupTable(this);
PaletteFlag = SupportsPalettes();
IsMixedVPModeDevice = BehaviorFlags & D3DCREATE_MIXED_VERTEXPROCESSING;
// The default value of D3DRS_POINTSIZE_MIN is 0.0f in D3D8,
// whereas in D3D9 it is 1.0f, so adjust it as needed
ProxyInterface->SetRenderState(D3DRS_POINTSIZE_MIN, (DWORD) 0.0f);
}
Direct3DDevice8::~Direct3DDevice8()
{
delete ProxyAddressLookupTable;
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::QueryInterface(REFIID riid, void **ppvObj)
{
if (ppvObj == nullptr)
return E_POINTER;
if (riid == __uuidof(IDirect3DDevice8) ||
riid == __uuidof(IUnknown))
{
AddRef();
*ppvObj = static_cast<IDirect3DDevice8 *>(this);
return S_OK;
}
const HRESULT hr = ProxyInterface->QueryInterface(ConvertREFIID(riid), ppvObj);
if (SUCCEEDED(hr))
GenericQueryInterface(riid, ppvObj, this);
return hr;
}
ULONG STDMETHODCALLTYPE Direct3DDevice8::AddRef()
{
ULONG LastRefCount = ProxyInterface->AddRef();
// Shaders and state blocks increase ref counter in d3d9 but not in d3d8
DWORD ExtraRefs = VertexShaderAndDeclarationCount + PixelShaderHandles.size() + StateBlockTokens.size();
if (ExtraRefs <= LastRefCount)
{
LastRefCount = LastRefCount - ExtraRefs;
}
return LastRefCount;
}
ULONG STDMETHODCALLTYPE Direct3DDevice8::Release()
{
// Get current value before releasing the device reference
ULONG LastRefCount = ProxyInterface->AddRef();
LastRefCount = ProxyInterface->Release();
// Shaders and StateBlocks are destroyed alongside the device that created them in D3D8 but not in D3D9
// so we need to Release any remaining shaders or state blocks when the device is released to mirror that behaviour
DWORD ExtraRefs = VertexShaderAndDeclarationCount + PixelShaderHandles.size() + StateBlockTokens.size();
if (ExtraRefs <= LastRefCount)
{
LastRefCount = LastRefCount - ExtraRefs;
if (LastRefCount == 1)
{
// Release shaders and state blocks when only one reference is left
ReleaseShadersAndStateBlocks();
}
}
// Release device reference
LastRefCount = ProxyInterface->Release();
if (LastRefCount == 0)
delete this;
return LastRefCount;
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::TestCooperativeLevel()
{
return ProxyInterface->TestCooperativeLevel();
}
UINT STDMETHODCALLTYPE Direct3DDevice8::GetAvailableTextureMem()
{
return ProxyInterface->GetAvailableTextureMem();
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::ResourceManagerDiscardBytes(DWORD Bytes)
{
UNREFERENCED_PARAMETER(Bytes);
return ProxyInterface->EvictManagedResources();
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::GetDirect3D(IDirect3D8 **ppD3D8)
{
if (ppD3D8 == nullptr)
return D3DERR_INVALIDCALL;
D3D->AddRef();
*ppD3D8 = D3D;
return D3D_OK;
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::GetDeviceCaps(D3DCAPS8 *pCaps)
{
if (pCaps == nullptr)
return D3DERR_INVALIDCALL;
D3DCAPS9 DeviceCaps;
const HRESULT hr = ProxyInterface->GetDeviceCaps(&DeviceCaps);
if (FAILED(hr))
return hr;
ConvertCaps(DeviceCaps, *pCaps);
return D3D_OK;
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::GetDisplayMode(D3DDISPLAYMODE *pMode)
{
return ProxyInterface->GetDisplayMode(0, pMode);
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::GetCreationParameters(D3DDEVICE_CREATION_PARAMETERS *pParameters)
{
return ProxyInterface->GetCreationParameters(pParameters);
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::SetCursorProperties(UINT XHotSpot, UINT YHotSpot, IDirect3DSurface8 *pCursorBitmap)
{
if (pCursorBitmap == nullptr)
return D3DERR_INVALIDCALL;
auto pCursorBitmapImpl = static_cast<Direct3DSurface8 *>(pCursorBitmap);
return ProxyInterface->SetCursorProperties(XHotSpot, YHotSpot, pCursorBitmapImpl->GetProxyInterface());
}
void STDMETHODCALLTYPE Direct3DDevice8::SetCursorPosition(UINT XScreenSpace, UINT YScreenSpace, DWORD Flags)
{
ProxyInterface->SetCursorPosition(XScreenSpace, YScreenSpace, Flags);
}
BOOL STDMETHODCALLTYPE Direct3DDevice8::ShowCursor(BOOL bShow)
{
return ProxyInterface->ShowCursor(bShow);
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::CreateAdditionalSwapChain(D3DPRESENT_PARAMETERS8 *pPresentationParameters, IDirect3DSwapChain8 **ppSwapChain)
{
#ifndef D3D8TO9NOLOG
LOG << "Redirecting '" << "IDirect3DDevice8::CreateAdditionalSwapChain" << "(" << this << ", " << pPresentationParameters << ", " << ppSwapChain << ")' ..." << std::endl;
#endif
if (pPresentationParameters == nullptr || ppSwapChain == nullptr)
return D3DERR_INVALIDCALL;
*ppSwapChain = nullptr;
D3DPRESENT_PARAMETERS PresentParams;
ConvertPresentParameters(*pPresentationParameters, PresentParams);
// Get multisample quality level
if (PresentParams.MultiSampleType != D3DMULTISAMPLE_NONE)
{
DWORD QualityLevels = 0;
D3DDEVICE_CREATION_PARAMETERS CreationParams;
ProxyInterface->GetCreationParameters(&CreationParams);
if (D3D->GetProxyInterface()->CheckDeviceMultiSampleType(CreationParams.AdapterOrdinal,
CreationParams.DeviceType, PresentParams.BackBufferFormat, PresentParams.Windowed,
PresentParams.MultiSampleType, &QualityLevels) == S_OK &&
D3D->GetProxyInterface()->CheckDeviceMultiSampleType(CreationParams.AdapterOrdinal,
CreationParams.DeviceType, PresentParams.AutoDepthStencilFormat, PresentParams.Windowed,
PresentParams.MultiSampleType, &QualityLevels) == S_OK)
{
PresentParams.MultiSampleQuality = (QualityLevels != 0) ? QualityLevels - 1 : 0;
}
}
IDirect3DSwapChain9 *SwapChainInterface = nullptr;
const HRESULT hr = ProxyInterface->CreateAdditionalSwapChain(&PresentParams, &SwapChainInterface);
if (FAILED(hr))
return hr;
*ppSwapChain = new Direct3DSwapChain8(this, SwapChainInterface);
return D3D_OK;
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::Reset(D3DPRESENT_PARAMETERS8 *pPresentationParameters)
{
#ifndef D3D8TO9NOLOG
LOG << "Redirecting '" << "IDirect3DDevice8::Reset" << "(" << this << ", " << pPresentationParameters << ")' ..." << std::endl;
#endif
if (pPresentationParameters == nullptr)
return D3DERR_INVALIDCALL;
pCurrentRenderTarget = nullptr;
D3DPRESENT_PARAMETERS PresentParams;
ConvertPresentParameters(*pPresentationParameters, PresentParams);
// Get multisample quality level
if (PresentParams.MultiSampleType != D3DMULTISAMPLE_NONE)
{
DWORD QualityLevels = 0;
D3DDEVICE_CREATION_PARAMETERS CreationParams;
ProxyInterface->GetCreationParameters(&CreationParams);
if (D3D->GetProxyInterface()->CheckDeviceMultiSampleType(CreationParams.AdapterOrdinal,
CreationParams.DeviceType, PresentParams.BackBufferFormat, PresentParams.Windowed,
PresentParams.MultiSampleType, &QualityLevels) == S_OK &&
D3D->GetProxyInterface()->CheckDeviceMultiSampleType(CreationParams.AdapterOrdinal,
CreationParams.DeviceType, PresentParams.AutoDepthStencilFormat, PresentParams.Windowed,
PresentParams.MultiSampleType, &QualityLevels) == S_OK)
{
PresentParams.MultiSampleQuality = (QualityLevels != 0) ? QualityLevels - 1 : 0;
}
}
HRESULT hr = ProxyInterface->Reset(&PresentParams);
if (SUCCEEDED(hr))
{
// The default value of D3DRS_POINTSIZE_MIN is 0.0f in D3D8,
// whereas in D3D9 it is 1.0f, so adjust it as needed
ProxyInterface->SetRenderState(D3DRS_POINTSIZE_MIN, (DWORD) 0.0f);
}
return hr;
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::Present(const RECT *pSourceRect, const RECT *pDestRect, HWND hDestWindowOverride, const RGNDATA *pDirtyRegion)
{
UNREFERENCED_PARAMETER(pDirtyRegion);
return ProxyInterface->Present(pSourceRect, pDestRect, hDestWindowOverride, nullptr);
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::GetBackBuffer(UINT iBackBuffer, D3DBACKBUFFER_TYPE Type, IDirect3DSurface8 **ppBackBuffer)
{
if (ppBackBuffer == nullptr)
return D3DERR_INVALIDCALL;
*ppBackBuffer = nullptr;
IDirect3DSurface9 *SurfaceInterface = nullptr;
const HRESULT hr = ProxyInterface->GetBackBuffer(0, iBackBuffer, Type, &SurfaceInterface);
if (FAILED(hr))
return hr;
*ppBackBuffer = ProxyAddressLookupTable->FindAddress<Direct3DSurface8>(SurfaceInterface);
return D3D_OK;
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::GetRasterStatus(D3DRASTER_STATUS *pRasterStatus)
{
return ProxyInterface->GetRasterStatus(0, pRasterStatus);
}
void STDMETHODCALLTYPE Direct3DDevice8::SetGammaRamp(DWORD Flags, const D3DGAMMARAMP *pRamp)
{
ProxyInterface->SetGammaRamp(0, Flags, pRamp);
}
void STDMETHODCALLTYPE Direct3DDevice8::GetGammaRamp(D3DGAMMARAMP *pRamp)
{
ProxyInterface->GetGammaRamp(0, pRamp);
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::CreateTexture(UINT Width, UINT Height, UINT Levels, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool, IDirect3DTexture8 **ppTexture)
{
if (ppTexture == nullptr)
return D3DERR_INVALIDCALL;
if (Format == D3DFMT_UNKNOWN)
return D3DERR_INVALIDCALL;
*ppTexture = nullptr;
if (Pool == D3DPOOL_DEFAULT)
{
D3DDEVICE_CREATION_PARAMETERS CreationParams;
ProxyInterface->GetCreationParameters(&CreationParams);
if ((Usage & D3DUSAGE_DYNAMIC) == 0 &&
SUCCEEDED(D3D->GetProxyInterface()->CheckDeviceFormat(CreationParams.AdapterOrdinal, CreationParams.DeviceType, D3DFMT_X8R8G8B8, D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE, Format)))
{
Usage |= D3DUSAGE_RENDERTARGET;
}
else if (Usage != D3DUSAGE_DEPTHSTENCIL)
{
Usage |= D3DUSAGE_DYNAMIC;
}
}
IDirect3DTexture9 *TextureInterface = nullptr;
const HRESULT hr = ProxyInterface->CreateTexture(Width, Height, Levels, Usage, Format, Pool, &TextureInterface, nullptr);
if (FAILED(hr))
return hr;
*ppTexture = new Direct3DTexture8(this, TextureInterface);
return D3D_OK;
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::CreateVolumeTexture(UINT Width, UINT Height, UINT Depth, UINT Levels, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool, IDirect3DVolumeTexture8 **ppVolumeTexture)
{
if (ppVolumeTexture == nullptr)
return D3DERR_INVALIDCALL;
if (Format == D3DFMT_UNKNOWN)
return D3DERR_INVALIDCALL;
*ppVolumeTexture = nullptr;
IDirect3DVolumeTexture9 *TextureInterface = nullptr;
const HRESULT hr = ProxyInterface->CreateVolumeTexture(Width, Height, Depth, Levels, Usage, Format, Pool, &TextureInterface, nullptr);
if (FAILED(hr))
return hr;
*ppVolumeTexture = new Direct3DVolumeTexture8(this, TextureInterface);
return D3D_OK;
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::CreateCubeTexture(UINT EdgeLength, UINT Levels, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool, IDirect3DCubeTexture8 **ppCubeTexture)
{
if (ppCubeTexture == nullptr)
return D3DERR_INVALIDCALL;
if (Format == D3DFMT_UNKNOWN)
return D3DERR_INVALIDCALL;
*ppCubeTexture = nullptr;
IDirect3DCubeTexture9 *TextureInterface = nullptr;
const HRESULT hr = ProxyInterface->CreateCubeTexture(EdgeLength, Levels, Usage, Format, Pool, &TextureInterface, nullptr);
if (FAILED(hr))
return hr;
*ppCubeTexture = new Direct3DCubeTexture8(this, TextureInterface);
return D3D_OK;
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::CreateVertexBuffer(UINT Length, DWORD Usage, DWORD FVF, D3DPOOL Pool, IDirect3DVertexBuffer8 **ppVertexBuffer)
{
if (ppVertexBuffer == nullptr)
return D3DERR_INVALIDCALL;
*ppVertexBuffer = nullptr;
IDirect3DVertexBuffer9 *BufferInterface = nullptr;
const HRESULT hr = ProxyInterface->CreateVertexBuffer(Length, Usage, FVF, Pool, &BufferInterface, nullptr);
if (FAILED(hr))
return hr;
*ppVertexBuffer = new Direct3DVertexBuffer8(this, BufferInterface);
return D3D_OK;
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::CreateIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool, IDirect3DIndexBuffer8 **ppIndexBuffer)
{
if (ppIndexBuffer == nullptr)
return D3DERR_INVALIDCALL;
*ppIndexBuffer = nullptr;
IDirect3DIndexBuffer9 *BufferInterface = nullptr;
const HRESULT hr = ProxyInterface->CreateIndexBuffer(Length, Usage, Format, Pool, &BufferInterface, nullptr);
if (FAILED(hr))
return hr;
*ppIndexBuffer = new Direct3DIndexBuffer8(this, BufferInterface);
return D3D_OK;
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::CreateRenderTarget(UINT Width, UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, BOOL Lockable, IDirect3DSurface8 **ppSurface)
{
if (ppSurface == nullptr)
return D3DERR_INVALIDCALL;
if (Format == D3DFMT_UNKNOWN)
return D3DERR_INVALIDCALL;
*ppSurface = nullptr;
DWORD QualityLevels = 0;
// Get multisample quality level
if (MultiSample != D3DMULTISAMPLE_NONE)
{
D3DDEVICE_CREATION_PARAMETERS CreationParams;
ProxyInterface->GetCreationParameters(&CreationParams);
D3D->GetProxyInterface()->CheckDeviceMultiSampleType(CreationParams.AdapterOrdinal, CreationParams.DeviceType, Format, FALSE, MultiSample, &QualityLevels);
QualityLevels = (QualityLevels != 0) ? QualityLevels - 1 : 0;
}
IDirect3DSurface9 *SurfaceInterface = nullptr;
const HRESULT hr = ProxyInterface->CreateRenderTarget(Width, Height, Format, MultiSample, QualityLevels, Lockable, &SurfaceInterface, nullptr);
if (FAILED(hr))
return hr;
*ppSurface = new Direct3DSurface8(this, SurfaceInterface);
return D3D_OK;
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::CreateDepthStencilSurface(UINT Width, UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, IDirect3DSurface8 **ppSurface)
{
if (ppSurface == nullptr)
return D3DERR_INVALIDCALL;
if (Format == D3DFMT_UNKNOWN)
return D3DERR_INVALIDCALL;
*ppSurface = nullptr;
DWORD QualityLevels = 0;
// Get multisample quality level
if (MultiSample != D3DMULTISAMPLE_NONE)
{
D3DDEVICE_CREATION_PARAMETERS CreationParams;
ProxyInterface->GetCreationParameters(&CreationParams);
D3D->GetProxyInterface()->CheckDeviceMultiSampleType(CreationParams.AdapterOrdinal, CreationParams.DeviceType, Format, FALSE, MultiSample, &QualityLevels);
QualityLevels = (QualityLevels != 0) ? QualityLevels - 1 : 0;
}
IDirect3DSurface9 *SurfaceInterface = nullptr;
const HRESULT hr = ProxyInterface->CreateDepthStencilSurface(Width, Height, Format, MultiSample, QualityLevels, ZBufferDiscarding, &SurfaceInterface, nullptr);
if (FAILED(hr))
return hr;
*ppSurface = new Direct3DSurface8(this, SurfaceInterface);
return D3D_OK;
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::CreateImageSurface(UINT Width, UINT Height, D3DFORMAT Format, IDirect3DSurface8 **ppSurface)
{
#ifndef D3D8TO9NOLOG
LOG << "Redirecting '" << "IDirect3DDevice8::CreateImageSurface" << "(" << this << ", " << Width << ", " << Height << ", " << Format << ", " << ppSurface << ")' ..." << std::endl;
#endif
if (ppSurface == nullptr)
return D3DERR_INVALIDCALL;
// Only CreateImageSurface clears the content of ppSurface
// before checking if Format is equal to D3DFMT_UNKNOWN.
*ppSurface = nullptr;
if (Format == D3DFMT_UNKNOWN)
return D3DERR_INVALIDCALL;
IDirect3DSurface9 *SurfaceInterface = nullptr;
const HRESULT hr = ProxyInterface->CreateOffscreenPlainSurface(Width, Height, Format, D3DPOOL_SYSTEMMEM, &SurfaceInterface, nullptr);
if (FAILED(hr) && FAILED(ProxyInterface->CreateOffscreenPlainSurface(Width, Height, Format, D3DPOOL_SCRATCH, &SurfaceInterface, nullptr)))
{
#ifndef D3D8TO9NOLOG
LOG << "> 'IDirect3DDevice9::CreateOffscreenPlainSurface' failed with error code " << std::hex << hr << std::dec << "!" << std::endl;
#endif
return hr;
}
*ppSurface = new Direct3DSurface8(this, SurfaceInterface);
return D3D_OK;
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::CopyRects(IDirect3DSurface8 *pSourceSurface, const RECT *pSourceRectsArray, UINT cRects, IDirect3DSurface8 *pDestinationSurface, const POINT *pDestPointsArray)
{
if (pSourceSurface == nullptr || pDestinationSurface == nullptr || pSourceSurface == pDestinationSurface)
return D3DERR_INVALIDCALL;
auto pSourceSurfaceImpl = static_cast<Direct3DSurface8 *>(pSourceSurface);
auto pDestinationSurfaceImpl = static_cast<Direct3DSurface8 *>(pDestinationSurface);
D3DSURFACE_DESC SourceDesc, DestinationDesc;
pSourceSurfaceImpl->GetProxyInterface()->GetDesc(&SourceDesc);
pDestinationSurfaceImpl->GetProxyInterface()->GetDesc(&DestinationDesc);
if (SourceDesc.Format != DestinationDesc.Format)
return D3DERR_INVALIDCALL;
if (IsDepthStencil(SourceDesc.Format))
return D3DERR_INVALIDCALL;
HRESULT hr = D3DERR_INVALIDCALL;
if (cRects == 0)
cRects = 1;
for (UINT i = 0; i < cRects; i++)
{
RECT SourceRect, DestinationRect;
if (pSourceRectsArray != nullptr)
{
SourceRect = pSourceRectsArray[i];
}
else
{
SourceRect.left = 0;
SourceRect.right = SourceDesc.Width;
SourceRect.top = 0;
SourceRect.bottom = SourceDesc.Height;
}
if (pDestPointsArray != nullptr)
{
DestinationRect.left = pDestPointsArray[i].x;
DestinationRect.right = DestinationRect.left + (SourceRect.right - SourceRect.left);
DestinationRect.top = pDestPointsArray[i].y;
DestinationRect.bottom = DestinationRect.top + (SourceRect.bottom - SourceRect.top);
}
else
{
DestinationRect = SourceRect;
}
if (SourceDesc.Pool == D3DPOOL_MANAGED || DestinationDesc.Pool != D3DPOOL_DEFAULT)
{
hr = D3DERR_INVALIDCALL;
if (D3DXLoadSurfaceFromSurface != nullptr)
{
if (SUCCEEDED(D3DXLoadSurfaceFromSurface(pDestinationSurfaceImpl->GetProxyInterface(), nullptr, &DestinationRect, pSourceSurfaceImpl->GetProxyInterface(), nullptr, &SourceRect, D3DX_FILTER_NONE, 0)))
{
// Explicitly call AddDirtyRect on the surface
void *pContainer = nullptr;
if (SUCCEEDED(pDestinationSurfaceImpl->GetContainer(IID_IDirect3DTexture9, &pContainer)) && pContainer)
{
IDirect3DTexture9 *pTexture = (IDirect3DTexture9*)pContainer;
pTexture->AddDirtyRect(&DestinationRect);
pTexture->Release();
}
hr = D3D_OK;
}
}
}
else if (SourceDesc.Pool == D3DPOOL_DEFAULT)
{
hr = ProxyInterface->StretchRect(pSourceSurfaceImpl->GetProxyInterface(), &SourceRect, pDestinationSurfaceImpl->GetProxyInterface(), &DestinationRect, D3DTEXF_NONE);
}
else if (SourceDesc.Pool == D3DPOOL_SYSTEMMEM)
{
const POINT pt = { DestinationRect.left, DestinationRect.top };
hr = ProxyInterface->UpdateSurface(pSourceSurfaceImpl->GetProxyInterface(), &SourceRect, pDestinationSurfaceImpl->GetProxyInterface(), &pt);
}
if (FAILED(hr))
{
#ifndef D3D8TO9NOLOG
LOG << "Failed to translate 'IDirect3DDevice8::CopyRects' call from '[" << SourceDesc.Width << "x" << SourceDesc.Height << ", " << SourceDesc.Format << ", " << SourceDesc.MultiSampleType << ", " << SourceDesc.Usage << ", " << SourceDesc.Pool << "]' to '[" << DestinationDesc.Width << "x" << DestinationDesc.Height << ", " << DestinationDesc.Format << ", " << DestinationDesc.MultiSampleType << ", " << DestinationDesc.Usage << ", " << DestinationDesc.Pool << "]'!" << std::endl;
#endif
break;
}
}
return hr;
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::UpdateTexture(IDirect3DBaseTexture8 *pSourceTexture, IDirect3DBaseTexture8 *pDestinationTexture)
{
if (pSourceTexture == nullptr || pDestinationTexture == nullptr || pSourceTexture->GetType() != pDestinationTexture->GetType())
return D3DERR_INVALIDCALL;
IDirect3DBaseTexture9 *SourceBaseTextureInterface, *DestinationBaseTextureInterface;
switch (pSourceTexture->GetType())
{
case D3DRTYPE_TEXTURE:
SourceBaseTextureInterface = static_cast<Direct3DTexture8 *>(pSourceTexture)->GetProxyInterface();
DestinationBaseTextureInterface = static_cast<Direct3DTexture8 *>(pDestinationTexture)->GetProxyInterface();
break;
case D3DRTYPE_VOLUMETEXTURE:
SourceBaseTextureInterface = static_cast<Direct3DVolumeTexture8 *>(pSourceTexture)->GetProxyInterface();
DestinationBaseTextureInterface = static_cast<Direct3DVolumeTexture8 *>(pDestinationTexture)->GetProxyInterface();
break;
case D3DRTYPE_CUBETEXTURE:
SourceBaseTextureInterface = static_cast<Direct3DCubeTexture8 *>(pSourceTexture)->GetProxyInterface();
DestinationBaseTextureInterface = static_cast<Direct3DCubeTexture8 *>(pDestinationTexture)->GetProxyInterface();
break;
default:
return D3DERR_INVALIDCALL;
}
return ProxyInterface->UpdateTexture(SourceBaseTextureInterface, DestinationBaseTextureInterface);
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::GetFrontBuffer(IDirect3DSurface8 *pDestSurface)
{
if (pDestSurface == nullptr)
return D3DERR_INVALIDCALL;
auto pDestSurfaceImpl = static_cast<Direct3DSurface8 *>(pDestSurface);
return ProxyInterface->GetFrontBufferData(0, pDestSurfaceImpl->GetProxyInterface());
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::SetRenderTarget(IDirect3DSurface8 *pRenderTarget, IDirect3DSurface8 *pNewZStencil)
{
HRESULT hr;
if (pRenderTarget != nullptr)
{
auto pRenderTargetImpl = static_cast<Direct3DSurface8 *>(pRenderTarget);
hr = ProxyInterface->SetRenderTarget(0, pRenderTargetImpl->GetProxyInterface());
if (FAILED(hr))
return hr;
pCurrentRenderTarget = pRenderTargetImpl->GetProxyInterface();
}
if (pNewZStencil != nullptr)
{
auto pNewZStencilImpl = static_cast<Direct3DSurface8 *>(pNewZStencil);
hr = ProxyInterface->SetDepthStencilSurface(pNewZStencilImpl->GetProxyInterface());
if (FAILED(hr))
return hr;
}
else
{
ProxyInterface->SetDepthStencilSurface(nullptr);
}
return D3D_OK;
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::GetRenderTarget(IDirect3DSurface8 **ppRenderTarget)
{
if (ppRenderTarget == nullptr)
return D3DERR_INVALIDCALL;
IDirect3DSurface9 *SurfaceInterface = nullptr;
const HRESULT hr = ProxyInterface->GetRenderTarget(0, &SurfaceInterface);
if (FAILED(hr))
return hr;
pCurrentRenderTarget = SurfaceInterface;
*ppRenderTarget = ProxyAddressLookupTable->FindAddress<Direct3DSurface8>(SurfaceInterface);
return D3D_OK;
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::GetDepthStencilSurface(IDirect3DSurface8 **ppZStencilSurface)
{
if (ppZStencilSurface == nullptr)
return D3DERR_INVALIDCALL;
IDirect3DSurface9 *SurfaceInterface = nullptr;
const HRESULT hr = ProxyInterface->GetDepthStencilSurface(&SurfaceInterface);
if (FAILED(hr))
return hr;
*ppZStencilSurface = ProxyAddressLookupTable->FindAddress<Direct3DSurface8>(SurfaceInterface);
return D3D_OK;
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::BeginScene()
{
return ProxyInterface->BeginScene();
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::EndScene()
{
return ProxyInterface->EndScene();
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::Clear(DWORD Count, const D3DRECT *pRects, DWORD Flags, D3DCOLOR Color, float Z, DWORD Stencil)
{
return ProxyInterface->Clear(Count, pRects, Flags, Color, Z, Stencil);
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::SetTransform(D3DTRANSFORMSTATETYPE State, const D3DMATRIX *pMatrix)
{
return ProxyInterface->SetTransform(State, pMatrix);
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::GetTransform(D3DTRANSFORMSTATETYPE State, D3DMATRIX *pMatrix)
{
return ProxyInterface->GetTransform(State, pMatrix);
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::MultiplyTransform(D3DTRANSFORMSTATETYPE State, const D3DMATRIX *pMatrix)
{
return ProxyInterface->MultiplyTransform(State, pMatrix);
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::SetViewport(const D3DVIEWPORT8 *pViewport)
{
if (pCurrentRenderTarget != nullptr)
{
D3DSURFACE_DESC Desc;
if (SUCCEEDED(pCurrentRenderTarget->GetDesc(&Desc)) && (pViewport->Y + pViewport->Height > Desc.Height || pViewport->X + pViewport->Width > Desc.Width))
return D3DERR_INVALIDCALL;
}
return ProxyInterface->SetViewport(pViewport);
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::GetViewport(D3DVIEWPORT8 *pViewport)
{
return ProxyInterface->GetViewport(pViewport);
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::SetMaterial(const D3DMATERIAL8 *pMaterial)
{
return ProxyInterface->SetMaterial(pMaterial);
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::GetMaterial(D3DMATERIAL8 *pMaterial)
{
return ProxyInterface->GetMaterial(pMaterial);
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::SetLight(DWORD Index, const D3DLIGHT8 *pLight)
{
if (pLight == nullptr)
return D3DERR_INVALIDCALL;
D3DLIGHT8 Light = *pLight;
// Make spot light work more like it did in Direct3D 8
if (Light.Type == D3DLIGHTTYPE::D3DLIGHT_SPOT)
{
// Theta must be in the range from 0 through the value specified by Phi
if (Light.Theta <= Light.Phi)
{
Light.Theta /= 1.75f;
}
}
return ProxyInterface->SetLight(Index, &Light);
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::GetLight(DWORD Index, D3DLIGHT8 *pLight)
{
return ProxyInterface->GetLight(Index, pLight);
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::LightEnable(DWORD Index, BOOL Enable)
{
return ProxyInterface->LightEnable(Index, Enable);
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::GetLightEnable(DWORD Index, BOOL *pEnable)
{
return ProxyInterface->GetLightEnable(Index, pEnable);
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::SetClipPlane(DWORD Index, const float *pPlane)
{
if (pPlane == nullptr || Index >= MAX_CLIP_PLANES)
return D3DERR_INVALIDCALL;
memcpy(StoredClipPlanes[Index], pPlane, sizeof(StoredClipPlanes[0]));
return D3D_OK;
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::GetClipPlane(DWORD Index, float *pPlane)
{
if (pPlane == nullptr || Index >= MAX_CLIP_PLANES)
return D3DERR_INVALIDCALL;
memcpy(pPlane, StoredClipPlanes[Index], sizeof(StoredClipPlanes[0]));
return D3D_OK;
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::SetRenderState(D3DRENDERSTATETYPE State, DWORD Value)
{
FLOAT Biased;
HRESULT hr;
switch (static_cast<DWORD>(State))
{
case D3DRS_ZVISIBLE:
case D3DRS_PATCHSEGMENTS:
case D3DRS_LINEPATTERN:
return D3D_OK;
case D3DRS_SOFTWAREVERTEXPROCESSING:
// SWVP can be modified by this render state only on devices
// created with the D3DCREATE_MIXED_VERTEXPROCESSING flag
if (IsMixedVPModeDevice)
return ProxyInterface->SetSoftwareVertexProcessing(static_cast<BOOL>(Value));
return D3D_OK;
case D3DRS_EDGEANTIALIAS:
return ProxyInterface->SetRenderState(D3DRS_ANTIALIASEDLINEENABLE, Value);
case D3DRS_CLIPPLANEENABLE:
hr = ProxyInterface->SetRenderState(State, Value);
if (SUCCEEDED(hr))
ClipPlaneRenderState = Value;
return hr;
case D3DRS_ZBIAS:
Biased = static_cast<FLOAT>(Value) * -0.000005f;
Value = *reinterpret_cast<const DWORD *>(&Biased);
State = D3DRS_DEPTHBIAS;
default:
return ProxyInterface->SetRenderState(State, Value);
}
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::GetRenderState(D3DRENDERSTATETYPE State, DWORD *pValue)
{
if (pValue == nullptr)
return D3DERR_INVALIDCALL;
HRESULT hr;
*pValue = 0;
switch (static_cast<DWORD>(State))
{
case D3DRS_ZVISIBLE:
case D3DRS_LINEPATTERN:
*pValue = 0;
return D3D_OK;
case D3DRS_EDGEANTIALIAS:
return ProxyInterface->GetRenderState(D3DRS_ANTIALIASEDLINEENABLE, pValue);
case D3DRS_ZBIAS:
hr = ProxyInterface->GetRenderState(D3DRS_DEPTHBIAS, pValue);
*pValue = static_cast<DWORD>(*reinterpret_cast<const FLOAT*>(pValue) * -200000.0f);
return hr;
case D3DRS_SOFTWAREVERTEXPROCESSING:
*pValue = static_cast<DWORD>(ProxyInterface->GetSoftwareVertexProcessing());
return D3D_OK;
case D3DRS_PATCHSEGMENTS:
*pValue = 1;
return D3D_OK;
default:
return ProxyInterface->GetRenderState(State, pValue);
}
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::BeginStateBlock()
{
if (IsRecordingState)
return D3DERR_INVALIDCALL;
HRESULT hr = ProxyInterface->BeginStateBlock();
if (SUCCEEDED(hr))
IsRecordingState = true;
return hr;
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::EndStateBlock(DWORD *pToken)
{
if (pToken == nullptr)
return D3DERR_INVALIDCALL;
if (!IsRecordingState)
return D3DERR_INVALIDCALL;
HRESULT hr = ProxyInterface->EndStateBlock(reinterpret_cast<IDirect3DStateBlock9**>(pToken));
if (SUCCEEDED(hr))
{
StateBlockTokens.insert(*pToken);
IsRecordingState = false;
}
return hr;
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::ApplyStateBlock(DWORD Token)
{
if (Token == 0)
return D3DERR_INVALIDCALL;
if (IsRecordingState)
return D3DERR_INVALIDCALL;
return reinterpret_cast<IDirect3DStateBlock9 *>(Token)->Apply();
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::CaptureStateBlock(DWORD Token)
{
if (Token == 0)
return D3DERR_INVALIDCALL;
if (IsRecordingState)
return D3DERR_INVALIDCALL;
return reinterpret_cast<IDirect3DStateBlock9 *>(Token)->Capture();
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::DeleteStateBlock(DWORD Token)
{
if (Token == 0)
return D3DERR_INVALIDCALL;
if (IsRecordingState)
return D3DERR_INVALIDCALL;
reinterpret_cast<IDirect3DStateBlock9 *>(Token)->Release();
StateBlockTokens.erase(Token);
return D3D_OK;
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::CreateStateBlock(D3DSTATEBLOCKTYPE Type, DWORD *pToken)
{
#ifndef D3D8TO9NOLOG
LOG << "Redirecting '" << "IDirect3DDevice8::CreateStateBlock" << "(" << Type << ", " << pToken << ")' ..." << std::endl;
#endif
if (pToken == nullptr)
return D3DERR_INVALIDCALL;
if (IsRecordingState)
return D3DERR_INVALIDCALL;
HRESULT hr = ProxyInterface->CreateStateBlock(Type, reinterpret_cast<IDirect3DStateBlock9 **>(pToken));
if (SUCCEEDED(hr))
StateBlockTokens.insert(*pToken);
return hr;
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::SetClipStatus(const D3DCLIPSTATUS8 *pClipStatus)
{
return ProxyInterface->SetClipStatus(pClipStatus);
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::GetClipStatus(D3DCLIPSTATUS8 *pClipStatus)
{
return ProxyInterface->GetClipStatus(pClipStatus);
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::GetTexture(DWORD Stage, IDirect3DBaseTexture8 **ppTexture)
{
if (ppTexture == nullptr)
return D3DERR_INVALIDCALL;
*ppTexture = nullptr;
IDirect3DBaseTexture9 *BaseTextureInterface = nullptr;
const HRESULT hr = ProxyInterface->GetTexture(Stage, &BaseTextureInterface);
if (FAILED(hr))
return hr;
if (BaseTextureInterface != nullptr)
{
IDirect3DTexture9 *TextureInterface = nullptr;
IDirect3DCubeTexture9 *CubeTextureInterface = nullptr;
IDirect3DVolumeTexture9 *VolumeTextureInterface = nullptr;
switch (BaseTextureInterface->GetType())
{
case D3DRTYPE_TEXTURE:
BaseTextureInterface->QueryInterface(IID_PPV_ARGS(&TextureInterface));
*ppTexture = ProxyAddressLookupTable->FindAddress<Direct3DTexture8>(TextureInterface);
BaseTextureInterface->Release();
break;
case D3DRTYPE_VOLUMETEXTURE:
BaseTextureInterface->QueryInterface(IID_PPV_ARGS(&VolumeTextureInterface));
*ppTexture = ProxyAddressLookupTable->FindAddress<Direct3DVolumeTexture8>(VolumeTextureInterface);
BaseTextureInterface->Release();
break;
case D3DRTYPE_CUBETEXTURE:
BaseTextureInterface->QueryInterface(IID_PPV_ARGS(&CubeTextureInterface));
*ppTexture = ProxyAddressLookupTable->FindAddress<Direct3DCubeTexture8>(CubeTextureInterface);
BaseTextureInterface->Release();
break;
default:
BaseTextureInterface->Release();
return D3DERR_INVALIDCALL;
}
}
return D3D_OK;
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::SetTexture(DWORD Stage, IDirect3DBaseTexture8 *pTexture)
{
if (pTexture == nullptr)
return ProxyInterface->SetTexture(Stage, nullptr);
IDirect3DBaseTexture9 *BaseTextureInterface;
switch (pTexture->GetType())
{
case D3DRTYPE_TEXTURE:
BaseTextureInterface = static_cast<Direct3DTexture8 *>(pTexture)->GetProxyInterface();
break;
case D3DRTYPE_VOLUMETEXTURE:
BaseTextureInterface = static_cast<Direct3DVolumeTexture8 *>(pTexture)->GetProxyInterface();
break;
case D3DRTYPE_CUBETEXTURE:
BaseTextureInterface = static_cast<Direct3DCubeTexture8 *>(pTexture)->GetProxyInterface();
break;
default:
return D3DERR_INVALIDCALL;
}
return ProxyInterface->SetTexture(Stage, BaseTextureInterface);
}
HRESULT STDMETHODCALLTYPE Direct3DDevice8::GetTextureStageState(DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *pValue)
{
switch (static_cast<DWORD>(Type))
{
case D3DTSS_ADDRESSU:
return ProxyInterface->GetSamplerState(Stage, D3DSAMP_ADDRESSU, pValue);
case D3DTSS_ADDRESSV:
return ProxyInterface->GetSamplerState(Stage, D3DSAMP_ADDRESSV, pValue);
case D3DTSS_ADDRESSW:
return ProxyInterface->GetSamplerState(Stage, D3DSAMP_ADDRESSW, pValue);
case D3DTSS_BORDERCOLOR:
return ProxyInterface->GetSamplerState(Stage, D3DSAMP_BORDERCOLOR, pValue);
case D3DTSS_MAGFILTER:
return ProxyInterface->GetSamplerState(Stage, D3DSAMP_MAGFILTER, pValue);