From 55183e6c3abea273dda5dc09decd6b8540d420f8 Mon Sep 17 00:00:00 2001 From: Stephen White Date: Mon, 27 Feb 2023 19:31:08 +0000 Subject: [PATCH] OpenGL: only load extension procs if extension supported. Only load extension entry points if the extension is supported. From the eglGetProcAddress manpage: "A non-NULL return value does not guarantee that an extension function is actually supported at runtime. The client must also make a corresponding query, such as glGetString(GL_EXTENSIONS) for OpenGL and OpenGL ES extensions [...] to determine if a function is supported by EGL or a specific client API context." This required moving extension initialization from OpenGLFunctions into OpenGLFunctionsBase. Change-Id: Ib4e8360ba455818701990b4476689b651d097ca8 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/121760 Commit-Queue: Stephen White Kokoro: Kokoro Reviewed-by: Austin Eng Reviewed-by: Corentin Wallez --- .../templates/opengl/OpenGLFunctionsBase.cpp | 33 ++++++++++++++++--- .../templates/opengl/OpenGLFunctionsBase.h | 6 ++++ src/dawn/native/opengl/OpenGLFunctions.cpp | 17 ---------- src/dawn/native/opengl/OpenGLFunctions.h | 7 ---- 4 files changed, 34 insertions(+), 29 deletions(-) diff --git a/generator/templates/opengl/OpenGLFunctionsBase.cpp b/generator/templates/opengl/OpenGLFunctionsBase.cpp index acddd4fd2f..4899c1558e 100644 --- a/generator/templates/opengl/OpenGLFunctionsBase.cpp +++ b/generator/templates/opengl/OpenGLFunctionsBase.cpp @@ -36,11 +36,15 @@ MaybeError OpenGLFunctionsBase::LoadOpenGLESProcs(GetProcAddress getProc, int ma {% endfor %} + InitializeSupportedGLExtensions(); + {% for block in extension_gles_blocks %} // {{block.extension}} - {% for proc in block.procs %} - DAWN_TRY(LoadProc(getProc, &{{proc.ProcName()}}, "{{proc.glProcName()}}")); + if (IsGLExtensionSupported("{{block.extension}}")) { + {% for proc in block.procs %} + DAWN_TRY(LoadProc(getProc, &{{proc.ProcName()}}, "{{proc.glProcName()}}")); {% endfor %} + } {% endfor %} return {}; @@ -57,14 +61,33 @@ MaybeError OpenGLFunctionsBase::LoadDesktopGLProcs(GetProcAddress getProc, int m {% endfor %} + InitializeSupportedGLExtensions(); + {% for block in extension_desktop_gl_blocks %} // {{block.extension}} - {% for proc in block.procs %} - DAWN_TRY(LoadProc(getProc, &{{proc.ProcName()}}, "{{proc.glProcName()}}")); - {% endfor %} + if (IsGLExtensionSupported("{{block.extension}}")) { + {% for proc in block.procs %} + DAWN_TRY(LoadProc(getProc, &{{proc.ProcName()}}, "{{proc.glProcName()}}")); + {% endfor %} + } {% endfor %} return {}; } +void OpenGLFunctionsBase::InitializeSupportedGLExtensions() { + int32_t numExtensions; + GetIntegerv(GL_NUM_EXTENSIONS, &numExtensions); + + for (int32_t i = 0; i < numExtensions; ++i) { + const char* extensionName = reinterpret_cast(GetStringi(GL_EXTENSIONS, i)); + mSupportedGLExtensionsSet.insert(extensionName); + } +} + +bool OpenGLFunctionsBase::IsGLExtensionSupported(const char* extension) const { + ASSERT(extension != nullptr); + return mSupportedGLExtensionsSet.count(extension) != 0; +} + } // namespace dawn::native::opengl diff --git a/generator/templates/opengl/OpenGLFunctionsBase.h b/generator/templates/opengl/OpenGLFunctionsBase.h index ac313c4f9f..2150a71437 100644 --- a/generator/templates/opengl/OpenGLFunctionsBase.h +++ b/generator/templates/opengl/OpenGLFunctionsBase.h @@ -15,6 +15,8 @@ #ifndef DAWNNATIVE_OPENGL_OPENGLFUNCTIONSBASE_H_ #define DAWNNATIVE_OPENGL_OPENGLFUNCTIONSBASE_H_ +#include + #include "dawn/native/Error.h" #include "dawn/native/opengl/opengl_platform.h" @@ -30,6 +32,7 @@ namespace dawn::native::opengl { {% endfor %} {% endfor%} + bool IsGLExtensionSupported(const char* extension) const; protected: MaybeError LoadDesktopGLProcs(GetProcAddress getProc, int majorVersion, int minorVersion); @@ -38,6 +41,9 @@ namespace dawn::native::opengl { private: template MaybeError LoadProc(GetProcAddress getProc, T* memberProc, const char* name); + void InitializeSupportedGLExtensions(); + + std::unordered_set mSupportedGLExtensionsSet; }; } // namespace dawn::native::opengl diff --git a/src/dawn/native/opengl/OpenGLFunctions.cpp b/src/dawn/native/opengl/OpenGLFunctions.cpp index ccd07202c2..365c9370f9 100644 --- a/src/dawn/native/opengl/OpenGLFunctions.cpp +++ b/src/dawn/native/opengl/OpenGLFunctions.cpp @@ -26,26 +26,9 @@ MaybeError OpenGLFunctions::Initialize(GetProcAddress getProc) { DAWN_TRY(LoadDesktopGLProcs(getProc, mVersion.GetMajor(), mVersion.GetMinor())); } - InitializeSupportedGLExtensions(); - return {}; } -void OpenGLFunctions::InitializeSupportedGLExtensions() { - int32_t numExtensions; - GetIntegerv(GL_NUM_EXTENSIONS, &numExtensions); - - for (int32_t i = 0; i < numExtensions; ++i) { - const char* extensionName = reinterpret_cast(GetStringi(GL_EXTENSIONS, i)); - mSupportedGLExtensionsSet.insert(extensionName); - } -} - -bool OpenGLFunctions::IsGLExtensionSupported(const char* extension) const { - ASSERT(extension != nullptr); - return mSupportedGLExtensionsSet.count(extension) != 0; -} - const OpenGLVersion& OpenGLFunctions::GetVersion() const { return mVersion; } diff --git a/src/dawn/native/opengl/OpenGLFunctions.h b/src/dawn/native/opengl/OpenGLFunctions.h index 4ab6e09644..870669d4cf 100644 --- a/src/dawn/native/opengl/OpenGLFunctions.h +++ b/src/dawn/native/opengl/OpenGLFunctions.h @@ -16,7 +16,6 @@ #define SRC_DAWN_NATIVE_OPENGL_OPENGLFUNCTIONS_H_ #include -#include #include "dawn/native/opengl/OpenGLFunctionsBase_autogen.h" #include "dawn/native/opengl/OpenGLVersion.h" @@ -31,14 +30,8 @@ struct OpenGLFunctions : OpenGLFunctionsBase { bool IsAtLeastGL(uint32_t majorVersion, uint32_t minorVersion) const; bool IsAtLeastGLES(uint32_t majorVersion, uint32_t minorVersion) const; - bool IsGLExtensionSupported(const char* extension) const; - private: - void InitializeSupportedGLExtensions(); - OpenGLVersion mVersion; - - std::unordered_set mSupportedGLExtensionsSet; }; } // namespace dawn::native::opengl