o
odinpkg.dev
packages / binding / odin-vk-bootstrap

odin-vk-bootstrap

0da1e4cbinding

A utility wrapper that jump starts initialization of Vulkan for Odin Language.

MIT · updated 3 days ago

Odin vk-bootstrap

Inspired by vk-bootstrap for C++, this is a utility library in Odin Language that jump starts initialization of Vulkan.

Read the Getting Started guide for a quick start on using odin-vk-bootstrap

Basic Usage

Just copy the vkbootstrap.odin file to your project.

import "core:fmt"
import vkb "."
import vk "vendor:vulkan"

main :: proc() {
    instance_builder: vkb.Instance_Builder
    vkb.instance_builder_init(&instance_builder)
    defer vkb.instance_builder_uninit(&instance_builder)

    // Require the minimum Vulkan api version 1.1
    vkb.instance_builder_require_api_version(&instance_builder, vk.API_VERSION_1_1)

    when ODIN_DEBUG {
        // Enable `VK_LAYER_KHRONOS_validation` layer
        vkb.instance_builder_enable_validation_layers(&instance_builder)

        // Enable debug reporting with a default messenger callback
        vkb.instance_builder_use_default_debug_messenger(&instance_builder)
    }

    vkb_instance: vkb.Instance
    vkb_instance_err := vkb.instance_builder_build(&instance_builder, &vkb_instance)
    if vkb_instance_err != nil {
        fmt.eprintfln("Failed to build instance: %#v", vkb_instance_err)
        return
    }
    defer vkb.destroy_instance(&vkb_instance)

    // Create a new physical device selector
    selector: vkb.Physical_Device_Selector
    vkb.physical_device_selector_init(&selector, vkb_instance)
    defer vkb.physical_device_selector_uninit(&selector)

    // We want a GPU that can render to current Window surface
    vkb.physical_device_selector_set_surface(&selector, /* from user created window*/)

    // Require a vulkan 1.1 capable device
    vkb.physical_device_selector_set_minimum_version(&selector, vk.API_VERSION_1_1)

    // Try to select a suitable device
    vkb_physical_device: vkb.Physical_Device
    vkb_physical_device_err := vkb.physical_device_selector_select(&selector, &vkb_physical_device)
    if vkb_physical_device_err != nil {
        fmt.eprintfln("Failed to select physical device: %#v", vkb_physical_device_err)
        return
    }
    // In Vulkan you don't need to destroy a physical device, but here you need
    // to free some resources when the physical device was created.
    defer vkb.destroy_physical_device(&vkb_physical_device)

    // Create a device builder
    device_builder: vkb.Device_Builder
    vkb.device_builder_init(&device_builder)
    defer vkb.device_builder_uninit(&device_builder)

    // Automatically propagate needed data from instance & physical device
    vkb_device: vkb.Device
    vkb_device_err := vkb.device_builder_build(&device_builder, &vkb_physical_device, &vkb_device)
    if vkb_device_err != nil {
        fmt.eprintfln("Failed to get logical device: %#v", vkb_device_err)
        return
    }
    defer vkb.destroy_device(&vkb_device)

    // Get the graphics queue with a helper function
    graphics_queue, graphics_queue_err := vkb.device_get_queue(&vkb_device, .Graphics)
    if graphics_queue_err != nil {
        fmt.eprintfln("Failed to get graphics queue: %#v", graphics_queue_err)
        return
    }
}

See Triangle Example for an example that renders a triangle to the screen.