xr_vrs.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**************************************************************************/
  2. /* xr_vrs.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 "xr_vrs.h"
  31. #include "servers/rendering/renderer_scene_render.h"
  32. #include "servers/rendering_server.h"
  33. void XRVRS::_bind_methods() {
  34. ClassDB::bind_method(D_METHOD("get_vrs_min_radius"), &XRVRS::get_vrs_min_radius);
  35. ClassDB::bind_method(D_METHOD("set_vrs_min_radius", "radius"), &XRVRS::set_vrs_min_radius);
  36. ClassDB::bind_method(D_METHOD("get_vrs_strength"), &XRVRS::get_vrs_strength);
  37. ClassDB::bind_method(D_METHOD("set_vrs_strength", "strength"), &XRVRS::set_vrs_strength);
  38. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "vrs_min_radius", PROPERTY_HINT_RANGE, "1.0,100.0,1.0"), "set_vrs_min_radius", "get_vrs_min_radius");
  39. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "vrs_strength", PROPERTY_HINT_RANGE, "0.1,10.0,0.1"), "set_vrs_strength", "get_vrs_strength");
  40. ClassDB::bind_method(D_METHOD("make_vrs_texture", "target_size", "eye_foci"), &XRVRS::make_vrs_texture);
  41. }
  42. XRVRS::~XRVRS() {
  43. if (vrs_texture.is_valid()) {
  44. ERR_FAIL_NULL(RS::get_singleton());
  45. RS::get_singleton()->free(vrs_texture);
  46. vrs_texture = RID();
  47. }
  48. }
  49. float XRVRS::get_vrs_min_radius() const {
  50. return vrs_min_radius;
  51. }
  52. void XRVRS::set_vrs_min_radius(float p_vrs_min_radius) {
  53. if (p_vrs_min_radius < 1.0) {
  54. WARN_PRINT_ONCE("VRS minimum radius can not be set below 1.0");
  55. vrs_min_radius = 1.0;
  56. } else if (p_vrs_min_radius > 100.0) {
  57. WARN_PRINT_ONCE("VRS minimum radius can not be set above 100.0");
  58. vrs_min_radius = 100.0;
  59. } else {
  60. vrs_min_radius = p_vrs_min_radius;
  61. vrs_dirty = true;
  62. }
  63. }
  64. float XRVRS::get_vrs_strength() const {
  65. return vrs_strength;
  66. }
  67. void XRVRS::set_vrs_strength(float p_vrs_strength) {
  68. if (p_vrs_strength < 0.1) {
  69. WARN_PRINT_ONCE("VRS strength can not be set below 0.1");
  70. vrs_strength = 0.1;
  71. } else if (p_vrs_strength > 10.0) {
  72. WARN_PRINT_ONCE("VRS strength can not be set above 10.0");
  73. vrs_strength = 10.0;
  74. } else {
  75. vrs_strength = p_vrs_strength;
  76. vrs_dirty = true;
  77. }
  78. }
  79. RID XRVRS::make_vrs_texture(const Size2 &p_target_size, const PackedVector2Array &p_eye_foci) {
  80. ERR_FAIL_COND_V(p_eye_foci.is_empty(), RID());
  81. int32_t texel_width = RD::get_singleton()->limit_get(RD::LIMIT_VRS_TEXEL_WIDTH);
  82. int32_t texel_height = RD::get_singleton()->limit_get(RD::LIMIT_VRS_TEXEL_HEIGHT);
  83. Size2 vrs_size = Size2(0.5 + p_target_size.x / texel_width, 0.5 + p_target_size.y / texel_height).round();
  84. float max_radius = 0.5 * MIN(vrs_size.x, vrs_size.y); // Maximum radius that fits inside of our image
  85. float min_radius = vrs_min_radius * max_radius / 100.0; // Minimum radius as a percentage of our size
  86. real_t outer_radius = MAX(1.0, (max_radius - min_radius) / vrs_strength);
  87. Size2 vrs_sizei = vrs_size;
  88. // Our density map is now unified, with a value of (0.0, 0.0) meaning a 1x1 texel size and (1.0, 1.0) an max texel size.
  89. // For our standard VRS extension on Vulkan this means a maximum of 8x8.
  90. // For the density map extension this scales depending on the max texel size.
  91. if (target_size != vrs_sizei || eye_foci != p_eye_foci || vrs_dirty) {
  92. // Out with the old.
  93. if (vrs_texture.is_valid()) {
  94. RS::get_singleton()->free(vrs_texture);
  95. vrs_texture = RID();
  96. }
  97. // In with the new.
  98. Vector<Ref<Image>> images;
  99. target_size = vrs_sizei;
  100. eye_foci = p_eye_foci;
  101. for (int i = 0; i < eye_foci.size() && i < RendererSceneRender::MAX_RENDER_VIEWS; i++) {
  102. PackedByteArray data;
  103. data.resize(vrs_sizei.x * vrs_sizei.y * 2);
  104. uint8_t *data_ptr = data.ptrw();
  105. Vector2i view_center;
  106. view_center.x = int(vrs_size.x * (eye_foci[i].x + 1.0) * 0.5);
  107. view_center.y = int(vrs_size.y * (eye_foci[i].y + 1.0) * 0.5);
  108. int d = 0;
  109. for (int y = 0; y < vrs_sizei.y; y++) {
  110. for (int x = 0; x < vrs_sizei.x; x++) {
  111. Vector2 offset = Vector2(x - view_center.x, y - view_center.y);
  112. real_t density = 255.0 * MAX(0.0, (Math::abs(offset.x) - min_radius) / outer_radius);
  113. data_ptr[d++] = MIN(255, density);
  114. density = 255.0 * MAX(0.0, (Math::abs(offset.y) - min_radius) / outer_radius);
  115. data_ptr[d++] = MIN(255, density);
  116. }
  117. }
  118. images.push_back(Image::create_from_data(vrs_sizei.x, vrs_sizei.y, false, Image::FORMAT_RG8, data));
  119. }
  120. if (images.size() == 1) {
  121. vrs_texture = RS::get_singleton()->texture_2d_create(images[0]);
  122. } else {
  123. vrs_texture = RS::get_singleton()->texture_2d_layered_create(images, RS::TEXTURE_LAYERED_2D_ARRAY);
  124. }
  125. vrs_dirty = false;
  126. }
  127. return vrs_texture;
  128. }