Skip to content

Commit 63e2ba5

Browse files
committed
Render availability check refactor
1 parent bc09520 commit 63e2ba5

File tree

6 files changed

+83
-157
lines changed

6 files changed

+83
-157
lines changed

src/xrEngine/EngineAPI.cpp

Lines changed: 69 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
#include "stdafx.h"
66
#include "EngineAPI.h"
7+
#include "XR_IOConsole.h"
78

89
#include "xrCore/ModuleLookup.hpp"
910

10-
extern xr_token* vid_quality_token;
11+
extern xr_vector<xr_token> vid_quality_token;
1112

1213
//////////////////////////////////////////////////////////////////////
1314
// Construction/Destruction
@@ -32,25 +33,20 @@ CEngineAPI::CEngineAPI()
3233

3334
CEngineAPI::~CEngineAPI()
3435
{
35-
// destroy quality token here
36-
if (vid_quality_token)
37-
{
38-
xr_free(vid_quality_token);
39-
vid_quality_token = NULL;
40-
}
36+
vid_quality_token.clear();
4137
}
4238

43-
extern u32 renderer_value; // con cmd
44-
4539
bool is_enough_address_space_available()
4640
{
4741
SYSTEM_INFO system_info;
4842
GetSystemInfo(&system_info);
4943
return (*(u32*)&system_info.lpMaximumApplicationAddress) > 0x90000000;
5044
}
5145

52-
void CEngineAPI::InitializeNotDedicated()
46+
void CEngineAPI::SetupCurrentRenderer()
5347
{
48+
GEnv.CurrentRenderer = -1;
49+
5450
if (psDeviceFlags.test(rsR4))
5551
{
5652
if (hRenderR4->exist())
@@ -87,30 +83,51 @@ void CEngineAPI::InitializeNotDedicated()
8783
GEnv.SetupCurrentRenderer = GEnv.SetupR2;
8884
}
8985
else
86+
{
9087
psDeviceFlags.set(rsR2, false);
88+
psDeviceFlags.set(rsR1, true);
89+
}
90+
}
91+
92+
if (psDeviceFlags.test(rsR1))
93+
{
94+
if (hRenderR2->exist())
95+
{
96+
GEnv.CurrentRenderer = 1;
97+
GEnv.SetupCurrentRenderer = GEnv.SetupR1;
98+
}
99+
else
100+
psDeviceFlags.set(rsR1, false);
91101
}
92102
}
93103

94104
void CEngineAPI::InitializeRenderers()
95-
{
96-
if (!GEnv.isDedicatedServer)
97-
InitializeNotDedicated();
105+
{
106+
SetupCurrentRenderer();
98107

99-
if (!psDeviceFlags.test(rsR4|rsR3|rsR2))
108+
if (GEnv.SetupCurrentRenderer == nullptr
109+
&& vid_quality_token[0].id != -1)
100110
{
101-
R_ASSERT(hRenderR1);
102-
renderer_value = 0; // con cmd
103-
GEnv.CurrentRenderer = 1;
104-
GEnv.SetupCurrentRenderer = GEnv.SetupR1;
111+
// if engine failed to load renderer
112+
// but there is at least one available
113+
// then try again
114+
string32 buf;
115+
xr_sprintf(buf, "renderer %s", vid_quality_token[0].name);
116+
Console->Execute(buf);
117+
118+
// Second attempt
119+
InitializeRenderers();
105120
}
106121

107122
// ask current renderer to setup GlobalEnv
108-
R_ASSERT(GEnv.SetupCurrentRenderer);
123+
R_ASSERT2(GEnv.SetupCurrentRenderer, "Can't setup renderer");
109124
GEnv.SetupCurrentRenderer();
110125
}
111126

112127
void CEngineAPI::Initialize(void)
113128
{
129+
InitializeRenderers();
130+
114131
hGame = std::make_unique<XRay::Module>("xrGame");
115132
R_ASSERT2(hGame, "Game DLL raised exception during loading or there is no game DLL at all");
116133

@@ -159,20 +176,18 @@ void CEngineAPI::CreateRendererList()
159176
{
160177
hRenderR1 = std::make_unique<XRay::Module>("xrRender_R1");
161178

162-
if (GEnv.isDedicatedServer)
179+
xr_vector<xr_token> modes;
180+
// load only R1 for dedicated server,
181+
// but if it's unavailable, then try other renderers
182+
if (GEnv.isDedicatedServer && hRenderR1->exist())
163183
{
164-
vid_quality_token = xr_alloc<xr_token>(2);
165-
166-
vid_quality_token[0].id = 0;
167-
vid_quality_token[0].name = xr_strdup("renderer_r1");
168-
169-
vid_quality_token[1].id = -1;
170-
vid_quality_token[1].name = nullptr;
171-
184+
modes.push_back(xr_token("renderer_r1", 0));
185+
modes.push_back(xr_token(nullptr, -1));
186+
vid_quality_token = std::move(modes);
172187
return;
173188
}
174189

175-
if (vid_quality_token != nullptr)
190+
if (!vid_quality_token.empty())
176191
return;
177192

178193
// Hide "d3d10.dll not found" message box for XP
@@ -185,127 +200,40 @@ void CEngineAPI::CreateRendererList()
185200
// Restore error handling
186201
SetErrorMode(0);
187202

188-
bool bSupports_r2 = false;
189-
bool bSupports_r2_5 = false;
190-
bool bSupports_r3 = false;
191-
bool bSupports_r4 = false;
192-
193-
if (strstr(Core.Params, "-perfhud_hack"))
203+
if (hRenderR1->exist())
194204
{
195-
bSupports_r2 = true;
196-
bSupports_r2_5 = true;
197-
bSupports_r3 = true;
198-
bSupports_r4 = true;
205+
modes.push_back(xr_token("renderer_r1", 0));
199206
}
200-
else
207+
208+
if (hRenderR2->exist())
201209
{
202-
if (hRenderR2->exist())
203-
{
204-
bSupports_r2 = true;
205-
if (GEnv.CheckR2 && GEnv.CheckR2())
206-
bSupports_r2_5 = true;
207-
}
208-
if (hRenderR3->exist())
209-
{
210-
if (GEnv.CheckR3 && GEnv.CheckR3())
211-
bSupports_r3 = true;
212-
else
213-
hRenderR3->close();
214-
}
215-
if (hRenderR4->exist())
216-
{
217-
if (GEnv.CheckR4 && GEnv.CheckR4())
218-
bSupports_r4 = true;
219-
else
220-
hRenderR4->close();
221-
}
210+
modes.push_back(xr_token("renderer_r2a", 1));
211+
modes.push_back(xr_token("renderer_r2", 2));
212+
if (GEnv.CheckR2 && GEnv.CheckR2())
213+
modes.push_back(xr_token("renderer_r2.5", 3));
222214
}
223215

224-
bool proceed = true;
225-
xr_vector<pcstr> tmp;
226-
tmp.push_back("renderer_r1");
227-
if (proceed &= bSupports_r2, proceed)
216+
if (hRenderR3->exist())
228217
{
229-
tmp.push_back("renderer_r2a");
230-
tmp.push_back("renderer_r2");
218+
if (GEnv.CheckR3 && GEnv.CheckR3())
219+
modes.push_back(xr_token("renderer_r3", 4));
220+
else
221+
hRenderR3->close();
231222
}
232-
if (proceed &= bSupports_r2_5, proceed)
233-
tmp.push_back("renderer_r2.5");
234-
if (proceed &= bSupports_r3, proceed)
235-
tmp.push_back("renderer_r3");
236-
if (proceed &= bSupports_r4, proceed)
237-
tmp.push_back("renderer_r4");
238-
u32 _cnt = tmp.size() + 1;
239-
vid_quality_token = xr_alloc<xr_token>(_cnt);
240-
241-
vid_quality_token[_cnt - 1].id = -1;
242-
vid_quality_token[_cnt - 1].name = NULL;
243-
244-
Msg("Available render modes[%d]:", tmp.size());
245-
for (u32 i = 0; i < tmp.size(); ++i)
223+
224+
if (hRenderR4->exist())
246225
{
247-
vid_quality_token[i].id = i;
248-
vid_quality_token[i].name = tmp[i];
249-
Msg("[%s]", tmp[i]);
226+
if (GEnv.CheckR4 && GEnv.CheckR4())
227+
modes.push_back(xr_token("renderer_r4", 5));
228+
else
229+
hRenderR4->close();
250230
}
231+
modes.push_back(xr_token(nullptr, -1));
251232

252-
/*
253-
if(vid_quality_token != NULL) return;
254-
255-
D3DCAPS9 caps;
256-
CHW _HW;
257-
_HW.CreateD3D ();
258-
_HW.pD3D->GetDeviceCaps (D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,&caps);
259-
_HW.DestroyD3D ();
260-
u16 ps_ver_major = u16 ( u32(u32(caps.PixelShaderVersion)&u32(0xf << 8ul))>>8 );
261-
262-
xr_vector<LPCSTR> _tmp;
263-
u32 i = 0;
264-
for(; i<5; ++i)
265-
{
266-
bool bBreakLoop = false;
267-
switch (i)
268-
{
269-
case 3: //"renderer_r2.5"
270-
if (ps_ver_major < 3)
271-
bBreakLoop = true;
272-
break;
273-
case 4: //"renderer_r_dx10"
274-
bBreakLoop = true;
275-
break;
276-
default: ;
277-
}
233+
Msg("Available render modes[%d]:", modes.size());
234+
for (auto& mode : modes)
235+
if (mode.name)
236+
Log(mode.name);
278237

279-
if (bBreakLoop) break;
280-
281-
_tmp.push_back (NULL);
282-
LPCSTR val = NULL;
283-
switch (i)
284-
{
285-
case 0: val ="renderer_r1"; break;
286-
case 1: val ="renderer_r2a"; break;
287-
case 2: val ="renderer_r2"; break;
288-
case 3: val ="renderer_r2.5"; break;
289-
case 4: val ="renderer_r_dx10"; break; // -)
290-
}
291-
_tmp.back() = xr_strdup(val);
292-
}
293-
u32 _cnt = _tmp.size()+1;
294-
vid_quality_token = xr_alloc<xr_token>(_cnt);
295-
296-
vid_quality_token[_cnt-1].id = -1;
297-
vid_quality_token[_cnt-1].name = NULL;
298-
299-
#ifdef DEBUG
300-
Msg("Available render modes[%d]:",_tmp.size());
301-
#endif // DEBUG
302-
for(u32 i=0; i<_tmp.size();++i)
303-
{
304-
vid_quality_token[i].id = i;
305-
vid_quality_token[i].name = _tmp[i];
306-
#ifdef DEBUG
307-
Msg ("[%s]",_tmp[i]);
308-
#endif // DEBUG
309-
}
310-
*/
238+
vid_quality_token = std::move(modes);
311239
}

src/xrEngine/EngineAPI.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class ENGINE_API CEngineAPI
6363
void Initialize();
6464

6565
void InitializeRenderers();
66-
void InitializeNotDedicated();
66+
void SetupCurrentRenderer();
6767

6868
void Destroy();
6969

src/xrEngine/defines.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ enum
3434
mtParticles = (1ul << 17ul),
3535

3636
rsCameraPos = (1ul << 18ul),
37-
rsR2 = (1ul << 19ul),
38-
rsR3 = (1ul << 20ul),
39-
rsR4 = (1ul << 21ul),
40-
// 22-32 bit - reserved to Editor
37+
38+
rsR1 = (1ul << 19ul),
39+
rsR2 = (1ul << 20ul),
40+
rsR3 = (1ul << 21ul),
41+
rsR4 = (1ul << 22ul), // 22 was reserved for editor too
42+
// 23-32 bit - reserved to Editor
4143
};
4244

4345
//. ENGINE_API extern u32 psCurrentMode ;

src/xrEngine/main.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,6 @@ int RunApplication(pcstr commandLine)
404404
else
405405
Console->Execute("renderer renderer_r1");
406406

407-
Engine.External.InitializeRenderers();
408407
Engine.External.Initialize();
409408
Startup();
410409
Core._destroy();

src/xrEngine/xrSASH.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,6 @@ void xrSASH::TryInitEngine(bool bNoRun)
481481

482482
InitInput();
483483

484-
Engine.External.InitializeRenderers();
485484
Engine.External.Initialize();
486485

487486
Console->Execute("unbindall");

src/xrEngine/xr_ioc_cmd.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "xr_object.h"
1414
#include "xr_object_list.h"
1515

16-
xr_token* vid_quality_token = NULL;
16+
xr_vector<xr_token> vid_quality_token;
1717

1818
xr_token vid_bpp_token[] = {{"16", 16}, {"32", 32}, {0, 0}};
1919
//-----------------------------------------------------------------------
@@ -537,13 +537,14 @@ class CCC_r2 : public CCC_Token
537537
virtual ~CCC_r2() {}
538538
virtual void Execute(LPCSTR args)
539539
{
540-
// vid_quality_token must be already created!
541-
tokens = vid_quality_token;
540+
tokens = vid_quality_token.data();
542541

543542
inherited::Execute(args);
544543
// 0 - r1
545544
// 1..3 - r2
546545
// 4 - r3
546+
// 5 - r4
547+
psDeviceFlags.set(rsR1, renderer_value == 0);
547548
psDeviceFlags.set(rsR2, ((renderer_value > 0) && renderer_value < 4));
548549
psDeviceFlags.set(rsR3, (renderer_value == 4));
549550
psDeviceFlags.set(rsR4, (renderer_value >= 5));
@@ -556,15 +557,12 @@ class CCC_r2 : public CCC_Token
556557
virtual void Save(IWriter* F)
557558
{
558559
// fill_render_mode_list ();
559-
tokens = vid_quality_token;
560-
if (!strstr(Core.Params, "-r2"))
561-
{
562-
inherited::Save(F);
563-
}
560+
tokens = vid_quality_token.data();
561+
inherited::Save(F);
564562
}
565563
virtual xr_token* GetToken()
566564
{
567-
tokens = vid_quality_token;
565+
tokens = vid_quality_token.data();
568566
return inherited::GetToken();
569567
}
570568
};

0 commit comments

Comments
 (0)