2016-02-16 17:18:48 +00:00
# ifndef OPENMW_COMPONENTS_SHADERMANAGER_H
# define OPENMW_COMPONENTS_SHADERMANAGER_H
# include <string>
# include <map>
2020-06-25 19:46:07 +00:00
# include <mutex>
2016-02-16 17:18:48 +00:00
# include <osg/ref_ptr>
# include <osg/Shader>
2017-12-17 01:57:53 +00:00
# include <osgViewer/Viewer>
2021-03-15 04:42:34 +00:00
# include <components/sceneutil/lightmanager.hpp>
2021-02-21 18:38:15 +00:00
namespace Resource
{
class SceneManager ;
}
2021-03-15 04:42:34 +00:00
namespace SceneUtil
{
enum class LightingMethod ;
}
2016-02-16 17:18:48 +00:00
namespace Shader
{
2021-03-15 04:42:34 +00:00
enum class UBOBinding
{
LightBuffer
} ;
2016-02-16 17:18:48 +00:00
/// @brief Reads shader template files and turns them into a concrete shader, based on a list of define's.
/// @par Shader templates can get the value of a define with the syntax @define.
class ShaderManager
{
public :
2021-02-21 18:38:15 +00:00
2021-03-02 07:30:54 +00:00
ShaderManager ( ) ;
2021-02-21 18:38:15 +00:00
2016-02-16 17:18:48 +00:00
void setShaderPath ( const std : : string & path ) ;
2021-03-15 04:42:34 +00:00
void setLightingMethod ( SceneUtil : : LightingMethod method ) ;
2021-03-02 07:30:54 +00:00
2016-02-16 17:18:48 +00:00
typedef std : : map < std : : string , std : : string > DefineMap ;
/// Create or retrieve a shader instance.
/// @param shaderTemplate The filename of the shader template.
/// @param defines Define values that can be retrieved by the shader template.
/// @param shaderType The type of shader (usually vertex or fragment shader).
2018-10-09 06:21:12 +00:00
/// @note May return nullptr on failure.
2016-02-16 17:18:48 +00:00
/// @note Thread safe.
2020-05-01 17:11:04 +00:00
osg : : ref_ptr < osg : : Shader > getShader ( const std : : string & templateName , const DefineMap & defines , osg : : Shader : : Type shaderType ) ;
2016-02-16 17:18:48 +00:00
2016-02-17 14:04:05 +00:00
osg : : ref_ptr < osg : : Program > getProgram ( osg : : ref_ptr < osg : : Shader > vertexShader , osg : : ref_ptr < osg : : Shader > fragmentShader ) ;
2017-11-22 20:07:07 +00:00
/// Get (a copy of) the DefineMap used to construct all shaders
DefineMap getGlobalDefines ( ) ;
/// Set the DefineMap used to construct all shaders
/// @param defines The DefineMap to use
2017-12-17 23:55:19 +00:00
/// @note This will change the source code for any shaders already created, potentially causing problems if they're being used to render a frame. It is recommended that any associated Viewers have their threading stopped while this function is running if any shaders are in use.
void setGlobalDefines ( DefineMap & globalDefines ) ;
2017-11-22 20:07:07 +00:00
2017-08-26 19:28:23 +00:00
void releaseGLObjects ( osg : : State * state ) ;
2016-02-17 14:04:05 +00:00
2016-02-16 17:18:48 +00:00
private :
std : : string mPath ;
2017-11-22 20:07:07 +00:00
DefineMap mGlobalDefines ;
2016-02-16 17:18:48 +00:00
// <name, code>
typedef std : : map < std : : string , std : : string > TemplateMap ;
TemplateMap mShaderTemplates ;
typedef std : : pair < std : : string , DefineMap > MapKey ;
typedef std : : map < MapKey , osg : : ref_ptr < osg : : Shader > > ShaderMap ;
ShaderMap mShaders ;
2016-02-17 14:04:05 +00:00
typedef std : : map < std : : pair < osg : : ref_ptr < osg : : Shader > , osg : : ref_ptr < osg : : Shader > > , osg : : ref_ptr < osg : : Program > > ProgramMap ;
ProgramMap mPrograms ;
2021-03-15 04:42:34 +00:00
SceneUtil : : LightingMethod mLightingMethod ;
2021-02-21 18:38:15 +00:00
2020-06-25 19:46:07 +00:00
std : : mutex mMutex ;
2016-02-16 17:18:48 +00:00
} ;
2020-05-01 21:03:13 +00:00
bool parseFors ( std : : string & source , const std : : string & templateName ) ;
bool parseDefines ( std : : string & source , const ShaderManager : : DefineMap & defines ,
const ShaderManager : : DefineMap & globalDefines , const std : : string & templateName ) ;
2016-02-16 17:18:48 +00:00
}
# endif