Fixed images and shaders sometimes not updated when they are modified while they are used

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1427 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
LaurentGom 2010-02-25 19:05:57 +00:00
parent 76a76a784f
commit 6f6481ef38
5 changed files with 86 additions and 24 deletions

View File

@ -37,6 +37,7 @@
namespace sf namespace sf
{ {
class Renderer;
class RenderImage; class RenderImage;
class RenderWindow; class RenderWindow;
@ -291,6 +292,7 @@ public :
private : private :
friend class Renderer;
friend class RenderImage; friend class RenderImage;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
@ -315,6 +317,12 @@ private :
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void EnsureArrayUpdate() const; void EnsureArrayUpdate() const;
////////////////////////////////////////////////////////////
/// Make sure that the image is ready to be used
///
////////////////////////////////////////////////////////////
void Use() const;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Reset the image attributes /// Reset the image attributes
/// ///

View File

@ -38,6 +38,8 @@
namespace sf namespace sf
{ {
class Renderer;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Pixel/fragment shader class /// \brief Pixel/fragment shader class
/// ///
@ -317,6 +319,8 @@ public :
private : private :
friend class Renderer;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Create the program and attach the shaders /// \brief Create the program and attach the shaders
/// ///
@ -325,6 +329,25 @@ private :
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool CompileProgram(); bool CompileProgram();
////////////////////////////////////////////////////////////
/// \brief Bind all the textures used by the shader
///
/// This function each texture to a different unit, and
/// updates the corresponding variables in the shader accordingly.
///
////////////////////////////////////////////////////////////
void BindTextures() const;
////////////////////////////////////////////////////////////
/// \brief Make sure that the shader is ready to be used
///
/// This function is called by the Renderer class, to make
/// sure that the shader's parameters are properly applied
/// even when Use() is not called due to internal optimizations.
///
////////////////////////////////////////////////////////////
void Use() const;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Types // Types
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

View File

@ -42,15 +42,15 @@ namespace sf
/// Default constructor /// Default constructor
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Image::Image() : Image::Image() :
myWidth (0), myWidth (0),
myHeight (0), myHeight (0),
myTextureWidth (0), myTextureWidth (0),
myTextureHeight (0), myTextureHeight (0),
myTexture (0), myTexture (0),
myIsSmooth (true), myIsSmooth (true),
myTextureUpdated (true), myTextureUpdated(true),
myArrayUpdated (true), myArrayUpdated (true),
myPixelsFlipped (false) myPixelsFlipped (false)
{ {
} }
@ -387,12 +387,12 @@ void Image::SetPixel(unsigned int x, unsigned int y, const Color& color)
EnsureArrayUpdate(); EnsureArrayUpdate();
// Check if pixel is whithin the image bounds // Check if pixel is whithin the image bounds
if ((x >= myWidth) || (y >= myHeight)) /*if ((x >= myWidth) || (y >= myHeight))
{ {
Err() << "Cannot set pixel (" << x << "," << y << ") for image " Err() << "Cannot set pixel (" << x << "," << y << ") for image "
<< "(width = " << myWidth << ", height = " << myHeight << ")" << std::endl; << "(width = " << myWidth << ", height = " << myHeight << ")" << std::endl;
return; return;
} }*/
myPixels[x + y * myWidth] = color; myPixels[x + y * myWidth] = color;
@ -723,10 +723,6 @@ void Image::EnsureArrayUpdate() const
{ {
if (!myArrayUpdated) if (!myArrayUpdated)
{ {
// First make sure the texture is up-to-date
// (may not be the case if an external update has been scheduled)
EnsureTextureUpdate();
// Save the previous texture // Save the previous texture
GLint previous; GLint previous;
GLCheck(glGetIntegerv(GL_TEXTURE_BINDING_2D, &previous)); GLCheck(glGetIntegerv(GL_TEXTURE_BINDING_2D, &previous));
@ -779,6 +775,15 @@ void Image::EnsureArrayUpdate() const
} }
////////////////////////////////////////////////////////////
/// Make sure that the image is ready to be used
////////////////////////////////////////////////////////////
void Image::Use() const
{
EnsureTextureUpdate();
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// Reset the image attributes /// Reset the image attributes
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

View File

@ -248,6 +248,12 @@ void Renderer::SetTexture(const Image* texture)
myTexture = texture; myTexture = texture;
myTextureIsValid = true; myTextureIsValid = true;
} }
else if (texture)
{
// If the texture was already the current one, make sure that
// it is synchronized (in case it was modified since last use)
texture->Use();
}
} }
@ -269,6 +275,12 @@ void Renderer::SetShader(const Shader* shader)
myShaderIsValid = true; myShaderIsValid = true;
} }
} }
else if (shader)
{
// If the shader was already the current one, make sure that
// it is synchronized (in case it was modified since last use)
shader->Use();
}
} }

View File

@ -245,15 +245,7 @@ void Shader::Bind() const
GLCheck(glUseProgramObjectARB(myShaderProgram)); GLCheck(glUseProgramObjectARB(myShaderProgram));
// Bind the textures // Bind the textures
TextureTable::const_iterator it = myTextures.begin(); BindTextures();
for (std::size_t i = 0; i < myTextures.size(); ++i)
{
GLint index = static_cast<GLsizei>(i + 1);
GLCheck(glUniform1iARB(it->first, index));
GLCheck(glActiveTextureARB(GL_TEXTURE0_ARB + index));
it->second->Bind();
it++;
}
// Make sure that the texture unit which is left active is the number 0 // Make sure that the texture unit which is left active is the number 0
GLCheck(glActiveTextureARB(GL_TEXTURE0_ARB)); GLCheck(glActiveTextureARB(GL_TEXTURE0_ARB));
@ -397,4 +389,26 @@ bool Shader::CompileProgram()
return true; return true;
} }
////////////////////////////////////////////////////////////
void Shader::BindTextures() const
{
TextureTable::const_iterator it = myTextures.begin();
for (std::size_t i = 0; i < myTextures.size(); ++i)
{
GLint index = static_cast<GLsizei>(i + 1);
GLCheck(glUniform1iARB(it->first, index));
GLCheck(glActiveTextureARB(GL_TEXTURE0_ARB + index));
it->second->Bind();
it++;
}
}
////////////////////////////////////////////////////////////
void Shader::Use() const
{
BindTextures();
}
} // namespace sf } // namespace sf