SFML community forums

General => Feature requests => Topic started by: Gregouar on March 14, 2018, 03:33:35 pm

Title: Smart Shader Binding
Post by: Gregouar 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.
Title: Re: Smart Shader Binding
Post by: binary1248 on March 14, 2018, 05:40:41 pm
Coming soon™ to an SFML near you. (https://github.com/SFML/SFML/pull/1379#issuecomment-372133369)
Title: Re: Smart Shader Binding
Post by: Gregouar on July 26, 2018, 05:28:35 am
Coming soon™ to an SFML near you. (https://github.com/SFML/SFML/pull/1379#issuecomment-372133369)
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 ?