2 ===========================================================================
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
22 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.
24 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.
26 ===========================================================================
29 // vertex cache calls should only be made by the front end
31 const int NUM_VERTEX_FRAMES = 2;
36 TAG_FIXED, // for the temp buffers
37 TAG_TEMP // in frame temp area, not static area
40 typedef struct vertCache_s {
42 void *virtMem; // only one of vbo / virtMem will be set
43 bool indexBuffer; // holds indexes instead of vertexes
46 int size; // may be larger than the amount asked for, due
47 // to round up and minimum fragment sizes
48 int tag; // a tag of 0 is a free block
49 struct vertCache_s ** user; // will be set to zero when purged
50 struct vertCache_s *next, *prev; // may be on the static list or one of the frame lists
51 int frameUsed; // it can't be purged if near the current frame
60 // just for gfxinfo printing
63 // called when vertex programs are enabled or disabled, because
64 // the cached data is no longer valid
67 // Tries to allocate space for the given data in fast vertex
68 // memory, and copies it over.
69 // Alloc does NOT do a touch, which allows purging of things
70 // created at level load time even if a frame hasn't passed yet.
71 // These allocations can be purged, which will zero the pointer.
72 void Alloc( void *data, int bytes, vertCache_t **buffer, bool indexBuffer = false );
74 // This will be a real pointer with virtual memory,
75 // but it will be an int offset cast to a pointer of ARB_vertex_buffer_object
76 void * Position( vertCache_t *buffer );
78 // if r_useIndexBuffers is enabled, but you need to draw something without
79 // an indexCache, this must be called to reset GL_ELEMENT_ARRAY_BUFFER_ARB
82 // automatically freed at the end of the next frame
83 // used for specular texture coordinates and gui drawing, which
84 // will change every frame.
85 // will return NULL if the vertex cache is completely full
86 // As with Position(), this may not actually be a pointer you can access.
87 vertCache_t * AllocFrameTemp( void *data, int bytes );
89 // notes that a buffer is used this frame, so it can't be purged
90 // out from under the GPU
91 void Touch( vertCache_t *buffer );
93 // this block won't have to zero a buffer pointer when it is purged,
94 // but it must still wait for the frames to pass, in case the GPU
95 // is still referencing it
96 void Free( vertCache_t *buffer );
98 // updates the counter for determining which temp space to use
99 // and which blocks can be purged
100 // Also prints debugging info when enabled
103 // listVertexCache calls this
107 void InitMemoryBlocks( int size );
108 void ActuallyFree( vertCache_t *block );
110 static idCVar r_showVertexCache;
111 static idCVar r_vertexBufferMegs;
113 int staticCountTotal;
114 int staticAllocTotal; // for end of frame purging
116 int staticAllocThisFrame; // debug counter
117 int staticCountThisFrame;
118 int dynamicAllocThisFrame;
119 int dynamicCountThisFrame;
121 int currentFrame; // for purgable block tracking
122 int listNum; // currentFrame % NUM_VERTEX_FRAMES, determines which tempBuffers to use
124 bool virtualMemory; // not fast stuff
126 bool allocatingTempBuffer; // force GL_STREAM_DRAW_ARB
128 vertCache_t *tempBuffers[NUM_VERTEX_FRAMES]; // allocated at startup
129 bool tempOverflow; // had to alloc a temp in static memory
131 idBlockAlloc<vertCache_t,1024> headerAllocator;
133 vertCache_t freeStaticHeaders; // head of doubly linked list
134 vertCache_t freeDynamicHeaders; // head of doubly linked list
135 vertCache_t dynamicHeaders; // head of doubly linked list
136 vertCache_t deferredFreeList; // head of doubly linked list
137 vertCache_t staticHeaders; // head of doubly linked list in MRU order,
138 // staticHeaders.next is most recently used
140 int frameBytes; // for each of NUM_VERTEX_FRAMES frames
143 extern idVertexCache vertexCache;