forked from themrdemonized/xray-monolith
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathr4_R_render.cpp
More file actions
604 lines (538 loc) · 16.4 KB
/
r4_R_render.cpp
File metadata and controls
604 lines (538 loc) · 16.4 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
#include "stdafx.h"
#include "../../xrEngine/igame_persistent.h"
#include "../xrRender/FBasicVisual.h"
#include "../../xrEngine/customhud.h"
#include "../../xrEngine/xr_object.h"
#include "../xrRender/QueryHelper.h"
IC bool pred_sp_sort(ISpatial* _1, ISpatial* _2)
{
float d1 = _1->spatial.sphere.P.distance_to_sqr(Device.vCameraPosition);
float d2 = _2->spatial.sphere.P.distance_to_sqr(Device.vCameraPosition);
return d1 < d2;
}
void CRender::render_main(Fmatrix& m_ViewProjection, bool _fportals)
{
PIX_EVENT(render_main);
// Msg ("---begin");
marker ++;
// Calculate sector(s) and their objects
if (pLastSector)
{
//!!!
//!!! BECAUSE OF PARALLEL HOM RENDERING TRY TO DELAY ACCESS TO HOM AS MUCH AS POSSIBLE
//!!!
{
// Traverse object database
g_SpatialSpace->q_frustum
(
lstRenderables,
ISpatial_DB::O_ORDERED,
STYPE_RENDERABLE + STYPE_LIGHTSOURCE,
ViewBase
);
// (almost) Exact sorting order (front-to-back)
std::sort(lstRenderables.begin(), lstRenderables.end(), pred_sp_sort);
// Determine visibility for dynamic part of scene
set_Object(0);
u32 uID_LTRACK = 0xffffffff;
if (phase == PHASE_NORMAL)
{
uLastLTRACK ++;
if (lstRenderables.size()) uID_LTRACK = uLastLTRACK % lstRenderables.size();
// update light-vis for current entity / actor
CObject* O = g_pGameLevel->CurrentViewEntity();
if (O)
{
CROS_impl* R = (CROS_impl*)O->ROS();
if (R) R->update(O);
}
// update light-vis for selected entity
// track lighting environment
if (lstRenderables.size())
{
IRenderable* renderable = lstRenderables[uID_LTRACK]->dcast_Renderable();
if (renderable)
{
CROS_impl* T = (CROS_impl*)renderable->renderable_ROS();
if (T) T->update(renderable);
}
}
}
}
// Traverse sector/portal structure
PortalTraverser.traverse
(
pLastSector,
ViewBase,
Device.vCameraPosition,
m_ViewProjection,
CPortalTraverser::VQ_HOM + CPortalTraverser::VQ_SSA + CPortalTraverser::VQ_FADE
//. disabled scissoring (HW.Caps.bScissor?CPortalTraverser::VQ_SCISSOR:0) // generate scissoring info
);
// Determine visibility for static geometry hierrarhy
for (u32 s_it = 0; s_it < PortalTraverser.r_sectors.size(); s_it++)
{
CSector* sector = (CSector*)PortalTraverser.r_sectors[s_it];
dxRender_Visual* root = sector->root();
for (u32 v_it = 0; v_it < sector->r_frustums.size(); v_it++)
{
set_Frustum(&(sector->r_frustums[v_it]));
add_Geometry(root);
}
}
// Traverse frustums
for (u32 o_it = 0; o_it < lstRenderables.size(); o_it++)
{
ISpatial* spatial = lstRenderables[o_it];
spatial->spatial_updatesector();
CSector* sector = (CSector*)spatial->spatial.sector;
if (0 == sector) continue; // disassociated from S/P structure
if (spatial->spatial.type & STYPE_LIGHTSOURCE)
{
// lightsource
light* L = (light*)(spatial->dcast_Light());
VERIFY(L);
float lod = L->get_LOD();
if (lod > EPS_L)
{
vis_data& vis = L->get_homdata();
if (HOM.visible(vis)) Lights.add_light(L);
}
continue ;
}
if (PortalTraverser.i_marker != sector->r_marker) continue; // inactive (untouched) sector
for (u32 v_it = 0; v_it < sector->r_frustums.size(); v_it++)
{
CFrustum& view = sector->r_frustums[v_it];
if (!view.testSphere_dirty(spatial->spatial.sphere.P, spatial->spatial.sphere.R)) continue;
if (spatial->spatial.type & STYPE_RENDERABLE)
{
// renderable
IRenderable* renderable = spatial->dcast_Renderable();
VERIFY(renderable);
// Occlusion
// casting is faster then using getVis method
vis_data& v_orig = ((dxRender_Visual*)renderable->renderable.visual)->vis;
vis_data v_copy = v_orig;
v_copy.box.xform(renderable->renderable.xform);
BOOL bVisible = HOM.visible(v_copy);
v_orig.marker = v_copy.marker;
v_orig.accept_frame = v_copy.accept_frame;
v_orig.hom_frame = v_copy.hom_frame;
v_orig.hom_tested = v_copy.hom_tested;
if (!bVisible) break; // exit loop on frustums
// Rendering
set_Object(renderable);
renderable->renderable_Render();
set_Object(0);
}
break; // exit loop on frustums
}
}
if (g_pGameLevel && (phase == PHASE_NORMAL))
{
g_hud->Render_Last(); // HUD
if (g_hud->RenderActiveItemUIQuery())
r_dsgraph_render_hud_ui();
}
}
else
{
set_Object(0);
if (g_pGameLevel && (phase == PHASE_NORMAL))
{
g_hud->Render_Last(); // HUD
if (g_hud->RenderActiveItemUIQuery())
r_dsgraph_render_hud_ui();
}
}
}
void CRender::render_menu()
{
PIX_EVENT(render_menu);
// Globals
RCache.set_CullMode(CULL_CCW);
RCache.set_Stencil(FALSE);
RCache.set_ColorWriteEnable();
// Main Render
{
Target->u_setrt(Target->rt_Generic_0, 0, 0, HW.pBaseZB); // LDR RT
g_pGamePersistent->OnRenderPPUI_main(); // PP-UI
}
// Distort
{
FLOAT ColorRGBA[4] = {127.0f / 255.0f, 127.0f / 255.0f, 0.0f, 127.0f / 255.0f};
Target->u_setrt(Target->rt_Generic_1, 0, 0, HW.pBaseZB); // Now RT is a distortion mask
HW.pContext->ClearRenderTargetView(Target->rt_Generic_1->pRT, ColorRGBA);
g_pGamePersistent->OnRenderPPUI_PP(); // PP-UI
}
// Actual Display
Target->u_setrt(Device.dwWidth, Device.dwHeight, HW.pBaseRT,NULL,NULL, HW.pBaseZB);
RCache.set_Shader(Target->s_menu);
RCache.set_Geometry(Target->g_menu);
Fvector2 p0, p1;
u32 Offset;
u32 C = color_rgba(255, 255, 255, 255);
float _w = float(Device.dwWidth);
float _h = float(Device.dwHeight);
float d_Z = EPS_S;
float d_W = 1.f;
p0.set(.5f / _w, .5f / _h);
p1.set((_w + .5f) / _w, (_h + .5f) / _h);
FVF::TL* pv = (FVF::TL*)RCache.Vertex.Lock(4, Target->g_menu->vb_stride, Offset);
pv->set(EPS, float(_h + EPS), d_Z, d_W, C, p0.x, p1.y);
pv++;
pv->set(EPS, EPS, d_Z, d_W, C, p0.x, p0.y);
pv++;
pv->set(float(_w + EPS), float(_h + EPS), d_Z, d_W, C, p1.x, p1.y);
pv++;
pv->set(float(_w + EPS), EPS, d_Z, d_W, C, p1.x, p0.y);
pv++;
RCache.Vertex.Unlock(4, Target->g_menu->vb_stride);
RCache.Render(D3DPT_TRIANGLELIST, Offset, 0, 4, 0, 2);
}
extern u32 g_r;
void CRender::Render()
{
PIX_EVENT(CRender_Render);
VERIFY(0==mapDistort.size());
rmNormal();
bool _menu_pp = g_pGamePersistent ? g_pGamePersistent->OnRenderPPUI_query() : false;
if (_menu_pp)
{
render_menu();
return;
};
IMainMenu* pMainMenu = g_pGamePersistent ? g_pGamePersistent->m_pMainMenu : 0;
bool bMenu = pMainMenu ? pMainMenu->CanSkipSceneRendering() : false;
if (!(g_pGameLevel && g_hud)
|| bMenu)
{
Target->u_setrt(Device.dwWidth, Device.dwHeight, HW.pBaseRT,NULL,NULL, HW.pBaseZB);
return;
}
if (m_bFirstFrameAfterReset)
{
m_bFirstFrameAfterReset = false;
return;
}
//. VERIFY (g_pGameLevel && g_pGameLevel->pHUD);
// Configure
RImplementation.o.distortion = FALSE; // disable distorion
Fcolor sun_color = ((light*)Lights.sun_adapted._get())->color;
BOOL bSUN = ps_r2_ls_flags.test(R2FLAG_SUN) && (u_diffuse2s(sun_color.r, sun_color.g, sun_color.b)>EPS) && !strstr(Core.Params, "-r4_dev");
if (o.sunstatic) bSUN = FALSE;
// Msg ("sstatic: %s, sun: %s",o.sunstatic?;"true":"false", bSUN?"true":"false");
// HOM
ViewBase.CreateFromMatrix(Device.mFullTransform, FRUSTUM_P_LRTB + FRUSTUM_P_FAR);
View = 0;
if (!ps_r2_ls_flags.test(R2FLAG_EXP_MT_CALC))
{
HOM.Enable();
HOM.Render(ViewBase);
}
//******* Z-prefill calc - DEFERRER RENDERER
if (ps_r2_ls_flags.test(R2FLAG_ZFILL))
{
PIX_EVENT(DEFER_Z_FILL);
Device.Statistic->RenderCALC.Begin();
float z_distance = ps_r2_zfill;
Fmatrix m_zfill, m_project;
m_project.build_projection(
deg2rad(Device.fFOV/* *Device.fASPECT*/),
Device.fASPECT, VIEWPORT_NEAR,
z_distance * g_pGamePersistent->Environment().CurrentEnv->far_plane);
m_zfill.mul(m_project, Device.mView);
r_pmask(true, false); // enable priority "0"
set_Recorder(NULL);
phase = PHASE_SMAP;
render_main(m_zfill, false);
r_pmask(true, false); // disable priority "1"
Device.Statistic->RenderCALC.End();
// flush
Target->phase_scene_prepare();
RCache.set_ColorWriteEnable(FALSE);
r_dsgraph_render_graph(0);
RCache.set_ColorWriteEnable();
}
else
{
Target->phase_scene_prepare();
}
//*******
// Sync point
Device.Statistic->RenderDUMP_Wait_S.Begin();
if (ps_r2_qsync)
{
CTimer T;
T.Start();
BOOL result = FALSE;
HRESULT hr = S_FALSE;
//while ((hr=q_sync_point[q_sync_count]->GetData (&result,sizeof(result),D3DGETDATA_FLUSH))==S_FALSE) {
while ((hr = GetData(q_sync_point[q_sync_count], &result, sizeof(result))) == S_FALSE)
{
if (!SwitchToThread()) Sleep(ps_r2_wait_sleep);
if (T.GetElapsed_ms() > 500)
{
result = FALSE;
break;
}
}
}
Device.Statistic->RenderDUMP_Wait_S.End();
q_sync_count = (q_sync_count + 1) % HW.Caps.iGPUNum;
//CHK_DX (q_sync_point[q_sync_count]->Issue(D3DISSUE_END));
CHK_DX(EndQuery(q_sync_point[q_sync_count]));
//******* Main calc - DEFERRER RENDERER
// Main calc
Device.Statistic->RenderCALC.Begin();
r_pmask(true, false, true); // enable priority "0",+ capture wmarks
if (bSUN) set_Recorder(&main_coarse_structure);
else set_Recorder(NULL);
phase = PHASE_NORMAL;
render_main(Device.mFullTransform, true);
set_Recorder(NULL);
r_pmask(true, false); // disable priority "1"
Device.Statistic->RenderCALC.End();
if (ps_r2_ls_flags.test(R2FLAG_TERRAIN_PREPASS))
{
Target->u_setrt(Device.dwWidth, Device.dwHeight, NULL, NULL, NULL, !RImplementation.o.dx10_msaa ? HW.pBaseZB : Target->rt_MSAADepth->pZRT);
r_dsgraph_render_landscape(0, false);
}
BOOL split_the_scene_to_minimize_wait = FALSE;
if (ps_r2_ls_flags.test(R2FLAG_EXP_SPLIT_SCENE)) split_the_scene_to_minimize_wait = TRUE;
//******* Main render :: PART-0 -- first
if (!split_the_scene_to_minimize_wait)
{
PIX_EVENT(DEFER_PART0_NO_SPLIT);
// level, DO NOT SPLIT
Target->phase_scene_begin();
r_dsgraph_render_hud();
r_dsgraph_render_graph(0);
r_dsgraph_render_lods(true, true);
if (Details) Details->Render();
if (ps_r2_ls_flags.test(R2FLAG_TERRAIN_PREPASS)) r_dsgraph_render_landscape(1, true);
Target->phase_scene_end();
}
else
{
PIX_EVENT(DEFER_PART0_SPLIT);
// level, SPLIT
Target->phase_scene_begin();
r_dsgraph_render_graph(0);
Target->disable_aniso();
}
//******* Occlusion testing of volume-limited light-sources
Target->phase_occq();
LP_normal.clear();
LP_pending.clear();
if (RImplementation.o.dx10_msaa)
RCache.set_ZB(RImplementation.Target->rt_MSAADepth->pZRT);
{
PIX_EVENT(DEFER_TEST_LIGHT_VIS);
// perform tests
u32 count = 0;
light_Package& LP = Lights.package;
// stats
stats.l_shadowed = LP.v_shadowed.size();
stats.l_unshadowed = LP.v_point.size() + LP.v_spot.size();
stats.l_total = stats.l_shadowed + stats.l_unshadowed;
// perform tests
count = _max(count, LP.v_point.size());
count = _max(count, LP.v_spot.size());
count = _max(count, LP.v_shadowed.size());
for (u32 it = 0; it < count; it++)
{
if (it < LP.v_point.size())
{
light* L = LP.v_point[it];
L->vis_prepare();
if (L->vis.pending) LP_pending.v_point.push_back(L);
else LP_normal.v_point.push_back(L);
}
if (it < LP.v_spot.size())
{
light* L = LP.v_spot[it];
L->vis_prepare();
if (L->vis.pending) LP_pending.v_spot.push_back(L);
else LP_normal.v_spot.push_back(L);
}
if (it < LP.v_shadowed.size())
{
light* L = LP.v_shadowed[it];
L->vis_prepare();
if (L->vis.pending) LP_pending.v_shadowed.push_back(L);
else LP_normal.v_shadowed.push_back(L);
}
}
}
LP_normal.sort();
LP_pending.sort();
//******* Main render :: PART-1 (second)
if (split_the_scene_to_minimize_wait)
{
PIX_EVENT(DEFER_PART1_SPLIT);
// skybox can be drawn here
if (0)
{
if (!RImplementation.o.dx10_msaa)
Target->u_setrt(Target->rt_Generic_0, Target->rt_Generic_1, 0, HW.pBaseZB);
else
Target->u_setrt(Target->rt_Generic_0_r, Target->rt_Generic_1, 0,
RImplementation.Target->rt_MSAADepth->pZRT);
RCache.set_CullMode(CULL_NONE);
RCache.set_Stencil(FALSE);
// draw skybox
RCache.set_ColorWriteEnable();
//CHK_DX(HW.pDevice->SetRenderState ( D3DRS_ZENABLE, FALSE ));
RCache.set_Z(FALSE);
g_pGamePersistent->Environment().RenderSky();
//CHK_DX(HW.pDevice->SetRenderState ( D3DRS_ZENABLE, TRUE ));
RCache.set_Z(TRUE);
}
ID3D11Resource* zbuffer_res;
HW.pBaseZB->GetResource(&zbuffer_res); //get the resource
HW.pContext->CopyResource(RImplementation.Target->rt_tempzb->pSurface, zbuffer_res);
// level
Target->phase_scene_begin();
r_dsgraph_render_hud();
r_dsgraph_render_lods(true, true);
if (Details) Details->Render();
if (ps_r2_ls_flags.test(R2FLAG_TERRAIN_PREPASS)) r_dsgraph_render_landscape(1, true);
Target->phase_scene_end();
}
// Wall marks
if (Wallmarks)
{
PIX_EVENT(DEFER_WALLMARKS);
Target->phase_wallmarks();
Wallmarks->Render(); // wallmarks has priority as normal geometry
}
// Update incremental shadowmap-visibility solver
{
PIX_EVENT(DEFER_FLUSH_OCCLUSION);
u32 it = 0;
for (it = 0; it < Lights_LastFrame.size(); it++)
{
if (0 == Lights_LastFrame[it]) continue ;
try
{
Lights_LastFrame[it]->svis.flushoccq();
}
catch (...)
{
Msg("! Failed to flush-OCCq on light [%d] %X", it, *(u32*)(&Lights_LastFrame[it]));
}
}
Lights_LastFrame.clear();
}
// full screen pass to mark msaa-edge pixels in highest stencil bit
if (RImplementation.o.dx10_msaa)
{
PIX_EVENT(MARK_MSAA_EDGES);
Target->mark_msaa_edges();
}
// TODO: DX10: Implement DX10 rain.
if (ps_r2_ls_flags.test(R3FLAG_DYN_WET_SURF))
{
PIX_EVENT(DEFER_RAIN);
render_rain();
}
// Directional light - fucking sun
if (bSUN) //bSUN && Device.dwFrame & 1 --Delayed sun update. Worth to check it in future
{
PIX_EVENT(DEFER_SUN);
RImplementation.stats.l_visible ++;
if (!ps_r2_ls_flags_ext.is(R2FLAGEXT_SUN_OLD))
render_sun_cascades();
else
{
render_sun_near();
render_sun();
render_sun_filtered();
}
Target->accum_direct_blend();
}
{
PIX_EVENT(DEFER_SELF_ILLUM);
Target->phase_accumulator();
// Render emissive geometry, stencil - write 0x0 at pixel pos
RCache.set_xform_project(Device.mProject);
RCache.set_xform_view(Device.mView);
// Stencil - write 0x1 at pixel pos -
if (!RImplementation.o.dx10_msaa)
RCache.set_Stencil(TRUE, D3DCMP_ALWAYS, 0x01, 0xff, 0xff, D3DSTENCILOP_KEEP, D3DSTENCILOP_REPLACE,
D3DSTENCILOP_KEEP);
else
RCache.set_Stencil(TRUE, D3DCMP_ALWAYS, 0x01, 0xff, 0x7f, D3DSTENCILOP_KEEP, D3DSTENCILOP_REPLACE,
D3DSTENCILOP_KEEP);
//RCache.set_Stencil (TRUE,D3DCMP_ALWAYS,0x00,0xff,0xff,D3DSTENCILOP_KEEP,D3DSTENCILOP_REPLACE,D3DSTENCILOP_KEEP);
RCache.set_CullMode(CULL_CCW);
RCache.set_ColorWriteEnable();
RImplementation.r_dsgraph_render_emissive();
}
// Lighting, non dependant on OCCQ
{
PIX_EVENT(DEFER_LIGHT_NO_OCCQ);
Target->phase_accumulator();
HOM.Disable();
render_lights(LP_normal);
}
// Lighting, dependant on OCCQ
{
PIX_EVENT(DEFER_LIGHT_OCCQ);
render_lights(LP_pending);
}
{
if (RImplementation.o.ssfx_volumetric)
Target->phase_ssfx_volumetric_blur();
}
// Postprocess
{
PIX_EVENT(DEFER_LIGHT_COMBINE);
Target->phase_combine();
}
if (Details)
Details->details_clear();
VERIFY(0==mapDistort.size());
}
void CRender::render_forward()
{
VERIFY(0==mapDistort.size());
RImplementation.o.distortion = RImplementation.o.distortion_enabled; // enable distorion
//******* Main render - second order geometry (the one, that doesn't support deffering)
//.todo: should be done inside "combine" with estimation of of luminance, tone-mapping, etc.
{
// level
r_pmask(false, true); // enable priority "1"
phase = PHASE_NORMAL;
render_main(Device.mFullTransform, false); //
// Igor: we don't want to render old lods on next frame.
mapLOD.clear();
r_dsgraph_render_graph(1); // normal level, secondary priority
PortalTraverser.fade_render(); // faded-portals
r_dsgraph_render_sorted(); // strict-sorted geoms
//g_pGamePersistent->Environment().RenderLast(); // rain/thunder-bolts
}
RImplementation.o.distortion = FALSE; // disable distorion
}
void CRender::RenderToTarget(RRT target)
{
ref_rt* RT = nullptr;
switch (target)
{
case rtPDA:
RT = &Target->rt_ui_pda;
break;
case rtSVP:
RT = &Target->rt_secondVP;
break;
default:
Debug.fatal(DEBUG_INFO, "None or wrong Target specified: %i", target);
break;
}
ID3DTexture2D* pBuffer = nullptr;
HW.m_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBuffer);
HW.pContext->CopyResource((*RT)->pSurface, pBuffer);
pBuffer->Release();
}