openxr_visibility_mask_extension.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /**************************************************************************/
  2. /* openxr_visibility_mask_extension.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 "openxr_visibility_mask_extension.h"
  31. #include "../openxr_api.h"
  32. #include "core/string/print_string.h"
  33. #include "core/variant/array.h"
  34. #include "core/variant/variant.h"
  35. #include "servers/rendering_server.h"
  36. static const char *VISIBILITY_MASK_SHADER_CODE =
  37. "shader_type spatial;\n"
  38. "render_mode unshaded, shadows_disabled, cull_disabled;\n"
  39. "void vertex() {\n"
  40. "\tif (int(VERTEX.z) == VIEW_INDEX) {\n"
  41. "\t\tVERTEX.z = -1.0;\n"
  42. "\t\tVERTEX += EYE_OFFSET;\n"
  43. "\t\tPOSITION = PROJECTION_MATRIX * vec4(VERTEX, 1.0);\n"
  44. "\t\tPOSITION.xy /= POSITION.w;\n"
  45. "\t\tPOSITION.z = 1.0;\n"
  46. "\t\tPOSITION.w = 1.0;\n"
  47. "\t} else {\n"
  48. "\t\tPOSITION = vec4(2.0, 2.0, 2.0, 1.0);\n"
  49. "\t}\n"
  50. "}\n"
  51. "void fragment() {\n"
  52. "\tALBEDO = vec3(0.0, 0.0, 0.0);\n"
  53. "}\n";
  54. OpenXRVisibilityMaskExtension *OpenXRVisibilityMaskExtension::singleton = nullptr;
  55. OpenXRVisibilityMaskExtension *OpenXRVisibilityMaskExtension::get_singleton() {
  56. return singleton;
  57. }
  58. OpenXRVisibilityMaskExtension::OpenXRVisibilityMaskExtension() {
  59. singleton = this;
  60. }
  61. OpenXRVisibilityMaskExtension::~OpenXRVisibilityMaskExtension() {
  62. singleton = nullptr;
  63. }
  64. HashMap<String, bool *> OpenXRVisibilityMaskExtension::get_requested_extensions() {
  65. HashMap<String, bool *> request_extensions;
  66. request_extensions[XR_KHR_VISIBILITY_MASK_EXTENSION_NAME] = &available;
  67. return request_extensions;
  68. }
  69. void OpenXRVisibilityMaskExtension::on_instance_created(const XrInstance p_instance) {
  70. if (available) {
  71. EXT_INIT_XR_FUNC(xrGetVisibilityMaskKHR);
  72. }
  73. }
  74. void OpenXRVisibilityMaskExtension::on_session_created(const XrSession p_instance) {
  75. if (available) {
  76. RS *rendering_server = RS::get_singleton();
  77. ERR_FAIL_NULL(rendering_server);
  78. OpenXRAPI *openxr_api = (OpenXRAPI *)OpenXRAPI::get_singleton();
  79. ERR_FAIL_NULL(openxr_api);
  80. // Create our shader.
  81. shader = rendering_server->shader_create();
  82. rendering_server->shader_set_code(shader, VISIBILITY_MASK_SHADER_CODE);
  83. // Create our material.
  84. material = rendering_server->material_create();
  85. rendering_server->material_set_shader(material, shader);
  86. rendering_server->material_set_render_priority(material, 99);
  87. // Create our mesh.
  88. mesh = rendering_server->mesh_create();
  89. // Get our initial mesh data.
  90. mesh_count = openxr_api->get_view_count(); // We need a mesh for each view.
  91. for (uint32_t i = 0; i < mesh_count; i++) {
  92. _update_mesh_data(i);
  93. }
  94. // And update our mesh
  95. _update_mesh();
  96. }
  97. }
  98. void OpenXRVisibilityMaskExtension::on_session_destroyed() {
  99. RS *rendering_server = RS::get_singleton();
  100. ERR_FAIL_NULL(rendering_server);
  101. // Free our mesh.
  102. if (mesh.is_valid()) {
  103. rendering_server->free(mesh);
  104. mesh = RID();
  105. }
  106. // Free our material.
  107. if (material.is_valid()) {
  108. rendering_server->free(material);
  109. material = RID();
  110. }
  111. // Free our shader.
  112. if (shader.is_valid()) {
  113. rendering_server->free(shader);
  114. shader = RID();
  115. }
  116. mesh_count = 0;
  117. }
  118. void OpenXRVisibilityMaskExtension::on_pre_render() {
  119. // Update mesh data if its dirty.
  120. // Here we call this from the rendering thread however as we're going through the rendering server this is safe.
  121. _update_mesh();
  122. }
  123. bool OpenXRVisibilityMaskExtension::on_event_polled(const XrEventDataBuffer &event) {
  124. if (event.type == XR_TYPE_EVENT_DATA_VISIBILITY_MASK_CHANGED_KHR) {
  125. XrEventDataVisibilityMaskChangedKHR *vismask_event = (XrEventDataVisibilityMaskChangedKHR *)&event;
  126. print_verbose("OpenXR EVENT: Visibility mask changed for view " + String::num_uint64(vismask_event->viewIndex));
  127. if (available) { // This event won't be called if this extension is not available but better safe than sorry.
  128. _update_mesh_data(vismask_event->viewIndex);
  129. }
  130. return true;
  131. }
  132. return false;
  133. }
  134. bool OpenXRVisibilityMaskExtension::is_available() {
  135. return available;
  136. }
  137. RID OpenXRVisibilityMaskExtension::get_mesh() {
  138. return mesh;
  139. }
  140. void OpenXRVisibilityMaskExtension::_update_mesh_data(uint32_t p_view) {
  141. if (available) {
  142. ERR_FAIL_UNSIGNED_INDEX(p_view, 4);
  143. OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();
  144. ERR_FAIL_NULL(openxr_api);
  145. XrSession session = openxr_api->get_session();
  146. XrViewConfigurationType view_configuration_type = openxr_api->get_view_configuration();
  147. // Figure out how much data we're getting.
  148. XrVisibilityMaskKHR visibility_mask_data = {
  149. XR_TYPE_VISIBILITY_MASK_KHR,
  150. nullptr,
  151. 0,
  152. 0,
  153. nullptr,
  154. 0,
  155. 0,
  156. nullptr,
  157. };
  158. XrResult result = xrGetVisibilityMaskKHR(session, view_configuration_type, p_view, XR_VISIBILITY_MASK_TYPE_HIDDEN_TRIANGLE_MESH_KHR, &visibility_mask_data);
  159. if (XR_FAILED(result)) {
  160. print_line("OpenXR: Unable to obtain visibility mask metrics [", openxr_api->get_error_string(result), "]");
  161. return;
  162. }
  163. // Resize buffers
  164. mesh_data[p_view].vertices.resize(visibility_mask_data.vertexCountOutput);
  165. mesh_data[p_view].indices.resize(visibility_mask_data.indexCountOutput);
  166. visibility_mask_data.vertexCapacityInput = visibility_mask_data.vertexCountOutput;
  167. visibility_mask_data.vertices = mesh_data[p_view].vertices.ptrw();
  168. visibility_mask_data.indexCapacityInput = visibility_mask_data.indexCountOutput;
  169. visibility_mask_data.indices = mesh_data[p_view].indices.ptrw();
  170. result = xrGetVisibilityMaskKHR(session, view_configuration_type, p_view, XR_VISIBILITY_MASK_TYPE_HIDDEN_TRIANGLE_MESH_KHR, &visibility_mask_data);
  171. if (XR_FAILED(result)) {
  172. print_line("OpenXR: Unable to obtain visibility mask data [", openxr_api->get_error_string(result), "]");
  173. return;
  174. }
  175. // Mark as dirty, we have updated mesh data.
  176. is_dirty = true;
  177. }
  178. }
  179. void OpenXRVisibilityMaskExtension::_update_mesh() {
  180. if (available && is_dirty && mesh_count > 0) {
  181. RS *rendering_server = RS::get_singleton();
  182. ERR_FAIL_NULL(rendering_server);
  183. OpenXRAPI *openxr_api = (OpenXRAPI *)OpenXRAPI::get_singleton();
  184. ERR_FAIL_NULL(openxr_api);
  185. // Combine all vertex and index buffers into one.
  186. PackedVector3Array vertices;
  187. PackedInt32Array indices;
  188. uint64_t vertice_count = 0;
  189. uint64_t index_count = 0;
  190. for (uint32_t i = 0; i < mesh_count; i++) {
  191. vertice_count += mesh_data[i].vertices.size();
  192. index_count += mesh_data[i].indices.size();
  193. }
  194. vertices.resize(vertice_count);
  195. indices.resize(index_count);
  196. uint64_t offset = 0;
  197. Vector3 *v_out = vertices.ptrw();
  198. int32_t *i_out = indices.ptrw();
  199. for (uint32_t i = 0; i < mesh_count; i++) {
  200. const XrVector2f *v_in = mesh_data[i].vertices.ptr();
  201. for (uint32_t j = 0; j < mesh_data[i].vertices.size(); j++) {
  202. v_out->x = v_in->x;
  203. v_out->y = v_in->y;
  204. v_out->z = float(i); // We store our view in our Z component, our shader will filter the right faces out.
  205. v_out++;
  206. v_in++;
  207. }
  208. const uint32_t *i_in = mesh_data[i].indices.ptr();
  209. for (uint32_t j = 0; j < mesh_data[i].indices.size(); j++) {
  210. *i_out = offset + *i_in;
  211. i_out++;
  212. i_in++;
  213. }
  214. offset += mesh_data[i].vertices.size();
  215. }
  216. // Update our mesh.
  217. Array arr;
  218. arr.resize(RS::ARRAY_MAX);
  219. arr[RS::ARRAY_VERTEX] = vertices;
  220. arr[RS::ARRAY_INDEX] = indices;
  221. rendering_server->mesh_clear(mesh);
  222. rendering_server->mesh_add_surface_from_arrays(mesh, RS::PRIMITIVE_TRIANGLES, arr);
  223. rendering_server->mesh_surface_set_material(mesh, 0, material);
  224. // Set no longer dirty.
  225. is_dirty = false;
  226. }
  227. }