PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
graphics_state_internal.h
1#pragma once
2
3#include "graphics_frame_runtime_internal.h"
4
5#include "pixelbullet/application/launch_gpu_defaults_internal.h"
6#include "pixelbullet/graphics/render_device.h"
7#include "pixelbullet/graphics/render_target.h"
8#include "pixelbullet/graphics/renderer.h"
9#include "pixelbullet/time/elapsed.h"
10
11#include <map>
12#include <memory>
13#include <mutex>
14#include <optional>
15#include <string>
16#include <string_view>
17#include <thread>
18#include <vector>
19
20namespace pixelbullet
21{
23class CommandBuffer;
24class CommandPool;
25class Descriptor;
26class Instance;
27class LogicalDevice;
28class PhysicalDevice;
30class RenderTarget2D;
31class ImageDepth;
32class Surface;
33class Swapchain;
34
36{
37public:
38 static GraphicsState& GetState(Graphics& graphics) noexcept;
39 static const GraphicsState& GetState(const Graphics& graphics) noexcept;
40};
41
42class GraphicsState
43{
44public:
45 using CommandPoolKey = std::pair<std::thread::id, VkQueueFlagBits>;
46
48 {
49 std::vector<VkSemaphore> present_completes;
50 std::vector<VkSemaphore> render_completes;
51 std::vector<VkFence> flight_fences;
52 std::vector<VkFence> image_fences;
53 std::size_t current_frame = 0;
54 bool framebuffer_resized = false;
55
56 std::vector<std::unique_ptr<CommandBuffer>> command_buffers;
57 };
58
59 GraphicsState(Graphics& owner, const ApplicationSpecification& specification, const ApplicationLaunchOptions& launch_options,
60 Window& window);
61 ~GraphicsState();
62
63 Graphics::FrameOutcome Update();
64 GraphicsFrameRuntimeAccess::PrepareFrameOutcome PrepareFrame();
65 GraphicsFrameRuntimeAccess::RenderFrameOutcome RenderPreparedFrame();
66 void WaitIdle() const;
67 void CaptureScreenshot(const std::filesystem::path& filename, std::size_t id) const;
68
69 void SetRenderer(std::unique_ptr<Renderer>&& renderer);
70 const Descriptor* GetAttachment(const std::string& name) const;
71 const ImageDepth* GetDepthAttachment(std::string_view name) const;
72 std::optional<RenderTargetView> GetRenderTarget(std::string_view name) const;
73 void SetFramebufferResized(std::size_t id);
74
75 std::shared_ptr<CommandPool> GetCommandPool(VkQueueFlagBits queue_type = VK_QUEUE_GRAPHICS_BIT,
76 const std::thread::id& thread_id = std::this_thread::get_id()) const;
77
78 const PhysicalDevice* GetPhysicalDevice() const noexcept;
79 const LogicalDevice* GetLogicalDevice() const noexcept;
80 const VkPipelineCache& GetPipelineCache() const noexcept;
81 const Surface* GetSurface(std::size_t id) const noexcept;
82 const Swapchain* GetSwapchain(std::size_t id) const noexcept;
83 bool HasSwapchain(std::size_t id) const noexcept;
84 bool HasPreparedFrame() const noexcept;
85 uint32_t GetPreparedImageIndex(std::size_t id) const;
86 const Instance* GetInstance() const noexcept;
87
88 Graphics& owner;
89 application_internal::ResolvedLaunchGpuDefaults launch_gpu_defaults;
90 const ApplicationSpecification& specification;
91 Window& window;
92 RenderDevice render_device;
93 std::unique_ptr<Renderer> renderer;
94 std::map<std::string, const Descriptor*> attachments;
95 std::map<std::string, std::unique_ptr<RenderTarget2D>> render_targets;
96 std::vector<std::unique_ptr<RenderStageRuntime>> render_stage_runtimes;
97 std::vector<std::unique_ptr<Swapchain>> swapchains;
98 bool prepared_frame_ready = false;
99 mutable std::map<CommandPoolKey, std::shared_ptr<CommandPool>> command_pools;
100 mutable std::mutex command_pools_mutex;
101 Elapsed elapsed_purge;
102 std::vector<std::unique_ptr<PerSurfaceBuffers>> per_surface_buffers;
103 std::unique_ptr<Instance> instance;
104 std::unique_ptr<PhysicalDevice> physical_device;
105 std::unique_ptr<LogicalDevice> logical_device;
106 VkPipelineCache pipeline_cache = VK_NULL_HANDLE;
107 std::vector<std::unique_ptr<Surface>> surfaces;
108
109private:
110 void CreatePipelineCache();
111 void ResetRenderStages();
112 void RebuildRenderStages(bool recreate_swapchain);
113 void RecreateSwapchain();
114 void RecreateCommandBuffers(std::size_t id);
115 void RebuildRenderTargets();
116 void RecreateAttachmentsMap();
117 void StartRenderpass(std::size_t id, RenderStageRuntime& render_stage);
118 bool EndRenderpass(std::size_t id, RenderStageRuntime& render_stage);
119};
120} // namespace pixelbullet
Definition elapsed.h:9
Definition graphics_state_internal.h:36
Definition graphics_state_internal.h:43
Host-facing graphics subsystem shell.
Definition graphics.h:21
Definition instance.h:13
Definition logical_device.h:13
Definition physical_device.h:17
Definition render_device.h:15
Definition render_stage_runtime.h:27
Definition render_target_internal.h:15
Definition renderer.h:15
Definition surface.h:12
Definition window.h:34
Definition specification.h:69
Contains configuration options for both the game and the engine.
Definition specification.h:162
Definition graphics_state_internal.h:48
Definition launch_gpu_defaults.h:35