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 <core/property_tools/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 DECLARE_PROPERTY_TYPE(CameraComponent::SampleCount);
42
43 // Declare their metadata
44 ENUM_TYPE_METADATA(CameraComponent::Projection, ENUM_VALUE(ORTHOGRAPHIC, "Orthographic"),
45 ENUM_VALUE(PERSPECTIVE, "Perspective"), ENUM_VALUE(FRUSTUM, "Frustum"), ENUM_VALUE(CUSTOM, "Custom Projection"))
46
47 ENUM_TYPE_METADATA(CameraComponent::RenderingPipeline, ENUM_VALUE(LIGHT_FORWARD, "Light-Weight Forward"),
48 ENUM_VALUE(FORWARD, "Forward"), ENUM_VALUE(DEFERRED, "Deferred"), ENUM_VALUE(CUSTOM, "Custom"))
49
50 ENUM_TYPE_METADATA(CameraComponent::Culling, ENUM_VALUE(NONE, "None"), ENUM_VALUE(VIEW_FRUSTUM, "View Frustum"))
51
52 ENUM_TYPE_METADATA(CameraComponent::SceneFlagBits, ENUM_VALUE(ACTIVE_RENDER_BIT, "Active Render"),
53 ENUM_VALUE(MAIN_CAMERA_BIT, "Main Camera"))
54
55 ENUM_TYPE_METADATA(CameraComponent::PipelineFlagBits, ENUM_VALUE(CLEAR_DEPTH_BIT, "Clear Depth"),
56 ENUM_VALUE(CLEAR_COLOR_BIT, "Clear Color"), ENUM_VALUE(MSAA_BIT, "MSAA"),
57 ENUM_VALUE(ALLOW_COLOR_PRE_PASS_BIT, "Allow Color Pre-Pass"),
58 ENUM_VALUE(FORCE_COLOR_PRE_PASS_BIT, "Force Color Pre-Pass"),
59 ENUM_VALUE(HISTORY_BIT, "Create And Store History Image"),
60 ENUM_VALUE(JITTER_BIT, "Jitter Camera, Offset Within Pixel"),
61 ENUM_VALUE(VELOCITY_OUTPUT_BIT, "Output Samplable Velocity"),
62 ENUM_VALUE(DEPTH_OUTPUT_BIT, "Output Samplable Depth"),
63 ENUM_VALUE(MULTI_VIEW_ONLY_BIT, "Camera Will Be Used Only As A Multi-View Layer"),
64 ENUM_VALUE(DISALLOW_REFLECTION_BIT, "Disallows Reflection Plane For Camera"),
65 ENUM_VALUE(CUBEMAP_BIT, "Camera Will Have Automatic Cubemap Targets"))
66
67 // define some meaningful image formats for camera component usage
68 ENUM_TYPE_METADATA(Format, ENUM_VALUE(BASE_FORMAT_UNDEFINED, "BASE_FORMAT_UNDEFINED"),
69 ENUM_VALUE(BASE_FORMAT_R8G8B8A8_UNORM, "BASE_FORMAT_R8G8B8A8_UNORM"),
70 ENUM_VALUE(BASE_FORMAT_R8G8B8A8_SRGB, "BASE_FORMAT_R8G8B8A8_SRGB"),
71 ENUM_VALUE(BASE_FORMAT_A2R10G10B10_UNORM_PACK32, "BASE_FORMAT_A2R10G10B10_UNORM_PACK32"),
72 ENUM_VALUE(BASE_FORMAT_R16G16B16A16_UNORM, "BASE_FORMAT_R16G16B16A16_UNORM"),
73 ENUM_VALUE(BASE_FORMAT_R16G16B16A16_SFLOAT, "BASE_FORMAT_R16G16B16A16_SFLOAT"),
74 ENUM_VALUE(BASE_FORMAT_B10G11R11_UFLOAT_PACK32, "BASE_FORMAT_B10G11R11_UFLOAT_PACK32"),
75 ENUM_VALUE(BASE_FORMAT_D16_UNORM, "BASE_FORMAT_D16_UNORM"),
76 ENUM_VALUE(BASE_FORMAT_D32_SFLOAT, "BASE_FORMAT_D32_SFLOAT"),
77 ENUM_VALUE(BASE_FORMAT_D24_UNORM_S8_UINT, "BASE_FORMAT_D24_UNORM_S8_UINT"))
78
79 ENUM_TYPE_METADATA(CameraComponent::SampleCount, ENUM_VALUE(SAMPLE_COUNT_2, "2 Samples"),
80 ENUM_VALUE(SAMPLE_COUNT_4, "4 Samples"), ENUM_VALUE(SAMPLE_COUNT_8, "8 Samples"))
81
82 DATA_TYPE_METADATA(
83 CameraComponent::TargetUsage, MEMBER_PROPERTY(format, "Format", 0), MEMBER_PROPERTY(usageFlags, "Usage", 0))
84
85 CORE_END_NAMESPACE()
86
87 CORE3D_BEGIN_NAMESPACE()
88 using BASE_NS::array_view;
89 using BASE_NS::countof;
90
91 using CORE_NS::BaseManager;
92 using CORE_NS::IComponentManager;
93 using CORE_NS::IEcs;
94 using CORE_NS::Property;
95 using CORE_NS::PropertyFlags;
96
97 class CameraComponentManager final : public BaseManager<CameraComponent, ICameraComponentManager> {
98 BEGIN_PROPERTY(CameraComponent, componentMetaData_)
99 #include <3d/ecs/components/camera_component.h>
100 END_PROPERTY();
101
102 public:
CameraComponentManager(IEcs & ecs)103 explicit CameraComponentManager(IEcs& ecs)
104 : BaseManager<CameraComponent, ICameraComponentManager>(ecs, CORE_NS::GetName<CameraComponent>())
105 {}
106
107 ~CameraComponentManager() = default;
108
PropertyCount() const109 size_t PropertyCount() const override
110 {
111 return BASE_NS::countof(componentMetaData_);
112 }
113
MetaData(size_t index) const114 const Property* MetaData(size_t index) const override
115 {
116 if (index < BASE_NS::countof(componentMetaData_)) {
117 return &componentMetaData_[index];
118 }
119 return nullptr;
120 }
121
MetaData() const122 array_view<const Property> MetaData() const override
123 {
124 return componentMetaData_;
125 }
126 };
127
ICameraComponentManagerInstance(IEcs & ecs)128 IComponentManager* ICameraComponentManagerInstance(IEcs& ecs)
129 {
130 return new CameraComponentManager(ecs);
131 }
132
ICameraComponentManagerDestroy(IComponentManager * instance)133 void ICameraComponentManagerDestroy(IComponentManager* instance)
134 {
135 delete static_cast<CameraComponentManager*>(instance);
136 }
137 CORE3D_END_NAMESPACE()
138