Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
VertexBuffer.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2013 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 
18 /*
19  * encapsulation of VBOs with sharing
20  */
21 
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"
30 
31 CVertexBuffer::CVertexBuffer(size_t vertexSize, GLenum usage, GLenum target)
32  : m_VertexSize(vertexSize), m_Handle(0), m_SysMem(0), m_Usage(usage), m_Target(target)
33 {
34  size_t size = MAX_VB_SIZE_BYTES;
35 
36  if (target == GL_ARRAY_BUFFER) // vertex data buffer
37  {
38  // We want to store 16-bit indices to any vertex in a buffer, so the
39  // buffer must never be bigger than vertexSize*64K bytes since we can
40  // address at most 64K of them with 16-bit indices
41  size = std::min(size, vertexSize*65536);
42  }
43 
44  // allocate raw buffer
45  if (g_Renderer.m_Caps.m_VBO)
46  {
47  pglGenBuffersARB(1, &m_Handle);
48  pglBindBufferARB(m_Target, m_Handle);
49  pglBufferDataARB(m_Target, size, 0, m_Usage);
50  pglBindBufferARB(m_Target, 0);
51  }
52  else
53  {
54  m_SysMem = new u8[size];
55  }
56 
57  // store max/free vertex counts
58  m_MaxVertices = m_FreeVertices = size/vertexSize;
59 
60  // create sole free chunk
61  VBChunk* chunk = new VBChunk;
62  chunk->m_Owner = this;
63  chunk->m_Count = m_FreeVertices;
64  chunk->m_Index = 0;
65  m_FreeList.push_front(chunk);
66 }
67 
69 {
70  if (m_Handle)
71  pglDeleteBuffersARB(1, &m_Handle);
72 
73  delete[] m_SysMem;
74 
75  typedef std::list<VBChunk*>::iterator Iter;
76  for (Iter iter = m_FreeList.begin(); iter != m_FreeList.end(); ++iter)
77  delete *iter;
78 }
79 
80 
81 bool CVertexBuffer::CompatibleVertexType(size_t vertexSize, GLenum usage, GLenum target)
82 {
83  if (usage != m_Usage || target != m_Target || vertexSize != m_VertexSize)
84  return false;
85 
86  return true;
87 }
88 
89 ///////////////////////////////////////////////////////////////////////////////
90 // Allocate: try to allocate a buffer of given number of vertices (each of
91 // given size), with the given type, and using the given texture - return null
92 // if no free chunks available
93 CVertexBuffer::VBChunk* CVertexBuffer::Allocate(size_t vertexSize, size_t numVertices, GLenum usage, GLenum target)
94 {
95  // check this is the right kind of buffer
96  if (!CompatibleVertexType(vertexSize, usage, target))
97  return 0;
98 
99  // quick check there's enough vertices spare to allocate
100  if (numVertices > m_FreeVertices)
101  return 0;
102 
103  // trawl free list looking for first free chunk with enough space
104  VBChunk* chunk = 0;
105  typedef std::list<VBChunk*>::iterator Iter;
106  for (Iter iter = m_FreeList.begin(); iter != m_FreeList.end(); ++iter) {
107  if (numVertices <= (*iter)->m_Count) {
108  chunk = *iter;
109  // remove this chunk from the free list
110  m_FreeList.erase(iter);
111  m_FreeVertices -= chunk->m_Count;
112  // no need to search further ..
113  break;
114  }
115  }
116 
117  if (!chunk) {
118  // no big enough spare chunk available
119  return 0;
120  }
121 
122  // split chunk into two; - allocate a new chunk using all unused vertices in the
123  // found chunk, and add it to the free list
124  if (chunk->m_Count > numVertices)
125  {
126  VBChunk* newchunk = new VBChunk;
127  newchunk->m_Owner = this;
128  newchunk->m_Count = chunk->m_Count - numVertices;
129  newchunk->m_Index = chunk->m_Index + numVertices;
130  m_FreeList.push_front(newchunk);
131  m_FreeVertices += newchunk->m_Count;
132 
133  // resize given chunk
134  chunk->m_Count = numVertices;
135  }
136 
137  // return found chunk
138  return chunk;
139 }
140 
141 ///////////////////////////////////////////////////////////////////////////////
142 // Release: return given chunk to this buffer
144 {
145  // Update total free count before potentially modifying this chunk's count
146  m_FreeVertices += chunk->m_Count;
147 
148  typedef std::list<VBChunk*>::iterator Iter;
149 
150  // Coalesce with any free-list items that are adjacent to this chunk;
151  // merge the found chunk with the new one, and remove the old one
152  // from the list, and repeat until no more are found
153  bool coalesced;
154  do
155  {
156  coalesced = false;
157  for (Iter iter = m_FreeList.begin(); iter != m_FreeList.end(); ++iter)
158  {
159  if ((*iter)->m_Index == chunk->m_Index + chunk->m_Count
160  || (*iter)->m_Index + (*iter)->m_Count == chunk->m_Index)
161  {
162  chunk->m_Index = std::min(chunk->m_Index, (*iter)->m_Index);
163  chunk->m_Count += (*iter)->m_Count;
164  delete *iter;
165  m_FreeList.erase(iter);
166  coalesced = true;
167  break;
168  }
169  }
170  }
171  while (coalesced);
172 
173  m_FreeList.push_front(chunk);
174 }
175 
176 ///////////////////////////////////////////////////////////////////////////////
177 // UpdateChunkVertices: update vertex data for given chunk
179 {
180  if (g_Renderer.m_Caps.m_VBO)
181  {
182  ENSURE(m_Handle);
183  pglBindBufferARB(m_Target, m_Handle);
184  pglBufferSubDataARB(m_Target, chunk->m_Index * m_VertexSize, chunk->m_Count * m_VertexSize, data);
185  pglBindBufferARB(m_Target, 0);
186  }
187  else
188  {
189  ENSURE(m_SysMem);
190  memcpy(m_SysMem + chunk->m_Index * m_VertexSize, data, chunk->m_Count * m_VertexSize);
191  }
192 }
193 
194 ///////////////////////////////////////////////////////////////////////////////
195 // Bind: bind to this buffer; return pointer to address required as parameter
196 // to glVertexPointer ( + etc) calls
198 {
199  if (g_Renderer.m_Caps.m_VBO)
200  {
201  pglBindBufferARB(m_Target, m_Handle);
202  return (u8*)0;
203  }
204  else
205  {
206  return m_SysMem;
207  }
208 }
209 
211 {
212  if (g_Renderer.m_Caps.m_VBO)
213  return (u8*)0;
214  else
215  return m_SysMem;
216 }
217 
219 {
220  if (g_Renderer.m_Caps.m_VBO)
221  {
222  pglBindBufferARB(GL_ARRAY_BUFFER, 0);
223  pglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, 0);
224  }
225 }
226 
228 {
229  return MAX_VB_SIZE_BYTES;
230 }
231 
233 {
235 }
236 
238 {
239  debug_printf(L"freeverts = %d\n", (int)m_FreeVertices);
240 
241  size_t maxSize = 0;
242  typedef std::list<VBChunk*>::iterator Iter;
243  for (Iter iter = m_FreeList.begin(); iter != m_FreeList.end(); ++iter)
244  {
245  debug_printf(L"free chunk %p: size=%d\n", *iter, (int)((*iter)->m_Count));
246  maxSize = std::max((*iter)->m_Count, maxSize);
247  }
248  debug_printf(L"max size = %d\n", (int)maxSize);
249 }
#define u8
Definition: types.h:39
size_t m_Index
Start index of this chunk in owner.
Definition: VertexBuffer.h:52
u8 * Bind()
Bind to this buffer; return pointer to address required as parameter to glVertexPointer ( + etc) call...
size_t GetBytesReserved() const
GLenum m_Usage
Usage type of the buffer (GL_STATIC_DRAW etc)
Definition: VertexBuffer.h:115
VBChunk: describes a portion of this vertex buffer.
Definition: VertexBuffer.h:47
GLenum m_Target
Buffer target (GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER)
Definition: VertexBuffer.h:117
u8 * GetBindAddress()
Get the address that Bind() will return, without actually binding.
#define g_Renderer
Definition: Renderer.h:61
#define ENSURE(expr)
ensure the expression &lt;expr&gt; evaluates to non-zero.
Definition: debug.h:282
u8 * m_SysMem
Raw system memory for systems not supporting VBOs.
Definition: VertexBuffer.h:113
std::list< VBChunk * > m_FreeList
List of free chunks in this buffer.
Definition: VertexBuffer.h:107
size_t m_FreeVertices
Available free vertices - total of all free vertices in the free list.
Definition: VertexBuffer.h:109
CVertexBuffer(size_t vertexSize, GLenum usage, GLenum target)
void UpdateChunkVertices(VBChunk *chunk, void *data)
Update vertex data for given chunk. Transfers the provided data to the actual OpenGL vertex buffer...
void Release(VBChunk *chunk)
Return given chunk to this buffer.
size_t m_Count
Number of vertices used by chunk.
Definition: VertexBuffer.h:54
size_t m_MaxVertices
Number of vertices of above size in this buffer.
Definition: VertexBuffer.h:105
CVertexBuffer * m_Owner
Owning (parent) vertex buffer.
Definition: VertexBuffer.h:50
size_t GetBytesAllocated() const
GLuint m_Handle
Handle to the actual GL vertex buffer object.
Definition: VertexBuffer.h:111
size_t m_VertexSize
Vertex size of this vertex buffer.
Definition: VertexBuffer.h:103
bool CompatibleVertexType(size_t vertexSize, GLenum usage, GLenum target)
Returns true if this vertex buffer is compatible with the specified vertex type and intended usage...
#define MAX_VB_SIZE_BYTES
Definition: VertexBuffer.h:34
static void Unbind()
Unbind any currently-bound buffer, so glVertexPointer etc calls will not attempt to use it...
VBChunk * Allocate(size_t vertexSize, size_t numVertices, GLenum usage, GLenum target)
Try to allocate a buffer of given number of vertices (each of given size), and with the given type - ...
void debug_printf(const wchar_t *fmt,...)
write a formatted string to the debug channel, subject to filtering (see below).
Definition: debug.cpp:142