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 #include "render_copy.h"
17 
18 #include <render/device/intf_gpu_resource_manager.h>
19 #include <render/device/intf_shader_manager.h>
20 #include <render/device/pipeline_layout_desc.h>
21 #include <render/device/pipeline_state_desc.h>
22 #include <render/namespace.h>
23 #include <render/nodecontext/intf_node_context_descriptor_set_manager.h>
24 #include <render/nodecontext/intf_node_context_pso_manager.h>
25 #include <render/nodecontext/intf_pipeline_descriptor_set_binder.h>
26 #include <render/nodecontext/intf_render_command_list.h>
27 #include <render/nodecontext/intf_render_node_context_manager.h>
28 #include <render/nodecontext/intf_render_node_util.h>
29 
30 #include "default_engine_constants.h"
31 #include "device/gpu_resource_handle_util.h"
32 #include "util/log.h"
33 
34 // shaders
35 #include <render/shaders/common/render_post_process_structs_common.h>
36 
37 using namespace BASE_NS;
38 
39 RENDER_BEGIN_NAMESPACE()
40 namespace {
41 constexpr DynamicStateEnum DYNAMIC_STATES[] = { CORE_DYNAMIC_STATE_ENUM_VIEWPORT, CORE_DYNAMIC_STATE_ENUM_SCISSOR };
42 
CreateRenderPass(const IRenderNodeGpuResourceManager & gpuResourceMgr,const RenderHandle input)43 RenderPass CreateRenderPass(const IRenderNodeGpuResourceManager& gpuResourceMgr, const RenderHandle input)
44 {
45     const GpuImageDesc desc = gpuResourceMgr.GetImageDescriptor(input);
46     RenderPass rp;
47     rp.renderPassDesc.attachmentCount = 1u;
48     rp.renderPassDesc.attachmentHandles[0u] = input;
49     rp.renderPassDesc.attachments[0u].loadOp = AttachmentLoadOp::CORE_ATTACHMENT_LOAD_OP_DONT_CARE;
50     rp.renderPassDesc.attachments[0u].storeOp = AttachmentStoreOp::CORE_ATTACHMENT_STORE_OP_STORE;
51     rp.renderPassDesc.renderArea = { 0, 0, desc.width, desc.height };
52 
53     rp.renderPassDesc.subpassCount = 1u;
54     rp.subpassDesc.colorAttachmentCount = 1u;
55     rp.subpassDesc.colorAttachmentIndices[0u] = 0u;
56     rp.subpassStartIndex = 0u;
57     return rp;
58 }
59 
CreatePso(IRenderNodeContextManager & renderNodeContextMgr,const RenderHandle & shader,const PipelineLayout & pipelineLayout)60 RenderHandle CreatePso(
61     IRenderNodeContextManager& renderNodeContextMgr, const RenderHandle& shader, const PipelineLayout& pipelineLayout)
62 {
63     auto& psoMgr = renderNodeContextMgr.GetPsoManager();
64     const auto& shaderMgr = renderNodeContextMgr.GetShaderManager();
65     const RenderHandle graphicsStateHandle = shaderMgr.GetGraphicsStateHandleByShaderHandle(shader);
66     return psoMgr.GetGraphicsPsoHandle(
67         shader, graphicsStateHandle, pipelineLayout, {}, {}, { DYNAMIC_STATES, countof(DYNAMIC_STATES) });
68 }
69 } // namespace
70 
Init(IRenderNodeContextManager & renderNodeContextMgr,const CopyInfo & copyInfo)71 void RenderCopy::Init(IRenderNodeContextManager& renderNodeContextMgr, const CopyInfo& copyInfo)
72 {
73     copyInfo_ = copyInfo;
74     const IRenderNodeShaderManager& shaderMgr = renderNodeContextMgr.GetShaderManager();
75     {
76         renderData_ = {};
77         renderData_.shader = shaderMgr.GetShaderHandle("rendershaders://shader/fullscreen_copy.shader");
78         renderData_.pipelineLayout = shaderMgr.GetReflectionPipelineLayout(renderData_.shader);
79         renderData_.sampler = renderNodeContextMgr.GetGpuResourceManager().GetSamplerHandle(
80             DefaultEngineGpuResourceConstants::CORE_DEFAULT_SAMPLER_LINEAR_CLAMP);
81 
82         renderData_.shaderLayer = shaderMgr.GetShaderHandle("rendershaders://shader/fullscreen_copy_layer.shader");
83         renderData_.pipelineLayoutLayer = shaderMgr.GetReflectionPipelineLayout(renderData_.shaderLayer);
84     }
85     {
86         // single binder for both
87         INodeContextDescriptorSetManager& descriptorSetMgr = renderNodeContextMgr.GetDescriptorSetManager();
88         const auto& bindings = renderData_.pipelineLayout.descriptorSetLayouts[0U].bindings;
89         const RenderHandle descHandle = descriptorSetMgr.CreateDescriptorSet(bindings);
90         binder_ = descriptorSetMgr.CreateDescriptorSetBinder(descHandle, bindings);
91     }
92 }
93 
PreExecute(IRenderNodeContextManager & renderNodeContextMgr,const CopyInfo & copyInfo)94 void RenderCopy::PreExecute(IRenderNodeContextManager& renderNodeContextMgr, const CopyInfo& copyInfo)
95 {
96     copyInfo_ = copyInfo;
97 }
98 
Execute(IRenderNodeContextManager & renderNodeContextMgr,IRenderCommandList & cmdList)99 void RenderCopy::Execute(IRenderNodeContextManager& renderNodeContextMgr, IRenderCommandList& cmdList)
100 {
101     // extra blit from input to ouput
102     if (RenderHandleUtil::IsGpuImage(copyInfo_.input.handle) && RenderHandleUtil::IsGpuImage(copyInfo_.output.handle) &&
103         binder_) {
104         auto& gpuResourceMgr = renderNodeContextMgr.GetGpuResourceManager();
105 
106         auto renderPass = CreateRenderPass(gpuResourceMgr, copyInfo_.output.handle);
107         RenderHandle pso;
108         if (copyInfo_.copyType == CopyType::LAYER_COPY) {
109             if (!RenderHandleUtil::IsValid(renderData_.psoLayer)) {
110                 renderData_.psoLayer =
111                     CreatePso(renderNodeContextMgr, renderData_.shaderLayer, renderData_.pipelineLayoutLayer);
112             }
113             pso = renderData_.psoLayer;
114         } else {
115             if (!RenderHandleUtil::IsValid(renderData_.pso)) {
116                 renderData_.pso = CreatePso(renderNodeContextMgr, renderData_.shader, renderData_.pipelineLayout);
117             }
118             pso = renderData_.pso;
119         }
120 
121         cmdList.BeginRenderPass(renderPass.renderPassDesc, renderPass.subpassStartIndex, renderPass.subpassDesc);
122         cmdList.BindPipeline(pso);
123 
124         {
125             auto& binder = *binder_;
126             binder.ClearBindings();
127             uint32_t binding = 0u;
128             binder.BindSampler(
129                 binding, RenderHandleUtil::IsValid(copyInfo_.sampler) ? copyInfo_.sampler : renderData_.sampler);
130             binder.BindImage(++binding, copyInfo_.input);
131             cmdList.UpdateDescriptorSet(
132                 binder.GetDescriptorSetHandle(), binder.GetDescriptorSetLayoutBindingResources());
133             cmdList.BindDescriptorSet(0u, binder.GetDescriptorSetHandle());
134         }
135 
136         // dynamic state
137         const IRenderNodeUtil& renderNodeUtil = renderNodeContextMgr.GetRenderNodeUtil();
138         const ViewportDesc viewportDesc = renderNodeUtil.CreateDefaultViewport(renderPass);
139         const ScissorDesc scissorDesc = renderNodeUtil.CreateDefaultScissor(renderPass);
140         cmdList.SetDynamicStateViewport(viewportDesc);
141         cmdList.SetDynamicStateScissor(scissorDesc);
142 
143         const auto& pl =
144             (copyInfo_.copyType == CopyType::LAYER_COPY) ? renderData_.pipelineLayoutLayer : renderData_.pipelineLayout;
145         if (pl.pushConstant.byteSize > 0) {
146             const float fWidth = static_cast<float>(renderPass.renderPassDesc.renderArea.extentWidth);
147             const float fHeight = static_cast<float>(renderPass.renderPassDesc.renderArea.extentHeight);
148             const LocalPostProcessPushConstantStruct pc { { fWidth, fHeight, 1.0f / fWidth, 1.0f / fHeight },
149                 { static_cast<float>(copyInfo_.input.layer), 0.0f, 0.0f, 0.0f } };
150             cmdList.PushConstantData(pl.pushConstant, arrayviewU8(pc));
151         }
152 
153         cmdList.Draw(3u, 1u, 0u, 0u);
154         cmdList.EndRenderPass();
155     }
156 }
157 
GetDescriptorCounts() const158 DescriptorCounts RenderCopy::GetDescriptorCounts() const
159 {
160     // prepare only for a single copy operation per frame
161     return DescriptorCounts { {
162         { CORE_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1U },
163         { CORE_DESCRIPTOR_TYPE_SAMPLER, 1U },
164     } };
165 }
166 RENDER_END_NAMESPACE()
167