diff --git a/include/Nazara/OpenGLRenderer/OpenGLDevice.hpp b/include/Nazara/OpenGLRenderer/OpenGLDevice.hpp index 52effa121..2bbb95c33 100644 --- a/include/Nazara/OpenGLRenderer/OpenGLDevice.hpp +++ b/include/Nazara/OpenGLRenderer/OpenGLDevice.hpp @@ -41,6 +41,7 @@ namespace Nz std::unique_ptr 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; diff --git a/include/Nazara/OpenGLRenderer/OpenGLDevice.inl b/include/Nazara/OpenGLRenderer/OpenGLDevice.inl index 66219abc5..a9c823a7e 100644 --- a/include/Nazara/OpenGLRenderer/OpenGLDevice.inl +++ b/include/Nazara/OpenGLRenderer/OpenGLDevice.inl @@ -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) diff --git a/include/Nazara/OpenGLRenderer/Wrapper/Context.hpp b/include/Nazara/OpenGLRenderer/Wrapper/Context.hpp index 50eea5c7b..d18843c08 100644 --- a/include/Nazara/OpenGLRenderer/Wrapper/Context.hpp +++ b/include/Nazara/OpenGLRenderer/Wrapper/Context.hpp @@ -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 bufferTargets = { 0 }; std::vector textureUnits; + GLuint boundProgram = 0; UInt32 currentTextureUnit = 0; }; diff --git a/include/Nazara/OpenGLRenderer/Wrapper/Context.inl b/include/Nazara/OpenGLRenderer/Wrapper/Context.inl index b7d5139a2..bf2dbe86b 100644 --- a/include/Nazara/OpenGLRenderer/Wrapper/Context.inl +++ b/include/Nazara/OpenGLRenderer/Wrapper/Context.inl @@ -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) diff --git a/include/Nazara/OpenGLRenderer/Wrapper/Program.hpp b/include/Nazara/OpenGLRenderer/Wrapper/Program.hpp new file mode 100644 index 000000000..5bdae0efc --- /dev/null +++ b/include/Nazara/OpenGLRenderer/Wrapper/Program.hpp @@ -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 +#include +#include +#include + +namespace Nz::GL +{ + class Program : public DeviceObject + { + 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 + +#endif diff --git a/include/Nazara/OpenGLRenderer/Wrapper/Program.inl b/include/Nazara/OpenGLRenderer/Wrapper/Program.inl new file mode 100644 index 000000000..ca8795d70 --- /dev/null +++ b/include/Nazara/OpenGLRenderer/Wrapper/Program.inl @@ -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 +#include +#include + +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 diff --git a/include/Nazara/OpenGLRenderer/Wrapper/ProgramPipeline.hpp b/include/Nazara/OpenGLRenderer/Wrapper/ProgramPipeline.hpp deleted file mode 100644 index 37d83bf40..000000000 --- a/include/Nazara/OpenGLRenderer/Wrapper/ProgramPipeline.hpp +++ /dev/null @@ -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 -#include - -namespace Nz -{ - namespace Vk - { - class Image : public DeviceObject - { - 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 - -#endif // NAZARA_OPENGLRENDERER_VKIMAGE_HPP diff --git a/include/Nazara/OpenGLRenderer/Wrapper/ProgramPipeline.inl b/include/Nazara/OpenGLRenderer/Wrapper/ProgramPipeline.inl deleted file mode 100644 index cbc7bcb77..000000000 --- a/include/Nazara/OpenGLRenderer/Wrapper/ProgramPipeline.inl +++ /dev/null @@ -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 -#include -#include - -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