forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctexturecompositor.cpp
2842 lines (2310 loc) · 89.9 KB
/
ctexturecompositor.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 Valve Corporation, All rights reserved. ================================== //
//
// Purpose:
//
//============================================================================================== //
#include "pch_materialsystem.h"
#include "ctexturecompositor.h"
#include "materialsystem/itexture.h"
#include "materialsystem/imaterialsystem.h"
#include "materialsystem/combineoperations.h"
#include "texturemanager.h"
#define MATSYS_INTERNAL // Naughty!
#include "cmaterialsystem.h"
#include "tier0/memdbgon.h"
#ifndef _WINDOWS
#define sscanf_s sscanf
#endif
// If this is 0 or unset, we won't use the caching functionality.
#define WITH_TEX_COMPOSITE_CACHE 1
#ifdef STAGING_ONLY // Always should remain staging only.
ConVar r_texcomp_dump( "r_texcomp_dump", "0", FCVAR_NONE, "Whether we should dump the textures to disk or not. 1: Save all; 2: Save Final; 3: Save Final with name suitable for scripting; 4: Save Final and skip saving workshop icons." );
#endif
const int cMaxSelectors = 16;
// Ugh, this is annoying and matches TF's enums. That's lame. We should workaround this.
enum { Neutral = 0, Red = 2, Blue = 3 };
static int s_nDumpCount = 0;
static CInterlockedInt s_nCompositeCount = 0;
void ComputeTextureMatrixFromRectangle( VMatrix* pOutMat, const Vector2D& bl, const Vector2D& tl, const Vector2D& tr );
bool HasCycle( CTextureCompositorTemplate* pStartTempl );
CTextureCompositorTemplate* Advance( CTextureCompositorTemplate* pTmpl, int nSteps );
void PrintMinimumCycle( CTextureCompositorTemplate* pStartTempl );
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
struct CTCStageResult_t
{
ITexture* m_pTexture;
ITexture* m_pRenderTarget;
float m_fAdjustBlackPoint;
float m_fAdjustWhitePoint;
float m_fAdjustGamma;
matrix3x4_t m_mUvAdjust;
inline CTCStageResult_t()
: m_pTexture(NULL)
, m_pRenderTarget(NULL)
, m_fAdjustBlackPoint(0.0f)
, m_fAdjustWhitePoint(1.0f)
, m_fAdjustGamma(1.0f)
{
SetIdentityMatrix( m_mUvAdjust );
}
inline void Cleanup( CTextureCompositor* _comp )
{
if ( m_pRenderTarget )
_comp->ReleaseCompositorRenderTarget( m_pRenderTarget );
m_pTexture = NULL;
m_pRenderTarget = NULL;
}
};
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
class CTCStage : public IAsyncTextureOperationReceiver
{
public:
CTCStage();
protected:
// Called by Release()
virtual ~CTCStage();
public:
// IAsyncTextureOperationReceiver
virtual int AddRef() OVERRIDE;
virtual int Release() OVERRIDE;
virtual int GetRefCount() const OVERRIDE { return m_nReferenceCount; }
virtual void OnAsyncCreateComplete( ITexture* pTex, void* pExtraArgs ) OVERRIDE { }
virtual void OnAsyncFindComplete( ITexture* pTex, void* pExtraArgs ) OVERRIDE { }
virtual void OnAsyncMapComplete( ITexture* pTex, void* pExtraArgs, void* pMemory, int pPitch ) OVERRIDE { }
virtual void OnAsyncReadbackBegin( ITexture* pDst, ITexture* pSrc, void* pExtraArgs ) OVERRIDE { }
// Our stuff.
void Resolve( bool bFirstTime, CTextureCompositor* _comp );
inline ECompositeResolveStatus GetResolveStatus() const { return m_ResolveStatus; }
inline const CTCStageResult_t& GetResult() const { Assert( GetResolveStatus() == ECRS_Complete ); return m_Result; }
bool HasTeamSpecifics() const;
void ComputeRandomValues( int* pCurIndex, CUniformRandomStream* pRNGs, int nRNGCount );
inline void SetFirstChild( CTCStage* _stage ) { m_pFirstChild = _stage; }
inline void SetNextSibling( CTCStage* _stage ) { m_pNextSibling = _stage; }
inline CTCStage* GetFirstChild() { return m_pFirstChild; }
inline CTCStage* GetNextSibling() { return m_pNextSibling; }
inline const CTCStage* GetFirstChild() const { return m_pFirstChild; }
inline const CTCStage* GetNextSibling() const { return m_pNextSibling; }
void AppendChildren( const CUtlVector< CTCStage* >& _children )
{
// Do these in reverse order, they will wind up in the right order
FOR_EACH_VEC_BACK( _children, i )
{
CTCStage* childStage = _children[i];
childStage->SetNextSibling( GetFirstChild() );
SetFirstChild( childStage );
}
}
void CleanupChildResults( CTextureCompositor* _comp );
// Render a quad with _mat using _inputs to _destRT
void Render( ITexture* _destRT, IMaterial* _mat, const CUtlVector<CTCStageResult_t>& _inputs, CTextureCompositor* _comp, bool bClear );
void Cleanup( CTextureCompositor* _comp );
// Does this stage target a render target or a texture?
virtual bool DoesTargetRenderTarget() const = 0;
inline void SetResult( const CTCStageResult_t& _result )
{
Assert( m_ResolveStatus != ECRS_Complete );
m_Result = _result;
m_ResolveStatus = ECRS_Complete;
}
protected:
inline void SetResolveStatus( ECompositeResolveStatus _status )
{
m_ResolveStatus = _status;
}
// This function is called only once during the first ResolveTraversal, and is
// for the compositor to request its textures. Textures should not be requested
// before this or they can be held waaaay too long.
virtual void RequestTextures() = 0;
// This function will be called during Resolve traversal. At the point when this is called,
// all of this node's children will have had their resolve completed. Our siblings will
// not have resolved yet.
virtual void ResolveThis( CTextureCompositor* _comp ) = 0;
// This function is called during HasTeamSpecifics traversal.
virtual bool HasTeamSpecificsThis() const = 0;
virtual bool ComputeRandomValuesThis( CUniformRandomStream* pRNG ) = 0;
private:
CInterlockedInt m_nReferenceCount;
CTCStage* m_pFirstChild;
CTCStage* m_pNextSibling;
CTCStageResult_t m_Result;
ECompositeResolveStatus m_ResolveStatus;
};
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
typedef void ( *ParseSingleKV )( KeyValues* _kv, void* _dest );
struct ParseTableEntry
{
const char* keyName;
ParseSingleKV parseFunc;
size_t structOffset;
};
// ------------------------------------------------------------------------------------------------
struct Range
{
float low;
float high;
Range( )
: low( 0 )
, high( 0 )
{ }
Range( float _l, float _h )
: low( _l )
, high( _h )
{ }
};
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
void ParseBoolFromKV( KeyValues* _kv, void* _pDest )
{
bool* realDest = ( bool* ) _pDest;
( *realDest ) = _kv->GetBool();
}
// ------------------------------------------------------------------------------------------------
template<int N>
void ParseIntVectorFromKV( KeyValues* _kv, void* _pDest )
{
CCopyableUtlVector<int>* realDest = ( CCopyableUtlVector<int>* ) _pDest;
const int parsedValue = _kv->GetInt();
if ( realDest->Size() < N )
{
realDest->AddToTail( parsedValue );
}
else
{
DevWarning( "Too many numbers (>%d), ignoring the value '%d'.\n", N, parsedValue );
}
}
// ------------------------------------------------------------------------------------------------
template< class T >
CUtlString AsStringT( const T& _val )
{
#ifdef _WIN32
// Not sure why linux is unhappy here. Error messages unhelpful. Thanks, GCC.
static_assert( false, "Must add specialization for typename T" );
#endif
return CUtlString( "" );
}
// ------------------------------------------------------------------------------------------------
template<>
CUtlString AsStringT< int >( const int& _val )
{
char buffer[ 12 ];
V_sprintf_safe( buffer, "%d", _val );
return CUtlString( buffer );
}
// ------------------------------------------------------------------------------------------------
template< class T >
void ParseTFromKV( KeyValues* _kv, void* _pDest )
{
#ifdef _WIN32
// Not sure why linux is unhappy here. Error messages unhelpful. Thanks, GCC.
static_assert( false, "Must add specialization for typename T" );
#endif
}
// ------------------------------------------------------------------------------------------------
template<>
void ParseTFromKV< int >( KeyValues* _kv, void* _pDest )
{
int* realDest = ( int* ) _pDest;
( *realDest ) = _kv->GetInt();
}
// ------------------------------------------------------------------------------------------------
template<>
void ParseTFromKV< Vector2D >( KeyValues* _kv, void* _pDest )
{
Vector2D* realDest = ( Vector2D* ) _pDest;
Vector2D tmpDest;
int count = sscanf_s( _kv->GetString(), "%f %f", &tmpDest.x, &tmpDest.y );
if ( count != 2 )
{
Error( "Expected exactly two values, %d were provided.\n", count );
return;
}
*realDest = tmpDest;
}
// ------------------------------------------------------------------------------------------------
template< class T, int N = INT_MAX >
void ParseVectorFromKV( KeyValues* _kv, void* _pDest )
{
CCopyableUtlVector< T >* realDest = ( CCopyableUtlVector< T >* ) _pDest;
T parsedValue = T();
ParseTFromKV<T>( _kv, &parsedValue );
if ( realDest->Size() < N )
{
realDest->AddToTail( parsedValue );
}
else
{
DevWarning( "Too many entries (>%d), ignoring the value '%s'.\n", N, AsStringT( parsedValue ).Get() );
}
}
// ------------------------------------------------------------------------------------------------
void ParseRangeFromKV( KeyValues* _kv, void* _pDest )
{
Range* realDest = ( Range* ) _pDest;
Range tmpDest;
int count = sscanf_s( _kv->GetString(), "%f %f", &tmpDest.low, &tmpDest.high );
switch (count)
{
case 1:
// If we parse one, use the same value for low and high.
( *realDest ).low = tmpDest.low;
( *realDest ).high = tmpDest.low;
break;
case 2:
// If we parse two, they're both correct.
( *realDest ).low = tmpDest.low;
( *realDest ).high = tmpDest.high;
break;
// error cases
case EOF:
case 0:
default:
Error( "Incorrect number of numbers while parsing, using defaults. This error message should be improved\n" );
};
}
// ------------------------------------------------------------------------------------------------
void ParseInverseRangeFromKV( KeyValues* _kv, void* _pDest )
{
const float kSubstValue = 0.00001;
ParseRangeFromKV( _kv, _pDest );
Range* realDest = ( Range* ) _pDest;
if ( realDest->low != 0.0f )
{
( *realDest ).low = 1.0f / realDest->low;
}
else
{
Error( "Specified 0.0 for low value, that is illegal in this field. Substituting %.5f\n", kSubstValue );
( *realDest ).low = kSubstValue;
}
if ( realDest->high != 0.0f )
{
( *realDest ).high = 1.0f / realDest->high;
}
else
{
Error( "Specified 0.0 for high value, that is illegal in this field. Substituting %.5f\n", kSubstValue );
( *realDest ).high = kSubstValue;
}
}
// ------------------------------------------------------------------------------------------------
template < int Div >
void ParseRangeThenDivideBy( KeyValues *_kv, void* _pDest )
{
static_assert( Div != 0, "Cannot specify a divisor of 0." );
float fDiv = (float) Div;
ParseRangeFromKV( _kv, _pDest );
Range* realDest = ( Range* ) _pDest;
( *realDest ).low = ( *realDest ).low / fDiv;
( *realDest ).high = ( *realDest ).high / fDiv;
}
// ------------------------------------------------------------------------------------------------
void ParseStringFromKV( KeyValues* _kv, void* _pDest )
{
CUtlString* realDest = ( CUtlString* ) _pDest;
(*realDest) = _kv->GetString();
}
// ------------------------------------------------------------------------------------------------
struct TextureStageParameters
{
CUtlString m_pTexFilename;
CUtlString m_pTexRedFilename;
CUtlString m_pTexBlueFilename;
Range m_AdjustBlack;
Range m_AdjustOffset;
Range m_AdjustGamma;
Range m_Rotation;
Range m_TranslateU;
Range m_TranslateV;
Range m_ScaleUV;
bool m_AllowFlipU;
bool m_AllowFlipV;
bool m_Evaluate;
TextureStageParameters()
: m_AdjustBlack( 0, 0 )
, m_AdjustOffset( 1, 1 )
, m_AdjustGamma( 1, 1 )
, m_Rotation( 0 , 0 )
, m_TranslateU( 0, 0 )
, m_TranslateV( 0, 0 )
, m_ScaleUV( 1, 1 )
, m_AllowFlipU( false )
, m_AllowFlipV( false )
, m_Evaluate( true )
{ }
};
// ------------------------------------------------------------------------------------------------
const ParseTableEntry cTextureStageParametersParseTable[] =
{
{ "texture", ParseStringFromKV, offsetof( TextureStageParameters, m_pTexFilename ) },
{ "texture_red", ParseStringFromKV, offsetof( TextureStageParameters, m_pTexRedFilename ) },
{ "texture_blue", ParseStringFromKV, offsetof( TextureStageParameters, m_pTexBlueFilename ) },
{ "adjust_black", ParseRangeThenDivideBy<255>, offsetof( TextureStageParameters, m_AdjustBlack ) },
{ "adjust_offset", ParseRangeThenDivideBy<255>, offsetof( TextureStageParameters, m_AdjustOffset ) },
{ "adjust_gamma", ParseInverseRangeFromKV, offsetof( TextureStageParameters, m_AdjustGamma ) },
{ "rotation", ParseRangeFromKV, offsetof( TextureStageParameters, m_Rotation ) },
{ "translate_u", ParseRangeFromKV, offsetof( TextureStageParameters, m_TranslateU ) },
{ "translate_v", ParseRangeFromKV, offsetof( TextureStageParameters, m_TranslateV ) },
{ "scale_uv", ParseRangeFromKV, offsetof( TextureStageParameters, m_ScaleUV ) },
{ "flip_u", ParseBoolFromKV, offsetof( TextureStageParameters, m_AllowFlipU ) },
{ "flip_v", ParseBoolFromKV, offsetof( TextureStageParameters, m_AllowFlipV ) },
{ "evaluate?", ParseBoolFromKV, offsetof( TextureStageParameters, m_Evaluate ) },
{ 0, 0 }
};
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
class CTCTextureStage : public CTCStage
{
public:
CTCTextureStage( const TextureStageParameters& _tsp, uint32 nTexCompositeCreateFlags )
: m_Parameters( _tsp )
, m_pTex( NULL )
, m_pTexRed( NULL )
, m_pTexBlue( NULL )
{
}
virtual ~CTCTextureStage()
{
SafeRelease( &m_pTex );
SafeRelease( &m_pTexBlue );
SafeRelease( &m_pTexRed );
}
virtual void OnAsyncFindComplete( ITexture* pTex, void* pExtraArgs )
{
switch ( ( intp ) pExtraArgs )
{
case Neutral:
SafeAssign( &m_pTex, pTex );
break;
case Red:
SafeAssign( &m_pTexRed, pTex );
break;
case Blue:
SafeAssign( &m_pTexBlue, pTex );
break;
default:
Assert( !"Unexpected value passed to OnAsyncFindComplete" );
break;
};
}
virtual bool DoesTargetRenderTarget() const { return false; }
protected:
bool AreTexturesLoaded() const
{
if ( !m_Parameters.m_pTexFilename.IsEmpty() && !m_pTex )
return false;
if ( !m_Parameters.m_pTexRedFilename.IsEmpty() && !m_pTexRed )
return false;
if ( !m_Parameters.m_pTexBlueFilename.IsEmpty() && !m_pTexBlue )
return false;
return true;
}
ITexture* GetTeamSpecificTexture( int nTeam )
{
if ( nTeam == Red && m_pTexRed )
return m_pTexRed;
if ( nTeam == Blue && m_pTexBlue )
return m_pTexBlue;
return m_pTex;
}
virtual void RequestTextures()
{
if ( !m_Parameters.m_pTexFilename.IsEmpty() )
materials->AsyncFindTexture( m_Parameters.m_pTexFilename.Get(), TEXTURE_GROUP_RUNTIME_COMPOSITE, this, ( void* ) Neutral, false, TEXTUREFLAGS_IMMEDIATE_CLEANUP );
if ( !m_Parameters.m_pTexRedFilename.IsEmpty() )
materials->AsyncFindTexture( m_Parameters.m_pTexRedFilename.Get(), TEXTURE_GROUP_RUNTIME_COMPOSITE, this, ( void* ) Red, false, TEXTUREFLAGS_IMMEDIATE_CLEANUP );
if ( !m_Parameters.m_pTexBlueFilename.IsEmpty() )
materials->AsyncFindTexture( m_Parameters.m_pTexBlueFilename.Get(), TEXTURE_GROUP_RUNTIME_COMPOSITE, this, ( void* ) Blue, false, TEXTUREFLAGS_IMMEDIATE_CLEANUP );
}
virtual void ResolveThis( CTextureCompositor* _comp )
{
tmZone( TELEMETRY_LEVEL0, TMZF_NONE, "%s", __FUNCTION__ );
// We shouldn't have any children, we're going to ignore them anyways.
Assert( GetFirstChild() == NULL );
ECompositeResolveStatus resolveStatus = GetResolveStatus();
// If we're done, we're done.
if ( resolveStatus == ECRS_Complete || resolveStatus == ECRS_Error )
return;
if ( resolveStatus == ECRS_Scheduled )
SetResolveStatus( ECRS_PendingTextureLoads );
// Someone is misusing this node if this assert fires.
Assert( GetResolveStatus() == ECRS_PendingTextureLoads );
// When the texture has finished loading, this will be set to the texture we should use.
if ( !AreTexturesLoaded() )
return;
if ( !m_pTex && !m_pTexRed && !m_pTexBlue )
{
_comp->Error( false, "Invalid texture_lookup node, must specify at least texture (or texture_red and texture_blue) or all of them.\n" );
return;
}
if ( m_pTex && m_pTex->IsError() )
{
_comp->Error( false, "Failed to load texture '%s', this is non-recoverable.\n", m_Parameters.m_pTexFilename.Get() );
return;
}
if ( m_pTexRed && m_pTexRed->IsError() )
{
_comp->Error( false, "Failed to load texture_red '%s', this is non-recoverable.\n", m_Parameters.m_pTexRedFilename.Get() );
return;
}
if ( m_pTexBlue && m_pTexBlue->IsError() )
{
_comp->Error( false, "Failed to load texture_blue '%s', this is non-recoverable.\n", m_Parameters.m_pTexBlueFilename.Get() );
return;
}
CTCStageResult_t res;
res.m_pTexture = GetTeamSpecificTexture( _comp->GetTeamNumber() );
res.m_fAdjustBlackPoint = m_fAdjustBlack;
res.m_fAdjustWhitePoint = m_fAdjustWhite;
res.m_fAdjustGamma = m_fAdjustGamma;
// Store the matrix into the uv adjustment matrix
m_mTextureAdjust.Set3x4( res.m_mUvAdjust );
SetResult( res );
CleanupChildResults( _comp );
tmMessage( TELEMETRY_LEVEL0, TMMF_ICON_NOTE, "Completed: %s", __FUNCTION__ );
}
virtual bool HasTeamSpecificsThis() const OVERRIDE
{
return !m_Parameters.m_pTexBlueFilename.IsEmpty();
}
virtual bool ComputeRandomValuesThis( CUniformRandomStream* pRNG ) OVERRIDE
{
// If you change the order of these random numbers being generated, or add new ones, you will
// change the look of existing players' weapons! Don't do that.
const bool shouldFlipU = m_Parameters.m_AllowFlipU ? pRNG->RandomInt( 0, 1 ) != 0 : false;
const bool shouldFlipV = m_Parameters.m_AllowFlipV ? pRNG->RandomInt( 0, 1 ) != 0 : false;
const float translateU = pRNG->RandomFloat( m_Parameters.m_TranslateU.low, m_Parameters.m_TranslateU.high );
const float translateV = pRNG->RandomFloat( m_Parameters.m_TranslateV.low, m_Parameters.m_TranslateV.high );
const float rotation = pRNG->RandomFloat( m_Parameters.m_Rotation.low, m_Parameters.m_Rotation.high );
const float scaleUV = pRNG->RandomFloat( m_Parameters.m_ScaleUV.low, m_Parameters.m_ScaleUV.high );
const float adjustBlack = pRNG->RandomFloat( m_Parameters.m_AdjustBlack.low, m_Parameters.m_AdjustBlack.high );
const float adjustOffset = pRNG->RandomFloat( m_Parameters.m_AdjustOffset.low, m_Parameters.m_AdjustOffset.high );
const float adjustGamma = pRNG->RandomFloat( m_Parameters.m_AdjustGamma.low, m_Parameters.m_AdjustGamma.high );
const float adjustWhite = adjustBlack + adjustOffset;
m_fAdjustBlack = adjustBlack;
m_fAdjustWhite = adjustWhite;
m_fAdjustGamma = adjustGamma;
const float finalScaleU = scaleUV * ( shouldFlipU ? -1.0f : 1.0f );
const float finalScaleV = scaleUV * ( shouldFlipV ? -1.0f : 1.0f );
MatrixBuildRotateZ( m_mTextureAdjust, rotation );
m_mTextureAdjust = m_mTextureAdjust.Scale( Vector( finalScaleU, finalScaleV, 1.0f ) );
MatrixTranslate( m_mTextureAdjust, Vector( translateU, translateV, 0 ) );
// Copy W into Z because we're doing a texture matrix.
m_mTextureAdjust[ 0 ][ 2 ] = m_mTextureAdjust[ 0 ][ 3 ];
m_mTextureAdjust[ 1 ][ 2 ] = m_mTextureAdjust[ 1 ][ 3 ];
m_mTextureAdjust[ 2 ][ 2 ] = 1.0f;
return true;
}
private:
TextureStageParameters m_Parameters;
ITexture* m_pTex;
ITexture* m_pTexRed;
ITexture* m_pTexBlue;
// Random values here
float m_fAdjustBlack;
float m_fAdjustWhite;
float m_fAdjustGamma;
VMatrix m_mTextureAdjust;
};
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
// Keep in sync with CombineOperation
const char* cCombineMaterialName[] =
{
"dev/CompositorMultiply",
"dev/CompositorAdd",
"dev/CompositorLerp",
"dev/CompositorSelect",
"\0 ECO_Legacy_Lerp_FirstPass", // Procedural; starting with \0 will skip precaching
"\0 ECO_Legacy_Lerp_SecondPass", // Procedural; starting with \0 will skip precaching
"dev/CompositorBlend",
"\0 ECO_LastPrecacheMaterial", //
"CompositorError",
NULL
};
static_assert( ARRAYSIZE( cCombineMaterialName ) == ECO_COUNT + 1, "cCombineMaterialName and ECombineOperation are out of sync." );
// ------------------------------------------------------------------------------------------------
struct CombineStageParameters
{
ECombineOperation m_CombineOp;
Range m_AdjustBlack;
Range m_AdjustOffset;
Range m_AdjustGamma;
Range m_Rotation;
Range m_TranslateU;
Range m_TranslateV;
Range m_ScaleUV;
bool m_AllowFlipU;
bool m_AllowFlipV;
bool m_Evaluate;
CombineStageParameters()
: m_CombineOp( ECO_Error )
, m_AdjustBlack( 0, 0 )
, m_AdjustOffset( 1, 1 )
, m_AdjustGamma( 1, 1 )
, m_Rotation( 0 , 0 )
, m_TranslateU( 0, 0 )
, m_TranslateV( 0, 0 )
, m_ScaleUV( 1, 1 )
, m_AllowFlipU( false )
, m_AllowFlipV( false )
, m_Evaluate( true )
{ }
};
// ------------------------------------------------------------------------------------------------
void ParseOperationFromKV( KeyValues* _kv, void* _pDest )
{
ECombineOperation* realDest = ( ECombineOperation* ) _pDest;
const char* opStr = _kv->GetString();
if ( V_stricmp( "multiply", opStr ) == 0 )
(*realDest) = ECO_Multiply;
else if ( V_stricmp( "add", opStr ) == 0 )
(*realDest) = ECO_Add;
else if ( V_stricmp( "lerp", opStr) == 0 )
(*realDest) = ECO_Lerp;
else
(*realDest) = ECO_Error;
}
// ------------------------------------------------------------------------------------------------
const ParseTableEntry cCombineStageParametersParseTable[] =
{
{ "adjust_black", ParseRangeThenDivideBy<255>, offsetof( CombineStageParameters, m_AdjustBlack ) },
{ "adjust_offset", ParseRangeThenDivideBy<255>, offsetof( CombineStageParameters, m_AdjustOffset ) },
{ "adjust_gamma", ParseInverseRangeFromKV, offsetof( CombineStageParameters, m_AdjustGamma ) },
{ "rotation", ParseRangeFromKV, offsetof( CombineStageParameters, m_Rotation ) },
{ "translate_u", ParseRangeFromKV, offsetof( CombineStageParameters, m_TranslateU ) },
{ "translate_v", ParseRangeFromKV, offsetof( CombineStageParameters, m_TranslateV ) },
{ "scale_uv", ParseRangeFromKV, offsetof( CombineStageParameters, m_ScaleUV ) },
{ "flip_u", ParseBoolFromKV, offsetof( CombineStageParameters, m_AllowFlipU ) },
{ "flip_v", ParseBoolFromKV, offsetof( CombineStageParameters, m_AllowFlipV ) },
{ "evaluate?", ParseBoolFromKV, offsetof( CombineStageParameters, m_Evaluate ) },
{ 0, 0 }
};
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
class CTCCombineStage : public CTCStage
{
public:
CTCCombineStage( const CombineStageParameters& _csp, uint32 nTexCompositeCreateFlags )
: m_Parameters( _csp )
, m_pMaterial( NULL )
{
Assert( m_Parameters.m_CombineOp >= 0 && m_Parameters.m_CombineOp < ECO_COUNT );
SafeAssign( &m_pMaterial, materials->FindMaterial( cCombineMaterialName[ m_Parameters.m_CombineOp ], TEXTURE_GROUP_RUNTIME_COMPOSITE ) );
}
virtual ~CTCCombineStage()
{
SafeRelease( &m_pMaterial );
}
virtual bool DoesTargetRenderTarget() const { return true; }
protected:
virtual void RequestTextures() { /* No textures here */ }
virtual void ResolveThis( CTextureCompositor* _comp )
{
tmZone( TELEMETRY_LEVEL0, TMZF_NONE, "%s", __FUNCTION__ );
ECompositeResolveStatus resolveStatus = GetResolveStatus();
// If we're done, we're done.
if ( resolveStatus == ECRS_Complete || resolveStatus == ECRS_Error )
return;
if ( resolveStatus == ECRS_Scheduled )
SetResolveStatus( ECRS_PendingTextureLoads );
// Someone is misusing this node if this assert fires.
Assert( GetResolveStatus() == ECRS_PendingTextureLoads );
for ( CTCStage* child = GetFirstChild(); child; child = child->GetNextSibling() )
{
// If any child isn't ready to go, we're not ready to go.
if ( child->GetResolveStatus() != ECRS_Complete )
return;
}
ITexture* pRenderTarget = _comp->AllocateCompositorRenderTarget();
CUtlVector<CTCStageResult_t> results;
uint childCount = 0;
for ( CTCStage* child = GetFirstChild(); child; child = child->GetNextSibling() )
{
results.AddToTail( child->GetResult() );
++childCount;
}
// TODO: If there are more than 8 children, need to split them into multiple groups here. Skip it for now.
Render( pRenderTarget, m_pMaterial, results, _comp, true );
CTCStageResult_t res;
res.m_pRenderTarget = pRenderTarget;
res.m_fAdjustBlackPoint = m_fAdjustBlack;
res.m_fAdjustWhitePoint = m_fAdjustWhite;
res.m_fAdjustGamma = m_fAdjustGamma;
SetResult( res );
// As soon as we have scheduled the read of a child render target, we can release that
// texture back to the pool for use by another stage. Everything is pipelined, so this just
// works.
CleanupChildResults( _comp );
tmMessage( TELEMETRY_LEVEL0, TMMF_ICON_NOTE, "Completed: %s", __FUNCTION__ );
}
virtual bool HasTeamSpecificsThis() const OVERRIDE{ return false; }
virtual bool ComputeRandomValuesThis( CUniformRandomStream* pRNG ) OVERRIDE
{
const float adjustBlack = pRNG->RandomFloat( m_Parameters.m_AdjustBlack.low, m_Parameters.m_AdjustBlack.high );
const float adjustOffset = pRNG->RandomFloat( m_Parameters.m_AdjustOffset.low, m_Parameters.m_AdjustOffset.high );
const float adjustGamma = pRNG->RandomFloat( m_Parameters.m_AdjustGamma.low, m_Parameters.m_AdjustGamma.high );
const float adjustWhite = adjustBlack + adjustOffset;
m_fAdjustBlack = adjustBlack;
m_fAdjustWhite = adjustWhite;
m_fAdjustGamma = adjustGamma;
return true;
}
private:
CombineStageParameters m_Parameters;
IMaterial* m_pMaterial;
float m_fAdjustBlack;
float m_fAdjustWhite;
float m_fAdjustGamma;
};
// ------------------------------------------------------------------------------------------------
struct SelectStageParameters
{
CUtlString m_pTexFilename;
CCopyableUtlVector<int> m_Select;
bool m_Evaluate;
SelectStageParameters()
: m_Evaluate( true )
{
}
};
// ------------------------------------------------------------------------------------------------
const ParseTableEntry cSelectStageParametersParseTable[] =
{
{ "groups", ParseStringFromKV, offsetof( SelectStageParameters, m_pTexFilename ) },
{ "select", ParseVectorFromKV< int, cMaxSelectors >, offsetof( SelectStageParameters, m_Select ) },
{ "evaluate?", ParseBoolFromKV, offsetof( SelectStageParameters, m_Evaluate ) },
{ 0, 0 }
};
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
class CTCSelectStage : public CTCStage
{
public:
CTCSelectStage( const SelectStageParameters& _ssp, uint32 nTexCompositeCreateFlags )
: m_Parameters( _ssp )
, m_pMaterial( NULL )
, m_pTex( NULL )
{
SafeAssign( &m_pMaterial, materials->FindMaterial( cCombineMaterialName[ ECO_Select ], TEXTURE_GROUP_RUNTIME_COMPOSITE ) );
}
virtual ~CTCSelectStage()
{
SafeRelease( &m_pMaterial );
SafeRelease( &m_pTex );
}
virtual void OnAsyncFindComplete( ITexture* pTex, void* pExtraArgs ) { SafeAssign( &m_pTex, pTex ); }
virtual bool DoesTargetRenderTarget() const { return true; }
protected:
virtual void RequestTextures()
{
materials->AsyncFindTexture( m_Parameters.m_pTexFilename.Get(), TEXTURE_GROUP_RUNTIME_COMPOSITE, this, NULL, false, TEXTUREFLAGS_IMMEDIATE_CLEANUP );
}
virtual void ResolveThis( CTextureCompositor* _comp )
{
tmZone( TELEMETRY_LEVEL0, TMZF_NONE, "%s", __FUNCTION__ );
// We shouldn't have any children, we're going to ignore them anyways.
Assert( GetFirstChild() == NULL );
ECompositeResolveStatus resolveStatus = GetResolveStatus();
// If we're done, we're done.
if ( resolveStatus == ECRS_Complete || resolveStatus == ECRS_Error )
return;
if ( resolveStatus == ECRS_Scheduled )
SetResolveStatus( ECRS_PendingTextureLoads );
// Someone is misusing this node if this assert fires.
Assert( GetResolveStatus() == ECRS_PendingTextureLoads );
// When the texture has finished loading, this will be set to the texture we should use.
if ( m_pTex == NULL )
return;
if ( m_pTex->IsError() )
{
_comp->Error( false, "Failed to load texture %s, this is non-recoverable.\n", m_Parameters.m_pTexFilename.Get() );
return;
}
ITexture* pRenderTarget = _comp->AllocateCompositorRenderTarget();
char buffer[128];
for ( int i = 0; i < cMaxSelectors; ++i )
{
bool bFound = false;
V_snprintf( buffer, ARRAYSIZE( buffer ), "$selector%d", i );
IMaterialVar* pVar = m_pMaterial->FindVar( buffer, &bFound );
Assert(bFound);
if ( i < m_Parameters.m_Select.Size() )
pVar->SetIntValue( m_Parameters.m_Select[i] );
else
pVar->SetIntValue( 0 );
}
CTCStageResult_t inRes;
inRes.m_pTexture = m_pTex;
CUtlVector<CTCStageResult_t> fakeResults;
fakeResults.AddToTail( inRes );
Render( pRenderTarget, m_pMaterial, fakeResults, _comp, true );
CTCStageResult_t outRes;
outRes.m_pRenderTarget = pRenderTarget;
SetResult( outRes );
CleanupChildResults( _comp );
tmMessage( TELEMETRY_LEVEL0, TMMF_ICON_NOTE, "Completed: %s", __FUNCTION__ );
}
virtual bool HasTeamSpecificsThis() const OVERRIDE { return false; }
virtual bool ComputeRandomValuesThis( CUniformRandomStream* pRNG ) OVERRIDE
{
// No RNG here.
return false;
}
private:
SelectStageParameters m_Parameters;
IMaterial* m_pMaterial;
ITexture* m_pTex;
};
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
struct Sticker_t
{
float m_fWeight; // Random likelihood this one is to be selected
CUtlString m_baseFilename; // Name of the base file for the sticker (the albedo).
CUtlString m_specFilename; // Name of the specular file for the sticker, or if blank we will assume it is baseFilename + _spec + baseExtension
Sticker_t()
: m_fWeight( 1.0 )
{ }
};
// ------------------------------------------------------------------------------------------------
template<>
void ParseTFromKV< Sticker_t >( KeyValues* _kv, void* _pDest )
{
Sticker_t* realDest = ( Sticker_t* ) _pDest;
Sticker_t tmpDest;
tmpDest.m_fWeight = _kv->GetFloat( "weight", 1.0 );
tmpDest.m_baseFilename = _kv->GetString( "base" );
KeyValues* pSpec = _kv->FindKey( "spec" );
if ( pSpec )
tmpDest.m_specFilename = pSpec->GetString();
else
{
CUtlString specPath = tmpDest.m_baseFilename.StripExtension()
+ "_s"
+ tmpDest.m_baseFilename.GetExtension();
tmpDest.m_specFilename = specPath;
}
*realDest = tmpDest;
}
// ------------------------------------------------------------------------------------------------
template <>
CUtlString AsStringT< Sticker_t >( const Sticker_t& _val )
{
char buffer[ 80 ];
V_sprintf_safe( buffer, "[ weight %.2f; base \"%s\"; spec \"%s\" ]", _val.m_fWeight, _val.m_baseFilename.Get(), _val.m_specFilename.Get() );
return CUtlString( buffer );
}
// ------------------------------------------------------------------------------------------------
template< class T >
struct Settable_t
{
T m_val;
bool m_bSet;
Settable_t()
: m_val( T() )
, m_bSet( false )