Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
VertexBuffer.h
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 batching and sharing
20  */
21 
22 #ifndef INCLUDED_VERTEXBUFFER
23 #define INCLUDED_VERTEXBUFFER
24 
26 
27 #include <list>
28 #include <vector>
29 
30 // Absolute maximum (bytewise) size of each GL vertex buffer object.
31 // Make it large enough for the maximum feasible mesh size (64K vertexes,
32 // 32 bytes per vertex in ShaderModelRenderer).
33 // TODO: measure what influence this has on performance
34 #define MAX_VB_SIZE_BYTES (4*1024*1024)
35 
36 /**
37  * CVertexBuffer: encapsulation of ARB_vertex_buffer_object, also supplying
38  * some additional functionality for sharing buffers between multiple objects
39  */
41 {
43 
44 public:
45 
46  /// VBChunk: describes a portion of this vertex buffer
47  struct VBChunk
48  {
49  /// Owning (parent) vertex buffer
51  /// Start index of this chunk in owner
52  size_t m_Index;
53  /// Number of vertices used by chunk
54  size_t m_Count;
55 
56  private:
57  // Only CVertexBuffer can construct/delete these
58  // (Other people should use g_VBMan.Allocate, g_VBMan.Release)
59  friend class CVertexBuffer;
60  VBChunk() {}
61  ~VBChunk() {}
62  };
63 
64 public:
65  // constructor, destructor
66  CVertexBuffer(size_t vertexSize, GLenum usage, GLenum target);
68 
69  /// Bind to this buffer; return pointer to address required as parameter
70  /// to glVertexPointer ( + etc) calls
71  u8* Bind();
72 
73  /// Get the address that Bind() will return, without actually binding
74  u8* GetBindAddress();
75 
76  /// Unbind any currently-bound buffer, so glVertexPointer etc calls will not attempt to use it
77  static void Unbind();
78 
79  /// Update vertex data for given chunk. Transfers the provided data to the actual OpenGL vertex buffer.
80  void UpdateChunkVertices(VBChunk* chunk, void* data);
81 
82  size_t GetVertexSize() const { return m_VertexSize; }
83  size_t GetBytesReserved() const;
84  size_t GetBytesAllocated() const;
85 
86  /// Returns true if this vertex buffer is compatible with the specified vertex type and intended usage.
87  bool CompatibleVertexType(size_t vertexSize, GLenum usage, GLenum target);
88 
89  void DumpStatus();
90 
91 protected:
92  friend class CVertexBufferManager; // allow allocate only via CVertexBufferManager
93 
94  /// Try to allocate a buffer of given number of vertices (each of given size),
95  /// and with the given type - return null if no free chunks available
96  VBChunk* Allocate(size_t vertexSize, size_t numVertices, GLenum usage, GLenum target);
97  /// Return given chunk to this buffer
98  void Release(VBChunk* chunk);
99 
100 
101 private:
102  /// Vertex size of this vertex buffer
103  size_t m_VertexSize;
104  /// Number of vertices of above size in this buffer
106  /// List of free chunks in this buffer
107  std::list<VBChunk*> m_FreeList;
108  /// Available free vertices - total of all free vertices in the free list
110  /// Handle to the actual GL vertex buffer object
111  GLuint m_Handle;
112  /// Raw system memory for systems not supporting VBOs
114  /// Usage type of the buffer (GL_STATIC_DRAW etc)
115  GLenum m_Usage;
116  /// Buffer target (GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER)
117  GLenum m_Target;
118 };
119 
120 #endif
size_t GetVertexSize() const
Definition: VertexBuffer.h:82
#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
CVertexBuffer: encapsulation of ARB_vertex_buffer_object, also supplying some additional functionalit...
Definition: VertexBuffer.h:40
u8 * GetBindAddress()
Get the address that Bind() will return, without actually binding.
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
NONCOPYABLE(CVertexBuffer)
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...
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 - ...