camera_attributes_storage.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #include "camera_attributes_storage.h"
  31. RendererCameraAttributes *RendererCameraAttributes::singleton = nullptr;
  32. uint64_t RendererCameraAttributes::auto_exposure_counter = 2;
  33. RendererCameraAttributes::RendererCameraAttributes() {
  34. singleton = this;
  35. }
  36. RendererCameraAttributes::~RendererCameraAttributes() {
  37. singleton = nullptr;
  38. }
  39. RID RendererCameraAttributes::camera_attributes_allocate() {
  40. return camera_attributes_owner.allocate_rid();
  41. }
  42. void RendererCameraAttributes::camera_attributes_initialize(RID p_rid) {
  43. camera_attributes_owner.initialize_rid(p_rid, CameraAttributes());
  44. }
  45. void RendererCameraAttributes::camera_attributes_free(RID p_rid) {
  46. camera_attributes_owner.free(p_rid);
  47. }
  48. void RendererCameraAttributes::camera_attributes_set_dof_blur_quality(RS::DOFBlurQuality p_quality, bool p_use_jitter) {
  49. dof_blur_quality = p_quality;
  50. dof_blur_use_jitter = p_use_jitter;
  51. }
  52. void RendererCameraAttributes::camera_attributes_set_dof_blur_bokeh_shape(RS::DOFBokehShape p_shape) {
  53. dof_blur_bokeh_shape = p_shape;
  54. }
  55. 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) {
  56. CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes);
  57. ERR_FAIL_NULL(cam_attributes);
  58. #ifdef DEBUG_ENABLED
  59. if (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility" && (p_far_enable || p_near_enable)) {
  60. WARN_PRINT_ONCE_ED("DoF blur is only available when using the Forward+ or Mobile rendering backends.");
  61. }
  62. #endif
  63. cam_attributes->dof_blur_far_enabled = p_far_enable;
  64. cam_attributes->dof_blur_far_distance = p_far_distance;
  65. cam_attributes->dof_blur_far_transition = p_far_transition;
  66. cam_attributes->dof_blur_near_enabled = p_near_enable;
  67. cam_attributes->dof_blur_near_distance = p_near_distance;
  68. cam_attributes->dof_blur_near_transition = p_near_transition;
  69. cam_attributes->dof_blur_amount = p_amount;
  70. }
  71. bool RendererCameraAttributes::camera_attributes_get_dof_far_enabled(RID p_camera_attributes) {
  72. CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes);
  73. ERR_FAIL_NULL_V(cam_attributes, false);
  74. return cam_attributes->dof_blur_far_enabled;
  75. }
  76. float RendererCameraAttributes::camera_attributes_get_dof_far_distance(RID p_camera_attributes) {
  77. CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes);
  78. ERR_FAIL_NULL_V(cam_attributes, 0.0);
  79. return cam_attributes->dof_blur_far_distance;
  80. }
  81. float RendererCameraAttributes::camera_attributes_get_dof_far_transition(RID p_camera_attributes) {
  82. CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes);
  83. ERR_FAIL_NULL_V(cam_attributes, 0.0);
  84. return cam_attributes->dof_blur_far_transition;
  85. }
  86. bool RendererCameraAttributes::camera_attributes_get_dof_near_enabled(RID p_camera_attributes) {
  87. CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes);
  88. ERR_FAIL_NULL_V(cam_attributes, false);
  89. return cam_attributes->dof_blur_near_enabled;
  90. }
  91. float RendererCameraAttributes::camera_attributes_get_dof_near_distance(RID p_camera_attributes) {
  92. CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes);
  93. ERR_FAIL_NULL_V(cam_attributes, 0.0);
  94. return cam_attributes->dof_blur_near_distance;
  95. }
  96. float RendererCameraAttributes::camera_attributes_get_dof_near_transition(RID p_camera_attributes) {
  97. CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes);
  98. ERR_FAIL_NULL_V(cam_attributes, 0.0);
  99. return cam_attributes->dof_blur_near_transition;
  100. }
  101. float RendererCameraAttributes::camera_attributes_get_dof_blur_amount(RID p_camera_attributes) {
  102. CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes);
  103. ERR_FAIL_NULL_V(cam_attributes, 0.0);
  104. return cam_attributes->dof_blur_amount;
  105. }
  106. void RendererCameraAttributes::camera_attributes_set_exposure(RID p_camera_attributes, float p_multiplier, float p_exposure_normalization) {
  107. CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes);
  108. ERR_FAIL_NULL(cam_attributes);
  109. cam_attributes->exposure_multiplier = p_multiplier;
  110. cam_attributes->exposure_normalization = p_exposure_normalization;
  111. }
  112. float RendererCameraAttributes::camera_attributes_get_exposure_normalization_factor(RID p_camera_attributes) {
  113. CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes);
  114. ERR_FAIL_NULL_V(cam_attributes, 1.0);
  115. return cam_attributes->exposure_multiplier * cam_attributes->exposure_normalization;
  116. }
  117. 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) {
  118. CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes);
  119. ERR_FAIL_NULL(cam_attributes);
  120. if (!cam_attributes->use_auto_exposure && p_enable) {
  121. cam_attributes->auto_exposure_version = ++auto_exposure_counter;
  122. }
  123. #ifdef DEBUG_ENABLED
  124. if (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility" && p_enable) {
  125. WARN_PRINT_ONCE_ED("Auto exposure is only available when using the Forward+ or Mobile rendering backends.");
  126. }
  127. #endif
  128. cam_attributes->use_auto_exposure = p_enable;
  129. cam_attributes->auto_exposure_min_sensitivity = p_min_sensitivity;
  130. cam_attributes->auto_exposure_max_sensitivity = p_max_sensitivity;
  131. cam_attributes->auto_exposure_adjust_speed = p_speed;
  132. cam_attributes->auto_exposure_scale = p_scale;
  133. }
  134. float RendererCameraAttributes::camera_attributes_get_auto_exposure_min_sensitivity(RID p_camera_attributes) {
  135. CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes);
  136. ERR_FAIL_NULL_V(cam_attributes, 0.0);
  137. return cam_attributes->auto_exposure_min_sensitivity;
  138. }
  139. float RendererCameraAttributes::camera_attributes_get_auto_exposure_max_sensitivity(RID p_camera_attributes) {
  140. CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes);
  141. ERR_FAIL_NULL_V(cam_attributes, 0.0);
  142. return cam_attributes->auto_exposure_max_sensitivity;
  143. }
  144. float RendererCameraAttributes::camera_attributes_get_auto_exposure_adjust_speed(RID p_camera_attributes) {
  145. CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes);
  146. ERR_FAIL_NULL_V(cam_attributes, 0.0);
  147. return cam_attributes->auto_exposure_adjust_speed;
  148. }
  149. float RendererCameraAttributes::camera_attributes_get_auto_exposure_scale(RID p_camera_attributes) {
  150. CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes);
  151. ERR_FAIL_NULL_V(cam_attributes, 0.0);
  152. return cam_attributes->auto_exposure_scale;
  153. }
  154. uint64_t RendererCameraAttributes::camera_attributes_get_auto_exposure_version(RID p_camera_attributes) {
  155. CameraAttributes *cam_attributes = camera_attributes_owner.get_or_null(p_camera_attributes);
  156. ERR_FAIL_NULL_V(cam_attributes, 0);
  157. return cam_attributes->auto_exposure_version;
  158. }