OpenGLRenderer: Get rid of std::function by using function indexes

This commit is contained in:
Jérôme Leclercq 2020-09-03 13:54:44 +02:00
parent 6848ff8b34
commit 0609a10c25
4 changed files with 45 additions and 28 deletions

View File

@ -9,16 +9,16 @@
#include <Nazara/Prerequisites.hpp> #include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Algorithm.hpp> #include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Core/DynLib.hpp>
#include <Nazara/OpenGLRenderer/Config.hpp> #include <Nazara/OpenGLRenderer/Config.hpp>
#include <Nazara/OpenGLRenderer/OpenGLVaoCache.hpp> #include <Nazara/OpenGLRenderer/OpenGLVaoCache.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/CoreFunctions.hpp> #include <Nazara/OpenGLRenderer/Wrapper/CoreFunctions.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Loader.hpp>
#include <Nazara/Renderer/RenderStates.hpp> #include <Nazara/Renderer/RenderStates.hpp>
#include <array> #include <array>
#include <string> #include <string>
#include <unordered_set> #include <unordered_set>
#define NAZARA_OPENGLRENDERER_DEBUG 1
namespace Nz namespace Nz
{ {
class OpenGLDevice; class OpenGLDevice;
@ -91,8 +91,6 @@ namespace Nz::GL
unsigned int stencilBits = 8; unsigned int stencilBits = 8;
}; };
class Loader;
class NAZARA_OPENGLRENDERER_API Context class NAZARA_OPENGLRENDERER_API Context
{ {
struct SymbolLoader; struct SymbolLoader;
@ -118,6 +116,7 @@ namespace Nz::GL
inline const OpenGLDevice* GetDevice() const; inline const OpenGLDevice* GetDevice() const;
inline ExtensionStatus GetExtensionStatus(Extension extension) const; inline ExtensionStatus GetExtensionStatus(Extension extension) const;
inline GLFunction GetFunctionByIndex(std::size_t funcIndex) const;
inline const OpenGLVaoCache& GetVaoCache() const; inline const OpenGLVaoCache& GetVaoCache() const;
inline const ContextParams& GetParams() const; inline const ContextParams& GetParams() const;
@ -142,11 +141,7 @@ namespace Nz::GL
void UpdateStates(const RenderStates& renderStates) const; void UpdateStates(const RenderStates& renderStates) const;
#if NAZARA_OPENGLRENDERER_DEBUG
#define NAZARA_OPENGLRENDERER_FUNC(name, sig) std::function<std::remove_pointer_t<sig>> name;
#else
#define NAZARA_OPENGLRENDERER_FUNC(name, sig) sig name = nullptr; #define NAZARA_OPENGLRENDERER_FUNC(name, sig) sig name = nullptr;
#endif
NAZARA_OPENGLRENDERER_FOREACH_GLES_FUNC(NAZARA_OPENGLRENDERER_FUNC, NAZARA_OPENGLRENDERER_FUNC) NAZARA_OPENGLRENDERER_FOREACH_GLES_FUNC(NAZARA_OPENGLRENDERER_FUNC, NAZARA_OPENGLRENDERER_FUNC)
#undef NAZARA_OPENGLRENDERER_FUNC #undef NAZARA_OPENGLRENDERER_FUNC
@ -168,6 +163,15 @@ namespace Nz::GL
private: private:
void GL_APIENTRY HandleDebugMessage(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message) const; void GL_APIENTRY HandleDebugMessage(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message) const;
enum class FunctionIndex
{
#define NAZARA_OPENGLRENDERER_FUNC(name, sig) name,
NAZARA_OPENGLRENDERER_FOREACH_GLES_FUNC(NAZARA_OPENGLRENDERER_FUNC, NAZARA_OPENGLRENDERER_FUNC)
#undef NAZARA_OPENGLRENDERER_FUNC
Count
};
struct State struct State
{ {
struct Box struct Box
@ -203,6 +207,7 @@ namespace Nz::GL
}; };
std::array<ExtensionStatus, UnderlyingCast(Extension::Max) + 1> m_extensionStatus; std::array<ExtensionStatus, UnderlyingCast(Extension::Max) + 1> m_extensionStatus;
std::array<GLFunction, UnderlyingCast(FunctionIndex::Count)> m_originalFunctionPointer;
std::unordered_set<std::string> m_supportedExtensions; std::unordered_set<std::string> m_supportedExtensions;
OpenGLVaoCache m_vaoCache; OpenGLVaoCache m_vaoCache;
const OpenGLDevice* m_device; const OpenGLDevice* m_device;

View File

@ -23,6 +23,12 @@ namespace Nz::GL
return m_extensionStatus[UnderlyingCast(extension)]; return m_extensionStatus[UnderlyingCast(extension)];
} }
inline GLFunction Context::GetFunctionByIndex(std::size_t funcIndex) const
{
assert(funcIndex < m_originalFunctionPointer.size());
return m_originalFunctionPointer[funcIndex];
}
inline const OpenGLVaoCache& Context::GetVaoCache() const inline const OpenGLVaoCache& Context::GetVaoCache() const
{ {
return m_vaoCache; return m_vaoCache;

View File

@ -10,7 +10,6 @@
#include <Nazara/Prerequisites.hpp> #include <Nazara/Prerequisites.hpp>
#include <Nazara/Platform/WindowHandle.hpp> #include <Nazara/Platform/WindowHandle.hpp>
#include <Nazara/OpenGLRenderer/Config.hpp> #include <Nazara/OpenGLRenderer/Config.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Context.hpp>
#include <memory> #include <memory>
namespace Nz namespace Nz
@ -20,10 +19,11 @@ namespace Nz
namespace Nz::GL namespace Nz::GL
{ {
class Context;
using GLFunction = void(*)(void); using GLFunction = void(*)(void);
class Context;
struct ContextParams;
class NAZARA_OPENGLRENDERER_API Loader class NAZARA_OPENGLRENDERER_API Loader
{ {
public: public:

View File

@ -14,6 +14,8 @@
#include <stdexcept> #include <stdexcept>
#include <Nazara/OpenGLRenderer/Debug.hpp> #include <Nazara/OpenGLRenderer/Debug.hpp>
#define NAZARA_OPENGLRENDERER_DEBUG 1
namespace Nz::GL namespace Nz::GL
{ {
thread_local const Context* s_currentContext = nullptr; thread_local const Context* s_currentContext = nullptr;
@ -26,14 +28,16 @@ namespace Nz::GL
template<typename Ret, typename... Args> template<typename Ret, typename... Args>
struct GLWrapper<Ret(Args...)> struct GLWrapper<Ret(Args...)>
{ {
template<typename FuncType> template<typename FuncType, std::size_t FuncIndex>
static auto WrapErrorHandling(FuncType funcPtr) static auto WrapErrorHandling()
{ {
return [funcPtr](Args&&... args) -> Ret return [](Args... args) -> Ret
{ {
const Context* context = s_currentContext; //< pay TLS cost once const Context* context = s_currentContext; //< pay TLS cost once
assert(context); assert(context);
FuncType funcPtr = reinterpret_cast<FuncType>(context->GetFunctionByIndex(FuncIndex));
context->ClearErrorStack(); context->ClearErrorStack();
if constexpr (std::is_same_v<Ret, void>) if constexpr (std::is_same_v<Ret, void>)
@ -62,21 +66,19 @@ namespace Nz::GL
{ {
} }
template<typename FuncType, typename Func> template<typename FuncType, std::size_t FuncIndex, typename Func>
bool Load(Func& func, const char* funcName, bool mandatory, bool implementFallback = true) bool Load(Func& func, const char* funcName, bool mandatory, bool implementFallback = true)
{ {
FuncType funcPtr = LoadRaw<FuncType>(funcName); FuncType originalFunc = LoadRaw<FuncType>(funcName);
if (funcPtr) func = originalFunc;
{
#if NAZARA_OPENGLRENDERER_DEBUG #if NAZARA_OPENGLRENDERER_DEBUG
if (originalFunc)
{
if (std::strcmp(funcName, "glGetError") != 0) //< Prevent infinite recursion if (std::strcmp(funcName, "glGetError") != 0) //< Prevent infinite recursion
func = GLWrapper<std::remove_pointer_t<FuncType>>::template WrapErrorHandling(funcPtr); func = GLWrapper<std::remove_pointer_t<FuncType>>::template WrapErrorHandling<FuncType, FuncIndex>();
else
func = funcPtr;
#else
func = funcPtr;
#endif
} }
#endif
if (!func) if (!func)
{ {
@ -87,6 +89,8 @@ namespace Nz::GL
} }
} }
context.m_originalFunctionPointer[FuncIndex] = reinterpret_cast<GLFunction>(originalFunc);
return func != nullptr; return func != nullptr;
} }
@ -249,7 +253,7 @@ namespace Nz::GL
try try
{ {
#define NAZARA_OPENGLRENDERER_FUNC(name, sig) loader.Load<sig>(name, #name, true); #define NAZARA_OPENGLRENDERER_FUNC(name, sig) loader.Load<sig, UnderlyingCast(FunctionIndex:: name)>(name, #name, true);
#define NAZARA_OPENGLRENDERER_EXT_FUNC(name, sig) //< Do nothing #define NAZARA_OPENGLRENDERER_EXT_FUNC(name, sig) //< Do nothing
NAZARA_OPENGLRENDERER_FOREACH_GLES_FUNC(NAZARA_OPENGLRENDERER_FUNC, NAZARA_OPENGLRENDERER_EXT_FUNC) NAZARA_OPENGLRENDERER_FOREACH_GLES_FUNC(NAZARA_OPENGLRENDERER_FUNC, NAZARA_OPENGLRENDERER_EXT_FUNC)
#undef NAZARA_OPENGLRENDERER_EXT_FUNC #undef NAZARA_OPENGLRENDERER_EXT_FUNC
@ -299,7 +303,7 @@ namespace Nz::GL
m_extensionStatus[UnderlyingCast(Extension::SpirV)] = ExtensionStatus::ARB; m_extensionStatus[UnderlyingCast(Extension::SpirV)] = ExtensionStatus::ARB;
#define NAZARA_OPENGLRENDERER_FUNC(name, sig) #define NAZARA_OPENGLRENDERER_FUNC(name, sig)
#define NAZARA_OPENGLRENDERER_EXT_FUNC(name, sig) loader.Load<sig>(name, #name, false); #define NAZARA_OPENGLRENDERER_EXT_FUNC(name, sig) loader.Load<sig, UnderlyingCast(FunctionIndex:: name)>(name, #name, false);
NAZARA_OPENGLRENDERER_FOREACH_GLES_FUNC(NAZARA_OPENGLRENDERER_FUNC, NAZARA_OPENGLRENDERER_EXT_FUNC) NAZARA_OPENGLRENDERER_FOREACH_GLES_FUNC(NAZARA_OPENGLRENDERER_FUNC, NAZARA_OPENGLRENDERER_EXT_FUNC)
#undef NAZARA_OPENGLRENDERER_EXT_FUNC #undef NAZARA_OPENGLRENDERER_EXT_FUNC
#undef NAZARA_OPENGLRENDERER_FUNC #undef NAZARA_OPENGLRENDERER_FUNC
@ -665,8 +669,10 @@ namespace Nz::GL
if (function == "glDebugMessageCallback") if (function == "glDebugMessageCallback")
{ {
if (!loader.Load<PFNGLDEBUGMESSAGECALLBACKPROC>(glDebugMessageCallback, "glDebugMessageCallbackARB", false, false)) constexpr std::size_t functionIndex = UnderlyingCast(FunctionIndex::glDebugMessageCallback);
return loader.Load<PFNGLDEBUGMESSAGECALLBACKPROC>(glDebugMessageCallback, "DebugMessageCallbackAMD", false, false);
if (!loader.Load<PFNGLDEBUGMESSAGECALLBACKPROC, functionIndex>(glDebugMessageCallback, "glDebugMessageCallbackARB", false, false))
return loader.Load<PFNGLDEBUGMESSAGECALLBACKPROC, functionIndex>(glDebugMessageCallback, "DebugMessageCallbackAMD", false, false);
return true; return true;
} }