/
VertexCache.cpp
568 lines (470 loc) · 15.8 KB
/
VertexCache.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
/*
===========================================================================
Doom 3 GPL Source Code
Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
Copyright (C) 2022 Hugues Nouvel
This file is part of the Doom 3 GPL Source Code ("Doom 3 Source Code").
Doom 3 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 3 of the License, or
(at your option) any later version.
Doom 3 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 Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
//hunoppc 2022
#include "../framework/CVarSystem.h"
#include "../renderer/qgl.h"
#include "../framework/Common.h"
#include "../renderer/tr_local.h"
#include "../renderer/VertexCache.h"
static const int FRAME_MEMORY_BYTES = 0x200000;
static const int EXPAND_HEADERS = 1024;
idCVar idVertexCache::r_showVertexCache( "r_showVertexCache", "0", CVAR_INTEGER|CVAR_RENDERER, "" );
idCVar idVertexCache::r_vertexBufferMegs( "r_vertexBufferMegs", "48", CVAR_INTEGER|CVAR_RENDERER, "" );
idVertexCache vertexCache;
/*
==============
R_ListVertexCache_f
==============
*/
static void R_ListVertexCache_f( const idCmdArgs &args ) {
vertexCache.List();
}
/*
==============
idVertexCache::ActuallyFree
==============
*/
void idVertexCache::ActuallyFree( vertCache_t *block ) {
if (!block) {
common->Error( "idVertexCache Free: NULL pointer" );
}
if ( block->user ) {
// let the owner know we have purged it
*block->user = NULL;
block->user = NULL;
}
// temp blocks are in a shared space that won't be freed
if ( block->tag != TAG_TEMP ) {
staticAllocTotal -= block->size;
staticCountTotal--;
if ( block->vbo ) {
//NULL
} else if ( block->virtMem ) {
Mem_Free( block->virtMem );
block->virtMem = NULL;
}
}
block->tag = TAG_FREE; // mark as free
// unlink stick it back on the free list
block->next->prev = block->prev;
block->prev->next = block->next;
#if 1
// stick it on the front of the free list so it will be reused immediately
block->next = freeStaticHeaders.next;
block->prev = &freeStaticHeaders;
#else
// stick it on the back of the free list so it won't be reused soon (just for debugging)
block->next = &freeStaticHeaders;
block->prev = freeStaticHeaders.prev;
#endif
block->next->prev = block;
block->prev->next = block;
}
/*
==============
idVertexCache::Position
this will be a real pointer with virtual memory,
but it will be an int offset cast to a pointer with
ARB_vertex_buffer_object
The ARB_vertex_buffer_object will be bound
==============
*/
void *idVertexCache::Position( vertCache_t *buffer ) {
if ( !buffer || buffer->tag == TAG_FREE ) {
common->FatalError( "idVertexCache::Position: bad vertCache_t" );
}
// the ARB vertex object just uses an offset
if ( buffer->vbo ) {
if ( r_showVertexCache.GetInteger() == 2 ) {
if ( buffer->tag == TAG_TEMP ) {
common->Printf( "GL_ARRAY_BUFFER_ARB = %i + %zd (%i bytes)\n", buffer->vbo, buffer->offset, buffer->size );
} else {
common->Printf( "GL_ARRAY_BUFFER_ARB = %i (%i bytes)\n", buffer->vbo, buffer->size );
}
}
if ( buffer->indexBuffer ) {
qglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, buffer->vbo );
} else {
qglBindBufferARB( GL_ARRAY_BUFFER_ARB, buffer->vbo );
}
return (void *)buffer->offset;
}
// virtual memory is a real pointer
return (void *)((byte *)buffer->virtMem + buffer->offset);
}
void idVertexCache::UnbindIndex() {
qglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
}
//================================================================================
/*
===========
idVertexCache::Init
===========
*/
void idVertexCache::Init() {
cmdSystem->AddCommand( "listVertexCache", R_ListVertexCache_f, CMD_FL_RENDERER, "lists vertex cache" );
if ( r_vertexBufferMegs.GetInteger() < 8 ) {
r_vertexBufferMegs.SetInteger( 8 );
}
virtualMemory = false;
// use ARB_vertex_buffer_object unless explicitly disabled
if( r_useVertexBuffers.GetInteger() && glConfig.ARBVertexBufferObjectAvailable ) {
common->Printf( "using ARB_vertex_buffer_object memory\n" );
} else {
virtualMemory = true;
r_useIndexBuffers.SetBool( false );
common->Printf( "WARNING: vertex array range in virtual memory (SLOW)\n" );
}
// initialize the cache memory blocks
freeStaticHeaders.next = freeStaticHeaders.prev = &freeStaticHeaders;
staticHeaders.next = staticHeaders.prev = &staticHeaders;
freeDynamicHeaders.next = freeDynamicHeaders.prev = &freeDynamicHeaders;
dynamicHeaders.next = dynamicHeaders.prev = &dynamicHeaders;
deferredFreeList.next = deferredFreeList.prev = &deferredFreeList;
// set up the dynamic frame memory
frameBytes = FRAME_MEMORY_BYTES;
staticAllocTotal = 0;
byte *junk = (byte *)Mem_Alloc( frameBytes );
for ( int i = 0 ; i < NUM_VERTEX_FRAMES ; i++ ) {
allocatingTempBuffer = true; // force the alloc to use GL_STREAM_DRAW_ARB
Alloc( junk, frameBytes, &tempBuffers[i] );
allocatingTempBuffer = false;
tempBuffers[i]->tag = TAG_FIXED;
// unlink these from the static list, so they won't ever get purged
tempBuffers[i]->next->prev = tempBuffers[i]->prev;
tempBuffers[i]->prev->next = tempBuffers[i]->next;
}
Mem_Free( junk );
EndFrame();
}
/*
===========
idVertexCache::PurgeAll
Used when toggling vertex programs on or off, because
the cached data isn't valid
===========
*/
void idVertexCache::PurgeAll() {
while( staticHeaders.next != &staticHeaders ) {
ActuallyFree( staticHeaders.next );
}
}
/*
===========
idVertexCache::Shutdown
===========
*/
void idVertexCache::Shutdown() {
// PurgeAll(); // !@#: also purge the temp buffers
headerAllocator.Shutdown();
}
/*
===========
idVertexCache::Alloc
===========
*/
void idVertexCache::Alloc( void *data, int size, vertCache_t **buffer, bool indexBuffer ) {
vertCache_t *block;
if ( size <= 0 ) {
common->Error( "idVertexCache::Alloc: size = %i\n", size );
}
// if we can't find anything, it will be NULL
*buffer = NULL;
// if we don't have any remaining unused headers, allocate some more
if ( freeStaticHeaders.next == &freeStaticHeaders ) {
for ( int i = 0; i < EXPAND_HEADERS; i++ ) {
block = headerAllocator.Alloc();
block->next = freeStaticHeaders.next;
block->prev = &freeStaticHeaders;
block->next->prev = block;
block->prev->next = block;
if( !virtualMemory ) {
qglGenBuffersARB( 1, & block->vbo );
}
}
}
// move it from the freeStaticHeaders list to the staticHeaders list
block = freeStaticHeaders.next;
block->next->prev = block->prev;
block->prev->next = block->next;
block->next = staticHeaders.next;
block->prev = &staticHeaders;
block->next->prev = block;
block->prev->next = block;
block->size = size;
block->offset = 0;
block->tag = TAG_USED;
// save data for debugging
staticAllocThisFrame += block->size;
staticCountThisFrame++;
staticCountTotal++;
staticAllocTotal += block->size;
// this will be set to zero when it is purged
block->user = buffer;
*buffer = block;
// allocation doesn't imply used-for-drawing, because at level
// load time lots of things may be created, but they aren't
// referenced by the GPU yet, and can be purged if needed.
block->frameUsed = currentFrame - NUM_VERTEX_FRAMES;
block->indexBuffer = indexBuffer;
// copy the data
if ( block->vbo ) {
if ( indexBuffer ) {
//Added by HunoPPC for VBO
qglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, block->vbo );
qglBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, (GLsizeiptrARB)size, data, GL_STATIC_DRAW_ARB );
qglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
} else {
qglBindBufferARB( GL_ARRAY_BUFFER_ARB, block->vbo );
if ( allocatingTempBuffer ) {
//Added by HunoPPC for VBO
qglBufferDataARB( GL_ARRAY_BUFFER_ARB, (GLsizeiptrARB)size, data, GL_STREAM_DRAW_ARB );
qglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0);
} else {
//Added by HunoPPC for VBO
qglBufferDataARB( GL_ARRAY_BUFFER_ARB, (GLsizeiptrARB)size, data, GL_STATIC_DRAW_ARB );
qglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0);
}
}
} else {
block->virtMem = Mem_Alloc( size );
SIMDProcessor->Memcpy( block->virtMem, data, size );
}
}
/*
===========
idVertexCache::Touch
===========
*/
void idVertexCache::Touch( vertCache_t *block ) {
if ( !block ) {
common->Error( "idVertexCache Touch: NULL pointer" );
}
if ( block->tag == TAG_FREE ) {
common->FatalError( "idVertexCache Touch: freed pointer" );
}
if ( block->tag == TAG_TEMP ) {
common->FatalError( "idVertexCache Touch: temporary pointer" );
}
block->frameUsed = currentFrame;
// move to the head of the LRU list
block->next->prev = block->prev;
block->prev->next = block->next;
block->next = staticHeaders.next;
block->prev = &staticHeaders;
staticHeaders.next->prev = block;
staticHeaders.next = block;
}
/*
===========
idVertexCache::Free
===========
*/
void idVertexCache::Free( vertCache_t *block ) {
if (!block) {
return;
}
if ( block->tag == TAG_FREE ) {
common->FatalError( "idVertexCache Free: freed pointer" );
}
if ( block->tag == TAG_TEMP ) {
common->FatalError( "idVertexCache Free: temporary pointer" );
}
// this block still can't be purged until the frame count has expired,
// but it won't need to clear a user pointer when it is
block->user = NULL;
block->next->prev = block->prev;
block->prev->next = block->next;
block->next = deferredFreeList.next;
block->prev = &deferredFreeList;
deferredFreeList.next->prev = block;
deferredFreeList.next = block;
}
/*
===========
idVertexCache::AllocFrameTemp
A frame temp allocation must never be allowed to fail due to overflow.
We can't simply sync with the GPU and overwrite what we have, because
there may still be future references to dynamically created surfaces.
===========
*/
vertCache_t *idVertexCache::AllocFrameTemp( void *data, int size ) {
vertCache_t *block;
if ( size <= 0 ) {
common->Error( "idVertexCache::AllocFrameTemp: size = %i\n", size );
}
if ( dynamicAllocThisFrame + size > frameBytes ) {
// if we don't have enough room in the temp block, allocate a static block,
// but immediately free it so it will get freed at the next frame
tempOverflow = true;
Alloc( data, size, &block );
Free( block);
return block;
}
// this data is just going on the shared dynamic list
// if we don't have any remaining unused headers, allocate some more
if ( freeDynamicHeaders.next == &freeDynamicHeaders ) {
for ( int i = 0; i < EXPAND_HEADERS; i++ ) {
block = headerAllocator.Alloc();
block->next = freeDynamicHeaders.next;
block->prev = &freeDynamicHeaders;
block->next->prev = block;
block->prev->next = block;
}
}
// move it from the freeDynamicHeaders list to the dynamicHeaders list
block = freeDynamicHeaders.next;
block->next->prev = block->prev;
block->prev->next = block->next;
block->next = dynamicHeaders.next;
block->prev = &dynamicHeaders;
block->next->prev = block;
block->prev->next = block;
block->size = size;
block->tag = TAG_TEMP;
block->indexBuffer = false;
block->offset = dynamicAllocThisFrame;
dynamicAllocThisFrame += block->size;
dynamicCountThisFrame++;
block->user = NULL;
block->frameUsed = 0;
// copy the data
block->virtMem = tempBuffers[listNum]->virtMem;
block->vbo = tempBuffers[listNum]->vbo;
if ( block->vbo ) {
//Added by HunoPPC for VBO
qglBindBufferARB( GL_ARRAY_BUFFER_ARB, block->vbo );
qglBufferSubDataARB( GL_ARRAY_BUFFER_ARB, block->offset, (GLsizeiptrARB)size, data );
qglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0);
} else {
SIMDProcessor->Memcpy( (byte *)block->virtMem + block->offset, data, size );
}
return block;
}
/*
===========
idVertexCache::EndFrame
===========
*/
void idVertexCache::EndFrame() {
// display debug information
if ( r_showVertexCache.GetBool() ) {
int staticUseCount = 0;
int staticUseSize = 0;
for ( vertCache_t *block = staticHeaders.next ; block != &staticHeaders ; block = block->next ) {
if ( block->frameUsed == currentFrame ) {
staticUseCount++;
staticUseSize += block->size;
}
}
const char *frameOverflow = tempOverflow ? "(OVERFLOW)" : "";
common->Printf( "vertex dynamic:%i=%ik%s, static alloc:%i=%ik used:%i=%ik total:%i=%ik\n",
dynamicCountThisFrame, dynamicAllocThisFrame/1024, frameOverflow,
staticCountThisFrame, staticAllocThisFrame/1024,
staticUseCount, staticUseSize/1024,
staticCountTotal, staticAllocTotal/1024 );
}
#if 0
// if our total static count is above our working memory limit, start purging things
while ( staticAllocTotal > r_vertexBufferMegs.GetInteger() * 1024 * 1024 ) {
// free the least recently used
}
#endif
if( !virtualMemory ) {
// unbind vertex buffers so normal virtual memory will be used in case
// r_useVertexBuffers / r_useIndexBuffers
qglBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 );
qglBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 );
}
currentFrame = tr.frameCount;
listNum = currentFrame % NUM_VERTEX_FRAMES;
staticAllocThisFrame = 0;
staticCountThisFrame = 0;
dynamicAllocThisFrame = 0;
dynamicCountThisFrame = 0;
tempOverflow = false;
// free all the deferred free headers
while( deferredFreeList.next != &deferredFreeList ) {
ActuallyFree( deferredFreeList.next );
}
// free all the frame temp headers
vertCache_t *block = dynamicHeaders.next;
if ( block != &dynamicHeaders ) {
block->prev = &freeDynamicHeaders;
dynamicHeaders.prev->next = freeDynamicHeaders.next;
freeDynamicHeaders.next->prev = dynamicHeaders.prev;
freeDynamicHeaders.next = block;
dynamicHeaders.next = dynamicHeaders.prev = &dynamicHeaders;
}
}
/*
=============
idVertexCache::List
=============
*/
void idVertexCache::List( void ) {
int numActive = 0;
int frameStatic = 0;
int totalStatic = 0;
vertCache_t *block;
for ( block = staticHeaders.next ; block != &staticHeaders ; block = block->next) {
numActive++;
totalStatic += block->size;
if ( block->frameUsed == currentFrame ) {
frameStatic += block->size;
}
}
int numFreeStaticHeaders = 0;
for ( block = freeStaticHeaders.next ; block != &freeStaticHeaders ; block = block->next ) {
numFreeStaticHeaders++;
}
int numFreeDynamicHeaders = 0;
for ( block = freeDynamicHeaders.next ; block != &freeDynamicHeaders ; block = block->next ) {
numFreeDynamicHeaders++;
}
common->Printf( "%i megs working set\n", r_vertexBufferMegs.GetInteger() );
common->Printf( "%i dynamic temp buffers of %ik\n", NUM_VERTEX_FRAMES, frameBytes / 1024 );
common->Printf( "%5i active static headers\n", numActive );
common->Printf( "%5i free static headers\n", numFreeStaticHeaders );
common->Printf( "%5i free dynamic headers\n", numFreeDynamicHeaders );
if ( !virtualMemory ) {
common->Printf( "Vertex cache is in ARB_vertex_buffer_object memory (FAST).\n");
} else {
common->Printf( "Vertex cache is in virtual memory (SLOW)\n" );
}
if ( r_useIndexBuffers.GetBool() ) {
common->Printf( "Index buffers are accelerated.\n" );
} else {
common->Printf( "Index buffers are not used.\n" );
}
}
/*
=============
idVertexCache::IsFast
just for gfxinfo printing
=============
*/
bool idVertexCache::IsFast() {
if ( virtualMemory ) {
return false;
}
return true;
}