Commit 775b123f authored by Robert Menzel's avatar Robert Menzel

more robust shader reloading

parent 13333421
......@@ -159,8 +159,8 @@ protected:
SharedLocationMappings mUniformBufferLocations;
private:
// set attribute & fragdata locations prior to shader program linking
void setBindings(SharedShaderProgram &_shaderProgram);
// set attribute, UBO & fragdata locations and links the program
bool setBindings(SharedShaderProgram &_shaderProgram);
};
} // OpenGL
......
......@@ -45,8 +45,19 @@ bool ShaderControlFile::update(SharedShader& shader)
if(fileIsUpToDate())
return false;
if(!shader->setFromFile(mFullFilePath))
return false;
{ // try to compile the source in another shader, only proceed if that worked!
Shader dummy( shader->getType() );
if (!dummy.setFromFile(mFullFilePath)) {
// we had a shader compile error, update the timestamp to prevent a second try
// of compiling the exact same (non-working) shader again
updateFileModificationTime();
return false;
}
}
// it worked, so load the source in "our" shader:
shader->setFromFile(mFullFilePath);
updateFileModificationTime();
return true;
......
......@@ -113,27 +113,27 @@ SharedShaderProgram ShaderProgramControlFiles::create(void)
}
}
if (!shaderProgram->link()) {
return SharedShaderProgram(); // linking failed
if (!setBindings( shaderProgram )) {
return SharedShaderProgram(); // e.g. linking failed
}
setBindings( shaderProgram ); // will relink, but needs a linked program :-(
openGLCommonErrorOccured();
updateFileModificationTimes();
return shaderProgram;
}
// program has to be linked before calling setBindings!
void ShaderProgramControlFiles::setBindings(SharedShaderProgram &_shaderProgram)
// will get called from the create and update functions:
bool ShaderProgramControlFiles::setBindings(SharedShaderProgram &_shaderProgram)
{
openGLRareErrorOccured();
_shaderProgram->link();
# if (ACGL_OPENGL_VERSION >= 30)
_shaderProgram->setFragmentDataLocations( mFragmentDataLocations ); // will relink on it's own
_shaderProgram->setFragmentDataLocations( mFragmentDataLocations ); // might relink on it's own
openGLRareErrorOccured();
_shaderProgram->setAttributeLocations( mAttributeLocations );
SharedLocationMappings oldAttributeMap = _shaderProgram->getAttributeLocations();
mAttributeLocations->addLocations( oldAttributeMap ); // add as many old locations as possible without destoying the location map
_shaderProgram->setAttributeLocations( mAttributeLocations ); // will relink on it's own
_shaderProgram->setAttributeLocations( mAttributeLocations ); // might relink on it's own
openGLRareErrorOccured();
# else
......@@ -142,6 +142,9 @@ void ShaderProgramControlFiles::setBindings(SharedShaderProgram &_shaderProgram)
}
# endif
// in case the attribute/fragdata locations did not change, those calls will not call glLinkProgram
if (_shaderProgram->link() == false) return false;
// uniform block binding have to be set after linking!
if ( mUniformBufferLocations->getSize() > 0) {
# if (ACGL_OPENGL_VERSION >= 31)
......@@ -154,14 +157,14 @@ void ShaderProgramControlFiles::setBindings(SharedShaderProgram &_shaderProgram)
Utils::error() << "can't set uniform buffer locations on OpenGL < 3.1" << std::endl;
# endif
}
return true;
}
bool ShaderProgramControlFiles::update(SharedShaderProgram &_shaderProgram)
{
bool update = false;
//Utils::debug() << "updating " << mResourceName << std::endl;
for (std::vector<std::string>::size_type i = 0; i < mFileNames.size(); ++i) {
if ( mShaderType[i] != GL_INVALID_VALUE ) {
update |= ShaderFileManager::the()->update( mFileNames[i] );
......@@ -170,10 +173,12 @@ bool ShaderProgramControlFiles::update(SharedShaderProgram &_shaderProgram)
if ((!filesAreUpToDate() ) || (update))
{
Utils::debug() << "updating " << mResourceName << " AND RELINKING!" << std::endl;
setBindings( _shaderProgram );
// at least one shader was updated, so update the program:
// keep the old mappings and relink:
Utils::debug() << "updating " << mResourceName << std::endl;
bool shaderProgramOK = setBindings( _shaderProgram ); // will relink, and set UBO bindings (don't relink after this!)
updateFileModificationTimes();
return _shaderProgram->link();
return shaderProgramOK;
}
return false;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment