openxr_vulkan_extension.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*************************************************************************/
  2. /* openxr_vulkan_extension.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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_VULKAN_EXTENSION_H
  31. #define OPENXR_VULKAN_EXTENSION_H
  32. #include "core/templates/vector.h"
  33. #include "openxr_extension_wrapper.h"
  34. #include "drivers/vulkan/vulkan_context.h"
  35. // Forward declare these so we don't need OpenXR headers where-ever this is included
  36. // Including OpenXR at this point gives loads and loads of compile issues especially
  37. // on Windows because windows.h is EVIL and really shouldn't be included outside of platform
  38. // but we really don't have a choice in the matter
  39. struct XrGraphicsRequirementsVulkanKHR;
  40. struct XrVulkanInstanceCreateInfoKHR;
  41. struct XrVulkanGraphicsDeviceGetInfoKHR;
  42. struct XrVulkanDeviceCreateInfoKHR;
  43. struct XrGraphicsBindingVulkanKHR;
  44. class OpenXRVulkanExtension : public OpenXRGraphicsExtensionWrapper, VulkanHooks {
  45. public:
  46. OpenXRVulkanExtension(OpenXRAPI *p_openxr_api);
  47. virtual ~OpenXRVulkanExtension() override;
  48. virtual void on_instance_created(const XrInstance p_instance) override;
  49. virtual void *set_session_create_and_get_next_pointer(void *p_next_pointer) override;
  50. virtual bool create_vulkan_instance(const VkInstanceCreateInfo *p_vulkan_create_info, VkInstance *r_instance) override;
  51. virtual bool get_physical_device(VkPhysicalDevice *r_device) override;
  52. virtual bool create_vulkan_device(const VkDeviceCreateInfo *p_device_create_info, VkDevice *r_device) override;
  53. virtual void get_usable_swapchain_formats(Vector<int64_t> &p_usable_swap_chains) override;
  54. virtual String get_swapchain_format_name(int64_t p_swapchain_format) const override;
  55. virtual bool get_swapchain_image_data(XrSwapchain p_swapchain, int64_t p_swapchain_format, uint32_t p_width, uint32_t p_height, uint32_t p_sample_count, uint32_t p_array_size, void **r_swapchain_graphics_data) override;
  56. virtual void cleanup_swapchain_graphics_data(void **p_swapchain_graphics_data) override;
  57. virtual bool create_projection_fov(const XrFovf p_fov, double p_z_near, double p_z_far, CameraMatrix &r_camera_matrix) override;
  58. virtual bool copy_render_target_to_image(RID p_from_render_target, void *p_swapchain_graphics_data, int p_image_index) override;
  59. private:
  60. static OpenXRVulkanExtension *singleton;
  61. static XrGraphicsBindingVulkanKHR graphics_binding_vulkan; // declaring this as static so we don't need to know its size and we only need it once when creating our session
  62. struct SwapchainGraphicsData {
  63. bool is_multiview;
  64. Vector<RID> image_rids;
  65. Vector<RID> framebuffers;
  66. };
  67. bool check_graphics_api_support(XrVersion p_desired_version);
  68. VkInstance vulkan_instance = nullptr;
  69. VkPhysicalDevice vulkan_physical_device = nullptr;
  70. VkDevice vulkan_device = nullptr;
  71. uint32_t vulkan_queue_family_index = 0;
  72. uint32_t vulkan_queue_index = 0;
  73. XrResult xrGetVulkanGraphicsRequirements2KHR(XrInstance p_instance, XrSystemId p_system_id, XrGraphicsRequirementsVulkanKHR *p_graphics_requirements);
  74. XrResult xrCreateVulkanInstanceKHR(XrInstance p_instance, const XrVulkanInstanceCreateInfoKHR *p_create_info, VkInstance *r_vulkan_instance, VkResult *r_vulkan_result);
  75. XrResult xrGetVulkanGraphicsDevice2KHR(XrInstance p_instance, const XrVulkanGraphicsDeviceGetInfoKHR *p_get_info, VkPhysicalDevice *r_vulkan_physical_device);
  76. XrResult xrCreateVulkanDeviceKHR(XrInstance p_instance, const XrVulkanDeviceCreateInfoKHR *p_create_info, VkDevice *r_device, VkResult *r_result);
  77. };
  78. #endif // !OPENXR_VULKAN_EXTENSION_H