1 /* 2 * Copyright (c) 2022 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_POSTPROCESS_RENDER_POST_PROCESS_BLOOM_NODE_H 17 #define RENDER_POSTPROCESS_RENDER_POST_PROCESS_BLOOM_NODE_H 18 19 #include <array> 20 21 #include <base/containers/string.h> 22 #include <base/containers/string_view.h> 23 #include <base/containers/unique_ptr.h> 24 #include <base/math/vector.h> 25 #include <base/util/uid.h> 26 #include <core/plugin/intf_interface.h> 27 #include <core/plugin/intf_interface_helper.h> 28 #include <core/property/intf_property_api.h> 29 #include <core/property/intf_property_handle.h> 30 #include <core/property/property_types.h> 31 #include <core/property_tools/property_api_impl.h> 32 #include <core/property_tools/property_macros.h> 33 #include <render/datastore/render_data_store_render_pods.h> 34 #include <render/device/intf_shader_manager.h> 35 #include <render/namespace.h> 36 #include <render/nodecontext/intf_pipeline_descriptor_set_binder.h> 37 #include <render/nodecontext/intf_render_node.h> 38 #include <render/nodecontext/intf_render_post_process.h> 39 #include <render/nodecontext/intf_render_post_process_node.h> 40 #include <render/render_data_structures.h> 41 42 RENDER_BEGIN_NAMESPACE() 43 class IRenderCommandList; 44 class IRenderNodeRenderDataStoreManager; 45 struct RenderNodeGraphInputs; 46 47 class RenderPostProcessBloomNode final : public CORE_NS::IInterfaceHelper<IRenderPostProcessNode> { 48 public: 49 static constexpr auto UID = BASE_NS::Uid("89b83608-c910-433e-9fd8-22f3ef64eabd"); 50 51 using Ptr = BASE_NS::refcnt_ptr<RenderPostProcessBloomNode>; 52 53 RenderPostProcessBloomNode(); 54 ~RenderPostProcessBloomNode() = default; 55 56 CORE_NS::IPropertyHandle* GetRenderInputProperties() override; 57 CORE_NS::IPropertyHandle* GetRenderOutputProperties() override; 58 DescriptorCounts GetRenderDescriptorCounts() const override; 59 void SetRenderAreaRequest(const RenderAreaRequest& renderAreaRequest) override; 60 IRenderNode::ExecuteFlags GetExecuteFlags() const override; 61 62 RenderHandle GetFinalTarget() const; 63 64 struct EffectProperties { 65 bool enabled { false }; 66 BloomConfiguration bloomConfiguration; 67 }; 68 69 void Init(const IRenderPostProcess::Ptr& postProcess, IRenderNodeContextManager& renderNodeContextMgr) override; 70 void PreExecute() override; 71 void Execute(IRenderCommandList& cmdList) override; 72 73 // for plugin / factory interface 74 static constexpr const char* TYPE_NAME = "RenderPostProcessBloomNode"; 75 76 struct NodeInputs { 77 BindableImage input; 78 }; 79 struct NodeOutputs { 80 BindableImage output; 81 }; 82 83 NodeInputs nodeInputsData; 84 NodeOutputs nodeOutputsData; 85 86 private: 87 IRenderNodeContextManager* renderNodeContextMgr_ { nullptr }; 88 IRenderPostProcess::Ptr postProcess_; 89 90 void ComputeBloom(IRenderCommandList& cmdList); 91 void ComputeDownscaleAndThreshold(const PushConstant& pc, IRenderCommandList& cmdList); 92 void ComputeDownscale(const PushConstant& pc, IRenderCommandList& cmdList); 93 void ComputeUpscale(const PushConstant& pc, IRenderCommandList& cmdList); 94 void ComputeCombine(const PushConstant& pc, IRenderCommandList& cmdList); 95 96 void RenderDownscaleAndThreshold(RenderPass& renderPass, const PushConstant& pc, IRenderCommandList& cmdList); 97 void RenderDownscale(RenderPass& renderPass, const PushConstant& pc, IRenderCommandList& cmdList); 98 void RenderUpscale(RenderPass& renderPass, const PushConstant& pc, IRenderCommandList& cmdList); 99 void RenderCombine(RenderPass& renderPass, const PushConstant& pc, IRenderCommandList& cmdList); 100 101 void GraphicsBloom(IRenderCommandList& cmdList); 102 103 void CreateTargets(const BASE_NS::Math::UVec2 baseSize); 104 void CreatePsos(); 105 void CreateComputePsos(); 106 void CreateRenderPsos(); 107 108 std::pair<RenderHandle, const PipelineLayout&> CreateAndReflectRenderPso(const BASE_NS::string_view shader); 109 110 static constexpr uint32_t TARGET_COUNT { 7u }; 111 static constexpr uint32_t CORE_BLOOM_QUALITY_LOW { 1u }; 112 static constexpr uint32_t CORE_BLOOM_QUALITY_NORMAL { 2u }; 113 static constexpr uint32_t CORE_BLOOM_QUALITY_HIGH { 4u }; 114 static constexpr int CORE_BLOOM_QUALITY_COUNT { 3u }; 115 116 struct Targets { 117 std::array<RenderHandleReference, TARGET_COUNT> tex1; 118 // separate target needed in graphics bloom upscale 119 std::array<RenderHandleReference, TARGET_COUNT> tex2; 120 std::array<BASE_NS::Math::UVec2, TARGET_COUNT> tex1Size; 121 }; 122 Targets targets_; 123 124 struct PSOs { 125 struct DownscaleHandles { 126 RenderHandle regular; 127 RenderHandle threshold; 128 }; 129 130 std::array<DownscaleHandles, CORE_BLOOM_QUALITY_COUNT> downscaleHandles; 131 std::array<DownscaleHandles, CORE_BLOOM_QUALITY_COUNT> downscaleHandlesCompute; 132 133 RenderHandle downscaleAndThreshold; 134 RenderHandle downscale; 135 RenderHandle upscale; 136 RenderHandle combine; 137 138 ShaderThreadGroup downscaleAndThresholdTGS { 1, 1, 1 }; 139 ShaderThreadGroup downscaleTGS { 1, 1, 1 }; 140 ShaderThreadGroup upscaleTGS { 1, 1, 1 }; 141 ShaderThreadGroup combineTGS { 1, 1, 1 }; 142 }; 143 PSOs psos_; 144 145 struct Binders { 146 IDescriptorSetBinder::Ptr downscaleAndThreshold; 147 std::array<IDescriptorSetBinder::Ptr, TARGET_COUNT> downscale; 148 std::array<IDescriptorSetBinder::Ptr, TARGET_COUNT> upscale; 149 IDescriptorSetBinder::Ptr combine; 150 }; 151 Binders binders_; 152 153 // calculated from the amount of textures and scale factor 154 size_t frameScaleMaxCount_ { 0 }; 155 156 RenderHandleReference samplerHandle_; 157 BASE_NS::Format format_ { BASE_NS::Format::BASE_FORMAT_UNDEFINED }; 158 159 ViewportDesc baseViewportDesc_; 160 ScissorDesc baseScissorDesc_; 161 162 RenderHandleReference postProcessUbo_; 163 164 RenderAreaRequest renderAreaRequest_; 165 bool useRequestedRenderArea_ { false }; 166 167 CORE_NS::PropertyApiImpl<NodeInputs> inputProperties_; 168 CORE_NS::PropertyApiImpl<NodeOutputs> outputProperties_; 169 170 bool valid_ { false }; 171 172 EffectProperties effectProperties_; 173 BASE_NS::Math::UVec2 baseSize_ { 0U, 0U }; 174 BASE_NS::Math::Vec4 bloomParameters_ { 0.0f, 0.0f, 0.0f, 0.0f }; 175 }; 176 RENDER_END_NAMESPACE() 177 178 #endif // RENDER_POSTPROCESS_RENDER_POST_PROCESS_BLOOM_NODE_H 179