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 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 bool useMultiSwapChain_ { false }; 52 }; 53 54 class LumeCustomRender { 55 public: LumeCustomRender(bool needsFrameCallback)56 LumeCustomRender(bool needsFrameCallback): needsFrameCallback_(needsFrameCallback) {}; 57 virtual ~LumeCustomRender(); 58 59 virtual BASE_NS::vector<RENDER_NS::RenderHandleReference> GetRenderHandles(); 60 void Initialize(const CustomRenderInput& input); 61 void LoadImages(const std::vector<std::string>& imageUris); 62 void RegistorShaderPath(const std::string& shaderPath); 63 virtual bool UpdateShaderInputBuffer(const std::shared_ptr<ShaderInputBuffer>& shaderInputBuffer); 64 void UpdateShaderSpecialization(const std::vector<uint32_t>& values); 65 virtual void OnSizeChange(int32_t width, int32_t height); 66 virtual void OnDrawFrame(); 67 void LoadRenderNodeGraph(const std::string& rngUri, const RENDER_NS::RenderHandleReference& output); 68 void UnloadImages(); 69 void UnloadRenderNodeGraph(); NeedsFrameCallback()70 bool NeedsFrameCallback() 71 { 72 return needsFrameCallback_; 73 } SetScaleInfo(float widthScale,float heightScale)74 void SetScaleInfo(float widthScale, float heightScale) 75 { 76 widthScale_ = widthScale; 77 heightScale_ = heightScale; 78 } 79 GetSwapchainName()80 const std::string& GetSwapchainName() const 81 { 82 return swapchainName_; 83 } 84 GetInputBufferName()85 const std::string& GetInputBufferName() const 86 { 87 return inputBufferName_; 88 } 89 GetResolutionBufferName()90 const std::string& GetResolutionBufferName() const 91 { 92 return resolutionBufferName_; 93 } 94 GetUseBasisEngine()95 bool GetUseBasisEngine() 96 { 97 return useBasisEngine_; // get useBasisEngine_ of the customRender instance 98 } 99 100 protected: 101 float widthScale_ = 1.0f; 102 float heightScale_ = 1.0f; 103 uint32_t width_ = 0U;; 104 uint32_t height_ = 0U; 105 bool useMultiSwapChain_ = false; 106 bool useBasisEngine_ = false; // set to true by ProductBasis if in rain or snow scene, which uses physics engine 107 108 private: 109 const RENDER_NS::RenderHandleReference GetRenderHandle(); 110 void SetRenderOutput(const RENDER_NS::RenderHandleReference& output); 111 void LoadImage(const std::string& imageUri); 112 void GetDefaultStaging(); 113 void PrepareResolutionInputBuffer(); 114 void DestroyBuffer(); 115 void DestroyDataStorePod(); 116 void DestroyRes(); 117 118 CORE_NS::IEngine::Ptr engine_; 119 CORE3D_NS::IGraphicsContext::Ptr graphicsContext_; 120 RENDER_NS::IRenderContext::Ptr renderContext_; 121 BASE_NS::refcnt_ptr<RENDER_NS::IRenderDataStoreDefaultStaging> renderDataStoreDefaultStaging_ { nullptr }; 122 123 RENDER_NS::RenderHandleReference shaderInputBufferHandle_; 124 RENDER_NS::RenderHandleReference resolutionBufferHandle_; 125 RENDER_NS::RenderHandleReference renderHandle_; 126 RENDER_NS::GpuBufferDesc bufferDesc_ { 127 RENDER_NS::CORE_BUFFER_USAGE_UNIFORM_BUFFER_BIT | RENDER_NS::CORE_BUFFER_USAGE_TRANSFER_DST_BIT, 128 RENDER_NS::CORE_MEMORY_PROPERTY_HOST_VISIBLE_BIT | RENDER_NS::CORE_MEMORY_PROPERTY_HOST_COHERENT_BIT, 129 RENDER_NS::CORE_ENGINE_BUFFER_CREATION_DYNAMIC_RING_BUFFER, 0u }; 130 131 CORE_NS::EntityReference envCubeHandle_; 132 CORE_NS::EntityReference bgHandle_; 133 CORE_NS::IEcs::Ptr ecs_; 134 135 bool needsFrameCallback_ = false; 136 std::vector<std::pair<std::string, CORE_NS::EntityReference>> images_; 137 std::shared_ptr<ShaderInputBuffer> shaderInputBuffer_; 138 ShaderInputBuffer resolutionBuffer_; 139 140 const char* const RENDER_DATA_STORE_DEFAULT_STAGING = "RenderDataStoreDefaultStaging"; 141 const char* const RENDER_DATA_STORE_POD = "RenderDataStorePod"; 142 const char* const SPECIALIZATION_TYPE_NAME = "ShaderSpecializationRenderPod"; 143 const char* const SPECIALIZATION_CONFIG_NAME = "ShaderSpecializationConfig"; 144 const char* const IMAGE_NAME = "IMAGE"; 145 const char* const INPUT_BUFFER = "INPUT_BUFFER"; 146 const char* const RESOLUTION_BUFFER = "RESOLUTION_BUFFER"; 147 148 std::string swapchainName_ = RENDER_DATA_STORE_DEFAULT_STAGING; 149 std::string inputBufferName_ = INPUT_BUFFER; 150 std::string resolutionBufferName_ = RESOLUTION_BUFFER; 151 }; 152 153 } // namespace name 154 155 #endif //OHOS_RENDER_3D_LUME_CUSTOM_RENDER_H 156