2012-04-22 06:04:02 +02:00
|
|
|
/* Copyright (C) 2012 Wildfire Games.
|
2009-04-18 19:00:33 +02:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-04-18 19:51:05 +02:00
|
|
|
/*
|
|
|
|
* encapsulation of VBOs with batching and sharing
|
2007-05-07 18:33:24 +02:00
|
|
|
*/
|
2004-06-11 00:24:03 +02:00
|
|
|
|
2007-05-07 18:33:24 +02:00
|
|
|
#ifndef INCLUDED_VERTEXBUFFER
|
|
|
|
#define INCLUDED_VERTEXBUFFER
|
2004-06-11 00:24:03 +02:00
|
|
|
|
2005-08-12 19:06:53 +02:00
|
|
|
#include "lib/res/graphics/ogl_tex.h"
|
2004-06-11 00:24:03 +02:00
|
|
|
|
|
|
|
#include <list>
|
|
|
|
#include <vector>
|
|
|
|
|
2012-03-21 19:45:10 +01:00
|
|
|
// Absolute maximum (bytewise) size of each GL vertex buffer object.
|
|
|
|
// Make it large enough for the maximum feasible mesh size (64K vertexes,
|
|
|
|
// 32 bytes per vertex in ShaderModelRenderer).
|
|
|
|
// TODO: measure what influence this has on performance
|
|
|
|
#define MAX_VB_SIZE_BYTES (2*1024*1024)
|
2004-06-11 00:24:03 +02:00
|
|
|
|
2012-04-22 06:04:02 +02:00
|
|
|
/**
|
|
|
|
* CVertexBuffer: encapsulation of ARB_vertex_buffer_object, also supplying
|
|
|
|
* some additional functionality for sharing buffers between multiple objects
|
|
|
|
*/
|
2004-06-11 00:24:03 +02:00
|
|
|
class CVertexBuffer
|
|
|
|
{
|
|
|
|
public:
|
2012-04-22 06:04:02 +02:00
|
|
|
|
|
|
|
/// VBChunk: describes a portion of this vertex buffer
|
2004-06-11 00:24:03 +02:00
|
|
|
struct VBChunk
|
|
|
|
{
|
2012-04-22 06:04:02 +02:00
|
|
|
/// Owning (parent) vertex buffer
|
2004-06-11 00:24:03 +02:00
|
|
|
CVertexBuffer* m_Owner;
|
2012-04-22 06:04:02 +02:00
|
|
|
/// Start index of this chunk in owner
|
2004-06-11 00:24:03 +02:00
|
|
|
size_t m_Index;
|
2012-04-22 06:04:02 +02:00
|
|
|
/// Number of vertices used by chunk
|
2004-06-11 00:24:03 +02:00
|
|
|
size_t m_Count;
|
2011-07-30 02:56:45 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Only CVertexBuffer can construct/delete these
|
|
|
|
// (Other people should use g_VBMan.Allocate, g_VBMan.Release)
|
|
|
|
friend class CVertexBuffer;
|
|
|
|
VBChunk() {}
|
|
|
|
~VBChunk() {}
|
2004-06-11 00:24:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
// constructor, destructor
|
2011-03-13 19:58:09 +01:00
|
|
|
CVertexBuffer(size_t vertexSize, GLenum usage, GLenum target);
|
2004-06-11 00:24:03 +02:00
|
|
|
~CVertexBuffer();
|
|
|
|
|
2012-04-22 06:04:02 +02:00
|
|
|
/// Bind to this buffer; return pointer to address required as parameter
|
|
|
|
/// to glVertexPointer ( + etc) calls
|
2004-06-11 00:24:03 +02:00
|
|
|
u8* Bind();
|
|
|
|
|
2012-04-22 06:04:02 +02:00
|
|
|
/// Get the address that Bind() will return, without actually binding
|
2011-03-13 20:06:33 +01:00
|
|
|
u8* GetBindAddress();
|
|
|
|
|
2012-04-22 06:04:02 +02:00
|
|
|
/// Unbind any currently-bound buffer, so glVertexPointer etc calls will not attempt to use it
|
2010-05-30 15:42:56 +02:00
|
|
|
static void Unbind();
|
|
|
|
|
2012-04-22 06:04:02 +02:00
|
|
|
/// Update vertex data for given chunk. Transfers the provided data to the actual OpenGL vertex buffer.
|
2011-03-13 19:58:09 +01:00
|
|
|
void UpdateChunkVertices(VBChunk* chunk, void* data);
|
2004-06-11 00:24:03 +02:00
|
|
|
|
2006-12-16 02:01:15 +01:00
|
|
|
size_t GetVertexSize() const { return m_VertexSize; }
|
2011-03-13 19:58:09 +01:00
|
|
|
size_t GetBytesReserved() const;
|
|
|
|
size_t GetBytesAllocated() const;
|
2004-06-19 22:54:36 +02:00
|
|
|
|
2012-04-22 06:04:02 +02:00
|
|
|
/// Returns true if this vertex buffer is compatible with the specified vertex type and intended usage.
|
2011-07-30 02:56:45 +02:00
|
|
|
bool CompatibleVertexType(size_t vertexSize, GLenum usage, GLenum target);
|
|
|
|
|
|
|
|
void DumpStatus();
|
|
|
|
|
2004-06-11 00:24:03 +02:00
|
|
|
protected:
|
|
|
|
friend class CVertexBufferManager; // allow allocate only via CVertexBufferManager
|
|
|
|
|
2012-04-22 06:04:02 +02:00
|
|
|
/// Try to allocate a buffer of given number of vertices (each of given size),
|
|
|
|
/// and with the given type - return null if no free chunks available
|
2011-03-13 19:58:09 +01:00
|
|
|
VBChunk* Allocate(size_t vertexSize, size_t numVertices, GLenum usage, GLenum target);
|
2012-04-22 06:04:02 +02:00
|
|
|
/// Return given chunk to this buffer
|
2004-06-11 00:24:03 +02:00
|
|
|
void Release(VBChunk* chunk);
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
2012-04-22 06:04:02 +02:00
|
|
|
/// Vertex size of this vertex buffer
|
2004-06-11 00:24:03 +02:00
|
|
|
size_t m_VertexSize;
|
2012-04-22 06:04:02 +02:00
|
|
|
/// Number of vertices of above size in this buffer
|
2004-06-11 00:24:03 +02:00
|
|
|
size_t m_MaxVertices;
|
2012-04-22 06:04:02 +02:00
|
|
|
/// List of free chunks in this buffer
|
2004-06-11 00:24:03 +02:00
|
|
|
std::list<VBChunk*> m_FreeList;
|
2012-04-22 06:04:02 +02:00
|
|
|
/// Available free vertices - total of all free vertices in the free list
|
2004-06-11 00:24:03 +02:00
|
|
|
size_t m_FreeVertices;
|
2012-04-22 06:04:02 +02:00
|
|
|
/// Handle to the actual GL vertex buffer object
|
2004-06-11 00:24:03 +02:00
|
|
|
GLuint m_Handle;
|
2012-04-22 06:04:02 +02:00
|
|
|
/// Raw system memory for systems not supporting VBOs
|
2004-06-11 00:24:03 +02:00
|
|
|
u8* m_SysMem;
|
2012-04-22 06:04:02 +02:00
|
|
|
/// Usage type of the buffer (GL_STATIC_DRAW etc)
|
2011-03-13 19:58:09 +01:00
|
|
|
GLenum m_Usage;
|
2012-04-22 06:04:02 +02:00
|
|
|
/// Buffer target (GL_ARRAY_BUFFER, GL_ELEMENT_ARRAY_BUFFER)
|
2011-03-13 19:58:09 +01:00
|
|
|
GLenum m_Target;
|
2004-06-11 00:24:03 +02:00
|
|
|
};
|
|
|
|
|
2004-06-11 04:14:18 +02:00
|
|
|
#endif
|