Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
VertexBufferManager.h
Go to the documentation of this file.
1/* Copyright (C) 2024 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 * Allocate and destroy CVertexBuffers
20 */
21
22#ifndef INCLUDED_VERTEXBUFFERMANAGER
23#define INCLUDED_VERTEXBUFFERMANAGER
24
25#include "lib/types.h"
27
28#include <memory>
29#include <utility>
30#include <vector>
31
32// CVertexBufferManager: owner object for CVertexBuffer objects; acts as
33// 'front end' for their allocation and destruction
35{
36public:
38
39 enum class Group : u32
40 {
41 DEFAULT,
42 TERRAIN,
43 WATER,
44 COUNT
45 };
46
47 // Helper for automatic VBChunk lifetime management.
48 class Handle
49 {
50 public:
51 Handle() = default;
52 Handle(const Handle&) = delete;
53 Handle& operator=(const Handle&) = delete;
54
55 explicit Handle(CVertexBuffer::VBChunk* chunk);
56 Handle(Handle&& other);
57 Handle& operator=(Handle&& other);
58
59 ~Handle() { Reset(); }
60
61 bool IsValid() const { return m_Chunk != nullptr; }
62 explicit operator bool() const { return IsValid(); }
63 bool operator!() const { return !static_cast<bool>(*this); }
64 void Reset();
65
66 friend void swap(Handle& lhs, Handle& rhs)
67 {
68 using std::swap;
69
70 swap(lhs.m_Chunk, rhs.m_Chunk);
71 }
72
75 CVertexBuffer::VBChunk* Get() const { return m_Chunk; }
76
77 private:
79 };
80
81 /**
82 * Try to allocate a vertex buffer of the given size and type.
83 *
84 * @param vertexSize size of each vertex in the buffer
85 * @param numberOfVertices number of vertices in the buffer
86 * @param type buffer type
87 * @param dynamic will be buffer updated frequently or not
88 * @param backingStore if not dynamic, this is nullptr; else for dynamic,
89 * this must be a copy of the vertex data that remains valid for the
90 * lifetime of the VBChunk
91 * @return chunk, or empty handle if no free chunks available
92 */
94 const size_t vertexSize, const size_t numberOfVertices,
96 const uint32_t usage, void* backingStore = nullptr, Group group = Group::DEFAULT);
97
98 /// Returns the given @p chunk to its owning buffer
100
101 size_t GetBytesReserved() const;
102 size_t GetBytesAllocated() const;
103
104private:
106
107 /// List of all known vertex buffers
108 std::vector<std::unique_ptr<CVertexBuffer>> m_Buffers[static_cast<std::size_t>(Group::COUNT)];
109};
110
111#endif // INCLUDED_VERTEXBUFFERMANAGER
#define swap(a, i, j)
Definition: VertexBufferManager.h:49
CVertexBuffer::VBChunk * operator->() const
Definition: VertexBufferManager.h:74
~Handle()
Definition: VertexBufferManager.h:59
CVertexBuffer::VBChunk * m_Chunk
Definition: VertexBufferManager.h:78
void Reset()
Definition: VertexBufferManager.cpp:98
CVertexBuffer::VBChunk & operator*() const
Definition: VertexBufferManager.h:73
Handle & operator=(const Handle &)=delete
friend void swap(Handle &lhs, Handle &rhs)
Definition: VertexBufferManager.h:66
Handle(const Handle &)=delete
bool operator!() const
Definition: VertexBufferManager.h:63
bool IsValid() const
Definition: VertexBufferManager.h:61
CVertexBuffer::VBChunk * Get() const
Definition: VertexBufferManager.h:75
Definition: VertexBufferManager.h:35
std::vector< std::unique_ptr< CVertexBuffer > > m_Buffers[static_cast< std::size_t >(Group::COUNT)]
List of all known vertex buffers.
Definition: VertexBufferManager.h:108
size_t GetBytesAllocated() const
Definition: VertexBufferManager.cpp:190
size_t GetBytesReserved() const
Definition: VertexBufferManager.cpp:181
CVertexBufferManager(Renderer::Backend::IDevice *device)
Definition: VertexBufferManager.h:37
Group
Definition: VertexBufferManager.h:40
Handle AllocateChunk(const size_t vertexSize, const size_t numberOfVertices, const Renderer::Backend::IBuffer::Type type, const uint32_t usage, void *backingStore=nullptr, Group group=Group::DEFAULT)
Try to allocate a vertex buffer of the given size and type.
Definition: VertexBufferManager.cpp:112
void Release(CVertexBuffer::VBChunk *chunk)
Returns the given chunk to its owning buffer.
Definition: VertexBufferManager.cpp:172
Renderer::Backend::IDevice * m_Device
Definition: VertexBufferManager.h:105
Type
Definition: IBuffer.h:35
Definition: IDevice.h:48
#define DEFAULT(name)
Definition: mocks_test.cpp:29
Definition: VertexBuffer.h:65
uint32_t u32
Definition: types.h:39
unsigned int uint32_t
Definition: wposix_types.h:53