27 #define GLM_FORCE_RADIANS 28 #define GLM_FORCE_DEPTH_ZERO_TO_ONE 30 #include <ThirdParty/glm/glm/glm.hpp> 31 #include <ThirdParty/glm/glm/gtc/matrix_transform.hpp> 35 #include <ThirdParty/EASTL-master/include/EASTL/string.h> 36 #include <ThirdParty/EASTL-master/include/EASTL/array.h> 37 #include <ThirdParty/EASTL-master/include/EASTL/vector.h> 38 #include <ThirdParty/EASTL-master/include/EASTL/memory.h> 39 #include <ThirdParty/EASTL-master/include/EASTL/chrono.h> 46 const bool enableValidationLayers =
false;
48 const bool enableValidationLayers =
true;
54 class ENGINE_API VulkanRenderer :
public Renderer {
57 friend VulkanPipeline;
60 VulkanRenderer() noexcept;
61 VulkanRenderer(VulkanRenderer const &other) = default;
62 VulkanRenderer(VulkanRenderer &&other) noexcept = default;
72 void InitializeRenderer();
79 void InitializeRenderer(eastl::weak_ptr<VulkanWindow> window);
90 void setClearColor(glm::vec4 color);
96 VkRenderPass GetRenderPass() const;
102 VkRenderPass GetGBufferRenderPass() const;
108 VmaAllocator GetVmaAllocator() const;
115 VkCommandPool GetGraphicsCommandPool() const;
122 VkCommandPool GetComputeCommandPool() const;
128 VkExtent2D GetSwapChainExtent() const;
137 virtual
void RendererBegin();
143 virtual
void RendererBegin(const glm::mat4x4& view, const glm::mat4x4& projection);
157 virtual
void Render(const glm::mat4x4& modelMatrix, eastl::shared_ptr<Model> model, const glm::vec4& mainColor = glm::vec4(1.f, 1.f, 1.f, 1.f));
164 virtual
void RenderSprite(eastl::weak_ptr<Texture> texture, glm::mat4 modelMatrix);
172 virtual
void RenderLine(glm::vec3 start, glm::vec3 end, glm::vec4 color = glm::vec4(1.f, 1.f, 1.f, 1.f));
179 virtual
void RendererEnd();
189 void StartSecondaryCommandBufferRecording(VkCommandBuffer buffer, VkCommandBufferUsageFlags flags, VkRenderPass renderPass, uint32_t subPass, VkFramebuffer framebuffer) const;
195 void EndSecondaryCommandBufferRecording(VkCommandBuffer buffer) const;
201 enum class GBufferSubPasses {
210 enum class SsaoSubPasses {
219 enum class RenderSubPasses {
242 void CreateLight(eastl::string name,
LightType lightType,
243 glm::vec3 position, glm::vec3 direction, glm::vec3 color,
244 float radius,
float attunuation,
float coneInnerAngle,
float coneOuterAngle);
251 void SetLightType(eastl::string name,
LightType lightType);
258 LightType GetLightType(eastl::string name);
265 void SetLightPosition(eastl::string name, glm::vec3 position);
272 glm::vec3 GetLightPosition(eastl::string name);
279 void SetLightDirection(eastl::string name, glm::vec3 direction);
286 glm::vec3 GetLightDirection(eastl::string name);
293 void SetLightColor(eastl::string name, glm::vec3 color);
300 glm::vec3 GetLightColor(eastl::string name);
307 void SetLightRadius(eastl::string name,
float radius);
314 float GetLightRadius(eastl::string name);
321 void SetLightConeInnerAngle(eastl::string name,
float angle);
328 float GetLightConeInnerAngle(eastl::string name);
335 void SetLightConeOuterAngle(eastl::string name,
float angle);
342 float GetLightConeOuterAngle(eastl::string name);
348 VkDescriptorSetLayout CreateLightDescriptorSetLayout();
357 VkDescriptorSet CreateLightDescriptorSet(
size_t threadID,
size_t pipelineID,
size_t set, VkDescriptorSetLayout layout);
366 VkDescriptorSet GetLightDescriptorSet(
size_t threadID,
size_t pipelineID,
size_t set);
372 VkFramebuffer GetFrameBuffer();
380 VulkanDescriptorPool* GetDescriptorPool(
size_t thread);
387 size_t GetThreadCount();
390 void CreateInstance();
391 void FindPhysicalDevice();
392 void CreateLogicalDevice();
393 void CreateSwapChain();
394 void GetSwapChainImages();
395 void CreateSwapChainImageViews();
396 void CreateGraphicsCommandPool();
397 void CreateComputeCommandPool();
398 void CreateVmaAllocator();
399 void CreateDepthImage();
400 void CreateAttachmentImages();
401 void CreateRenderPass();
402 void CreateFramebuffers();
403 void GenerateCommandBuffers();
404 void CreateDescriptorPool();
406 void CreateSemaphores();
407 void CreateCompositingPipeline();
408 void CreateGBufferRenderPipeline();
409 void CreateSsaoPipelines();
410 void CreateRenderers();
411 void AllocateThreads();
413 void RecreateSwapChain();
415 void DestroyInstance();
416 void DestroyLogicalDevice();
417 void DestroySwapChain();
418 void DestroySwapChainImageViews();
419 void DestroyGraphicsCommandPool();
420 void DestroyComputeCommandPool();
421 void DestroyVmaAllocator();
422 void DestroyDepthImage();
423 void DestroyAttachmentImages();
424 void DestroyRenderPass();
425 void DestroyFramebuffers();
426 void DestroyDecriptorPool();
427 void DestroyImGui()
const;
428 void DestroySemaphores();
429 void DestroyCompositingPipeline();
430 void DestroyGBufferRenderPipeline();
431 void DestroySsaoPipelines();
432 void DestroyRenderers();
433 void DestroyThreads();
435 VkFormat VulkanRenderer::FindSupportedDepthFormat(
const eastl::vector<VkFormat>& candidates, VkImageTiling tiling, VkFormatFeatureFlags features);
437 eastl::unique_ptr<VulkanInstance> vulkanInstance_;
438 eastl::unique_ptr<VulkanPhysicalDevice> vulkanPhysicalDevice_;
439 eastl::unique_ptr<VulkanLogicalDevice> vulkanLogicalDevice_;
440 eastl::vector<eastl::shared_ptr<VulkanDescriptorPool>> vulkanDescriptorPools_;
448 VulkanDeviceFeatures_t requiredFeatures;
450 eastl::weak_ptr<VulkanWindow> window;
452 VkSurfaceKHR surface;
456 #pragma region Swapchain 458 VkSwapchainKHR swapChain;
461 VkFormat swapChainImageFormat;
462 VkExtent2D swapChainImageExtent;
464 uint32_t currentImage;
467 VkCommandPool graphicsCommandPool;
468 VkCommandPool computeCommandPool;
470 eastl::vector<VkImage> swapChainImages;
471 eastl::vector<VkImageView> swapChainImageViews;
475 #pragma region Depth image 478 VkImageView depthImageView;
479 VmaAllocation depthImageAllocation;
480 VmaAllocationInfo depthImageAllocationInfo;
482 VkFormat depthImageFormat;
486 #pragma region Renderpass 488 enum class GBufferAttachments {
496 enum class SsaoAttachments {
501 enum class RenderPassAttachments {
502 COLOR_ATTACHMENT = 0,
506 SSAO_BLUR_ATTACHMENT,
514 eastl::vector<VkAttachmentDescription> gBufferPassAttachments;
515 eastl::vector<VkSubpassDescription> gBufferPassSubPasses;
517 eastl::vector<VkAttachmentDescription> ssaoPassAttachments;
518 eastl::vector<VkSubpassDescription> ssaoPassSubPasses;
520 eastl::vector<VkAttachmentDescription> renderPassAttachments;
521 eastl::vector<VkSubpassDescription> renderPassSubPasses;
523 VkRenderPass gBufferPass;
524 VkRenderPass ssaoPass;
525 VkRenderPass renderPass;
529 #pragma region Attachment images 532 VkImageView imguiImageView;
533 VmaAllocation imguiImageAllocation;
534 VmaAllocationInfo imguiImageAllocationInfo;
537 VkImageView renderImageView;
538 VmaAllocation renderImageAllocation;
539 VmaAllocationInfo renderImageAllocationInfo;
542 VkImageView albedoImageView;
543 VmaAllocation albedoImageAllocation;
544 VmaAllocationInfo albedoImageAllocationInfo;
546 VkImage positionImage;
547 VkImageView positionImageView;
548 VmaAllocation positionImageAllocation;
549 VmaAllocationInfo positionImageAllocationInfo;
552 VkImageView normalImageView;
553 VmaAllocation normalImageAllocation;
554 VmaAllocationInfo normalImageAllocationInfo;
557 VkImageView ssaoImageView;
558 VmaAllocation ssaoImageAllocation;
559 VmaAllocationInfo ssaoImageAllocationInfo;
561 VkImage ssaoBlurImage;
562 VkImageView ssaoBlurImageView;
563 VmaAllocation ssaoBlurImageAllocation;
564 VmaAllocationInfo ssaoBlurImageAllocationInfo;
570 #pragma region Framebuffers 572 eastl::vector<VkFramebuffer> framebuffers;
574 VkFramebuffer gBufferFrameBuffer;
575 VkFramebuffer ssaoFrameBuffer;
579 #pragma region CommandBuffers 581 eastl::vector<VkCommandBuffer> primaryRenderCommandBuffers_;
582 eastl::vector<VkCommandBuffer> primaryComputeCommandBuffers_;
584 eastl::vector<eastl::vector<VkCommandBuffer>> staticMeshShadowCommandBuffers_;
585 eastl::vector<eastl::vector<VkCommandBuffer>> skeletalMeshShadowCommandBuffers_;
586 eastl::vector<eastl::vector<VkCommandBuffer>> gBufferShadowCommandBuffers_;
588 VkCommandBuffer compositeCommandBuffer_;
590 VkCommandBuffer gBufferRenderCommandBuffer_;
592 eastl::vector<VkCommandBuffer> imguiCommandBuffers_;
594 VkCommandBuffer currentBuffer_;
595 VkCommandBuffer currentComputeBuffer_;
597 eastl::vector<VkCommandBuffer> spriteRenderCommandBuffers_;
599 eastl::vector<VkCommandBuffer> debugRenderCommandBuffers_;
601 eastl::vector<VkCommandBuffer> staticMeshCommandBuffers_;
603 eastl::vector<VkCommandBuffer> skeletalMeshCommandBuffers_;
605 eastl::vector<VkCommandBuffer> clearStencilCommandBuffers_;
607 void GenerateSecondaryCommandBuffers(VkCommandBuffer *buffers, uint32_t count);
608 void DestroySecondaryCommandBuffers(VkCommandBuffer*buffers, uint32_t count);
612 #pragma region Pipelines 614 eastl::unique_ptr<VulkanPipeline> compositingPipeline_;
616 eastl::unique_ptr<VulkanPipeline> gBufferRenderPipeline_;
618 eastl::unique_ptr<VulkanPipeline> ambientLightPipeline_;
620 eastl::unique_ptr<VulkanPipeline> ssaoPipeline_;
621 eastl::unique_ptr<VulkanPipeline> ssaoBlurPipeline_;
623 VkDescriptorSet gBufferAttachmentDescriptorSet_;
625 VkDescriptorSet SceneInfoDescriptorSet_;
627 VkDescriptorSet CompositingAttachmentDescriptorSet_;
629 VkDescriptorSet ssaoSamplerDescriptor_;
631 VkDescriptorSet projectionDescriptor_;
633 VkBuffer vertexBuffer_;
634 VmaAllocation vertexBufferAllocation_;
636 eastl::unique_ptr<VulkanBuffer> ssaoKernelBuffer_;
637 eastl::unique_ptr<VulkanBuffer> projectionBuffer_;
639 glm::mat4 projection_;
643 #pragma region Renderers 645 eastl::unique_ptr<VulkanSpriteRenderer> vulkanSpriteRenderer;
646 eastl::unique_ptr<VulkanDebugRenderer> vulkanDebugRenderer;
647 eastl::unique_ptr<VulkanStaticMeshRenderer> vulkanStaticMeshRenderer;
648 eastl::unique_ptr<VulkanSkeletalMeshRenderer> vulkanSkeletalMeshRenderer;
654 eastl::unique_ptr<VulkanBuffer> sceneDataBuffer;
655 eastl::unique_ptr<VulkanBuffer> lightBuffer;
657 eastl::array<Light, 1024> lightData;
669 size_t lightDataIndex;
676 eastl::map<eastl::string, LightInfo> lights;
678 eastl::vector<eastl::vector<eastl::vector<VkDescriptorSet>>> lightDescriptorSets_;
680 bool lightDataChanged;
681 bool lightDataRestructured;
683 void RestructureLights();
685 void UpdateLightData();
689 #pragma region Threads 693 std::packaged_task<void()> task;
694 std::future<void> future;
695 bool initialized =
false;
697 VkCommandPool commandPool;
700 eastl::vector<ThreadInfo*> threads;
702 ThreadInfo* GetFreeThread();
703 void JoinAllThreads();
704 void ResetCommandPools();
708 VmaAllocator vmaAllocator_;
710 eastl::vector<VkSemaphore> imageAvailableSemaphores_;
711 eastl::vector<VkSemaphore> renderFinishedSemaphores_;
712 eastl::vector<VkSemaphore> computeFinishedSemaphores_;
714 eastl::vector<VkFence> drawingFinishedFences_;
715 eastl::vector<VkFence> computeFinishedFences_;
717 VkClearValue clearValue;
727 #endif // USING_VULKAN
enum ENGINE_API LightType
Enum containing the different types of lights that can be created.