• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 CORE_ECS_RENDERSYSTEM_H
17 #define CORE_ECS_RENDERSYSTEM_H
18 
19 #include <ComponentTools/component_query.h>
20 #include <PropertyTools/property_api_impl.h>
21 #include <limits>
22 
23 #include <3d/ecs/components/layer_defines.h>
24 #include <3d/ecs/components/render_configuration_component.h>
25 #include <3d/ecs/systems/intf_render_system.h>
26 #include <3d/render/render_data_defines_3d.h>
27 #include <base/math/vector.h>
28 #include <core/namespace.h>
29 #include <render/namespace.h>
30 #include <render/resource_handle.h>
31 
32 #include "property/property_handle.h"
33 
BASE_BEGIN_NAMESPACE()34 BASE_BEGIN_NAMESPACE()
35 namespace Math {
36 class Mat4X4;
37 } // namespace Math
38 BASE_END_NAMESPACE()
39 
40 RENDER_BEGIN_NAMESPACE()
41 class IShaderManager;
42 class IRenderContext;
43 RENDER_END_NAMESPACE()
44 
45 CORE3D_BEGIN_NAMESPACE()
46 class IEnvironmentComponentManager;
47 class IFogComponentManager;
48 class IRenderHandleComponentManager;
49 class INameComponentManager;
50 class INodeComponentManager;
51 class IRenderMeshBatchComponentManager;
52 class IRenderMeshComponentManager;
53 class IWorldMatrixComponentManager;
54 class IPreviousWorldMatrixComponentManager;
55 class IRenderConfigurationComponentManager;
56 class ICameraComponentManager;
57 class ILayerComponentManager;
58 class ILightComponentManager;
59 class IJointMatricesComponentManager;
60 class IMaterialExtensionComponentManager;
61 class IMaterialComponentManager;
62 class IMeshComponentManager;
63 class ISkinJointsComponentManager;
64 class IPlanarReflectionComponentManager;
65 class IPreviousJointMatricesComponentManager;
66 class IPostProcessComponentManager;
67 class IUriComponentManager;
68 class IRenderDataStoreDefaultCamera;
69 class IRenderDataStoreDefaultLight;
70 class IRenderDataStoreDefaultMaterial;
71 class IRenderDataStoreDefaultScene;
72 
73 class IRenderPreprocessorSystem;
74 
75 class IMesh;
76 class IMaterial;
77 class IPicking;
78 class IRenderUtil;
79 class IGraphicsContext;
80 
81 struct RenderMeshComponent;
82 struct JointMatricesComponent;
83 struct PreviousJointMatricesComponent;
84 struct MaterialComponent;
85 struct WorldMatrixComponent;
86 struct LightComponent;
87 struct MinAndMax;
88 
89 class RenderSystem final : public IRenderSystem {
90 public:
91     explicit RenderSystem(CORE_NS::IEcs& ecs);
92     ~RenderSystem() override;
93     BASE_NS::string_view GetName() const override;
94     BASE_NS::Uid GetUid() const override;
95     CORE_NS::IPropertyHandle* GetProperties() override;
96     const CORE_NS::IPropertyHandle* GetProperties() const override;
97     void SetProperties(const CORE_NS::IPropertyHandle&) override;
98     bool IsActive() const override;
99     void SetActive(bool state) override;
100 
101     void Initialize() override;
102     bool Update(bool frameRenderingQueued, uint64_t totalTime, uint64_t deltaTime) override;
103     void Uninitialize() override;
104 
105     const CORE_NS::IEcs& GetECS() const override;
106 
107     BASE_NS::array_view<const RENDER_NS::RenderHandleReference> GetRenderNodeGraphs() const override;
108 
109     struct BatchData {
110         CORE_NS::Entity entity; // node, render mesh component
111         CORE_NS::Entity mesh;   // mesh component
112         uint64_t layerMask { LayerConstants::DEFAULT_LAYER_MASK };
113         CORE_NS::IComponentManager::ComponentId jointId { CORE_NS::IComponentManager::INVALID_COMPONENT_ID };
114         CORE_NS::IComponentManager::ComponentId prevJointId { CORE_NS::IComponentManager::INVALID_COMPONENT_ID };
115 
116         BASE_NS::Math::Mat4X4 mtx;       // world matrix
117         BASE_NS::Math::Mat4X4 prevWorld; // previous world matrix
118     };
119     using BatchDataVector = BASE_NS::vector<BatchData>;
120 
121 private:
122     struct SceneBoundingVolumeHelper {
123         BASE_NS::Math::Vec3 sumOfSubmeshPoints { 0.0f, 0.0f, 0.0f };
124         uint32_t submeshCount { 0 };
125 
126         BASE_NS::Math::Vec3 minAABB { std::numeric_limits<float>::max(), std::numeric_limits<float>::max(),
127             std::numeric_limits<float>::max() };
128         BASE_NS::Math::Vec3 maxAABB { -std::numeric_limits<float>::max(), -std::numeric_limits<float>::max(),
129             -std::numeric_limits<float>::max() };
130     };
131     struct MeshProcessData {
132         const uint64_t layerMask { 0 };
133         const uint32_t batchInstanceCount { 0 };
134         const CORE_NS::Entity& renderMeshEntity;
135         const CORE_NS::Entity& meshEntity;
136         const MeshComponent& meshComponent;
137         const RenderMeshComponent& renderMeshComponent;
138         const BASE_NS::Math::Mat4X4& world;
139         const BASE_NS::Math::Mat4X4& prevWorld;
140         SceneBoundingVolumeHelper& sceneBoundingVolume;
141     };
142     struct LightProcessData {
143         const uint64_t layerMask { 0 };
144         const CORE_NS::Entity& entity;
145         const LightComponent& lightComponent;
146         const BASE_NS::Math::Mat4X4& world;
147         RenderScene& renderScene;
148         uint32_t& spotLightIndex;
149     };
150     struct SkinProcessData {
151         const JointMatricesComponent* const jointMatricesComponent { nullptr };
152         const PreviousJointMatricesComponent* const prevJointMatricesComponent { nullptr };
153     };
154 
155     void SetDataStorePointers(RENDER_NS::IRenderDataStoreManager& manager);
156     // returns the instance's valid scene component
157     RenderConfigurationComponent GetRenderConfigurationComponent();
158     CORE_NS::Entity ProcessScene(const RenderConfigurationComponent& sc);
159     void ProcessSubmesh(const MeshProcessData& mpd, const MeshComponent::Submesh& submesh, const uint32_t meshIndex,
160         const uint32_t subMeshIdx, const uint32_t skinJointIndex, const MinAndMax& mam, const bool isNegative);
161     void ProcessMesh(const MeshProcessData& mpd, const MinAndMax& batchMam, const SkinProcessData& spd);
162     void ProcessRenderables(SceneBoundingVolumeHelper& sceneBoundingVolumeHelper);
163     void ProcessBatchRenderables(SceneBoundingVolumeHelper& sceneBoundingVolumeHelper);
164     void CalculateSceneBounds(const SceneBoundingVolumeHelper& sceneBoundingVolumeHelper);
165     void ProcessCameras(const RenderConfigurationComponent& sceneCompnent, const CORE_NS::Entity& mainCameraEntity,
166         RenderScene& renderScene);
167     void ProcessLight(const LightProcessData& lightProcessData);
168     void ProcessLights(RenderScene& renderScene);
169     void ProcessShadowCamera(const LightProcessData lightProcessData, RenderLight& light);
170     bool ProcessReflection(const CORE_NS::ComponentQuery::ResultRow& row, const RenderCamera& camera);
171     bool ProcessReflections(CORE_NS::Entity cameraEntity, const RenderScene& renderScene);
172     void ProcessPostProcesses();
173     void FetchFullScene();
174     void EvaluateMaterialModifications(const MaterialComponent& matComp);
175     // calculates min max from all submeshes and does min max for inout
176     struct BatchIndices {
177         uint32_t submeshIndex { ~0u };
178         uint32_t batchStartIndex { ~0u };
179         uint32_t batchEndCount { ~0u };
180     };
181     // with submeshIndex == ~0u processes all submeshes
182     void CombineBatchWorldMinAndMax(const BatchDataVector& batchData, const BatchIndices& batchIndices,
183         const MeshComponent& mesh, MinAndMax& mam) const;
184 
185     void ProcessRenderNodeGraphs(const RenderConfigurationComponent& renderConfig, const RenderScene& renderScene);
186     void DestroyRenderDataStores();
187     RENDER_NS::RenderHandleReference GetCameraRenderNodeGraph(
188         const RenderScene& renderScene, const RenderCamera& renderCamera);
189     RENDER_NS::RenderHandleReference GetSceneRenderNodeGraph(const RenderScene& renderScene);
190 
191     struct CameraData;
192     CameraData UpdateAndGetPreviousFrameCameraData(
193         const CORE_NS::Entity& entity, const BASE_NS::Math::Mat4X4& view, const BASE_NS::Math::Mat4X4& proj);
194 
195     bool active_ = true;
196     CORE_NS::IEcs& ecs_;
197 
198     IRenderSystem::Properties properties_;
199 
200     IRenderDataStoreDefaultCamera* dsCamera_ = nullptr;
201     IRenderDataStoreDefaultLight* dsLight_ = nullptr;
202     IRenderDataStoreDefaultMaterial* dsMaterial_ = nullptr;
203     IRenderDataStoreDefaultScene* dsScene_ = nullptr;
204     RENDER_NS::IShaderManager* shaderMgr_ = nullptr;
205     RENDER_NS::IGpuResourceManager* gpuResourceMgr_ = nullptr;
206 
207     INodeComponentManager* nodeMgr_ = nullptr;
208     IRenderMeshBatchComponentManager* renderMeshBatchMgr_ = nullptr;
209     IRenderMeshComponentManager* renderMeshMgr_ = nullptr;
210     IWorldMatrixComponentManager* worldMatrixMgr_ = nullptr;
211     IPreviousWorldMatrixComponentManager* prevWorldMatrixMgr_ = nullptr;
212     IRenderConfigurationComponentManager* renderConfigMgr_ = nullptr;
213     ICameraComponentManager* cameraMgr_ = nullptr;
214     ILightComponentManager* lightMgr_ = nullptr;
215     IPlanarReflectionComponentManager* planarReflectionMgr_ = nullptr;
216     IMaterialExtensionComponentManager* materialExtensionMgr_ = nullptr;
217     IMaterialComponentManager* materialMgr_ = nullptr;
218     IMeshComponentManager* meshMgr_ = nullptr;
219     IUriComponentManager* uriMgr_ = nullptr;
220     INameComponentManager* nameMgr_ = nullptr;
221     IEnvironmentComponentManager* environmentMgr_ = nullptr;
222     IFogComponentManager* fogMgr_ = nullptr;
223     IRenderHandleComponentManager* gpuHandleMgr_ = nullptr;
224     ILayerComponentManager* layerMgr_ = nullptr;
225 
226     IJointMatricesComponentManager* jointMatricesMgr_ = nullptr;
227     IPreviousJointMatricesComponentManager* prevJointMatricesMgr_ = nullptr;
228 
229     IPostProcessComponentManager* postProcessMgr_ = nullptr;
230 
231     IPicking* picking_ = nullptr;
232 
233     IGraphicsContext* graphicsContext_ = nullptr;
234     IRenderUtil* renderUtil_ = nullptr;
235     RENDER_NS::IRenderContext* renderContext_ = nullptr;
236 
237     IRenderPreprocessorSystem* renderPreprocessorSystem_ = nullptr;
238 
239     CORE_NS::ComponentQuery lightQuery_;
240     CORE_NS::ComponentQuery renderableQuery_;
241     CORE_NS::ComponentQuery reflectionsQuery_;
242 
243     BASE_NS::Math::Vec3 sceneBoundingSpherePosition_ { 0.0f, 0.0f, 0.0f };
244     float sceneBoundingSphereRadius_ { 0.0f };
245 
246     CORE_NS::PropertyApiImpl<IRenderSystem::Properties> RENDER_SYSTEM_PROPERTIES;
247 
248     uint64_t totalTime_ { 0u };
249     uint64_t deltaTime_ { 0u };
250     uint64_t frameIndex_ { 0u };
251 
252     // additionally these could be stored to somewhere else
253     // though this is ECS render system and the render node graphs are owned by ECS (RS)
254     // these do not add overhead if the property bits are not set (only clear per frame)
255     struct RenderProcessing {
256         struct AdditionalCameraContainer {
257             RENDER_NS::RenderHandleReference handle;
258             RenderCamera::Flags flags { 0 };
259             RenderCamera::RenderPipelineType renderPipelineType { RenderCamera::RenderPipelineType::FORWARD };
260             uint64_t lastFrameIndex { 0 };   // frame when used
261             bool enableAutoDestroy { true }; // some might not be allowed to be destroyed (e.g. color pre-pass)
262             BASE_NS::fixed_string<RENDER_NS::RenderDataConstants::MAX_DEFAULT_NAME_LENGTH> postProcessName;
263         };
264 
265         // all render node graphs (scene rng is always the first and this needs to be in order)
266         BASE_NS::vector<RENDER_NS::RenderHandleReference> orderedRenderNodeGraphs;
267         // camera component id with generation hash
268         BASE_NS::unordered_map<uint64_t, AdditionalCameraContainer> camIdToRng;
269 
270         RENDER_NS::RenderHandleReference sceneRng;
271         uint64_t sceneMainCamId { 0 };
272 
273         // reset every frame (flags for rendering hints)
274         uint32_t frameFlags { 0u };
275 
276         // store created pods
277         BASE_NS::vector<BASE_NS::string> postProcessPods;
278     };
279     RenderProcessing renderProcessing_;
280 
281     struct CameraData {
282         BASE_NS::Math::Mat4X4 view;
283         BASE_NS::Math::Mat4X4 proj;
284         uint64_t lastFrameIndex { 0 }; // frame when used
285     };
286     // store previous frame matrices
287     BASE_NS::unordered_map<CORE_NS::Entity, CameraData> cameraData_;
288 
289     BASE_NS::unordered_map<CORE_NS::Entity, BatchDataVector> batches_;
290 };
291 CORE3D_END_NAMESPACE()
292 
293 #endif // CORE_ECS_RENDERSYSTEM_H
294