OpenGL: Implement program wrapper

This commit is contained in:
Lynix 2020-04-26 18:22:31 +02:00
parent b7a7c84a89
commit eba0571f03
8 changed files with 128 additions and 90 deletions

View File

@ -41,6 +41,7 @@ namespace Nz
std::unique_ptr<TextureSampler> InstantiateTextureSampler(const TextureSamplerInfo& params) override;
inline void NotifyBufferDestruction(GLuint buffer) const;
inline void NotifyProgramDestruction(GLuint program) const;
inline void NotifySamplerDestruction(GLuint sampler) const;
inline void NotifyTextureDestruction(GLuint texture) const;

View File

@ -18,6 +18,12 @@ namespace Nz
context->NotifyBufferDestruction(buffer);
}
inline void OpenGLDevice::NotifyProgramDestruction(GLuint program) const
{
for (const GL::Context* context : m_contexts)
context->NotifyProgramDestruction(program);
}
inline void OpenGLDevice::NotifySamplerDestruction(GLuint sampler) const
{
for (const GL::Context* context : m_contexts)

View File

@ -105,6 +105,7 @@ namespace Nz::GL
bool Initialize(const ContextParams& params);
inline void NotifyBufferDestruction(GLuint buffer) const;
inline void NotifyProgramDestruction(GLuint program) const;
inline void NotifySamplerDestruction(GLuint sampler) const;
inline void NotifyTextureDestruction(GLuint texture) const;
@ -143,6 +144,7 @@ namespace Nz::GL
std::array<GLuint, UnderlyingCast(BufferTarget::Max) + 1> bufferTargets = { 0 };
std::vector<TextureUnit> textureUnits;
GLuint boundProgram = 0;
UInt32 currentTextureUnit = 0;
};

View File

@ -46,6 +46,12 @@ namespace Nz::GL
}
}
inline void Context::NotifyProgramDestruction(GLuint program) const
{
if (m_state.boundProgram == program)
m_state.boundProgram = 0;
}
inline void Context::NotifySamplerDestruction(GLuint sampler) const
{
for (auto& unit : m_state.textureUnits)

View File

@ -0,0 +1,44 @@
// 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_OPENGLRENDERER_GLSHADER_HPP
#define NAZARA_OPENGLRENDERER_GLSHADER_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/MovableValue.hpp>
#include <Nazara/OpenGLRenderer/OpenGLDevice.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/DeviceObject.hpp>
namespace Nz::GL
{
class Program : public DeviceObject<Program, GL_PROGRAM>
{
friend DeviceObject;
public:
Program() = default;
Program(const Program&) = delete;
Program(Program&&) noexcept = default;
~Program() = default;
inline void AttachShader(GLuint shader);
inline bool GetLinkStatus(std::string* error = nullptr);
inline void Link();
Program& operator=(const Program&) = delete;
Program& operator=(Program&&) noexcept = default;
private:
static inline GLuint CreateHelper(OpenGLDevice& device, const Context& context);
static inline void DestroyHelper(OpenGLDevice& device, const Context& context, GLuint objectId);
};
}
#include <Nazara/OpenGLRenderer/Wrapper/Program.inl>
#endif

View File

@ -0,0 +1,69 @@
// 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/Wrapper/Program.hpp>
#include <cassert>
#include <Nazara/OpenGLRenderer/Debug.hpp>
namespace Nz::GL
{
inline void Program::AttachShader(GLuint shader)
{
assert(m_objectId);
const Context& context = EnsureDeviceContext();
context.glAttachShader(m_objectId, shader);
}
inline bool Program::GetLinkStatus(std::string* error)
{
assert(m_objectId);
const Context& context = EnsureDeviceContext();
GLint success;
context.glGetProgramiv(m_objectId, GL_LINK_STATUS, &success);
if (!success)
{
if (error)
{
GLint logLength;
context.glGetProgramiv(m_objectId, GL_INFO_LOG_LENGTH, &logLength);
error->resize(logLength);
if (logLength > 0)
{
GLsizei dummy;
context.glGetProgramInfoLog(m_objectId, logLength, &dummy, error->data());
}
}
return false;
}
return true;
}
inline void Program::Link()
{
assert(m_objectId);
const Context& context = EnsureDeviceContext();
context.glLinkProgram(m_objectId);
}
inline GLuint Program::CreateHelper(OpenGLDevice& /*device*/, const Context& context)
{
return context.glCreateProgram();
}
inline void Program::DestroyHelper(OpenGLDevice& device, const Context& context, GLuint objectId)
{
context.glDeleteProgram(objectId);
device.NotifyProgramDestruction(objectId);
}
}
#include <Nazara/OpenGLRenderer/DebugOff.hpp>

View File

@ -1,43 +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_OPENGLRENDERER_VKIMAGE_HPP
#define NAZARA_OPENGLRENDERER_VKIMAGE_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/OpenGLRenderer/Wrapper/DeviceObject.hpp>
namespace Nz
{
namespace Vk
{
class Image : public DeviceObject<Image, VkImage, VkImageCreateInfo, VK_OBJECT_TYPE_IMAGE>
{
friend DeviceObject;
public:
Image() = default;
Image(const Image&) = delete;
Image(Image&&) = default;
~Image() = default;
bool BindImageMemory(VkDeviceMemory memory, VkDeviceSize offset = 0);
VkMemoryRequirements GetMemoryRequirements() const;
Image& operator=(const Image&) = delete;
Image& operator=(Image&&) = delete;
private:
static inline VkResult CreateHelper(Device& device, const VkImageCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkImage* handle);
static inline void DestroyHelper(Device& device, VkImage handle, const VkAllocationCallbacks* allocator);
};
}
}
#include <Nazara/OpenGLRenderer/Wrapper/Image.inl>
#endif // NAZARA_OPENGLRENDERER_VKIMAGE_HPP

View File

@ -1,47 +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/Wrapper/Image.hpp>
#include <Nazara/OpenGLRenderer/Utils.hpp>
#include <Nazara/OpenGLRenderer/Debug.hpp>
namespace Nz
{
namespace Vk
{
inline bool Image::BindImageMemory(VkDeviceMemory memory, VkDeviceSize offset)
{
m_lastErrorCode = m_device->vkBindImageMemory(*m_device, m_handle, memory, offset);
if (m_lastErrorCode != VK_SUCCESS)
{
NazaraError("Failed to bind image memory: " + TranslateOpenGLError(m_lastErrorCode));
return false;
}
return true;
}
inline VkMemoryRequirements Image::GetMemoryRequirements() const
{
NazaraAssert(IsValid(), "Invalid image");
VkMemoryRequirements memoryRequirements;
m_device->vkGetImageMemoryRequirements(*m_device, m_handle, &memoryRequirements);
return memoryRequirements;
}
inline VkResult Image::CreateHelper(Device& device, const VkImageCreateInfo* createInfo, const VkAllocationCallbacks* allocator, VkImage* handle)
{
return device.vkCreateImage(device, createInfo, allocator, handle);
}
inline void Image::DestroyHelper(Device& device, VkImage handle, const VkAllocationCallbacks* allocator)
{
return device.vkDestroyImage(device, handle, allocator);
}
}
}
#include <Nazara/OpenGLRenderer/DebugOff.hpp>