2020-10-26 17:24:57 +00:00
# `vk-bootstrap`
2020-01-30 08:15:10 +00:00
2020-10-26 17:24:57 +00:00
A utility library that jump starts initialization of Vulkan
2020-01-30 08:15:10 +00:00
This library simplifies the tedious process of:
2020-06-10 19:39:14 +00:00
* Instance creation
* Physical Device selection
* Device creation
* Getting queues
* Swapchain creation
2020-01-30 08:15:10 +00:00
2020-02-04 03:51:52 +00:00
It also adds several conveniences for:
2020-02-04 03:34:46 +00:00
2020-04-19 06:42:57 +00:00
* Enabling validation layers
2020-06-10 19:39:14 +00:00
* Adding a debug callback messenger
* Enabling extensions on a physical device
* Select a gpu based on a set of criteria like features, extensions, memory, etc
2020-02-04 03:34:46 +00:00
2020-04-19 06:42:57 +00:00
Read the [Getting Started ](docs/getting_started.md ) guide for a quick start on using `vk-bootstrap`
## Basic Usage
2020-01-30 08:15:10 +00:00
```cpp
2020-02-04 03:51:52 +00:00
#include "VkBootstrap.h"
2020-10-26 17:24:57 +00:00
bool init_vulkan () {
2020-02-04 03:51:52 +00:00
vkb::InstanceBuilder builder;
2020-04-19 03:34:51 +00:00
auto inst_ret = builder.set_app_name ("Example Vulkan Application")
.request_validation_layers ()
.use_default_debug_messenger ()
.build ();
if (!inst_ret) {
2020-12-23 22:18:51 +00:00
std::cerr << "Failed to create Vulkan instance. Error: " < < inst_ret.error (). message () << " \n";
2020-10-26 17:24:57 +00:00
return false;
2020-02-04 03:51:52 +00:00
}
2020-04-19 03:34:51 +00:00
vkb::Instance vkb_inst = inst_ret.value ();
vkb::PhysicalDeviceSelector selector{ vkb_inst };
auto phys_ret = selector.set_surface (/* from user created window*/)
.set_minimum_version (1, 1) // require a vulkan 1.1 capable device
.require_dedicated_transfer_queue ()
.select ();
if (!phys_ret) {
2020-12-23 22:18:51 +00:00
std::cerr << "Failed to select Vulkan Physical Device. Error: " < < phys_ret.error (). message () << " \n";
2020-10-26 17:24:57 +00:00
return false;
2020-02-04 03:51:52 +00:00
}
2020-04-19 03:34:51 +00:00
vkb::DeviceBuilder device_builder{ phys_ret.value () };
// automatically propagate needed data from instance & physical device
2020-02-04 03:51:52 +00:00
auto dev_ret = device_builder.build ();
2020-04-19 03:34:51 +00:00
if (!dev_ret) {
2020-12-23 22:18:51 +00:00
std::cerr << "Failed to create Vulkan device. Error: " < < dev_ret.error (). message () << " \n";
2020-10-26 17:24:57 +00:00
return false;
2020-02-04 03:51:52 +00:00
}
2020-04-19 03:34:51 +00:00
vkb::Device vkb_device = dev_ret.value ();
2020-02-04 03:51:52 +00:00
2020-02-19 02:12:31 +00:00
// Get the VkDevice handle used in the rest of a vulkan application
2020-04-19 03:34:51 +00:00
VkDevice device = vkb_device.device;
2020-02-19 02:12:31 +00:00
// Get the graphics queue with a helper function
2020-04-19 03:34:51 +00:00
auto graphics_queue_ret = vkb_device.get_queue (vkb::QueueType::graphics);
if (!graphics_queue_ret) {
2020-12-23 22:18:51 +00:00
std::cerr << "Failed to get graphics queue. Error: " < < graphics_queue_ret.error (). message () << " \n";
2020-10-26 17:24:57 +00:00
return false;
2020-02-17 20:13:09 +00:00
}
2020-04-19 03:34:51 +00:00
VkQueue graphics_queue = graphics_queue_ret.value ();
2020-02-19 02:12:31 +00:00
2020-03-08 00:19:58 +00:00
// Turned 400-500 lines of boilerplate into less than fifty.
2020-10-26 17:24:57 +00:00
return true;
2020-01-30 08:15:10 +00:00
}
2020-02-04 03:51:52 +00:00
```
2020-02-19 02:12:31 +00:00
See `example/triangle.cpp` for an example that renders a triangle to the screen.
2020-10-26 17:24:57 +00:00
## Setting up `vk-bootstrap`
2020-02-04 03:51:52 +00:00
2021-03-30 15:41:15 +00:00
This library has no external dependencies beyond C++14, its standard library, and the Vulkan Headers.
2020-02-04 03:51:52 +00:00
2021-03-02 16:47:31 +00:00
Note: on Unix platforms, `vk-bootstrap` will require the dynamic linker in order to compile as the library doesn't link against `vulkan-1.dll` /`libvulkan.so` directly.
2020-04-19 06:42:57 +00:00
2020-10-26 17:24:57 +00:00
### Copy-Paste
2020-02-04 03:54:03 +00:00
2020-10-26 17:24:57 +00:00
Copy the `src/VkBootstrap.h` and `src/VkBootstrap.cpp` files into your project, include them into your build, then compile as you normally would.
2020-02-04 03:54:03 +00:00
2020-10-26 17:24:57 +00:00
`vk-bootstrap` is *not* a header only library, so no need to worry about macros in the header.
2021-04-23 05:47:07 +00:00
#### Linux specific
vk-bootstrap will load the required symbols at runtime, which requires that the application is linked to the system dynamic link.
How the dynamic linker is linked into the project depends on the build system in question.
If CMake is being used, link vk-bootstrap with `${CMAKE_DL_LIBS}` .
2020-10-26 17:24:57 +00:00
### git-submodule + CMake
Add this project as a git-submodule into the root directory. Suggested is using a subdirectory to hold all submodules.
2020-02-04 03:51:52 +00:00
```bash
git submodule add https://github.com/charles-lunarg/vk-bootstrap
```
2020-10-26 17:24:57 +00:00
With CMake, add the subdirectory to include the project
2020-02-04 03:34:46 +00:00
2020-02-04 03:51:52 +00:00
```cmake
add_subdirectory(vk-bootstrap)
2020-10-26 17:24:57 +00:00
```
Then use `target_link_libraries` to use the library in whichever target needs it.
```cmake
target_link_libraries(your_application_name vk-bootstrap)
```
2020-02-19 02:12:31 +00:00
2020-10-26 17:24:57 +00:00
### CMake Fetch Content
If cmake 3.12 is available, use the FetchContent capability of cmake to directly download and build the library for you.
```cmake
2021-01-10 04:49:58 +00:00
include(FetchContent)
2020-10-26 17:24:57 +00:00
FetchContent_Declare(
fetch_vk_bootstrap
GIT_REPOSITORY https://github.com/charles-lunarg/vk-bootstrap
GIT_TAG BRANCH_OR_TAG #suggest using a tag so the library doesn't update whenever new commits are pushed to a branch
)
FetchContent_MakeAvailable(fetch_vk_bootstrap)
2020-02-19 02:12:31 +00:00
target_link_libraries(your_application_name vk-bootstrap)
2020-01-30 08:15:10 +00:00
```
2020-02-19 02:12:31 +00:00
### Manually Building
2020-01-30 08:15:10 +00:00
2020-01-30 09:01:49 +00:00
```bash
git clone https://github.com/charles-lunarg/vk-bootstrap
cd vk-bootstrap
mkdir build
cd build
cmake ..
```
2021-03-02 16:47:31 +00:00
### Vulkan-Headers dependency
2020-02-19 02:12:31 +00:00
2021-03-02 16:47:31 +00:00
By default, when using vk-bootstrap through CMake, it will attempt to locate the Vulkan-Headers on the system and fall back to downloading them directly if they aren't present. If the `VK_BOOTSTRAP_VULKAN_HEADER_DIR` option is specified, it will use that directory instead.
### Testing
Tests will be enabled if you open this project standalone. If you include this project as a subdirectory or sub-project, you can force enable tests by setting the option `VK_BOOTSTRAP_TEST` to `ON` . Testing requires GLFW and Catch2 but are acquired automatically using cmake fetch content.
2020-01-30 09:01:49 +00:00
```bash
2020-06-10 19:39:14 +00:00
cmake ../path/to/your_project/ -DVK_BOOTSTRAP_TEST=ON
2020-01-30 09:01:49 +00:00
```
2021-03-02 16:47:31 +00:00
### Build Options
| Name | Type | Default Value | Description |
| ---- | --- | ---- | ----- |
2021-04-18 20:09:32 +00:00
| `VK_BOOTSTRAP_WERROR` | bool | `OFF` | Enable warnings as errors during compilation. |
2021-03-02 16:47:31 +00:00
| `VK_BOOTSTRAP_TEST` | bool | `OFF` | Enable building of the tests in this project. Will download GLFW and Catch2 automatically if enabled. |
| `VK_BOOTSTRAP_VULKAN_HEADER_DIR` | string | `""` | Optional. Specify the directory that contains the Vulkan Headers. Useful if you are downloading the headers manually and don't want vk-bootstrap to download them itself. |