forked from themrdemonized/xray-monolith
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathr4_rendertarget.cpp
More file actions
1323 lines (1147 loc) · 39 KB
/
r4_rendertarget.cpp
File metadata and controls
1323 lines (1147 loc) · 39 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
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
#include "stdafx.h"
#include "../xrRender/resourcemanager.h"
#include "blender_light_occq.h"
#include "blender_light_mask.h"
#include "blender_light_direct.h"
#include "blender_light_point.h"
#include "blender_light_spot.h"
#include "blender_light_reflected.h"
#include "blender_combine.h"
#include "blender_bloom_build.h"
#include "blender_luminance.h"
#include "blender_ssao.h"
#include "dx11MinMaxSMBlender.h"
#include "dx11HDAOCSBlender.h"
#include "../xrRenderDX10/msaa/dx10MSAABlender.h"
#include "../xrRenderDX10/DX10 Rain/dx10RainBlender.h"
////////////////////////////lvutner
#include "blender_ss_sunshafts.h"
#include "blender_gasmask_drops.h"
#include "blender_gasmask_dudv.h"
#include "blender_smaa.h"
#include "blender_blur.h"
#include "blender_dof.h"
#include "blender_pp_bloom.h"
#include "blender_nightvision.h"
#include "blender_lut.h"
#include "../xrRender/dxRenderDeviceRender.h"
#include <D3DX10Tex.h>
D3D_VIEWPORT custom_viewport[1] = { 0, 0, 0, 0, 0.f, 1.f };
void CRenderTarget::set_viewport_size(ID3DDeviceContext * dev, float w, float h)
{
custom_viewport[0].Width = w;
custom_viewport[0].Height = h;
dev->RSSetViewports(1, custom_viewport);
}
void CRenderTarget::u_setrt(const ref_rt& _1, const ref_rt& _2, const ref_rt& _3, ID3DDepthStencilView* zb)
{
VERIFY(_1||zb);
if (_1)
{
dwWidth = _1->dwWidth;
dwHeight = _1->dwHeight;
}
else
{
D3D_DEPTH_STENCIL_VIEW_DESC desc;
zb->GetDesc(&desc);
if (!RImplementation.o.dx10_msaa)
VERIFY(desc.ViewDimension==D3D_DSV_DIMENSION_TEXTURE2D);
ID3DResource* pRes;
zb->GetResource(&pRes);
ID3DTexture2D* pTex = (ID3DTexture2D *)pRes;
D3D_TEXTURE2D_DESC TexDesc;
pTex->GetDesc(&TexDesc);
dwWidth = TexDesc.Width;
dwHeight = TexDesc.Height;
_RELEASE(pRes);
}
if (_1) RCache.set_RT(_1->pRT, 0);
else RCache.set_RT(NULL, 0);
if (_2) RCache.set_RT(_2->pRT, 1);
else RCache.set_RT(NULL, 1);
if (_3) RCache.set_RT(_3->pRT, 2);
else RCache.set_RT(NULL, 2);
RCache.set_ZB(zb);
// RImplementation.rmNormal ();
}
void CRenderTarget::u_setrt(const ref_rt& _1, const ref_rt& _2, ID3DDepthStencilView* zb)
{
VERIFY(_1||zb);
if (_1)
{
dwWidth = _1->dwWidth;
dwHeight = _1->dwHeight;
}
else
{
D3D_DEPTH_STENCIL_VIEW_DESC desc;
zb->GetDesc(&desc);
if (! RImplementation.o.dx10_msaa)
VERIFY(desc.ViewDimension==D3D_DSV_DIMENSION_TEXTURE2D);
ID3DResource* pRes;
zb->GetResource(&pRes);
ID3DTexture2D* pTex = (ID3DTexture2D *)pRes;
D3D_TEXTURE2D_DESC TexDesc;
pTex->GetDesc(&TexDesc);
dwWidth = TexDesc.Width;
dwHeight = TexDesc.Height;
_RELEASE(pRes);
}
if (_1) RCache.set_RT(_1->pRT, 0);
else RCache.set_RT(NULL, 0);
if (_2) RCache.set_RT(_2->pRT, 1);
else RCache.set_RT(NULL, 1);
RCache.set_ZB(zb);
// RImplementation.rmNormal ();
}
void CRenderTarget::u_setrt(u32 W, u32 H, ID3DRenderTargetView* _1, ID3DRenderTargetView* _2, ID3DRenderTargetView* _3,
ID3DDepthStencilView* zb)
{
//VERIFY (_1);
dwWidth = W;
dwHeight = H;
//VERIFY (_1);
RCache.set_RT(_1, 0);
RCache.set_RT(_2, 1);
RCache.set_RT(_3, 2);
RCache.set_ZB(zb);
// RImplementation.rmNormal ();
}
void CRenderTarget::u_stencil_optimize(eStencilOptimizeMode eSOM)
{
// TODO: DX10: remove half pixel offset?
VERIFY(RImplementation.o.nvstencil);
//RCache.set_ColorWriteEnable (FALSE);
u32 Offset;
float _w = float(Device.dwWidth);
float _h = float(Device.dwHeight);
u32 C = color_rgba(255, 255, 255, 255);
float eps = 0;
float _dw = 0.5f;
float _dh = 0.5f;
FVF::TL* pv = (FVF::TL*)RCache.Vertex.Lock(4, g_combine->vb_stride, Offset);
pv->set(-_dw, _h - _dh, eps, 1.f, C, 0, 0);
pv++;
pv->set(-_dw, -_dh, eps, 1.f, C, 0, 0);
pv++;
pv->set(_w - _dw, _h - _dh, eps, 1.f, C, 0, 0);
pv++;
pv->set(_w - _dw, -_dh, eps, 1.f, C, 0, 0);
pv++;
RCache.Vertex.Unlock(4, g_combine->vb_stride);
RCache.set_Element(s_occq->E[1]);
switch (eSOM)
{
case SO_Light:
StateManager.SetStencilRef(dwLightMarkerID);
break;
case SO_Combine:
StateManager.SetStencilRef(0x01);
break;
default:
VERIFY(!"CRenderTarget::u_stencil_optimize. switch no default!");
}
RCache.set_Geometry(g_combine);
RCache.Render(D3DPT_TRIANGLELIST, Offset, 0, 4, 0, 2);
}
// 2D texgen (texture adjustment matrix)
void CRenderTarget::u_compute_texgen_screen(Fmatrix& m_Texgen)
{
//float _w = float(Device.dwWidth);
//float _h = float(Device.dwHeight);
//float o_w = (.5f / _w);
//float o_h = (.5f / _h);
Fmatrix m_TexelAdjust =
{
0.5f, 0.0f, 0.0f, 0.0f,
0.0f, -0.5f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
// Removing half pixel offset
//0.5f + o_w, 0.5f + o_h, 0.0f, 1.0f
0.5f, 0.5f, 0.0f, 1.0f
};
m_Texgen.mul(m_TexelAdjust, RCache.xforms.m_wvp);
}
// 2D texgen for jitter (texture adjustment matrix)
void CRenderTarget::u_compute_texgen_jitter(Fmatrix& m_Texgen_J)
{
// place into 0..1 space
Fmatrix m_TexelAdjust =
{
0.5f, 0.0f, 0.0f, 0.0f,
0.0f, -0.5f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.0f, 1.0f
};
m_Texgen_J.mul(m_TexelAdjust, RCache.xforms.m_wvp);
// rescale - tile it
float scale_X = float(Device.dwWidth) / float(TEX_jitter);
float scale_Y = float(Device.dwHeight) / float(TEX_jitter);
//float offset = (.5f / float(TEX_jitter));
m_TexelAdjust.scale(scale_X, scale_Y, 1.f);
//m_TexelAdjust.translate_over(offset, offset, 0 );
m_Texgen_J.mulA_44(m_TexelAdjust);
}
u8 fpack(float v)
{
s32 _v = iFloor(((v + 1) * .5f) * 255.f + .5f);
clamp(_v, 0, 255);
return u8(_v);
}
u8 fpackZ(float v)
{
s32 _v = iFloor(_abs(v) * 255.f + .5f);
clamp(_v, 0, 255);
return u8(_v);
}
Fvector vunpack(s32 x, s32 y, s32 z)
{
Fvector pck;
pck.x = (float(x) / 255.f - .5f) * 2.f;
pck.y = (float(y) / 255.f - .5f) * 2.f;
pck.z = -float(z) / 255.f;
return pck;
}
Fvector vunpack(Ivector src)
{
return vunpack(src.x, src.y, src.z);
}
Ivector vpack(Fvector src)
{
Fvector _v;
int bx = fpack(src.x);
int by = fpack(src.y);
int bz = fpackZ(src.z);
// dumb test
float e_best = flt_max;
int r = bx, g = by, b = bz;
#ifdef DEBUG
int d=0;
#else
int d = 3;
#endif
for (int x = _max(bx - d, 0); x <= _min(bx + d, 255); x++)
for (int y = _max(by - d, 0); y <= _min(by + d, 255); y++)
for (int z = _max(bz - d, 0); z <= _min(bz + d, 255); z++)
{
_v = vunpack(x, y, z);
float m = _v.magnitude();
float me = _abs(m - 1.f);
if (me > 0.03f) continue;
_v.div(m);
float e = _abs(src.dotproduct(_v) - 1.f);
if (e < e_best)
{
e_best = e;
r = x, g = y, b = z;
}
}
Ivector ipck;
ipck.set(r, g, b);
return ipck;
}
void generate_jitter(DWORD* dest, u32 elem_count)
{
const int cmax = 8;
svector<Ivector2, cmax> samples;
while (samples.size() < elem_count * 2)
{
Ivector2 test;
test.set(::Random.randI(0, 256), ::Random.randI(0, 256));
BOOL valid = TRUE;
for (u32 t = 0; t < samples.size(); t++)
{
int dist = _abs(test.x - samples[t].x) + _abs(test.y - samples[t].y);
if (dist < 32)
{
valid = FALSE;
break;
}
}
if (valid) samples.push_back(test);
}
for (u32 it = 0; it < elem_count; it++, dest++)
*dest = color_rgba(samples[2 * it].x, samples[2 * it].y, samples[2 * it + 1].y, samples[2 * it + 1].x);
}
CRenderTarget::CRenderTarget()
{
u32 SampleCount = 1;
if (ps_r_ssao_mode != 2/*hdao*/)
ps_r_ssao = _min(ps_r_ssao, 3);
RImplementation.o.ssao_ultra = ps_r_ssao > 3;
if (RImplementation.o.dx10_msaa)
SampleCount = RImplementation.o.dx10_msaa_samples;
#ifdef DEBUG
Msg ("MSAA samples = %d", SampleCount );
if( RImplementation.o.dx10_msaa_opt )
Msg ("dx10_MSAA_opt = on" );
#endif // DEBUG
param_blur = 0.f;
param_gray = 0.f;
param_noise = 0.f;
param_duality_h = 0.f;
param_duality_v = 0.f;
param_noise_fps = 25.f;
param_noise_scale = 1.f;
im_noise_time = 1.0f / 100.0f; //Alundaio should be float?
im_noise_shift_w = 0;
im_noise_shift_h = 0;
param_color_base = color_rgba(127, 127, 127, 0);
param_color_gray = color_rgba(85, 85, 85, 0);
//param_color_add = color_rgba(0,0,0, 0);
param_color_add.set(0.0f, 0.0f, 0.0f);
dwAccumulatorClearMark = 0;
dxRenderDeviceRender::Instance().Resources->Evict();
// Blenders
b_occq = xr_new<CBlender_light_occq>();
b_accum_mask = xr_new<CBlender_accum_direct_mask>();
b_accum_direct = xr_new<CBlender_accum_direct>();
b_accum_point = xr_new<CBlender_accum_point>();
b_accum_spot = xr_new<CBlender_accum_spot>();
b_accum_reflected = xr_new<CBlender_accum_reflected>();
b_bloom = xr_new<CBlender_bloom_build>();
if (RImplementation.o.dx10_msaa)
{
b_bloom_msaa = xr_new<CBlender_bloom_build_msaa>();
b_postprocess_msaa = xr_new<CBlender_postprocess_msaa>();
}
else
{
b_bloom_msaa = nullptr;
b_postprocess_msaa = nullptr;
}
b_luminance = xr_new<CBlender_luminance>();
b_combine = xr_new<CBlender_combine>();
b_ssao = xr_new<CBlender_SSAO_noMSAA>();
///////////////////////////////////lvutner
b_sunshafts = xr_new<CBlender_sunshafts>();
b_blur = xr_new<CBlender_blur>();
b_pp_bloom = xr_new<CBlender_pp_bloom>();
b_dof = xr_new<CBlender_dof>();
b_gasmask_drops = xr_new<CBlender_gasmask_drops>();
b_gasmask_dudv = xr_new<CBlender_gasmask_dudv>();
b_nightvision = xr_new<CBlender_nightvision>();
b_fakescope = xr_new<CBlender_fakescope>(); //crookr
b_heatvision = xr_new<CBlender_heatvision>(); //--DSR-- HeatVision
b_lut = xr_new<CBlender_lut>();
b_smaa = xr_new<CBlender_smaa>();
// Screen Space Shaders Stuff
b_ssfx_ssr = xr_new<CBlender_ssfx_ssr>(); // [Ascii1457] SSS new Phase
b_ssfx_volumetric_blur = xr_new<CBlender_ssfx_volumetric_blur>(); // [Ascii1457] SSS new Phase
// HDAO
b_hdao_cs = xr_new<CBlender_CS_HDAO>();
if (RImplementation.o.dx10_msaa)
{
b_hdao_msaa_cs = xr_new<CBlender_CS_HDAO_MSAA>();
}
if (RImplementation.o.dx10_msaa)
{
int bound = RImplementation.o.dx10_msaa_samples;
if (RImplementation.o.dx10_msaa_opt)
bound = 1;
for (int i = 0; i < bound; ++i)
{
static LPCSTR SampleDefs[] = {"0", "1", "2", "3", "4", "5", "6", "7"};
b_combine_msaa[i] = xr_new<CBlender_combine_msaa>();
b_accum_mask_msaa[i] = xr_new<CBlender_accum_direct_mask_msaa>();
b_accum_direct_msaa[i] = xr_new<CBlender_accum_direct_msaa>();
b_accum_direct_volumetric_msaa[i] = xr_new<CBlender_accum_direct_volumetric_msaa>();
//b_accum_direct_volumetric_sun_msaa[i] = xr_new<CBlender_accum_direct_volumetric_sun_msaa> ();
b_accum_spot_msaa[i] = xr_new<CBlender_accum_spot_msaa>();
b_accum_volumetric_msaa[i] = xr_new<CBlender_accum_volumetric_msaa>();
b_accum_point_msaa[i] = xr_new<CBlender_accum_point_msaa>();
b_accum_reflected_msaa[i] = xr_new<CBlender_accum_reflected_msaa>();
b_ssao_msaa[i] = xr_new<CBlender_SSAO_MSAA>();
static_cast<CBlender_accum_direct_mask_msaa*>(b_accum_mask_msaa[i])->SetDefine("ISAMPLE", SampleDefs[i]);
static_cast<CBlender_accum_direct_volumetric_msaa*>(b_accum_direct_volumetric_msaa[i])->SetDefine(
"ISAMPLE", SampleDefs[i]);
//static_cast<CBlender_accum_direct_volumetric_sun_msaa*>(b_accum_direct_volumetric_sun_msaa[i])->SetDefine( "ISAMPLE", SampleDefs[i]);
static_cast<CBlender_accum_direct_msaa*>(b_accum_direct_msaa[i])->SetDefine("ISAMPLE", SampleDefs[i]);
static_cast<CBlender_accum_volumetric_msaa*>(b_accum_volumetric_msaa[i])->SetDefine(
"ISAMPLE", SampleDefs[i]);
static_cast<CBlender_accum_spot_msaa*>(b_accum_spot_msaa[i])->SetDefine("ISAMPLE", SampleDefs[i]);
static_cast<CBlender_accum_point_msaa*>(b_accum_point_msaa[i])->SetDefine("ISAMPLE", SampleDefs[i]);
static_cast<CBlender_accum_reflected_msaa*>(b_accum_reflected_msaa[i])->SetDefine("ISAMPLE", SampleDefs[i]);
static_cast<CBlender_combine_msaa*>(b_combine_msaa[i])->SetDefine("ISAMPLE", SampleDefs[i]);
static_cast<CBlender_SSAO_MSAA*>(b_ssao_msaa[i])->SetDefine("ISAMPLE", SampleDefs[i]);
}
}
// NORMAL
{
u32 w = Device.dwWidth, h = Device.dwHeight;
rt_Position.create(r2_RT_P, w, h, D3DFMT_A16B16G16R16F, SampleCount);
rt_tempzb.create("$user$temp_zb", w, h, D3DFMT_D24S8);
if (RImplementation.o.dx10_msaa)
rt_MSAADepth.create(r2_RT_MSAAdepth, w, h, D3DFMT_D24S8, SampleCount);
// select albedo & accum
if (RImplementation.o.mrtmixdepth)
{
// NV50
rt_Color.create(r2_RT_albedo, w, h, D3DFMT_A8R8G8B8, SampleCount);
rt_Accumulator.create(r2_RT_accum, w, h, D3DFMT_A16B16G16R16F, SampleCount);
}
else
{
// can't - mix-depth
if (RImplementation.o.fp16_blend)
{
// NV40
rt_Color.create(r2_RT_albedo, w, h, D3DFMT_A16B16G16R16F, SampleCount); // expand to full
rt_Accumulator.create(r2_RT_accum, w, h, D3DFMT_A16B16G16R16F, SampleCount);
}
else
{
// R4xx, no-fp-blend,-> albedo_wo
VERIFY(RImplementation.o.albedo_wo);
rt_Color.create(r2_RT_albedo, w, h, D3DFMT_A8R8G8B8, SampleCount); // normal
rt_Accumulator.create(r2_RT_accum, w, h, D3DFMT_A16B16G16R16F, SampleCount);
rt_Accumulator_temp.create(r2_RT_accum_temp, w, h, D3DFMT_A16B16G16R16F, SampleCount);
}
}
// generic(LDR) RTs
//LV - we should change their formats into D3DFMT_A16B16G16R16F for better HDR support.
rt_Generic_0.create(r2_RT_generic0, w, h, D3DFMT_A8R8G8B8, 1);
rt_Generic_1.create(r2_RT_generic1, w, h, D3DFMT_A8R8G8B8, 1);
rt_Generic.create(r2_RT_generic, w, h, D3DFMT_A8R8G8B8, 1);
rt_fakescope.create(r2_RT_scopert, w, h, D3DFMT_A8R8G8B8, 1); //crookr fakescope
//--DSR-- HeatVision_start
rt_Heat.create(r2_RT_heat, w, h, D3DFMT_A8R8G8B8, SampleCount);
//--DSR-- HeatVision_end
if (RImplementation.o.dx10_msaa)
rt_Generic_temp.create("$user$generic_temp", w, h, D3DFMT_A8R8G8B8, SampleCount);
else
rt_Generic_temp.create("$user$generic_temp", w, h, D3DFMT_A8R8G8B8, 1);
rt_secondVP.create(r2_RT_secondVP, w, h, D3DFMT_A8R8G8B8, 1); //--#SM+#-- +SecondVP+
rt_dof.create(r2_RT_dof, w, h, D3DFMT_A8R8G8B8);
rt_ui_pda.create(r2_RT_ui, w, h, D3DFMT_A8R8G8B8);
// RT - KD
rt_sunshafts_0.create(r2_RT_sunshafts0, w, h, D3DFMT_A8R8G8B8);
rt_sunshafts_1.create(r2_RT_sunshafts1, w, h, D3DFMT_A8R8G8B8);
// RT Blur
rt_blur_h_2.create(r2_RT_blur_h_2, u32(w/2), u32(h/2), D3DFMT_A8R8G8B8);
rt_blur_2.create(r2_RT_blur_2, u32(w/2), u32(h/2), D3DFMT_A8R8G8B8);
rt_blur_h_4.create(r2_RT_blur_h_4, u32(w/4), u32(h/4), D3DFMT_A8R8G8B8);
rt_blur_4.create(r2_RT_blur_4, u32(w/4), u32(h/4), D3DFMT_A8R8G8B8);
rt_blur_h_8.create(r2_RT_blur_h_8, u32(w/8), u32(h/8), D3DFMT_A8R8G8B8);
rt_blur_8.create(r2_RT_blur_8, u32(w/8), u32(h/8), D3DFMT_A8R8G8B8);
rt_pp_bloom.create(r2_RT_pp_bloom, w, h, D3DFMT_A8R8G8B8);
// Screen Space Shaders Stuff
rt_ssfx.create(r2_RT_ssfx, w, h, D3DFMT_A8R8G8B8); // Generic RT
rt_ssfx_temp.create(r2_RT_ssfx_temp, w, h, D3DFMT_A8R8G8B8); // Temp RT
rt_ssfx_temp2.create(r2_RT_ssfx_temp2, w, h, D3DFMT_A8R8G8B8); // Temp RT 8B
rt_ssfx_accum.create(r2_RT_ssfx_accum, w, h, D3DFMT_A16B16G16R16F, SampleCount); // Temp RT 16B
rt_ssfx_hud.create(r2_RT_ssfx_hud, w, h, D3DFMT_L8); // Temp RT 8B
if (RImplementation.o.dx10_msaa)
{
rt_Generic_0_r.create(r2_RT_generic0_r, w, h, D3DFMT_A8R8G8B8, SampleCount);
rt_Generic_1_r.create(r2_RT_generic1_r, w, h, D3DFMT_A8R8G8B8, SampleCount);
//rt_Generic.create (r2_RT_generic,w,h, D3DFMT_A8R8G8B8, 1 );
}
// Igor: for volumetric lights
//rt_Generic_2.create (r2_RT_generic2,w,h,D3DFMT_A8R8G8B8 );
// temp: for higher quality blends
if (RImplementation.o.advancedpp)
rt_Generic_2.create(r2_RT_generic2, w, h, D3DFMT_A16B16G16R16F, SampleCount);
}
s_sunshafts.create(b_sunshafts, "r2\\sunshafts");
s_blur.create(b_blur, "r2\\blur");
s_pp_bloom.create(b_pp_bloom, "r2\\pp_bloom");
s_dof.create(b_dof, "r2\\dof");
s_gasmask_drops.create(b_gasmask_drops, "r2\\gasmask_drops");
s_gasmask_dudv.create(b_gasmask_dudv, "r2\\gasmask_dudv");
s_nightvision.create(b_nightvision, "r2\\nightvision");
s_fakescope.create(b_fakescope, "r2\\fakescope"); //crookr
s_heatvision.create(b_heatvision, "r2\\heatvision"); //--DSR-- HeatVision
s_lut.create(b_lut, "r2\\lut");
// OCCLUSION
s_occq.create(b_occq, "r2\\occq");
// Screen Space Shaders Stuff
s_ssfx_ssr.create(b_ssfx_ssr, "r2\\ssfx_ssr"); // SSR
s_ssfx_volumetric_blur.create(b_ssfx_volumetric_blur, "r2\\ssfx_volumetric_blur"); // Volumetric Blur
s_ssfx_dumb.create("ssfx_dumb"); // Dumb shader
// DIRECT (spot)
D3DFORMAT depth_format = (D3DFORMAT)RImplementation.o.HW_smap_FORMAT;
if (RImplementation.o.HW_smap)
{
D3DFORMAT nullrt = D3DFMT_R5G6B5;
if (RImplementation.o.nullrt) nullrt = (D3DFORMAT)MAKEFOURCC('N', 'U', 'L', 'L');
u32 size = RImplementation.o.smapsize;
rt_smap_depth.create(r2_RT_smap_depth, size, size, depth_format);
if (RImplementation.o.dx10_minmax_sm)
{
rt_smap_depth_minmax.create(r2_RT_smap_depth_minmax, size / 4, size / 4, D3DFMT_R32F);
CBlender_createminmax TempBlender;
s_create_minmax_sm.create(&TempBlender, "null");
}
//rt_smap_surf.create (r2_RT_smap_surf, size,size,nullrt );
//rt_smap_ZB = NULL;
s_accum_mask.create(b_accum_mask, "r3\\accum_mask");
s_accum_direct.create(b_accum_direct, "r3\\accum_direct");
if (RImplementation.o.dx10_msaa)
{
int bound = RImplementation.o.dx10_msaa_samples;
if (RImplementation.o.dx10_msaa_opt)
bound = 1;
for (int i = 0; i < bound; ++i)
{
s_accum_direct_msaa[i].create(b_accum_direct_msaa[i], "r3\\accum_direct");
s_accum_mask_msaa[i].create(b_accum_mask_msaa[i], "r3\\accum_direct");
}
}
if (RImplementation.o.advancedpp)
{
s_accum_direct_volumetric.create("accum_volumetric_sun_nomsaa");
if (RImplementation.o.dx10_minmax_sm)
s_accum_direct_volumetric_minmax.create("accum_volumetric_sun_nomsaa_minmax");
if (RImplementation.o.dx10_msaa)
{
static LPCSTR snames[] = {
"accum_volumetric_sun_msaa0",
"accum_volumetric_sun_msaa1",
"accum_volumetric_sun_msaa2",
"accum_volumetric_sun_msaa3",
"accum_volumetric_sun_msaa4",
"accum_volumetric_sun_msaa5",
"accum_volumetric_sun_msaa6",
"accum_volumetric_sun_msaa7"
};
int bound = RImplementation.o.dx10_msaa_samples;
if (RImplementation.o.dx10_msaa_opt)
bound = 1;
for (int i = 0; i < bound; ++i)
{
//s_accum_direct_volumetric_msaa[i].create (b_accum_direct_volumetric_sun_msaa[i], "r3\\accum_direct");
s_accum_direct_volumetric_msaa[i].create(snames[i]);
}
}
}
}
else
{
// TODO: DX10: Check if we need old-style SMap
VERIFY(!"Use HW SMAPs only!");
//u32 size =RImplementation.o.smapsize ;
//rt_smap_surf.create (r2_RT_smap_surf, size,size,D3DFMT_R32F);
//rt_smap_depth = NULL;
//R_CHK (HW.pDevice->CreateDepthStencilSurface (size,size,D3DFMT_D24X8,D3DMULTISAMPLE_NONE,0,TRUE,&rt_smap_ZB,NULL));
//s_accum_mask.create (b_accum_mask, "r2\\accum_mask");
//s_accum_direct.create (b_accum_direct, "r2\\accum_direct");
//if (RImplementation.o.advancedpp)
// s_accum_direct_volumetric.create("accum_volumetric_sun");
}
// RAIN
// TODO: DX10: Create resources only when DX10 rain is enabled.
// Or make DX10 rain switch dynamic?
{
CBlender_rain TempBlender;
s_rain.create(&TempBlender, "null");
if (RImplementation.o.dx10_msaa)
{
static LPCSTR SampleDefs[] = {"0", "1", "2", "3", "4", "5", "6", "7"};
CBlender_rain_msaa TempBlender[8];
int bound = RImplementation.o.dx10_msaa_samples;
if (RImplementation.o.dx10_msaa_opt)
bound = 1;
for (int i = 0; i < bound; ++i)
{
TempBlender[i].SetDefine("ISAMPLE", SampleDefs[i]);
s_rain_msaa[i].create(&TempBlender[i], "null");
s_accum_spot_msaa[i].create(b_accum_spot_msaa[i], "r2\\accum_spot_s", "lights\\lights_spot01");
s_accum_point_msaa[i].create(b_accum_point_msaa[i], "r2\\accum_point_s");
//s_accum_volume_msaa[i].create(b_accum_direct_volumetric_msaa[i], "lights\\lights_spot01");
s_accum_volume_msaa[i].create(b_accum_volumetric_msaa[i], "lights\\lights_spot01");
s_combine_msaa[i].create(b_combine_msaa[i], "r2\\combine");
}
}
}
if (RImplementation.o.dx10_msaa)
{
CBlender_msaa TempBlender;
s_mark_msaa_edges.create(&TempBlender, "null");
}
// POINT
{
s_accum_point.create(b_accum_point, "r2\\accum_point_s");
accum_point_geom_create();
g_accum_point.create(D3DFVF_XYZ, g_accum_point_vb, g_accum_point_ib);
accum_omnip_geom_create();
g_accum_omnipart.create(D3DFVF_XYZ, g_accum_omnip_vb, g_accum_omnip_ib);
}
// SPOT
{
s_accum_spot.create(b_accum_spot, "r2\\accum_spot_s", "lights\\lights_spot01");
accum_spot_geom_create();
g_accum_spot.create(D3DFVF_XYZ, g_accum_spot_vb, g_accum_spot_ib);
}
{
s_accum_volume.create("accum_volumetric", "lights\\lights_spot01");
accum_volumetric_geom_create();
g_accum_volumetric.create(D3DFVF_XYZ, g_accum_volumetric_vb, g_accum_volumetric_ib);
}
// REFLECTED
{
s_accum_reflected.create(b_accum_reflected, "r2\\accum_refl");
if (RImplementation.o.dx10_msaa)
{
int bound = RImplementation.o.dx10_msaa_samples;
if (RImplementation.o.dx10_msaa_opt)
bound = 1;
for (int i = 0; i < bound; ++i)
{
s_accum_reflected_msaa[i].create(b_accum_reflected_msaa[i], "null");
}
}
}
// BLOOM
{
D3DFORMAT fmt = D3DFMT_A8R8G8B8; //; // D3DFMT_X8R8G8B8
u32 w = BLOOM_size_X, h = BLOOM_size_Y;
u32 fvf_build = D3DFVF_XYZRHW | D3DFVF_TEX4 | D3DFVF_TEXCOORDSIZE2(0) | D3DFVF_TEXCOORDSIZE2(1) |
D3DFVF_TEXCOORDSIZE2(2) | D3DFVF_TEXCOORDSIZE2(3);
u32 fvf_filter = (u32)D3DFVF_XYZRHW | D3DFVF_TEX8 | D3DFVF_TEXCOORDSIZE4(0) | D3DFVF_TEXCOORDSIZE4(1) |
D3DFVF_TEXCOORDSIZE4(2) | D3DFVF_TEXCOORDSIZE4(3) | D3DFVF_TEXCOORDSIZE4(4) | D3DFVF_TEXCOORDSIZE4(5) |
D3DFVF_TEXCOORDSIZE4(6) | D3DFVF_TEXCOORDSIZE4(7);
rt_Bloom_1.create(r2_RT_bloom1, w, h, fmt);
rt_Bloom_2.create(r2_RT_bloom2, w, h, fmt);
g_bloom_build.create(fvf_build, RCache.Vertex.Buffer(), RCache.QuadIB);
g_bloom_filter.create(fvf_filter, RCache.Vertex.Buffer(), RCache.QuadIB);
s_bloom_dbg_1.create("effects\\screen_set", r2_RT_bloom1);
s_bloom_dbg_2.create("effects\\screen_set", r2_RT_bloom2);
s_bloom.create(b_bloom, "r2\\bloom");
if (RImplementation.o.dx10_msaa)
{
s_bloom_msaa.create(b_bloom_msaa, "r2\\bloom");
s_postprocess_msaa.create(b_postprocess_msaa, "r2\\post");
}
f_bloom_factor = 0.5f;
}
//SMAA
{
u32 w = Device.dwWidth;
u32 h = Device.dwHeight;
rt_smaa_edgetex.create(r2_RT_smaa_edgetex, w, h, D3DFMT_A8R8G8B8);
rt_smaa_blendtex.create(r2_RT_smaa_blendtex, w, h, D3DFMT_A8R8G8B8);
s_smaa.create(b_smaa, "r3\\smaa");
}
// TONEMAP
{
rt_LUM_64.create(r2_RT_luminance_t64, 64, 64, D3DFMT_A16B16G16R16F);
rt_LUM_8.create(r2_RT_luminance_t8, 8, 8, D3DFMT_A16B16G16R16F);
s_luminance.create(b_luminance, "r2\\luminance");
f_luminance_adapt = 0.5f;
t_LUM_src.create(r2_RT_luminance_src);
t_LUM_dest.create(r2_RT_luminance_cur);
// create pool
for (u32 it = 0; it < HW.Caps.iGPUNum * 2; it++)
{
string256 name;
xr_sprintf(name, "%s_%d", r2_RT_luminance_pool, it);
rt_LUM_pool[it].create(name, 1, 1, D3DFMT_R32F);
//u_setrt (rt_LUM_pool[it], 0, 0, 0 );
//CHK_DX (HW.pDevice->Clear( 0L, NULL, D3DCLEAR_TARGET, 0x7f7f7f7f, 1.0f, 0L));
FLOAT ColorRGBA[4] = {127.0f / 255.0f, 127.0f / 255.0f, 127.0f / 255.0f, 127.0f / 255.0f};
HW.pContext->ClearRenderTargetView(rt_LUM_pool[it]->pRT, ColorRGBA);
}
u_setrt(Device.dwWidth, Device.dwHeight, HW.pBaseRT,NULL,NULL, HW.pBaseZB);
}
// HBAO
if (RImplementation.o.ssao_opt_data)
{
u32 w = 0;
u32 h = 0;
if (RImplementation.o.ssao_half_data)
{
w = Device.dwWidth / 2;
h = Device.dwHeight / 2;
}
else
{
w = Device.dwWidth;
h = Device.dwHeight;
}
D3DFORMAT fmt = HW.Caps.id_vendor == 0x10DE ? D3DFMT_R32F : D3DFMT_R16F;
rt_half_depth.create(r2_RT_half_depth, w, h, fmt);
s_ssao.create(b_ssao, "r2\\ssao");
}
//if (RImplementation.o.ssao_blur_on)
//{
// u32 w = Device.dwWidth, h = Device.dwHeight;
// rt_ssao_temp.create (r2_RT_ssao_temp, w, h, D3DFMT_G16R16F, SampleCount);
// s_ssao.create (b_ssao, "r2\\ssao");
// if( RImplementation.o.dx10_msaa )
// {
// int bound = RImplementation.o.dx10_msaa_opt ? 1 : RImplementation.o.dx10_msaa_samples;
// for( int i = 0; i < bound; ++i )
// {
// s_ssao_msaa[i].create( b_ssao_msaa[i], "null");
// }
// }
//}
// HDAO
if (RImplementation.o.ssao_hdao && RImplementation.o.ssao_ultra)
{
u32 w = Device.dwWidth, h = Device.dwHeight;
rt_ssao_temp.create(r2_RT_ssao_temp, w, h, D3DFMT_R16F, 1, true);
s_hdao_cs.create(b_hdao_cs, "r2\\ssao");
if (RImplementation.o.dx10_msaa)
{
s_hdao_cs_msaa.create(b_hdao_msaa_cs, "r2\\ssao");
}
}
// COMBINE
{
static D3DVERTEXELEMENT9 dwDecl[] =
{
{0, 0, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, // pos+uv
D3DDECL_END()
};
s_combine.create(b_combine, "r2\\combine");
s_combine_volumetric.create("combine_volumetric");
s_combine_dbg_0.create("effects\\screen_set", r2_RT_smap_surf);
s_combine_dbg_1.create("effects\\screen_set", r2_RT_luminance_t8);
s_combine_dbg_Accumulator.create("effects\\screen_set", r2_RT_accum);
g_combine_VP.create(dwDecl, RCache.Vertex.Buffer(), RCache.QuadIB);
g_combine.create(FVF::F_TL, RCache.Vertex.Buffer(), RCache.QuadIB);
g_combine_2UV.create(FVF::F_TL2uv, RCache.Vertex.Buffer(), RCache.QuadIB);
g_combine_cuboid.create(dwDecl, RCache.Vertex.Buffer(), RCache.Index.Buffer());
u32 fvf_aa_blur = D3DFVF_XYZRHW | D3DFVF_TEX4 | D3DFVF_TEXCOORDSIZE2(0) | D3DFVF_TEXCOORDSIZE2(1) |
D3DFVF_TEXCOORDSIZE2(2) | D3DFVF_TEXCOORDSIZE2(3);
g_aa_blur.create(fvf_aa_blur, RCache.Vertex.Buffer(), RCache.QuadIB);
u32 fvf_aa_AA = D3DFVF_XYZRHW | D3DFVF_TEX7 | D3DFVF_TEXCOORDSIZE2(0) | D3DFVF_TEXCOORDSIZE2(1) |
D3DFVF_TEXCOORDSIZE2(2) | D3DFVF_TEXCOORDSIZE2(3) | D3DFVF_TEXCOORDSIZE2(4) | D3DFVF_TEXCOORDSIZE4(5) |
D3DFVF_TEXCOORDSIZE4(6);
g_aa_AA.create(fvf_aa_AA, RCache.Vertex.Buffer(), RCache.QuadIB);
u32 fvf_KD = D3DFVF_XYZRHW | D3DFVF_TEX1 | D3DFVF_TEXCOORDSIZE2(0);
g_KD.create(fvf_KD, RCache.Vertex.Buffer(), RCache.QuadIB);
t_envmap_0.create(r2_T_envs0);
t_envmap_1.create(r2_T_envs1);
}
// Build textures
{
// Testure for async sreenshots
{
D3D_TEXTURE2D_DESC desc;
desc.Width = Device.dwWidth;
desc.Height = Device.dwHeight;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Format = DXGI_FORMAT_R8G8B8A8_SNORM;
desc.Usage = D3D_USAGE_STAGING;
desc.BindFlags = 0;
desc.CPUAccessFlags = D3D_CPU_ACCESS_READ;
desc.MiscFlags = 0;
R_CHK(HW.pDevice->CreateTexture2D(&desc, 0, &t_ss_async));
}
// Build material(s)
{
// Create immutable texture.
// So we need to init data _before_ the creation.
// Surface
//R_CHK (D3DXCreateVolumeTexture(HW.pDevice,TEX_material_LdotN,TEX_material_LdotH,4,1,0,D3DFMT_A8L8,D3DPOOL_MANAGED,&t_material_surf));
//t_material = dxRenderDeviceRender::Instance().Resources->_CreateTexture(r2_material);
//t_material->surface_set (t_material_surf);
// Use DXGI_FORMAT_R8G8_UNORM
u16 tempData[TEX_material_LdotN * TEX_material_LdotH * TEX_material_Count];
D3D_TEXTURE3D_DESC desc;
desc.Width = TEX_material_LdotN;
desc.Height = TEX_material_LdotH;
desc.Depth = TEX_material_Count;
desc.MipLevels = 1;
desc.Format = DXGI_FORMAT_R8G8_UNORM;
desc.Usage = D3D_USAGE_IMMUTABLE;
desc.BindFlags = D3D_BIND_SHADER_RESOURCE;
desc.CPUAccessFlags = 0;
desc.MiscFlags = 0;
D3D_SUBRESOURCE_DATA subData;
subData.pSysMem = tempData;
subData.SysMemPitch = desc.Width * 2;
subData.SysMemSlicePitch = desc.Height * subData.SysMemPitch;
// Fill it (addr: x=dot(L,N),y=dot(L,H))
//D3DLOCKED_BOX R;
//R_CHK (t_material_surf->LockBox (0,&R,0,0));
for (u32 slice = 0; slice < TEX_material_Count; slice++)
{
for (u32 y = 0; y < TEX_material_LdotH; y++)
{
for (u32 x = 0; x < TEX_material_LdotN; x++)
{
u16* p = (u16*)
(LPBYTE(subData.pSysMem)
+ slice * subData.SysMemSlicePitch
+ y * subData.SysMemPitch + x * 2);
float ld = float(x) / float(TEX_material_LdotN - 1);
float ls = float(y) / float(TEX_material_LdotH - 1) + EPS_S;
ls *= powf(ld, 1 / 32.f);
float fd, fs;
switch (slice)
{
case 0:
{
// looks like OrenNayar
fd = powf(ld, 0.75f); // 0.75
fs = powf(ls, 16.f) * .5f;
}
break;
case 1:
{
// looks like Blinn
fd = powf(ld, 0.90f); // 0.90
fs = powf(ls, 24.f);
}
break;
case 2:
{
// looks like Phong
fd = ld; // 1.0
fs = powf(ls * 1.01f, 128.f);
}
break;
case 3:
{
// looks like Metal
float s0 = _abs(1 - _abs(0.05f * _sin(33.f * ld) + ld - ls));
float s1 = _abs(1 - _abs(0.05f * _cos(33.f * ld * ls) + ld - ls));
float s2 = _abs(1 - _abs(ld - ls));
fd = ld; // 1.0
fs = powf(_max(_max(s0, s1), s2), 24.f);
fs *= powf(ld, 1 / 7.f);
}
break;
default:
fd = fs = 0;
}
s32 _d = clampr(iFloor(fd * 255.5f), 0, 255);
s32 _s = clampr(iFloor(fs * 255.5f), 0, 255);
if ((y == (TEX_material_LdotH - 1)) && (x == (TEX_material_LdotN - 1)))
{
_d = 255;
_s = 255;
}
*p = u16(_s * 256 + _d);
}
}
}
//R_CHK (t_material_surf->UnlockBox (0));
R_CHK(HW.pDevice->CreateTexture3D(&desc, &subData, &t_material_surf));
t_material = dxRenderDeviceRender::Instance().Resources->_CreateTexture(r2_material);
t_material->surface_set(t_material_surf);
//R_CHK (D3DXCreateVolumeTexture(HW.pDevice,TEX_material_LdotN,TEX_material_LdotH,4,1,0,D3DFMT_A8L8,D3DPOOL_MANAGED,&t_material_surf));
//t_material = dxRenderDeviceRender::Instance().Resources->_CreateTexture(r2_material);
//t_material->surface_set (t_material_surf);
// #ifdef DEBUG
// R_CHK (D3DXSaveTextureToFile ("x:\\r2_material.dds",D3DXIFF_DDS,t_material_surf,0));
// #endif
}
// Build noise table
if (1)
{
// Surfaces
//D3DLOCKED_RECT R[TEX_jitter_count];
//for (int it=0; it<TEX_jitter_count; it++)
//{
// string_path name;
// xr_sprintf (name,"%s%d",r2_jitter,it);
// R_CHK (D3DXCreateTexture (HW.pDevice,TEX_jitter,TEX_jitter,1,0,D3DFMT_Q8W8V8U8,D3DPOOL_MANAGED,&t_noise_surf[it]));
// t_noise[it] = dxRenderDeviceRender::Instance().Resources->_CreateTexture (name);
// t_noise[it]->surface_set (t_noise_surf[it]);
// R_CHK (t_noise_surf[it]->LockRect (0,&R[it],0,0));
//}
// Use DXGI_FORMAT_R8G8B8A8_SNORM
static const int sampleSize = 4;
u32 tempData[TEX_jitter_count][TEX_jitter * TEX_jitter];
D3D_TEXTURE2D_DESC desc;
desc.Width = TEX_jitter;
desc.Height = TEX_jitter;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.Format = DXGI_FORMAT_R8G8B8A8_SNORM;
//desc.Usage = D3D_USAGE_IMMUTABLE;
desc.Usage = D3D_USAGE_DEFAULT;
desc.BindFlags = D3D_BIND_SHADER_RESOURCE;
desc.CPUAccessFlags = 0;
desc.MiscFlags = 0;
D3D_SUBRESOURCE_DATA subData[TEX_jitter_count];
for (int it = 0; it < TEX_jitter_count - 1; it++)
{