-
Notifications
You must be signed in to change notification settings - Fork 84
/
d3d8to9_base.cpp
210 lines (176 loc) · 6.69 KB
/
d3d8to9_base.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
/**
* Copyright (C) 2015 Patrick Mours. All rights reserved.
* License: https://github.com/crosire/d3d8to9#license
*/
#include "d3d8to9.hpp"
static const D3DFORMAT AdapterFormats[] = {
D3DFMT_A8R8G8B8,
D3DFMT_X8R8G8B8,
D3DFMT_R5G6B5,
D3DFMT_X1R5G5B5,
D3DFMT_A1R5G5B5
};
Direct3D8::Direct3D8(IDirect3D9 *ProxyInterface) :
ProxyInterface(ProxyInterface)
{
D3DDISPLAYMODE pMode;
CurrentAdapterCount = ProxyInterface->GetAdapterCount();
if (CurrentAdapterCount > MaxAdapters)
CurrentAdapterCount = MaxAdapters;
for (UINT Adapter = 0; Adapter < CurrentAdapterCount; Adapter++)
{
for (D3DFORMAT Format : AdapterFormats)
{
const UINT ModeCount = ProxyInterface->GetAdapterModeCount(Adapter, Format);
for (UINT Mode = 0; Mode < ModeCount; Mode++)
{
ProxyInterface->EnumAdapterModes(Adapter, Format, Mode, &pMode);
CurrentAdapterModes[Adapter].push_back(pMode);
CurrentAdapterModeCount[Adapter]++;
}
}
}
}
Direct3D8::~Direct3D8()
{
}
HRESULT STDMETHODCALLTYPE Direct3D8::QueryInterface(REFIID riid, void **ppvObj)
{
if (ppvObj == nullptr)
return E_POINTER;
if (riid == __uuidof(IDirect3D8) ||
riid == __uuidof(IUnknown))
{
AddRef();
*ppvObj = static_cast<IDirect3D8 *>(this);
return S_OK;
}
return ProxyInterface->QueryInterface(ConvertREFIID(riid), ppvObj);
}
ULONG STDMETHODCALLTYPE Direct3D8::AddRef()
{
return ProxyInterface->AddRef();
}
ULONG STDMETHODCALLTYPE Direct3D8::Release()
{
const ULONG LastRefCount = ProxyInterface->Release();
if (LastRefCount == 0)
delete this;
return LastRefCount;
}
HRESULT STDMETHODCALLTYPE Direct3D8::RegisterSoftwareDevice(void *pInitializeFunction)
{
return ProxyInterface->RegisterSoftwareDevice(pInitializeFunction);
}
UINT STDMETHODCALLTYPE Direct3D8::GetAdapterCount()
{
return CurrentAdapterCount;
}
HRESULT STDMETHODCALLTYPE Direct3D8::GetAdapterIdentifier(UINT Adapter, DWORD Flags, D3DADAPTER_IDENTIFIER8 *pIdentifier)
{
if (pIdentifier == nullptr)
return D3DERR_INVALIDCALL;
D3DADAPTER_IDENTIFIER9 AdapterIndentifier;
if ((Flags & D3DENUM_NO_WHQL_LEVEL) == 0)
{
Flags |= D3DENUM_WHQL_LEVEL;
}
else
{
Flags ^= D3DENUM_NO_WHQL_LEVEL;
}
const HRESULT hr = ProxyInterface->GetAdapterIdentifier(Adapter, Flags, &AdapterIndentifier);
if (FAILED(hr))
return hr;
ConvertAdapterIdentifier(AdapterIndentifier, *pIdentifier);
return D3D_OK;
}
UINT STDMETHODCALLTYPE Direct3D8::GetAdapterModeCount(UINT Adapter)
{
return CurrentAdapterModeCount[Adapter];
}
HRESULT STDMETHODCALLTYPE Direct3D8::EnumAdapterModes(UINT Adapter, UINT Mode, D3DDISPLAYMODE *pMode)
{
if (pMode == nullptr || !(Adapter < CurrentAdapterCount && Mode < CurrentAdapterModeCount[Adapter]))
return D3DERR_INVALIDCALL;
pMode->Format = CurrentAdapterModes[Adapter].at(Mode).Format;
pMode->Height = CurrentAdapterModes[Adapter].at(Mode).Height;
pMode->RefreshRate = CurrentAdapterModes[Adapter].at(Mode).RefreshRate;
pMode->Width = CurrentAdapterModes[Adapter].at(Mode).Width;
return D3D_OK;
}
HRESULT STDMETHODCALLTYPE Direct3D8::GetAdapterDisplayMode(UINT Adapter, D3DDISPLAYMODE *pMode)
{
return ProxyInterface->GetAdapterDisplayMode(Adapter, pMode);
}
HRESULT STDMETHODCALLTYPE Direct3D8::CheckDeviceType(UINT Adapter, D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat, D3DFORMAT BackBufferFormat, BOOL bWindowed)
{
return ProxyInterface->CheckDeviceType(Adapter, CheckType, DisplayFormat, BackBufferFormat, bWindowed);
}
HRESULT STDMETHODCALLTYPE Direct3D8::CheckDeviceFormat(UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat, DWORD Usage, D3DRESOURCETYPE RType, D3DFORMAT CheckFormat)
{
if (CheckFormat == D3DFMT_UYVY ||
CheckFormat == D3DFMT_YUY2 ||
CheckFormat == MAKEFOURCC('Y', 'V', '1', '2') ||
CheckFormat == MAKEFOURCC('N', 'V', '1', '2'))
{
return D3DERR_NOTAVAILABLE;
}
return ProxyInterface->CheckDeviceFormat(Adapter, DeviceType, AdapterFormat, Usage, RType, CheckFormat);
}
HRESULT STDMETHODCALLTYPE Direct3D8::CheckDeviceMultiSampleType(UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat, BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType)
{
return ProxyInterface->CheckDeviceMultiSampleType(Adapter, DeviceType, SurfaceFormat, Windowed, MultiSampleType, nullptr);
}
HRESULT STDMETHODCALLTYPE Direct3D8::CheckDepthStencilMatch(UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat, D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat)
{
return ProxyInterface->CheckDepthStencilMatch(Adapter, DeviceType, AdapterFormat, RenderTargetFormat, DepthStencilFormat);
}
HRESULT STDMETHODCALLTYPE Direct3D8::GetDeviceCaps(UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS8 *pCaps)
{
if (pCaps == nullptr)
return D3DERR_INVALIDCALL;
D3DCAPS9 DeviceCaps;
const HRESULT hr = ProxyInterface->GetDeviceCaps(Adapter, DeviceType, &DeviceCaps);
if (FAILED(hr))
return hr;
ConvertCaps(DeviceCaps, *pCaps);
return D3D_OK;
}
HMONITOR STDMETHODCALLTYPE Direct3D8::GetAdapterMonitor(UINT Adapter)
{
return ProxyInterface->GetAdapterMonitor(Adapter);
}
HRESULT STDMETHODCALLTYPE Direct3D8::CreateDevice(UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow, DWORD BehaviorFlags, D3DPRESENT_PARAMETERS8 *pPresentationParameters, IDirect3DDevice8 **ppReturnedDeviceInterface)
{
#ifndef D3D8TO9NOLOG
LOG << "Redirecting '" << "IDirect3D8::CreateDevice" << "(" << this << ", " << Adapter << ", " << DeviceType << ", " << hFocusWindow << ", " << BehaviorFlags << ", " << pPresentationParameters << ", " << ppReturnedDeviceInterface << ")' ..." << std::endl;
#endif
if (pPresentationParameters == nullptr || ppReturnedDeviceInterface == nullptr)
return D3DERR_INVALIDCALL;
*ppReturnedDeviceInterface = nullptr;
D3DPRESENT_PARAMETERS PresentParams;
ConvertPresentParameters(*pPresentationParameters, PresentParams);
// Get multisample quality level
if (PresentParams.MultiSampleType != D3DMULTISAMPLE_NONE)
{
DWORD QualityLevels = 0;
if (ProxyInterface->CheckDeviceMultiSampleType(Adapter,
DeviceType, PresentParams.BackBufferFormat, PresentParams.Windowed,
PresentParams.MultiSampleType, &QualityLevels) == S_OK &&
ProxyInterface->CheckDeviceMultiSampleType(Adapter,
DeviceType, PresentParams.AutoDepthStencilFormat, PresentParams.Windowed,
PresentParams.MultiSampleType, &QualityLevels) == S_OK)
{
PresentParams.MultiSampleQuality = (QualityLevels != 0) ? QualityLevels - 1 : 0;
}
}
IDirect3DDevice9 *DeviceInterface = nullptr;
const HRESULT hr = ProxyInterface->CreateDevice(Adapter, DeviceType, hFocusWindow, BehaviorFlags, &PresentParams, &DeviceInterface);
if (FAILED(hr))
return hr;
*ppReturnedDeviceInterface = new Direct3DDevice8(this, DeviceInterface, BehaviorFlags, (PresentParams.Flags & D3DPRESENTFLAG_DISCARD_DEPTHSTENCIL) != 0);
// Set default vertex declaration
DeviceInterface->SetFVF(D3DFVF_XYZ);
return D3D_OK;
}