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.
This commit is contained in:
EasonZxp
2026-05-26 09:40:37 +02:00
committed by GitHub
parent 18d685976e
commit 6a72c537c2
11 changed files with 451 additions and 4 deletions
+120
View File
@@ -21,7 +21,10 @@
#include "xr/check.h"
#include "xr/instance.h"
#include "xr/session.h"
#include "xr/to_string.h"
#include <algorithm>
#include <cassert>
#include <spdlog/spdlog.h>
xr::hand_tracker::hand_tracker(instance & inst, session & session, const XrHandTrackerCreateInfoEXT & info) :
handle(inst.get_proc<PFN_xrDestroyHandTrackerEXT>("xrDestroyHandTrackerEXT"))
@@ -29,6 +32,8 @@ xr::hand_tracker::hand_tracker(instance & inst, session & session, const XrHandT
auto xrCreateHandTrackerEXT = inst.get_proc<PFN_xrCreateHandTrackerEXT>("xrCreateHandTrackerEXT");
assert(xrCreateHandTrackerEXT);
xrLocateHandJointsEXT = inst.get_proc<PFN_xrLocateHandJointsEXT>("xrLocateHandJointsEXT");
if (inst.has_extension(XR_FB_HAND_TRACKING_MESH_EXTENSION_NAME))
xrGetHandMeshFB = inst.get_proc<PFN_xrGetHandMeshFB>("xrGetHandMeshFB");
CHECK_XR(xrCreateHandTrackerEXT(session, &info, &id));
}
@@ -87,6 +92,121 @@ std::optional<std::array<xr::hand_tracker::joint, XR_HAND_JOINT_COUNT_EXT>> xr::
return joints;
}
const xr::hand_tracker::mesh_data * xr::hand_tracker::mesh()
{
if (hand_mesh_fb_fetched)
return cached_hand_mesh_fb ? &*cached_hand_mesh_fb : nullptr;
hand_mesh_fb_fetched = true;
if (!id || !xrGetHandMeshFB)
return nullptr;
XrHandTrackingMeshFB mesh{
.type = XR_TYPE_HAND_TRACKING_MESH_FB,
};
XrResult result = xrGetHandMeshFB(id, &mesh);
if (XR_FAILED(result))
{
spdlog::warn("xrGetHandMeshFB count query failed: {}", xr::to_string(result));
return nullptr;
}
if (mesh.jointCountOutput != XR_HAND_JOINT_COUNT_EXT)
{
spdlog::warn("Unsupported hand mesh joint count {}", mesh.jointCountOutput);
return nullptr;
}
if (mesh.jointCountOutput > 32)
{
spdlog::warn("Hand mesh joint count {} exceeds renderer limit", mesh.jointCountOutput);
return nullptr;
}
if (mesh.vertexCountOutput == 0 || mesh.indexCountOutput == 0)
{
spdlog::warn("Hand mesh data is empty");
return nullptr;
}
mesh_data data;
data.joint_bind_poses.resize(mesh.jointCountOutput);
data.joint_radii.resize(mesh.jointCountOutput);
data.joint_parents.resize(mesh.jointCountOutput);
data.vertex_positions.resize(mesh.vertexCountOutput);
data.vertex_normals.resize(mesh.vertexCountOutput);
data.vertex_uvs.resize(mesh.vertexCountOutput);
data.vertex_blend_indices.resize(mesh.vertexCountOutput);
data.vertex_blend_weights.resize(mesh.vertexCountOutput);
data.indices.resize(mesh.indexCountOutput);
mesh.jointCapacityInput = data.joint_bind_poses.size();
mesh.jointBindPoses = data.joint_bind_poses.data();
mesh.jointRadii = data.joint_radii.data();
mesh.jointParents = data.joint_parents.data();
mesh.vertexCapacityInput = data.vertex_positions.size();
mesh.vertexPositions = data.vertex_positions.data();
mesh.vertexNormals = data.vertex_normals.data();
mesh.vertexUVs = data.vertex_uvs.data();
mesh.vertexBlendIndices = data.vertex_blend_indices.data();
mesh.vertexBlendWeights = data.vertex_blend_weights.data();
mesh.indexCapacityInput = data.indices.size();
mesh.indices = data.indices.data();
result = xrGetHandMeshFB(id, &mesh);
if (XR_FAILED(result))
{
spdlog::warn("xrGetHandMeshFB data query failed: {}", xr::to_string(result));
return nullptr;
}
auto count_changed = [](const char * what, uint32_t output, size_t expected) {
if (output == expected)
return false;
spdlog::warn("Hand mesh {} count changed from {} to {}", what, expected, output);
return true;
};
if (count_changed("joint", mesh.jointCountOutput, data.joint_bind_poses.size()) ||
count_changed("vertex", mesh.vertexCountOutput, data.vertex_positions.size()) ||
count_changed("index", mesh.indexCountOutput, data.indices.size()))
{
return nullptr;
}
if (mesh.indexCountOutput % 3 != 0)
{
spdlog::warn("Hand mesh index count {} is not a triangle list", mesh.indexCountOutput);
return nullptr;
}
const int joint_count = static_cast<int>(mesh.jointCountOutput);
if (!std::ranges::all_of(data.vertex_blend_indices, [joint_count](const XrVector4sFB & blend_indices) {
return 0 <= blend_indices.x && blend_indices.x < joint_count &&
0 <= blend_indices.y && blend_indices.y < joint_count &&
0 <= blend_indices.z && blend_indices.z < joint_count &&
0 <= blend_indices.w && blend_indices.w < joint_count;
}))
{
spdlog::warn("Hand mesh has out-of-range blend indices");
return nullptr;
}
const int vertex_count = static_cast<int>(mesh.vertexCountOutput);
if (!std::ranges::all_of(data.indices, [vertex_count](int16_t index) {
return 0 <= index && index < vertex_count;
}))
{
spdlog::warn("Hand mesh has out-of-range indices");
return nullptr;
}
cached_hand_mesh_fb = std::move(data);
return &*cached_hand_mesh_fb;
}
bool xr::hand_tracker::check_flags(const std::array<joint, XR_HAND_JOINT_COUNT_EXT> & joints, XrSpaceLocationFlags position, XrSpaceVelocityFlags velocity)
{
return std::ranges::all_of(joints, [position, velocity](const auto & joint) {
+22 -2
View File
@@ -22,6 +22,7 @@
#include "utils/handle.h"
#include <array>
#include <optional>
#include <vector>
#include <openxr/openxr.h>
namespace xr
@@ -31,14 +32,33 @@ class session;
class hand_tracker : public utils::handle<XrHandTrackerEXT>
{
public:
using joint = std::pair<XrHandJointLocationEXT, XrHandJointVelocityEXT>;
struct mesh_data
{
std::vector<XrPosef> joint_bind_poses;
std::vector<float> joint_radii;
std::vector<XrHandJointEXT> joint_parents;
std::vector<XrVector3f> vertex_positions;
std::vector<XrVector3f> vertex_normals;
std::vector<XrVector2f> vertex_uvs;
std::vector<XrVector4sFB> vertex_blend_indices;
std::vector<XrVector4f> vertex_blend_weights;
std::vector<int16_t> indices;
};
private:
PFN_xrLocateHandJointsEXT xrLocateHandJointsEXT{};
PFN_xrGetHandMeshFB xrGetHandMeshFB{};
std::optional<mesh_data> cached_hand_mesh_fb;
bool hand_mesh_fb_fetched = false;
public:
hand_tracker(instance & inst, session & session, const XrHandTrackerCreateInfoEXT & info);
using joint = std::pair<XrHandJointLocationEXT, XrHandJointVelocityEXT>;
std::optional<std::array<joint, XR_HAND_JOINT_COUNT_EXT>> locate(XrSpace space, XrTime time);
const mesh_data * mesh();
static bool check_flags(const std::array<joint, XR_HAND_JOINT_COUNT_EXT> & joints, XrSpaceLocationFlags position, XrSpaceVelocityFlags velocity);
};
+8
View File
@@ -47,6 +47,9 @@ xr::system::system(xr::instance & inst, XrFormFactor formfactor)
if (inst.has_extension(XR_EXT_HAND_TRACKING_EXTENSION_NAME))
hand_tracking_supported_ = hand_tracking_properties().supportsHandTracking;
if (hand_tracking_supported_ and inst.has_extension(XR_FB_HAND_TRACKING_MESH_EXTENSION_NAME))
hand_mesh_fb_supported_ = true;
body_tracker = xr::body_tracker_supported(inst, *this);
face_tracker = xr::face_tracker_supported(inst, *this);
}
@@ -275,6 +278,11 @@ bool xr::system::hand_tracking_supported() const
return hand_tracking_supported_;
}
bool xr::system::hand_mesh_fb_supported() const
{
return hand_mesh_fb_supported_;
}
vk::raii::PhysicalDevice xr::system::physical_device(vk::raii::Instance & vulkan) const
{
auto xrGetVulkanGraphicsDevice2KHR =
+2
View File
@@ -61,6 +61,7 @@ class system
instance * inst = nullptr;
XrSystemId id = XR_NULL_SYSTEM_ID;
bool hand_tracking_supported_ = false;
bool hand_mesh_fb_supported_ = false;
passthrough_type passthrough;
body_tracker_type body_tracker;
face_tracker_type face_tracker;
@@ -91,6 +92,7 @@ public:
XrSystemBodyTrackingPropertiesBD bd_body_tracking_properties() const;
bool hand_tracking_supported() const;
bool hand_mesh_fb_supported() const;
passthrough_type passthrough_supported() const;
face_tracker_type face_tracker_supported() const;
body_tracker_type body_tracker_supported() const;