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 #include <3d/ecs/components/animation_component.h>
17 #include <3d/ecs/components/animation_input_component.h>
18 #include <3d/ecs/components/animation_output_component.h>
19 #include <3d/ecs/components/animation_state_component.h>
20 #include <3d/ecs/components/animation_track_component.h>
21 #include <3d/ecs/components/camera_component.h>
22 #include <3d/ecs/components/environment_component.h>
23 #include <3d/ecs/components/fog_component.h>
24 #include <3d/ecs/components/joint_matrices_component.h>
25 #include <3d/ecs/components/layer_component.h>
26 #include <3d/ecs/components/light_component.h>
27 #include <3d/ecs/components/local_matrix_component.h>
28 #include <3d/ecs/components/material_component.h>
29 #include <3d/ecs/components/material_extension_component.h>
30 #include <3d/ecs/components/mesh_component.h>
31 #include <3d/ecs/components/morph_component.h>
32 #include <3d/ecs/components/name_component.h>
33 #include <3d/ecs/components/node_component.h>
34 #include <3d/ecs/components/physical_camera_component.h>
35 #include <3d/ecs/components/planar_reflection_component.h>
36 #include <3d/ecs/components/post_process_component.h>
37 #include <3d/ecs/components/post_process_configuration_component.h>
38 #include <3d/ecs/components/render_configuration_component.h>
39 #include <3d/ecs/components/render_handle_component.h>
40 #include <3d/ecs/components/render_mesh_batch_component.h>
41 #include <3d/ecs/components/render_mesh_component.h>
42 #include <3d/ecs/components/rsdz_model_id_component.h>
43 #include <3d/ecs/components/skin_component.h>
44 #include <3d/ecs/components/skin_ibm_component.h>
45 #include <3d/ecs/components/skin_joints_component.h>
46 #include <3d/ecs/components/transform_component.h>
47 #include <3d/ecs/components/uri_component.h>
48 #include <3d/ecs/components/world_matrix_component.h>
49 #include <3d/ecs/systems/intf_morphing_system.h>
50 #include <3d/ecs/systems/intf_node_system.h>
51 #include <3d/ecs/systems/intf_render_preprocessor_system.h>
52 #include <3d/ecs/systems/intf_render_system.h>
53 #include <3d/ecs/systems/intf_skinning_system.h>
54 #include <core/log.h>
55 #include <core/namespace.h>
56 #include <render/datastore/intf_render_data_store_manager.h>
57 #include <render/intf_plugin.h>
58 
59 #include "ecs/components/initial_transform_component.h"
60 #include "ecs/components/previous_joint_matrices_component.h"
61 #include "ecs/components/previous_world_matrix_component.h"
62 #include "ecs/systems/animation_system.h"
63 #include "ecs/systems/local_matrix_system.h"
64 #include "render/datastore/render_data_store_default_camera.h"
65 #include "render/datastore/render_data_store_default_light.h"
66 #include "render/datastore/render_data_store_default_material.h"
67 #include "render/datastore/render_data_store_default_scene.h"
68 #include "render/datastore/render_data_store_morph.h"
69 #include "render/node/render_node_camera_cubemap.h"
70 #include "render/node/render_node_camera_single_post_process.h"
71 #include "render/node/render_node_create_default_camera_gpu_images.h"
72 #include "render/node/render_node_default_camera_controller.h"
73 #include "render/node/render_node_default_camera_post_process_controller.h"
74 #include "render/node/render_node_default_cameras.h"
75 #include "render/node/render_node_default_depth_render_slot.h"
76 #include "render/node/render_node_default_env.h"
77 #include "render/node/render_node_default_lights.h"
78 #include "render/node/render_node_default_material_deferred_shading.h"
79 #include "render/node/render_node_default_material_objects.h"
80 #include "render/node/render_node_default_material_render_slot.h"
81 #include "render/node/render_node_default_shadow_render_slot.h"
82 #include "render/node/render_node_default_shadows_blur.h"
83 #include "render/node/render_node_morph.h"
84 
85 // Include the declarations directly from engine.
86 // NOTE: macro defined by cmake as CORE_STATIC_PLUGIN_HEADER="${CORE_ROOT_DIRECTORY}/src/static_plugin_decl.h"
87 // this is so that the core include directories are not leaked here, but we want this one header in this case.
88 #ifndef __APPLE__
89 #include CORE_STATIC_PLUGIN_HEADER
90 #include "registry_data.cpp"
91 #endif
92 
93 #define SYSTEM_FACTORY(type) type##Instance, type##Destroy
94 #define MANAGER_FACTORY(type) type##Instance, type##Destroy
95 
96 #define MANAGER(name, type)                                                                               \
97     extern IComponentManager* type##Instance(IEcs&);                                                      \
98     extern void type##Destroy(IComponentManager*);                                                        \
99     namespace {                                                                                           \
100     static constexpr auto name = ComponentManagerTypeInfo { { ComponentManagerTypeInfo::UID }, type::UID, \
101         CORE_NS::GetName<type>().data(), MANAGER_FACTORY(type) };                                         \
102     }
103 
104 #define SYSTEM(name, type, deps, readOnlyDeps)                                                                         \
105     extern ISystem* type##Instance(IEcs&);                                                                             \
106     extern void type##Destroy(ISystem*);                                                                               \
107     namespace {                                                                                                        \
108     static constexpr auto name = SystemTypeInfo { { SystemTypeInfo::UID }, type::UID, CORE_NS::GetName<type>().data(), \
109         SYSTEM_FACTORY(type), deps, readOnlyDeps };                                                                    \
110     }
111 
112 CORE3D_BEGIN_NAMESPACE()
113 using namespace BASE_NS;
114 using namespace CORE_NS;
115 using namespace RENDER_NS;
116 
117 // ECS components
118 MANAGER(CAMERA_COMPONENT_TYPE_INFO, ICameraComponentManager);
119 MANAGER(PHYSICAL_CAMERA_COMPONENT_TYPE_INFO, IPhysicalCameraComponentManager);
120 MANAGER(LIGHT_COMPONENT_TYPE_INFO, ILightComponentManager);
121 MANAGER(LOCAL_MATRIX_COMPONENT_TYPE_INFO, ILocalMatrixComponentManager);
122 MANAGER(NODE_COMPONENT_TYPE_INFO, INodeComponentManager);
123 MANAGER(RENDER_MESH_COMPONENT_TYPE_INFO, IRenderMeshComponentManager);
124 MANAGER(WORLD_MATRIX_COMPONENT_TYPE_INFO, IWorldMatrixComponentManager);
125 MANAGER(TRANSFORM_COMPONENT_TYPE_INFO, ITransformComponentManager);
126 MANAGER(INITIAL_TRANSFORM_COMPONENT_TYPE_INFO, IInitialTransformComponentManager);
127 MANAGER(RENDER_CONFIGURATION_COMPONENT_TYPE_INFO, IRenderConfigurationComponentManager);
128 MANAGER(SKIN_COMPONENT_TYPE_INFO, ISkinComponentManager);
129 MANAGER(SKIN_IBM_COMPONENT_TYPE_INFO, ISkinIbmComponentManager);
130 MANAGER(SKIN_JOINTS_COMPONENT_TYPE_INFO, ISkinJointsComponentManager);
131 MANAGER(JOINT_MATRICES_COMPONENT_TYPE_INFO, IJointMatricesComponentManager);
132 MANAGER(MORPH_COMPONENT_TYPE_INFO, IMorphComponentManager);
133 MANAGER(PLANAR_REFLECTION_COMPONENT_TYPE_INFO, IPlanarReflectionComponentManager);
134 MANAGER(RSDZ_MODEL_ID_COMPONENT_TYPE_INFO, IRSDZModelIdComponentManager);
135 MANAGER(MATERIAL_COMPONENT_TYPE_INFO, IMaterialComponentManager);
136 MANAGER(MATERIAL_EXTENSION_COMPONENT_TYPE_INFO, IMaterialExtensionComponentManager);
137 MANAGER(NAME_COMPONENT_TYPE_INFO, INameComponentManager);
138 MANAGER(MESH_COMPONENT_TYPE_INFO, IMeshComponentManager);
139 MANAGER(URI_COMPONENT_TYPE_INFO, IUriComponentManager);
140 MANAGER(ANIMATION_COMPONENT_TYPE_INFO, IAnimationComponentManager);
141 MANAGER(ANIMATION_INPUT_COMPONENT_TYPE_INFO, IAnimationInputComponentManager);
142 MANAGER(ANIMATION_OUTPUT_COMPONENT_TYPE_INFO, IAnimationOutputComponentManager);
143 MANAGER(ANIMATION_STATE_COMPONENT_TYPE_INFO, IAnimationStateComponentManager);
144 MANAGER(ANIMATION_TRACK_COMPONENT_TYPE_INFO, IAnimationTrackComponentManager);
145 MANAGER(ENVIRONMENT_COMPONENT_TYPE_INFO, IEnvironmentComponentManager);
146 MANAGER(FOG_COMPONENT_TYPE_INFO, IFogComponentManager);
147 MANAGER(RENDER_HANDLE_COMPONENT_TYPE_INFO, IRenderHandleComponentManager);
148 MANAGER(POST_PROCESS_COMPONENT_TYPE_INFO, IPostProcessComponentManager);
149 MANAGER(POST_PROCESS_CONFIGURATION_COMPONENT_TYPE_INFO, IPostProcessConfigurationComponentManager);
150 MANAGER(LAYER_COMPONENT_TYPE_INFO, ILayerComponentManager);
151 MANAGER(RENDER_MESH_BATCH_COMPONENT_TYPE_INFO, IRenderMeshBatchComponentManager);
152 MANAGER(PREV_WORLD_MATRIX_COMPONENT_TYPE_INFO, IPreviousWorldMatrixComponentManager);
153 MANAGER(PREV_JOINT_MATRICES_COMPONENT_TYPE_INFO, IPreviousJointMatricesComponentManager);
154 
155 namespace {
156 // Local matrix system dependencies.
157 static constexpr Uid LOCAL_MATRIX_SYSTEM_RW_DEPS[] = { LOCAL_MATRIX_COMPONENT_TYPE_INFO.uid };
158 static constexpr Uid LOCAL_MATRIX_SYSTEM_R_DEPS[] = { TRANSFORM_COMPONENT_TYPE_INFO.uid };
159 
160 // Node system dependencies.
161 static constexpr Uid NODE_SYSTEM_RW_DEPS[] = { WORLD_MATRIX_COMPONENT_TYPE_INFO.uid, NODE_COMPONENT_TYPE_INFO.uid,
162     PREV_WORLD_MATRIX_COMPONENT_TYPE_INFO.uid };
163 static constexpr Uid NODE_SYSTEM_R_DEPS[] = { NAME_COMPONENT_TYPE_INFO.uid, TRANSFORM_COMPONENT_TYPE_INFO.uid,
164     LOCAL_MATRIX_COMPONENT_TYPE_INFO.uid, RSDZ_MODEL_ID_COMPONENT_TYPE_INFO.uid };
165 
166 // Render preprocessor system dependencies.
167 static constexpr Uid RENDER_PREPROCESSOR_SYSTEM_R_DEPS[] = {
168     JOINT_MATRICES_COMPONENT_TYPE_INFO.uid,
169     LAYER_COMPONENT_TYPE_INFO.uid,
170     MATERIAL_COMPONENT_TYPE_INFO.uid,
171     MESH_COMPONENT_TYPE_INFO.uid,
172     NODE_COMPONENT_TYPE_INFO.uid,
173     RENDER_MESH_COMPONENT_TYPE_INFO.uid,
174     SKIN_COMPONENT_TYPE_INFO.uid,
175     WORLD_MATRIX_COMPONENT_TYPE_INFO.uid,
176 };
177 
178 // Render system dependencies.
179 static constexpr Uid RENDER_SYSTEM_RW_DEPS[] = {
180     PLANAR_REFLECTION_COMPONENT_TYPE_INFO.uid,
181     MATERIAL_COMPONENT_TYPE_INFO.uid,
182 };
183 static constexpr Uid RENDER_SYSTEM_R_DEPS[] = {
184     NODE_COMPONENT_TYPE_INFO.uid,
185     RENDER_MESH_COMPONENT_TYPE_INFO.uid,
186     WORLD_MATRIX_COMPONENT_TYPE_INFO.uid,
187     RENDER_CONFIGURATION_COMPONENT_TYPE_INFO.uid,
188     CAMERA_COMPONENT_TYPE_INFO.uid,
189     LIGHT_COMPONENT_TYPE_INFO.uid,
190     MATERIAL_EXTENSION_COMPONENT_TYPE_INFO.uid,
191     MESH_COMPONENT_TYPE_INFO.uid,
192     URI_COMPONENT_TYPE_INFO.uid,
193     NAME_COMPONENT_TYPE_INFO.uid,
194     ENVIRONMENT_COMPONENT_TYPE_INFO.uid,
195     FOG_COMPONENT_TYPE_INFO.uid,
196     RENDER_HANDLE_COMPONENT_TYPE_INFO.uid,
197     JOINT_MATRICES_COMPONENT_TYPE_INFO.uid,
198     POST_PROCESS_COMPONENT_TYPE_INFO.uid,
199     POST_PROCESS_CONFIGURATION_COMPONENT_TYPE_INFO.uid,
200     LAYER_COMPONENT_TYPE_INFO.uid,
201     RENDER_MESH_BATCH_COMPONENT_TYPE_INFO.uid,
202     PREV_WORLD_MATRIX_COMPONENT_TYPE_INFO.uid,
203     PREV_JOINT_MATRICES_COMPONENT_TYPE_INFO.uid,
204 };
205 
206 // Animation system dependencies.
207 static constexpr Uid ANIMATION_SYSTEM_RW_DEPS[] = {
208     INITIAL_TRANSFORM_COMPONENT_TYPE_INFO.uid,
209     ANIMATION_COMPONENT_TYPE_INFO.uid,
210     ANIMATION_STATE_COMPONENT_TYPE_INFO.uid,
211 };
212 static constexpr Uid ANIMATION_SYSTEM_R_DEPS[] = {
213     ANIMATION_INPUT_COMPONENT_TYPE_INFO.uid,
214     ANIMATION_OUTPUT_COMPONENT_TYPE_INFO.uid,
215     ANIMATION_TRACK_COMPONENT_TYPE_INFO.uid,
216     NAME_COMPONENT_TYPE_INFO.uid,
217     Uid {},
218 };
219 
220 // Skinning system dependencies.
221 static constexpr Uid SKINNING_SYSTEM_RW_DEPS[] = {
222     JOINT_MATRICES_COMPONENT_TYPE_INFO.uid,
223 };
224 static constexpr Uid SKINNING_SYSTEM_R_DEPS[] = {
225     SKIN_COMPONENT_TYPE_INFO.uid,
226     SKIN_IBM_COMPONENT_TYPE_INFO.uid,
227     SKIN_JOINTS_COMPONENT_TYPE_INFO.uid,
228     WORLD_MATRIX_COMPONENT_TYPE_INFO.uid,
229     NODE_COMPONENT_TYPE_INFO.uid,
230     RENDER_MESH_COMPONENT_TYPE_INFO.uid,
231     MESH_COMPONENT_TYPE_INFO.uid,
232     PREV_JOINT_MATRICES_COMPONENT_TYPE_INFO.uid,
233 };
234 
235 // Morph system dependencies.
236 static constexpr Uid MORPHING_SYSTEM_R_DEPS[] = {
237     NODE_COMPONENT_TYPE_INFO.uid,
238     MESH_COMPONENT_TYPE_INFO.uid,
239     MORPH_COMPONENT_TYPE_INFO.uid,
240     RENDER_MESH_COMPONENT_TYPE_INFO.uid,
241     RENDER_HANDLE_COMPONENT_TYPE_INFO.uid,
242 };
243 
244 static constexpr ComponentManagerTypeInfo CORE_COMPONENT_TYPE_INFOS[] = { CAMERA_COMPONENT_TYPE_INFO,
245     INITIAL_TRANSFORM_COMPONENT_TYPE_INFO, PHYSICAL_CAMERA_COMPONENT_TYPE_INFO, LIGHT_COMPONENT_TYPE_INFO,
246     LOCAL_MATRIX_COMPONENT_TYPE_INFO, NODE_COMPONENT_TYPE_INFO, WORLD_MATRIX_COMPONENT_TYPE_INFO,
247     RENDER_MESH_COMPONENT_TYPE_INFO, TRANSFORM_COMPONENT_TYPE_INFO, RENDER_CONFIGURATION_COMPONENT_TYPE_INFO,
248     SKIN_COMPONENT_TYPE_INFO, SKIN_JOINTS_COMPONENT_TYPE_INFO, JOINT_MATRICES_COMPONENT_TYPE_INFO,
249     MORPH_COMPONENT_TYPE_INFO, PLANAR_REFLECTION_COMPONENT_TYPE_INFO, RSDZ_MODEL_ID_COMPONENT_TYPE_INFO,
250     MATERIAL_COMPONENT_TYPE_INFO, MATERIAL_EXTENSION_COMPONENT_TYPE_INFO, NAME_COMPONENT_TYPE_INFO,
251     MESH_COMPONENT_TYPE_INFO, URI_COMPONENT_TYPE_INFO, SKIN_IBM_COMPONENT_TYPE_INFO, ANIMATION_COMPONENT_TYPE_INFO,
252     ANIMATION_INPUT_COMPONENT_TYPE_INFO, ANIMATION_OUTPUT_COMPONENT_TYPE_INFO, ANIMATION_STATE_COMPONENT_TYPE_INFO,
253     ANIMATION_TRACK_COMPONENT_TYPE_INFO, ENVIRONMENT_COMPONENT_TYPE_INFO, FOG_COMPONENT_TYPE_INFO,
254     RENDER_HANDLE_COMPONENT_TYPE_INFO, POST_PROCESS_COMPONENT_TYPE_INFO, POST_PROCESS_CONFIGURATION_COMPONENT_TYPE_INFO,
255     LAYER_COMPONENT_TYPE_INFO, RENDER_MESH_BATCH_COMPONENT_TYPE_INFO, PREV_WORLD_MATRIX_COMPONENT_TYPE_INFO,
256     PREV_JOINT_MATRICES_COMPONENT_TYPE_INFO };
257 } // namespace
258 
259 SYSTEM(LOCAL_MATRIX_SYSTEM_TYPE_INFO, LocalMatrixSystem, LOCAL_MATRIX_SYSTEM_RW_DEPS, LOCAL_MATRIX_SYSTEM_R_DEPS);
260 SYSTEM(NODE_SYSTEM_TYPE_INFO, INodeSystem, NODE_SYSTEM_RW_DEPS, NODE_SYSTEM_R_DEPS);
261 SYSTEM(RENDER_PREPROCESSOR_SYSTEM_TYPE_INFO, IRenderPreprocessorSystem, {}, RENDER_PREPROCESSOR_SYSTEM_R_DEPS);
262 SYSTEM(RENDER_SYSTEM_TYPE_INFO, IRenderSystem, RENDER_SYSTEM_RW_DEPS, RENDER_SYSTEM_R_DEPS);
263 SYSTEM(ANIMATION_SYSTEM_TYPE_INFO, IAnimationSystem, ANIMATION_SYSTEM_RW_DEPS, ANIMATION_SYSTEM_R_DEPS);
264 SYSTEM(SKINNING_SYSTEM_TYPE_INFO, ISkinningSystem, SKINNING_SYSTEM_RW_DEPS, SKINNING_SYSTEM_R_DEPS);
265 SYSTEM(MORPHING_SYSTEM_TYPE_INFO, IMorphingSystem, {}, MORPHING_SYSTEM_R_DEPS);
266 
267 namespace {
268 static constexpr SystemTypeInfo CORE_SYSTEM_TYPE_INFOS[] = {
269     LOCAL_MATRIX_SYSTEM_TYPE_INFO,
270     NODE_SYSTEM_TYPE_INFO,
271     RENDER_PREPROCESSOR_SYSTEM_TYPE_INFO,
272     RENDER_SYSTEM_TYPE_INFO,
273     ANIMATION_SYSTEM_TYPE_INFO,
274     SKINNING_SYSTEM_TYPE_INFO,
275     MORPHING_SYSTEM_TYPE_INFO,
276 };
277 
278 template<typename TypeInfo, typename RenderType>
Fill()279 constexpr auto Fill()
280 {
281     return TypeInfo { { TypeInfo::UID }, RenderType::UID, RenderType::TYPE_NAME, RenderType::Create,
282         RenderType::Destroy };
283 }
284 
285 static constexpr RenderDataStoreTypeInfo CORE_RENDER_DATA_STORE_INFOS[] = {
286     Fill<RenderDataStoreTypeInfo, RenderDataStoreDefaultCamera>(),
287     Fill<RenderDataStoreTypeInfo, RenderDataStoreDefaultLight>(),
288     Fill<RenderDataStoreTypeInfo, RenderDataStoreDefaultMaterial>(),
289     Fill<RenderDataStoreTypeInfo, RenderDataStoreDefaultScene>(),
290     Fill<RenderDataStoreTypeInfo, RenderDataStoreMorph>(),
291 };
292 
293 static constexpr RenderNodeTypeInfo CORE_RENDER_NODE_TYPE_INFOS[] = {
294     Fill<RenderNodeTypeInfo, RenderNodeCreateDefaultCameraGpuImages>(),
295     Fill<RenderNodeTypeInfo, RenderNodeDefaultCameraController>(),
296     Fill<RenderNodeTypeInfo, RenderNodeDefaultCameraPostProcessController>(),
297     Fill<RenderNodeTypeInfo, RenderNodeDefaultCameras>(),
298     Fill<RenderNodeTypeInfo, RenderNodeDefaultDepthRenderSlot>(),
299     Fill<RenderNodeTypeInfo, RenderNodeDefaultEnv>(),
300     Fill<RenderNodeTypeInfo, RenderNodeDefaultLights>(),
301     Fill<RenderNodeTypeInfo, RenderNodeDefaultMaterialDeferredShading>(),
302     Fill<RenderNodeTypeInfo, RenderNodeDefaultMaterialObjects>(),
303     Fill<RenderNodeTypeInfo, RenderNodeDefaultMaterialRenderSlot>(),
304     Fill<RenderNodeTypeInfo, RenderNodeDefaultShadowRenderSlot>(),
305     Fill<RenderNodeTypeInfo, RenderNodeDefaultShadowsBlur>(),
306     Fill<RenderNodeTypeInfo, RenderNodeMorph>(),
307     Fill<RenderNodeTypeInfo, RenderNodeCameraSinglePostProcess>(),
308     Fill<RenderNodeTypeInfo, RenderNodeCameraCubemap>(),
309 };
310 } // namespace
311 
RegisterTypes(IPluginRegister & pluginRegistry)312 void RegisterTypes(IPluginRegister& pluginRegistry)
313 {
314     for (const auto& info : CORE_RENDER_DATA_STORE_INFOS) {
315         pluginRegistry.RegisterTypeInfo(info);
316     }
317     for (const auto& info : CORE_RENDER_NODE_TYPE_INFOS) {
318         pluginRegistry.RegisterTypeInfo(info);
319     }
320     for (const auto& info : CORE_COMPONENT_TYPE_INFOS) {
321         pluginRegistry.RegisterTypeInfo(info);
322     }
323     for (const auto& info : CORE_SYSTEM_TYPE_INFOS) {
324         pluginRegistry.RegisterTypeInfo(info);
325     }
326 }
327 
UnregisterTypes(IPluginRegister & pluginRegistry)328 void UnregisterTypes(IPluginRegister& pluginRegistry)
329 {
330     for (const auto& info : CORE_SYSTEM_TYPE_INFOS) {
331         pluginRegistry.UnregisterTypeInfo(info);
332     }
333     for (const auto& info : CORE_COMPONENT_TYPE_INFOS) {
334         pluginRegistry.UnregisterTypeInfo(info);
335     }
336     for (const auto& info : CORE_RENDER_NODE_TYPE_INFOS) {
337         pluginRegistry.UnregisterTypeInfo(info);
338     }
339     for (const auto& info : CORE_RENDER_DATA_STORE_INFOS) {
340         pluginRegistry.UnregisterTypeInfo(info);
341     }
342 }
343 CORE3D_END_NAMESPACE()
344