OpenGLRenderer: Improve OpenGL wrapper

This commit is contained in:
Jérôme Leclercq 2021-09-21 16:34:34 +02:00
parent 250044b47a
commit 78358337f3
12 changed files with 57 additions and 18 deletions

View File

@ -19,7 +19,7 @@ namespace Nz::GL
friend DeviceObject;
public:
Buffer() = default;
using DeviceObject::DeviceObject;
Buffer(const Buffer&) = delete;
Buffer(Buffer&&) noexcept = default;
~Buffer() = default;

View File

@ -19,6 +19,7 @@ namespace Nz::GL
{
public:
ContextObject() = default;
ContextObject(const Context& context, CreateArgs... args);
ContextObject(const ContextObject&) = delete;
ContextObject(ContextObject&& object) noexcept = default;
~ContextObject();
@ -26,6 +27,8 @@ namespace Nz::GL
bool Create(const Context& context, CreateArgs... args);
void Destroy();
const Context& EnsureContext() const;
bool IsValid() const;
const Context* GetContext() const;
@ -39,8 +42,6 @@ namespace Nz::GL
static constexpr GLuint InvalidObject = 0;
protected:
void EnsureContext();
MovablePtr<const Context> m_context;
MovableValue<GLuint> m_objectId;
};

View File

@ -4,11 +4,19 @@
#include <Nazara/OpenGLRenderer/Wrapper/ContextObject.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/ErrorFlags.hpp>
#include <Nazara/OpenGLRenderer/Utils.hpp>
#include <Nazara/OpenGLRenderer/Debug.hpp>
namespace Nz::GL
{
template<typename C, GLenum ObjectType, typename... CreateArgs>
ContextObject<C, ObjectType, CreateArgs...>::ContextObject(const Context& context, CreateArgs... args)
{
ErrorFlags errFlags(ErrorMode::ThrowException);
Create(context, args...);
}
template<typename C, GLenum ObjectType, typename... CreateArgs>
ContextObject<C, ObjectType, CreateArgs...>::~ContextObject()
{
@ -46,6 +54,19 @@ namespace Nz::GL
}
}
template<typename C, GLenum ObjectType, typename... CreateArgs>
const Context& ContextObject<C, ObjectType, CreateArgs...>::EnsureContext() const
{
const Context* activeContext = Context::GetCurrentContext();
if (activeContext != m_context.Get())
{
if (!Context::SetCurrentContext(m_context.Get()))
throw std::runtime_error("failed to activate context");
}
return *m_context;
}
template<typename C, GLenum ObjectType, typename... CreateArgs>
bool ContextObject<C, ObjectType, CreateArgs...>::IsValid() const
{
@ -72,17 +93,6 @@ namespace Nz::GL
if (m_context->glObjectLabel)
m_context->glObjectLabel(ObjectType, m_objectId, name.size(), name.data());
}
template<typename C, GLenum ObjectType, typename... CreateArgs>
void ContextObject<C, ObjectType, CreateArgs...>::EnsureContext()
{
const Context* activeContext = Context::GetCurrentContext();
if (activeContext != m_context.Get())
{
if (!Context::SetCurrentContext(m_context.Get()))
throw std::runtime_error("failed to activate context");
}
}
}
#include <Nazara/OpenGLRenderer/DebugOff.hpp>

View File

@ -20,6 +20,7 @@ namespace Nz::GL
{
public:
DeviceObject() = default;
DeviceObject(OpenGLDevice& device, CreateArgs... args);
DeviceObject(const DeviceObject&) = delete;
DeviceObject(DeviceObject&& object) noexcept = default;
~DeviceObject();

View File

@ -4,12 +4,20 @@
#include <Nazara/OpenGLRenderer/Wrapper/DeviceObject.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/ErrorFlags.hpp>
#include <Nazara/OpenGLRenderer/OpenGLDevice.hpp>
#include <Nazara/OpenGLRenderer/Utils.hpp>
#include <Nazara/OpenGLRenderer/Debug.hpp>
namespace Nz::GL
{
template<typename C, GLenum ObjectType, typename... CreateArgs>
DeviceObject<C, ObjectType, CreateArgs...>::DeviceObject(OpenGLDevice& device, CreateArgs... args)
{
ErrorFlags errFlags(ErrorMode::ThrowException);
Create(device, args...);
}
template<typename C, GLenum ObjectType, typename... CreateArgs>
DeviceObject<C, ObjectType, CreateArgs...>::~DeviceObject()
{

View File

@ -19,7 +19,7 @@ namespace Nz::GL
friend DeviceObject;
public:
Program() = default;
using DeviceObject::DeviceObject;
Program(const Program&) = delete;
Program(Program&&) noexcept = default;
~Program() = default;

View File

@ -19,7 +19,7 @@ namespace Nz::GL
friend DeviceObject;
public:
Sampler() = default;
using DeviceObject::DeviceObject;
Sampler(const Sampler&) = delete;
Sampler(Sampler&&) noexcept = default;
~Sampler() = default;

View File

@ -19,7 +19,7 @@ namespace Nz::GL
friend DeviceObject;
public:
Shader() = default;
using DeviceObject::DeviceObject;
Shader(const Shader&) = delete;
Shader(Shader&&) noexcept = default;
~Shader() = default;
@ -30,6 +30,7 @@ namespace Nz::GL
inline void SetBinarySource(GLenum binaryFormat, const void* binary, GLsizei length);
inline void SetSource(const char* source, GLint length);
inline void SetSource(const std::string_view& source);
// GL_ARB_gl_spirv
inline void SpecializeShader(const GLchar* pEntryPoint, GLuint numSpecializationConstants, const GLuint* pConstantIndex, const GLuint* pConstantValue);

View File

@ -59,6 +59,11 @@ namespace Nz::GL
m_device->GetReferenceContext().glShaderSource(m_objectId, 1U, &source, &length);
}
inline void Shader::SetSource(const std::string_view& source)
{
return SetSource(source.data(), GLint(source.size()));
}
inline void Shader::SpecializeShader(const GLchar* pEntryPoint, GLuint numSpecializationConstants, const GLuint* pConstantIndex, const GLuint* pConstantValue)
{
assert(m_objectId);

View File

@ -19,11 +19,13 @@ namespace Nz::GL
friend DeviceObject;
public:
Texture() = default;
using DeviceObject::DeviceObject;
Texture(const Texture&) = delete;
Texture(Texture&&) noexcept = default;
~Texture() = default;
inline TextureTarget GetTarget() const;
inline void SetParameterf(GLenum pname, GLfloat param);
inline void SetParameteri(GLenum pname, GLint param);
inline void SetParameterfv(GLenum pname, const GLfloat* param);

View File

@ -8,6 +8,11 @@
namespace Nz::GL
{
inline TextureTarget Texture::GetTarget() const
{
return m_target;
}
inline void Texture::SetParameterf(GLenum pname, GLfloat param)
{
assert(m_objectId);

View File

@ -20,6 +20,9 @@ namespace Nz
if (!format)
throw std::runtime_error("unsupported texture format");
const GL::Context& context = m_texture.EnsureDeviceContext();
context.ClearErrorStack();
switch (params.type)
{
case ImageType::E1D:
@ -46,6 +49,9 @@ namespace Nz
break;
}
if (!context.DidLastCallSucceed())
throw std::runtime_error("failed to create texture");
m_texture.SetParameteri(GL_TEXTURE_MAX_LEVEL, m_params.mipmapLevel);
m_texture.SetParameteri(GL_TEXTURE_SWIZZLE_R, format->swizzleR);
m_texture.SetParameteri(GL_TEXTURE_SWIZZLE_G, format->swizzleG);