FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_garbageCollector.hpp
1/*
2 * Copyright 2024 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FGE_VULKAN_C_GARBAGECOLLECTOR_HPP_INCLUDED
18#define _FGE_VULKAN_C_GARBAGECOLLECTOR_HPP_INCLUDED
19
20#include "FastEngine/fge_extern.hpp"
21#include "volk.h"
22#include "FastEngine/vulkan/vulkanGlobal.hpp"
23#include <array>
24#include <vector>
25
26namespace fge::vulkan
27{
28
29class Context;
30
31enum class GarbageType
32{
33 GARBAGE_EMPTY,
34
35 GARBAGE_DESCRIPTOR_SET,
36 GARBAGE_VERTEX_BUFFER,
37 GARBAGE_GRAPHIC_PIPELINE,
38 GARBAGE_PIPELINE_LAYOUT,
39 GARBAGE_COMMAND_POOL,
40 GARBAGE_COMMAND_BUFFER,
41 GARBAGE_FRAMEBUFFER,
42 GARBAGE_RENDERPASS,
43 GARBAGE_SAMPLER,
44 GARBAGE_IMAGE
45};
46
48{
49 GarbageType _type;
50};
52{
53 constexpr GarbageDescriptorSet(VkDescriptorSet descriptorSet,
54 VkDescriptorPool descriptorPool,
55 VkDevice logicalDevice) :
56 _type(GarbageType::GARBAGE_DESCRIPTOR_SET),
57 _descriptorSet(descriptorSet),
58 _descriptorPool(descriptorPool),
59 _logicalDevice(logicalDevice)
60 {}
61
62 GarbageType _type;
63 VkDescriptorSet _descriptorSet;
64 VkDescriptorPool _descriptorPool;
65 VkDevice _logicalDevice;
66};
68{
69 constexpr GarbageBuffer(VkBuffer buffer, VmaAllocation bufferAllocation, VmaAllocator allocator) :
70 _type(GarbageType::GARBAGE_VERTEX_BUFFER),
71 _buffer(buffer),
72 _bufferAllocation(bufferAllocation),
73 _allocator(allocator)
74 {}
75
76 GarbageType _type;
77 VkBuffer _buffer;
78 VmaAllocation _bufferAllocation;
79 VmaAllocator _allocator;
80};
82{
83 constexpr GarbageGraphicPipeline(VkPipeline pipeline, VkDevice logicalDevice) :
84 _type(GarbageType::GARBAGE_GRAPHIC_PIPELINE),
85 _pipeline(pipeline),
86 _logicalDevice(logicalDevice)
87 {}
88
89 GarbageType _type;
90 VkPipeline _pipeline;
91 VkDevice _logicalDevice;
92};
94{
95 constexpr GarbagePipelineLayout(VkPipelineLayout pipelineLayout, VkDevice logicalDevice) :
96 _type(GarbageType::GARBAGE_PIPELINE_LAYOUT),
97 _pipelineLayout(pipelineLayout),
98 _logicalDevice(logicalDevice)
99 {}
100
101 GarbageType _type;
102 VkPipelineLayout _pipelineLayout;
103 VkDevice _logicalDevice;
104};
106{
107 constexpr GarbageCommandPool(VkCommandPool commandPool, VkDevice logicalDevice) :
108 _type(GarbageType::GARBAGE_COMMAND_POOL),
109 _commandPool(commandPool),
110 _logicalDevice(logicalDevice)
111 {}
112
113 GarbageType _type;
114 VkCommandPool _commandPool;
115 VkDevice _logicalDevice;
116};
118{
119 constexpr GarbageCommandBuffer(VkCommandPool commandPool, VkCommandBuffer commandBuffer, VkDevice logicalDevice) :
120 _type(GarbageType::GARBAGE_COMMAND_BUFFER),
121 _commandPool(commandPool),
122 _commandBuffer(commandBuffer),
123 _logicalDevice(logicalDevice)
124 {}
125
126 GarbageType _type;
127 VkCommandPool _commandPool;
128 VkCommandBuffer _commandBuffer;
129 VkDevice _logicalDevice;
130};
132{
133 constexpr GarbageFramebuffer(VkFramebuffer framebuffer, VkDevice logicalDevice) :
134 _type(GarbageType::GARBAGE_FRAMEBUFFER),
135 _framebuffer(framebuffer),
136 _logicalDevice(logicalDevice)
137 {}
138
139 GarbageType _type;
140 VkFramebuffer _framebuffer;
141 VkDevice _logicalDevice;
142};
144{
145 constexpr GarbageRenderPass(VkRenderPass renderPass, VkDevice logicalDevice) :
146 _type(GarbageType::GARBAGE_RENDERPASS),
147 _renderPass(renderPass),
148 _logicalDevice(logicalDevice)
149 {}
150
151 GarbageType _type;
152 VkRenderPass _renderPass;
153 VkDevice _logicalDevice;
154};
156{
157 constexpr GarbageSampler(VkSampler sampler, VkDevice logicalDevice) :
158 _type(GarbageType::GARBAGE_SAMPLER),
159 _sampler(sampler),
160 _logicalDevice(logicalDevice)
161 {}
162
163 GarbageType _type;
164 VkSampler _sampler;
165 VkDevice _logicalDevice;
166};
168{
169 constexpr GarbageImage(VkImage image,
170 VmaAllocation bufferAllocation,
171 VkImageView imageView,
172 Context const* context) :
173 _type(GarbageType::GARBAGE_IMAGE),
174 _image(image),
175 _allocation(bufferAllocation),
176 _imageView(imageView),
177 _context(context)
178 {}
179
180 GarbageType _type;
181 VkImage _image;
182 VmaAllocation _allocation;
183 VkImageView _imageView;
184 Context const* _context;
185};
186
194class FGE_API Garbage final
195{
196private:
197 constexpr Garbage() :
198 g_data(GarbageType::GARBAGE_EMPTY)
199 {}
200
201public:
202 constexpr Garbage(GarbageDescriptorSet const& garbage) :
203 g_data(garbage)
204 {}
205 constexpr Garbage(GarbageBuffer const& garbage) :
206 g_data(garbage)
207 {}
208 constexpr Garbage(GarbageGraphicPipeline const& garbage) :
209 g_data(garbage)
210 {}
211 constexpr Garbage(GarbagePipelineLayout const& garbage) :
212 g_data(garbage)
213 {}
214 constexpr Garbage(GarbageCommandPool const& garbage) :
215 g_data(garbage)
216 {}
217 constexpr Garbage(GarbageCommandBuffer const& garbage) :
218 g_data(garbage)
219 {}
220 constexpr Garbage(GarbageFramebuffer const& garbage) :
221 g_data(garbage)
222 {}
223 constexpr Garbage(GarbageRenderPass const& garbage) :
224 g_data(garbage)
225 {}
226 constexpr Garbage(GarbageSampler const& garbage) :
227 g_data(garbage)
228 {}
229 constexpr Garbage(GarbageImage const& garbage) :
230 g_data(garbage)
231 {}
232 Garbage(Garbage const& r) = delete;
233 Garbage(Garbage&& r) noexcept :
234 g_data(r.g_data)
235 {
236 r.g_data._generic._type = GarbageType::GARBAGE_EMPTY;
237 }
238 ~Garbage();
239
240 Garbage& operator=(Garbage const& r) = delete;
241 Garbage& operator=(Garbage&& r) noexcept = delete;
242
243private:
244 union Data
245 {
246 explicit constexpr Data(GarbageType type) :
247 _generic{type}
248 {}
249 explicit constexpr Data(GarbageDescriptorSet const& data) :
250 _descriptorSet{data}
251 {}
252 explicit constexpr Data(GarbageBuffer const& data) :
253 _buffer{data}
254 {}
255 explicit constexpr Data(GarbageGraphicPipeline const& data) :
256 _graphicPipeline{data}
257 {}
258 explicit constexpr Data(GarbagePipelineLayout const& data) :
259 _pipelineLayout{data}
260 {}
261 explicit constexpr Data(GarbageCommandPool const& data) :
262 _commandPool{data}
263 {}
264 explicit constexpr Data(GarbageCommandBuffer const& data) :
265 _commandBuffer{data}
266 {}
267 explicit constexpr Data(GarbageFramebuffer const& data) :
268 _framebuffer{data}
269 {}
270 explicit constexpr Data(GarbageRenderPass const& data) :
271 _renderPass{data}
272 {}
273 explicit constexpr Data(GarbageSampler const& data) :
274 _sampler{data}
275 {}
276 explicit constexpr Data(GarbageImage const& data) :
277 _image{data}
278 {}
279
280 GarbageGeneric _generic;
281 GarbageDescriptorSet _descriptorSet;
282 GarbageBuffer _buffer;
283 GarbageGraphicPipeline _graphicPipeline;
284 GarbagePipelineLayout _pipelineLayout;
285 GarbageCommandPool _commandPool;
286 GarbageCommandBuffer _commandBuffer;
287 GarbageFramebuffer _framebuffer;
288 GarbageRenderPass _renderPass;
289 GarbageSampler _sampler;
290 GarbageImage _image;
291 };
292
293 Data g_data;
294};
295
305class FGE_API GarbageCollector
306{
307public:
308 using ContainerType = std::vector<Garbage>;
309
310 GarbageCollector() = default;
311 GarbageCollector(GarbageCollector const& r) = delete;
313 ~GarbageCollector() = default;
314
315 GarbageCollector& operator=(GarbageCollector const& r) = delete;
316 GarbageCollector& operator=(GarbageCollector&& r) noexcept = delete;
317
327 void setCurrentFrame(uint32_t frame);
328 [[nodiscard]] uint32_t getCurrentFrame() const;
337 void push(Garbage garbage) const;
343 void free();
350 void freeAll();
351
365 void enable(bool stat);
366 [[nodiscard]] bool isEnabled() const;
367
368private:
369 mutable std::array<ContainerType, FGE_MAX_FRAMES_IN_FLIGHT> g_containers;
370 uint32_t g_currentFrame = 0;
371 bool g_enabled = false;
372};
373
374} // namespace fge::vulkan
375
376#endif //_FGE_VULKAN_C_GARBAGECOLLECTOR_HPP_INCLUDED
Vulkan context.
Definition C_context.hpp:70
A garbage collector for Vulkan objects.
Definition C_garbageCollector.hpp:306
void free()
Free all the garbage objects in the current frame.
void enable(bool stat)
Enable or disable the garbage collector.
void setCurrentFrame(uint32_t frame)
Set the current frame.
void freeAll()
Free all the garbage objects.
void push(Garbage garbage) const
Push a garbage object.
A class that holds a garbage object.
Definition C_garbageCollector.hpp:195
Definition C_garbageCollector.hpp:68
Definition C_garbageCollector.hpp:118
Definition C_garbageCollector.hpp:106
Definition C_garbageCollector.hpp:52
Definition C_garbageCollector.hpp:132
Definition C_garbageCollector.hpp:48
Definition C_garbageCollector.hpp:82
Definition C_garbageCollector.hpp:168
Definition C_garbageCollector.hpp:94
Definition C_garbageCollector.hpp:144
Definition C_garbageCollector.hpp:156