1 /*
2  * Copyright (C) 2023 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 OHOS_RENDER_3D_LUME_CUSTOM_RENDER_H
17 #define OHOS_RENDER_3D_LUME_CUSTOM_RENDER_H
18 
19 #include <algorithm>
20 #include <vector>
21 #include <string>
22 #include <securec.h>
23 
24 #include <3d/intf_graphics_context.h>
25 
26 #include <base/math/vector.h>
27 
28 #include <core/intf_engine.h>
29 #include <core/log.h>
30 #include <core/namespace.h>
31 
32 #include <render/datastore/intf_render_data_store_default_staging.h>
33 #include <render/datastore/intf_render_data_store_manager.h>
34 #include <render/datastore/intf_render_data_store_pod.h>
35 #include <render/datastore/render_data_store_render_pods.h>
36 #include <render/device/intf_gpu_resource_manager.h>
37 #include <render/intf_render_context.h>
38 
39 #include "3d_widget_adapter_log.h"
40 #include "custom/shader_input_buffer.h"
41 
42 namespace OHOS::Render3D {
43 
44 struct CustomRenderInput {
45     CORE_NS::IEngine::Ptr engine_;
46     CORE3D_NS::IGraphicsContext::Ptr graphicsContext_;
47     RENDER_NS::IRenderContext::Ptr renderContext_;
48     CORE_NS::IEcs::Ptr ecs_;
49     uint32_t width_ { 0U };
50     uint32_t height_ { 0U };
51 };
52 
53 class LumeCustomRender {
54 public:
LumeCustomRender(bool needsFrameCallback)55     LumeCustomRender(bool needsFrameCallback): needsFrameCallback_(needsFrameCallback) {};
56     virtual ~LumeCustomRender();
57 
58     virtual BASE_NS::vector<RENDER_NS::RenderHandleReference> GetRenderHandles();
59     void Initialize(const CustomRenderInput& input);
60     void LoadImages(const std::vector<std::string>& imageUris);
61     void RegistorShaderPath(const std::string& shaderPath);
62     virtual bool UpdateShaderInputBuffer(const std::shared_ptr<ShaderInputBuffer>& shaderInputBuffer);
63     void UpdateShaderSpecialization(const std::vector<uint32_t>& values);
64     virtual void OnSizeChange(int32_t width, int32_t height);
65     virtual void OnDrawFrame();
66     void LoadRenderNodeGraph(const std::string& rngUri, const RENDER_NS::RenderHandleReference& output);
67     void UnloadImages();
68     void UnloadRenderNodeGraph();
NeedsFrameCallback()69     bool NeedsFrameCallback()
70     {
71         return needsFrameCallback_;
72     }
SetScaleInfo(float widthScale,float heightScale)73     void SetScaleInfo(float widthScale, float heightScale)
74     {
75         widthScale_ = widthScale;
76         heightScale_ = heightScale;
77     }
78 protected:
79     float widthScale_ = 1.0f;
80     float heightScale_ = 1.0f;
81     uint32_t width_ = 0U;
82     uint32_t height_ = 0U;
83 
84 private:
85     const RENDER_NS::RenderHandleReference GetRenderHandle();
86     void SetRenderOutput(const RENDER_NS::RenderHandleReference& output);
87     void LoadImage(const std::string& imageUri);
88     void GetDefaultStaging();
89     void PrepareResolutionInputBuffer();
90     void DestroyBuffer();
91     void DestroyDataStorePod();
92     void DestroyRes();
93 
94     CORE_NS::IEngine::Ptr engine_;
95     CORE3D_NS::IGraphicsContext::Ptr graphicsContext_;
96     RENDER_NS::IRenderContext::Ptr renderContext_;
97     RENDER_NS::IRenderDataStoreDefaultStaging* renderDataStoreDefaultStaging_ { nullptr };
98 
99     RENDER_NS::RenderHandleReference shaderInputBufferHandle_;
100     RENDER_NS::RenderHandleReference resolutionBufferHandle_;
101     RENDER_NS::RenderHandleReference renderHandle_;
102     RENDER_NS::GpuBufferDesc bufferDesc_ {
103         RENDER_NS::CORE_BUFFER_USAGE_UNIFORM_BUFFER_BIT | RENDER_NS::CORE_BUFFER_USAGE_TRANSFER_DST_BIT,
104         RENDER_NS::CORE_MEMORY_PROPERTY_HOST_VISIBLE_BIT | RENDER_NS::CORE_MEMORY_PROPERTY_HOST_COHERENT_BIT,
105         RENDER_NS::CORE_ENGINE_BUFFER_CREATION_DYNAMIC_RING_BUFFER, 0u };
106 
107     CORE_NS::EntityReference envCubeHandle_;
108     CORE_NS::EntityReference bgHandle_;
109     CORE_NS::IEcs::Ptr ecs_;
110 
111     bool needsFrameCallback_ = false;
112     std::vector<std::pair<std::string, CORE_NS::EntityReference>> images_;
113     std::shared_ptr<ShaderInputBuffer> shaderInputBuffer_;
114     ShaderInputBuffer resolutionBuffer_;
115 
116     const char* const RENDER_DATA_STORE_DEFAULT_STAGING = "RenderDataStoreDefaultStaging";
117     const char* const RENDER_DATA_STORE_POD = "RenderDataStorePod";
118     const char* const SPECIALIZATION_TYPE_NAME = "ShaderSpecializationRenderPod";
119     const char* const SPECIALIZATION_CONFIG_NAME = "ShaderSpecializationConfig";
120     const char* const IMAGE_NAME = "IMAGE";
121     const char* const INPUT_BUFFER = "INPUT_BUFFER";
122     const char* const RESOLUTION_BUFFER = "RESOLUTION_BUFFER";
123 };
124 
125 } // namespace name
126 
127 #endif //OHOS_RENDER_3D_LUME_CUSTOM_RENDER_H
128