I finally found the answers myself! Posting it here so that it may help someone in future.
Since OpenGL doesn't support ARGB, giving BGRA is understandable. But how come RGBA is working correctly here? This seems wrong.
By inspecting the memory pointed to by void* data
that GLI returns when a pointer to the image's binary data is asked for, it can be seen that GLI had already reordered the bytes when transferring data from the file to client memory. The memeory window shows, from lower to higher address, data in the form RR GG BB AA
. This explains why passing GL_RGBA
works. However, the wrong on GLI's part is that when external format is queried for it returns GL_BGRA
instead of GL_RGBA
. A bug to address this has been raised.
Why does the type have no effect on the ordering of the channels?
No, it has an effect. The machine that I'm trying this experiment on is an Intel x86_64 little endian machine. OpenGL Wiki clearly states that the client pixel data is always in client byte ordering. Now when GL_UNSIGNED_BYTE
or GL_UNSIGNED_INT_8_8_8_8_REV
is passed, the underlying base type (not the component type) is an unsigned int
for both; thus reading an int
from data
, on a little-endian machine would mean, the variable in register would end up with the bytes swapped i.e. RR GG BB AA
in the RAM would reach the VRAM asAA BB GG RR
; when addressed by a texture of type RGBA (RR GG BB AA
), reading AA
would actually give RR
. To correct it, the OpenGL implementation swaps the bytes to neutralise the endianness of the machine, in the case of GL_UNSIGNED_BYTE
type. While for GL_UNSIGNED_INT_8_8_8_8_REV
we explicitly instruct OpenGL to swap the byte order and thus it renders correctly. However, if the type is passed as GL_UNSIGNED_INT_8_8_8_8
then the rendering is screwed up, since we instruct OpenGL to copy the bytes as it was read on the machine.
Does the GL_UNPACK_ALIGNMENT
have a bearing in this? I left it as the default (4). If I read the manual right, this should have no effect on how the client memory is read.
It does have a bearing on the unpacking of texture data from client memory to server memory. However, that's to account for the padding bytes present in an image's rows to compute the stride (pitch) correctly. But to this issue specifically it doesn't have a bearing since it's pitch flag is 0 i.e. there're no padding bits in the DDS file in question.
Related material: https://www.opengl.org/wiki/Pixel_Transfer#Pixel_type