/* * 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 #include #include namespace xr::details { template struct base_struct { using type = T; }; template using base_struct_t = base_struct::type; template <> struct base_struct { using type = XrSwapchainImageBaseHeader; }; template struct structure_traits { static constexpr inline XrStructureType type = XR_TYPE_UNKNOWN; }; #define TRAIT(S, T) \ template <> \ struct structure_traits \ { \ static constexpr inline XrStructureType type = T; \ }; XR_LIST_STRUCTURE_TYPES(TRAIT) #undef TRAIT template constexpr inline XrStructureType structure_type = structure_traits::type; template void enumerate(F f, T & data, Args &&... args) { using V = T::value_type; uint32_t count; uint32_t capacity = data.size(); // For strings, add the null byte as capacity if (std::is_same_v and capacity) capacity++; XrResult result = f(std::forward(args)..., capacity, &count, reinterpret_cast *>(data.data())); if (result == XR_ERROR_SIZE_INSUFFICIENT or (data.empty() and XR_SUCCEEDED(result) and count > 0)) { if constexpr (structure_type != XR_TYPE_UNKNOWN) data.resize(count, V{.type = structure_type}); else if constexpr (std::is_same_v) data.resize(count - 1); // count includes the null terminator else data.resize(count); return enumerate(f, data, std::forward(args)...); } if (not XR_SUCCEEDED(result)) throw std::system_error(result, xr::error_category(), "enumerating " + type_name()); data.resize(count - std::is_same_v); } template auto enumerate(F f, Args &&... args) -> auto { using array_type = std::conditional_t, std::string, std::vector>; array_type array; enumerate(f, array, std::forward(args)...); return array; } template void resize_and_link(std::tuple...> & tup, size_t size) { auto & vec = std::get(tup); vec.resize(size); for (size_t j = 0; j < size; ++j) { auto & item = vec[j]; item.type = structure_type>>; if constexpr (i < sizeof...(T) - 1) { item.next = &std::get(tup)[j]; } } if constexpr (i > 0) { resize_and_link(tup, size); } } template void resize(std::tuple...> & tup, size_t size) { if constexpr (i < sizeof...(T)) { auto & vec = std::get(tup); vec.resize(size); resize(tup, size); } } template void enumerate2(F f, std::tuple...> & data, Args &&... args) { using T0 = std::tuple_element_t<0, std::tuple>; uint32_t count; auto & data0 = std::get<0>(data); uint32_t capacity = data0.size(); XrResult result = f(std::forward(args)..., capacity, &count, reinterpret_cast *>(data0.data())); if (result == XR_ERROR_SIZE_INSUFFICIENT or (capacity == 0 and XR_SUCCEEDED(result) and count > 0)) { resize_and_link(data, count); return enumerate2(f, data, std::forward(args)...); } if (not XR_SUCCEEDED(result)) throw std::system_error(result, xr::error_category(), "enumerating " + type_name()); resize(data, count); } template auto enumerate2(F f, Args &&... args) -> auto { using array_type = std::tuple...>; array_type array; enumerate2(f, array, std::forward(args)...); return array; } } // namespace xr::details