xr_vrs.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. // Should return sensible data or graphics API does not support VRS.
  84. ERR_FAIL_COND_V(texel_width < 1 || texel_height < 1, RID());
  85. Size2 vrs_size = Size2(0.5 + p_target_size.x / texel_width, 0.5 + p_target_size.y / texel_height).round();
  86. // Make sure we have at least one pixel.
  87. vrs_size = vrs_size.maxf(1.0);
  88. float max_radius = 0.5 * MIN(vrs_size.x, vrs_size.y); // Maximum radius that fits inside of our image
  89. float min_radius = vrs_min_radius * max_radius / 100.0; // Minimum radius as a percentage of our size
  90. real_t outer_radius = MAX(1.0, (max_radius - min_radius) / vrs_strength);
  91. Size2 vrs_sizei = vrs_size;
  92. // 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.
  93. // For our standard VRS extension on Vulkan this means a maximum of 8x8.
  94. // For the density map extension this scales depending on the max texel size.
  95. if (target_size != vrs_sizei || eye_foci != p_eye_foci || vrs_dirty) {
  96. // Out with the old.
  97. if (vrs_texture.is_valid()) {
  98. RS::get_singleton()->free(vrs_texture);
  99. vrs_texture = RID();
  100. }
  101. // In with the new.
  102. Vector<Ref<Image>> images;
  103. target_size = vrs_sizei;
  104. eye_foci = p_eye_foci;
  105. for (int i = 0; i < eye_foci.size() && i < RendererSceneRender::MAX_RENDER_VIEWS; i++) {
  106. PackedByteArray data;
  107. data.resize(vrs_sizei.x * vrs_sizei.y * 2);
  108. uint8_t *data_ptr = data.ptrw();
  109. Vector2i view_center;
  110. view_center.x = int(vrs_size.x * (eye_foci[i].x + 1.0) * 0.5);
  111. view_center.y = int(vrs_size.y * (eye_foci[i].y + 1.0) * 0.5);
  112. int d = 0;
  113. for (int y = 0; y < vrs_sizei.y; y++) {
  114. for (int x = 0; x < vrs_sizei.x; x++) {
  115. Vector2 offset = Vector2(x - view_center.x, y - view_center.y);
  116. real_t density = 255.0 * MAX(0.0, (Math::abs(offset.x) - min_radius) / outer_radius);
  117. data_ptr[d++] = MIN(255, density);
  118. density = 255.0 * MAX(0.0, (Math::abs(offset.y) - min_radius) / outer_radius);
  119. data_ptr[d++] = MIN(255, density);
  120. }
  121. }
  122. images.push_back(Image::create_from_data(vrs_sizei.x, vrs_sizei.y, false, Image::FORMAT_RG8, data));
  123. }
  124. if (images.size() == 1) {
  125. vrs_texture = RS::get_singleton()->texture_2d_create(images[0]);
  126. } else {
  127. vrs_texture = RS::get_singleton()->texture_2d_layered_create(images, RS::TEXTURE_LAYERED_2D_ARRAY);
  128. }
  129. vrs_dirty = false;
  130. }
  131. return vrs_texture;
  132. }