ash-rs / ash

Vulkan bindings for Rust
Apache License 2.0
1.76k stars 186 forks source link

Add ExtensionMeta traits #913

Open Neo-Zhixing opened 2 months ago

Neo-Zhixing commented 2 months ago

Supersedes #912

Create new marker traits ExtensionMeta as defined below:

pub enum PromotionStatus {
    None,
    PromotedToCore(u32),
    PromotedToExtension(&'static core::ffi::CStr),
}

pub trait ExtensionMeta {
    const NAME: &'static core::ffi::CStr;
    const SPEC_VERSION: u32;
    const PROMOTION_STATUS: PromotionStatus;

    type Device;
    fn new_device(instance: &crate::Instance, device: &crate::Device) -> Self::Device;
    type Instance;
    fn new_instance(entry: &crate::Entry, instance: &crate::Instance) -> Self::Instance;
}

Add marker struct Meta in all extension mod root. ExtensionMeta trait is automatically implemented for all Meta marker structs.

When an extension does not have Instance or Device fp, the corresponding associated types on the trait will be defined as the unit type ().

Why is this useful?

Higher level abstractions may want to offer a way for users to enable extensions during application startup. Instead of asking for a &'static CStr extension name, we could instead ask for an impl ExtensionMeta which is significantly more idiomatic. This also gives us the promotion status of an extension, which allows the framework to behave differently depending on the core Vulkan version.

// During application startup
app.enable_extension<ash::khr::acceleration_structure::Meta>();

Unfortunately, we cannot implement traits for modules. That's why we need the extra Meta type that we added on module roots. These types give us a way to refer to the whole extension as a type.

Neo-Zhixing commented 2 months ago

One additional thing we can do: Exposing the meta types directly on ash::khr::*. So instead of ash::khr::acceleration_structure::Meta you can write ash::khr::AccelerationStructure. And as long as the ExtensionMeta trait is in scope, you can also write ash::khr::AccelerationStructure::Device

Neo-Zhixing commented 1 week ago

@Ralith @MarijnS95 any chances for this to be adopted?