// Copyright (C) 2024 Jérôme "SirLynix" Leclercq (lynix680@gmail.com) // This file is part of the "Nazara Engine - Graphics module" // For conditions of distribution and use, see copyright notice in Export.hpp #pragma once #ifndef NAZARA_GRAPHICS_FRAMEGRAPH_HPP #define NAZARA_GRAPHICS_FRAMEGRAPH_HPP #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace Nz { class NAZARA_GRAPHICS_API FrameGraph { friend class BakedFrameGraph; public: FrameGraph() = default; FrameGraph(const FrameGraph&) = delete; FrameGraph(FrameGraph&&) noexcept = default; ~FrameGraph() = default; inline std::size_t AddAttachment(FramePassAttachment attachment); inline std::size_t AddAttachmentArray(FramePassAttachment attachment, unsigned int layerCount); inline std::size_t AddAttachmentArrayLayer(std::size_t attachmentId, std::size_t layerIndex); inline std::size_t AddAttachmentCube(FramePassAttachment attachment); inline std::size_t AddAttachmentCubeFace(std::size_t attachmentId, CubemapFace face); inline std::size_t AddAttachmentProxy(std::string name, std::size_t attachmentId); inline std::size_t AddDummyAttachment(); inline FramePass& AddPass(std::string name); inline void AddOutput(std::size_t attachmentIndex); BakedFrameGraph Bake(); inline void BindExternalTexture(std::size_t attachmentIndex, std::shared_ptr texture); FrameGraph& operator=(const FrameGraph&) = delete; FrameGraph& operator=(FrameGraph&&) noexcept = default; private: struct PassBarriers; using BarrierList = std::vector; using PassList = std::vector; using AttachmentIdToPassMap = std::unordered_map; using AttachmentIdToPassId = std::unordered_map; using AttachmentIdToTextureId = std::unordered_map; using PassIdToPhysicalPassIndex = std::unordered_map; using TextureBarrier = BakedFrameGraph::TextureBarrier; struct AttachmentArray : FramePassAttachment { unsigned int layerCount; }; struct AttachmentCube : FramePassAttachment { }; struct AttachmentLayer { std::size_t attachmentId; std::size_t layerIndex; }; struct AttachmentProxy { std::size_t attachmentId; std::string name; }; struct Barrier { std::size_t textureId; MemoryAccessFlags access; PipelineStageFlags stages; TextureLayout layout; }; struct DummyAttachment { }; struct PassBarriers { std::vector invalidationBarriers; std::vector flushBarriers; }; struct PhysicalPassData { struct Subpass { std::size_t passIndex; }; std::string name; std::vector textureBarrier; std::vector passes; }; struct WorkData { std::vector> renderPasses; std::vector physicalPasses; std::vector textures; std::vector texture2DPool; std::vector texture2DArrayPool; std::vector textureCubePool; AttachmentIdToPassId attachmentLastUse; AttachmentIdToPassMap attachmentReadList; AttachmentIdToPassMap attachmentWriteList; AttachmentIdToTextureId attachmentToTextures; BarrierList barrierList; PassList passList; PassIdToPhysicalPassIndex passIdToPhysicalPassIndex; }; void AssignPhysicalPasses(); void AssignPhysicalTextures(); void BuildBarriers(); void BuildPhysicalBarriers(); void BuildPhysicalPassDependencies(std::size_t colorAttachmentCount, bool hasDepthStencilAttachment, std::vector& renderPassAttachments, std::vector& subpasses, std::vector& dependencies); void BuildPhysicalPasses(); void BuildReadWriteList(); bool HasAttachment(const std::vector& inputs, std::size_t attachmentIndex) const; void RemoveDuplicatePasses(); std::size_t ResolveAttachmentIndex(std::size_t attachmentIndex) const; void RegisterPassInput(std::size_t passIndex, std::size_t attachmentIndex); std::size_t RegisterTexture(std::size_t attachmentIndex); void ReorderPasses(); void TraverseGraph(std::size_t passIndex); using AttachmentType = std::variant; static constexpr std::size_t InvalidAttachmentIndex = std::numeric_limits::max(); static constexpr std::size_t InvalidTextureIndex = std::numeric_limits::max(); std::vector m_graphOutputs; std::vector m_framePasses; std::vector m_attachments; std::unordered_map> m_externalTextures; WorkData m_pending; }; } #include #endif // NAZARA_GRAPHICS_FRAMEGRAPH_HPP