-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathtr_vbo.cpp
More file actions
1025 lines (833 loc) · 27.4 KB
/
tr_vbo.cpp
File metadata and controls
1025 lines (833 loc) · 27.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
===========================================================================
Copyright (C) 2007-2011 Robert Beckebans <trebor_7@users.sourceforge.net>
This file is part of Daemon source code.
Daemon source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
Daemon source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Daemon source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// tr_vbo.c
#include "VBO.h"
#include "Material.h"
#include "GeometryCache.h"
#include "GLMemory.h"
// interleaved data: position, colour, qtangent, texcoord
// -> struct shaderVertex_t in tr_local.h
const GLsizei sizeShaderVertex = sizeof( shaderVertex_t );
static void R_SetAttributeLayoutsStatic( VBO_t *vbo )
{
vbo->attribs[ ATTR_INDEX_POSITION ].numComponents = 3;
vbo->attribs[ ATTR_INDEX_POSITION ].componentType = GL_FLOAT;
vbo->attribs[ ATTR_INDEX_POSITION ].normalize = GL_FALSE;
vbo->attribs[ ATTR_INDEX_POSITION ].ofs = offsetof( shaderVertex_t, xyz );
vbo->attribs[ ATTR_INDEX_POSITION ].stride = sizeShaderVertex;
vbo->attribs[ ATTR_INDEX_POSITION ].frameOffset = 0;
vbo->attribs[ ATTR_INDEX_COLOR ].numComponents = 4;
vbo->attribs[ ATTR_INDEX_COLOR ].componentType = GL_UNSIGNED_BYTE;
vbo->attribs[ ATTR_INDEX_COLOR ].normalize = GL_TRUE;
vbo->attribs[ ATTR_INDEX_COLOR ].ofs = offsetof( shaderVertex_t, color );
vbo->attribs[ ATTR_INDEX_COLOR ].stride = sizeShaderVertex;
vbo->attribs[ ATTR_INDEX_COLOR ].frameOffset = 0;
vbo->attribs[ ATTR_INDEX_QTANGENT ].numComponents = 4;
vbo->attribs[ ATTR_INDEX_QTANGENT ].componentType = GL_SHORT;
vbo->attribs[ ATTR_INDEX_QTANGENT ].normalize = GL_TRUE;
vbo->attribs[ ATTR_INDEX_QTANGENT ].ofs = offsetof( shaderVertex_t, qtangents );
vbo->attribs[ ATTR_INDEX_QTANGENT ].stride = sizeShaderVertex;
vbo->attribs[ ATTR_INDEX_QTANGENT ].frameOffset = 0;
vbo->attribs[ ATTR_INDEX_TEXCOORD ].numComponents = 4;
vbo->attribs[ ATTR_INDEX_TEXCOORD ].componentType = GL_FLOAT;
vbo->attribs[ ATTR_INDEX_TEXCOORD ].normalize = GL_FALSE;
vbo->attribs[ ATTR_INDEX_TEXCOORD ].ofs = offsetof( shaderVertex_t, texCoords );
vbo->attribs[ ATTR_INDEX_TEXCOORD ].stride = sizeShaderVertex;
vbo->attribs[ ATTR_INDEX_TEXCOORD ].frameOffset = 0;
// total size
vbo->vertexesSize = sizeShaderVertex * vbo->vertexesNum;
}
static void R_SetVBOAttributeLayouts( VBO_t *vbo )
{
if ( vbo->layout == vboLayout_t::VBO_LAYOUT_STATIC )
{
R_SetAttributeLayoutsStatic( vbo );
}
else
{
Sys::Drop("%sUnknown attribute layout for vbo: %s\n",
Color::ToString( Color::Yellow ), vbo->name );
}
}
uint32_t R_ComponentSize( GLenum type )
{
switch ( type )
{
case GL_UNSIGNED_BYTE:
return 1;
case GL_SHORT:
case GL_UNSIGNED_SHORT:
case GL_HALF_FLOAT:
return 2;
case GL_FLOAT:
return 4;
}
Sys::Error( "VBO R_ComponentSize: unknown type %d", type );
}
/*
============
R_InitRingbuffer
============
*/
static void R_InitRingbuffer( GLenum target, GLsizei elementSize,
GLsizei segmentElements, glRingbuffer_t *rb ) {
GLsizei totalSize = elementSize * segmentElements * DYN_BUFFER_SEGMENTS;
glBufferStorage( target, totalSize, nullptr,
GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT );
rb->baseAddr = glMapBufferRange( target, 0, totalSize,
GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_FLUSH_EXPLICIT_BIT );
rb->elementSize = elementSize;
rb->segmentElements = segmentElements;
rb->activeSegment = 0;
for( int i = 1; i < DYN_BUFFER_SEGMENTS; i++ ) {
rb->syncs[ i ] = glFenceSync( GL_SYNC_GPU_COMMANDS_COMPLETE, 0 );
}
}
/*
============
R_RotateRingbuffer
============
*/
static GLsizei R_RotateRingbuffer( glRingbuffer_t *rb ) {
rb->syncs[ rb->activeSegment ] = glFenceSync( GL_SYNC_GPU_COMMANDS_COMPLETE, 0 );
rb->activeSegment++;
if ( rb->activeSegment >= DYN_BUFFER_SEGMENTS ) {
rb->activeSegment = 0;
}
// wait until next segment is ready in 10ms intervals
const GLuint64 TIMEOUT = 10000000;
while( glClientWaitSync( rb->syncs[ rb->activeSegment ], GL_SYNC_FLUSH_COMMANDS_BIT, TIMEOUT )
== GL_TIMEOUT_EXPIRED ) {
Log::Warn( "Long wait for dynamic GL buffer: active segment: %i, timeout: %uns", rb->activeSegment, TIMEOUT );
};
glDeleteSync( rb->syncs[ rb->activeSegment ] );
return rb->activeSegment * rb->segmentElements;
}
/*
============
R_ShutdownRingbuffer
============
*/
static void R_ShutdownRingbuffer( GLenum target, glRingbuffer_t *rb ) {
glUnmapBuffer( target );
rb->baseAddr = nullptr;
for( int i = 0; i < DYN_BUFFER_SEGMENTS; i++ ) {
if( i == rb->activeSegment )
continue;
glDeleteSync( rb->syncs[ i ] );
}
}
VBO_t *R_CreateDynamicVBO( const char *name, int numVertexes, uint32_t stateBits, vboLayout_t layout )
{
if ( !numVertexes )
{
return nullptr;
}
// make sure the render thread is stopped
R_SyncRenderThread();
VBO_t* vbo = (VBO_t*) ri.Hunk_Alloc( sizeof( *vbo ), ha_pref::h_low );
*vbo = {};
tr.vbos.push_back( vbo );
Q_strncpyz( vbo->name, name, sizeof( vbo->name ) );
vbo->layout = layout;
vbo->framesNum = 0;
vbo->vertexesNum = numVertexes;
vbo->attribBits = stateBits;
vbo->usage = GL_DYNAMIC_DRAW;
R_SetVBOAttributeLayouts( vbo );
glGenBuffers( 1, &vbo->vertexesVBO );
R_BindVBO( vbo );
if( glConfig2.mapBufferRangeAvailable && glConfig2.bufferStorageAvailable &&
glConfig2.syncAvailable ) {
R_InitRingbuffer( GL_ARRAY_BUFFER, sizeof( shaderVertex_t ), numVertexes, &tess.vertexRB );
} else {
glBufferData( GL_ARRAY_BUFFER, vbo->vertexesSize, nullptr, vbo->usage );
}
R_BindNullVBO();
GL_CheckErrors();
return vbo;
}
void R_CopyVertexAttribute(
const vboAttributeLayout_t &attrib, const vertexAttributeSpec_t &spec,
uint32_t count, byte *interleavedData )
{
if ( count == 0 )
{
return; // some loops below are 'do/while'-like
}
const size_t inStride = spec.stride;
const size_t outStride = attrib.stride;
byte *out = interleavedData + attrib.ofs;
const byte *in = reinterpret_cast<const byte *>( spec.begin );
if ( attrib.componentType == spec.componentInputType )
{
uint32_t size = attrib.numComponents * R_ComponentSize( attrib.componentType );
for ( uint32_t v = count; ; )
{
memcpy( out, in, size );
if ( --v == 0 ) break;
in += inStride;
out += outStride;
}
}
else if ( spec.componentInputType == GL_FLOAT && attrib.componentType == GL_HALF_FLOAT )
{
for ( uint32_t v = count; ; )
{
const float *single = reinterpret_cast<const float *>( in );
f16_t *half = reinterpret_cast<f16_t *>( out );
for ( uint32_t n = spec.numComponents; n--; )
{
*half++ = floatToHalf( *single++ );
}
if ( --v == 0 ) break;
in += inStride;
out += outStride;
}
}
else if ( spec.componentInputType == GL_FLOAT && attrib.componentType == GL_SHORT
&& spec.attrOptions & ATTR_OPTION_NORMALIZE )
{
for ( uint32_t v = count; ; )
{
const float *single = reinterpret_cast<const float *>( in );
int16_t *snorm = reinterpret_cast<int16_t *>( out );
for ( uint32_t n = spec.numComponents; n--; )
{
*snorm++ = floatToSnorm16( *single++ );
}
if ( --v == 0 ) break;
in += inStride;
out += outStride;
}
}
else if ( spec.componentInputType == GL_FLOAT && attrib.componentType == GL_UNSIGNED_SHORT
&& spec.attrOptions & ATTR_OPTION_NORMALIZE )
{
for ( uint32_t v = count; ; )
{
const float *single = reinterpret_cast<const float *>( in );
uint16_t *unorm = reinterpret_cast<uint16_t *>( out );
for ( uint32_t n = spec.numComponents; n--; )
{
*unorm++ = floatToUnorm16( *single++ );
}
if ( --v == 0 ) break;
in += inStride;
out += outStride;
}
}
else
{
Sys::Error( "Unsupported GL type conversion (%d to %d)",
spec.componentInputType, attrib.componentType );
}
}
VBO_t *R_CreateStaticVBO(
Str::StringRef name,
const vertexAttributeSpec_t *attrBegin, const vertexAttributeSpec_t *attrEnd,
uint32_t numVerts, uint32_t numFrames )
{
// make sure the render thread is stopped
R_SyncRenderThread();
VBO_t *vbo = (VBO_t*) ri.Hunk_Alloc( sizeof( *vbo ), ha_pref::h_low );
*vbo = {};
tr.vbos.push_back( vbo );
Q_strncpyz( vbo->name, name.c_str(), sizeof(vbo->name));
vbo->vertexesNum = numVerts;
vbo->framesNum = numFrames;
vbo->usage = GL_STATIC_DRAW;
glGenBuffers( 1, &vbo->vertexesVBO );
R_BindVBO( vbo );
uint32_t ofsFrameless = 0;
uint32_t ofsFrameful = 0;
for ( const vertexAttributeSpec_t *spec = attrBegin; spec != attrEnd; ++spec )
{
vboAttributeLayout_t &attrib = vbo->attribs[ spec->attrIndex ];
ASSERT_EQ( attrib.numComponents, 0 );
ASSERT_NQ( spec->numComponents, 0U );
vbo->attribBits |= 1 << spec->attrIndex;
attrib.componentType = spec->componentStorageType;
if ( attrib.componentType == GL_HALF_FLOAT && !glConfig2.halfFloatVertexAvailable )
{
attrib.componentType = GL_FLOAT;
}
attrib.numComponents = spec->numComponents;
attrib.normalize = spec->attrOptions & ATTR_OPTION_NORMALIZE ? GL_TRUE : GL_FALSE;
uint32_t &ofs = spec->attrOptions & ATTR_OPTION_HAS_FRAMES ? ofsFrameful : ofsFrameless;
attrib.ofs = ofs;
ofs += attrib.numComponents * R_ComponentSize( attrib.componentType );
ofs = ( ofs + 3 ) & ~3; // 4 is minimum alignment for any vertex attribute
}
uint32_t framelessSize = ( ( numVerts * ofsFrameless ) + 31 ) & ~31;
vbo->vertexesSize = framelessSize + numFrames * numVerts * ofsFrameful;
// TODO: does it really need to be interleaved?
byte *interleavedData = (byte *)ri.Hunk_AllocateTempMemory( vbo->vertexesSize );
// When there is data with frames (only used for MD3), there are effectively two arrays:
// first one of length numVerts, then one of length numVerts * numFrames.
for ( const vertexAttributeSpec_t *spec = attrBegin; spec != attrEnd; ++spec )
{
vboAttributeLayout_t &attrib = vbo->attribs[ spec->attrIndex ];
if ( spec->attrOptions & ATTR_OPTION_HAS_FRAMES )
{
attrib.stride = ofsFrameful;
attrib.frameOffset = numVerts * ofsFrameful;
attrib.ofs += framelessSize;
R_CopyVertexAttribute( attrib, *spec, numVerts * numFrames, interleavedData );
}
else
{
attrib.stride = ofsFrameless;
R_CopyVertexAttribute( attrib, *spec, numVerts, interleavedData );
}
}
if( glConfig2.bufferStorageAvailable ) {
glBufferStorage( GL_ARRAY_BUFFER, vbo->vertexesSize, interleavedData, 0 );
} else {
glBufferData( GL_ARRAY_BUFFER, vbo->vertexesSize, interleavedData, vbo->usage );
}
ri.Hunk_FreeTempMemory( interleavedData );
R_BindNullVBO();
GL_CheckErrors();
return vbo;
}
/*
============
R_CreateIBO
============
*/
IBO_t *R_CreateDynamicIBO( const char *name, int numIndexes )
{
// make sure the render thread is stopped
R_SyncRenderThread();
IBO_t* ibo = (IBO_t*) ri.Hunk_Alloc( sizeof( *ibo ), ha_pref::h_low );
tr.ibos.push_back( ibo );
Q_strncpyz( ibo->name, name, sizeof( ibo->name ) );
ibo->indexesSize = numIndexes * sizeof( glIndex_t );
ibo->indexesNum = numIndexes;
glGenBuffers( 1, &ibo->indexesVBO );
R_BindIBO( ibo );
if( glConfig2.mapBufferRangeAvailable && glConfig2.bufferStorageAvailable &&
glConfig2.syncAvailable ) {
R_InitRingbuffer( GL_ELEMENT_ARRAY_BUFFER, sizeof( glIndex_t ), numIndexes, &tess.indexRB );
} else {
glBufferData( GL_ELEMENT_ARRAY_BUFFER, ibo->indexesSize, nullptr, GL_DYNAMIC_DRAW );
}
R_BindNullIBO();
GL_CheckErrors();
return ibo;
}
/*
============
R_CreateIBO2
============
*/
IBO_t *R_CreateStaticIBO( const char *name, glIndex_t *indexes, int numIndexes )
{
IBO_t *ibo;
if ( !numIndexes )
{
return nullptr;
}
// make sure the render thread is stopped
R_SyncRenderThread();
ibo = ( IBO_t * ) ri.Hunk_Alloc( sizeof( *ibo ), ha_pref::h_low );
tr.ibos.push_back( ibo );
Q_strncpyz( ibo->name, name, sizeof( ibo->name ) );
ibo->indexesSize = numIndexes * sizeof( glIndex_t );
ibo->indexesNum = numIndexes;
glGenBuffers( 1, &ibo->indexesVBO );
R_BindIBO( ibo );
if( glConfig2.bufferStorageAvailable ) {
glBufferStorage( GL_ELEMENT_ARRAY_BUFFER, ibo->indexesSize, indexes, 0 );
} else {
glBufferData( GL_ELEMENT_ARRAY_BUFFER, ibo->indexesSize, indexes, GL_STATIC_DRAW );
}
R_BindNullIBO();
GL_CheckErrors();
return ibo;
}
void SetupVAOBuffers( VBO_t* VBO, const IBO_t* IBO, const uint32_t stateBits,
GLVAO* VAO ) {
VAO->GenVAO();
VAO->Bind();
R_BindVBO( VBO );
GL_VertexAttribsState( stateBits, true );
if ( IBO ) {
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, IBO->indexesVBO );
}
GL_BindVAO( backEnd.defaultVAO );
}
/*
============
R_BindVBO
============
*/
void R_BindVBO( VBO_t *vbo )
{
if ( !vbo ) {
if ( tr.skipVBO ) {
R_BindNullVBO();
} else {
Sys::Drop( "R_BindNullVBO: NULL vbo" );
}
return;
}
GLIMP_LOGCOMMENT( "--- R_BindVBO( %s ) ---", vbo->name );
if ( glState.currentVBO != vbo )
{
glState.currentVBO = vbo;
glState.vertexAttribPointersSet = 0;
glState.vertexAttribsInterpolation = -1;
glState.vertexAttribsOldFrame = 0;
glState.vertexAttribsNewFrame = 0;
glBindBuffer( GL_ARRAY_BUFFER, vbo->vertexesVBO );
backEnd.pc.c_vboVertexBuffers++;
}
}
/*
============
R_BindNullVBO
============
*/
void R_BindNullVBO()
{
GLIMP_LOGCOMMENT( "--- R_BindNullVBO ---" );
if ( glState.currentVBO )
{
glBindBuffer( GL_ARRAY_BUFFER, 0 );
glState.currentVBO = nullptr;
}
GL_CheckErrors();
}
/*
============
R_BindIBO
============
*/
void R_BindIBO( IBO_t *ibo )
{
if ( !ibo )
{
Sys::Drop( "R_BindIBO: NULL ibo" );
}
GLIMP_LOGCOMMENT( "--- R_BindIBO( %s ) ---", ibo->name );
if ( glState.currentIBO != ibo )
{
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ibo->indexesVBO );
glState.currentIBO = ibo;
backEnd.pc.c_vboIndexBuffers++;
}
}
/*
============
R_BindNullIBO
============
*/
void R_BindNullIBO()
{
GLIMP_LOGCOMMENT( "--- R_BindNullIBO ---" );
if ( glState.currentIBO )
{
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
glState.currentIBO = nullptr;
glState.vertexAttribPointersSet = 0;
}
}
static void R_InitGenericVBOs() {
/* Those values are chosen to specify a full-screen rectangle for
screen-space shaders without a projection matrix. The values here
don't influence the triangle but this is a fallback for when the
gl_VertexID-based triangle can't be used (when GL_EXT_gpu_shader4
is missing).
See: https://github.com/DaemonEngine/Daemon/pull/1739 */
// Min and max coordinates of the quad
static const vec3_t min = { -1.0f, -1.0f, 0.0f };
static const vec3_t max = { 1.0f, 1.0f, 0.0f };
{
/*
Quad is a static mesh with 4 vertices and 2 triangles
Verts:
0: -1.0 0.0 0.0
1: -1.0 1.0 0.0
2: 1.0 1.0 0.0
3: 1.0 0.0 0.0
Surfs:
0: 0 2 1 / 0 3 2
*/
drawSurf_t* genericQuad;
genericQuad = ( drawSurf_t* ) ri.Hunk_Alloc( sizeof( *genericQuad ), ha_pref::h_low );
genericQuad->entity = &tr.worldEntity;
srfVBOMesh_t* surface;
surface = ( srfVBOMesh_t* ) ri.Hunk_Alloc( sizeof( *surface ), ha_pref::h_low );
surface->surfaceType = surfaceType_t::SF_VBO_MESH;
surface->numVerts = 4;
surface->numTriangles = 2;
surface->firstIndex = 0;
vec3_t verts[4] = {
{ min[0], min[1], min[2] },
{ min[0], max[1], min[2] },
{ max[0], max[1], min[2] },
{ max[0], min[1], min[2] },
};
vec2_t texCoords[4];
for ( int i = 0; i < 4; i++ ) {
texCoords[i][0] = i < 2 ? 0.0f : 1.0f;
texCoords[i][1] = i > 0 && i < 3 ? 1.0f : 0.0f;
}
Color::Color32Bit color = Color::White;
vertexAttributeSpec_t attrs[] = {
{ ATTR_INDEX_POSITION, GL_FLOAT, GL_FLOAT, verts, 3, sizeof( vec3_t ), 0 },
{ ATTR_INDEX_COLOR, GL_UNSIGNED_BYTE, GL_UNSIGNED_BYTE, color.ToArray(), 4, 0, ATTR_OPTION_NORMALIZE },
{ ATTR_INDEX_TEXCOORD, GL_FLOAT, GL_HALF_FLOAT, texCoords, 2, sizeof( vec2_t ), 0 },
};
surface->vbo = R_CreateStaticVBO( "genericQuad_VBO", std::begin( attrs ), std::end( attrs ), surface->numVerts );
glIndex_t indexes[6] = { 0, 2, 1, 0, 3, 2 }; // Front
surface->ibo = R_CreateStaticIBO( "genericQuad_IBO", indexes, surface->numTriangles * 3 );
genericQuad->surface = ( surfaceType_t* ) surface;
SetupVAOBuffers( surface->vbo, surface->ibo,
ATTR_POSITION | ATTR_COLOR | ATTR_TEXCOORD,
&surface->vbo->VAO );
tr.genericQuad = genericQuad;
}
{
drawSurf_t* genericTriangle;
genericTriangle = ( drawSurf_t* ) ri.Hunk_Alloc( sizeof( *genericTriangle ), ha_pref::h_low );
genericTriangle->entity = &tr.worldEntity;
srfVBOMesh_t* surface = ( srfVBOMesh_t* ) ri.Hunk_Alloc( sizeof( *surface ), ha_pref::h_low );
surface->surfaceType = surfaceType_t::SF_VBO_MESH;
surface->numVerts = 0;
surface->numTriangles = 1;
surface->firstIndex = 0;
surface->vbo = nullptr;
glIndex_t indexes[6] = { 0, 2, 1 }; // Front
surface->ibo = R_CreateStaticIBO( "genericTriangle_IBO", indexes, surface->numTriangles * 3 );
genericTriangle->surface = ( surfaceType_t* ) surface;
tr.genericTriangle = genericTriangle;
}
}
static void R_InitTileVBO()
{
if ( !glConfig2.realtimeLighting )
{
return;
}
R_SyncRenderThread();
int x, y, w, h;
w = tr.depthtile2RenderImage->width;
h = tr.depthtile2RenderImage->height;
auto *xy = ( vec2_t * ) ri.Hunk_AllocateTempMemory( sizeof( vec2_t ) * w * h );
auto *stf = ( vec2_t * ) ri.Hunk_AllocateTempMemory( sizeof( vec2_t ) * w * h );
for (y = 0; y < h; y++ ) {
for (x = 0; x < w; x++ ) {
Vector2Set( xy[ y * w + x ],
(2 * x - w + 1) * (1.0f / w),
(2 * y - h + 1) * (1.0f / h) );
Vector2Set( stf[ y * w + x ],
2 * x * glState.tileStep[ 0 ] + glState.tileStep[ 0 ] - 1.0f,
2 * y * glState.tileStep[ 1 ] + glState.tileStep[ 1 ] - 1.0f );
}
}
vertexAttributeSpec_t attrs[] = {
{ ATTR_INDEX_POSITION, GL_FLOAT, GL_FLOAT, xy, 2, sizeof(vec2_t), 0 },
{ ATTR_INDEX_TEXCOORD, GL_FLOAT, GL_FLOAT, stf, 2, sizeof(vec2_t), 0 },
};
tr.lighttileVBO = R_CreateStaticVBO( "lighttile_VBO", std::begin( attrs ), std::end( attrs ), w * h );
SetupVAOBuffers( tr.lighttileVBO, nullptr,
ATTR_POSITION | ATTR_TEXCOORD,
&tr.lighttileVBO->VAO );
ri.Hunk_FreeTempMemory( stf );
ri.Hunk_FreeTempMemory( xy );
}
const int vertexCapacity = DYN_BUFFER_SIZE / sizeof( shaderVertex_t );
const int indexCapacity = DYN_BUFFER_SIZE / sizeof( glIndex_t );
static void R_InitLightUBO()
{
if ( !glConfig2.realtimeLighting )
{
return;
}
if( glConfig2.uniformBufferObjectAvailable ) {
glGenBuffers( 1, &tr.dlightUBO );
glBindBuffer( GL_UNIFORM_BUFFER, tr.dlightUBO );
glBufferData( GL_UNIFORM_BUFFER, MAX_REF_LIGHTS * sizeof( shaderLight_t ), nullptr, GL_DYNAMIC_DRAW );
glBindBuffer( GL_UNIFORM_BUFFER, 0 );
}
}
/*
============
R_InitVBOs
============
*/
void R_InitVBOs()
{
uint32_t attribs = ATTR_POSITION | ATTR_TEXCOORD | ATTR_QTANGENT | ATTR_COLOR;
Log::Debug("------- R_InitVBOs -------" );
tr.vbos.reserve( 100 );
tr.ibos.reserve( 100 );
tess.vertsBuffer = ( shaderVertex_t * ) Com_Allocate_Aligned( 64, SHADER_MAX_VERTEXES * sizeof( shaderVertex_t ) );
tess.indexesBuffer = ( glIndex_t * ) Com_Allocate_Aligned( 64, SHADER_MAX_INDEXES * sizeof( glIndex_t ) );
if( glConfig2.mapBufferRangeAvailable ) {
tess.vbo = R_CreateDynamicVBO( "tessVertexArray_VBO", vertexCapacity, attribs, vboLayout_t::VBO_LAYOUT_STATIC );
tess.ibo = R_CreateDynamicIBO( "tessVertexArray_IBO", indexCapacity );
tess.vertsWritten = tess.indexesWritten = 0;
} else {
// use glBufferSubData to update VBO
tess.vbo = R_CreateDynamicVBO( "tessVertexArray_VBO", SHADER_MAX_VERTEXES, attribs, vboLayout_t::VBO_LAYOUT_STATIC );
tess.ibo = R_CreateDynamicIBO( "tessVertexArray_IBO", SHADER_MAX_INDEXES );
}
SetupVAOBuffers( tess.vbo, tess.ibo, attribs, &tess.vbo->VAO );
tess.vbo->dynamicVAO = true;
R_InitGenericVBOs();
R_InitTileVBO();
// allocate a PBO for color grade map transfers
glGenBuffers( 1, &tr.colorGradePBO );
glBindBuffer( GL_PIXEL_PACK_BUFFER, tr.colorGradePBO );
glBufferData( GL_PIXEL_PACK_BUFFER,
REF_COLORGRADEMAP_STORE_SIZE * sizeof(u8vec4_t),
nullptr, GL_STREAM_COPY );
glBindBuffer( GL_PIXEL_PACK_BUFFER, 0 );
R_InitLightUBO();
if ( glConfig2.usingMaterialSystem ) {
materialSystem.InitGLBuffers();
}
if ( glConfig2.usingGeometryCache ) {
geometryCache.InitGLBuffers();
}
if ( glConfig2.directStateAccessAvailable && glConfig2.uniformBufferObjectAvailable ) {
stagingBuffer.InitGLBuffer();
}
GL_CheckErrors();
}
/*
============
R_ShutdownVBOs
============
*/
void R_ShutdownVBOs()
{
Log::Debug("------- R_ShutdownVBOs -------" );
if( !glConfig2.mapBufferRangeAvailable ) {
// nothing
} else if( glConfig2.bufferStorageAvailable &&
glConfig2.syncAvailable ) {
R_BindVBO( tess.vbo );
R_ShutdownRingbuffer( GL_ARRAY_BUFFER, &tess.vertexRB );
R_BindIBO( tess.ibo );
R_ShutdownRingbuffer( GL_ELEMENT_ARRAY_BUFFER, &tess.indexRB );
} else {
if( tess.verts != nullptr && tess.verts != tess.vertsBuffer ) {
R_BindVBO( tess.vbo );
glUnmapBuffer( GL_ARRAY_BUFFER );
}
if( tess.indexes != nullptr && tess.indexes != tess.indexesBuffer ) {
R_BindIBO( tess.ibo );
glUnmapBuffer( GL_ELEMENT_ARRAY_BUFFER );
}
}
R_BindNullVBO();
R_BindNullIBO();
glDeleteBuffers( 1, &tr.colorGradePBO );
for ( VBO_t *vbo : tr.vbos )
{
if ( vbo->vertexesVBO )
{
glDeleteBuffers( 1, &vbo->vertexesVBO );
vbo->VAO.DelVAO();
}
}
for ( IBO_t *ibo : tr.ibos )
{
if ( ibo->indexesVBO )
{
glDeleteBuffers( 1, &ibo->indexesVBO );
}
}
tr.vbos.clear();
tr.ibos.clear();
Com_Free_Aligned( tess.vertsBuffer );
Com_Free_Aligned( tess.indexesBuffer );
if( glConfig2.realtimeLighting ) {
glDeleteBuffers( 1, &tr.dlightUBO );
tr.dlightUBO = 0;
}
if ( glConfig2.usingMaterialSystem ) {
materialSystem.FreeGLBuffers();
}
if ( glConfig2.usingGeometryCache ) {
geometryCache.FreeGLBuffers();
}
if ( glConfig2.directStateAccessAvailable && glConfig2.uniformBufferObjectAvailable ) {
stagingBuffer.FreeGLBuffer();
}
tess.verts = tess.vertsBuffer = nullptr;
tess.indexes = tess.indexesBuffer = nullptr;
}
/*
==============
Tess_MapVBOs
Map the default VBOs
Must be called before writing to tess.verts or tess.indexes
==============
*/
void Tess_MapVBOs( bool forceCPU ) {
if( forceCPU || !glConfig2.mapBufferRangeAvailable ) {
// use host buffers
tess.verts = tess.vertsBuffer;
tess.indexes = tess.indexesBuffer;
return;
}
if( tess.verts == nullptr ) {
R_BindVBO( tess.vbo );
if( glConfig2.bufferStorageAvailable &&
glConfig2.syncAvailable ) {
GLsizei segmentEnd = (tess.vertexRB.activeSegment + 1) * tess.vertexRB.segmentElements;
if( tess.vertsWritten + SHADER_MAX_VERTEXES > (unsigned) segmentEnd ) {
tess.vertsWritten = R_RotateRingbuffer( &tess.vertexRB );
}
tess.verts = ( shaderVertex_t * )tess.vertexRB.baseAddr + tess.vertsWritten;
} else {
if( vertexCapacity - tess.vertsWritten < SHADER_MAX_VERTEXES ) {
// buffer is full, allocate a new one
glBufferData( GL_ARRAY_BUFFER, vertexCapacity * sizeof( shaderVertex_t ), nullptr, GL_DYNAMIC_DRAW );
tess.vertsWritten = 0;
}
tess.verts = ( shaderVertex_t *) glMapBufferRange(
GL_ARRAY_BUFFER, tess.vertsWritten * sizeof( shaderVertex_t ),
SHADER_MAX_VERTEXES * sizeof( shaderVertex_t ),
GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT |
GL_MAP_UNSYNCHRONIZED_BIT | GL_MAP_FLUSH_EXPLICIT_BIT );
}
}
if( tess.indexes == nullptr ) {
R_BindIBO( tess.ibo );
if( glConfig2.bufferStorageAvailable &&
glConfig2.syncAvailable ) {
GLsizei segmentEnd = (tess.indexRB.activeSegment + 1) * tess.indexRB.segmentElements;
if( tess.indexesWritten + SHADER_MAX_INDEXES > (unsigned) segmentEnd ) {
tess.indexesWritten = R_RotateRingbuffer( &tess.indexRB );
}
tess.indexes = ( glIndex_t * )tess.indexRB.baseAddr + tess.indexesWritten;
} else {
if( indexCapacity - tess.indexesWritten < SHADER_MAX_INDEXES ) {
// buffer is full, allocate a new one
glBufferData( GL_ELEMENT_ARRAY_BUFFER, indexCapacity * sizeof( glIndex_t ), nullptr, GL_DYNAMIC_DRAW );
tess.indexesWritten = 0;
}
tess.indexes = ( glIndex_t *) glMapBufferRange(
GL_ELEMENT_ARRAY_BUFFER, tess.indexesWritten * sizeof( glIndex_t ),
SHADER_MAX_INDEXES * sizeof( glIndex_t ),
GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT |
GL_MAP_UNSYNCHRONIZED_BIT | GL_MAP_FLUSH_EXPLICIT_BIT );
}
}
}
/*
==============
Tess_UpdateVBOs
Tr3B: update the default VBO to replace the client side vertex arrays
==============
*/
void Tess_UpdateVBOs()
{
GLIMP_LOGCOMMENT( "--- Tess_UpdateVBOs( ) ---" );
GL_CheckErrors();
// update the default VBO
if ( tess.numVertexes > 0 && tess.numVertexes <= SHADER_MAX_VERTEXES )
{
GLsizei size = tess.numVertexes * sizeof( shaderVertex_t );
GL_CheckErrors();
GLIMP_LOGCOMMENT( "glBufferSubData( vbo = '%s', numVertexes = %i )",
tess.vbo->name, tess.numVertexes );
if( !glConfig2.mapBufferRangeAvailable ) {
R_BindVBO( tess.vbo );
glBufferSubData( GL_ARRAY_BUFFER, 0, size, tess.verts );
} else {
R_BindVBO( tess.vbo );
if( glConfig2.bufferStorageAvailable &&
glConfig2.syncAvailable ) {
GLsizei offset = tess.vertexBase * sizeof( shaderVertex_t );
glFlushMappedBufferRange( GL_ARRAY_BUFFER,
offset, size );
} else {
glFlushMappedBufferRange( GL_ARRAY_BUFFER,
0, size );
glUnmapBuffer( GL_ARRAY_BUFFER );
}
tess.vertexBase = tess.vertsWritten;
tess.vertsWritten += tess.numVertexes;
tess.verts = nullptr;
}
}
GL_CheckErrors();
// update the default IBO
if ( tess.numIndexes > 0 && tess.numIndexes <= SHADER_MAX_INDEXES )
{
GLsizei size = tess.numIndexes * sizeof( glIndex_t );
if( !glConfig2.mapBufferRangeAvailable ) {
R_BindIBO( tess.ibo );
glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, 0, size,
tess.indexes );
} else {
R_BindIBO( tess.ibo );
if( glConfig2.bufferStorageAvailable &&
glConfig2.syncAvailable ) {
GLsizei offset = tess.indexBase * sizeof( glIndex_t );
glFlushMappedBufferRange( GL_ELEMENT_ARRAY_BUFFER,
offset, size );
} else {
glFlushMappedBufferRange( GL_ELEMENT_ARRAY_BUFFER,
0, size );
glUnmapBuffer( GL_ELEMENT_ARRAY_BUFFER );
}
tess.indexBase = tess.indexesWritten;
tess.indexesWritten += tess.numIndexes;
tess.indexes = nullptr;
}
}
GL_CheckErrors();
}
class ListVBOsCommand : public Cmd::StaticCmd
{
public:
ListVBOsCommand() : StaticCmd("listVBOs", Cmd::RENDERER, "list VBOs and IBOs") {}
void Run( const Cmd::Args & ) const override
{
int vertexesSize = 0;
int indexesSize = 0;