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_CAMERA_COMPONENT_H) || defined(IMPLEMENT_MANAGER)
17 #define API_3D_ECS_COMPONENTS_CAMERA_COMPONENT_H
18 
19 #if !defined(IMPLEMENT_MANAGER)
20 #include <3d/ecs/components/layer_defines.h>
21 #include <3d/namespace.h>
22 #include <base/containers/string.h>
23 #include <base/math/matrix.h>
24 #include <base/util/formats.h>
25 #include <core/ecs/component_struct_macros.h>
26 #include <core/ecs/entity_reference.h>
27 #include <core/ecs/intf_component_manager.h>
28 #include <core/namespace.h>
29 #include <render/device/gpu_resource_desc.h>
30 #include <render/resource_handle.h>
31 
32 CORE3D_BEGIN_NAMESPACE()
33 #endif
34 
35 BEGIN_COMPONENT(ICameraComponentManager, CameraComponent)
36 #if !defined(IMPLEMENT_MANAGER)
37     enum class Projection : uint8_t {
38         /** Orthographic camera */
39         ORTHOGRAPHIC = 0,
40         /** Perspective camera */
41         PERSPECTIVE = 1,
42         /** Frustum camera */
43         FRUSTUM = 2,
44         /** Custom matrix provided for camera */
45         CUSTOM = 3,
46     };
47 
48     enum class Culling : uint8_t {
49         /** None */
50         NONE = 0,
51         /** Basic view frustum cull for objects */
52         VIEW_FRUSTUM = 1,
53     };
54 
55     enum SceneFlagBits : uint32_t {
56         /** Camera is rendered when it's active. */
57         ACTIVE_RENDER_BIT = (1 << 0),
58         /** Main camera. If multiple main cameras, the first is chosen as ECS main camera. Main camera is treated always
59            as active. */
60         MAIN_CAMERA_BIT = (1 << 1),
61     };
62 
63     enum PipelineFlagBits : uint32_t {
64         /** Target clear flags depth. Override camera render node graph loadOp with clear.
65          * Without clear the default render node graph based loadOp is used. (Default pipelines use depth clear)
66          */
67         CLEAR_DEPTH_BIT = (1 << 0),
68         /** Target clear flags color. Override camera render node graph loadOp with clear.
69          * Without clear the default render node graph based loadOp is used. (Default pipelines do not use color clear)
70          */
71         CLEAR_COLOR_BIT = (1 << 1),
72         /** Enable MSAA for rendering. Only affects non deferred default pipelines. */
73         MSAA_BIT = (1 << 2),
74         /** Automatically use pre-pass if there are default material needs (e.g. for transmission). Automatic RNG
75            generation needs to be enabled for the ECS scene. */
76         ALLOW_COLOR_PRE_PASS_BIT = (1 << 3),
77         /** Force pre-pass every frame. Use for e.g. custom shaders without default material needs. Automatic RNG
78            generation needs to be enabled for the ECS scene. */
79         FORCE_COLOR_PRE_PASS_BIT = (1 << 4),
80         /** Store history (store history for next frame, needed for e.g. temporal filtering) */
81         HISTORY_BIT = (1 << 5),
82         /** Jitter camera. With Halton sampling */
83         JITTER_BIT = (1 << 6),
84         /** Output samplable velocity / normal */
85         VELOCITY_OUTPUT_BIT = (1 << 7),
86         /** Output samplable depth */
87         DEPTH_OUTPUT_BIT = (1 << 8),
88         /** Is a multi-view camera and is not be rendered separately at all
89          * The camera is added to other camera as multiViewCameras
90          */
91         MULTI_VIEW_ONLY_BIT = (1 << 9),
92         /** Generate environment cubemap dynamically for the camera
93          */
94         DYNAMIC_CUBEMAP_BIT = (1 << 10),
95         /** Disallow reflection plane for camera
96          */
97         DISALLOW_REFLECTION_BIT = (1 << 11),
98     };
99 
100     /** Target customization */
101     struct TargetUsage {
102         /** Target format */
103         BASE_NS::Format format { BASE_NS::Format::BASE_FORMAT_UNDEFINED };
104         /** Usage flags hints for optimizing resource creation */
105         RENDER_NS::ImageUsageFlags usageFlags { 0 };
106     };
107 
108     /** With default render node graphs one can select the pipeline */
109     enum class RenderingPipeline : uint8_t {
110         /** Light weight forward pipeline. Renders directly to back buffer */
111         LIGHT_FORWARD = 0,
112         /** Default forward pipeline */
113         FORWARD = 1,
114         /** Deferred pipeline */
115         DEFERRED = 2,
116         /** Custom rendering pipeline */
117         CUSTOM = 3,
118     };
119 #endif
120     /** Projection type of the camera.
121      */
122     DEFINE_PROPERTY(Projection, projection, "Projection", 0, VALUE(Projection::PERSPECTIVE))
123 
124     /** Culling mode for the camera.
125      */
126     DEFINE_PROPERTY(Culling, culling, "Culling", 0, VALUE(Culling::VIEW_FRUSTUM))
127 
128     /** Default camera rendering pipeline.
129      */
130     DEFINE_PROPERTY(RenderingPipeline, renderingPipeline, "Rendering Pipeline", 0, VALUE(RenderingPipeline::FORWARD))
131 
132     /** Scene flags.
133      */
134     DEFINE_BITFIELD_PROPERTY(
135         uint32_t, sceneFlags, "Scene Flags", PropertyFlags::IS_BITFIELD, VALUE(0), CameraComponent::SceneFlagBits)
136 
137     /** Render pipeline flags.
138      */
139     DEFINE_BITFIELD_PROPERTY(uint32_t, pipelineFlags, "Pipeline Flags", PropertyFlags::IS_BITFIELD,
140         VALUE(PipelineFlagBits::ALLOW_COLOR_PRE_PASS_BIT), CameraComponent::PipelineFlagBits)
141 
142     /** Aspect ratio of the camera (perspective only).
143      *  If aspect is 0 or less the aspect ratio of the canvas should be used.
144      */
145     DEFINE_PROPERTY(float, aspect, "Aspect Ratio", 0, VALUE(-1.f))
146 
147     /** Field of view of the camera (perspective only).
148      */
149     DEFINE_PROPERTY(float, yFov, "Vertical Fov", 0, VALUE(60.f * BASE_NS::Math::DEG2RAD))
150 
151     /** Viewport scale for orthographic.
152      */
153     DEFINE_PROPERTY(float, xMag, "X Scale for Orthographic", 0, VALUE(1.f))
154 
155     /** Viewport scale for orthographic.
156      */
157     DEFINE_PROPERTY(float, yMag, "Y Scale for Orthographic", 0, VALUE(1.f))
158 
159     /** Viewport horizontal offset for frustum.
160      */
161     DEFINE_PROPERTY(float, xOffset, "X Offset for Frustum", 0, VALUE(0.f))
162 
163     /** Viewport height for orthographic.
164      * Viewport vertical offset for frustum.
165      */
166     DEFINE_PROPERTY(float, yOffset, "Y Offset for Frustum", 0, VALUE(0.f))
167 
168     /** Near distance.
169      */
170     DEFINE_PROPERTY(float, zNear, "Z Near", 0, VALUE(0.3f))
171 
172     /** Far distance.
173      */
174     DEFINE_PROPERTY(float, zFar, "Z Far", 0, VALUE(1000.f))
175 
176     /** Viewport position and size in normalized render resolution: [0] = x, [1] = y, [2] = relative width [3] =
177      * relative height. (NOTE: relative width/height does not remove the offset)
178      */
179     DEFINE_PROPERTY(BASE_NS::Math::Vec4, viewport, "Viewport", 0, ARRAY_VALUE(0.0f, 0.0f, 1.0f, 1.0f))
180 
181     /** Scissor offset and size in normalized render resolution: [0] = x, [1] = y, [2] = relative width [3] =
182      * relative height. (NOTE: relative width/height does not remove the offset)
183      */
184     DEFINE_PROPERTY(BASE_NS::Math::Vec4, scissor, "Scissor", 0, ARRAY_VALUE(0.0f, 0.0f, 1.0f, 1.0f))
185 
186     /** Render resolution of the camera: [0] = width in pixels, [1] = height in pixels.
187      */
188     DEFINE_PROPERTY(BASE_NS::Math::UVec2, renderResolution, "Render Resolution", 0, ARRAY_VALUE(0, 0))
189 
190     /** Projection matrix used when type is CORE_CAMERA_TYPE_CUSTOM. For other camera types projection matrix is
191      * calculated from the other properties.
192      *
193      * The Vulkan coordinate system is used. Compared to the OpenGL coordinate system the NDC Y-axis is flipped and the
194      * depth range is from 0 to 1 (instead of -1 to 1).
195      *
196      * One possibility to convert an OpenGL projection matrix to the coordinate system used by the engine is to multiply
197      * it with matrix:
198      *
199      * | 1.0   0.0   0.0   0.0 |
200      * | 0.0  -1.0   0.0   0.0 |
201      * | 0.0   0.0   0.5   0.5 |
202      * | 0.0   0.0   0.0   1.0 |
203 
204      */
205     DEFINE_PROPERTY(BASE_NS::Math::Mat4X4, customProjectionMatrix, "Custom Projection Matrix", 0,
206         ARRAY_VALUE(1.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 0.f, 1.f))
207 
208     /** Clear color value. Clears the color buffer(s) if clearFlags set.
209      */
210     DEFINE_PROPERTY(BASE_NS::Math::Vec4, clearColorValue, "Clear Color Value", 0, ARRAY_VALUE(0.0f, 0.0f, 0.0f, 0.0f))
211 
212     /** Clear depth value. Clears the depth buffer(s) if clearFlags set.
213      */
214     DEFINE_PROPERTY(float, clearDepthValue, "Clear Depth Value", 0, VALUE(1.0f))
215 
216     /** Entity for environment component. Controls indirect and environment lighting.
217      */
218 
219     /** Entity containing an EnvironmentComponent that is used by this camera when rendering. Controls indirect and
220      * environment lighting options. If not defined the scene default environment options will be used.
221      */
222     DEFINE_PROPERTY(CORE_NS::Entity, environment, "Environment", 0, )
223 
224     /** Entity containing a FogComponent that is used by this camera when rendering. If not defined the
225      * camera will use default RenderConfigurationComponent configuration.
226      */
227     DEFINE_PROPERTY(CORE_NS::Entity, fog, "Fog", 0, )
228 
229     /** Entity containing a PostProcessComponent that is used by this camera when rendering. If not defined the
230      * camera will use default CORE3D_POST_PROCESS_CAM configuration.
231      */
232     DEFINE_PROPERTY(CORE_NS::Entity, postProcess, "Post process", 0, )
233 
234     /** Defines a layer mask which affects camera's rendering. Default is all layer mask, when the camera renders
235      * objects from all layers. */
236     DEFINE_BITFIELD_PROPERTY(uint64_t, layerMask, "Layer mask", PropertyFlags::IS_BITFIELD,
237         VALUE(LayerConstants::ALL_LAYER_MASK), LayerFlagBits)
238 
239     /** Entity containing a CameraComponent for pre-pass camera. Most of the values are copied for
240      * pre-pass render camera. PipelineFlagBits::XXX_COLOR_PRE_PASS_BIT (s) must be set.
241      * Pre-pass can be done automatically as well, but for resolution/layer etc. config ser can control it better.
242      * The active bit needs to be disabled from the pre-pass camera, otherwise the camera is processed normally.
243      */
244     DEFINE_PROPERTY(CORE_NS::Entity, prePassCamera, "Pre-pass camera", 0, )
245 
246     /** NOTE: add array of four to targets */
247 
248     /** Custom depth target. Must be a valid handle if using RenderTarget::CUSTOM.
249      */
250     DEFINE_PROPERTY(CORE_NS::EntityReference, customDepthTarget, "Custom Depth Target", 0, )
251 
252     /** Custom color target. Must be a valid handle if using RenderTarget::CUSTOM.
253      */
254     DEFINE_PROPERTY(BASE_NS::vector<CORE_NS::EntityReference>, customColorTargets, "Custom Color Targets", 0, )
255 
256     /** Depth target creation customization
257      */
258     DEFINE_PROPERTY(TargetUsage, depthTargetCustomization, "Depth Target Creation Customization", 0, )
259 
260     /** Color target creation customization
261      */
262     DEFINE_PROPERTY(BASE_NS::vector<TargetUsage>, colorTargetCustomization, "Color Target Creation Customization", 0, )
263 
264     /** Explicit custom camera render node graph. (Prefer using customRenderNodeGraphFile for correct patching)
265      */
266     DEFINE_PROPERTY(CORE_NS::EntityReference, customRenderNodeGraph, "Explicit Custom Camera Render Node Graph", 0, )
267 
268     /** Custom camera render node graph file. (Can be patched with e.g. post process ids etc.)
269      * Chosen only if no explicit customSceneRenderNodeGraph
270      */
271     DEFINE_PROPERTY(BASE_NS::string, customRenderNodeGraphFile, "Custom Scene Render Node Graph File", 0, )
272 
273     /** Multi-view camera entities for the base camera
274      */
275     DEFINE_PROPERTY(BASE_NS::vector<CORE_NS::Entity>, multiViewCameras, "Multi-view camera entities", 0, )
276 
277     /** Entity containing multiple EnvironmentComponents that are pushed to camera buffers.
278      * Controls indirect and environment lighting. If not defined the scene default environment options will be used */
279     DEFINE_PROPERTY(BASE_NS::vector<CORE_NS::Entity>, environments, "Environments", 0, )
280 
281 END_COMPONENT(ICameraComponentManager, CameraComponent, "184c996b-67aa-4456-9f03-72e2d968931b")
282 #if !defined(IMPLEMENT_MANAGER)
283 CORE3D_END_NAMESPACE()
284 #endif
285 
286 #endif
287