Path: blob/master/modules/openxr/extensions/openxr_fb_update_swapchain_extension.cpp
10278 views
/**************************************************************************/1/* openxr_fb_update_swapchain_extension.cpp */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#include "openxr_fb_update_swapchain_extension.h"3132// Always include this as late as possible.33#include "../openxr_platform_inc.h"3435#ifndef GL_CUBIC_IMG36#define GL_CUBIC_IMG 0x913937#endif38#ifndef GL_CUBIC_MIPMAP_LINEAR_IMG39#define GL_CUBIC_MIPMAP_LINEAR_IMG 0x913B40#endif41#ifndef GL_CUBIC_MIPMAP_NEAREST_IMG42#define GL_CUBIC_MIPMAP_NEAREST_IMG 0x913A43#endif44#ifndef GL_CLAMP_TO_BORDER45#define GL_CLAMP_TO_BORDER 0x812D46#endif4748OpenXRFBUpdateSwapchainExtension *OpenXRFBUpdateSwapchainExtension::singleton = nullptr;4950OpenXRFBUpdateSwapchainExtension *OpenXRFBUpdateSwapchainExtension::get_singleton() {51return singleton;52}5354OpenXRFBUpdateSwapchainExtension::OpenXRFBUpdateSwapchainExtension(const String &p_rendering_driver) {55singleton = this;56rendering_driver = p_rendering_driver;57}5859OpenXRFBUpdateSwapchainExtension::~OpenXRFBUpdateSwapchainExtension() {60singleton = nullptr;61}6263HashMap<String, bool *> OpenXRFBUpdateSwapchainExtension::get_requested_extensions() {64HashMap<String, bool *> request_extensions;6566request_extensions[XR_FB_SWAPCHAIN_UPDATE_STATE_EXTENSION_NAME] = &fb_swapchain_update_state_ext;6768if (rendering_driver == "vulkan") {69#ifdef XR_USE_GRAPHICS_API_VULKAN70request_extensions[XR_FB_SWAPCHAIN_UPDATE_STATE_VULKAN_EXTENSION_NAME] = &fb_swapchain_update_state_vulkan_ext;71#endif72} else if (rendering_driver == "opengl3") {73#ifdef XR_USE_GRAPHICS_API_OPENGL_ES74request_extensions[XR_FB_SWAPCHAIN_UPDATE_STATE_OPENGL_ES_EXTENSION_NAME] = &fb_swapchain_update_state_opengles_ext;75#endif76}7778#ifdef ANDROID_ENABLED79request_extensions[XR_FB_SWAPCHAIN_UPDATE_STATE_ANDROID_SURFACE_EXTENSION_NAME] = &fb_swapchain_update_state_android_ext;80#endif8182return request_extensions;83}8485void OpenXRFBUpdateSwapchainExtension::on_instance_created(const XrInstance p_instance) {86if (fb_swapchain_update_state_ext) {87EXT_INIT_XR_FUNC(xrUpdateSwapchainFB);88EXT_INIT_XR_FUNC(xrGetSwapchainStateFB);89}9091if (fb_swapchain_update_state_vulkan_ext) {92// nothing to register here...93}9495if (fb_swapchain_update_state_opengles_ext) {96// nothing to register here...97}98}99100void OpenXRFBUpdateSwapchainExtension::on_instance_destroyed() {101fb_swapchain_update_state_ext = false;102fb_swapchain_update_state_vulkan_ext = false;103fb_swapchain_update_state_opengles_ext = false;104}105106bool OpenXRFBUpdateSwapchainExtension::is_enabled() const {107if (rendering_driver == "vulkan") {108return fb_swapchain_update_state_ext && fb_swapchain_update_state_vulkan_ext;109} else if (rendering_driver == "opengl3") {110#ifdef XR_USE_GRAPHICS_API_OPENGL_ES111return fb_swapchain_update_state_ext && fb_swapchain_update_state_opengles_ext;112#else113return fb_swapchain_update_state_ext;114#endif115}116117return false;118}119120bool OpenXRFBUpdateSwapchainExtension::is_android_ext_enabled() const {121return fb_swapchain_update_state_android_ext;122}123124void OpenXRFBUpdateSwapchainExtension::update_swapchain_state(XrSwapchain p_swapchain, const OpenXRViewportCompositionLayerProvider::SwapchainState *p_swapchain_state) {125if (!p_swapchain_state) {126return;127}128129if (rendering_driver == "vulkan") {130#ifdef XR_USE_GRAPHICS_API_VULKAN131if (!fb_swapchain_update_state_ext || !fb_swapchain_update_state_vulkan_ext) {132return;133}134135Color border_color = p_swapchain_state->border_color;136XrSwapchainStateSamplerVulkanFB swapchain_state = {137XR_TYPE_SWAPCHAIN_STATE_SAMPLER_VULKAN_FB, // type138nullptr, // next139(VkFilter)filter_to_vk(p_swapchain_state->min_filter), // minFilter140(VkFilter)filter_to_vk(p_swapchain_state->mag_filter), // magFilter141(VkSamplerMipmapMode)mipmap_mode_to_vk(p_swapchain_state->mipmap_mode), // mipmapMode142(VkSamplerAddressMode)wrap_to_vk(p_swapchain_state->horizontal_wrap), // wrapModeS;143(VkSamplerAddressMode)wrap_to_vk(p_swapchain_state->vertical_wrap), // wrapModeT144(VkComponentSwizzle)swizzle_to_vk(p_swapchain_state->red_swizzle), // swizzleRed145(VkComponentSwizzle)swizzle_to_vk(p_swapchain_state->green_swizzle), // swizzleGreen146(VkComponentSwizzle)swizzle_to_vk(p_swapchain_state->blue_swizzle), // swizzleBlue147(VkComponentSwizzle)swizzle_to_vk(p_swapchain_state->alpha_swizzle), // swizzleAlpha148p_swapchain_state->max_anisotropy, // maxAnisotropy149{ border_color.r, border_color.g, border_color.b, border_color.a } // borderColor150};151152XrResult result = xrUpdateSwapchainFB(p_swapchain, (XrSwapchainStateBaseHeaderFB *)&swapchain_state);153if (XR_FAILED(result)) {154print_error(vformat("OpenXR: Failed to update swapchain [%s]", OpenXRAPI::get_singleton()->get_error_string(result)));155return;156}157#endif158} else if (rendering_driver == "opengl3") {159#ifdef XR_USE_GRAPHICS_API_OPENGL_ES160if (!fb_swapchain_update_state_ext || !fb_swapchain_update_state_opengles_ext) {161return;162}163164Color border_color = p_swapchain_state->border_color;165XrSwapchainStateSamplerOpenGLESFB swapchain_state = {166XR_TYPE_SWAPCHAIN_STATE_SAMPLER_OPENGL_ES_FB, // type167nullptr, // next168filter_to_gl(p_swapchain_state->min_filter, p_swapchain_state->mipmap_mode), // minFilter169filter_to_gl(p_swapchain_state->mag_filter), // magFilter170wrap_to_gl(p_swapchain_state->horizontal_wrap), // wrapModeS;171wrap_to_gl(p_swapchain_state->vertical_wrap), // wrapModeT172swizzle_to_gl(p_swapchain_state->red_swizzle), // swizzleRed173swizzle_to_gl(p_swapchain_state->green_swizzle), // swizzleGreen174swizzle_to_gl(p_swapchain_state->blue_swizzle), // swizzleBlue175swizzle_to_gl(p_swapchain_state->alpha_swizzle), // swizzleAlpha176p_swapchain_state->max_anisotropy, // maxAnisotropy177{ border_color.r, border_color.g, border_color.b, border_color.a } // borderColor178};179180XrResult result = xrUpdateSwapchainFB(p_swapchain, (XrSwapchainStateBaseHeaderFB *)&swapchain_state);181if (XR_FAILED(result)) {182print_error(vformat("OpenXR: Failed to update swapchain [%s]", OpenXRAPI::get_singleton()->get_error_string(result)));183return;184}185#endif186}187}188189void OpenXRFBUpdateSwapchainExtension::update_swapchain_surface_size(XrSwapchain p_swapchain, const Size2i &p_size) {190#ifdef ANDROID_ENABLED191if (!fb_swapchain_update_state_ext || !fb_swapchain_update_state_android_ext) {192return;193}194195XrSwapchainStateAndroidSurfaceDimensionsFB swapchain_state = {196XR_TYPE_SWAPCHAIN_STATE_ANDROID_SURFACE_DIMENSIONS_FB, // type197nullptr, // next198(uint32_t)p_size.width, // width199(uint32_t)p_size.height // height200};201202XrResult result = xrUpdateSwapchainFB(p_swapchain, (XrSwapchainStateBaseHeaderFB *)&swapchain_state);203if (XR_FAILED(result)) {204print_error(vformat("OpenXR: Failed to update swapchain surface size [%s]", OpenXRAPI::get_singleton()->get_error_string(result)));205}206#endif207}208209uint32_t OpenXRFBUpdateSwapchainExtension::filter_to_gl(OpenXRViewportCompositionLayerProvider::Filter p_filter, OpenXRViewportCompositionLayerProvider::MipmapMode p_mipmap_mode) {210#ifdef XR_USE_GRAPHICS_API_OPENGL_ES211switch (p_mipmap_mode) {212case OpenXRViewportCompositionLayerProvider::MipmapMode::MIPMAP_MODE_DISABLED:213switch (p_filter) {214case OpenXRViewportCompositionLayerProvider::Filter::FILTER_NEAREST:215return GL_NEAREST;216case OpenXRViewportCompositionLayerProvider::Filter::FILTER_LINEAR:217return GL_LINEAR;218case OpenXRViewportCompositionLayerProvider::Filter::FILTER_CUBIC:219return GL_CUBIC_IMG;220}221case OpenXRViewportCompositionLayerProvider::MipmapMode::MIPMAP_MODE_NEAREST:222switch (p_filter) {223case OpenXRViewportCompositionLayerProvider::Filter::FILTER_NEAREST:224return GL_NEAREST_MIPMAP_NEAREST;225case OpenXRViewportCompositionLayerProvider::Filter::FILTER_LINEAR:226return GL_LINEAR_MIPMAP_NEAREST;227case OpenXRViewportCompositionLayerProvider::Filter::FILTER_CUBIC:228return GL_CUBIC_MIPMAP_NEAREST_IMG;229}230case OpenXRViewportCompositionLayerProvider::MipmapMode::MIPMAP_MODE_LINEAR:231switch (p_filter) {232case OpenXRViewportCompositionLayerProvider::Filter::FILTER_NEAREST:233return GL_NEAREST_MIPMAP_LINEAR;234case OpenXRViewportCompositionLayerProvider::Filter::FILTER_LINEAR:235return GL_LINEAR_MIPMAP_LINEAR;236case OpenXRViewportCompositionLayerProvider::Filter::FILTER_CUBIC:237return GL_CUBIC_MIPMAP_LINEAR_IMG;238}239}240#endif241return 0;242}243244uint32_t OpenXRFBUpdateSwapchainExtension::wrap_to_gl(OpenXRViewportCompositionLayerProvider::Wrap p_wrap) {245#ifdef XR_USE_GRAPHICS_API_OPENGL_ES246switch (p_wrap) {247case OpenXRViewportCompositionLayerProvider::Wrap::WRAP_CLAMP_TO_BORDER:248return GL_CLAMP_TO_BORDER;249case OpenXRViewportCompositionLayerProvider::Wrap::WRAP_CLAMP_TO_EDGE:250return GL_CLAMP_TO_EDGE;251case OpenXRViewportCompositionLayerProvider::Wrap::WRAP_REPEAT:252return GL_REPEAT;253case OpenXRViewportCompositionLayerProvider::Wrap::WRAP_MIRRORED_REPEAT:254return GL_MIRRORED_REPEAT;255case OpenXRViewportCompositionLayerProvider::Wrap::WRAP_MIRROR_CLAMP_TO_EDGE:256return GL_CLAMP_TO_EDGE;257}258#endif259return 0;260}261262uint32_t OpenXRFBUpdateSwapchainExtension::swizzle_to_gl(OpenXRViewportCompositionLayerProvider::Swizzle p_swizzle) {263#ifdef XR_USE_GRAPHICS_API_OPENGL_ES264switch (p_swizzle) {265case OpenXRViewportCompositionLayerProvider::Swizzle::SWIZZLE_RED:266return GL_RED;267case OpenXRViewportCompositionLayerProvider::Swizzle::SWIZZLE_GREEN:268return GL_GREEN;269case OpenXRViewportCompositionLayerProvider::Swizzle::SWIZZLE_BLUE:270return GL_BLUE;271case OpenXRViewportCompositionLayerProvider::Swizzle::SWIZZLE_ALPHA:272return GL_ALPHA;273case OpenXRViewportCompositionLayerProvider::Swizzle::SWIZZLE_ZERO:274return GL_ZERO;275case OpenXRViewportCompositionLayerProvider::Swizzle::SWIZZLE_ONE:276return GL_ONE;277}278#endif279return 0;280}281282uint32_t OpenXRFBUpdateSwapchainExtension::filter_to_vk(OpenXRViewportCompositionLayerProvider::Filter p_filter) {283#ifdef XR_USE_GRAPHICS_API_VULKAN284switch (p_filter) {285case OpenXRViewportCompositionLayerProvider::Filter::FILTER_NEAREST:286return VK_FILTER_NEAREST;287case OpenXRViewportCompositionLayerProvider::Filter::FILTER_LINEAR:288return VK_FILTER_LINEAR;289case OpenXRViewportCompositionLayerProvider::Filter::FILTER_CUBIC:290return VK_FILTER_CUBIC_EXT;291}292#endif293return 0;294}295296uint32_t OpenXRFBUpdateSwapchainExtension::mipmap_mode_to_vk(OpenXRViewportCompositionLayerProvider::MipmapMode p_mipmap_mode) {297#ifdef XR_USE_GRAPHICS_API_VULKAN298switch (p_mipmap_mode) {299case OpenXRViewportCompositionLayerProvider::MipmapMode::MIPMAP_MODE_DISABLED:300return VK_SAMPLER_MIPMAP_MODE_LINEAR;301case OpenXRViewportCompositionLayerProvider::MipmapMode::MIPMAP_MODE_NEAREST:302return VK_SAMPLER_MIPMAP_MODE_NEAREST;303case OpenXRViewportCompositionLayerProvider::MipmapMode::MIPMAP_MODE_LINEAR:304return VK_SAMPLER_MIPMAP_MODE_LINEAR;305}306#endif307return 0;308}309310uint32_t OpenXRFBUpdateSwapchainExtension::wrap_to_vk(OpenXRViewportCompositionLayerProvider::Wrap p_wrap) {311#ifdef XR_USE_GRAPHICS_API_VULKAN312switch (p_wrap) {313case OpenXRViewportCompositionLayerProvider::Wrap::WRAP_CLAMP_TO_BORDER:314return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;315case OpenXRViewportCompositionLayerProvider::Wrap::WRAP_CLAMP_TO_EDGE:316return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;317case OpenXRViewportCompositionLayerProvider::Wrap::WRAP_REPEAT:318return VK_SAMPLER_ADDRESS_MODE_REPEAT;319case OpenXRViewportCompositionLayerProvider::Wrap::WRAP_MIRRORED_REPEAT:320return VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT;321case OpenXRViewportCompositionLayerProvider::Wrap::WRAP_MIRROR_CLAMP_TO_EDGE:322return VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE;323}324#endif325return 0;326}327328uint32_t OpenXRFBUpdateSwapchainExtension::swizzle_to_vk(OpenXRViewportCompositionLayerProvider::Swizzle p_swizzle) {329#ifdef XR_USE_GRAPHICS_API_VULKAN330switch (p_swizzle) {331case OpenXRViewportCompositionLayerProvider::Swizzle::SWIZZLE_RED:332return VK_COMPONENT_SWIZZLE_R;333case OpenXRViewportCompositionLayerProvider::Swizzle::SWIZZLE_GREEN:334return VK_COMPONENT_SWIZZLE_G;335case OpenXRViewportCompositionLayerProvider::Swizzle::SWIZZLE_BLUE:336return VK_COMPONENT_SWIZZLE_B;337case OpenXRViewportCompositionLayerProvider::Swizzle::SWIZZLE_ALPHA:338return VK_COMPONENT_SWIZZLE_A;339case OpenXRViewportCompositionLayerProvider::Swizzle::SWIZZLE_ZERO:340return VK_COMPONENT_SWIZZLE_ZERO;341case OpenXRViewportCompositionLayerProvider::Swizzle::SWIZZLE_ONE:342return VK_COMPONENT_SWIZZLE_ONE;343}344#endif345return 0;346}347348349