Mesa (main): Fix static glapi on Windows

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Thu Jul 14 20:35:23 UTC 2022


Module: Mesa
Branch: main
Commit: ddbbe96c88f7ab428c6c0212f4624f0166e98d32
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=ddbbe96c88f7ab428c6c0212f4624f0166e98d32

Author: Jesse Natalie <jenatali at microsoft.com>
Date:   Wed May 25 13:50:42 2022 -0700

Fix static glapi on Windows

On Linux, the static glapi path sees libGL.so implementing the static
glapi, and the drivers (libgallium_dri.so) updating/reading the TLS
vars.

On Windows, to allow libgallium_wgl.dll to be a full ICD, it's
responsible for implementing the actual static glapi. However, before
this change, OpenGL32.dll was also implementing the static glapi,
meaning that GL API calls from OpenGL32.dll didn't route to the driver
correctly because the TLS vars were never actually set - the driver set
its copy, and OpenGL32.dll read its own copy.

Now, always build a bridge and static version of glapi when not using
shared. The bridge version is linked into OpenGL32.dll, and the static
version is linked into the driver on Windows. GLES only builds with
shared glapi - but after this, shared glapi is not really needed on
Windows for GLES, since the driver has all of the data.

Fixes: f36921ef ("wgl: Refactor drivers to a libgallium_wgl.dll")
Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/6560
Acked-by: Marek Olšák <marek.olsak at amd.com>
Reviewed-by: Dylan Baker <dylan at pnwbakers.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/16713>

---

 meson.build                               |   8 ---
 src/gallium/targets/libgl-gdi/meson.build |   2 +-
 src/mapi/glapi/glapi.h                    |   2 +-
 src/mapi/glapi/meson.build                | 113 +++++++++++++++++-------------
 4 files changed, 66 insertions(+), 59 deletions(-)

diff --git a/meson.build b/meson.build
index 5570a8a1f39..85bd12ff000 100644
--- a/meson.build
+++ b/meson.build
@@ -550,14 +550,6 @@ if not have_mtls_dialect
   endif
 endif
 
-if with_platform_windows and with_shared_glapi
-  # Windows doesn't support DLL exports/imports being TLS variables. When using shared
-  # glapi, libglapi.dll hosts the TLS, but other DLLs need to use them. Instead of falling
-  # all the way back to using globals and manual per-thread data, keep using TLS but just
-  # put accesses to it behind a function so the variable doesn't need to be exported.
-  pre_args += '-DUSE_TLS_BEHIND_FUNCTIONS'
-endif
-
 if with_glx != 'disabled'
   if not (with_platform_x11 and with_any_opengl)
     error('Cannot build GLX support without X11 platform support and at least one OpenGL API')
diff --git a/src/gallium/targets/libgl-gdi/meson.build b/src/gallium/targets/libgl-gdi/meson.build
index e344090e2e7..7ddcd8a1928 100644
--- a/src/gallium/targets/libgl-gdi/meson.build
+++ b/src/gallium/targets/libgl-gdi/meson.build
@@ -38,7 +38,7 @@ libopengl32 = shared_library(
     inc_include, inc_wgl, inc_src,
   ],
   link_with : [
-    libgallium_wgl, libglapi_static, libglapi
+    libgallium_wgl, libglapi_bridge, libglapi
   ],
   dependencies : [
     idep_mesautil
diff --git a/src/mapi/glapi/glapi.h b/src/mapi/glapi/glapi.h
index 60be92a1cd8..1903c3054fd 100644
--- a/src/mapi/glapi/glapi.h
+++ b/src/mapi/glapi/glapi.h
@@ -91,7 +91,7 @@ _GLAPI_EXPORT extern __THREAD_INITIAL_EXEC void * _glapi_tls_Context;
 _GLAPI_EXPORT extern const struct _glapi_table *_glapi_Dispatch;
 _GLAPI_EXPORT extern const void *_glapi_Context;
 
-#if defined (USE_TLS_BEHIND_FUNCTIONS)
+#if DETECT_OS_WINDOWS && !defined(MAPI_MODE_UTIL) && !defined(MAPI_MODE_GLAPI)
 # define GET_DISPATCH() _glapi_get_dispatch()
 # define GET_CURRENT_CONTEXT(C)  struct gl_context *C = (struct gl_context *) _glapi_get_context()
 #else
diff --git a/src/mapi/glapi/meson.build b/src/mapi/glapi/meson.build
index d2523bc19fe..db4a12664b8 100644
--- a/src/mapi/glapi/meson.build
+++ b/src/mapi/glapi/meson.build
@@ -29,62 +29,77 @@ if with_dri and ['apple', 'windows'].contains(with_dri_platform)
   static_glapi_files += [glapi_gentable_c, glapitable_h]
 endif
 
-if with_shared_glapi
-  static_glapi_files += files(
-    '../entry.c',
-    '../entry.h',
-    '../entry_x86-64_tls.h',
-    '../entry_x86_tls.h',
-    '../entry_x86_tsd.h',
-    '../entry_ppc64le_tls.h',
-    '../entry_ppc64le_tsd.h',
-    '../mapi_tmp.h',
-  )
-  static_glapi_files += glapi_mapi_tmp_h
-  static_glapi_args += [
-    '-DMAPI_MODE_BRIDGE',
-    '-DMAPI_ABI_HEADER="@0@"'.format(glapi_mapi_tmp_h.full_path()),
-    gcc_lto_quirk,
-  ]
-  if with_platform_windows
-    static_glapi_args += ['-D_GDI32_']
-  endif
-else
-  static_glapi_args += '-DMAPI_MODE_UTIL'
-  if with_platform_windows
-    static_glapi_args += ['-D_GDI32_', '-DKHRONOS_DLL_EXPORTS', '-D_GLAPI_NO_EXPORTS']
-  endif
-  static_glapi_files += files(
-    'glapi_dispatch.c',
-    'glapi_entrypoint.c',
-    'glapi_getproc.c',
-    'glapi_nop.c',
-    'glapi.c',
-    'glapi.h',
-    'glapi_priv.h',
-  )
-  static_glapi_files += files_mapi_util
-  static_glapi_files += [
-    glapitable_h, glapi_mapi_tmp_h, glprocs_h, glapitemp_h,
-  ]
-  if with_asm_arch == 'x86'
-    static_glapi_files += glapi_x86_s
-  elif with_asm_arch == 'x86_64'
-    static_glapi_files += glapi_x86_64_s
-  elif with_asm_arch == 'sparc'
-    static_glapi_files += glapi_sparc_s
-  endif
+bridge_glapi_files = static_glapi_files
+bridge_glapi_args = static_glapi_args
+
+bridge_glapi_files += files(
+  '../entry.c',
+  '../entry.h',
+  '../entry_x86-64_tls.h',
+  '../entry_x86_tls.h',
+  '../entry_x86_tsd.h',
+  '../entry_ppc64le_tls.h',
+  '../entry_ppc64le_tsd.h',
+  '../mapi_tmp.h',
+)
+bridge_glapi_files += glapi_mapi_tmp_h
+bridge_glapi_args += [
+  '-DMAPI_MODE_BRIDGE',
+  '-DMAPI_ABI_HEADER="@0@"'.format(glapi_mapi_tmp_h.full_path()),
+  gcc_lto_quirk,
+]
+if with_platform_windows
+  bridge_glapi_args += ['-D_GDI32_']
+endif
+
+static_glapi_args += '-DMAPI_MODE_UTIL'
+if with_platform_windows
+  static_glapi_args += ['-D_GDI32_', '-DKHRONOS_DLL_EXPORTS', '-D_GLAPI_DLL_EXPORTS']
+endif
+static_glapi_files += files(
+  'glapi_dispatch.c',
+  'glapi_entrypoint.c',
+  'glapi_getproc.c',
+  'glapi_nop.c',
+  'glapi.c',
+  'glapi.h',
+  'glapi_priv.h',
+)
+static_glapi_files += files_mapi_util
+static_glapi_files += [
+  glapitable_h, glapi_mapi_tmp_h, glprocs_h, glapitemp_h,
+]
+if with_asm_arch == 'x86'
+  static_glapi_files += glapi_x86_s
+elif with_asm_arch == 'x86_64'
+  static_glapi_files += glapi_x86_64_s
+elif with_asm_arch == 'sparc'
+  static_glapi_files += glapi_sparc_s
 endif
 
-libglapi_static = static_library(
-  'glapi_static',
-  static_glapi_files,
+
+libglapi_bridge = static_library(
+  'glapi_bridge',
+  bridge_glapi_files,
   include_directories : [inc_mesa, inc_include, inc_src, inc_mapi],
-  c_args : [c_msvc_compat_args, static_glapi_args],
+  c_args : [c_msvc_compat_args, bridge_glapi_args],
   dependencies : [dep_thread, dep_selinux, idep_mesautilc11],
   build_by_default : false,
 )
 
+if with_shared_glapi
+  libglapi_static = libglapi_bridge
+else
+  libglapi_static = static_library(
+    'glapi_static',
+    static_glapi_files,
+    include_directories : [inc_mesa, inc_include, inc_src, inc_mapi],
+    c_args : [c_msvc_compat_args, static_glapi_args],
+    dependencies : [dep_thread, dep_selinux, idep_mesautilc11],
+    build_by_default : false,
+  )
+endif
+
 # TODO: this test doesn't compile on windows with mingw or msvc due to
 # undefined symbols from libglapi_static, but that should be fixable.
 if with_any_opengl and not with_shared_glapi and with_tests and not with_platform_windows



More information about the mesa-commit mailing list