/* * WiVRn VR streaming * Copyright (C) 2022 Guillaume Meunier * Copyright (C) 2022 Patrick Nicolas * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #pragma once #ifdef __ANDROID__ #include #endif #include "utils/typename.h" #include "xr/check.h" #include #include #include #include namespace xr::details { template struct structure_traits_base { static constexpr inline XrStructureType type = XR_TYPE_UNKNOWN; using base = T; }; template struct structure_traits : structure_traits_base {}; template <> struct structure_traits : structure_traits_base { static constexpr inline XrStructureType type = XR_TYPE_VIEW_CONFIGURATION_VIEW; }; template <> struct structure_traits : structure_traits_base { static constexpr inline XrStructureType type = XR_TYPE_VIEW; }; template <> struct structure_traits : structure_traits_base { static constexpr inline XrStructureType type = XR_TYPE_API_LAYER_PROPERTIES; }; template <> struct structure_traits : structure_traits_base { static constexpr inline XrStructureType type = XR_TYPE_EXTENSION_PROPERTIES; }; template <> struct structure_traits : structure_traits_base { static constexpr inline XrStructureType type = XR_TYPE_SWAPCHAIN_IMAGE_VULKAN_KHR; using base = XrSwapchainImageBaseHeader; }; template constexpr inline XrStructureType structure_type = structure_traits::type; template auto enumerate(F f, Args &&... args) -> auto { using array_type = std::conditional_t, std::string, std::vector>; array_type array; uint32_t count; XrResult result; result = f(std::forward(args)..., 0, &count, nullptr); if (XR_SUCCEEDED(result)) { if constexpr (std::is_same_v) { array.resize(count - 1); // count includes the null terminator } else if constexpr (structure_type == XR_TYPE_UNKNOWN) { array.resize(count); } else { T default_value{}; default_value.type = structure_type; array.resize(count, default_value); } result = f(std::forward(args)..., count, &count, reinterpret_cast::base *>(array.data())); } if (!XR_SUCCEEDED(result)) { throw std::system_error(result, xr::error_category(), "enumerating " + type_name()); } return array; } template auto enumerate2(F f, uint32_t count, Args &&... args) -> auto { assert(count); using array_type = std::conditional_t, std::string, std::vector>; T default_value{}; uint32_t size_offset = 0; if constexpr (std::is_same_v) { size_offset = 1; } else if constexpr (structure_type != XR_TYPE_UNKNOWN) { default_value.type = structure_type; } array_type array(count - size_offset, default_value); XrResult result = f(std::forward(args)..., count, &count, reinterpret_cast::base *>(array.data())); if (XR_SUCCEEDED(result)) { assert(count >= size_offset); array.resize(count - size_offset, default_value); return array; } if (result == XR_ERROR_SIZE_INSUFFICIENT) { assert(count >= size_offset); array.resize(count - size_offset, default_value); result = f(std::forward(args)..., count, &count, reinterpret_cast::base *>(array.data())); } if (!XR_SUCCEEDED(result)) { throw std::system_error(result, xr::error_category(), "enumerating " + type_name()); } return array; } } // namespace xr::details