oarust 0.8.6

GPU-first semantic computing engine written in Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
//! Device-owned swapchain resources, frame synchronization and presentation.

use std::{rc::Rc, sync::Arc};

use crate::runtime::{Device, RecordedCommandBuffer};
use crate::{Error, Result};

/// Vulkan values selected from one exact surface query for swapchain creation.
pub(in crate::runtime) struct SwapchainConfig {
	pub(in crate::runtime) extent: ash::vk::Extent2D,
	pub(in crate::runtime) image_count: u32,
	pub(in crate::runtime) format: ash::vk::SurfaceFormatKHR,
	pub(in crate::runtime) present_mode: ash::vk::PresentModeKHR,
	pub(in crate::runtime) composite_alpha: ash::vk::CompositeAlphaFlagsKHR,
	pub(in crate::runtime) pre_transform: ash::vk::SurfaceTransformFlagsKHR,
}

pub(in crate::runtime) struct PresentationSwapchain {
	_device: Arc<Device>,
	loader: ash::khr::swapchain::Device,
	handle: ash::vk::SwapchainKHR,
	extent: ash::vk::Extent2D,
	images: Vec<ash::vk::Image>,
	views: Vec<ash::vk::ImageView>,
	frames: Vec<PresentationFrameSync>,
	next_frame: usize,
}

struct PresentationFrameSync {
	image_available: ash::vk::Semaphore,
	render_finished: ash::vk::Semaphore,
	in_flight: ash::vk::Fence,
	command: Option<RecordedCommandBuffer>,
	source: Option<Rc<dyn std::any::Any>>,
}

const FRAMES_IN_FLIGHT: usize = 2;

impl PresentationSwapchain {
	pub(in crate::runtime) fn create(
		device: &Arc<Device>,
		surface: ash::vk::SurfaceKHR,
		present_queue_family: Option<u32>,
		config: SwapchainConfig,
	) -> Result<Self> {
		let loader = ash::khr::swapchain::Device::new(device.instance().raw(), device.raw());

		// Use CONCURRENT sharing when graphics and present are distinct families;
		// EXCLUSIVE otherwise (the common case).
		let graphics_family = device.physical().graphics_queue_family;
		let present_family = present_queue_family;
		let distinct_families = graphics_family
			.zip(present_family)
			.is_some_and(|(g, p)| g != p);
		let sharing_families: Vec<u32> = if distinct_families {
			[graphics_family, present_family]
				.iter()
				.filter_map(|f| *f)
				.collect()
		} else {
			vec![]
		};
		let (sharing_mode, queue_family_indices) = if distinct_families {
			(
				ash::vk::SharingMode::CONCURRENT,
				sharing_families.as_slice(),
			)
		} else {
			(ash::vk::SharingMode::EXCLUSIVE, [].as_slice())
		};
		let create_info = ash::vk::SwapchainCreateInfoKHR::default()
			.surface(surface)
			.min_image_count(config.image_count)
			.image_format(config.format.format)
			.image_color_space(config.format.color_space)
			.image_extent(config.extent)
			.image_array_layers(1)
			.image_usage(
				ash::vk::ImageUsageFlags::COLOR_ATTACHMENT | ash::vk::ImageUsageFlags::TRANSFER_DST,
			)
			.image_sharing_mode(sharing_mode)
			.queue_family_indices(queue_family_indices)
			.pre_transform(config.pre_transform)
			.composite_alpha(config.composite_alpha)
			.present_mode(config.present_mode)
			.clipped(true);
		let handle = unsafe { loader.create_swapchain(&create_info, None) }
			.map_err(|source| Error::backend_failure("Vulkan", "swapchain creation", source))?;
		let images = match unsafe { loader.get_swapchain_images(handle) } {
			Ok(images) => images,
			Err(source) => {
				unsafe { loader.destroy_swapchain(handle, None) };
				return Err(Error::backend_failure(
					"Vulkan",
					"swapchain image query",
					source,
				));
			}
		};
		let mut views = Vec::with_capacity(images.len());
		for image in &images {
			let view_info = ash::vk::ImageViewCreateInfo::default()
				.image(*image)
				.view_type(ash::vk::ImageViewType::TYPE_2D)
				.format(config.format.format)
				.subresource_range(
					ash::vk::ImageSubresourceRange::default()
						.aspect_mask(ash::vk::ImageAspectFlags::COLOR)
						.level_count(1)
						.layer_count(1),
				);
			match unsafe { device.raw().create_image_view(&view_info, None) } {
				Ok(view) => views.push(view),
				Err(source) => {
					for view in views {
						unsafe { device.raw().destroy_image_view(view, None) }
					}
					unsafe { loader.destroy_swapchain(handle, None) };
					return Err(Error::backend_failure(
						"Vulkan",
						"swapchain image-view creation",
						source,
					));
				}
			}
		}
		let mut frames = Vec::with_capacity(FRAMES_IN_FLIGHT);
		for _ in 0..FRAMES_IN_FLIGHT {
			let semaphore_info = ash::vk::SemaphoreCreateInfo::default();
			let image_available = match unsafe { device.raw().create_semaphore(&semaphore_info, None) } {
				Ok(semaphore) => semaphore,
				Err(source) => {
					destroy_partial_swapchain(device.raw(), &mut views, &mut frames, &loader, handle);
					return Err(Error::backend_failure(
						"Vulkan",
						"present image-available semaphore creation",
						source,
					));
				}
			};
			let render_finished = match unsafe { device.raw().create_semaphore(&semaphore_info, None) } {
				Ok(semaphore) => semaphore,
				Err(source) => {
					unsafe { device.raw().destroy_semaphore(image_available, None) };
					destroy_partial_swapchain(device.raw(), &mut views, &mut frames, &loader, handle);
					return Err(Error::backend_failure(
						"Vulkan",
						"present render-finished semaphore creation",
						source,
					));
				}
			};
			let fence_info =
				ash::vk::FenceCreateInfo::default().flags(ash::vk::FenceCreateFlags::SIGNALED);
			let in_flight = match unsafe { device.raw().create_fence(&fence_info, None) } {
				Ok(fence) => fence,
				Err(source) => {
					unsafe {
						device.raw().destroy_semaphore(render_finished, None);
						device.raw().destroy_semaphore(image_available, None);
					}
					destroy_partial_swapchain(device.raw(), &mut views, &mut frames, &loader, handle);
					return Err(Error::backend_failure(
						"Vulkan",
						"present in-flight fence creation",
						source,
					));
				}
			};
			frames.push(PresentationFrameSync {
				image_available,
				render_finished,
				in_flight,
				command: None,
				source: None,
			});
		}
		Ok(Self {
			_device: device.clone(),
			loader,
			handle,
			extent: config.extent,
			images,
			views,
			frames,
			next_frame: 0,
		})
	}
}

impl PresentationSwapchain {
	pub(in crate::runtime) fn is_valid(&self) -> bool {
		self.handle != ash::vk::SwapchainKHR::null()
	}

	pub(in crate::runtime) fn image_count(&self) -> usize {
		self.views.len()
	}

	pub(in crate::runtime) fn extent_px(&self) -> [u32; 2] {
		[self.extent.width, self.extent.height]
	}

	pub(in crate::runtime) fn frames_in_flight(&self) -> usize {
		self.frames.len()
	}

	pub(in crate::runtime) fn has_submitted_frames(&self) -> bool {
		self.frames.iter().any(|frame| frame.command.is_some())
	}

	pub(in crate::runtime) fn present_clear(&mut self, color: [f32; 4]) -> Result<bool> {
		let slot = self.next_frame;
		let frame = &mut self.frames[slot];
		unsafe {
			self
				._device
				.raw()
				.wait_for_fences(&[frame.in_flight], true, u64::MAX)
		}
		.map_err(|source| Error::backend_failure("Vulkan", "presentation fence wait", source))?;
		if let Some(command) = frame.command.take() {
			self._device.free(command);
		}
		frame.source = None;
		let (image_index, acquire_suboptimal) = match unsafe {
			self.loader.acquire_next_image(
				self.handle,
				u64::MAX,
				frame.image_available,
				ash::vk::Fence::null(),
			)
		} {
			Ok(acquired) => acquired,
			Err(ash::vk::Result::ERROR_OUT_OF_DATE_KHR) => return Ok(true),
			Err(source) => {
				return Err(Error::backend_failure(
					"Vulkan",
					"swapchain image acquisition",
					source,
				));
			}
		};
		let image = *self.images.get(image_index as usize).ok_or_else(|| {
			Error::invalid_argument("Vulkan returned an out-of-range swapchain image index")
		})?;
		unsafe { self._device.raw().reset_fences(&[frame.in_flight]) }
			.map_err(|source| Error::backend_failure("Vulkan", "presentation fence reset", source))?;
		let command = self._device.record_present_clear(image, color)?;
		self._device.submit_present(
			&command,
			frame.image_available,
			frame.render_finished,
			frame.in_flight,
		)?;
		frame.command = Some(command);
		let waits = [frame.render_finished];
		let chains = [self.handle];
		let indices = [image_index];
		let info = ash::vk::PresentInfoKHR::default()
			.wait_semaphores(&waits)
			.swapchains(&chains)
			.image_indices(&indices);
		let present_suboptimal = match unsafe {
			self
				.loader
				.queue_present(self._device.graphics_queue_handle()?, &info)
		} {
			Ok(suboptimal) => suboptimal,
			Err(ash::vk::Result::ERROR_OUT_OF_DATE_KHR) => true,
			Err(source) => {
				return Err(Error::backend_failure(
					"Vulkan",
					"queue presentation",
					source,
				));
			}
		};
		self.next_frame = (self.next_frame + 1) % self.frames.len();
		Ok(acquire_suboptimal || present_suboptimal)
	}

	pub(in crate::runtime) fn present_render_target(
		&mut self,
		source: ash::vk::Image,
		source_extent: [u32; 2],
		retention: Rc<dyn std::any::Any>,
	) -> Result<bool> {
		let slot = self.next_frame;
		let frame = &mut self.frames[slot];
		unsafe {
			self
				._device
				.raw()
				.wait_for_fences(&[frame.in_flight], true, u64::MAX)
		}
		.map_err(|source| Error::backend_failure("Vulkan", "presentation fence wait", source))?;
		if let Some(command) = frame.command.take() {
			self._device.free(command);
		}
		frame.source = None;
		let (image_index, acquire_suboptimal) = match unsafe {
			self.loader.acquire_next_image(
				self.handle,
				u64::MAX,
				frame.image_available,
				ash::vk::Fence::null(),
			)
		} {
			Ok(acquired) => acquired,
			Err(ash::vk::Result::ERROR_OUT_OF_DATE_KHR) => return Ok(true),
			Err(source) => {
				return Err(Error::backend_failure(
					"Vulkan",
					"swapchain image acquisition",
					source,
				));
			}
		};
		let target = *self.images.get(image_index as usize).ok_or_else(|| {
			Error::invalid_argument("Vulkan returned an out-of-range swapchain image index")
		})?;
		unsafe { self._device.raw().reset_fences(&[frame.in_flight]) }
			.map_err(|source| Error::backend_failure("Vulkan", "presentation fence reset", source))?;
		let command = self._device.record_present_blit(
			source,
			source_extent,
			target,
			[self.extent.width, self.extent.height],
		)?;
		self._device.submit_present(
			&command,
			frame.image_available,
			frame.render_finished,
			frame.in_flight,
		)?;
		frame.command = Some(command);
		frame.source = Some(retention);
		let waits = [frame.render_finished];
		let chains = [self.handle];
		let indices = [image_index];
		let info = ash::vk::PresentInfoKHR::default()
			.wait_semaphores(&waits)
			.swapchains(&chains)
			.image_indices(&indices);
		let present_suboptimal = match unsafe {
			self
				.loader
				.queue_present(self._device.graphics_queue_handle()?, &info)
		} {
			Ok(suboptimal) => suboptimal,
			Err(ash::vk::Result::ERROR_OUT_OF_DATE_KHR) => true,
			Err(source) => {
				return Err(Error::backend_failure(
					"Vulkan",
					"queue presentation",
					source,
				));
			}
		};
		self.next_frame = (self.next_frame + 1) % self.frames.len();
		Ok(acquire_suboptimal || present_suboptimal)
	}

	pub(in crate::runtime) fn present_texture(
		&mut self,
		texture: Rc<crate::runtime::NativeRgbaImage>,
	) -> Result<bool> {
		let slot = self.next_frame;
		let frame = &mut self.frames[slot];
		unsafe {
			self
				._device
				.raw()
				.wait_for_fences(&[frame.in_flight], true, u64::MAX)
		}
		.map_err(|source| Error::backend_failure("Vulkan", "presentation fence wait", source))?;
		if let Some(command) = frame.command.take() {
			self._device.free(command);
		}
		frame.source = None;
		let (image_index, acquire_suboptimal) = match unsafe {
			self.loader.acquire_next_image(
				self.handle,
				u64::MAX,
				frame.image_available,
				ash::vk::Fence::null(),
			)
		} {
			Ok(acquired) => acquired,
			Err(ash::vk::Result::ERROR_OUT_OF_DATE_KHR) => return Ok(true),
			Err(source) => {
				return Err(Error::backend_failure(
					"Vulkan",
					"swapchain image acquisition",
					source,
				));
			}
		};
		let image = *self.images.get(image_index as usize).ok_or_else(|| {
			Error::invalid_argument("Vulkan returned an out-of-range swapchain image index")
		})?;
		unsafe { self._device.raw().reset_fences(&[frame.in_flight]) }
			.map_err(|source| Error::backend_failure("Vulkan", "presentation fence reset", source))?;
		let command = self._device.record_present_blit(
			texture.handle,
			[texture.width, texture.height],
			image,
			[self.extent.width, self.extent.height],
		)?;
		self._device.submit_present(
			&command,
			frame.image_available,
			frame.render_finished,
			frame.in_flight,
		)?;
		frame.command = Some(command);
		frame.source = Some(texture);
		let waits = [frame.render_finished];
		let chains = [self.handle];
		let indices = [image_index];
		let info = ash::vk::PresentInfoKHR::default()
			.wait_semaphores(&waits)
			.swapchains(&chains)
			.image_indices(&indices);
		let present_suboptimal = match unsafe {
			self
				.loader
				.queue_present(self._device.graphics_queue_handle()?, &info)
		} {
			Ok(suboptimal) => suboptimal,
			Err(ash::vk::Result::ERROR_OUT_OF_DATE_KHR) => true,
			Err(source) => {
				return Err(Error::backend_failure(
					"Vulkan",
					"queue presentation",
					source,
				));
			}
		};
		self.next_frame = (self.next_frame + 1) % self.frames.len();
		Ok(acquire_suboptimal || present_suboptimal)
	}

	/// Compose the UI onto the swapchain in a single frame without a readback.
	///
	/// `record` fills one compute command buffer with UI dispatch work that
	/// writes into `compose_image` (RGBA8 STORAGE_IMAGE, UNDEFINED at entry).
	/// The swapchain acquire, compute barrier, blit, and present are submitted
	/// on the graphics queue as one serialized command buffer — no host readback.
	pub(in crate::runtime) fn present_compose(
		&mut self,
		compose_image: ash::vk::Image,
		compose_width: u32,
		compose_height: u32,
		descriptor_set: ash::vk::DescriptorSet,
		pipeline_layout: ash::vk::PipelineLayout,
		record: impl FnOnce(&ash::Device, ash::vk::CommandBuffer) -> Result<()>,
	) -> Result<bool> {
		let slot = self.next_frame;
		let frame = &mut self.frames[slot];
		unsafe {
			self
				._device
				.raw()
				.wait_for_fences(&[frame.in_flight], true, u64::MAX)
		}
		.map_err(|source| Error::backend_failure("Vulkan", "presentation fence wait", source))?;
		if let Some(command) = frame.command.take() {
			self._device.free(command);
		}
		frame.source = None;
		let (image_index, acquire_suboptimal) = match unsafe {
			self.loader.acquire_next_image(
				self.handle,
				u64::MAX,
				frame.image_available,
				ash::vk::Fence::null(),
			)
		} {
			Ok(acquired) => acquired,
			Err(ash::vk::Result::ERROR_OUT_OF_DATE_KHR) => return Ok(true),
			Err(source) => {
				return Err(Error::backend_failure(
					"Vulkan",
					"swapchain image acquisition",
					source,
				));
			}
		};
		let target = *self.images.get(image_index as usize).ok_or_else(|| {
			Error::invalid_argument("Vulkan returned an out-of-range swapchain image index")
		})?;
		unsafe { self._device.raw().reset_fences(&[frame.in_flight]) }
			.map_err(|source| Error::backend_failure("Vulkan", "presentation fence reset", source))?;

		let swapchain_extent = [self.extent.width, self.extent.height];
		let device = self._device.clone();

		let command = device.record_compute_commands(|cmd| {
			// Bind the shared bindless descriptor set.
			unsafe {
				device.raw().cmd_bind_descriptor_sets(
					cmd,
					ash::vk::PipelineBindPoint::COMPUTE,
					pipeline_layout,
					0,
					&[descriptor_set],
					&[],
				);
			}
			// Run UI compute dispatches (writes compose_image).
			record(device.raw(), cmd)?;

			let subresource_range = ash::vk::ImageSubresourceRange::default()
				.aspect_mask(ash::vk::ImageAspectFlags::COLOR)
				.level_count(1)
				.layer_count(1);
			let subresource_layers = ash::vk::ImageSubresourceLayers::default()
				.aspect_mask(ash::vk::ImageAspectFlags::COLOR)
				.layer_count(1);

			// Transition compose: GENERAL → TRANSFER_SRC
			// Transition swapchain target: UNDEFINED → TRANSFER_DST
			let compose_to_src = ash::vk::ImageMemoryBarrier2::default()
				.src_stage_mask(ash::vk::PipelineStageFlags2::COMPUTE_SHADER)
				.src_access_mask(ash::vk::AccessFlags2::SHADER_STORAGE_WRITE)
				.dst_stage_mask(ash::vk::PipelineStageFlags2::BLIT)
				.dst_access_mask(ash::vk::AccessFlags2::TRANSFER_READ)
				.old_layout(ash::vk::ImageLayout::GENERAL)
				.new_layout(ash::vk::ImageLayout::TRANSFER_SRC_OPTIMAL)
				.image(compose_image)
				.subresource_range(subresource_range);
			let swapchain_to_dst = ash::vk::ImageMemoryBarrier2::default()
				.src_stage_mask(ash::vk::PipelineStageFlags2::NONE)
				.dst_stage_mask(ash::vk::PipelineStageFlags2::BLIT)
				.dst_access_mask(ash::vk::AccessFlags2::TRANSFER_WRITE)
				.old_layout(ash::vk::ImageLayout::UNDEFINED)
				.new_layout(ash::vk::ImageLayout::TRANSFER_DST_OPTIMAL)
				.image(target)
				.subresource_range(subresource_range);
			unsafe {
				device.pipeline_barrier(
					cmd,
					&ash::vk::DependencyInfo::default()
						.image_memory_barriers(&[compose_to_src, swapchain_to_dst]),
				);
			}

			// Blit (with linear filter to handle DPI scaling).
			let blit = ash::vk::ImageBlit::default()
				.src_subresource(subresource_layers)
				.src_offsets([
					ash::vk::Offset3D::default(),
					ash::vk::Offset3D {
						x: compose_width as i32,
						y: compose_height as i32,
						z: 1,
					},
				])
				.dst_subresource(subresource_layers)
				.dst_offsets([
					ash::vk::Offset3D::default(),
					ash::vk::Offset3D {
						x: swapchain_extent[0] as i32,
						y: swapchain_extent[1] as i32,
						z: 1,
					},
				]);
			unsafe {
				device.raw().cmd_blit_image(
					cmd,
					compose_image,
					ash::vk::ImageLayout::TRANSFER_SRC_OPTIMAL,
					target,
					ash::vk::ImageLayout::TRANSFER_DST_OPTIMAL,
					&[blit],
					ash::vk::Filter::LINEAR,
				);
			}

			// Transition swapchain → PRESENT_SRC.
			let swapchain_to_present = ash::vk::ImageMemoryBarrier2::default()
				.src_stage_mask(ash::vk::PipelineStageFlags2::BLIT)
				.src_access_mask(ash::vk::AccessFlags2::TRANSFER_WRITE)
				.dst_stage_mask(ash::vk::PipelineStageFlags2::BOTTOM_OF_PIPE)
				.old_layout(ash::vk::ImageLayout::TRANSFER_DST_OPTIMAL)
				.new_layout(ash::vk::ImageLayout::PRESENT_SRC_KHR)
				.image(target)
				.subresource_range(subresource_range);
			unsafe {
				device.pipeline_barrier(
					cmd,
					&ash::vk::DependencyInfo::default()
						.image_memory_barriers(std::slice::from_ref(&swapchain_to_present)),
				);
			}
			Ok(())
		})?;

		self._device.submit_present(
			&command,
			frame.image_available,
			frame.render_finished,
			frame.in_flight,
		)?;
		frame.command = Some(command);

		let waits = [frame.render_finished];
		let chains = [self.handle];
		let indices = [image_index];
		let info = ash::vk::PresentInfoKHR::default()
			.wait_semaphores(&waits)
			.swapchains(&chains)
			.image_indices(&indices);
		let present_suboptimal = match unsafe {
			self
				.loader
				.queue_present(self._device.graphics_queue_handle()?, &info)
		} {
			Ok(suboptimal) => suboptimal,
			Err(ash::vk::Result::ERROR_OUT_OF_DATE_KHR) => true,
			Err(source) => {
				return Err(Error::backend_failure(
					"Vulkan",
					"queue presentation",
					source,
				));
			}
		};
		self.next_frame = (self.next_frame + 1) % self.frames.len();
		Ok(acquire_suboptimal || present_suboptimal)
	}

	pub(in crate::runtime) fn wait_idle(&mut self) -> Result<()> {
		unsafe { self._device.raw().device_wait_idle() }.map_err(|source| {
			Error::backend_failure("Vulkan", "presentation device-idle wait", source)
		})?;
		for frame in &mut self.frames {
			if let Some(command) = frame.command.take() {
				self._device.free(command);
			}
			frame.source = None;
		}
		Ok(())
	}
}

fn destroy_partial_swapchain(
	device: &ash::Device,
	views: &mut Vec<ash::vk::ImageView>,
	frames: &mut Vec<PresentationFrameSync>,
	loader: &ash::khr::swapchain::Device,
	handle: ash::vk::SwapchainKHR,
) {
	for frame in frames.drain(..) {
		unsafe {
			device.destroy_fence(frame.in_flight, None);
			device.destroy_semaphore(frame.render_finished, None);
			device.destroy_semaphore(frame.image_available, None);
		}
	}
	for view in views.drain(..) {
		unsafe { device.destroy_image_view(view, None) };
	}
	unsafe { loader.destroy_swapchain(handle, None) };
}

impl Drop for PresentationSwapchain {
	fn drop(&mut self) {
		debug_assert!(
			!self.has_submitted_frames(),
			"submitted presentation work must be retired by close_swapchain before destruction"
		);
		for frame in self.frames.drain(..) {
			unsafe {
				self._device.raw().destroy_fence(frame.in_flight, None);
				self
					._device
					.raw()
					.destroy_semaphore(frame.render_finished, None);
				self
					._device
					.raw()
					.destroy_semaphore(frame.image_available, None);
			}
		}
		for view in self.views.drain(..) {
			unsafe { self._device.raw().destroy_image_view(view, None) };
		}
		unsafe { self.loader.destroy_swapchain(self.handle, None) };
	}
}