1 /* 2 * Copyright (c) 2022 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 93 /** Empty bit (1 << 10) */ 94 95 /** Disallow reflection plane for camera 96 */ 97 DISALLOW_REFLECTION_BIT = (1 << 11), 98 /** Automatic cubemap camera targets 99 * Can be used e.g. with dynamic cubemap renderings for camera 100 */ 101 CUBEMAP_BIT = (1 << 12), 102 }; 103 104 /** Target customization */ 105 struct TargetUsage { 106 /** Target format */ 107 BASE_NS::Format format { BASE_NS::Format::BASE_FORMAT_UNDEFINED }; 108 /** Usage flags hints for optimizing resource creation */ 109 RENDER_NS::ImageUsageFlags usageFlags { 0 }; 110 }; 111 112 /** With default render node graphs one can select the pipeline */ 113 enum class RenderingPipeline : uint8_t { 114 /** Light weight forward pipeline. Renders directly to back buffer */ 115 LIGHT_FORWARD = 0, 116 /** Default forward pipeline */ 117 FORWARD = 1, 118 /** Deferred pipeline */ 119 DEFERRED = 2, 120 /** Custom rendering pipeline */ 121 CUSTOM = 3, 122 }; 123 124 enum class SampleCount : uint8_t { 125 /** 2 samples */ 126 SAMPLE_COUNT_2 = 2U, 127 /** 4 samples */ 128 SAMPLE_COUNT_4 = 4U, 129 /** 8 samples */ 130 SAMPLE_COUNT_8 = 8U, 131 }; 132 #endif 133 /** Projection type of the camera. 134 */ 135 DEFINE_PROPERTY(Projection, projection, "Projection", 0, VALUE(Projection::PERSPECTIVE)) 136 137 /** Culling mode for the camera. 138 */ 139 DEFINE_PROPERTY(Culling, culling, "Culling", 0, VALUE(Culling::VIEW_FRUSTUM)) 140 141 /** Default camera rendering pipeline. 142 */ 143 DEFINE_PROPERTY(RenderingPipeline, renderingPipeline, "Rendering Pipeline", 0, VALUE(RenderingPipeline::FORWARD)) 144 145 /** Scene flags. 146 */ 147 DEFINE_BITFIELD_PROPERTY( 148 uint32_t, sceneFlags, "Scene Flags", PropertyFlags::IS_BITFIELD, VALUE(0), CameraComponent::SceneFlagBits) 149 150 /** Render pipeline flags. 151 */ 152 DEFINE_BITFIELD_PROPERTY(uint32_t, pipelineFlags, "Pipeline Flags", PropertyFlags::IS_BITFIELD, 153 VALUE(PipelineFlagBits::ALLOW_COLOR_PRE_PASS_BIT), CameraComponent::PipelineFlagBits) 154 155 /** Aspect ratio of the camera (perspective only). 156 * If aspect is 0 or less the aspect ratio of the canvas should be used. 157 */ 158 DEFINE_PROPERTY(float, aspect, "Aspect Ratio", 0, VALUE(-1.f)) 159 160 /** Field of view of the camera (perspective only). 161 */ 162 DEFINE_PROPERTY(float, yFov, "Vertical Fov", 0, VALUE(60.f * BASE_NS::Math::DEG2RAD)) 163 164 /** Viewport scale for orthographic. 165 */ 166 DEFINE_PROPERTY(float, xMag, "X Scale for Orthographic", 0, VALUE(1.f)) 167 168 /** Viewport scale for orthographic. 169 */ 170 DEFINE_PROPERTY(float, yMag, "Y Scale for Orthographic", 0, VALUE(1.f)) 171 172 /** Viewport horizontal offset for frustum. 173 */ 174 DEFINE_PROPERTY(float, xOffset, "X Offset for Frustum", 0, VALUE(0.f)) 175 176 /** Viewport height for orthographic. 177 * Viewport vertical offset for frustum. 178 */ 179 DEFINE_PROPERTY(float, yOffset, "Y Offset for Frustum", 0, VALUE(0.f)) 180 181 /** Near distance. 182 */ 183 DEFINE_PROPERTY(float, zNear, "Z Near", 0, VALUE(0.3f)) 184 185 /** Far distance. 186 */ 187 DEFINE_PROPERTY(float, zFar, "Z Far", 0, VALUE(1000.f)) 188 189 /** Viewport position and size in normalized render resolution: [0] = x, [1] = y, [2] = relative width [3] = 190 * relative height. (NOTE: relative width/height does not remove the offset) 191 */ 192 DEFINE_PROPERTY(BASE_NS::Math::Vec4, viewport, "Viewport", 0, ARRAY_VALUE(0.0f, 0.0f, 1.0f, 1.0f)) 193 194 /** Scissor offset and size in normalized render resolution: [0] = x, [1] = y, [2] = relative width [3] = 195 * relative height. (NOTE: relative width/height does not remove the offset) 196 */ 197 DEFINE_PROPERTY(BASE_NS::Math::Vec4, scissor, "Scissor", 0, ARRAY_VALUE(0.0f, 0.0f, 1.0f, 1.0f)) 198 199 /** Render resolution of the camera: [0] = width in pixels, [1] = height in pixels. 200 */ 201 DEFINE_PROPERTY(BASE_NS::Math::UVec2, renderResolution, "Render Resolution", 0, ARRAY_VALUE(0, 0)) 202 203 /** Projection matrix used when type is CORE_CAMERA_TYPE_CUSTOM. For other camera types projection matrix is 204 * calculated from the other properties. 205 * 206 * The Vulkan coordinate system is used. Compared to the OpenGL coordinate system the NDC Y-axis is flipped and the 207 * depth range is from 0 to 1 (instead of -1 to 1). 208 * 209 * One possibility to convert an OpenGL projection matrix to the coordinate system used by the engine is to multiply 210 * it with matrix: 211 * 212 * | 1.0 0.0 0.0 0.0 | 213 * | 0.0 -1.0 0.0 0.0 | 214 * | 0.0 0.0 0.5 0.5 | 215 * | 0.0 0.0 0.0 1.0 | 216 217 */ 218 DEFINE_PROPERTY(BASE_NS::Math::Mat4X4, customProjectionMatrix, "Custom Projection Matrix", 0, 219 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)) 220 221 /** Clear color value. Clears the color buffer(s) if clearFlags set. 222 */ 223 DEFINE_PROPERTY(BASE_NS::Math::Vec4, clearColorValue, "Clear Color Value", 0, ARRAY_VALUE(0.0f, 0.0f, 0.0f, 0.0f)) 224 225 /** Clear depth value. Clears the depth buffer(s) if clearFlags set. 226 */ 227 DEFINE_PROPERTY(float, clearDepthValue, "Clear Depth Value", 0, VALUE(1.0f)) 228 229 /** Entity for environment component. Controls indirect and environment lighting. 230 */ 231 232 /** Entity containing an EnvironmentComponent that is used by this camera when rendering. Controls indirect and 233 * environment lighting options. If not defined the scene default environment options will be used. 234 */ 235 DEFINE_PROPERTY(CORE_NS::Entity, environment, "Environment", 0, ) 236 237 /** Entity containing a FogComponent that is used by this camera when rendering. If not defined the 238 * camera will use default RenderConfigurationComponent configuration. 239 */ 240 DEFINE_PROPERTY(CORE_NS::Entity, fog, "Fog", 0, ) 241 242 /** Entity containing a PostProcessComponent that is used by this camera when rendering. If not defined the 243 * camera will use default CORE3D_POST_PROCESS_CAM configuration. 244 */ 245 DEFINE_PROPERTY(CORE_NS::Entity, postProcess, "Post process", 0, ) 246 247 /** Defines a layer mask which affects camera's rendering. Default is all layer mask, when the camera renders 248 * objects from all layers. */ 249 DEFINE_BITFIELD_PROPERTY(uint64_t, layerMask, "Layer mask", PropertyFlags::IS_BITFIELD, 250 VALUE(LayerConstants::ALL_LAYER_MASK), LayerFlagBits) 251 252 /** Entity containing a CameraComponent for pre-pass camera. Most of the values are copied for 253 * pre-pass render camera. PipelineFlagBits::XXX_COLOR_PRE_PASS_BIT (s) must be set. 254 * Pre-pass can be done automatically as well, but for resolution/layer etc. config ser can control it better. 255 * The active bit needs to be disabled from the pre-pass camera, otherwise the camera is processed normally. 256 */ 257 DEFINE_PROPERTY(CORE_NS::Entity, prePassCamera, "Pre-pass camera", 0, ) 258 259 /** NOTE: add array of four to targets */ 260 261 /** Custom depth target. Must be a valid handle if using RenderTarget::CUSTOM. 262 */ 263 DEFINE_PROPERTY(CORE_NS::EntityReference, customDepthTarget, "Custom Depth Target", 0, ) 264 265 /** Custom color target. Must be a valid handle if using RenderTarget::CUSTOM. 266 */ 267 DEFINE_PROPERTY(BASE_NS::vector<CORE_NS::EntityReference>, customColorTargets, "Custom Color Targets", 0, ) 268 269 /** Depth target creation customization 270 */ 271 DEFINE_PROPERTY(TargetUsage, depthTargetCustomization, "Depth Target Creation Customization", 0, ) 272 273 /** Color target creation customization 274 */ 275 DEFINE_PROPERTY(BASE_NS::vector<TargetUsage>, colorTargetCustomization, "Color Target Creation Customization", 0, ) 276 277 /** Explicit custom camera render node graph. (Prefer using customRenderNodeGraphFile for correct patching) 278 */ 279 DEFINE_PROPERTY(CORE_NS::EntityReference, customRenderNodeGraph, "Explicit Custom Camera Render Node Graph", 0, ) 280 281 /** Custom camera render node graph file. (Can be patched with e.g. post process ids etc.) 282 * Chosen only if no explicit customSceneRenderNodeGraph 283 */ 284 DEFINE_PROPERTY(BASE_NS::string, customRenderNodeGraphFile, "Custom Scene Render Node Graph File", 0, ) 285 286 /** Multi-view camera entities for the base camera 287 */ 288 DEFINE_PROPERTY(BASE_NS::vector<CORE_NS::Entity>, multiViewCameras, "Multi-view Camera Entities", 0, ) 289 290 /** MSAA sample count. Only in use when MSAA_BIT enabled 291 * Default value is 4U when MSAA is enabled 292 */ 293 DEFINE_PROPERTY(SampleCount, msaaSampleCount, "MSAA Sample Count", 0, VALUE(SampleCount::SAMPLE_COUNT_4)) 294 295 END_COMPONENT(ICameraComponentManager, CameraComponent, "184c996b-67aa-4456-9f03-72e2d968931b") 296 #if !defined(IMPLEMENT_MANAGER) 297 CORE3D_END_NAMESPACE() 298 #endif 299 300 #endif 301