]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/renderer/VertexCache.h
Use the same OpenAL headers on all platforms.
[icculus/iodoom3.git] / neo / renderer / VertexCache.h
1 /*
2 ===========================================================================
3
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company. 
6
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).  
8
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.
13
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.
18
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/>.
21
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.
23
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.
25
26 ===========================================================================
27 */
28
29 // vertex cache calls should only be made by the front end
30
31 const int NUM_VERTEX_FRAMES = 2;
32
33 typedef enum {
34         TAG_FREE,
35         TAG_USED,
36         TAG_FIXED,              // for the temp buffers
37         TAG_TEMP                // in frame temp area, not static area
38 } vertBlockTag_t;
39
40 typedef struct vertCache_s {
41         GLuint                  vbo;
42         void                    *virtMem;                       // only one of vbo / virtMem will be set
43         bool                    indexBuffer;            // holds indexes instead of vertexes
44
45         int                             offset;
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
52 } vertCache_t;
53
54
55 class idVertexCache {
56 public:
57         void                    Init();
58         void                    Shutdown();
59
60         // just for gfxinfo printing
61         bool                    IsFast();
62
63         // called when vertex programs are enabled or disabled, because
64         // the cached data is no longer valid
65         void                    PurgeAll();
66
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 );
73
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 );
77
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
80         void                    UnbindIndex();
81
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 );
88
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 );
92
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 );    
97
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
101         void                    EndFrame();
102
103         // listVertexCache calls this
104         void                    List();
105
106 private:
107         void                    InitMemoryBlocks( int size );
108         void                    ActuallyFree( vertCache_t *block );
109
110         static idCVar   r_showVertexCache;
111         static idCVar   r_vertexBufferMegs;
112
113         int                             staticCountTotal;
114         int                             staticAllocTotal;               // for end of frame purging
115
116         int                             staticAllocThisFrame;   // debug counter
117         int                             staticCountThisFrame;
118         int                             dynamicAllocThisFrame;
119         int                             dynamicCountThisFrame;
120
121         int                             currentFrame;                   // for purgable block tracking
122         int                             listNum;                                // currentFrame % NUM_VERTEX_FRAMES, determines which tempBuffers to use
123
124         bool                    virtualMemory;                  // not fast stuff
125
126         bool                    allocatingTempBuffer;   // force GL_STREAM_DRAW_ARB
127
128         vertCache_t             *tempBuffers[NUM_VERTEX_FRAMES];                // allocated at startup
129         bool                    tempOverflow;                   // had to alloc a temp in static memory
130
131         idBlockAlloc<vertCache_t,1024>  headerAllocator;
132
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
139
140         int                             frameBytes;                             // for each of NUM_VERTEX_FRAMES frames
141 };
142
143 extern  idVertexCache   vertexCache;