Make sure native textures have the same channel precision if possible

Fixes https://github.com/libsdl-org/SDL/issues/14882
This commit is contained in:
Sam Lantinga
2026-02-06 09:50:18 -08:00
parent ef41e0a0f0
commit fdfcfc0566

View File

@@ -1469,10 +1469,12 @@ static SDL_PixelFormat GetClosestSupportedFormat(SDL_Renderer *renderer, SDL_Pix
} else {
bool hasAlpha = SDL_ISPIXELFORMAT_ALPHA(format);
bool isIndexed = SDL_ISPIXELFORMAT_INDEXED(format);
int size = SDL_BYTESPERPIXEL(format);
// We just want to match the first format that has the same channels
for (i = 0; i < renderer->num_texture_formats; ++i) {
if (!SDL_ISPIXELFORMAT_FOURCC(renderer->texture_formats[i]) &&
SDL_BYTESPERPIXEL(renderer->texture_formats[i]) == size &&
SDL_ISPIXELFORMAT_ALPHA(renderer->texture_formats[i]) == hasAlpha &&
SDL_ISPIXELFORMAT_INDEXED(renderer->texture_formats[i]) == isIndexed) {
return renderer->texture_formats[i];
@@ -1846,9 +1848,11 @@ SDL_Texture *SDL_CreateTextureFromSurface(SDL_Renderer *renderer, SDL_Surface *s
// Indexed formats don't support the transparency needed for color-keyed surfaces
bool preferIndexed = SDL_ISPIXELFORMAT_INDEXED(surface->format) && !needAlpha;
int size = SDL_BYTESPERPIXEL(format);
for (i = 0; i < renderer->num_texture_formats; ++i) {
if (!SDL_ISPIXELFORMAT_FOURCC(renderer->texture_formats[i]) &&
SDL_BYTESPERPIXEL(renderer->texture_formats[i]) == size &&
SDL_ISPIXELFORMAT_ALPHA(renderer->texture_formats[i]) == needAlpha &&
SDL_ISPIXELFORMAT_INDEXED(renderer->texture_formats[i]) == preferIndexed) {
format = renderer->texture_formats[i];