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 <ComponentTools/base_manager.h>
17 #include <ComponentTools/base_manager.inl>
18 
19 #include <3d/ecs/components/material_extension_component.h>
20 #include <core/ecs/intf_ecs.h>
21 #include <core/intf_engine.h>
22 #include <core/plugin/intf_class_factory.h>
23 #include <core/plugin/intf_class_register.h>
24 #include <core/property/property_types.h>
25 #include <render/device/intf_gpu_resource_manager.h>
26 #include <render/implementation_uids.h>
27 #include <render/intf_render_context.h>
28 #include <render/resource_handle.h>
29 
30 #define IMPLEMENT_MANAGER
31 #include "PropertyTools/property_macros.h"
32 
33 CORE3D_BEGIN_NAMESPACE()
34 using BASE_NS::array_view;
35 using BASE_NS::countof;
36 
37 using CORE_NS::BaseManager;
38 using CORE_NS::GetInstance;
39 using CORE_NS::IClassRegister;
40 using CORE_NS::IComponentManager;
41 using CORE_NS::IEcs;
42 using CORE_NS::Property;
43 
44 using RENDER_NS::IGpuResourceManager;
45 using RENDER_NS::IRenderContext;
46 
47 class MaterialExtensionComponentManager final
48     : public BaseManager<MaterialExtensionComponent, IMaterialExtensionComponentManager> {
49     BEGIN_PROPERTY(MaterialExtensionComponent, ComponentMetadata)
50 #include <3d/ecs/components/material_extension_component.h>
51     END_PROPERTY();
52     const array_view<const Property> componentMetaData_ { ComponentMetadata, countof(ComponentMetadata) };
53     IGpuResourceManager& gpuResourceManager_;
54 
55 public:
MaterialExtensionComponentManager(IEcs & ecs)56     explicit MaterialExtensionComponentManager(IEcs& ecs)
57         : BaseManager<MaterialExtensionComponent, IMaterialExtensionComponentManager>(
58               ecs, CORE_NS::GetName<MaterialExtensionComponent>()),
59           gpuResourceManager_(GetInstance<IRenderContext>(
60               *ecs.GetClassFactory().GetInterface<IClassRegister>(), RENDER_NS::UID_RENDER_CONTEXT)
61                                   ->GetDevice()
62                                   .GetGpuResourceManager())
63     {}
64 
65     ~MaterialExtensionComponentManager() = default;
66 
PropertyCount() const67     size_t PropertyCount() const override
68     {
69         return componentMetaData_.size();
70     }
71 
MetaData(size_t index) const72     const Property* MetaData(size_t index) const override
73     {
74         if (index < componentMetaData_.size()) {
75             return &componentMetaData_[index];
76         }
77         return nullptr;
78     }
79 
MetaData() const80     array_view<const Property> MetaData() const override
81     {
82         return componentMetaData_;
83     }
84 
Destroy(CORE_NS::Entity entity)85     bool Destroy(CORE_NS::Entity entity) override
86     {
87         if (const auto id = GetComponentId(entity); id != INVALID_COMPONENT_ID) {
88             if (auto handle = Write(id); handle) {
89                 auto& comp = *handle;
90                 for (uint32_t idx = 0; idx < MaterialExtensionComponent::RESOURCE_COUNT; ++idx) {
91                     comp.resources[idx] = {};
92                 }
93             }
94             return BaseManager::Destroy(entity);
95         }
96         return false;
97     }
98 };
99 
IMaterialExtensionComponentManagerInstance(IEcs & ecs)100 IComponentManager* IMaterialExtensionComponentManagerInstance(IEcs& ecs)
101 {
102     return new MaterialExtensionComponentManager(ecs);
103 }
104 
IMaterialExtensionComponentManagerDestroy(IComponentManager * instance)105 void IMaterialExtensionComponentManagerDestroy(IComponentManager* instance)
106 {
107     delete static_cast<MaterialExtensionComponentManager*>(instance);
108 }
109 CORE3D_END_NAMESPACE()
110