-
Notifications
You must be signed in to change notification settings - Fork 363
Expand file tree
/
Copy pathcmatqueuedrendercontext.cpp
More file actions
1753 lines (1493 loc) · 51.5 KB
/
cmatqueuedrendercontext.cpp
File metadata and controls
1753 lines (1493 loc) · 51.5 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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================
#include "pch_materialsystem.h"
#include "tier1/functors.h"
#include "itextureinternal.h"
#define MATSYS_INTERNAL
#include "cmatqueuedrendercontext.h"
#include "cmaterialsystem.h" // @HACKHACK
// NOTE: This has to be the last file included!
#include "tier0/memdbgon.h"
ConVar mat_report_queue_status( "mat_report_queue_status", "0", FCVAR_MATERIAL_SYSTEM_THREAD );
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
#if defined( _WIN32 )
void FastCopy( byte *pDest, const byte *pSrc, size_t nBytes )
{
if ( !nBytes )
{
return;
}
#if !defined( _X360 )
if ( (size_t)pDest % 16 == 0 && (size_t)pSrc % 16 == 0 )
{
const int BYTES_PER_FULL = 128;
int nBytesFull = nBytes - ( nBytes % BYTES_PER_FULL );
for ( byte *pLimit = pDest + nBytesFull; pDest < pLimit; pDest += BYTES_PER_FULL, pSrc += BYTES_PER_FULL )
{
#ifdef __i386__
__asm
{
mov esi, pSrc
mov edi, pDest
movaps xmm0, [esi + 0]
movaps xmm1, [esi + 16]
movaps xmm2, [esi + 32]
movaps xmm3, [esi + 48]
movaps xmm4, [esi + 64]
movaps xmm5, [esi + 80]
movaps xmm6, [esi + 96]
movaps xmm7, [esi + 112]
movntps [edi + 0], xmm0
movntps [edi + 16], xmm1
movntps [edi + 32], xmm2
movntps [edi + 48], xmm3
movntps [edi + 64], xmm4
movntps [edi + 80], xmm5
movntps [edi + 96], xmm6
movntps [edi + 112], xmm7
}
#else
memcpy( pDest, pSrc, BYTES_PER_FULL);
#endif
}
nBytes -= nBytesFull;
}
if ( nBytes )
{
memcpy( pDest, pSrc, nBytes );
}
#else
if ( (size_t)pDest % 4 == 0 && nBytes % 4 == 0 )
{
XMemCpyStreaming_WriteCombined( pDest, pSrc, nBytes );
}
else
{
// work around a bug in memcpy
if ((size_t)pDest % 2 == 0 && nBytes == 4)
{
*(reinterpret_cast<short *>(pDest)) = *(reinterpret_cast<const short *>(pSrc));
*(reinterpret_cast<short *>(pDest)+1) = *(reinterpret_cast<const short *>(pSrc)+1);
}
else
{
memcpy( pDest, pSrc, nBytes );
}
}
#endif
}
#else
#define FastCopy memcpy
#endif
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
enum MatQueuedMeshFlags_t
{
MQM_BUFFERED = ( 1 << 0 ),
MQM_FLEX = ( 1 << 1 ),
};
class CMatQueuedMesh : public IMesh
{
public:
CMatQueuedMesh( CMatQueuedRenderContext *pOwner, IMatRenderContextInternal *pHardwareContext )
: m_pLateBoundMesh( &m_pActualMesh ),
m_pOwner( pOwner ),
m_pCallQueue( pOwner->GetCallQueueInternal() ),
m_pHardwareContext( pHardwareContext ),
m_pVertexData( NULL ),
m_pIndexData( NULL ),
m_nVerts( 0 ),
m_nIndices( 0 ),
m_VertexSize( 0 ),
m_Type(MATERIAL_TRIANGLES),
m_pVertexOverride( NULL ),
m_pIndexOverride ( NULL ),
m_pActualMesh( NULL ),
m_nActualVertexOffsetInBytes( 0 ),
m_VertexFormat( 0 ),
m_MorphFormat( 0 )
{
}
CLateBoundPtr<IMesh> &AccessLateBoundMesh()
{
return m_pLateBoundMesh;
}
byte *GetVertexData() { return m_pVertexData; }
uint16 *GetIndexData() { return m_pIndexData; }
IMesh *DetachActualMesh() { IMesh *p = m_pActualMesh; m_pActualMesh = NULL; return p; }
IMesh *GetActualMesh() { return m_pActualMesh; }
int GetActualVertexOffsetInBytes() { return m_nActualVertexOffsetInBytes; }
void DeferredGetDynamicMesh( VertexFormat_t vertexFormat, unsigned flags, IMesh* pVertexOverride, IMesh* pIndexOverride, IMaterialInternal *pMaterial )
{
if ( !( flags & MQM_FLEX ))
{
if ( vertexFormat == 0 )
{
m_pActualMesh = m_pHardwareContext->GetDynamicMesh( ( ( flags & MQM_BUFFERED ) != 0 ), pVertexOverride, pIndexOverride, pMaterial );
}
else
{
m_pActualMesh = m_pHardwareContext->GetDynamicMeshEx( vertexFormat, ( ( flags & MQM_BUFFERED ) != 0 ), pVertexOverride, pIndexOverride, pMaterial );
}
}
else
{
m_pActualMesh = m_pHardwareContext->GetFlexMesh();
}
}
bool OnGetDynamicMesh( VertexFormat_t vertexFormat, unsigned flags, IMesh* pVertexOverride, IMesh* pIndexOverride, IMaterialInternal *pMaterial, int nHWSkinBoneCount )
{
if ( !m_pVertexOverride && ( m_pVertexData || m_pIndexData ) )
{
CannotSupport();
if ( IsDebug() )
{
Assert( !"Getting a dynamic mesh without resolving the previous one" );
}
else
{
Error( "Getting a dynamic mesh without resolving the previous one" );
}
}
FreeBuffers();
m_pVertexOverride = pVertexOverride;
m_pIndexOverride = pIndexOverride;
if ( !( flags & MQM_FLEX ) )
{
if ( pVertexOverride )
{
m_VertexFormat = pVertexOverride->GetVertexFormat();
}
else
{
// Remove VERTEX_FORMAT_COMPRESSED from the material's format (dynamic meshes don't
// support compression, and all materials should support uncompressed verts too)
m_VertexFormat = ( vertexFormat == 0 ) ? ( pMaterial->GetVertexFormat() & ~VERTEX_FORMAT_COMPRESSED ) : vertexFormat;
if ( vertexFormat != 0 )
{
int nVertexFormatBoneWeights = NumBoneWeights( vertexFormat );
if ( nHWSkinBoneCount < nVertexFormatBoneWeights )
{
nHWSkinBoneCount = nVertexFormatBoneWeights;
}
}
// Force the requested number of bone weights
m_VertexFormat &= ~VERTEX_BONE_WEIGHT_MASK;
m_VertexFormat |= VERTEX_BONEWEIGHT( nHWSkinBoneCount );
if ( nHWSkinBoneCount > 0 )
{
m_VertexFormat |= VERTEX_BONE_INDEX;
}
}
}
else
{
m_VertexFormat = VERTEX_POSITION | VERTEX_NORMAL | VERTEX_FORMAT_USE_EXACT_FORMAT;
if ( g_pMaterialSystemHardwareConfig->SupportsPixelShaders_2_b() )
{
m_VertexFormat |= VERTEX_WRINKLE;
}
}
MeshDesc_t temp;
g_pShaderAPI->ComputeVertexDescription( 0, m_VertexFormat, temp );
m_VertexSize = temp.m_ActualVertexSize;
// queue up get of real dynamic mesh, allocate space for verts & indices
m_pCallQueue->QueueCall( this, &CMatQueuedMesh::DeferredGetDynamicMesh, vertexFormat, flags, pVertexOverride, pIndexOverride, pMaterial );
return true;
}
void ModifyBegin( int firstVertex, int numVerts, int firstIndex, int numIndices, MeshDesc_t& desc )
{
CannotSupport();
}
void ModifyBeginEx( bool bReadOnly, int firstVertex, int numVerts, int firstIndex, int numIndices, MeshDesc_t& desc )
{
CannotSupport();
}
void ModifyEnd( MeshDesc_t& desc )
{
CannotSupport();
}
void GenerateSequentialIndexBuffer( unsigned short* pIndexMemory, int numIndices, int firstVertex )
{
Assert( pIndexMemory == m_pIndexData );
m_pCallQueue->QueueCall( &::GenerateSequentialIndexBuffer, pIndexMemory, numIndices, firstVertex );
}
void GenerateQuadIndexBuffer( unsigned short* pIndexMemory, int numIndices, int firstVertex )
{
Assert( pIndexMemory == m_pIndexData );
m_pCallQueue->QueueCall( &::GenerateQuadIndexBuffer, pIndexMemory, numIndices, firstVertex );
}
void GeneratePolygonIndexBuffer( unsigned short* pIndexMemory, int numIndices, int firstVertex )
{
Assert( pIndexMemory == m_pIndexData );
m_pCallQueue->QueueCall( &::GeneratePolygonIndexBuffer, pIndexMemory, numIndices, firstVertex );
}
void GenerateLineStripIndexBuffer( unsigned short* pIndexMemory, int numIndices, int firstVertex )
{
Assert( pIndexMemory == m_pIndexData );
m_pCallQueue->QueueCall( &::GenerateLineStripIndexBuffer, pIndexMemory, numIndices, firstVertex );
}
void GenerateLineLoopIndexBuffer( unsigned short* pIndexMemory, int numIndices, int firstVertex )
{
Assert( pIndexMemory == m_pIndexData );
m_pCallQueue->QueueCall( &::GenerateLineLoopIndexBuffer, pIndexMemory, numIndices, firstVertex );
}
int VertexCount() const
{
return m_VertexSize ? m_nVerts : 0;
}
int IndexCount() const
{
return m_nIndices;
}
int GetVertexSize()
{
return m_VertexSize;
}
void SetPrimitiveType( MaterialPrimitiveType_t type )
{
m_Type = type;
m_pCallQueue->QueueCall( m_pLateBoundMesh, &IMesh::SetPrimitiveType, type );
}
void SetColorMesh( IMesh *pColorMesh, int nVertexOffset )
{
m_pCallQueue->QueueCall( m_pLateBoundMesh, &IMesh::SetColorMesh, pColorMesh, nVertexOffset );
}
void Draw( CPrimList *pLists, int nLists )
{
CannotSupport();
}
void CopyToMeshBuilder( int iStartVert, int nVerts, int iStartIndex, int nIndices, int indexOffset, CMeshBuilder &builder )
{
CannotSupport();
}
void Spew( int numVerts, int numIndices, const MeshDesc_t & desc )
{
}
void ValidateData( int numVerts, int numIndices, const MeshDesc_t & desc )
{
}
void LockMesh( int numVerts, int numIndices, MeshDesc_t& desc )
{
if ( !m_pVertexOverride )
{
m_nVerts = numVerts;
}
else
{
m_nVerts = 0;
}
if ( !m_pIndexOverride )
{
m_nIndices = numIndices;
}
else
{
m_nIndices = 0;
}
if( numVerts > 0 )
{
Assert( m_VertexSize );
Assert( !m_pVertexData );
m_pVertexData = (byte *)m_pOwner->AllocVertices( numVerts, m_VertexSize );
Assert( (uintp)m_pVertexData % 16 == 0 );
// Compute the vertex index..
desc.m_nFirstVertex = 0;
static_cast< VertexDesc_t* >( &desc )->m_nOffset = 0;
// Set up the mesh descriptor
g_pShaderAPI->ComputeVertexDescription( m_pVertexData, m_VertexFormat, desc );
}
else
{
desc.m_nFirstVertex = 0;
static_cast< VertexDesc_t* >( &desc )->m_nOffset = 0;
// Set up the mesh descriptor
g_pShaderAPI->ComputeVertexDescription( 0, 0, desc );
}
if ( m_Type != MATERIAL_POINTS && numIndices > 0 )
{
Assert( !m_pIndexData );
m_pIndexData = m_pOwner->AllocIndices( numIndices );
desc.m_pIndices = m_pIndexData;
desc.m_nIndexSize = 1;
desc.m_nFirstIndex = 0;
static_cast< IndexDesc_t* >( &desc )->m_nOffset = 0;
}
else
{
desc.m_pIndices = &gm_ScratchIndexBuffer[0];
desc.m_nIndexSize = 0;
desc.m_nFirstIndex = 0;
static_cast< IndexDesc_t* >( &desc )->m_nOffset = 0;
}
}
void UnlockMesh( int numVerts, int numIndices, MeshDesc_t& desc )
{
if ( m_pVertexData && numVerts < m_nVerts )
{
m_pVertexData = m_pOwner->ReallocVertices( m_pVertexData, m_nVerts, numVerts, m_VertexSize );
}
m_nVerts = numVerts;
if ( m_pIndexData && numIndices < m_nIndices )
{
m_pIndexData = m_pOwner->ReallocIndices( m_pIndexData, m_nIndices, numIndices );
}
m_nIndices = numIndices;
}
void SetFlexMesh( IMesh *pMesh, int nVertexOffset )
{
m_pCallQueue->QueueCall( m_pLateBoundMesh, &IMesh::SetFlexMesh, pMesh, nVertexOffset );
}
void DisableFlexMesh()
{
m_pCallQueue->QueueCall( m_pLateBoundMesh, &IMesh::DisableFlexMesh );
}
void ExecuteDefferredBuild( byte *pVertexData, int nVerts, int nBytesVerts, uint16 *pIndexData, int nIndices )
{
Assert( m_pActualMesh );
MeshDesc_t desc;
m_pActualMesh->LockMesh( nVerts, nIndices, desc );
m_nActualVertexOffsetInBytes = desc.m_nFirstVertex * desc.m_ActualVertexSize;
if ( pVertexData && desc.m_ActualVertexSize ) // if !desc.m_ActualVertexSize, device lost
{
void *pDest;
if ( desc.m_VertexSize_Position != 0 )
{
pDest = desc.m_pPosition;
}
else
{
#define FindMin( desc, pCurrent, tag ) ( ( desc.m_VertexSize_##tag != 0 ) ? min( pCurrent, (void *)desc.m_p##tag ) : pCurrent )
pDest = (void *)(((byte *)0) - 1);
pDest = FindMin( desc, pDest, BoneWeight );
pDest = FindMin( desc, pDest, BoneMatrixIndex );
pDest = FindMin( desc, pDest, Normal );
pDest = FindMin( desc, pDest, Color );
pDest = FindMin( desc, pDest, Specular );
pDest = FindMin( desc, pDest, TangentS );
pDest = FindMin( desc, pDest, TangentT );
pDest = FindMin( desc, pDest, Wrinkle );
for ( int i = 0; i < VERTEX_MAX_TEXTURE_COORDINATES; i++ )
{
if ( desc.m_VertexSize_TexCoord[i] && desc.m_pTexCoord < pDest )
{
pDest = desc.m_pTexCoord;
}
}
#undef FindMin
}
Assert( pDest );
if ( pDest )
{
FastCopy( (byte *)pDest, pVertexData, nBytesVerts );
}
}
if ( pIndexData && pIndexData != &gm_ScratchIndexBuffer[0] && desc.m_nIndexSize )
{
if ( !desc.m_nFirstVertex )
{
// AssertMsg(desc.m_pIndices & 0x03 == 0,"desc.m_pIndices is misaligned in CMatQueuedMesh::ExecuteDefferedBuild\n");
FastCopy( (byte *)desc.m_pIndices, (byte *)pIndexData, nIndices * sizeof(*pIndexData) );
}
else
{
static ALIGN16 uint16 tempIndices[256];
// original method
int i = 0;
while ( i < nIndices )
{
int nToCopy = min( (int)ARRAYSIZE(tempIndices), nIndices - i );
for ( int j = 0; j < nToCopy; j++ )
{
tempIndices[j] = pIndexData[i+j] + desc.m_nFirstVertex;
}
FastCopy( (byte *)(desc.m_pIndices + i), (byte *)tempIndices, nToCopy * sizeof(uint16) );
i += nToCopy;
}
}
}
m_pActualMesh->UnlockMesh( nVerts, nIndices, desc );
if ( pIndexData && pIndexData != &gm_ScratchIndexBuffer[0])
{
m_pOwner->FreeIndices( pIndexData, nIndices );
}
if ( pVertexData )
{
m_pOwner->FreeVertices( pVertexData, nVerts, desc.m_ActualVertexSize );
}
}
void QueueBuild( bool bDetachBuffers = true )
{
m_pCallQueue->QueueCall( this, &CMatQueuedMesh::ExecuteDefferredBuild, m_pVertexData, m_nVerts, m_nVerts * m_VertexSize, m_pIndexData, m_nIndices );
if ( bDetachBuffers )
{
DetachBuffers();
m_Type = MATERIAL_TRIANGLES;
}
}
void Draw( int firstIndex = -1, int numIndices = 0 )
{
if ( !m_nVerts && !m_nIndices )
{
MarkAsDrawn();
return;
}
void (IMesh::*pfnDraw)( int, int) = &IMesh::Draw; // need assignment to disambiguate overloaded function
bool bDetachBuffers;
if ( firstIndex == -1 || numIndices == 0 )
{
bDetachBuffers = true;
}
else if ( m_pIndexOverride )
{
bDetachBuffers = ( firstIndex + numIndices == m_pIndexOverride->IndexCount() );
}
else if ( !m_nIndices || firstIndex + numIndices == m_nIndices )
{
bDetachBuffers = true;
}
else
{
bDetachBuffers = false;
}
QueueBuild( bDetachBuffers );
m_pCallQueue->QueueCall( m_pLateBoundMesh, pfnDraw, firstIndex, numIndices );
}
void MarkAsDrawn()
{
FreeBuffers();
m_pCallQueue->QueueCall( m_pLateBoundMesh, &IMesh::MarkAsDrawn );
}
void FreeBuffers()
{
if ( m_pIndexData && m_pIndexData != &gm_ScratchIndexBuffer[0])
{
m_pOwner->FreeIndices( m_pIndexData, m_nIndices );
m_pIndexData = NULL;
}
if ( m_pVertexData )
{
m_pOwner->FreeVertices( m_pVertexData, m_nVerts, m_VertexSize );
m_pVertexData = NULL;
}
}
void DetachBuffers()
{
m_pVertexData = NULL;
m_pIndexData = NULL;
}
unsigned ComputeMemoryUsed()
{
return 0;
}
virtual VertexFormat_t GetVertexFormat() const
{
return m_VertexFormat;
}
virtual IMesh *GetMesh()
{
return this;
}
// FIXME: Implement!
virtual bool Lock( int nMaxIndexCount, bool bAppend, IndexDesc_t& desc )
{
Assert( 0 );
return false;
}
virtual void Unlock( int nWrittenIndexCount, IndexDesc_t& desc )
{
Assert( 0 );
}
virtual void ModifyBegin( bool bReadOnly, int nFirstIndex, int nIndexCount, IndexDesc_t& desc )
{
CannotSupport();
}
void ModifyEnd( IndexDesc_t& desc )
{
CannotSupport();
}
virtual void Spew( int nIndexCount, const IndexDesc_t & desc )
{
Assert( 0 );
}
virtual void ValidateData( int nIndexCount, const IndexDesc_t &desc )
{
Assert( 0 );
}
virtual bool Lock( int nVertexCount, bool bAppend, VertexDesc_t &desc )
{
Assert( 0 );
return false;
}
virtual void Unlock( int nVertexCount, VertexDesc_t &desc )
{
Assert( 0 );
}
virtual void Spew( int nVertexCount, const VertexDesc_t &desc )
{
Assert( 0 );
}
virtual void ValidateData( int nVertexCount, const VertexDesc_t & desc )
{
Assert( 0 );
}
virtual bool IsDynamic() const
{
Assert( 0 );
return false;
}
virtual MaterialIndexFormat_t IndexFormat() const
{
Assert( 0 );
return MATERIAL_INDEX_FORMAT_UNKNOWN;
}
virtual void BeginCastBuffer( VertexFormat_t format )
{
Assert( 0 );
}
virtual void BeginCastBuffer( MaterialIndexFormat_t format )
{
Assert( 0 );
}
virtual void EndCastBuffer( )
{
Assert( 0 );
}
// Returns the number of vertices that can still be written into the buffer
virtual int GetRoomRemaining() const
{
Assert( 0 );
return 0;
}
//----------------------------------------------------------------------------
static void DoDraw( int firstIndex = -1, int numIndices = 0 )
{
}
private:
IMesh *m_pActualMesh;
int m_nActualVertexOffsetInBytes;
CLateBoundPtr<IMesh> m_pLateBoundMesh;
CMatQueuedRenderContext *m_pOwner;
CMatCallQueue *m_pCallQueue;
IMatRenderContextInternal *m_pHardwareContext;
//-----------------------------------------------------
// The vertex format we're using...
VertexFormat_t m_VertexFormat;
// The morph format we're using
MorphFormat_t m_MorphFormat;
byte *m_pVertexData;
uint16 *m_pIndexData;
int m_nVerts;
int m_nIndices;
unsigned short m_VertexSize;
MaterialPrimitiveType_t m_Type;
// Used in rendering sub-parts of the mesh
//static unsigned int s_NumIndices;
//static unsigned int s_FirstIndex;
IMesh *m_pVertexOverride;
IMesh *m_pIndexOverride;
static unsigned short gm_ScratchIndexBuffer[6];
};
unsigned short CMatQueuedMesh::gm_ScratchIndexBuffer[6];
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
bool CMatQueuedRenderContext::Init( CMaterialSystem *pMaterialSystem, CMatRenderContextBase *pHardwareContext )
{
BaseClass::Init();
m_pMaterialSystem = pMaterialSystem;
m_pHardwareContext = pHardwareContext;
m_pQueuedMesh = new CMatQueuedMesh( this, pHardwareContext );
MEM_ALLOC_CREDIT();
int nSize = 16 * 1024 * 1024;
int nCommitSize = 128 * 1024;
#if defined(DEDICATED)
Assert( !"CMatQueuedRenderContext shouldn't be initialized on dedicated servers..." );
nSize = nCommitSize = 1024;
#endif
bool bVerticesInit = m_Vertices.Init( nSize, nCommitSize );
bool bIndicesInit = m_Indices.Init( nSize, nCommitSize );
if ( !bVerticesInit || !bIndicesInit )
{
return false;
}
return true;
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void CMatQueuedRenderContext::Shutdown()
{
if ( !m_pHardwareContext )
{
return;
}
Assert( !m_pCurrentMaterial );
delete m_pQueuedMesh;
m_pMaterialSystem = NULL;
m_pHardwareContext = NULL;
m_pQueuedMesh = NULL;
m_Vertices.Term();
m_Indices.Term();
BaseClass::Shutdown();
Assert(m_queue.Count() == 0);
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void CMatQueuedRenderContext::Free()
{
m_Vertices.FreeAll();
m_Indices.FreeAll();
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void CMatQueuedRenderContext::CompactMemory()
{
BaseClass::CompactMemory();
m_Vertices.FreeAll();
m_Indices.FreeAll();
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void CMatQueuedRenderContext::BeginQueue( CMatRenderContextBase *pInitialState )
{
if ( !pInitialState )
{
pInitialState = m_pHardwareContext;
}
CMatRenderContextBase::InitializeFrom( pInitialState );
g_pShaderAPI->GetBackBufferDimensions( m_WidthBackBuffer, m_HeightBackBuffer );
m_FogMode = pInitialState->GetFogMode();
m_nBoneCount = pInitialState->GetCurrentNumBones();
pInitialState->GetFogDistances( &m_flFogStart, &m_flFogEnd, &m_flFogZ );
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void CMatQueuedRenderContext::EndQueue( bool bCallQueued )
{
if ( bCallQueued )
{
CallQueued();
}
int i;
if ( m_pCurrentMaterial )
{
m_pCurrentMaterial = NULL;
}
if ( m_pUserDefinedLightmap )
{
m_pUserDefinedLightmap = NULL;
}
if ( m_pLocalCubemapTexture )
{
m_pLocalCubemapTexture = NULL;
}
for ( i = 0; i < MAX_FB_TEXTURES; i++ )
{
if ( m_pCurrentFrameBufferCopyTexture[i] )
{
m_pCurrentFrameBufferCopyTexture[i] = NULL;
}
}
for ( i = 0; i < m_RenderTargetStack.Count(); i++ )
{
for ( int j = 0; j < MAX_RENDER_TARGETS; j++ )
{
if ( m_RenderTargetStack[i].m_pRenderTargets[j] )
{
m_RenderTargetStack[i].m_pRenderTargets[j] = NULL;
}
}
}
m_RenderTargetStack.Clear();
}
void CMatQueuedRenderContext::Bind( IMaterial *iMaterial, void *proxyData )
{
if ( !iMaterial )
{
if( !g_pErrorMaterial )
return;
}
else
{
iMaterial = ((IMaterialInternal *)iMaterial)->GetRealTimeVersion(); //always work with the real time versions of materials internally
}
CMatRenderContextBase::Bind( iMaterial, proxyData );
// We've always gotta call the bind proxy (assuming there is one)
// so we can copy off the material vars at this point.
IMaterialInternal* pIMaterial = GetCurrentMaterialInternal();
pIMaterial->CallBindProxy( proxyData );
m_queue.QueueCall( m_pHardwareContext, &IMatRenderContext::Bind, iMaterial, proxyData );
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void CMatQueuedRenderContext::BeginRender()
{
if ( ++m_iRenderDepth == 1 )
{
VPROF_INCREMENT_GROUP_COUNTER( "render/CMatQBeginRender", COUNTER_GROUP_TELEMETRY, 1 );
m_queue.QueueCall( m_pHardwareContext, &IMatRenderContext::BeginRender );
}
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void CMatQueuedRenderContext::EndRender()
{
if ( --m_iRenderDepth == 0 )
{
m_queue.QueueCall( m_pHardwareContext, &IMatRenderContext::EndRender );
}
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void CMatQueuedRenderContext::CallQueued( bool bTermAfterCall )
{
if ( mat_report_queue_status.GetBool() )
{
Msg( "%d calls queued for %llu bytes in parameters and overhead, %d bytes verts, %d bytes indices, %d bytes other\n", m_queue.Count(), (uint64)(m_queue.GetMemoryUsed()), m_Vertices.GetUsed(), m_Indices.GetUsed(), RenderDataSizeUsed() );
}
m_queue.CallQueued();
m_Vertices.FreeAll( false );
m_Indices.FreeAll( false );
if ( bTermAfterCall )
{
Shutdown();
}
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void CMatQueuedRenderContext::FlushQueued()
{
m_queue.Flush();
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
ICallQueue *CMatQueuedRenderContext::GetCallQueue()
{
return &m_CallQueueExternal;
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void CMatQueuedRenderContext::SetRenderTargetEx( int nRenderTargetID, ITexture *pNewTarget )
{
CMatRenderContextBase::SetRenderTargetEx( nRenderTargetID, pNewTarget );
m_queue.QueueCall( m_pHardwareContext, &IMatRenderContext::SetRenderTargetEx, nRenderTargetID, pNewTarget );
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void CMatQueuedRenderContext::GetRenderTargetDimensions( int &width, int &height) const
{
// Target at top of stack
ITexture *pTOS = NULL;
if ( m_RenderTargetStack.Count() )
{
pTOS = m_RenderTargetStack.Top().m_pRenderTargets[ 0 ];
}
// If top of stack isn't the back buffer, get dimensions from the texture
if ( pTOS != NULL )
{
width = pTOS->GetActualWidth();
height = pTOS->GetActualHeight();
}
else // otherwise, get them from the shader API
{
width = m_WidthBackBuffer;
height = m_HeightBackBuffer;
}
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void CMatQueuedRenderContext::Viewport( int x, int y, int width, int height )
{
CMatRenderContextBase::Viewport( x, y, width, height );
m_queue.QueueCall( m_pHardwareContext, &IMatRenderContext::Viewport, x, y, width, height );
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void CMatQueuedRenderContext::SetLight( int i, const LightDesc_t &desc )
{
m_queue.QueueCall( m_pHardwareContext, &IMatRenderContext::SetLight, i, RefToVal( desc ) );
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void CMatQueuedRenderContext::SetLightingOrigin( Vector vLightingOrigin )
{
m_queue.QueueCall( m_pHardwareContext, &IMatRenderContext::SetLightingOrigin, vLightingOrigin );
}
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void CMatQueuedRenderContext::SetAmbientLightCube( LightCube_t cube )
{
// FIXME: does compiler do the right thing, is envelope needed?
m_queue.QueueCall( m_pHardwareContext, &IMatRenderContext::SetAmbientLightCube, CUtlEnvelope<Vector4D>( &cube[0], 6 ) );
}
//-----------------------------------------------------------------------------
// Bone count
//-----------------------------------------------------------------------------
void CMatQueuedRenderContext::SetNumBoneWeights( int nBoneCount )
{
m_nBoneCount = nBoneCount;
m_queue.QueueCall( m_pHardwareContext, &IMatRenderContext::SetNumBoneWeights, nBoneCount );
}