LiteFX 0.3.1.2022
Computer Graphics Engine
LiteFX::Rendering::DescriptorSet< TBuffer, TImage, TSampler > Class Template Referenceabstract

Defines a set of descriptors. More...

#include <rendering.hpp>

Inherits LiteFX::Rendering::IDescriptorSet.

Public Types

using buffer_type = TBuffer
 
using sampler_type = TSampler
 
using image_type = TImage
 

Public Member Functions

virtual ~DescriptorSet () noexcept=default
 
virtual void update (const UInt32 &binding, const buffer_type &buffer, const UInt32 &bufferElement=0, const UInt32 &elements=1, const UInt32 &firstDescriptor=0) const =0
 
virtual void update (const UInt32 &binding, const image_type &texture, const UInt32 &descriptor=0, const UInt32 &firstLevel=0, const UInt32 &levels=0, const UInt32 &firstLayer=0, const UInt32 &layers=0) const =0
 
virtual void update (const UInt32 &binding, const sampler_type &sampler, const UInt32 &descriptor=0) const =0
 
virtual void attach (const UInt32 &binding, const image_type &image) const =0
 
- Public Member Functions inherited from LiteFX::Rendering::IDescriptorSet
virtual ~IDescriptorSet () noexcept=default
 
void update (const UInt32 &binding, const IBuffer &buffer, const UInt32 &bufferElement=0, const UInt32 &elements=1, const UInt32 &firstDescriptor=0) const
 Updates a constant buffer within the current descriptor set. More...
 
void update (const UInt32 &binding, const IImage &texture, const UInt32 &descriptor=0, const UInt32 &firstLevel=0, const UInt32 &levels=0, const UInt32 &firstLayer=0, const UInt32 &layers=0) const
 Updates a texture within the current descriptor set. More...
 
void update (const UInt32 &binding, const ISampler &sampler, const UInt32 &descriptor=0) const
 Updates a sampler within the current descriptor set. More...
 
void attach (const UInt32 &binding, const IImage &image) const
 Attaches an image as an input attachment to a descriptor bound at . More...
 

Detailed Description

template<typename TBuffer, typename TImage, typename TSampler>
requires std::derived_from<TBuffer, IBuffer> && std::derived_from<TSampler, ISampler> && std::derived_from<TImage, IImage>
class LiteFX::Rendering::DescriptorSet< TBuffer, TImage, TSampler >

Defines a set of descriptors.

Descriptors can be grouped into multiple descriptor sets. It is generally a good practice to group descriptors based on the frequency of the updates they receive. For example, it typically makes sense to store the camera buffer in a descriptor set, since it only needs to be updated once per frame for each camera, whilst the object or material data should be stored in separate descriptor sets, that are possibly updated before each draw call. However, other scenarios employing multiple descriptor sets are also possible.

From a shader perspective, a descriptor set is identified by a set (GLSL) or space (HLSL), whilst a descriptor is addressed by a binding (GLSL) or register (HLSL). Descriptor sets are read from GPU-visible memory, depending on how they are bound during the current draw call.

From a CPU perspective, think of a descriptor set as an array of pointers to different buffers (i.e. descriptors) for the shader. A descriptor can be bound to a set by calling DescriptorSet::update. Note that this does not automatically ensure, that the buffer memory is visible for the GPU. Instead, a buffer may also require a transfer into GPU visible memory, depending on the BufferUsage. However, as long as a descriptor within a set is mapped to a buffer, modifying this buffer also reflects the change to the shader, without requiring to update the descriptor, similarly to how modifying the object behind a pointer does not require the pointer to change.

Note, that there might be multiple descriptor set instances of the same DescriptorSetLayout, pointing to different IBuffer instances, depending on the number of frames in flight. Since multiple frames can be computed concurrently, it is important to properly synchronize descriptor set updates. Generally, there are three strategies to choose from, that you can implement or mix in custom flavors, depending on your use case:

  • Naive: The naive approach most closely matches earlier graphics API concepts. Create one buffer per descriptor and synchronize frames. This basically means that each back buffer swap is synchronized to wait for the graphics pipeline. This way, writing to a buffer ensures, that it is only read within the frame of reference and modifying it does not interfere with other frames. This strategy is memory efficient, but may cause the GPU to stall. It may, however be a valid strategy, for data that is only written once or very infrequently.
  • Array of Buffers: The helper methods for creating and updating constant buffers are able to create buffer arrays. Those arrays can be used to create a buffer for each frame in flight. When binding a buffer to a descriptor, it is possible to bind only one element of the array. This way, each frame has its own buffer and does not interfere with other buffer writes.
  • Ring-Buffer: The most efficient (yet not always applicable) approach involves creating one large buffer array, that is bound to multiple descriptor sets. This ensures that the buffer memory stays contiguous and does not get fragmented. However, this requires to know upfront, how many buffers are required for each descriptor, which might not always be possible. Thus another flavor of using this technique involves a creating a large enough descriptor array and updating the descriptor set with an increasing array element for each object as a ring-buffer. As long as there are enough elements in the buffer, so that no second update interferes with a buffer write in an earlier frame, this method provides the most efficient approach. However, it may be hard or impossible to determine the ideal size of the ring-buffer upfront.

Note that samplers, textures and input attachments currently do not support array binding, since they are typically only updated once or require pipeline synchronization anyway.

Also note, that another buffer management strategy is currently not available: the Monolithic Buffer. In this strategy, there is only one large buffer for all buffers. Differently from the ring buffer strategy, where there is one buffer per descriptor type, a monolithic buffer combines multiple constant buffers, containing different data into one giant buffer block. Calling RenderPipeline::bind for a descriptor set would then receive an additional dynamic offset for each descriptor within the descriptor set.

Template Parameters
TBufferThe type of the buffer interface. Must inherit from IBuffer.
TImageThe type of the image interface. Must inherit from IImage.
TSamplerThe type of the sampler interface. Must inherit from ISampler.
See also
DescriptorSetLayout

Member Typedef Documentation

◆ buffer_type

template<typename TBuffer , typename TImage , typename TSampler >
using LiteFX::Rendering::DescriptorSet< TBuffer, TImage, TSampler >::buffer_type = TBuffer

◆ image_type

template<typename TBuffer , typename TImage , typename TSampler >
using LiteFX::Rendering::DescriptorSet< TBuffer, TImage, TSampler >::image_type = TImage

◆ sampler_type

template<typename TBuffer , typename TImage , typename TSampler >
using LiteFX::Rendering::DescriptorSet< TBuffer, TImage, TSampler >::sampler_type = TSampler

Constructor & Destructor Documentation

◆ ~DescriptorSet()

template<typename TBuffer , typename TImage , typename TSampler >
virtual LiteFX::Rendering::DescriptorSet< TBuffer, TImage, TSampler >::~DescriptorSet ( )
virtualdefaultnoexcept

Member Function Documentation

◆ attach()

template<typename TBuffer , typename TImage , typename TSampler >
virtual void LiteFX::Rendering::DescriptorSet< TBuffer, TImage, TSampler >::attach ( const UInt32 binding,
const image_type image 
) const
pure virtual

◆ update() [1/3]

template<typename TBuffer , typename TImage , typename TSampler >
virtual void LiteFX::Rendering::DescriptorSet< TBuffer, TImage, TSampler >::update ( const UInt32 binding,
const buffer_type buffer,
const UInt32 bufferElement = 0,
const UInt32 elements = 1,
const UInt32 firstDescriptor = 0 
) const
pure virtual

◆ update() [2/3]

template<typename TBuffer , typename TImage , typename TSampler >
virtual void LiteFX::Rendering::DescriptorSet< TBuffer, TImage, TSampler >::update ( const UInt32 binding,
const image_type texture,
const UInt32 descriptor = 0,
const UInt32 firstLevel = 0,
const UInt32 levels = 0,
const UInt32 firstLayer = 0,
const UInt32 layers = 0 
) const
pure virtual

◆ update() [3/3]

template<typename TBuffer , typename TImage , typename TSampler >
virtual void LiteFX::Rendering::DescriptorSet< TBuffer, TImage, TSampler >::update ( const UInt32 binding,
const sampler_type sampler,
const UInt32 descriptor = 0 
) const
pure virtual