Merge 'remotes/trunk'
[0ad.git] / source / renderer / VertexBuffer.cpp
blob2d190e0bb60ea1a541de58916fd3840069456b7d
1 /* Copyright (C) 2016 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 * encapsulation of VBOs with sharing
22 #include "precompiled.h"
23 #include "ps/Errors.h"
24 #include "lib/ogl.h"
25 #include "lib/sysdep/cpu.h"
26 #include "Renderer.h"
27 #include "VertexBuffer.h"
28 #include "VertexBufferManager.h"
29 #include "ps/CLogger.h"
31 // Absolute maximum (bytewise) size of each GL vertex buffer object.
32 // Make it large enough for the maximum feasible mesh size (64K vertexes,
33 // 64 bytes per vertex in InstancingModelRenderer).
34 // TODO: measure what influence this has on performance
35 #define MAX_VB_SIZE_BYTES (4*1024*1024)
37 CVertexBuffer::CVertexBuffer(size_t vertexSize, GLenum usage, GLenum target)
38 : m_VertexSize(vertexSize), m_Handle(0), m_SysMem(0), m_Usage(usage), m_Target(target)
40 size_t size = MAX_VB_SIZE_BYTES;
42 if (target == GL_ARRAY_BUFFER) // vertex data buffer
44 // We want to store 16-bit indices to any vertex in a buffer, so the
45 // buffer must never be bigger than vertexSize*64K bytes since we can
46 // address at most 64K of them with 16-bit indices
47 size = std::min(size, vertexSize*65536);
50 // store max/free vertex counts
51 m_MaxVertices = m_FreeVertices = size / vertexSize;
53 // allocate raw buffer
54 if (g_Renderer.m_Caps.m_VBO)
56 pglGenBuffersARB(1, &m_Handle);
57 pglBindBufferARB(m_Target, m_Handle);
58 pglBufferDataARB(m_Target, m_MaxVertices * m_VertexSize, 0, m_Usage);
59 pglBindBufferARB(m_Target, 0);
61 else
63 m_SysMem = new u8[m_MaxVertices * m_VertexSize];
66 // create sole free chunk
67 VBChunk* chunk = new VBChunk;
68 chunk->m_Owner = this;
69 chunk->m_Count = m_FreeVertices;
70 chunk->m_Index = 0;
71 m_FreeList.push_front(chunk);
74 CVertexBuffer::~CVertexBuffer()
76 // Must have released all chunks before destroying the buffer
77 ENSURE(m_AllocList.empty());
79 if (m_Handle)
80 pglDeleteBuffersARB(1, &m_Handle);
82 delete[] m_SysMem;
84 typedef std::list<VBChunk*>::iterator Iter;
85 for (Iter iter = m_FreeList.begin(); iter != m_FreeList.end(); ++iter)
86 delete *iter;
90 bool CVertexBuffer::CompatibleVertexType(size_t vertexSize, GLenum usage, GLenum target)
92 if (usage != m_Usage || target != m_Target || vertexSize != m_VertexSize)
93 return false;
95 return true;
98 ///////////////////////////////////////////////////////////////////////////////
99 // Allocate: try to allocate a buffer of given number of vertices (each of
100 // given size), with the given type, and using the given texture - return null
101 // if no free chunks available
102 CVertexBuffer::VBChunk* CVertexBuffer::Allocate(size_t vertexSize, size_t numVertices, GLenum usage, GLenum target, void* backingStore)
104 // check this is the right kind of buffer
105 if (!CompatibleVertexType(vertexSize, usage, target))
106 return 0;
108 if (UseStreaming(usage))
109 ENSURE(backingStore != NULL);
111 // quick check there's enough vertices spare to allocate
112 if (numVertices > m_FreeVertices)
113 return 0;
115 // trawl free list looking for first free chunk with enough space
116 VBChunk* chunk = 0;
117 typedef std::list<VBChunk*>::iterator Iter;
118 for (Iter iter = m_FreeList.begin(); iter != m_FreeList.end(); ++iter) {
119 if (numVertices <= (*iter)->m_Count) {
120 chunk = *iter;
121 // remove this chunk from the free list
122 m_FreeList.erase(iter);
123 m_FreeVertices -= chunk->m_Count;
124 // no need to search further ..
125 break;
129 if (!chunk) {
130 // no big enough spare chunk available
131 return 0;
134 chunk->m_BackingStore = backingStore;
135 chunk->m_Dirty = false;
136 chunk->m_Needed = false;
138 // split chunk into two; - allocate a new chunk using all unused vertices in the
139 // found chunk, and add it to the free list
140 if (chunk->m_Count > numVertices)
142 VBChunk* newchunk = new VBChunk;
143 newchunk->m_Owner = this;
144 newchunk->m_Count = chunk->m_Count - numVertices;
145 newchunk->m_Index = chunk->m_Index + numVertices;
146 m_FreeList.push_front(newchunk);
147 m_FreeVertices += newchunk->m_Count;
149 // resize given chunk
150 chunk->m_Count = numVertices;
153 // return found chunk
154 m_AllocList.push_back(chunk);
155 return chunk;
158 ///////////////////////////////////////////////////////////////////////////////
159 // Release: return given chunk to this buffer
160 void CVertexBuffer::Release(VBChunk* chunk)
162 // Update total free count before potentially modifying this chunk's count
163 m_FreeVertices += chunk->m_Count;
165 m_AllocList.remove(chunk);
167 typedef std::list<VBChunk*>::iterator Iter;
169 // Coalesce with any free-list items that are adjacent to this chunk;
170 // merge the found chunk with the new one, and remove the old one
171 // from the list, and repeat until no more are found
172 bool coalesced;
175 coalesced = false;
176 for (Iter iter = m_FreeList.begin(); iter != m_FreeList.end(); ++iter)
178 if ((*iter)->m_Index == chunk->m_Index + chunk->m_Count
179 || (*iter)->m_Index + (*iter)->m_Count == chunk->m_Index)
181 chunk->m_Index = std::min(chunk->m_Index, (*iter)->m_Index);
182 chunk->m_Count += (*iter)->m_Count;
183 delete *iter;
184 m_FreeList.erase(iter);
185 coalesced = true;
186 break;
190 while (coalesced);
192 m_FreeList.push_front(chunk);
195 ///////////////////////////////////////////////////////////////////////////////
196 // UpdateChunkVertices: update vertex data for given chunk
197 void CVertexBuffer::UpdateChunkVertices(VBChunk* chunk, void* data)
199 if (g_Renderer.m_Caps.m_VBO)
201 ENSURE(m_Handle);
202 if (UseStreaming(m_Usage))
204 // The VBO is now out of sync with the backing store
205 chunk->m_Dirty = true;
207 // Sanity check: Make sure the caller hasn't tried to reallocate
208 // their backing store
209 ENSURE(data == chunk->m_BackingStore);
211 else
213 pglBindBufferARB(m_Target, m_Handle);
214 pglBufferSubDataARB(m_Target, chunk->m_Index * m_VertexSize, chunk->m_Count * m_VertexSize, data);
215 pglBindBufferARB(m_Target, 0);
218 else
220 ENSURE(m_SysMem);
221 memcpy(m_SysMem + chunk->m_Index * m_VertexSize, data, chunk->m_Count * m_VertexSize);
225 ///////////////////////////////////////////////////////////////////////////////
226 // Bind: bind to this buffer; return pointer to address required as parameter
227 // to glVertexPointer ( + etc) calls
228 u8* CVertexBuffer::Bind()
230 if (!g_Renderer.m_Caps.m_VBO)
231 return m_SysMem;
233 pglBindBufferARB(m_Target, m_Handle);
235 if (UseStreaming(m_Usage))
237 // If any chunks are out of sync with the current VBO, and are
238 // needed for rendering this frame, we'll need to re-upload the VBO
239 bool needUpload = false;
240 for (VBChunk* const& chunk : m_AllocList)
242 if (chunk->m_Dirty && chunk->m_Needed)
244 needUpload = true;
245 break;
249 if (needUpload)
251 // Tell the driver that it can reallocate the whole VBO
252 pglBufferDataARB(m_Target, m_MaxVertices * m_VertexSize, NULL, m_Usage);
254 // (In theory, glMapBufferRange with GL_MAP_INVALIDATE_BUFFER_BIT could be used
255 // here instead of glBufferData(..., NULL, ...) plus glMapBuffer(), but with
256 // current Intel Windows GPU drivers (as of 2015-01) it's much faster if you do
257 // the explicit glBufferData.)
259 while (true)
261 void* p = pglMapBufferARB(m_Target, GL_WRITE_ONLY);
262 if (p == NULL)
264 // This shouldn't happen unless we run out of virtual address space
265 LOGERROR("glMapBuffer failed");
266 break;
269 #ifndef NDEBUG
270 // To help detect bugs where PrepareForRendering() was not called,
271 // force all not-needed data to 0, so things won't get rendered
272 // with undefined (but possibly still correct-looking) data.
273 memset(p, 0, m_MaxVertices * m_VertexSize);
274 #endif
276 // Copy only the chunks we need. (This condition is helpful when
277 // the VBO contains data for every unit in the world, but only a
278 // handful are visible on screen and we don't need to bother copying
279 // the rest.)
280 for (VBChunk* const& chunk : m_AllocList)
281 if (chunk->m_Needed)
282 memcpy((u8 *)p + chunk->m_Index * m_VertexSize, chunk->m_BackingStore, chunk->m_Count * m_VertexSize);
284 if (pglUnmapBufferARB(m_Target) == GL_TRUE)
285 break;
287 // Unmap might fail on e.g. resolution switches, so just try again
288 // and hope it will eventually succeed
289 debug_printf("glUnmapBuffer failed, trying again...\n");
292 // Anything we just uploaded is clean; anything else is dirty
293 // since the rest of the VBO content is now undefined
294 for (VBChunk* const& chunk : m_AllocList)
296 if (chunk->m_Needed)
297 chunk->m_Dirty = false;
298 else
299 chunk->m_Dirty = true;
303 // Reset the flags for the next phase
304 for (VBChunk* const& chunk : m_AllocList)
305 chunk->m_Needed = false;
308 return (u8*)0;
311 u8* CVertexBuffer::GetBindAddress()
313 if (g_Renderer.m_Caps.m_VBO)
314 return (u8*)0;
315 else
316 return m_SysMem;
319 void CVertexBuffer::Unbind()
321 if (g_Renderer.m_Caps.m_VBO)
323 pglBindBufferARB(GL_ARRAY_BUFFER, 0);
324 pglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, 0);
328 size_t CVertexBuffer::GetBytesReserved() const
330 return MAX_VB_SIZE_BYTES;
333 size_t CVertexBuffer::GetBytesAllocated() const
335 return (m_MaxVertices - m_FreeVertices) * m_VertexSize;
338 void CVertexBuffer::DumpStatus()
340 debug_printf("freeverts = %d\n", (int)m_FreeVertices);
342 size_t maxSize = 0;
343 typedef std::list<VBChunk*>::iterator Iter;
344 for (Iter iter = m_FreeList.begin(); iter != m_FreeList.end(); ++iter)
346 debug_printf("free chunk %p: size=%d\n", (void *)*iter, (int)((*iter)->m_Count));
347 maxSize = std::max((*iter)->m_Count, maxSize);
349 debug_printf("max size = %d\n", (int)maxSize);
352 bool CVertexBuffer::UseStreaming(GLenum usage)
354 return (usage == GL_DYNAMIC_DRAW || usage == GL_STREAM_DRAW);