VEX Engine
Retro looking game engine made in vulkan mostly because i wanted to understand it better.
Loading...
Searching...
No Matches
Renderer.hpp
Go to the documentation of this file.
1
6
7#pragma once
8
10#include "context.hpp"
11#include "Resources.hpp"
12#include "Pipeline.hpp"
13#include "SwapchainManager.hpp"
15#include "VulkanMesh.hpp"
17#include "MeshManager.hpp"
18#include "PhysicsDebug.hpp"
19
20#include <glm/glm.hpp>
21#include <chrono>
25
26namespace vex {
28 struct RenderItem {
29 vex::Entity entity;
30 uint32_t modelIndex;
31 };
32
35 vex::Entity entity;
36 uint32_t texID;
37 };
38
40 vex::Entity entity;
41 uint32_t texID;
42 float offsetX;
43 };
44
47 glm::vec3 pos;
48 float sx;
49 float sy;
50 uint32_t tID;
51 uint32_t unlit;
52 float pad;
53 glm::vec4 col;
54 };
55
58 VkCommandBuffer commandBuffer;
59 uint32_t frameIndex;
60 uint32_t imageIndex;
61 VkDescriptorSet imguiTextureID;
62 bool isSwapchainValid;
63 };
64
66 class Renderer {
67 public:
83 Renderer(VulkanContext& context,
84 std::unique_ptr<VulkanResources>& resources,
85 std::unique_ptr<VulkanPipeline>& pipeline,
86 std::unique_ptr<VulkanPipeline>& transPipeline,
87 std::unique_ptr<VulkanPipeline>& maskPipeline,
88 std::unique_ptr<VulkanPipeline>& billboardTransPipeline,
89 std::unique_ptr<VulkanPipeline>& billboardMaskedPipeline,
90 std::unique_ptr<VulkanPipeline>& particleTransPipeline,
91 std::unique_ptr<VulkanPipeline>& particleMaskedPipeline,
92 std::unique_ptr<VulkanPipeline>& uiPipeline,
93 std::unique_ptr<VulkanPipeline>& fullscreenPipeline,
94 std::unique_ptr<VulkanPipeline>& compositePipeline,
95 std::unique_ptr<VulkanSwapchainManager>& swapchainManager,
96 std::unique_ptr<MeshManager>& meshManager);
97
100 ~Renderer();
101
107 bool beginFrame(glm::uvec2 renderResolution, SceneRenderData& outData);
108
123 void renderScene(SceneRenderData& data,
124 const vex::Entity cameraEntity,
125 vex::Registry& registry,
126 int frame,
127 const std::vector<DebugVertex>* debugLines = nullptr,
128 bool isEditorMode = false);
129
134 VkDescriptorSet getImGuiTextureID(ImGUIWrapper& ui);
135
143 void composeFrame(SceneRenderData& data,
144 ImGUIWrapper& ui,
145 bool isEditorMode);
146
150 void endFrame(SceneRenderData& data);
151
152 #if DEBUG
155 void setDebugPipeline(std::unique_ptr<VulkanPipeline>* pipeline) { m_pp_debugPipeline = pipeline; }
156
162 void renderDebug(VkCommandBuffer cmd, int frameIndex, const std::vector<DebugVertex>& lines);
163 #endif
164
165 private:
176 void transitionImageLayout(VkCommandBuffer cmd, VkImage image,
177 VkImageLayout oldLayout, VkImageLayout newLayout,
178 VkAccessFlags srcAccessMask, VkAccessFlags dstAccessMask,
179 VkPipelineStageFlags srcStage, VkPipelineStageFlags dstStage);
180
184 void issueMultiDrawIndexed(VkCommandBuffer cmd, const std::vector<VkMultiDrawIndexedInfoEXT>& commands);
185
188 void updateScreenDescriptor(VkImageView view);
189
190 VulkanContext& m_r_context;
191 std::unique_ptr<VulkanResources>& m_p_resources;
192 std::unique_ptr<VulkanPipeline>& m_p_pipeline;
193 std::unique_ptr<VulkanPipeline>& m_p_transPipeline;
194 std::unique_ptr<VulkanPipeline>& m_p_maskPipeline;
195 std::unique_ptr<VulkanPipeline>& m_p_billboardTransPipeline;
196 std::unique_ptr<VulkanPipeline>& m_p_billboardMaskedPipeline;
197 std::unique_ptr<VulkanPipeline>& m_p_particleTransPipeline;
198 std::unique_ptr<VulkanPipeline>& m_p_particleMaskedPipeline;
199 std::unique_ptr<VulkanPipeline>& m_p_uiPipeline;
200 std::unique_ptr<VulkanPipeline>& m_p_fullscreenPipeline;
201 std::unique_ptr<VulkanPipeline>& m_p_compositePipeline;
202 std::unique_ptr<VulkanSwapchainManager>& m_p_swapchainManager;
203 std::unique_ptr<MeshManager>& m_p_meshManager;
204
205 std::chrono::high_resolution_clock::time_point startTime;
206 float currentTime = 0.0f;
207
208 bool basicDiag = true;
209
210 std::vector<TransparentTriangle> m_transparentTriangles;
211 std::map<uint32_t, glm::mat4> trnasMatrixes;
212 std::vector<UiComponent> m_uiObjects;
213 std::vector<VkMultiDrawIndexedInfoEXT> m_multiDrawInfos;
214 size_t approxTriangles = 0;
215
216 SceneUBO m_sceneUBO;
217
218 VkDescriptorSet m_screenDescriptorSet = VK_NULL_HANDLE;
219 VkDescriptorSet m_crtDescriptorSet = VK_NULL_HANDLE;
220 VkSampler m_screenSampler = VK_NULL_HANDLE;
221 VkSampler m_linearSampler = VK_NULL_HANDLE;
222 VkImageView m_lastUsedView = VK_NULL_HANDLE;
223 VkDescriptorSet m_cachedImGuiDescriptor = VK_NULL_HANDLE;
224 std::vector<std::vector<VkDescriptorSet>> m_garbageDescriptors;
225 VkDescriptorPool m_localPool = VK_NULL_HANDLE;
226
227 std::vector<RenderItem> opaqueQueue;
228 std::vector<RenderItem> maskedQueue;
229 std::vector<RenderItem> transparentQueue;
230 std::vector<BillboardItem> bMaskedQueue;
231 std::vector<BillboardItem> bTransQueue;
232 std::vector<EditorBillboardItem> bEditorQueue;
233 std::vector<vex::Entity> pMaskedQueue;
234 std::vector<vex::Entity> pTransQueue;
235
236 std::vector<VkBuffer> m_indirectBuffers;
237 std::vector<VmaAllocation> m_indirectAllocations;
238
239 #if DEBUG
240 std::vector<VkBuffer> m_debugBuffers;
241 std::vector<VmaAllocation> m_debugAllocations;
242 std::unique_ptr<VulkanPipeline>* m_pp_debugPipeline = nullptr;
243 std::unique_ptr<VulkanMesh> m_editorCameraVulkanMesh;
244 MeshData m_editorCameraMesh;
245 #endif
246 };
247}
Contains basic components like transform, camera, name components..
Simple 2D quad rendered in 3d space, great for placeholders or visualising invisible objects.
Particle emitter component.
This file defines VulkanPipeline Class.
This file defines VulkanResources Class.
This file defines VulkanSwapchainManager Class.
Component you add to your GameObject to add ui elements to your game. ex. hud to player.
This file defines vex ui class, very basic ui system.
This file defines class with Imgui for vulkan backend definition.
This file defines VulkanMesh class.
This class provides an interface template for ImGui Implementation. Every backend should implement th...
Central registry for managing entities and their components in the ECS system.
Definition Registry.hpp:29
std::vector< vex::Entity > pMaskedQueue
Queue of entities with masked particle emitters.
Definition Renderer.hpp:233
void issueMultiDrawIndexed(VkCommandBuffer cmd, const std::vector< VkMultiDrawIndexedInfoEXT > &commands)
Issues a multi-draw indexed command used by transparent meshes since they are drawn triangle by trian...
std::unique_ptr< VulkanPipeline > & m_p_transPipeline
Standard transparent geometry pipeline.
Definition Renderer.hpp:193
bool beginFrame(glm::uvec2 renderResolution, SceneRenderData &outData)
Prepares the frame for rendering.
Definition Renderer.cpp:227
std::unique_ptr< VulkanPipeline > & m_p_billboardTransPipeline
Transparent billboard rendering pipeline.
Definition Renderer.hpp:195
void transitionImageLayout(VkCommandBuffer cmd, VkImage image, VkImageLayout oldLayout, VkImageLayout newLayout, VkAccessFlags srcAccessMask, VkAccessFlags dstAccessMask, VkPipelineStageFlags srcStage, VkPipelineStageFlags dstStage)
Helper function for image transition.
void composeFrame(SceneRenderData &data, ImGUIWrapper &ui, bool isEditorMode)
Composes the final frame onto the Swapchain image.
void updateScreenDescriptor(VkImageView view)
Updates the screen descriptor.
std::vector< BillboardItem > bTransQueue
Queue of transparent billboards to render.
Definition Renderer.hpp:231
std::unique_ptr< VulkanPipeline > & m_p_maskPipeline
Standard masked geometry pipeline.
Definition Renderer.hpp:194
std::vector< EditorBillboardItem > bEditorQueue
Queue of editor billboards to render.
Definition Renderer.hpp:232
std::unique_ptr< VulkanPipeline > & m_p_particleMaskedPipeline
Masked particle rendering pipeline.
Definition Renderer.hpp:198
VkDescriptorSet getImGuiTextureID(ImGUIWrapper &ui)
Gets or creates a cached ImGui texture descriptor for the scene.
std::vector< RenderItem > maskedQueue
Queue of masked (alpha-cutout) 3D objects to render.
Definition Renderer.hpp:228
std::unique_ptr< VulkanPipeline > & m_p_billboardMaskedPipeline
Masked billboard rendering pipeline.
Definition Renderer.hpp:196
std::vector< vex::Entity > pTransQueue
Queue of entities with transparent particle emitters.
Definition Renderer.hpp:234
~Renderer()
Destructor for Renderer class.
Definition Renderer.cpp:167
std::vector< BillboardItem > bMaskedQueue
Queue of masked billboards to render.
Definition Renderer.hpp:230
Renderer(VulkanContext &context, std::unique_ptr< VulkanResources > &resources, std::unique_ptr< VulkanPipeline > &pipeline, std::unique_ptr< VulkanPipeline > &transPipeline, std::unique_ptr< VulkanPipeline > &maskPipeline, std::unique_ptr< VulkanPipeline > &billboardTransPipeline, std::unique_ptr< VulkanPipeline > &billboardMaskedPipeline, std::unique_ptr< VulkanPipeline > &particleTransPipeline, std::unique_ptr< VulkanPipeline > &particleMaskedPipeline, std::unique_ptr< VulkanPipeline > &uiPipeline, std::unique_ptr< VulkanPipeline > &fullscreenPipeline, std::unique_ptr< VulkanPipeline > &compositePipeline, std::unique_ptr< VulkanSwapchainManager > &swapchainManager, std::unique_ptr< MeshManager > &meshManager)
Constructor for Renderer class.
Definition Renderer.cpp:56
void endFrame(SceneRenderData &data)
Submits the command buffer and presents the image.
std::vector< RenderItem > transparentQueue
Queue of transparent 3D objects to render.
Definition Renderer.hpp:229
std::unique_ptr< VulkanPipeline > & m_p_particleTransPipeline
Transparent particle rendering pipeline.
Definition Renderer.hpp:197
void renderScene(SceneRenderData &data, const vex::Entity cameraEntity, vex::Registry &registry, int frame, const std::vector< DebugVertex > *debugLines=nullptr, bool isEditorMode=false)
Renders the 3D scene and UI to an offscreen low-res texture.
Definition Renderer.cpp:345
std::vector< RenderItem > opaqueQueue
Queue of opaque 3D objects to render.
Definition Renderer.hpp:227
This file defines struct holding all vulkan data, like device, surface, swapchain,...
uint32_t Entity
Type alias representing a unique entity identifier in the ECS system.
Definition Types.hpp:11
Struct to hold information about a billboard render item.
Definition Renderer.hpp:34
Struct to hold push data for billboard rendering.
Definition Renderer.hpp:46
uint32_t tID
Texture ID for bindless access.
Definition Renderer.hpp:50
uint32_t unlit
Flag determining if the billboard is unlit.
Definition Renderer.hpp:51
glm::vec4 col
Tint color of the billboard.
Definition Renderer.hpp:53
float sx
Horizontal scale.
Definition Renderer.hpp:48
float sy
Vertical scale.
Definition Renderer.hpp:49
glm::vec3 pos
World position of the billboard.
Definition Renderer.hpp:47
float pad
Padding for 16-byte alignment.
Definition Renderer.hpp:52
Mesh data structure for loading and managing mesh data.
Definition Mesh.hpp:41
Struct to hold information about a render item.
Definition Renderer.hpp:28
Data structure to pass state between render stages.
Definition Renderer.hpp:57
Scene uniform buffer object. Holds data that is bind once per scene.
Definition uniforms.hpp:13
Struct holding all vulkan data, like device, surface, swapchain, images, views, and more.
Definition context.hpp:26