/
meshdx8.cpp
6931 lines (5773 loc) · 209 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 (c) Valve Corporation, All rights reserved. ======//
//
// Purpose:
//
//==================================================================//
#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"
#include "smartptr.h"
// fixme - stick this in a header file.
#if defined( _DEBUG ) && !defined( _GAMECONSOLE )
// 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"
#ifdef _GAMECONSOLE
#define MAX_TEMP_BUFFER 3
static int s_nMemoryFrame;
static CMemoryStack s_BufferMemory[MAX_TEMP_BUFFER];
void *AllocateTempBuffer( size_t nSizeInBytes )
{
return s_BufferMemory[s_nMemoryFrame].Alloc( nSizeInBytes, true );
}
#endif // _GAMECONSOLE
//-----------------------------------------------------------------------------
void FailedLock( const char *pszMsg )
{
if ( IsPC() )
{
Error( "%s", pszMsg );
}
else
{
Warning( "%s", pszMsg );
}
g_pMemAlloc->OutOfMemory();
}
#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
// NOTE: Using 6 here since we don't want extra checks in CIndexBuilder::FastQuad
static unsigned int g_pScratchIndexBuffer[6]; // shove indices into this if you don't actually want indices
// used to hold instance data when drawing multiple instances
static const MeshInstanceData_t *g_pInstanceData = NULL;
static CompiledLightingState_t *g_pInstanceCompiledState = NULL;
static InstanceInfo_t *g_pInstanceInfo = NULL;
static int g_nInstanceCount = 0;
#ifdef _DEBUG
int CVertexBuffer::s_BufferCount = 0;
int CIndexBuffer::s_BufferCount = 0;
#endif
//-----------------------------------------------------------------------------
// Important enumerations
//-----------------------------------------------------------------------------
enum
{
VERTEX_BUFFER_SIZE = 32768,
MAX_QUAD_INDICES = 16384,
MAX_TESS_DIVISIONS_PER_SIDE = 16, // We can put any arbitrary number here, but we should tie it to the max that HW tessellators can do
};
//-----------------------------------------------------------------------------
//
// 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 );
void SetIndexStreamState( int nFirstVertexIdx );
virtual bool IsExternal() const { return false; }
#ifdef CHECK_INDICES
unsigned short GetShadowIndex( int i ) const;
#endif
IDirect3DIndexBuffer9 *m_pIndexBuffer;
private:
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
friend class CExternalIndexBufferDx8;
};
#ifdef _GAMECONSOLE
#include "tier0/memdbgoff.h"
//-----------------------------------------------------------------------------
// For externally allocated index buffers
//-----------------------------------------------------------------------------
class CExternalIndexBufferDx8 : public CIndexBufferDx8
{
typedef CIndexBufferDx8 BaseClass;
public:
// constructor
CExternalIndexBufferDx8( ) : BaseClass( SHADER_BUFFER_TYPE_STATIC, MATERIAL_INDEX_FORMAT_16BIT, 0, "external ib - ignore" )
{
}
virtual ~CExternalIndexBufferDx8()
{
if( IsPS3() )
{
m_pIndexBuffer = NULL; // we don't have to release the external dynamic IB
}
}
void Init( int nIndexCount, uint16 *pIndexData )
{
m_pIndexBuffer = CreateExternalDynamicIB( pIndexData, nIndexCount );
m_nBufferSize = m_nFirstUnwrittenOffset = nIndexCount * sizeof(uint16);
m_nIndexCount = nIndexCount;
}
virtual bool IsExternal() const { return true; }
};
#include "tier0/memdbgon.h"
#endif // _GAMECONSOLE
//-----------------------------------------------------------------------------
//
// 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, bool bHasVertexOverride, bool bHasIndexOverride );
virtual VertexFormat_t GetVertexFormat() const;
// Sets/gets the morph format
// Am I using morph data?
bool IsUsingVertexID() const
{
return ShaderAPI()->GetBoundMaterial()->IsUsingVertexID();
}
virtual bool IsExternal() const { return false; }
// 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 );
}
virtual void GetColorMesh( const IVertexBuffer** pMesh, int *pMeshVertexOffsetInBytes ) const
{
*pMesh = 0; *pMeshVertexOffsetInBytes = 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; }
VertexStreamSpec_t *GetVertexStreamSpec() const { return NULL; }
// Draws the mesh
void DrawMesh( const Vector4D *pVecDiffuseModulation );
// 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 int ComputeMemoryUsed();
bool m_bMeshLocked;
protected:
bool DebugTrace() const;
// The vertex format we're using...
VertexFormat_t m_VertexFormat;
#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, MeshBuffersAllocationSettings_t *pSettings = 0 );
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 );
virtual void GetColorMesh( const IVertexBuffer** pMesh, int *pMeshVertexOffsetInBytes ) const;
void SetFlexMesh( IMesh *pMesh, int nVertexOffsetInBytes );
void DisableFlexMesh();
virtual void HandleLateCreation( );
bool HasColorMesh( ) const;
bool HasFlexMesh( ) const;
VertexStreamSpec_t *GetVertexStreamSpec() const;
void SetVertexStreamSpec( VertexStreamSpec_t *pStreamSpec );
virtual void * AccessRawHardwareDataStream( uint8 nRawStreamIndex, uint32 numBytes, uint32 uiFlags, void *pvContext );
// Draws the mesh
void Draw( int nFirstIndex, int nIndexCount );
virtual void DrawModulated( const Vector4D &diffuseModulation, int nFirstIndex, int nIndexCount );
void Draw( CPrimList *pLists, int nLists );
void DrawInternal( const Vector4D *pDiffuseModulation, CPrimList *pLists, int nLists );
void DrawPrims( const unsigned char *pInstanceCommandBuffer );
// Draws a single pass
void RenderPass( const unsigned char *pInstanceCommandBuffer );
void RenderPassForInstances( const unsigned char *pInstanceCommandBuffer );
// Sets the primitive type
void SetPrimitiveType( MaterialPrimitiveType_t type );
MaterialPrimitiveType_t GetPrimitiveType() const;
// Is it using tessellation
#if ENABLE_TESSELLATION
TessellationMode_t GetTessellationType() const;
#else
TessellationMode_t GetTessellationType() const { return TESSELLATION_MODE_DISABLED; }
#endif
// Is it using vertexID for morphs or subdivision surfaces?
bool IsUsingVertexID() const;
bool IsDynamic() const { return false; }
protected:
// Sets the render state.
bool SetRenderState( int nVertexOffsetInBytes, int nFirstVertexIdx, int nIDOffsetBytes = 0, 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, MeshBuffersAllocationSettings_t *pSettings = 0 );
void Unlock( int nIndexCount, IndexDesc_t &desc );
// computes how many primitives we've got
int NumPrimitives( int nVertexCount, int nIndexCount ) const;
int NumPrimitives( MaterialPrimitiveType_t type, int nIndexCount ) const;
// Debugging output...
void SpewMaterialVerts( );
// Stream source setting methods
void SetVertexIDStreamState( int nIDOffsetBytes );
void SetTessellationStreamState( int nVertOffsetInBytes, int iSubdivLevel );
void SetColorStreamState_Internal( CVertexBuffer *pColorVB, int nVertOffset );
inline void SetColorStreamState( );
void SetCustomStreamsState( );
void SetVertexStreamState( int nVertOffsetInBytes, bool bIsRenderingInstances );
void SetIndexStreamState( int firstVertexIdx );
void CheckIndices( CPrimList *pPrim, int numPrimitives );
void CheckIndices( int nFirstIndex, 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;
VertexFormat_t m_fmtStreamSpec;
CArrayAutoPtr< VertexStreamSpec_t > m_pVertexStreamSpec;
CVertexBuffer *m_pVbTexCoord1;
LPDIRECT3DVERTEXBUFFER m_arrRawHardwareDataStreams[1];
CVertexBuffer *m_pFlexVertexBuffer;
bool m_bHasRawHardwareDataStreams;
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
friend void CheckIndices( D3DPRIMITIVETYPE nMode, int nFirstVertex, int nVertexCount, int nBaseIndex, int nFirstIndex, int numPrimitives );
};
//-----------------------------------------------------------------------------
// 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, bool bHasVertexOverride, bool bHasIndexOverride );
// 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, MeshBuffersAllocationSettings_t *pSettings = 0 );
// 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 );
virtual void DrawModulated( const Vector4D &diffuseModulation, 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();
void DrawInternal( const Vector4D *pVecDiffuseModulation, int nFirstIndex, int nIndexCount );
// 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;
};
#ifdef _GAMECONSOLE
//-----------------------------------------------------------------------------
// For use as a mesh that we've already written into write-combined memory
//-----------------------------------------------------------------------------
class CExternalMeshDX8 : public CMeshDX8
{
typedef CMeshDX8 BaseClass;
public:
// constructor, destructor
CExternalMeshDX8() : BaseClass( "external vb - ignore" )
{
m_pVertexBufferExternal = new CVertexBuffer;
m_pIndexBufferExternal = new CIndexBuffer;
}
virtual ~CExternalMeshDX8()
{
CleanUp();
}
void CleanUp()
{
if ( m_pVertexBufferExternal )
{
if( m_pVertexBufferExternal == m_pVertexBuffer )
{
m_pVertexBuffer = NULL; // let's avoid double-delete
}
delete m_pVertexBufferExternal;
m_pVertexBufferExternal = NULL;
}
if ( m_pIndexBufferExternal )
{
if( m_pIndexBufferExternal == m_pIndexBuffer )
{
m_pIndexBuffer = NULL; // let's avoid double-delete
}
delete m_pIndexBufferExternal;
m_pIndexBufferExternal = NULL;
}
}
// Initializes the mesh
void Init( const ExternalMeshInfo_t& info )
{
m_NumVertices = 0;
m_NumIndices = 0;
// SetMaterial is only for debugging;
// it actually shows up a tiny bit on the profile might as well ifdef it
#ifdef _DEBUG
SetMaterial( info.m_pMaterial );
#endif
if ( info.m_pVertexOverride )
{
CBaseMeshDX8 *pDX8Mesh = static_cast<CBaseMeshDX8*>( info.m_pVertexOverride );
SetVertexFormat( pDX8Mesh->GetVertexFormat(), true, ( info.m_pIndexOverride != NULL ) );
m_pVertexBuffer = pDX8Mesh->GetVertexBuffer();
}
else
{
SetVertexFormat( info.m_VertexFormat, false, ( info.m_pIndexOverride != NULL ) );
m_pVertexBuffer = m_pVertexBufferExternal;
}
if ( info.m_pIndexOverride )
{
CBaseMeshDX8 *pDX8Mesh = static_cast<CBaseMeshDX8*>( info.m_pIndexOverride );
m_pIndexBuffer = pDX8Mesh->GetIndexBuffer();
}
else
{
m_pIndexBuffer = m_pIndexBufferExternal;
}
}
void SetExternalData( const ExternalMeshData_t &data )
{
if ( m_pVertexBuffer == m_pVertexBufferExternal )
{
if ( data.m_nVertexCount > 0 )
{
m_pVertexBuffer->Init( Dx9Device(), GetVertexFormat(),
0, data.m_pVertexData, data.m_nVertexSizeInBytes, data.m_nVertexCount );
m_NumVertices = data.m_nVertexCount;
}
else
{
m_pVertexBuffer = NULL;
m_NumVertices = 0;
}
}
if ( m_pIndexBuffer == m_pIndexBufferExternal )
{
if ( data.m_nIndexCount > 0 )
{
m_pIndexBuffer->Init( Dx9Device(), data.m_pIndexData, data.m_nIndexCount );
m_NumIndices = data.m_nIndexCount;
}
else
{
m_pIndexBuffer = NULL;
m_NumIndices = 0;
}
}
}
virtual bool IsExternal() const { return true; }
private:
CVertexBuffer *m_pVertexBufferExternal;
CIndexBuffer *m_pIndexBufferExternal;
};
#endif // _GAMECONSOLE
//-----------------------------------------------------------------------------
// 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, bool bHasVertexOverride, bool bHasIndexOverride );
// Locks/unlocks the mesh
void LockMesh( int nVertexCount, int nIndexCount, MeshDesc_t &desc, MeshBuffersAllocationSettings_t *pSettings = 0 );
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;
// Using tessellation for subd surfaces?
TessellationMode_t GetTessellationType() const { return TESSELLATION_MODE_DISABLED; }
// Begins a pass
void BeginPass( );
virtual void DrawPrims( const unsigned char *pInstanceCommandBuffer ) {}
// Draws a single pass
void RenderPass( const unsigned char *pInstanceCommandBuffer );
virtual void HandleLateCreation()
{
Assert( !"TBD - CTempMeshDX8::HandleLateCreation()" );
}
// Draws the entire beast
void Draw( int nFirstIndex, int nIndexCount );
virtual void DrawModulated( const Vector4D &diffuseModulation, 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 );
void DrawInternal( const Vector4D *pVecDiffuseModulation, int nFirstIndex, int nIndexCount );
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
//-----------------------------------------------------------------------------
// 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 vertex buffers
void DiscardVertexBuffers();
// Creates, destroys static meshes
IMesh *CreateStaticMesh( VertexFormat_t vertexFormat, const char *pTextureBudgetGroup, IMaterial *pMaterial = NULL, VertexStreamSpec_t *pStreamSpec = 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( );
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( const unsigned char *pInstanceCommandBuffer );
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;