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 #include "render_node_context_manager.h"
17 
18 #include <cstdint>
19 
20 #include <render/intf_render_context.h>
21 #include <render/namespace.h>
22 #include <render/nodecontext/intf_node_context_descriptor_set_manager.h>
23 #include <render/nodecontext/intf_node_context_pso_manager.h>
24 #include <render/nodecontext/intf_render_node_context_manager.h>
25 
26 #include "datastore/render_data_store_manager.h"
27 #include "device/gpu_resource_manager.h"
28 #include "device/shader_manager.h"
29 #include "nodecontext/node_context_descriptor_set_manager.h"
30 #include "nodecontext/node_context_pso_manager.h"
31 #include "nodecontext/render_command_list.h"
32 #include "nodecontext/render_node_graph_share_manager.h"
33 #include "nodecontext/render_node_parser_util.h"
34 #include "nodecontext/render_node_util.h"
35 
36 using namespace BASE_NS;
37 
RENDER_BEGIN_NAMESPACE()38 RENDER_BEGIN_NAMESPACE()
39 RenderNodeContextManager::RenderNodeContextManager(const CreateInfo& createInfo)
40     : renderContext_(createInfo.renderContext), renderNodeGraphData_(createInfo.renderNodeGraphData),
41       renderNodeGraphInputs_(createInfo.renderNodeGraphInputs),
42       fullName_(renderNodeGraphData_.renderNodeGraphName + createInfo.name), nodeName_(createInfo.name),
43       nodeJson_(createInfo.nodeJson), renderNodeGraphShareDataMgr_(createInfo.renderNodeGraphShareDataMgr),
44       descriptorSetMgr_(createInfo.descriptorSetMgr), psoMgr_(createInfo.psoMgr), renderCommandList_(createInfo.cmdList)
45 {
46     IDevice& dev = createInfo.renderContext.GetDevice();
47     renderNodeGpuResourceMgr_ = make_unique<RenderNodeGpuResourceManager>(
48         static_cast<GpuResourceManager&>(renderContext_.GetDevice().GetGpuResourceManager()));
49     renderNodeShaderMgr_ = make_unique<RenderNodeShaderManager>((ShaderManager&)dev.GetShaderManager());
50     renderNodeRenderDataStoreMgr_ = make_unique<RenderNodeRenderDataStoreManager>(
51         (RenderDataStoreManager&)renderContext_.GetRenderDataStoreManager());
52     renderNodeUtil_ = make_unique<RenderNodeUtil>(*this);
53     renderNodeGraphShareMgr_ = make_unique<RenderNodeGraphShareManager>(renderNodeGraphShareDataMgr_);
54     renderNodeParserUtil_ = make_unique<RenderNodeParserUtil>(RenderNodeParserUtil::CreateInfo {});
55 
56     // there are only build-in render node context interfaces
57     contextInterfaces_.push_back({ RenderNodeContextManager::UID, this });
58     contextInterfaces_.push_back({ RenderCommandList::UID, &renderCommandList_ });
59     contextInterfaces_.push_back({ RenderNodeGraphShareManager::UID, renderNodeGraphShareMgr_.get() });
60     contextInterfaces_.push_back({ RenderNodeParserUtil::UID, renderNodeParserUtil_.get() });
61 }
62 
63 RenderNodeContextManager::~RenderNodeContextManager() = default;
64 
BeginFrame(const uint32_t renderNodeIdx,const PerFrameTimings & frameTimings)65 void RenderNodeContextManager::BeginFrame(const uint32_t renderNodeIdx, const PerFrameTimings& frameTimings)
66 {
67     if (renderNodeIdx_ == ~0u) {
68         renderNodeGraphShareMgr_->Init(renderNodeIdx, nodeName_, fullName_);
69     }
70     renderNodeIdx_ = renderNodeIdx;
71     renderNodeGraphShareMgr_->BeginFrame(renderNodeIdx);
72     {
73         // automatic timings to render nodes
74         constexpr double uToMsDiv = 1000.0;
75         constexpr double uToSDiv = 1000000.0;
76         const float deltaTime = static_cast<float>(
77             static_cast<double>(frameTimings.deltaTimeUs) / uToMsDiv); // real delta time used for scene as well
78         const float totalTime = static_cast<float>(static_cast<double>(frameTimings.totalTimeUs) / uToSDiv);
79         const uint32_t frameIndex =
80             static_cast<uint32_t>((frameTimings.frameIndex % std::numeric_limits<uint32_t>::max()));
81         auto& timings = renderNodeGraphData_.renderingConfiguration.renderTimings;
82         timings = { deltaTime, deltaTime, totalTime, *reinterpret_cast<const float*>(&frameIndex) };
83     }
84 }
85 
GetRenderDataStoreManager() const86 const IRenderNodeRenderDataStoreManager& RenderNodeContextManager::GetRenderDataStoreManager() const
87 {
88     return *renderNodeRenderDataStoreMgr_;
89 }
90 
GetShaderManager() const91 const IRenderNodeShaderManager& RenderNodeContextManager::GetShaderManager() const
92 {
93     return *renderNodeShaderMgr_;
94 }
95 
GetGpuResourceManager()96 IRenderNodeGpuResourceManager& RenderNodeContextManager::GetGpuResourceManager()
97 {
98     return *renderNodeGpuResourceMgr_;
99 }
100 
GetDescriptorSetManager()101 INodeContextDescriptorSetManager& RenderNodeContextManager::GetDescriptorSetManager()
102 {
103     return descriptorSetMgr_;
104 }
105 
GetPsoManager()106 INodeContextPsoManager& RenderNodeContextManager::GetPsoManager()
107 {
108     return psoMgr_;
109 }
110 
GetRenderNodeGraphShareManager()111 IRenderNodeGraphShareManager& RenderNodeContextManager::GetRenderNodeGraphShareManager()
112 {
113     return *renderNodeGraphShareMgr_;
114 }
115 
GetRenderNodeUtil() const116 const IRenderNodeUtil& RenderNodeContextManager::GetRenderNodeUtil() const
117 {
118     return *renderNodeUtil_;
119 }
120 
GetRenderNodeParserUtil() const121 const IRenderNodeParserUtil& RenderNodeContextManager::GetRenderNodeParserUtil() const
122 {
123     return *renderNodeParserUtil_;
124 }
125 
GetRenderNodeGraphData() const126 const RenderNodeGraphData& RenderNodeContextManager::GetRenderNodeGraphData() const
127 {
128     return renderNodeGraphData_;
129 }
130 
GetName() const131 string_view RenderNodeContextManager::GetName() const
132 {
133     return fullName_;
134 }
135 
GetNodeName() const136 string_view RenderNodeContextManager::GetNodeName() const
137 {
138     return nodeName_;
139 }
140 
GetNodeJson() const141 CORE_NS::json::value RenderNodeContextManager::GetNodeJson() const
142 {
143     return CORE_NS::json::parse(nodeJson_.data());
144 }
145 
GetRenderNodeGraphInputs() const146 const RenderNodeGraphInputs& RenderNodeContextManager::GetRenderNodeGraphInputs() const
147 {
148     return renderNodeGraphInputs_;
149 }
150 
GetRenderContext() const151 IRenderContext& RenderNodeContextManager::GetRenderContext() const
152 {
153     return renderContext_;
154 }
155 
GetRenderNodeContextInterface(const Uid & uid) const156 CORE_NS::IInterface* RenderNodeContextManager::GetRenderNodeContextInterface(const Uid& uid) const
157 {
158     for (const auto& ref : contextInterfaces_) {
159         if (ref.uid == uid) {
160             return ref.contextInterface;
161         }
162     }
163     return nullptr;
164 }
165 
GetInterface(const BASE_NS::Uid & uid) const166 const CORE_NS::IInterface* RenderNodeContextManager::GetInterface(const BASE_NS::Uid& uid) const
167 {
168     if ((uid == IRenderNodeContextManager::UID) || (uid == IInterface::UID)) {
169         return this;
170     }
171     return nullptr;
172 }
173 
GetInterface(const BASE_NS::Uid & uid)174 CORE_NS::IInterface* RenderNodeContextManager::GetInterface(const BASE_NS::Uid& uid)
175 {
176     if ((uid == IRenderNodeContextManager::UID) || (uid == IInterface::UID)) {
177         return this;
178     }
179     return nullptr;
180 }
181 
Ref()182 void RenderNodeContextManager::Ref() {}
183 
Unref()184 void RenderNodeContextManager::Unref() {}
185 RENDER_END_NAMESPACE()
186