gskglcompiler.c: Force GLSL version 300 es as needed
authorChun-wei Fan <fanchunwei@src.gnome.org>
Tue, 23 Nov 2021 10:22:55 +0000 (18:22 +0800)
committerChun-wei Fan <fanchunwei@src.gnome.org>
Fri, 3 Dec 2021 02:39:59 +0000 (10:39 +0800)
For libANGLE to work with our shaders, we must use "300 es" for
the #version directive in our shaders, as well as using the non-legacy/
non-GLES codepath in the shaders.  In order to check whether we are
using the GLSL 300 es shaders, we check whether we are using a GLES 3.0+
context.  As a result, make ->glsl_version a const char* and make sure
the existing shader version macros are defined apprpriately, and add a
new macro for the "300 es" shader version string.

This will allow the gtk4 programs to run under Windows using EGL via
libANGLE.  Some of the GL demos won't work for now, but at least this
makes things a lot better for using GL-accelerated graphics under Windows
for those that want to or need to use libANGLE (such as those with
graphics drivers that aren't capable of our Desktop (W)GL requirements in
GTK.

gsk/gl/gskglcompiler.c

index c885df8f4507edaa65cbb57c09369c4d09f4d18c..ca76c60aaebfa1d651a72d027032fe9a7a0995f2 100644 (file)
 #include "gskglcompilerprivate.h"
 #include "gskglprogramprivate.h"
 
-#define SHADER_VERSION_GLES       100
-#define SHADER_VERSION_GL2_LEGACY 110
-#define SHADER_VERSION_GL3_LEGACY 130
-#define SHADER_VERSION_GL3        150
+#define SHADER_VERSION_GLES       "100"
+#define SHADER_VERSION_GLES3      "300 es"
+#define SHADER_VERSION_GL2_LEGACY "110"
+#define SHADER_VERSION_GL3_LEGACY "130"
+#define SHADER_VERSION_GL3        "150"
 
 struct _GskGLCompiler
 {
@@ -49,7 +50,7 @@ struct _GskGLCompiler
 
   GArray *attrib_locations;
 
-  int glsl_version;
+  const char *glsl_version;
 
   guint gl3 : 1;
   guint gles : 1;
@@ -98,7 +99,7 @@ gsk_gl_compiler_class_init (GskGLCompilerClass *klass)
 static void
 gsk_gl_compiler_init (GskGLCompiler *self)
 {
-  self->glsl_version = 150;
+  self->glsl_version = "150";
   self->attrib_locations = g_array_new (FALSE, FALSE, sizeof (GskGLProgramAttrib));
   self->all_preamble = g_bytes_ref (empty_bytes);
   self->vertex_preamble = g_bytes_ref (empty_bytes);
@@ -127,8 +128,18 @@ gsk_gl_compiler_new (GskGLDriver *driver,
 
   if (gdk_gl_context_get_use_es (context))
     {
-      self->glsl_version = SHADER_VERSION_GLES;
-      self->gles = TRUE;
+      int maj, min;
+
+      /* for OpenGL/ES 3.0+, use "300 es" as our shader version */
+      gdk_gl_context_get_version (context, &maj, &min);
+
+      if (maj >= 3)
+        self->glsl_version = SHADER_VERSION_GLES3;
+      else
+        {
+          self->glsl_version = SHADER_VERSION_GLES;
+          self->gles = TRUE;
+        }
     }
   else if (gdk_gl_context_is_legacy (context))
     {
@@ -546,7 +557,7 @@ gsk_gl_compiler_compile (GskGLCompiler  *self,
 
   gsk_gl_command_queue_make_current (self->driver->command_queue);
 
-  g_snprintf (version, sizeof version, "#version %d\n", self->glsl_version);
+  g_snprintf (version, sizeof version, "#version %s\n", self->glsl_version);
 
   if (self->debug_shaders)
     debug = "#define GSK_DEBUG 1\n";