FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
fge::vulkan::Context Class Reference

Vulkan context. More...

#include <C_context.hpp>

Classes

struct  GlobalTransform
 
class  SubmitableCommandBuffer
 

Public Types

enum class  SubmitTypes { DIRECT_WAIT_EXECUTION , INDIRECT_EXECUTION }
 

Public Member Functions

 Context (Surface const &surface)
 Shortcut to initVulkan(surface)
 
 Context (Context const &r)=delete
 
 Context (Context &&r) noexcept=delete
 
Contextoperator= (Context const &r)=delete
 
Contextoperator= (Context &&r) noexcept=delete
 
void destroy ()
 
SubmitableCommandBuffer beginCommands (SubmitTypes type, CommandBuffer::RenderPassScopes wantedRenderPassScope, CommandBuffer::SupportedQueueTypes_t wantedQueue) const
 Begin commands.
 
bool submitCommands (SubmitableCommandBuffer &&buffer) const
 Submit commands.
 
VkSemaphore getIndirectSemaphore () const
 Retrieve the semaphore that is signaled when all indirect command buffers have finished executing.
 
void submit () const
 Submit Context command buffers.
 
void initVulkan (Surface const &surface)
 Initialize Vulkan.
 
void initVulkanSurfaceless (Instance const &instance)
 
void waitIdle ()
 Wait for the device to be idle.
 
Instance const & getInstance () const
 
Surface const & getSurface () const
 
LogicalDevice const & getLogicalDevice () const
 
PhysicalDevice const & getPhysicalDevice () const
 
VkCommandPool getGraphicsCommandPool () const
 Retrieve a command pool for graphics commands.
 
void allocateGraphicsCommandBuffers (VkCommandBufferLevel level, VkCommandBuffer commandBuffers[], uint32_t commandBufferCount) const
 Allocate graphics command buffers.
 
DescriptorPool const & getMultiUseDescriptorPool () const
 Retrieve a "multi-usage" descriptor pool.
 
DescriptorSetLayout const & getTextureLayout () const
 Retrieve a "texture" descriptor set layout.
 
DescriptorSetLayout const & getTransformLayout () const
 Retrieve a "transform" descriptor set layout.
 
DescriptorPool const & getTextureDescriptorPool () const
 Retrieve a "texture" descriptor pool.
 
DescriptorPool const & getTransformDescriptorPool () const
 Retrieve a "transform" descriptor pool.
 
VmaAllocator getAllocator () const
 Retrieve the VMA (Vulkan Memory Allocator)
 
void pushGraphicsCommandBuffer (VkCommandBuffer commandBuffer) const
 Push a graphics command buffer to a list.
 
std::vector< VkCommandBuffer > const & getGraphicsCommandBuffers () const
 Retrieve the list of submitable graphics command buffers.
 
void clearGraphicsCommandBuffers () const
 Clear the list of submitable graphics command buffers.
 
void startMainRenderTarget (RenderTarget &renderTarget) const
 
RenderTargetgetMainRenderTarget () const
 
bool isMainRenderTarget (RenderTarget const &renderTarget) const
 
void endMainRenderTarget (RenderTarget const &renderTarget) const
 
GlobalTransform const & getGlobalTransform () const
 
fge::TransformUboData const * getGlobalTransform (uint32_t index) const
 
std::pair< uint32_t, fge::TransformUboData * > requestGlobalTransform () const
 
void clearLayoutPipelineCache () const
 
LayoutPipelinerequestLayoutPipeline (Shader const *vertexShader, Shader const *geometryShader, Shader const *fragmentShader) const
 Retrieve a layout pipeline.
 
void clearDescriptorLayoutCache () const
 
std::vector< DescriptorSetLayout > const * requestDescriptorLayout (Shader const *vertexShader, Shader const *geometryShader, Shader const *fragmentShader) const
 Retrieve a descriptor set layout.
 
std::vector< DescriptorSetLayout > const * requestDescriptorLayout (Shader const *shader) const
 
std::optional< DescriptorSetcreateDescriptorSet (std::string_view shaderName, uint32_t setIndex, uint32_t variableElements=0) const
 Helper to request a descriptor set.
 

Static Public Member Functions

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.
 
static void initVolk ()
 Initialize Volk (Vulkan loader)
 
static void enumerateExtensions ()
 Enumerate to standard output the available extensions.
 
static std::vector< std::string > retrieveExtensions ()
 Retrieve the available extensions.
 

Public Attributes

GarbageCollector _garbageCollector
 

Detailed Description

Vulkan context.

This class is the main starting point for Vulkan usage.

Member Enumeration Documentation

◆ SubmitTypes

Enumerator
DIRECT_WAIT_EXECUTION 

The command buffer is submitted directly to the queue and vkQueueWaitIdle is called.

INDIRECT_EXECUTION 

The command buffer is transferred to a queue in order to be submitted later and will always be executed before the rendering.

Constructor & Destructor Documentation

◆ Context()

fge::vulkan::Context::Context ( Surface const & surface)
explicit

Shortcut to initVulkan(surface)

See also
initVulkan()
Parameters
surfaceA valid Surface

Member Function Documentation

◆ allocateGraphicsCommandBuffers()

void fge::vulkan::Context::allocateGraphicsCommandBuffers ( VkCommandBufferLevel level,
VkCommandBuffer commandBuffers[],
uint32_t commandBufferCount ) const

Allocate graphics command buffers.

This is a shortcut for vkAllocateCommandBuffers with the graphics command pool.

See also
getGraphicsCommandPool()
Parameters
levelThe level of the command buffers (primary or secondary)
commandBuffersAn array of VkCommandBuffer structures in which the resulting command buffer objects are returned
commandBufferCountThe number of command buffers to allocate

◆ beginCommands()

SubmitableCommandBuffer fge::vulkan::Context::beginCommands ( SubmitTypes type,
CommandBuffer::RenderPassScopes wantedRenderPassScope,
CommandBuffer::SupportedQueueTypes_t wantedQueue ) const
nodiscard

Begin commands.

This return a command buffer that is ready to be used.

The DIRECT_WAIT_EXECUTION type is used to execute a command buffer directly, this implies create the buffer, submit the buffer, and waiting for the corresponding queue operations to be finished. This is not ideal for performance.

The INDIRECT_EXECUTION type will create a command buffer that will be submitted later and executed with a semaphore that is signaled after every command is done so this assures that every command are finished before graphics commands. This is ideal for performance like copying staging buffers to device local buffers.

On certain cases, a reusable command buffer is returned in order to optimize command buffer creation/destruction. Current case is when the command buffer is used outside a render scope and the graphics queue is wanted.

Warning
This function must be pared with submitCommands(). CommandBuffer::begin(), CommandBuffer::end() and CommandBuffer::reset() should not be called.
Parameters
typeThe submit type of the command buffer
wantedRenderPassScopeThe wanted render pass scope (for optimization purposes)
wantedQueueThe wanted queue (for optimization purposes)
Returns
A submitable command buffer

◆ clearGraphicsCommandBuffers()

void fge::vulkan::Context::clearGraphicsCommandBuffers ( ) const

Clear the list of submitable graphics command buffers.

See also
pushGraphicsCommandBuffer()

◆ createDescriptorSet()

std::optional< DescriptorSet > fge::vulkan::Context::createDescriptorSet ( std::string_view shaderName,
uint32_t setIndex,
uint32_t variableElements = 0 ) const
nodiscard

Helper to request a descriptor set.

This function will create a new descriptor set with the provided shader and set index. This will also select for you the correct descriptor pool to use.

Parameters
shaderNameThe name of the shader from the manager
setIndexThe index of the wanted set in the shader (not the set number)
variableElementsThe number of elements in the descriptor set (for variable descriptor sets)
Returns
The descriptor set or std::nullopt if the something went wrong

◆ enumerateExtensions()

static void fge::vulkan::Context::enumerateExtensions ( )
static

Enumerate to standard output the available extensions.

See also
retrieveExtensions()

◆ getAllocator()

VmaAllocator fge::vulkan::Context::getAllocator ( ) const
nodiscard

Retrieve the VMA (Vulkan Memory Allocator)

Returns
The allocator

◆ getGraphicsCommandBuffers()

std::vector< VkCommandBuffer > const & fge::vulkan::Context::getGraphicsCommandBuffers ( ) const
nodiscard

Retrieve the list of submitable graphics command buffers.

See also
pushGraphicsCommandBuffer()
Returns
The list of executable graphics command buffers

◆ getGraphicsCommandPool()

VkCommandPool fge::vulkan::Context::getGraphicsCommandPool ( ) const
nodiscard

Retrieve a command pool for graphics commands.

This command pool is used to create command buffers that will be used to submit commands to the graphics queue.

This command pool is created with the following flags: VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT

Returns
The command pool

◆ getIndirectSemaphore()

VkSemaphore fge::vulkan::Context::getIndirectSemaphore ( ) const
nodiscard

Retrieve the semaphore that is signaled when all indirect command buffers have finished executing.

This can return VK_NULL_HANDLE if there is no command buffers to execute.

See also
submit()
Returns
The semaphore

◆ getMultiUseDescriptorPool()

DescriptorPool const & fge::vulkan::Context::getMultiUseDescriptorPool ( ) const
nodiscard

Retrieve a "multi-usage" descriptor pool.

This pool was created with the following types: VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER VK_DESCRIPTOR_TYPE_STORAGE_BUFFER VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC

Returns
The descriptor pool

◆ getTextureDescriptorPool()

DescriptorPool const & fge::vulkan::Context::getTextureDescriptorPool ( ) const
nodiscard

Retrieve a "texture" descriptor pool.

This pool can only contain VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER type.

Returns
The descriptor pool

◆ getTextureLayout()

DescriptorSetLayout const & fge::vulkan::Context::getTextureLayout ( ) const
nodiscard

Retrieve a "texture" descriptor set layout.

This layout is used with default provided shaders.

This layout was created with the following: binding: FGE_VULKAN_TEXTURE_BINDING type: VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER stage: VK_SHADER_STAGE_FRAGMENT_BIT

Returns
The descriptor set layout

◆ getTransformDescriptorPool()

DescriptorPool const & fge::vulkan::Context::getTransformDescriptorPool ( ) const
nodiscard

Retrieve a "transform" descriptor pool.

This pool can only contain VK_DESCRIPTOR_TYPE_STORAGE_BUFFER type.

Returns
The descriptor pool

◆ getTransformLayout()

DescriptorSetLayout const & fge::vulkan::Context::getTransformLayout ( ) const
nodiscard

Retrieve a "transform" descriptor set layout.

This layout is used with default provided shaders.

This layout was created with the following: binding: FGE_VULKAN_TRANSFORM_BINDING type: VK_DESCRIPTOR_TYPE_STORAGE_BUFFER stage: VK_SHADER_STAGE_VERTEX_BIT

Returns
The descriptor set layout

◆ init()

static Instance fge::vulkan::Context::init ( uint32_t sdlFlag,
std::string_view applicationName,
uint16_t versionMajor = 1,
uint16_t versionMinor = 0,
uint16_t versionPatch = 0 )
staticnodiscard

Helper to init SDL, volk and create an Instance.

This function do that in order :

  • SDL_Init()
  • SDL_Vulkan_LoadLibrary()
  • Context::initVolk()
  • and create an instance with 'applicationName'
Parameters
sdlFlagSDL flag passed to SDL_Init()
applicationNameThe name of the application
versionMajor
versionMinor
versionPatch
Returns
A valid Instance or throw on error

◆ initVolk()

static void fge::vulkan::Context::initVolk ( )
static

Initialize Volk (Vulkan loader)

Warning
This function must be called once before any other graphics usage, generally at the start of the program

◆ initVulkan()

void fge::vulkan::Context::initVulkan ( Surface const & surface)

Initialize Vulkan.

Once a surface is correctly created, this function must be called to initialize Vulkan. Automatically call SetActiveContext() when no error.

Parameters
surfaceA valid surface

◆ pushGraphicsCommandBuffer()

void fge::vulkan::Context::pushGraphicsCommandBuffer ( VkCommandBuffer commandBuffer) const

Push a graphics command buffer to a list.

This is used to keep track of submitable command buffers that will be submitted to the graphics queue. This list must be cleared once the command buffers are submitted. Generally, this is done by a RenderScreen when the RenderScreen::display() method is called.

Parameters
commandBufferThe command buffer to push

◆ requestDescriptorLayout()

std::vector< DescriptorSetLayout > const * fge::vulkan::Context::requestDescriptorLayout ( Shader const * vertexShader,
Shader const * geometryShader,
Shader const * fragmentShader ) const
nodiscard

Retrieve a descriptor set layout.

This function will create a new descriptor set layout if it doesn't exist. The descriptor set layout is created with the provided shader with reflection of the SPIR-V code to retrieve the descriptor set layouts.

Parameters
vertexShaderThe vertex shader, can be nullptr
geometryShaderThe geometry shader, can be nullptr
fragmentShaderThe fragment shader, can be nullptr
Returns
The descriptor set layout or nullptr if the shader is invalid

◆ requestLayoutPipeline()

LayoutPipeline & fge::vulkan::Context::requestLayoutPipeline ( Shader const * vertexShader,
Shader const * geometryShader,
Shader const * fragmentShader ) const
nodiscard

Retrieve a layout pipeline.

This function will create a new layout pipeline if it doesn't exist. The layout pipeline is created with the provided shaders with reflection of the SPIR-V code to retrieve the descriptor set layouts and push constant ranges.

Parameters
vertexShaderThe vertex shader, can be nullptr
geometryShaderThe geometry shader, can be nullptr
fragmentShaderThe fragment shader, can be nullptr
Returns
The layout pipeline

◆ retrieveExtensions()

static std::vector< std::string > fge::vulkan::Context::retrieveExtensions ( )
staticnodiscard

Retrieve the available extensions.

Returns
The available extensions

◆ submit()

void fge::vulkan::Context::submit ( ) const

Submit Context command buffers.

Indirect CommandBuffers are submitted with a semaphore that is signaled when the all of them have finished executing.

The semaphore should be retrieved with getIndirectSemaphore() and you must wait for it to be signaled before rendering commands as this buffer generally contain some buffer transfer operations.

This also increment the internal current frame counter.

This is automatically called by a RenderScreen when the RenderScreen::display() method is called.

◆ submitCommands()

bool fge::vulkan::Context::submitCommands ( SubmitableCommandBuffer && buffer) const

Submit commands.

See also
beginCommands()
Parameters
bufferThe command buffer to submit

◆ waitIdle()

void fge::vulkan::Context::waitIdle ( )

Wait for the device to be idle.

This is generally called before any new commands submission. Also when the program is about to exit, this function must be called to make sure that all commands are finished.


The documentation for this class was generated from the following file: