Rendu
A lightweight rendering engine for experimentations
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | Friends | List of all members
Program Class Reference

Represents a group of shaders used for rendering. More...

#include <Program.hpp>

Classes

struct  BufferDef
 Buffer reflection information. More...
 
struct  DynamicBufferState
 Internal state for a dynamic uniform buffer. More...
 
struct  ImageDef
 Image-sampler reflection information. More...
 
struct  Stage
 Per-stage reflection information. More...
 
struct  State
 Program pipeline state. More...
 
struct  StaticBufferState
 Internal state for a static (external) uniform buffer. More...
 
struct  TextureState
 Internal state for an image. More...
 
struct  UniformDef
 Uniform reflection information. More...
 

Public Types

enum class  Type { GRAPHICS , COMPUTE }
 
using Uniforms = std::unordered_map< std::string, UniformDef >
 List of named uniforms.
 

Public Member Functions

 Program (const std::string &name, const std::string &vertexContent, const std::string &fragmentContent, const std::string &tessControlContent="", const std::string &tessEvalContent="")
 
 Program (const std::string &name, const std::string &computeContent)
 
void reload (const std::string &vertexContent, const std::string &fragmentContent, const std::string &tessControlContent="", const std::string &tessEvalContent="")
 
void reload (const std::string &computeContent)
 
bool reloaded () const
 
bool reloaded (bool absorb)
 
void use () const
 
void clean ()
 
void buffer (const UniformBufferBase &buffer, uint slot)
 
void buffer (const Buffer &buffer, uint slot)
 
void bufferArray (const std::vector< const Buffer * > &buffers, uint slot)
 
void texture (const Texture *texture, uint slot, uint mip=Program::ALL_MIPS)
 
void texture (const Texture &texture, uint slot, uint mip=Program::ALL_MIPS)
 
void textureArray (const std::vector< const Texture * > &textures, uint slot, uint mip=Program::ALL_MIPS)
 
void textures (const std::vector< const Texture * > &textures, size_t slot=0)
 
void defaultTexture (uint slot)
 
void transitionResourcesTo (Program::Type type)
 
void update ()
 
void uniform (const std::string &name, bool t)
 
void uniform (const std::string &name, int t)
 
void uniform (const std::string &name, uint t)
 
void uniform (const std::string &name, float t)
 
void uniform (const std::string &name, const glm::vec2 &t)
 
void uniform (const std::string &name, const glm::vec3 &t)
 
void uniform (const std::string &name, const glm::vec4 &t)
 
void uniform (const std::string &name, const glm::ivec2 &t)
 
void uniform (const std::string &name, const glm::ivec3 &t)
 
void uniform (const std::string &name, const glm::ivec4 &t)
 
void uniform (const std::string &name, const glm::mat3 &t)
 
void uniform (const std::string &name, const glm::mat4 &t)
 
void getUniform (const std::string &name, bool &t) const
 
void getUniform (const std::string &name, int &t) const
 
void getUniform (const std::string &name, uint &t) const
 
void getUniform (const std::string &name, float &t) const
 
void getUniform (const std::string &name, glm::vec2 &t) const
 
void getUniform (const std::string &name, glm::vec3 &t) const
 
void getUniform (const std::string &name, glm::vec4 &t) const
 
void getUniform (const std::string &name, glm::ivec2 &t) const
 
void getUniform (const std::string &name, glm::ivec3 &t) const
 
void getUniform (const std::string &name, glm::ivec4 &t) const
 
void getUniform (const std::string &name, glm::mat3 &t) const
 
void getUniform (const std::string &name, glm::mat4 &t) const
 
const Uniformsuniforms () const
 
const std::string & name () const
 
Program::Type type () const
 
const glm::uvec3 & size () const
 
Programoperator= (const Program &)=delete
 
 Program (const Program &)=delete
 
Programoperator= (Program &&)=delete
 
 Program (Program &&)=default
 
const StategetState () const
 
Stagestage (ShaderType type)
 

Static Public Attributes

static uint ALL_MIPS = 0xFFFF
 

Private Member Functions

void reflect ()
 Reflect all uniforms/textures/storage buffers and images based on the shader content.
 
void updateUniformMetric () const
 Update internal metrics.
 
const char * retrieveUniform (const UniformDef::Location &location) const
 
char * retrieveUniformNonConst (const UniformDef::Location &location)
 

Private Attributes

std::string _name
 Debug name.
 
std::array< Stage, int(ShaderType::COUNT)> _stages
 Per-stage reflection data.
 
State _state
 Program pipeline state.
 
std::unordered_map< std::string, UniformDef_uniforms
 All dynamic uniform definitions.
 
std::unordered_map< int, DynamicBufferState_dynamicBuffers
 Dynamic uniform buffer definitions (set 0).
 
std::unordered_map< int, TextureState_textures
 Dynamic image-sampler definitions (set 2).
 
std::unordered_map< int, StaticBufferState_staticBuffers
 Static uniform buffer definitions (set 3).
 
std::array< bool, 4 > _dirtySets
 Marks which descriptor sets are dirty.
 
std::array< DescriptorSet, 4 > _currentSets
 Descriptor sets.
 
std::vector< uint32_t > _currentOffsets
 Offsets in the descriptor set for dynamic uniform buffers.
 
bool _reloaded = false
 Has the program been reloaded.
 
const Type _type
 Is this a compute shader.
 

Friends

class GPU
 Utilities will need to access GPU handle.
 

Detailed Description

Represents a group of shaders used for rendering.

Internally responsible for handling uniforms locations, shaders reloading and values caching. Uniform sets are predefined: set 0 is for dynamic uniforms, set 1 for image-samplers, set 2 for static/per-frame uniform buffers.

Member Enumeration Documentation

◆ Type

enum class Program::Type
strong

Type of program

Enumerator
GRAPHICS 

Graphics program for draw calls.

COMPUTE 

Compute program for dispatch calls.

Constructor & Destructor Documentation

◆ Program() [1/4]

Program::Program ( const std::string &  name,
const std::string &  vertexContent,
const std::string &  fragmentContent,
const std::string &  tessControlContent = "",
const std::string &  tessEvalContent = "" 
)

Load, compile and link shaders into a GPU graphics program.

Parameters
namethe program name for logging
vertexContentthe content of the vertex shader
fragmentContentthe content of the fragment shader
tessControlContentthe content of the tessellation control shader (can be empty)
tessEvalContentthe content of the tessellation evaluation shader (can be empty)

◆ Program() [2/4]

Program::Program ( const std::string &  name,
const std::string &  computeContent 
)

Load, compile and link shaders into a GPU compute program.

Parameters
namethe program name for logging
computeContentthe content of the compute shader

◆ Program() [3/4]

Program::Program ( const Program )
delete

Copy constructor (disabled).

◆ Program() [4/4]

Program::Program ( Program &&  )
default

Move constructor.

Member Function Documentation

◆ buffer() [1/2]

void Program::buffer ( const Buffer buffer,
uint  slot 
)

Bind a buffer to a given location.

Parameters
bufferthe buffer to bind
slotthe location to bind to

◆ buffer() [2/2]

void Program::buffer ( const UniformBufferBase buffer,
uint  slot 
)

Bind a buffer to a given location.

Parameters
bufferthe buffer to bind
slotthe location to bind to

◆ bufferArray()

void Program::bufferArray ( const std::vector< const Buffer * > &  buffers,
uint  slot 
)

Bind a set of buffers to a given location.

Parameters
buffersthe buffers to bind
slotthe location to bind to

◆ clean()

void Program::clean ( )

Delete the program on the GPU.

◆ defaultTexture()

void Program::defaultTexture ( uint  slot)

Bind a default texture to a given location.

Parameters
slotthe location to bind the texture to

◆ getState()

const State & Program::getState ( ) const
inline
Returns
the program state for a pipeline

◆ getUniform() [1/12]

void Program::getUniform ( const std::string &  name,
bool &  t 
) const

Get a given uniform value.

Parameters
namethe uniform name
twill contain the value

◆ getUniform() [2/12]

void Program::getUniform ( const std::string &  name,
float &  t 
) const

Get a given uniform value.

Parameters
namethe uniform name
twill contain the value

◆ getUniform() [3/12]

void Program::getUniform ( const std::string &  name,
glm::ivec2 &  t 
) const

Get a given uniform value.

Parameters
namethe uniform name
twill contain the value

◆ getUniform() [4/12]

void Program::getUniform ( const std::string &  name,
glm::ivec3 &  t 
) const

Get a given uniform value.

Parameters
namethe uniform name
twill contain the value

◆ getUniform() [5/12]

void Program::getUniform ( const std::string &  name,
glm::ivec4 &  t 
) const

Get a given uniform value.

Parameters
namethe uniform name
twill contain the value

◆ getUniform() [6/12]

void Program::getUniform ( const std::string &  name,
glm::mat3 &  t 
) const

Get a given uniform value.

Parameters
namethe uniform name
twill contain the value

◆ getUniform() [7/12]

void Program::getUniform ( const std::string &  name,
glm::mat4 &  t 
) const

Get a given uniform value.

Parameters
namethe uniform name
twill contain the value

◆ getUniform() [8/12]

void Program::getUniform ( const std::string &  name,
glm::vec2 &  t 
) const

Get a given uniform value.

Parameters
namethe uniform name
twill contain the value

◆ getUniform() [9/12]

void Program::getUniform ( const std::string &  name,
glm::vec3 &  t 
) const

Get a given uniform value.

Parameters
namethe uniform name
twill contain the value

◆ getUniform() [10/12]

void Program::getUniform ( const std::string &  name,
glm::vec4 &  t 
) const

Get a given uniform value.

Parameters
namethe uniform name
twill contain the value

◆ getUniform() [11/12]

void Program::getUniform ( const std::string &  name,
int &  t 
) const

Get a given uniform value.

Parameters
namethe uniform name
twill contain the value

◆ getUniform() [12/12]

void Program::getUniform ( const std::string &  name,
uint &  t 
) const

Get a given uniform value.

Parameters
namethe uniform name
twill contain the value

◆ name()

const std::string & Program::name ( ) const
inline
Returns
the program name

◆ operator=() [1/2]

Program & Program::operator= ( const Program )
delete

Copy assignment operator (disabled).

Returns
a reference to the object assigned to

◆ operator=() [2/2]

Program & Program::operator= ( Program &&  )
delete

Move assignment operator.

Returns
a reference to the object assigned to

◆ reload() [1/2]

void Program::reload ( const std::string &  computeContent)

Load the program, compiling the shader and updating all uniform locations.

Parameters
computeContentthe content of the compute shader

◆ reload() [2/2]

void Program::reload ( const std::string &  vertexContent,
const std::string &  fragmentContent,
const std::string &  tessControlContent = "",
const std::string &  tessEvalContent = "" 
)

Load the program, compiling the shader and updating all uniform locations.

Parameters
vertexContentthe content of the vertex shader
fragmentContentthe content of the fragment shader
tessControlContentthe content of the tessellation control shader (can be empty)
tessEvalContentthe content of the tessellation evaluation shader (can be empty)

◆ reloaded() [1/2]

bool Program::reloaded ( ) const
Returns
true if the program has been recently reloaded.

◆ reloaded() [2/2]

bool Program::reloaded ( bool  absorb)

Check if the program has been recently reloaded.

Parameters
absorbshould the reloaded flag be set to false afterwards
Returns
true if reloaded

◆ retrieveUniform()

const char * Program::retrieveUniform ( const UniformDef::Location location) const
inlineprivate

Obtain a pointer to the CPU memory for a uniform.

Parameters
locationthe uniform location to retrieve
Returns
a const pointer to CPU memory

◆ retrieveUniformNonConst()

char * Program::retrieveUniformNonConst ( const UniformDef::Location location)
inlineprivate

Obtain a pointer to the CPU memory for a uniform.

Parameters
locationthe uniform location to retrieve
Returns
a pointer to CPU memory
Note
This will mark the corresponding buffer as dirty.

◆ size()

const glm::uvec3 & Program::size ( ) const
Returns
the local group size for compute programs

◆ stage()

Stage & Program::stage ( ShaderType  type)
inline

Query shader information for a stage.

Parameters
typethe stage to query
Returns
the stage reflection information

◆ texture() [1/2]

void Program::texture ( const Texture texture,
uint  slot,
uint  mip = Program::ALL_MIPS 
)

Bind a texture to a given location.

Parameters
texturethe texture to bind
slotthe location to bind to
mipthe mip of the texture to bind (or all mips if left at its default value)

◆ texture() [2/2]

void Program::texture ( const Texture texture,
uint  slot,
uint  mip = Program::ALL_MIPS 
)

Bind a texture to a given location.

Parameters
texturethe texture to bind
slotthe location to bind to
mipthe mip of the texture to bind (or all mips if left at its default value)

◆ textureArray()

void Program::textureArray ( const std::vector< const Texture * > &  textures,
uint  slot,
uint  mip = Program::ALL_MIPS 
)

Bind a set of textures to a given location.

Parameters
texturesthe textures to bind
slotthe location to bind to
mipthe mip of the textures to bind (or all mips if left at its default value)

◆ textures()

void Program::textures ( const std::vector< const Texture * > &  textures,
size_t  slot = 0 
)

Bind a set of textures to successive locations.

Parameters
texturesthe textures to bind
slotthe location to bind the first texture to
Note
Successive textures will be bound to locations slot+1, slot+2,...

◆ transitionResourcesTo()

void Program::transitionResourcesTo ( Program::Type  type)

Ensure all currently set resources are in the proper layout/state and are synchronized for a given type of use (graphics or compute).

Parameters
typethe type of use to prepare resources for

◆ type()

Program::Type Program::type ( ) const
inline
Returns
whether this is a graphics or compute program

◆ uniform() [1/12]

void Program::uniform ( const std::string &  name,
bool  t 
)

Set a given uniform value.

Parameters
namethe uniform name
tthe value to set the uniform to

◆ uniform() [2/12]

void Program::uniform ( const std::string &  name,
const glm::ivec2 &  t 
)

Set a given uniform value.

Parameters
namethe uniform name
tthe value to set the uniform to

◆ uniform() [3/12]

void Program::uniform ( const std::string &  name,
const glm::ivec3 &  t 
)

Set a given uniform value.

Parameters
namethe uniform name
tthe value to set the uniform to

◆ uniform() [4/12]

void Program::uniform ( const std::string &  name,
const glm::ivec4 &  t 
)

Set a given uniform value.

Parameters
namethe uniform name
tthe value to set the uniform to

◆ uniform() [5/12]

void Program::uniform ( const std::string &  name,
const glm::mat3 &  t 
)

Set a given uniform value.

Parameters
namethe uniform name
tthe value to set the uniform to

◆ uniform() [6/12]

void Program::uniform ( const std::string &  name,
const glm::mat4 &  t 
)

Set a given uniform value.

Parameters
namethe uniform name
tthe value to set the uniform to

◆ uniform() [7/12]

void Program::uniform ( const std::string &  name,
const glm::vec2 &  t 
)

Set a given uniform value.

Parameters
namethe uniform name
tthe value to set the uniform to

◆ uniform() [8/12]

void Program::uniform ( const std::string &  name,
const glm::vec3 &  t 
)

Set a given uniform value.

Parameters
namethe uniform name
tthe value to set the uniform to

◆ uniform() [9/12]

void Program::uniform ( const std::string &  name,
const glm::vec4 &  t 
)

Set a given uniform value.

Parameters
namethe uniform name
tthe value to set the uniform to

◆ uniform() [10/12]

void Program::uniform ( const std::string &  name,
float  t 
)

Set a given uniform value.

Parameters
namethe uniform name
tthe value to set the uniform to

◆ uniform() [11/12]

void Program::uniform ( const std::string &  name,
int  t 
)

Set a given uniform value.

Parameters
namethe uniform name
tthe value to set the uniform to

◆ uniform() [12/12]

void Program::uniform ( const std::string &  name,
uint  t 
)

Set a given uniform value.

Parameters
namethe uniform name
tthe value to set the uniform to

◆ uniforms()

const Uniforms & Program::uniforms ( ) const
inline
Returns
the list of registered basic uniforms.

◆ update()

void Program::update ( )

Update internal data (descriptors,...) and bind them before a draw/dispatch.

Todo:
Should we use the real buffer current offset here, if an update happened under the hood more than once per frame?

◆ use()

void Program::use ( ) const

Activate the program shaders.

Member Data Documentation

◆ ALL_MIPS

uint Program::ALL_MIPS = 0xFFFF
static

Bind all mip levels of a texture


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