Merge 'remotes/trunk'
[0ad.git] / source / renderer / VertexBufferManager.cpp
blob0efed3ff1913efad96086bfd52db8fd5ee827a28
1 /* Copyright (C) 2011 Wildfire Games.
2 * This file is part of 0 A.D.
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
19 * Allocate and destroy CVertexBuffers
22 #include "precompiled.h"
24 #include "VertexBufferManager.h"
26 #include "lib/ogl.h"
27 #include "ps/CLogger.h"
29 #define DUMP_VB_STATS 0 // for debugging
31 CVertexBufferManager g_VBMan;
33 ///////////////////////////////////////////////////////////////////////////////
34 // Explicit shutdown of the vertex buffer subsystem.
35 // This avoids the ordering issues that arise when using destructors of
36 // global instances.
37 void CVertexBufferManager::Shutdown()
39 typedef std::list<CVertexBuffer*>::iterator Iter;
40 for (Iter iter = m_Buffers.begin(); iter != m_Buffers.end(); ++iter)
41 delete *iter;
42 m_Buffers.clear();
46 ///////////////////////////////////////////////////////////////////////////////
47 // Allocate: try to allocate a buffer of given number of vertices (each of
48 // given size), with the given type, and using the given texture - return null
49 // if no free chunks available
50 CVertexBuffer::VBChunk* CVertexBufferManager::Allocate(size_t vertexSize, size_t numVertices, GLenum usage, GLenum target, void* backingStore)
52 CVertexBuffer::VBChunk* result=0;
54 ENSURE(usage == GL_STREAM_DRAW || usage == GL_STATIC_DRAW || usage == GL_DYNAMIC_DRAW);
56 ENSURE(target == GL_ARRAY_BUFFER || target == GL_ELEMENT_ARRAY_BUFFER);
58 if (CVertexBuffer::UseStreaming(usage))
59 ENSURE(backingStore != NULL);
61 // TODO, RC - run some sanity checks on allocation request
63 typedef std::list<CVertexBuffer*>::iterator Iter;
65 #if DUMP_VB_STATS
66 debug_printf("\n============================\n# allocate vsize=%d nverts=%d\n\n", vertexSize, numVertices);
67 for (Iter iter = m_Buffers.begin(); iter != m_Buffers.end(); ++iter) {
68 CVertexBuffer* buffer = *iter;
69 if (buffer->CompatibleVertexType(vertexSize, usage, target))
71 debug_printf("%p\n", buffer);
72 buffer->DumpStatus();
75 #endif
77 // iterate through all existing buffers testing for one that'll
78 // satisfy the allocation
79 for (Iter iter = m_Buffers.begin(); iter != m_Buffers.end(); ++iter) {
80 CVertexBuffer* buffer = *iter;
81 result = buffer->Allocate(vertexSize, numVertices, usage, target, backingStore);
82 if (result)
83 return result;
86 // got this far; need to allocate a new buffer
87 CVertexBuffer* buffer = new CVertexBuffer(vertexSize, usage, target);
88 m_Buffers.push_front(buffer);
89 result = buffer->Allocate(vertexSize, numVertices, usage, target, backingStore);
91 if (!result)
93 LOGERROR("Failed to create VBOs (%lu*%lu)", (unsigned long)vertexSize, (unsigned long)numVertices);
96 return result;
99 ///////////////////////////////////////////////////////////////////////////////
100 // Release: return given chunk to its owner
101 void CVertexBufferManager::Release(CVertexBuffer::VBChunk* chunk)
103 ENSURE(chunk);
104 #if DUMP_VB_STATS
105 debug_printf("\n============================\n# release %p nverts=%d\n\n", chunk, chunk->m_Count);
106 #endif
107 chunk->m_Owner->Release(chunk);
111 size_t CVertexBufferManager::GetBytesReserved()
113 size_t total = 0;
115 typedef std::list<CVertexBuffer*>::iterator Iter;
116 for (Iter iter = m_Buffers.begin(); iter != m_Buffers.end(); ++iter)
117 total += (*iter)->GetBytesReserved();
119 return total;
122 size_t CVertexBufferManager::GetBytesAllocated()
124 size_t total = 0;
126 typedef std::list<CVertexBuffer*>::iterator Iter;
127 for (Iter iter = m_Buffers.begin(); iter != m_Buffers.end(); ++iter)
128 total += (*iter)->GetBytesAllocated();
130 return total;