-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvulkan_hooks.cpp
More file actions
366 lines (307 loc) · 14 KB
/
Copy pathvulkan_hooks.cpp
File metadata and controls
366 lines (307 loc) · 14 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include "vulkan_hooks.hpp"
#include "steamvr_linux_fixes.hpp"
#include <cstring>
#include <iostream>
#include <vector>
VKAPI_ATTR VkResult VKAPI_CALL Hook_vkCreateInstance(const VkInstanceCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator,
VkInstance* pInstance) {
VkLayerInstanceCreateInfo* layerInfo = (VkLayerInstanceCreateInfo*)pCreateInfo->pNext;
while (layerInfo && (layerInfo->sType != VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO ||
layerInfo->function != VK_LAYER_LINK_INFO)) {
layerInfo = (VkLayerInstanceCreateInfo*)layerInfo->pNext;
}
if (!layerInfo)
return VK_ERROR_INITIALIZATION_FAILED;
PFN_vkGetInstanceProcAddr next_gipa = layerInfo->u.pLayerInfo->pfnNextGetInstanceProcAddr;
layerInfo->u.pLayerInfo = layerInfo->u.pLayerInfo->pNext;
PFN_vkCreateInstance next_createInstance = (PFN_vkCreateInstance)next_gipa(VK_NULL_HANDLE, "vkCreateInstance");
VkApplicationInfo newAppInfo = {};
if (pCreateInfo->pApplicationInfo) {
newAppInfo = *(pCreateInfo->pApplicationInfo);
} else {
newAppInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
}
VkInstanceCreateInfo newCreateInfo = *pCreateInfo;
newCreateInfo.pApplicationInfo = &newAppInfo;
VkResult result = next_createInstance(&newCreateInfo, pAllocator, pInstance);
if (result == VK_SUCCESS) {
std::lock_guard<std::mutex> lock(g_mapMutex);
g_next_gipa[*pInstance] = next_gipa;
PFN_vkEnumeratePhysicalDevices enumPd =
(PFN_vkEnumeratePhysicalDevices)next_gipa(*pInstance, "vkEnumeratePhysicalDevices");
if (enumPd) {
uint32_t pdCount = 0;
enumPd(*pInstance, &pdCount, nullptr);
std::vector<VkPhysicalDevice> pds(pdCount);
enumPd(*pInstance, &pdCount, pds.data());
for (VkPhysicalDevice pd : pds) {
g_physDevToInstance[pd] = *pInstance;
}
}
}
return result;
}
VKAPI_ATTR VkResult VKAPI_CALL Hook_vkCreateDevice(VkPhysicalDevice physicalDevice,
const VkDeviceCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator,
VkDevice* pDevice) {
VkLayerDeviceCreateInfo* layerInfo = (VkLayerDeviceCreateInfo*)pCreateInfo->pNext;
while (layerInfo && (layerInfo->sType != VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO ||
layerInfo->function != VK_LAYER_LINK_INFO)) {
layerInfo = (VkLayerDeviceCreateInfo*)layerInfo->pNext;
}
if (!layerInfo)
return VK_ERROR_INITIALIZATION_FAILED;
PFN_vkGetInstanceProcAddr next_gipa = layerInfo->u.pLayerInfo->pfnNextGetInstanceProcAddr;
PFN_vkGetDeviceProcAddr next_gdpa = layerInfo->u.pLayerInfo->pfnNextGetDeviceProcAddr;
layerInfo->u.pLayerInfo = layerInfo->u.pLayerInfo->pNext;
PFN_vkCreateDevice next_createDevice = (PFN_vkCreateDevice)next_gipa(VK_NULL_HANDLE, "vkCreateDevice");
VkResult result;
if (g_patchesInstalled) {
std::vector<const char*> newExtensions;
bool hasPresentIdExt = false;
bool hasPresentWaitExt = false;
bool hasTimelineExt = false;
bool hasFifoLatestReady = false;
for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
newExtensions.push_back(pCreateInfo->ppEnabledExtensionNames[i]);
if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_PRESENT_ID_EXTENSION_NAME) == 0)
hasPresentIdExt = true;
if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_PRESENT_WAIT_EXTENSION_NAME) == 0)
hasPresentWaitExt = true;
if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_KHR_TIMELINE_SEMAPHORE_EXTENSION_NAME) == 0)
hasTimelineExt = true;
if (strcmp(pCreateInfo->ppEnabledExtensionNames[i], VK_EXT_PRESENT_MODE_FIFO_LATEST_READY_EXTENSION_NAME) == 0)
hasFifoLatestReady = true;
}
if (!hasPresentIdExt)
newExtensions.push_back(VK_KHR_PRESENT_ID_EXTENSION_NAME);
if (!hasPresentWaitExt)
newExtensions.push_back(VK_KHR_PRESENT_WAIT_EXTENSION_NAME);
if (!hasTimelineExt)
newExtensions.push_back(VK_KHR_TIMELINE_SEMAPHORE_EXTENSION_NAME);
// Check if the physical device supports
// VK_EXT_present_mode_fifo_latest_ready
if (!hasFifoLatestReady) {
VkInstance instance = g_physDevToInstance[physicalDevice];
if (instance) {
PFN_vkEnumerateDeviceExtensionProperties enumDevExts =
(PFN_vkEnumerateDeviceExtensionProperties)next_gipa(instance, "vkEnumerateDeviceExtensionProperties");
if (enumDevExts) {
uint32_t extCount = 0;
enumDevExts(physicalDevice, nullptr, &extCount, nullptr);
std::vector<VkExtensionProperties> exts(extCount);
enumDevExts(physicalDevice, nullptr, &extCount, exts.data());
for (const auto& ext : exts) {
if (strcmp(ext.extensionName, VK_EXT_PRESENT_MODE_FIFO_LATEST_READY_EXTENSION_NAME) == 0) {
newExtensions.push_back(VK_EXT_PRESENT_MODE_FIFO_LATEST_READY_EXTENSION_NAME);
hasFifoLatestReady = true;
break;
}
}
}
}
}
bool foundTimeline = false, foundPresentId = false, foundPresentWait = false, foundFifoLatestReady = false;
void* currentNext = const_cast<void*>(pCreateInfo->pNext);
while (currentNext) {
VkBaseOutStructure* base = (VkBaseOutStructure*)currentNext;
if (base->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TIMELINE_SEMAPHORE_FEATURES_KHR ||
base->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES) {
foundTimeline = true;
} else if (base->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENT_ID_FEATURES_KHR) {
foundPresentId = true;
} else if (base->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENT_WAIT_FEATURES_KHR) {
foundPresentWait = true;
} else if (base->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENT_MODE_FIFO_LATEST_READY_FEATURES_EXT) {
foundFifoLatestReady = true;
((VkPhysicalDevicePresentModeFifoLatestReadyFeaturesEXT*)base)->presentModeFifoLatestReady = VK_TRUE;
}
currentNext = base->pNext;
}
void* headNext = const_cast<void*>(pCreateInfo->pNext);
VkPhysicalDeviceTimelineSemaphoreFeaturesKHR featTimeline = {
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TIMELINE_SEMAPHORE_FEATURES_KHR};
if (!foundTimeline) {
featTimeline.timelineSemaphore = VK_TRUE;
featTimeline.pNext = headNext;
headNext = &featTimeline;
}
VkPhysicalDevicePresentIdFeaturesKHR featId = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENT_ID_FEATURES_KHR};
if (!foundPresentId) {
featId.presentId = VK_TRUE;
featId.pNext = headNext;
headNext = &featId;
}
VkPhysicalDevicePresentWaitFeaturesKHR featWait = {VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENT_WAIT_FEATURES_KHR};
if (!foundPresentWait) {
featWait.presentWait = VK_TRUE;
featWait.pNext = headNext;
headNext = &featWait;
}
VkPhysicalDevicePresentModeFifoLatestReadyFeaturesEXT featFifoLatestReady = {
VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENT_MODE_FIFO_LATEST_READY_FEATURES_EXT};
if (hasFifoLatestReady && !foundFifoLatestReady) {
featFifoLatestReady.presentModeFifoLatestReady = VK_TRUE;
featFifoLatestReady.pNext = headNext;
headNext = &featFifoLatestReady;
}
VkDeviceCreateInfo newCreateInfo = *pCreateInfo;
newCreateInfo.enabledExtensionCount = (uint32_t)newExtensions.size();
newCreateInfo.ppEnabledExtensionNames = newExtensions.data();
newCreateInfo.pNext = headNext;
result = next_createDevice(physicalDevice, &newCreateInfo, pAllocator, pDevice);
} else {
result = next_createDevice(physicalDevice, pCreateInfo, pAllocator, pDevice);
}
if (result == VK_SUCCESS) {
std::lock_guard<std::mutex> lock(g_mapMutex);
g_deviceToPhysDev[*pDevice] = physicalDevice;
DeviceDispatch& dispatch = g_deviceDispatch[*pDevice];
dispatch.GetDeviceProcAddr = next_gdpa;
dispatch.GetDeviceQueue = (PFN_vkGetDeviceQueue)next_gdpa(*pDevice, "vkGetDeviceQueue");
dispatch.WaitForPresentKHR = (PFN_vkWaitForPresentKHR)next_gdpa(*pDevice, "vkWaitForPresentKHR");
dispatch.QueuePresentKHR = (PFN_vkQueuePresentKHR)next_gdpa(*pDevice, "vkQueuePresentKHR");
dispatch.CreateSwapchainKHR = (PFN_vkCreateSwapchainKHR)next_gdpa(*pDevice, "vkCreateSwapchainKHR");
dispatch.CreateImage = (PFN_vkCreateImage)next_gdpa(*pDevice, "vkCreateImage");
}
return result;
}
VKAPI_ATTR VkResult VKAPI_CALL Hook_vkCreateImage(VkDevice device,
const VkImageCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator,
VkImage* pImage) {
PFN_vkCreateImage next_create = nullptr;
{
std::lock_guard<std::mutex> lock(g_mapMutex);
auto it = g_deviceDispatch.find(device);
if (it != g_deviceDispatch.end())
next_create = it->second.CreateImage;
}
if (!next_create)
return VK_ERROR_INITIALIZATION_FAILED;
// Fix issue with 0x0 textures crashing vrcompositor on some platforms
VkImageCreateInfo modifiedInfo = *pCreateInfo;
if (modifiedInfo.extent.width == 0)
modifiedInfo.extent.width = 1;
if (modifiedInfo.extent.height == 0)
modifiedInfo.extent.height = 1;
return next_create(device, &modifiedInfo, pAllocator, pImage);
}
VKAPI_ATTR void VKAPI_CALL Hook_vkGetDeviceQueue(VkDevice device,
uint32_t queueFamilyIndex,
uint32_t queueIndex,
VkQueue* pQueue) {
PFN_vkGetDeviceQueue next_func = nullptr;
{
std::lock_guard<std::mutex> lock(g_mapMutex);
auto it = g_deviceDispatch.find(device);
if (it != g_deviceDispatch.end())
next_func = it->second.GetDeviceQueue;
}
if (next_func) {
next_func(device, queueFamilyIndex, queueIndex, pQueue);
if (pQueue && *pQueue != VK_NULL_HANDLE) {
std::lock_guard<std::mutex> lock(g_mapMutex);
g_queueToDevice[*pQueue] = device;
}
}
}
VKAPI_ATTR VkResult VKAPI_CALL Hook_vkCreateSwapchainKHR(VkDevice device,
const VkSwapchainCreateInfoKHR* pCreateInfo,
const VkAllocationCallbacks* pAllocator,
VkSwapchainKHR* pSwapchain) {
PFN_vkCreateSwapchainKHR next_create = nullptr;
VkPhysicalDevice physDev = VK_NULL_HANDLE;
{
std::lock_guard<std::mutex> lock(g_mapMutex);
auto it = g_deviceDispatch.find(device);
if (it != g_deviceDispatch.end())
next_create = it->second.CreateSwapchainKHR;
if (g_deviceToPhysDev.count(device))
physDev = g_deviceToPhysDev[device];
}
if (!next_create)
return VK_ERROR_INITIALIZATION_FAILED;
VkSwapchainCreateInfoKHR modifiedInfo = *pCreateInfo;
// Ensure color bit is added.
modifiedInfo.imageUsage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
// Try to switch to FIFO_LATEST_READY if available
do {
if (physDev == VK_NULL_HANDLE) {
break;
}
VkInstance instance = g_physDevToInstance[physDev];
PFN_vkGetInstanceProcAddr gipa = g_next_gipa[instance];
if (!gipa) {
break;
}
auto getModes =
(PFN_vkGetPhysicalDeviceSurfacePresentModesKHR)gipa(instance, "vkGetPhysicalDeviceSurfacePresentModesKHR");
if (!getModes) {
break;
}
uint32_t count = 0;
getModes(physDev, pCreateInfo->surface, &count, nullptr);
std::vector<VkPresentModeKHR> modes(count);
getModes(physDev, pCreateInfo->surface, &count, modes.data());
for (auto mode : modes) {
if (mode == VK_PRESENT_MODE_FIFO_LATEST_READY_EXT) {
std::cerr << "Switching Present Mode to "
"VK_PRESENT_MODE_FIFO_LATEST_READY_EXT"
<< std::endl;
modifiedInfo.presentMode = VK_PRESENT_MODE_FIFO_LATEST_READY_EXT;
break;
}
}
} while (0);
return next_create(device, &modifiedInfo, pAllocator, pSwapchain);
}
VKAPI_ATTR VkResult VKAPI_CALL Hook_vkQueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pPresentInfo) {
PFN_vkQueuePresentKHR next_present = nullptr;
{
std::lock_guard<std::mutex> lock(g_mapMutex);
auto qIt = g_queueToDevice.find(queue);
if (qIt != g_queueToDevice.end()) {
VkDevice device = qIt->second;
auto dIt = g_deviceDispatch.find(device);
if (dIt != g_deviceDispatch.end()) {
next_present = dIt->second.QueuePresentKHR;
}
}
}
if (!next_present) {
std::cerr << "CRITICAL: Failed to resolve dispatch for Queue " << queue << ". Dropping frame." << std::endl;
return VK_SUCCESS;
}
g_lastSwapchain = (pPresentInfo->swapchainCount > 0) ? pPresentInfo->pSwapchains[0] : VK_NULL_HANDLE;
// Scan the pNext chain to see if SteamVR already provided a Present ID
const VkPresentIdKHR* existingPid = nullptr;
const VkBaseInStructure* current = (const VkBaseInStructure*)pPresentInfo->pNext;
while (current) {
if (current->sType == VK_STRUCTURE_TYPE_PRESENT_ID_KHR) {
existingPid = (const VkPresentIdKHR*)current;
break;
}
current = current->pNext;
}
if (existingPid && existingPid->swapchainCount > 0 && existingPid->pPresentIds) {
// SteamVR already injected the ID. (currently SteamVR doesn't do this)
g_currentPresentId = existingPid->pPresentIds[0];
return next_present(queue, pPresentInfo);
} else {
// SteamVR didn't inject one. We must append it ourselves.
uint64_t currentId = g_presentCounter.fetch_add(1);
std::vector<uint64_t> ids(pPresentInfo->swapchainCount, currentId);
VkPresentIdKHR pid = {};
pid.sType = VK_STRUCTURE_TYPE_PRESENT_ID_KHR;
pid.pNext = pPresentInfo->pNext;
pid.swapchainCount = pPresentInfo->swapchainCount;
pid.pPresentIds = ids.data();
VkPresentInfoKHR mod = *pPresentInfo;
mod.pNext = &pid;
g_currentPresentId = currentId;
return next_present(queue, &mod);
}
}