Skip to contentSkip to conversation

sdl2: add opengl rendering support

PATCH In review Gerd Hoffmann 2 participants 20 messages Jan 19, 2015 – Jan 20, 2015 Kbuild

lore
Share & tools
lore
#0 lore raw

v2 cover sdl2: add opengl rendering support

Hi,

Next round of the opengl rendering patches. Rendering code got some major changes, it using opengl es now. So there is support for compiling shaders etc now, all the stuff we need when moving away from the old opengl1 desktop rendering model, toward opengl es (and also opengl core 3/4).

Using gles2 because:

(1) I want move to the modern, programmable opengl rendering pipeline, even if that means my opengl knowledge needs a major update [ book ordered ;) ]
(2) The stripped down es (embedded systems) API runs on the desktop too, and it should have no problems handling our opengl needs.
(3) Maybe we'll go move to embedded devices some day (android tablets being the most likely thing here).

please review,
Gerd

Gerd Hoffmann (6):
configure: opengl overhaul
pixman: add a bunch of PIXMAN_BE_* defines for 32bpp
console-gl: add opengl rendering helper functions
console-gl: externalize shader programs
sdl2: move SDL_* includes to sdl2.h
sdl2: add support for display rendering using opengl.

Jeremy White (1):
Allow the use of X11 from a non standard location.

 Makefile                         |  17 +++
 configure                        |  59 +++++----
 default-configs/lm32-softmmu.mak |   2 +-
 hw/display/Makefile.objs         |   3 +-
 hw/lm32/milkymist-hw.h           |   4 +-
 include/sysemu/sysemu.h          |   1 +
 include/ui/console.h             |  32 +++++
 include/ui/qemu-pixman.h         |  16 +++
 include/ui/sdl2.h                |  17 +++
 scripts/shaderinclude.pl         |  16 +++
 ui/Makefile.objs                 |   8 ++
 ui/console-gl.c                  | 262 +++++++++++++++++++++++++++++++++++++++
 ui/sdl.c                         |  11 ++
 ui/sdl2-2d.c                     |  13 +-
 ui/sdl2-gl.c                     | 112 +++++++++++++++++
 ui/sdl2-input.c                  |   6 -
 ui/sdl2.c                        |  73 +++++++++--
 ui/shader/texture-blit.frag      |  10 ++
 ui/shader/texture-blit.vert      |  11 ++
 vl.c                             |  12 ++
 20 files changed, 631 insertions(+), 54 deletions(-)
 create mode 100644 scripts/shaderinclude.pl
 create mode 100644 ui/console-gl.c
 create mode 100644 ui/sdl2-gl.c
 create mode 100644 ui/shader/texture-blit.frag
 create mode 100644 ui/shader/texture-blit.vert
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v2 1/7 configure: opengl overhaul

Rename config option from "glx" to "opengl", glx will not be the only option for opengl in near future. Also switch over to pkg-config for opengl support detection.

---
 configure                        | 39 +++++++++++++++++----------------------
 default-configs/lm32-softmmu.mak |  2 +-
 hw/display/Makefile.objs         |  2 +-
 hw/lm32/milkymist-hw.h           |  4 ++--
 include/sysemu/sysemu.h          |  1 +
 vl.c                             |  1 +
 6 files changed, 23 insertions(+), 26 deletions(-)
configure +17 -22
index 7539645..8216410 100755
@@ -309,7 +309,7 @@ rbd=""
 smartcard_nss=""
 libusb=""
 usb_redir=""
-glx=""
+opengl=""
 zlib="yes"
 lzo=""
 snappy=""
@@ -1026,9 +1026,9 @@ for opt do
   ;;
   --enable-vhost-scsi) vhost_scsi="yes"
   ;;
-  --disable-glx) glx="no"
+  --disable-opengl) opengl="no"
   ;;
-  --enable-glx) glx="yes"
+  --enable-opengl) opengl="yes"
   ;;
   --disable-rbd) rbd="no"
   ;;
@@ -3056,23 +3056,18 @@ fi
 libs_softmmu="$libs_softmmu $fdt_libs"
 
 ##########################################
-# GLX probe, used by milkymist-tmu2
-if test "$glx" != "no" ; then
-  glx_libs="-lGL -lX11"
-  cat > $TMPC << EOF
-#include <X11/Xlib.h>
-#include <GL/gl.h>
-#include <GL/glx.h>
-int main(void) { glBegin(0); glXQueryVersion(0,0,0); return 0; }
-EOF
-  if compile_prog "" "-lGL -lX11" ; then
-    glx=yes
+# opengl probe (for sdl2, milkymist-tmu2)
+if test "$opengl" != "no" ; then
+  opengl_pkgs="gl"
+  if $pkg_config $opengl_pkgs; then
+    opengl_libs="$($pkg_config --libs $opengl_pkgs) -lX11"
+    opengl=yes
   else
-    if test "$glx" = "yes" ; then
-      feature_not_found "glx" "Install GL devel (e.g. MESA)"
+    if test "$opengl" = "yes" ; then
+      feature_not_found "opengl" "Install GL devel (e.g. MESA)"
     fi
-    glx_libs=
-    glx=no
+    opengl_libs=""
+    opengl=no
   fi
 fi
 
@@ -4320,7 +4315,7 @@ echo "xfsctl support    $xfs"
 echo "nss used          $smartcard_nss"
 echo "libusb            $libusb"
 echo "usb net redir     $usb_redir"
-echo "GLX support       $glx"
+echo "OpenGL support    $opengl"
 echo "libiscsi support  $libiscsi"
 echo "libnfs support    $libnfs"
 echo "build guest agent $guest_agent"
@@ -4682,9 +4677,9 @@ if test "$usb_redir" = "yes" ; then
   echo "CONFIG_USB_REDIR=y" >> $config_host_mak
 fi
 
-if test "$glx" = "yes" ; then
-  echo "CONFIG_GLX=y" >> $config_host_mak
-  echo "GLX_LIBS=$glx_libs" >> $config_host_mak
+if test "$opengl" = "yes" ; then
+  echo "CONFIG_OPENGL=y" >> $config_host_mak
+  echo "OPENGL_LIBS=$opengl_libs" >> $config_host_mak
 fi
 
 if test "$lzo" = "yes" ; then
default-configs/lm32-softmmu.mak +1 -1
index 7df58c8..4889348 100644
@@ -2,7 +2,7 @@
 
 CONFIG_LM32=y
 CONFIG_MILKYMIST=y
-CONFIG_MILKYMIST_TMU2=$(CONFIG_GLX)
+CONFIG_MILKYMIST_TMU2=$(CONFIG_OPENGL)
 CONFIG_FRAMEBUFFER=y
 CONFIG_PTIMER=y
 CONFIG_PFLASH_CFI01=y
hw/display/Makefile.objs +1 -1
index 7ed76a9..e18ea57 100644
@@ -20,7 +20,7 @@ common-obj-$(CONFIG_ZAURUS) += tc6393xb.o
 
 ifeq ($(CONFIG_MILKYMIST_TMU2),y)
 common-obj-y += milkymist-tmu2.o
-libs_softmmu += $(GLX_LIBS)
+libs_softmmu += $(OPENGL_LIBS)
 endif
 
 obj-$(CONFIG_OMAP) += omap_dss.o
hw/lm32/milkymist-hw.h +2 -2
index 5317ce6..8d20cac 100644
@@ -86,7 +86,7 @@ static inline DeviceState *milkymist_pfpu_create(hwaddr base,
     return dev;
 }
 
-#ifdef CONFIG_GLX
+#ifdef CONFIG_OPENGL
 #include <X11/Xlib.h>
 #include <GL/glx.h>
 static const int glx_fbconfig_attr[] = {
@@ -100,7 +100,7 @@ static const int glx_fbconfig_attr[] = {
 static inline DeviceState *milkymist_tmu2_create(hwaddr base,
         qemu_irq irq)
 {
-#ifdef CONFIG_GLX
+#ifdef CONFIG_OPENGL
     DeviceState *dev;
     Display *d;
     GLXFBConfig *configs;
include/sysemu/sysemu.h +1 -0
index 748d059..310078c 100644
@@ -117,6 +117,7 @@ extern int graphic_width;
 extern int graphic_height;
 extern int graphic_depth;
 extern DisplayType display_type;
+extern int display_opengl;
 extern const char *keyboard_layout;
 extern int win2k_install_hack;
 extern int alt_grab;
vl.c +1 -0
index fbf4240..0bf2acb 100644
@@ -129,6 +129,7 @@ static int data_dir_idx;
 const char *bios_name = NULL;
 enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
 DisplayType display_type = DT_DEFAULT;
+int display_opengl;
 static int display_remote;
 const char* keyboard_layout = NULL;
 ram_addr_t ram_size;
Signature · 1 line
1.8.3.1
Reviewed-by on v2 1/7
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/7] configure: opengl overhaul

Quoted 5 lines · Gerd Hoffmann

Rename config option from "glx" to "opengl", glx will not be the only option for opengl in near future. Also switch over to pkg-config for opengl support detection.

Quoted 8 lines
---
  configure                        | 39 +++++++++++++++++----------------------
  default-configs/lm32-softmmu.mak |  2 +-
  hw/display/Makefile.objs         |  2 +-
  hw/lm32/milkymist-hw.h           |  4 ++--
  include/sysemu/sysemu.h          |  1 +
  vl.c                             |  1 +
  6 files changed, 23 insertions(+), 26 deletions(-)
#3 lore raw

PATCH v2 2/7 Allow the use of X11 from a non standard location.

In reply to Gerd Hoffmann

From: Jeremy White <[email protected]>

[ kraxel: solve opengl patch conflicts ]

---
 configure                | 24 +++++++++++++++++++-----
 hw/display/Makefile.objs |  1 +
 2 files changed, 20 insertions(+), 5 deletions(-)
configure +19 -5
index 8216410..6d673c1 100755
@@ -2034,6 +2034,15 @@ if test "$sparse" != "no" ; then
 fi
 
 ##########################################
+# X11 probe
+x11_cflags=
+x11_libs=-lX11
+if $pkg_config --exists "x11"; then
+    x11_cflags=`$pkg_config --cflags x11`
+    x11_libs=`$pkg_config --libs x11`
+fi
+
+##########################################
 # GTK probe
 
 if test "$gtkabi" = ""; then
@@ -2060,7 +2069,8 @@ if test "$gtk" != "no"; then
         gtk_cflags=`$pkg_config --cflags $gtkpackage`
         gtk_libs=`$pkg_config --libs $gtkpackage`
         if $pkg_config --exists "$gtkx11package >= $gtkversion"; then
-            gtk_libs="$gtk_libs -lX11"
+            gtk_cflags="$gtk_cflags $x11_cflags"
+            gtk_libs="$gtk_libs $x11_libs"
         fi
         libs_softmmu="$gtk_libs $libs_softmmu"
         gtk="yes"
@@ -2185,8 +2195,9 @@ if test "$sdl" = "yes" ; then
 #endif
 int main(void) { return 0; }
 EOF
-  if compile_prog "$sdl_cflags" "$sdl_libs" ; then
-    sdl_libs="$sdl_libs -lX11"
+  if compile_prog "$sdl_cflags $x11_cflags" "$sdl_libs $x11_libs" ; then
+    sdl_cflags="$sdl_cflags $x11_cflags"
+    sdl_libs="$sdl_libs $x11_libs"
   fi
   libs_softmmu="$sdl_libs $libs_softmmu"
 fi
@@ -3059,13 +3070,15 @@ libs_softmmu="$libs_softmmu $fdt_libs"
 # opengl probe (for sdl2, milkymist-tmu2)
 if test "$opengl" != "no" ; then
   opengl_pkgs="gl"
-  if $pkg_config $opengl_pkgs; then
-    opengl_libs="$($pkg_config --libs $opengl_pkgs) -lX11"
+  if $pkg_config $opengl_pkgs x11; then
+    opengl_cflags="$($pkg_config --libs $opengl_pkgs) $x11_cflags"
+    opengl_libs="$($pkg_config --libs $opengl_pkgs) $x11_libs"
     opengl=yes
   else
     if test "$opengl" = "yes" ; then
       feature_not_found "opengl" "Install GL devel (e.g. MESA)"
     fi
+    opengl_cflags=""
     opengl_libs=""
     opengl=no
   fi
@@ -4679,6 +4692,7 @@ fi
 
 if test "$opengl" = "yes" ; then
   echo "CONFIG_OPENGL=y" >> $config_host_mak
+  echo "OPENGL_CFLAGS=$opengl_cflags" >> $config_host_mak
   echo "OPENGL_LIBS=$opengl_libs" >> $config_host_mak
 fi
 
hw/display/Makefile.objs +1 -0
index e18ea57..e73cb7d 100644
@@ -20,6 +20,7 @@ common-obj-$(CONFIG_ZAURUS) += tc6393xb.o
 
 ifeq ($(CONFIG_MILKYMIST_TMU2),y)
 common-obj-y += milkymist-tmu2.o
+milkymist-tmu2.o-cflags := $(OPENGL_CFLAGS)
 libs_softmmu += $(OPENGL_LIBS)
 endif
 
Signature · 1 line
1.8.3.1
Reviewed-by on v2 2/7
#4 lore raw

Re: [Qemu-devel] [PATCH v2 2/7] Allow the use of X11 from a non standard location.

Quoted 11 lines · Gerd Hoffmann

From: Jeremy White <[email protected]>

[ kraxel: solve opengl patch conflicts ]

---
  configure                | 24 +++++++++++++++++++-----
  hw/display/Makefile.objs |  1 +
  2 files changed, 20 insertions(+), 5 deletions(-)
#5 lore raw

PATCH v2 3/7 pixman: add a bunch of PIXMAN_BE_* defines for 32bpp

In reply to Gerd Hoffmann
---
 include/ui/qemu-pixman.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
include/ui/qemu-pixman.h +16 -0
index 381969d..37f9bc2 100644
@@ -27,8 +27,24 @@
 
 #ifdef HOST_WORDS_BIGENDIAN
 # define PIXMAN_BE_r8g8b8     PIXMAN_r8g8b8
+# define PIXMAN_BE_x8r8g8b8   PIXMAN_x8r8g8b8
+# define PIXMAN_BE_a8r8g8b8   PIXMAN_a8r8g8b8
+# define PIXMAN_BE_b8g8r8x8   PIXMAN_b8g8r8x8
+# define PIXMAN_BE_b8g8r8a8   PIXMAN_b8g8r8a8
+# define PIXMAN_BE_r8g8b8x8   PIXMAN_r8g8b8x8
+# define PIXMAN_BE_r8g8b8a8   PIXMAN_r8g8b8a8
+# define PIXMAN_BE_x8b8g8r8   PIXMAN_x8b8g8r8
+# define PIXMAN_BE_a8b8g8r8   PIXMAN_a8b8g8r8
 #else
 # define PIXMAN_BE_r8g8b8     PIXMAN_b8g8r8
+# define PIXMAN_BE_x8r8g8b8   PIXMAN_b8g8r8x8
+# define PIXMAN_BE_a8r8g8b8   PIXMAN_b8g8r8a8
+# define PIXMAN_BE_b8g8r8x8   PIXMAN_x8r8g8b8
+# define PIXMAN_BE_b8g8r8a8   PIXMAN_a8r8g8b8
+# define PIXMAN_BE_r8g8b8x8   PIXMAN_x8b8g8r8
+# define PIXMAN_BE_r8g8b8a8   PIXMAN_a8b8g8r8
+# define PIXMAN_BE_x8b8g8r8   PIXMAN_r8g8b8x8
+# define PIXMAN_BE_a8b8g8r8   PIXMAN_r8g8b8a8
 #endif
 
 /* -------------------------------------------------------------------- */
Signature · 1 line
1.8.3.1
#7 lore raw

PATCH v2 4/7 console-gl: add opengl rendering helper functions

In reply to Gerd Hoffmann
---
 configure            |   2 +-
 include/ui/console.h |  31 ++++++
 ui/Makefile.objs     |   5 +
 ui/console-gl.c      | 286 +++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 323 insertions(+), 1 deletion(-)
 create mode 100644 ui/console-gl.c
configure +1 -1
index 6d673c1..e6b29a6 100755
@@ -3069,7 +3069,7 @@ libs_softmmu="$libs_softmmu $fdt_libs"
 ##########################################
 # opengl probe (for sdl2, milkymist-tmu2)
 if test "$opengl" != "no" ; then
-  opengl_pkgs="gl"
+  opengl_pkgs="gl glesv2"
   if $pkg_config $opengl_pkgs x11; then
     opengl_cflags="$($pkg_config --libs $opengl_pkgs) $x11_cflags"
     opengl_libs="$($pkg_config --libs $opengl_pkgs) $x11_libs"
include/ui/console.h +31 -0
index 22ef8ca..9157b64 100644
@@ -9,6 +9,11 @@
 #include "qapi-types.h"
 #include "qapi/error.h"
 
+#ifdef CONFIG_OPENGL
+# include <GLES2/gl2.h>
+# include <GLES2/gl2ext.h>
+#endif
+
 /* keyboard/mouse support */
 
 #define MOUSE_EVENT_LBUTTON 0x01
@@ -118,6 +123,11 @@ struct DisplaySurface {
     pixman_format_code_t format;
     pixman_image_t *image;
     uint8_t flags;
+#ifdef CONFIG_OPENGL
+    GLenum glformat;
+    GLenum gltype;
+    GLuint texture;
+#endif
 };
 
 typedef struct QemuUIInfo {
@@ -320,6 +330,27 @@ void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
 DisplaySurface *qemu_console_surface(QemuConsole *con);
 DisplayState *qemu_console_displaystate(QemuConsole *console);
 
+/* console-gl.c */
+typedef struct ConsoleGLState ConsoleGLState;
+#ifdef CONFIG_OPENGL
+ConsoleGLState *console_gl_init_context(void);
+void console_gl_fini_context(ConsoleGLState *gls);
+bool console_gl_check_format(DisplayChangeListener *dcl,
+                             pixman_format_code_t format);
+void surface_gl_create_texture(ConsoleGLState *gls,
+                               DisplaySurface *surface);
+void surface_gl_update_texture(ConsoleGLState *gls,
+                               DisplaySurface *surface,
+                               int x, int y, int w, int h);
+void surface_gl_render_texture(ConsoleGLState *gls,
+                               DisplaySurface *surface);
+void surface_gl_destroy_texture(ConsoleGLState *gls,
+                               DisplaySurface *surface);
+void surface_gl_setup_viewport(ConsoleGLState *gls,
+                               DisplaySurface *surface,
+                               int ww, int wh);
+#endif
+
 /* sdl.c */
 void sdl_display_init(DisplayState *ds, int full_screen, int no_frame);
 
ui/Makefile.objs +5 -0
index 13b5cfb..3173778 100644
@@ -24,4 +24,9 @@ sdl.mo-objs := sdl2.o sdl2-input.o sdl2-2d.o
 endif
 sdl.mo-cflags := $(SDL_CFLAGS)
 
+ifeq ($(CONFIG_OPENGL),y)
+common-obj-y += console-gl.o
+libs_softmmu += $(OPENGL_LIBS)
+endif
+
 gtk.o-cflags := $(GTK_CFLAGS) $(VTE_CFLAGS)
ui/console-gl.c new file +286 -0

286 lines not on this page · raw message

Signature · 1 line
1.8.3.1
Reviewed-by on v2 4/7
#8 lore raw

Re: [Qemu-devel] [PATCH v2 4/7] console-gl: add opengl rendering helper functions

Quoted 14 lines · Gerd Hoffmann
---
  configure            |   2 +-
  include/ui/console.h |  31 ++++++
  ui/Makefile.objs     |   5 +
  ui/console-gl.c      | 286 +++++++++++++++++++++++++++++++++++++++++++++++++++
  4 files changed, 323 insertions(+), 1 deletion(-)

create mode 100644 ui/console-gl.c

configure +0 -0
index 6d673c1..e6b29a6 100755

configure libs_softmmu="$libs_softmmu $fdt_libs" · line 3072

Quoted 5 lines
configure +1 -1
@@ -3069,7 +3069,7 @@ libs_softmmu="$libs_softmmu $fdt_libs"
  ##########################################
  # opengl probe (for sdl2, milkymist-tmu2)
  if test "$opengl" != "no" ; then
-  opengl_pkgs="gl"
+  opengl_pkgs="gl glesv2"

I don't know anything about GLES except for it being (as I've been told) mostly similar to the normal OpenGL. I hope that'll suffice…

Quoted 110 lines
configure +0 -0
    if $pkg_config $opengl_pkgs x11; then
      opengl_cflags="$($pkg_config --libs $opengl_pkgs) $x11_cflags"
      opengl_libs="$($pkg_config --libs $opengl_pkgs) $x11_libs"
include/ui/console.h +31 -0
index 22ef8ca..9157b64 100644
@@ -9,6 +9,11 @@
  #include "qapi-types.h"
  #include "qapi/error.h"
 
+#ifdef CONFIG_OPENGL
+# include <GLES2/gl2.h>
+# include <GLES2/gl2ext.h>
+#endif
+
  /* keyboard/mouse support */
 
  #define MOUSE_EVENT_LBUTTON 0x01
@@ -118,6 +123,11 @@ struct DisplaySurface {
      pixman_format_code_t format;
      pixman_image_t *image;
      uint8_t flags;
+#ifdef CONFIG_OPENGL
+    GLenum glformat;
+    GLenum gltype;
+    GLuint texture;
+#endif
  };
 
  typedef struct QemuUIInfo {
@@ -320,6 +330,27 @@ void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
  DisplaySurface *qemu_console_surface(QemuConsole *con);
  DisplayState *qemu_console_displaystate(QemuConsole *console);
 
+/* console-gl.c */
+typedef struct ConsoleGLState ConsoleGLState;
+#ifdef CONFIG_OPENGL
+ConsoleGLState *console_gl_init_context(void);
+void console_gl_fini_context(ConsoleGLState *gls);
+bool console_gl_check_format(DisplayChangeListener *dcl,
+                             pixman_format_code_t format);
+void surface_gl_create_texture(ConsoleGLState *gls,
+                               DisplaySurface *surface);
+void surface_gl_update_texture(ConsoleGLState *gls,
+                               DisplaySurface *surface,
+                               int x, int y, int w, int h);
+void surface_gl_render_texture(ConsoleGLState *gls,
+                               DisplaySurface *surface);
+void surface_gl_destroy_texture(ConsoleGLState *gls,
+                               DisplaySurface *surface);
+void surface_gl_setup_viewport(ConsoleGLState *gls,
+                               DisplaySurface *surface,
+                               int ww, int wh);
+#endif
+
  /* sdl.c */
  void sdl_display_init(DisplayState *ds, int full_screen, int no_frame);
 
ui/Makefile.objs +5 -0
index 13b5cfb..3173778 100644
@@ -24,4 +24,9 @@ sdl.mo-objs := sdl2.o sdl2-input.o sdl2-2d.o
  endif
  sdl.mo-cflags := $(SDL_CFLAGS)
 
+ifeq ($(CONFIG_OPENGL),y)
+common-obj-y += console-gl.o
+libs_softmmu += $(OPENGL_LIBS)
+endif
+
  gtk.o-cflags := $(GTK_CFLAGS) $(VTE_CFLAGS)
ui/console-gl.c new file +31 -0
new file mode 100644
index 0000000..589c682
@@ -0,0 +1,286 @@
+/*
+ * QEMU graphical console -- opengl helper bits
+ *
+ * Copyright (c) 2014 Red Hat
+ *
+ * Authors:
+ *    Gerd Hoffmann <[email protected]>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#include "qemu-common.h"
+#include "ui/console.h"
+
+struct ConsoleGLState {
+    GLint texture_blit_prog;

ui/console-gl.c line 41

Quoted 10 lines
ui/console-gl.c new file +10 -0
+};
+
+/* ---------------------------------------------------------------------- */
+
+static GLchar texture_blit_vert_src[] =
+    "\n"
+    "#version 300 es\n"
+    "\n"
+    "in vec2  in_position;\n"
+    "in vec2  in_tex_coord;\n"

You could calculate the texture coordinate from the position in the shader, but this is mostly my premature optimization instinct kicking in instead of a real performance difference considering how few vertices there are in this case.

ui/console-gl.c line 45

Quoted 4 lines
ui/console-gl.c new file +4 -0
+    "out vec2 ex_tex_coord;\n"
+    "\n"
+    "void main(void) {\n"
+    "    gl_Position = vec4(in_position.x, in_position.y, 0.0, 1.0);\n"

vec4(in_position, 0.0, 1.0) *cough*

Okay, perhaps it's OCD and not a premature optimization instinct.

Quoted 16 lines
ui/console-gl.c new file +16 -0
+    "    ex_tex_coord = in_tex_coord;\n"
+    "}\n"
+    "\n";
+
+static GLchar texture_blit_frag_src[] =
+    "\n"
+    "#version 300 es\n"
+    "\n"
+    "uniform sampler2D image;\n"
+    "in  highp vec2 ex_tex_coord;\n"
+    "out highp vec4 out_frag_color;\n"
+    "\n"
+    "void main(void) {\n"
+    "     out_frag_color = texture(image, ex_tex_coord);\n"
+    "}\n"
+    "\n";

ui/console-gl.c line 71

Quoted 10 lines
ui/console-gl.c new file +10 -0
+
+static void gl_run_texture_blit(ConsoleGLState *gls)
+{
+    GLfloat in_position[] = {
+        -1,  1,
+        -1, -1,
+        1,  -1,
+        -1,  1,
+        1,   1,
+        1,  -1,

This looks like a list for GL_TRIANGLES instead of GL_TRIANGLE_STRIP. For GL_TRIANGLE_STRIP, you first define one whole triangle (by specifying each of the three vertices) and after that, each vertex results in a new triangle drawn (whose two other vertices are the two vertices preceding the last one).

Therefore, for a quad, you only need four vertices in GL_TRIANGLE_STRIP mode. You will find that the following works just fine:

GLfloat in_position[] = {
    -1, -1,
     1, -1,
    -1,  1,
     1,  1,
};

(1)

ui/console-gl.c line 80

Quoted 9 lines
ui/console-gl.c new file +9 -0
+    };
+    GLfloat in_tex_coord[] = {
+        0, 0,
+        0, 1,
+        1, 1,
+        0, 0,
+        1, 0,
+        1, 1,
+    };

(1) and

GLfloat in_tex_coord[] = {
    0, 1,
    1, 1,
    0, 0,
    1, 0,
};

here.

ui/console-gl.c line 88

Quoted 8 lines
ui/console-gl.c new file +8 -0
+    GLint l_position;
+    GLint l_tex_coord;
+
+    glUseProgram(gls->texture_blit_prog);
+    l_position = glGetAttribLocation(gls->texture_blit_prog, "in_position");
+    l_tex_coord = glGetAttribLocation(gls->texture_blit_prog, "in_tex_coord");
+    glVertexAttribPointer(l_position, 2, GL_FLOAT, GL_FALSE, 0, in_position);
+    glVertexAttribPointer(l_tex_coord, 2, GL_FLOAT, GL_FALSE, 0, in_tex_coord);

I think it is disallowed to refer to non-OpenGL buffers here in the core profile. The 4.4 core specification states (section 10.3, vertex arrays): "Vertex data are placed into arrays that are stored in the server’s address space"; the 4.4 compatibility specification states: "Vertex data may also be placed into arrays that are stored in the client’s address space (described here) or in the server’s address space".

("client" refers to the main memory, "server" refers to the video memory, as far as I know)

As I said before, though, I am completely fine with going for the compatibility profile for now and making it core compliant later on.

ui/console-gl.c line 91

Quoted 3 lines
ui/console-gl.c new file +3 -0
+    glEnableVertexAttribArray(l_position);
+    glEnableVertexAttribArray(l_tex_coord);
+    glDrawArrays(GL_TRIANGLE_STRIP, l_position, 6);

(1) and of course s/6/4/ here.

Quoted 53 lines
ui/console-gl.c new file +53 -0
+}
+
+/* ---------------------------------------------------------------------- */
+
+static GLuint gl_create_compile_shader(GLenum type, const GLchar *src)
+{
+    GLuint shader;
+    GLint status, length;
+    char *errmsg;
+
+    shader = glCreateShader(type);
+    glShaderSource(shader, 1, &src, 0);
+    glCompileShader(shader);
+
+    glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
+    if (!status) {
+        glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
+        errmsg = malloc(length);
+        glGetShaderInfoLog(shader, length, &length, errmsg);
+        fprintf(stderr, "%s: compile %s error\n%s\n", __func__,
+                (type == GL_VERTEX_SHADER) ? "vertex" : "fragment",
+                errmsg);
+        free(errmsg);
+        return 0;
+    }
+    return shader;
+}
+
+static GLuint gl_create_link_program(GLuint vert, GLuint frag)
+{
+    GLuint program;
+    GLint status, length;
+    char *errmsg;
+
+    program = glCreateProgram();
+    glAttachShader(program, vert);
+    glAttachShader(program, frag);
+    glLinkProgram(program);
+
+    glGetProgramiv(program, GL_LINK_STATUS, &status);
+    if (!status) {
+        glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length);
+        errmsg = malloc(length);
+        glGetProgramInfoLog(program, length, &length, errmsg);
+        fprintf(stderr, "%s: link program: %s\n", __func__, errmsg);
+        free(errmsg);
+        return 0;
+    }
+    return program;
+}
+
+static GLuint gl_create_compile_link_program(const GLchar *vert_src,
+                                             const GLchar *frag_src)

ui/console-gl.c line 154

Quoted 10 lines
ui/console-gl.c new file +10 -0
+{
+    GLuint vert_shader, frag_shader;
+
+    vert_shader = gl_create_compile_shader(GL_VERTEX_SHADER, vert_src);
+    frag_shader = gl_create_compile_shader(GL_FRAGMENT_SHADER, frag_src);
+    if (!vert_shader || !frag_shader) {
+        return 0;
+    }
+
+    return gl_create_link_program(vert_shader, frag_shader);

Minor thing: You are free to delete the shaders after they have been linked into the program (because the references will be lost when this function returns).

Quoted 37 lines
ui/console-gl.c new file +37 -0
+}
+
+/* ---------------------------------------------------------------------- */
+
+ConsoleGLState *console_gl_init_context(void)
+{
+    ConsoleGLState *gls = g_new0(ConsoleGLState, 1);
+
+    gls->texture_blit_prog = gl_create_compile_link_program
+        (texture_blit_vert_src, texture_blit_frag_src);
+    if (!gls->texture_blit_prog) {
+        exit(1);
+    }
+
+    return gls;
+}
+
+void console_gl_fini_context(ConsoleGLState *gls)
+{
+    if (!gls) {
+        return;
+    }
+    g_free(gls);
+}
+
+bool console_gl_check_format(DisplayChangeListener *dcl,
+                             pixman_format_code_t format)
+{
+    switch (format) {
+    case PIXMAN_BE_b8g8r8x8:
+    case PIXMAN_BE_b8g8r8a8:
+    case PIXMAN_r5g6b5:
+        return true;
+    default:
+        return false;
+    }
+}

ui/console-gl.c line 201

Quoted 10 lines
ui/console-gl.c new file +10 -0
+
+void surface_gl_create_texture(ConsoleGLState *gls,
+                               DisplaySurface *surface)
+{
+    assert(gls);
+
+    switch (surface->format) {
+    case PIXMAN_BE_b8g8r8x8:
+    case PIXMAN_BE_b8g8r8a8:
+        surface->glformat = GL_BGRA_EXT;

If you want to avoid the _EXT, you could use GL_RGBA here and texture().bgra in the fragment shader. However, this would require a different shader for the 32 bit and the 16 bit formats (because the 16 bit format has the right order, apparently).

I'm voting for keeping GL_BGRA_EXT until someone complains.

ui/console-gl.c line 206

Quoted 5 lines
ui/console-gl.c new file +5 -0
+        surface->gltype = GL_UNSIGNED_BYTE;
+        break;
+    case PIXMAN_r5g6b5:
+        surface->glformat = GL_RGB;
+        surface->gltype = GL_UNSIGNED_SHORT_5_6_5;

I haven't been able to really test 16 bit mode (forcing 16 bit mode in hw/display/vga.c doesn't count...), so I'm trusting you this works.

Quoted 20 lines
ui/console-gl.c new file +20 -0
+        break;
+    default:
+        g_assert_not_reached();
+    }
+
+    glGenTextures(1, &surface->texture);
+    glEnable(GL_TEXTURE_2D);
+    glBindTexture(GL_TEXTURE_2D, surface->texture);
+    glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT,
+                  surface_stride(surface) / surface_bytes_per_pixel(surface));
+    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
+                 surface_width(surface),
+                 surface_height(surface),
+                 0, surface->glformat, surface->gltype,
+                 surface_data(surface));
+
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+}
+

ui/console-gl.c line 236

Quoted 10 lines
ui/console-gl.c new file +10 -0
+void surface_gl_update_texture(ConsoleGLState *gls,
+                               DisplaySurface *surface,
+                               int x, int y, int w, int h)
+{
+    uint8_t *data = (void *)surface_data(surface);
+
+    assert(gls);
+
+    glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT,
+                  surface_stride(surface) / surface_bytes_per_pixel(surface));

Maybe we should assert that surface_stride(surface) % surface_bytes_per_pixel(surface) == 0 here.

Quoted 40 lines
ui/console-gl.c new file +40 -0
+    glTexSubImage2D(GL_TEXTURE_2D, 0,
+                    x, y, w, h,
+                    surface->glformat, surface->gltype,
+                    data + surface_stride(surface) * y
+                    + surface_bytes_per_pixel(surface) * x);
+}
+
+void surface_gl_render_texture(ConsoleGLState *gls,
+                               DisplaySurface *surface)
+{
+    assert(gls);
+
+    glClearColor(0.1f, 0.1f, 0.1f, 0.0f);
+    glClear(GL_COLOR_BUFFER_BIT);
+
+    gl_run_texture_blit(gls);
+}
+
+void surface_gl_destroy_texture(ConsoleGLState *gls,
+                                DisplaySurface *surface)
+{
+    if (!surface || !surface->texture) {
+        return;
+    }
+    glDeleteTextures(1, &surface->texture);
+    surface->texture = 0;
+}
+
+void surface_gl_setup_viewport(ConsoleGLState *gls,
+                               DisplaySurface *surface,
+                               int ww, int wh)
+{
+    int gw, gh, stripe;
+    float sw, sh;
+
+    assert(gls);
+
+    gw = surface_width(surface);
+    gh = surface_height(surface);
+

ui/console-gl.c line 286

Quoted 10 lines
ui/console-gl.c new file +10 -0
+    sw = (float)ww/gw;
+    sh = (float)wh/gh;
+    if (sw < sh) {
+        stripe = wh - wh*sw/sh;
+        glViewport(0, stripe / 2, ww, wh - stripe);
+    } else {
+        stripe = ww - ww*sh/sw;
+        glViewport(stripe / 2, 0, ww - stripe, wh);
+    }
+}

With the vertex data changed to make use of GL_TRIANGLE_STRIP and with or without the stride assertion and with or without deleting the shaders after the program has been linked:

#9 lore raw

Re: [Qemu-devel] [PATCH v2 4/7] console-gl: add opengl rendering helper functions

Hi,

Quoted 11 lines

ui/console-gl.c line 41

Quoted 6 lines
ui/console-gl.c new file +6 -0
+static GLchar texture_blit_vert_src[] =
+    "\n"
+    "#version 300 es\n"
+    "\n"
+    "in vec2  in_position;\n"
+    "in vec2  in_tex_coord;\n"

You could calculate the texture coordinate from the position in the shader, but this is mostly my premature optimization instinct kicking in instead of a real performance difference considering how few vertices there are in this case.

Still makes sense. Done.

Quoted 6 lines

ui/console-gl.c line 45

Quoted 4 lines
ui/console-gl.c new file +4 -0
+    "out vec2 ex_tex_coord;\n"
+    "\n"
+    "void main(void) {\n"
+    "    gl_Position = vec4(in_position.x, in_position.y, 0.0, 1.0);\n"

vec4(in_position, 0.0, 1.0) *cough*

Done.

Quoted 5 lines

This looks like a list for GL_TRIANGLES instead of GL_TRIANGLE_STRIP. For GL_TRIANGLE_STRIP, you first define one whole triangle (by specifying each of the three vertices) and after that, each vertex results in a new triangle drawn (whose two other vertices are the two vertices preceding the last one).

Thanks for the nice description.

So the trick to get it done with only four vertexes is to put the correct points (which are going to be shared by both triangles) into the middle. I've played around with it a bit without success (and before my new opengl book arrived), and this 6-point version worked ...

Quoted 6 lines

ui/console-gl.c line 88

Quoted 5 lines
ui/console-gl.c new file +5 -0
+    glUseProgram(gls->texture_blit_prog);
+    l_position = glGetAttribLocation(gls->texture_blit_prog, "in_position");
+    l_tex_coord = glGetAttribLocation(gls->texture_blit_prog, "in_tex_coord");
+    glVertexAttribPointer(l_position, 2, GL_FLOAT, GL_FALSE, 0, in_position);
+    glVertexAttribPointer(l_tex_coord, 2, GL_FLOAT, GL_FALSE, 0, in_tex_coord);
Quoted 6 lines

I think it is disallowed to refer to non-OpenGL buffers here in the core profile. The 4.4 core specification states (section 10.3, vertex arrays): "Vertex data are placed into arrays that are stored in the server’s address space"; the 4.4 compatibility specification states:
"Vertex data may also be placed into arrays that are stored in the client’s address space (described here) or in the server’s address space".

Got this from gles sample code, so that should be ok ;)

I've also seen code explicitly storing vertex data in a buffer, which I assume is more efficient, but I decided to not care for now, especially given the small number of vertexes we are processing here.

Quoted 5 lines
Quoted 1 line

+ return gl_create_link_program(vert_shader, frag_shader);

Minor thing: You are free to delete the shaders after they have been linked into the program (because the references will be lost when this function returns).

Done.

Quoted 9 lines

ui/console-gl.c line 201

Quoted 4 lines
ui/console-gl.c new file +4 -0
+    switch (surface->format) {
+    case PIXMAN_BE_b8g8r8x8:
+    case PIXMAN_BE_b8g8r8a8:
+        surface->glformat = GL_BGRA_EXT;

If you want to avoid the _EXT, you could use GL_RGBA here and texture().bgra in the fragment shader. However, this would require a different shader for the 32 bit and the 16 bit formats (because the 16 bit format has the right order, apparently).

At least it worked in testing.

Quoted 2 lines

I haven't been able to really test 16 bit mode (forcing 16 bit mode in hw/display/vga.c doesn't count...), so I'm trusting you this works.

-kernel /boot/vmlinux-$whatever -append vga=0x314

Makes the kernel run vesafb with 800x600 @ 16bpp, and thanks to the little friendly penguin in the upper left corner (CONFIG_LOGO=y) you can easily spot colors being messed up.

Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+    glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT,
+                  surface_stride(surface) / surface_bytes_per_pixel(surface));

Maybe we should assert that surface_stride(surface) % surface_bytes_per_pixel(surface) == 0 here.

Done.

thanks,
Gerd

#10 lore raw

Re: [Qemu-devel] [PATCH v2 4/7] console-gl: add opengl rendering helper functions

Quoted 26 lines · Gerd Hoffmann
   Hi,
Quoted 10 lines

ui/console-gl.c line 41

Quoted 6 lines
ui/console-gl.c new file +6 -0
+static GLchar texture_blit_vert_src[] =
+    "\n"
+    "#version 300 es\n"
+    "\n"
+    "in vec2  in_position;\n"
+    "in vec2  in_tex_coord;\n"

You could calculate the texture coordinate from the position in the shader, but this is mostly my premature optimization instinct kicking in instead of a real performance difference considering how few vertices there are in this case.

Still makes sense. Done.

Quoted 5 lines

ui/console-gl.c line 45

Quoted 4 lines
ui/console-gl.c new file +4 -0
+    "out vec2 ex_tex_coord;\n"
+    "\n"
+    "void main(void) {\n"
+    "    gl_Position = vec4(in_position.x, in_position.y, 0.0, 1.0);\n"

vec4(in_position, 0.0, 1.0) *cough*

Done.

Quoted 5 lines

This looks like a list for GL_TRIANGLES instead of GL_TRIANGLE_STRIP. For GL_TRIANGLE_STRIP, you first define one whole triangle (by specifying each of the three vertices) and after that, each vertex results in a new triangle drawn (whose two other vertices are the two vertices preceding the last one).

Quoted 6 lines

Thanks for the nice description.

So the trick to get it done with only four vertexes is to put the correct points (which are going to be shared by both triangles) into the middle. I've played around with it a bit without success (and before my new opengl book arrived), and this 6-point version worked ...

Hm, I did write a working solution in my reply, didn't I? It was: { -1, -1, 1, -1, -1, 1, 1, 1 } for in_position[] and { 0, 1, 1, 1, 0, 0, 1, 0 } for in_tex_coord[]. At least it worked for me.

You haven't (explicitly) enabled face culling, but remember that while normally counter-clockwise faces are backfaces and thus culled, the direction is inverted for every triangle; so the first triangle needs to be CCW, the second CW, the third CCW again, and so on. Maybe that's why it didn't work for you...

Quoted 33 lines
Quoted 11 lines

ui/console-gl.c line 88

Quoted 5 lines
ui/console-gl.c new file +5 -0
+    glUseProgram(gls->texture_blit_prog);
+    l_position = glGetAttribLocation(gls->texture_blit_prog, "in_position");
+    l_tex_coord = glGetAttribLocation(gls->texture_blit_prog, "in_tex_coord");
+    glVertexAttribPointer(l_position, 2, GL_FLOAT, GL_FALSE, 0, in_position);
+    glVertexAttribPointer(l_tex_coord, 2, GL_FLOAT, GL_FALSE, 0, in_tex_coord);

I think it is disallowed to refer to non-OpenGL buffers here in the core profile. The 4.4 core specification states (section 10.3, vertex arrays): "Vertex data are placed into arrays that are stored in the server’s address space"; the 4.4 compatibility specification states:
"Vertex data may also be placed into arrays that are stored in the client’s address space (described here) or in the server’s address space".

Got this from gles sample code, so that should be ok ;)

I've also seen code explicitly storing vertex data in a buffer, which I assume is more efficient, but I decided to not care for now, especially given the small number of vertexes we are processing here.

Quoted 4 lines
Quoted 1 line

+ return gl_create_link_program(vert_shader, frag_shader);

Minor thing: You are free to delete the shaders after they have been linked into the program (because the references will be lost when this function returns).

Done.

Quoted 8 lines

ui/console-gl.c line 201

Quoted 4 lines
ui/console-gl.c new file +4 -0
+    switch (surface->format) {
+    case PIXMAN_BE_b8g8r8x8:
+    case PIXMAN_BE_b8g8r8a8:
+        surface->glformat = GL_BGRA_EXT;

If you want to avoid the _EXT, you could use GL_RGBA here and texture().bgra in the fragment shader. However, this would require a different shader for the 32 bit and the 16 bit formats (because the 16 bit format has the right order, apparently).

At least it worked in testing.

Quoted 7 lines
Quoted 2 lines

I haven't been able to really test 16 bit mode (forcing 16 bit mode in hw/display/vga.c doesn't count...), so I'm trusting you this works.

-kernel /boot/vmlinux-$whatever -append vga=0x314

Makes the kernel run vesafb with 800x600 @ 16bpp, and thanks to the little friendly penguin in the upper left corner (CONFIG_LOGO=y) you can easily spot colors being messed up.

Thanks!

Max

#11 lore raw

Re: [Qemu-devel] [PATCH v2 4/7] console-gl: add opengl rendering helper functions

Hi,

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

This looks like a list for GL_TRIANGLES instead of GL_TRIANGLE_STRIP. For GL_TRIANGLE_STRIP, you first define one whole triangle (by specifying each of the three vertices) and after that, each vertex results in a new triangle drawn (whose two other vertices are the two vertices preceding the last one).

Quoted 8 lines
Quoted 6 lines

Thanks for the nice description.

So the trick to get it done with only four vertexes is to put the correct points (which are going to be shared by both triangles) into the middle. I've played around with it a bit without success (and before my new opengl book arrived), and this 6-point version worked ...

Hm, I did write a working solution in my reply, didn't I?

Yep, was just trying to explain how I ended up with the 6 vertex version, not having fully figured how triangle strips are working.

All fine now, I've changed it to use 4 vertexes version instead, and I _also_ understood why it is working as intended.

FYI: current code is at
https://www.kraxel.org/cgit/qemu/log/?h=rebase/ui-sdl-next

[ incremental fixes not squashed in yet ]

thanks,
Gerd

#12 lore raw

Re: [Qemu-devel] [PATCH v2 4/7] console-gl: add opengl rendering helper functions

Quoted 9 lines · Gerd Hoffmann
   Hi,
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines

This looks like a list for GL_TRIANGLES instead of GL_TRIANGLE_STRIP. For GL_TRIANGLE_STRIP, you first define one whole triangle (by specifying each of the three vertices) and after that, each vertex results in a new triangle drawn (whose two other vertices are the two vertices preceding the last one).

Thanks for the nice description.

Quoted 7 lines
Quoted 5 lines
Quoted 4 lines

So the trick to get it done with only four vertexes is to put the correct points (which are going to be shared by both triangles) into the middle. I've played around with it a bit without success (and before my new opengl book arrived), and this 6-point version worked ...

Hm, I did write a working solution in my reply, didn't I?

Yep, was just trying to explain how I ended up with the 6 vertex version, not having fully figured how triangle strips are working.

I see.

Quoted 2 lines

All fine now, I've changed it to use 4 vertexes version instead, and I _also_ understood why it is working as intended.

Great. :-)

Quoted 2 lines

FYI: current code is at
https://www.kraxel.org/cgit/qemu/log/?h=rebase/ui-sdl-next

Thanks!

Max

#13 lore raw

PATCH v2 5/7 console-gl: externalize shader programs

In reply to Gerd Hoffmann
---
 Makefile                    | 17 +++++++++++++++++
 scripts/shaderinclude.pl    | 16 ++++++++++++++++
 ui/console-gl.c             | 28 ++--------------------------
 ui/shader/texture-blit.frag | 10 ++++++++++
 ui/shader/texture-blit.vert | 11 +++++++++++
 5 files changed, 56 insertions(+), 26 deletions(-)
 create mode 100644 scripts/shaderinclude.pl
 create mode 100644 ui/shader/texture-blit.frag
 create mode 100644 ui/shader/texture-blit.vert
Makefile +17 -0
index 6817c6f..6d77782 100644
@@ -292,6 +292,7 @@ clean:
 	rm -f fsdev/*.pod
 	rm -rf .libs */.libs
 	rm -f qemu-img-cmds.h
+	rm -f ui/shader/*-vert.h ui/shader/*-frag.h
 	@# May not be present in GENERATED_HEADERS
 	rm -f trace/generated-tracers-dtrace.dtrace*
 	rm -f trace/generated-tracers-dtrace.h*
@@ -437,6 +438,22 @@ cscope:
 	find "$(SRC_PATH)" -name "*.[chsS]" -print | sed 's,^\./,,' > ./cscope.files
 	cscope -b
 
+# opengl shader programs
+ui/shader/%-vert.h: $(SRC_PATH)/ui/shader/%.vert $(SRC_PATH)/scripts/shaderinclude.pl
+	@mkdir -p $(dir $@)
+	$(call quiet-command,\
+		perl $(SRC_PATH)/scripts/shaderinclude.pl $< > $@,\
+		"  VERT  $@")
+
+ui/shader/%-frag.h: $(SRC_PATH)/ui/shader/%.frag $(SRC_PATH)/scripts/shaderinclude.pl
+	@mkdir -p $(dir $@)
+	$(call quiet-command,\
+		perl $(SRC_PATH)/scripts/shaderinclude.pl $< > $@,\
+		"  FRAG  $@")
+
+ui/console-gl.o: $(SRC_PATH)/ui/console-gl.c \
+	ui/shader/texture-blit-vert.h ui/shader/texture-blit-frag.h
+
 # documentation
 MAKEINFO=makeinfo
 MAKEINFOFLAGS=--no-headers --no-split --number-sections
scripts/shaderinclude.pl new file +16 -0
new file mode 100644
index 0000000..81b5146
@@ -0,0 +1,16 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+my $file = shift;
+open FILE, "<", $file or die "open $file: $!";
+my $name = $file;
+$name =~ s|.*/||;
+$name =~ s/[-.]/_/g;
+print "static GLchar ${name}_src[] =\n";
+while (<FILE>) {
+    chomp;
+    printf "    \"%s\\n\"\n", $_;
+}
+print "    \"\\n\";\n";
+close FILE;
ui/console-gl.c +2 -26
index 589c682..2c9412d 100644
@@ -33,32 +33,8 @@ struct ConsoleGLState {
 
 /* ---------------------------------------------------------------------- */
 
-static GLchar texture_blit_vert_src[] =
-    "\n"
-    "#version 300 es\n"
-    "\n"
-    "in vec2  in_position;\n"
-    "in vec2  in_tex_coord;\n"
-    "out vec2 ex_tex_coord;\n"
-    "\n"
-    "void main(void) {\n"
-    "    gl_Position = vec4(in_position.x, in_position.y, 0.0, 1.0);\n"
-    "    ex_tex_coord = in_tex_coord;\n"
-    "}\n"
-    "\n";
-
-static GLchar texture_blit_frag_src[] =
-    "\n"
-    "#version 300 es\n"
-    "\n"
-    "uniform sampler2D image;\n"
-    "in  highp vec2 ex_tex_coord;\n"
-    "out highp vec4 out_frag_color;\n"
-    "\n"
-    "void main(void) {\n"
-    "     out_frag_color = texture(image, ex_tex_coord);\n"
-    "}\n"
-    "\n";
+#include "shader/texture-blit-vert.h"
+#include "shader/texture-blit-frag.h"
 
 static void gl_run_texture_blit(ConsoleGLState *gls)
 {
ui/shader/texture-blit.frag new file +10 -0
new file mode 100644
index 0000000..148b1aa
@@ -0,0 +1,10 @@
+
+#version 300 es
+
+uniform sampler2D image;
+in  highp vec2 ex_tex_coord;
+out highp vec4 out_frag_color;
+
+void main(void) {
+     out_frag_color = texture(image, ex_tex_coord);
+}
ui/shader/texture-blit.vert new file +11 -0
new file mode 100644
index 0000000..4ffb5d1
@@ -0,0 +1,11 @@
+
+#version 300 es
+
+in vec2  in_position;
+in vec2  in_tex_coord;
+out vec2 ex_tex_coord;
+
+void main(void) {
+    gl_Position = vec4(in_position.x, in_position.y, 0.0, 1.0);
+    ex_tex_coord = in_tex_coord;
+}
Signature · 1 line
1.8.3.1
Reviewed-by on v2 5/7
#14 lore raw

Re: [Qemu-devel] [PATCH v2 5/7] console-gl: externalize shader programs

Quoted 1 line · Gerd Hoffmann

---

S-o-b is missing.

Quoted 114 lines
  Makefile                    | 17 +++++++++++++++++
  scripts/shaderinclude.pl    | 16 ++++++++++++++++
  ui/console-gl.c             | 28 ++--------------------------
  ui/shader/texture-blit.frag | 10 ++++++++++
  ui/shader/texture-blit.vert | 11 +++++++++++
  5 files changed, 56 insertions(+), 26 deletions(-)

create mode 100644 scripts/shaderinclude.pl
create mode 100644 ui/shader/texture-blit.frag
create mode 100644 ui/shader/texture-blit.vert

Makefile +17 -0
index 6817c6f..6d77782 100644
@@ -292,6 +292,7 @@ clean:
  	rm -f fsdev/*.pod
  	rm -rf .libs */.libs
  	rm -f qemu-img-cmds.h
+	rm -f ui/shader/*-vert.h ui/shader/*-frag.h
  	@# May not be present in GENERATED_HEADERS
  	rm -f trace/generated-tracers-dtrace.dtrace*
  	rm -f trace/generated-tracers-dtrace.h*
@@ -437,6 +438,22 @@ cscope:
  	find "$(SRC_PATH)" -name "*.[chsS]" -print | sed 's,^\./,,' > ./cscope.files
  	cscope -b
 
+# opengl shader programs
+ui/shader/%-vert.h: $(SRC_PATH)/ui/shader/%.vert $(SRC_PATH)/scripts/shaderinclude.pl
+	@mkdir -p $(dir $@)
+	$(call quiet-command,\
+		perl $(SRC_PATH)/scripts/shaderinclude.pl $< > $@,\
+		"  VERT  $@")
+
+ui/shader/%-frag.h: $(SRC_PATH)/ui/shader/%.frag $(SRC_PATH)/scripts/shaderinclude.pl
+	@mkdir -p $(dir $@)
+	$(call quiet-command,\
+		perl $(SRC_PATH)/scripts/shaderinclude.pl $< > $@,\
+		"  FRAG  $@")
+
+ui/console-gl.o: $(SRC_PATH)/ui/console-gl.c \
+	ui/shader/texture-blit-vert.h ui/shader/texture-blit-frag.h
+
  # documentation
  MAKEINFO=makeinfo
  MAKEINFOFLAGS=--no-headers --no-split --number-sections
scripts/shaderinclude.pl new file +16 -0
new file mode 100644
index 0000000..81b5146
@@ -0,0 +1,16 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+my $file = shift;
+open FILE, "<", $file or die "open $file: $!";
+my $name = $file;
+$name =~ s|.*/||;
+$name =~ s/[-.]/_/g;
+print "static GLchar ${name}_src[] =\n";
+while (<FILE>) {
+    chomp;
+    printf "    \"%s\\n\"\n", $_;
+}
+print "    \"\\n\";\n";
+close FILE;
ui/console-gl.c +2 -26
index 589c682..2c9412d 100644
@@ -33,32 +33,8 @@ struct ConsoleGLState {
 
  /* ---------------------------------------------------------------------- */
 
-static GLchar texture_blit_vert_src[] =
-    "\n"
-    "#version 300 es\n"
-    "\n"
-    "in vec2  in_position;\n"
-    "in vec2  in_tex_coord;\n"
-    "out vec2 ex_tex_coord;\n"
-    "\n"
-    "void main(void) {\n"
-    "    gl_Position = vec4(in_position.x, in_position.y, 0.0, 1.0);\n"
-    "    ex_tex_coord = in_tex_coord;\n"
-    "}\n"
-    "\n";
-
-static GLchar texture_blit_frag_src[] =
-    "\n"
-    "#version 300 es\n"
-    "\n"
-    "uniform sampler2D image;\n"
-    "in  highp vec2 ex_tex_coord;\n"
-    "out highp vec4 out_frag_color;\n"
-    "\n"
-    "void main(void) {\n"
-    "     out_frag_color = texture(image, ex_tex_coord);\n"
-    "}\n"
-    "\n";
+#include "shader/texture-blit-vert.h"
+#include "shader/texture-blit-frag.h"
 
  static void gl_run_texture_blit(ConsoleGLState *gls)
  {
ui/shader/texture-blit.frag new file +0 -0
new file mode 100644
index 0000000..148b1aa

ui/shader/texture-blit.frag line 6

Quoted 6 lines
ui/shader/texture-blit.frag new file +6 -0
@@ -0,0 +1,10 @@
+
+#version 300 es
+
+uniform sampler2D image;
+in  highp vec2 ex_tex_coord;
+out highp vec4 out_frag_color;

Apart from me not knowing what highp really does because I don't know GLES, I can imagine what it's probably supposed to do (high-precision); do we really need it for these values?

(I know, I should've said that in the previous patch already…)

Quoted 11 lines
ui/shader/texture-blit.frag new file +4 -0
+
+void main(void) {
+     out_frag_color = texture(image, ex_tex_coord);
+}
ui/shader/texture-blit.vert new file +1 -0
new file mode 100644
index 0000000..4ffb5d1
@@ -0,0 +1,11 @@
+

ui/shader/texture-blit.vert line 11

Quoted 10 lines
ui/shader/texture-blit.vert new file +10 -0
+#version 300 es
+
+in vec2  in_position;
+in vec2  in_tex_coord;
+out vec2 ex_tex_coord;
+
+void main(void) {
+    gl_Position = vec4(in_position.x, in_position.y, 0.0, 1.0);
+    ex_tex_coord = in_tex_coord;
+}

gl_Position = vec4(in_position, 0.0, 1.0);
ex_tex_coord = vec2(1.0 + in_position.x, 1.0 - in_position.y) * 0.5;

*duck*

Anyway:

#15 lore raw

PATCH v2 6/7 sdl2: move SDL_* includes to sdl2.h

In reply to Gerd Hoffmann
---
 include/ui/sdl2.h | 6 ++++++
 ui/sdl2-2d.c      | 6 ------
 ui/sdl2-input.c   | 6 ------
 ui/sdl2.c         | 6 ------
 4 files changed, 6 insertions(+), 18 deletions(-)
include/ui/sdl2.h +6 -0
index f56c596..148308c 100644
@@ -1,6 +1,12 @@
 #ifndef SDL2_H
 #define SDL2_H
 
+/* Avoid compiler warning because macro is redefined in SDL_syswm.h. */
+#undef WIN32_LEAN_AND_MEAN
+
+#include <SDL.h>
+#include <SDL_syswm.h>
+
 struct sdl2_console {
     DisplayChangeListener dcl;
     DisplaySurface *surface;
ui/sdl2-2d.c +0 -6
index 9264817..7a82d50 100644
@@ -23,12 +23,6 @@
  */
 /* Ported SDL 1.2 code to 2.0 by Dave Airlie. */
 
-/* Avoid compiler warning because macro is redefined in SDL_syswm.h. */
-#undef WIN32_LEAN_AND_MEAN
-
-#include <SDL.h>
-#include <SDL_syswm.h>
-
 #include "qemu-common.h"
 #include "ui/console.h"
 #include "ui/input.h"
ui/sdl2-input.c +0 -6
index a1973fc..ac5dc94 100644
@@ -23,12 +23,6 @@
  */
 /* Ported SDL 1.2 code to 2.0 by Dave Airlie. */
 
-/* Avoid compiler warning because macro is redefined in SDL_syswm.h. */
-#undef WIN32_LEAN_AND_MEAN
-
-#include <SDL.h>
-#include <SDL_syswm.h>
-
 #include "qemu-common.h"
 #include "ui/console.h"
 #include "ui/input.h"
ui/sdl2.c +0 -6
index 1ae2781..943d1da 100644
@@ -23,12 +23,6 @@
  */
 /* Ported SDL 1.2 code to 2.0 by Dave Airlie. */
 
-/* Avoid compiler warning because macro is redefined in SDL_syswm.h. */
-#undef WIN32_LEAN_AND_MEAN
-
-#include <SDL.h>
-#include <SDL_syswm.h>
-
 #include "qemu-common.h"
 #include "ui/console.h"
 #include "ui/input.h"
Signature · 1 line
1.8.3.1
#17 lore raw

PATCH v2 7/7 sdl2: add support for display rendering using opengl.

In reply to Gerd Hoffmann

Add new sdl2-gl.c file, with display
rendering functions using opengl.

---
 include/ui/console.h |   1 +
 include/ui/sdl2.h    |  11 +++++
 ui/Makefile.objs     |   3 ++
 ui/sdl.c             |  11 +++++
 ui/sdl2-2d.c         |   7 ++++
 ui/sdl2-gl.c         | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++
 ui/sdl2.c            |  67 ++++++++++++++++++++++++++----
 vl.c                 |  11 +++++
 8 files changed, 216 insertions(+), 7 deletions(-)
 create mode 100644 ui/sdl2-gl.c
include/ui/console.h +1 -0
index 9157b64..1540d67 100644
@@ -352,6 +352,7 @@ void surface_gl_setup_viewport(ConsoleGLState *gls,
 #endif
 
 /* sdl.c */
+void sdl_display_early_init(int opengl);
 void sdl_display_init(DisplayState *ds, int full_screen, int no_frame);
 
 /* cocoa.m */
include/ui/sdl2.h +11 -0
index 148308c..3f89d30 100644
@@ -17,6 +17,10 @@ struct sdl2_console {
     int last_vm_running; /* per console for caption reasons */
     int x, y;
     int hidden;
+    int opengl;
+    int updates;
+    SDL_GLContext winctx;
+    ConsoleGLState *gls;
 };
 
 void sdl2_window_create(struct sdl2_console *scon);
@@ -35,4 +39,11 @@ void sdl2_2d_switch(DisplayChangeListener *dcl,
 void sdl2_2d_refresh(DisplayChangeListener *dcl);
 void sdl2_2d_redraw(struct sdl2_console *scon);
 
+void sdl2_gl_update(DisplayChangeListener *dcl,
+                    int x, int y, int w, int h);
+void sdl2_gl_switch(DisplayChangeListener *dcl,
+                    DisplaySurface *new_surface);
+void sdl2_gl_refresh(DisplayChangeListener *dcl);
+void sdl2_gl_redraw(struct sdl2_console *scon);
+
 #endif /* SDL2_H */
ui/Makefile.objs +3 -0
index 3173778..a88f730 100644
@@ -21,6 +21,9 @@ sdl.mo-objs := sdl.o sdl_zoom.o
 endif
 ifeq ($(CONFIG_SDLABI),2.0)
 sdl.mo-objs := sdl2.o sdl2-input.o sdl2-2d.o
+ifeq ($(CONFIG_OPENGL),y)
+sdl.mo-objs += sdl2-gl.o
+endif
 endif
 sdl.mo-cflags := $(SDL_CFLAGS)
 
ui/sdl.c +11 -0
index 3e9d810..1fe002d 100644
@@ -873,6 +873,17 @@ static const DisplayChangeListenerOps dcl_ops = {
     .dpy_cursor_define = sdl_mouse_define,
 };
 
+void sdl_display_early_init(int opengl)
+{
+    if (opengl == 1 /* on */) {
+        fprintf(stderr,
+                "SDL1 display code has no opengl support.\n"
+                "Please recompile qemu with SDL2, using\n"
+                "./configure --enable-sdl --with-sdlabi=2.0\n");
+        exit(1);
+    }
+}
+
 void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
 {
     int flags;
ui/sdl2-2d.c +7 -0
index 7a82d50..f6cb438 100644
@@ -36,6 +36,8 @@ void sdl2_2d_update(DisplayChangeListener *dcl,
     DisplaySurface *surf = qemu_console_surface(dcl->con);
     SDL_Rect rect;
 
+    assert(!scon->opengl);
+
     if (!surf) {
         return;
     }
@@ -61,6 +63,8 @@ void sdl2_2d_switch(DisplayChangeListener *dcl,
     DisplaySurface *old_surface = scon->surface;
     int format = 0;
 
+    assert(!scon->opengl);
+
     scon->surface = new_surface;
 
     if (scon->texture) {
@@ -101,12 +105,15 @@ void sdl2_2d_refresh(DisplayChangeListener *dcl)
 {
     struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
 
+    assert(!scon->opengl);
     graphic_hw_update(dcl->con);
     sdl2_poll_events(scon);
 }
 
 void sdl2_2d_redraw(struct sdl2_console *scon)
 {
+    assert(!scon->opengl);
+
     if (!scon->surface) {
         return;
     }
ui/sdl2-gl.c new file +112 -0

112 lines not on this page · raw message

ui/sdl2.c +60 -7

120 lines not on this page · raw message

vl.c +11 -0
index 0bf2acb..3ce1389 100644
@@ -1947,6 +1947,7 @@ static DisplayType select_display(const char *p)
 {
     const char *opts;
     DisplayType display = DT_DEFAULT;
+    int opengl = -1;
 
     if (strstart(p, "sdl", &opts)) {
 #ifdef CONFIG_SDL
@@ -1990,6 +1991,15 @@ static DisplayType select_display(const char *p)
                 } else {
                     goto invalid_sdl_args;
                 }
+            } else if (strstart(opts, ",gl=", &nextopt)) {
+                opts = nextopt;
+                if (strstart(opts, "on", &nextopt)) {
+                    opengl = 1;
+                } else if (strstart(opts, "off", &nextopt)) {
+                    opengl = 0;
+                } else {
+                    goto invalid_sdl_args;
+                }
             } else {
             invalid_sdl_args:
                 fprintf(stderr, "Invalid SDL option string: %s\n", p);
@@ -1997,6 +2007,7 @@ static DisplayType select_display(const char *p)
             }
             opts = nextopt;
         }
+        sdl_display_early_init(opengl);
 #else
         fprintf(stderr, "SDL support is disabled\n");
         exit(1);
Signature · 1 line
1.8.3.1
#18 lore raw

Re: [Qemu-devel] [PATCH v2 7/7] sdl2: add support for display rendering using opengl.

Quoted 15 lines · Gerd Hoffmann

Add new sdl2-gl.c file, with display
rendering functions using opengl.

---
  include/ui/console.h |   1 +
  include/ui/sdl2.h    |  11 +++++
  ui/Makefile.objs     |   3 ++
  ui/sdl.c             |  11 +++++
  ui/sdl2-2d.c         |   7 ++++
  ui/sdl2-gl.c         | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++
  ui/sdl2.c            |  67 ++++++++++++++++++++++++++----
  vl.c                 |  11 +++++
  8 files changed, 216 insertions(+), 7 deletions(-)

create mode 100644 ui/sdl2-gl.c

[snip]

Quoted 64 lines
ui/sdl2-gl.c new file +58 -0
new file mode 100644
index 0000000..b604c06
@@ -0,0 +1,112 @@
+/*
+ * QEMU SDL display driver -- opengl support
+ *
+ * Copyright (c) 2014 Red Hat
+ *
+ * Authors:
+ *     Gerd Hoffmann <[email protected]>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "qemu-common.h"
+#include "ui/console.h"
+#include "ui/input.h"
+#include "ui/sdl2.h"
+#include "sysemu/sysemu.h"
+
+static void sdl2_gl_render_surface(struct sdl2_console *scon)
+{
+    int ww, wh;
+
+    SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
+
+    SDL_GetWindowSize(scon->real_window, &ww, &wh);
+    surface_gl_setup_viewport(scon->gls, scon->surface, ww, wh);
+
+    surface_gl_render_texture(scon->gls, scon->surface);
+    SDL_GL_SwapWindow(scon->real_window);
+}
+
+void sdl2_gl_update(DisplayChangeListener *dcl,
+                    int x, int y, int w, int h)
+{
+    struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
+
+    assert(scon->opengl);
+
+    SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
+    surface_gl_update_texture(scon->gls, scon->surface, x, y, w, h);
+    scon->updates++;
+}
+

ui/sdl2-gl.c line 68

Quoted 10 lines
ui/sdl2-gl.c new file +10 -0
+void sdl2_gl_switch(DisplayChangeListener *dcl,
+                    DisplaySurface *new_surface)
+{
+    struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
+    DisplaySurface *old_surface = scon->surface;
+
+    assert(scon->opengl);
+
+    SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
+    surface_gl_destroy_texture(scon->gls, scon->surface);

Same question as for v1: Can a surface be in use by multiple DCLs?

Quoted 6 lines
ui/sdl2-gl.c new file +6 -0
+
+    scon->surface = new_surface;
+
+    if (!new_surface) {
+        console_gl_fini_context(scon->gls);
+        scon->gls = NULL;

ui/sdl2-gl.c line 84

Quoted 10 lines
ui/sdl2-gl.c new file +10 -0
+        sdl2_window_destroy(scon);
+        return;
+    }
+
+    if (!scon->real_window) {
+        sdl2_window_create(scon);
+        scon->gls = console_gl_init_context();
+    } else if (old_surface &&
+               ((surface_width(old_surface)  != surface_width(new_surface)) ||
+                (surface_height(old_surface) != surface_height(new_surface)))) {

And as in v1: If the window is scaled, this will reset the scaling to 100 %, which is fine. However, if the new surface has the same dimensions as the old surface, the window will not be scaled. That would seem strange to me (why is the scaling reset for some surface changes but not for others?).

Max

#19 lore raw

Re: [Qemu-devel] [PATCH v2 7/7] sdl2: add support for display rendering using opengl.

Hi,

Quoted 4 lines
Quoted 4 lines
ui/sdl2-gl.c new file +4 -0
+void sdl2_gl_switch(DisplayChangeListener *dcl,
+                    DisplaySurface *new_surface)
+{
+    struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
Quoted 8 lines

ui/sdl2-gl.c line 68

Quoted 6 lines
ui/sdl2-gl.c new file +6 -0
+    DisplaySurface *old_surface = scon->surface;
+
+    assert(scon->opengl);
+
+    SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
+    surface_gl_destroy_texture(scon->gls, scon->surface);

Same question as for v1: Can a surface be in use by multiple DCLs?

Oops, I remember that comment for the last series, forgot to answer ...

Yes. Old surface is released after notifying all DCLs about the new one (see dpy_gfx_replace_surface() in console.c), so the old is still valid at this point. That way refcounting should not be needed in most cases.

If a DCL needs the old surface image stay around after it returns from the switch callback (say due to threads still holding pointers to it) it can grab a reference on the pixman image backing the surface:
pixman_image_ref(surface->image). The surfaces itself are not reference counted.

Quoted 9 lines

ui/sdl2-gl.c line 84

Quoted 3 lines
ui/sdl2-gl.c new file +3 -0
+    } else if (old_surface &&
+               ((surface_width(old_surface)  != surface_width(new_surface)) ||
+                (surface_height(old_surface) != surface_height(new_surface)))) {

And as in v1: If the window is scaled, this will reset the scaling to 100 %, which is fine. However, if the new surface has the same dimensions as the old surface, the window will not be scaled. That would seem strange to me (why is the scaling reset for some surface changes but not for others?).

Yea, there are some corner cases. But they are not specific to opengl, they happen in sdl2-2d mode too, so that is something for another patch series ...

cheers,
Gerd