Use the correct pixel formats for OpenGL ES on big endian

This commit is contained in:
Cameron Cawley 2023-11-17 18:40:50 +00:00 committed by Sam Lantinga
parent 2222f44c94
commit 1bb5448fe8
3 changed files with 44 additions and 62 deletions

View file

@ -276,11 +276,19 @@ typedef enum
SDL_PIXELFORMAT_ARGB32 = SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_ARGB32 = SDL_PIXELFORMAT_ARGB8888,
SDL_PIXELFORMAT_BGRA32 = SDL_PIXELFORMAT_BGRA8888, SDL_PIXELFORMAT_BGRA32 = SDL_PIXELFORMAT_BGRA8888,
SDL_PIXELFORMAT_ABGR32 = SDL_PIXELFORMAT_ABGR8888, SDL_PIXELFORMAT_ABGR32 = SDL_PIXELFORMAT_ABGR8888,
SDL_PIXELFORMAT_RGBX32 = SDL_PIXELFORMAT_RGBX8888,
SDL_PIXELFORMAT_XRGB32 = SDL_PIXELFORMAT_XRGB8888,
SDL_PIXELFORMAT_BGRX32 = SDL_PIXELFORMAT_BGRX8888,
SDL_PIXELFORMAT_XBGR32 = SDL_PIXELFORMAT_XBGR8888,
#else #else
SDL_PIXELFORMAT_RGBA32 = SDL_PIXELFORMAT_ABGR8888, SDL_PIXELFORMAT_RGBA32 = SDL_PIXELFORMAT_ABGR8888,
SDL_PIXELFORMAT_ARGB32 = SDL_PIXELFORMAT_BGRA8888, SDL_PIXELFORMAT_ARGB32 = SDL_PIXELFORMAT_BGRA8888,
SDL_PIXELFORMAT_BGRA32 = SDL_PIXELFORMAT_ARGB8888, SDL_PIXELFORMAT_BGRA32 = SDL_PIXELFORMAT_ARGB8888,
SDL_PIXELFORMAT_ABGR32 = SDL_PIXELFORMAT_RGBA8888, SDL_PIXELFORMAT_ABGR32 = SDL_PIXELFORMAT_RGBA8888,
SDL_PIXELFORMAT_RGBX32 = SDL_PIXELFORMAT_XBGR8888,
SDL_PIXELFORMAT_XRGB32 = SDL_PIXELFORMAT_BGRX8888,
SDL_PIXELFORMAT_BGRX32 = SDL_PIXELFORMAT_XRGB8888,
SDL_PIXELFORMAT_XBGR32 = SDL_PIXELFORMAT_RGBX8888,
#endif #endif
SDL_PIXELFORMAT_YV12 = /**< Planar mode: Y + V + U (3 planes) */ SDL_PIXELFORMAT_YV12 = /**< Planar mode: Y + V + U (3 planes) */

View file

@ -322,7 +322,7 @@ static int GLES_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture)
GLES_ActivateRenderer(renderer); GLES_ActivateRenderer(renderer);
switch (texture->format) { switch (texture->format) {
case SDL_PIXELFORMAT_ABGR8888: case SDL_PIXELFORMAT_RGBA32:
internalFormat = GL_RGBA; internalFormat = GL_RGBA;
format = GL_RGBA; format = GL_RGBA;
type = GL_UNSIGNED_BYTE; type = GL_UNSIGNED_BYTE;
@ -900,7 +900,7 @@ static int GLES_RenderReadPixels(SDL_Renderer *renderer, const SDL_Rect *rect,
Uint32 pixel_format, void *pixels, int pitch) Uint32 pixel_format, void *pixels, int pitch)
{ {
GLES_RenderData *data = (GLES_RenderData *)renderer->driverdata; GLES_RenderData *data = (GLES_RenderData *)renderer->driverdata;
Uint32 temp_format = renderer->target ? renderer->target->format : SDL_PIXELFORMAT_ABGR8888; Uint32 temp_format = renderer->target ? renderer->target->format : SDL_PIXELFORMAT_RGBA32;
void *temp_pixels; void *temp_pixels;
int temp_pitch; int temp_pitch;
Uint8 *src, *dst, *tmp; Uint8 *src, *dst, *tmp;
@ -1210,7 +1210,7 @@ SDL_RenderDriver GLES_RenderDriver = {
{ "opengles", { "opengles",
(SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC), (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
1, 1,
{ SDL_PIXELFORMAT_ABGR8888 }, { SDL_PIXELFORMAT_RGBA32 },
0, 0,
0 } 0 }
}; };

View file

@ -739,7 +739,7 @@ static int GLES2_QueueSetViewport(SDL_Renderer *renderer, SDL_RenderCommand *cmd
static int GLES2_QueueDrawPoints(SDL_Renderer *renderer, SDL_RenderCommand *cmd, const SDL_FPoint *points, int count) static int GLES2_QueueDrawPoints(SDL_Renderer *renderer, SDL_RenderCommand *cmd, const SDL_FPoint *points, int count)
{ {
const SDL_bool colorswap = (renderer->target && (renderer->target->format == SDL_PIXELFORMAT_ARGB8888 || renderer->target->format == SDL_PIXELFORMAT_RGB888)); const SDL_bool colorswap = (renderer->target && (renderer->target->format == SDL_PIXELFORMAT_BGRA32 || renderer->target->format == SDL_PIXELFORMAT_BGRX32));
SDL_VertexSolid *verts = (SDL_VertexSolid *)SDL_AllocateRenderVertices(renderer, count * sizeof(*verts), 0, &cmd->data.draw.first); SDL_VertexSolid *verts = (SDL_VertexSolid *)SDL_AllocateRenderVertices(renderer, count * sizeof(*verts), 0, &cmd->data.draw.first);
int i; int i;
SDL_Color color; SDL_Color color;
@ -771,7 +771,7 @@ static int GLES2_QueueDrawPoints(SDL_Renderer *renderer, SDL_RenderCommand *cmd,
static int GLES2_QueueDrawLines(SDL_Renderer *renderer, SDL_RenderCommand *cmd, const SDL_FPoint *points, int count) static int GLES2_QueueDrawLines(SDL_Renderer *renderer, SDL_RenderCommand *cmd, const SDL_FPoint *points, int count)
{ {
const SDL_bool colorswap = (renderer->target && (renderer->target->format == SDL_PIXELFORMAT_ARGB8888 || renderer->target->format == SDL_PIXELFORMAT_RGB888)); const SDL_bool colorswap = (renderer->target && (renderer->target->format == SDL_PIXELFORMAT_BGRA32 || renderer->target->format == SDL_PIXELFORMAT_BGRX32));
int i; int i;
GLfloat prevx, prevy; GLfloat prevx, prevy;
SDL_VertexSolid *verts = (SDL_VertexSolid *)SDL_AllocateRenderVertices(renderer, count * sizeof(*verts), 0, &cmd->data.draw.first); SDL_VertexSolid *verts = (SDL_VertexSolid *)SDL_AllocateRenderVertices(renderer, count * sizeof(*verts), 0, &cmd->data.draw.first);
@ -831,7 +831,7 @@ static int GLES2_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, S
float scale_x, float scale_y) float scale_x, float scale_y)
{ {
int i; int i;
const SDL_bool colorswap = (renderer->target && (renderer->target->format == SDL_PIXELFORMAT_ARGB8888 || renderer->target->format == SDL_PIXELFORMAT_RGB888)); const SDL_bool colorswap = (renderer->target && (renderer->target->format == SDL_PIXELFORMAT_BGRA32 || renderer->target->format == SDL_PIXELFORMAT_BGRX32));
int count = indices ? num_indices : num_vertices; int count = indices ? num_indices : num_vertices;
cmd->data.draw.count = count; cmd->data.draw.count = count;
@ -1029,50 +1029,50 @@ static int SetCopyState(SDL_Renderer *renderer, const SDL_RenderCommand *cmd, vo
/* Check if we need to do color mapping between the source and render target textures */ /* Check if we need to do color mapping between the source and render target textures */
if (renderer->target->format != texture->format) { if (renderer->target->format != texture->format) {
switch (texture->format) { switch (texture->format) {
case SDL_PIXELFORMAT_ARGB8888: case SDL_PIXELFORMAT_BGRA32:
switch (renderer->target->format) { switch (renderer->target->format) {
case SDL_PIXELFORMAT_ABGR8888: case SDL_PIXELFORMAT_RGBA32:
case SDL_PIXELFORMAT_BGR888: case SDL_PIXELFORMAT_RGBX32:
sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB; sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
break; break;
case SDL_PIXELFORMAT_RGB888: case SDL_PIXELFORMAT_BGRX32:
sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR; sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR;
break; break;
} }
break; break;
case SDL_PIXELFORMAT_ABGR8888: case SDL_PIXELFORMAT_RGBA32:
switch (renderer->target->format) { switch (renderer->target->format) {
case SDL_PIXELFORMAT_ARGB8888: case SDL_PIXELFORMAT_BGRA32:
case SDL_PIXELFORMAT_RGB888: case SDL_PIXELFORMAT_BGRX32:
sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB; sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
break; break;
case SDL_PIXELFORMAT_BGR888: case SDL_PIXELFORMAT_RGBX32:
sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR; sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR;
break; break;
} }
break; break;
case SDL_PIXELFORMAT_RGB888: case SDL_PIXELFORMAT_BGRX32:
switch (renderer->target->format) { switch (renderer->target->format) {
case SDL_PIXELFORMAT_ABGR8888: case SDL_PIXELFORMAT_RGBA32:
sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB; sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
break; break;
case SDL_PIXELFORMAT_ARGB8888: case SDL_PIXELFORMAT_BGRA32:
sourceType = GLES2_IMAGESOURCE_TEXTURE_BGR; sourceType = GLES2_IMAGESOURCE_TEXTURE_BGR;
break; break;
case SDL_PIXELFORMAT_BGR888: case SDL_PIXELFORMAT_RGBX32:
sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB; sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
break; break;
} }
break; break;
case SDL_PIXELFORMAT_BGR888: case SDL_PIXELFORMAT_RGBX32:
switch (renderer->target->format) { switch (renderer->target->format) {
case SDL_PIXELFORMAT_ABGR8888: case SDL_PIXELFORMAT_RGBA32:
sourceType = GLES2_IMAGESOURCE_TEXTURE_BGR; sourceType = GLES2_IMAGESOURCE_TEXTURE_BGR;
break; break;
case SDL_PIXELFORMAT_ARGB8888: case SDL_PIXELFORMAT_BGRA32:
sourceType = GLES2_IMAGESOURCE_TEXTURE_RGB; sourceType = GLES2_IMAGESOURCE_TEXTURE_RGB;
break; break;
case SDL_PIXELFORMAT_RGB888: case SDL_PIXELFORMAT_BGRX32:
sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB; sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
break; break;
} }
@ -1100,16 +1100,16 @@ static int SetCopyState(SDL_Renderer *renderer, const SDL_RenderCommand *cmd, vo
} }
} else { } else {
switch (texture->format) { switch (texture->format) {
case SDL_PIXELFORMAT_ARGB8888: case SDL_PIXELFORMAT_BGRA32:
sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB; sourceType = GLES2_IMAGESOURCE_TEXTURE_ARGB;
break; break;
case SDL_PIXELFORMAT_ABGR8888: case SDL_PIXELFORMAT_RGBA32:
sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR; sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR;
break; break;
case SDL_PIXELFORMAT_RGB888: case SDL_PIXELFORMAT_BGRX32:
sourceType = GLES2_IMAGESOURCE_TEXTURE_RGB; sourceType = GLES2_IMAGESOURCE_TEXTURE_RGB;
break; break;
case SDL_PIXELFORMAT_BGR888: case SDL_PIXELFORMAT_RGBX32:
sourceType = GLES2_IMAGESOURCE_TEXTURE_BGR; sourceType = GLES2_IMAGESOURCE_TEXTURE_BGR;
break; break;
#if SDL_HAVE_YUV #if SDL_HAVE_YUV
@ -1162,7 +1162,7 @@ static int SetCopyState(SDL_Renderer *renderer, const SDL_RenderCommand *cmd, vo
static int GLES2_RunCommandQueue(SDL_Renderer *renderer, SDL_RenderCommand *cmd, void *vertices, size_t vertsize) static int GLES2_RunCommandQueue(SDL_Renderer *renderer, SDL_RenderCommand *cmd, void *vertices, size_t vertsize)
{ {
GLES2_RenderData *data = (GLES2_RenderData *)renderer->driverdata; GLES2_RenderData *data = (GLES2_RenderData *)renderer->driverdata;
const SDL_bool colorswap = (renderer->target && (renderer->target->format == SDL_PIXELFORMAT_ARGB8888 || renderer->target->format == SDL_PIXELFORMAT_RGB888)); const SDL_bool colorswap = (renderer->target && (renderer->target->format == SDL_PIXELFORMAT_BGRA32 || renderer->target->format == SDL_PIXELFORMAT_BGRX32));
#if USE_VERTEX_BUFFER_OBJECTS #if USE_VERTEX_BUFFER_OBJECTS
const int vboidx = data->current_vertex_buffer; const int vboidx = data->current_vertex_buffer;
@ -1422,10 +1422,10 @@ static int GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture)
/* Determine the corresponding GLES texture format params */ /* Determine the corresponding GLES texture format params */
switch (texture->format) { switch (texture->format) {
case SDL_PIXELFORMAT_ARGB8888: case SDL_PIXELFORMAT_BGRA32:
case SDL_PIXELFORMAT_ABGR8888: case SDL_PIXELFORMAT_RGBA32:
case SDL_PIXELFORMAT_RGB888: case SDL_PIXELFORMAT_BGRX32:
case SDL_PIXELFORMAT_BGR888: case SDL_PIXELFORMAT_RGBX32:
format = GL_RGBA; format = GL_RGBA;
type = GL_UNSIGNED_BYTE; type = GL_UNSIGNED_BYTE;
break; break;
@ -1574,9 +1574,6 @@ static int GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture)
static int GLES2_TexSubImage2D(GLES2_RenderData *data, GLenum target, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels, GLint pitch, GLint bpp) static int GLES2_TexSubImage2D(GLES2_RenderData *data, GLenum target, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels, GLint pitch, GLint bpp)
{ {
Uint8 *blob = NULL; Uint8 *blob = NULL;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
Uint32 *blob2 = NULL;
#endif
Uint8 *src; Uint8 *src;
size_t src_pitch; size_t src_pitch;
int y; int y;
@ -1602,33 +1599,10 @@ static int GLES2_TexSubImage2D(GLES2_RenderData *data, GLenum target, GLint xoff
src = blob; src = blob;
} }
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
if (format == GL_RGBA) {
int i;
Uint32 *src32 = (Uint32 *)src;
blob2 = (Uint32 *)SDL_malloc(src_pitch * height);
if (!blob2) {
if (blob) {
SDL_free(blob);
}
return SDL_OutOfMemory();
}
for (i = 0; i < (src_pitch * height) / 4; i++) {
blob2[i] = SDL_Swap32(src32[i]);
}
src = (Uint8 *)blob2;
}
#endif
data->glTexSubImage2D(target, 0, xoffset, yoffset, width, height, format, type, src); data->glTexSubImage2D(target, 0, xoffset, yoffset, width, height, format, type, src);
if (blob) { if (blob) {
SDL_free(blob); SDL_free(blob);
} }
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
if (blob2) {
SDL_free(blob2);
}
#endif
return 0; return 0;
} }
@ -1919,7 +1893,7 @@ static int GLES2_RenderReadPixels(SDL_Renderer *renderer, const SDL_Rect *rect,
Uint32 pixel_format, void *pixels, int pitch) Uint32 pixel_format, void *pixels, int pitch)
{ {
GLES2_RenderData *data = (GLES2_RenderData *)renderer->driverdata; GLES2_RenderData *data = (GLES2_RenderData *)renderer->driverdata;
Uint32 temp_format = renderer->target ? renderer->target->format : SDL_PIXELFORMAT_ABGR8888; Uint32 temp_format = renderer->target ? renderer->target->format : SDL_PIXELFORMAT_RGBA32;
size_t buflen; size_t buflen;
void *temp_pixels; void *temp_pixels;
int temp_pitch; int temp_pitch;
@ -2262,10 +2236,10 @@ SDL_RenderDriver GLES2_RenderDriver = {
{ "opengles2", { "opengles2",
(SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE), (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE),
4, 4,
{ SDL_PIXELFORMAT_ARGB8888, { SDL_PIXELFORMAT_RGBA32,
SDL_PIXELFORMAT_ABGR8888, SDL_PIXELFORMAT_BGRA32,
SDL_PIXELFORMAT_RGB888, SDL_PIXELFORMAT_BGRX32,
SDL_PIXELFORMAT_BGR888 }, SDL_PIXELFORMAT_RGBX32 },
0, 0,
0 } 0 }
}; };