Fling Engine  0.00.1
Fling Engine is a game engine written in Vulkan
Renderer.h
Go to the documentation of this file.
1 #pragma once
2 
3 // Resolve warnings
4 #include "Platform.h"
5 
6 #include "FlingVulkan.h"
7 
8 #include <GLFW/glfw3.h>
9 
10 #include "Singleton.hpp"
11 
12 #include "Vertex.h"
13 #include "UniformBufferObject.h"
14 #include "FirstPersonCamera.h"
15 
16 #include "FlingWindow.h"
17 #include "Instance.h"
18 #include "PhyscialDevice.h"
19 #include "LogicalDevice.h"
20 #include "Buffer.h"
21 #include "SwapChain.h"
22 #include "DepthBuffer.h"
23 #include "Model.h"
24 
25 namespace Fling
26 {
27  // File resource
28  class File;
29 
33  class Renderer : public Singleton<Renderer>
34  {
35  public:
36 
37  virtual void Init() override;
38 
39  virtual void Shutdown() override;
40 
46  void CreateGameWindow( const UINT32 t_width, const UINT32 t_height );
47 
49 
51  void Tick();
52 
56  void DrawFrame();
57 
61  void PrepShutdown();
62 
68  const VkDevice& GetLogicalVkDevice() const { return m_LogicalDevice->GetVkDevice(); }
69 
71 
77  const VkPhysicalDevice& GetPhysicalVkDevice() const { return m_PhysicalDevice->GetVkPhysicalDevice(); }
78 
80 
81  const VkCommandPool& GetCommandPool() const { return m_CommandPool; }
82 
83  const VkQueue& GetGraphicsQueue() const { return m_LogicalDevice->GetGraphicsQueue(); }
84 
85  void SetFrameBufferHasBeenResized(bool t_Setting){ m_FrameBufferResized = t_Setting; }
86 
87  const VkSurfaceKHR& GetVkSurface() const { return m_Surface; }
88 
89  Swapchain* GetSwapChain() const { return m_SwapChain; }
90 
91  private:
92 
96  void InitGraphics();
97 
102  void CreateDescriptorLayout();
103 
107  void CreateGraphicsPipeline();
108 
112  void CreateRenderPass();
113 
117  void CreateFrameBuffers();
118 
122  void CreateCommandPool();
123 
124  void CreateCommandBuffers();
125 
129  void CreateSyncObjects();
130 
131  void CleanupFrameResources();
132 
136  void RecreateFrameResources();
137 
138  void CreateUniformBuffers();
139 
140  void CreateDescriptorPool();
141 
142  void CreateDescriptorSets();
143 
150  SwapChainSupportDetails QuerySwapChainSupport(VkPhysicalDevice t_Device);
151 
161  VkSurfaceFormatKHR ChooseSwapChainSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& t_AvailableFormats);
162 
171  VkPresentModeKHR ChooseSwapChainPresentMode(const std::vector< VkPresentModeKHR>& t_AvialableFormats);
172 
178  VkExtent2D ChooseSwapExtent();
179 
185  void UpdateUniformBuffer(UINT32 t_CurrentImage);
186 
193  VkShaderModule CreateShaderModule(std::shared_ptr<File> t_ShaderCode);
194 
196  std::unique_ptr<FirstPersonCamera> m_camera;
197 
199 
200  Instance* m_Instance = nullptr;
201 
203 
205 
207  VkSurfaceKHR m_Surface = VK_NULL_HANDLE;
208 
209  Swapchain* m_SwapChain = nullptr;
210 
211  VkRenderPass m_RenderPass;
212 
214  VkDescriptorSetLayout m_DescriptorSetLayout;
215  VkPipelineLayout m_PipelineLayout;
216 
217  VkPipeline m_GraphicsPipeline;
218 
220  VkCommandPool m_CommandPool;
221 
223  VkDescriptorPool m_DescriptorPool;
224 
226 
227  size_t CurrentFrameIndex = 0;
228 
230  bool m_FrameBufferResized = false;
231 
232  static const int MAX_FRAMES_IN_FLIGHT;
233 
235  std::vector<Buffer*> m_UniformBuffers;
236 
237  std::vector<VkDescriptorSet> m_DescriptorSets;
238 
243  std::vector<VkFramebuffer> m_SwapChainFramebuffers;
244 
249  std::vector<VkCommandBuffer> m_CommandBuffers;
250 
251  std::vector<VkSemaphore> m_ImageAvailableSemaphores;
252  std::vector<VkSemaphore> m_RenderFinishedSemaphores;
253  std::vector<VkFence> m_InFlightFences;
254 
255  std::shared_ptr<class Image> m_TestImage;
256 
257  std::shared_ptr<Model> m_TestModel;
258  };
259 } // namespace Fling
const VkQueue & GetGraphicsQueue() const
Definition: Renderer.h:83
void UpdateUniformBuffer(UINT32 t_CurrentImage)
Update the uniform buffer data.
Definition: Renderer.cpp:823
Core renderer for the application
Definition: Renderer.h:33
PhysicalDevice * GetPhysicalDevice() const
Definition: Renderer.h:79
void CreateSyncObjects()
Create semaphores and fence objects.
Definition: Renderer.cpp:453
const VkDevice & GetLogicalVkDevice() const
Get the logical graphics Device object.
Definition: Renderer.h:68
std::vector< VkCommandBuffer > m_CommandBuffers
Command buffers.
Definition: Renderer.h:249
const VkPhysicalDevice & GetPhysicalVkDevice() const
Get the Physical Device object used by this renderer.
Definition: Renderer.h:77
FlingWindow * m_CurrentWindow
Definition: Renderer.h:198
std::unique_ptr< FirstPersonCamera > m_camera
Camera Instance.
Definition: Renderer.h:196
std::vector< VkSemaphore > m_RenderFinishedSemaphores
Definition: Renderer.h:252
VkCommandPool m_CommandPool
Definition: Renderer.h:220
Base class that represents a window to the Fling Engine.
Definition: FlingWindow.h:20
VkSurfaceKHR m_Surface
Handle to the surface extension used to interact with the windows system.
Definition: Renderer.h:207
VkPresentModeKHR ChooseSwapChainPresentMode(const std::vector< VkPresentModeKHR > &t_AvialableFormats)
Choose a present mode for the swap chain based on the given formats.
Definition: Renderer.cpp:662
LogicalDevice * GetLogicalDevice() const
Definition: Renderer.h:70
const VkDevice & GetVkDevice() const
Definition: LogicalDevice.h:18
A physical device represents the Vulkan physical device (the GPU) that we are currently using...
Definition: PhyscialDevice.h:11
LogicalDevice * m_LogicalDevice
Definition: Renderer.h:202
VkPipelineLayout m_PipelineLayout
Definition: Renderer.h:215
void CreateCommandPool()
Create the command pool to be sent every frame.
Definition: Renderer.cpp:371
void CreateGraphicsPipeline()
Create the graphics pipeline (IA, VS, FS, etc)
Definition: Renderer.cpp:156
VkRenderPass m_RenderPass
Definition: Renderer.h:211
void CleanupFrameResources()
Definition: Renderer.cpp:477
Class that can have only one instance.
Definition: Singleton.hpp:11
virtual void Shutdown() override
Definition: Renderer.cpp:866
static const int MAX_FRAMES_IN_FLIGHT
Definition: Renderer.h:232
std::vector< VkFramebuffer > m_SwapChainFramebuffers
The frame buffers for the swap chain.
Definition: Renderer.h:243
A logical device represents the application view of the device.
Definition: LogicalDevice.h:10
Swapchain * GetSwapChain() const
Definition: Renderer.h:89
The instance is a representation of this application graphics instance in Vulkan. ...
Definition: Instance.h:11
void CreateFrameBuffers()
Create the frame buffers for use by the swap chain.
Definition: Renderer.cpp:338
std::vector< VkFence > m_InFlightFences
Definition: Renderer.h:253
void RecreateFrameResources()
Re-create the image views, render passes, and command buffers.
Definition: Renderer.cpp:508
void SetFrameBufferHasBeenResized(bool t_Setting)
Definition: Renderer.h:85
VkExtent2D ChooseSwapExtent()
Determine the best match extents based on our window width and height.
Definition: Renderer.cpp:681
void CreateRenderPass()
Create the frame buffer that will be used by the graphics pipeline.
Definition: Renderer.cpp:57
Represents a swap chain that can be used throughout the program.
Definition: SwapChain.h:19
std::vector< VkDescriptorSet > m_DescriptorSets
Definition: Renderer.h:237
SwapChainSupportDetails QuerySwapChainSupport(VkPhysicalDevice t_Device)
Check the swap chain support of a given device.
Definition: Renderer.cpp:623
void Tick()
Happens before draw frame.
Definition: Renderer.cpp:759
const VkQueue & GetGraphicsQueue() const
Definition: LogicalDevice.h:20
void CreateGameWindow(const UINT32 t_width, const UINT32 t_height)
Init GLFW and create the game window
Definition: Renderer.cpp:722
std::vector< Buffer * > m_UniformBuffers
Uniform buffers.
Definition: Renderer.h:235
virtual void Init() override
Definition: Renderer.cpp:13
VkPipeline m_GraphicsPipeline
Definition: Renderer.h:217
Instance * m_Instance
Definition: Renderer.h:200
void CreateDescriptorLayout()
Create a Descriptor Layout object.
Definition: Renderer.cpp:125
void CreateDescriptorPool()
Definition: Renderer.cpp:545
const VkCommandPool & GetCommandPool() const
Definition: Renderer.h:81
void InitGraphics()
Init the current graphics API
Definition: Renderer.cpp:21
std::vector< VkSemaphore > m_ImageAvailableSemaphores
Definition: Renderer.h:251
Swapchain * m_SwapChain
Definition: Renderer.h:209
const VkSurfaceKHR & GetVkSurface() const
Definition: Renderer.h:87
VkDescriptorPool m_DescriptorPool
Definition: Renderer.h:223
std::shared_ptr< class Image > m_TestImage
Definition: Renderer.h:255
void CreateCommandBuffers()
Definition: Renderer.cpp:385
void CreateUniformBuffers()
Definition: Renderer.cpp:531
size_t CurrentFrameIndex
Definition: Renderer.h:227
void CreateDescriptorSets()
Definition: Renderer.cpp:567
VkShaderModule CreateShaderModule(std::shared_ptr< File > t_ShaderCode)
Create a shader module based on the given shader code.
Definition: Renderer.cpp:706
void PrepShutdown()
Prepare for shutdown of the rendering pipeline, close any open semaphores.
Definition: Renderer.cpp:850
uint32_t UINT32
Definition: FlingTypes.h:10
const VkPhysicalDevice & GetVkPhysicalDevice() const
Definition: PhyscialDevice.h:21
Definition: Engine.h:13
VkDescriptorSetLayout m_DescriptorSetLayout
Pipeline layout stores uniforms (global shader vars)
Definition: Renderer.h:214
DepthBuffer * m_DepthBuffer
Definition: Renderer.h:225
void DrawFrame()
Draw the frame!
Definition: Renderer.cpp:764
PhysicalDevice * m_PhysicalDevice
Definition: Renderer.h:204
Definition: SwapChain.h:8
FlingWindow * GetCurrentWindow() const
Definition: Renderer.h:48
bool m_FrameBufferResized
Used to determine if the frame buffer has been resized or not.
Definition: Renderer.h:230
VkSurfaceFormatKHR ChooseSwapChainSurfaceFormat(const std::vector< VkSurfaceFormatKHR > &t_AvailableFormats)
Choose a swap chain format based on the available formats.
Definition: Renderer.cpp:649
std::shared_ptr< Model > m_TestModel
Definition: Renderer.h:257
Definition: DepthBuffer.h:8