Renderer: Refactor transient resources (allow access at any time)
This commit is contained in:
parent
de68033a0e
commit
4db5b59ec9
|
|
@ -32,8 +32,6 @@ namespace Nz
|
||||||
std::shared_ptr<GL::Context> CreateContext(GL::ContextParams params) const;
|
std::shared_ptr<GL::Context> CreateContext(GL::ContextParams params) const;
|
||||||
std::shared_ptr<GL::Context> CreateContext(GL::ContextParams params, WindowHandle handle) const;
|
std::shared_ptr<GL::Context> CreateContext(GL::ContextParams params, WindowHandle handle) const;
|
||||||
|
|
||||||
void Execute(const FunctionRef<void(CommandBufferBuilder& builder)>& callback, QueueType queueType) override;
|
|
||||||
|
|
||||||
const RenderDeviceInfo& GetDeviceInfo() const override;
|
const RenderDeviceInfo& GetDeviceInfo() const override;
|
||||||
const RenderDeviceFeatures& GetEnabledFeatures() const override;
|
const RenderDeviceFeatures& GetEnabledFeatures() const override;
|
||||||
inline const GL::Context& GetReferenceContext() const;
|
inline const GL::Context& GetReferenceContext() const;
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,8 @@ namespace Nz
|
||||||
|
|
||||||
void Present();
|
void Present();
|
||||||
|
|
||||||
|
TransientResources& Transient() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::optional<OpenGLRenderPass> m_renderPass;
|
std::optional<OpenGLRenderPass> m_renderPass;
|
||||||
std::size_t m_currentFrame;
|
std::size_t m_currentFrame;
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@
|
||||||
#include <Nazara/Renderer/Texture.hpp>
|
#include <Nazara/Renderer/Texture.hpp>
|
||||||
#include <Nazara/Renderer/TextureSampler.hpp>
|
#include <Nazara/Renderer/TextureSampler.hpp>
|
||||||
#include <Nazara/Utility/PixelFormat.hpp>
|
#include <Nazara/Utility/PixelFormat.hpp>
|
||||||
#include <Nazara/Utils/FunctionRef.hpp>
|
#include <NazaraUtils/FunctionRef.hpp>
|
||||||
#include <NZSL/ShaderWriter.hpp>
|
#include <NZSL/ShaderWriter.hpp>
|
||||||
#include <NZSL/Ast/Module.hpp>
|
#include <NZSL/Ast/Module.hpp>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
@ -40,8 +40,6 @@ namespace Nz
|
||||||
RenderDevice() = default;
|
RenderDevice() = default;
|
||||||
virtual ~RenderDevice();
|
virtual ~RenderDevice();
|
||||||
|
|
||||||
virtual void Execute(const FunctionRef<void(CommandBufferBuilder& builder)>& callback, QueueType queueType) = 0;
|
|
||||||
|
|
||||||
virtual const RenderDeviceInfo& GetDeviceInfo() const = 0;
|
virtual const RenderDeviceInfo& GetDeviceInfo() const = 0;
|
||||||
virtual const RenderDeviceFeatures& GetEnabledFeatures() const = 0;
|
virtual const RenderDeviceFeatures& GetEnabledFeatures() const = 0;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,14 @@ namespace Nz
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline void RenderFrame::Execute(const FunctionRef<void(CommandBufferBuilder& builder)>& callback, QueueTypeFlags queueTypeFlags)
|
||||||
|
{
|
||||||
|
if NAZARA_UNLIKELY(!m_image)
|
||||||
|
throw std::runtime_error("frame is either invalid or has already been presented");
|
||||||
|
|
||||||
|
return m_image->Execute(callback, queueTypeFlags);
|
||||||
|
}
|
||||||
|
|
||||||
inline std::size_t RenderFrame::GetFramebufferIndex() const
|
inline std::size_t RenderFrame::GetFramebufferIndex() const
|
||||||
{
|
{
|
||||||
return m_framebufferIndex;
|
return m_framebufferIndex;
|
||||||
|
|
@ -29,6 +37,14 @@ namespace Nz
|
||||||
return m_size;
|
return m_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline UploadPool& RenderFrame::GetUploadPool()
|
||||||
|
{
|
||||||
|
if NAZARA_UNLIKELY(!m_image)
|
||||||
|
throw std::runtime_error("frame is either invalid or has already been presented");
|
||||||
|
|
||||||
|
return m_image->GetUploadPool();
|
||||||
|
}
|
||||||
|
|
||||||
inline bool RenderFrame::IsFramebufferInvalidated() const
|
inline bool RenderFrame::IsFramebufferInvalidated() const
|
||||||
{
|
{
|
||||||
return m_framebufferInvalidation;
|
return m_framebufferInvalidation;
|
||||||
|
|
@ -37,18 +53,39 @@ namespace Nz
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void RenderFrame::PushForRelease(T&& value)
|
void RenderFrame::PushForRelease(T&& value)
|
||||||
{
|
{
|
||||||
return PushReleaseCallback([v = std::forward<T>(value)] {});
|
if NAZARA_UNLIKELY(!m_image)
|
||||||
|
throw std::runtime_error("frame is either invalid or has already been presented");
|
||||||
|
|
||||||
|
m_image->PushForRelease(std::forward<T>(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename F>
|
template<typename F>
|
||||||
void RenderFrame::PushReleaseCallback(F&& releaseCallback)
|
void RenderFrame::PushReleaseCallback(F&& releaseCallback)
|
||||||
{
|
{
|
||||||
if (!m_image)
|
if NAZARA_UNLIKELY(!m_image)
|
||||||
throw std::runtime_error("frame is either invalid or has already been presented");
|
throw std::runtime_error("frame is either invalid or has already been presented");
|
||||||
|
|
||||||
m_image->PushReleaseCallback(std::forward<F>(releaseCallback));
|
m_image->PushReleaseCallback(std::forward<F>(releaseCallback));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline void RenderFrame::Present()
|
||||||
|
{
|
||||||
|
if NAZARA_UNLIKELY(!m_image)
|
||||||
|
throw std::runtime_error("frame is either invalid or has already been presented");
|
||||||
|
|
||||||
|
m_image->Present();
|
||||||
|
m_image = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void RenderFrame::SubmitCommandBuffer(CommandBuffer* commandBuffer, QueueTypeFlags queueTypeFlags)
|
||||||
|
{
|
||||||
|
if NAZARA_UNLIKELY(!m_image)
|
||||||
|
throw std::runtime_error("frame is either invalid or has already been presented");
|
||||||
|
|
||||||
|
m_image->SubmitCommandBuffer(commandBuffer, queueTypeFlags);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
inline RenderFrame::operator bool()
|
inline RenderFrame::operator bool()
|
||||||
{
|
{
|
||||||
return m_image != nullptr;
|
return m_image != nullptr;
|
||||||
|
|
|
||||||
|
|
@ -8,76 +8,14 @@
|
||||||
#define NAZARA_RENDERER_RENDERIMAGE_HPP
|
#define NAZARA_RENDERER_RENDERIMAGE_HPP
|
||||||
|
|
||||||
#include <NazaraUtils/Prerequisites.hpp>
|
#include <NazaraUtils/Prerequisites.hpp>
|
||||||
#include <Nazara/Renderer/Config.hpp>
|
#include <Nazara/Renderer/TransientResources.hpp>
|
||||||
#include <Nazara/Renderer/Enums.hpp>
|
|
||||||
#include <NazaraUtils/FunctionRef.hpp>
|
|
||||||
#include <functional>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
{
|
{
|
||||||
class CommandBuffer;
|
class NAZARA_RENDERER_API RenderImage : public TransientResources
|
||||||
class CommandBufferBuilder;
|
|
||||||
class UploadPool;
|
|
||||||
|
|
||||||
class NAZARA_RENDERER_API RenderImage
|
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
class Releasable;
|
|
||||||
template<typename T> class ReleasableLambda;
|
|
||||||
|
|
||||||
virtual ~RenderImage();
|
|
||||||
|
|
||||||
virtual void Execute(const FunctionRef<void(CommandBufferBuilder& builder)>& callback, QueueTypeFlags queueTypeFlags) = 0;
|
|
||||||
|
|
||||||
inline void FlushReleaseQueue();
|
|
||||||
|
|
||||||
virtual UploadPool& GetUploadPool() = 0;
|
|
||||||
|
|
||||||
virtual void Present() = 0;
|
virtual void Present() = 0;
|
||||||
|
|
||||||
template<typename F> void PushReleaseCallback(F&& callback);
|
|
||||||
|
|
||||||
virtual void SubmitCommandBuffer(CommandBuffer* commandBuffer, QueueTypeFlags queueTypeFlags) = 0;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
RenderImage() = default;
|
|
||||||
RenderImage(const RenderImage&) = delete;
|
|
||||||
RenderImage(RenderImage&&) = delete;
|
|
||||||
|
|
||||||
private:
|
|
||||||
static constexpr std::size_t BlockSize = 4 * 1024 * 1024;
|
|
||||||
|
|
||||||
using Block = std::vector<UInt8>;
|
|
||||||
|
|
||||||
std::vector<Releasable*> m_releaseQueue;
|
|
||||||
std::vector<Block> m_releaseMemoryPool;
|
|
||||||
};
|
|
||||||
|
|
||||||
class NAZARA_RENDERER_API RenderImage::Releasable
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
virtual ~Releasable();
|
|
||||||
|
|
||||||
virtual void Release() = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
class RenderImage::ReleasableLambda : public Releasable
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
template<typename U> ReleasableLambda(U&& lambda);
|
|
||||||
ReleasableLambda(const ReleasableLambda&) = delete;
|
|
||||||
ReleasableLambda(ReleasableLambda&&) = delete;
|
|
||||||
~ReleasableLambda() = default;
|
|
||||||
|
|
||||||
void Release() override;
|
|
||||||
|
|
||||||
ReleasableLambda& operator=(const ReleasableLambda&) = delete;
|
|
||||||
ReleasableLambda& operator=(ReleasableLambda&&) = delete;
|
|
||||||
|
|
||||||
private:
|
|
||||||
T m_lambda;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,91 +2,10 @@
|
||||||
// This file is part of the "Nazara Engine - Renderer module"
|
// This file is part of the "Nazara Engine - Renderer module"
|
||||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
#include <Nazara/Core/Algorithm.hpp>
|
|
||||||
#include <NazaraUtils/MemoryHelper.hpp>
|
|
||||||
#include <Nazara/Renderer/Debug.hpp>
|
#include <Nazara/Renderer/Debug.hpp>
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
{
|
{
|
||||||
inline void RenderImage::FlushReleaseQueue()
|
|
||||||
{
|
|
||||||
for (Releasable* releasable : m_releaseQueue)
|
|
||||||
{
|
|
||||||
releasable->Release();
|
|
||||||
PlacementDestroy(releasable);
|
|
||||||
}
|
|
||||||
m_releaseQueue.clear();
|
|
||||||
|
|
||||||
for (auto& memoryblock : m_releaseMemoryPool)
|
|
||||||
memoryblock.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename F>
|
|
||||||
void RenderImage::PushReleaseCallback(F&& callback)
|
|
||||||
{
|
|
||||||
using Functor = ReleasableLambda<std::remove_cv_t<std::remove_reference_t<F>>>;
|
|
||||||
|
|
||||||
constexpr std::size_t functorSize = sizeof(Functor);
|
|
||||||
constexpr std::size_t functorAlignment = alignof(Functor);
|
|
||||||
|
|
||||||
// Try to minimize lost space
|
|
||||||
struct
|
|
||||||
{
|
|
||||||
Block* block = nullptr;
|
|
||||||
UInt64 alignedOffset = 0;
|
|
||||||
UInt64 lostSpace = 0;
|
|
||||||
} bestBlock;
|
|
||||||
|
|
||||||
for (Block& block : m_releaseMemoryPool)
|
|
||||||
{
|
|
||||||
std::size_t freeOffset = block.size();
|
|
||||||
|
|
||||||
UInt64 alignedOffset = Align(freeOffset, functorAlignment);
|
|
||||||
if (alignedOffset + functorSize > block.capacity())
|
|
||||||
continue; //< Not enough space
|
|
||||||
|
|
||||||
UInt64 lostSpace = alignedOffset - 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;
|
|
||||||
newBlock.reserve(BlockSize);
|
|
||||||
|
|
||||||
bestBlock.block = &m_releaseMemoryPool.emplace_back(std::move(newBlock));
|
|
||||||
bestBlock.alignedOffset = 0;
|
|
||||||
bestBlock.lostSpace = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
Block& targetBlock = *bestBlock.block;
|
|
||||||
targetBlock.resize(bestBlock.alignedOffset + functorSize);
|
|
||||||
|
|
||||||
Functor* releasable = reinterpret_cast<Functor*>(&targetBlock[bestBlock.alignedOffset]);
|
|
||||||
PlacementNew(releasable, std::forward<F>(callback));
|
|
||||||
|
|
||||||
m_releaseQueue.push_back(releasable);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
template<typename U>
|
|
||||||
RenderImage::ReleasableLambda<T>::ReleasableLambda(U&& lambda) :
|
|
||||||
m_lambda(std::forward<U>(lambda))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void RenderImage::ReleasableLambda<T>::Release()
|
|
||||||
{
|
|
||||||
m_lambda();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <Nazara/Renderer/DebugOff.hpp>
|
#include <Nazara/Renderer/DebugOff.hpp>
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ namespace Nz
|
||||||
{
|
{
|
||||||
class CommandPool;
|
class CommandPool;
|
||||||
class RenderDevice;
|
class RenderDevice;
|
||||||
|
class TransientResources;
|
||||||
|
|
||||||
class NAZARA_RENDERER_API Swapchain : public RenderTarget
|
class NAZARA_RENDERER_API Swapchain : public RenderTarget
|
||||||
{
|
{
|
||||||
|
|
@ -33,6 +34,8 @@ namespace Nz
|
||||||
|
|
||||||
virtual void NotifyResize(const Vector2ui& newSize) = 0;
|
virtual void NotifyResize(const Vector2ui& newSize) = 0;
|
||||||
|
|
||||||
|
virtual TransientResources& Transient() = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
static void BuildRenderPass(PixelFormat colorFormat, PixelFormat depthFormat, std::vector<RenderPass::Attachment>& attachments, std::vector<RenderPass::SubpassDescription>& subpassDescriptions, std::vector<RenderPass::SubpassDependency>& subpassDependencies);
|
static void BuildRenderPass(PixelFormat colorFormat, PixelFormat depthFormat, std::vector<RenderPass::Attachment>& attachments, std::vector<RenderPass::SubpassDescription>& subpassDescriptions, std::vector<RenderPass::SubpassDependency>& subpassDependencies);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,87 @@
|
||||||
|
// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||||
|
// This file is part of the "Nazara Engine - Renderer module"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifndef NAZARA_RENDERER_TRANSIENTRESOURCES_HPP
|
||||||
|
#define NAZARA_RENDERER_TRANSIENTRESOURCES_HPP
|
||||||
|
|
||||||
|
#include <NazaraUtils/Prerequisites.hpp>
|
||||||
|
#include <Nazara/Renderer/Config.hpp>
|
||||||
|
#include <Nazara/Renderer/Enums.hpp>
|
||||||
|
#include <NazaraUtils/FunctionRef.hpp>
|
||||||
|
#include <functional>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace Nz
|
||||||
|
{
|
||||||
|
class CommandBuffer;
|
||||||
|
class CommandBufferBuilder;
|
||||||
|
class UploadPool;
|
||||||
|
|
||||||
|
class NAZARA_RENDERER_API TransientResources
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
class Releasable;
|
||||||
|
template<typename T> class ReleasableLambda;
|
||||||
|
|
||||||
|
virtual ~TransientResources();
|
||||||
|
|
||||||
|
virtual void Execute(const FunctionRef<void(CommandBufferBuilder& builder)>& callback, QueueTypeFlags queueTypeFlags) = 0;
|
||||||
|
|
||||||
|
inline void FlushReleaseQueue();
|
||||||
|
|
||||||
|
virtual UploadPool& GetUploadPool() = 0;
|
||||||
|
|
||||||
|
template<typename T> void PushForRelease(const T& value) = delete;
|
||||||
|
template<typename T> void PushForRelease(T&& value);
|
||||||
|
template<typename F> void PushReleaseCallback(F&& callback);
|
||||||
|
|
||||||
|
virtual void SubmitCommandBuffer(CommandBuffer* commandBuffer, QueueTypeFlags queueTypeFlags) = 0;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
TransientResources() = default;
|
||||||
|
TransientResources(const TransientResources&) = delete;
|
||||||
|
TransientResources(TransientResources&&) = delete;
|
||||||
|
|
||||||
|
private:
|
||||||
|
static constexpr std::size_t BlockSize = 4 * 1024 * 1024;
|
||||||
|
|
||||||
|
using Block = std::vector<UInt8>;
|
||||||
|
|
||||||
|
std::vector<Releasable*> m_releaseQueue;
|
||||||
|
std::vector<Block> m_releaseMemoryPool;
|
||||||
|
};
|
||||||
|
|
||||||
|
class NAZARA_RENDERER_API TransientResources::Releasable
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual ~Releasable();
|
||||||
|
|
||||||
|
virtual void Release() = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class TransientResources::ReleasableLambda : public Releasable
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
template<typename U> ReleasableLambda(U&& lambda);
|
||||||
|
ReleasableLambda(const ReleasableLambda&) = delete;
|
||||||
|
ReleasableLambda(ReleasableLambda&&) = delete;
|
||||||
|
~ReleasableLambda() = default;
|
||||||
|
|
||||||
|
void Release() override;
|
||||||
|
|
||||||
|
ReleasableLambda& operator=(const ReleasableLambda&) = delete;
|
||||||
|
ReleasableLambda& operator=(ReleasableLambda&&) = delete;
|
||||||
|
|
||||||
|
private:
|
||||||
|
T m_lambda;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <Nazara/Renderer/TransientResources.inl>
|
||||||
|
|
||||||
|
#endif // NAZARA_RENDERER_TRANSIENTRESOURCES_HPP
|
||||||
|
|
@ -0,0 +1,100 @@
|
||||||
|
// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||||
|
// This file is part of the "Nazara Engine - Renderer module"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
|
#include <NazaraUtils/Algorithm.hpp>
|
||||||
|
#include <NazaraUtils/MemoryHelper.hpp>
|
||||||
|
#include <Nazara/Renderer/Debug.hpp>
|
||||||
|
|
||||||
|
namespace Nz
|
||||||
|
{
|
||||||
|
inline void TransientResources::FlushReleaseQueue()
|
||||||
|
{
|
||||||
|
for (Releasable* releasable : m_releaseQueue)
|
||||||
|
{
|
||||||
|
releasable->Release();
|
||||||
|
PlacementDestroy(releasable);
|
||||||
|
}
|
||||||
|
m_releaseQueue.clear();
|
||||||
|
|
||||||
|
for (auto& memoryblock : m_releaseMemoryPool)
|
||||||
|
memoryblock.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void TransientResources::PushForRelease(T&& value)
|
||||||
|
{
|
||||||
|
static_assert(std::is_rvalue_reference_v<decltype(value)>);
|
||||||
|
|
||||||
|
return PushReleaseCallback([v = std::move(value)] {});
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename F>
|
||||||
|
void TransientResources::PushReleaseCallback(F&& callback)
|
||||||
|
{
|
||||||
|
using Functor = ReleasableLambda<std::remove_cv_t<std::remove_reference_t<F>>>;
|
||||||
|
|
||||||
|
constexpr std::size_t functorSize = sizeof(Functor);
|
||||||
|
constexpr std::size_t functorAlignment = alignof(Functor);
|
||||||
|
|
||||||
|
// Try to minimize lost space
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
Block* block = nullptr;
|
||||||
|
UInt64 alignedOffset = 0;
|
||||||
|
UInt64 lostSpace = 0;
|
||||||
|
} bestBlock;
|
||||||
|
|
||||||
|
for (Block& block : m_releaseMemoryPool)
|
||||||
|
{
|
||||||
|
std::size_t freeOffset = block.size();
|
||||||
|
|
||||||
|
UInt64 alignedOffset = Align(freeOffset, functorAlignment);
|
||||||
|
if (alignedOffset + functorSize > block.capacity())
|
||||||
|
continue; //< Not enough space
|
||||||
|
|
||||||
|
UInt64 lostSpace = alignedOffset - 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;
|
||||||
|
newBlock.reserve(BlockSize);
|
||||||
|
|
||||||
|
bestBlock.block = &m_releaseMemoryPool.emplace_back(std::move(newBlock));
|
||||||
|
bestBlock.alignedOffset = 0;
|
||||||
|
bestBlock.lostSpace = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
Block& targetBlock = *bestBlock.block;
|
||||||
|
targetBlock.resize(bestBlock.alignedOffset + functorSize);
|
||||||
|
|
||||||
|
Functor* releasable = reinterpret_cast<Functor*>(&targetBlock[bestBlock.alignedOffset]);
|
||||||
|
PlacementNew(releasable, std::forward<F>(callback));
|
||||||
|
|
||||||
|
m_releaseQueue.push_back(releasable);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
template<typename U>
|
||||||
|
TransientResources::ReleasableLambda<T>::ReleasableLambda(U&& lambda) :
|
||||||
|
m_lambda(std::forward<U>(lambda))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void TransientResources::ReleasableLambda<T>::Release()
|
||||||
|
{
|
||||||
|
m_lambda();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <Nazara/Renderer/DebugOff.hpp>
|
||||||
|
|
@ -40,6 +40,8 @@ namespace Nz
|
||||||
inline Swapchain& GetSwapchain();
|
inline Swapchain& GetSwapchain();
|
||||||
inline const Swapchain& GetSwapchain() const;
|
inline const Swapchain& GetSwapchain() const;
|
||||||
|
|
||||||
|
inline TransientResources& Transient();
|
||||||
|
|
||||||
WindowSwapchain& operator=(const WindowSwapchain&) = delete;
|
WindowSwapchain& operator=(const WindowSwapchain&) = delete;
|
||||||
WindowSwapchain& operator=(WindowSwapchain&& windowSwapchain) = delete;
|
WindowSwapchain& operator=(WindowSwapchain&& windowSwapchain) = delete;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,11 @@ namespace Nz
|
||||||
return *m_swapchain;
|
return *m_swapchain;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline TransientResources& WindowSwapchain::Transient()
|
||||||
|
{
|
||||||
|
return m_swapchain->Transient();
|
||||||
|
}
|
||||||
|
|
||||||
void WindowSwapchain::DisconnectSignals()
|
void WindowSwapchain::DisconnectSignals()
|
||||||
{
|
{
|
||||||
m_onGainedFocus.Disconnect();
|
m_onGainedFocus.Disconnect();
|
||||||
|
|
|
||||||
|
|
@ -23,8 +23,6 @@ namespace Nz
|
||||||
VulkanDevice(VulkanDevice&&) = delete; ///TODO?
|
VulkanDevice(VulkanDevice&&) = delete; ///TODO?
|
||||||
~VulkanDevice();
|
~VulkanDevice();
|
||||||
|
|
||||||
void Execute(const FunctionRef<void(CommandBufferBuilder& builder)>& callback, QueueType queueType) override;
|
|
||||||
|
|
||||||
const RenderDeviceInfo& GetDeviceInfo() const override;
|
const RenderDeviceInfo& GetDeviceInfo() const override;
|
||||||
const RenderDeviceFeatures& GetEnabledFeatures() const override;
|
const RenderDeviceFeatures& GetEnabledFeatures() const override;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -59,6 +59,8 @@ namespace Nz
|
||||||
|
|
||||||
void Present(UInt32 imageIndex, VkSemaphore waitSemaphore = VK_NULL_HANDLE);
|
void Present(UInt32 imageIndex, VkSemaphore waitSemaphore = VK_NULL_HANDLE);
|
||||||
|
|
||||||
|
TransientResources& Transient() override;
|
||||||
|
|
||||||
VulkanSwapchain& operator=(const VulkanSwapchain&) = delete;
|
VulkanSwapchain& operator=(const VulkanSwapchain&) = delete;
|
||||||
VulkanSwapchain& operator=(VulkanSwapchain&&) = delete;
|
VulkanSwapchain& operator=(VulkanSwapchain&&) = delete;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -60,9 +60,9 @@ namespace Nz
|
||||||
UpdateObservers();
|
UpdateObservers();
|
||||||
UpdateInstances();
|
UpdateInstances();
|
||||||
|
|
||||||
for (auto& windowPtr : m_windowSwapchains)
|
for (auto& swapchainPtr : m_windowSwapchains)
|
||||||
{
|
{
|
||||||
RenderFrame frame = windowPtr->AcquireFrame();
|
RenderFrame frame = swapchainPtr->AcquireFrame();
|
||||||
if (!frame)
|
if (!frame)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -156,22 +156,6 @@ namespace Nz
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenGLDevice::Execute(const FunctionRef<void(CommandBufferBuilder& builder)>& callback, QueueType /*queueType*/)
|
|
||||||
{
|
|
||||||
const GL::Context* activeContext = GL::Context::GetCurrentContext();
|
|
||||||
if (!activeContext || activeContext->GetDevice() != this)
|
|
||||||
{
|
|
||||||
if (!GL::Context::SetCurrentContext(m_referenceContext.get()))
|
|
||||||
throw std::runtime_error("failed to activate context");
|
|
||||||
}
|
|
||||||
|
|
||||||
OpenGLCommandBuffer commandBuffer; //< TODO: Use a pool and remove default constructor
|
|
||||||
OpenGLCommandBufferBuilder builder(commandBuffer);
|
|
||||||
callback(builder);
|
|
||||||
|
|
||||||
commandBuffer.Execute();
|
|
||||||
}
|
|
||||||
|
|
||||||
const RenderDeviceInfo& OpenGLDevice::GetDeviceInfo() const
|
const RenderDeviceInfo& OpenGLDevice::GetDeviceInfo() const
|
||||||
{
|
{
|
||||||
return m_deviceInfo;
|
return m_deviceInfo;
|
||||||
|
|
|
||||||
|
|
@ -116,4 +116,9 @@ namespace Nz
|
||||||
m_context->SwapBuffers();
|
m_context->SwapBuffers();
|
||||||
m_currentFrame = (m_currentFrame + 1) % m_renderImage.size();
|
m_currentFrame = (m_currentFrame + 1) % m_renderImage.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TransientResources& OpenGLSwapchain::Transient()
|
||||||
|
{
|
||||||
|
return *m_renderImage[m_currentFrame];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,44 +0,0 @@
|
||||||
// Copyright (C) 2023 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
|
||||||
// This file is part of the "Nazara Engine - Renderer module"
|
|
||||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
|
||||||
|
|
||||||
#include <Nazara/Renderer/RenderFrame.hpp>
|
|
||||||
#include <Nazara/Renderer/RenderImage.hpp>
|
|
||||||
#include <stdexcept>
|
|
||||||
#include <Nazara/Renderer/Debug.hpp>
|
|
||||||
|
|
||||||
namespace Nz
|
|
||||||
{
|
|
||||||
void RenderFrame::Execute(const FunctionRef<void(CommandBufferBuilder& builder)>& callback, QueueTypeFlags queueTypeFlags)
|
|
||||||
{
|
|
||||||
if (!m_image)
|
|
||||||
throw std::runtime_error("frame is either invalid or has already been presented");
|
|
||||||
|
|
||||||
return m_image->Execute(callback, queueTypeFlags);
|
|
||||||
}
|
|
||||||
|
|
||||||
UploadPool& RenderFrame::GetUploadPool()
|
|
||||||
{
|
|
||||||
if (!m_image)
|
|
||||||
throw std::runtime_error("frame is either invalid or has already been presented");
|
|
||||||
|
|
||||||
return m_image->GetUploadPool();
|
|
||||||
}
|
|
||||||
|
|
||||||
void RenderFrame::Present()
|
|
||||||
{
|
|
||||||
if (!m_image)
|
|
||||||
throw std::runtime_error("frame is either invalid or has already been presented");
|
|
||||||
|
|
||||||
m_image->Present();
|
|
||||||
m_image = nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void RenderFrame::SubmitCommandBuffer(CommandBuffer* commandBuffer, QueueTypeFlags queueTypeFlags)
|
|
||||||
{
|
|
||||||
if (!m_image)
|
|
||||||
throw std::runtime_error("frame is either invalid or has already been presented");
|
|
||||||
|
|
||||||
m_image->SubmitCommandBuffer(commandBuffer, queueTypeFlags);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -2,15 +2,15 @@
|
||||||
// This file is part of the "Nazara Engine - Renderer module"
|
// This file is part of the "Nazara Engine - Renderer module"
|
||||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
#include <Nazara/Renderer/RenderImage.hpp>
|
#include <Nazara/Renderer/TransientResources.hpp>
|
||||||
#include <Nazara/Renderer/Debug.hpp>
|
#include <Nazara/Renderer/Debug.hpp>
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
{
|
{
|
||||||
RenderImage::~RenderImage()
|
TransientResources::~TransientResources()
|
||||||
{
|
{
|
||||||
FlushReleaseQueue();
|
FlushReleaseQueue();
|
||||||
}
|
}
|
||||||
|
|
||||||
RenderImage::Releasable::~Releasable() = default;
|
TransientResources::Releasable::~Releasable() = default;
|
||||||
}
|
}
|
||||||
|
|
@ -21,22 +21,6 @@ namespace Nz
|
||||||
{
|
{
|
||||||
VulkanDevice::~VulkanDevice() = default;
|
VulkanDevice::~VulkanDevice() = default;
|
||||||
|
|
||||||
void VulkanDevice::Execute(const FunctionRef<void(CommandBufferBuilder& builder)>& callback, QueueType queueType)
|
|
||||||
{
|
|
||||||
Vk::AutoCommandBuffer commandBuffer = AllocateCommandBuffer(queueType);
|
|
||||||
if (!commandBuffer->Begin(VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT))
|
|
||||||
throw std::runtime_error("failed to begin command buffer: " + TranslateVulkanError(commandBuffer->GetLastErrorCode()));
|
|
||||||
|
|
||||||
VulkanCommandBufferBuilder builder(commandBuffer);
|
|
||||||
callback(builder);
|
|
||||||
|
|
||||||
if (!commandBuffer->End())
|
|
||||||
throw std::runtime_error("failed to build command buffer: " + TranslateVulkanError(commandBuffer->GetLastErrorCode()));
|
|
||||||
|
|
||||||
GetQueue(GetDefaultFamilyIndex(queueType), 0).Submit(commandBuffer);
|
|
||||||
GetQueue(GetDefaultFamilyIndex(queueType), 0).WaitIdle();
|
|
||||||
}
|
|
||||||
|
|
||||||
const RenderDeviceInfo& VulkanDevice::GetDeviceInfo() const
|
const RenderDeviceInfo& VulkanDevice::GetDeviceInfo() const
|
||||||
{
|
{
|
||||||
return m_renderDeviceInfo;
|
return m_renderDeviceInfo;
|
||||||
|
|
|
||||||
|
|
@ -332,6 +332,11 @@ namespace Nz
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TransientResources& VulkanSwapchain::Transient()
|
||||||
|
{
|
||||||
|
return *m_concurrentImageData[m_currentFrame];
|
||||||
|
}
|
||||||
|
|
||||||
bool VulkanSwapchain::SetupDepthBuffer()
|
bool VulkanSwapchain::SetupDepthBuffer()
|
||||||
{
|
{
|
||||||
VkImageCreateInfo imageCreateInfo = {
|
VkImageCreateInfo imageCreateInfo = {
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ struct SpriteRenderPipeline
|
||||||
std::shared_ptr<Nz::ComputePipeline> BuildComputePipeline(Nz::RenderDevice& device, std::shared_ptr<Nz::RenderPipelineLayout> pipelineLayout, std::shared_ptr<nzsl::ModuleResolver> moduleResolver);
|
std::shared_ptr<Nz::ComputePipeline> BuildComputePipeline(Nz::RenderDevice& device, std::shared_ptr<Nz::RenderPipelineLayout> pipelineLayout, std::shared_ptr<nzsl::ModuleResolver> moduleResolver);
|
||||||
SpriteRenderPipeline BuildSpritePipeline(Nz::RenderDevice& device);
|
SpriteRenderPipeline BuildSpritePipeline(Nz::RenderDevice& device);
|
||||||
SpriteRenderData BuildSpriteData(Nz::RenderDevice& device, const SpriteRenderPipeline& pipelineData, const Nz::Rectf& textureRect, const Nz::Vector2f& screenSize, const Nz::RenderBufferView& buffer, const Nz::RenderBufferView& particleBuffer, std::shared_ptr<Nz::Texture> texture, std::shared_ptr<Nz::TextureSampler> sampler);
|
SpriteRenderData BuildSpriteData(Nz::RenderDevice& device, const SpriteRenderPipeline& pipelineData, const Nz::Rectf& textureRect, const Nz::Vector2f& screenSize, const Nz::RenderBufferView& buffer, const Nz::RenderBufferView& particleBuffer, std::shared_ptr<Nz::Texture> texture, std::shared_ptr<Nz::TextureSampler> sampler);
|
||||||
std::shared_ptr<Nz::Texture> GenerateSpriteTexture(Nz::RenderDevice& device, std::shared_ptr<nzsl::ModuleResolver> moduleResolver);
|
std::shared_ptr<Nz::Texture> GenerateSpriteTexture(Nz::RenderDevice& device, std::shared_ptr<nzsl::ModuleResolver> moduleResolver, Nz::WindowSwapchain& swapchain);
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
|
@ -179,7 +179,7 @@ int main()
|
||||||
|
|
||||||
std::shared_ptr<Nz::RenderBuffer> uniformBuffer = device->InstantiateBuffer(Nz::BufferType::Uniform, viewerBufferSize, Nz::BufferUsage::DeviceLocal, viewerBufferInitialData.data());
|
std::shared_ptr<Nz::RenderBuffer> uniformBuffer = device->InstantiateBuffer(Nz::BufferType::Uniform, viewerBufferSize, Nz::BufferUsage::DeviceLocal, viewerBufferInitialData.data());
|
||||||
|
|
||||||
std::shared_ptr<Nz::Texture> texture = GenerateSpriteTexture(*device, moduleResolver);
|
std::shared_ptr<Nz::Texture> texture = GenerateSpriteTexture(*device, moduleResolver, windowSwapchain);
|
||||||
std::shared_ptr<Nz::TextureSampler> textureSampler = device->InstantiateTextureSampler({});
|
std::shared_ptr<Nz::TextureSampler> textureSampler = device->InstantiateTextureSampler({});
|
||||||
|
|
||||||
SpriteRenderPipeline spriteRenderPipeline = BuildSpritePipeline(*device);
|
SpriteRenderPipeline spriteRenderPipeline = BuildSpritePipeline(*device);
|
||||||
|
|
@ -503,7 +503,7 @@ SpriteRenderData BuildSpriteData(Nz::RenderDevice& device, const SpriteRenderPip
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<Nz::Texture> GenerateSpriteTexture(Nz::RenderDevice& device, std::shared_ptr<nzsl::ModuleResolver> moduleResolver)
|
std::shared_ptr<Nz::Texture> GenerateSpriteTexture(Nz::RenderDevice& device, std::shared_ptr<nzsl::ModuleResolver> moduleResolver, Nz::WindowSwapchain& swapchain)
|
||||||
{
|
{
|
||||||
nzsl::Ast::ModulePtr shaderModule = moduleResolver->Resolve("Compute.ParticleTexture");
|
nzsl::Ast::ModulePtr shaderModule = moduleResolver->Resolve("Compute.ParticleTexture");
|
||||||
if (!shaderModule)
|
if (!shaderModule)
|
||||||
|
|
@ -550,7 +550,7 @@ std::shared_ptr<Nz::Texture> GenerateSpriteTexture(Nz::RenderDevice& device, std
|
||||||
texParams.type = Nz::ImageType::E2D;
|
texParams.type = Nz::ImageType::E2D;
|
||||||
texParams.pixelFormat = Nz::PixelFormat::RGBA8;
|
texParams.pixelFormat = Nz::PixelFormat::RGBA8;
|
||||||
texParams.width = texParams.height = 256;
|
texParams.width = texParams.height = 256;
|
||||||
texParams.usageFlags = Nz::TextureUsage::ShaderReadWrite | Nz::TextureUsage::ShaderSampling;
|
texParams.usageFlags = Nz::TextureUsage::ShaderReadWrite | Nz::TextureUsage::ShaderSampling | Nz::TextureUsage::TransferSource | Nz::TextureUsage::TransferDestination;
|
||||||
|
|
||||||
std::shared_ptr<Nz::Texture> targetTexture = device.InstantiateTexture(texParams);
|
std::shared_ptr<Nz::Texture> targetTexture = device.InstantiateTexture(texParams);
|
||||||
|
|
||||||
|
|
@ -564,7 +564,7 @@ std::shared_ptr<Nz::Texture> GenerateSpriteTexture(Nz::RenderDevice& device, std
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
device.Execute([&](Nz::CommandBufferBuilder& builder)
|
swapchain.Transient().Execute([&](Nz::CommandBufferBuilder& builder)
|
||||||
{
|
{
|
||||||
builder.TextureBarrier(Nz::PipelineStage::BottomOfPipe, Nz::PipelineStage::ComputeShader, {}, Nz::MemoryAccess::ShaderWrite, Nz::TextureLayout::Undefined, Nz::TextureLayout::General, *targetTexture);
|
builder.TextureBarrier(Nz::PipelineStage::BottomOfPipe, Nz::PipelineStage::ComputeShader, {}, Nz::MemoryAccess::ShaderWrite, Nz::TextureLayout::Undefined, Nz::TextureLayout::General, *targetTexture);
|
||||||
|
|
||||||
|
|
@ -576,5 +576,7 @@ std::shared_ptr<Nz::Texture> GenerateSpriteTexture(Nz::RenderDevice& device, std
|
||||||
|
|
||||||
}, Nz::QueueType::Compute);
|
}, Nz::QueueType::Compute);
|
||||||
|
|
||||||
|
device.WaitForIdle();
|
||||||
|
|
||||||
return targetTexture;
|
return targetTexture;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue