-
Notifications
You must be signed in to change notification settings - Fork 250
/
Copy pathmeshdx8.cpp
5980 lines (4946 loc) · 172 KB
/
meshdx8.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:
//
//===========================================================================//
#define DISABLE_PROTECTED_THINGS
#include "locald3dtypes.h"
#include "imeshdx8.h"
#include "shaderapidx8_global.h"
#include "materialsystem/IShader.h"
#include "tier0/vprof.h"
#include "studio.h"
#include "tier1/fmtstr.h"
#include "tier0/platform.h"
#include "tier0/systeminformation.h"
// fixme - stick this in a header file.
#if defined( _DEBUG ) && !defined( _X360 )
// define this if you want to range check all indices when drawing
#define CHECK_INDICES
#endif
#ifdef CHECK_INDICES
#define CHECK_INDICES_MAX_NUM_STREAMS 2
#endif
#include "dynamicib.h"
#include "dynamicvb.h"
#include "utlvector.h"
#include "shaderapi/ishaderapi.h"
#include "imaterialinternal.h"
#include "imaterialsysteminternal.h"
#include "shaderapidx8.h"
#include "shaderapi/ishaderutil.h"
#include "materialsystem/imaterialsystemhardwareconfig.h"
#include "materialsystem/materialsystem_config.h"
#include "materialsystem/ivballoctracker.h"
#include "tier1/strtools.h"
#include "convar.h"
#include "shaderdevicedx8.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
// Uncomment this to test buffered state
//-----------------------------------------------------------------------------
//#define DEBUG_BUFFERED_MESHES 1
#define MAX_DX8_STREAMS 16
#define VERTEX_FORMAT_INVALID 0xFFFFFFFFFFFFFFFFull
// this is hooked into the engines convar
extern ConVar mat_debugalttab;
//#define DRAW_SELECTION 1
static bool g_bDrawSelection = true; // only used in DRAW_SELECTION
static unsigned short g_nScratchIndexBuffer[6]; // large enough for a fast quad; used when device is not active
#ifdef _DEBUG
int CVertexBuffer::s_BufferCount = 0;
int CIndexBuffer::s_BufferCount = 0;
#endif
//-----------------------------------------------------------------------------
// Important enumerations
//-----------------------------------------------------------------------------
enum
{
VERTEX_BUFFER_SIZE = 32768,
MAX_QUAD_INDICES = 16384,
};
//-----------------------------------------------------------------------------
//
// Code related to vertex buffers start here
//
//-----------------------------------------------------------------------------
class CVertexBufferDx8 : public CVertexBufferBase
{
typedef CVertexBufferBase BaseClass;
// Methods of IVertexBuffer
public:
virtual int VertexCount() const;
virtual VertexFormat_t GetVertexFormat() const;
virtual bool IsDynamic() const;
virtual void BeginCastBuffer( VertexFormat_t format );
virtual void EndCastBuffer( );
virtual int GetRoomRemaining() const;
virtual bool Lock( int nVertexCount, bool bAppend, VertexDesc_t &desc );
virtual void Unlock( int nVertexCount, VertexDesc_t &desc );
public:
// constructor
CVertexBufferDx8( ShaderBufferType_t type, VertexFormat_t fmt, int nVertexCount, const char *pBudgetGroupName );
virtual ~CVertexBufferDx8();
// Allocates, deallocates the index buffer
bool Allocate( );
void Free();
// Returns the vertex size
int VertexSize() const;
// Only used by dynamic buffers, indicates the next lock should perform a discard.
void Flush();
// Returns the D3D buffer
IDirect3DVertexBuffer9* GetDx9Buffer();
// Used to measure how much static buffer memory is touched each frame
void HandlePerFrameTextureStats( int nFrame );
protected:
IDirect3DVertexBuffer9 *m_pVertexBuffer;
VertexFormat_t m_VertexFormat;
int m_nVertexCount;
int m_nBufferSize;
int m_nFirstUnwrittenOffset; // Used only for dynamic buffers, indicates where it's safe to write (nooverwrite)
// Is it locked?
bool m_bIsLocked : 1;
bool m_bIsDynamic : 1;
bool m_bFlush : 1; // Used only for dynamic buffers, indicates to discard the next time
#ifdef VPROF_ENABLED
int m_nVProfFrame;
int *m_pFrameCounter;
int *m_pGlobalCounter;
#endif
#ifdef _DEBUG
static int s_nBufferCount;
#endif
};
//-----------------------------------------------------------------------------
//
// Code related to index buffers start here
//
//-----------------------------------------------------------------------------
class CIndexBufferDx8 : public CIndexBufferBase
{
typedef CIndexBufferBase BaseClass;
// Methods of IIndexBuffer
public:
virtual int IndexCount( ) const;
virtual MaterialIndexFormat_t IndexFormat() const;
virtual int GetRoomRemaining() const;
virtual bool Lock( int nIndexCount, bool bAppend, IndexDesc_t &desc );
virtual void Unlock( int nIndexCount, IndexDesc_t &desc );
virtual void BeginCastBuffer( MaterialIndexFormat_t format );
virtual void EndCastBuffer( );
virtual bool IsDynamic() const;
virtual void ModifyBegin( bool bReadOnly, int nFirstIndex, int nIndexCount, IndexDesc_t& desc ) { Assert(0); }
virtual void ModifyEnd( IndexDesc_t& desc ) { Assert(0); }
public:
// constructor
CIndexBufferDx8( ShaderBufferType_t bufferType, MaterialIndexFormat_t fmt, int nIndexCount, const char *pBudgetGroupName );
virtual ~CIndexBufferDx8();
// Allocates, deallocates the index buffer
bool Allocate( );
void Free();
// Returns the index size
int IndexSize() const;
// Only used by dynamic buffers, indicates the next lock should perform a discard.
void Flush();
// Returns the D3D buffer
IDirect3DIndexBuffer9* GetDx9Buffer();
// Used to measure how much static buffer memory is touched each frame
void HandlePerFrameTextureStats( int nFrame );
#ifdef CHECK_INDICES
unsigned short GetShadowIndex( int i ) const;
#endif
private:
IDirect3DIndexBuffer9 *m_pIndexBuffer;
MaterialIndexFormat_t m_IndexFormat;
int m_nIndexCount;
int m_nBufferSize;
int m_nFirstUnwrittenOffset; // Used only for dynamic buffers, indicates where it's safe to write (nooverwrite)
// Is it locked?
bool m_bIsLocked : 1;
bool m_bIsDynamic : 1;
bool m_bFlush : 1; // Used only for dynamic buffers, indicates to discard the next time
#ifdef CHECK_INDICES
unsigned char *m_pShadowIndices;
void *m_pLockIndexBuffer;
int m_nLockIndexBufferSize;
int m_nLockIndexOffset;
#endif
#ifdef VPROF_ENABLED
int m_nVProfFrame;
#endif
#ifdef _DEBUG
static int s_nBufferCount;
#endif
};
//-----------------------------------------------------------------------------
//
// Backward compat mesh code; will go away soon
//
//-----------------------------------------------------------------------------
abstract_class CBaseMeshDX8 : public CMeshBase
{
public:
// constructor, destructor
CBaseMeshDX8();
virtual ~CBaseMeshDX8();
// FIXME: Make this work! Unsupported methods of IIndexBuffer + IVertexBuffer
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 ) { Assert(0); }
virtual void ModifyEnd( IndexDesc_t& desc ) { Assert(0); }
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); }
// Locks mesh for modifying
void ModifyBeginEx( bool bReadOnly, int nFirstVertex, int nVertexCount, int nFirstIndex, int nIndexCount, MeshDesc_t& desc );
void ModifyBegin( int nFirstVertex, int nVertexCount, int nFirstIndex, int nIndexCount, MeshDesc_t& desc );
void ModifyEnd( MeshDesc_t& desc );
// Sets/gets the vertex format
virtual void SetVertexFormat( VertexFormat_t format );
virtual VertexFormat_t GetVertexFormat() const;
// Sets/gets the morph format
virtual void SetMorphFormat( MorphFormat_t format );
virtual MorphFormat_t GetMorphFormat() const;
// Am I using morph data?
virtual bool IsUsingMorphData() const;
bool IsUsingVertexID() const
{
return ShaderAPI()->GetBoundMaterial()->IsUsingVertexID();
}
// Sets the material
virtual void SetMaterial( IMaterial* pMaterial );
// returns the # of vertices (static meshes only)
int VertexCount() const { return 0; }
void SetColorMesh( IMesh *pColorMesh, int nVertexOffsetInBytes )
{
Assert( 0 );
}
void SetFlexMesh( IMesh *pMesh, int nVertexOffsetInBytes )
{
Assert( pMesh == NULL && nVertexOffsetInBytes == 0 );
}
void DisableFlexMesh( )
{
Assert( 0 );
}
void MarkAsDrawn() {}
bool HasColorMesh( ) const { return false; }
bool HasFlexMesh( ) const { return false; }
// Draws the mesh
void DrawMesh( );
// Begins a pass
void BeginPass( );
// Spews the mesh data
virtual void Spew( int nVertexCount, int nIndexCount, const MeshDesc_t & desc );
// Call this in debug mode to make sure our data is good.
virtual void ValidateData( int nVertexCount, int nIndexCount, const MeshDesc_t & desc );
virtual void HandleLateCreation( ) = 0;
void Draw( CPrimList *pLists, int nLists );
// Copy verts and/or indices to a mesh builder. This only works for temp meshes!
virtual void CopyToMeshBuilder(
int iStartVert, // Which vertices to copy.
int nVerts,
int iStartIndex, // Which indices to copy.
int nIndices,
int indexOffset, // This is added to each index.
CMeshBuilder &builder );
// returns the primitive type
virtual MaterialPrimitiveType_t GetPrimitiveType() const = 0;
// Returns the number of indices in a mesh..
virtual int IndexCount( ) const = 0;
// FIXME: Make this work!
virtual MaterialIndexFormat_t IndexFormat() const { return MATERIAL_INDEX_FORMAT_16BIT; }
// NOTE: For dynamic index buffers only!
// Casts the memory of the dynamic index buffer to the appropriate type
virtual void BeginCastBuffer( MaterialIndexFormat_t format ) { Assert(0); }
virtual void BeginCastBuffer( VertexFormat_t format ) { Assert(0); }
virtual void EndCastBuffer( ) { Assert(0); }
virtual int GetRoomRemaining() const { Assert(0); return 0; }
// returns a static vertex buffer...
virtual CVertexBuffer *GetVertexBuffer() { return 0; }
virtual CIndexBuffer *GetIndexBuffer() { return 0; }
// Do I need to reset the vertex format?
virtual bool NeedsVertexFormatReset( VertexFormat_t fmt ) const;
// Do I have enough room?
virtual bool HasEnoughRoom( int nVertexCount, int nIndexCount ) const;
// Operation to do pre-lock
virtual void PreLock() {}
virtual unsigned ComputeMemoryUsed();
bool m_bMeshLocked;
protected:
bool DebugTrace() const;
// The vertex format we're using...
VertexFormat_t m_VertexFormat;
// The morph format we're using
MorphFormat_t m_MorphFormat;
#ifdef DBGFLAG_ASSERT
IMaterialInternal* m_pMaterial;
bool m_IsDrawing;
#endif
};
//-----------------------------------------------------------------------------
// Implementation of the mesh
//-----------------------------------------------------------------------------
class CMeshDX8 : public CBaseMeshDX8
{
public:
// constructor
CMeshDX8( const char *pTextureGroupName );
virtual ~CMeshDX8();
// Locks/unlocks the mesh
void LockMesh( int nVertexCount, int nIndexCount, MeshDesc_t& desc );
void UnlockMesh( int nVertexCount, int nIndexCount, MeshDesc_t& desc );
// Locks mesh for modifying
void ModifyBeginEx( bool bReadOnly, int nFirstVertex, int nVertexCount, int nFirstIndex, int nIndexCount, MeshDesc_t& desc );
void ModifyBegin( int nFirstVertex, int nVertexCount, int nFirstIndex, int nIndexCount, MeshDesc_t& desc );
void ModifyEnd( MeshDesc_t& desc );
// returns the # of vertices (static meshes only)
int VertexCount() const;
// returns the # of indices
virtual int IndexCount( ) const;
// Sets up the vertex and index buffers
void UseIndexBuffer( CIndexBuffer* pBuffer );
void UseVertexBuffer( CVertexBuffer* pBuffer );
// returns a static vertex buffer...
CVertexBuffer *GetVertexBuffer() { return m_pVertexBuffer; }
CIndexBuffer *GetIndexBuffer() { return m_pIndexBuffer; }
void SetColorMesh( IMesh *pColorMesh, int nVertexOffsetInBytes );
void SetFlexMesh( IMesh *pMesh, int nVertexOffsetInBytes );
void DisableFlexMesh();
virtual void HandleLateCreation( );
bool HasColorMesh( ) const;
bool HasFlexMesh( ) const;
// Draws the mesh
void Draw( int nFirstIndex, int nIndexCount );
void Draw( CPrimList *pLists, int nLists );
void DrawInternal( CPrimList *pLists, int nLists );
// Draws a single pass
void RenderPass();
// Sets the primitive type
void SetPrimitiveType( MaterialPrimitiveType_t type );
MaterialPrimitiveType_t GetPrimitiveType() const;
bool IsDynamic() const { return false; }
protected:
// Sets the render state.
bool SetRenderState( int nVertexOffsetInBytes, int nFirstVertexIdx, VertexFormat_t vertexFormat = VERTEX_FORMAT_INVALID );
// Is the vertex format valid?
bool IsValidVertexFormat( VertexFormat_t vertexFormat = VERTEX_FORMAT_INVALID );
// Locks/ unlocks the vertex buffer
bool Lock( int nVertexCount, bool bAppend, VertexDesc_t &desc );
void Unlock( int nVertexCount, VertexDesc_t &desc );
// Locks/unlocks the index buffer
// Pass in nFirstIndex=-1 to lock wherever the index buffer is. Pass in a value
// >= 0 to specify where to lock.
int Lock( bool bReadOnly, int nFirstIndex, int nIndexCount, IndexDesc_t &pIndices );
void Unlock( int nIndexCount, IndexDesc_t &desc );
// computes how many primitives we've got
int NumPrimitives( int nVertexCount, int nIndexCount ) const;
// Debugging output...
void SpewMaterialVerts( );
// Stream source setting methods
void SetVertexIDStreamState( );
void SetColorStreamState( );
void SetVertexStreamState( int nVertOffsetInBytes );
void SetIndexStreamState( int firstVertexIdx );
void CheckIndices( CPrimList *pPrim, int numPrimitives );
// The vertex and index buffers
CVertexBuffer* m_pVertexBuffer;
CIndexBuffer* m_pIndexBuffer;
// The current color mesh (to be bound to stream 1)
// The vertex offset allows use of a global, shared color mesh VB
CMeshDX8 * m_pColorMesh;
int m_nColorMeshVertOffsetInBytes;
CVertexBuffer *m_pFlexVertexBuffer;
bool m_bHasFlexVerts;
int m_nFlexVertOffsetInBytes;
int m_flexVertCount;
// Primitive type
MaterialPrimitiveType_t m_Type;
// Primitive mode
D3DPRIMITIVETYPE m_Mode;
// Number of primitives
unsigned int m_NumIndices;
unsigned short m_NumVertices;
// Is it locked?
bool m_IsVBLocked;
bool m_IsIBLocked;
// Used in rendering sub-parts of the mesh
static CPrimList *s_pPrims;
static int s_nPrims;
static unsigned int s_FirstVertex; // Gets reset during CMeshDX8::DrawInternal
static unsigned int s_NumVertices;
int m_FirstIndex;
#ifdef RECORDING
int m_LockVertexBufferSize;
void *m_LockVertexBuffer;
#endif
#if defined( RECORDING ) || defined( CHECK_INDICES )
void *m_LockIndexBuffer;
int m_LockIndexBufferSize;
#endif
const char *m_pTextureGroupName;
friend class CMeshMgr; // MESHFIXME
};
//-----------------------------------------------------------------------------
// A little extra stuff for the dynamic version
//-----------------------------------------------------------------------------
class CDynamicMeshDX8 : public CMeshDX8
{
public:
// constructor, destructor
CDynamicMeshDX8();
virtual ~CDynamicMeshDX8();
// Initializes the dynamic mesh
void Init( int nBufferId );
// Sets the vertex format
virtual void SetVertexFormat( VertexFormat_t format );
// Resets the state in case of a task switch
void Reset();
// Do I have enough room in the buffer?
bool HasEnoughRoom( int nVertexCount, int nIndexCount ) const;
// returns the # of indices
int IndexCount( ) const;
// Locks the mesh
void LockMesh( int nVertexCount, int nIndexCount, MeshDesc_t& desc );
// Unlocks the mesh
void UnlockMesh( int nVertexCount, int nIndexCount, MeshDesc_t& desc );
// Override vertex + index buffer
void OverrideVertexBuffer( CVertexBuffer *pStaticVertexBuffer );
void OverrideIndexBuffer( CIndexBuffer *pStaticIndexBuffer );
// Do I need to reset the vertex format?
bool NeedsVertexFormatReset(VertexFormat_t fmt) const;
// Draws it
void Draw( int nFirstIndex, int nIndexCount );
void MarkAsDrawn() { m_HasDrawn = true; }
// Simply draws what's been buffered up immediately, without state change
void DrawSinglePassImmediately();
// Operation to do pre-lock
void PreLock();
bool IsDynamic() const { return true; }
private:
// Resets buffering state
void ResetVertexAndIndexCounts();
// Buffer Id
int m_nBufferId;
// total queued vertices
int m_TotalVertices;
int m_TotalIndices;
// the first vertex and index since the last draw
int m_nFirstVertex;
int m_FirstIndex;
// Have we drawn since the last lock?
bool m_HasDrawn;
// Any overrides?
bool m_VertexOverride;
bool m_IndexOverride;
};
//-----------------------------------------------------------------------------
// A mesh that stores temporary vertex data in the correct format (for modification)
//-----------------------------------------------------------------------------
class CTempMeshDX8 : public CBaseMeshDX8
{
public:
// constructor, destructor
CTempMeshDX8( bool isDynamic );
virtual ~CTempMeshDX8();
// Sets the material
virtual void SetVertexFormat( VertexFormat_t format );
// Locks/unlocks the mesh
void LockMesh( int nVertexCount, int nIndexCount, MeshDesc_t &desc );
void UnlockMesh( int nVertexCount, int nIndexCount, MeshDesc_t &desc );
// Locks mesh for modifying
virtual void ModifyBeginEx( bool bReadOnly, int nFirstVertex, int nVertexCount, int nFirstIndex, int nIndexCount, MeshDesc_t& desc );
virtual void ModifyBegin( int nFirstVertex, int nVertexCount, int nFirstIndex, int nIndexCount, MeshDesc_t& desc );
virtual void ModifyEnd( MeshDesc_t& desc );
// Number of indices + vertices
int VertexCount() const;
virtual int IndexCount() const;
virtual bool IsDynamic() const;
// Sets the primitive type
void SetPrimitiveType( MaterialPrimitiveType_t type );
MaterialPrimitiveType_t GetPrimitiveType() const;
// Begins a pass
void BeginPass( );
// Draws a single pass
void RenderPass();
virtual void HandleLateCreation()
{
Assert( !"TBD - CTempMeshDX8::HandleLateCreation()" );
}
// Draws the entire beast
void Draw( int nFirstIndex, int nIndexCount );
virtual void CopyToMeshBuilder(
int iStartVert, // Which vertices to copy.
int nVerts,
int iStartIndex, // Which indices to copy.
int nIndices,
int indexOffset, // This is added to each index.
CMeshBuilder &builder );
private:
// Selection mode
void TestSelection( );
void ClipTriangle( D3DXVECTOR3 **ppVert, float zNear, D3DXMATRIX &proj );
CDynamicMeshDX8 *GetDynamicMesh();
CUtlVector< unsigned char, CUtlMemoryAligned< unsigned char, 32 > > m_VertexData;
CUtlVector< unsigned short > m_IndexData;
unsigned short m_VertexSize;
MaterialPrimitiveType_t m_Type;
int m_LockedVerts;
int m_LockedIndices;
bool m_IsDynamic;
// Used in rendering sub-parts of the mesh
static unsigned int s_NumIndices;
static unsigned int s_FirstIndex;
#ifdef DBGFLAG_ASSERT
bool m_Locked;
bool m_InPass;
#endif
};
#if 0
//-----------------------------------------------------------------------------
// A mesh that stores temporary vertex data in the correct format (for modification)
//-----------------------------------------------------------------------------
class CTempIndexBufferDX8 : public CIndexBufferBase
{
public:
// constructor, destructor
CTempIndexBufferDX8( bool isDynamic );
virtual ~CTempIndexBufferDX8();
// Locks/unlocks the mesh
void LockIndexBuffer( int nIndexCount );
void UnlockMesh( int nIndexCount );
// Locks mesh for modifying
virtual void ModifyBeginEx( bool bReadOnly, int nFirstIndex, int nIndexCount );
virtual void ModifyEnd();
// Number of indices
virtual int IndexCount() const;
virtual bool IsDynamic() const;
virtual void CopyToIndexBuilder(
int iStartIndex, // Which indices to copy.
int nIndices,
int indexOffset, // This is added to each index.
CIndexBuilder &builder );
private:
// Selection mode
void TestSelection( );
CDynamicMeshDX8 *GetDynamicMesh();
CUtlVector< unsigned short > m_IndexData;
MaterialPrimitiveType_t m_Type;
int m_LockedIndices;
bool m_IsDynamic;
// Used in rendering sub-parts of the mesh
static unsigned int s_NumIndices;
static unsigned int s_FirstIndex;
#ifdef DBGFLAG_ASSERT
bool m_Locked;
bool m_InPass;
#endif
};
#endif
//-----------------------------------------------------------------------------
// This is a version that buffers up vertex data so we can blast through it later
//-----------------------------------------------------------------------------
class CBufferedMeshDX8 : public CBaseMeshDX8
{
public:
// constructor, destructor
CBufferedMeshDX8();
virtual ~CBufferedMeshDX8();
// checks to see if it was rendered..
void ResetRendered();
bool WasNotRendered() const;
// Sets the mesh we're really going to draw into
void SetMesh( CBaseMeshDX8* pMesh );
const CBaseMeshDX8* GetMesh() const { return m_pMesh; }
// Spews the mesh data
virtual void Spew( int nVertexCount, int nIndexCount, const MeshDesc_t &spewDesc );
// Sets the vertex format
virtual void SetVertexFormat( VertexFormat_t format );
virtual VertexFormat_t GetVertexFormat() const;
// Sets the morph format
virtual void SetMorphFormat( MorphFormat_t format );
// Sets the material
void SetMaterial( IMaterial *pMaterial );
// returns the number of indices (should never be called!)
virtual int IndexCount() const { Assert(0); return 0; }
virtual MaterialIndexFormat_t IndexFormat() const { Assert(0); return MATERIAL_INDEX_FORMAT_16BIT; }
virtual bool IsDynamic() const { Assert(0); return true; }
virtual void BeginCastBuffer( MaterialIndexFormat_t format ) { Assert(0); }
virtual void EndCastBuffer( ) { Assert(0); }
virtual int GetRoomRemaining() const { Assert(0); return 0; }
// Locks the mesh
void LockMesh( int nVertexCount, int nIndexCount, MeshDesc_t& desc );
void UnlockMesh( int nVertexCount, int nIndexCount, MeshDesc_t& desc );
// Sets the primitive type
void SetPrimitiveType( MaterialPrimitiveType_t type );
MaterialPrimitiveType_t GetPrimitiveType( ) const;
void ValidateData( int nVertexCount, int nIndexCount, const MeshDesc_t & spewDesc );
virtual void HandleLateCreation( )
{
if ( m_pMesh )
{
m_pMesh->HandleLateCreation();
}
}
// Draws it
void Draw( int nFirstIndex, int nIndexCount );
// Renders a pass
void RenderPass();
// Flushes queued data
void Flush( );
void SetFlexMesh( IMesh *pMesh, int nVertexOffsetInBytes );
private:
// The actual mesh we need to render....
CBaseMeshDX8* m_pMesh;
// The index of the last vertex (for tristrip fixup)
unsigned short m_LastIndex;
// Extra padding indices for tristrips
unsigned short m_ExtraIndices;
// Am I currently flushing?
bool m_IsFlushing;
// has the dynamic mesh been rendered?
bool m_WasRendered;
// Do I need to flush?
bool m_FlushNeeded;
#ifdef DEBUG_BUFFERED_MESHES
// for debugging only
bool m_BufferedStateSet;
BufferedState_t m_BufferedState;
#endif
};
//-----------------------------------------------------------------------------
// Implementation of the mesh manager
//-----------------------------------------------------------------------------
class CMeshMgr : public IMeshMgr
{
public:
// constructor, destructor
CMeshMgr();
virtual ~CMeshMgr();
// Initialize, shutdown
void Init();
void Shutdown();
// Task switch...
void ReleaseBuffers();
void RestoreBuffers();
// Releases all dynamic vertex buffers
void DestroyVertexBuffers();
// Flushes the dynamic mesh
void Flush();
// Flushes the vertex buffers
void DiscardVertexBuffers();
// Creates, destroys static meshes
IMesh *CreateStaticMesh( VertexFormat_t vertexFormat, const char *pTextureBudgetGroup, IMaterial *pMaterial = NULL );
void DestroyStaticMesh( IMesh *pMesh );
// Gets at the dynamic mesh (spoofs it though)
IMesh *GetDynamicMesh( IMaterial *pMaterial, VertexFormat_t vertexFormat, int nHWSkinBoneCount, bool buffered,
IMesh *pVertexOverride, IMesh *pIndexOverride );
// -----------------------------------------------------------
// ------------ New Vertex/Index Buffer interface ----------------------------
// Do we need support for bForceTempMesh and bSoftwareVertexShader?
// I don't think we use bSoftwareVertexShader anymore. .need to look into bForceTempMesh.
IVertexBuffer *CreateVertexBuffer( ShaderBufferType_t type, VertexFormat_t fmt, int nVertexCount, const char *pBudgetGroup );
IIndexBuffer *CreateIndexBuffer( ShaderBufferType_t bufferType, MaterialIndexFormat_t fmt, int nIndexCount, const char *pBudgetGroup );
void DestroyVertexBuffer( IVertexBuffer * );
void DestroyIndexBuffer( IIndexBuffer * );
// Do we need to specify the stream here in the case of locking multiple dynamic VBs on different streams?
IVertexBuffer *GetDynamicVertexBuffer( int streamID, VertexFormat_t vertexFormat, bool bBuffered = true );
IIndexBuffer *GetDynamicIndexBuffer( MaterialIndexFormat_t fmt, bool bBuffered = true );
void BindVertexBuffer( int streamID, IVertexBuffer *pVertexBuffer, int nOffsetInBytes, int nFirstVertex, int nVertexCount, VertexFormat_t fmt, int nRepetitions = 1 );
void BindIndexBuffer( IIndexBuffer *pIndexBuffer, int nOffsetInBytes );
void Draw( MaterialPrimitiveType_t primitiveType, int nFirstIndex, int nIndexCount );
// ------------ End ----------------------------
void RenderPassWithVertexAndIndexBuffers( void );
VertexFormat_t GetCurrentVertexFormat( void ) const { return m_CurrentVertexFormat; }
// Gets at the *actual* dynamic mesh
IMesh* GetActualDynamicMesh( VertexFormat_t vertexFormat );
IMesh *GetFlexMesh();
// Computes vertex format from a list of ingredients
VertexFormat_t ComputeVertexFormat( unsigned int flags,
int numTexCoords, int *pTexCoordDimensions, int numBoneWeights,
int userDataSize ) const;
// Use fat vertices (for tools)
virtual void UseFatVertices( bool bUseFat );
// Returns the number of vertices we can render using the dynamic mesh
virtual void GetMaxToRender( IMesh *pMesh, bool bMaxUntilFlush, int *pMaxVerts, int *pMaxIndices );
virtual int GetMaxVerticesToRender( IMaterial *pMaterial );
virtual int GetMaxIndicesToRender( );
// Returns a vertex buffer appropriate for the flags
CVertexBuffer *FindOrCreateVertexBuffer( int nDynamicBufferId, VertexFormat_t fmt );
CIndexBuffer *GetDynamicIndexBuffer();
// Is the mesh dynamic?
bool IsDynamicMesh( IMesh *pMesh ) const;
bool IsBufferedDynamicMesh( IMesh *pMesh ) const;
// Is the vertex buffer dynamic?
bool IsDynamicVertexBuffer( IVertexBuffer *pVertexBuffer ) const;
// Is the index buffer dynamic?
bool IsDynamicIndexBuffer( IIndexBuffer *pIndexBuffer ) const;
// Returns the vertex size
int VertexFormatSize( VertexFormat_t vertexFormat ) const
{
return CVertexBufferBase::VertexFormatSize( vertexFormat );
}
// Computes the vertex buffer pointers
void ComputeVertexDescription( unsigned char *pBuffer,
VertexFormat_t vertexFormat, MeshDesc_t &desc ) const;
// Returns the number of buffers...
int BufferCount() const
{
#ifdef _DEBUG
return CVertexBuffer::BufferCount() + CIndexBuffer::BufferCount();
#else
return 0;
#endif
}
CVertexBuffer *GetVertexIDBuffer();
IVertexBuffer *GetDynamicVertexBuffer( IMaterial *pMaterial, bool buffered = true );
IIndexBuffer *GetDynamicIndexBuffer( IMaterial *pMaterial, bool buffered = true );
virtual void MarkUnusedVertexFields( unsigned int nFlags, int nTexCoordCount, bool *pUnusedTexCoords );
int UnusedVertexFields() const { return m_nUnusedVertexFields; }
int UnusedTextureCoords() const { return m_nUnusedTextureCoords; }
IDirect3DVertexBuffer9 *GetZeroVertexBuffer() const { return m_pZeroVertexBuffer; }
private:
void SetVertexIDStreamState( );
void SetColorStreamState( );
void SetVertexStreamState( int nVertOffsetInBytes, int nVertexStride );
void SetIndexStreamState( int firstVertexIdx );
bool SetRenderState( int nVertexOffsetInBytes, int nFirstVertexIdx, VertexFormat_t vertexFormat, int nVertexStride );
struct VertexBufferLookup_t
{
CVertexBuffer* m_pBuffer;
int m_VertexSize;
};
void CopyStaticMeshIndexBufferToTempMeshIndexBuffer( CTempMeshDX8 *pDstIndexMesh, CMeshDX8 *pSrcIndexMesh );
// Cleans up the class
void CleanUp();
// Creates, destroys the dynamic index
void CreateDynamicIndexBuffer();
void DestroyDynamicIndexBuffer();
// Creates, destroys the vertexID buffer
void CreateVertexIDBuffer();
void DestroyVertexIDBuffer();
void CreateZeroVertexBuffer();
void DestroyZeroVertexBuffer();
// Fills a vertexID buffer
void FillVertexIDBuffer( CVertexBuffer *pVertexIDBuffer, int nCount );
// The dynamic index buffer
CIndexBuffer *m_pDynamicIndexBuffer;
// A static vertexID buffer
CVertexBuffer *m_pVertexIDBuffer;
// The dynamic vertex buffers
CUtlVector< VertexBufferLookup_t > m_DynamicVertexBuffers;
// The buffered mesh
CBufferedMeshDX8 m_BufferedMesh;
// The current dynamic mesh
CDynamicMeshDX8 m_DynamicMesh;
CDynamicMeshDX8 m_DynamicFlexMesh;
// The current dynamic vertex buffer
CVertexBufferDx8 m_DynamicVertexBuffer;
// The current dynamic index buffer
CIndexBufferDx8 m_DynamicIndexBuffer;
// The dynamic mesh temp version (for shaders that modify vertex data)
CTempMeshDX8 m_DynamicTempMesh;
// Am I buffering or not?
bool m_BufferedMode;
// Using fat vertices?
bool m_bUseFatVertices;
CVertexBufferDx8 *m_pCurrentVertexBuffer;
VertexFormat_t m_CurrentVertexFormat;
int m_pVertexBufferOffset[MAX_DX8_STREAMS];
int m_pCurrentVertexStride[MAX_DX8_STREAMS];
int m_pFirstVertex[MAX_DX8_STREAMS];
int m_pVertexCount[MAX_DX8_STREAMS];
CIndexBufferBase *m_pCurrentIndexBuffer;
int m_nIndexBufferOffset;
MaterialPrimitiveType_t m_PrimitiveType;
int m_nFirstIndex;
int m_nNumIndices;
unsigned int m_nUnusedVertexFields;
unsigned int m_nUnusedTextureCoords;
// 4096 byte static VB containing all-zeros
IDirect3DVertexBuffer9 *m_pZeroVertexBuffer;
};
//-----------------------------------------------------------------------------
// Singleton...
//-----------------------------------------------------------------------------
static CMeshMgr g_MeshMgr;
IMeshMgr* MeshMgr()
{
return &g_MeshMgr;
}
//-----------------------------------------------------------------------------
// Tracks stream state and queued data