openxr_fb_passthrough_extension_wrapper.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**************************************************************************/
  2. /* openxr_fb_passthrough_extension_wrapper.h */
  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. #ifndef OPENXR_FB_PASSTHROUGH_EXTENSION_WRAPPER_H
  31. #define OPENXR_FB_PASSTHROUGH_EXTENSION_WRAPPER_H
  32. #include "../openxr_api.h"
  33. #include "../util.h"
  34. #include "openxr_composition_layer_provider.h"
  35. #include "openxr_extension_wrapper.h"
  36. #include <map>
  37. class Viewport;
  38. // Wrapper for the set of Facebook XR passthrough extensions.
  39. class OpenXRFbPassthroughExtensionWrapper : public OpenXRExtensionWrapper, public OpenXRCompositionLayerProvider {
  40. public:
  41. OpenXRFbPassthroughExtensionWrapper();
  42. ~OpenXRFbPassthroughExtensionWrapper();
  43. virtual HashMap<String, bool *> get_requested_extensions() override;
  44. void on_instance_created(const XrInstance instance) override;
  45. void on_session_created(const XrSession session) override;
  46. void on_session_destroyed() override;
  47. void on_instance_destroyed() override;
  48. XrCompositionLayerBaseHeader *get_composition_layer() override;
  49. bool is_passthrough_supported() {
  50. return fb_passthrough_ext;
  51. }
  52. bool is_passthrough_enabled();
  53. bool start_passthrough();
  54. void stop_passthrough();
  55. static OpenXRFbPassthroughExtensionWrapper *get_singleton();
  56. private:
  57. // Create a passthrough feature
  58. EXT_PROTO_XRRESULT_FUNC3(xrCreatePassthroughFB,
  59. (XrSession), session,
  60. (const XrPassthroughCreateInfoFB *), create_info,
  61. (XrPassthroughFB *), feature_out)
  62. // Destroy a previously created passthrough feature
  63. EXT_PROTO_XRRESULT_FUNC1(xrDestroyPassthroughFB, (XrPassthroughFB), feature)
  64. //*** Passthrough feature state management functions *********
  65. // Start the passthrough feature
  66. EXT_PROTO_XRRESULT_FUNC1(xrPassthroughStartFB, (XrPassthroughFB), passthrough)
  67. // Pause the passthrough feature
  68. EXT_PROTO_XRRESULT_FUNC1(xrPassthroughPauseFB, (XrPassthroughFB), passthrough)
  69. EXT_PROTO_XRRESULT_FUNC3(xrCreatePassthroughLayerFB, (XrSession), session,
  70. (const XrPassthroughLayerCreateInfoFB *), config,
  71. (XrPassthroughLayerFB *), layer_out)
  72. EXT_PROTO_XRRESULT_FUNC1(xrDestroyPassthroughLayerFB, (XrPassthroughLayerFB), layer)
  73. EXT_PROTO_XRRESULT_FUNC1(xrPassthroughLayerPauseFB, (XrPassthroughLayerFB), layer)
  74. EXT_PROTO_XRRESULT_FUNC1(xrPassthroughLayerResumeFB, (XrPassthroughLayerFB), layer)
  75. // Set the style of an existing passthrough layer. If the enabled feature set
  76. // doesn’t change, this is a lightweight operation that can be called in every
  77. // frame to animate the style. Changes that may incur a bigger cost:
  78. // - Enabling/disabling the color mapping, or changing the type of mapping
  79. // (monochromatic to RGBA or back).
  80. // - Changing `textureOpacityFactor` from 0 to non-zero or vice versa
  81. // - Changing `edgeColor[3]` from 0 to non-zero or vice versa
  82. // NOTE: For XR_FB_passthrough, all color values are treated as linear.
  83. EXT_PROTO_XRRESULT_FUNC2(xrPassthroughLayerSetStyleFB,
  84. (XrPassthroughLayerFB), layer,
  85. (const XrPassthroughStyleFB *), style)
  86. // Create a geometry instance to be used as a projection surface for passthrough.
  87. // A geometry instance assigns a triangle mesh as part of the specified layer's
  88. // projection surface.
  89. // The operation is only valid if the passthrough layer's purpose has been set to
  90. // `XR_PASSTHROUGH_LAYER_PURPOSE_PROJECTED_FB`. Otherwise, the call this function will
  91. // result in an error. In the specified layer, Passthrough will be visible where the view
  92. // is covered by the user-specified geometries.
  93. //
  94. // A triangle mesh object can be instantiated multiple times - in the same or different layers'
  95. // projection surface. Each instantiation has its own transformation, which
  96. // can be updated using `xrGeometryInstanceSetTransformFB`.
  97. EXT_PROTO_XRRESULT_FUNC3(xrCreateGeometryInstanceFB,
  98. (XrSession), session,
  99. (const XrGeometryInstanceCreateInfoFB *), create_info,
  100. (XrGeometryInstanceFB *), out_geometry_instance)
  101. // Destroys a previously created geometry instance from passthrough rendering.
  102. // This removes the geometry instance from passthrough rendering.
  103. // The operation has no effect on other instances or the underlying mesh.
  104. EXT_PROTO_XRRESULT_FUNC1(xrDestroyGeometryInstanceFB, (XrGeometryInstanceFB), instance)
  105. // Update the transformation of a passthrough geometry instance.
  106. EXT_PROTO_XRRESULT_FUNC2(xrGeometryInstanceSetTransformFB,
  107. (XrGeometryInstanceFB), instance,
  108. (const XrGeometryInstanceTransformFB *), transformation)
  109. // Create a triangle mesh geometry object.
  110. // Depending on the behavior flags, the mesh could be created immutable (data is assigned
  111. // at creation and cannot be changed) or mutable (the mesh is created empty and can be updated
  112. // by calling begin/end update functions).
  113. EXT_PROTO_XRRESULT_FUNC3(xrCreateTriangleMeshFB,
  114. (XrSession), session,
  115. (const XrTriangleMeshCreateInfoFB *), create_info,
  116. (XrTriangleMeshFB *), out_triangle_mesh)
  117. // Destroy an `XrTriangleMeshFB` object along with its data. The mesh buffers must not be
  118. // accessed anymore after their parent mesh object has been destroyed.
  119. EXT_PROTO_XRRESULT_FUNC1(xrDestroyTriangleMeshFB, (XrTriangleMeshFB), mesh)
  120. // Retrieve a pointer to the vertex buffer. The vertex buffer is structured as an array of 3 floats
  121. // per vertex representing x, y, and z: `[x0, y0, z0, x1, y1, z1, ...]`. The size of the buffer is
  122. // `maxVertexCount * 3` floats. The application must call `xrTriangleMeshBeginUpdateFB` or
  123. // `xrTriangleMeshBeginVertexBufferUpdateFB` before making modifications to the vertex
  124. // buffer. The buffer location is guaranteed to remain constant over the lifecycle of the mesh
  125. // object.
  126. EXT_PROTO_XRRESULT_FUNC2(xrTriangleMeshGetVertexBufferFB,
  127. (XrTriangleMeshFB), mesh,
  128. (XrVector3f **), out_vertex_buffer)
  129. // Retrieve the index buffer that defines the topology of the triangle mesh. Each triplet of
  130. // consecutive elements point to three vertices in the vertex buffer and thus form a triangle. The
  131. // size of each element is `indexElementSize` bytes, and thus the size of the buffer is
  132. // `maxTriangleCount * 3 * indexElementSize` bytes. The application must call
  133. // `xrTriangleMeshBeginUpdateFB` before making modifications to the index buffer. The buffer
  134. // location is guaranteed to remain constant over the lifecycle of the mesh object.
  135. EXT_PROTO_XRRESULT_FUNC2(xrTriangleMeshGetIndexBufferFB,
  136. (XrTriangleMeshFB), mesh,
  137. (uint32_t **), out_index_buffer)
  138. // Begin updating the mesh buffer data. The application must call this function before it makes any
  139. // modifications to the buffers retrieved by `xrTriangleMeshGetVertexBufferFB` and
  140. // `xrTriangleMeshGetIndexBufferFB`. If only the vertex buffer needs to be updated,
  141. // `xrTriangleMeshBeginVertexBufferUpdateFB` can be used instead. To commit the
  142. // modifications, the application must call `xrTriangleMeshEndUpdateFB`.
  143. EXT_PROTO_XRRESULT_FUNC1(xrTriangleMeshBeginUpdateFB, (XrTriangleMeshFB), mesh)
  144. // Signal the API that the application has finished updating the mesh buffers after a call to
  145. // `xrTriangleMeshBeginUpdateFB`. `vertexCount` and `triangleCount` specify the actual
  146. // number of primitives that make up the mesh after the update. They must be larger than zero but
  147. // smaller or equal to the maximum counts defined at create time. Buffer data beyond these counts
  148. // is ignored.
  149. EXT_PROTO_XRRESULT_FUNC3(xrTriangleMeshEndUpdateFB,
  150. (XrTriangleMeshFB), mesh,
  151. (uint32_t), vertexCount,
  152. (uint32_t), triangle_count)
  153. // Update the vertex positions of a triangle mesh. Can only be called once the mesh topology has
  154. // been set using `xrTriangleMeshBeginUpdateFB`/`xrTriangleMeshEndUpdateFB`. The
  155. // vertex count is defined by the last invocation to `xrTriangleMeshEndUpdateFB`. Once the
  156. // modification is done, `xrTriangleMeshEndVertexBufferUpdateFB` must be called.
  157. EXT_PROTO_XRRESULT_FUNC2(xrTriangleMeshBeginVertexBufferUpdateFB,
  158. (XrTriangleMeshFB), mesh,
  159. (uint32_t *), out_vertex_count)
  160. // Signal the API that the contents of the vertex buffer data has been updated
  161. // after a call to `xrTriangleMeshBeginVertexBufferUpdateFB`.
  162. EXT_PROTO_XRRESULT_FUNC1(xrTriangleMeshEndVertexBufferUpdateFB, (XrTriangleMeshFB), mesh)
  163. bool initialize_fb_passthrough_extension(const XrInstance instance);
  164. bool initialize_fb_triangle_mesh_extension(const XrInstance instance);
  165. void cleanup();
  166. // TODO: Temporary workaround (https://github.com/GodotVR/godot_openxr/issues/138)
  167. // Address a bug in the passthrough api where XR_ERROR_UNEXPECTED_STATE_PASSTHROUGH_FB is
  168. // returned even when the operation is valid on Meta Quest devices.
  169. // The issue should be addressed on that platform in OS release v37.
  170. inline bool is_valid_passthrough_result(XrResult result, const char *format) {
  171. return OpenXRAPI::get_singleton()->xr_result(result, format) || result == XR_ERROR_UNEXPECTED_STATE_PASSTHROUGH_FB;
  172. }
  173. Viewport *get_main_viewport();
  174. static OpenXRFbPassthroughExtensionWrapper *singleton;
  175. bool fb_passthrough_ext = false; // required for any passthrough functionality
  176. bool fb_triangle_mesh_ext = false; // only use for projected passthrough
  177. XrPassthroughFB passthrough_handle = XR_NULL_HANDLE;
  178. XrPassthroughLayerFB passthrough_layer = XR_NULL_HANDLE;
  179. XrCompositionLayerPassthroughFB composition_passthrough_layer = {
  180. XR_TYPE_COMPOSITION_LAYER_PASSTHROUGH_FB,
  181. nullptr,
  182. XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT,
  183. XR_NULL_HANDLE,
  184. XR_NULL_HANDLE,
  185. };
  186. };
  187. #endif // OPENXR_FB_PASSTHROUGH_EXTENSION_WRAPPER_H