Files
EasonZxp 6a72c537c2 Add FB hand mesh support with GLB fallback (#934)
Use the XR_FB_HAND_TRACKING_MESH extension to render hand models when supported,
and fall back to the bundled GLB hand models otherwise. This fixes incorrect hand
model rendering on PFDM MR device, Have tested with PFDM MR V4.5.0 version.
2026-05-26 09:40:37 +02:00

162 lines
4.8 KiB
C++

/*
* WiVRn VR streaming
* Copyright (C) 2024 Guillaume Meunier <[email protected]>
*
* 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
#include "application.h"
#include "utils/alignment.h"
#include "utils/contains.h"
#include "vk/allocation.h"
#include <fastgltf/tools.hpp>
#include <fastgltf/types.hpp>
#include <span>
#include <vector>
#include <vulkan/vulkan.hpp>
class gpu_buffer
{
std::vector<std::byte> bytes;
vk::PhysicalDeviceLimits limits;
vk::PhysicalDeviceIndexTypeUint8FeaturesEXT feat_uint8;
fastgltf::Asset & asset;
vk::BufferUsageFlags usage;
size_t add(size_t alignment, const void * data_to_add, size_t size)
{
size_t offset = utils::align_up(alignment, bytes.size());
bytes.resize(offset + size);
memcpy(&bytes[offset], data_to_add, size);
return offset;
}
template <typename T>
requires requires(T t) {
{
t.data()
} -> std::convertible_to<const void *>;
{
t.size()
} -> std::convertible_to<size_t>;
}
size_t add(size_t alignment, const T & data_to_add)
{
return add(alignment, data_to_add.data(), data_to_add.size() * sizeof(data_to_add.data()[0]));
}
public:
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)
{
usage |= vk::BufferUsageFlagBits::eUniformBuffer;
size_t alignment = std::max(limits.minUniformBufferOffsetAlignment, alignof(T));
return add(alignment, &data_to_add, sizeof(data_to_add));
}
template <typename T>
size_t add_vertices(const T & data_to_add)
{
usage |= vk::BufferUsageFlagBits::eVertexBuffer;
size_t alignment = std::max<size_t>(4, alignof(T));
return add(alignment, data_to_add);
}
size_t add_indices(std::span<const uint16_t> indices)
{
usage |= vk::BufferUsageFlagBits::eIndexBuffer;
return add(4, indices.data(), indices.size_bytes());
}
std::pair<size_t, vk::IndexType> add_indices(const fastgltf::Accessor & accessor)
{
usage |= vk::BufferUsageFlagBits::eIndexBuffer;
switch (accessor.componentType)
{
case fastgltf::ComponentType::Byte:
case fastgltf::ComponentType::UnsignedByte: {
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());
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:
case fastgltf::ComponentType::UnsignedShort: {
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), vk::IndexType::eUint16};
}
case fastgltf::ComponentType::Int:
case fastgltf::ComponentType::UnsignedInt: {
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), vk::IndexType::eUint32};
}
default:
throw std::runtime_error("Invalid index type");
}
}
buffer_allocation copy_to_gpu(vk::raii::Device & device)
{
buffer_allocation gpu_buffer{
device,
vk::BufferCreateInfo{
.size = bytes.size(),
.usage = usage},
VmaAllocationCreateInfo{
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
.usage = VMA_MEMORY_USAGE_AUTO},
"gpu_buffer::copy_to_gpu"};
memcpy(gpu_buffer.map(), bytes.data(), bytes.size());
gpu_buffer.unmap();
return gpu_buffer;
}
size_t size() const
{
return bytes.size();
}
};