Merge 'remotes/trunk'
[0ad.git] / source / renderer / VertexBufferManager.cpp
blobd9ada2f5e8c2242b9b949cdd57bfd8b4f19c3ce8
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)
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 // TODO, RC - run some sanity checks on allocation request
60 typedef std::list<CVertexBuffer*>::iterator Iter;
62 #if DUMP_VB_STATS
63 debug_printf(L"\n============================\n# allocate vsize=%d nverts=%d\n\n", vertexSize, numVertices);
64 for (Iter iter = m_Buffers.begin(); iter != m_Buffers.end(); ++iter) {
65 CVertexBuffer* buffer = *iter;
66 if (buffer->CompatibleVertexType(vertexSize, usage, target))
68 debug_printf(L"%p\n", buffer);
69 buffer->DumpStatus();
72 #endif
74 // iterate through all existing buffers testing for one that'll
75 // satisfy the allocation
76 for (Iter iter = m_Buffers.begin(); iter != m_Buffers.end(); ++iter) {
77 CVertexBuffer* buffer = *iter;
78 result = buffer->Allocate(vertexSize, numVertices, usage, target);
79 if (result)
80 return result;
83 // got this far; need to allocate a new buffer
84 CVertexBuffer* buffer = new CVertexBuffer(vertexSize, usage, target);
85 m_Buffers.push_front(buffer);
86 result = buffer->Allocate(vertexSize, numVertices, usage, target);
88 if (!result)
90 LOGERROR(L"Failed to create VBOs (%lu*%lu)", (unsigned long)vertexSize, (unsigned long)numVertices);
93 return result;
96 ///////////////////////////////////////////////////////////////////////////////
97 // Release: return given chunk to its owner
98 void CVertexBufferManager::Release(CVertexBuffer::VBChunk* chunk)
100 ENSURE(chunk);
101 #if DUMP_VB_STATS
102 debug_printf(L"\n============================\n# release %p nverts=%d\n\n", chunk, chunk->m_Count);
103 #endif
104 chunk->m_Owner->Release(chunk);
108 size_t CVertexBufferManager::GetBytesReserved()
110 size_t total = 0;
112 typedef std::list<CVertexBuffer*>::iterator Iter;
113 for (Iter iter = m_Buffers.begin(); iter != m_Buffers.end(); ++iter)
114 total += (*iter)->GetBytesReserved();
116 return total;
119 size_t CVertexBufferManager::GetBytesAllocated()
121 size_t total = 0;
123 typedef std::list<CVertexBuffer*>::iterator Iter;
124 for (Iter iter = m_Buffers.begin(); iter != m_Buffers.end(); ++iter)
125 total += (*iter)->GetBytesAllocated();
127 return total;