/
viewpostprocess.cpp
4006 lines (3277 loc) · 146 KB
/
viewpostprocess.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
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
//========== Copyright (c) 2008, Valve Corporation, All rights reserved. ==========
//
// Purpose:
//
//=================================================================================
#include "cbase.h"
#include "materialsystem/imaterialsystem.h"
#include "materialsystem/itexture.h"
#include "materialsystem/imaterialvar.h"
#include "materialsystem/imaterialsystemhardwareconfig.h"
#include "materialsystem/materialsystem_config.h"
#include "tier1/callqueue.h"
#include "colorcorrectionmgr.h"
#include "view_scene.h"
#include "c_world.h"
#include "renderparm.h"
#include "shaderapi/ishaderapi.h"
#include "proxyentity.h"
#include "imaterialproxydict.h"
#include "model_types.h"
#include "bitmap/tgawriter.h"
#include "filesystem.h"
#ifdef PORTAL2
#include "c_portal_player.h"
#endif
#ifdef CSTRIKE15
#include "weapon_csbase.h"
#include "c_cs_player.h"
#endif
// NOTE: This has to be the last file included!
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
// Globals
//-----------------------------------------------------------------------------
// mapmaker controlled autoexposure
bool g_bUseCustomAutoExposureMin = false;
bool g_bUseCustomAutoExposureMax = false;
bool g_bUseCustomBloomScale = false;
float g_flCustomAutoExposureMin = 0;
float g_flCustomAutoExposureMax = 0;
float g_flCustomBloomScale = 0.0f;
float g_flCustomBloomScaleMinimum = 0.0f;
float g_flBloomExponent = 2.5f;
float g_flBloomSaturation = 1.0f;
float g_flTonemapPercentBrightPixels = 2.0f;
float g_flTonemapMinAvgLum = 3.0f;
float g_flTonemapRate = 1.0f;
#if defined( _X360 )
#if defined( CSTRIKE15 )
float g_flTonemapPercentTarget = 60.0f;
#else
// Move "up" the percent target to make X360 a bit brighter than it's been to compensate for our bad 8-bit histogram utilization and to also compensate for the non-PWL texture change.
float g_flTonemapPercentTarget = 80.0f
#endif
#else
float g_flTonemapPercentTarget = 60.0f;
#endif
extern void GetTonemapSettingsFromEnvTonemapController( void );
// mapmaker controlled depth of field
bool g_bDOFEnabled = false;
float g_flDOFNearBlurDepth = 50.0f;
float g_flDOFNearFocusDepth = 200.0f;
float g_flDOFFarFocusDepth = 250.0f;
float g_flDOFFarBlurDepth = 1000.0f;
float g_flDOFNearBlurRadius = 0.0f;
float g_flDOFFarBlurRadius = 5.0f;
bool g_bFlashlightIsOn = false;
// hdr parameters
ConVar mat_bloomscale( "mat_bloomscale", "1", FCVAR_CHEAT | FCVAR_DEVELOPMENTONLY );
ConVar mat_hdr_level( "mat_hdr_level", "2", FCVAR_DEVELOPMENTONLY );
ConVar mat_bloomamount_rate( "mat_bloomamount_rate", "0.05f", FCVAR_CHEAT );
static ConVar debug_postproc( "mat_debug_postprocessing_effects", "0", FCVAR_CHEAT, "0 = off, 1 = show post-processing passes in quadrants of the screen, 2 = only apply post-processing to the centre of the screen" );
static ConVar mat_dynamic_tonemapping( "mat_dynamic_tonemapping", "1", FCVAR_CHEAT );
static ConVar mat_tonemapping_occlusion_use_stencil( "mat_tonemapping_occlusion_use_stencil", "0", FCVAR_DEVELOPMENTONLY );
static ConVar mat_autoexposure_max( "mat_autoexposure_max", "2", FCVAR_CHEAT );
#if defined( _X360 )
#if defined( CSTRIKE15 )
static ConVar mat_autoexposure_max_multiplier("mat_autoexposure_max_multiplier","1.0", FCVAR_CHEAT);
#else
// Allow the max. pixel shader multiplier to be 50% higher to better utilize the available 8-bit output range, and to help compensate for the gamma ramp adjustments we've made. (At some point we should also adjust the PS3.)
static ConVar mat_autoexposure_max_multiplier("mat_autoexposure_max_multiplier","1.5", FCVAR_CHEAT );
#endif
#else
static ConVar mat_autoexposure_max_multiplier("mat_autoexposure_max_multiplier","1.0", FCVAR_CHEAT );
#endif
static ConVar mat_autoexposure_min( "mat_autoexposure_min", "0.5", FCVAR_CHEAT );
static ConVar mat_show_histogram( "mat_show_histogram", "0", FCVAR_CHEAT );
ConVar mat_hdr_uncapexposure( "mat_hdr_uncapexposure", "0", FCVAR_CHEAT );
ConVar mat_force_bloom("mat_force_bloom","0", FCVAR_CHEAT );
ConVar mat_disable_bloom("mat_disable_bloom","0", FCVAR_CHEAT );
ConVar mat_debug_bloom("mat_debug_bloom","0", FCVAR_CHEAT);
ConVar mat_colorcorrection( "mat_colorcorrection", "1", FCVAR_CHEAT );
ConVar mat_accelerate_adjust_exposure_down( "mat_accelerate_adjust_exposure_down", "40.0", FCVAR_CHEAT );
// fudge factor to make non-hdr bloom more closely match hdr bloom. Because of auto-exposure, high
// bloomscales don't blow out as much in hdr. this factor was derived by comparing images in a
// reference scene.
ConVar mat_non_hdr_bloom_scalefactor("mat_non_hdr_bloom_scalefactor",".3", FCVAR_CHEAT | FCVAR_DEVELOPMENTONLY );
// Apply addition scale to the final bloom scale
static ConVar mat_bloom_scalefactor_scalar( "mat_bloom_scalefactor_scalar", "1.0", FCVAR_CHEAT | FCVAR_DEVELOPMENTONLY );
//ConVar mat_exposure_center_region_x( "mat_exposure_center_region_x","0.75", FCVAR_CHEAT );
//ConVar mat_exposure_center_region_y( "mat_exposure_center_region_y","0.80", FCVAR_CHEAT );
ConVar mat_exposure_center_region_x( "mat_exposure_center_region_x","0.9", FCVAR_CHEAT );
ConVar mat_exposure_center_region_y( "mat_exposure_center_region_y","0.85", FCVAR_CHEAT );
ConVar mat_tonemap_algorithm( "mat_tonemap_algorithm", "1", FCVAR_CHEAT, "0 = Original Algorithm 1 = New Algorithm" );
ConVar mat_force_tonemap_percent_target( "mat_force_tonemap_percent_target", "-1", FCVAR_CHEAT, "Override. Old default was 60." );
ConVar mat_force_tonemap_percent_bright_pixels( "mat_force_tonemap_percent_bright_pixels", "-1", FCVAR_CHEAT, "Override. Old value was 2.0" );
ConVar mat_force_tonemap_min_avglum( "mat_force_tonemap_min_avglum", "-1", FCVAR_CHEAT, "Override. Old default was 3.0" );
ConVar mat_force_tonemap_scale( "mat_force_tonemap_scale", "0.0", FCVAR_CHEAT );
ConVar mat_fullbright( "mat_fullbright", "0", FCVAR_CHEAT );
ConVar mat_grain_enable( "mat_grain_enable", "1" );
//ConVar mat_vignette_enable( "mat_vignette_enable", "1", FCVAR_RELEASE | FCVAR_REPLICATED );
ConVar mat_local_contrast_enable( "mat_local_contrast_enable", "1", FCVAR_DEVELOPMENTONLY );
ConVar mat_blur_r( "mat_blur_r", "0.7" );
ConVar mat_blur_g( "mat_blur_g", "0.7" );
ConVar mat_blur_b( "mat_blur_b", "0.7" );
#if defined(_PS3)
ConVar mat_PS3_findpostvarsfast( "mat_PS3_findpostvarsfast", "1" );
#endif
// These material proxies + the GetXXXMaterial calls within have been added for PS3 for perf reasons (to avoid the expensive PS3 mutexes on Find_Var, Find_Material)
// gives us almost 0.5ms saving from main thread, could be added to other platforms - need to test
// FIXME: also need to test in and out of levels, but that is not currently working without this change on the current PS3 CS:GO build anyway
//=====================================================================================================================
// LumCompare material proxy ============================================================================================
//=====================================================================================================================
class CLumCompareMaterialProxy : public CEntityMaterialProxy
{
public:
CLumCompareMaterialProxy();
virtual ~CLumCompareMaterialProxy() {};
virtual bool Init( IMaterial *pMaterial, KeyValues *pKeyValues );
virtual void OnBind( C_BaseEntity *pEntity );
virtual IMaterial *GetMaterial();
static IMaterial *GetLumCompareMaterial( IMaterialSystem * materials );
static void SetupLumCompareMaterial( float flTestRangeMin, float flTestRangeMax );
private:
static IMaterialVar *s_pMaterialParam_C0_X;
static IMaterialVar *s_pMaterialParam_C0_Y;
static float s_C0_X;
static float s_C0_Y;
static IMaterial *s_pLumCompareMaterial;
};
IMaterialVar *CLumCompareMaterialProxy::s_pMaterialParam_C0_X = NULL;
IMaterialVar *CLumCompareMaterialProxy::s_pMaterialParam_C0_Y = NULL;
float CLumCompareMaterialProxy::s_C0_X = -1e20f;
float CLumCompareMaterialProxy::s_C0_Y = 1e20f;
IMaterial *CLumCompareMaterialProxy::s_pLumCompareMaterial = NULL;
CLumCompareMaterialProxy::CLumCompareMaterialProxy()
{
s_pMaterialParam_C0_X = NULL;
s_pMaterialParam_C0_Y = NULL;
s_pLumCompareMaterial = NULL;
}
bool CLumCompareMaterialProxy::Init( IMaterial *pMaterial, KeyValues *pKeyValues )
{
bool bFoundVar = false;
s_pLumCompareMaterial = materials->FindMaterial( "dev/lumcompare", TEXTURE_GROUP_OTHER, true );
s_pMaterialParam_C0_X = pMaterial->FindVar( "$C0_X", &bFoundVar, false );
s_pMaterialParam_C0_Y = pMaterial->FindVar( "$C0_Y", &bFoundVar, false );
return true;
}
void CLumCompareMaterialProxy::OnBind( C_BaseEntity *pEnt )
{
}
IMaterial *CLumCompareMaterialProxy::GetMaterial()
{
if ( s_pMaterialParam_C0_X == NULL)
return NULL;
return s_pMaterialParam_C0_X->GetOwningMaterial();
}
IMaterial *CLumCompareMaterialProxy::GetLumCompareMaterial( IMaterialSystem * materials )
{
if( s_pLumCompareMaterial == NULL)
{
s_pLumCompareMaterial = materials->FindMaterial( "dev/lumcompare", TEXTURE_GROUP_OTHER, true );
}
return s_pLumCompareMaterial;
}
void CLumCompareMaterialProxy::SetupLumCompareMaterial( float flTestRangeMin, float flTestRangeMax )
{
s_C0_X = flTestRangeMin;
s_C0_Y = flTestRangeMax;
s_pMaterialParam_C0_X->SetFloatValue( s_C0_X );
s_pMaterialParam_C0_Y->SetFloatValue( s_C0_Y );
}
EXPOSE_MATERIAL_PROXY( CLumCompareMaterialProxy, LumCompare );
//=====================================================================================================================
// LumCompareStencil material proxy ============================================================================================
//=====================================================================================================================
class CLumCompareStencilMaterialProxy : public CEntityMaterialProxy
{
public:
CLumCompareStencilMaterialProxy();
virtual ~CLumCompareStencilMaterialProxy() {};
virtual bool Init( IMaterial *pMaterial, KeyValues *pKeyValues );
virtual void OnBind( C_BaseEntity *pEntity );
virtual IMaterial *GetMaterial();
private:
public:
static IMaterial *GetLumCompareStencilMaterial( IMaterialSystem * materials );
private:
static IMaterial *s_pLumCompareStencilMaterial;
};
IMaterial *CLumCompareStencilMaterialProxy::s_pLumCompareStencilMaterial = NULL;
CLumCompareStencilMaterialProxy::CLumCompareStencilMaterialProxy()
{
s_pLumCompareStencilMaterial = NULL;
}
bool CLumCompareStencilMaterialProxy::Init( IMaterial *pMaterial, KeyValues *pKeyValues )
{
return true;
}
void CLumCompareStencilMaterialProxy::OnBind( C_BaseEntity *pEnt )
{
}
IMaterial *CLumCompareStencilMaterialProxy::GetMaterial()
{
if( s_pLumCompareStencilMaterial == NULL)
{
s_pLumCompareStencilMaterial = materials->FindMaterial( "dev/no_pixel_write", TEXTURE_GROUP_OTHER, true );
}
return s_pLumCompareStencilMaterial;
}
IMaterial *CLumCompareStencilMaterialProxy::GetLumCompareStencilMaterial( IMaterialSystem * materials )
{
if( s_pLumCompareStencilMaterial == NULL)
{
s_pLumCompareStencilMaterial = materials->FindMaterial( "dev/no_pixel_write", TEXTURE_GROUP_OTHER, true );
}
return s_pLumCompareStencilMaterial;
}
EXPOSE_MATERIAL_PROXY( CLumCompareStencilMaterialProxy, no_pixel_write );
//=====================================================================================================================
// Downsample material proxy ============================================================================================
//=====================================================================================================================
class CDownsampleMaterialProxy : public CEntityMaterialProxy
{
public:
CDownsampleMaterialProxy();
virtual ~CDownsampleMaterialProxy() {};
virtual bool Init( IMaterial *pMaterial, KeyValues *pKeyValues );
virtual void OnBind( C_BaseEntity *pEntity );
virtual IMaterial *GetMaterial();
public:
static IMaterial *GetDownsampleMaterial( IMaterialSystem * materials );
static void SetupDownsampleMaterial( float flBloomExponent, float flBloomSaturation );
private:
static IMaterialVar *s_pMaterialParam_bloomexp;
static IMaterialVar *s_pMaterialParam_bloomsaturation;
static float s_bloomexp;
static float s_bloomsaturation;
static IMaterial *s_pDownsampleMaterial;
};
IMaterialVar *CDownsampleMaterialProxy::s_pMaterialParam_bloomexp = NULL;
IMaterialVar *CDownsampleMaterialProxy::s_pMaterialParam_bloomsaturation = NULL;
float CDownsampleMaterialProxy::s_bloomexp = 2.5f;
float CDownsampleMaterialProxy::s_bloomsaturation = 1.0f;
IMaterial *CDownsampleMaterialProxy::s_pDownsampleMaterial = NULL;
CDownsampleMaterialProxy::CDownsampleMaterialProxy()
{
s_pMaterialParam_bloomexp = NULL;
s_pMaterialParam_bloomsaturation = NULL;
s_pDownsampleMaterial = NULL;
}
bool CDownsampleMaterialProxy::Init( IMaterial *pMaterial, KeyValues *pKeyValues )
{
bool bFoundVar = false;
s_pMaterialParam_bloomexp = pMaterial->FindVar( "$bloomexp", &bFoundVar, false );
s_pMaterialParam_bloomsaturation = pMaterial->FindVar( "$bloomsaturation", &bFoundVar, false );
s_bloomexp = 2.5f;
s_bloomsaturation = 1.0f;
return true;
}
void CDownsampleMaterialProxy::OnBind( C_BaseEntity *pEnt )
{
}
IMaterial *CDownsampleMaterialProxy::GetMaterial()
{
if ( s_pMaterialParam_bloomexp == NULL)
return NULL;
return s_pMaterialParam_bloomexp->GetOwningMaterial();
}
IMaterial *CDownsampleMaterialProxy::GetDownsampleMaterial( IMaterialSystem * materials )
{
if( s_pDownsampleMaterial == NULL)
{
// FIXME: doesn't support dev/downsample (bFloathdr == true) path on PS3 here yet...
s_pDownsampleMaterial = materials->FindMaterial( "dev/downsample_non_hdr", TEXTURE_GROUP_OTHER, true );
}
return s_pDownsampleMaterial;
}
void CDownsampleMaterialProxy::SetupDownsampleMaterial( float flBloomExponent, float flBloomSaturation )
{
s_bloomexp = flBloomExponent;
s_bloomsaturation = flBloomSaturation;
s_pMaterialParam_bloomexp->SetFloatValue( s_bloomexp );
s_pMaterialParam_bloomsaturation->SetFloatValue( s_bloomsaturation );
}
EXPOSE_MATERIAL_PROXY( CDownsampleMaterialProxy, downsample_non_hdr );
//=====================================================================================================================
// XBlur material proxy ============================================================================================
//=====================================================================================================================
class CXBlurMaterialProxy : public CEntityMaterialProxy
{
public:
CXBlurMaterialProxy();
virtual ~CXBlurMaterialProxy() {};
virtual bool Init( IMaterial *pMaterial, KeyValues *pKeyValues );
virtual void OnBind( C_BaseEntity *pEntity );
virtual IMaterial *GetMaterial();
private:
public:
static IMaterial *GetXBlurMaterial( IMaterialSystem * materials );
private:
static IMaterial *s_pXBlurMaterial;
};
IMaterial *CXBlurMaterialProxy::s_pXBlurMaterial = NULL;
CXBlurMaterialProxy::CXBlurMaterialProxy()
{
s_pXBlurMaterial = NULL;
}
bool CXBlurMaterialProxy::Init( IMaterial *pMaterial, KeyValues *pKeyValues )
{
return true;
}
void CXBlurMaterialProxy::OnBind( C_BaseEntity *pEnt )
{
}
IMaterial *CXBlurMaterialProxy::GetMaterial()
{
if( s_pXBlurMaterial == NULL)
{
s_pXBlurMaterial = materials->FindMaterial( "dev/blurfilterx_nohdr", TEXTURE_GROUP_OTHER, true );
}
return s_pXBlurMaterial;
}
IMaterial *CXBlurMaterialProxy::GetXBlurMaterial( IMaterialSystem * materials )
{
if( s_pXBlurMaterial == NULL)
{
s_pXBlurMaterial = materials->FindMaterial( "dev/blurfilterx_nohdr", TEXTURE_GROUP_OTHER, true );
}
return s_pXBlurMaterial;
}
EXPOSE_MATERIAL_PROXY( CXBlurMaterialProxy, blurfilterx );
//=====================================================================================================================
// YBlur material proxy ============================================================================================
//=====================================================================================================================
class CYBlurMaterialProxy : public CEntityMaterialProxy
{
public:
CYBlurMaterialProxy();
virtual ~CYBlurMaterialProxy() {};
virtual bool Init( IMaterial *pMaterial, KeyValues *pKeyValues );
virtual void OnBind( C_BaseEntity *pEntity );
virtual IMaterial *GetMaterial();
private:
public:
static IMaterial *GetYBlurMaterial( IMaterialSystem * materials );
private:
static IMaterial *s_pYBlurMaterial;
};
IMaterial *CYBlurMaterialProxy::s_pYBlurMaterial = NULL;
CYBlurMaterialProxy::CYBlurMaterialProxy()
{
s_pYBlurMaterial = NULL;
}
bool CYBlurMaterialProxy::Init( IMaterial *pMaterial, KeyValues *pKeyValues )
{
return true;
}
void CYBlurMaterialProxy::OnBind( C_BaseEntity *pEnt )
{
}
IMaterial *CYBlurMaterialProxy::GetMaterial()
{
if( s_pYBlurMaterial == NULL)
{
s_pYBlurMaterial = materials->FindMaterial( "dev/blurfiltery_nohdr", TEXTURE_GROUP_OTHER, true );
}
return s_pYBlurMaterial;
}
IMaterial *CYBlurMaterialProxy::GetYBlurMaterial( IMaterialSystem * materials )
{
if( s_pYBlurMaterial == NULL)
{
s_pYBlurMaterial = materials->FindMaterial( "dev/blurfiltery_nohdr", TEXTURE_GROUP_OTHER, true );
}
return s_pYBlurMaterial;
}
EXPOSE_MATERIAL_PROXY( CYBlurMaterialProxy, blurfiltery );
static void SetRenderTargetAndViewPort(ITexture *rt)
{
CMatRenderContextPtr pRenderContext( materials );
pRenderContext->SetRenderTarget(rt);
if ( rt )
{
pRenderContext->Viewport(0,0,rt->GetActualWidth(),rt->GetActualHeight());
}
}
enum HistogramEntryState_t
{
HESTATE_INITIAL = 0,
HESTATE_FIRST_QUERY_IN_FLIGHT,
HESTATE_QUERY_IN_FLIGHT,
HESTATE_QUERY_DONE,
};
#define NUM_HISTOGRAM_BUCKETS 31
#define NUM_HISTOGRAM_BUCKETS_NEW 17
#define MAX_QUERIES_PER_FRAME 1
class CHistogramBucket
{
public:
HistogramEntryState_t m_state;
OcclusionQueryObjectHandle_t m_hOcclusionQueryHandle;
int m_nFrameQueued; // when this query was last queued
int m_nPixels; // # of pixels this histogram represents
int m_nPixelsInRange;
float m_flMinLuminance, m_flMaxLuminance; // the luminance range this entry was queried with
float m_flScreenMinX, m_flScreenMinY, m_flScreenMaxX, m_flScreenMaxY; // range is 0..1 in fractions of the screen
bool ContainsValidData( void )
{
return ( m_state == HESTATE_QUERY_DONE ) || ( m_state == HESTATE_QUERY_IN_FLIGHT );
}
void IssueQuery( int nFrameNum );
};
void CHistogramBucket::IssueQuery( int nFrameNum )
{
CMatRenderContextPtr pRenderContext( materials );
if ( !m_hOcclusionQueryHandle )
{
m_hOcclusionQueryHandle = pRenderContext->CreateOcclusionQueryObject();
}
int nViewportX, nViewportY, nViewportWidth, nViewportHeight;
pRenderContext->GetViewport( nViewportX, nViewportY, nViewportWidth, nViewportHeight );
// Find min and max gamma-space text range
float flTestRangeMin = ( m_flMinLuminance == 0.0f ) ? -1e20f : m_flMinLuminance; // Count all pixels < 0.0 as 0.0 (for float HDR buffers)
float flTestRangeMax = ( m_flMaxLuminance == 1.0f ) ? 1e20f : m_flMaxLuminance; // Count all pixels >1.0 as 1.0
// Set stencil bits where the colors match
IMaterial *pLumCompareMaterial;
#if defined(_PS3)
if( mat_PS3_findpostvarsfast.GetInt() )
{
pLumCompareMaterial = CLumCompareMaterialProxy::GetLumCompareMaterial( materials );
CLumCompareMaterialProxy::SetupLumCompareMaterial( flTestRangeMin, flTestRangeMax );
}
else
#endif
{
pLumCompareMaterial = materials->FindMaterial( "dev/lumcompare", TEXTURE_GROUP_OTHER, true );
IMaterialVar *pMinVar = pLumCompareMaterial->FindVar( "$C0_X", NULL );
pMinVar->SetFloatValue( flTestRangeMin );
IMaterialVar *pMaxVar = pLumCompareMaterial->FindVar( "$C0_Y", NULL );
pMaxVar->SetFloatValue( flTestRangeMax );
}
int nScreenMinX = FLerp( nViewportX, ( nViewportX + nViewportWidth - 1 ), 0, 1, m_flScreenMinX );
int nScreenMaxX = FLerp( nViewportX, ( nViewportX + nViewportWidth - 1 ), 0, 1, m_flScreenMaxX );
int nScreenMinY = FLerp( nViewportY, ( nViewportY + nViewportHeight - 1 ), 0, 1, m_flScreenMinY );
int nScreenMaxY = FLerp( nViewportY, ( nViewportY + nViewportHeight - 1 ), 0, 1, m_flScreenMaxY );
float flExposureWidthScale, flExposureHeightScale;
// Shrink region of interest if the flashlight is on
flExposureWidthScale = ( 0.5f * ( 1.0f - mat_exposure_center_region_x.GetFloat() ) );
flExposureHeightScale = ( 0.5f * ( 1.0f - mat_exposure_center_region_y.GetFloat() ) );
int nBorderWidth = ( nScreenMaxX - nScreenMinX + 1 ) * flExposureWidthScale;
int nBorderHeight = ( nScreenMaxY - nScreenMinY + 1 ) * flExposureHeightScale;
// Do luminance compare
m_nPixels = ( 1 + nScreenMaxX - nScreenMinX ) * ( 1 + nScreenMaxY - nScreenMinY );
ShaderStencilState_t state;
if ( mat_tonemapping_occlusion_use_stencil.GetInt() )
{
state.m_nWriteMask = 1;
state.m_bEnable = true;
state.m_PassOp = SHADER_STENCILOP_SET_TO_REFERENCE;
state.m_CompareFunc = SHADER_STENCILFUNC_ALWAYS;
state.m_FailOp = SHADER_STENCILOP_KEEP;
state.m_ZFailOp = SHADER_STENCILOP_KEEP;
state.m_nReferenceValue = 0x80;
state.m_nWriteMask = 0x80;
pRenderContext->SetStencilState( state );
}
else
{
pRenderContext->BeginOcclusionQueryDrawing( m_hOcclusionQueryHandle );
}
int nWindowWidth = 0;
int nWindowHeight = 0;
pRenderContext->GetWindowSize( nWindowWidth, nWindowHeight );
nScreenMinX += nBorderWidth;
nScreenMinY += nBorderHeight;
nScreenMaxX -= nBorderWidth;
nScreenMaxY -= nBorderHeight;
pRenderContext->DrawScreenSpaceRectangle( pLumCompareMaterial,
nScreenMinX - nViewportX, nScreenMinY - nViewportY,
1 + nScreenMaxX - nScreenMinX,
1 + nScreenMaxY - nScreenMinY,
nScreenMinX, nScreenMinY,
nScreenMaxX, nScreenMaxY,
nWindowWidth, nWindowHeight );
if ( mat_tonemapping_occlusion_use_stencil.GetInt() )
{
// Start counting how many pixels had their stencil bit set via an occlusion query
pRenderContext->BeginOcclusionQueryDrawing( m_hOcclusionQueryHandle );
// Issue an occlusion query using stencil as the mask
state.m_bEnable = true;
state.m_nTestMask = 0x80;
state.m_PassOp = SHADER_STENCILOP_KEEP;
state.m_CompareFunc = SHADER_STENCILFUNC_EQUAL;
state.m_FailOp = SHADER_STENCILOP_KEEP;
state.m_ZFailOp = SHADER_STENCILOP_KEEP;
state.m_nReferenceValue = 0x80;
pRenderContext->SetStencilState( state );
IMaterial *pLumCompareStencilMaterial;
#if defined(_PS3)
if( mat_PS3_findpostvarsfast.GetInt() )
pLumCompareStencilMaterial = CLumCompareStencilMaterialProxy::GetLumCompareStencilMaterial( materials );
else
#endif
pLumCompareStencilMaterial = materials->FindMaterial( "dev/no_pixel_write", TEXTURE_GROUP_OTHER, true);
pRenderContext->DrawScreenSpaceRectangle( pLumCompareStencilMaterial,
nScreenMinX, nScreenMinY,
1 + nScreenMaxX - nScreenMinX,
1 + nScreenMaxY - nScreenMinY,
nScreenMinX, nScreenMinY,
nScreenMaxX, nScreenMaxY,
nWindowWidth, nWindowHeight );
ShaderStencilState_t stateDisable;
stateDisable.m_bEnable = false;
pRenderContext->SetStencilState( stateDisable );
}
pRenderContext->EndOcclusionQueryDrawing( m_hOcclusionQueryHandle );
if ( m_state == HESTATE_INITIAL )
m_state = HESTATE_FIRST_QUERY_IN_FLIGHT;
else
m_state = HESTATE_QUERY_IN_FLIGHT;
m_nFrameQueued = nFrameNum;
}
#define HISTOGRAM_BAR_SIZE 200
class CTonemapSystem
{
CHistogramBucket m_histogramBucketArray[NUM_HISTOGRAM_BUCKETS];
int m_nCurrentQueryFrame;
int m_nCurrentAlgorithm;
float m_flTargetTonemapScale;
float m_flCurrentTonemapScale;
int m_nNumMovingAverageValid;
float m_movingAverageTonemapScale[10];
bool m_bOverrideTonemapScaleEnabled;
float m_flOverrideTonemapScale;
public:
void IssueAndReceiveBucketQueries();
void UpdateBucketRanges();
float FindLocationOfPercentBrightPixels( float flPercentBrightPixels, float flPercentTarget );
float ComputeTargetTonemapScalar( bool bGetIdealTargetForDebugMode );
void UpdateMaterialSystemTonemapScalar();
void SetTargetTonemappingScale( float flTonemapScale );
void ResetTonemappingScale( float flTonemapScale );
void SetTonemapScale( IMatRenderContext *pRenderContext, float newvalue, float minvalue, float maxvalue );
float GetTargetTonemappingScale() { return m_flTargetTonemapScale; }
float GetCurrentTonemappingScale() { return m_flCurrentTonemapScale; }
void SetOverrideTonemapScale( bool bEnableOverride, float flTonemapScale );
// Dev functions
void DisplayHistogram();
// Constructor
CTonemapSystem()
{
m_nCurrentQueryFrame = 0;
m_nCurrentAlgorithm = -1;
m_flTargetTonemapScale = 1.0f;
m_flCurrentTonemapScale = 1.0f;
m_nNumMovingAverageValid = 0;
for ( int i = 0; i < ARRAYSIZE( m_movingAverageTonemapScale ) - 1; i++ )
{
m_movingAverageTonemapScale[i] = 1.0f;
}
m_bOverrideTonemapScaleEnabled = false;
m_flOverrideTonemapScale = 1.0f;
UpdateBucketRanges();
}
};
CTonemapSystem * GetCurrentTonemappingSystem()
{
static CTonemapSystem s_HDR_HistogramSystem[ MAX_SPLITSCREEN_PLAYERS ];
int slot = GET_ACTIVE_SPLITSCREEN_SLOT();
return &( s_HDR_HistogramSystem[ slot ] );
}
void CTonemapSystem::IssueAndReceiveBucketQueries()
{
UpdateBucketRanges();
// Find which histogram entries should have something done this frame
int nQueriesIssuedThisFrame = 0;
m_nCurrentQueryFrame++;
int nNumHistogramBuckets = NUM_HISTOGRAM_BUCKETS;
if ( mat_tonemap_algorithm.GetInt() == 1 )
nNumHistogramBuckets = NUM_HISTOGRAM_BUCKETS_NEW;
for ( int i = 0; i < nNumHistogramBuckets; i++ )
{
switch ( m_histogramBucketArray[i].m_state )
{
case HESTATE_INITIAL:
if ( nQueriesIssuedThisFrame<MAX_QUERIES_PER_FRAME )
{
m_histogramBucketArray[i].IssueQuery(m_nCurrentQueryFrame);
nQueriesIssuedThisFrame++;
}
break;
case HESTATE_FIRST_QUERY_IN_FLIGHT:
case HESTATE_QUERY_IN_FLIGHT:
if ( m_nCurrentQueryFrame > m_histogramBucketArray[i].m_nFrameQueued + 2 )
{
CMatRenderContextPtr pRenderContext( materials );
int np = pRenderContext->OcclusionQuery_GetNumPixelsRendered(
m_histogramBucketArray[i].m_hOcclusionQueryHandle );
if ( np != -1 ) // -1 = Query not finished...wait until next time
{
m_histogramBucketArray[i].m_nPixelsInRange = np;
m_histogramBucketArray[i].m_state = HESTATE_QUERY_DONE;
}
}
break;
}
}
// Now, issue queries for the oldest finished queries we have
while ( nQueriesIssuedThisFrame < MAX_QUERIES_PER_FRAME )
{
int nNumHistogramBuckets = NUM_HISTOGRAM_BUCKETS;
if ( mat_tonemap_algorithm.GetInt() == 1 )
nNumHistogramBuckets = NUM_HISTOGRAM_BUCKETS_NEW;
int nOldestSoFar = -1;
for ( int i = 0; i < nNumHistogramBuckets; i++ )
{
if ( ( m_histogramBucketArray[i].m_state == HESTATE_QUERY_DONE ) &&
( ( nOldestSoFar == -1 ) || ( m_histogramBucketArray[i].m_nFrameQueued < m_histogramBucketArray[nOldestSoFar].m_nFrameQueued ) ) )
{
nOldestSoFar = i;
}
}
if ( nOldestSoFar == -1 ) // Nothing to do
break;
m_histogramBucketArray[nOldestSoFar].IssueQuery( m_nCurrentQueryFrame );
nQueriesIssuedThisFrame++;
}
}
float CTonemapSystem::FindLocationOfPercentBrightPixels( float flPercentBrightPixels, float flPercentTargetToSnapToIfInSameBin = -1.0f )
{
if ( mat_tonemap_algorithm.GetInt() == 1 ) // New algorithm
{
int nTotalValidPixels = 0;
for ( int i = 0; i < ( NUM_HISTOGRAM_BUCKETS_NEW - 1 ); i++ )
{
if ( m_histogramBucketArray[i].ContainsValidData() )
{
nTotalValidPixels += m_histogramBucketArray[i].m_nPixelsInRange;
}
}
if ( nTotalValidPixels == 0 )
{
return -1.0f;
}
// Find where percent range border is
float flTotalPercentRangeTested = 0.0f;
float flTotalPercentPixelsTested = 0.0f;
for ( int i = ( NUM_HISTOGRAM_BUCKETS_NEW - 2 ); i >= 0; i-- ) // Start at the bright end
{
if ( !m_histogramBucketArray[i].ContainsValidData() )
return -1.0f;
float flPixelPercentNeeded = ( flPercentBrightPixels / 100.0f ) - flTotalPercentPixelsTested;
float flThisBinPercentOfTotalPixels = float( m_histogramBucketArray[i].m_nPixelsInRange ) / float( nTotalValidPixels );
float flThisBinLuminanceRange = m_histogramBucketArray[i].m_flMaxLuminance - m_histogramBucketArray[i].m_flMinLuminance;
if ( flThisBinPercentOfTotalPixels >= flPixelPercentNeeded ) // We found the bin needed
{
if ( flPercentTargetToSnapToIfInSameBin >= 0.0f )
{
if ( ( m_histogramBucketArray[i].m_flMinLuminance <= ( flPercentTargetToSnapToIfInSameBin / 100.0f ) ) && ( m_histogramBucketArray[i].m_flMaxLuminance >= ( flPercentTargetToSnapToIfInSameBin / 100.0f ) ) )
{
// Sticky bin...We're in the same bin as the target so keep the tonemap scale where it is
return ( flPercentTargetToSnapToIfInSameBin / 100.0f );
}
}
float flPercentOfThesePixelsNeeded = flPixelPercentNeeded / flThisBinPercentOfTotalPixels;
float flPercentLocationOfBorder = 1.0f - ( flTotalPercentRangeTested + ( flThisBinLuminanceRange * flPercentOfThesePixelsNeeded ) );
flPercentLocationOfBorder = MAX( m_histogramBucketArray[i].m_flMinLuminance, MIN( m_histogramBucketArray[i].m_flMaxLuminance, flPercentLocationOfBorder ) ); // Clamp to this bin just in case
return flPercentLocationOfBorder;
}
flTotalPercentPixelsTested += flThisBinPercentOfTotalPixels;
flTotalPercentRangeTested += flThisBinLuminanceRange;
}
return -1.0f;
}
else
{
// Don't know what to do for other algorithms yet
return -1.0f;
}
}
float CTonemapSystem::ComputeTargetTonemapScalar( bool bGetIdealTargetForDebugMode = false )
{
if ( mat_tonemap_algorithm.GetInt() == 1 ) // New algorithm
{
float flTonemapPercentTarget = mat_force_tonemap_percent_target.GetFloat() >= 0.0f ? mat_force_tonemap_percent_target.GetFloat() : g_flTonemapPercentTarget;
float flTonemapPercentBrightPixels = mat_force_tonemap_percent_bright_pixels.GetFloat() >= 0.0f ? mat_force_tonemap_percent_bright_pixels.GetFloat() : g_flTonemapPercentBrightPixels;
float flTonemapMinAvgLum = mat_force_tonemap_min_avglum.GetFloat() >= 0.0f ? mat_force_tonemap_min_avglum.GetFloat() : g_flTonemapMinAvgLum;
float flPercentLocationOfTarget;
if ( bGetIdealTargetForDebugMode == true)
flPercentLocationOfTarget = FindLocationOfPercentBrightPixels( flTonemapPercentBrightPixels ); // Don't pass in the second arg so the scalar doesn't snap to a bin
else
flPercentLocationOfTarget = FindLocationOfPercentBrightPixels( flTonemapPercentBrightPixels, flTonemapPercentTarget );
if ( flPercentLocationOfTarget < 0.0f ) // This is the return error code
{
flPercentLocationOfTarget = flTonemapPercentTarget / 100.0f; // Pretend we're at the target
}
// Make sure this is > 0.0f
flPercentLocationOfTarget = MAX( 0.0001f, flPercentLocationOfTarget );
// Compute target scalar
float flTargetScalar = ( flTonemapPercentTarget / 100.0f ) / flPercentLocationOfTarget;
// Compute secondary target scalar
float flAverageLuminanceLocation = FindLocationOfPercentBrightPixels( 50.0f );
if ( flAverageLuminanceLocation > 0.0f )
{
float flTargetScalar2 = ( flTonemapMinAvgLum / 100.0f ) / flAverageLuminanceLocation;
// Only override it if it's trying to brighten the image more than the primary algorithm
if ( flTargetScalar2 > flTargetScalar )
{
flTargetScalar = flTargetScalar2;
}
}
// Apply this against last frames scalar
CMatRenderContextPtr pRenderContext( materials );
float flLastScale = m_flCurrentTonemapScale;
flTargetScalar *= flLastScale;
flTargetScalar = MAX( 0.001f, flTargetScalar );
return flTargetScalar;
}
else // Original tonemapping algorithm
{
float flScaleValue = 1.0f;
if ( m_histogramBucketArray[NUM_HISTOGRAM_BUCKETS-1].ContainsValidData() )
{
flScaleValue = m_histogramBucketArray[NUM_HISTOGRAM_BUCKETS-1].m_nPixels * ( 1.0f / m_histogramBucketArray[NUM_HISTOGRAM_BUCKETS-1].m_nPixelsInRange );
}
if ( !IsFinite( flScaleValue ) )
{
flScaleValue = 1.0f;
}
float flTotal = 0.0f;
int nTotalPixels = 0;
for ( int i=0; i<NUM_HISTOGRAM_BUCKETS-1; i++ )
{
if ( m_histogramBucketArray[i].ContainsValidData() )
{
flTotal += flScaleValue * m_histogramBucketArray[i].m_nPixelsInRange * AVG( m_histogramBucketArray[i].m_flMinLuminance, m_histogramBucketArray[i].m_flMaxLuminance );
nTotalPixels += m_histogramBucketArray[i].m_nPixels;
}
}
float flAverageLuminance = 0.5f;
if ( nTotalPixels > 0 )
flAverageLuminance = flTotal * ( 1.0f / nTotalPixels );
else
flAverageLuminance = 0.5f;
// Make sure this is > 0.0f
flAverageLuminance = MAX( 0.0001f, flAverageLuminance );
// Compute target scalar
float flTargetScalar = 0.005f / flAverageLuminance;
return flTargetScalar;
}
}
static float GetCurrentBloomScale( void )
{
// Use the appropriate bloom scale settings. Mapmakers's overrides the convar settings.
float flCurrentBloomScale = 1.0f;
if ( g_bUseCustomBloomScale )
{
flCurrentBloomScale = g_flCustomBloomScale;
}
else
{
flCurrentBloomScale = mat_bloomscale.GetFloat();
}
return flCurrentBloomScale;
}
static void GetExposureRange( float *pflAutoExposureMin, float *pflAutoExposureMax )
{
// Get min
if ( ( g_bUseCustomAutoExposureMin ) && ( g_flCustomAutoExposureMin > 0.0f ) )
{
*pflAutoExposureMin = g_flCustomAutoExposureMin;
}
else
{
*pflAutoExposureMin = mat_autoexposure_min.GetFloat();
}
// Get max