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_NODE_RENDER_BLOOM_H
17 #define RENDER_NODE_RENDER_BLOOM_H
18
19 #include <array>
20
21 #include <base/containers/string.h>
22 #include <base/containers/string_view.h>
23 #include <base/math/vector.h>
24 #include <render/datastore/render_data_store_render_pods.h>
25 #include <render/namespace.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
RENDER_BEGIN_NAMESPACE()31 RENDER_BEGIN_NAMESPACE()
32 class RenderBloom final {
33 public:
34 RenderBloom() = default;
35 ~RenderBloom() = default;
36
37 struct BloomInfo {
38 BindableImage input;
39 BindableImage output;
40 RenderHandle globalUbo;
41 bool useCompute { false };
42 };
43
44 void Init(IRenderNodeContextManager& renderNodeContextMgr, const BloomInfo& bloomInfo);
45 void PreExecute(IRenderNodeContextManager& renderNodeContextMgr, const BloomInfo& bloomInfo,
46 const PostProcessConfiguration& ppConfig);
47 void Execute(IRenderNodeContextManager& renderNodeContextMgr, IRenderCommandList& cmdList,
48 const PostProcessConfiguration& ppConfig);
49
50 static DescriptorCounts GetDescriptorCounts();
51 // call after PreExecute, to get the output
52 RenderHandle GetFinalTarget() const;
53
54 private:
55 void ComputeBloom(IRenderNodeContextManager& renderNodeContextMgr, IRenderCommandList& cmdList);
56 void ComputeDownscaleAndThreshold(const PushConstant& pc, IRenderCommandList& cmdList);
57 void ComputeDownscale(const PushConstant& pc, IRenderCommandList& cmdList);
58 void ComputeUpscale(const PushConstant& pc, IRenderCommandList& cmdList);
59 void ComputeCombine(const PushConstant& pc, IRenderCommandList& cmdList);
60
61 void GraphicsBloom(IRenderCommandList& cmdList);
62
63 void RenderDownscaleAndThreshold(RenderPass& renderPass, const PushConstant& pc, IRenderCommandList& cmdList);
64 void RenderDownscale(RenderPass& renderPass, const PushConstant& pc, IRenderCommandList& cmdList);
65 void RenderUpscale(RenderPass& renderPass, const PushConstant& pc, IRenderCommandList& cmdList);
66 void RenderCombine(RenderPass& renderPass, const PushConstant& pc, IRenderCommandList& cmdList);
67
68 void CreateTargets(IRenderNodeContextManager& renderNodeContextMgr, const BASE_NS::Math::UVec2 baseSize);
69 void CreatePsos(IRenderNodeContextManager& renderNodeContextMgr);
70 void CreateComputePsos(IRenderNodeContextManager& renderNodeContextMgr);
71 void CreateRenderPsos(IRenderNodeContextManager& renderNodeContextMgr);
72 static std::pair<RenderHandle, const PipelineLayout&> CreateAndReflectRenderPso(
73 IRenderNodeContextManager& renderNodeContextMgr, const Base::string_view shader);
74 void UpdateGlobalSet(IRenderCommandList& cmdList);
75
76 static constexpr uint32_t TARGET_COUNT { 7u };
77 static constexpr uint32_t CORE_BLOOM_QUALITY_LOW { 1u };
78 static constexpr uint32_t CORE_BLOOM_QUALITY_NORMAL { 2u };
79 static constexpr uint32_t CORE_BLOOM_QUALITY_HIGH { 4u };
80 static constexpr int CORE_BLOOM_QUALITY_COUNT { 3u };
81
82 struct Targets {
83 std::array<RenderHandleReference, TARGET_COUNT> tex1;
84 // separate target needed in graphics bloom upscale
85 std::array<RenderHandleReference, TARGET_COUNT> tex2;
86 std::array<BASE_NS::Math::UVec2, TARGET_COUNT> tex1Size;
87 };
88 Targets targets_;
89
90 struct PSOs {
91 struct DownscaleHandles {
92 RenderHandle regular;
93 RenderHandle threshold;
94 };
95
96 std::array<DownscaleHandles, CORE_BLOOM_QUALITY_COUNT> downscaleHandles;
97 std::array<DownscaleHandles, CORE_BLOOM_QUALITY_COUNT> downscaleHandlesCompute;
98
99 RenderHandle downscaleAndThreshold;
100 RenderHandle downscale;
101 RenderHandle upscale;
102 RenderHandle combine;
103
104 ShaderThreadGroup downscaleAndThresholdTGS { 1, 1, 1 };
105 ShaderThreadGroup downscaleTGS { 1, 1, 1 };
106 ShaderThreadGroup upscaleTGS { 1, 1, 1 };
107 ShaderThreadGroup combineTGS { 1, 1, 1 };
108 };
109 PSOs psos_;
110
111 struct Binders {
112 IDescriptorSetBinder::Ptr globalSet0;
113
114 IDescriptorSetBinder::Ptr downscaleAndThreshold;
115 std::array<IDescriptorSetBinder::Ptr, TARGET_COUNT> downscale;
116 std::array<IDescriptorSetBinder::Ptr, TARGET_COUNT> upscale;
117 IDescriptorSetBinder::Ptr combine;
118 };
119 Binders binders_;
120
121 BASE_NS::Math::UVec2 baseSize_ { 0u, 0u };
122 BASE_NS::Math::Vec4 bloomParameters_ { 0.0f, 0.0f, 0.0f, 0.0f };
123 float scaleFactor_ { 1.0f };
124 // calculated from the amount of textures and scale factor
125 size_t frameScaleMaxCount_ { 0 };
126
127 RenderHandleReference samplerHandle_;
128 BASE_NS::Format format_ { BASE_NS::Format::BASE_FORMAT_UNDEFINED };
129
130 ViewportDesc baseViewportDesc_;
131 ScissorDesc baseScissorDesc_;
132
133 bool bloomEnabled_ { false };
134 BloomInfo bloomInfo_;
135 };
136 RENDER_END_NAMESPACE()
137
138 #endif // CORE__RENDER__NODE__RENDER_BLOOM_H
139