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 #if !defined(API_3D_ECS_COMPONENTS_RENDER_HANDLE_COMPONENT_H) || defined(IMPLEMENT_MANAGER)
17 #define API_3D_ECS_COMPONENTS_RENDER_HANDLE_COMPONENT_H
18
19 #if !defined(IMPLEMENT_MANAGER)
20 #include <3d/namespace.h>
21 #include <core/ecs/component_struct_macros.h>
22 #include <core/ecs/entity.h>
23 #include <core/ecs/entity_reference.h>
24 #include <core/ecs/intf_component_manager.h>
25 #include <core/ecs/intf_entity_manager.h>
26 #include <core/namespace.h>
27 #include <render/resource_handle.h>
28
29 CORE3D_BEGIN_NAMESPACE()
30 #endif
31 /**
32 * Render handle component for sharing and naming rendering resources.
33 * One can add Uri and NameComponents to the owning entity for easier resource identification. The entity owning this
34 * component should be wrapped in an EntityReference to allow reference counted usage of the resource from other
35 * components. When a RenderHandleComponent is destroyed it releases its render handle reference. Once all references
36 * are gone the resource will be released.
37 */
BEGIN_COMPONENT(IRenderHandleComponentManager,RenderHandleComponent)38 BEGIN_COMPONENT(IRenderHandleComponentManager, RenderHandleComponent)
39
40 /** Handle to the render resources.
41 * Further details of the image can be queried with IGpuResourceManager::IsGpuBuffer/Image/Sampler*,
42 * IGpuResourceManager::GetBuffer/Image/SamplerDescriptor.
43 */
44 DEFINE_PROPERTY(RENDER_NS::RenderHandleReference, reference, "Render Handle Reference", 0, )
45
46 END_COMPONENT_EXT(
47 IRenderHandleComponentManager, RenderHandleComponent, "fb5c16b5-c369-4f7b-bc02-5398ddfdfa1d",
48 // The following getters are helpers for asking a handle or handle reference without first asking the component.
49 /** Get render handle reference.
50 * @param entity Entity.
51 * @return If the entity had a RenderHandleComponent the component's handle reference is returned.
52 */
53 virtual RENDER_NS::RenderHandleReference GetRenderHandleReference(CORE_NS::Entity entity) const = 0;
54 /** Get render handle reference.
55 * @param index Index of the component.
56 * @return The handle reference of the component at the given index is returned.
57 */
58 virtual RENDER_NS::RenderHandleReference GetRenderHandleReference(CORE_NS::IComponentManager::ComponentId index)
59 const = 0;
60 /** Get render handle.
61 * @param entity Entity.
62 * @return If the entity had a RenderHandleComponent the component's handle reference is returned.
63 */
64 virtual RENDER_NS::RenderHandle GetRenderHandle(CORE_NS::Entity entity) const = 0;
65 /** Get render handle.
66 * @param index Index of the component.
67 * @return The handle of the component at the given index is returned.
68 */
69 virtual RENDER_NS::RenderHandle GetRenderHandle(CORE_NS::IComponentManager::ComponentId index) const = 0;
70 /** Get an entity which has RenderHandleComponent referencing the given render handle.
71 * @param handle Render handle to search for.
72 * @return Entity with a RenderHandleComponent referencing the handle.
73 */
74 virtual CORE_NS::Entity GetEntityWithReference(const RENDER_NS::RenderHandleReference& handle) const = 0;)
75
76 #if !defined(IMPLEMENT_MANAGER)
77 /**
78 * Helper function to get or create entity reference for render handle reference.
79 * @param entityMgr Entity manager.
80 * @param rhcMgr Render handle component manager.
81 * @param handle Render handle reference for the render resource.
82 * @return EntityReference for render handle reference
83 */
84 inline CORE_NS::EntityReference GetOrCreateEntityReference(CORE_NS::IEntityManager& entityMgr,
85 IRenderHandleComponentManager& rhcMgr, const RENDER_NS::RenderHandleReference& handle)
86 {
87 CORE_NS::EntityReference entity = entityMgr.GetReferenceCounted(rhcMgr.GetEntityWithReference(handle));
88 if (!entity) {
89 entity = entityMgr.CreateReferenceCounted();
90 rhcMgr.Create(entity);
91 rhcMgr.Write(entity)->reference = handle;
92 }
93 return entity;
94 }
95
96 CORE3D_END_NAMESPACE()
97 #endif
98 #endif // API_3D_ECS_COMPONENTS_RENDER_HANDLE_COMPONENT_H
99