From 8e242fe7c11cb1a6853c0658290b27678558e400 Mon Sep 17 00:00:00 2001
From: David Snopek <dsnopek@gmail.com>
Date: Sat, 22 Jun 2024 15:55:48 -0500
Subject: [PATCH] Disable `*glGetProcAddress()` on the web

---
 drivers/gles3/rasterizer_gles3.cpp | 2 +-
 platform/web/detect.py             | 7 +++----
 platform/web/platform_gl.h         | 4 ++++
 3 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/drivers/gles3/rasterizer_gles3.cpp b/drivers/gles3/rasterizer_gles3.cpp
index ae39c86d44db..5ff95391a2fd 100644
--- a/drivers/gles3/rasterizer_gles3.cpp
+++ b/drivers/gles3/rasterizer_gles3.cpp
@@ -72,7 +72,7 @@
 
 #if !defined(IOS_ENABLED) && !defined(WEB_ENABLED)
 // We include EGL below to get debug callback on GLES2 platforms,
-// but EGL is not available on iOS.
+// but EGL is not available on iOS or the web.
 #define CAN_DEBUG
 #endif
 
diff --git a/platform/web/detect.py b/platform/web/detect.py
index 1b15ff8e2ea8..cb4dac11255d 100644
--- a/platform/web/detect.py
+++ b/platform/web/detect.py
@@ -207,11 +207,10 @@ def configure(env: "SConsEnvironment"):
         env.Append(LINKFLAGS=["-sMAX_WEBGL_VERSION=2"])
         # Allow use to take control of swapping WebGL buffers.
         env.Append(LINKFLAGS=["-sOFFSCREEN_FRAMEBUFFER=1"])
-        # Breaking change since emscripten 3.1.51
-        # https://github.com/emscripten-core/emscripten/blob/main/ChangeLog.md#3151---121323
+        # Disables the use of *glGetProcAddress() which is inefficient.
+        # See https://emscripten.org/docs/tools_reference/settings_reference.html#gl-enable-get-proc-address
         if cc_semver >= (3, 1, 51):
-            # Enables the use of *glGetProcAddress()
-            env.Append(LINKFLAGS=["-sGL_ENABLE_GET_PROC_ADDRESS=1"])
+            env.Append(LINKFLAGS=["-sGL_ENABLE_GET_PROC_ADDRESS=0"])
 
     if env["javascript_eval"]:
         env.Append(CPPDEFINES=["JAVASCRIPT_EVAL_ENABLED"])
diff --git a/platform/web/platform_gl.h b/platform/web/platform_gl.h
index be6e1462a7c3..8aadab81def8 100644
--- a/platform/web/platform_gl.h
+++ b/platform/web/platform_gl.h
@@ -35,6 +35,10 @@
 #define GLES_API_ENABLED // Allow using GLES.
 #endif
 
+// Make using *glGetProcAddress() an error on the web.
+#define glGetProcAddress(n) static_assert(false, "Usage of glGetProcessAddress() on the web is a bug.")
+#define eglGetProcAddress(n) static_assert(false, "Usage of eglGetProcessAddress() on the web is a bug.")
+
 #include "platform/web/godot_webgl2.h"
 
 #endif // PLATFORM_GL_H