forked from hitmen047/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimesh.h
4049 lines (3312 loc) · 126 KB
/
imesh.h
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:
//
//===========================================================================//
#ifndef IMESH_H
#define IMESH_H
#ifdef _WIN32
#pragma once
#endif
#include "tier1/interface.h"
#include "materialsystem/imaterial.h"
#include <float.h>
#include <string.h>
#include "tier0/dbg.h"
#include "tier2/meshutils.h"
#include "mathlib/mathlib.h"
#if defined( DX_TO_GL_ABSTRACTION )
// Swap these so that we do color swapping on 10.6.2, which doesn't have EXT_vertex_array_bgra
#define OPENGL_SWAP_COLORS
#endif
//-----------------------------------------------------------------------------
// forward declarations
//-----------------------------------------------------------------------------
class IMaterial;
class CMeshBuilder;
class IMaterialVar;
typedef uint64 VertexFormat_t;
//-----------------------------------------------------------------------------
// Define this to find write-combine problems
//-----------------------------------------------------------------------------
#ifdef _DEBUG
//#ifndef DEBUG_WRITE_COMBINE
//#define DEBUG_WRITE_COMBINE 1
//#endif
#endif
//-----------------------------------------------------------------------------
// The Vertex Buffer interface
//-----------------------------------------------------------------------------
enum
{
VERTEX_MAX_TEXTURE_COORDINATES = 8,
BONE_MATRIX_INDEX_INVALID = 255
};
// Internal maximums for sizes. Don't use directly, use IMaterialSystem::GetMaxToRender()
enum
{
INDEX_BUFFER_SIZE = 32768,
DYNAMIC_VERTEX_BUFFER_MEMORY = ( 1024 + 512 ) * 1024,
DYNAMIC_VERTEX_BUFFER_MEMORY_SMALL = 384 * 1024, // Only allocate this much during map transitions
};
// Vertex fields must be written in well-defined order to achieve write combining,
// which is a perf booster
enum WriteCombineOrdering_t
{
MB_FIELD_NONE = -1,
MB_FIELD_POSITION = 0,
MB_FIELD_BONE_WEIGHTS,
MB_FIELD_BONE_INDEX,
MB_FIELD_NORMAL,
MB_FIELD_COLOR,
MB_FIELD_SPECULAR,
MB_FIELD_TEXCOORD_FIRST,
MB_FIELD_TEXCOORD_LAST = MB_FIELD_TEXCOORD_FIRST + VERTEX_MAX_TEXTURE_COORDINATES - 1,
MB_FIELD_TANGENT_S,
MB_FIELD_TANGENT_T,
MB_FIELD_USERDATA,
};
#define MB_FIELD_TEXCOORD( nStage ) ( MB_FIELD_TEXCOORD_FIRST + ( nStage ) )
struct VertexDesc_t
{
// These can be set to zero if there are pointers to dummy buffers, when the
// actual buffer format doesn't contain the data but it needs to be safe to
// use all the CMeshBuilder functions.
int m_VertexSize_Position;
int m_VertexSize_BoneWeight;
int m_VertexSize_BoneMatrixIndex;
int m_VertexSize_Normal;
int m_VertexSize_Color;
int m_VertexSize_Specular;
int m_VertexSize_TexCoord[VERTEX_MAX_TEXTURE_COORDINATES];
int m_VertexSize_TangentS;
int m_VertexSize_TangentT;
int m_VertexSize_Wrinkle;
int m_VertexSize_UserData;
int m_ActualVertexSize; // Size of the vertices.. Some of the m_VertexSize_ elements above
// are set to this value and some are set to zero depending on which
// fields exist in a buffer's vertex format.
// The type of compression applied to this vertex data
VertexCompressionType_t m_CompressionType;
// Number of bone weights per vertex...
int m_NumBoneWeights;
// Pointers to our current vertex data
float *m_pPosition;
float *m_pBoneWeight;
#ifndef NEW_SKINNING
unsigned char *m_pBoneMatrixIndex;
#else
float *m_pBoneMatrixIndex;
#endif
float *m_pNormal;
unsigned char *m_pColor;
unsigned char *m_pSpecular;
float *m_pTexCoord[VERTEX_MAX_TEXTURE_COORDINATES];
// Tangent space *associated with one particular set of texcoords*
float *m_pTangentS;
float *m_pTangentT;
float *m_pWrinkle;
// user data
float *m_pUserData;
// The first vertex index (used for buffered vertex buffers, or cards that don't support stream offset)
int m_nFirstVertex;
// The offset in bytes of the memory we're writing into
// from the start of the D3D buffer (will be 0 for static meshes)
unsigned int m_nOffset;
#ifdef DEBUG_WRITE_COMBINE
int m_nLastWrittenField;
unsigned char* m_pLastWrittenAddress;
#endif
};
struct IndexDesc_t
{
// Pointers to the index data
unsigned short *m_pIndices;
// The offset in bytes of the memory we're writing into
// from the start of the D3D buffer (will be 0 for static meshes)
unsigned int m_nOffset;
// The first index (used for buffered index buffers, or cards that don't support stream offset)
unsigned int m_nFirstIndex;
// 1 if the device is active, 0 if the device isn't active.
// Faster than doing if checks for null m_pIndices if someone is
// trying to write the m_pIndices while the device is inactive.
unsigned char m_nIndexSize;
};
//-----------------------------------------------------------------------------
// The Mesh memory descriptor
//-----------------------------------------------------------------------------
struct MeshDesc_t : public VertexDesc_t, public IndexDesc_t
{
};
//-----------------------------------------------------------------------------
// Standard vertex formats for models
//-----------------------------------------------------------------------------
struct ModelVertexDX7_t
{
Vector m_vecPosition;
Vector2D m_flBoneWeights;
unsigned int m_nBoneIndices;
Vector m_vecNormal;
unsigned int m_nColor; // ARGB
Vector2D m_vecTexCoord;
};
struct ModelVertexDX8_t : public ModelVertexDX7_t
{
Vector4D m_vecUserData;
};
//-----------------------------------------------------------------------------
// Utility methods for buffer builders
//-----------------------------------------------------------------------------
inline float *OffsetFloatPointer( float *pBufferPointer, int nVertexCount, int vertexSize )
{
return reinterpret_cast<float *>(
reinterpret_cast<unsigned char *>(pBufferPointer) +
nVertexCount * vertexSize);
}
inline const float *OffsetFloatPointer( const float *pBufferPointer, int nVertexCount, int vertexSize )
{
return reinterpret_cast<const float*>(
reinterpret_cast<unsigned char const*>(pBufferPointer) +
nVertexCount * vertexSize);
}
inline void IncrementFloatPointer( float* &pBufferPointer, int vertexSize )
{
pBufferPointer = reinterpret_cast<float*>( reinterpret_cast<unsigned char*>( pBufferPointer ) + vertexSize );
}
//-----------------------------------------------------------------------------
// Used in lists of indexed primitives.
//-----------------------------------------------------------------------------
class CPrimList
{
public:
CPrimList();
CPrimList( int nFirstIndex, int nIndexCount );
int m_FirstIndex;
int m_NumIndices;
};
inline CPrimList::CPrimList()
{
}
inline CPrimList::CPrimList( int nFirstIndex, int nIndexCount )
{
m_FirstIndex = nFirstIndex;
m_NumIndices = nIndexCount;
}
abstract_class IVertexBuffer
{
public:
// Add a virtual destructor to silence the clang warning.
// This is harmless but not important since the only derived class
// doesn't have a destructor.
virtual ~IVertexBuffer() {}
// NOTE: The following two methods are only valid for static vertex buffers
// Returns the number of vertices and the format of the vertex buffer
virtual int VertexCount() const = 0;
virtual VertexFormat_t GetVertexFormat() const = 0;
// Is this vertex buffer dynamic?
virtual bool IsDynamic() const = 0;
// NOTE: For dynamic vertex buffers only!
// Casts the memory of the dynamic vertex buffer to the appropriate type
virtual void BeginCastBuffer( VertexFormat_t format ) = 0;
virtual void EndCastBuffer() = 0;
// Returns the number of vertices that can still be written into the buffer
virtual int GetRoomRemaining() const = 0;
virtual bool Lock( int nVertexCount, bool bAppend, VertexDesc_t &desc ) = 0;
virtual void Unlock( int nVertexCount, VertexDesc_t &desc ) = 0;
// Spews the mesh data
virtual void Spew( int nVertexCount, const VertexDesc_t &desc ) = 0;
// Call this in debug mode to make sure our data is good.
virtual void ValidateData( int nVertexCount, const VertexDesc_t & desc ) = 0;
};
abstract_class IIndexBuffer
{
public:
// Add a virtual destructor to silence the clang warning.
// This is harmless but not important since the only derived class
// doesn't have a destructor.
virtual ~IIndexBuffer() {}
// NOTE: The following two methods are only valid for static index buffers
// Returns the number of indices and the format of the index buffer
virtual int IndexCount() const = 0;
virtual MaterialIndexFormat_t IndexFormat() const = 0;
// Is this index buffer dynamic?
virtual bool IsDynamic() const = 0;
// NOTE: For dynamic index buffers only!
// Casts the memory of the dynamic index buffer to the appropriate type
virtual void BeginCastBuffer( MaterialIndexFormat_t format ) = 0;
virtual void EndCastBuffer() = 0;
// Returns the number of indices that can still be written into the buffer
virtual int GetRoomRemaining() const = 0;
// Locks, unlocks the index buffer
virtual bool Lock( int nMaxIndexCount, bool bAppend, IndexDesc_t &desc ) = 0;
virtual void Unlock( int nWrittenIndexCount, IndexDesc_t &desc ) = 0;
// FIXME: Remove this!!
// Locks, unlocks the index buffer for modify
virtual void ModifyBegin( bool bReadOnly, int nFirstIndex, int nIndexCount, IndexDesc_t& desc ) = 0;
virtual void ModifyEnd( IndexDesc_t& desc ) = 0;
// Spews the mesh data
virtual void Spew( int nIndexCount, const IndexDesc_t &desc ) = 0;
// Ensures the data in the index buffer is valid
virtual void ValidateData( int nIndexCount, const IndexDesc_t &desc ) = 0;
};
//-----------------------------------------------------------------------------
// Interface to the mesh - needs to contain an IVertexBuffer and an IIndexBuffer to emulate old mesh behavior
//-----------------------------------------------------------------------------
abstract_class IMesh : public IVertexBuffer, public IIndexBuffer
{
public:
// -----------------------------------
// Sets/gets the primitive type
virtual void SetPrimitiveType( MaterialPrimitiveType_t type ) = 0;
// Draws the mesh
virtual void Draw( int nFirstIndex = -1, int nIndexCount = 0 ) = 0;
virtual void SetColorMesh( IMesh *pColorMesh, int nVertexOffset ) = 0;
// Draw a list of (lists of) primitives. Batching your lists together that use
// the same lightmap, material, vertex and index buffers with multipass shaders
// can drastically reduce state-switching overhead.
// NOTE: this only works with STATIC meshes.
virtual void Draw( CPrimList *pLists, int nLists ) = 0;
// 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 ) = 0;
// Spews the mesh data
virtual void Spew( int nVertexCount, int nIndexCount, const MeshDesc_t &desc ) = 0;
// Call this in debug mode to make sure our data is good.
virtual void ValidateData( int nVertexCount, int nIndexCount, const MeshDesc_t &desc ) = 0;
// New version
// Locks/unlocks the mesh, providing space for nVertexCount and nIndexCount.
// nIndexCount of -1 means don't lock the index buffer...
virtual void LockMesh( int nVertexCount, int nIndexCount, MeshDesc_t &desc ) = 0;
virtual void ModifyBegin( int nFirstVertex, int nVertexCount, int nFirstIndex, int nIndexCount, MeshDesc_t& desc ) = 0;
virtual void ModifyEnd( MeshDesc_t& desc ) = 0;
virtual void UnlockMesh( int nVertexCount, int nIndexCount, MeshDesc_t &desc ) = 0;
virtual void ModifyBeginEx( bool bReadOnly, int nFirstVertex, int nVertexCount, int nFirstIndex, int nIndexCount, MeshDesc_t &desc ) = 0;
virtual void SetFlexMesh( IMesh *pMesh, int nVertexOffset ) = 0;
virtual void DisableFlexMesh() = 0;
virtual void MarkAsDrawn() = 0;
virtual unsigned ComputeMemoryUsed() = 0;
};
#include "meshreader.h"
#define INVALID_BUFFER_OFFSET 0xFFFFFFFFUL
// flags for advancevertex optimization
#define VTX_HAVEPOS 1
#define VTX_HAVENORMAL 2
#define VTX_HAVECOLOR 4
#define VTX_HAVEALL ( VTX_HAVEPOS | VTX_HAVENORMAL | VTX_HAVECOLOR )
//-----------------------------------------------------------------------------
//
// Helper class used to define vertex buffers
//
//-----------------------------------------------------------------------------
class CVertexBuilder : private VertexDesc_t
{
public:
CVertexBuilder();
CVertexBuilder( IVertexBuffer *pVertexBuffer, VertexFormat_t fmt = 0 );
~CVertexBuilder();
// Begins, ends modification of the index buffer (returns true if the lock succeeded)
// A lock may not succeed if append is set to true and there isn't enough room
// NOTE: Append is only used with dynamic index buffers; it's ignored for static buffers
bool Lock( int nMaxIndexCount, bool bAppend = false );
void Unlock();
// Spews the current data
// NOTE: Can only be called during a lock/unlock block
void SpewData();
// Returns the number of indices we can fit into the buffer without needing to discard
int GetRoomRemaining() const;
// Binds this vertex buffer
void Bind( IMatRenderContext *pContext, int nStreamID, VertexFormat_t usage = 0 );
// Returns the byte offset
int Offset() const;
// This must be called before Begin, if a vertex buffer with a compressed format is to be used
void SetCompressionType( VertexCompressionType_t compressionType );
void ValidateCompressionType();
void Begin( IVertexBuffer *pVertexBuffer, int nVertexCount, int *nFirstVertex );
void Begin( IVertexBuffer *pVertexBuffer, int nVertexCount );
// Use this when you're done writing
// Set bDraw to true to call m_pMesh->Draw automatically.
void End( bool bSpewData = false );
// Locks the vertex buffer to modify existing data
// Passing nVertexCount == -1 says to lock all the vertices for modification.
void BeginModify( IVertexBuffer *pVertexBuffer, int nFirstVertex = 0, int nVertexCount = -1 );
void EndModify( bool bSpewData = false );
// returns the number of vertices
int VertexCount() const;
// Returns the total number of vertices across all Locks()
int TotalVertexCount() const;
// Resets the mesh builder so it points to the start of everything again
void Reset();
// Returns the size of the vertex
int VertexSize() { return m_ActualVertexSize; }
// returns the data size of a given texture coordinate
int TextureCoordinateSize( int nTexCoordNumber ) { return m_VertexSize_TexCoord[ nTexCoordNumber ]; }
// Returns the base vertex memory pointer
void* BaseVertexData();
// Selects the nth Vertex and Index
void SelectVertex( int idx );
// Advances the current vertex and index by one
void AdvanceVertex( void );
template<int nFlags, int nNumTexCoords> void AdvanceVertexF( void );
void AdvanceVertices( int nVerts );
int GetCurrentVertex() const;
int GetFirstVertex() const;
// Data retrieval...
const float *Position() const;
const float *Normal() const;
unsigned int Color() const;
unsigned char *Specular() const;
const float *TexCoord( int stage ) const;
const float *TangentS() const;
const float *TangentT() const;
const float *BoneWeight() const;
float Wrinkle() const;
int NumBoneWeights() const;
#ifndef NEW_SKINNING
unsigned char *BoneMatrix() const;
#else
float *BoneMatrix() const;
#endif
// position setting
void Position3f( float x, float y, float z );
void Position3fv( const float *v );
// normal setting
void Normal3f( float nx, float ny, float nz );
void Normal3fv( const float *n );
void NormalDelta3fv( const float *n );
void NormalDelta3f( float nx, float ny, float nz );
// normal setting (templatized for code which needs to support compressed vertices)
template <VertexCompressionType_t T> void CompressedNormal3f( float nx, float ny, float nz );
template <VertexCompressionType_t T> void CompressedNormal3fv( const float *n );
// color setting
void Color3f( float r, float g, float b );
void Color3fv( const float *rgb );
void Color4f( float r, float g, float b, float a );
void Color4fv( const float *rgba );
// Faster versions of color
void Color3ub( unsigned char r, unsigned char g, unsigned char b );
void Color3ubv( unsigned char const* rgb );
void Color4ub( unsigned char r, unsigned char g, unsigned char b, unsigned char a );
void Color4ubv( unsigned char const* rgba );
// specular color setting
void Specular3f( float r, float g, float b );
void Specular3fv( const float *rgb );
void Specular4f( float r, float g, float b, float a );
void Specular4fv( const float *rgba );
// Faster version of specular
void Specular3ub( unsigned char r, unsigned char g, unsigned char b );
void Specular3ubv( unsigned char const *c );
void Specular4ub( unsigned char r, unsigned char g, unsigned char b, unsigned char a );
void Specular4ubv( unsigned char const *c );
// texture coordinate setting
void TexCoord1f( int stage, float s );
void TexCoord2f( int stage, float s, float t );
void TexCoord2fv( int stage, const float *st );
void TexCoord3f( int stage, float s, float t, float u );
void TexCoord3fv( int stage, const float *stu );
void TexCoord4f( int stage, float s, float t, float u, float w );
void TexCoord4fv( int stage, const float *stuv );
void TexCoordSubRect2f( int stage, float s, float t, float offsetS, float offsetT, float scaleS, float scaleT );
void TexCoordSubRect2fv( int stage, const float *st, const float *offset, const float *scale );
// tangent space
void TangentS3f( float sx, float sy, float sz );
void TangentS3fv( const float* s );
void TangentT3f( float tx, float ty, float tz );
void TangentT3fv( const float* t );
// Wrinkle
void Wrinkle1f( float flWrinkle );
// bone weights
void BoneWeight( int idx, float weight );
// bone weights (templatized for code which needs to support compressed vertices)
template <VertexCompressionType_t T> void CompressedBoneWeight3fv( const float * pWeights );
// bone matrix index
void BoneMatrix( int idx, int matrixIndex );
// Generic per-vertex data
void UserData( const float* pData );
// Generic per-vertex data (templatized for code which needs to support compressed vertices)
template <VertexCompressionType_t T> void CompressedUserData( const float* pData );
// Fast Vertex! No need to call advance vertex, and no random access allowed.
// WARNING - these are low level functions that are intended only for use
// in the software vertex skinner.
void FastVertex( const ModelVertexDX7_t &vertex );
void FastVertexSSE( const ModelVertexDX7_t &vertex );
// store 4 dx7 vertices fast. for special sse dx7 pipeline
void Fast4VerticesSSE(
ModelVertexDX7_t const *vtx_a,
ModelVertexDX7_t const *vtx_b,
ModelVertexDX7_t const *vtx_c,
ModelVertexDX7_t const *vtx_d);
void FastVertex( const ModelVertexDX8_t &vertex );
void FastVertexSSE( const ModelVertexDX8_t &vertex );
// Add number of verts and current vert since FastVertex routines do not update.
void FastAdvanceNVertices( int n );
#if defined( _X360 )
void VertexDX8ToX360( const ModelVertexDX8_t &vertex );
#endif
// FIXME: Remove! Backward compat so we can use this from a CMeshBuilder.
void AttachBegin( IMesh* pMesh, int nMaxVertexCount, const MeshDesc_t &desc );
void AttachEnd();
void AttachBeginModify( IMesh* pMesh, int nFirstVertex, int nVertexCount, const MeshDesc_t &desc );
void AttachEndModify();
private:
// The vertex buffer we're modifying
IVertexBuffer *m_pVertexBuffer;
// Used to make sure Begin/End calls and BeginModify/EndModify calls match.
bool m_bModify;
// Max number of indices and vertices
int m_nMaxVertexCount;
// Number of indices and vertices
int m_nVertexCount;
// The current vertex and index
mutable int m_nCurrentVertex;
// Optimization: Pointer to the current pos, norm, texcoord, and color
mutable float *m_pCurrPosition;
mutable float *m_pCurrNormal;
mutable float *m_pCurrTexCoord[VERTEX_MAX_TEXTURE_COORDINATES];
mutable unsigned char *m_pCurrColor;
// Total number of vertices appended
int m_nTotalVertexCount;
// First vertex buffer offset + index
unsigned int m_nBufferOffset;
unsigned int m_nBufferFirstVertex;
#if ( COMPRESSED_NORMALS_TYPE == COMPRESSED_NORMALS_COMBINEDTANGENTS_UBYTE4 )
// Debug checks to make sure we write userdata4/tangents AFTER normals
bool m_bWrittenNormal : 1;
bool m_bWrittenUserData : 1;
#endif
friend class CMeshBuilder;
};
//-----------------------------------------------------------------------------
//
// Inline methods of CVertexBuilder
//
//-----------------------------------------------------------------------------
inline CVertexBuilder::CVertexBuilder()
{
m_pVertexBuffer = NULL;
m_nBufferOffset = INVALID_BUFFER_OFFSET;
m_nBufferFirstVertex = 0;
m_nVertexCount = 0;
m_nCurrentVertex = 0;
m_nMaxVertexCount = 0;
m_nTotalVertexCount = 0;
m_CompressionType = VERTEX_COMPRESSION_INVALID;
#ifdef _DEBUG
m_pCurrPosition = NULL;
m_pCurrNormal = NULL;
m_pCurrColor = NULL;
memset( m_pCurrTexCoord, 0, sizeof( m_pCurrTexCoord ) );
m_bModify = false;
#endif
}
inline CVertexBuilder::CVertexBuilder( IVertexBuffer *pVertexBuffer, VertexFormat_t fmt )
{
m_pVertexBuffer = pVertexBuffer;
m_nBufferOffset = INVALID_BUFFER_OFFSET;
m_nBufferFirstVertex = 0;
m_nVertexCount = 0;
m_nCurrentVertex = 0;
m_nMaxVertexCount = 0;
m_nTotalVertexCount = 0;
m_CompressionType = VERTEX_COMPRESSION_INVALID;
if ( m_pVertexBuffer->IsDynamic() )
{
m_pVertexBuffer->BeginCastBuffer( fmt );
}
else
{
Assert( m_pVertexBuffer->GetVertexFormat() == fmt );
}
#ifdef _DEBUG
m_pCurrPosition = NULL;
m_pCurrNormal = NULL;
m_pCurrColor = NULL;
memset( m_pCurrTexCoord, 0, sizeof( m_pCurrTexCoord ) );
m_bModify = false;
#endif
}
inline CVertexBuilder::~CVertexBuilder()
{
if ( m_pVertexBuffer && m_pVertexBuffer->IsDynamic() )
{
m_pVertexBuffer->EndCastBuffer();
}
}
//-----------------------------------------------------------------------------
// Begins, ends modification of the index buffer
//-----------------------------------------------------------------------------
inline bool CVertexBuilder::Lock( int nMaxVertexCount, bool bAppend )
{
Assert( m_pVertexBuffer );
m_bModify = false;
m_nMaxVertexCount = nMaxVertexCount;
bool bFirstLock = ( m_nBufferOffset == INVALID_BUFFER_OFFSET );
if ( bFirstLock )
{
bAppend = false;
}
if ( !bAppend )
{
m_nTotalVertexCount = 0;
}
// Lock the vertex buffer
if ( !m_pVertexBuffer->Lock( m_nMaxVertexCount, bAppend, *this ) )
{
m_nMaxVertexCount = 0;
return false;
}
Reset();
if ( bFirstLock )
{
m_nBufferOffset = m_nOffset;
m_nBufferFirstVertex = m_nFirstVertex;
}
return true;
}
inline void CVertexBuilder::Unlock()
{
Assert( !m_bModify && m_pVertexBuffer );
#ifdef _DEBUG
m_pVertexBuffer->ValidateData( m_nVertexCount, *this );
#endif
m_pVertexBuffer->Unlock( m_nVertexCount, *this );
m_nTotalVertexCount += m_nVertexCount;
m_nMaxVertexCount = 0;
#ifdef _DEBUG
// Null out our data...
m_pCurrPosition = NULL;
m_pCurrNormal = NULL;
m_pCurrColor = NULL;
memset( m_pCurrTexCoord, 0, sizeof( m_pCurrTexCoord ) );
memset( static_cast<VertexDesc_t*>( this ), 0, sizeof(VertexDesc_t) );
#endif
}
inline void CVertexBuilder::SpewData()
{
m_pVertexBuffer->Spew( m_nVertexCount, *this );
}
//-----------------------------------------------------------------------------
// Binds this vertex buffer
//-----------------------------------------------------------------------------
inline void CVertexBuilder::Bind( IMatRenderContext *pContext, int nStreamID, VertexFormat_t usage )
{
if ( m_pVertexBuffer && ( m_nBufferOffset != INVALID_BUFFER_OFFSET ) )
{
pContext->BindVertexBuffer( nStreamID, m_pVertexBuffer, m_nBufferOffset,
m_nFirstVertex, m_nTotalVertexCount, usage ? usage : m_pVertexBuffer->GetVertexFormat() );
}
else
{
pContext->BindVertexBuffer( nStreamID, NULL, 0, 0, 0, 0 );
}
}
//-----------------------------------------------------------------------------
// Returns the byte offset
//-----------------------------------------------------------------------------
inline int CVertexBuilder::Offset() const
{
return m_nBufferOffset;
}
inline int CVertexBuilder::GetFirstVertex() const
{
return m_nBufferFirstVertex;
}
//-----------------------------------------------------------------------------
// Specify the type of vertex compression that this CMeshBuilder will perform
//-----------------------------------------------------------------------------
inline void CVertexBuilder::SetCompressionType( VertexCompressionType_t compressionType )
{
// The real purpose of this method is to allow us to emit a Warning in Begin()
m_CompressionType = compressionType;
}
inline void CVertexBuilder::ValidateCompressionType()
{
#ifdef _DEBUG
VertexCompressionType_t vbCompressionType = CompressionType( m_pVertexBuffer->GetVertexFormat() );
if ( vbCompressionType != VERTEX_COMPRESSION_NONE )
{
Assert( m_CompressionType == vbCompressionType );
if ( m_CompressionType != vbCompressionType )
{
Warning( "ERROR: CVertexBuilder::SetCompressionType() must be called to specify the same vertex compression type (%s) as the vertex buffer being modified."
"Junk vertices will be rendered, or there will be a crash in CVertexBuilder!\n",
vbCompressionType == VERTEX_COMPRESSION_ON ? "VERTEX_COMPRESSION_ON" : "VERTEX_COMPRESSION_NONE" );
}
// Never use vertex compression for dynamic VBs (the conversions can really hurt perf)
Assert( !m_pVertexBuffer->IsDynamic() );
}
#endif
}
inline void CVertexBuilder::Begin( IVertexBuffer *pVertexBuffer, int nVertexCount )
{
Assert( pVertexBuffer && (!m_pVertexBuffer) );
m_pVertexBuffer = pVertexBuffer;
m_bModify = false;
m_nMaxVertexCount = nVertexCount;
m_nVertexCount = 0;
// Make sure SetCompressionType was called correctly, if this VB is compressed
ValidateCompressionType();
// Lock the vertex and index buffer
m_pVertexBuffer->Lock( m_nMaxVertexCount, false, *this );
// Point to the start of the buffers..
Reset();
}
//-----------------------------------------------------------------------------
// Use this when you're done modifying the mesh
//-----------------------------------------------------------------------------
inline void CVertexBuilder::End( bool bSpewData )
{
// Make sure they called Begin()
Assert( !m_bModify );
if ( bSpewData )
{
m_pVertexBuffer->Spew( m_nVertexCount, *this );
}
#ifdef _DEBUG
m_pVertexBuffer->ValidateData( m_nVertexCount, *this );
#endif
// Unlock our buffers
m_pVertexBuffer->Unlock( m_nVertexCount, *this );
m_pVertexBuffer = 0;
m_nMaxVertexCount = 0;
m_CompressionType = VERTEX_COMPRESSION_INVALID;
#ifdef _DEBUG
// Null out our pointers...
m_pCurrPosition = NULL;
m_pCurrNormal = NULL;
m_pCurrColor = NULL;
memset( m_pCurrTexCoord, 0, sizeof( m_pCurrTexCoord ) );
memset( static_cast< VertexDesc_t* >( this ), 0, sizeof(VertexDesc_t) );
#endif
}
//-----------------------------------------------------------------------------
// FIXME: Remove! Backward compat so we can use this from a CMeshBuilder.
//-----------------------------------------------------------------------------
inline void CVertexBuilder::AttachBegin( IMesh* pMesh, int nMaxVertexCount, const MeshDesc_t &desc )
{
VertexCompressionType_t compressionType = m_CompressionType;
m_pVertexBuffer = pMesh;
memcpy( static_cast<VertexDesc_t*>( this ), static_cast<const VertexDesc_t*>( &desc ), sizeof(VertexDesc_t) );
m_nMaxVertexCount = nMaxVertexCount;
m_NumBoneWeights = m_NumBoneWeights == 0 ? 0 : 2; // Two weights if any
m_nVertexCount = 0;
m_bModify = false;
if ( compressionType != VERTEX_COMPRESSION_INVALID )
m_CompressionType = compressionType;
// Make sure SetCompressionType was called correctly, if this VB is compressed
ValidateCompressionType();
if ( m_nBufferOffset == INVALID_BUFFER_OFFSET )
{
m_nTotalVertexCount = 0;
m_nBufferOffset = static_cast< const VertexDesc_t* >( &desc )->m_nOffset;
m_nBufferFirstVertex = desc.m_nFirstVertex;
}
}
inline void CVertexBuilder::AttachEnd()
{
// Make sure they called Begin()
Assert( !m_bModify );
m_nMaxVertexCount = 0;
m_pVertexBuffer = NULL;
m_CompressionType = VERTEX_COMPRESSION_INVALID;
#ifdef _DEBUG
// Null out our pointers...
m_pCurrPosition = NULL;
m_pCurrNormal = NULL;
m_pCurrColor = NULL;
memset( m_pCurrTexCoord, 0, sizeof( m_pCurrTexCoord ) );
memset( static_cast<VertexDesc_t*>( this ), 0, sizeof(VertexDesc_t) );
#endif
}
inline void CVertexBuilder::AttachBeginModify( IMesh* pMesh, int nFirstVertex, int nVertexCount, const MeshDesc_t &desc )
{
Assert( pMesh && (!m_pVertexBuffer) );
m_pVertexBuffer = pMesh;
memcpy( static_cast<VertexDesc_t*>( this ), static_cast<const VertexDesc_t*>( &desc ), sizeof(VertexDesc_t) );
m_nMaxVertexCount = m_nVertexCount = nVertexCount;
m_NumBoneWeights = m_NumBoneWeights == 0 ? 0 : 2; // Two weights if any
m_bModify = true;
// Make sure SetCompressionType was called correctly, if this VB is compressed
ValidateCompressionType();
}
inline void CVertexBuilder::AttachEndModify()
{
Assert( m_pVertexBuffer );
Assert( m_bModify ); // Make sure they called BeginModify.
m_pVertexBuffer = 0;
m_nMaxVertexCount = 0;
m_CompressionType = VERTEX_COMPRESSION_INVALID;
#ifdef _DEBUG
// Null out our pointers...
m_pCurrPosition = NULL;
m_pCurrNormal = NULL;
m_pCurrColor = NULL;
memset( m_pCurrTexCoord, 0, sizeof( m_pCurrTexCoord ) );
memset( static_cast<VertexDesc_t*>( this ), 0, sizeof(VertexDesc_t) );
#endif
}
//-----------------------------------------------------------------------------
// Computes the first min non-null address
//-----------------------------------------------------------------------------
inline unsigned char* FindMinAddress( void *pAddress1, void *pAddress2, int nAddress2Size )
{
if ( nAddress2Size == 0 )
return (unsigned char*)pAddress1;
if ( !pAddress1 )
return (unsigned char*)pAddress2;
return ( pAddress1 < pAddress2 ) ? (unsigned char*)pAddress1 : (unsigned char*)pAddress2;
}
//-----------------------------------------------------------------------------
// Resets the vertex buffer builder so it points to the start of everything again
//-----------------------------------------------------------------------------
inline void CVertexBuilder::Reset()
{
m_nCurrentVertex = 0;
m_pCurrPosition = m_pPosition;
m_pCurrNormal = m_pNormal;
for ( int i = 0; i < NELEMS( m_pCurrTexCoord ); i++ )
{
m_pCurrTexCoord[i] = m_pTexCoord[i];
}
m_pCurrColor = m_pColor;
#if ( defined( _DEBUG ) && ( COMPRESSED_NORMALS_TYPE == COMPRESSED_NORMALS_COMBINEDTANGENTS_UBYTE4 ) )
m_bWrittenNormal = false;
m_bWrittenUserData = false;
#endif
#ifdef DEBUG_WRITE_COMBINE
// Logic for m_pLastWrittenAddress is tricky. It really wants the min of the
// non-null address pointers.
m_nLastWrittenField = MB_FIELD_NONE;
m_pLastWrittenAddress = NULL;
m_pLastWrittenAddress = FindMinAddress( m_pLastWrittenAddress, m_pPosition, m_VertexSize_Position );
m_pLastWrittenAddress = FindMinAddress( m_pLastWrittenAddress, m_pBoneWeight, m_VertexSize_BoneWeight );
m_pLastWrittenAddress = FindMinAddress( m_pLastWrittenAddress, m_pBoneMatrixIndex, m_VertexSize_BoneMatrixIndex );
m_pLastWrittenAddress = FindMinAddress( m_pLastWrittenAddress, m_pNormal, m_VertexSize_Normal );
m_pLastWrittenAddress = FindMinAddress( m_pLastWrittenAddress, m_pColor, m_VertexSize_Color );
m_pLastWrittenAddress = FindMinAddress( m_pLastWrittenAddress, m_pSpecular, m_VertexSize_Specular );
for ( int i = 0; i < VERTEX_MAX_TEXTURE_COORDINATES; ++i )
{
m_pLastWrittenAddress = FindMinAddress( m_pLastWrittenAddress, m_pTexCoord[i], m_VertexSize_TexCoord[i] );
}
m_pLastWrittenAddress = FindMinAddress( m_pLastWrittenAddress, m_pTangentS, m_VertexSize_TangentS );
m_pLastWrittenAddress = FindMinAddress( m_pLastWrittenAddress, m_pTangentT, m_VertexSize_TangentT );
m_pLastWrittenAddress = FindMinAddress( m_pLastWrittenAddress, m_pUserData, m_VertexSize_UserData );
#endif