diff --git a/client/application.cpp b/client/application.cpp index 9adbb0d4..77cc1014 100644 --- a/client/application.cpp +++ b/client/application.cpp @@ -24,6 +24,7 @@ #include "scene.h" #include "spdlog/common.h" #include "spdlog/spdlog.h" +#include "utils/class_from_member.h" #include "utils/contains.h" #include "utils/files.h" #include "utils/i18n.h" @@ -761,6 +762,7 @@ void application::initialize_vulkan() vk_device_extensions.push_back(VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME); vk_device_extensions.push_back(VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME); vk_device_extensions.push_back(VK_KHR_MULTIVIEW_EXTENSION_NAME); + optional_device_extensions.emplace(VK_EXT_INDEX_TYPE_UINT8_EXTENSION_NAME); optional_device_extensions.emplace(VK_IMG_FILTER_CUBIC_EXTENSION_NAME); optional_device_extensions.emplace(VK_KHR_TIMELINE_SEMAPHORE_EXTENSION_NAME); optional_device_extensions.emplace(VK_EXT_FRAGMENT_DENSITY_MAP_EXTENSION_NAME); @@ -884,24 +886,30 @@ void application::initialize_vulkan() vk::PhysicalDeviceSamplerYcbcrConversionFeaturesKHR{ .samplerYcbcrConversion = true, }, - vk::PhysicalDeviceTimelineSemaphoreFeaturesKHR{ - .timelineSemaphore = true, - }, + vk::PhysicalDeviceTimelineSemaphoreFeaturesKHR{}, vk::PhysicalDeviceMultiviewFeaturesKHR{ .multiview = true, }, + vk::PhysicalDeviceIndexTypeUint8FeaturesEXT{}}; + + auto check_feature_flag = [&](auto feature_flag, const char * extension_name) -> bool { + using FeatureStruct = class_from_member_t; + + if (utils::contains(vk_device_extensions, extension_name) and + vk_physical_device.getFeatures2().template get().*feature_flag) + { + device_create_info.get().*feature_flag = true; + return true; + } + else + { + device_create_info.unlink(); + return false; + } }; - if (utils::contains(vk_device_extensions, VK_KHR_TIMELINE_SEMAPHORE_EXTENSION_NAME)) - { - auto [_, feat] = vk_physical_device.getFeatures2(); - auto & create_feat = device_create_info.get(); - create_feat.timelineSemaphore = feat.timelineSemaphore; - } - else - { - device_create_info.unlink(); - } + check_feature_flag(&vk::PhysicalDeviceTimelineSemaphoreFeaturesKHR::timelineSemaphore, VK_KHR_TIMELINE_SEMAPHORE_EXTENSION_NAME); + check_feature_flag(&vk::PhysicalDeviceIndexTypeUint8FeaturesEXT::indexTypeUint8, VK_EXT_INDEX_TYPE_UINT8_EXTENSION_NAME); vk_device = xr_system_id.create_device(vk_physical_device, device_create_info.get()); *vk_queue.lock() = vk_device.getQueue(vk_queue_family_index, 0); diff --git a/client/render/gpu_buffer.h b/client/render/gpu_buffer.h index 892dc3fb..d1c06d9a 100644 --- a/client/render/gpu_buffer.h +++ b/client/render/gpu_buffer.h @@ -18,7 +18,9 @@ #pragma once +#include "application.h" #include "utils/alignment.h" +#include "utils/contains.h" #include "vk/allocation.h" #include #include @@ -29,6 +31,8 @@ class gpu_buffer { std::vector bytes; vk::PhysicalDeviceLimits limits; + vk::PhysicalDeviceIndexTypeUint8FeaturesEXT feat_uint8; + fastgltf::Asset & asset; vk::BufferUsageFlags usage; @@ -58,8 +62,15 @@ class gpu_buffer } public: - gpu_buffer(vk::PhysicalDeviceProperties properties, fastgltf::Asset & asset) : - limits(properties.limits), asset(asset), usage(0) {} + gpu_buffer(vk::raii::PhysicalDevice physical_device, fastgltf::Asset & asset) : + asset(asset), + usage(0) + { + limits = physical_device.getProperties().limits; + + if (utils::contains(application::instance().get_vk_device_extensions(), VK_EXT_INDEX_TYPE_UINT8_EXTENSION_NAME)) + std::tie(std::ignore, feat_uint8) = physical_device.getFeatures2(); + } template size_t add_uniform(const T & data_to_add) @@ -79,7 +90,7 @@ public: return add(alignment, data_to_add); } - size_t add_indices(const fastgltf::Accessor & accessor) + std::pair add_indices(const fastgltf::Accessor & accessor) { usage |= vk::BufferUsageFlagBits::eIndexBuffer; @@ -90,7 +101,11 @@ public: std::vector indices(accessor.count); using index_t = std::remove_cvref_t; fastgltf::copyFromAccessor(asset, accessor, indices.data()); - return add(4, indices); + + if (feat_uint8.indexTypeUint8) + return {add(4, indices), vk::IndexType::eUint8EXT}; + else + return {add(4, std::vector{indices.begin(), indices.end()}), vk::IndexType::eUint16}; } case fastgltf::ComponentType::Short: @@ -98,7 +113,7 @@ public: std::vector indices(accessor.count); using index_t = std::remove_cvref_t; fastgltf::copyFromAccessor(asset, accessor, indices.data()); - return add(4, indices); + return {add(4, indices), vk::IndexType::eUint16}; } case fastgltf::ComponentType::Int: @@ -106,7 +121,7 @@ public: std::vector indices(accessor.count); using index_t = std::remove_cvref_t; fastgltf::copyFromAccessor(asset, accessor, indices.data()); - return add(4, indices); + return {add(4, indices), vk::IndexType::eUint32}; } default: diff --git a/client/render/scene_loader.cpp b/client/render/scene_loader.cpp index 60b3d739..b22b7bfa 100644 --- a/client/render/scene_loader.cpp +++ b/client/render/scene_loader.cpp @@ -18,10 +18,12 @@ #include "scene_loader.h" +#include "application.h" #include "gpu_buffer.h" #include "image_loader.h" #include "render/scene_components.h" #include "render/vertex_layout.h" +#include "utils/contains.h" #include "utils/files.h" #include "utils/json_string.h" #include "utils/mapped_file.h" @@ -808,30 +810,8 @@ public: fastgltf::Accessor & indices_accessor = gltf.accessors.at(*gltf_primitive.indicesAccessor); primitive_ref.indexed = true; - primitive_ref.index_offset = staging_buffer.add_indices(indices_accessor); + std::tie(primitive_ref.index_offset, primitive_ref.index_type) = staging_buffer.add_indices(indices_accessor); primitive_ref.index_count = indices_accessor.count; - - switch (indices_accessor.componentType) - { - case fastgltf::ComponentType::Byte: - case fastgltf::ComponentType::UnsignedByte: - primitive_ref.index_type = vk::IndexType::eUint8EXT; - break; - - case fastgltf::ComponentType::Short: - case fastgltf::ComponentType::UnsignedShort: - primitive_ref.index_type = vk::IndexType::eUint16; - break; - - case fastgltf::ComponentType::Int: - case fastgltf::ComponentType::UnsignedInt: - primitive_ref.index_type = vk::IndexType::eUint32; - break; - - default: - throw std::runtime_error("Invalid index type"); - break; - } } else primitive_ref.indexed = false; @@ -1089,8 +1069,6 @@ std::shared_ptr scene_loader::operator()( const std::filesystem::path & gltf_texture_cache, std::function progress_cb) { - vk::PhysicalDeviceProperties physical_device_properties = physical_device.getProperties(); - auto data_buffer = fastgltf::GltfDataBuffer::FromBytes(data.data(), data.size()); if (auto error = data_buffer.error(); error != fastgltf::Error::None) throw std::system_error((int)error, fastgltf_error_category); @@ -1108,7 +1086,7 @@ std::shared_ptr scene_loader::operator()( // Load all buffers from URIs ctx.load_all_buffers(); - gpu_buffer staging_buffer(physical_device_properties, asset); + gpu_buffer staging_buffer(physical_device, asset); // Load all textures auto textures = ctx.load_all_textures(gltf_texture_cache, progress_cb); diff --git a/client/utils/class_from_member.h b/client/utils/class_from_member.h new file mode 100644 index 00000000..95f6321f --- /dev/null +++ b/client/utils/class_from_member.h @@ -0,0 +1,32 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2025 Guillaume Meunier + * + * 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 +template +struct class_from_member +{ +}; + +template +struct class_from_member +{ + using type = U; +}; + +template +using class_from_member_t = class_from_member::type;