de1e9f21296c | atagen | 2026-10-17 04:49 UTC | 1 | use crate::image::{ImageInner, color_range}; |
de1e9f21296c | atagen | 2026-10-17 04:49 UTC | 2 | use ash::vk; |
ad1f04ba8cb2 | atagen | 2026-10-19 07:30 UTC | 3 | use std::sync::Arc; |
ad1f04ba8cb2 | atagen | 2026-10-19 07:30 UTC | 4 | |
ad1f04ba8cb2 | atagen | 2026-10-19 07:30 UTC | 5 | /// Layout changes recorded into a command buffer that has not been submitted. |
ad1f04ba8cb2 | atagen | 2026-10-19 07:30 UTC | 6 | /// Images keep their old layout until `commit`, so a failed submit leaves the |
ad1f04ba8cb2 | atagen | 2026-10-19 07:30 UTC | 7 | /// tracking true to what the GPU has done. |
ad1f04ba8cb2 | atagen | 2026-10-19 07:30 UTC | 8 | #[derive(Default)] |
ad1f04ba8cb2 | atagen | 2026-10-19 07:30 UTC | 9 | pub(crate) struct LayoutLog(Vec<(Arc<ImageInner>, vk::ImageLayout)>); |
ad1f04ba8cb2 | atagen | 2026-10-19 07:30 UTC | 10 | |
ad1f04ba8cb2 | atagen | 2026-10-19 07:30 UTC | 11 | impl LayoutLog { |
ad1f04ba8cb2 | atagen | 2026-10-19 07:30 UTC | 12 | fn current(&self, image: &Arc<ImageInner>) -> vk::ImageLayout { |
ad1f04ba8cb2 | atagen | 2026-10-19 07:30 UTC | 13 | self.0 |
ad1f04ba8cb2 | atagen | 2026-10-19 07:30 UTC | 14 | .iter() |
ad1f04ba8cb2 | atagen | 2026-10-19 07:30 UTC | 15 | .rev() |
ad1f04ba8cb2 | atagen | 2026-10-19 07:30 UTC | 16 | .find(|(i, _)| Arc::ptr_eq(i, image)) |
ad1f04ba8cb2 | atagen | 2026-10-19 07:30 UTC | 17 | .map_or_else(|| image.layout(), |(_, layout)| *layout) |
ad1f04ba8cb2 | atagen | 2026-10-19 07:30 UTC | 18 | } |
ad1f04ba8cb2 | atagen | 2026-10-19 07:30 UTC | 19 | |
ad1f04ba8cb2 | atagen | 2026-10-19 07:30 UTC | 20 | pub fn commit(self) { |
ad1f04ba8cb2 | atagen | 2026-10-19 07:30 UTC | 21 | for (image, layout) in self.0 { |
ad1f04ba8cb2 | atagen | 2026-10-19 07:30 UTC | 22 | image.set_layout(layout); |
ad1f04ba8cb2 | atagen | 2026-10-19 07:30 UTC | 23 | } |
ad1f04ba8cb2 | atagen | 2026-10-19 07:30 UTC | 24 | } |
ad1f04ba8cb2 | atagen | 2026-10-19 07:30 UTC | 25 | } |
de1e9f21296c | atagen | 2026-10-17 04:49 UTC | 26 | |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 27 | struct Scope { |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 28 | stage: vk::PipelineStageFlags2, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 29 | access: vk::AccessFlags2, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 30 | } |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 31 | |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 32 | /// What an image may still be doing when it leaves `layout`. Layouts the |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 33 | /// renderer does not use itself, the general layout of dmabufs among them, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 34 | /// wait for everything. |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 35 | fn leaving(layout: vk::ImageLayout) -> Scope { |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 36 | let (stage, access) = match layout { |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 37 | vk::ImageLayout::UNDEFINED => (vk::PipelineStageFlags2::NONE, vk::AccessFlags2::NONE), |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 38 | vk::ImageLayout::TRANSFER_DST_OPTIMAL => ( |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 39 | vk::PipelineStageFlags2::TRANSFER, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 40 | vk::AccessFlags2::TRANSFER_WRITE, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 41 | ), |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 42 | vk::ImageLayout::TRANSFER_SRC_OPTIMAL => { |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 43 | (vk::PipelineStageFlags2::TRANSFER, vk::AccessFlags2::NONE) |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 44 | } |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 45 | vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL => ( |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 46 | vk::PipelineStageFlags2::FRAGMENT_SHADER, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 47 | vk::AccessFlags2::NONE, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 48 | ), |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 49 | vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL => ( |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 50 | vk::PipelineStageFlags2::COLOR_ATTACHMENT_OUTPUT, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 51 | vk::AccessFlags2::COLOR_ATTACHMENT_WRITE, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 52 | ), |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 53 | _ => ( |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 54 | vk::PipelineStageFlags2::ALL_COMMANDS, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 55 | vk::AccessFlags2::MEMORY_WRITE, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 56 | ), |
de1e9f21296c | atagen | 2026-10-17 04:49 UTC | 57 | }; |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 58 | Scope { stage, access } |
de1e9f21296c | atagen | 2026-10-17 04:49 UTC | 59 | } |
de1e9f21296c | atagen | 2026-10-17 04:49 UTC | 60 | |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 61 | /// What an image does once it is in `layout`. |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 62 | fn entering(layout: vk::ImageLayout) -> Scope { |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 63 | let (stage, access) = match layout { |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 64 | vk::ImageLayout::TRANSFER_DST_OPTIMAL => ( |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 65 | vk::PipelineStageFlags2::TRANSFER, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 66 | vk::AccessFlags2::TRANSFER_WRITE, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 67 | ), |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 68 | vk::ImageLayout::TRANSFER_SRC_OPTIMAL => ( |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 69 | vk::PipelineStageFlags2::TRANSFER, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 70 | vk::AccessFlags2::TRANSFER_READ, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 71 | ), |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 72 | vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL => ( |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 73 | vk::PipelineStageFlags2::FRAGMENT_SHADER, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 74 | vk::AccessFlags2::SHADER_SAMPLED_READ, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 75 | ), |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 76 | vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL => ( |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 77 | vk::PipelineStageFlags2::COLOR_ATTACHMENT_OUTPUT, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 78 | vk::AccessFlags2::COLOR_ATTACHMENT_READ | vk::AccessFlags2::COLOR_ATTACHMENT_WRITE, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 79 | ), |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 80 | _ => ( |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 81 | vk::PipelineStageFlags2::ALL_COMMANDS, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 82 | vk::AccessFlags2::MEMORY_READ | vk::AccessFlags2::MEMORY_WRITE, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 83 | ), |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 84 | }; |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 85 | Scope { stage, access } |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 86 | } |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 87 | |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 88 | /// The image barriers of one phase of a command buffer, recorded together. |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 89 | #[derive(Default)] |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 90 | pub(crate) struct Barriers(Vec<vk::ImageMemoryBarrier2<'static>>); |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 91 | |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 92 | impl Barriers { |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 93 | fn push( |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 94 | &mut self, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 95 | image: vk::Image, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 96 | (old, src_family): (vk::ImageLayout, u32), |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 97 | (new, dst_family): (vk::ImageLayout, u32), |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 98 | ) { |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 99 | let (from, to) = (leaving(old), entering(new)); |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 100 | self.0.push( |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 101 | vk::ImageMemoryBarrier2::default() |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 102 | .src_stage_mask(from.stage) |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 103 | .src_access_mask(from.access) |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 104 | .dst_stage_mask(to.stage) |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 105 | .dst_access_mask(to.access) |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 106 | .old_layout(old) |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 107 | .new_layout(new) |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 108 | .src_queue_family_index(src_family) |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 109 | .dst_queue_family_index(dst_family) |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 110 | .image(image) |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 111 | .subresource_range(color_range()), |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 112 | ); |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 113 | } |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 114 | |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 115 | /// Moves an image into `layout` for use on this queue. Dmabuf images come |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 116 | /// from the foreign queue family in the general layout, others continue |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 117 | /// from their tracked layout. |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 118 | pub fn acquire( |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 119 | &mut self, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 120 | family: u32, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 121 | image: &Arc<ImageInner>, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 122 | layout: vk::ImageLayout, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 123 | log: &mut LayoutLog, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 124 | ) { |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 125 | if image.external { |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 126 | self.push( |
de1e9f21296c | atagen | 2026-10-17 04:49 UTC | 127 | image.image, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 128 | (vk::ImageLayout::GENERAL, vk::QUEUE_FAMILY_FOREIGN_EXT), |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 129 | (layout, family), |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 130 | ); |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 131 | } else { |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 132 | let old = log.current(image); |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 133 | if old != layout { |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 134 | self.push( |
de1e9f21296c | atagen | 2026-10-17 04:49 UTC | 135 | image.image, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 136 | (old, vk::QUEUE_FAMILY_IGNORED), |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 137 | (layout, vk::QUEUE_FAMILY_IGNORED), |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 138 | ); |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 139 | log.0.push((image.clone(), layout)); |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 140 | } |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 141 | } |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 142 | } |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 143 | |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 144 | /// Hands a dmabuf image back to the foreign queue family after use. |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 145 | pub fn release(&mut self, family: u32, image: &ImageInner, layout: vk::ImageLayout) { |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 146 | if image.external { |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 147 | self.push( |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 148 | image.image, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 149 | (layout, family), |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 150 | (vk::ImageLayout::GENERAL, vk::QUEUE_FAMILY_FOREIGN_EXT), |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 151 | ); |
de1e9f21296c | atagen | 2026-10-17 04:49 UTC | 152 | } |
de1e9f21296c | atagen | 2026-10-17 04:49 UTC | 153 | } |
de1e9f21296c | atagen | 2026-10-17 04:49 UTC | 154 | |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 155 | pub unsafe fn record(self, d: &ash::Device, cmd: vk::CommandBuffer) { |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 156 | if self.0.is_empty() { |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 157 | return; |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 158 | } |
de1e9f21296c | atagen | 2026-10-17 04:49 UTC | 159 | unsafe { |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 160 | d.cmd_pipeline_barrier2( |
de1e9f21296c | atagen | 2026-10-17 04:49 UTC | 161 | cmd, |
d2f26399309b | atagen | 2026-10-19 07:32 UTC | 162 | &vk::DependencyInfo::default().image_memory_barriers(&self.0), |
de1e9f21296c | atagen | 2026-10-17 04:49 UTC | 163 | ) |
de1e9f21296c | atagen | 2026-10-17 04:49 UTC | 164 | }; |
de1e9f21296c | atagen | 2026-10-17 04:49 UTC | 165 | } |
de1e9f21296c | atagen | 2026-10-17 04:49 UTC | 166 | } |