aboutsummaryrefslogtreecommitdiffstats
path: root/steamvr/vrcompositor/vulkan-wait.c
blob: 8b915f481826dae5df6bba0549574062354a8519 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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_GRAPHICS && target_queue != Q_COMPUTE) 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_GRAPHICS && queue != Q_COMPUTE) 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"
);