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/Core/Algorithm.hpp>
#include <Nazara/Core/DynLib.hpp>
#include <Nazara/OpenGLRenderer/Config.hpp>
#include <Nazara/OpenGLRenderer/OpenGLVaoCache.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/CoreFunctions.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Loader.hpp>
#include <Nazara/Renderer/RenderStates.hpp>
#include <array>
#include <string>
#include <unordered_set>
#define NAZARA_OPENGLRENDERER_DEBUG 1
namespace Nz
{
class OpenGLDevice;
@ -91,8 +91,6 @@ namespace Nz::GL
unsigned int stencilBits = 8;
};
class Loader;
class NAZARA_OPENGLRENDERER_API Context
{
struct SymbolLoader;
@ -118,6 +116,7 @@ namespace Nz::GL
inline const OpenGLDevice* GetDevice() const;
inline ExtensionStatus GetExtensionStatus(Extension extension) const;
inline GLFunction GetFunctionByIndex(std::size_t funcIndex) const;
inline const OpenGLVaoCache& GetVaoCache() const;
inline const ContextParams& GetParams() const;
@ -142,11 +141,7 @@ namespace Nz::GL
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;
#endif
NAZARA_OPENGLRENDERER_FOREACH_GLES_FUNC(NAZARA_OPENGLRENDERER_FUNC, NAZARA_OPENGLRENDERER_FUNC)
#undef NAZARA_OPENGLRENDERER_FUNC
@ -168,6 +163,15 @@ namespace Nz::GL
private:
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 Box
@ -203,6 +207,7 @@ namespace Nz::GL
};
std::array<ExtensionStatus, UnderlyingCast(Extension::Max) + 1> m_extensionStatus;
std::array<GLFunction, UnderlyingCast(FunctionIndex::Count)> m_originalFunctionPointer;
std::unordered_set<std::string> m_supportedExtensions;
OpenGLVaoCache m_vaoCache;
const OpenGLDevice* m_device;

View File

@ -23,6 +23,12 @@ namespace Nz::GL
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
{
return m_vaoCache;

View File

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

View File

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