Add proper support for 8 bit indices

This commit is contained in:
Guillaume Meunier
2025-12-02 19:57:11 +01:00
parent 91a41b306e
commit ab82fb5866
4 changed files with 78 additions and 45 deletions
+21 -13
View File
@@ -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<decltype(feature_flag)>;
if (utils::contains(vk_device_extensions, extension_name) and
vk_physical_device.getFeatures2<vk::PhysicalDeviceFeatures2, FeatureStruct>().template get<FeatureStruct>().*feature_flag)
{
device_create_info.get<FeatureStruct>().*feature_flag = true;
return true;
}
else
{
device_create_info.unlink<FeatureStruct>();
return false;
}
};
if (utils::contains(vk_device_extensions, VK_KHR_TIMELINE_SEMAPHORE_EXTENSION_NAME))
{
auto [_, feat] = vk_physical_device.getFeatures2<vk::PhysicalDeviceFeatures2, vk::PhysicalDeviceTimelineSemaphoreFeaturesKHR>();
auto & create_feat = device_create_info.get<vk::PhysicalDeviceTimelineSemaphoreFeaturesKHR>();
create_feat.timelineSemaphore = feat.timelineSemaphore;
}
else
{
device_create_info.unlink<vk::PhysicalDeviceTimelineSemaphoreFeaturesKHR>();
}
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);
+21 -6
View File
@@ -18,7 +18,9 @@
#pragma once
#include "application.h"
#include "utils/alignment.h"
#include "utils/contains.h"
#include "vk/allocation.h"
#include <fastgltf/tools.hpp>
#include <fastgltf/types.hpp>
@@ -29,6 +31,8 @@ class gpu_buffer
{
std::vector<std::byte> 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<vk::PhysicalDeviceFeatures2, vk::PhysicalDeviceIndexTypeUint8FeaturesEXT>();
}
template <typename T>
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<size_t, vk::IndexType> add_indices(const fastgltf::Accessor & accessor)
{
usage |= vk::BufferUsageFlagBits::eIndexBuffer;
@@ -90,7 +101,11 @@ public:
std::vector<uint8_t> indices(accessor.count);
using index_t = std::remove_cvref_t<decltype(*indices.data())>;
fastgltf::copyFromAccessor<index_t>(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<uint16_t>{indices.begin(), indices.end()}), vk::IndexType::eUint16};
}
case fastgltf::ComponentType::Short:
@@ -98,7 +113,7 @@ public:
std::vector<uint16_t> indices(accessor.count);
using index_t = std::remove_cvref_t<decltype(*indices.data())>;
fastgltf::copyFromAccessor<index_t>(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<uint32_t> indices(accessor.count);
using index_t = std::remove_cvref_t<decltype(*indices.data())>;
fastgltf::copyFromAccessor<index_t>(asset, accessor, indices.data());
return add(4, indices);
return {add(4, indices), vk::IndexType::eUint32};
}
default:
+4 -26
View File
@@ -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<entt::registry> scene_loader::operator()(
const std::filesystem::path & gltf_texture_cache,
std::function<void(float)> 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<entt::registry> 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);
+32
View File
@@ -0,0 +1,32 @@
/*
* WiVRn VR streaming
* Copyright (C) 2025 Guillaume Meunier <guillaume.meunier@centraliens.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#pragma once
template <typename T>
struct class_from_member
{
};
template <typename T, typename U>
struct class_from_member<T U::*>
{
using type = U;
};
template <typename pointer>
using class_from_member_t = class_from_member<pointer>::type;