fix: harden Quest VR compatibility

This commit is contained in:
XxJSONDeruloxX committed 2026-10-15 18:55:23 -04:00
1 parent 92f2696a0c
commit 5e9ebbc7df
17 files changed
+377 -55

No files matched your search

+14 -7
View File
@@ -559,13 +559,8 @@ public:
VRProperties()->SetFloatProperty(c, Prop_DisplayFrequency_Float, hz);
VRProperties()->SetFloatProperty(c, Prop_SecondsFromVsyncToPhotons_Float, 0.011f);
VRProperties()->SetUint64Property(c, Prop_CurrentUniverseId_Uint64, 2);
if (xrMode_) {
char timestamp[32];
std::time_t now = std::time(nullptr); std::tm utc{}; gmtime_r(&now, &utc);
std::strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%SZ", &utc);
const auto chaperone = xr_chaperone_json(xr.head, timestamp);
VRProperties()->SetStringProperty(c, Prop_DriverProvidedChaperoneJson_String, chaperone.c_str());
}
chaperone_.Reset();
if (xrMode_) PublishChaperone(xr);
VRProperties()->SetBoolProperty(c, Prop_IsOnDesktop_Bool, false);
VRProperties()->SetBoolProperty(c, Prop_DisplayDebugMode_Bool, false);
directMode_ = VRSettings()->GetBool(kSettingsSection, "directMode");
@@ -680,6 +675,16 @@ public:
}
private:
void PublishChaperone(const DroidDeckXrTracking &tracking) {
chaperone_.Update(tracking, [this](const DroidDeckXrPose &head) {
char timestamp[32];
std::time_t now = std::time(nullptr); std::tm utc{}; gmtime_r(&now, &utc);
std::strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%SZ", &utc);
const auto json = xr_chaperone_json(head, timestamp);
const auto container = VRProperties()->TrackedDeviceToPropertyContainer(id_);
return VRProperties()->SetStringProperty(container, Prop_DriverProvidedChaperoneJson_String, json.c_str()) == TrackedProp_Success;
});
}
void PoseLoop() {
uint32_t lastSeq = 0;
auto lastSent = std::chrono::steady_clock::now();
@@ -687,6 +692,7 @@ private:
DroidDeckXrTracking xr;
if (xrMode_) {
bool fresh = xr_.Read(&xr) && XrTrackingFile::Fresh(xr);
if (fresh) PublishChaperone(xr);
DriverPose_t head = GetPose();
VRServerDriverHost()->TrackedDevicePoseUpdated(id_, head, sizeof(head));
for (int hand = 0; hand < 2; hand++) if (hands_[hand]) {
@@ -732,6 +738,7 @@ public:
private:
PoseFile pose_;
XrTrackingFile xr_;
XrChaperoneOrigin chaperone_;
bool xrMode_ = false;
PadRing pad_;
PadState lastPad_;
+49 -6
View File
@@ -101,6 +101,7 @@ struct dev {
int active;
int display;
int producer;
int quest_images;
};
static int steamvr_running(void) {
@@ -256,7 +257,7 @@ static VkResult VKAPI_CALL CreateDevice(VkPhysicalDevice pd, const VkDeviceCreat
for (int i = 0; i < MAX_OBJS; i++) {
if (!g_dev[i].handle) {
g_dev[i] = (struct dev){*out, next_gdpa, (PFN_vkDestroyDevice)next_gdpa(*out, "vkDestroyDevice"), active, display,
in && in->producer};
in && in->producer, active && access("/run/droiddeck/droiddeck-xr-frames", F_OK) == 0};
break;
}
}
@@ -526,6 +527,7 @@ static PFN_vkVoidFunction display_device_hook(const char *name) {
#define POLL_US 300
static pthread_mutex_t g_submission_lock = PTHREAD_MUTEX_INITIALIZER;
static unsigned g_producer_idle_calls;
struct producer_queue {
VkQueue queue;
VkDevice device;
@@ -547,7 +549,7 @@ static int producer_acquire(int64_t deadline) {
usleep(100);
}
int queues = 0;
if (g_producer_overflow) goto fail;
if (g_producer_overflow || g_producer_idle_calls) goto fail;
for (unsigned index = 0; index < MAX_OBJS; index++) {
struct producer_queue *entry = &g_producer_queues[index];
if (!entry->queue) continue;
@@ -931,18 +933,34 @@ static VkResult VKAPI_CALL QueueBindSparse(VkQueue queue, uint32_t count, const
static VkResult VKAPI_CALL QueueWaitIdle(VkQueue queue) {
struct dev *device = dev_for(queue);
PFN_vkQueueWaitIdle next = (PFN_vkQueueWaitIdle)device->gdpa(device->handle, "vkQueueWaitIdle");
if (device->producer) pthread_mutex_lock(&g_submission_lock);
if (device->producer) {
pthread_mutex_lock(&g_submission_lock);
g_producer_idle_calls++;
pthread_mutex_unlock(&g_submission_lock);
}
VkResult result = next(queue);
if (device->producer) pthread_mutex_unlock(&g_submission_lock);
if (device->producer) {
pthread_mutex_lock(&g_submission_lock);
g_producer_idle_calls--;
pthread_mutex_unlock(&g_submission_lock);
}
return result;
}
static VkResult VKAPI_CALL DeviceWaitIdle(VkDevice handle) {
struct dev *device = dev_for(handle);
PFN_vkDeviceWaitIdle next = (PFN_vkDeviceWaitIdle)device->gdpa(handle, "vkDeviceWaitIdle");
if (device->producer) pthread_mutex_lock(&g_submission_lock);
if (device->producer) {
pthread_mutex_lock(&g_submission_lock);
g_producer_idle_calls++;
pthread_mutex_unlock(&g_submission_lock);
}
VkResult result = next(handle);
if (device->producer) pthread_mutex_unlock(&g_submission_lock);
if (device->producer) {
pthread_mutex_lock(&g_submission_lock);
g_producer_idle_calls--;
pthread_mutex_unlock(&g_submission_lock);
}
return result;
}
@@ -959,6 +977,29 @@ static VkResult VKAPI_CALL GetSemaphoreCounterValue(VkDevice d, VkSemaphore s, u
}
static VkResult VKAPI_CALL CreateImage(VkDevice d, const VkImageCreateInfo *ci, const VkAllocationCallbacks *a, VkImage *out) {
VkImageCreateInfo compatible = *ci;
VkExternalMemoryImageCreateInfo shared;
const VkBaseInStructure *first = ci->pNext;
// SteamVR omits a format list, but DXVK adds one; Turnip can choose a different shared-memory layout.
if (dev_for(d)->quest_images && (ci->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) && ci->tiling == VK_IMAGE_TILING_OPTIMAL
&& first && first->sType == VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO) {
const VkBaseInStructure *second = first->pNext;
if (second && second->sType == VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO
&& ((const VkExternalMemoryImageCreateInfo *)second)->handleTypes == VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT) {
compatible.pNext = second; ci = &compatible;
say("%s", "matching opaque shared image layout to SteamVR's unrestricted format list");
}
}
if (dev_for(d)->quest_images && (ci->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) && ci->tiling == VK_IMAGE_TILING_OPTIMAL
&& first && first->sType == VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO) {
const VkExternalMemoryImageCreateInfo *external = (const void *)first;
const VkBaseInStructure *second = external->pNext;
if (external->handleTypes == VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT && second
&& second->sType == VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO) {
shared = *external; shared.pNext = second->pNext; compatible.pNext = &shared; ci = &compatible;
say("%s", "matching opaque shared image layout to SteamVR's unrestricted format list");
}
}
VkResult r = NEXT(d, CreateImage)(d, ci, a, out);
if (!sem_trace_on()) return r;
const VkExternalMemoryImageCreateInfo *ext = NULL;
@@ -974,6 +1015,8 @@ static VkResult VKAPI_CALL CreateImage(VkDevice d, const VkImageCreateInfo *ci,
ci->tiling, ci->arrayLayers, ci->mipLevels, ci->samples, ext->handleTypes);
for (uint32_t i = 0; list && i < list->viewFormatCount && n < (int)sizeof(line) - 8; i++)
n += snprintf(line + n, sizeof(line) - n, " %d", list->pViewFormats[i]);
for (const VkBaseInStructure *p = ci->pNext; p && n < (int)sizeof(line) - 16; p = p->pNext)
n += snprintf(line + n, sizeof(line) - n, " chain:%d", p->sType);
say("%s", line);
return r;
}
+12 -1
View File
@@ -1,5 +1,5 @@
#pragma once
#include "xr_tracking.h"
#include "xr_tracking_file.h"
#include <cmath>
#include <iomanip>
#include <locale>
@@ -10,6 +10,17 @@ struct XrSeatedOrigin {
double yaw, translation[3];
};
class XrChaperoneOrigin {
public:
void Reset() { published_ = false; }
template <typename Publish> void Update(const DroidDeckXrTracking &tracking, Publish publish) {
if (!published_ && XrTrackingFile::Fresh(tracking) && XrTrackingFile::Tracked(tracking.head))
published_ = publish(tracking.head);
}
private:
bool published_ = false;
};
inline XrSeatedOrigin xr_seated_origin(const DroidDeckXrPose &head) {
const auto *q = head.orientation;
double yaw = std::atan2(2 * (q[0] * q[2] + q[3] * q[1]), 1 - 2 * (q[0] * q[0] + q[1] * q[1]));
+6
View File
@@ -175,6 +175,12 @@ private:
if (!xr_frame_peer(client)) { rejected_++; close(client); continue; }
uint64_t lastFrame = 0;
while (!stopping_) {
pollfd idle{client, POLLIN, 0};
int available;
do {
available = poll(&idle, 1, 50);
} while (!stopping_ && (available == 0 || (available < 0 && errno == EINTR)));
if (stopping_ || available <= 0 || !(idle.revents & POLLIN)) break;
DroidDeckXrPixels frame;
XrFrameDescriptor shared;
int64_t deadline = xr_frame_now() + 200000000LL;
+1 -1
View File
@@ -54,7 +54,7 @@ int main(void) {
VkDeviceCreateInfo create_device = {VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, &timeline_feature, 0, 1, &queue_info, 0, NULL, 0, NULL, NULL};
assert(vkCreateDevice(physical, &create_device, NULL, &test_device) == VK_SUCCESS);
vkGetDeviceQueue(test_device, family, 0, &test_queue);
g_dev[0] = (struct dev){test_device, vkGetDeviceProcAddr, NULL, 1, 0, 1};
g_dev[0] = (struct dev){test_device, vkGetDeviceProcAddr, NULL, 1, 0, 1, 0};
g_producer_started = 1;
VkBufferCreateInfo create_buffer = {VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, NULL, 0, 4096, VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VK_SHARING_MODE_EXCLUSIVE, 0, NULL};
+82 -3
View File
@@ -9,6 +9,58 @@ static const VkSemaphoreWaitInfo *expected_wait;
static unsigned create_calls;
static unsigned destroyed;
static VkResult submit_result = VK_SUCCESS;
static const void *expected_image_chain;
static const void *expected_external_tail;
static int expect_cloned_external;
static VkResult VKAPI_CALL mock_image(VkDevice device, const VkImageCreateInfo *info,
const VkAllocationCallbacks *allocator, VkImage *output) {
(void)device; (void)allocator; (void)output;
if (expect_cloned_external) {
const VkExternalMemoryImageCreateInfo *external = info->pNext;
assert(external != expected_image_chain);
assert(external->sType == VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO);
assert(external->handleTypes == VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT);
assert(external->pNext == expected_external_tail);
} else assert(info->pNext == expected_image_chain);
return VK_ERROR_FORMAT_NOT_SUPPORTED;
}
static void check_shared_images(VkDevice device) {
VkBaseInStructure tail = {VK_STRUCTURE_TYPE_APPLICATION_INFO, NULL};
VkExternalMemoryImageCreateInfo external = {VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO,
&tail, VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT};
VkFormat formats[] = {VK_FORMAT_R8G8B8A8_SRGB, VK_FORMAT_R8G8B8A8_UNORM};
VkImageFormatListCreateInfo list = {VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO, &external, 2, formats};
VkImageCreateInfo image = {0};
image.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
image.pNext = &list;
image.flags = VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
image.tiling = VK_IMAGE_TILING_OPTIMAL;
VkImage output;
/* Other targets, nonmutable, linear and DMA-BUF images retain their caller's chain. */
expected_image_chain = &list;
assert(CreateImage(device, &image, NULL, &output) == VK_ERROR_FORMAT_NOT_SUPPORTED);
g_dev[0].quest_images = 1;
image.flags = 0;
assert(CreateImage(device, &image, NULL, &output) == VK_ERROR_FORMAT_NOT_SUPPORTED);
image.flags = VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT;
image.tiling = VK_IMAGE_TILING_LINEAR;
assert(CreateImage(device, &image, NULL, &output) == VK_ERROR_FORMAT_NOT_SUPPORTED);
image.tiling = VK_IMAGE_TILING_OPTIMAL;
external.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
assert(CreateImage(device, &image, NULL, &output) == VK_ERROR_FORMAT_NOT_SUPPORTED);
external.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT;
expected_image_chain = &external;
assert(CreateImage(device, &image, NULL, &output) == VK_ERROR_FORMAT_NOT_SUPPORTED);
assert(image.pNext == &list && list.pNext == &external && external.pNext == &tail);
/* The reverse chain order preserves its tail without modifying caller memory. */
external.pNext = &list; list.pNext = &tail; image.pNext = &external;
expect_cloned_external = 1; expected_external_tail = &tail;
assert(CreateImage(device, &image, NULL, &output) == VK_ERROR_FORMAT_NOT_SUPPORTED);
assert(image.pNext == &external && external.pNext == &list && list.pNext == &tail);
expect_cloned_external = 0; g_dev[0].quest_images = 0;
}
static VkResult VKAPI_CALL mock_submit(VkQueue queue, uint32_t count, const VkSubmitInfo *submits, VkFence fence) {
assert(queue && count == 1 && fence == VK_NULL_HANDLE);
@@ -49,15 +101,36 @@ static void VKAPI_CALL mock_destroy(VkDevice device, VkSemaphore semaphore, cons
}
static int expect_queue_lock;
static VkQueue other_queue;
static _Atomic int other_submitted;
static void *signal_from_other_queue(void *unused) {
(void)unused;
assert(QueueSubmit(other_queue, 1, expected_submit, VK_NULL_HANDLE) == VK_SUCCESS);
atomic_store(&other_submitted, 1);
return NULL;
}
static void check_queue_lock(void) {
int acquired = pthread_mutex_trylock(&g_submission_lock);
assert(acquired == (expect_queue_lock ? EBUSY : 0));
assert(acquired == 0);
if (!acquired) pthread_mutex_unlock(&g_submission_lock);
assert(g_producer_idle_calls == (unsigned)expect_queue_lock);
if (expect_queue_lock) assert(!producer_acquire(droiddeck_producer_now() + 10000000LL));
}
static VkResult VKAPI_CALL mock_queue_idle(VkQueue queue) {
assert(queue); check_queue_lock(); return VK_ERROR_DEVICE_LOST;
assert(queue); check_queue_lock();
if (expect_queue_lock) {
VkSubmitInfo signal = {VK_STRUCTURE_TYPE_SUBMIT_INFO, NULL, 1, NULL, NULL, 0, NULL, 0, NULL};
expected_submit = &signal;
pthread_t worker; atomic_store(&other_submitted, 0);
assert(pthread_create(&worker, NULL, signal_from_other_queue, NULL) == 0);
int64_t deadline = droiddeck_producer_now() + 1000000000LL;
while (!atomic_load(&other_submitted) && droiddeck_producer_now() < deadline) usleep(1000);
assert(atomic_load(&other_submitted)); pthread_join(worker, NULL);
}
return VK_ERROR_DEVICE_LOST;
}
static VkResult VKAPI_CALL mock_device_idle(VkDevice device) {
@@ -66,6 +139,7 @@ static VkResult VKAPI_CALL mock_device_idle(VkDevice device) {
static PFN_vkVoidFunction VKAPI_CALL mock_proc(VkDevice device, const char *name) {
(void)device;
if (!strcmp(name, "vkCreateImage")) return (PFN_vkVoidFunction)mock_image;
if (!strcmp(name, "vkQueueWaitIdle")) return (PFN_vkVoidFunction)mock_queue_idle;
if (!strcmp(name, "vkDeviceWaitIdle")) return (PFN_vkVoidFunction)mock_device_idle;
if (!strcmp(name, "vkQueueSubmit")) return (PFN_vkVoidFunction)mock_submit;
@@ -82,7 +156,11 @@ int main(void) {
void *dispatch = &dispatch;
VkDevice device = (VkDevice)&dispatch;
VkQueue queue = (VkQueue)&dispatch;
g_dev[0] = (struct dev){device, mock_proc, NULL, 1, 0, 0};
void *other_dispatch = dispatch;
other_queue = (VkQueue)&other_dispatch;
g_producer_retry_at = INT64_MAX;
g_dev[0] = (struct dev){device, mock_proc, NULL, 1, 0, 0, 0};
check_shared_images(device);
g_poller_started = 1;
PFN_vkQueueWaitIdle queue_idle = (PFN_vkQueueWaitIdle)semaphore_hook("vkQueueWaitIdle");
PFN_vkDeviceWaitIdle device_idle = (PFN_vkDeviceWaitIdle)semaphore_hook("vkDeviceWaitIdle");
@@ -92,6 +170,7 @@ int main(void) {
expect_queue_lock = producer;
assert(queue_idle(queue) == VK_ERROR_DEVICE_LOST);
assert(device_idle(device) == VK_ERROR_DEVICE_LOST);
assert(g_producer_idle_calls == 0);
}
g_dev[0].producer = 0;
VkExportSemaphoreCreateInfo export_info = {VK_STRUCTURE_TYPE_EXPORT_SEMAPHORE_CREATE_INFO, NULL,
+16
View File
@@ -4,6 +4,22 @@
int main() {
DroidDeckXrPose head{{0, 0, 0, 1}, {2, 1.6f, -3}, 15};
XrChaperoneOrigin deferred;
DroidDeckXrTracking tracking{};
tracking.active = 1; tracking.period_ns = 11111111; tracking.sampled_ns = XrTrackingFile::Now();
tracking.eyes[0].width = tracking.eyes[0].height = 4;
unsigned publications = 0;
auto publish = [&](const auto &pose) { publications++; assert(pose.position[1] == 1.6f); return true; };
deferred.Update(tracking, publish); assert(publications == 0);
tracking.head = head; tracking.head.flags = 0;
deferred.Update(tracking, publish); assert(publications == 0);
tracking.head = head;
deferred.Update(tracking, publish); assert(publications == 1);
tracking.head.position[1] = 2;
deferred.Update(tracking, publish); assert(publications == 1);
deferred.Reset(); tracking.head = head;
deferred.Update(tracking, [&](const auto &) { publications++; return false; });
deferred.Update(tracking, publish); assert(publications == 3);
auto origin = xr_seated_origin(head);
assert(origin.translation[0] == -2 && std::abs(origin.translation[1] + 1.6) < 1e-6 && origin.translation[2] == 3);
head.orientation[1] = head.orientation[3] = std::sqrt(0.5f);
+1
View File
@@ -86,6 +86,7 @@ int main(int argc, char **argv) {
assert(taken.header.frame_id == 1 && taken.pixels[0] == 17 && taken.header.eye_pose[0][0][3] == -0.032f);
assert(taken.header.color_space == DROIDDECK_XR_COLOR_SRGB_RGBA8);
assert(!receiver.Take(&taken));
usleep(350000);
for (uint64_t id = 2; id <= 100; id++) {
auto header = Header(id); header.eye_pose[0][0][3] = static_cast<float>(id);
pixels.assign(header.bytes, static_cast<unsigned char>(id));