Renderer: Add RenderPassCache
This commit is contained in:
70
include/Nazara/Renderer/RenderPassCache.hpp
Normal file
70
include/Nazara/Renderer/RenderPassCache.hpp
Normal file
@@ -0,0 +1,70 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Utility module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_RENDERPASSCACHE_HPP
|
||||
#define NAZARA_RENDERPASSCACHE_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Renderer/Config.hpp>
|
||||
#include <Nazara/Renderer/RenderPass.hpp>
|
||||
#include <memory>
|
||||
#include <variant>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class RenderDevice;
|
||||
|
||||
class NAZARA_RENDERER_API RenderPassCache
|
||||
{
|
||||
public:
|
||||
inline RenderPassCache(RenderDevice& device);
|
||||
RenderPassCache(const RenderPassCache&) = delete;
|
||||
RenderPassCache(RenderPassCache&&) noexcept = default;
|
||||
~RenderPassCache() = default;
|
||||
|
||||
const std::shared_ptr<RenderPass>& Get(const std::vector<RenderPass::Attachment>& attachments, const std::vector<RenderPass::SubpassDescription>& subpassDescriptions, const std::vector<RenderPass::SubpassDependency>& subpassDependencies) const;
|
||||
|
||||
RenderPassCache& operator=(const RenderPassCache&) = delete;
|
||||
RenderPassCache& operator=(RenderPassCache&&) noexcept = default;
|
||||
|
||||
private:
|
||||
struct RenderPassData
|
||||
{
|
||||
std::size_t attachmentCount;
|
||||
std::size_t dependencyCount;
|
||||
std::size_t descriptionCount;
|
||||
const RenderPass::Attachment* attachments;
|
||||
const RenderPass::SubpassDependency* subpassDependencies;
|
||||
const RenderPass::SubpassDescription* subpassDescriptions;
|
||||
};
|
||||
|
||||
using Key = std::variant<std::shared_ptr<RenderPass>, RenderPassData>;
|
||||
|
||||
struct Hasher
|
||||
{
|
||||
template<typename T> std::size_t operator()(const T& renderPass) const;
|
||||
std::size_t operator()(const RenderPassData& renderPassData) const;
|
||||
};
|
||||
|
||||
struct EqualityChecker
|
||||
{
|
||||
template<typename T1, typename T2> bool operator()(const T1& lhs, const T2& rhs) const;
|
||||
bool operator()(const RenderPassData& lhs, const RenderPassData& rhs) const;
|
||||
};
|
||||
|
||||
static inline auto ToRenderPassData(const Key& key);
|
||||
static inline RenderPassData ToRenderPassData(const std::shared_ptr<RenderPass>& renderPass);
|
||||
static inline const RenderPassData& ToRenderPassData(const RenderPassData& renderPassData);
|
||||
|
||||
mutable std::unordered_map<Key, std::shared_ptr<RenderPass>, Hasher, EqualityChecker> m_renderPasses;
|
||||
RenderDevice& m_device;
|
||||
};
|
||||
}
|
||||
|
||||
#include <Nazara/Renderer/RenderPassCache.inl>
|
||||
|
||||
#endif // NAZARA_RENDERPASS_HPP
|
||||
61
include/Nazara/Renderer/RenderPassCache.inl
Normal file
61
include/Nazara/Renderer/RenderPassCache.inl
Normal file
@@ -0,0 +1,61 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Utility module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Renderer/RenderPassCache.hpp>
|
||||
#include <cassert>
|
||||
#include <Nazara/Renderer/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
inline RenderPassCache::RenderPassCache(RenderDevice& device) :
|
||||
m_device(device)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::size_t RenderPassCache::Hasher::operator()(const T& key) const
|
||||
{
|
||||
return operator()(ToRenderPassData(key));
|
||||
}
|
||||
|
||||
template<typename T1, typename T2>
|
||||
inline bool RenderPassCache::EqualityChecker::operator()(const T1& lhs, const T2& rhs) const
|
||||
{
|
||||
return operator()(ToRenderPassData(lhs), ToRenderPassData(rhs));
|
||||
}
|
||||
|
||||
inline auto RenderPassCache::ToRenderPassData(const Key& key)
|
||||
{
|
||||
return std::visit([&](auto&& arg)
|
||||
{
|
||||
return ToRenderPassData(arg);
|
||||
}, key);
|
||||
}
|
||||
|
||||
inline auto RenderPassCache::ToRenderPassData(const std::shared_ptr<RenderPass>& renderPass) -> RenderPassData
|
||||
{
|
||||
const auto& attachments = renderPass->GetAttachments();
|
||||
const auto& subpassDeps = renderPass->GetSubpassDependencies();
|
||||
const auto& subpassDesc = renderPass->GetSubpassDescriptions();
|
||||
|
||||
RenderPassData data;
|
||||
data.attachmentCount = attachments.size();
|
||||
data.attachments = attachments.data();
|
||||
|
||||
data.dependencyCount = subpassDeps.size();
|
||||
data.subpassDependencies = subpassDeps.data();
|
||||
|
||||
data.descriptionCount = subpassDesc.size();
|
||||
data.subpassDescriptions = subpassDesc.data();
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
inline auto RenderPassCache::ToRenderPassData(const RenderPassData& renderPassData) -> const RenderPassData&
|
||||
{
|
||||
return renderPassData;
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Renderer/DebugOff.hpp>
|
||||
Reference in New Issue
Block a user