SDL_PIXELFORMAT_RGBA8888 on macOS

I’m wondering about the SDL_PIXELFORMAT_XBGR8888 pixel format of a window surface on macOS running on an M4.

When I SDL_GetWindowPixelFormat I get SDL_PIXELFORMAT_ARGB8888. Why are these two different? I would expect them to be the same.

Since M4 is little endian, this means

  • SDL_PIXELFORMAT_ARGB8888 = SDL_PIXELFORMAT_BGRA32
  • SDL_PIXELFORMAT_XBGR8888 = SDL_PIXELFORMAT_RGBX32

Most graphic libs use XRGB32 or PRGB32.

So, when I use such a library to draw into a SDL_surface, the colors are wrong.

What’s the best way to either avoid this pixelformat mess, or convert the pixels to SDL_PIXELFORMAT_XBGR8888 surface format just before updating the window?

One solution might be to render to an off-screen render texture and then render that texture to the framebuffer. The render texture can use your preferred texture format and the underlying graphics API will take care of the byte swapping for you.

SDL_BlitSurface() does an automatic pixel format conversion. That’s much simpler than twiddling with textures, which are mainly targeting GPU usage.

However, I’m wondering why the window and window.surface pixel formats are different at all.