Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Shader uniform not updating?  (Read 3282 times)

0 Members and 1 Guest are viewing this topic.

_Fiction_

  • Newbie
  • *
  • Posts: 35
    • View Profile
Shader uniform not updating?
« on: January 03, 2022, 04:46:30 am »
I am having an issue that probably stems from some misunderstanding of sf::Shader::CurrentTexture, but I am very confused.

This code sets the texture in my shader correctly, and everything works.

pShader.setUniform("u_texture", *texture);

However, I had a bug in part of my code for awhile where it wasn't working, and I couldn't figure out why. After investigating a bit, I found that I had set the texture parameter earlier like this.

pShader.setUniform("u_texture", sf::Shader::CurrentTexture);

But my understanding of uniforms is that I can freely update them. Running this code:

pShader.setUniform("u_texture", sf::Shader::CurrentTexture);
pShader.setUniform("u_texture", *texture);

the second line is ignored completely, and the texture doesn't get set correctly. Can someone explain this to me?

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Shader uniform not updating?
« Reply #1 on: January 03, 2022, 05:16:30 am »
From what I can see, the problem is in Shader::bind().
Inside of here, it does this to bind textures to the shader:
// Bind the textures
shader->bindTextures();

// Bind the current texture
if (shader->m_currentTexture != -1)
    glCheck(GLEXT_glUniform1i(shader->m_currentTexture, 0));

Doing a setUniform with a texture puts it into the list of textures used by bindTextures().
Doing a setUniform with sf::Shader::CurrentTexture sets m_currentTexture. So neither immediately apply the texture, it is delayed until binding. Then the order is always manual textures first, then CurrentTexture  (if present) replaces texture 0.

_Fiction_

  • Newbie
  • *
  • Posts: 35
    • View Profile
Re: Shader uniform not updating?
« Reply #2 on: January 03, 2022, 05:40:30 am »
What does this mean for me? Does that just mean its something I have to work around?

Thanks for the response.

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Shader uniform not updating?
« Reply #3 on: January 03, 2022, 08:12:56 am »
It means if you do both pShader.setUniform("u_texture", sf::Shader::CurrentTexture); and pShader.setUniform("u_texture", *texture); to the same shader, the order you call them doesn't matter, the sf::Shader::CurrentTexture will always happen last and overwrite what the other one did.
(Note: I'm just looking at the source, I don't think I've ever used sf::Shader::CurrentTexture myself)

As to whether you need to work around it, what is the use case you are trying to achieve that needs the texture set using both techniques on the one shader?