From 3b735d74d82e879aa7534677c565ece9fa556f78 Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Wed, 18 Nov 2026 01:52:10 +0000 Subject: steamvr: even bug fixes have bugs The stutter fix was incomplete, waits were added for images which the renderers render and consume, but not for images coming from the application (e.g. eye textures). This hid some of the problem but you can still get stutter in intensive games that regularly miss their frame targets. Also, the headset can send spurious acks for frame 0 after a reconnect, despite not having received a frame, which can put the host into 3.3FPS mode. --- steamvr/vrcompositor/facet-wait.c | 102 +++++++++++++++++++++++++++++++++++++ steamvr/vrcompositor/shared.h | 79 ++++++++++++++++++++++++++++ steamvr/vrcompositor/vulkan-wait.c | 102 +++++++++++++++++++++++++++++++++++++ 3 files changed, 283 insertions(+) create mode 100644 steamvr/vrcompositor/facet-wait.c create mode 100644 steamvr/vrcompositor/shared.h create mode 100644 steamvr/vrcompositor/vulkan-wait.c (limited to 'steamvr/vrcompositor') diff --git a/steamvr/vrcompositor/facet-wait.c b/steamvr/vrcompositor/facet-wait.c new file mode 100644 index 0000000..086026c --- /dev/null +++ b/steamvr/vrcompositor/facet-wait.c @@ -0,0 +1,102 @@ +// SPDX-FileCopyrightText: 2026 Tomasz Kramkowski +// SPDX-License-Identifier: CC0-1.0 + +#include + +#include "shared.h" + +enum { VK_PIPELINE_STAGE_ALL_COMMANDS_BIT = 0x00010000 }; + +struct Texture { + unsigned char unknown_000[0x48]; + struct Image *image; +}; +ASSERT_OFFSET(struct Texture, image, 0x48); + +struct Batch { + unsigned char unknown_000[0xf0]; + SemaphoreEntry *waits; + SemaphoreEntry *waits_end; + unsigned char unknown_100[0x140 - 0x100]; + uint32_t *wait_stages; + unsigned char unknown_148[0x398 - 0x148]; + SemaphoreEntry *signals; + SemaphoreEntry *signals_end; +}; +ASSERT_OFFSET(struct Batch, waits, 0xf0); +ASSERT_OFFSET(struct Batch, waits_end, 0xf8); +ASSERT_OFFSET(struct Batch, wait_stages, 0x140); +ASSERT_OFFSET(struct Batch, signals, 0x398); +ASSERT_OFFSET(struct Batch, signals_end, 0x3a0); + +struct RendererContext { + unsigned char unknown_000[8]; + struct Batch *batch; +}; +ASSERT_OFFSET(struct RendererContext, batch, 8); + +// CFacetVRRenderer +struct Renderer { + unsigned char unknown_000[0x12b0]; + struct RendererContext *context; +}; +ASSERT_OFFSET(struct Renderer, context, 0x12b0); + +extern void append_wait( + struct Batch *batch, uintptr_t semaphore, uint32_t stages, uint64_t value +) __attribute__((visibility("hidden"))); + +void register_image_wait(struct Renderer *renderer, struct Texture *texture) +{ + // First we need the completion semaphore for the texture. + struct Image *image = texture->image; + if (!image) return; + + struct SharedResource *resource = image_to_resource(image); + + struct Semaphore *semaphore = resource->vtable->get_semaphore(resource); + if (!semaphore) return; + + uintptr_t handle = semaphore->vtable->get_handle(semaphore); + if (!handle) return; + + // The current batch that is being prepared, and that may need the image + // semaphore as a dependency. + struct Batch *batch = renderer->context->batch; + + // Ignore images whose semaphores this batch signals. + for (SemaphoreEntry *p = batch->signals; p != batch->signals_end; p++) + if (p->semaphore == handle) return; + + // If there's already a wait on this texture's semaphore, ensure it will + // block all work in this batch and that the value is at least as high as the + // semaphore value required for this texture to be ready. + uint64_t value = semaphore->vtable->get_value(semaphore); + uint32_t *stages = batch->wait_stages; + for (SemaphoreEntry *p = batch->waits; p != batch->waits_end; + p++, stages++) { + if (p->semaphore != handle) continue; + + if (p->value < value) p->value = value; + *stages |= VK_PIPELINE_STAGE_ALL_COMMANDS_BIT; + return; + } + + // Otherwise append a wait on this texture's completion semaphore. + append_wait(batch, handle, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, value); +} + +// This hooks at the beginning of a method in the "Facet" renderer which is +// called, among other things, on textures that the current submission batch +// will use as shader inputs (e.g. eye textures). +TRAMPOLINE( + facet_wait_hook, + CALL(register_image_wait, 8, "rdi, rsi, rdx, rcx", "") + + // clobbered by jump + "push rbp\n" + "mov rax, rsi\n" + "mov r9d, edx\n" + + "jmp facet_wait_hook_end\n" +); diff --git a/steamvr/vrcompositor/shared.h b/steamvr/vrcompositor/shared.h new file mode 100644 index 0000000..2fdd755 --- /dev/null +++ b/steamvr/vrcompositor/shared.h @@ -0,0 +1,79 @@ +// SPDX-FileCopyrightText: 2026 Tomasz Kramkowski +// SPDX-License-Identifier: CC0-1.0 + +#ifndef STEAMVR_VRCOMPOSITOR_SHARED_H +#define STEAMVR_VRCOMPOSITOR_SHARED_H + +#include +#include +#include + +ASSERT_SIZE(void *, 8); + +struct Image; +struct SharedResource; +struct Semaphore; +struct NativeDevice; + +typedef uint32_t GetOwner(struct Image *); +typedef struct Semaphore *GetSemaphore(struct SharedResource *); +typedef uint64_t SemaphoreGetU64(struct Semaphore *); +typedef unsigned char +SubmitWait(struct Semaphore *, uint64_t, struct NativeDevice *, unsigned char); + +struct ImageVtable { + unsigned char unknown_000[0xd8]; + GetOwner *get_owner; +}; +ASSERT_OFFSET(struct ImageVtable, get_owner, 0xd8); + +// vr::IVRSharedImage +struct Image { + const struct ImageVtable *vtable; +}; + +struct SharedResourceVtable { + unsigned char unknown_000[0x30]; + GetSemaphore *get_semaphore; +}; +ASSERT_OFFSET(struct SharedResourceVtable, get_semaphore, 0x30); + +// vr::IVRSharedResource +struct SharedResource { + const struct SharedResourceVtable *vtable; +}; + +struct SemaphoreVtable { + unsigned char unknown_000[0x40]; + SemaphoreGetU64 *get_handle; + unsigned char unknown_048[0x70 - 0x48]; + SemaphoreGetU64 *get_value; + unsigned char unknown_078[8]; + SubmitWait *submit_wait; +}; +ASSERT_OFFSET(struct SemaphoreVtable, get_handle, 0x40); +ASSERT_OFFSET(struct SemaphoreVtable, get_value, 0x70); +ASSERT_OFFSET(struct SemaphoreVtable, submit_wait, 0x80); + +// vr::IVRSharedSemaphore +struct Semaphore { + const struct SemaphoreVtable *vtable; +}; + +typedef struct { + uintptr_t semaphore; + uint64_t value; +} SemaphoreEntry; +ASSERT_SIZE(SemaphoreEntry, 16); +ASSERT_OFFSET(SemaphoreEntry, value, 8); + +static inline struct SharedResource *image_to_resource(struct Image *image) +{ + // The vbase offset entry for converting `vr::IVRSharedImage *` to + // `vr::IVRSharedResource *` is located at `image->vtable - 0x50`. + ptrdiff_t resource_offset = + *(const ptrdiff_t *)((const char *)image->vtable - 0x50); + return (void *)((char *)image + resource_offset); +} + +#endif diff --git a/steamvr/vrcompositor/vulkan-wait.c b/steamvr/vrcompositor/vulkan-wait.c new file mode 100644 index 0000000..6f2830d --- /dev/null +++ b/steamvr/vrcompositor/vulkan-wait.c @@ -0,0 +1,102 @@ +// SPDX-FileCopyrightText: 2026 Tomasz Kramkowski +// SPDX-License-Identifier: CC0-1.0 + +#include "shared.h" + +#include +#include + +enum { + VK_QUEUE_FAMILY_EXTERNAL = UINT32_MAX - 1, + VK_QUEUE_FAMILY_FOREIGN_EXT = UINT32_MAX - 2, +}; + +struct Texture { + unsigned char unknown_000[0xd8]; + struct Image *image; +}; +ASSERT_OFFSET(struct Texture, image, 0xd8); + +struct NativeDevice { + void *context; + unsigned char unknown_008[8]; +}; +ASSERT_SIZE(struct NativeDevice, 16); + +// CVulkanVRRenderer +struct Renderer { + unsigned char unknown_000[0x238]; + uint32_t current_queue; + unsigned char unknown_23c[4]; + struct NativeDevice queues[2]; + unsigned char unknown_260[0x10a0 - 0x260]; + SemaphoreEntry *signals; + SemaphoreEntry *signals_end; + unsigned char unknown_10b0[0x1520 - 0x10b0]; + pthread_mutex_t queue_mutex; +}; +ASSERT_OFFSET(struct Renderer, current_queue, 0x238); +ASSERT_OFFSET(struct Renderer, queues, 0x240); +ASSERT_OFFSET(struct Renderer, signals, 0x10a0); +ASSERT_OFFSET(struct Renderer, signals_end, 0x10a8); +ASSERT_OFFSET(struct Renderer, queue_mutex, 0x1520); + +void wait_for_input( + struct Renderer *renderer, struct Texture *texture, uint32_t target_queue +) +{ + enum Queue { + Q_GRAPHICS = 0, + Q_COMPUTE = 1, + Q_EXTERNAL = 3, + }; + + // We don't care about images we're giving someone else, or things we already + // own. + if (target_queue == Q_EXTERNAL) return; + struct Image *image = texture->image; + if (!image) return; + uint32_t owner = image->vtable->get_owner(image); + if (owner != VK_QUEUE_FAMILY_EXTERNAL && owner != VK_QUEUE_FAMILY_FOREIGN_EXT) + return; + + struct SharedResource *resource = image_to_resource(image); + struct Semaphore *semaphore = resource->vtable->get_semaphore(resource); + if (!semaphore) return; + uintptr_t sem_handle = semaphore->vtable->get_handle(semaphore); + if (!sem_handle) return; + + // Ignore textures whose associated semaphores this renderer will signal. + for (SemaphoreEntry *p = renderer->signals; p != renderer->signals_end; p++) + if (p->semaphore == sem_handle) return; + + uint64_t value = semaphore->vtable->get_value(semaphore); + if (!value) return; + uint32_t queue = renderer->current_queue; + if (queue == Q_EXTERNAL) return; + struct NativeDevice *device = &renderer->queues[queue]; + if (!device->context) return; + + pthread_mutex_lock(&renderer->queue_mutex); + semaphore->vtable->submit_wait(semaphore, value, device, 0); + pthread_mutex_unlock(&renderer->queue_mutex); +} + +// This hooks in via a function @ 0x431380 (method of CVulkanVRRenderer) which +// is a called to "prepare" an image for use by the GPU. It changes its layout +// and queue if needed. The important part is that it gets called for images a +// shader is about to read which are the problematic cases which cause stutters. +TRAMPOLINE( + vulkan_wait_hook, + CALL( + wait_for_input, 0, "rax, rcx, rdx, rsi, rdi, r8, r9, r10, r11", + "mov rdi, [rbp - 0x40]\n" // Renderer * + "mov rsi, rax\n" // Texture * + "mov edx, [rbp + 0x18]\n" // uint32_t target_queue + ) + // restore CPU flags + "cmp dword ptr [rbp + 0x18], 1\n" + // clobbered by jump + "mov r15, rax\n" + "jmp vulkan_wait_hook_end\n" +); -- cgit v1.3.1