SFML community forums

Help => Graphics => Topic started by: roccio on November 03, 2015, 09:16:37 am

Title: Conversion from BGRA to RGBA
Post by: roccio on November 03, 2015, 09:16:37 am
Hello,
I have a byte array in BGRA format and I use it to update a sf::Texture. The problem is that sf::Texture only supports RGBA forrmat. Is there a fast solution for this conversion? Or (like I do now) I have to swap the bytes manually?

Thank you
Title: Re: Conversion from BGRA to RGBA
Post by: Laurent on November 03, 2015, 09:34:06 am
You can hack a faster solution with an OpenGL call:

sf::Texture::bind(&texture);
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, GL_BGRA, GL_UNSIGNED_BYTE, bytes);

Depending on how the driver implements the conversion, the speed difference may be more or less noticeable.

However this may break the internal cache of SFML. You can make it almost safe by preserving the texture binding:

GLint textureBinding;
glGetIntegerv(GL_TEXTURE_BINDING_2D, &textureBinding);
sf::Texture::bind(&texture);
glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, GL_BGRA, GL_UNSIGNED_BYTE, bytes);
glBindTexture(GL_TEXTURE_2D, textureBinding);

Try with caution ;)
Title: Re: Conversion from BGRA to RGBA
Post by: roccio on November 03, 2015, 12:12:26 pm
I will try. Thank you Laurent!
Title: Re: Conversion from BGRA to RGBA
Post by: Aravol on February 22, 2016, 07:55:54 pm
Looking at needing to do this exact thing for a current project. Would the rebind be necessary if the texture is only edited from a dedicated thread (ie only this thread edits this image, this thread only edits this image), or does the mentioned internal cache cross threads? If there's a difference, I'm also using the .NET wrappers rather than the C++ libraries.
Title: Re: Conversion from BGRA to RGBA
Post by: Laurent on February 24, 2016, 08:44:58 pm
What's important is in which OpenGL context you perform the operation. If the thread uses the window's context (ie. you called window.SetActive(true) in the thread), then you need to preserve OpenGL states. If you're using a dedicated dummy context (ie. you instanciate the Context class at the beginning of your thread) then you don't have to do it.