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/camera_component.h>
17 #include <core/property/property_types.h>
18
19 #include "ComponentTools/base_manager.h"
20 #include "ComponentTools/base_manager.inl"
21 #include "ecs/components/layer_flag_bits_metadata.h"
22
23 #define IMPLEMENT_MANAGER
24 #include "PropertyTools/property_macros.h"
25
26 CORE_BEGIN_NAMESPACE()
27 using BASE_NS::array_view;
28 using BASE_NS::countof;
29 using BASE_NS::Format;
30 using BASE_NS::vector;
31
32 using CORE3D_NS::CameraComponent;
33
34 // Extend propertysystem with the enums
35 DECLARE_PROPERTY_TYPE(CameraComponent::Projection);
36 DECLARE_PROPERTY_TYPE(CameraComponent::RenderingPipeline);
37 DECLARE_PROPERTY_TYPE(CameraComponent::Culling);
38 DECLARE_PROPERTY_TYPE(CameraComponent::TargetUsage);
39 DECLARE_PROPERTY_TYPE(vector<CameraComponent::TargetUsage>);
40 DECLARE_PROPERTY_TYPE(Format);
41
42 // Declare their metadata
43 BEGIN_ENUM(CameraTypeMetaData, CameraComponent::Projection)
44 DECL_ENUM(CameraComponent::Projection, ORTHOGRAPHIC, "Orthographic")
45 DECL_ENUM(CameraComponent::Projection, PERSPECTIVE, "Perspective")
46 DECL_ENUM(CameraComponent::Projection, FRUSTUM, "Frustum")
47 DECL_ENUM(CameraComponent::Projection, CUSTOM, "Custom Projection")
48 END_ENUM(CameraTypeMetaData, CameraComponent::Projection)
49
50 BEGIN_ENUM(CameraRenderPipelineTypeMetaData, CameraComponent::RenderingPipeline)
51 DECL_ENUM(CameraComponent::RenderingPipeline, LIGHT_FORWARD, "Light-Weight Forward")
52 DECL_ENUM(CameraComponent::RenderingPipeline, FORWARD, "Forward")
53 DECL_ENUM(CameraComponent::RenderingPipeline, DEFERRED, "Deferred")
54 DECL_ENUM(CameraComponent::RenderingPipeline, CUSTOM, "Custom")
55 END_ENUM(CameraRenderPipelineTypeMetaData, CameraComponent::RenderingPipeline)
56
57 BEGIN_ENUM(CameraCullTypeMetaData, CameraComponent::Culling)
58 DECL_ENUM(CameraComponent::Culling, NONE, "None")
59 DECL_ENUM(CameraComponent::Culling, VIEW_FRUSTUM, "View Frustum")
60 END_ENUM(CameraCullTypeMetaData, CameraComponent::Culling)
61
62 BEGIN_ENUM(CameraSceneFlagBitsMetaData, CameraComponent::SceneFlagBits)
63 DECL_ENUM(CameraComponent::SceneFlagBits, ACTIVE_RENDER_BIT, "Active Render")
64 DECL_ENUM(CameraComponent::SceneFlagBits, MAIN_CAMERA_BIT, "Main Camera")
65 END_ENUM(CameraSceneFlagBitsMetaData, CameraComponent::SceneFlagBits)
66
67 BEGIN_ENUM(CameraPipelineFlagBitsMetaData, CameraComponent::PipelineFlagBits)
68 DECL_ENUM(CameraComponent::PipelineFlagBits, CLEAR_DEPTH_BIT, "Clear Depth")
69 DECL_ENUM(CameraComponent::PipelineFlagBits, CLEAR_COLOR_BIT, "Clear Color")
70 DECL_ENUM(CameraComponent::PipelineFlagBits, MSAA_BIT, "MSAA")
71 DECL_ENUM(CameraComponent::PipelineFlagBits, ALLOW_COLOR_PRE_PASS_BIT, "Allow Color Pre-Pass")
72 DECL_ENUM(CameraComponent::PipelineFlagBits, FORCE_COLOR_PRE_PASS_BIT, "Force Color Pre-Pass")
73 DECL_ENUM(CameraComponent::PipelineFlagBits, HISTORY_BIT, "Create And Store History Image")
74 DECL_ENUM(CameraComponent::PipelineFlagBits, JITTER_BIT, "Jitter Camera, Offset Within Pixel")
75 DECL_ENUM(CameraComponent::PipelineFlagBits, VELOCITY_OUTPUT_BIT, "Output Samplable Velocity")
76 DECL_ENUM(CameraComponent::PipelineFlagBits, DEPTH_OUTPUT_BIT, "Output Samplable Depth")
77 DECL_ENUM(CameraComponent::PipelineFlagBits, MULTI_VIEW_ONLY_BIT, "Camera Will Be Used Only As A Multi-View Layer")
78 END_ENUM(CameraPipelineFlagBitsMetaData, CameraComponent::PipelineFlagBits)
79
80 // define some meaningful image formats for camera component usage
81 BEGIN_ENUM(FormatMetaData, Format)
82 DECL_ENUM(Format, BASE_FORMAT_UNDEFINED, "BASE_FORMAT_UNDEFINED")
83 DECL_ENUM(Format, BASE_FORMAT_R8G8B8A8_UNORM, "BASE_FORMAT_R8G8B8A8_UNORM")
84 DECL_ENUM(Format, BASE_FORMAT_R8G8B8A8_SRGB, "BASE_FORMAT_R8G8B8A8_SRGB")
85 DECL_ENUM(Format, BASE_FORMAT_A2R10G10B10_UNORM_PACK32, "BASE_FORMAT_A2R10G10B10_UNORM_PACK32")
86 DECL_ENUM(Format, BASE_FORMAT_R16G16B16A16_UNORM, "BASE_FORMAT_R16G16B16A16_UNORM")
87 DECL_ENUM(Format, BASE_FORMAT_R16G16B16A16_SFLOAT, "BASE_FORMAT_R16G16B16A16_SFLOAT")
88 DECL_ENUM(Format, BASE_FORMAT_B10G11R11_UFLOAT_PACK32, "BASE_FORMAT_B10G11R11_UFLOAT_PACK32")
89 DECL_ENUM(Format, BASE_FORMAT_D16_UNORM, "BASE_FORMAT_D16_UNORM")
90 DECL_ENUM(Format, BASE_FORMAT_D32_SFLOAT, "BASE_FORMAT_D32_SFLOAT")
91 DECL_ENUM(Format, BASE_FORMAT_D24_UNORM_S8_UINT, "BASE_FORMAT_D24_UNORM_S8_UINT")
92 END_ENUM(FormatMetaData, Format)
93
94 BEGIN_METADATA(CameraTargetUsageMetaData, CameraComponent::TargetUsage)
95 DECL_PROPERTY2(CameraComponent::TargetUsage, format, "Format", 0)
96 DECL_PROPERTY2(CameraComponent::TargetUsage, usageFlags, "Usage", 0)
97 END_METADATA(CameraTargetUsageMetaData, CameraComponent::TargetUsage)
98
99 DECLARE_CONTAINER_API(CameraTargetUsageContainerApi, CameraComponent::TargetUsage)
100 CORE_END_NAMESPACE()
101
102 CORE3D_BEGIN_NAMESPACE()
103 using BASE_NS::array_view;
104 using BASE_NS::countof;
105
106 using CORE_NS::BaseManager;
107 using CORE_NS::IComponentManager;
108 using CORE_NS::IEcs;
109 using CORE_NS::Property;
110 using CORE_NS::PropertyFlags;
111
112 class CameraComponentManager final : public BaseManager<CameraComponent, ICameraComponentManager> {
113 BEGIN_PROPERTY(CameraComponent, ComponentMetadata)
114 #include <3d/ecs/components/camera_component.h>
115 END_PROPERTY();
116 const array_view<const Property> componentMetaData_ { ComponentMetadata, countof(ComponentMetadata) };
117
118 public:
CameraComponentManager(IEcs & ecs)119 explicit CameraComponentManager(IEcs& ecs)
120 : BaseManager<CameraComponent, ICameraComponentManager>(ecs, CORE_NS::GetName<CameraComponent>())
121 {}
122
123 ~CameraComponentManager() = default;
124
PropertyCount() const125 size_t PropertyCount() const override
126 {
127 return componentMetaData_.size();
128 }
129
MetaData(size_t index) const130 const Property* MetaData(size_t index) const override
131 {
132 if (index < componentMetaData_.size()) {
133 return &componentMetaData_[index];
134 }
135 return nullptr;
136 }
137
MetaData() const138 array_view<const Property> MetaData() const override
139 {
140 return componentMetaData_;
141 }
142 };
143
ICameraComponentManagerInstance(IEcs & ecs)144 IComponentManager* ICameraComponentManagerInstance(IEcs& ecs)
145 {
146 return new CameraComponentManager(ecs);
147 }
148
ICameraComponentManagerDestroy(IComponentManager * instance)149 void ICameraComponentManagerDestroy(IComponentManager* instance)
150 {
151 delete static_cast<CameraComponentManager*>(instance);
152 }
153 CORE3D_END_NAMESPACE()
154