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 CORE__RENDER__NODE__RENDER_NODE_DEFAULT_SHADOWS_BLUR_H
17 #define CORE__RENDER__NODE__RENDER_NODE_DEFAULT_SHADOWS_BLUR_H
18 
19 #include <3d/render/intf_render_data_store_default_light.h>
20 #include <base/containers/vector.h>
21 #include <base/math/vector.h>
22 #include <base/util/uid.h>
23 #include <core/namespace.h>
24 #include <render/device/gpu_resource_desc.h>
25 #include <render/device/pipeline_state_desc.h>
26 #include <render/nodecontext/intf_pipeline_descriptor_set_binder.h>
27 #include <render/nodecontext/intf_render_node.h>
28 #include <render/render_data_structures.h>
29 #include <render/resource_handle.h>
30 
31 #include "render/render_node_scene_util.h"
32 
CORE3D_BEGIN_NAMESPACE()33 CORE3D_BEGIN_NAMESPACE()
34 class RenderNodeDefaultShadowsBlur final : public RENDER_NS::IRenderNode {
35 public:
36     RenderNodeDefaultShadowsBlur() = default;
37     ~RenderNodeDefaultShadowsBlur() override = default;
38 
39     void InitNode(RENDER_NS::IRenderNodeContextManager& renderNodeContextMgr) override;
40     void PreExecuteFrame() override;
41     void ExecuteFrame(RENDER_NS::IRenderCommandList& cmdList) override;
42     ExecuteFlags GetExecuteFlags() const override;
43 
44     // for plugin / factory interface
45     static constexpr BASE_NS::Uid UID { "6bf65ac3-6ede-4baa-a61a-2207085b235c" };
46     static constexpr char const* const TYPE_NAME = "RenderNodeDefaultShadowsBlur";
47     static constexpr IRenderNode::BackendFlags BACKEND_FLAGS = IRenderNode::BackendFlagBits::BACKEND_FLAG_BITS_DEFAULT;
48     static constexpr IRenderNode::ClassType CLASS_TYPE = IRenderNode::ClassType::CLASS_TYPE_NODE;
49     static IRenderNode* Create();
50     static void Destroy(IRenderNode* instance);
51 
52 private:
53     RENDER_NS::IRenderNodeContextManager* renderNodeContextMgr_ { nullptr };
54 
55     struct TemporaryImage {
56         RENDER_NS::RenderHandleReference imageHandle;
57         uint32_t width { 0u };
58         uint32_t height { 0u };
59         BASE_NS::Format format { BASE_NS::Format::BASE_FORMAT_UNDEFINED };
60         RENDER_NS::SampleCountFlags sampleCountFlags { RENDER_NS::SampleCountFlagBits::CORE_SAMPLE_COUNT_1_BIT };
61     };
62 
63     void ProcessSingleShadow(RENDER_NS::IRenderCommandList& cmdList, const uint32_t drawIdx,
64         const RENDER_NS::RenderHandle imageHandle, const TemporaryImage& tempImage);
65     void RenderData(RENDER_NS::IRenderCommandList& cmdList, const RENDER_NS::RenderPass& renderPassBase,
66         const RENDER_NS::ViewportDesc& viewport, const RENDER_NS::ScissorDesc& scissor,
67         const RENDER_NS::RenderHandle inputHandle, const RENDER_NS::RenderHandle outputHandle, const uint32_t drawIdx,
68         const BASE_NS::Math::Vec4 texSizeInvTexSize);
69     void RenderBlur(RENDER_NS::IRenderCommandList& cmdList, const RENDER_NS::RenderPass& renderPass,
70         const RENDER_NS::ViewportDesc& viewport, const RENDER_NS::ScissorDesc& scissor,
71         const RENDER_NS::IDescriptorSetBinder::Ptr& binder, const BASE_NS::Math::Vec4& texSizeInvTexSize,
72         const BASE_NS::Math::Vec4& dir, RENDER_NS::RenderHandle imageHandle);
73     void CreateDescriptorSets();
74 
75     void ExplicitInputBarrier(RENDER_NS::IRenderCommandList& cmdList, const RENDER_NS::RenderHandle handle);
76     void ExplicitOutputBarrier(RENDER_NS::IRenderCommandList& cmdList, const RENDER_NS::RenderHandle handle);
77 
78     TemporaryImage temporaryImage_;
79     RENDER_NS::RenderHandle shadowColorBufferHandle_;
80 
81     RENDER_NS::RenderHandle samplerHandle_;
82     RENDER_NS::RenderHandle bufferHandle_;
83 
84     struct AllDescriptorSets {
85         RENDER_NS::IDescriptorSetBinder::Ptr globalSet;
86 
87         BASE_NS::vector<RENDER_NS::IDescriptorSetBinder::Ptr> set1Horizontal;
88         BASE_NS::vector<RENDER_NS::IDescriptorSetBinder::Ptr> set1Vertical;
89     };
90     AllDescriptorSets allDescriptorSets_;
91 
92     struct ShaderData {
93         RENDER_NS::RenderHandle shaderHandle;
94         RENDER_NS::PushConstant pushConstant;
95 
96         RENDER_NS::RenderHandle psoHandle;
97     };
98     ShaderData shaderData_;
99 
100     SceneRenderDataStores stores_;
101 
102     IRenderDataStoreDefaultLight::ShadowTypes shadowTypes_;
103     uint32_t shadowCount_ { 0U };
104 };
105 CORE3D_END_NAMESPACE()
106 
107 #endif // CORE__RENDER__NODE__RENDER_NODE_DEFAULT_SHADOWS_BLUR_H
108