PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
Graphics.hpp
1#pragma once
2
3#include "PixelBullet/Graphics/Renderer.hpp"
4#include "PixelBullet/Time/Elapsed.hpp"
5
6#include <volk.h>
7
8#include <filesystem>
9#include <map>
10#include <memory>
11#include <string>
12#include <thread>
13#include <vector>
14
15namespace PixelBullet
16{
17 class CommandPool;
18 class RenderStage;
19 class Descriptor;
20 class PhysicalDevice;
21 class LogicalDevice;
22 class Swapchain;
23 class CommandBuffer;
24 class Instance;
25 class Surface;
26 class VirtualPath;
27
32 {
33 public:
34 Graphics();
35 ~Graphics();
36
37 void Update();
38
39 static std::string StringifyResultVk(VkResult result);
40 static void CheckVk(VkResult result);
41
42 template <typename T>
43 inline static const T* SafeVkPtr(const std::vector<T>& vec)
44 {
45 return vec.empty() ? nullptr : vec.data();
46 }
47
48 template <typename T, std::size_t N>
49 inline static const T* SafeVkPtr(const std::array<T, N>& arr)
50 {
51 return N == 0 ? nullptr : arr.data();
52 }
53
58 void CaptureScreenshot(const VirtualPath& filename, std::size_t id = 0) const;
59
60 const std::shared_ptr<CommandPool>&
61 GetCommandPool(const std::thread::id& threadId = std::this_thread::get_id());
62
68 {
69 return m_Renderer.get();
70 }
71
76 void SetRenderer(std::unique_ptr<Renderer>&& renderer)
77 {
78 this->m_Renderer = std::move(renderer);
79 }
80
81 const RenderStage* GetRenderStage(uint32_t index) const;
82
83 const Descriptor* GetAttachment(const std::string& name) const;
84
85 const PhysicalDevice* GetPhysicalDevice() const
86 {
87 return m_PhysicalDevice.get();
88 }
89 const LogicalDevice* GetLogicalDevice() const
90 {
91 return m_LogicalDevice.get();
92 }
93 const VkPipelineCache& GetPipelineCache() const
94 {
95 return m_PipelineCache;
96 }
97
98 const Surface* GetSurface(std::size_t id) const
99 {
100 return m_Surfaces[id].get();
101 }
102 const Swapchain* GetSwapchain(std::size_t id) const
103 {
104 return m_Swapchains[id].get();
105 }
106 void SetFramebufferResized(std::size_t id)
107 {
108 m_PerSurfaceBuffers[id]->FramebufferResized = true;
109 }
110
111 const Instance* GetInstance() const
112 {
113 return m_Instance.get();
114 }
115
116 private:
117 void CreatePipelineCache();
118 void ResetRenderStages();
119 void RecreateSwapchain();
120 void RecreateCommandBuffers(std::size_t id);
121 void RecreatePass(std::size_t id, RenderStage& renderStage);
122 void RecreateAttachmentsMap();
123 bool StartRenderpass(std::size_t id, RenderStage& renderStage);
124 void EndRenderpass(std::size_t id, RenderStage& renderStage);
125
126 private:
127 std::unique_ptr<Renderer> m_Renderer;
128 std::map<std::string, const Descriptor*> m_Attachments;
129 std::vector<std::unique_ptr<Swapchain>> m_Swapchains;
130
131 std::map<std::thread::id, std::shared_ptr<CommandPool>> m_CommandPools;
133 Elapsed m_ElapsedPurge;
134
135 struct PerSurfaceBuffers
136 {
137 std::vector<VkSemaphore> PresentCompletes;
138 std::vector<VkSemaphore> RenderCompletes;
139 std::vector<VkFence> FlightFences;
140 std::size_t CurrentFrame = 0;
141 bool FramebufferResized = false;
142
143 std::vector<std::unique_ptr<CommandBuffer>> CommandBuffers;
144 };
145 std::vector<std::unique_ptr<PerSurfaceBuffers>> m_PerSurfaceBuffers;
146
147 std::unique_ptr<Instance> m_Instance;
148 std::unique_ptr<PhysicalDevice> m_PhysicalDevice;
149 std::unique_ptr<LogicalDevice> m_LogicalDevice;
150 VkPipelineCache m_PipelineCache = VK_NULL_HANDLE;
151 std::vector<std::unique_ptr<Surface>> m_Surfaces;
152 };
153} // namespace PixelBullet
Definition Descriptor.hpp:75
Module that manages the Vulkan m_Instance, Surface, Window and the renderpass structure.
Definition Graphics.hpp:32
Renderer * GetRenderer() const
Definition Graphics.hpp:67
void CaptureScreenshot(const VirtualPath &filename, std::size_t id=0) const
Definition Graphics.cpp:206
void SetRenderer(std::unique_ptr< Renderer > &&renderer)
Definition Graphics.hpp:76
Definition PhysicalDevice.hpp:12
Definition RenderStage.hpp:205
Class used to manage Subrender objects to create a list of render pass.
Definition Renderer.hpp:12
Definition VirtualPath.hpp:11