Commit 2ee9b1ddce5a44e12db57fce5bc85d3ae92c98cd

Sam Lantinga 2019-05-19T11:01:36

Fixed bug 4025 - SDL_Renderer OpenGL : add support for textures ABGR, RGB, BGR Sylvain OpenGLES2 SDL renderer has support for textures ARGB, ABGR, RGB and BGR, whereas OpenGL SDL renderer only had ARGB. If you think it's worth adding it, here's a patch. I quickly tried and it worked, but there may be missing things or corner case.

diff --git a/src/render/opengl/SDL_render_gl.c b/src/render/opengl/SDL_render_gl.c
index a32ac6e..6c09e16 100644
--- a/src/render/opengl/SDL_render_gl.c
+++ b/src/render/opengl/SDL_render_gl.c
@@ -405,10 +405,17 @@ convert_format(GL_RenderData *renderdata, Uint32 pixel_format,
 {
     switch (pixel_format) {
     case SDL_PIXELFORMAT_ARGB8888:
+    case SDL_PIXELFORMAT_RGB888:
         *internalFormat = GL_RGBA8;
         *format = GL_BGRA;
         *type = GL_UNSIGNED_INT_8_8_8_8_REV;
         break;
+    case SDL_PIXELFORMAT_ABGR8888:
+    case SDL_PIXELFORMAT_BGR888:
+        *internalFormat = GL_RGBA8;
+        *format = GL_RGBA;
+        *type = GL_UNSIGNED_INT_8_8_8_8_REV;
+        break;
     case SDL_PIXELFORMAT_YV12:
     case SDL_PIXELFORMAT_IYUV:
     case SDL_PIXELFORMAT_NV12:
@@ -1019,7 +1026,13 @@ SetCopyState(GL_RenderData *data, const SDL_RenderCommand *cmd)
 {
     SDL_Texture *texture = cmd->data.draw.texture;
     const GL_TextureData *texturedata = (GL_TextureData *) texture->driverdata;
-    GL_Shader shader = SHADER_RGB;
+    GL_Shader shader;
+
+    if (texture->format == SDL_PIXELFORMAT_ABGR8888 || texture->format == SDL_PIXELFORMAT_ARGB8888) {
+        shader = SHADER_RGBA;
+    } else {
+        shader = SHADER_RGB;
+    }
 
     if (data->shaders) {
         if (texturedata->yuv || texturedata->nv12) {
@@ -1733,8 +1746,13 @@ SDL_RenderDriver GL_RenderDriver = {
     {
      "opengl",
      (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE),
-     1,
-     {SDL_PIXELFORMAT_ARGB8888},
+     4,
+     {
+         SDL_PIXELFORMAT_ARGB8888,
+         SDL_PIXELFORMAT_ABGR8888,
+         SDL_PIXELFORMAT_RGB888,
+         SDL_PIXELFORMAT_BGR888
+     },
      0,
      0}
 };
diff --git a/src/render/opengl/SDL_shaders_gl.c b/src/render/opengl/SDL_shaders_gl.c
index 7572191..c325973 100644
--- a/src/render/opengl/SDL_shaders_gl.c
+++ b/src/render/opengl/SDL_shaders_gl.c
@@ -240,6 +240,23 @@ static const char *shader_source[NUM_SHADERS][2] =
 "\n"
 "void main()\n"
 "{\n"
+"    gl_FragColor = texture2D(tex0, v_texCoord);\n"
+"    gl_FragColor.a = 1.0;\n"
+"    gl_FragColor *= v_color;\n"
+"}"
+    },
+
+    /* SHADER_RGBA */
+    {
+        /* vertex shader */
+        TEXTURE_VERTEX_SHADER,
+        /* fragment shader */
+"varying vec4 v_color;\n"
+"varying vec2 v_texCoord;\n"
+"uniform sampler2D tex0;\n"
+"\n"
+"void main()\n"
+"{\n"
 "    gl_FragColor = texture2D(tex0, v_texCoord) * v_color;\n"
 "}"
     },
diff --git a/src/render/opengl/SDL_shaders_gl.h b/src/render/opengl/SDL_shaders_gl.h
index 3638b44..1917e26 100644
--- a/src/render/opengl/SDL_shaders_gl.h
+++ b/src/render/opengl/SDL_shaders_gl.h
@@ -31,6 +31,7 @@ typedef enum {
     SHADER_NONE,
     SHADER_SOLID,
     SHADER_RGB,
+    SHADER_RGBA,
     SHADER_YUV_JPEG,
     SHADER_YUV_BT601,
     SHADER_YUV_BT709,