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 CORE3D_ECS_RENDER_PREPROCESSOR_SYSTEM_H 17 #define CORE3D_ECS_RENDER_PREPROCESSOR_SYSTEM_H 18 19 #include <ComponentTools/component_query.h> 20 #include <limits> 21 22 #include <3d/ecs/systems/intf_render_preprocessor_system.h> 23 #include <3d/intf_graphics_context.h> 24 #include <base/math/vector.h> 25 #include <core/ecs/intf_ecs.h> 26 #include <core/property_tools/property_api_impl.h> 27 #include <render/datastore/intf_render_data_store_manager.h> 28 #include <render/intf_render_context.h> 29 30 #include "property/property_handle.h" 31 32 RENDER_BEGIN_NAMESPACE() 33 class IRenderContext; 34 class IRenderDataStoreManager; 35 RENDER_END_NAMESPACE() 36 37 CORE3D_BEGIN_NAMESPACE() 38 class IRenderDataStoreDefaultCamera; 39 class IRenderDataStoreDefaultLight; 40 class IRenderDataStoreDefaultMaterial; 41 class IRenderDataStoreDefaultScene; 42 class IRenderDataStoreMorph; 43 class IJointMatricesComponentManager; 44 class ILayerComponentManager; 45 class IMaterialComponentManager; 46 class IMaterialExtensionComponentManager; 47 class IMeshComponentManager; 48 class INodeComponentManager; 49 class IRenderMeshComponentManager; 50 class IRenderHandleComponentManager; 51 class ISkinComponentManager; 52 class IWorldMatrixComponentManager; 53 class IPicking; 54 struct MaterialComponent; 55 56 class RenderPreprocessorSystem final : public IRenderPreprocessorSystem, CORE_NS::IEcs::ComponentListener { 57 public: 58 explicit RenderPreprocessorSystem(CORE_NS::IEcs& ecs); 59 ~RenderPreprocessorSystem() override; 60 BASE_NS::string_view GetName() const override; 61 BASE_NS::Uid GetUid() const override; 62 CORE_NS::IPropertyHandle* GetProperties() override; 63 const CORE_NS::IPropertyHandle* GetProperties() const override; 64 void SetProperties(const CORE_NS::IPropertyHandle&) override; 65 bool IsActive() const override; 66 void SetActive(bool state) override; 67 68 void Initialize() override; 69 void Uninitialize() override; 70 bool Update(bool frameRenderingQueued, uint64_t totalTime, uint64_t deltaTime) override; 71 72 const CORE_NS::IEcs& GetECS() const override; 73 74 BASE_NS::array_view<const CORE_NS::Entity> GetRenderBatchMeshEntities() const; 75 BASE_NS::array_view<const CORE_NS::Entity> GetInstancingAllowedEntities() const; 76 BASE_NS::array_view<const CORE_NS::Entity> GetInstancingDisabledEntities() const; 77 78 struct Aabb { 79 BASE_NS::Math::Vec3 min { std::numeric_limits<float>::max(), std::numeric_limits<float>::max(), 80 std::numeric_limits<float>::max() }; 81 BASE_NS::Math::Vec3 max { -std::numeric_limits<float>::max(), -std::numeric_limits<float>::max(), 82 -std::numeric_limits<float>::max() }; 83 }; 84 85 // whole mesh 86 Aabb GetRenderMeshAabb(CORE_NS::Entity renderMesh) const; 87 // one for each submesh 88 BASE_NS::array_view<const Aabb> GetRenderMeshAabbs(CORE_NS::Entity renderMesh) const; 89 90 struct Sphere { 91 BASE_NS::Math::Vec3 center; 92 float radius {}; 93 }; 94 Sphere GetBoundingSphere() const; 95 96 struct SortData { 97 CORE_NS::IComponentManager::ComponentId renderMeshId; 98 CORE_NS::Entity mesh; 99 CORE_NS::Entity batch; 100 CORE_NS::Entity skin; 101 bool allowInstancing; 102 }; 103 104 struct MaterialProperties { 105 CORE_NS::Entity material; 106 bool disabled; 107 bool allowInstancing; 108 bool shadowCaster; 109 }; 110 111 private: 112 void OnComponentEvent(EventType type, const CORE_NS::IComponentManager& componentManager, 113 BASE_NS::array_view<const CORE_NS::Entity> entities) override; 114 void HandleMaterialEvents(); 115 void SetDataStorePointers(RENDER_NS::IRenderDataStoreManager& manager); 116 void CalculateSceneBounds(); 117 void GatherSortData(); 118 void UpdateMaterialProperties(); 119 void UpdateSingleMaterial(CORE_NS::Entity matEntity, const MaterialComponent* materialHandle); 120 MaterialProperties* GetMaterialProperties(CORE_NS::Entity matEntity); 121 122 CORE_NS::IEcs& ecs_; 123 IGraphicsContext* graphicsContext_ { nullptr }; 124 RENDER_NS::IRenderContext* renderContext_ { nullptr }; 125 IJointMatricesComponentManager* jointMatricesManager_ { nullptr }; 126 ILayerComponentManager* layerManager_ { nullptr }; 127 IMaterialComponentManager* materialManager_ { nullptr }; 128 IRenderHandleComponentManager* renderHandleManager_ { nullptr }; 129 IMeshComponentManager* meshManager_ { nullptr }; 130 INodeComponentManager* nodeManager_ { nullptr }; 131 IRenderMeshComponentManager* renderMeshManager_ { nullptr }; 132 ISkinComponentManager* skinManager_ { nullptr }; 133 IWorldMatrixComponentManager* worldMatrixManager_ { nullptr }; 134 bool active_ { true }; 135 136 IRenderPreprocessorSystem::Properties properties_ { 137 "RenderDataStoreDefaultScene", 138 "RenderDataStoreDefaultCamera", 139 "RenderDataStoreDefaultLight", 140 "RenderDataStoreDefaultMaterial", 141 "RenderDataStoreMorph", 142 "", 143 }; 144 CORE_NS::PropertyApiImpl<IRenderPreprocessorSystem::Properties> RENDER_PREPROCESSOR_SYSTEM_PROPERTIES; 145 146 BASE_NS::refcnt_ptr<IRenderDataStoreDefaultCamera> dsCamera_; 147 BASE_NS::refcnt_ptr<IRenderDataStoreDefaultLight> dsLight_; 148 BASE_NS::refcnt_ptr<IRenderDataStoreDefaultMaterial> dsMaterial_; 149 BASE_NS::refcnt_ptr<IRenderDataStoreDefaultScene> dsScene_; 150 BASE_NS::refcnt_ptr<IRenderDataStoreMorph> dsMorph_; 151 152 IPicking* picking_ = nullptr; 153 154 CORE_NS::ComponentQuery renderableQuery_; 155 uint32_t jointGeneration_ { 0U }; 156 uint32_t layerGeneration_ { 0U }; 157 uint32_t materialGeneration_ { 0U }; 158 uint32_t meshGeneration_ { 0U }; 159 uint32_t nodeGeneration_ { 0U }; 160 uint32_t renderMeshGeneration_ { 0U }; 161 uint32_t worldMatrixGeneration_ { 0U }; 162 163 BASE_NS::vector<CORE_NS::Entity> materialModifiedEvents_; 164 BASE_NS::vector<CORE_NS::Entity> materialDestroyedEvents_; 165 BASE_NS::vector<MaterialProperties> materialProperties_; 166 167 BASE_NS::vector<SortData> meshComponents_; 168 169 BASE_NS::vector<CORE_NS::Entity> renderMeshComponents_; 170 BASE_NS::array_view<const CORE_NS::Entity> renderBatchComponents_; 171 BASE_NS::array_view<const CORE_NS::Entity> instancingAllowed_; 172 BASE_NS::array_view<const CORE_NS::Entity> rest_; 173 struct RenderMeshAaabb { 174 CORE_NS::Entity entity; 175 Aabb meshAabb; 176 BASE_NS::vector<Aabb> submeshAabbs; 177 bool shadowCaster { true }; 178 }; 179 BASE_NS::vector<RenderMeshAaabb> renderMeshAabbs_; 180 Sphere boundingSphere_; 181 }; 182 CORE3D_END_NAMESPACE() 183 184 #endif // CORE3D_ECS_RENDER_PREPROCESSOR_SYSTEM_H 185