aboutsummaryrefslogtreecommitdiffstats
path: root/steamvr/vrcompositor/facet-wait.c
diff options
context:
space:
mode:
Diffstat (limited to 'steamvr/vrcompositor/facet-wait.c')
-rw-r--r--steamvr/vrcompositor/facet-wait.c102
1 files changed, 102 insertions, 0 deletions
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"
+);