Trait GlobalHooksInfo

Source
pub trait GlobalHooksInfo: Send + Sync {
    type HooksType: GlobalHooks;
    type HooksRefType<'a>: Deref<Target = Self::HooksType> + 'a
       where Self: 'a;

    // Required methods
    fn hooked_commands() -> &'static [VulkanCommand];
    fn hooks(&self) -> Self::HooksRefType<'_>;
}
Expand description

A trait for the layer implementation to provide metadata of GlobalHooks for the layer framework.

This trait serves as the container of the GlobalHooks type and provides the list of intercepted global commands for the layer framework. This trait and GlobalHooks can be implemented by the same type.

This trait can also be automatically implemented with the help of the auto_globalhooksinfo_impl attribute macro.

§Examples

A layer that needs to intercept the vkCreateInstance function.

struct MyLayerGlobalHooks;

impl GlobalHooks for MyLayerGlobalHooks {
    fn create_instance(
        &self,
        _p_create_info: &vk::InstanceCreateInfo,
        _layer_instance_link: &VkLayerInstanceLink,
        _p_allocator: Option<&vk::AllocationCallbacks>,
        _p_instance: *mut vk::Instance,
    ) -> LayerResult<ash::prelude::VkResult<()>> {
        LayerResult::Unhandled
    }
}

impl GlobalHooksInfo for MyLayerGlobalHooks {
    type HooksType = Self;
    type HooksRefType<'a> = &'a Self;

    fn hooked_commands() -> &'static [VulkanCommand] {
        &[VulkanCommand::CreateInstance]
    }

    fn hooks(&self) -> Self::HooksRefType<'_> {
        self
    }
}

Required Associated Types§

Source

type HooksType: GlobalHooks

The underlying GlobalHooks type that implements the core logic to intercept global commands.

If a type implements both this trait and the GlobalHooks trait simultaneously, GlobalHooksInfo::HooksType can be set to Self.

Source

type HooksRefType<'a>: Deref<Target = Self::HooksType> + 'a where Self: 'a

A type that can be dereferencing to GlobalHooksInfo::HooksType. Usually just &GlobalHooksInfo::HooksType.

This extra associated type allows the layer implementation to use a smart pointer like Arc or MutexGuard to hold the reference.

§Examples

A layer implementation that returns a Arc.

struct MyLayerGlobalHooksInfo(Arc<MyLayerGlobalHooks>);

impl GlobalHooksInfo for MyLayerGlobalHooksInfo {
    type HooksType = MyLayerGlobalHooks;
    type HooksRefType<'a> = Arc<MyLayerGlobalHooks>;

    // Other required methods omitted.

    fn hooks(&self) -> Self::HooksRefType<'_> {
        Arc::clone(&self.0)
    }
}

Required Methods§

Source

fn hooked_commands() -> &'static [VulkanCommand]

Returns a slice of global commands that the layer implementation needs to intercept.

Avoid returning commands other than global commands. Otherwise, those commands may or may not be intercepted.

Because this function is called in Global::default to initialize the singleton Global instance, implementations must not call into Global::default directly or indirectly including:

  • Layer::global_instance: this function will call Global::default under certain circumstances.
  • Any Global static methods: These functions may call into Layer::global_instance to obtain the global singleton.
  • Any Vulkan APIs exposed by the Vulkan loader: These functions will almost certainly trigger the layer initialization logic.
Source

fn hooks(&self) -> Self::HooksRefType<'_>

Returns the reference of the GlobalHooksInfo::HooksType.

The layer framework uses the returned value to call into the layer implementation of intercepted commands.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl GlobalHooksInfo for StubGlobalHooks

Source§

impl<T: TestLayerMock> GlobalHooksInfo for MockGlobalHooksInfo<T>

Available on crate feature _test only.
Source§

type HooksType = MockGlobalHooks

Source§

type HooksRefType<'a> = MutexGuard<'a, MockGlobalHooks>