### Complete Vulkan Initialization Flow Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/device-and-swapchain.md Demonstrates the full sequence for initializing Vulkan, from instance creation to swapchain setup. This example covers instance, surface, physical device, logical device, queues, and swapchain creation. ```cpp // 1. Create instance vkb::InstanceBuilder instance_builder; auto inst_ret = instance_builder .set_app_name("Example") .request_validation_layers() .use_default_debug_messenger() .build(); if (!inst_ret) { std::cerr << "Failed to create instance" << std::endl; return false; } vkb::Instance instance = inst_ret.value(); // 2. Create surface (with GLFW or other windowing library) VkSurfaceKHR surface = create_surface(instance); // 3. Select physical device vkb::PhysicalDeviceSelector selector(instance); auto phys_dev_ret = selector .set_surface(surface) .set_minimum_version(1, 2) .require_present(true) .select(); if (!phys_dev_ret) { std::cerr << "Failed to select physical device" << std::endl; return false; } vkb::PhysicalDevice physical_device = phys_dev_ret.value(); // 4. Create logical device vkb::DeviceBuilder device_builder(physical_device); auto dev_ret = device_builder.build(); if (!dev_ret) { std::cerr << "Failed to create device" << std::endl; return false; } vkb::Device device = dev_ret.value(); // 5. Get queues auto graphics_queue_ret = device.get_queue(vkb::QueueType::graphics); auto present_queue_ret = device.get_queue(vkb::QueueType::present); // 6. Create swapchain vkb::SwapchainBuilder swapchain_builder(device); auto swap_ret = swapchain_builder .set_desired_format({VK_FORMAT_B8G8R8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}) .set_desired_present_mode(VK_PRESENT_MODE_MAILBOX_KHR) .set_desired_extent(1920, 1080) .build(); if (!swap_ret) { std::cerr << "Failed to create swapchain" << std::endl; return false; } vkb::Swapchain swapchain = swap_ret.value(); // 7. Get swapchain images and create views auto images_ret = swapchain.get_images(); auto views_ret = swapchain.get_image_views(); std::vector image_views = views_ret.value(); ``` -------------------------------- ### Example: Default InstanceBuilder Construction Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/instance-builder.md Shows the basic instantiation of an InstanceBuilder using its default constructor. This is the simplest way to start configuring a Vulkan instance. ```cpp vkb::InstanceBuilder builder; ``` -------------------------------- ### Complete Vulkan Initialization Flow Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/overview.md This snippet demonstrates the complete initialization process using vk-bootstrap, including instance creation, physical device selection, logical device creation, queue retrieval, and swapchain setup. Ensure all necessary windowing system setup and surface creation are handled prior to device selection. ```cpp // 1. Check system capabilities (optional) auto sys_info_ret = vkb::SystemInfo::get_system_info(); // 2. Create instance vkb::InstanceBuilder instance_builder; auto inst_ret = instance_builder .set_app_name("MyApplication") .set_app_version(1, 0, 0) .require_api_version(1, 2) .request_validation_layers() .use_default_debug_messenger() .build(); if (!inst_ret) { std::cerr << "Instance creation failed" << std::endl; return false; } vkb::Instance instance = inst_ret.value(); // 3. Create surface (using windowing library) // VkSurfaceKHR surface = glfwCreateWindowSurface(instance, window, nullptr, &surface); // 4. Select physical device vkb::PhysicalDeviceSelector selector(instance); auto phys_ret = selector .set_surface(surface) .set_minimum_version(1, 2) .add_required_extension(VK_KHR_SWAPCHAIN_EXTENSION_NAME) .select(); if (!phys_ret) { std::cerr << "Device selection failed" << std::endl; for (const auto& reason : phys_ret.detailed_failure_reasons()) { std::cerr << " - " << reason << std::endl; } return false; } vkb::PhysicalDevice physical_device = phys_ret.value(); // 5. Create logical device vkb::DeviceBuilder device_builder(physical_device); auto dev_ret = device_builder.build(); if (!dev_ret) { std::cerr << "Device creation failed" << std::endl; return false; } vkb::Device device = dev_ret.value(); // 6. Get queues auto graphics_queue_ret = device.get_queue(vkb::QueueType::graphics); auto present_queue_ret = device.get_queue(vkb::QueueType::present); if (!graphics_queue_ret || !present_queue_ret) { std::cerr << "Failed to get queues" << std::endl; return false; } // 7. Create swapchain vkb::SwapchainBuilder swapchain_builder(device); auto swap_ret = swapchain_builder .set_desired_format({VK_FORMAT_B8G8R8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}) .set_desired_present_mode(VK_PRESENT_MODE_MAILBOX_KHR) .build(); if (!swap_ret) { std::cerr << "Swapchain creation failed" << std::endl; return false; } vkb::Swapchain swapchain = swap_ret.value(); // 8. Get swapchain images and image views auto images_ret = swapchain.get_images(); auto views_ret = swapchain.get_image_views(); if (!images_ret || !views_ret) { std::cerr << "Failed to get swapchain images" << std::endl; return false; } std::vector images = images_ret.value(); std::vector image_views = views_ret.value(); // Application can now render... // 9. Cleanup (reverse order) swapchain.destroy_image_views(image_views); vkb::destroy_swapchain(swapchain); vkb::destroy_device(device); vkb::destroy_surface(instance, surface); vkb::destroy_instance(instance); ``` -------------------------------- ### Find Code Examples Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/00-START-HERE.txt Use grep to find code examples in device-builder.md, showing the preceding two lines and limiting to the first 20 results. ```bash grep -B2 "^```cpp$" device-builder.md | head -20 ``` -------------------------------- ### Configure Example Header Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/example/CMakeLists.txt Configures a header file for examples using a template. This allows example-specific settings or defines to be generated based on the build configuration. ```cmake configure_file ( "${PROJECT_SOURCE_DIR}/example/example_config.h.in" "${CMAKE_CURRENT_BINARY_DIR}/example_config.h" ) ``` -------------------------------- ### CustomQueueDescription Examples Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/device-builder.md Examples demonstrating how to create CustomQueueDescription objects using different methods: from a vector, from an array, and utilizing move semantics. ```cpp // From vector std::vector prio = { 1.0f, 0.5f }; vkb::CustomQueueDescription desc1(0, prio); ``` ```cpp // From array float prio_array[] = { 1.0f, 0.5f }; vkb::CustomQueueDescription desc2(0, 2, prio_array); ``` ```cpp // Move semantics vkb::CustomQueueDescription desc3(0, std::vector{ 1.0f }); ``` -------------------------------- ### Install VkBootstrap Headers Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/CMakeLists.txt Installs the VkBootstrap header files to the system's include directory. ```cmake install(FILES src/VkBootstrap.h src/VkBootstrapDispatch.h src/VkBootstrapFeatureChain.h src/VkBootstrapFeatureChain.inl DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) ``` -------------------------------- ### Install Configuration Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/CMakeLists.txt Includes necessary CMake modules for installation, such as `GNUInstallDirs` and `CMakePackageConfigHelpers`, when `VK_BOOTSTRAP_INSTALL` is enabled. ```cmake if (VK_BOOTSTRAP_INSTALL) include(GNUInstallDirs) include(CMakePackageConfigHelpers) ``` -------------------------------- ### Example: Building a Vulkan Instance Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/instance-builder.md Demonstrates the typical usage of InstanceBuilder to set application name, request validation layers, and then build the Vulkan instance. Includes error handling for the build process. ```cpp vkb::InstanceBuilder builder; auto inst_ret = builder .set_app_name("My Application") .request_validation_layers() .build(); if (!inst_ret) { std::cerr << "Failed to create instance: " << inst_ret.error().message() << std::endl; return false; } vkb::Instance instance = inst_ret.value(); ``` -------------------------------- ### Custom Queue Setup with Vector Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/device-builder.md Specifies custom queue setup from a std::vector of queue descriptions. Returns a reference to the DeviceBuilder for chaining. ```cpp DeviceBuilder& custom_queue_setup(std::vector const& queue_descriptions); ``` -------------------------------- ### Device Creation with Custom Queue Setup Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/device-builder.md Illustrates creating a Vulkan device with custom queue priorities using `CustomQueueDescription` and the `custom_queue_setup` method of DeviceBuilder. It also shows how to manually retrieve queues after creation. ```cpp // Create 2 queues in family 0 with different priorities std::vector priorities = { 1.0f, 0.5f }; vkb::CustomQueueDescription queue_desc(0, priorities); vkb::DeviceBuilder device_builder(physical_device); auto dev_ret = device_builder .custom_queue_setup(1, &queue_desc) .build(); if (dev_ret) { vkb::Device device = dev_ret.value(); // Manually get queues using vkGetDeviceQueue VkQueue queue0, queue1; vkGetDeviceQueue(device, 0, 0, &queue0); vkGetDeviceQueue(device, 0, 1, &queue1); } ``` -------------------------------- ### Complete Instance Configuration Example Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/instance-builder.md Shows a comprehensive configuration of InstanceBuilder, including application and engine details, API version, validation layers, debug messenger, and headless mode. ```cpp // Complete configuration with validation vkb::InstanceBuilder builder; auto inst_ret = builder .set_app_name("MyApp") .set_app_version(1, 0, 0) .set_engine_name("MyEngine") .require_api_version(1, 3) .request_validation_layers() .use_default_debug_messenger() .set_headless(false) .build(); ``` -------------------------------- ### Find Error Handling Example Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/00-START-HERE.txt Use grep to find error handling examples within the errors.md file. ```bash grep -A5 "Error Handling Example" errors.md ``` -------------------------------- ### Advanced Queue Setup Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/physical-device.md Checks for the availability of dedicated compute and transfer queues and demonstrates how to find and utilize a compute-only queue family for custom queue setup. This is useful for optimizing performance by dedicating queues to specific tasks. ```cpp vkb::PhysicalDevice phys_dev = ...; // Check what kind of queues are available if (phys_dev.has_dedicated_compute_queue()) { std::cout << "Can use dedicated compute queue" << std::endl; } if (phys_dev.has_dedicated_transfer_queue()) { std::cout << "Can use dedicated transfer queue" << std::endl; } // Get queue families for custom queue setup auto queue_families = phys_dev.get_queue_families(); // Find compute-only family uint32_t compute_family = UINT32_MAX; for (size_t i = 0; i < queue_families.size(); ++i) { auto flags = queue_families[i].queueFlags; if ((flags & VK_QUEUE_COMPUTE_BIT) && !(flags & VK_QUEUE_GRAPHICS_BIT)) { compute_family = i; break; } } if (compute_family != UINT32_MAX) { std::cout << "Using compute-only queue family: " << compute_family << std::endl; } ``` -------------------------------- ### Example: Setting Application Name Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/instance-builder.md Demonstrates how to set the application name using the set_app_name method on an InstanceBuilder object. This is a common step before building the instance. ```cpp builder.set_app_name("MyGame"); ``` -------------------------------- ### Install VkBootstrap Targets Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/CMakeLists.txt Installs the vk-bootstrap and vk-bootstrap-compiler-warnings targets, along with export definitions for package management. ```cmake install( TARGETS vk-bootstrap vk-bootstrap-compiler-warnings EXPORT vk-bootstrap-targets INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ) ``` ```cmake install( EXPORT vk-bootstrap-targets DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/vk-bootstrap NAMESPACE vk-bootstrap:: ) ``` -------------------------------- ### Minimal Instance Creation Example Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/instance-builder.md Demonstrates the basic usage of InstanceBuilder to create a Vulkan instance with a specified application name and API version. Includes error handling for the build process. ```cpp // Minimal instance creation vkb::InstanceBuilder builder; auto inst_ret = builder .set_app_name("Example") .require_api_version(1, 2) .build(); if (!inst_ret) { std::cerr << "Error: " << inst_ret.error().message() << std::endl; return false; } vkb::Instance instance = inst_ret.value(); // Later, clean up vkb::destroy_instance(instance); ``` -------------------------------- ### Advanced Queue Setup Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/physical-device.md Explains how to check for dedicated queue types (compute, transfer) and how to find specific queue families for custom device queue configurations. ```APIDOC ## Advanced Queue Setup ### Description This example focuses on advanced queue management. It shows how to determine if the physical device offers dedicated compute or transfer queues, and how to iterate through available queue families to find specific types, such as a compute-only queue family, for custom device creation. ### Code Example ```cpp vkb::PhysicalDevice phys_dev = ...; // Check what kind of queues are available if (phys_dev.has_dedicated_compute_queue()) { std::cout << "Can use dedicated compute queue" << std::endl; } if (phys_dev.has_dedicated_transfer_queue()) { std::cout << "Can use dedicated transfer queue" << std::endl; } // Get queue families for custom queue setup auto queue_families = phys_dev.get_queue_families(); // Find compute-only family uint32_t compute_family = UINT32_MAX; for (size_t i = 0; i < queue_families.size(); ++i) { auto flags = queue_families[i].queueFlags; if ((flags & VK_QUEUE_COMPUTE_BIT) && !(flags & VK_QUEUE_GRAPHICS_BIT)) { compute_family = i; break; } } if (compute_family != UINT32_MAX) { std::cout << "Using compute-only queue family: " << compute_family << std::endl; } ``` ``` -------------------------------- ### Example: Constructing InstanceBuilder with Custom Loader Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/instance-builder.md Illustrates creating an InstanceBuilder instance by passing a custom Vulkan function loader. This is useful when you need explicit control over how Vulkan functions are resolved. ```cpp PFN_vkGetInstanceProcAddr custom_loader = ...; vkb::InstanceBuilder builder(custom_loader); ``` -------------------------------- ### Custom Queue Setup with Graphics Support Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/docs/getting_started.md Configure custom queue descriptions by iterating through physical device queue families and identifying those that support graphics operations. ```cpp std::vector queue_descriptions; auto queue_families = phys_device.get_queue_families (); for (uint32_t i = 0; i < static_cast(queue_families.size ()); i++) { if (queue_families[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) { // Find the first queue family with graphics operations supported queue_descriptions.push_back (vkb::CustomQueueDescription ( i, std::vector (queue_families[i].queueCount, 1.0f))); } } ``` -------------------------------- ### Error Handling Example for Physical Device Selection Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/errors.md Demonstrates how to select a physical device, handle potential errors, and check detailed failure reasons. Includes a fallback for when no surface is provided. ```cpp vkb::PhysicalDeviceSelector selector(instance); auto phys_dev_ret = selector .set_surface(surface) .set_minimum_version(1, 3) .add_required_extension(VK_KHR_SWAPCHAIN_EXTENSION_NAME) .select(); if (!phys_dev_ret) { auto error = phys_dev_ret.error(); std::cerr << "Physical device selection failed: " << error.error().message() << std::endl; // Check detailed reasons for (const auto& reason : phys_dev_ret.detailed_failure_reasons()) { std::cerr << " - " << reason << std::endl; } // Fallback: select without surface if (error.error() == vkb::PhysicalDeviceError::no_surface_provided) { auto alt_ret = selector.defer_surface_initialization().select(); // ... } return false; } ``` -------------------------------- ### Complete Device Creation from PhysicalDevice Selection Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/device-builder.md A comprehensive example showing the process from selecting a physical device using `PhysicalDeviceSelector` to building the final Vulkan device with `DeviceBuilder`. It includes setting surface, minimum version, and required extensions. ```cpp vkb::PhysicalDeviceSelector selector(instance); auto phys_dev_ret = selector .set_surface(surface) .set_minimum_version(1, 2) .add_required_extension(VK_KHR_SWAPCHAIN_EXTENSION_NAME) .select(); if (!phys_dev_ret) { std::cerr << "Failed to select physical device" return false; } vkb::PhysicalDevice physical_device = phys_dev_ret.value(); vkb::DeviceBuilder device_builder(physical_device); auto dev_ret = device_builder.build(); if (!dev_ret) { std::cerr << "Failed to create device: " << dev_ret.error().message() << std::endl; return false; } vkb::Device device = dev_ret.value(); ``` -------------------------------- ### custom_queue_setup(std::vector const&) Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/device-builder.md Specifies custom queue setup from a vector of queue descriptions. ```APIDOC ## custom_queue_setup(std::vector const&) ### Description Specifies custom queue setup from a vector of queue descriptions. ### Returns Reference to this DeviceBuilder for chaining ``` -------------------------------- ### Add Vulkan Example Executable Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/example/CMakeLists.txt Defines a function to create an executable for a Vulkan example. It links against the main vk-bootstrap library, compiler warnings, and Vulkan headers, and sets up include directories for shaders. ```cmake function(add_vulkan_example name) add_executable(vk-bootstrap-${name} ${name}.cpp) target_link_libraries(vk-bootstrap-${name} PRIVATE glfw vk-bootstrap vk-bootstrap-compiler-warnings Vulkan::Headers) target_include_directories(vk-bootstrap-${name} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) # path to build directory for shaders endfunction() ``` -------------------------------- ### custom_queue_setup(std::vector&&) Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/device-builder.md Specifies custom queue setup from a moved vector. ```APIDOC ## custom_queue_setup(std::vector&&) ### Description Specifies custom queue setup from a moved vector. ### Returns Reference to this DeviceBuilder for chaining ``` -------------------------------- ### Custom Queue Setup with Moved Vector Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/device-builder.md Specifies custom queue setup from a moved std::vector. Returns a reference to the DeviceBuilder for chaining. ```cpp DeviceBuilder& custom_queue_setup(std::vector&& queue_descriptions); ``` -------------------------------- ### DeviceBuilder Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/api-index.md Create a logical Vulkan device. Can be configured with custom queue setups, additional pNext structures, and allocation callbacks. ```APIDOC ## DeviceBuilder ### Description Create logical Vulkan device ### Constructor - `DeviceBuilder(PhysicalDevice)` ### Build - `build()` → `Result` ### Configuration - `custom_queue_setup(size_t, CustomQueueDescription const*)` / `custom_queue_setup(std::vector const&)` / `custom_queue_setup(std::vector&&)` - `add_pNext(void*)` - `set_allocation_callbacks(VkAllocationCallbacks*) ``` -------------------------------- ### Get Available Extensions Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/physical-device.md Returns a list of all extensions available on the physical device, irrespective of whether they are enabled. ```cpp std::vector get_available_extensions() const; ``` -------------------------------- ### Handle Device Creation Errors Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/errors.md Example of checking the result of `DeviceBuilder::build()` for errors and handling specific `DeviceError` types. ```cpp vkb::DeviceBuilder device_builder(physical_device); auto dev_ret = device_builder.build(); if (!dev_ret) { auto error = dev_ret.error(); if (error.error() == vkb::DeviceError::failed_create_device) { std::cerr << "Device creation failed (Vulkan error: " << error.vk_result() << ")" << std::endl; } else { std::cerr << "Device creation failed: " << error.error().message() << std::endl; } return false; } ``` -------------------------------- ### Physical Device Selection with Extension Features Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/physical-device-selector.md Demonstrates adding required Vulkan extension features to the PhysicalDeviceSelector. This example specifically enables buffer device address and descriptor indexing for Vulkan 1.2. ```cpp VkPhysicalDeviceVulkan12Features features12{}; features12.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES; features12.bufferDeviceAddress = VK_TRUE; features12.descriptorIndexing = VK_TRUE; auto phys_dev_ret = selector .add_required_extension_features(features12) .select(); ``` -------------------------------- ### Querying Device Information Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/physical-device.md Provides examples of how to query various information about a physical device, including its name, properties, queue families, and available extensions. ```APIDOC ## Querying Device Information ### Description This section illustrates how to retrieve detailed information about a selected physical device. It covers accessing the device's name, properties (like device type and driver version), listing all available queue families with their properties, and enumerating supported extensions. ### Code Example ```cpp vkb::PhysicalDevice phys_dev = ...; std::cout << "Device: " << phys_dev.name << std::endl; std::cout << "Type: " << phys_dev.properties.deviceType << std::endl; std::cout << "Driver: " << phys_dev.properties.driverVersion << std::endl; // List queue families auto queues = phys_dev.get_queue_families(); std::cout << "Queue families: " << queues.size() << std::endl; for (size_t i = 0; i < queues.size(); ++i) { std::cout << " [" << i << "] count=" << queues[i].queueCount << " flags=" << queues[i].queueFlags << std::endl; } // List available extensions auto exts = phys_dev.get_available_extensions(); std::cout << "Available extensions: " << exts.size() << std::endl; for (const auto& ext : exts) { std::cout << " - " << ext << std::endl; } ``` ``` -------------------------------- ### custom_queue_setup(size_t, CustomQueueDescription const*) Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/device-builder.md Specifies custom queue creation details. When used, the application is responsible for getting queues and queue indices instead of using the helper methods. ```APIDOC ## custom_queue_setup(size_t, CustomQueueDescription const*) ### Description Specifies custom queue creation details. When used, the application is responsible for getting queues and queue indices instead of using the helper methods. ### Parameters #### Path Parameters - **count** (size_t) - Required - Number of queue descriptions - **queue_descriptions** (CustomQueueDescription const*) - Required - Array of queue descriptions ### Notes `CustomQueueDescription` contains a queue family index and a vector of queue priorities (0.0 to 1.0). ### Example ```cpp std::vector priorities = { 1.0f, 0.5f }; vkb::CustomQueueDescription queue_desc(0, priorities); device_builder.custom_queue_setup(1, &queue_desc); ``` ### Returns Reference to this DeviceBuilder for chaining ``` -------------------------------- ### Get System Info with Default Loader Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/instance-and-system.md Queries system Vulkan capabilities using the default Vulkan loader. Ensure Vulkan is available on the system to avoid InstanceError::vulkan_unavailable. ```cpp auto sys_info_ret = vkb::SystemInfo::get_system_info(); if (!sys_info_ret) { std::cerr << "Failed to get system info" << std::endl; return false; } vkb::SystemInfo sys_info = sys_info_ret.value(); std::cout << "Vulkan version: " << sys_info.instance_api_version << std::endl; ``` -------------------------------- ### Handle Swapchain Creation Errors Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/errors.md Example of checking the result of `SwapchainBuilder::build()` for errors and handling specific `SwapchainError` types, including retrying with different parameters. ```cpp vkb::SwapchainBuilder swapchain_builder(device); auto swap_ret = swapchain_builder .set_desired_extent(1920, 1080) .set_required_min_image_count(3) .build(); if (!swap_ret) { auto error = swap_ret.error(); if (error.error() == vkb::SwapchainError::required_min_image_count_too_low) { std::cerr << "Surface does not support 3 images, trying 2..." << std::endl; auto retry = swapchain_builder .set_required_min_image_count(2) .build(); // ... } else if (error.error() == vkb::SwapchainError::surface_handle_not_provided) { std::cerr << "Surface not provided to swapchain builder" << std::endl; } else { std::cerr << "Swapchain creation failed: " << error.error().message() << std::endl; } return false; } ``` -------------------------------- ### Get Queue Family Properties Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/physical-device.md Retrieves all queue families supported by the physical device. Useful for advanced queue setup configurations. ```cpp std::vector get_queue_families() const; ``` ```cpp vkb::PhysicalDevice phys_dev = ...; auto queue_families = phys_dev.get_queue_families(); for (size_t i = 0; i < queue_families.size(); ++i) { std::cout << "Queue family " << i << ": " << queue_families[i].queueCount << " queues, " << "flags: " << queue_families[i].queueFlags << std::endl; } ``` -------------------------------- ### Get Swapchain Images and Create Image Views Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/device-and-swapchain.md Retrieves both the swapchain's Vulkan image handles and creates corresponding image views in a single operation. Remember to destroy the created image views later. ```cpp auto result = swapchain.get_images_and_image_views(); if (result) { auto [images, views] = result.value(); // Use images and views... // Cleanup swapchain.destroy_image_views(views); } ``` -------------------------------- ### Get Swapchain Images and Create Image Views (Custom pNext) Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/device-and-swapchain.md Retrieves swapchain images and creates image views with custom parameters specified through a `pNext` chain. ```cpp Result, std::vector>> get_images_and_image_views(const void* pNext); ``` -------------------------------- ### Custom Queue Setup with C-style Array Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/device-builder.md Specifies custom queue creation details using a C-style array of descriptions. When used, the application is responsible for getting queues and queue indices instead of using helper methods. `CustomQueueDescription` contains a queue family index and a vector of queue priorities (0.0 to 1.0). ```cpp DeviceBuilder& custom_queue_setup(size_t count, CustomQueueDescription const* queue_descriptions); ``` ```cpp std::vector priorities = { 1.0f, 0.5f }; vkb::CustomQueueDescription queue_desc(0, priorities); device_builder.custom_queue_setup(1, &queue_desc); ``` -------------------------------- ### Initialize Vulkan Instance, Physical Device, and Device Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/README.md This snippet demonstrates the basic usage of vk-bootstrap for initializing Vulkan. It covers creating an instance with validation layers and a debug messenger, selecting a physical device with specific requirements, and creating a logical device. Ensure Vulkan headers and necessary libraries are available. ```cpp #include "VkBootstrap.h" void init_vulkan () { vkb::InstanceBuilder builder; auto inst_ret = builder.set_app_name ("Example Vulkan Application") .request_validation_layers () .use_default_debug_messenger () .build (); if (!inst_ret) { /* report */ } vkb::Instance vkb_inst = inst_ret.value (); vkb::PhysicalDeviceSelector selector{ vkb_inst }; auto phys_ret = selector.set_surface (surface) .set_minimum_version (1, 1) .require_dedicated_transfer_queue () .select (); if (!phys_ret) { /* report */ } vkb::DeviceBuilder device_builder{ phys_ret.value () }; auto dev_ret = device_builder.build (); if (!dev_ret) { /* report */ } vkb::Device vkb_device = dev_ret.value (); auto graphics_queue_ret = vkb_device.get_queue (vkb::QueueType::graphics); if (!graphics_queue_ret) { /* report */ } VkQueue graphics_queue = graphics_queue_ret.value (); } ``` -------------------------------- ### Minimal Instance and Device Creation in C++ Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/overview.md This snippet demonstrates the basic steps to create a Vulkan instance and a logical device using vk-bootstrap. It includes requesting validation layers for debugging. Ensure you have the vk-bootstrap library included. ```cpp #include vkb::InstanceBuilder builder; auto inst_ret = builder .set_app_name("MyApp") .request_validation_layers() .build(); if (!inst_ret) return false; vkb::Instance instance = inst_ret.value(); vkb::PhysicalDeviceSelector selector(instance); auto phys_ret = selector.select(); if (!phys_ret) return false; vkb::DeviceBuilder device_builder(phys_ret.value()); auto dev_ret = device_builder.build(); if (!dev_ret) return false; vkb::Device device = dev_ret.value(); ``` -------------------------------- ### Simple Instance Creation with vk-bootstrap Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/docs/getting_started.md Shows how to create a Vulkan instance using vk-bootstrap's InstanceBuilder. Includes basic error checking and retrieval of the VkInstance handle. ```cpp vkb::InstanceBuilder instance_builder; auto instance_ret = instance_builder .set_app_name("Awesome Vulkan Application") .set_engine_name("Excellent Game Engine") .require_api_version(1,0,0) .build(); // build is always called last // simple error checking and helpful error messages if (!instance_ret) { std::cerr << "Failed to create Vulkan instance. Error: " << instance_ret.error().message() << "\n"; return -1; } // Get handle and use however you want! VkInstance instance = instance_ret.value(); ``` -------------------------------- ### Basic Device Creation with DeviceBuilder Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/device-builder.md Demonstrates the basic usage of DeviceBuilder to create a Vulkan device from a physical device. Includes error handling for the build process and retrieving a graphics queue. ```cpp vkb::PhysicalDevice physical_device = ...; vkb::DeviceBuilder device_builder(physical_device); auto dev_ret = device_builder.build(); if (!dev_ret) { std::cerr << "Failed to create device: " << dev_ret.error().message() << std::endl; return false; } vkb::Device device = dev_ret.value(); // Get queues auto graphics_queue_ret = device.get_queue(vkb::QueueType::graphics); if (graphics_queue_ret) { VkQueue graphics_queue = graphics_queue_ret.value(); } ``` -------------------------------- ### build() Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/instance-builder.md Creates and returns a Vulkan instance with the configured settings. ```APIDOC ## build() ### Description Creates and returns a Vulkan instance with the configured settings. ### Returns `Result` containing either an `Instance` object or an `Error` ### Error Conditions - `InstanceError::vulkan_unavailable` — Vulkan library not found - `InstanceError::vulkan_version_unavailable` — Required Vulkan version not available - `InstanceError::failed_create_instance` — VkCreateInstance failed - `InstanceError::failed_create_debug_messenger` — Debug messenger creation failed - `InstanceError::requested_layers_not_present` — A required layer is not available - `InstanceError::requested_extensions_not_present` — A required extension is not available ### Example ```cpp vkb::InstanceBuilder builder; auto inst_ret = builder.set_app_name("Example").build(); if (!inst_ret) { std::cerr << "Failed to create instance: " << inst_ret.error().message() << std::endl; return false; } vkb::Instance instance = inst_ret.value(); ``` ``` -------------------------------- ### Basic Vulkan Initialization Sequence Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/README.md Demonstrates the basic sequence for initializing Vulkan components: instance, physical device, logical device, and swapchain. Ensure to check the success of each step. ```cpp // Create instance vkb::InstanceBuilder builder; auto inst_ret = builder.set_app_name("MyApp").build(); if (!inst_ret) return false; // Select physical device vkb::PhysicalDeviceSelector selector(inst_ret.value()); auto phys_ret = selector.select(); if (!phys_ret) return false; // Create logical device vkb::DeviceBuilder device_builder(phys_ret.value()); auto dev_ret = device_builder.build(); if (!dev_ret) return false; // Create swapchain vkb::SwapchainBuilder swapchain_builder(dev_ret.value()); auto swap_ret = swapchain_builder.build(); if (!swap_ret) return false; ``` -------------------------------- ### Error Handling Example Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/errors.md This C++ example demonstrates how to handle potential QueueError exceptions when retrieving a graphics queue. It checks if the queue retrieval was successful and, if not, inspects the specific error to provide a more informative message. ```APIDOC ## Error Handling Example ```cpp auto graphics_queue_ret = device.get_queue(vkb::QueueType::graphics); if (!graphics_queue_ret) { auto error = graphics_queue_ret.error(); if (error.error() == vkb::QueueError::graphics_unavailable) { std::cerr << "Device has no graphics queue" << std::endl; } else { std::cerr << "Failed to get graphics queue: " << error.error().message() << std::endl; } return false; } VkQueue graphics_queue = graphics_queue_ret.value(); ``` ``` -------------------------------- ### get_queue_families() Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/physical-device.md Returns all queue families supported by the device. Useful for advanced queue setup. ```APIDOC ## get_queue_families() ### Description Returns all queue families supported by the device. ### Returns std::vector - Vector of `VkQueueFamilyProperties` structures ### Use Case Advanced queue setup with `DeviceBuilder::custom_queue_setup()`. ``` -------------------------------- ### Basic Physical Device Selection Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/physical-device-selector.md Demonstrates basic physical device selection using PhysicalDeviceSelector. Ensure the instance is created before using the selector. Handles potential selection failures. ```cpp vkb::PhysicalDeviceSelector selector(instance); auto phys_dev_ret = selector .set_surface(surface) .set_minimum_version(1, 2) .select(); if (!phys_dev_ret) { std::cerr << "Failed: " << phys_dev_ret.error().message() << std::endl; return false; } ``` -------------------------------- ### Get Swapchain Images Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/device-and-swapchain.md Retrieves all Vulkan image handles associated with the swapchain. Ensure the swapchain is valid before calling. ```cpp auto images_ret = swapchain.get_images(); if (images_ret) { std::vector images = images_ret.value(); std::cout << "Swapchain has " << images.size() << " images" << std::endl; } ``` -------------------------------- ### Create Vulkan Surface with GLFW or SDL2 Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/docs/getting_started.md Demonstrates how to create a Vulkan surface (VkSurfaceKHR) using windowing libraries like GLFW or SDL2 after a valid vk::Instance has been created. ```cpp vkb::Instance vkb_instance; //valid vkb::Instance VkSurfaceKHR surface = nullptr; // window is a valid library specific Window handle // GLFW VkResult err = glfwCreateWindowSurface (vkb_instance.instance, window, NULL, &surface); if (err != VK_SUCCESS) { /* handle error */ } // SDL2 SDL_bool err = SDL_Vulkan_CreateSurface(window, vkb_instance.instance, &surface); if (!err){ /* handle error */ } ``` -------------------------------- ### Get Enabled Extensions Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/physical-device.md Returns the list of extensions configured to be enabled on the device. These are extensions requested via `PhysicalDeviceSelector`. ```cpp std::vector get_extensions() const; ``` ```cpp auto extensions = phys_dev.get_extensions(); for (const auto& ext : extensions) { std::cout << "Will enable: " << ext << std::endl; } ``` -------------------------------- ### Basic Swapchain Creation Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/swapchain-builder.md Demonstrates the basic creation of a swapchain using SwapchainBuilder. Ensure you have a valid vkb::Device and set the desired format, present mode, and extent. ```cpp vkb::Device device = ...; vkb::SwapchainBuilder swapchain_builder(device); auto swap_ret = swapchain_builder .set_desired_format({VK_FORMAT_B8G8R8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}) .set_desired_present_mode(VK_PRESENT_MODE_MAILBOX_KHR) .set_desired_extent(1920, 1080) .build(); if (!swap_ret) { std::cerr << "Failed to create swapchain" << std::endl; return false; } vkb::Swapchain swapchain = swap_ret.value(); ``` -------------------------------- ### Get a Graphics Queue Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/docs/getting_started.md Retrieve a graphics queue from the device. Handles potential errors if the queue is not available or enabled. ```cpp auto queue_ret = vkb_device.get_queue (vkb::QueueType::graphics); if (!queue_ret) { // handle error } graphics_queue = queue_ret.value (); ``` -------------------------------- ### Instance Creation Error Handling Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/errors.md Demonstrates how to check for errors after attempting to build a Vulkan instance and handle specific error types like vulkan_unavailable or vulkan_version_unavailable. ```cpp vkb::InstanceBuilder builder; auto inst_ret = builder .set_app_name("MyApp") .require_api_version(1, 3) .request_validation_layers() .build(); if (!inst_ret) { auto error = inst_ret.error(); if (error.error() == vkb::InstanceError::vulkan_unavailable) { std::cerr << "Vulkan not installed on system" << std::endl; } else if (error.error() == vkb::InstanceError::vulkan_version_unavailable) { std::cerr << "Vulkan 1.3 not available" << std::endl; } else { std::cerr << "Instance creation failed: " << error.error().message() << std::endl; } return false; } ``` -------------------------------- ### Select Physical Device with vk-bootstrap Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/docs/getting_started.md Instantiate PhysicalDeviceSelector with a vkb::Instance and use select() to find a suitable GPU. Handles errors if no suitable device is found. ```cpp vkb::PhysicalDeviceSelector phys_device_selector(vkb_instance); // select() grabs a PhysicalDevice // By default, this will prefer a discrete GPU. auto physical_device_selector_return = phys_device_selector.require_things().select(); if (!physical_device_selector_return) { // If no suitable devices were found, detailed_failure_reasons() will contain a list of reasons why. if (physical_device_selector_return.error() == vkb::PhysicalDeviceError::no_suitable_device) { const auto& detailed_reasons = physical_device_selector_return.detailed_failure_reasons(); if (!detailed_reasons.empty()) { std::cerr << "GPU Selection failure reasons:\n"; for (const std::string& reason : detailed_reasons) { std::cerr << reason << "\n"; } } } /* handle error */ } ``` -------------------------------- ### Get Queue and Index Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/device-and-swapchain.md Retrieves both the `VkQueue` handle and its family index in a single call. This is efficient when both pieces of information are needed. ```cpp Result> get_queue_and_index(QueueType type) const; ``` ```cpp auto result = device.get_queue_and_index(vkb::QueueType::graphics); if (result) { auto [queue, index] = result.value(); // C++17 structured bindings } ``` -------------------------------- ### Physical Device Selection with Multiple Requirements Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/physical-device-selector.md Shows how to select a physical device with multiple requirements, including minimum Vulkan version, preferred device type, present mode, required extensions, and minimum device memory size. ```cpp vkb::PhysicalDeviceSelector selector(instance); auto phys_dev_ret = selector .set_surface(surface) .set_minimum_version(1, 3) .prefer_gpu_device_type(vkb::PreferredDeviceType::discrete) .require_present(true) .add_required_extension(VK_KHR_SWAPCHAIN_EXTENSION_NAME) .required_device_memory_size(2ULL * 1024ULL * 1024ULL * 1024ULL) // 2 GB .select(); ``` -------------------------------- ### Get Dedicated Queue and Index Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/device-and-swapchain.md Retrieves both a dedicated queue handle and its index. This method is restricted to compute or transfer queue types. ```cpp Result> get_dedicated_queue_and_index(QueueType type) const; ``` -------------------------------- ### Swapchain Recreation on Window Resize Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/swapchain-builder.md Shows how to recreate a swapchain when the window is resized. It's important to set the old swapchain and the new desired extent before building. ```cpp // When window is resized vkb::SwapchainBuilder swapchain_builder(device); auto new_swap_ret = swapchain_builder .set_old_swapchain(old_swapchain) .set_desired_extent(new_width, new_height) .build(); if (new_swap_ret) { vkb::destroy_swapchain(old_swapchain); swapchain = new_swap_ret.value(); } ``` -------------------------------- ### Get VkQueue Handle Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/device-and-swapchain.md Retrieves a `VkQueue` handle for a specified queue type. Use this when you need a direct handle to a queue for submitting commands. ```cpp Result get_queue(QueueType type) const; ``` ```cpp auto queue_ret = device.get_queue(vkb::QueueType::graphics); if (queue_ret) { VkQueue graphics_queue = queue_ret.value(); } ``` -------------------------------- ### Basic vk-bootstrap Build Pattern Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/docs/getting_started.md Demonstrates the general build pattern used by vk-bootstrap for creating Vulkan objects. Error handling is shown via the vkb::Result type. ```cpp vkb::Result result = vkb::WrapperBuilder() .set_thing() .more_things() .build(); if (!result) { /* handle error */ } // The result also holds the vk-bootstrap wrapper vkb::Wrapper wrapper = result.value(); // The underlying Vulkan handle can easily be acquired VkObject object = wrapper.object; ``` -------------------------------- ### DeviceBuilder::build() Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/device-builder.md Creates and returns a Vulkan logical device with the configured settings. Handles potential errors during device creation. ```APIDOC ## build() ### Description Creates and returns a Vulkan logical device with the configured settings. ### Returns `Result` containing either a `Device` object or an `Error` ### Error Conditions - `DeviceError::failed_create_device` — `vkCreateDevice` failed - `DeviceError::VkPhysicalDeviceFeatures2_in_pNext_chain_while_using_add_required_extension_features` — Invalid pNext configuration ### Request Example ```cpp auto dev_ret = device_builder.build(); if (!dev_ret) { std::cerr << "Error: " << dev_ret.error().message() << std::endl; return false; } vkb::Device device = dev_ret.value(); ``` ``` -------------------------------- ### Create Swapchain Image Views (Custom pNext) Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/device-and-swapchain.md Creates Vulkan image views for swapchain images, allowing for custom parameters via a `pNext` chain. Pass `nullptr` if no custom parameters are needed. ```cpp // Create views with a custom pNext structure if needed auto views_ret = swapchain.get_image_views(nullptr); ``` -------------------------------- ### Automatic Feature Propagation Example Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/README.md Demonstrates how features and extensions requested during PhysicalDeviceSelector are automatically enabled on the logical Device. No explicit action is needed on the DeviceBuilder. ```cpp // Selector requests features selector.add_required_extension_features(features12); // Device automatically inherits them device_builder.build(); // Features already enabled ``` -------------------------------- ### CustomQueueDescription Structure Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/device-builder.md Helper structure for specifying queue creation details in custom queue setup. Use this to define queue family index and priorities. ```cpp struct CustomQueueDescription { uint32_t index; std::vector priorities; }; ``` -------------------------------- ### Create Swapchain Image Views (Default) Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/device-and-swapchain.md Creates and returns Vulkan image views for all swapchain images using default parameters. These views must be explicitly destroyed using `destroy_image_views`. ```cpp auto views_ret = swapchain.get_image_views(); if (views_ret) { std::vector image_views = views_ret.value(); // Use image views... // Later, cleanup swapchain.destroy_image_views(image_views); } ``` -------------------------------- ### Get Queue Family Index Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/device-and-swapchain.md Retrieves the queue family index for a given queue type. Use this when you need the index to specify queues for operations. ```cpp Result get_queue_index(QueueType type) const; ``` ```cpp auto index_ret = device.get_queue_index(vkb::QueueType::graphics); if (index_ret) { uint32_t graphics_index = index_ret.value(); } ``` -------------------------------- ### Set Desired Surface Format Source: https://github.com/charles-lunarg/vk-bootstrap/blob/main/_autodocs/swapchain-builder.md Sets the primary format preference. This format will be used if supported. Example shows how to define and set a preferred RGBA format. ```cpp SwapchainBuilder& set_desired_format(VkSurfaceFormatKHR format); ``` ```cpp VkSurfaceFormatKHR format{}; format.format = VK_FORMAT_R8G8B8A8_SRGB; format.colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR; swapchain_builder.set_desired_format(format); ```