17#ifndef _FGE_VULKAN_C_CONTEXT_HPP_INCLUDED
18#define _FGE_VULKAN_C_CONTEXT_HPP_INCLUDED
20#include "FastEngine/fge_extern.hpp"
22#include "FastEngine/vulkan/vulkanGlobal.hpp"
24#include <unordered_map>
27#include "FastEngine/vulkan/C_commandBuffer.hpp"
28#include "FastEngine/vulkan/C_descriptorPool.hpp"
29#include "FastEngine/vulkan/C_descriptorSetLayout.hpp"
30#include "FastEngine/vulkan/C_garbageCollector.hpp"
31#include "FastEngine/vulkan/C_graphicPipeline.hpp"
32#include "FastEngine/vulkan/C_instance.hpp"
33#include "FastEngine/vulkan/C_logicalDevice.hpp"
34#include "FastEngine/vulkan/C_physicalDevice.hpp"
35#include "FastEngine/vulkan/C_surface.hpp"
36#include "FastEngine/vulkan/C_textureImage.hpp"
37#include "FastEngine/vulkan/C_uniformBuffer.hpp"
41#define FGE_VULKAN_USE_STRICT_LAYOUT_BINDINGS_STAGE 0
43#define FGE_VULKAN_TEXTURE_BINDING 0
44#define FGE_VULKAN_TRANSFORM_BINDING 0
45#define FGE_MULTIUSE_POOL_MAX_COMBINED_IMAGE_SAMPLER FGE_SHADER_MAX_BINDING_VARIABLE_DESCRIPTOR_COUNT
47#define FGE_CONTEXT_OUTSIDE_RENDER_SCOPE_COMMAND_WAITSTAGE VK_PIPELINE_STAGE_VERTEX_INPUT_BIT
49#define FGE_CONTEXT_GLOBALTRANSFORMS_COUNT_START 200
81 using CommandBuffer::CommandBuffer;
83 [[nodiscard]]
inline SubmitTypes getSubmitType()
const {
return g_submitType; }
86 inline void setSubmitType(
SubmitTypes type) { this->g_submitType = type; }
93 struct GlobalTransform
102 uint32_t _transformsCount;
115 Context(Context
const& r) =
delete;
116 Context(Context&& r)
noexcept =
delete;
119 Context& operator=(Context
const& r) =
delete;
120 Context& operator=(Context&& r)
noexcept =
delete;
152 CommandBuffer::RenderPassScopes wantedRenderPassScope,
153 CommandBuffer::SupportedQueueTypes_t wantedQueue)
const;
205 std::string_view applicationName,
206 uint16_t versionMajor = 1,
207 uint16_t versionMinor = 0,
208 uint16_t versionPatch = 0);
226 void initVulkanSurfaceless(
Instance const& instance);
249 [[nodiscard]]
Instance const& getInstance()
const;
250 [[nodiscard]]
Surface const& getSurface()
const;
277 VkCommandBuffer commandBuffers[],
278 uint32_t commandBufferCount)
const;
343 [[nodiscard]] std::optional<BufferInfo> createBuffer(VkDeviceSize size,
344 VkBufferUsageFlags usage,
345 VmaAllocationCreateFlags flags,
346 VkMemoryPropertyFlags requiredProperties = 0)
const;
347 [[nodiscard]] std::optional<ImageInfo> createImage(uint32_t width,
350 VkImageTiling tiling,
352 VkImageUsageFlags usage,
353 VmaAllocationCreateFlags flags,
354 VkMemoryPropertyFlags requiredProperties = 0)
const;
381 void startMainRenderTarget(
RenderTarget& renderTarget)
const;
382 [[nodiscard]]
RenderTarget* getMainRenderTarget()
const;
383 [[nodiscard]]
bool isMainRenderTarget(
RenderTarget const& renderTarget)
const;
384 void endMainRenderTarget(
RenderTarget const& renderTarget)
const;
388 [[nodiscard]] std::pair<uint32_t, fge::TransformUboData*> requestGlobalTransform()
const;
390 void clearLayoutPipelineCache()
const;
405 void clearDescriptorLayoutCache()
const;
419 Shader const* geometryShader,
420 Shader const* fragmentShader)
const;
434 [[nodiscard]] std::optional<DescriptorSet>
440 void createCommandPool();
441 void createMultiUseDescriptorPool();
442 void createTextureDescriptorPool();
443 void createTransformDescriptorPool();
444 void createSyncObjects();
448 struct ReusableCommandBuffer
450 constexpr ReusableCommandBuffer() =
default;
451 constexpr ReusableCommandBuffer(VkCommandBuffer commandBuffer,
bool isRecording) :
452 _commandBuffer(commandBuffer),
453 _isRecording(isRecording)
456 VkCommandBuffer _commandBuffer{VK_NULL_HANDLE};
457 bool _isRecording{
false};
460 PhysicalDevice g_physicalDevice;
461 LogicalDevice g_logicalDevice;
462 Surface
const* g_surface;
463 Instance
const* g_instance;
465 mutable std::unordered_map<LayoutPipeline::Key,
466 std::vector<DescriptorSetLayout>,
467 LayoutPipeline::Key::Hash,
468 LayoutPipeline::Key::Compare>
469 g_cacheDescriptorLayouts;
471 unordered_map<LayoutPipeline::Key, LayoutPipeline, LayoutPipeline::Key::Hash, LayoutPipeline::Key::Compare>
472 g_cachePipelineLayouts;
473 DescriptorPool g_multiUseDescriptorPool;
475 DescriptorSetLayout g_textureLayout;
476 DescriptorSetLayout g_transformLayout;
477 DescriptorPool g_textureDescriptorPool;
478 DescriptorPool g_transformDescriptorPool;
480 mutable RenderTarget* g_mainRenderTarget;
482 mutable VmaAllocator g_allocator;
484 mutable uint32_t g_currentFrame;
486 mutable std::vector<VkCommandBuffer> g_graphicsSubmitableCommandBuffers;
488 std::array<VkSemaphore, FGE_MAX_FRAMES_IN_FLIGHT> g_indirectFinishedSemaphores{};
489 mutable std::array<std::vector<CommandBuffer>, FGE_MAX_FRAMES_IN_FLIGHT> g_indirectSubmitableCommandBuffers{};
490 mutable std::array<ReusableCommandBuffer, FGE_MAX_FRAMES_IN_FLIGHT>
491 g_indirectOutsideRenderScopeGraphicsSubmitableCommandBuffers{};
493 VkCommandPool g_graphicsCommandPool;
Definition C_renderTarget.hpp:56
Definition C_context.hpp:79
Vulkan context.
Definition C_context.hpp:70
std::vector< VkCommandBuffer > const & getGraphicsCommandBuffers() const
Retrieve the list of submitable graphics command buffers.
DescriptorPool const & getMultiUseDescriptorPool() const
Retrieve a "multi-usage" descriptor pool.
SubmitTypes
Definition C_context.hpp:73
@ INDIRECT_EXECUTION
The command buffer is transferred to a queue in order to be submitted later and will always be execut...
Definition C_context.hpp:75
@ DIRECT_WAIT_EXECUTION
The command buffer is submitted directly to the queue and vkQueueWaitIdle is called.
Definition C_context.hpp:74
static void enumerateExtensions()
Enumerate to standard output the available extensions.
LayoutPipeline & requestLayoutPipeline(Shader const *vertexShader, Shader const *geometryShader, Shader const *fragmentShader) const
Retrieve a layout pipeline.
void waitIdle()
Wait for the device to be idle.
DescriptorPool const & getTransformDescriptorPool() const
Retrieve a "transform" descriptor pool.
std::vector< DescriptorSetLayout > const * requestDescriptorLayout(Shader const *vertexShader, Shader const *geometryShader, Shader const *fragmentShader) const
Retrieve a descriptor set layout.
void initVulkan(Surface const &surface)
Initialize Vulkan.
void pushGraphicsCommandBuffer(VkCommandBuffer commandBuffer) const
Push a graphics command buffer to a list.
static std::vector< std::string > retrieveExtensions()
Retrieve the available extensions.
Context(Surface const &surface)
Shortcut to initVulkan(surface)
DescriptorSetLayout const & getTextureLayout() const
Retrieve a "texture" descriptor set layout.
bool submitCommands(SubmitableCommandBuffer &&buffer) const
Submit commands.
static void initVolk()
Initialize Volk (Vulkan loader)
void clearGraphicsCommandBuffers() const
Clear the list of submitable graphics command buffers.
std::optional< DescriptorSet > createDescriptorSet(std::string_view shaderName, uint32_t setIndex, uint32_t variableElements=0) const
Helper to request a descriptor set.
void allocateGraphicsCommandBuffers(VkCommandBufferLevel level, VkCommandBuffer commandBuffers[], uint32_t commandBufferCount) const
Allocate graphics command buffers.
void submit() const
Submit Context command buffers.
VmaAllocator getAllocator() const
Retrieve the VMA (Vulkan Memory Allocator)
VkCommandPool getGraphicsCommandPool() const
Retrieve a command pool for graphics commands.
static Instance init(uint32_t sdlFlag, std::string_view applicationName, uint16_t versionMajor=1, uint16_t versionMinor=0, uint16_t versionPatch=0)
Helper to init SDL, volk and create an Instance.
VkSemaphore getIndirectSemaphore() const
Retrieve the semaphore that is signaled when all indirect command buffers have finished executing.
SubmitableCommandBuffer beginCommands(SubmitTypes type, CommandBuffer::RenderPassScopes wantedRenderPassScope, CommandBuffer::SupportedQueueTypes_t wantedQueue) const
Begin commands.
DescriptorPool const & getTextureDescriptorPool() const
Retrieve a "texture" descriptor pool.
DescriptorSetLayout const & getTransformLayout() const
Retrieve a "transform" descriptor set layout.
This class abstract the vulkan descriptor pool for easier use.
Definition C_descriptorPool.hpp:41
This class abstract the vulkan descriptor set layout for easier use.
Definition C_descriptorSetLayout.hpp:38
This class abstract the vulkan descriptor set for easier use.
Definition C_descriptorSet.hpp:41
A garbage collector for Vulkan objects.
Definition C_garbageCollector.hpp:304
Vulkan instance abstraction.
Definition C_instance.hpp:39
Definition C_graphicPipeline.hpp:35
Logical device abstraction.
Definition C_logicalDevice.hpp:37
Vulkan physical device abstraction.
Definition C_physicalDevice.hpp:34
Definition vulkan/C_shader.hpp:36
Vulkan surface abstraction.
Definition vulkan/C_surface.hpp:42
Definition C_context.hpp:94