ImFusion SDK 4.3
ProgramCache Class Reference

#include <ImFusion/Core/GL/ProgramCache.h>

Helper class to manage instances of the same base shader with different abstract includes and/or defines. More...

+ Inheritance diagram for ProgramCache:

Detailed Description

Helper class to manage instances of the same base shader with different abstract includes and/or defines.

Both abstract shader includes and defines change the actual source code of an OpenGL shader. Often the same base shader is used for different combinations of shader includes/defines. ProgramCache class facilitates managing these shader instances for combinations of multiple abstract includes. Based on the type of the abstract include, it will return (and if needed lazy-instantiate it) the correct shader variant that has the corresponding include code expanded.

// Instantiate the program cache with the shader base version
// Retrieve shader variant that has additionally `SOME_DEFINE` defined
GL::Program& p1 = cache.getProgram("#define SOME_DEFINE");
// Retrieve shader variant with two additional abstract shader includes and with `SOME_DEFINE`.
GL::Program& p2 = cache.getProgram({abstractInclude1, abstractInclude2}, "#define SOME_DEFINE");
Helper class to manage instances of the same base shader with different abstract includes and/or defi...
Definition ProgramCache.h:39
OpenGL GLSL program with a fragment and optional vertex and geometry shader.
Definition Program.h:99
static std::unique_ptr< Program > createVertexFragment(const std::string &vertexShaderName, const std::string &framentShaderName, const std::string &defines="", Compilation compileBehavior=Compilation::Immediate)
Convenience function to instantiate a new OpenGL program consisting of a vertex and a fragment shader...
Warning
Make sure not to alter the abstract includes and/or defines of shaders returned by this class. Doing so may lead to subtle bugs or even crashes as ProgramCache does not track any changes to these shader settings done from the outside.
See also
GL::Program, GL::AbstractInclude

Public Member Functions

 ProgramCache (std::unique_ptr< GL::Program > referenceProgram, std::size_t cacheSize=10)
 Instantiates a ProgramCache based on the given reference shader.
 
GL::ProgramgetProgram (const std::vector< const GL::AbstractInclude * > &includes, const std::string &additionalDefines="")
 Retrieves the correct shader version w.r.t the given abstract includes.
 
GL::ProgramgetProgram (const std::string &additionalDefines)
 Convenience overload that retrieves the shader with the given additional defines but without any additional includes.
 

Constructor & Destructor Documentation

◆ ProgramCache()

ProgramCache ( std::unique_ptr< GL::Program > referenceProgram,
std::size_t cacheSize = 10 )

Instantiates a ProgramCache based on the given reference shader.

Parameters
referenceProgramReference shader, must not be null.
cacheSizeNumber of instances that can be cached.
Exceptions
std::runtime_errorif referenceProgram is invalid/null.

Member Function Documentation

◆ getProgram() [1/2]

GL::Program & getProgram ( const std::vector< const GL::AbstractInclude * > & includes,
const std::string & additionalDefines = "" )

Retrieves the correct shader version w.r.t the given abstract includes.

Parameters
includesVector of abstract includes
additionalDefinesSet of defines inserted into the shader header.
Exceptions
std::runtime_errorif GL::Program instantiation for the requested combination of abstract includes and defines has failed.

◆ getProgram() [2/2]

GL::Program & getProgram ( const std::string & additionalDefines)

Convenience overload that retrieves the shader with the given additional defines but without any additional includes.

Parameters
additionalDefinesSet of defines inserted into the shader header.
Exceptions
std::runtime_errorif GL::Program instantiation for the requested combination of abstract includes and defines has failed.

The documentation for this class was generated from the following file:
Search Tab / S to search, Esc to close