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