Graphics/FramePipeline: Replace maps with memory pools and indices

This commit is contained in:
Jérôme Leclercq
2022-02-21 20:47:11 +01:00
parent a1b6f51398
commit 20a86312ff
14 changed files with 598 additions and 354 deletions

View File

@@ -10,6 +10,7 @@
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Graphics/InstancedRenderable.hpp>
#include <Nazara/Graphics/WorldInstance.hpp>
#include <Nazara/Math/Rect.hpp>
#include <entt/entt.hpp>
#include <memory>
#include <vector>
@@ -20,6 +21,7 @@ namespace Nz
{
public:
struct Renderable;
static constexpr std::size_t MaxRenderableCount = 8;
inline GraphicsComponent(bool initialyVisible = true);
GraphicsComponent(const GraphicsComponent&) = default;
@@ -32,7 +34,8 @@ namespace Nz
inline void DetachRenderable(const std::shared_ptr<InstancedRenderable>& renderable);
inline const std::vector<Renderable>& GetRenderables() const;
inline const Renderable& GetRenderableEntry(std::size_t renderableIndex) const;
inline const std::array<Renderable, MaxRenderableCount>& GetRenderables() const;
inline const Recti& GetScissorBox() const;
inline const WorldInstancePtr& GetWorldInstance() const;
@@ -47,8 +50,9 @@ namespace Nz
GraphicsComponent& operator=(const GraphicsComponent&) = default;
GraphicsComponent& operator=(GraphicsComponent&&) = default;
NazaraSignal(OnRenderableAttached, GraphicsComponent* /*graphicsComponent*/, const Renderable& /*renderable*/);
NazaraSignal(OnRenderableDetach, GraphicsComponent* /*graphicsComponent*/, const Renderable& /*renderable*/);
NazaraSignal(OnRenderableAttached, GraphicsComponent* /*graphicsComponent*/, std::size_t /*renderableIndex*/);
NazaraSignal(OnRenderableDetach, GraphicsComponent* /*graphicsComponent*/, std::size_t /*renderableIndex*/);
NazaraSignal(OnScissorBoxUpdate, GraphicsComponent* /*graphicsComponent*/, const Recti& /*newScissorBox*/);
NazaraSignal(OnVisibilityUpdate, GraphicsComponent* /*graphicsComponent*/, bool /*newVisibilityState*/);
struct Renderable
@@ -58,7 +62,7 @@ namespace Nz
};
private:
std::vector<Renderable> m_renderables;
std::array<Renderable, MaxRenderableCount> m_renderables;
Recti m_scissorBox;
WorldInstancePtr m_worldInstance;
bool m_isVisible;

View File

@@ -16,19 +16,31 @@ namespace Nz
inline void GraphicsComponent::AttachRenderable(std::shared_ptr<InstancedRenderable> renderable, UInt32 renderMask)
{
auto& entry = m_renderables.emplace_back();
entry.renderable = std::move(renderable);
entry.renderMask = renderMask;
for (std::size_t i = 0; i < m_renderables.size(); ++i)
{
auto& entry = m_renderables[i];
if (entry.renderable)
continue;
OnRenderableAttached(this, m_renderables.back());
entry.renderable = std::move(renderable);
entry.renderMask = renderMask;
OnRenderableAttached(this, i);
break;
}
}
inline void GraphicsComponent::Clear()
{
for (const auto& renderable : m_renderables)
OnRenderableDetach(this, renderable);
for (std::size_t i = 0; i < m_renderables.size(); ++i)
{
auto& entry = m_renderables[i];
if (entry.renderable)
continue;
m_renderables.clear();
OnRenderableDetach(this, i);
entry.renderable.reset();
}
}
inline void GraphicsComponent::DetachRenderable(const std::shared_ptr<InstancedRenderable>& renderable)
@@ -36,13 +48,19 @@ namespace Nz
auto it = std::find_if(m_renderables.begin(), m_renderables.end(), [&](const auto& renderableEntry) { return renderableEntry.renderable == renderable; });
if (it != m_renderables.end())
{
OnRenderableDetach(this, *it);
OnRenderableDetach(this, std::distance(m_renderables.begin(), it));
m_renderables.erase(it);
it->renderable.reset();
}
}
inline auto GraphicsComponent::GetRenderables() const -> const std::vector<Renderable>&
inline auto GraphicsComponent::GetRenderableEntry(std::size_t renderableIndex) const -> const Renderable&
{
assert(renderableIndex < m_renderables.size());
return m_renderables[renderableIndex];
}
inline auto GraphicsComponent::GetRenderables() const -> const std::array<Renderable, MaxRenderableCount>&
{
return m_renderables;
}

View File

@@ -11,6 +11,7 @@
#include <Nazara/Graphics/Light.hpp>
#include <Nazara/Graphics/WorldInstance.hpp>
#include <entt/entt.hpp>
#include <array>
#include <memory>
#include <vector>
@@ -20,6 +21,7 @@ namespace Nz
{
public:
struct LightEntry;
static constexpr std::size_t MaxLightCount = 8;
inline LightComponent(bool initialyVisible = true);
LightComponent(const LightComponent&) = default;
@@ -32,7 +34,8 @@ namespace Nz
inline void DetachLight(const std::shared_ptr<Light>& renderable);
inline const std::vector<LightEntry>& GetLights() const;
inline const LightEntry& GetLightEntry(std::size_t lightIndex) const;
inline const std::array<LightEntry, MaxLightCount>& GetLights() const;
inline void Hide();
@@ -43,8 +46,8 @@ namespace Nz
LightComponent& operator=(const LightComponent&) = default;
LightComponent& operator=(LightComponent&&) = default;
NazaraSignal(OnLightAttached, LightComponent* /*graphicsComponent*/, const LightEntry& /*lightEntry*/);
NazaraSignal(OnLightDetach, LightComponent* /*graphicsComponent*/, const LightEntry& /*lightEntry*/);
NazaraSignal(OnLightAttached, LightComponent* /*graphicsComponent*/, std::size_t /*lightIndex*/);
NazaraSignal(OnLightDetach, LightComponent* /*graphicsComponent*/, std::size_t /*lightIndex*/);
NazaraSignal(OnVisibilityUpdate, LightComponent* /*graphicsComponent*/, bool /*newVisibilityState*/);
struct LightEntry
@@ -54,7 +57,7 @@ namespace Nz
};
private:
std::vector<LightEntry> m_lightEntries;
std::array<LightEntry, MaxLightCount> m_lightEntries;
bool m_isVisible;
};
}

View File

@@ -14,19 +14,31 @@ namespace Nz
inline void LightComponent::AttachLight(std::shared_ptr<Light> light, UInt32 renderMask)
{
auto& entry = m_lightEntries.emplace_back();
entry.light = std::move(light);
entry.renderMask = renderMask;
for (std::size_t i = 0; i < m_lightEntries.size(); ++i)
{
auto& entry = m_lightEntries[i];
if (entry.light)
continue;
OnLightAttached(this, m_lightEntries.back());
entry.light = std::move(light);
entry.renderMask = renderMask;
OnLightAttached(this, i);
break;
}
}
inline void LightComponent::Clear()
{
for (const auto& lightEntry : m_lightEntries)
OnLightDetach(this, lightEntry);
for (std::size_t i = 0; i < m_lightEntries.size(); ++i)
{
auto& entry = m_lightEntries[i];
if (entry.light)
continue;
m_lightEntries.clear();
OnLightDetach(this, i);
entry.light.reset();
}
}
inline void LightComponent::DetachLight(const std::shared_ptr<Light>& light)
@@ -34,13 +46,19 @@ namespace Nz
auto it = std::find_if(m_lightEntries.begin(), m_lightEntries.end(), [&](const auto& lightEntry) { return lightEntry.light == light; });
if (it != m_lightEntries.end())
{
OnLightDetach(this, *it);
OnLightDetach(this, std::distance(m_lightEntries.begin(), it));
m_lightEntries.erase(it);
it->light.reset();
}
}
inline auto LightComponent::GetLights() const -> const std::vector<LightEntry>&
inline auto LightComponent::GetLightEntry(std::size_t lightIndex) const -> const LightEntry&
{
assert(lightIndex < m_lightEntries.size());
return m_lightEntries[lightIndex];
}
inline auto LightComponent::GetLights() const -> const std::array<LightEntry, MaxLightCount>&
{
return m_lightEntries;
}

View File

@@ -8,6 +8,7 @@
#define NAZARA_GRAPHICS_FORWARDFRAMEPIPELINE_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/MemoryPool.hpp>
#include <Nazara/Graphics/BakedFrameGraph.hpp>
#include <Nazara/Graphics/Config.hpp>
#include <Nazara/Graphics/DepthPipelinePass.hpp>
@@ -41,20 +42,27 @@ namespace Nz
ForwardFramePipeline(ForwardFramePipeline&&) = delete;
~ForwardFramePipeline();
void InvalidateViewer(AbstractViewer* viewerInstance) override;
void InvalidateWorldInstance(WorldInstance* worldInstance) override;
void InvalidateViewer(std::size_t viewerIndex) override;
void InvalidateWorldInstance(std::size_t renderableIndex) override;
void RegisterInstancedDrawable(WorldInstancePtr worldInstance, const InstancedRenderable* instancedRenderable, UInt32 renderMask) override;
void RegisterLight(std::shared_ptr<Light> light, UInt32 renderMask) override;
std::size_t RegisterLight(std::shared_ptr<Light> light, UInt32 renderMask) override;
void RegisterMaterialPass(MaterialPass* materialPass) override;
void RegisterViewer(AbstractViewer* viewerInstance, Int32 renderOrder) override;
std::size_t RegisterRenderable(std::size_t worldInstanceIndex, const InstancedRenderable* instancedRenderable, UInt32 renderMask, const Recti& scissorBox) override;
std::size_t RegisterViewer(AbstractViewer* viewerInstance, Int32 renderOrder) override;
std::size_t RegisterWorldInstance(WorldInstancePtr worldInstance) override;
void Render(RenderFrame& renderFrame) override;
void UnregisterInstancedDrawable(const WorldInstancePtr& worldInstance, const InstancedRenderable* instancedRenderable) override;
void UnregisterLight(Light* light) override;
void UnregisterLight(std::size_t lightIndex) override;
void UnregisterMaterialPass(MaterialPass* material) override;
void UnregisterViewer(AbstractViewer* viewerInstance) override;
void UnregisterRenderable(std::size_t renderableIndex) override;
void UnregisterViewer(std::size_t viewerIndex) override;
void UnregisterWorldInstance(std::size_t worldInstance) override;
void UpdateLightRenderMask(std::size_t lightIndex, UInt32 renderMask);
void UpdateRenderableRenderMask(std::size_t renderableIndex, UInt32 renderMask);
void UpdateRenderableScissorBox(std::size_t renderableIndex, const Recti& scissorBox);
void UpdateViewerRenderMask(std::size_t viewerIndex, Int32 renderOrder);
ForwardFramePipeline& operator=(const ForwardFramePipeline&) = delete;
ForwardFramePipeline& operator=(ForwardFramePipeline&&) = delete;
@@ -81,6 +89,9 @@ namespace Nz
struct RenderableData
{
std::size_t worldInstanceIndex;
const InstancedRenderable* renderable;
Recti scissorBox;
UInt32 renderMask = 0;
NazaraSlot(InstancedRenderable, OnElementInvalidated, onElementInvalidated);
@@ -100,26 +111,27 @@ namespace Nz
std::size_t depthStencilAttachment;
std::unique_ptr<DepthPipelinePass> depthPrepass;
std::unique_ptr<ForwardPipelinePass> forwardPass;
AbstractViewer* viewer;
Int32 renderOrder = 0;
RenderQueueRegistry forwardRegistry;
RenderQueue<RenderElement*> forwardRenderQueue;
ShaderBindingPtr blitShaderBinding;
};
std::size_t m_forwardPassIndex;
std::unordered_map<AbstractViewer*, ViewerData> m_viewers;
std::unordered_map<Light*, LightData> m_lights;
std::unordered_map<MaterialPass*, MaterialPassData> m_activeMaterialPasses;
std::unordered_map<WorldInstancePtr, std::unordered_map<const InstancedRenderable*, RenderableData>> m_renderables;
std::unordered_map<const RenderTarget*, RenderTargetData> m_renderTargets;
std::unordered_set<AbstractViewer*> m_invalidatedViewerInstances;
std::unordered_set<MaterialPass*> m_invalidatedMaterialPasses;
std::unordered_set<WorldInstance*> m_invalidatedWorldInstances;
std::unordered_set<WorldInstancePtr> m_removedWorldInstances;
std::vector<ElementRenderer::RenderStates> m_renderStates;
std::vector<FramePipelinePass::VisibleRenderable> m_visibleRenderables;
std::vector<const Light*> m_visibleLights;
BakedFrameGraph m_bakedFrameGraph;
Bitset<UInt64> m_invalidatedViewerInstances;
Bitset<UInt64> m_invalidatedWorldInstances;
Bitset<UInt64> m_removedWorldInstances;
MemoryPool<RenderableData> m_renderablePool;
MemoryPool<LightData> m_lightPool;
MemoryPool<ViewerData> m_viewerPool;
MemoryPool<WorldInstancePtr> m_worldInstances;
RenderFrame* m_currentRenderFrame;
bool m_rebuildFrameGraph;
};

View File

@@ -40,7 +40,7 @@ namespace Nz
void Prepare(RenderFrame& renderFrame, const Frustumf& frustum, const std::vector<FramePipelinePass::VisibleRenderable>& visibleRenderables, const std::vector<const Light*>& visibleLights, std::size_t visibilityHash);
void RegisterMaterial(const Material& material);
void RegisterToFrameGraph(FrameGraph& frameGraph, std::size_t colorBufferIndex, std::size_t depthBufferIndex);
void RegisterToFrameGraph(FrameGraph& frameGraph, std::size_t colorBufferIndex, std::size_t depthBufferIndex, bool hasDepthPrepass);
void UnregisterMaterial(const Material& material);

View File

@@ -37,22 +37,29 @@ namespace Nz
inline ElementRenderer& GetElementRenderer(std::size_t elementIndex);
inline std::size_t GetElementRendererCount() const;
virtual void InvalidateViewer(AbstractViewer* viewerInstance) = 0;
virtual void InvalidateWorldInstance(WorldInstance* worldInstance) = 0;
virtual void InvalidateViewer(std::size_t viewerIndex) = 0;
virtual void InvalidateWorldInstance(std::size_t) = 0;
template<typename F> void ProcessRenderQueue(const RenderQueue<RenderElement*>& renderQueue, F&& callback);
virtual void RegisterInstancedDrawable(WorldInstancePtr worldInstance, const InstancedRenderable* instancedRenderable, UInt32 renderMask) = 0;
virtual void RegisterLight(std::shared_ptr<Light> light, UInt32 renderMask) = 0;
virtual std::size_t RegisterLight(std::shared_ptr<Light> light, UInt32 renderMask) = 0;
virtual void RegisterMaterialPass(MaterialPass* materialPass) = 0;
virtual void RegisterViewer(AbstractViewer* viewerInstance, Int32 renderOrder) = 0;
virtual std::size_t RegisterRenderable(std::size_t worldInstanceIndex, const InstancedRenderable* instancedRenderable, UInt32 renderMask, const Recti& scissorBox) = 0;
virtual std::size_t RegisterViewer(AbstractViewer* viewerInstance, Int32 renderOrder) = 0;
virtual std::size_t RegisterWorldInstance(WorldInstancePtr worldInstance) = 0;
virtual void Render(RenderFrame& renderFrame) = 0;
virtual void UnregisterInstancedDrawable(const WorldInstancePtr& worldInstance, const InstancedRenderable* instancedRenderable) = 0;
virtual void UnregisterLight(Light* light) = 0;
virtual void UnregisterLight(std::size_t lightIndex) = 0;
virtual void UnregisterMaterialPass(MaterialPass* materialPass) = 0;
virtual void UnregisterViewer(AbstractViewer* viewerInstance) = 0;
virtual void UnregisterRenderable(std::size_t renderableIndex) = 0;
virtual void UnregisterViewer(std::size_t viewerIndex) = 0;
virtual void UnregisterWorldInstance(std::size_t worldInstance) = 0;
virtual void UpdateLightRenderMask(std::size_t lightIndex, UInt32 renderMask) = 0;
virtual void UpdateRenderableRenderMask(std::size_t renderableIndex, UInt32 renderMask) = 0;
virtual void UpdateRenderableScissorBox(std::size_t renderableIndex, const Recti& scissorBox) = 0;
virtual void UpdateViewerRenderMask(std::size_t viewerIndex, Int32 renderOrder) = 0;
FramePipeline& operator=(const FramePipeline&) = delete;
FramePipeline& operator=(FramePipeline&&) noexcept = default;

View File

@@ -8,11 +8,13 @@
#define NAZARA_GRAPHICS_SYSTEMS_RENDERSYSTEM_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/MemoryPool.hpp>
#include <Nazara/Graphics/Graphics.hpp>
#include <Nazara/Graphics/Components/GraphicsComponent.hpp>
#include <Nazara/Graphics/Components/LightComponent.hpp>
#include <Nazara/Utility/Node.hpp>
#include <entt/entt.hpp>
#include <array>
#include <memory>
#include <set>
#include <unordered_map>
@@ -47,19 +49,33 @@ namespace Nz
struct CameraEntity
{
entt::entity entity;
std::size_t poolIndex;
std::size_t viewerIndex;
NazaraSlot(Node, OnNodeInvalidation, onNodeInvalidation);
};
struct GraphicsEntity
{
entt::entity entity;
std::array<std::size_t, GraphicsComponent::MaxRenderableCount> renderableIndices;
std::size_t poolIndex;
std::size_t worldInstanceIndex;
NazaraSlot(GraphicsComponent, OnRenderableAttached, onRenderableAttached);
NazaraSlot(GraphicsComponent, OnRenderableDetach, onRenderableDetach);
NazaraSlot(GraphicsComponent, OnScissorBoxUpdate, onScissorBoxUpdate);
NazaraSlot(GraphicsComponent, OnVisibilityUpdate, onVisibilityUpdate);
NazaraSlot(Node, OnNodeInvalidation, onNodeInvalidation);
};
struct LightEntity
{
entt::entity entity;
std::array<std::size_t, LightComponent::MaxLightCount> lightIndices;
std::size_t poolIndex;
NazaraSlot(LightComponent, OnLightAttached, onLightAttached);
NazaraSlot(LightComponent, OnLightDetach, onLightDetach);
NazaraSlot(LightComponent, OnVisibilityUpdate, onVisibilityUpdate);
@@ -73,17 +89,20 @@ namespace Nz
entt::observer m_cameraConstructObserver;
entt::observer m_graphicsConstructObserver;
entt::observer m_lightConstructObserver;
std::set<entt::entity> m_invalidatedCameraNode;
std::set<entt::entity> m_invalidatedGfxWorldNode;
std::set<entt::entity> m_invalidatedLightWorldNode;
std::set<CameraEntity*> m_invalidatedCameraNode;
std::set<GraphicsEntity*> m_invalidatedGfxWorldNode;
std::set<LightEntity*> m_invalidatedLightWorldNode;
std::unique_ptr<FramePipeline> m_pipeline;
std::unordered_map<entt::entity, CameraEntity> m_cameraEntities;
std::unordered_map<entt::entity, GraphicsEntity> m_graphicsEntities;
std::unordered_map<entt::entity, LightEntity> m_lightEntities;
std::unordered_set<entt::entity> m_newlyHiddenGfxEntities;
std::unordered_set<entt::entity> m_newlyVisibleGfxEntities;
std::unordered_set<entt::entity> m_newlyHiddenLightEntities;
std::unordered_set<entt::entity> m_newlyVisibleLightEntities;
std::unordered_map<entt::entity, CameraEntity*> m_cameraEntities;
std::unordered_map<entt::entity, GraphicsEntity*> m_graphicsEntities;
std::unordered_map<entt::entity, LightEntity*> m_lightEntities;
std::unordered_set<GraphicsEntity*> m_newlyHiddenGfxEntities;
std::unordered_set<GraphicsEntity*> m_newlyVisibleGfxEntities;
std::unordered_set<LightEntity*> m_newlyHiddenLightEntities;
std::unordered_set<LightEntity*> m_newlyVisibleLightEntities;
MemoryPool<CameraEntity> m_cameraEntityPool;
MemoryPool<GraphicsEntity> m_graphicsEntityPool;
MemoryPool<LightEntity> m_lightEntityPool;
};
}