1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef RENDER_RENDER__RENDER_NODE_GRAPH_NODE_STORE_H
17 #define RENDER_RENDER__RENDER_NODE_GRAPH_NODE_STORE_H
18 
19 #include <base/containers/string.h>
20 #include <base/containers/string_view.h>
21 #include <base/containers/unique_ptr.h>
22 #include <base/containers/vector.h>
23 #include <render/namespace.h>
24 #include <render/nodecontext/intf_render_node.h>
25 #include <render/render_data_structures.h>
26 
27 #include "nodecontext/node_context_descriptor_set_manager.h"
28 #include "nodecontext/node_context_pool_manager.h"
29 #include "nodecontext/node_context_pso_manager.h"
30 #include "nodecontext/render_barrier_list.h"
31 #include "nodecontext/render_command_list.h"
32 #include "nodecontext/render_node_context_manager.h"
33 #include "nodecontext/render_node_graph_share_manager.h"
34 #include "nodecontext/render_node_manager.h"
35 
36 RENDER_BEGIN_NAMESPACE()
37 class IRenderBackendNode;
38 class RenderFrameSync;
39 class RenderFrameUtil;
40 class RenderNodeGpuResourceManager;
41 
42 struct SubmitDependencies {
43     // should this command buffer signal when submitting
44     bool signalSemaphore { false };
45     // valid command buffer indices for wait semaphores
46     uint32_t waitSemaphoreCount { 0u };
47     // render node indices or actual render command context indices (depending on the use context)
48     uint32_t waitSemaphoreNodeIndices[PipelineStateConstants::MAX_RENDER_NODE_GPU_WAIT_SIGNALS] {};
49     // wait for acquired swapchain image
50     bool waitForSwapchainAcquireSignal { false };
51 };
52 
53 struct RenderCommandContext {
54     // if backend specific node code
55     IRenderBackendNode* renderBackendNode { nullptr };
56     RenderCommandList* renderCommandList { nullptr };
57     RenderBarrierList* renderBarrierList { nullptr };
58     NodeContextPsoManager* nodeContextPsoMgr { nullptr };
59     NodeContextDescriptorSetManager* nodeContextDescriptorSetMgr { nullptr };
60     NodeContextPoolManager* nodeContextPoolMgr { nullptr };
61 
62     uint32_t renderGraphRenderNodeIndex { ~0u };
63     SubmitDependencies submitDepencies;
64 
65     BASE_NS::string_view debugName; // full node name with RNG name + node name
66 };
67 
68 struct RenderCommandFrameData {
69     BASE_NS::vector<RenderCommandContext> renderCommandContexts;
70     RenderFrameSync* renderFrameSync { nullptr };
71     RenderFrameUtil* renderFrameUtil { nullptr };
72     // the index of the first render node in backend which needs swapchain acquired image
73     uint32_t firstSwapchainNodeIdx { ~0u };
74 };
75 
76 struct RenderNodeContextData {
77     struct CpuDependency {
78         BASE_NS::vector<RenderDataConstants::RenderDataFixedString> cpuDependencyWaitRenderNodes;
79     };
80 
81     CpuDependency cpuDependency;
82     SubmitDependencies submitInfo;
83 
84     BASE_NS::unique_ptr<RenderCommandList> renderCommandList;
85     BASE_NS::unique_ptr<RenderBarrierList> renderBarrierList;
86     BASE_NS::unique_ptr<RenderNodeContextManager> renderNodeContextManager;
87     BASE_NS::unique_ptr<NodeContextPsoManager> nodeContextPsoMgr;
88     BASE_NS::unique_ptr<NodeContextDescriptorSetManager> nodeContextDescriptorSetMgr;
89     BASE_NS::unique_ptr<NodeContextPoolManager> nodeContextPoolMgr;
90 
91     // with dynamic render node graphs we need initilization data per render node
92     bool initialized { false };
93 
94     // optional render backend node pointer
95     IRenderBackendNode* renderBackendNode { nullptr };
96 };
97 
98 struct RenderNodeGraphShareData {
99     // render node graph inputs/outputs which can be set through render node graph manager
100     static constexpr uint32_t MAX_RENDER_NODE_GRAPH_RES_COUNT { 4u };
101     IRenderNodeGraphShareManager::NamedResource inputs[MAX_RENDER_NODE_GRAPH_RES_COUNT] { {}, {}, {}, {} };
102     IRenderNodeGraphShareManager::NamedResource outputs[MAX_RENDER_NODE_GRAPH_RES_COUNT] { {}, {}, {}, {} };
103     uint32_t inputCount { 0 };
104     uint32_t outputCount { 0 };
105 };
106 
107 /**
108  * RenderNodeGraphNodeStore.
109  * Store render nodes and related data per render node graph.
110  * NOTE: When running the render node graphs, the real global name used is render node graph name + render node name
111  */
112 struct RenderNodeGraphNodeStore {
113     struct RenderNodeData {
114         BASE_NS::unique_ptr<IRenderNode, RenderNodeTypeInfo::DestroyRenderNodeFn> node;
115         RenderDataConstants::RenderDataFixedString typeName;
116         RenderDataConstants::RenderDataFixedString fullName; // rng name + node name
117         RenderDataConstants::RenderDataFixedString nodeName; // node name
118         // NOTE: should be cleared after first init if not dev-mode
119         BASE_NS::unique_ptr<RenderNodeGraphInputs> inputData;
120         BASE_NS::string nodeJson;
121     };
122     BASE_NS::vector<RenderNodeData> renderNodeData;
123     BASE_NS::vector<RenderNodeContextData> renderNodeContextData;
124     // NOTE: used in backend and accessed in update
125     RenderNodeGraphShareData renderNodeGraphShareData;
126 
127     BASE_NS::unique_ptr<RenderNodeGraphShareDataManager> renderNodeGraphShareDataMgr;
128 
129     bool initialized { false };
130     bool dynamic { false };
131 
132     RenderDataConstants::RenderDataFixedString renderNodeGraphName;
133     RenderDataConstants::RenderDataFixedString renderNodeGraphDataStoreName;
134     RenderDataConstants::RenderDataFixedString renderNodeGraphUri;
135 };
136 RENDER_END_NAMESPACE()
137 
138 #endif // CORE__RENDER__RENDER_NODE_GRAPH_NODE_STORE_H
139