OpenGL: Link contexts to device

This commit is contained in:
Lynix
2020-04-19 15:33:36 +02:00
parent bd6924d66d
commit 349e915e10
12 changed files with 78 additions and 21 deletions

View File

@@ -14,21 +14,29 @@ namespace Nz
OpenGLDevice::OpenGLDevice(GL::Loader& loader) :
m_loader(loader)
{
m_referenceContext = loader.CreateContext({});
m_referenceContext = loader.CreateContext(this, {});
if (!m_referenceContext)
throw std::runtime_error("failed to create reference context");
m_contexts.insert(m_referenceContext.get());
}
OpenGLDevice::~OpenGLDevice() = default;
std::unique_ptr<GL::Context> OpenGLDevice::CreateContext(const GL::ContextParams& params) const
{
return m_loader.CreateContext(params, m_referenceContext.get());
auto contextPtr = m_loader.CreateContext(this, params, m_referenceContext.get());
m_contexts.insert(contextPtr.get());
return contextPtr;
}
std::unique_ptr<GL::Context> OpenGLDevice::CreateContext(const GL::ContextParams& params, WindowHandle handle) const
{
return m_loader.CreateContext(params, handle, m_referenceContext.get());
auto contextPtr = m_loader.CreateContext(this, params, handle, m_referenceContext.get());
m_contexts.insert(contextPtr.get());
return contextPtr;
}
std::unique_ptr<AbstractBuffer> OpenGLDevice::InstantiateBuffer(BufferType type)

View File

@@ -4,8 +4,11 @@
#include <Nazara/OpenGLRenderer/Wrapper/Context.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/Log.hpp>
#include <Nazara/Core/StringExt.hpp>
#include <Nazara/OpenGLRenderer/OpenGLDevice.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Loader.hpp>
#include <sstream>
#include <stdexcept>
#include <Nazara/OpenGLRenderer/Debug.hpp>
@@ -13,6 +16,12 @@ namespace Nz::GL
{
thread_local const Context* s_currentContext = nullptr;
Context::~Context()
{
if (m_device)
m_device->NotifyContextDestruction(*this);
}
bool Context::Initialize(const ContextParams& params)
{

View File

@@ -12,11 +12,6 @@
namespace Nz::GL
{
GL::WGLContext::WGLContext(const WGLLoader& loader) :
m_loader(loader)
{
}
WGLContext::~WGLContext()
{
Destroy();

View File

@@ -11,7 +11,7 @@ namespace Nz::GL
{
WGLLoader::WGLLoader(DynLib& openglLib) :
m_opengl32Lib(openglLib),
m_baseContext(*this)
m_baseContext(nullptr, *this)
{
if (!m_gdi32Lib.Load("gdi32.dll"))
throw std::runtime_error("failed to load gdi32.dll: " + m_gdi32Lib.GetLastError());
@@ -42,7 +42,7 @@ namespace Nz::GL
#undef NAZARA_OPENGLRENDERER_EXT_FUNC
// In order to load OpenGL functions, we have to create a context first
WGLContext loadContext(*this);
WGLContext loadContext(nullptr, *this);
if (!loadContext.Create(nullptr, {}))
throw std::runtime_error("failed to create load context");
@@ -56,9 +56,9 @@ namespace Nz::GL
throw std::runtime_error("failed to load OpenGL functions");
}
std::unique_ptr<Context> WGLLoader::CreateContext(const ContextParams& params, Context* shareContext) const
std::unique_ptr<Context> WGLLoader::CreateContext(const OpenGLDevice* device, const ContextParams& params, Context* shareContext) const
{
auto context = std::make_unique<WGLContext>(*this);
auto context = std::make_unique<WGLContext>(device, *this);
if (!context->Create(&m_baseContext, params, static_cast<WGLContext*>(shareContext)))
{
NazaraError("failed to create context");
@@ -74,9 +74,9 @@ namespace Nz::GL
return context;
}
std::unique_ptr<Context> WGLLoader::CreateContext(const ContextParams& params, WindowHandle handle, Context* shareContext) const
std::unique_ptr<Context> WGLLoader::CreateContext(const OpenGLDevice* device, const ContextParams& params, WindowHandle handle, Context* shareContext) const
{
auto context = std::make_unique<WGLContext>(*this);
auto context = std::make_unique<WGLContext>(device, *this);
if (!context->Create(&m_baseContext, params, handle, static_cast<WGLContext*>(shareContext)))
{
NazaraError("failed to create context");