Merge 'remotes/trunk'
[0ad.git] / source / renderer / VertexBuffer.h
blob54d107bfe681f9aed456160b4779b63acce1af15
1 /* Copyright (C) 2015 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 batching and sharing
22 #ifndef INCLUDED_VERTEXBUFFER
23 #define INCLUDED_VERTEXBUFFER
25 #include "lib/res/graphics/ogl_tex.h"
27 #include <list>
28 #include <vector>
30 /**
31 * CVertexBuffer: encapsulation of ARB_vertex_buffer_object, also supplying
32 * some additional functionality for sharing buffers between multiple objects.
34 * The class can be used in two modes, depending on the usage parameter:
36 * GL_STATIC_DRAW: Call Allocate() with backingStore = NULL. Then call
37 * UpdateChunkVertices() with any pointer - the data will be immediately copied
38 * to the VBO. This should be used for vertex data that rarely changes.
40 * GL_DYNAMIC_DRAW, GL_STREAM_DRAW: Call Allocate() with backingStore pointing
41 * at some memory that will remain valid for the lifetime of the CVertexBuffer.
42 * This should be used for vertex data that may change every frame.
43 * Rendering is expected to occur in two phases:
44 * - "Prepare" phase:
45 * If this chunk is going to be used for rendering during the next Bind phase,
46 * you must call PrepareForRendering().
47 * If the vertex data in backingStore has been modified since the last Bind phase,
48 * you must call UpdateChunkVertices().
49 * - "Bind" phase:
50 * Bind() can be called (multiple times). The vertex data will be uploaded
51 * to the GPU if necessary.
52 * It is okay to have multiple prepare/bind cycles per frame (though slightly less
53 * efficient), but they must occur sequentially.
55 class CVertexBuffer
57 NONCOPYABLE(CVertexBuffer);
59 public:
61 /// VBChunk: describes a portion of this vertex buffer
62 struct VBChunk
64 /// Owning (parent) vertex buffer
65 CVertexBuffer* m_Owner;
66 /// Start index of this chunk in owner
67 size_t m_Index;
68 /// Number of vertices used by chunk
69 size_t m_Count;
70 /// If UseStreaming() is true, points at the data for this chunk
71 void* m_BackingStore;
73 /// If true, the VBO is not consistent with the chunk's backing store
74 /// (and will need to be re-uploaded before rendering with this chunk)
75 bool m_Dirty;
77 /// If true, we have been told this chunk is going to be used for
78 /// rendering in the next bind phase and will need to be uploaded
79 bool m_Needed;
81 private:
82 // Only CVertexBuffer can construct/delete these
83 // (Other people should use g_VBMan.Allocate, g_VBMan.Release)
84 friend class CVertexBuffer;
85 VBChunk() {}
86 ~VBChunk() {}
89 public:
90 // constructor, destructor
91 CVertexBuffer(size_t vertexSize, GLenum usage, GLenum target);
92 ~CVertexBuffer();
94 /// Bind to this buffer; return pointer to address required as parameter
95 /// to glVertexPointer ( + etc) calls
96 u8* Bind();
98 /// Get the address that Bind() will return, without actually binding
99 u8* GetBindAddress();
101 /// Unbind any currently-bound buffer, so glVertexPointer etc calls will not attempt to use it
102 static void Unbind();
104 /// Make the vertex data available for the next call to Bind()
105 void PrepareForRendering(VBChunk* chunk) { chunk->m_Needed = true; }
107 /// Update vertex data for given chunk. Transfers the provided data to the actual OpenGL vertex buffer.
108 void UpdateChunkVertices(VBChunk* chunk, void* data);
110 size_t GetVertexSize() const { return m_VertexSize; }
111 size_t GetBytesReserved() const;
112 size_t GetBytesAllocated() const;
114 /// Returns true if this vertex buffer is compatible with the specified vertex type and intended usage.
115 bool CompatibleVertexType(size_t vertexSize, GLenum usage, GLenum target);
117 void DumpStatus();
120 * Given the usage flags of a buffer that has been (or will be) allocated:
122 * If true, we assume the buffer is going to be modified on every frame,
123 * so we will re-upload the entire buffer every frame using glMapBuffer.
124 * This requires the buffer's owner to hold onto its backing store.
126 * If false, we assume it will change rarely, and use glSubBufferData to
127 * update it incrementally. The backing store can be freed to save memory.
129 static bool UseStreaming(GLenum usage);
131 protected:
132 friend class CVertexBufferManager; // allow allocate only via CVertexBufferManager
134 /// Try to allocate a buffer of given number of vertices (each of given size),
135 /// and with the given type - return null if no free chunks available
136 VBChunk* Allocate(size_t vertexSize, size_t numVertices, GLenum usage, GLenum target, void* backingStore);
137 /// Return given chunk to this buffer
138 void Release(VBChunk* chunk);
141 private:
143 /// Vertex size of this vertex buffer
144 size_t m_VertexSize;
145 /// Number of vertices of above size in this buffer
146 size_t m_MaxVertices;
147 /// List of free chunks in this buffer
148 std::list<VBChunk*> m_FreeList;
149 /// List of allocated chunks
150 std::list<VBChunk*> m_AllocList;
151 /// Available free vertices - total of all free vertices in the free list
152 size_t m_FreeVertices;
153 /// Handle to the actual GL vertex buffer object
154 GLuint m_Handle;
155 /// Raw system memory for systems not supporting VBOs
156 u8* m_SysMem;
157 /// Usage type of the buffer (GL_STATIC_DRAW etc)
158 GLenum m_Usage;
159 /// Buffer target (GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER)
160 GLenum m_Target;
163 #endif