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

Author Topic: Smart Shader Binding  (Read 5053 times)

0 Members and 1 Guest are viewing this topic.

Gregouar

  • Sr. Member
  • ****
  • Posts: 462
  • www.holyspirit.fr : free hack'&'slash
    • MSN Messenger - Greg_le_sadique@hotmail.com
    • View Profile
    • www.holyspirit.fr
    • Email
Smart Shader Binding
« on: March 14, 2018, 03:33:35 pm »
Hi !

If I understood well, binding textures is a pretty heavy operation. That's why SFML try not to unbind them if we do several draw calls in a row, using the same texture.
I wonder if it would be possible, or even worth it, to try to do something similar with shaders ?

The idea would be to try to not bind/unbind shader if we continue to use the same, except maybe if a uniform sampler has changed (and in this case, only rebind the corresponding texture). I guess the difficulty would be to track down in which context the samplers have been updated yet... Maybe this can be done by keeping the linked textureId of the context in memory in a std::map (like TextureTable in the shader class). Then we only need to compare them to the one linked in the shader, if it has not been changed ? This could be good if comparing two small std::maps is really faster than binding textures.

For example, maybe all sprites we want to draw using a shader needs to read in the same texture. I'll try to do something like this and see if it has any significant impact on performances.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Smart Shader Binding
« Reply #1 on: March 14, 2018, 05:40:41 pm »
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Gregouar

  • Sr. Member
  • ****
  • Posts: 462
  • www.holyspirit.fr : free hack'&'slash
    • MSN Messenger - Greg_le_sadique@hotmail.com
    • View Profile
    • www.holyspirit.fr
    • Email
Re: Smart Shader Binding
« Reply #2 on: July 26, 2018, 05:28:35 am »
Coming soon™ to an SFML near you.
While your upgrade is really good, I believe it is not what I was thinking about. The thing is now whenever we do a RenderTarget::Draw() call, the shader is rebinded every time, and so are the linked sampler. So it means  quite a lot of texture binding if your shader use  a lot of different sampler. And I believe this could be avoided by checking if the shader has changed, or its sampler.
However I guess that now with your upgrade it would be even harder to track down the binding in the different contexts. So its probably not worth it except for some very specific cases, and thus it is maybe not something that SFML should do ?