| 1 | /**************************************************************************/ |
| 2 | /* camera_attributes_storage.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "camera_attributes_storage.h" |
| 32 | |
| 33 | RendererCameraAttributes *RendererCameraAttributes::singleton = nullptr; |
| 34 | uint64_t RendererCameraAttributes::auto_exposure_counter = 2; |
| 35 | |
| 36 | RendererCameraAttributes::RendererCameraAttributes() { |
| 37 | singleton = this; |
| 38 | } |
| 39 | |
| 40 | RendererCameraAttributes::~RendererCameraAttributes() { |
| 41 | singleton = nullptr; |
| 42 | } |
| 43 | |
| 44 | RID RendererCameraAttributes::camera_attributes_allocate() { |
| 45 | return camera_attributes_owner.allocate_rid(); |
| 46 | } |
| 47 | |
| 48 | void RendererCameraAttributes::camera_attributes_initialize(RID p_rid) { |
| 49 | camera_attributes_owner.initialize_rid(p_rid, CameraAttributes()); |
| 50 | } |
| 51 | |
| 52 | void RendererCameraAttributes::camera_attributes_free(RID p_rid) { |
| 53 | camera_attributes_owner.free(p_rid); |
| 54 | } |
| 55 | |
| 56 | void RendererCameraAttributes::camera_attributes_set_dof_blur_quality(RS::DOFBlurQuality p_quality, bool p_use_jitter) { |
| 57 | dof_blur_quality = p_quality; |
| 58 | dof_blur_use_jitter = p_use_jitter; |
| 59 | } |
| 60 | |
| 61 | void RendererCameraAttributes::camera_attributes_set_dof_blur_bokeh_shape(RS::DOFBokehShape p_shape) { |
| 62 | dof_blur_bokeh_shape = p_shape; |
| 63 | } |
| 64 | |
| 65 | void RendererCameraAttributes::camera_attributes_set_dof_blur(RID p_camera_attributes, bool p_far_enable, float p_far_distance, float p_far_transition, bool p_near_enable, float p_near_distance, float p_near_transition, float p_amount) { |
| 66 | CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes); |
| 67 | ERR_FAIL_COND(!cam_attributes); |
| 68 | #ifdef DEBUG_ENABLED |
| 69 | if (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility" && (p_far_enable || p_near_enable)) { |
| 70 | WARN_PRINT_ONCE_ED("DoF blur is only available when using the Forward+ or Mobile rendering backends." ); |
| 71 | } |
| 72 | #endif |
| 73 | cam_attributes->dof_blur_far_enabled = p_far_enable; |
| 74 | cam_attributes->dof_blur_far_distance = p_far_distance; |
| 75 | cam_attributes->dof_blur_far_transition = p_far_transition; |
| 76 | |
| 77 | cam_attributes->dof_blur_near_enabled = p_near_enable; |
| 78 | cam_attributes->dof_blur_near_distance = p_near_distance; |
| 79 | cam_attributes->dof_blur_near_transition = p_near_transition; |
| 80 | |
| 81 | cam_attributes->dof_blur_amount = p_amount; |
| 82 | } |
| 83 | |
| 84 | bool RendererCameraAttributes::camera_attributes_get_dof_far_enabled(RID p_camera_attributes) { |
| 85 | CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes); |
| 86 | ERR_FAIL_COND_V(!cam_attributes, false); |
| 87 | return cam_attributes->dof_blur_far_enabled; |
| 88 | } |
| 89 | |
| 90 | float RendererCameraAttributes::camera_attributes_get_dof_far_distance(RID p_camera_attributes) { |
| 91 | CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes); |
| 92 | ERR_FAIL_COND_V(!cam_attributes, 0.0); |
| 93 | return cam_attributes->dof_blur_far_distance; |
| 94 | } |
| 95 | |
| 96 | float RendererCameraAttributes::camera_attributes_get_dof_far_transition(RID p_camera_attributes) { |
| 97 | CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes); |
| 98 | ERR_FAIL_COND_V(!cam_attributes, 0.0); |
| 99 | return cam_attributes->dof_blur_far_transition; |
| 100 | } |
| 101 | |
| 102 | bool RendererCameraAttributes::camera_attributes_get_dof_near_enabled(RID p_camera_attributes) { |
| 103 | CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes); |
| 104 | ERR_FAIL_COND_V(!cam_attributes, false); |
| 105 | return cam_attributes->dof_blur_near_enabled; |
| 106 | } |
| 107 | |
| 108 | float RendererCameraAttributes::camera_attributes_get_dof_near_distance(RID p_camera_attributes) { |
| 109 | CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes); |
| 110 | ERR_FAIL_COND_V(!cam_attributes, 0.0); |
| 111 | return cam_attributes->dof_blur_near_distance; |
| 112 | } |
| 113 | |
| 114 | float RendererCameraAttributes::camera_attributes_get_dof_near_transition(RID p_camera_attributes) { |
| 115 | CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes); |
| 116 | ERR_FAIL_COND_V(!cam_attributes, 0.0); |
| 117 | return cam_attributes->dof_blur_near_transition; |
| 118 | } |
| 119 | |
| 120 | float RendererCameraAttributes::camera_attributes_get_dof_blur_amount(RID p_camera_attributes) { |
| 121 | CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes); |
| 122 | ERR_FAIL_COND_V(!cam_attributes, 0.0); |
| 123 | return cam_attributes->dof_blur_amount; |
| 124 | } |
| 125 | |
| 126 | void RendererCameraAttributes::camera_attributes_set_exposure(RID p_camera_attributes, float p_multiplier, float p_exposure_normalization) { |
| 127 | CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes); |
| 128 | ERR_FAIL_COND(!cam_attributes); |
| 129 | cam_attributes->exposure_multiplier = p_multiplier; |
| 130 | cam_attributes->exposure_normalization = p_exposure_normalization; |
| 131 | } |
| 132 | |
| 133 | float RendererCameraAttributes::camera_attributes_get_exposure_normalization_factor(RID p_camera_attributes) { |
| 134 | CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes); |
| 135 | ERR_FAIL_COND_V(!cam_attributes, 1.0); |
| 136 | |
| 137 | return cam_attributes->exposure_multiplier * cam_attributes->exposure_normalization; |
| 138 | } |
| 139 | |
| 140 | void RendererCameraAttributes::camera_attributes_set_auto_exposure(RID p_camera_attributes, bool p_enable, float p_min_sensitivity, float p_max_sensitivity, float p_speed, float p_scale) { |
| 141 | CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes); |
| 142 | ERR_FAIL_COND(!cam_attributes); |
| 143 | if (!cam_attributes->use_auto_exposure && p_enable) { |
| 144 | cam_attributes->auto_exposure_version = ++auto_exposure_counter; |
| 145 | } |
| 146 | #ifdef DEBUG_ENABLED |
| 147 | if (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility" && p_enable) { |
| 148 | WARN_PRINT_ONCE_ED("Auto exposure is only available when using the Forward+ or Mobile rendering backends." ); |
| 149 | } |
| 150 | #endif |
| 151 | cam_attributes->use_auto_exposure = p_enable; |
| 152 | cam_attributes->auto_exposure_min_sensitivity = p_min_sensitivity; |
| 153 | cam_attributes->auto_exposure_max_sensitivity = p_max_sensitivity; |
| 154 | cam_attributes->auto_exposure_adjust_speed = p_speed; |
| 155 | cam_attributes->auto_exposure_scale = p_scale; |
| 156 | } |
| 157 | |
| 158 | float RendererCameraAttributes::camera_attributes_get_auto_exposure_min_sensitivity(RID p_camera_attributes) { |
| 159 | CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes); |
| 160 | ERR_FAIL_COND_V(!cam_attributes, 0.0); |
| 161 | return cam_attributes->auto_exposure_min_sensitivity; |
| 162 | } |
| 163 | |
| 164 | float RendererCameraAttributes::camera_attributes_get_auto_exposure_max_sensitivity(RID p_camera_attributes) { |
| 165 | CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes); |
| 166 | ERR_FAIL_COND_V(!cam_attributes, 0.0); |
| 167 | return cam_attributes->auto_exposure_max_sensitivity; |
| 168 | } |
| 169 | |
| 170 | float RendererCameraAttributes::camera_attributes_get_auto_exposure_adjust_speed(RID p_camera_attributes) { |
| 171 | CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes); |
| 172 | ERR_FAIL_COND_V(!cam_attributes, 0.0); |
| 173 | return cam_attributes->auto_exposure_adjust_speed; |
| 174 | } |
| 175 | |
| 176 | float RendererCameraAttributes::camera_attributes_get_auto_exposure_scale(RID p_camera_attributes) { |
| 177 | CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes); |
| 178 | ERR_FAIL_COND_V(!cam_attributes, 0.0); |
| 179 | return cam_attributes->auto_exposure_scale; |
| 180 | } |
| 181 | |
| 182 | uint64_t RendererCameraAttributes::camera_attributes_get_auto_exposure_version(RID p_camera_attributes) { |
| 183 | CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes); |
| 184 | ERR_FAIL_COND_V(!cam_attributes, 0); |
| 185 | return cam_attributes->auto_exposure_version; |
| 186 | } |
| 187 | |