openxr_visibility_mask_extension.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**************************************************************************/
  2. /* openxr_visibility_mask_extension.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_VISIBILITY_MASK_EXTENSION_H
  31. #define OPENXR_VISIBILITY_MASK_EXTENSION_H
  32. #include "../util.h"
  33. #include "core/templates/vector.h"
  34. #include "openxr_extension_wrapper.h"
  35. #include "scene/resources/mesh.h"
  36. // The OpenXR visibility mask extension provides a mesh for each eye that
  37. // can be used as a mask to determine which part of our rendered result
  38. // is actually visible to the user. Due to lens distortion the edges of
  39. // the rendered image are never used in the final result output on the HMD.
  40. //
  41. // Blacking out this are of the render result can remove a fair amount of
  42. // overhead in rendering part of the screen that is unused.
  43. //
  44. // https://registry.khronos.org/OpenXR/specs/1.0/html/xrspec.html#XR_KHR_visibility_mask
  45. class OpenXRVisibilityMaskExtension : public OpenXRExtensionWrapper {
  46. public:
  47. static OpenXRVisibilityMaskExtension *get_singleton();
  48. OpenXRVisibilityMaskExtension();
  49. virtual ~OpenXRVisibilityMaskExtension() override;
  50. virtual HashMap<String, bool *> get_requested_extensions() override;
  51. virtual void on_instance_created(const XrInstance p_instance) override;
  52. virtual void on_session_created(const XrSession p_instance) override;
  53. virtual void on_session_destroyed() override;
  54. virtual void on_pre_render() override;
  55. virtual bool on_event_polled(const XrEventDataBuffer &event) override;
  56. bool is_available();
  57. RID get_mesh();
  58. private:
  59. static OpenXRVisibilityMaskExtension *singleton;
  60. bool available = false;
  61. bool is_dirty = false;
  62. RID shader;
  63. RID material;
  64. RID mesh;
  65. struct MeshData {
  66. Vector<XrVector2f> vertices;
  67. Vector<uint32_t> indices;
  68. };
  69. uint32_t mesh_count = 0;
  70. MeshData mesh_data[4];
  71. void _update_mesh_data(uint32_t p_view);
  72. void _update_mesh();
  73. // OpenXR API call wrappers
  74. EXT_PROTO_XRRESULT_FUNC5(xrGetVisibilityMaskKHR, (XrSession), session, (XrViewConfigurationType), viewConfigurationType, (uint32_t), viewIndex, (XrVisibilityMaskTypeKHR), visibilityMaskType, (XrVisibilityMaskKHR *), visibilityMask);
  75. };
  76. #endif // OPENXR_VISIBILITY_MASK_EXTENSION_H