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