diff mbox series

SDL: OpenGL 3 window context

Message ID 20230601140019.66013-1-quic_acaggian@quicinc.com (mailing list archive)
State New, archived
Headers show
Series SDL: OpenGL 3 window context | expand

Commit Message

Antonio Caggiano June 1, 2023, 2 p.m. UTC
SDL renderer creates an OpenGL 2.1 context while QEMU expects minimum
OpenGL version 3.3 or ES 3.0. To fix this we create an OpenGL context
directly, ignoring the SDL renderer when OpenGL is enabled.

Signed-off-by: Antonio Caggiano <quic_acaggian@quicinc.com>
---
 ui/sdl2.c | 34 ++++++++++++++++++++++++++--------
 1 file changed, 26 insertions(+), 8 deletions(-)

Comments

Marc-André Lureau June 1, 2023, 3:47 p.m. UTC | #1
Hi

On Thu, Jun 1, 2023 at 6:30 PM Antonio Caggiano <quic_acaggian@quicinc.com>
wrote:

> SDL renderer creates an OpenGL 2.1 context while QEMU expects minimum
> OpenGL version 3.3 or ES 3.0. To fix this we create an OpenGL context
> directly, ignoring the SDL renderer when OpenGL is enabled.
>
> Signed-off-by: Antonio Caggiano <quic_acaggian@quicinc.com>
> ---
>  ui/sdl2.c | 34 ++++++++++++++++++++++++++--------
>  1 file changed, 26 insertions(+), 8 deletions(-)
>
> diff --git a/ui/sdl2.c b/ui/sdl2.c
> index 9d703200bf..42512588b5 100644
> --- a/ui/sdl2.c
> +++ b/ui/sdl2.c
> @@ -104,7 +104,24 @@ void sdl2_window_create(struct sdl2_console *scon)
>                                           surface_width(scon->surface),
>                                           surface_height(scon->surface),
>                                           flags);
> +
>      if (scon->opengl) {
> +        /* Set the minimum version required by the texture blit shaders */
> +        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
> +        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
> +
> +        if (scon->opts->gl == DISPLAYGL_MODE_ES) {
> +            SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
> +                                SDL_GL_CONTEXT_PROFILE_ES);
> +            SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
> +        }
> +
> +        scon->winctx = SDL_GL_CreateContext(scon->real_window);
> +    } else {
> +        /*
> +         * The SDL renderer is only used by sdl2 2D callbacks, when
> OpenGL is
> +         * disabled at configuration time
> +         */
>

"configuration" time usually means during compilation configuration. Here I
guess you also mean during runtime configuration.

I suggest you simply drop "at configuation time", as it is confusing imho.



>          const char *driver = "opengl";
>
>          if (scon->opts->gl == DISPLAYGL_MODE_ES) {
> @@ -113,11 +130,8 @@ void sdl2_window_create(struct sdl2_console *scon)
>
>          SDL_SetHint(SDL_HINT_RENDER_DRIVER, driver);
>          SDL_SetHint(SDL_HINT_RENDER_BATCHING, "1");
> -    }
> -    scon->real_renderer = SDL_CreateRenderer(scon->real_window, -1, 0);
>
> -    if (scon->opengl) {
> -        scon->winctx = SDL_GL_CreateContext(scon->real_window);
> +        scon->real_renderer = SDL_CreateRenderer(scon->real_window, -1,
> 0);
>      }
>      sdl_update_caption(scon);
>  }
> @@ -128,10 +142,14 @@ void sdl2_window_destroy(struct sdl2_console *scon)
>          return;
>      }
>
> -    SDL_GL_DeleteContext(scon->winctx);
> -    scon->winctx = NULL;
> -    SDL_DestroyRenderer(scon->real_renderer);
> -    scon->real_renderer = NULL;
> +    if (scon->winctx) {
> +        SDL_GL_DeleteContext(scon->winctx);
> +        scon->winctx = NULL;
> +    }
> +    if (scon->real_renderer) {
> +        SDL_DestroyRenderer(scon->real_renderer);
> +        scon->real_renderer = NULL;
> +    }
>      SDL_DestroyWindow(scon->real_window);
>      scon->real_window = NULL;
>  }
> --
> 2.40.0
>
>
Have you checked the behaviour on Windows?

thanks
diff mbox series

Patch

diff --git a/ui/sdl2.c b/ui/sdl2.c
index 9d703200bf..42512588b5 100644
--- a/ui/sdl2.c
+++ b/ui/sdl2.c
@@ -104,7 +104,24 @@  void sdl2_window_create(struct sdl2_console *scon)
                                          surface_width(scon->surface),
                                          surface_height(scon->surface),
                                          flags);
+
     if (scon->opengl) {
+        /* Set the minimum version required by the texture blit shaders */
+        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
+        SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
+
+        if (scon->opts->gl == DISPLAYGL_MODE_ES) {
+            SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
+                                SDL_GL_CONTEXT_PROFILE_ES);
+            SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
+        }
+
+        scon->winctx = SDL_GL_CreateContext(scon->real_window);
+    } else {
+        /*
+         * The SDL renderer is only used by sdl2 2D callbacks, when OpenGL is
+         * disabled at configuration time
+         */
         const char *driver = "opengl";
 
         if (scon->opts->gl == DISPLAYGL_MODE_ES) {
@@ -113,11 +130,8 @@  void sdl2_window_create(struct sdl2_console *scon)
 
         SDL_SetHint(SDL_HINT_RENDER_DRIVER, driver);
         SDL_SetHint(SDL_HINT_RENDER_BATCHING, "1");
-    }
-    scon->real_renderer = SDL_CreateRenderer(scon->real_window, -1, 0);
 
-    if (scon->opengl) {
-        scon->winctx = SDL_GL_CreateContext(scon->real_window);
+        scon->real_renderer = SDL_CreateRenderer(scon->real_window, -1, 0);
     }
     sdl_update_caption(scon);
 }
@@ -128,10 +142,14 @@  void sdl2_window_destroy(struct sdl2_console *scon)
         return;
     }
 
-    SDL_GL_DeleteContext(scon->winctx);
-    scon->winctx = NULL;
-    SDL_DestroyRenderer(scon->real_renderer);
-    scon->real_renderer = NULL;
+    if (scon->winctx) {
+        SDL_GL_DeleteContext(scon->winctx);
+        scon->winctx = NULL;
+    }
+    if (scon->real_renderer) {
+        SDL_DestroyRenderer(scon->real_renderer);
+        scon->real_renderer = NULL;
+    }
     SDL_DestroyWindow(scon->real_window);
     scon->real_window = NULL;
 }