Line data Source code
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
34 2 : class CVertexBufferManager
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 24 : 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 36 : ~Handle() { Reset(); }
58 :
59 126 : bool IsValid() const { return m_Chunk != nullptr; }
60 12 : explicit operator bool() const { return IsValid(); }
61 12 : bool operator!() const { return !static_cast<bool>(*this); }
62 : void Reset();
63 :
64 6 : friend void swap(Handle& lhs, Handle& rhs)
65 : {
66 : using std::swap;
67 :
68 6 : swap(lhs.m_Chunk, rhs.m_Chunk);
69 6 : }
70 :
71 : CVertexBuffer::VBChunk& operator*() const { return *m_Chunk; }
72 6 : CVertexBuffer::VBChunk* operator->() const { return m_Chunk; }
73 6 : 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 : */
91 : Handle AllocateChunk(
92 : const size_t vertexSize, const size_t numberOfVertices,
93 : const Renderer::Backend::IBuffer::Type type,
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 :
111 : extern CVertexBufferManager g_VBMan;
112 :
113 : #endif
|