ailloli_ui_render_vulkan/context.rs
1//! Host-owned Vulkan device and submission context.
2
3use ash::vk;
4
5/// Borrowed Vulkan objects supplied by the host runtime.
6///
7/// The renderer never creates a session or owns the presented images. The host is
8/// responsible for frame lifecycle and passes the active Vulkan device/queue
9/// objects here.
10///
11/// # Examples
12///
13/// ```no_run
14/// use ailloli_ui_render_vulkan::VulkanRenderContext;
15/// use ash::vk;
16///
17/// fn borrow_host<'a>(
18/// device: &'a ash::Device,
19/// queue: vk::Queue,
20/// family: u32,
21/// pool: vk::CommandPool,
22/// ) -> VulkanRenderContext<'a> {
23/// VulkanRenderContext::new(device, queue, family, pool)
24/// }
25/// ```
26pub struct VulkanRenderContext<'a> {
27 /// Logical device used to create and destroy renderer-owned resources.
28 pub device: &'a ash::Device,
29 /// Host queue used for renderer submissions.
30 pub queue: vk::Queue,
31 /// Index of the family owning [`Self::queue`].
32 pub queue_family_index: u32,
33 /// Host command pool used for transient frame command buffers.
34 pub command_pool: vk::CommandPool,
35 /// Physical-device memory types; required by rendering paths that allocate resources.
36 pub memory_properties: Option<&'a vk::PhysicalDeviceMemoryProperties>,
37}
38
39/// Constructors for borrowed host contexts.
40impl<'a> VulkanRenderContext<'a> {
41 /// Borrows device/queue objects without memory properties.
42 ///
43 /// This form is suitable only until a path needs buffer or image allocation;
44 /// [`VulkanRenderer::new`](crate::VulkanRenderer::new) then returns
45 /// [`VulkanRendererError::MissingMemoryProperties`](crate::VulkanRendererError::MissingMemoryProperties).
46 ///
47 /// # Examples
48 ///
49 /// ```no_run
50 /// use ailloli_ui_render_vulkan::VulkanRenderContext;
51 /// use ash::vk;
52 /// fn make<'a>(device: &'a ash::Device, queue: vk::Queue, pool: vk::CommandPool) {
53 /// let context = VulkanRenderContext::new(device, queue, 2, pool);
54 /// assert_eq!(context.queue_family_index, 2);
55 /// assert!(context.memory_properties.is_none());
56 /// }
57 /// ```
58 pub fn new(
59 device: &'a ash::Device,
60 queue: vk::Queue,
61 queue_family_index: u32,
62 command_pool: vk::CommandPool,
63 ) -> Self {
64 Self {
65 device,
66 queue,
67 queue_family_index,
68 command_pool,
69 memory_properties: None,
70 }
71 }
72
73 /// Borrows device/queue objects and physical-device memory properties.
74 ///
75 /// # Examples
76 ///
77 /// ```no_run
78 /// use ailloli_ui_render_vulkan::VulkanRenderContext;
79 /// use ash::vk;
80 /// fn make<'a>(
81 /// device: &'a ash::Device,
82 /// queue: vk::Queue,
83 /// pool: vk::CommandPool,
84 /// memory: &'a vk::PhysicalDeviceMemoryProperties,
85 /// ) {
86 /// let context = VulkanRenderContext::with_memory_properties(device, queue, 2, pool, memory);
87 /// assert!(context.memory_properties.is_some());
88 /// }
89 /// ```
90 pub fn with_memory_properties(
91 device: &'a ash::Device,
92 queue: vk::Queue,
93 queue_family_index: u32,
94 command_pool: vk::CommandPool,
95 memory_properties: &'a vk::PhysicalDeviceMemoryProperties,
96 ) -> Self {
97 Self {
98 device,
99 queue,
100 queue_family_index,
101 command_pool,
102 memory_properties: Some(memory_properties),
103 }
104 }
105}