OpenGL: Implement device

This commit is contained in:
Lynix 2020-04-19 01:36:44 +02:00
parent 0fa095e8f7
commit 5c3eb31d4a
7 changed files with 97 additions and 143 deletions

View File

@ -1,35 +0,0 @@
// Copyright (C) 2020 Jérôme Leclercq
// 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_OPENGL_HPP
#define NAZARA_OPENGL_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Initializer.hpp>
#include <Nazara/OpenGLRenderer/Config.hpp>
#include <list>
#include <memory>
#include <vector>
namespace Nz
{
class OpenGLDevice;
class NAZARA_OPENGLRENDERER_API OpenGL
{
public:
OpenGL() = delete;
~OpenGL() = delete;
static bool Initialize();
static bool IsInitialized();
static void Uninitialize();
};
}
#endif // NAZARA_OPENGL_HPP

View File

@ -8,21 +8,27 @@
#define NAZARA_OPENGLRENDERER_OPENGLDEVICE_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Platform/WindowHandle.hpp>
#include <Nazara/OpenGLRenderer/Config.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Context.hpp>
#include <Nazara/Renderer/RenderDevice.hpp>
#include <Nazara/OpenGLRenderer/OpenGLBuffer.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Device.hpp>
#include <vector>
namespace Nz
{
class NAZARA_OPENGLRENDERER_API OpenGLDevice : public RenderDevice, public Vk::Device
class NAZARA_OPENGLRENDERER_API OpenGLDevice : public RenderDevice
{
public:
using Device::Device;
OpenGLDevice(GL::Loader& loader);
OpenGLDevice(const OpenGLDevice&) = delete;
OpenGLDevice(OpenGLDevice&&) = delete; ///TODO?
~OpenGLDevice();
std::unique_ptr<GL::Context> CreateContext(const GL::ContextParams& params) const;
std::unique_ptr<GL::Context> CreateContext(const GL::ContextParams& params, WindowHandle handle) const;
inline const GL::Context& GetReferenceContext() const;
std::unique_ptr<AbstractBuffer> InstantiateBuffer(BufferType type) override;
std::unique_ptr<CommandPool> InstantiateCommandPool(QueueType queueType) override;
std::unique_ptr<RenderPipeline> InstantiateRenderPipeline(RenderPipelineInfo pipelineInfo) override;
@ -33,6 +39,10 @@ namespace Nz
OpenGLDevice& operator=(const OpenGLDevice&) = delete;
OpenGLDevice& operator=(OpenGLDevice&&) = delete; ///TODO?
private:
std::unique_ptr<GL::Context> m_referenceContext;
GL::Loader& m_loader;
};
}

View File

@ -7,6 +7,10 @@
namespace Nz
{
inline const GL::Context& OpenGLDevice::GetReferenceContext() const
{
return *m_referenceContext;
}
}
#include <Nazara/OpenGLRenderer/DebugOff.hpp>

View File

@ -8,10 +8,12 @@
#define NAZARA_OPENGLRENDERER_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/DynLib.hpp>
#include <Nazara/Renderer/RendererImpl.hpp>
#include <Nazara/OpenGLRenderer/Config.hpp>
#include <list>
#include <vector>
#include <Nazara/OpenGLRenderer/OpenGLDevice.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Loader.hpp>
#include <memory>
namespace Nz
{
@ -34,6 +36,11 @@ namespace Nz
std::vector<RenderDeviceInfo> QueryRenderDevices() const override;
bool Prepare(const ParameterList& parameters) override;
private:
DynLib m_opengl32Lib;
std::shared_ptr<OpenGLDevice> m_device;
std::unique_ptr<GL::Loader> m_loader;
};
}

View File

@ -1,72 +0,0 @@
// Copyright (C) 2020 Jérôme Leclercq
// This file is part of the "Nazara Engine - OpenGL Renderer"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/OpenGLRenderer/OpenGL.hpp>
#include <Nazara/Core/DynLib.hpp>
#include <Nazara/Core/Error.hpp>
#ifdef NAZARA_PLATFORM_WINDOWS
#include <Nazara/OpenGLRenderer/Wrapper/Win32/WGLLoader.hpp>
#endif
#include <Nazara/OpenGLRenderer/Debug.hpp>
namespace Nz
{
struct OpenGLImpl
{
DynLib opengl32Lib;
};
static std::unique_ptr<OpenGLImpl> s_impl;
bool OpenGL::Initialize()
{
if (s_impl)
return true;
auto impl = std::make_unique<OpenGLImpl>();
if (!impl->opengl32Lib.Load("opengl32" NAZARA_DYNLIB_EXTENSION))
{
NazaraError("Failed to load opengl32 library, is OpenGL installed on your system?");
return false;
}
std::unique_ptr<GL::Loader> loader;
#ifdef NAZARA_PLATFORM_WINDOWS
try
{
loader = std::make_unique<GL::WGLLoader>(impl->opengl32Lib);
}
catch (const std::exception& e)
{
NazaraWarning(std::string("Failed to load WGL: ") + e.what());
}
#endif
if (!loader)
{
NazaraError("Failed to initialize OpenGL loader");
return false;
}
s_impl = std::move(impl);
return true;
}
bool OpenGL::IsInitialized()
{
return s_impl != nullptr;
}
void OpenGL::Uninitialize()
{
if (!s_impl)
return;
s_impl.reset();
}
}

View File

@ -2,63 +2,68 @@
// This file is part of the "Nazara Engine - OpenGL Renderer"
// For conditions of distribution and use, see copyright notice in Config.hpp
#if 0
#include <Nazara/OpenGLRenderer/OpenGLDevice.hpp>
#include <Nazara/OpenGLRenderer/OpenGLCommandPool.hpp>
#include <Nazara/OpenGLRenderer/OpenGLRenderPipeline.hpp>
#include <Nazara/OpenGLRenderer/OpenGLRenderPipelineLayout.hpp>
#include <Nazara/Renderer/CommandPool.hpp>
#include <Nazara/OpenGLRenderer/OpenGLShaderStage.hpp>
#include <Nazara/OpenGLRenderer/OpenGLTexture.hpp>
#include <Nazara/OpenGLRenderer/OpenGLTextureSampler.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/Loader.hpp>
#include <stdexcept>
#include <Nazara/OpenGLRenderer/Debug.hpp>
namespace Nz
{
OpenGLDevice::OpenGLDevice(GL::Loader& loader) :
m_loader(loader)
{
m_referenceContext = loader.CreateContext({});
if (!m_referenceContext)
throw std::runtime_error("failed to create reference context");
}
OpenGLDevice::~OpenGLDevice() = default;
std::unique_ptr<GL::Context> OpenGLDevice::CreateContext(const GL::ContextParams& params) const
{
return m_loader.CreateContext(params, m_referenceContext.get());
}
std::unique_ptr<GL::Context> OpenGLDevice::CreateContext(const GL::ContextParams& params, WindowHandle handle) const
{
return m_loader.CreateContext(params, handle, m_referenceContext.get());
}
std::unique_ptr<AbstractBuffer> OpenGLDevice::InstantiateBuffer(BufferType type)
{
return std::make_unique<OpenGLBuffer>(*this, type);
return {};
}
std::unique_ptr<CommandPool> OpenGLDevice::InstantiateCommandPool(QueueType queueType)
{
return std::make_unique<OpenGLCommandPool>(*this, queueType);
return {};
}
std::unique_ptr<RenderPipeline> OpenGLDevice::InstantiateRenderPipeline(RenderPipelineInfo pipelineInfo)
{
return std::make_unique<OpenGLRenderPipeline>(*this, std::move(pipelineInfo));
return {};
}
std::shared_ptr<RenderPipelineLayout> OpenGLDevice::InstantiateRenderPipelineLayout(RenderPipelineLayoutInfo pipelineLayoutInfo)
{
auto pipelineLayout = std::make_shared<OpenGLRenderPipelineLayout>();
if (!pipelineLayout->Create(*this, std::move(pipelineLayoutInfo)))
return {};
return pipelineLayout;
return {};
}
std::shared_ptr<ShaderStageImpl> OpenGLDevice::InstantiateShaderStage(ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize)
{
auto stage = std::make_shared<OpenGLShaderStage>();
if (!stage->Create(*this, type, lang, source, sourceSize))
return {};
return stage;
}
std::unique_ptr<Texture> OpenGLDevice::InstantiateTexture(const TextureInfo& params)
{
return std::make_unique<OpenGLTexture>(*this, params);
return {};
}
std::unique_ptr<TextureSampler> OpenGLDevice::InstantiateTextureSampler(const TextureSamplerInfo& params)
{
return std::make_unique<OpenGLTextureSampler>(*this, params);
return {};
}
}
#endif

View File

@ -7,33 +7,39 @@
#include <Nazara/Renderer/RenderDevice.hpp>
#include <Nazara/Renderer/RenderSurface.hpp>
#include <Nazara/Renderer/RenderWindowImpl.hpp>
#include <Nazara/OpenGLRenderer/OpenGL.hpp>
#include <Nazara/OpenGLRenderer/DummySurface.hpp>
#include <Nazara/OpenGLRenderer/OpenGLRenderWindow.hpp>
#include <cassert>
#include <sstream>
#ifdef NAZARA_PLATFORM_WINDOWS
#include <Nazara/OpenGLRenderer/Wrapper/Win32/WGLLoader.hpp>
#endif
#include <Nazara/OpenGLRenderer/Debug.hpp>
namespace Nz
{
OpenGLRenderer::~OpenGLRenderer()
{
OpenGL::Uninitialize();
m_device.reset();
m_loader.reset();
}
std::unique_ptr<RenderSurface> OpenGLRenderer::CreateRenderSurfaceImpl()
{
return {};
return std::make_unique<DummySurface>();
}
std::unique_ptr<RenderWindowImpl> OpenGLRenderer::CreateRenderWindowImpl()
{
return {};
return std::make_unique<OpenGLRenderWindow>();
}
std::shared_ptr<RenderDevice> OpenGLRenderer::InstanciateRenderDevice(std::size_t deviceIndex)
{
//assert(deviceIndex < m_physDevices.size());
//return OpenGL::SelectDevice(m_physDevices[deviceIndex]);
return {};
assert(deviceIndex == 0);
return m_device;
}
bool OpenGLRenderer::IsBetterThan(const RendererImpl* other) const
@ -46,7 +52,36 @@ namespace Nz
bool OpenGLRenderer::Prepare(const ParameterList& parameters)
{
return OpenGL::Initialize();
if (!m_opengl32Lib.Load("opengl32" NAZARA_DYNLIB_EXTENSION))
{
NazaraError("Failed to load opengl32 library, is OpenGL installed on your system?");
return false;
}
std::unique_ptr<GL::Loader> loader;
#ifdef NAZARA_PLATFORM_WINDOWS
try
{
loader = std::make_unique<GL::WGLLoader>(m_opengl32Lib);
}
catch (const std::exception& e)
{
NazaraWarning(std::string("Failed to load WGL: ") + e.what());
}
#endif
if (!loader)
{
NazaraError("Failed to initialize OpenGL loader");
return false;
}
m_loader = std::move(loader);
m_device = std::make_shared<OpenGLDevice>(*m_loader);
return true;
}
RenderAPI OpenGLRenderer::QueryAPI() const