A Template for creating Vulkan layers in C++. Vulkan Layers allow you to intercept/hook the calls to Vulkan made by a program.
You can configure the template in CMakeLists.txt.
To add a function, first create a function with the correct signature following this template. Say the function you want to hook is called vkWhatever:
VKAPI_ATTR <return type of vkWhatever> VKAPI_CALL
Whatever(<arguments of vkWhatever>)
Notice that the function's name is vkWhatever without the vk part.
Then, add the following code to either GetInstanceProcAddr or GetDeviceProcAddr depending on whether vkWhatever is an instance or device function:
REGISTER_HOOK(Whatever);
If you want your hooked function to call the function it hooks, you can do something like this:
VKAPI_ATTR <return type of vkWhatever> VKAPI_CALL
Whatever(<arguments of vkWhatever>) {
ENTER_FUNCTION_PRINT(); // <- optional, this will log the name of the function when it is called if the debug environment variable is enabled
// ...
DeviceEntry *entry = getDeviceEntry(device);
PFN_vkWhatever next = (PFN_vkWhatever)entry->getDeviceProcAddr(*pDevice, "vkWhatever");
auto return_value = next(<args of vkWhatever>);
// ...
return return_value;
}
Note that you shouldn't just call vkWhatever as you normally would, because that would cause a loop.
You can compile your layer as you would compile any other CMake application:
mkdir build
cd build
cmake ..
make
After compilation, you should see a script called run_layer.sh in the build directory. You can run applications with your layer using it like this:
./run_layer.sh vkcube
Or, if you want to enable the logging:
ENABLE_VULKAN_LAYER_LOG=1 ./run_layer.sh vkcube
This template is based off Monado's vulkan-layers repo.