Apply clang-format across the whole tree

Run clang-format (the repo's .clang-format: LLVM base, 120 cols, tabs,
Allman functions) over every source file so the tree is formatter-clean.
Whitespace only -- no behavior change; full x64 + x86 suites pass.

Also set SortIncludes: false in .clang-format. Windows include order is
load-bearing (windows.h must precede tlhelp32.h / mmreg.h / xinput.h /
dinput.h; winsock2.h must precede windows.h), and the default
alphabetical sort reorders tlhelp32.h ahead of windows.h -- a build
break. Leaving order alone keeps the manual, correct grouping.
This commit is contained in:
2026-07-12 11:52:53 +02:00
parent c684a15fb9
commit 30eccf749d
155 changed files with 3333 additions and 6171 deletions

View File

@@ -38,67 +38,59 @@
#include "vk_capture.hpp"
// --- Loader/layer interface (interface version 2) ---------------------------
extern "C"
{
typedef enum VkLayerFunction_
{
COOP_VK_LAYER_LINK_INFO = 0,
COOP_VK_LOADER_DATA_CALLBACK = 1,
COOP_VK_LOADER_LAYER_CREATE_DEVICE_CALLBACK = 2,
COOP_VK_LOADER_FEATURES = 3,
} CoopVkLayerFunction;
extern "C" {
typedef enum VkLayerFunction_ {
COOP_VK_LAYER_LINK_INFO = 0,
COOP_VK_LOADER_DATA_CALLBACK = 1,
COOP_VK_LOADER_LAYER_CREATE_DEVICE_CALLBACK = 2,
COOP_VK_LOADER_FEATURES = 3,
} CoopVkLayerFunction;
typedef PFN_vkVoidFunction(VKAPI_PTR* PFN_GetPhysicalDeviceProcAddr)(VkInstance, const char*);
typedef PFN_vkVoidFunction(VKAPI_PTR* PFN_GetPhysicalDeviceProcAddr)(VkInstance, const char*);
typedef struct VkLayerInstanceLink_
{
struct VkLayerInstanceLink_* pNext;
PFN_vkGetInstanceProcAddr pfnNextGetInstanceProcAddr;
PFN_GetPhysicalDeviceProcAddr pfnNextGetPhysicalDeviceProcAddr;
} VkLayerInstanceLink;
typedef struct VkLayerInstanceLink_ {
struct VkLayerInstanceLink_* pNext;
PFN_vkGetInstanceProcAddr pfnNextGetInstanceProcAddr;
PFN_GetPhysicalDeviceProcAddr pfnNextGetPhysicalDeviceProcAddr;
} VkLayerInstanceLink;
typedef struct VkLayerInstanceCreateInfo
{
VkStructureType sType; // 1000000000 = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO
const void* pNext;
CoopVkLayerFunction function;
union {
VkLayerInstanceLink* pLayerInfo;
void* pfnCallback; // other callbacks (unused here); keeps the union pointer-sized
} u;
} VkLayerInstanceCreateInfo;
typedef struct VkLayerInstanceCreateInfo {
VkStructureType sType; // 1000000000 = VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO
const void* pNext;
CoopVkLayerFunction function;
union {
VkLayerInstanceLink* pLayerInfo;
void* pfnCallback; // other callbacks (unused here); keeps the union pointer-sized
} u;
} VkLayerInstanceCreateInfo;
typedef struct VkLayerDeviceLink_
{
struct VkLayerDeviceLink_* pNext;
PFN_vkGetInstanceProcAddr pfnNextGetInstanceProcAddr;
PFN_vkGetDeviceProcAddr pfnNextGetDeviceProcAddr;
} VkLayerDeviceLink;
typedef struct VkLayerDeviceLink_ {
struct VkLayerDeviceLink_* pNext;
PFN_vkGetInstanceProcAddr pfnNextGetInstanceProcAddr;
PFN_vkGetDeviceProcAddr pfnNextGetDeviceProcAddr;
} VkLayerDeviceLink;
typedef struct VkLayerDeviceCreateInfo
{
VkStructureType sType; // 1000000001 = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO
const void* pNext;
CoopVkLayerFunction function;
union {
VkLayerDeviceLink* pLayerInfo;
void* pfnCallback;
} u;
} VkLayerDeviceCreateInfo;
typedef struct VkLayerDeviceCreateInfo {
VkStructureType sType; // 1000000001 = VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO
const void* pNext;
CoopVkLayerFunction function;
union {
VkLayerDeviceLink* pLayerInfo;
void* pfnCallback;
} u;
} VkLayerDeviceCreateInfo;
typedef struct VkNegotiateLayerInterface
{
uint32_t sType; // 1 = LAYER_NEGOTIATE_INTERFACE_STRUCT
void* pNext;
uint32_t loaderLayerInterfaceVersion;
PFN_vkGetInstanceProcAddr pfnGetInstanceProcAddr;
PFN_vkGetDeviceProcAddr pfnGetDeviceProcAddr;
PFN_GetPhysicalDeviceProcAddr pfnGetPhysicalDeviceProcAddr;
} VkNegotiateLayerInterface;
typedef struct VkNegotiateLayerInterface {
uint32_t sType; // 1 = LAYER_NEGOTIATE_INTERFACE_STRUCT
void* pNext;
uint32_t loaderLayerInterfaceVersion;
PFN_vkGetInstanceProcAddr pfnGetInstanceProcAddr;
PFN_vkGetDeviceProcAddr pfnGetDeviceProcAddr;
PFN_GetPhysicalDeviceProcAddr pfnGetPhysicalDeviceProcAddr;
} VkNegotiateLayerInterface;
}
namespace
{
namespace {
// The loader tags its chain-link structs with small, loader-internal sType values (not the
// 1000000000-range): VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO = 47, _DEVICE = 48. These are
// from the (unvendored) vk_layer.h and are stable across loader versions.
@@ -126,8 +118,7 @@ std::uint32_t g_qfam = 0;
VkCapture g_cap; // the shared, off-present-thread read-back
struct SwapInfo
{
struct SwapInfo {
VkSwapchainKHR sc;
VkFormat fmt;
std::uint32_t w, h;
@@ -150,22 +141,18 @@ bool eq(const char* a, const char* b)
void logvk(const char* fmt, ...)
{
static int enabled = -1;
if (enabled < 0)
{
if (enabled < 0) {
enabled = GetEnvironmentVariableW(L"COOP_VK_LAYER_LOG", nullptr, 0) != 0 ? 1 : 0;
}
if (enabled == 0)
{
if (enabled == 0) {
return;
}
wchar_t dir[MAX_PATH] = {};
if (GetTempPathW(MAX_PATH, dir) == 0)
{
if (GetTempPathW(MAX_PATH, dir) == 0) {
return;
}
FILE* f = _wfopen((std::wstring(dir) + L"coop_vk_layer.log").c_str(), L"a");
if (f == nullptr)
{
if (f == nullptr) {
return;
}
va_list ap;
@@ -179,20 +166,17 @@ void logvk(const char* fmt, ...)
// Decide whether this process is the host's capture target (see file header).
bool decide_active()
{
if (GetEnvironmentVariableW(L"COOP_VK_LAYER_FORCE", nullptr, 0) != 0)
{
if (GetEnvironmentVariableW(L"COOP_VK_LAYER_FORCE", nullptr, 0) != 0) {
return true;
}
wchar_t dir[MAX_PATH] = {};
const DWORD n = GetTempPathW(MAX_PATH, dir);
if (n == 0 || n >= MAX_PATH)
{
if (n == 0 || n >= MAX_PATH) {
return false;
}
HANDLE f = CreateFileW((std::wstring(dir) + L"coop_vk_target.txt").c_str(), GENERIC_READ, FILE_SHARE_READ,
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (f == INVALID_HANDLE_VALUE)
{
HANDLE f = CreateFileW((std::wstring(dir) + L"coop_vk_target.txt").c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (f == INVALID_HANDLE_VALUE) {
return false;
}
char want[MAX_PATH] = {};
@@ -200,12 +184,10 @@ bool decide_active()
ReadFile(f, want, sizeof(want) - 1, &got, nullptr);
CloseHandle(f);
// Trim trailing whitespace/newline.
while (got > 0 && (want[got - 1] == '\n' || want[got - 1] == '\r' || want[got - 1] == ' '))
{
while (got > 0 && (want[got - 1] == '\n' || want[got - 1] == '\r' || want[got - 1] == ' ')) {
want[--got] = '\0';
}
if (got == 0)
{
if (got == 0) {
return false;
}
wchar_t self[MAX_PATH] = {};
@@ -221,10 +203,8 @@ bool decide_active()
// can reallocate and dangle the returned pointer).
const SwapInfo* find_swap(VkSwapchainKHR sc)
{
for (const SwapInfo& s : g_swaps)
{
if (s.sc == sc)
{
for (const SwapInfo& s : g_swaps) {
if (s.sc == sc) {
return &s;
}
}
@@ -237,20 +217,17 @@ const SwapInfo* find_swap(VkSwapchainKHR sc)
void trace_present_rate()
{
static int enabled = -1;
if (enabled < 0)
{
if (enabled < 0) {
enabled = GetEnvironmentVariableW(L"COOP_VK_LAYER_LOG", nullptr, 0) != 0 ? 1 : 0;
}
if (enabled == 0)
{
if (enabled == 0) {
return;
}
static LARGE_INTEGER freq{};
static LARGE_INTEGER last{};
static int count = 0;
static std::uint64_t last_published = 0;
if (freq.QuadPart == 0)
{
if (freq.QuadPart == 0) {
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&last);
}
@@ -258,8 +235,7 @@ void trace_present_rate()
LARGE_INTEGER now;
QueryPerformanceCounter(&now);
const double sec = static_cast<double>(now.QuadPart - last.QuadPart) / static_cast<double>(freq.QuadPart);
if (sec >= 1.0)
{
if (sec >= 1.0) {
const std::uint64_t pub = g_cap.frames_published();
logvk("present rate %.1f/s captured %.1f/s (game keeps its rate; capture is off the present thread)",
count / sec, (pub - last_published) / sec);
@@ -272,12 +248,10 @@ void trace_present_rate()
VKAPI_ATTR VkResult VKAPI_CALL layer_QueuePresentKHR(VkQueue queue, const VkPresentInfoKHR* pi)
{
trace_present_rate(); // unconditional so an inactive (pass-through) run gives a baseline to compare
if (g_active && g_ipc.connected())
{
if (g_active && g_ipc.connected()) {
g_ipc.note_present();
}
if (g_active && pi != nullptr && pi->swapchainCount == 1)
{
if (g_active && pi != nullptr && pi->swapchainCount == 1) {
// Copy the matched swapchain out under the lock, then capture without holding it.
VkImage image = VK_NULL_HANDLE;
VkFormat fmt = VK_FORMAT_UNDEFINED;
@@ -287,8 +261,7 @@ VKAPI_ATTR VkResult VKAPI_CALL layer_QueuePresentKHR(VkQueue queue, const VkPres
std::scoped_lock lock(g_swaps_mutex);
const SwapInfo* s = find_swap(pi->pSwapchains[0]);
const std::uint32_t idx = pi->pImageIndices[0];
if (s != nullptr && idx < s->images.size())
{
if (s != nullptr && idx < s->images.size()) {
image = s->images[idx];
fmt = s->fmt;
w = s->w;
@@ -296,11 +269,9 @@ VKAPI_ATTR VkResult VKAPI_CALL layer_QueuePresentKHR(VkQueue queue, const VkPres
matched = true;
}
}
if (matched)
{
if (matched) {
VkSemaphore chained = VK_NULL_HANDLE;
if (g_cap.present(image, fmt, w, h, pi->pWaitSemaphores, pi->waitSemaphoreCount, chained))
{
if (g_cap.present(image, fmt, w, h, pi->pWaitSemaphores, pi->waitSemaphoreCount, chained)) {
VkPresentInfoKHR p = *pi;
p.waitSemaphoreCount = 1;
p.pWaitSemaphores = &chained;
@@ -312,7 +283,7 @@ VKAPI_ATTR VkResult VKAPI_CALL layer_QueuePresentKHR(VkQueue queue, const VkPres
}
VKAPI_ATTR VkResult VKAPI_CALL layer_CreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR* ci,
const VkAllocationCallbacks* a, VkSwapchainKHR* out)
const VkAllocationCallbacks* a, VkSwapchainKHR* out)
{
// Log the game's chosen present mode (= its sync mode) -- 0 IMMEDIATE, 1 MAILBOX, 2 FIFO (vsync),
// 3 FIFO_RELAXED. We pass `ci` straight through, so whatever the game asked for is what it gets;
@@ -320,8 +291,7 @@ VKAPI_ATTR VkResult VKAPI_CALL layer_CreateSwapchainKHR(VkDevice device, const V
logvk("CreateSwapchain: presentMode=%d (0=IMMEDIATE 1=MAILBOX 2=FIFO 3=FIFO_RELAXED) %ux%u minImageCount=%u",
static_cast<int>(ci->presentMode), ci->imageExtent.width, ci->imageExtent.height, ci->minImageCount);
const VkResult r = g_real_create_swapchain(device, ci, a, out);
if (g_active && r == VK_SUCCESS && out && g_get_swapchain_images)
{
if (g_active && r == VK_SUCCESS && out && g_get_swapchain_images) {
SwapInfo info{};
info.sc = *out;
info.fmt = ci->imageFormat;
@@ -333,12 +303,11 @@ VKAPI_ATTR VkResult VKAPI_CALL layer_CreateSwapchainKHR(VkDevice device, const V
g_get_swapchain_images(device, *out, &n, info.images.data());
{
std::scoped_lock lock(g_swaps_mutex);
g_swaps.erase(std::remove_if(g_swaps.begin(), g_swaps.end(),
[&](const SwapInfo& e) { return e.sc == info.sc; }),
g_swaps.end());
g_swaps.erase(
std::remove_if(g_swaps.begin(), g_swaps.end(), [&](const SwapInfo& e) { return e.sc == info.sc; }),
g_swaps.end());
g_swaps.push_back(std::move(info));
if (g_swaps.size() > kMaxTrackedSwaps)
{
if (g_swaps.size() > kMaxTrackedSwaps) {
g_swaps.erase(g_swaps.begin());
}
}
@@ -381,19 +350,16 @@ void start_capture(VkDevice dev)
#undef LOAD
f.GetPhysicalDeviceMemoryProperties = reinterpret_cast<PFN_vkGetPhysicalDeviceMemoryProperties>(
g_next_gipa(g_instance, "vkGetPhysicalDeviceMemoryProperties"));
g_get_swapchain_images =
reinterpret_cast<PFN_vkGetSwapchainImagesKHR>(g_next_gdpa(dev, "vkGetSwapchainImagesKHR"));
g_get_swapchain_images = reinterpret_cast<PFN_vkGetSwapchainImagesKHR>(g_next_gdpa(dev, "vkGetSwapchainImagesKHR"));
g_cap.init(g_phys, dev, g_qfam, f, GetCurrentProcessId(), [](std::uint32_t w, std::uint32_t h) {
// Runs on the reaper thread after each frame is published to the shared texture. Connect the
// IPC channel lazily here (the host may not have created it yet at device-create time), then
// publish the frame so the host's generation counter advances.
if (!g_ipc_tried.exchange(true))
{
if (!g_ipc_tried.exchange(true)) {
g_ipc.connect(/*attempts=*/40, /*delay_ms=*/25);
}
if (g_ipc.connected())
{
if (g_ipc.connected()) {
g_ipc.publish_video_frame(w, h, static_cast<std::uint32_t>(DXGI_FORMAT_R8G8B8A8_UNORM));
}
});
@@ -405,13 +371,10 @@ VKAPI_ATTR VkResult VKAPI_CALL layer_CreateDevice(VkPhysicalDevice phys, const V
const VkAllocationCallbacks* a, VkDevice* out)
{
auto* link = reinterpret_cast<VkLayerDeviceCreateInfo*>(const_cast<void*>(ci->pNext));
while (link != nullptr &&
!(link->sType == kLoaderDeviceCreateInfo && link->function == COOP_VK_LAYER_LINK_INFO))
{
while (link != nullptr && !(link->sType == kLoaderDeviceCreateInfo && link->function == COOP_VK_LAYER_LINK_INFO)) {
link = reinterpret_cast<VkLayerDeviceCreateInfo*>(const_cast<void*>(link->pNext));
}
if (link == nullptr)
{
if (link == nullptr) {
logvk("CreateDevice: LINK_INFO not found");
return VK_ERROR_INITIALIZATION_FAILED;
}
@@ -421,33 +384,29 @@ VKAPI_ATTR VkResult VKAPI_CALL layer_CreateDevice(VkPhysicalDevice phys, const V
auto create = reinterpret_cast<PFN_vkCreateDevice>(next_gipa(g_instance, "vkCreateDevice"));
const VkResult r = create(phys, ci, a, out);
logvk("CreateDevice: result=%d active=%d", (int)r, g_active ? 1 : 0);
if (r == VK_SUCCESS && out != nullptr && g_device == VK_NULL_HANDLE)
{
if (r == VK_SUCCESS && out != nullptr && g_device == VK_NULL_HANDLE) {
g_phys = phys;
g_device = *out;
g_next_gdpa = next_gdpa;
g_qfam = ci->queueCreateInfoCount > 0 ? ci->pQueueCreateInfos[0].queueFamilyIndex : 0;
g_real_present = reinterpret_cast<PFN_vkQueuePresentKHR>(next_gdpa(*out, "vkQueuePresentKHR"));
g_real_create_swapchain = reinterpret_cast<PFN_vkCreateSwapchainKHR>(next_gdpa(*out, "vkCreateSwapchainKHR"));
if (g_active)
{
if (g_active) {
start_capture(*out);
}
}
return r;
}
VKAPI_ATTR VkResult VKAPI_CALL layer_CreateInstance(const VkInstanceCreateInfo* ci,
const VkAllocationCallbacks* a, VkInstance* out)
VKAPI_ATTR VkResult VKAPI_CALL layer_CreateInstance(const VkInstanceCreateInfo* ci, const VkAllocationCallbacks* a,
VkInstance* out)
{
auto* link = reinterpret_cast<VkLayerInstanceCreateInfo*>(const_cast<void*>(ci->pNext));
while (link != nullptr &&
!(link->sType == kLoaderInstanceCreateInfo && link->function == COOP_VK_LAYER_LINK_INFO))
{
while (link != nullptr
&& !(link->sType == kLoaderInstanceCreateInfo && link->function == COOP_VK_LAYER_LINK_INFO)) {
link = reinterpret_cast<VkLayerInstanceCreateInfo*>(const_cast<void*>(link->pNext));
}
if (link == nullptr)
{
if (link == nullptr) {
logvk("CreateInstance: LINK_INFO not found");
return VK_ERROR_INITIALIZATION_FAILED;
}
@@ -455,8 +414,7 @@ VKAPI_ATTR VkResult VKAPI_CALL layer_CreateInstance(const VkInstanceCreateInfo*
link->u.pLayerInfo = link->u.pLayerInfo->pNext; // advance the chain
auto create = reinterpret_cast<PFN_vkCreateInstance>(next_gipa(nullptr, "vkCreateInstance"));
const VkResult r = create(ci, a, out);
if (r == VK_SUCCESS && out != nullptr)
{
if (r == VK_SUCCESS && out != nullptr) {
g_instance = *out;
g_next_gipa = next_gipa;
g_active = decide_active();
@@ -468,8 +426,7 @@ VKAPI_ATTR VkResult VKAPI_CALL layer_CreateInstance(const VkInstanceCreateInfo*
VKAPI_ATTR void VKAPI_CALL layer_DestroyDevice(VkDevice device, const VkAllocationCallbacks* a)
{
auto destroy = reinterpret_cast<PFN_vkDestroyDevice>(g_next_gdpa(device, "vkDestroyDevice"));
if (g_active && device == g_device)
{
if (g_active && device == g_device) {
g_cap.shutdown(); // joins the reaper, drains the device, frees the read-back resources
{
std::scoped_lock lock(g_swaps_mutex);
@@ -489,8 +446,7 @@ VKAPI_ATTR void VKAPI_CALL layer_DestroyInstance(VkInstance instance, const VkAl
VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL layer_gdpa(VkDevice device, const char* name)
{
if (name == nullptr)
{
if (name == nullptr) {
return nullptr;
}
if (eq(name, "vkGetDeviceProcAddr"))
@@ -506,8 +462,7 @@ VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL layer_gdpa(VkDevice device, const char*
VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL layer_gipa(VkInstance instance, const char* name)
{
if (name == nullptr)
{
if (name == nullptr) {
return nullptr;
}
if (eq(name, "vkGetInstanceProcAddr"))
@@ -526,11 +481,10 @@ VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL layer_gipa(VkInstance instance, const c
} // namespace
extern "C" __declspec(dllexport) VkResult VKAPI_CALL
vkNegotiateLoaderLayerInterfaceVersion(VkNegotiateLayerInterface* pVersionStruct)
vkNegotiateLoaderLayerInterfaceVersion(VkNegotiateLayerInterface* pVersionStruct)
{
logvk("negotiate: requestedVersion=%u", pVersionStruct->loaderLayerInterfaceVersion);
if (pVersionStruct->loaderLayerInterfaceVersion > 2)
{
if (pVersionStruct->loaderLayerInterfaceVersion > 2) {
pVersionStruct->loaderLayerInterfaceVersion = 2;
}
pVersionStruct->pfnGetInstanceProcAddr = layer_gipa;
@@ -540,8 +494,7 @@ extern "C" __declspec(dllexport) VkResult VKAPI_CALL
}
// Also export the entry points directly, for loaders that probe them by name.
extern "C" __declspec(dllexport) PFN_vkVoidFunction VKAPI_CALL coop_vkGetInstanceProcAddr(VkInstance i,
const char* n)
extern "C" __declspec(dllexport) PFN_vkVoidFunction VKAPI_CALL coop_vkGetInstanceProcAddr(VkInstance i, const char* n)
{
return layer_gipa(i, n);
}