Replace EGL symbol loader

This commit is contained in:
Jérôme Leclercq 2020-09-03 16:33:37 +02:00
parent addcd52552
commit 0255541b44
5 changed files with 86 additions and 58 deletions

View File

@ -12,7 +12,8 @@
#include <Nazara/Platform/WindowHandle.hpp>
#include <Nazara/OpenGLRenderer/Config.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Context.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/EGL/EGLFunctions.hpp>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <string>
#include <type_traits>
#include <unordered_set>

View File

@ -2,30 +2,28 @@
// This file is part of the "Nazara Engine - OpenGL Renderer"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_OPENGLRENDERER_EGLFUNCTIONS_HPP
#define NAZARA_OPENGLRENDERER_EGLFUNCTIONS_HPP
#include <GLES3/gl3.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#define NAZARA_OPENGLRENDERER_FOREACH_EGL_FUNC(func, extBegin, extEnd, extFunc) \
func(eglBindAPI, PFNEGLBINDAPIPROC) \
func(eglChooseConfig, PFNEGLCHOOSECONFIGPROC) \
func(eglCreateContext, PFNEGLCREATECONTEXTPROC) \
func(eglCreatePbufferSurface, PFNEGLCREATEPBUFFERSURFACEPROC) \
func(eglCreateWindowSurface, PFNEGLCREATEWINDOWSURFACEPROC) \
func(eglDestroyContext, PFNEGLDESTROYCONTEXTPROC) \
func(eglDestroySurface, PFNEGLDESTROYSURFACEPROC) \
func(eglGetDisplay, PFNEGLGETDISPLAYPROC) \
func(eglGetError, PFNEGLGETERRORPROC) \
func(eglGetProcAddress, PFNEGLGETPROCADDRESSPROC) \
func(eglInitialize, PFNEGLINITIALIZEPROC) \
func(eglMakeCurrent, PFNEGLMAKECURRENTPROC) \
func(eglQueryString, PFNEGLQUERYSTRINGPROC) \
func(eglSwapBuffers, PFNEGLSWAPBUFFERSPROC) \
func(eglTerminate, PFNEGLTERMINATEPROC)
#if !defined(NAZARA_OPENGLRENDERER_EGL_FUNC) || !defined(NAZARA_OPENGLRENDERER_EGL_FUNC_OPT)
#error You must define NAZARA_OPENGLRENDERER_EGL_FUNC and NAZARA_OPENGLRENDERER_EGL_FUNC_OPT before including this file
#endif
NAZARA_OPENGLRENDERER_EGL_FUNC(eglBindAPI, PFNEGLBINDAPIPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC(eglChooseConfig, PFNEGLCHOOSECONFIGPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC(eglCreateContext, PFNEGLCREATECONTEXTPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC(eglCreatePbufferSurface, PFNEGLCREATEPBUFFERSURFACEPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC(eglCreateWindowSurface, PFNEGLCREATEWINDOWSURFACEPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC(eglDestroyContext, PFNEGLDESTROYCONTEXTPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC(eglDestroySurface, PFNEGLDESTROYSURFACEPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC(eglGetDisplay, PFNEGLGETDISPLAYPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC(eglGetError, PFNEGLGETERRORPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC(eglGetProcAddress, PFNEGLGETPROCADDRESSPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC(eglInitialize, PFNEGLINITIALIZEPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC(eglMakeCurrent, PFNEGLMAKECURRENTPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC(eglQueryString, PFNEGLQUERYSTRINGPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC(eglSwapBuffers, PFNEGLSWAPBUFFERSPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC(eglTerminate, PFNEGLTERMINATEPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC_OPT(eglCreatePlatformWindowSurface, PFNEGLCREATEPLATFORMWINDOWSURFACEPROC)
NAZARA_OPENGLRENDERER_EGL_FUNC_OPT(eglCreatePlatformWindowSurfaceEXT, PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC)
#undef NAZARA_OPENGLRENDERER_EGL_FUNC
#undef NAZARA_OPENGLRENDERER_EGL_FUNC_OPT

View File

@ -11,13 +11,15 @@
#include <Nazara/OpenGLRenderer/Config.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Loader.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/EGL/EGLContextBase.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/EGL/EGLFunctions.hpp>
#include <string>
namespace Nz::GL
{
class NAZARA_OPENGLRENDERER_API EGLLoader : public Loader
{
struct SymbolLoader;
friend SymbolLoader;
public:
EGLLoader();
~EGLLoader();
@ -29,19 +31,16 @@ namespace Nz::GL
GLFunction LoadFunction(const char* name) const override;
#define NAZARA_OPENGLRENDERER_FUNC(name, sig) sig name = nullptr;
#define NAZARA_OPENGLRENDERER_EXT_BEGIN(ext)
#define NAZARA_OPENGLRENDERER_EXT_END()
#define NAZARA_OPENGLRENDERER_EXT_FUNC(name, sig)
NAZARA_OPENGLRENDERER_FOREACH_EGL_FUNC(NAZARA_OPENGLRENDERER_FUNC, NAZARA_OPENGLRENDERER_EXT_BEGIN, NAZARA_OPENGLRENDERER_EXT_END, NAZARA_OPENGLRENDERER_EXT_FUNC)
#undef NAZARA_OPENGLRENDERER_EXT_BEGIN
#undef NAZARA_OPENGLRENDERER_EXT_END
#undef NAZARA_OPENGLRENDERER_EXT_FUNC
#undef NAZARA_OPENGLRENDERER_FUNC
#define NAZARA_OPENGLRENDERER_EGL_FUNC(name, sig) sig name = nullptr;
#define NAZARA_OPENGLRENDERER_EGL_FUNC_OPT(name, sig) NAZARA_OPENGLRENDERER_EGL_FUNC(name, sig)
#include <Nazara/OpenGLRenderer/Wrapper/EGL/EGLFunctions.hpp>
static const char* TranslateError(EGLint errorId);
private:
bool ImplementFallback(const std::string_view& function);
EGLDisplay m_defaultDisplay;
DynLib m_eglLib;
};

View File

@ -321,7 +321,7 @@ namespace Nz::GL
bool EGLContextBase::LoadEGLExt()
{
if (!Activate())
if (!SetCurrentContext(this))
return false;
const char* extensionString = m_loader.eglQueryString(m_display, EGL_EXTENSIONS);

View File

@ -20,32 +20,53 @@
namespace Nz::GL
{
struct EGLLoader::SymbolLoader
{
SymbolLoader(EGLLoader& parent) :
loader(parent)
{
}
template<typename FuncType, typename Func>
bool Load(Func& func, const char* funcName, bool mandatory, bool implementFallback = true)
{
FuncType funcPtr = LoadRaw<FuncType>(funcName);
if (funcPtr)
func = funcPtr;
if (!func)
{
if (!implementFallback || (!loader.ImplementFallback(funcName) && !func)) //< double-check
{
if (mandatory)
throw std::runtime_error("failed to load core function " + std::string(funcName));
}
}
return func != nullptr;
}
template<typename FuncType>
FuncType LoadRaw(const char* funcName)
{
return reinterpret_cast<FuncType>(loader.LoadFunction(funcName));
}
EGLLoader& loader;
};
EGLLoader::EGLLoader() :
m_defaultDisplay(nullptr)
{
if (!m_eglLib.Load("libEGL"))
throw std::runtime_error("failed to load gdi32.dll: " + m_eglLib.GetLastError());
throw std::runtime_error("failed to load libEGL: " + m_eglLib.GetLastError());
auto LoadSymbol = [](DynLib& lib, auto& func, const char* funcName)
{
func = reinterpret_cast<std::decay_t<decltype(func)>>(lib.GetSymbol(funcName));
if (!func)
throw std::runtime_error("failed to load core function " + std::string(funcName));
};
SymbolLoader loader(*this);
// Load gdi32 functions
#define NAZARA_OPENGLRENDERER_EXT_BEGIN(ext)
#define NAZARA_OPENGLRENDERER_EXT_END()
#define NAZARA_OPENGLRENDERER_EXT_FUNC(name, sig) //< Ignore extensions
#define NAZARA_OPENGLRENDERER_EGL_FUNC(name, sig) loader.Load<sig>(name, #name, true);
#define NAZARA_OPENGLRENDERER_EGL_FUNC_OPT(name, sig) loader.Load<sig>(name, #name, false);
// Load base WGL functions
#define NAZARA_OPENGLRENDERER_FUNC(name, sig) LoadSymbol(m_eglLib, name, #name);
NAZARA_OPENGLRENDERER_FOREACH_EGL_FUNC(NAZARA_OPENGLRENDERER_FUNC, NAZARA_OPENGLRENDERER_EXT_BEGIN, NAZARA_OPENGLRENDERER_EXT_END, NAZARA_OPENGLRENDERER_EXT_FUNC)
#undef NAZARA_OPENGLRENDERER_FUNC
#undef NAZARA_OPENGLRENDERER_EXT_BEGIN
#undef NAZARA_OPENGLRENDERER_EXT_END
#undef NAZARA_OPENGLRENDERER_EXT_FUNC
#include <Nazara/OpenGLRenderer/Wrapper/EGL/EGLFunctions.hpp>
EGLDisplay defaultDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (!defaultDisplay)
@ -151,7 +172,11 @@ namespace Nz::GL
GLFunction EGLLoader::LoadFunction(const char* name) const
{
return eglGetProcAddress(name);
GLFunction func = reinterpret_cast<GLFunction>(m_eglLib.GetSymbol(name));
if (!func && eglGetProcAddress)
func = reinterpret_cast<GLFunction>(eglGetProcAddress(name));
return func;
}
const char* EGLLoader::TranslateError(EGLint errorId)
@ -176,4 +201,9 @@ namespace Nz::GL
default: return "Invalid or unknown error.";
}
}
bool EGLLoader::ImplementFallback(const std::string_view& function)
{
return false;
}
}