diff --git a/include/Nazara/OpenGLRenderer/DummySurface.hpp b/include/Nazara/OpenGLRenderer/DummySurface.hpp new file mode 100644 index 000000000..10246a9d1 --- /dev/null +++ b/include/Nazara/OpenGLRenderer/DummySurface.hpp @@ -0,0 +1,34 @@ +// Copyright (C) 2015 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_SURFACE_HPP +#define NAZARA_OPENGLRENDERER_SURFACE_HPP + +#include +#include +#include + +namespace Nz +{ + class NAZARA_OPENGLRENDERER_API DummySurface : public RenderSurface + { + public: + DummySurface() = default; + ~DummySurface() = default; + + bool Create(WindowHandle handle) override; + void Destroy() override; + + inline WindowHandle GetWindowHandle() const; + + private: + WindowHandle m_handle; + }; +} + +#include + +#endif // NAZARA_OPENGLRENDERER_SURFACE_HPP diff --git a/include/Nazara/OpenGLRenderer/OpenGLSurface.inl b/include/Nazara/OpenGLRenderer/DummySurface.inl similarity index 69% rename from include/Nazara/OpenGLRenderer/OpenGLSurface.inl rename to include/Nazara/OpenGLRenderer/DummySurface.inl index f3af9ae40..fd4f1e5a3 100644 --- a/include/Nazara/OpenGLRenderer/OpenGLSurface.inl +++ b/include/Nazara/OpenGLRenderer/DummySurface.inl @@ -2,14 +2,14 @@ // 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 { - inline Vk::Surface& OpenGLSurface::GetSurface() + inline WindowHandle DummySurface::GetWindowHandle() const { - return m_surface; + return m_handle; } } diff --git a/include/Nazara/OpenGLRenderer/OpenGLCommandBuffer.hpp b/include/Nazara/OpenGLRenderer/OpenGLCommandBuffer.hpp index e45f143bc..a597a2958 100644 --- a/include/Nazara/OpenGLRenderer/OpenGLCommandBuffer.hpp +++ b/include/Nazara/OpenGLRenderer/OpenGLCommandBuffer.hpp @@ -10,7 +10,6 @@ #include #include #include -#include #include namespace Nz @@ -18,19 +17,13 @@ namespace Nz class NAZARA_OPENGLRENDERER_API OpenGLCommandBuffer final : public CommandBuffer { public: - inline OpenGLCommandBuffer(Vk::AutoCommandBuffer commandBuffer); - inline OpenGLCommandBuffer(std::vector commandBuffers); + inline OpenGLCommandBuffer(); OpenGLCommandBuffer(const OpenGLCommandBuffer&) = delete; OpenGLCommandBuffer(OpenGLCommandBuffer&&) noexcept = default; ~OpenGLCommandBuffer() = default; - inline Vk::CommandBuffer& GetCommandBuffer(std::size_t imageIndex = 0); - OpenGLCommandBuffer& operator=(const OpenGLCommandBuffer&) = delete; OpenGLCommandBuffer& operator=(OpenGLCommandBuffer&&) = delete; - - private: - std::vector m_commandBuffers; }; } diff --git a/include/Nazara/OpenGLRenderer/OpenGLCommandBuffer.inl b/include/Nazara/OpenGLRenderer/OpenGLCommandBuffer.inl index 21a49438e..7ccd4573a 100644 --- a/include/Nazara/OpenGLRenderer/OpenGLCommandBuffer.inl +++ b/include/Nazara/OpenGLRenderer/OpenGLCommandBuffer.inl @@ -7,20 +7,6 @@ namespace Nz { - inline OpenGLCommandBuffer::OpenGLCommandBuffer(Vk::AutoCommandBuffer commandBuffer) - { - m_commandBuffers.push_back(std::move(commandBuffer)); - } - - inline OpenGLCommandBuffer::OpenGLCommandBuffer(std::vector commandBuffers) : - m_commandBuffers(std::move(commandBuffers)) - { - } - - inline Vk::CommandBuffer& OpenGLCommandBuffer::GetCommandBuffer(std::size_t imageIndex) - { - return m_commandBuffers[imageIndex].Get(); - } } #include diff --git a/include/Nazara/OpenGLRenderer/OpenGLCommandBufferBuilder.hpp b/include/Nazara/OpenGLRenderer/OpenGLCommandBufferBuilder.hpp index dce64f6f1..dc50b94d6 100644 --- a/include/Nazara/OpenGLRenderer/OpenGLCommandBufferBuilder.hpp +++ b/include/Nazara/OpenGLRenderer/OpenGLCommandBufferBuilder.hpp @@ -10,7 +10,6 @@ #include #include #include -#include namespace Nz { @@ -19,7 +18,7 @@ namespace Nz class NAZARA_OPENGLRENDERER_API OpenGLCommandBufferBuilder final : public CommandBufferBuilder { public: - inline OpenGLCommandBufferBuilder(Vk::CommandBuffer& commandBuffer, std::size_t imageIndex = 0); + OpenGLCommandBufferBuilder() = default; OpenGLCommandBufferBuilder(const OpenGLCommandBufferBuilder&) = delete; OpenGLCommandBufferBuilder(OpenGLCommandBufferBuilder&&) noexcept = default; ~OpenGLCommandBufferBuilder() = default; @@ -41,9 +40,6 @@ namespace Nz void EndDebugRegion() override; void EndRenderPass() override; - inline Vk::CommandBuffer& GetCommandBuffer(); - inline std::size_t GetMaxFramebufferCount() const; - void PreTransferBarrier() override; void PostTransferBarrier() override; @@ -52,12 +48,6 @@ namespace Nz OpenGLCommandBufferBuilder& operator=(const OpenGLCommandBufferBuilder&) = delete; OpenGLCommandBufferBuilder& operator=(OpenGLCommandBufferBuilder&&) = delete; - - private: - Vk::CommandBuffer& m_commandBuffer; - const OpenGLRenderPass* m_currentRenderPass; - std::size_t m_framebufferCount; - std::size_t m_imageIndex; }; } diff --git a/include/Nazara/OpenGLRenderer/OpenGLCommandBufferBuilder.inl b/include/Nazara/OpenGLRenderer/OpenGLCommandBufferBuilder.inl index a785ac906..8649e54e2 100644 --- a/include/Nazara/OpenGLRenderer/OpenGLCommandBufferBuilder.inl +++ b/include/Nazara/OpenGLRenderer/OpenGLCommandBufferBuilder.inl @@ -7,22 +7,6 @@ namespace Nz { - inline OpenGLCommandBufferBuilder::OpenGLCommandBufferBuilder(Vk::CommandBuffer& commandBuffer, std::size_t imageIndex) : - m_commandBuffer(commandBuffer), - m_framebufferCount(0), - m_imageIndex(imageIndex) - { - } - - inline Vk::CommandBuffer& OpenGLCommandBufferBuilder::GetCommandBuffer() - { - return m_commandBuffer; - } - - inline std::size_t OpenGLCommandBufferBuilder::GetMaxFramebufferCount() const - { - return m_framebufferCount; - } } #include diff --git a/include/Nazara/OpenGLRenderer/OpenGLFramebuffer.hpp b/include/Nazara/OpenGLRenderer/OpenGLFramebuffer.hpp index 2a9a5906a..87198b8c2 100644 --- a/include/Nazara/OpenGLRenderer/OpenGLFramebuffer.hpp +++ b/include/Nazara/OpenGLRenderer/OpenGLFramebuffer.hpp @@ -10,31 +10,19 @@ #include #include #include -#include namespace Nz { class NAZARA_OPENGLRENDERER_API OpenGLFramebuffer : public Framebuffer { public: - enum class Type - { - Multiple, - Single - }; - - inline OpenGLFramebuffer(Type type); + OpenGLFramebuffer() = default; OpenGLFramebuffer(const OpenGLFramebuffer&) = delete; OpenGLFramebuffer(OpenGLFramebuffer&&) noexcept = default; ~OpenGLFramebuffer() = default; - inline Type GetType() const; - OpenGLFramebuffer& operator=(const OpenGLFramebuffer&) = delete; OpenGLFramebuffer& operator=(OpenGLFramebuffer&&) noexcept = default; - - private: - Type m_type; }; } diff --git a/include/Nazara/OpenGLRenderer/OpenGLFramebuffer.inl b/include/Nazara/OpenGLRenderer/OpenGLFramebuffer.inl index df37fb3dd..c629eb72a 100644 --- a/include/Nazara/OpenGLRenderer/OpenGLFramebuffer.inl +++ b/include/Nazara/OpenGLRenderer/OpenGLFramebuffer.inl @@ -7,15 +7,6 @@ namespace Nz { - inline OpenGLFramebuffer::OpenGLFramebuffer(Type type) : - m_type(type) - { - } - - inline auto OpenGLFramebuffer::GetType() const -> Type - { - return m_type; - } } #include diff --git a/include/Nazara/OpenGLRenderer/OpenGLRenderImage.hpp b/include/Nazara/OpenGLRenderer/OpenGLRenderImage.hpp index fb2b6c30b..def3104e2 100644 --- a/include/Nazara/OpenGLRenderer/OpenGLRenderImage.hpp +++ b/include/Nazara/OpenGLRenderer/OpenGLRenderImage.hpp @@ -10,53 +10,34 @@ #include #include #include -#include -#include -#include -#include #include namespace Nz { - class VkRenderWindow; + class OpenGLRenderWindow; class NAZARA_OPENGLRENDERER_API OpenGLRenderImage : public RenderImage { public: - OpenGLRenderImage(VkRenderWindow& owner); + OpenGLRenderImage(OpenGLRenderWindow& owner); OpenGLRenderImage(const OpenGLRenderImage&) = delete; OpenGLRenderImage(OpenGLRenderImage&&) noexcept = default; - ~OpenGLRenderImage(); + ~OpenGLRenderImage() = default; void Execute(const std::function& callback, QueueTypeFlags queueTypeFlags) override; - inline Vk::Fence& GetInFlightFence(); - inline Vk::Semaphore& GetImageAvailableSemaphore(); - inline UInt32 GetImageIndex(); - inline Vk::Semaphore& GetRenderFinishedSemaphore(); OpenGLUploadPool& GetUploadPool() override; void SubmitCommandBuffer(CommandBuffer* commandBuffer, QueueTypeFlags queueTypeFlags) override; - void SubmitCommandBuffer(VkCommandBuffer commandBuffer, QueueTypeFlags queueTypeFlags); void Present() override; - inline void Reset(UInt32 imageIndex); - OpenGLRenderImage& operator=(const OpenGLRenderImage&) = delete; OpenGLRenderImage& operator=(OpenGLRenderImage&&) = delete; private: - std::size_t m_currentCommandBuffer; - std::vector m_inFlightCommandBuffers; - std::vector m_graphicalCommandsBuffers; - VkRenderWindow& m_owner; - Vk::CommandPool m_commandPool; - Vk::Fence m_inFlightFence; - Vk::Semaphore m_imageAvailableSemaphore; - Vk::Semaphore m_renderFinishedSemaphore; + OpenGLRenderWindow& m_owner; OpenGLUploadPool m_uploadPool; - UInt32 m_imageIndex; }; } diff --git a/include/Nazara/OpenGLRenderer/OpenGLRenderImage.inl b/include/Nazara/OpenGLRenderer/OpenGLRenderImage.inl index f89800c06..8fffed84c 100644 --- a/include/Nazara/OpenGLRenderer/OpenGLRenderImage.inl +++ b/include/Nazara/OpenGLRenderer/OpenGLRenderImage.inl @@ -7,33 +7,6 @@ namespace Nz { - inline Vk::Fence& Nz::OpenGLRenderImage::GetInFlightFence() - { - return m_inFlightFence; - } - - inline Vk::Semaphore& OpenGLRenderImage::GetImageAvailableSemaphore() - { - return m_imageAvailableSemaphore; - } - - inline UInt32 OpenGLRenderImage::GetImageIndex() - { - return m_imageIndex; - } - - inline Vk::Semaphore& OpenGLRenderImage::GetRenderFinishedSemaphore() - { - return m_renderFinishedSemaphore; - } - - inline void OpenGLRenderImage::Reset(UInt32 imageIndex) - { - m_graphicalCommandsBuffers.clear(); - m_currentCommandBuffer = 0; - m_imageIndex = imageIndex; - m_uploadPool.Reset(); - } } #include diff --git a/include/Nazara/OpenGLRenderer/OpenGLRenderPass.hpp b/include/Nazara/OpenGLRenderer/OpenGLRenderPass.hpp index 370835853..8c4ff18cd 100644 --- a/include/Nazara/OpenGLRenderer/OpenGLRenderPass.hpp +++ b/include/Nazara/OpenGLRenderer/OpenGLRenderPass.hpp @@ -10,7 +10,6 @@ #include #include #include -#include #include namespace Nz @@ -18,21 +17,13 @@ namespace Nz class NAZARA_OPENGLRENDERER_API OpenGLRenderPass final : public RenderPass { public: - inline OpenGLRenderPass(Vk::RenderPass renderPass, std::initializer_list formats); //< FIXME + OpenGLRenderPass() = default; OpenGLRenderPass(const OpenGLRenderPass&) = delete; OpenGLRenderPass(OpenGLRenderPass&&) noexcept = default; ~OpenGLRenderPass() = default; - inline PixelFormat GetAttachmentFormat(std::size_t attachmentIndex) const; - inline Vk::RenderPass& GetRenderPass(); - inline const Vk::RenderPass& GetRenderPass() const; - OpenGLRenderPass& operator=(const OpenGLRenderPass&) = delete; OpenGLRenderPass& operator=(OpenGLRenderPass&&) noexcept = default; - - private: - std::vector m_formats; - Vk::RenderPass m_renderPass; }; } diff --git a/include/Nazara/OpenGLRenderer/OpenGLRenderPass.inl b/include/Nazara/OpenGLRenderer/OpenGLRenderPass.inl index 97bafee02..1c09020a4 100644 --- a/include/Nazara/OpenGLRenderer/OpenGLRenderPass.inl +++ b/include/Nazara/OpenGLRenderer/OpenGLRenderPass.inl @@ -7,26 +7,6 @@ namespace Nz { - inline OpenGLRenderPass::OpenGLRenderPass(Vk::RenderPass renderPass, std::initializer_list formats) : - m_formats(std::begin(formats), std::end(formats)), - m_renderPass(std::move(renderPass)) - { - } - - inline PixelFormat OpenGLRenderPass::GetAttachmentFormat(std::size_t attachmentIndex) const - { - return m_formats[attachmentIndex]; - } - - inline Vk::RenderPass& OpenGLRenderPass::GetRenderPass() - { - return m_renderPass; - } - - inline const Vk::RenderPass& OpenGLRenderPass::GetRenderPass() const - { - return m_renderPass; - } } #include diff --git a/include/Nazara/OpenGLRenderer/OpenGLRenderWindow.hpp b/include/Nazara/OpenGLRenderer/OpenGLRenderWindow.hpp index 9a057fc0c..f8673cd44 100644 --- a/include/Nazara/OpenGLRenderer/OpenGLRenderWindow.hpp +++ b/include/Nazara/OpenGLRenderer/OpenGLRenderWindow.hpp @@ -8,27 +8,13 @@ #define NAZARA_OPENGLRENDERER_RENDERWINDOW_HPP #include -#include -#include -#include -#include -#include -#include #include #include -#include +#include #include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include #include #include @@ -37,54 +23,31 @@ namespace Nz class NAZARA_OPENGLRENDERER_API OpenGLRenderWindow : public RenderWindowImpl { public: - VkRenderWindow(); - VkRenderWindow(const VkRenderWindow&) = delete; - VkRenderWindow(VkRenderWindow&&) = delete; ///TODO - virtual ~VkRenderWindow(); + OpenGLRenderWindow(); + ~OpenGLRenderWindow() = default; OpenGLRenderImage& Acquire() override; bool Create(RendererImpl* renderer, RenderSurface* surface, const Vector2ui& size, const RenderWindowParameters& parameters) override; std::unique_ptr CreateCommandPool(QueueType queueType) override; - inline const OpenGLFramebuffer& GetFramebuffer() const override; - inline OpenGLDevice& GetDevice(); - inline const OpenGLDevice& GetDevice() const; - inline Vk::QueueHandle& GetGraphicsQueue(); + const OpenGLFramebuffer& GetFramebuffer() const override; const OpenGLRenderPass& GetRenderPass() const override; - inline const Vk::Swapchain& GetSwapchain() const; std::shared_ptr GetRenderDevice() override; - void Present(UInt32 imageIndex, VkSemaphore waitSemaphore = VK_NULL_HANDLE); - - VkRenderWindow& operator=(const VkRenderWindow&) = delete; - VkRenderWindow& operator=(VkRenderWindow&&) = delete; ///TODO + void Present(); private: - bool SetupDepthBuffer(const Vector2ui& size); - bool SetupRenderPass(); - bool SetupSwapchain(const Vk::PhysicalDevice& deviceInfo, Vk::Surface& surface, const Vector2ui& size); - std::size_t m_currentFrame; - Clock m_clock; - VkFormat m_depthStencilFormat; - VkSurfaceFormatKHR m_surfaceFormat; - std::optional m_framebuffer; - std::optional m_renderPass; std::shared_ptr m_device; - std::vector m_inflightFences; - std::vector m_concurrentImageData; - Vk::DeviceMemory m_depthBufferMemory; - Vk::Image m_depthBuffer; - Vk::ImageView m_depthBufferView; - Vk::QueueHandle m_graphicsQueue; - Vk::QueueHandle m_presentQueue; - Vk::QueueHandle m_transferQueue; - Vk::Swapchain m_swapchain; + std::vector m_renderImage; + std::unique_ptr m_context; + OpenGLFramebuffer m_framebuffer; + OpenGLRenderPass m_renderPass; }; } -#include +#include #endif // NAZARA_OPENGLRENDERER_RENDERWINDOW_HPP diff --git a/include/Nazara/OpenGLRenderer/OpenGLRenderWindow.inl b/include/Nazara/OpenGLRenderer/OpenGLRenderWindow.inl index c0e1dd62d..ba24c776a 100644 --- a/include/Nazara/OpenGLRenderer/OpenGLRenderWindow.inl +++ b/include/Nazara/OpenGLRenderer/OpenGLRenderWindow.inl @@ -2,49 +2,11 @@ // 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 { - inline const OpenGLMultipleFramebuffer& VkRenderWindow::GetFramebuffer() const - { - return *m_framebuffer; - } - - inline OpenGLDevice& VkRenderWindow::GetDevice() - { - return *m_device; - } - - inline const OpenGLDevice& VkRenderWindow::GetDevice() const - { - return *m_device; - } - - inline Vk::QueueHandle& VkRenderWindow::GetGraphicsQueue() - { - return m_graphicsQueue; - } - - inline const Vk::Swapchain& VkRenderWindow::GetSwapchain() const - { - return m_swapchain; - } - - inline std::shared_ptr Nz::VkRenderWindow::GetRenderDevice() - { - return m_device; - } - - inline void VkRenderWindow::Present(UInt32 imageIndex, VkSemaphore waitSemaphore) - { - NazaraAssert(imageIndex < m_inflightFences.size(), "Invalid image index"); - - m_presentQueue.Present(m_swapchain, imageIndex, waitSemaphore); - - m_currentFrame = (m_currentFrame + 1) % m_inflightFences.size(); - } } #include diff --git a/include/Nazara/OpenGLRenderer/OpenGLSurface.hpp b/include/Nazara/OpenGLRenderer/OpenGLSurface.hpp deleted file mode 100644 index 235813865..000000000 --- a/include/Nazara/OpenGLRenderer/OpenGLSurface.hpp +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (C) 2015 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_SURFACE_HPP -#define NAZARA_OPENGLRENDERER_SURFACE_HPP - -#include -#include -#include -#include - -namespace Nz -{ - class NAZARA_OPENGLRENDERER_API OpenGLSurface : public RenderSurface - { - public: - OpenGLSurface(); - OpenGLSurface(const OpenGLSurface&) = delete; - OpenGLSurface(OpenGLSurface&&) = delete; ///TODO - virtual ~OpenGLSurface(); - - bool Create(WindowHandle handle) override; - void Destroy() override; - - inline Vk::Surface& GetSurface(); - - OpenGLSurface& operator=(const OpenGLSurface&) = delete; - OpenGLSurface& operator=(OpenGLSurface&&) = delete; ///TODO - - private: - Vk::Surface m_surface; - }; -} - -#include - -#endif // NAZARA_OPENGLRENDERER_SURFACE_HPP diff --git a/include/Nazara/OpenGLRenderer/OpenGLUploadPool.hpp b/include/Nazara/OpenGLRenderer/OpenGLUploadPool.hpp index b668cf5cf..badfd8901 100644 --- a/include/Nazara/OpenGLRenderer/OpenGLUploadPool.hpp +++ b/include/Nazara/OpenGLRenderer/OpenGLUploadPool.hpp @@ -9,9 +9,8 @@ #include #include +#include #include -#include -#include #include #include @@ -20,18 +19,13 @@ namespace Nz class NAZARA_OPENGLRENDERER_API OpenGLUploadPool : public UploadPool { public: - struct OpenGLAllocation : Allocation - { - VkBuffer buffer; - }; - - inline OpenGLUploadPool(Vk::Device& device, UInt64 blockSize); + inline OpenGLUploadPool(UInt64 blockSize); OpenGLUploadPool(const OpenGLUploadPool&) = delete; OpenGLUploadPool(OpenGLUploadPool&&) noexcept = default; ~OpenGLUploadPool() = default; - OpenGLAllocation& Allocate(UInt64 size) override; - OpenGLAllocation& Allocate(UInt64 size, UInt64 alignment) override; + Allocation& Allocate(UInt64 size) override; + Allocation& Allocate(UInt64 size, UInt64 alignment) override; void Reset() override; @@ -41,15 +35,11 @@ namespace Nz private: struct Block { - Vk::DeviceMemory blockMemory; - Vk::Buffer buffer; + //< TODO UInt64 freeOffset; }; UInt64 m_blockSize; - Vk::Device& m_device; - std::vector m_blocks; - std::vector m_allocations; }; } diff --git a/include/Nazara/OpenGLRenderer/OpenGLUploadPool.inl b/include/Nazara/OpenGLRenderer/OpenGLUploadPool.inl index 2de0a8237..afb35ba5f 100644 --- a/include/Nazara/OpenGLRenderer/OpenGLUploadPool.inl +++ b/include/Nazara/OpenGLRenderer/OpenGLUploadPool.inl @@ -7,9 +7,8 @@ namespace Nz { - inline OpenGLUploadPool::OpenGLUploadPool(Vk::Device& device, UInt64 blockSize) : - m_blockSize(blockSize), - m_device(device) + inline OpenGLUploadPool::OpenGLUploadPool(UInt64 blockSize) : + m_blockSize(blockSize) { } } diff --git a/src/Nazara/OpenGLRenderer/OpenGLFramebuffer.cpp b/src/Nazara/OpenGLRenderer/OpenGLFramebuffer.cpp index c93488b3c..ffd56e2e3 100644 --- a/src/Nazara/OpenGLRenderer/OpenGLFramebuffer.cpp +++ b/src/Nazara/OpenGLRenderer/OpenGLFramebuffer.cpp @@ -2,13 +2,9 @@ // 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 #include namespace Nz { } - -#endif diff --git a/src/Nazara/OpenGLRenderer/OpenGLRenderImage.cpp b/src/Nazara/OpenGLRenderer/OpenGLRenderImage.cpp index b45621dc3..ee6d1500a 100644 --- a/src/Nazara/OpenGLRenderer/OpenGLRenderImage.cpp +++ b/src/Nazara/OpenGLRenderer/OpenGLRenderImage.cpp @@ -2,62 +2,21 @@ // 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 -#include -#include -#include +#include #include #include namespace Nz { - OpenGLRenderImage::OpenGLRenderImage(VkRenderWindow& owner) : + OpenGLRenderImage::OpenGLRenderImage(OpenGLRenderWindow& owner) : m_owner(owner), - m_uploadPool(m_owner.GetDevice(), 2 * 1024 * 1024) + m_uploadPool(2 * 1024 * 1024) { - Vk::QueueHandle& graphicsQueue = m_owner.GetGraphicsQueue(); - if (!m_commandPool.Create(m_owner.GetDevice(), graphicsQueue.GetQueueFamilyIndex(), VK_COMMAND_POOL_CREATE_TRANSIENT_BIT | VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT)) - throw std::runtime_error("failed to create command pool: " + TranslateOpenGLError(m_commandPool.GetLastErrorCode())); - - if (!m_imageAvailableSemaphore.Create(m_owner.GetDevice())) - throw std::runtime_error("failed to create image available semaphore: " + TranslateOpenGLError(m_imageAvailableSemaphore.GetLastErrorCode())); - - if (!m_renderFinishedSemaphore.Create(m_owner.GetDevice())) - throw std::runtime_error("failed to create image finished semaphore: " + TranslateOpenGLError(m_imageAvailableSemaphore.GetLastErrorCode())); - - if (!m_inFlightFence.Create(m_owner.GetDevice(), VK_FENCE_CREATE_SIGNALED_BIT)) - throw std::runtime_error("failed to create in-flight fence: " + TranslateOpenGLError(m_inFlightFence.GetLastErrorCode())); - } - - OpenGLRenderImage::~OpenGLRenderImage() - { - m_inFlightCommandBuffers.clear(); } void OpenGLRenderImage::Execute(const std::function& callback, QueueTypeFlags queueTypeFlags) { - Vk::CommandBuffer* commandBuffer; - if (m_currentCommandBuffer >= m_inFlightCommandBuffers.size()) - { - Vk::AutoCommandBuffer& newlyAllocatedBuffer = m_inFlightCommandBuffers.emplace_back(m_commandPool.AllocateCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY)); - commandBuffer = &newlyAllocatedBuffer.Get(); - m_currentCommandBuffer++; - } - else - commandBuffer = &m_inFlightCommandBuffers[m_currentCommandBuffer++].Get(); - - if (!commandBuffer->Begin(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT)) - throw std::runtime_error("failed to begin command buffer: " + TranslateOpenGLError(commandBuffer->GetLastErrorCode())); - - OpenGLCommandBufferBuilder builder(*commandBuffer, m_imageIndex); - callback(builder); - - if (!commandBuffer->End()) - throw std::runtime_error("failed to build command buffer: " + TranslateOpenGLError(commandBuffer->GetLastErrorCode())); - - SubmitCommandBuffer(*commandBuffer, queueTypeFlags); } OpenGLUploadPool& OpenGLRenderImage::GetUploadPool() @@ -67,31 +26,10 @@ namespace Nz void OpenGLRenderImage::SubmitCommandBuffer(CommandBuffer* commandBuffer, QueueTypeFlags queueTypeFlags) { - OpenGLCommandBuffer& vkCommandBuffer = *static_cast(commandBuffer); - - return SubmitCommandBuffer(vkCommandBuffer.GetCommandBuffer(m_imageIndex), queueTypeFlags); - } - - void OpenGLRenderImage::SubmitCommandBuffer(VkCommandBuffer commandBuffer, QueueTypeFlags queueTypeFlags) - { - if (queueTypeFlags & QueueType::Graphics) - m_graphicalCommandsBuffers.push_back(commandBuffer); - else - { - Vk::QueueHandle& graphicsQueue = m_owner.GetGraphicsQueue(); - if (!graphicsQueue.Submit(commandBuffer)) - throw std::runtime_error("Failed to submit command buffer: " + TranslateOpenGLError(graphicsQueue.GetLastErrorCode())); - } } void OpenGLRenderImage::Present() { - Vk::QueueHandle& graphicsQueue = m_owner.GetGraphicsQueue(); - if (!graphicsQueue.Submit(UInt32(m_graphicalCommandsBuffers.size()), m_graphicalCommandsBuffers.data(), m_imageAvailableSemaphore, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, m_renderFinishedSemaphore, m_inFlightFence)) - throw std::runtime_error("Failed to submit command buffers: " + TranslateOpenGLError(graphicsQueue.GetLastErrorCode())); - - m_owner.Present(m_imageIndex, m_renderFinishedSemaphore); + m_owner.Present(); } } - -#endif diff --git a/src/Nazara/OpenGLRenderer/OpenGLRenderPass.cpp b/src/Nazara/OpenGLRenderer/OpenGLRenderPass.cpp index e7c7b8100..57a8f8afd 100644 --- a/src/Nazara/OpenGLRenderer/OpenGLRenderPass.cpp +++ b/src/Nazara/OpenGLRenderer/OpenGLRenderPass.cpp @@ -2,13 +2,9 @@ // 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 #include namespace Nz { } - -#endif diff --git a/src/Nazara/OpenGLRenderer/OpenGLRenderWindow.cpp b/src/Nazara/OpenGLRenderer/OpenGLRenderWindow.cpp index 0c4df36e7..9799a9dca 100644 --- a/src/Nazara/OpenGLRenderer/OpenGLRenderWindow.cpp +++ b/src/Nazara/OpenGLRenderer/OpenGLRenderWindow.cpp @@ -2,485 +2,64 @@ // This file is part of the "Nazara Engine - Renderer module" // For conditions of distribution and use, see copyright notice in Config.hpp -#if 0 - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include #include namespace Nz { - VkRenderWindow::VkRenderWindow() : - m_currentFrame(0), - m_depthStencilFormat(VK_FORMAT_MAX_ENUM) + OpenGLRenderWindow::OpenGLRenderWindow() : + m_currentFrame(0) { } - VkRenderWindow::~VkRenderWindow() + OpenGLRenderImage& OpenGLRenderWindow::Acquire() { - if (m_device) - m_device->WaitForIdle(); - - m_concurrentImageData.clear(); - m_renderPass.reset(); - m_framebuffer.reset(); - m_swapchain.Destroy(); + return m_renderImage[m_currentFrame]; } - OpenGLRenderImage& VkRenderWindow::Acquire() + bool OpenGLRenderWindow::Create(RendererImpl* renderer, RenderSurface* surface, const Vector2ui& size, const RenderWindowParameters& parameters) { - OpenGLRenderImage& currentFrame = m_concurrentImageData[m_currentFrame]; - Vk::Fence& inFlightFence = currentFrame.GetInFlightFence(); + DummySurface* dummySurface = static_cast(surface); + OpenGLRenderer* glRenderer = static_cast(renderer); - // Wait until previous rendering to this image has been done - inFlightFence.Wait(); + m_device = std::static_pointer_cast(glRenderer->InstanciateRenderDevice(0)); - UInt32 imageIndex; - if (!m_swapchain.AcquireNextImage(std::numeric_limits::max(), currentFrame.GetImageAvailableSemaphore(), VK_NULL_HANDLE, &imageIndex)) - throw std::runtime_error("Failed to acquire next image: " + TranslateOpenGLError(m_swapchain.GetLastErrorCode())); + GL::ContextParams contextParams; - if (m_inflightFences[imageIndex]) - m_inflightFences[imageIndex]->Wait(); - - m_inflightFences[imageIndex] = &inFlightFence; - m_inflightFences[imageIndex]->Reset(); - - currentFrame.Reset(imageIndex); - - return currentFrame; - } - - bool VkRenderWindow::Create(RendererImpl* /*renderer*/, RenderSurface* surface, const Vector2ui& size, const RenderWindowParameters& parameters) - { - const auto& deviceInfo = OpenGL::GetPhysicalDevices()[0]; - - Vk::Surface& vulkanSurface = static_cast(surface)->GetSurface(); - - UInt32 graphicsFamilyQueueIndex; - UInt32 presentableFamilyQueueIndex; - UInt32 transferFamilyQueueIndex; - m_device = OpenGL::SelectDevice(deviceInfo, vulkanSurface, &graphicsFamilyQueueIndex, &presentableFamilyQueueIndex, &transferFamilyQueueIndex); - if (!m_device) - { - NazaraError("Failed to get compatible OpenGL device"); + m_context = m_device->CreateContext(contextParams, dummySurface->GetWindowHandle()); + if (!m_context) return false; - } - - m_graphicsQueue = m_device->GetQueue(graphicsFamilyQueueIndex, 0); - m_presentQueue = m_device->GetQueue(presentableFamilyQueueIndex, 0); - m_transferQueue = m_device->GetQueue(transferFamilyQueueIndex, 0); - - std::vector surfaceFormats; - if (!vulkanSurface.GetFormats(deviceInfo.physDevice, &surfaceFormats)) - { - NazaraError("Failed to query supported surface formats"); - return false; - } - - m_surfaceFormat = [&] () -> VkSurfaceFormatKHR - { - if (surfaceFormats.size() == 1 && surfaceFormats.front().format == VK_FORMAT_UNDEFINED) - { - // If the list contains one undefined format, it means any format can be used - return { VK_FORMAT_R8G8B8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR }; - } - else - { - // Search for RGBA8 and default to first format - for (const VkSurfaceFormatKHR& surfaceFormat : surfaceFormats) - { - if (surfaceFormat.format == VK_FORMAT_R8G8B8A8_UNORM) - return surfaceFormat; - } - - return surfaceFormats.front(); - } - }(); - - if (!parameters.depthFormats.empty()) - { - for (PixelFormat format : parameters.depthFormats) - { - switch (format) - { - case PixelFormat_Depth16: - m_depthStencilFormat = VK_FORMAT_D16_UNORM; - break; - - case PixelFormat_Depth24: - case PixelFormat_Depth24Stencil8: - m_depthStencilFormat = VK_FORMAT_D24_UNORM_S8_UINT; - break; - - case PixelFormat_Depth32: - m_depthStencilFormat = VK_FORMAT_D32_SFLOAT; - break; - - case PixelFormat_Stencil1: - case PixelFormat_Stencil4: - case PixelFormat_Stencil8: - m_depthStencilFormat = VK_FORMAT_S8_UINT; - break; - - case PixelFormat_Stencil16: - m_depthStencilFormat = VK_FORMAT_MAX_ENUM; - break; - - default: - { - PixelFormatContent formatContent = PixelFormatInfo::GetContent(format); - if (formatContent != PixelFormatContent_DepthStencil && formatContent != PixelFormatContent_Stencil) - NazaraWarning("Invalid format " + PixelFormatInfo::GetName(format) + " for depth-stencil attachment"); - - m_depthStencilFormat = VK_FORMAT_MAX_ENUM; - break; - } - } - - if (m_depthStencilFormat != VK_FORMAT_MAX_ENUM) - { - VkFormatProperties formatProperties = m_device->GetInstance().GetPhysicalDeviceFormatProperties(deviceInfo.physDevice, m_depthStencilFormat); - if (formatProperties.optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) - break; //< Found it - - m_depthStencilFormat = VK_FORMAT_MAX_ENUM; - } - } - } - - if (!SetupSwapchain(deviceInfo, vulkanSurface, size)) - { - NazaraError("Failed to create swapchain"); - return false; - } - - if (m_depthStencilFormat != VK_FORMAT_MAX_ENUM && !SetupDepthBuffer(size)) - { - NazaraError("Failed to create depth buffer"); - return false; - } - - if (!SetupRenderPass()) - { - NazaraError("Failed to create render pass"); - return false; - } - - UInt32 imageCount = m_swapchain.GetBufferCount(); - - // Framebuffers - m_inflightFences.resize(imageCount); - - Nz::StackArray framebuffers = NazaraStackArray(Vk::Framebuffer, imageCount); - for (UInt32 i = 0; i < imageCount; ++i) - { - std::array attachments = { m_swapchain.GetBuffer(i).view, m_depthBufferView }; - - VkFramebufferCreateInfo frameBufferCreate = { - VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, - nullptr, - 0, - m_renderPass->GetRenderPass(), - (attachments[1] != VK_NULL_HANDLE) ? 2U : 1U, - attachments.data(), - size.x, - size.y, - 1U - }; - - if (!framebuffers[i].Create(*m_device, frameBufferCreate)) - { - NazaraError("Failed to create framebuffer for image #" + String::Number(i) + ": " + TranslateOpenGLError(framebuffers[i].GetLastErrorCode())); - return false; - } - } - - m_framebuffer.emplace(framebuffers.data(), framebuffers.size()); - - const std::size_t MaxConcurrentImage = imageCount; - m_concurrentImageData.reserve(MaxConcurrentImage); - - for (std::size_t i = 0; i < MaxConcurrentImage; ++i) - m_concurrentImageData.emplace_back(*this); - - m_clock.Restart(); return true; } - std::unique_ptr VkRenderWindow::CreateCommandPool(QueueType queueType) + std::unique_ptr OpenGLRenderWindow::CreateCommandPool(QueueType queueType) { - UInt32 queueFamilyIndex; - switch (queueType) - { - case QueueType::Compute: - queueFamilyIndex = m_device->GetDefaultFamilyIndex(QueueType::Compute); - break; - - case QueueType::Graphics: - queueFamilyIndex = m_graphicsQueue.GetQueueFamilyIndex(); - break; - - case QueueType::Transfer: - queueFamilyIndex = m_transferQueue.GetQueueFamilyIndex(); - break; - } - - return std::make_unique(*m_device, queueFamilyIndex); + return {}; } - const OpenGLRenderPass& VkRenderWindow::GetRenderPass() const + const OpenGLFramebuffer& OpenGLRenderWindow::GetFramebuffer() const { - return *m_renderPass; + return m_framebuffer; } - bool VkRenderWindow::SetupDepthBuffer(const Vector2ui& size) + const OpenGLRenderPass& OpenGLRenderWindow::GetRenderPass() const { - VkImageCreateInfo imageCreateInfo = { - VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // VkStructureType sType; - nullptr, // const void* pNext; - 0U, // VkImageCreateFlags flags; - VK_IMAGE_TYPE_2D, // VkImageType imageType; - m_depthStencilFormat, // VkFormat format; - {size.x, size.y, 1U}, // VkExtent3D extent; - 1U, // uint32_t mipLevels; - 1U, // uint32_t arrayLayers; - VK_SAMPLE_COUNT_1_BIT, // VkSampleCountFlagBits samples; - VK_IMAGE_TILING_OPTIMAL, // VkImageTiling tiling; - VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT, // VkImageUsageFlags usage; - VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode; - 0U, // uint32_t queueFamilyIndexCount; - nullptr, // const uint32_t* pQueueFamilyIndices; - VK_IMAGE_LAYOUT_UNDEFINED, // VkImageLayout initialLayout; - }; - - if (!m_depthBuffer.Create(*m_device, imageCreateInfo)) - { - NazaraError("Failed to create depth buffer"); - return false; - } - - VkMemoryRequirements memoryReq = m_depthBuffer.GetMemoryRequirements(); - if (!m_depthBufferMemory.Create(*m_device, memoryReq.size, memoryReq.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)) - { - NazaraError("Failed to allocate depth buffer memory"); - return false; - } - - if (!m_depthBuffer.BindImageMemory(m_depthBufferMemory)) - { - NazaraError("Failed to bind depth buffer to buffer"); - return false; - } - - VkImageViewCreateInfo imageViewCreateInfo = { - VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, // VkStructureType sType; - nullptr, // const void* pNext; - 0, // VkImageViewCreateFlags flags; - m_depthBuffer, // VkImage image; - VK_IMAGE_VIEW_TYPE_2D, // VkImageViewType viewType; - m_depthStencilFormat, // VkFormat format; - { // VkComponentMapping components; - VK_COMPONENT_SWIZZLE_R, // VkComponentSwizzle .r; - VK_COMPONENT_SWIZZLE_G, // VkComponentSwizzle .g; - VK_COMPONENT_SWIZZLE_B, // VkComponentSwizzle .b; - VK_COMPONENT_SWIZZLE_A // VkComponentSwizzle .a; - }, - { // VkImageSubresourceRange subresourceRange; - VK_IMAGE_ASPECT_DEPTH_BIT, // VkImageAspectFlags .aspectMask; - 0, // uint32_t .baseMipLevel; - 1, // uint32_t .levelCount; - 0, // uint32_t .baseArrayLayer; - 1 // uint32_t .layerCount; - } - }; - - if (!m_depthBufferView.Create(*m_device, imageViewCreateInfo)) - { - NazaraError("Failed to create depth buffer view"); - return false; - } - - return true; + return m_renderPass; } - bool VkRenderWindow::SetupRenderPass() + std::shared_ptr OpenGLRenderWindow::GetRenderDevice() { - std::array attachments = { - { - { - 0, // VkAttachmentDescriptionFlags flags; - m_surfaceFormat.format, // VkFormat format; - VK_SAMPLE_COUNT_1_BIT, // VkSampleCountFlagBits samples; - VK_ATTACHMENT_LOAD_OP_CLEAR, // VkAttachmentLoadOp loadOp; - VK_ATTACHMENT_STORE_OP_STORE, // VkAttachmentStoreOp storeOp; - VK_ATTACHMENT_LOAD_OP_DONT_CARE, // VkAttachmentLoadOp stencilLoadOp; - VK_ATTACHMENT_STORE_OP_DONT_CARE, // VkAttachmentStoreOp stencilStoreOp; - VK_IMAGE_LAYOUT_UNDEFINED, // VkImageLayout initialLayout; - VK_IMAGE_LAYOUT_PRESENT_SRC_KHR // VkImageLayout finalLayout; - }, - { - 0, // VkAttachmentDescriptionFlags flags; - m_depthStencilFormat, // VkFormat format; - VK_SAMPLE_COUNT_1_BIT, // VkSampleCountFlagBits samples; - VK_ATTACHMENT_LOAD_OP_CLEAR, // VkAttachmentLoadOp loadOp; - VK_ATTACHMENT_STORE_OP_DONT_CARE, // VkAttachmentStoreOp storeOp; - VK_ATTACHMENT_LOAD_OP_DONT_CARE, // VkAttachmentLoadOp stencilLoadOp; - VK_ATTACHMENT_STORE_OP_DONT_CARE, // VkAttachmentStoreOp stencilStoreOp; - VK_IMAGE_LAYOUT_UNDEFINED, // VkImageLayout initialLayout; - VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL // VkImageLayout finalLayout; - }, - } - }; - - VkAttachmentReference colorReference = { - 0, // uint32_t attachment; - VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL // VkImageLayout layout; - }; - - VkAttachmentReference depthReference = { - 1, // uint32_t attachment; - VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL // VkImageLayout layout; - }; - - VkSubpassDescription subpass = { - 0, // VkSubpassDescriptionFlags flags; - VK_PIPELINE_BIND_POINT_GRAPHICS, // VkPipelineBindPoint pipelineBindPoint; - 0U, // uint32_t inputAttachmentCount; - nullptr, // const VkAttachmentReference* pInputAttachments; - 1U, // uint32_t colorAttachmentCount; - &colorReference, // const VkAttachmentReference* pColorAttachments; - nullptr, // const VkAttachmentReference* pResolveAttachments; - (m_depthStencilFormat != VK_FORMAT_MAX_ENUM) ? &depthReference : nullptr, // const VkAttachmentReference* pDepthStencilAttachment; - 0U, // uint32_t preserveAttachmentCount; - nullptr // const uint32_t* pPreserveAttachments; - }; - - std::array dependencies; - // First dependency at the start of the render pass - // Does the transition from final to initial layout - dependencies[0].srcSubpass = VK_SUBPASS_EXTERNAL; // Producer of the dependency - dependencies[0].dstSubpass = 0; // Consumer is our single subpass that will wait for the execution dependency - dependencies[0].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; - dependencies[0].dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; - dependencies[0].srcAccessMask = 0; - dependencies[0].dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; - dependencies[0].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT; - - // Second dependency at the end the render pass - // Does the transition from the initial to the final layout - dependencies[1].srcSubpass = 0; // Producer of the dependency is our single subpass - dependencies[1].dstSubpass = VK_SUBPASS_EXTERNAL; // Consumer are all commands outside of the render pass - dependencies[1].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; - dependencies[1].dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; - dependencies[1].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; - dependencies[1].dstAccessMask = VK_ACCESS_MEMORY_READ_BIT; - dependencies[1].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT; - - VkRenderPassCreateInfo createInfo = { - VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, // VkStructureType sType; - nullptr, // const void* pNext; - 0, // VkRenderPassCreateFlags flags; - (m_depthStencilFormat != VK_FORMAT_MAX_ENUM) ? 2U : 1U, // uint32_t attachmentCount; - attachments.data(), // const VkAttachmentDescription* pAttachments; - 1U, // uint32_t subpassCount; - &subpass, // const VkSubpassDescription* pSubpasses; - UInt32(dependencies.size()), // uint32_t dependencyCount; - dependencies.data() // const VkSubpassDependency* pDependencies; - }; - - Vk::RenderPass renderPass; - if (!renderPass.Create(*m_device, createInfo)) - { - NazaraError("Failed to create render pass: " + TranslateOpenGLError(renderPass.GetLastErrorCode())); - return false; - } - - std::initializer_list fixmeplease = { PixelFormat::PixelFormat_RGB8, PixelFormat::PixelFormat_Depth24Stencil8 }; - m_renderPass.emplace(std::move(renderPass), fixmeplease); - return true; + return m_device; } - bool VkRenderWindow::SetupSwapchain(const Vk::PhysicalDevice& deviceInfo, Vk::Surface& surface, const Vector2ui& size) + void OpenGLRenderWindow::Present() { - VkSurfaceCapabilitiesKHR surfaceCapabilities; - if (!surface.GetCapabilities(deviceInfo.physDevice, &surfaceCapabilities)) - { - NazaraError("Failed to query surface capabilities"); - return false; - } - - Nz::UInt32 imageCount = surfaceCapabilities.minImageCount + 1; - if (surfaceCapabilities.maxImageCount > 0 && imageCount > surfaceCapabilities.maxImageCount) - imageCount = surfaceCapabilities.maxImageCount; - - VkExtent2D extent; - if (surfaceCapabilities.currentExtent.width == -1) - { - extent.width = Nz::Clamp(size.x, surfaceCapabilities.minImageExtent.width, surfaceCapabilities.maxImageExtent.width); - extent.height = Nz::Clamp(size.y, surfaceCapabilities.minImageExtent.height, surfaceCapabilities.maxImageExtent.height); - } - else - extent = surfaceCapabilities.currentExtent; - - std::vector presentModes; - if (!surface.GetPresentModes(deviceInfo.physDevice, &presentModes)) - { - NazaraError("Failed to query supported present modes"); - return false; - } - - VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR; - for (VkPresentModeKHR presentMode : presentModes) - { - if (presentMode == VK_PRESENT_MODE_MAILBOX_KHR) - { - swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR; - break; - } - - if (presentMode == VK_PRESENT_MODE_IMMEDIATE_KHR) - swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR; - } - - VkSwapchainCreateInfoKHR swapchainInfo = { - VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR, - nullptr, - 0, - surface, - imageCount, - m_surfaceFormat.format, - m_surfaceFormat.colorSpace, - extent, - 1, - VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, - VK_SHARING_MODE_EXCLUSIVE, - 0, nullptr, - surfaceCapabilities.currentTransform, - VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR, - swapchainPresentMode, - VK_TRUE, - VK_NULL_HANDLE - }; - - if (!m_swapchain.Create(*m_device, swapchainInfo)) - { - NazaraError("Failed to create swapchain"); - return false; - } - - return true; + m_context->SwapBuffers(); + m_currentFrame = (m_currentFrame + 1) % m_renderImage.size(); } } -#endif diff --git a/src/Nazara/OpenGLRenderer/OpenGLShaderStage.cpp b/src/Nazara/OpenGLRenderer/OpenGLShaderStage.cpp index 24c18fd3b..0d8072596 100644 --- a/src/Nazara/OpenGLRenderer/OpenGLShaderStage.cpp +++ b/src/Nazara/OpenGLRenderer/OpenGLShaderStage.cpp @@ -2,30 +2,50 @@ // 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 +#include +#include #include namespace Nz { - bool OpenGLShaderStage::Create(Vk::Device& device, ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize) + bool OpenGLShaderStage::Create(OpenGLDevice& device, ShaderStageType type, ShaderLanguage lang, const void* source, std::size_t sourceSize) { - if (lang != ShaderLanguage::SpirV) + if (!m_shader.Create(device, ToOpenGL(type))) + return false; //< TODO: Handle error message + + switch (lang) { - NazaraError("Only Spir-V is supported for now"); + case ShaderLanguage::GLSL: + m_shader.SetSource(reinterpret_cast(source), GLint(sourceSize)); + break; + + case ShaderLanguage::SpirV: + { + if (!device.GetReferenceContext().IsExtensionSupported("GL_ARB_gl_spirv")) + { + NazaraError("Spir-V is not supported"); + return false; + } + + constexpr GLenum SHADER_BINARY_FORMAT_SPIR_V = 0x9551; + + m_shader.SetBinarySource(SHADER_BINARY_FORMAT_SPIR_V, source, GLsizei(sourceSize)); + break; + } + + default: + NazaraError("Unsupported shader language"); + return false; + } + + std::string errorLog; + if (!m_shader.Compile(&errorLog)) + { + NazaraError("Failed to compile shader: " + errorLog); return false; } - if (!m_shaderModule.Create(device, reinterpret_cast(source), sourceSize)) - { - NazaraError("Failed to create shader module"); - return false; - } - - m_stage = type; return true; } } - -#endif diff --git a/src/Nazara/OpenGLRenderer/OpenGLSurface.cpp b/src/Nazara/OpenGLRenderer/OpenGLSurface.cpp deleted file mode 100644 index 74f9bd620..000000000 --- a/src/Nazara/OpenGLRenderer/OpenGLSurface.cpp +++ /dev/null @@ -1,49 +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 - -#if 0 - -#include -#include -#include - -namespace Nz -{ - OpenGLSurface::OpenGLSurface() : - m_surface(OpenGL::GetInstance()) - { - } - - OpenGLSurface::~OpenGLSurface() = default; - - bool OpenGLSurface::Create(WindowHandle handle) - { - bool success = false; - #if defined(NAZARA_PLATFORM_WINDOWS) - { - HWND winHandle = reinterpret_cast(handle); - HINSTANCE instance = reinterpret_cast(GetWindowLongPtrW(winHandle, GWLP_HINSTANCE)); - - success = m_surface.Create(instance, winHandle); - } - #else - #error This OS is not supported by OpenGL - #endif - - if (!success) - { - NazaraError("Failed to create OpenGL surface: " + TranslateOpenGLError(m_surface.GetLastErrorCode())); - return false; - } - - return true; - } - - void OpenGLSurface::Destroy() - { - m_surface.Destroy(); - } -} - -#endif diff --git a/src/Nazara/OpenGLRenderer/OpenGLUploadPool.cpp b/src/Nazara/OpenGLRenderer/OpenGLUploadPool.cpp index 687988f9b..f16c2335a 100644 --- a/src/Nazara/OpenGLRenderer/OpenGLUploadPool.cpp +++ b/src/Nazara/OpenGLRenderer/OpenGLUploadPool.cpp @@ -2,8 +2,6 @@ // 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 #include #include @@ -11,81 +9,25 @@ namespace Nz { - auto OpenGLUploadPool::Allocate(UInt64 size) -> OpenGLAllocation& + auto OpenGLUploadPool::Allocate(UInt64 size) -> Allocation& { - const auto& deviceProperties = m_device.GetPhysicalDeviceInfo().properties; - UInt64 preferredAlignement = deviceProperties.limits.optimalBufferCopyOffsetAlignment; + /*const auto& deviceProperties = m_device.GetPhysicalDeviceInfo().properties; + UInt64 preferredAlignement = deviceProperties.limits.optimalBufferCopyOffsetAlignment;*/ - return Allocate(size, preferredAlignement); + return Allocate(size, 0); //< FIXME } - auto OpenGLUploadPool::Allocate(UInt64 size, UInt64 alignment) -> OpenGLAllocation& + auto OpenGLUploadPool::Allocate(UInt64 size, UInt64 alignment) -> Allocation& { assert(size <= m_blockSize); - // Try to minimize lost space - struct - { - Block* block = nullptr; - UInt64 alignedOffset = 0; - UInt64 lostSpace = 0; - } bestBlock; - - for (Block& block : m_blocks) - { - UInt64 alignedOffset = AlignPow2(block.freeOffset, alignment); - if (alignedOffset + size > m_blockSize) - continue; //< Not enough space - - UInt64 lostSpace = alignedOffset - block.freeOffset; - - if (!bestBlock.block || lostSpace < bestBlock.lostSpace) - { - bestBlock.block = █ - bestBlock.alignedOffset = alignedOffset; - bestBlock.lostSpace = lostSpace; - } - } - - // No block found, allocate a new one - if (!bestBlock.block) - { - Block newBlock; - if (!newBlock.buffer.Create(m_device, 0U, m_blockSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT)) - throw std::runtime_error("Failed to create block buffer: " + TranslateOpenGLError(newBlock.buffer.GetLastErrorCode())); - - VkMemoryRequirements requirement = newBlock.buffer.GetMemoryRequirements(); - - if (!newBlock.blockMemory.Create(m_device, requirement.size, requirement.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)) - throw std::runtime_error("Failed to allocate block memory: " + TranslateOpenGLError(newBlock.blockMemory.GetLastErrorCode())); - - if (!newBlock.buffer.BindBufferMemory(newBlock.blockMemory)) - throw std::runtime_error("Failed to bind buffer memory: " + TranslateOpenGLError(newBlock.buffer.GetLastErrorCode())); - - if (!newBlock.blockMemory.Map()) - throw std::runtime_error("Failed to map buffer memory: " + TranslateOpenGLError(newBlock.buffer.GetLastErrorCode())); - - bestBlock.block = &m_blocks.emplace_back(std::move(newBlock)); - bestBlock.alignedOffset = 0; - bestBlock.lostSpace = 0; - } - - OpenGLAllocation& allocationData = m_allocations.emplace_back(); - allocationData.buffer = bestBlock.block->buffer; - allocationData.mappedPtr = static_cast(bestBlock.block->blockMemory.GetMappedPointer()) + bestBlock.alignedOffset; - allocationData.offset = bestBlock.alignedOffset; - allocationData.size = size; - - return allocationData; + static Allocation dummy; + return dummy; } void OpenGLUploadPool::Reset() { - for (Block& block : m_blocks) - block.freeOffset = 0; - - m_allocations.clear(); + /*for (Block& block : m_blocks) + block.freeOffset = 0;*/ } } - -#endif