diff options
| author | Tomasz Kramkowski <tomasz@kramkow.ski> | 2026-11-18 01:52:10 +0000 |
|---|---|---|
| committer | Tomasz Kramkowski <tomasz@kramkow.ski> | 2026-11-18 01:52:10 +0000 |
| commit | 3b735d74d82e879aa7534677c565ece9fa556f78 (patch) | |
| tree | c1ba84a2aab546792fc75ab5c072f8416193afc6 /steamvr | |
| parent | 363fe07ad4cd514d907647ee0eda2310f6efa3c0 (diff) | |
| download | steamvr-patches-3b735d74d82e879aa7534677c565ece9fa556f78.tar.gz steamvr-patches-3b735d74d82e879aa7534677c565ece9fa556f78.tar.xz steamvr-patches-3b735d74d82e879aa7534677c565ece9fa556f78.zip | |
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.
Diffstat (limited to 'steamvr')
| -rw-r--r-- | steamvr/driver_vrlink/ack-window.c | 35 | ||||
| -rw-r--r-- | steamvr/patches.toml | 52 | ||||
| -rw-r--r-- | steamvr/vrcompositor/facet-wait.c | 102 | ||||
| -rw-r--r-- | steamvr/vrcompositor/shared.h | 79 | ||||
| -rw-r--r-- | steamvr/vrcompositor/vulkan-wait.c | 102 |
5 files changed, 359 insertions, 11 deletions
diff --git a/steamvr/driver_vrlink/ack-window.c b/steamvr/driver_vrlink/ack-window.c new file mode 100644 index 0000000..acd668d --- /dev/null +++ b/steamvr/driver_vrlink/ack-window.c @@ -0,0 +1,35 @@ +// SPDX-FileCopyrightText: 2026 Tomasz Kramkowski <tomasz@kramkow.ski> +// SPDX-License-Identifier: CC0-1.0 + +#include <layout.h> +#include <stdint.h> +#include <trampoline.h> + +struct Transmitter { + unsigned char unknown_000[0x17c]; + int32_t next_frame; // -1 before the first frame is issued + int32_t last_ack; + int32_t cutoff; +}; +ASSERT_OFFSET(struct Transmitter, next_frame, 0x17c); +ASSERT_OFFSET(struct Transmitter, last_ack, 0x180); +ASSERT_OFFSET(struct Transmitter, cutoff, 0x184); + +void maybe_update_ack(struct Transmitter *tx, uint32_t incoming) +{ + if (tx->next_frame < 0) return; + uint16_t behind = (uint16_t)(tx->next_frame - 1 - incoming); + if (behind >= 0x8000) return; + + if (tx->cutoff >= 0 && (uint16_t)(incoming - tx->cutoff) >= 0x8000) return; + tx->last_ack = incoming; + tx->cutoff = incoming; +} + +// Hooks where the current code checks an incoming ack, clears the cutoff, and +// updates the last ack. The intent is to specifically replace all of that. +TRAMPOLINE( + accept_ack, CALL(maybe_update_ack, 0, "r8", + "mov rdi, rbx\n" + "mov esi, eax\n") "jmp resume_ack\n" +); diff --git a/steamvr/patches.toml b/steamvr/patches.toml index 3061ede..d0f3698 100644 --- a/steamvr/patches.toml +++ b/steamvr/patches.toml @@ -39,21 +39,36 @@ edits = [ [0x436614, "00040000"], ] +[targets.compiled] +sources = ["vrcompositor/vulkan-wait.c", "vrcompositor/facet-wait.c"] + +[targets.compiled.imports] +pthread_mutex_lock = 0x157320 +pthread_mutex_unlock = 0x157710 +facet_wait_hook_end = 0x410277 +vulkan_wait_hook_end = 0x4313c2 +append_wait = 0x468180 + +# Add semaphore waits for things that may be eye textures (among other things, +# but it seems harmless). +# This avoids nasty stutters when the renderers sample eye textures before +# they're actually rendered by the GPU. This affects both renderers, which one +# is chosen when isn't totally clear to me. + +[[targets.compiled.hooks]] +address = 0x4313bb +length = 7 +target = "vulkan_wait_hook" + +[[targets.compiled.hooks]] +address = 0x410270 +length = 7 +target = "facet_wait_hook" + [[targets]] path = "drivers/vrlink/bin/linux64/driver_vrlink.so" sha256 = "b78a5a6bbfcb0cd80cd44f4b88ed97f5a93110ad62ebaaffff20768d0a7a9135" edits = [ - # Keep increasing (mod(2^16)) the ACK cutoff instead of clearing it (#965). - # If the headset's ACKs start to fall behind, the driver throttles new - # frame generation to once every 0.3s. Normally the driver stores the last - # ACK number regardless of whether it is newer or older than the currently - # stored one. This ACK number is used to determine if the headset has - # fallen behind. When a reset occurs, throttling should stop to allow the - # new frame (which does not depend on any prior frames) to be sent out - # immediately. But clearing the ACK cutoff means that the next frame - # immediately following that one can get throttled if the host is still - # receiving old ACKs. - [0xd7a6d, "89838401000090909090"], # Ignore stale tracking IDs and repeated older tracking attempts [0x116581, "89d02b47084189c6c1e004"], [0x11658c, "0f8853050000"], @@ -71,3 +86,18 @@ edits = [ [0x115524, "e83886ffff"], ] +# Don't clear the ACK cutoff. Also ignore ACKs which are beyond the latest +# frame ID. This is mainly to avoid the case where the headset erroneously +# sends an ack for frame ID 0 after a reconnect before it has anything to +# acknowledge. +# The current headset patch fixes this, but people aren't as eager to patch the +# headset as it's a bit more complicated, and it's not hard to add a workaround +# here. It improves robustness regardless. +[targets.compiled] +sources = ["driver_vrlink/ack-window.c"] +imports = { resume_ack = 0xd8a77 } + +[[targets.compiled.hooks]] +address = 0xd8a58 +length = 6 +target = "accept_ack" 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 <tomasz@kramkow.ski> +// SPDX-License-Identifier: CC0-1.0 + +#include <trampoline.h> + +#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 <tomasz@kramkow.ski> +// SPDX-License-Identifier: CC0-1.0 + +#ifndef STEAMVR_VRCOMPOSITOR_SHARED_H +#define STEAMVR_VRCOMPOSITOR_SHARED_H + +#include <layout.h> +#include <stddef.h> +#include <stdint.h> + +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 <tomasz@kramkow.ski> +// SPDX-License-Identifier: CC0-1.0 + +#include "shared.h" + +#include <pthread.h> +#include <trampoline.h> + +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" +); |
