M2(Vulkan): capture hook via GPA interception + vkCmdCopyImageToBuffer read-back

New vk_hook.cpp inline-hooks the vulkan-1.dll vkGetInstanceProcAddr export and
hands back our wrappers for vkCreateInstance / vkCreateDevice / vkGetDeviceProcAddr
/ vkCreateSwapchainKHR / vkQueuePresentKHR, so a volk-using (loader-bypass) app
resolves our hooks. On present it reads the swap-chain image back with
vkCmdCopyImageToBuffer into a host-visible buffer (same read-back model as
D3D10/D3D9/OpenGL), swizzles BGRA->RGBA, and uploads it into the shared
keyed-mutex texture on a hook-owned D3D11 device. The read-back submit re-chains
the present's wait semaphores (consume the originals, signal our own that the
real present waits on) so capture orders after rendering without double-waiting.

Wired into the video subsystem with a lazy retry (vulkan-1.dll loads late). Hook
links the official Vulkan-Headers (headers only, VK_NO_PROTOTYPES) via the
include dir so the x86 sub-build builds too.

Because Vulkan caches its present pointer at init, late injection can't hook it:
mock_game_test launches the mock **suspended**, injects, resumes, and under
COOP_MOCK_VK_EARLY the mock loads Vulkan and waits so the hook arms first -- then
decodes frames through the hook like the other backends. 15/15 ctest (x64 +
x86); skips cleanly without a Vulkan driver.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-22 12:39:07 +02:00
parent d4a83e75bb
commit dd976979a1
6 changed files with 875 additions and 26 deletions

View File

@@ -300,35 +300,107 @@ void test_video_capture(const char* backend, ID3D11Device* device)
game.kill();
}
// Liveness smoke check for a backend whose capture can't be exercised by late injection
// (Vulkan caches its present pointer at init): launch it for a couple of seconds and assert it
// comes up and exits cleanly. Exit code 2 = backend unavailable on this machine (e.g. no Vulkan
// driver) -> skip without failing.
void test_liveness(const char* backend)
// Vulkan capture: Vulkan caches its present pointer at init, so late injection can't hook it.
// We launch the mock **suspended**, inject the hook, and resume; under COOP_MOCK_VK_EARLY the mock
// loads vulkan-1.dll and waits, giving the hook's worker time to hook vkGetInstanceProcAddr before
// the mock calls vkCreateInstance. Then we decode frames out of the captured pixels like the other
// backends. Exit code 2 = no Vulkan driver -> skip without failing.
void test_vk_capture(ID3D11Device* device)
{
std::printf("== liveness: %s ==\n", backend);
std::wstring args;
for (const char* p = backend; *p != '\0'; ++p)
std::printf("== video capture: vk (early-load) ==\n");
SetEnvironmentVariableW(L"COOP_MOCK_VK_EARLY", L"1");
PROCESS_INFORMATION pi{};
STARTUPINFOW si{};
si.cb = sizeof(si);
const std::wstring exe = tool_path(L"coop_mock_game.exe");
std::wstring cmd = L"\"" + exe + L"\" vk 30";
const BOOL ok =
CreateProcessW(exe.c_str(), cmd.data(), nullptr, nullptr, FALSE, CREATE_SUSPENDED, nullptr, nullptr, &si, &pi);
SetEnvironmentVariableW(L"COOP_MOCK_VK_EARLY", nullptr);
if (!ok)
{
args.push_back(static_cast<wchar_t>(*p));
}
args += L" 2"; // run 2s then exit on its own
MockGame game = MockGame::launch(args);
if (!game.ok)
{
check(false, "launch coop_mock_game (liveness)");
check(false, "launch suspended vk mock");
return;
}
WaitForSingleObject(game.pi.hProcess, 6000); // let the 2s run finish
if (!game.alive() && game.exit_code() == 2)
SharedMemory shm;
const std::uint32_t disabled = (1u << HookSubsys_Input) | (1u << HookSubsys_Focus) |
(1u << HookSubsys_Audio) | (1u << HookSubsys_Mkb);
SharedBlock* block = make_ipc(shm, pi.dwProcessId, disabled);
const bool injected = block != nullptr && inject_retry(pi.dwProcessId);
ResumeThread(pi.hThread); // the mock loads vulkan + waits, then renders
auto cleanup = [&] {
TerminateProcess(pi.hProcess, 0);
WaitForSingleObject(pi.hProcess, 2000);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
};
if (!injected)
{
std::printf(" backend '%s' unavailable on this machine -- skipping\n", backend);
game.kill();
check(false, "inject suspended vk mock");
cleanup();
return;
}
check(!game.alive(), "backend ran and exited within the time limit (no hang)");
check(game.exit_code() == 0, "backend came up and exited cleanly");
game.kill();
auto alive = [&] { return WaitForSingleObject(pi.hProcess, 0) == WAIT_TIMEOUT; };
auto exit_code = [&] {
DWORD c = 0;
GetExitCodeProcess(pi.hProcess, &c);
return c;
};
SharedTextureSource src;
src.init(device);
std::vector<std::uint32_t> seq;
std::uint64_t backward = 0;
std::uint32_t last = 0;
bool have_last = false;
for (int i = 0; i < 200 && alive(); ++i) // up to ~10 s (the mock waits 1.5 s at start)
{
Sleep(50);
const VideoShareView share = read_video_share(block);
if (!src.update(share, pi.dwProcessId))
{
continue;
}
std::uint8_t px[4] = {};
if (!src.read_pixel(coop::mock::kFrameBlock / 2, coop::mock::kFrameBlock / 2, px))
{
continue;
}
const std::uint32_t f = coop::mock::rgb_to_frame(px[0], px[1], px[2]);
if (have_last && f < last)
{
++backward;
}
last = f;
have_last = true;
seq.push_back(f);
if (seq.size() >= 40)
{
break;
}
}
if (!alive() && exit_code() == 2 && seq.empty())
{
std::printf(" Vulkan unavailable on this machine -- skipping vk capture\n");
cleanup();
return;
}
const std::uint32_t present = static_cast<std::uint32_t>(block->video.present_calls);
std::printf(" present_calls=%u copied=%llu samples=%zu backward=%llu\n", present,
static_cast<unsigned long long>(src.frames_copied()), seq.size(),
static_cast<unsigned long long>(backward));
check(present > 0, "hook captured Vulkan present calls");
check(src.frames_copied() >= 5, "host copied multiple shared frames (vk)");
check(seq.size() >= 5, "decoded multiple frame numbers from the captured pixels (vk)");
check(backward == 0, "captured vk frame numbers never go backwards");
if (!seq.empty())
{
check(seq.back() - seq.front() >= 10, "captured vk frame numbers advance");
}
cleanup();
}
// Launch the mock game rendering audio at `rate`/`channels`/`bits`/`fmt`, inject the audio
@@ -553,10 +625,9 @@ int main()
return 0;
}
// Vulkan: its present pointer is cached at init, so late injection can't hook it -- a
// liveness check (the mock comes up + presents + exits) is the automated coverage here; the
// capture path is exercised via the early-load path documented in the README.
test_liveness("vk");
// Vulkan: present pointer cached at init -> can't be late-hooked, so we capture via the
// early-load path (suspended launch + inject + resume; the mock loads Vulkan and waits).
test_vk_capture(device);
test_video_capture("gl", device);
test_video_capture("dx9ex", device);
test_video_capture("dx9", device);