FastEngine 0.9.4
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_garbageCollector.hpp
1/*
2 * Copyright 2025 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};
51struct GarbageDescriptorSet
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};
67struct GarbageBuffer
68{
69 constexpr GarbageBuffer(BufferInfo bufferInfo, VmaAllocator allocator) :
70 _type(GarbageType::GARBAGE_VERTEX_BUFFER),
71 _bufferInfo(bufferInfo),
72 _allocator(allocator)
73 {}
74
75 GarbageType _type;
76 BufferInfo _bufferInfo;
77 VmaAllocator _allocator;
78};
79struct GarbageGraphicPipeline
80{
81 constexpr GarbageGraphicPipeline(VkPipeline pipeline, VkDevice logicalDevice) :
82 _type(GarbageType::GARBAGE_GRAPHIC_PIPELINE),
83 _pipeline(pipeline),
84 _logicalDevice(logicalDevice)
85 {}
86
87 GarbageType _type;
88 VkPipeline _pipeline;
89 VkDevice _logicalDevice;
90};
91struct GarbagePipelineLayout
92{
93 constexpr GarbagePipelineLayout(VkPipelineLayout pipelineLayout, VkDevice logicalDevice) :
94 _type(GarbageType::GARBAGE_PIPELINE_LAYOUT),
95 _pipelineLayout(pipelineLayout),
96 _logicalDevice(logicalDevice)
97 {}
98
99 GarbageType _type;
100 VkPipelineLayout _pipelineLayout;
101 VkDevice _logicalDevice;
102};
103struct GarbageCommandPool
104{
105 constexpr GarbageCommandPool(VkCommandPool commandPool, VkDevice logicalDevice) :
106 _type(GarbageType::GARBAGE_COMMAND_POOL),
107 _commandPool(commandPool),
108 _logicalDevice(logicalDevice)
109 {}
110
111 GarbageType _type;
112 VkCommandPool _commandPool;
113 VkDevice _logicalDevice;
114};
115struct GarbageCommandBuffer
116{
117 constexpr GarbageCommandBuffer(VkCommandPool commandPool, VkCommandBuffer commandBuffer, VkDevice logicalDevice) :
118 _type(GarbageType::GARBAGE_COMMAND_BUFFER),
119 _commandPool(commandPool),
120 _commandBuffer(commandBuffer),
121 _logicalDevice(logicalDevice)
122 {}
123
124 GarbageType _type;
125 VkCommandPool _commandPool;
126 VkCommandBuffer _commandBuffer;
127 VkDevice _logicalDevice;
128};
129struct GarbageFramebuffer
130{
131 constexpr GarbageFramebuffer(VkFramebuffer framebuffer, VkDevice logicalDevice) :
132 _type(GarbageType::GARBAGE_FRAMEBUFFER),
133 _framebuffer(framebuffer),
134 _logicalDevice(logicalDevice)
135 {}
136
137 GarbageType _type;
138 VkFramebuffer _framebuffer;
139 VkDevice _logicalDevice;
140};
141struct GarbageRenderPass
142{
143 constexpr GarbageRenderPass(VkRenderPass renderPass, VkDevice logicalDevice) :
144 _type(GarbageType::GARBAGE_RENDERPASS),
145 _renderPass(renderPass),
146 _logicalDevice(logicalDevice)
147 {}
148
149 GarbageType _type;
150 VkRenderPass _renderPass;
151 VkDevice _logicalDevice;
152};
153struct GarbageSampler
154{
155 constexpr GarbageSampler(VkSampler sampler, VkDevice logicalDevice) :
156 _type(GarbageType::GARBAGE_SAMPLER),
157 _sampler(sampler),
158 _logicalDevice(logicalDevice)
159 {}
160
161 GarbageType _type;
162 VkSampler _sampler;
163 VkDevice _logicalDevice;
164};
165struct GarbageImage
166{
167 constexpr GarbageImage(VkImage image,
168 VmaAllocation bufferAllocation,
169 VkImageView imageView,
170 Context const* context) :
171 _type(GarbageType::GARBAGE_IMAGE),
172 _image(image),
173 _allocation(bufferAllocation),
174 _imageView(imageView),
175 _context(context)
176 {}
177
178 GarbageType _type;
179 VkImage _image;
180 VmaAllocation _allocation;
181 VkImageView _imageView;
182 Context const* _context;
183};
184
192class FGE_API Garbage final
193{
194private:
195 constexpr Garbage() :
196 g_data(GarbageType::GARBAGE_EMPTY)
197 {}
198
199public:
200 constexpr Garbage(GarbageDescriptorSet const& garbage) :
201 g_data(garbage)
202 {}
203 constexpr Garbage(GarbageBuffer const& garbage) :
204 g_data(garbage)
205 {}
206 constexpr Garbage(GarbageGraphicPipeline const& garbage) :
207 g_data(garbage)
208 {}
209 constexpr Garbage(GarbagePipelineLayout const& garbage) :
210 g_data(garbage)
211 {}
212 constexpr Garbage(GarbageCommandPool const& garbage) :
213 g_data(garbage)
214 {}
215 constexpr Garbage(GarbageCommandBuffer const& garbage) :
216 g_data(garbage)
217 {}
218 constexpr Garbage(GarbageFramebuffer const& garbage) :
219 g_data(garbage)
220 {}
221 constexpr Garbage(GarbageRenderPass const& garbage) :
222 g_data(garbage)
223 {}
224 constexpr Garbage(GarbageSampler const& garbage) :
225 g_data(garbage)
226 {}
227 constexpr Garbage(GarbageImage const& garbage) :
228 g_data(garbage)
229 {}
230 Garbage(Garbage const& r) = delete;
231 Garbage(Garbage&& r) noexcept :
232 g_data(r.g_data)
233 {
234 r.g_data._generic._type = GarbageType::GARBAGE_EMPTY;
235 }
236 ~Garbage();
237
238 Garbage& operator=(Garbage const& r) = delete;
239 Garbage& operator=(Garbage&& r) noexcept = delete;
240
241private:
242 union Data
243 {
244 explicit constexpr Data(GarbageType type) :
245 _generic{type}
246 {}
247 explicit constexpr Data(GarbageDescriptorSet const& data) :
248 _descriptorSet{data}
249 {}
250 explicit constexpr Data(GarbageBuffer const& data) :
251 _buffer{data}
252 {}
253 explicit constexpr Data(GarbageGraphicPipeline const& data) :
254 _graphicPipeline{data}
255 {}
256 explicit constexpr Data(GarbagePipelineLayout const& data) :
257 _pipelineLayout{data}
258 {}
259 explicit constexpr Data(GarbageCommandPool const& data) :
260 _commandPool{data}
261 {}
262 explicit constexpr Data(GarbageCommandBuffer const& data) :
263 _commandBuffer{data}
264 {}
265 explicit constexpr Data(GarbageFramebuffer const& data) :
266 _framebuffer{data}
267 {}
268 explicit constexpr Data(GarbageRenderPass const& data) :
269 _renderPass{data}
270 {}
271 explicit constexpr Data(GarbageSampler const& data) :
272 _sampler{data}
273 {}
274 explicit constexpr Data(GarbageImage const& data) :
275 _image{data}
276 {}
277
278 GarbageGeneric _generic;
279 GarbageDescriptorSet _descriptorSet;
280 GarbageBuffer _buffer;
281 GarbageGraphicPipeline _graphicPipeline;
282 GarbagePipelineLayout _pipelineLayout;
283 GarbageCommandPool _commandPool;
284 GarbageCommandBuffer _commandBuffer;
285 GarbageFramebuffer _framebuffer;
286 GarbageRenderPass _renderPass;
287 GarbageSampler _sampler;
288 GarbageImage _image;
289 };
290
291 Data g_data;
292};
293
303class FGE_API GarbageCollector
304{
305public:
306 using ContainerType = std::vector<Garbage>;
307
308 GarbageCollector() = default;
309 GarbageCollector(GarbageCollector const& r) = delete;
310 GarbageCollector(GarbageCollector&& r) noexcept;
311 ~GarbageCollector() = default;
312
313 GarbageCollector& operator=(GarbageCollector const& r) = delete;
314 GarbageCollector& operator=(GarbageCollector&& r) noexcept = delete;
315
325 void setCurrentFrame(uint32_t frame);
326 [[nodiscard]] uint32_t getCurrentFrame() const;
335 void push(Garbage garbage) const;
341 void free();
348 void freeAll();
349
363 void enable(bool stat);
364 [[nodiscard]] bool isEnabled() const;
365
366private:
367 mutable std::array<ContainerType, FGE_MAX_FRAMES_IN_FLIGHT> g_containers;
368 uint32_t g_currentFrame = 0;
369 bool g_enabled = false;
370};
371
372} // namespace fge::vulkan
373
374#endif //_FGE_VULKAN_C_GARBAGECOLLECTOR_HPP_INCLUDED
Vulkan context.
Definition C_context.hpp:70
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:193
Definition vulkanGlobal.hpp:44
Definition C_garbageCollector.hpp:68
Definition C_garbageCollector.hpp:116
Definition C_garbageCollector.hpp:104
Definition C_garbageCollector.hpp:52
Definition C_garbageCollector.hpp:130
Definition C_garbageCollector.hpp:48
Definition C_garbageCollector.hpp:80
Definition C_garbageCollector.hpp:166
Definition C_garbageCollector.hpp:92
Definition C_garbageCollector.hpp:142
Definition C_garbageCollector.hpp:154