Pyrogenesis  trunk
VertexBufferManager.h
Go to the documentation of this file.
1 /* Copyright (C) 2022 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"
26 #include "renderer/VertexBuffer.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 {
36 public:
37  enum class Group : u32
38  {
39  DEFAULT,
40  TERRAIN,
41  WATER,
42  COUNT
43  };
44 
45  // Helper for automatic VBChunk lifetime management.
46  class Handle
47  {
48  public:
49  Handle() = default;
50  Handle(const Handle&) = delete;
51  Handle& operator=(const Handle&) = delete;
52 
53  explicit Handle(CVertexBuffer::VBChunk* chunk);
54  Handle(Handle&& other);
55  Handle& operator=(Handle&& other);
56 
57  ~Handle() { Reset(); }
58 
59  bool IsValid() const { return m_Chunk != nullptr; }
60  explicit operator bool() const { return IsValid(); }
61  bool operator!() const { return !static_cast<bool>(*this); }
62  void Reset();
63 
64  friend void swap(Handle& lhs, Handle& rhs)
65  {
66  using std::swap;
67 
68  swap(lhs.m_Chunk, rhs.m_Chunk);
69  }
70 
71  CVertexBuffer::VBChunk& operator*() const { return *m_Chunk; }
72  CVertexBuffer::VBChunk* operator->() const { return m_Chunk; }
73  CVertexBuffer::VBChunk* Get() const { return m_Chunk; }
74 
75  private:
76  CVertexBuffer::VBChunk* m_Chunk = nullptr;
77  };
78 
79  /**
80  * Try to allocate a vertex buffer of the given size and type.
81  *
82  * @param vertexSize size of each vertex in the buffer
83  * @param numberOfVertices number of vertices in the buffer
84  * @param type buffer type
85  * @param dynamic will be buffer updated frequently or not
86  * @param backingStore if not dynamic, this is nullptr; else for dynamic,
87  * this must be a copy of the vertex data that remains valid for the
88  * lifetime of the VBChunk
89  * @return chunk, or empty handle if no free chunks available
90  */
92  const size_t vertexSize, const size_t numberOfVertices,
94  const bool dynamic, void* backingStore = nullptr, Group group = Group::DEFAULT);
95 
96  /// Returns the given @p chunk to its owning buffer
97  void Release(CVertexBuffer::VBChunk* chunk);
98 
99  size_t GetBytesReserved() const;
100  size_t GetBytesAllocated() const;
101 
102  /// Explicit shutdown of the vertex buffer subsystem; releases all currently-allocated buffers.
103  void Shutdown();
104 
105 private:
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 
112 
113 #endif
Type
Definition: IBuffer.h:34
Definition: VertexBufferManager.h:34
void Shutdown()
Explicit shutdown of the vertex buffer subsystem; releases all currently-allocated buffers...
Definition: VertexBufferManager.cpp:111
Group
Definition: VertexBufferManager.h:37
CVertexBufferManager g_VBMan
Definition: VertexBufferManager.cpp:72
Definition: VertexBuffer.h:63
CVertexBuffer::VBChunk * m_Chunk
Definition: VertexBufferManager.h:76
bool operator!() const
Definition: VertexBufferManager.h:61
Definition: VertexBufferManager.h:46
uint32_t u32
Definition: types.h:39
Handle AllocateChunk(const size_t vertexSize, const size_t numberOfVertices, const Renderer::Backend::IBuffer::Type type, const bool dynamic, void *backingStore=nullptr, Group group=Group::DEFAULT)
Try to allocate a vertex buffer of the given size and type.
Definition: VertexBufferManager.cpp:122
CVertexBuffer::VBChunk * Get() const
Definition: VertexBufferManager.h:73
CVertexBuffer::VBChunk * operator->() const
Definition: VertexBufferManager.h:72
void Release(CVertexBuffer::VBChunk *chunk)
Returns the given chunk to its owning buffer.
Definition: VertexBufferManager.cpp:182
size_t GetBytesReserved() const
Definition: VertexBufferManager.cpp:191
friend void swap(Handle &lhs, Handle &rhs)
Definition: VertexBufferManager.h:64
~Handle()
Definition: VertexBufferManager.h:57
CVertexBuffer::VBChunk & operator*() const
Definition: VertexBufferManager.h:71
size_t GetBytesAllocated() const
Definition: VertexBufferManager.cpp:200
bool IsValid() const
Definition: VertexBufferManager.h:59
#define swap(a, i, j)
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