FCT
载入中...
搜索中...
未找到
RenderGraph.h
浏览该文件的文档.
1//
2// Created by Administrator on 2025/8/10.
3//
4
5#ifndef RENDERGRAPH_H
6#define RENDERGRAPH_H
7
8#include "./ResourceManager.h"
10#include "../RHI/ImageLayout.h"
12#include "../RHI/AccessFlags.h"
13
14#include "../ThirdParty.h"
15#include "./Format.h"
16#include "../Base/TokenGraph.h"
17#include "../Base/UnionFind.h"
19#include "./ShaderStage.h"
20#include "./ClearTypes.h"
21#include "Vec.h"
22#include "../Base/Graph.h"
23#include "./SizeNode.h"
24#include "CommandBufferGraph.h"
25#include "../Base/UnionFind.h"
27#include "./FlowControl.h"
28#include "./Device.h"
29
30namespace FCT
31{
32 class TextureEdge;
33 class Context;
34 class Window;
35 namespace RHI
36 {
37 class CommandBuffer;
38 }
39}
46namespace FCT
47{
55 {
56 protected:
57 std::string m_name;
58 public:
59 RenderGraphNode() = default;
60 RenderGraphNode(const std::string& name) : m_name(name) {}
61 };
62
63 class Edge
64 {
65 public:
66
67 };
68
69 class TargetEdge : public Edge
70 {
71 public:
72 std::string fromPass;
73 std::string toImage;
74 uint32_t order;
75 };
76
77 class DepthStencilEdge : public Edge
78 {
79 public:
80 std::string fromPass;
81 std::string toImage;
82 uint32_t order;
83 };
84
86 {
87 public:
90 virtual RenderGraphImageNode& operator|=(const Texture& other) = 0;
91 virtual RenderGraphImageNode& operator|=(const Target& other) = 0;
92 virtual RenderGraphImageNode& operator|=(const DepthStencil& other) = 0;
93
95 m_textureOutgoingEdges.push_back(edge);
96 }
97
100 m_targetIncomingEdges.push_back(edge);
101 }
102
105 m_depthStencilIncomingEdges.push_back(edge);
106 }
107
108 const std::vector<TextureEdge*>& getTextureOutgoingEdges() const {
110 }
111
112 const std::vector<TargetEdge*>& getTargetIncomingEdges() const {
114 }
115
116 const std::vector<DepthStencilEdge*>& getDepthStencilIncomingEdges() const {
118 }
119 virtual Image* getImage() const = 0;
120 virtual void fillDefaultData() = 0;
121 private:
122 std::vector<TextureEdge*> m_textureOutgoingEdges;
123 std::vector<TargetEdge*> m_targetIncomingEdges;
124 std::vector<DepthStencilEdge*> m_depthStencilIncomingEdges;
127 };
128}
130namespace FCT
131{
132 class RenderGraphPassNode;
133 class TextureEdge : public Edge
134 {
135 public:
137 std::string fromImage;
138 std::string toPass;
145 };
146
148 {
149 public:
151
152 RenderGraphPassNode(std::string name) : RenderGraphNode(name)
153 {
154
155 }
156
158 m_textureIncomingEdges.push_back(edge);
159 }
160
162 m_targetOutgoingEdges.push_back(edge);
163 }
164
166 m_depthStencilOutgoingEdges.push_back(edge);
167 }
168
169 const std::vector<TextureEdge*>& getTextureIncomingEdges() const {
171 }
172
173 const std::vector<TargetEdge*>& getTargetOutgoingEdges() const {
175 }
176
177 const std::vector<DepthStencilEdge*>& getDepthStencilOutgoingEdges() const {
179 }
180 void applyPassDesc(const PassDesc& desc) {
181 m_clearInfo = desc.clear;
182 }
184 return m_clearInfo;
185 }
186 private:
187 std::vector<TextureEdge*> m_textureIncomingEdges;
188 std::vector<TargetEdge*> m_targetOutgoingEdges;
189 std::vector<DepthStencilEdge*> m_depthStencilOutgoingEdges;
191 };
193 {
194 std::string passName;
196 };
197
199 {
200 constexpr const char* RenderGraphSubmit = "RenderGraphSubmit";
201 }
202 class RenderGraph : private IEventSystem<EventSystemConfig::TriggerOnly>{
203 private:
211 void initForSubmit();
213 public:
214 private:
219 public:
220 RenderGraph(Device* device, FlowControl* flowControl, CommandBufferGraph* commandBufferGraph, ResourceManager* resourceManager);
221 private:
239 std::unordered_map<std::string, std::vector<BarrierInfo>> m_passGroupBarriers;
240 std::map<std::string, std::set<std::string>> m_passGroupDependencies;
241 std::unordered_map<std::string, std::unique_ptr<RenderGraphImageNode>> m_imageNodes;
242 std::unordered_map<std::string,RenderGraphPassNode> m_passNodes;
244 std::vector<std::unique_ptr<Edge>> m_edges;
245 std::unordered_map<std::string, Image*> m_allocatedImages;
246 std::unordered_map<std::string, RHI::Pass*> m_allocatedPasses;
247 RenderGraphImageNode* getOrCreateImageNode(const std::string& name, const Texture& texture);
248 RenderGraphImageNode* getOrCreateImageNode(const std::string& name, const Target& target);
249 RenderGraphImageNode* getOrCreateImageNode(const std::string& name, const DepthStencil& depthStencil);
250 void createTextureEdge(const std::string& passName, const std::string& textureName, const Texture& texture);
251 void createTargetEdge(const std::string& passName, const std::string& targetName, const Target& target);
252 void createDepthStencilEdge(const std::string& passName, const std::string& depthStencilName, const DepthStencil& depthStencil);
253 void simulatePassGroupExecution(const std::string& groupLeader,
254 const std::vector<std::string>& groupMembers,
255 std::unordered_map<Image*, ImageState>& imageStates);
257 std::vector<BarrierInfo> checkBarriersBeforePassGroup(const std::string& groupLeader,
258 const std::vector<std::string>& groupMembers,
259 const std::unordered_map<Image*, ImageState>& imageStates);
260
261 void executeBarriers(RHI::CommandBuffer* cmdBuffer, const std::vector<BarrierInfo>& barriers);
262 void addPass(const PassDesc& desc);
263 void cleanUpCompile();
264 void cleanUp();
282 void resolveTextureSizes();
292 void groupPasses();
293
294 void allocateResources();
303 void createRHIPasses();
304 void analyzePassGroupDependencies(const std::string& groupLeader, const std::vector<std::string>& groupMembers,
305 std::map<std::string, std::set<std::string>>& dependencies) const;
306 std::vector<std::string> topologicalSort(const std::map<std::string, std::set<std::string>>& dependencies) const;
309 std::unordered_map<std::string, RHI::PassGroup*> m_allocatedPassGroups; // 存储创建的PassGroup
321 void createPassGroups();
322 public:
323 template<typename Func>
324 SubscribeId subscribe(std::string passName,Func&& func)
325 {
327 {
328 if (env.passName == passName)
329 {
330 func(env);
331 }
332 });
333 return subId;
334 }
339 template<typename... Args>
340 void addPass(std::string name,Args&&... args)
341 {
342 PassDesc desc(name);
343 desc.processArgs(std::forward<Args>(args)...);
344 addPass(desc);
345 }
357 RHI::Pass* getPass(const std::string& name) const;
358 Image* getImage(const std::string& name) const;
359 std::vector<TextureEdge*> getTextureEdges(const std::string& passName) const;
360 private:
362 std::unordered_map<std::string, std::vector<std::string>> m_passGroupOrders;
363 std::vector<std::string> m_passGroupExecutionOrder;
364 void submitPassGroup(RHI::CommandBuffer* cmdBuffer,
365 const std::string& groupLeader);
366
367 };
368
369} // FCT
370
371#endif //RENDERGRAPH_H
void unsubscribe(SubscribeId subscribeId)
SubscribeId subscribe(Func &&func)
void simulateExecutionAndAnalyzeBarriers()
void simulatePassGroupExecution(const std::string &groupLeader, const std::vector< std::string > &groupMembers, std::unordered_map< Image *, ImageState > &imageStates)
RenderGraph(Device *device, FlowControl *flowControl, CommandBufferGraph *commandBufferGraph, ResourceManager *resourceManager)
void createTextureEdge(const std::string &passName, const std::string &textureName, const Texture &texture)
std::unordered_map< std::string, Image * > m_allocatedImages
void unsubscribe(SubscribeId subscribeId)
void analyzePassGroupBarriers()
std::map< std::string, std::set< std::string > > m_passGroupDependencies
std::unordered_map< std::string, std::unique_ptr< RenderGraphImageNode > > m_imageNodes
SubscribeId subscribe(std::string passName, Func &&func)
void addPass(std::string name, Args &&... args)
FlowControl * m_flowControl
std::vector< TextureEdge * > getTextureEdges(const std::string &passName) const
RenderGraphImageNode * getOrCreateImageNode(const std::string &name, const Texture &texture)
void addPass(const PassDesc &desc)
void submitPassGroup(RHI::CommandBuffer *cmdBuffer, const std::string &groupLeader)
RHI::Pass * getPass(const std::string &name) const
void executeBarriers(RHI::CommandBuffer *cmdBuffer, const std::vector< BarrierInfo > &barriers)
CommandBufferGraph * m_commandBufferGraph
Image * getImage(const std::string &name) const
std::unordered_map< std::string, RHI::PassGroup * > m_allocatedPassGroups
std::unordered_map< std::string, RenderGraphPassNode > m_passNodes
ResourceManager * m_resourceManager
std::vector< std::string > m_passGroupExecutionOrder
std::vector< std::unique_ptr< Edge > > m_edges
UnionFind< std::string, char > m_passesUnions
std::unordered_map< std::string, std::vector< BarrierInfo > > m_passGroupBarriers
void executeAllPassGroups(RHI::CommandBuffer *cmdBuffer)
PipelineStage convertShaderStageToPipelineStage(ShaderStage stage) const
std::vector< std::string > topologicalSort(const std::map< std::string, std::set< std::string > > &dependencies) const
std::vector< BarrierInfo > checkBarriersBeforePassGroup(const std::string &groupLeader, const std::vector< std::string > &groupMembers, const std::unordered_map< Image *, ImageState > &imageStates)
std::unordered_map< std::string, RHI::Pass * > m_allocatedPasses
void createDepthStencilEdge(const std::string &passName, const std::string &depthStencilName, const DepthStencil &depthStencil)
void analyzePassGroupDependencies(const std::string &groupLeader, const std::vector< std::string > &groupMembers, std::map< std::string, std::set< std::string > > &dependencies) const
void createTargetEdge(const std::string &passName, const std::string &targetName, const Target &target)
std::unordered_map< std::string, std::vector< std::string > > m_passGroupOrders
CommandBufferToken m_commandBufferToken
RenderGraphImageNode(const std::string &name)
std::vector< TargetEdge * > m_targetIncomingEdges
void addOutgoingEdge(TextureEdge *edge)
virtual Image * getImage() const =0
virtual RenderGraphImageNode & operator|=(const DepthStencil &other)=0
const std::vector< TargetEdge * > & getTargetIncomingEdges() const
std::vector< DepthStencilEdge * > m_depthStencilIncomingEdges
const std::vector< DepthStencilEdge * > & getDepthStencilIncomingEdges() const
std::vector< TextureEdge * > m_textureOutgoingEdges
virtual RenderGraphImageNode & operator|=(const Texture &other)=0
void addIncomingEdge(TargetEdge *edge)
const std::vector< TextureEdge * > & getTextureOutgoingEdges() const
void addIncomingEdge(DepthStencilEdge *edge)
virtual void fillDefaultData()=0
virtual RenderGraphImageNode & operator|=(const Target &other)=0
RenderGraphNode()=default
RenderGraphNode(const std::string &name)
void addOutgoingEdge(DepthStencilEdge *edge)
std::vector< TargetEdge * > m_targetOutgoingEdges
const std::vector< DepthStencilEdge * > & getDepthStencilOutgoingEdges() const
std::vector< TextureEdge * > m_textureIncomingEdges
RenderGraphPassNode(std::string name)
void addOutgoingEdge(TargetEdge *edge)
std::vector< DepthStencilEdge * > m_depthStencilOutgoingEdges
const std::vector< TextureEdge * > & getTextureIncomingEdges() const
const std::vector< TargetEdge * > & getTargetOutgoingEdges() const
void applyPassDesc(const PassDesc &desc)
void addIncomingEdge(TextureEdge *edge)
const EnablePassClear & getClearInfo() const
constexpr const char * RenderGraphSubmit
std::size_t SubscribeId
CommandBufferNodes::CommandBuffer * CommandBufferToken
void processArgs(const EnablePassClear &other, Rest &&... rest)
RHI::CommandBuffer * cmdBuf
SizeNode() noexcept