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