1#include <Engine/Data/ShaderConfiguration.hpp>
2#include <Engine/Data/ShaderProgram.hpp>
3#include <Engine/Data/ShaderProgramManager.hpp>
5#include <Core/Containers/MakeShared.hpp>
6#include <Core/Utils/Log.hpp>
8#include <globjects/NamedString.h>
9#include <globjects/Shader.h>
10#include <globjects/base/File.h>
16using namespace Core::Utils;
21ShaderProgramManager::~ShaderProgramManager() {
22 m_shaderPrograms.clear();
27 auto el = m_namedStrings.
find( includepath );
28 if ( el != m_namedStrings.
end() ) {
29 if ( el->second.first->filePath() != realfile ) {
31 <<
"[ShaderProgramManager] A named string already exists with this key: "
32 << includepath <<
" --> " << el->second.first->filePath();
35 LOG( logINFO ) <<
"[ShaderProgramManager] Named string already inserted, skipping: "
36 << includepath <<
" --> " << realfile;
42 auto file = globjects::File::create( realfile );
44 std::move( file ), globjects::NamedString::create( includepath, file.get() ) );
50 for (
auto& el : m_namedStrings ) {
51 el.second.first->reload();
52 el.second.second.reset(
nullptr );
53 el.second.second = globjects::NamedString::create( el.first, el.second.first.get() );
57Core::Utils::optional<const Data::ShaderProgram*>
59 auto found = m_shaderPrograms.find( config );
61 if ( found != m_shaderPrograms.end() ) {
return found->second.get(); }
64 for (
const auto& p : config.getNamedStrings() ) {
71 if ( prog->getProgramObject()->isLinked() ) {
72 insertShader( config, prog );
77 LOG( logERROR ) <<
"Error occurred while loading shader program "
78 << config.getName().
c_str() <<
":\nDefault shader program used instead.";
80 for (
const auto& strings : config.getNamedStrings() ) {
81 auto el = m_namedStrings.
find( strings.first );
82 if ( el != m_namedStrings.
end() ) {
83 if ( el->second.first->filePath() == strings.second ) {
84 LOG( logINFO ) <<
"[ShaderProgramManager] Removing named string "
85 << strings.first <<
" --> " << strings.second;
91 m_shaderFailedConfs.push_back( config );
98 auto found = m_shaderProgramIds.find(
id );
100 if ( found != m_shaderProgramIds.end() ) {
return getShaderProgram( found->second ); }
107 return bool( res ) ? *res :
nullptr;
115 for (
auto& shader : m_shaderPrograms ) {
116 shader.second->reload();
125 for (
const auto& conf : m_shaderFailedConfs ) {
128 if ( prog->getProgramObject()->isValid() ) {
129 insertShader( conf, prog );
137 m_shaderProgramIds.insert( { config.getName(), config } );
138 m_shaderPrograms.insert( { config, shader } );
void reloadNotCompiledShaderPrograms()
Core::Utils::optional< const Data::ShaderProgram * > addShaderProgram(const Data::ShaderConfiguration &config)
ShaderProgramManager()
Rule of three needed here to prevent copy on this manager (only movable)
const Data::ShaderProgram * getShaderProgram(const std::string &id)
bool addNamedString(const std::string &includepath, const std::string &realfile)
void reloadAllShaderPrograms()
std::shared_ptr< T > make_shared(Args &&... args)
hepler function to manage enum as underlying types in VariableSet