Path: blob/master/modules/openxr/extensions/openxr_composition_layer_extension.h
21658 views
/**************************************************************************/1/* openxr_composition_layer_extension.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "openxr_extension_wrapper.h"3334#include "../openxr_api.h"3536#ifdef ANDROID_ENABLED37#include <jni.h>3839// Copied here from openxr_platform.h, in order to avoid including that whole header,40// which can cause compilation issues on some platforms.41typedef XrResult(XRAPI_PTR *PFN_xrCreateSwapchainAndroidSurfaceKHR)(XrSession session, const XrSwapchainCreateInfo *info, XrSwapchain *swapchain, jobject *surface);42#endif4344class JavaObject;4546// This extension provides access to composition layers for displaying 2D content through the XR compositor.4748#define OPENXR_LAYER_FUNC1(m_name, m_arg1) \49void _composition_layer_##m_name##_rt(RID p_layer, m_arg1 p1) { \50CompositionLayer *layer = composition_layer_owner.get_or_null(p_layer); \51ERR_FAIL_NULL(layer); \52layer->m_name(p1); \53} \54void composition_layer_##m_name(RID p_layer, m_arg1 p1) { \55RenderingServer::get_singleton()->call_on_render_thread(callable_mp(this, &OpenXRCompositionLayerExtension::_composition_layer_##m_name##_rt).bind(p_layer, p1)); \56}5758#define OPENXR_LAYER_FUNC2(m_name, m_arg1, m_arg2) \59void _composition_layer_##m_name##_rt(RID p_layer, m_arg1 p1, m_arg2 p2) { \60CompositionLayer *layer = composition_layer_owner.get_or_null(p_layer); \61ERR_FAIL_NULL(layer); \62layer->m_name(p1, p2); \63} \64void composition_layer_##m_name(RID p_layer, m_arg1 p1, m_arg2 p2) { \65RenderingServer::get_singleton()->call_on_render_thread(callable_mp(this, &OpenXRCompositionLayerExtension::_composition_layer_##m_name##_rt).bind(p_layer, p1, p2)); \66}6768// OpenXRCompositionLayerExtension enables the extensions related to this functionality69class OpenXRCompositionLayerExtension : public OpenXRExtensionWrapper {70GDCLASS(OpenXRCompositionLayerExtension, OpenXRExtensionWrapper);7172protected:73static void _bind_methods() {}7475public:76// Must be identical to Filter enum definition in OpenXRCompositionLayer.77enum Filter {78FILTER_NEAREST,79FILTER_LINEAR,80FILTER_CUBIC,81};8283// Must be identical to MipmapMode enum definition in OpenXRCompositionLayer.84enum MipmapMode {85MIPMAP_MODE_DISABLED,86MIPMAP_MODE_NEAREST,87MIPMAP_MODE_LINEAR,88};8990// Must be identical to Wrap enum definition in OpenXRCompositionLayer.91enum Wrap {92WRAP_CLAMP_TO_BORDER,93WRAP_CLAMP_TO_EDGE,94WRAP_REPEAT,95WRAP_MIRRORED_REPEAT,96WRAP_MIRROR_CLAMP_TO_EDGE,97};9899// Must be identical to Swizzle enum definition in OpenXRCompositionLayer.100enum Swizzle {101SWIZZLE_RED,102SWIZZLE_GREEN,103SWIZZLE_BLUE,104SWIZZLE_ALPHA,105SWIZZLE_ZERO,106SWIZZLE_ONE,107};108109// Must be identical to EyeVisibility enum definition in OpenXRCompositionLayer.110enum EyeVisibility {111EYE_VISIBILITY_BOTH,112EYE_VISIBILITY_LEFT,113EYE_VISIBILITY_RIGHT,114};115116enum PoseSpace {117POSE_WORLD_LOCKED,118POSE_HEAD_LOCKED,119};120121struct SwapchainState {122Filter min_filter = Filter::FILTER_LINEAR;123Filter mag_filter = Filter::FILTER_LINEAR;124MipmapMode mipmap_mode = MipmapMode::MIPMAP_MODE_LINEAR;125Wrap horizontal_wrap = Wrap::WRAP_CLAMP_TO_BORDER;126Wrap vertical_wrap = Wrap::WRAP_CLAMP_TO_BORDER;127Swizzle red_swizzle = Swizzle::SWIZZLE_RED;128Swizzle green_swizzle = Swizzle::SWIZZLE_GREEN;129Swizzle blue_swizzle = Swizzle::SWIZZLE_BLUE;130Swizzle alpha_swizzle = Swizzle::SWIZZLE_ALPHA;131float max_anisotropy = 1.0;132Color border_color = { 0.0, 0.0, 0.0, 0.0 };133};134135static OpenXRCompositionLayerExtension *get_singleton();136137OpenXRCompositionLayerExtension();138virtual ~OpenXRCompositionLayerExtension() override;139140virtual HashMap<String, bool *> get_requested_extensions(XrVersion p_version) override;141virtual void on_instance_created(const XrInstance p_instance) override;142virtual void on_session_created(const XrSession p_session) override;143virtual void on_session_destroyed() override;144virtual void on_pre_render() override;145146virtual int get_composition_layer_count() override;147virtual XrCompositionLayerBaseHeader *get_composition_layer(int p_index) override;148virtual int get_composition_layer_order(int p_index) override;149150// The data on p_openxr_layer will be copied - there is no need to keep it valid after this call.151RID composition_layer_create(XrCompositionLayerBaseHeader *p_openxr_layer);152void composition_layer_free(RID p_layer);153154void composition_layer_register(RID p_layer);155void composition_layer_unregister(RID p_layer);156157OPENXR_LAYER_FUNC2(set_viewport, RID, const Size2i &);158OPENXR_LAYER_FUNC2(set_use_android_surface, bool, const Size2i &);159OPENXR_LAYER_FUNC1(set_sort_order, int);160OPENXR_LAYER_FUNC1(set_alpha_blend, bool);161OPENXR_LAYER_FUNC1(set_transform, const Transform3D &);162OPENXR_LAYER_FUNC1(set_protected_content, bool);163OPENXR_LAYER_FUNC1(set_extension_property_values, Dictionary);164165OPENXR_LAYER_FUNC1(set_min_filter, Filter);166OPENXR_LAYER_FUNC1(set_mag_filter, Filter);167OPENXR_LAYER_FUNC1(set_mipmap_mode, MipmapMode);168OPENXR_LAYER_FUNC1(set_horizontal_wrap, Wrap);169OPENXR_LAYER_FUNC1(set_vertical_wrap, Wrap);170OPENXR_LAYER_FUNC1(set_red_swizzle, Swizzle);171OPENXR_LAYER_FUNC1(set_blue_swizzle, Swizzle);172OPENXR_LAYER_FUNC1(set_green_swizzle, Swizzle);173OPENXR_LAYER_FUNC1(set_alpha_swizzle, Swizzle);174OPENXR_LAYER_FUNC1(set_max_anisotropy, float);175OPENXR_LAYER_FUNC1(set_border_color, const Color &);176OPENXR_LAYER_FUNC1(set_pose_space, PoseSpace);177OPENXR_LAYER_FUNC1(set_eye_visibility, EyeVisibility);178179OPENXR_LAYER_FUNC1(set_quad_size, const Size2 &);180181OPENXR_LAYER_FUNC1(set_cylinder_radius, float);182OPENXR_LAYER_FUNC1(set_cylinder_aspect_ratio, float);183OPENXR_LAYER_FUNC1(set_cylinder_central_angle, float);184185OPENXR_LAYER_FUNC1(set_equirect_radius, float);186OPENXR_LAYER_FUNC1(set_equirect_central_horizontal_angle, float);187OPENXR_LAYER_FUNC1(set_equirect_upper_vertical_angle, float);188OPENXR_LAYER_FUNC1(set_equirect_lower_vertical_angle, float);189190Ref<JavaObject> composition_layer_get_android_surface(RID p_layer);191192bool is_available(XrStructureType p_which);193bool is_android_surface_swapchain_available() { return android_surface_ext_available; }194195private:196static OpenXRCompositionLayerExtension *singleton;197198bool cylinder_ext_available = false;199bool equirect_ext_available = false;200bool android_surface_ext_available = false;201202void _composition_layer_free_rt(RID p_layer);203void _composition_layer_register_rt(RID p_layer);204void _composition_layer_unregister_rt(RID p_layer);205206#ifdef ANDROID_ENABLED207bool create_android_surface_swapchain(XrSwapchainCreateInfo *p_info, XrSwapchain *r_swapchain, jobject *r_surface);208209EXT_PROTO_XRRESULT_FUNC1(xrDestroySwapchain, (XrSwapchain), swapchain)210EXT_PROTO_XRRESULT_FUNC4(xrCreateSwapchainAndroidSurfaceKHR, (XrSession), session, (const XrSwapchainCreateInfo *), info, (XrSwapchain *), swapchain, (jobject *), surface)211#endif212213struct CompositionLayer {214union {215XrCompositionLayerBaseHeader composition_layer;216XrCompositionLayerQuad composition_layer_quad;217XrCompositionLayerCylinderKHR composition_layer_cylinder;218XrCompositionLayerEquirect2KHR composition_layer_equirect;219};220221int sort_order = 1;222bool alpha_blend = false;223Dictionary extension_property_values;224bool extension_property_values_changed = true;225226struct {227RID viewport;228Size2i viewport_size;229OpenXRAPI::OpenXRSwapChainInfo swapchain_info;230bool static_image = false;231bool swapchain_protected_content = false;232} subviewport;233234#ifdef ANDROID_ENABLED235struct {236XrSwapchain swapchain = XR_NULL_HANDLE;237Ref<JavaObject> surface;238} android_surface;239#endif240241PoseSpace pose_space = POSE_WORLD_LOCKED;242XrSpace layer_reference_space = XR_NULL_HANDLE;243244bool use_android_surface = false;245bool protected_content = false;246Size2i swapchain_size;247248SwapchainState swapchain_state;249bool swapchain_state_is_dirty = false;250251void set_viewport(RID p_viewport, const Size2i &p_size);252void set_use_android_surface(bool p_use_android_surface, const Size2i &p_size);253254void set_sort_order(int p_sort_order) { sort_order = p_sort_order; }255void set_alpha_blend(bool p_alpha_blend);256void set_protected_content(bool p_protected_content) { protected_content = p_protected_content; }257void set_transform(const Transform3D &p_transform);258void set_extension_property_values(const Dictionary &p_extension_property_values);259260void set_min_filter(Filter p_mode);261void set_mag_filter(Filter p_mode);262void set_mipmap_mode(MipmapMode p_mode);263void set_horizontal_wrap(Wrap p_mode);264void set_vertical_wrap(Wrap p_mode);265void set_red_swizzle(Swizzle p_mode);266void set_green_swizzle(Swizzle p_mode);267void set_blue_swizzle(Swizzle p_mode);268void set_alpha_swizzle(Swizzle p_mode);269void set_max_anisotropy(float p_value);270void set_border_color(const Color &p_color);271void set_pose_space(PoseSpace p_pose_space);272void set_eye_visibility(EyeVisibility p_eye_visibility);273274void set_quad_size(const Size2 &p_size);275276void set_cylinder_radius(float p_radius);277void set_cylinder_aspect_ratio(float p_aspect_ratio);278void set_cylinder_central_angle(float p_central_angle);279280void set_equirect_radius(float p_radius);281void set_equirect_central_horizontal_angle(float p_angle);282void set_equirect_upper_vertical_angle(float p_angle);283void set_equirect_lower_vertical_angle(float p_angle);284285Ref<JavaObject> get_android_surface();286void on_pre_render();287XrCompositionLayerBaseHeader *get_composition_layer();288void free();289290private:291void update_swapchain_state();292void update_swapchain_sub_image(XrSwapchainSubImage &r_subimage);293bool update_and_acquire_swapchain(bool p_static_image);294RID get_current_swapchain_texture();295void free_swapchain();296297#ifdef ANDROID_ENABLED298void create_android_surface();299#endif300};301302Mutex composition_layer_mutex;303RID_Owner<CompositionLayer, true> composition_layer_owner;304LocalVector<CompositionLayer *> registered_composition_layers;305};306307VARIANT_ENUM_CAST(OpenXRCompositionLayerExtension::Filter);308VARIANT_ENUM_CAST(OpenXRCompositionLayerExtension::MipmapMode);309VARIANT_ENUM_CAST(OpenXRCompositionLayerExtension::Wrap);310VARIANT_ENUM_CAST(OpenXRCompositionLayerExtension::Swizzle);311VARIANT_ENUM_CAST(OpenXRCompositionLayerExtension::PoseSpace);312VARIANT_ENUM_CAST(OpenXRCompositionLayerExtension::EyeVisibility);313314#undef OPENXR_LAYER_FUNC1315#undef OPENXR_LAYER_FUNC2316317318