824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 1 | use crate::Error; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 2 | use crate::device::Shared; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 3 | use crate::format::{BYTES_PER_PIXEL, SUPPORTED}; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 4 | use crate::frame::{Op, VulkanFrame}; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 5 | use crate::image::{ |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 6 | ImageInner, Staging, VulkanImage, VulkanMapping, VulkanTarget, VulkanTexture, color_range, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 7 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 8 | use crate::pipeline::Pipelines; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 9 | use crate::sync::{FenceInner, VkFence}; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 10 | use ash::vk; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 11 | use gpu_allocator::MemoryLocation; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 12 | use smithay::backend::allocator::Fourcc; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 13 | use smithay::backend::renderer::sync::SyncPoint; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 14 | use smithay::backend::renderer::{ |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 15 | Bind, ContextId, DebugFlags, ExportMem, ImportMem, Offscreen, Renderer, RendererSuper, Texture, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 16 | TextureFilter, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 17 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 18 | use smithay::utils::{Buffer, Physical, Rectangle, Size, Transform}; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 19 | use std::fmt; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 20 | use std::sync::Arc; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 21 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 22 | struct Pending { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 23 | fence: Arc<FenceInner>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 24 | cmd: vk::CommandBuffer, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 25 | _keep: Vec<VulkanTexture>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 26 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 27 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 28 | pub struct VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 29 | pending: Vec<Pending>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 30 | pipelines: Pipelines, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 31 | pool: vk::CommandPool, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 32 | samplers: [vk::Sampler; 4], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 33 | min_filter: TextureFilter, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 34 | mag_filter: TextureFilter, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 35 | debug: DebugFlags, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 36 | context: ContextId<VulkanTexture>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 37 | shared: Arc<Shared>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 38 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 39 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 40 | impl fmt::Debug for VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 41 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 42 | f.debug_struct("VulkanRenderer") |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 43 | .field("device", &self.shared.device.name) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 44 | .finish() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 45 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 46 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 47 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 48 | impl VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 49 | pub fn new() -> Result<Self, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 50 | let shared = Arc::new(Shared::new()?); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 51 | let d = &shared.device.device; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 52 | let pool = unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 53 | d.create_command_pool( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 54 | &vk::CommandPoolCreateInfo::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 55 | .queue_family_index(shared.device.queue_family) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 56 | .flags(vk::CommandPoolCreateFlags::TRANSIENT), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 57 | None, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 58 | )? |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 59 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 60 | let filter = |f: TextureFilter| match f { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 61 | TextureFilter::Linear => vk::Filter::LINEAR, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 62 | TextureFilter::Nearest => vk::Filter::NEAREST, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 63 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 64 | let mut samplers = [vk::Sampler::null(); 4]; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 65 | for (i, s) in samplers.iter_mut().enumerate() { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 66 | let (min, mag) = (sampler_filter(i >> 1), sampler_filter(i & 1)); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 67 | *s = unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 68 | d.create_sampler( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 69 | &vk::SamplerCreateInfo::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 70 | .min_filter(filter(min)) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 71 | .mag_filter(filter(mag)) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 72 | .address_mode_u(vk::SamplerAddressMode::CLAMP_TO_EDGE) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 73 | .address_mode_v(vk::SamplerAddressMode::CLAMP_TO_EDGE) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 74 | .address_mode_w(vk::SamplerAddressMode::CLAMP_TO_EDGE), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 75 | None, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 76 | )? |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 77 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 78 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 79 | Ok(Self { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 80 | pending: Vec::new(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 81 | pipelines: Pipelines::new(&shared)?, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 82 | pool, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 83 | samplers, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 84 | min_filter: TextureFilter::Linear, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 85 | mag_filter: TextureFilter::Linear, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 86 | debug: DebugFlags::empty(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 87 | context: ContextId::new(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 88 | shared, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 89 | }) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 90 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 91 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 92 | /// Name of the physical device in use. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 93 | pub fn device_name(&self) -> &str { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 94 | &self.shared.device.name |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 95 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 96 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 97 | pub(crate) fn shared(&self) -> &Arc<Shared> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 98 | &self.shared |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 99 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 100 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 101 | pub(crate) fn context_id_ref(&self) -> &ContextId<VulkanTexture> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 102 | &self.context |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 103 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 104 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 105 | fn sampler(&self) -> vk::Sampler { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 106 | let idx = |f: TextureFilter| usize::from(f == TextureFilter::Nearest); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 107 | self.samplers[(idx(self.min_filter) << 1) | idx(self.mag_filter)] |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 108 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 109 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 110 | fn reap(&mut self) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 111 | let d = &self.shared.device.device; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 112 | let pool = self.pool; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 113 | self.pending.retain(|p| { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 114 | if !p.fence.signaled() { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 115 | return true; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 116 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 117 | unsafe { d.free_command_buffers(pool, &[p.cmd]) }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 118 | false |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 119 | }); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 120 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 121 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 122 | fn begin(&self) -> Result<vk::CommandBuffer, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 123 | let d = &self.shared.device.device; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 124 | let cmd = unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 125 | d.allocate_command_buffers( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 126 | &vk::CommandBufferAllocateInfo::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 127 | .command_pool(self.pool) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 128 | .level(vk::CommandBufferLevel::PRIMARY) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 129 | .command_buffer_count(1), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 130 | )?[0] |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 131 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 132 | unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 133 | d.begin_command_buffer( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 134 | cmd, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 135 | &vk::CommandBufferBeginInfo::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 136 | .flags(vk::CommandBufferUsageFlags::ONE_TIME_SUBMIT), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 137 | )? |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 138 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 139 | Ok(cmd) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 140 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 141 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 142 | fn end_and_submit(&self, cmd: vk::CommandBuffer) -> Result<Arc<FenceInner>, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 143 | let d = &self.shared.device.device; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 144 | unsafe { d.end_command_buffer(cmd)? }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 145 | let fence = FenceInner::new(&self.shared)?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 146 | let cmds = [vk::CommandBufferSubmitInfo::default().command_buffer(cmd)]; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 147 | let submit = vk::SubmitInfo2::default().command_buffer_infos(&cmds); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 148 | let queue = self |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 149 | .shared |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 150 | .device |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 151 | .queue |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 152 | .lock() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 153 | .unwrap_or_else(|e| e.into_inner()); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 154 | unsafe { d.queue_submit2(*queue, &[submit], fence.fence)? }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 155 | Ok(fence) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 156 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 157 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 158 | /// Records, submits and waits for a short command buffer. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 159 | fn one_shot( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 160 | &mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 161 | record: impl FnOnce(&ash::Device, vk::CommandBuffer), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 162 | ) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 163 | let cmd = self.begin()?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 164 | record(&self.shared.device.device, cmd); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 165 | let fence = self.end_and_submit(cmd)?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 166 | let res = fence.wait(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 167 | unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 168 | self.shared |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 169 | .device |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 170 | .device |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 171 | .free_command_buffers(self.pool, &[cmd]) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 172 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 173 | Ok(res?) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 174 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 175 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 176 | pub(crate) fn submit( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 177 | &mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 178 | target: &mut VulkanTarget<'_>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 179 | ops: Vec<Op>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 180 | ) -> Result<SyncPoint, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 181 | self.reap(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 182 | let image = target.0.0.clone(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 183 | let mut keep = vec![VulkanTexture(image.clone())]; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 184 | let mut transitions = Vec::new(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 185 | for op in &ops { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 186 | if let Op::Draw { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 187 | texture: Some(t), .. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 188 | } = op |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 189 | { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 190 | if t.0.layout() != vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 191 | transitions.push(t.clone()); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 192 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 193 | keep.push(t.clone()); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 194 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 195 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 196 | let mut pipelines = Vec::with_capacity(ops.len()); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 197 | for op in &ops { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 198 | pipelines.push(match op { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 199 | Op::Draw { kind, .. } => Some(self.pipelines.get(image.format, *kind)?), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 200 | Op::Clear { .. } => None, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 201 | }); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 202 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 203 | let cmd = self.begin()?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 204 | let d = &self.shared.device.device; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 205 | let push = &self.shared.device.push_descriptor; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 206 | let (w, h) = (image.width, image.height); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 207 | let sampler = self.sampler(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 208 | unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 209 | for t in &transitions { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 210 | barrier( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 211 | d, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 212 | cmd, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 213 | t.0.image, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 214 | t.0.layout(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 215 | vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 216 | ); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 217 | t.0.set_layout(vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 218 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 219 | barrier( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 220 | d, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 221 | cmd, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 222 | image.image, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 223 | image.layout(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 224 | vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 225 | ); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 226 | let attachments = [vk::RenderingAttachmentInfo::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 227 | .image_view(image.view) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 228 | .image_layout(vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 229 | .load_op(vk::AttachmentLoadOp::LOAD) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 230 | .store_op(vk::AttachmentStoreOp::STORE)]; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 231 | let area = vk::Rect2D { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 232 | offset: vk::Offset2D::default(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 233 | extent: vk::Extent2D { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 234 | width: w, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 235 | height: h, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 236 | }, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 237 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 238 | d.cmd_begin_rendering( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 239 | cmd, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 240 | &vk::RenderingInfo::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 241 | .render_area(area) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 242 | .layer_count(1) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 243 | .color_attachments(&attachments), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 244 | ); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 245 | d.cmd_set_viewport( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 246 | cmd, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 247 | 0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 248 | &[vk::Viewport { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 249 | x: 0.0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 250 | y: 0.0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 251 | width: w as f32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 252 | height: h as f32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 253 | min_depth: 0.0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 254 | max_depth: 1.0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 255 | }], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 256 | ); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 257 | let mut bound = vk::Pipeline::null(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 258 | for (op, pipeline) in ops.iter().zip(pipelines) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 259 | match op { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 260 | Op::Clear { color, rects } => { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 261 | let clear = [vk::ClearAttachment { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 262 | aspect_mask: vk::ImageAspectFlags::COLOR, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 263 | color_attachment: 0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 264 | clear_value: vk::ClearValue { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 265 | color: vk::ClearColorValue { float32: *color }, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 266 | }, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 267 | }]; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 268 | let rects: Vec<_> = rects |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 269 | .iter() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 270 | .map(|&rect| vk::ClearRect { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 271 | rect, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 272 | base_array_layer: 0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 273 | layer_count: 1, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 274 | }) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 275 | .collect(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 276 | d.cmd_clear_attachments(cmd, &clear, &rects); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 277 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 278 | Op::Draw { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 279 | texture, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 280 | push: constants, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 281 | scissors, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 282 | .. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 283 | } => { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 284 | let pipeline = pipeline.unwrap_or_default(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 285 | if pipeline != bound { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 286 | d.cmd_bind_pipeline(cmd, vk::PipelineBindPoint::GRAPHICS, pipeline); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 287 | bound = pipeline; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 288 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 289 | if let Some(t) = texture { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 290 | let info = [vk::DescriptorImageInfo::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 291 | .image_view(t.0.view) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 292 | .image_layout(vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 293 | .sampler(sampler)]; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 294 | let write = vk::WriteDescriptorSet::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 295 | .dst_binding(0) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 296 | .descriptor_type(vk::DescriptorType::COMBINED_IMAGE_SAMPLER) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 297 | .image_info(&info); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 298 | push.cmd_push_descriptor_set( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 299 | cmd, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 300 | vk::PipelineBindPoint::GRAPHICS, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 301 | self.pipelines.layout, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 302 | 0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 303 | &[write], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 304 | ); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 305 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 306 | d.cmd_push_constants( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 307 | cmd, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 308 | self.pipelines.layout, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 309 | vk::ShaderStageFlags::VERTEX | vk::ShaderStageFlags::FRAGMENT, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 310 | 0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 311 | constants.bytes(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 312 | ); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 313 | for s in scissors { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 314 | d.cmd_set_scissor(cmd, 0, &[*s]); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 315 | d.cmd_draw(cmd, 4, 1, 0, 0); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 316 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 317 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 318 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 319 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 320 | d.cmd_end_rendering(cmd); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 321 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 322 | image.set_layout(vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 323 | let fence = self.end_and_submit(cmd)?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 324 | self.pending.push(Pending { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 325 | fence: fence.clone(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 326 | cmd, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 327 | _keep: keep, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 328 | }); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 329 | Ok(SyncPoint::from(VkFence(fence))) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 330 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 331 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 332 | fn upload( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 333 | &mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 334 | image: &Arc<ImageInner>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 335 | data: &[u8], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 336 | region: Rectangle<i32, Buffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 337 | flipped: bool, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 338 | ) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 339 | let width = image.width as usize; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 340 | let needed = width * image.height as usize * BYTES_PER_PIXEL; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 341 | if data.len() < needed { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 342 | return Err(Error::BufferTooSmall { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 343 | needed, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 344 | got: data.len(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 345 | }); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 346 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 347 | let mut staging = Staging::new( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 348 | &self.shared, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 349 | needed, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 350 | vk::BufferUsageFlags::TRANSFER_SRC, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 351 | MemoryLocation::CpuToGpu, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 352 | )?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 353 | let row = width * BYTES_PER_PIXEL; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 354 | let rows = image.height as usize; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 355 | let dst = staging.bytes_mut(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 356 | for y in 0..rows { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 357 | let from = if flipped { rows - 1 - y } else { y }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 358 | dst[y * row..(y + 1) * row].copy_from_slice(&data[from * row..(from + 1) * row]); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 359 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 360 | let old = image.layout(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 361 | let copy = vk::BufferImageCopy::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 362 | .buffer_offset( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 363 | ((region.loc.y as usize * width + region.loc.x as usize) * BYTES_PER_PIXEL) as u64, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 364 | ) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 365 | .buffer_row_length(image.width) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 366 | .image_subresource( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 367 | vk::ImageSubresourceLayers::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 368 | .aspect_mask(vk::ImageAspectFlags::COLOR) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 369 | .layer_count(1), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 370 | ) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 371 | .image_offset(vk::Offset3D { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 372 | x: region.loc.x, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 373 | y: region.loc.y, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 374 | z: 0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 375 | }) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 376 | .image_extent(vk::Extent3D { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 377 | width: region.size.w as u32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 378 | height: region.size.h as u32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 379 | depth: 1, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 380 | }); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 381 | let (img, buffer) = (image.image, staging.buffer); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 382 | self.one_shot(|d, cmd| unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 383 | barrier(d, cmd, img, old, vk::ImageLayout::TRANSFER_DST_OPTIMAL); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 384 | d.cmd_copy_buffer_to_image( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 385 | cmd, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 386 | buffer, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 387 | img, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 388 | vk::ImageLayout::TRANSFER_DST_OPTIMAL, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 389 | &[copy], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 390 | ); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 391 | barrier( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 392 | d, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 393 | cmd, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 394 | img, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 395 | vk::ImageLayout::TRANSFER_DST_OPTIMAL, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 396 | vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 397 | ); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 398 | })?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 399 | image.set_layout(vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 400 | Ok(()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 401 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 402 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 403 | fn read_back( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 404 | &mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 405 | image: &Arc<ImageInner>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 406 | region: Rectangle<i32, Buffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 407 | fourcc: Fourcc, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 408 | ) -> Result<VulkanMapping, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 409 | let region = region |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 410 | .intersection(Rectangle::from_size( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 411 | (image.width as i32, image.height as i32).into(), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 412 | )) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 413 | .unwrap_or_default(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 414 | let (w, h) = (region.size.w.max(0) as u32, region.size.h.max(0) as u32); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 415 | let swap = needs_swizzle(image.fourcc, fourcc)?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 416 | let len = (w as usize * h as usize * BYTES_PER_PIXEL).max(BYTES_PER_PIXEL); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 417 | let staging = Staging::new( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 418 | &self.shared, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 419 | len, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 420 | vk::BufferUsageFlags::TRANSFER_DST, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 421 | MemoryLocation::GpuToCpu, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 422 | )?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 423 | if w > 0 && h > 0 { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 424 | let old = image.layout(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 425 | let copy = vk::BufferImageCopy::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 426 | .image_subresource( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 427 | vk::ImageSubresourceLayers::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 428 | .aspect_mask(vk::ImageAspectFlags::COLOR) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 429 | .layer_count(1), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 430 | ) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 431 | .image_offset(vk::Offset3D { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 432 | x: region.loc.x, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 433 | y: region.loc.y, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 434 | z: 0, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 435 | }) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 436 | .image_extent(vk::Extent3D { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 437 | width: w, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 438 | height: h, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 439 | depth: 1, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 440 | }); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 441 | let (img, buffer) = (image.image, staging.buffer); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 442 | self.one_shot(|d, cmd| unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 443 | barrier(d, cmd, img, old, vk::ImageLayout::TRANSFER_SRC_OPTIMAL); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 444 | d.cmd_copy_image_to_buffer( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 445 | cmd, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 446 | img, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 447 | vk::ImageLayout::TRANSFER_SRC_OPTIMAL, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 448 | buffer, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 449 | &[copy], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 450 | ); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 451 | })?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 452 | image.set_layout(vk::ImageLayout::TRANSFER_SRC_OPTIMAL); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 453 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 454 | let mut data = staging.bytes()[..w as usize * h as usize * BYTES_PER_PIXEL].to_vec(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 455 | if swap { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 456 | for px in data.as_chunks_mut::<BYTES_PER_PIXEL>().0 { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 457 | px.swap(0, 2); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 458 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 459 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 460 | Ok(VulkanMapping { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 461 | data, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 462 | width: w, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 463 | height: h, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 464 | fourcc, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 465 | }) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 466 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 467 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 468 | fn own(&self, image: &Arc<ImageInner>) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 469 | if Arc::ptr_eq(&image.shared, &self.shared) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 470 | Ok(()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 471 | } else { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 472 | Err(Error::ForeignTexture) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 473 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 474 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 475 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 476 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 477 | impl Drop for VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 478 | fn drop(&mut self) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 479 | let d = &self.shared.device.device; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 480 | unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 481 | let _ = d.device_wait_idle(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 482 | for p in self.pending.drain(..) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 483 | d.free_command_buffers(self.pool, &[p.cmd]); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 484 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 485 | for s in self.samplers { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 486 | d.destroy_sampler(s, None); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 487 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 488 | d.destroy_command_pool(self.pool, None); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 489 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 490 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 491 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 492 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 493 | fn sampler_filter(bit: usize) -> TextureFilter { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 494 | if bit == 0 { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 495 | TextureFilter::Linear |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 496 | } else { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 497 | TextureFilter::Nearest |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 498 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 499 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 500 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 501 | /// A full barrier between all earlier and later work on the image. Coarse, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 502 | /// but correct for every transition the renderer makes. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 503 | unsafe fn barrier( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 504 | d: &ash::Device, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 505 | cmd: vk::CommandBuffer, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 506 | image: vk::Image, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 507 | old: vk::ImageLayout, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 508 | new: vk::ImageLayout, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 509 | ) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 510 | let b = [vk::ImageMemoryBarrier2::default() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 511 | .src_stage_mask(vk::PipelineStageFlags2::ALL_COMMANDS) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 512 | .src_access_mask(vk::AccessFlags2::MEMORY_WRITE) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 513 | .dst_stage_mask(vk::PipelineStageFlags2::ALL_COMMANDS) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 514 | .dst_access_mask(vk::AccessFlags2::MEMORY_READ | vk::AccessFlags2::MEMORY_WRITE) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 515 | .old_layout(old) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 516 | .new_layout(new) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 517 | .image(image) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 518 | .subresource_range(color_range())]; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 519 | unsafe { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 520 | d.cmd_pipeline_barrier2( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 521 | cmd, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 522 | &vk::DependencyInfo::default().image_memory_barriers(&b), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 523 | ) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 524 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 525 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 526 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 527 | /// Whether reading `from` as `to` needs red and blue swapped. |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 528 | fn needs_swizzle(from: Fourcc, to: Fourcc) -> Result<bool, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 529 | let bgr = |f: Fourcc| match f { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 530 | Fourcc::Argb8888 | Fourcc::Xrgb8888 => Ok(true), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 531 | Fourcc::Abgr8888 | Fourcc::Xbgr8888 => Ok(false), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 532 | f => Err(Error::UnsupportedFormat(f)), |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 533 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 534 | Ok(bgr(from)? != bgr(to)?) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 535 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 536 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 537 | impl RendererSuper for VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 538 | type Error = Error; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 539 | type TextureId = VulkanTexture; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 540 | type Framebuffer<'buffer> = VulkanTarget<'buffer>; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 541 | type Frame<'frame, 'buffer> |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 542 | = VulkanFrame<'frame, 'buffer> |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 543 | where |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 544 | 'buffer: 'frame, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 545 | Self: 'frame; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 546 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 547 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 548 | impl Renderer for VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 549 | fn context_id(&self) -> ContextId<VulkanTexture> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 550 | self.context.clone() |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 551 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 552 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 553 | fn downscale_filter(&mut self, filter: TextureFilter) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 554 | self.min_filter = filter; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 555 | Ok(()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 556 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 557 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 558 | fn upscale_filter(&mut self, filter: TextureFilter) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 559 | self.mag_filter = filter; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 560 | Ok(()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 561 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 562 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 563 | fn set_debug_flags(&mut self, flags: DebugFlags) { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 564 | self.debug = flags; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 565 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 566 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 567 | fn debug_flags(&self) -> DebugFlags { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 568 | self.debug |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 569 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 570 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 571 | fn render<'frame, 'buffer>( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 572 | &'frame mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 573 | framebuffer: &'frame mut VulkanTarget<'buffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 574 | output_size: Size<i32, Physical>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 575 | dst_transform: Transform, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 576 | ) -> Result<VulkanFrame<'frame, 'buffer>, Error> |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 577 | where |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 578 | 'buffer: 'frame, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 579 | { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 580 | self.own(&framebuffer.0.0)?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 581 | self.reap(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 582 | Ok(VulkanFrame::new( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 583 | self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 584 | framebuffer, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 585 | output_size, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 586 | dst_transform, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 587 | )) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 588 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 589 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 590 | fn wait(&mut self, sync: &SyncPoint) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 591 | sync.wait().map_err(|_| Error::Interrupted) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 592 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 593 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 594 | fn cleanup_texture_cache(&mut self) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 595 | self.reap(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 596 | Ok(()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 597 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 598 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 599 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 600 | impl Bind<VulkanImage> for VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 601 | fn bind<'a>(&mut self, target: &'a mut VulkanImage) -> Result<VulkanTarget<'a>, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 602 | self.own(&target.0)?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 603 | Ok(VulkanTarget(target)) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 604 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 605 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 606 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 607 | impl Offscreen<VulkanImage> for VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 608 | fn create_buffer( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 609 | &mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 610 | format: Fourcc, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 611 | size: Size<i32, Buffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 612 | ) -> Result<VulkanImage, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 613 | if size.w <= 0 || size.h <= 0 { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 614 | return Err(Error::InvalidSize(size.w, size.h)); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 615 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 616 | let usage = vk::ImageUsageFlags::COLOR_ATTACHMENT |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 617 | | vk::ImageUsageFlags::SAMPLED |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 618 | | vk::ImageUsageFlags::TRANSFER_SRC |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 619 | | vk::ImageUsageFlags::TRANSFER_DST; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 620 | Ok(VulkanImage(ImageInner::create( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 621 | &self.shared, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 622 | format, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 623 | size.w as u32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 624 | size.h as u32, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 625 | usage, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 626 | )?)) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 627 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 628 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 629 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 630 | impl ImportMem for VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 631 | fn import_memory( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 632 | &mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 633 | data: &[u8], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 634 | format: Fourcc, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 635 | size: Size<i32, Buffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 636 | flipped: bool, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 637 | ) -> Result<VulkanTexture, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 638 | if size.w <= 0 || size.h <= 0 { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 639 | return Err(Error::InvalidSize(size.w, size.h)); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 640 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 641 | let usage = vk::ImageUsageFlags::SAMPLED |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 642 | | vk::ImageUsageFlags::TRANSFER_DST |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 643 | | vk::ImageUsageFlags::TRANSFER_SRC; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 644 | let image = ImageInner::create(&self.shared, format, size.w as u32, size.h as u32, usage)?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 645 | self.upload(&image, data, Rectangle::from_size(size), flipped)?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 646 | Ok(VulkanTexture(image)) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 647 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 648 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 649 | fn update_memory( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 650 | &mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 651 | texture: &VulkanTexture, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 652 | data: &[u8], |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 653 | region: Rectangle<i32, Buffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 654 | ) -> Result<(), Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 655 | self.own(&texture.0)?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 656 | let Some(region) = region.intersection(Rectangle::from_size(texture.size())) else { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 657 | return Ok(()); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 658 | }; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 659 | self.upload(&texture.0, data, region, false) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 660 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 661 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 662 | fn mem_formats(&self) -> Box<dyn Iterator<Item = Fourcc>> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 663 | Box::new(SUPPORTED.into_iter()) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 664 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 665 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 666 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 667 | impl ExportMem for VulkanRenderer { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 668 | type TextureMapping = VulkanMapping; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 669 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 670 | fn copy_framebuffer( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 671 | &mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 672 | target: &VulkanTarget<'_>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 673 | region: Rectangle<i32, Buffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 674 | format: Fourcc, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 675 | ) -> Result<VulkanMapping, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 676 | self.own(&target.0.0)?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 677 | let image = target.0.0.clone(); |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 678 | self.read_back(&image, region, format) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 679 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 680 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 681 | fn copy_texture( |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 682 | &mut self, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 683 | texture: &VulkanTexture, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 684 | region: Rectangle<i32, Buffer>, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 685 | format: Fourcc, |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 686 | ) -> Result<VulkanMapping, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 687 | self.own(&texture.0)?; |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 688 | self.read_back(&texture.0, region, format) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 689 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 690 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 691 | fn can_read_texture(&mut self, _texture: &VulkanTexture) -> Result<bool, Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 692 | Ok(true) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 693 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 694 | |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 695 | fn map_texture<'a>(&mut self, mapping: &'a VulkanMapping) -> Result<&'a [u8], Error> { |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 696 | Ok(&mapping.data) |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 697 | } |
824bf02b6e55 | atagen | 2026-10-16 09:15 UTC | 698 | } |