Skip to content

Commit 287b0a6

Browse files
committed
xrRender/HW.cpp and dx10HW.cpp: code cleanup and refactoring
1 parent 6a86c73 commit 287b0a6

File tree

2 files changed

+115
-142
lines changed

2 files changed

+115
-142
lines changed

src/Layers/xrRender/HW.cpp

Lines changed: 89 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,8 @@ void free_vid_mode_list();
1212

1313
CHW HW;
1414

15-
CHW::CHW()
16-
{
17-
18-
}
19-
20-
CHW::~CHW()
21-
{
22-
23-
}
15+
CHW::CHW() {}
16+
CHW::~CHW() {}
2417

2518
//////////////////////////////////////////////////////////////////////
2619
// Construction/Destruction
@@ -30,7 +23,7 @@ void CHW::CreateD3D()
3023
hD3D = XRay::LoadModule(GEnv.isDedicatedServer ? "xrD3D9-Null" : "d3d9.dll");
3124
R_ASSERT2(hD3D->exist(), "Can't find 'd3d9.dll'\nPlease install latest version of DirectX before running this program");
3225

33-
typedef IDirect3D9* WINAPI _Direct3DCreate9(UINT SDKVersion);
26+
using _Direct3DCreate9 = IDirect3D9* WINAPI(UINT SDKVersion);
3427
auto createD3D = (_Direct3DCreate9*)hD3D->getProcAddress("Direct3DCreate9");
3528
R_ASSERT(createD3D);
3629
pD3D = createD3D(D3D_SDK_VERSION);
@@ -49,11 +42,8 @@ void CHW::CreateDevice(HWND m_hWnd, bool move_window)
4942
CreateD3D();
5043

5144
bool bWindowed = !psDeviceFlags.is(rsFullscreen);
52-
53-
#ifndef _EDITOR
5445
if (GEnv.isDedicatedServer)
5546
bWindowed = true;
56-
#endif
5747

5848
m_DriverType = Caps.bForceGPU_REF ? D3DDEVTYPE_REF : D3DDEVTYPE_HAL;
5949

@@ -64,11 +54,11 @@ void CHW::CreateDevice(HWND m_hWnd, bool move_window)
6454
R_CHK(pD3D->GetAdapterIdentifier(DevAdapter, 0, &adapterID));
6555
Msg("* GPU [vendor:%X]-[device:%X]: %s", adapterID.VendorId, adapterID.DeviceId, adapterID.Description);
6656

67-
u16 drv_Product = HIWORD(adapterID.DriverVersion.HighPart);
68-
u16 drv_Version = LOWORD(adapterID.DriverVersion.HighPart);
69-
u16 drv_SubVersion = HIWORD(adapterID.DriverVersion.LowPart);
70-
u16 drv_Build = LOWORD(adapterID.DriverVersion.LowPart);
71-
Msg("* GPU driver: %d.%d.%d.%d", u32(drv_Product), u32(drv_Version), u32(drv_SubVersion), u32(drv_Build));
57+
const u16 driverProduct = HIWORD(adapterID.DriverVersion.HighPart);
58+
const u16 driverVersion = LOWORD(adapterID.DriverVersion.HighPart);
59+
const u16 driverSubVersion = HIWORD(adapterID.DriverVersion.LowPart);
60+
const u16 driverBuild = LOWORD(adapterID.DriverVersion.LowPart);
61+
Msg("* GPU driver: %d.%d.%d.%d", u32(driverProduct), u32(driverVersion), u32(driverSubVersion), u32(driverBuild));
7262

7363
Caps.id_vendor = adapterID.VendorId;
7464
Caps.id_device = adapterID.DeviceId;
@@ -134,12 +124,9 @@ void CHW::CreateDevice(HWND m_hWnd, bool move_window)
134124
D3DPRESENT_PARAMETERS& P = DevPP;
135125
ZeroMemory(&P, sizeof(P));
136126

137-
#ifndef _EDITOR
138127
selectResolution(P.BackBufferWidth, P.BackBufferHeight, bWindowed);
139-
#endif
128+
140129
// Back buffer
141-
//. P.BackBufferWidth = dwWidth;
142-
//. P.BackBufferHeight = dwHeight;
143130
P.BackBufferFormat = fTarget;
144131
P.BackBufferCount = 1;
145132

@@ -170,23 +157,23 @@ void CHW::CreateDevice(HWND m_hWnd, bool move_window)
170157
}
171158

172159
// Create the device
173-
u32 GPU = selectGPU();
174-
HRESULT R = HW.pD3D->CreateDevice(DevAdapter, m_DriverType, m_hWnd,
160+
const auto GPU = selectGPU();
161+
auto result = HW.pD3D->CreateDevice(DevAdapter, m_DriverType, m_hWnd,
175162
GPU | D3DCREATE_MULTITHREADED, //. ? locks at present
176163
&P, &pDevice);
177164

178-
if (FAILED(R))
165+
if (FAILED(result))
179166
{
180-
R = HW.pD3D->CreateDevice(DevAdapter, m_DriverType, m_hWnd,
167+
result = HW.pD3D->CreateDevice(DevAdapter, m_DriverType, m_hWnd,
181168
GPU | D3DCREATE_MULTITHREADED, //. ? locks at present
182169
&P, &pDevice);
183170
}
184-
if (D3DERR_DEVICELOST == R)
171+
if (D3DERR_DEVICELOST == result)
185172
{
186173
// Fatal error! Cannot create rendering device AT STARTUP !!!
187174
Msg("Failed to initialize graphics hardware.\n"
188175
"Please try to restart the game.\n"
189-
"CreateDevice returned 0x%08x(D3DERR_DEVICELOST)", R);
176+
"CreateDevice returned 0x%08x(D3DERR_DEVICELOST)", result);
190177
FlushLog();
191178
MessageBox(nullptr, "Failed to initialize graphics hardware.\nPlease try to restart the game.", "Error!",
192179
MB_OK | MB_ICONERROR);
@@ -211,10 +198,9 @@ void CHW::CreateDevice(HWND m_hWnd, bool move_window)
211198
u32 memory = pDevice->GetAvailableTextureMem();
212199
Msg("* Texture memory: %d M", memory / (1024 * 1024));
213200
Msg("* DDI-level: %2.1f", float(D3DXGetDriverLevel(pDevice)) / 100.f);
214-
#ifndef _EDITOR
201+
215202
updateWindowProps(m_hWnd);
216203
fill_vid_mode_list(this);
217-
#endif
218204
}
219205

220206
void CHW::DestroyDevice()
@@ -249,7 +235,6 @@ void CHW::Reset(HWND hwnd)
249235
_RELEASE(pBaseZB);
250236
_RELEASE(pBaseRT);
251237

252-
#ifndef _EDITOR
253238
bool bWindowed = true;
254239
if (!GEnv.isDedicatedServer)
255240
bWindowed = !psDeviceFlags.is(rsFullscreen);
@@ -268,25 +253,25 @@ void CHW::Reset(HWND hwnd)
268253
DevPP.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
269254
DevPP.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
270255
}
271-
#endif
272256

273257
while (true)
274258
{
275-
HRESULT _hr = HW.pDevice->Reset(&DevPP);
276-
if (SUCCEEDED(_hr))
259+
auto result = HW.pDevice->Reset(&DevPP);
260+
261+
if (SUCCEEDED(result))
277262
break;
278-
Msg("! ERROR: [%dx%d]: %s", DevPP.BackBufferWidth, DevPP.BackBufferHeight, xrDebug::ErrorToString(_hr));
263+
264+
Msg("! ERROR: [%dx%d]: %s", DevPP.BackBufferWidth, DevPP.BackBufferHeight, xrDebug::ErrorToString(result));
279265
Sleep(100);
280266
}
281267
R_CHK(pDevice->GetRenderTarget(0, &pBaseRT));
282268
R_CHK(pDevice->GetDepthStencilSurface(&pBaseZB));
283269
#ifdef DEBUG
284270
R_CHK(pDevice->CreateStateBlock(D3DSBT_ALL, &dwDebugSB));
285271
#endif
286-
#ifndef _EDITOR
272+
287273
updateWindowProps(hwnd);
288274
ShowWindow(hwnd, SW_SHOWNORMAL);
289-
#endif
290275
}
291276

292277
D3DFORMAT CHW::selectDepthStencil(D3DFORMAT fTarget)
@@ -319,36 +304,32 @@ D3DFORMAT CHW::selectDepthStencil(D3DFORMAT fTarget)
319304
void CHW::selectResolution(u32& dwWidth, u32& dwHeight, BOOL bWindowed)
320305
{
321306
fill_vid_mode_list(this);
322-
#ifndef _EDITOR
307+
323308
if (GEnv.isDedicatedServer)
324309
{
325310
dwWidth = 640;
326311
dwHeight = 480;
312+
return;
327313
}
328-
else
329-
#endif
314+
315+
if (bWindowed)
330316
{
331-
if (bWindowed)
332-
{
333-
dwWidth = psCurrentVidMode[0];
334-
dwHeight = psCurrentVidMode[1];
335-
}
336-
else // check
337-
{
338-
#ifndef _EDITOR
339-
string64 buff;
340-
xr_sprintf(buff, sizeof(buff), "%dx%d", psCurrentVidMode[0], psCurrentVidMode[1]);
341-
342-
if (_ParseItem(buff, AvailableVideoModes.data()) == u32(-1)) // not found
343-
{ // select safe
344-
xr_sprintf(buff, sizeof(buff), "vid_mode %s", AvailableVideoModes[0].name);
345-
Console->Execute(buff);
346-
}
317+
dwWidth = psCurrentVidMode[0];
318+
dwHeight = psCurrentVidMode[1];
319+
}
320+
else // check
321+
{
322+
string64 buff;
323+
xr_sprintf(buff, sizeof(buff), "%dx%d", psCurrentVidMode[0], psCurrentVidMode[1]);
347324

348-
dwWidth = psCurrentVidMode[0];
349-
dwHeight = psCurrentVidMode[1];
350-
#endif
325+
if (_ParseItem(buff, AvailableVideoModes.data()) == u32(-1)) // not found
326+
{ // select safe
327+
xr_sprintf(buff, sizeof(buff), "vid_mode %s", AvailableVideoModes[0].name);
328+
Console->Execute(buff);
351329
}
330+
331+
dwWidth = psCurrentVidMode[0];
332+
dwHeight = psCurrentVidMode[1];
352333
}
353334
}
354335

@@ -367,51 +348,59 @@ u32 CHW::selectPresentInterval()
367348
return D3DPRESENT_INTERVAL_DEFAULT;
368349
}
369350

370-
u32 CHW::selectGPU()
351+
void CheckForIntelGMA(CHWCaps& Caps)
371352
{
372-
#if RENDER == R_R1
373-
BOOL isIntelGMA = FALSE;
374-
375-
if (Caps.id_vendor == 0x8086)
376-
{ // Intel
377-
378-
#define GMA_SL_SIZE 43
353+
bool isIntelGMA = false;
379354

380-
DWORD IntelGMA_SoftList[GMA_SL_SIZE] = {0x2782, 0x2582, 0x2792, 0x2592, 0x2772, 0x2776, 0x27A2, 0x27A6, 0x27AE,
381-
0x2982, 0x2983, 0x2992, 0x2993, 0x29A2, 0x29A3, 0x2972, 0x2973, 0x2A02, 0x2A03, 0x2A12, 0x2A13, 0x29C2,
382-
0x29C3, 0x29B2, 0x29B3, 0x29D2, 0x29D3,
383-
384-
0x2A42, 0x2A43, 0x2E02, 0x2E03, 0x2E12, 0x2E13, 0x2E22, 0x2E23, 0x2E32, 0x2E33, 0x2E42, 0x2E43, 0x2E92,
385-
0x2E93, 0x0042, 0x0046};
355+
if (Caps.id_vendor == 0x8086) // Intel
356+
{
357+
constexpr auto GMA_SL_SIZE = 43;
358+
constexpr DWORD IntelGMA_SoftList[GMA_SL_SIZE] =
359+
{
360+
0x2782, 0x2582, 0x2792, 0x2592, 0x2772, 0x2776, 0x27A2, 0x27A6, 0x27AE,
361+
0x2982, 0x2983, 0x2992, 0x2993, 0x29A2, 0x29A3, 0x2972, 0x2973, 0x2A02,
362+
0x2A03, 0x2A12, 0x2A13, 0x29C2, 0x29C3, 0x29B2, 0x29B3, 0x29D2, 0x29D3,
363+
0x2A42, 0x2A43, 0x2E02, 0x2E03, 0x2E12, 0x2E13, 0x2E22, 0x2E23, 0x2E32,
364+
0x2E33, 0x2E42, 0x2E43, 0x2E92, 0x2E93, 0x0042, 0x0046
365+
};
386366

387367
for (int idx = 0; idx < GMA_SL_SIZE; ++idx)
368+
{
388369
if (IntelGMA_SoftList[idx] == Caps.id_device)
389370
{
390-
isIntelGMA = TRUE;
371+
isIntelGMA = true;
391372
break;
392373
}
374+
}
393375
}
394376

395377
if (isIntelGMA)
378+
{
396379
switch (ps_r1_SoftwareSkinning)
397380
{
398381
case 0:
399-
Msg("* Enabling software skinning");
382+
Log("* Enabling software skinning");
400383
ps_r1_SoftwareSkinning = 1;
401384
break;
402-
case 1: Msg("* Using software skinning"); break;
385+
case 1: Log("* Using software skinning"); break;
403386
case 2:
404-
Msg("* WARNING: Using hardware skinning");
405-
Msg("* setting 'r1_software_skinning' to '1' may improve performance");
387+
Log("* WARNING: Using hardware skinning");
388+
Log("* setting 'r1_software_skinning' to '1' may improve performance");
406389
break;
407390
}
391+
}
408392
else if (ps_r1_SoftwareSkinning == 1)
409393
{
410394
Msg("* WARNING: Using software skinning");
411395
Msg("* setting 'r1_software_skinning' to '0' should improve performance");
412396
}
397+
}
413398

414-
#endif // RENDER == R_R1
399+
u32 CHW::selectGPU()
400+
{
401+
#if RENDER == R_R1
402+
CheckForIntelGMA(Caps);
403+
#endif
415404

416405
if (Caps.bForceGPU_SW)
417406
return D3DCREATE_SOFTWARE_VERTEXPROCESSING;
@@ -423,62 +412,51 @@ u32 CHW::selectGPU()
423412
{
424413
if (Caps.bForceGPU_NonPure)
425414
return D3DCREATE_HARDWARE_VERTEXPROCESSING;
426-
else
427-
{
428-
if (caps.DevCaps & D3DDEVCAPS_PUREDEVICE)
429-
return D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE;
430-
else
431-
return D3DCREATE_HARDWARE_VERTEXPROCESSING;
432-
}
415+
if (caps.DevCaps & D3DDEVCAPS_PUREDEVICE)
416+
return D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE;
417+
return D3DCREATE_HARDWARE_VERTEXPROCESSING;
433418
// return D3DCREATE_MIXED_VERTEXPROCESSING;
434419
}
435-
else
436-
return D3DCREATE_SOFTWARE_VERTEXPROCESSING;
420+
421+
return D3DCREATE_SOFTWARE_VERTEXPROCESSING;
437422
}
438423

439424
u32 CHW::selectRefresh(u32 dwWidth, u32 dwHeight, D3DFORMAT fmt)
440425
{
441426
if (psDeviceFlags.is(rsRefresh60hz))
442-
{
443427
return D3DPRESENT_RATE_DEFAULT;
444-
}
445-
else
428+
429+
auto selected = D3DPRESENT_RATE_DEFAULT;
430+
const auto count = pD3D->GetAdapterModeCount(DevAdapter, fmt);
431+
for (u32 I = 0; I < count; I++)
446432
{
447-
u32 selected = D3DPRESENT_RATE_DEFAULT;
448-
u32 count = pD3D->GetAdapterModeCount(DevAdapter, fmt);
449-
for (u32 I = 0; I < count; I++)
433+
D3DDISPLAYMODE Mode;
434+
pD3D->EnumAdapterModes(DevAdapter, fmt, I, &Mode);
435+
if (Mode.Width == dwWidth && Mode.Height == dwHeight)
450436
{
451-
D3DDISPLAYMODE Mode;
452-
pD3D->EnumAdapterModes(DevAdapter, fmt, I, &Mode);
453-
if (Mode.Width == dwWidth && Mode.Height == dwHeight)
454-
{
455-
//if (Mode.RefreshRate > selected)
456-
// selected = Mode.RefreshRate;
457-
if (Mode.RefreshRate <= maxRefreshRate && Mode.RefreshRate>selected)
458-
selected = Mode.RefreshRate; //ECO_RENDER modif.
459-
}
437+
//if (Mode.RefreshRate > selected)
438+
// selected = Mode.RefreshRate;
439+
if (Mode.RefreshRate <= maxRefreshRate && Mode.RefreshRate>selected)
440+
selected = Mode.RefreshRate; //ECO_RENDER modif.
460441
}
461-
return selected;
462442
}
443+
return selected;
463444
}
464445

465446
BOOL CHW::support(D3DFORMAT fmt, DWORD type, DWORD usage)
466447
{
467-
HRESULT hr = pD3D->CheckDeviceFormat(DevAdapter, m_DriverType, Caps.fTarget, usage, (D3DRESOURCETYPE)type, fmt);
468-
if (FAILED(hr))
448+
auto result = pD3D->CheckDeviceFormat(DevAdapter, m_DriverType, Caps.fTarget, usage, (D3DRESOURCETYPE)type, fmt);
449+
if (FAILED(result))
469450
return FALSE;
470-
else
471-
return TRUE;
451+
return TRUE;
472452
}
473453

474454
void CHW::updateWindowProps(HWND m_hWnd)
475455
{
476456
bool bWindowed = !psDeviceFlags.is(rsFullscreen);
477457

478-
#ifndef _EDITOR
479458
if (GEnv.isDedicatedServer)
480459
bWindowed = true;
481-
#endif
482460

483461
u32 dwWindowStyle = 0;
484462
// Set window properties depending on what mode were in.
@@ -536,8 +514,7 @@ void CHW::updateWindowProps(HWND m_hWnd)
536514
}
537515
else
538516
{
539-
SetWindowLong(m_hWnd, GWL_STYLE, dwWindowStyle = (WS_POPUP | WS_VISIBLE));
540-
SetWindowLong(m_hWnd, GWL_EXSTYLE, WS_EX_TOPMOST);
517+
SetWindowLong(m_hWnd, GWL_STYLE, dwWindowStyle = WS_POPUP | WS_VISIBLE);
541518
}
542519

543520
if (!GEnv.isDedicatedServer)

0 commit comments

Comments
 (0)