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__NODE__RENDER_NODE_SINGLE_POST_PROCESS_H
17 #define RENDER_RENDER__NODE__RENDER_NODE_SINGLE_POST_PROCESS_H
18 
19 #include <base/util/uid.h>
20 #include <render/datastore/intf_render_data_store_post_process.h>
21 #include <render/datastore/render_data_store_render_pods.h>
22 #include <render/device/pipeline_state_desc.h>
23 #include <render/namespace.h>
24 #include <render/nodecontext/intf_pipeline_descriptor_set_binder.h>
25 #include <render/nodecontext/intf_render_node.h>
26 #include <render/render_data_structures.h>
27 #include <render/resource_handle.h>
28 
29 #include "node/render_bloom.h"
30 #include "node/render_blur.h"
31 #include "node/render_copy.h"
32 #include "util/log.h"
33 
34 RENDER_BEGIN_NAMESPACE()
35 class IGpuResourceManager;
36 class IRenderCommandList;
37 class IRenderNodeContextManager;
38 class IRenderNodeRenderDataStoreManager;
39 struct RenderNodeGraphInputs;
40 
41 class RenderNodeSinglePostProcess final : public IRenderNode {
42 public:
43     RenderNodeSinglePostProcess() = default;
44     ~RenderNodeSinglePostProcess() override = default;
45 
46     void InitNode(IRenderNodeContextManager& renderNodeContextMgr) override;
47     void PreExecuteFrame() override;
48     void ExecuteFrame(IRenderCommandList& cmdList) override;
GetExecuteFlags()49     ExecuteFlags GetExecuteFlags() const override
50     {
51         return 0U;
52     }
53 
54     // for plugin / factory interface
55     static constexpr BASE_NS::Uid UID { "f4495799-9db7-477f-9eaf-6fad26260304" };
56     static constexpr char const* TYPE_NAME = "CORE_RN_SINGLE_POST_PROCESS";
57     static constexpr IRenderNode::BackendFlags BACKEND_FLAGS = IRenderNode::BackendFlagBits::BACKEND_FLAG_BITS_DEFAULT;
58     static constexpr IRenderNode::ClassType CLASS_TYPE = IRenderNode::ClassType::CLASS_TYPE_NODE;
59     static IRenderNode* Create();
60     static void Destroy(IRenderNode* instance);
61 
62 private:
63     IRenderNodeContextManager* renderNodeContextMgr_ { nullptr };
64 
65     void ParseRenderNodeInputs();
66     void InitCreateBinders();
67     void UpdateGlobalPostProcessUbo();
68     void UpdateImageData();
69     void ProcessPostProcessConfiguration();
70     void RegisterOutputs(const RenderHandle output);
71     void BindDefaultResources(const uint32_t set, const DescriptorSetLayoutBindingResources& bindings);
72     void ExecuteSinglePostProcess(IRenderCommandList& cmdList);
73     void ExecuteCopy(IRenderCommandList& cmdList);
74 
75     enum class DefaultOutputImage : uint32_t {
76         OUTPUT = 0,
77         INPUT_OUTPUT_COPY = 1,
78         INPUT = 2,
79         WHITE = 3,
80         BLACK = 4,
81     };
82     // Json resources which might need re-fetching
83     struct JsonInputs {
84         RenderNodeGraphInputs::InputRenderPass renderPass;
85         RenderNodeGraphInputs::InputResources resources;
86         RenderNodeGraphInputs::InputResources dispatchResources;
87 
88         RenderNodeGraphInputs::RenderDataStore renderDataStore;
89 
90         BASE_NS::string ppName;
91         DefaultOutputImage defaultOutputImage { DefaultOutputImage::OUTPUT };
92 
93         bool hasChangeableRenderPassHandles { false };
94         bool hasChangeableResourceHandles { false };
95         bool hasChangeableDispatchHandles { false };
96 
97         uint32_t inputIdx { ~0u };
98         uint32_t outputIdx { ~0u };
99     };
100     JsonInputs jsonInputs_;
101 
102     struct BuiltInVariables {
103         RenderHandle input;
104 
105         RenderHandle output;
106 
107         RenderHandle defBuffer;
108         RenderHandle defBlackImage;
109         RenderHandle defWhiteImage;
110         RenderHandle defSampler;
111 
112         // the flag for the post built-in post process
113         uint32_t postProcessFlag { 0u };
114     };
115     BuiltInVariables builtInVariables_;
116 
117     RenderNodeHandles::InputRenderPass inputRenderPass_;
118     RenderNodeHandles::InputResources inputResources_;
119     RenderNodeHandles::InputResources dispatchResources_;
120     RenderHandle shader_;
121     PipelineLayout pipelineLayout_;
122     RenderHandle psoHandle_;
123     PushConstant pushConstant_;
124     bool graphics_ { true };
125     ShaderThreadGroup threadGroupSize_ { 1u, 1u, 1u };
126 
127     IPipelineDescriptorSetBinder::Ptr pipelineDescriptorSetBinder_;
128     IDescriptorSetBinder::Ptr copyBinder_;
129 
130     RenderBloom renderBloom_;
131     RenderBlur renderBlur_;
132     RenderCopy renderCopy_;
133 
134     PostProcessConfiguration ppGlobalConfig_;
135     IRenderDataStorePostProcess::PostProcess ppLocalConfig_;
136 
137     struct UboHandles {
138         // first 512 aligned is global post process
139         // after (256) we have effect local data
140         RenderHandleReference postProcess;
141 
142         uint32_t effectLocalDataIndex { 1u };
143     };
144     UboHandles ubos_;
145 
146     bool useAutoBindSet0_ { false };
147     bool valid_ { false };
148 };
149 RENDER_END_NAMESPACE()
150 
151 #endif // CORE__RENDER__NODE__RENDER_NODE_SINGLE_POST_PROCESS_H
152