• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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 IGraphicsStateComponentManager;
44 class IJointMatricesComponentManager;
45 class ILayerComponentManager;
46 class IMaterialComponentManager;
47 class IMaterialExtensionComponentManager;
48 class IMeshComponentManager;
49 class INodeComponentManager;
50 class IRenderMeshComponentManager;
51 class IRenderHandleComponentManager;
52 class ISkinComponentManager;
53 class IWorldMatrixComponentManager;
54 class IPicking;
55 struct MaterialComponent;
56 
57 class RenderPreprocessorSystem final : public IRenderPreprocessorSystem, CORE_NS::IEcs::ComponentListener {
58 public:
59     explicit RenderPreprocessorSystem(CORE_NS::IEcs& ecs);
60     ~RenderPreprocessorSystem() override;
61     BASE_NS::string_view GetName() const override;
62     BASE_NS::Uid GetUid() const override;
63     CORE_NS::IPropertyHandle* GetProperties() override;
64     const CORE_NS::IPropertyHandle* GetProperties() const override;
65     void SetProperties(const CORE_NS::IPropertyHandle&) override;
66     bool IsActive() const override;
67     void SetActive(bool state) override;
68 
69     void Initialize() override;
70     void Uninitialize() override;
71     bool Update(bool frameRenderingQueued, uint64_t totalTime, uint64_t deltaTime) override;
72 
73     const CORE_NS::IEcs& GetECS() const override;
74 
75     // for RenderSystem >
76     struct SceneData {
77         uint32_t sceneId;
78         BASE_NS::array_view<const CORE_NS::Entity> renderBatchComponents;
79         BASE_NS::array_view<const CORE_NS::Entity> instancingAllowed;
80         BASE_NS::array_view<const CORE_NS::Entity> rest;
81     };
82     BASE_NS::array_view<const SceneData> GetSceneData() const;
83     BASE_NS::array_view<const CORE_NS::Entity> GetRenderBatchMeshEntities(uint32_t sceneId) const;
84     BASE_NS::array_view<const CORE_NS::Entity> GetInstancingAllowedEntities(uint32_t sceneId) const;
85     BASE_NS::array_view<const CORE_NS::Entity> GetInstancingDisabledEntities(uint32_t sceneId) const;
86 
87     struct Aabb {
88         BASE_NS::Math::Vec3 min { std::numeric_limits<float>::max(), std::numeric_limits<float>::max(),
89             std::numeric_limits<float>::max() };
90         BASE_NS::Math::Vec3 max { -std::numeric_limits<float>::max(), -std::numeric_limits<float>::max(),
91             -std::numeric_limits<float>::max() };
92     };
93 
94     // whole mesh
95     Aabb GetRenderMeshAabb(CORE_NS::Entity renderMesh) const;
96     // one for each submesh
97     BASE_NS::array_view<const Aabb> GetRenderMeshAabbs(CORE_NS::Entity renderMesh) const;
98 
99     struct Sphere {
100         BASE_NS::Math::Vec3 center;
101         float radius {};
102     };
103     Sphere GetBoundingSphere() const;
104     // for RenderSystem <
105 
106     struct SortData {
107         uint32_t sceneId;
108         CORE_NS::IComponentManager::ComponentId renderMeshId;
109         CORE_NS::Entity mesh;
110         CORE_NS::Entity batch;
111         CORE_NS::Entity skin;
112         bool allowInstancing;
113     };
114 
115     struct MaterialProperties {
116         CORE_NS::Entity material;
117         bool disabled;
118         bool allowInstancing;
119         bool shadowCaster;
120     };
121 
122 private:
123     void OnComponentEvent(EventType type, const CORE_NS::IComponentManager& componentManager,
124         BASE_NS::array_view<const CORE_NS::Entity> entities) override;
125     void HandleMaterialEvents();
126     void HandleMeshEvents();
127     void HandleGraphicsStateEvents() noexcept;
128     void SetDataStorePointers(RENDER_NS::IRenderDataStoreManager& manager);
129     void CalculateSceneBounds();
130     void GatherSortData();
131     void UpdateMaterialProperties();
132     void UpdateSingleMaterial(CORE_NS::Entity matEntity, const MaterialComponent* materialHandle);
133     MaterialProperties* GetMaterialProperties(CORE_NS::Entity matEntity);
134     bool CheckIfDefaultDataStoreNames() const;
135 
136     CORE_NS::IEcs& ecs_;
137     IGraphicsContext* graphicsContext_ { nullptr };
138     RENDER_NS::IRenderContext* renderContext_ { nullptr };
139     IGraphicsStateComponentManager* graphicsStateManager_ { nullptr };
140     IJointMatricesComponentManager* jointMatricesManager_ { nullptr };
141     ILayerComponentManager* layerManager_ { nullptr };
142     IMaterialComponentManager* materialManager_ { nullptr };
143     IRenderHandleComponentManager* renderHandleManager_ { nullptr };
144     IMeshComponentManager* meshManager_ { nullptr };
145     INodeComponentManager* nodeManager_ { nullptr };
146     IRenderMeshComponentManager* renderMeshManager_ { nullptr };
147     ISkinComponentManager* skinManager_ { nullptr };
148     IWorldMatrixComponentManager* worldMatrixManager_ { nullptr };
149     bool active_ { true };
150 
151     IRenderPreprocessorSystem::Properties properties_ {
152         "RenderDataStoreDefaultScene",
153         "RenderDataStoreDefaultCamera",
154         "RenderDataStoreDefaultLight",
155         "RenderDataStoreDefaultMaterial",
156         "RenderDataStoreMorph",
157         "",
158     };
159     CORE_NS::PropertyApiImpl<IRenderPreprocessorSystem::Properties> RENDER_PREPROCESSOR_SYSTEM_PROPERTIES;
160 
161     BASE_NS::refcnt_ptr<IRenderDataStoreDefaultCamera> dsCamera_;
162     BASE_NS::refcnt_ptr<IRenderDataStoreDefaultLight> dsLight_;
163     BASE_NS::refcnt_ptr<IRenderDataStoreDefaultMaterial> dsMaterial_;
164     BASE_NS::refcnt_ptr<IRenderDataStoreDefaultScene> dsScene_;
165     BASE_NS::refcnt_ptr<IRenderDataStoreMorph> dsMorph_;
166 
167     IPicking* picking_ = nullptr;
168 
169     CORE_NS::ComponentQuery renderableQuery_;
170     uint32_t graphicsStateGeneration_ { 0U };
171     uint32_t jointGeneration_ { 0U };
172     uint32_t layerGeneration_ { 0U };
173     uint32_t nodeGeneration_ { 0U };
174     uint32_t renderMeshGeneration_ { 0U };
175     uint32_t worldMatrixGeneration_ { 0U };
176 
177     BASE_NS::vector<CORE_NS::Entity> graphicsStateModifiedEvents_;
178     BASE_NS::vector<CORE_NS::Entity> materialModifiedEvents_;
179     BASE_NS::vector<CORE_NS::Entity> materialDestroyedEvents_;
180     BASE_NS::vector<MaterialProperties> materialProperties_;
181 
182     BASE_NS::vector<CORE_NS::Entity> meshModifiedEvents_;
183     BASE_NS::vector<CORE_NS::Entity> meshDestroyedEvents_;
184 
185     BASE_NS::vector<SortData> meshComponents_;
186 
187     BASE_NS::vector<CORE_NS::Entity> renderMeshComponents_;
188 
189     BASE_NS::vector<SceneData> renderMeshComponentsPerScene_;
190 
191     struct RenderMeshAaabb {
192         CORE_NS::Entity entity;
193         Aabb meshAabb;
194         BASE_NS::vector<Aabb> submeshAabbs;
195         bool shadowCaster { true };
196     };
197     BASE_NS::vector<RenderMeshAaabb> renderMeshAabbs_;
198     Sphere boundingSphere_;
199 };
200 CORE3D_END_NAMESPACE()
201 
202 #endif // CORE3D_ECS_RENDER_PREPROCESSOR_SYSTEM_H
203