1use ash::{
2 khr,
3 vk::{self},
4};
5use openxr as xr;
6use std::{mem, num::NonZeroU32, path::PathBuf, ptr, sync::Mutex};
7
8mod command;
9mod descriptor;
10mod init;
11mod pipeline;
12mod resource;
13mod surface;
14
15struct VulkanInstance {
17 pub entry: ash::Entry,
18 pub instance: Instance,
19 pub driver_api_version: u32,
20}
21
22const QUERY_POOL_SIZE: usize = crate::limits::PASS_COUNT + 1;
23const MAX_XR_EYES: usize = 2;
24
25struct Instance {
26 core: ash::Instance,
27 _debug_utils: ash::ext::debug_utils::Instance,
28 get_physical_device_properties2: khr::get_physical_device_properties2::Instance,
29 cooperative_matrix: khr::cooperative_matrix::Instance,
30 get_surface_capabilities2: Option<khr::get_surface_capabilities2::Instance>,
31 surface: Option<khr::surface::Instance>,
32}
33
34#[derive(Clone)]
35struct RayTracingDevice {
36 acceleration_structure: khr::acceleration_structure::Device,
37 scratch_buffer_alignment: u64,
38}
39
40#[derive(Clone, Default)]
41struct CommandScopeDevice {}
42#[derive(Clone, Default)]
43struct TimingDevice {
44 period: f32,
45}
46
47#[derive(Clone)]
48struct Workarounds {
49 extra_sync_src_access: vk::AccessFlags,
50 extra_sync_dst_access: vk::AccessFlags,
51 extra_descriptor_pool_create_flags: vk::DescriptorPoolCreateFlags,
52}
53
54#[derive(Clone)]
55struct Device {
56 core: ash::Device,
57 device_information: crate::DeviceInformation,
58 swapchain: Option<khr::swapchain::Device>,
59 debug_utils: ash::ext::debug_utils::Device,
60 timeline_semaphore: khr::timeline_semaphore::Device,
61 dynamic_rendering: khr::dynamic_rendering::Device,
62 ray_tracing: Option<RayTracingDevice>,
63 buffer_device_address: bool,
64 inline_uniform_blocks: bool,
65 buffer_marker: Option<ash::amd::buffer_marker::Device>,
66 shader_info: Option<ash::amd::shader_info::Device>,
67 full_screen_exclusive: Option<ash::ext::full_screen_exclusive::Device>,
68 #[cfg(target_os = "windows")]
69 external_memory: Option<ash::khr::external_memory_win32::Device>,
70 #[cfg(not(target_os = "windows"))]
71 external_memory: Option<ash::khr::external_memory_fd::Device>,
72 command_scope: Option<CommandScopeDevice>,
73 timing: Option<TimingDevice>,
74 workarounds: Workarounds,
75}
76
77struct MemoryManager {
78 allocator: gpu_alloc::GpuAllocator<vk::DeviceMemory>,
79 slab: slab::Slab<gpu_alloc::MemoryBlock<vk::DeviceMemory>>,
80 valid_ash_memory_types: u32,
81}
82
83struct Queue {
84 raw: vk::Queue,
85 timeline_semaphore: vk::Semaphore,
86 last_progress: u64,
87}
88
89#[derive(Clone, Copy, Debug, Default, PartialEq)]
90struct InternalFrame {
91 acquire_semaphore: vk::Semaphore,
92 present_semaphore: vk::Semaphore,
93 image: vk::Image,
94 view: vk::ImageView,
95 xr_views: [vk::ImageView; MAX_XR_EYES],
96}
97
98#[derive(Clone, Copy, Debug, PartialEq)]
99struct Swapchain {
100 raw: vk::SwapchainKHR,
101 format: crate::TextureFormat,
102 alpha: crate::AlphaMode,
103 target_size: [u16; 2],
104}
105
106pub struct Surface {
107 device: khr::swapchain::Device,
108 raw: vk::SurfaceKHR,
109 frames: Vec<InternalFrame>,
110 next_semaphore: vk::Semaphore,
111 swapchain: Swapchain,
112 full_screen_exclusive: bool,
113}
114
115pub struct XrSurface {
116 raw: openxr::Swapchain<openxr::Vulkan>,
117 frames: Vec<InternalFrame>,
118 swapchain: Swapchain,
119 view_count: u32,
120}
121
122pub struct XrSessionState {
123 pub instance: xr::Instance,
124 pub system_id: xr::SystemId,
125 pub session: xr::Session<xr::Vulkan>,
126 pub frame_wait: xr::FrameWaiter,
127 pub frame_stream: xr::FrameStream<xr::Vulkan>,
128 pub view_type: xr::ViewConfigurationType,
129 pub environment_blend_mode: xr::EnvironmentBlendMode,
130 pub space: Option<xr::Space>,
131 pub predicted_display_time: Option<xr::Time>,
132}
133
134#[derive(Clone, Copy, Debug)]
135enum Presentation {
136 Window {
137 swapchain: vk::SwapchainKHR,
138 image_index: u32,
139 acquire_semaphore: vk::Semaphore,
140 present_semaphore: vk::Semaphore,
141 },
142 Xr {
143 swapchain: usize,
144 view_count: u32,
145 target_size: [u16; 2],
146 views: [XrView; MAX_XR_EYES],
147 },
148}
149
150#[derive(Clone, Copy, Debug, Default)]
151pub struct XrPose {
152 pub orientation: [f32; 4],
153 pub position: [f32; 3],
154}
155
156#[derive(Clone, Copy, Debug, Default)]
157pub struct XrFov {
158 pub angle_left: f32,
159 pub angle_right: f32,
160 pub angle_up: f32,
161 pub angle_down: f32,
162}
163
164#[derive(Clone, Copy, Debug, Default)]
165pub struct XrView {
166 pub pose: XrPose,
167 pub fov: XrFov,
168}
169
170#[derive(Clone, Copy, Debug)]
171pub struct Frame {
172 swapchain: Swapchain,
173 image_index: Option<u32>,
174 internal: InternalFrame,
175 xr_swapchain: usize,
176 xr_view_count: u32,
177 xr_views: [XrView; MAX_XR_EYES],
178}
179
180impl Frame {
181 pub fn texture(&self) -> Texture {
182 Texture {
183 raw: self.internal.image,
184 memory_handle: !0,
185 target_size: self.swapchain.target_size,
186 format: self.swapchain.format,
187 external: None,
188 }
189 }
190
191 pub fn texture_view(&self) -> TextureView {
192 TextureView {
193 raw: self.internal.view,
194 target_size: self.swapchain.target_size,
195 aspects: crate::TexelAspects::COLOR,
196 }
197 }
198
199 pub fn xr_texture_view(&self, eye: u32) -> TextureView {
200 let eye = eye as usize;
201 assert!(eye < MAX_XR_EYES, "XR eye {} is out of range", eye);
202 let raw = self.internal.xr_views[eye];
203 assert_ne!(
204 raw,
205 vk::ImageView::null(),
206 "XR eye {} view is not initialized",
207 eye
208 );
209 TextureView {
210 raw,
211 target_size: self.swapchain.target_size,
212 aspects: crate::TexelAspects::COLOR,
213 }
214 }
215
216 pub fn xr_view_count(&self) -> u32 {
217 self.xr_view_count
218 }
219
220 pub fn xr_view(&self, eye: u32) -> XrView {
221 let eye = eye as usize;
222 assert!(
223 eye < self.xr_view_count as usize,
224 "XR eye {} is out of range",
225 eye
226 );
227 self.xr_views[eye]
228 }
229}
230
231impl Context {
232 pub fn xr_session(&self) -> Option<xr::Session<xr::Vulkan>> {
233 self.xr
234 .as_ref()
235 .map(|xr| xr.lock().unwrap().session.clone())
236 }
237
238 pub fn xr_locate_space(&self, action_space: &xr::Space) -> Option<xr::Posef> {
241 let xr = self.xr.as_ref()?.lock().unwrap();
242 let time = xr.predicted_display_time?;
243 let ref_space = xr.space.as_ref()?;
244 let location = action_space.locate(ref_space, time).ok()?;
245 let flags = location.location_flags;
246 if flags.contains(
247 xr::SpaceLocationFlags::POSITION_VALID | xr::SpaceLocationFlags::ORIENTATION_VALID,
248 ) {
249 Some(location.pose)
250 } else {
251 None
252 }
253 }
254}
255
256fn map_timeout(millis: u32) -> u64 {
257 if millis == !0 {
258 !0
259 } else {
260 millis as u64 * 1_000_000
261 }
262}
263
264pub struct Context {
265 memory: Mutex<MemoryManager>,
266 device: Device,
267 queue_family_index: u32,
268 queue: Mutex<Queue>,
269 physical_device: vk::PhysicalDevice,
270 naga_flags: naga::back::spv::WriterFlags,
271 shader_debug_path: Option<PathBuf>,
272 min_buffer_alignment: u64,
273 min_uniform_buffer_offset_alignment: u64,
274 sample_count_flags: vk::SampleCountFlags,
275 dual_source_blending: bool,
276 shader_float16: bool,
277 cooperative_matrix: crate::CooperativeMatrix,
278 binding_array: bool,
279 memory_budget: bool,
280 inner: VulkanInstance,
281 xr: Option<Mutex<XrSessionState>>,
282}
283
284#[derive(Clone, Copy, Debug, Hash, PartialEq)]
285pub struct Buffer {
286 raw: vk::Buffer,
287 memory_handle: usize,
288 mapped_data: *mut u8,
289 size: u64,
290 external: Option<crate::ExternalMemorySource>,
291}
292
293impl Default for Buffer {
294 fn default() -> Self {
295 Self {
296 raw: vk::Buffer::null(),
297 memory_handle: !0,
298 mapped_data: ptr::null_mut(),
299 size: 0,
300 external: None,
301 }
302 }
303}
304
305impl Buffer {
306 pub fn data(&self) -> *mut u8 {
307 self.mapped_data
308 }
309
310 pub fn size(&self) -> u64 {
311 self.size
312 }
313}
314
315unsafe impl Send for Buffer {}
316unsafe impl Sync for Buffer {}
317
318#[derive(Clone, Copy, Debug, Hash, PartialEq)]
319pub struct Texture {
320 raw: vk::Image,
321 memory_handle: usize,
322 target_size: [u16; 2],
323 format: crate::TextureFormat,
324 external: Option<crate::ExternalMemorySource>,
325}
326
327impl Default for Texture {
328 fn default() -> Self {
329 Self {
330 raw: vk::Image::default(),
331 memory_handle: !0,
332 target_size: [0; 2],
333 format: crate::TextureFormat::Rgba8Unorm,
334 external: None,
335 }
336 }
337}
338
339#[derive(Clone, Copy, Debug, Default, Hash, PartialEq)]
340pub struct TextureView {
341 raw: vk::ImageView,
342 target_size: [u16; 2],
343 aspects: crate::TexelAspects,
344}
345
346#[derive(Clone, Copy, Debug, Hash, PartialEq)]
347pub struct Sampler {
348 raw: vk::Sampler,
349}
350
351#[derive(Clone, Copy, Debug, Default, Hash, PartialEq)]
352pub struct AccelerationStructure {
353 raw: vk::AccelerationStructureKHR,
354 buffer: vk::Buffer,
355 memory_handle: usize,
356}
357
358#[derive(Debug, Default)]
359struct DescriptorSetLayout {
360 raw: vk::DescriptorSetLayout,
361 update_template: vk::DescriptorUpdateTemplate,
362 template_size: u32,
363 template_offsets: Box<[u32]>,
364}
365
366impl DescriptorSetLayout {
367 fn is_empty(&self) -> bool {
368 self.template_size == 0
369 }
370}
371
372#[derive(Debug)]
373struct PipelineLayout {
374 raw: vk::PipelineLayout,
375 descriptor_set_layouts: Vec<DescriptorSetLayout>,
376}
377
378#[derive(Debug)]
379struct ScratchBuffer {
380 raw: vk::Buffer,
381 memory_handle: usize,
382 mapped: *mut u8,
383 capacity: u64,
384 offset: u64,
385 alignment: u64,
386}
387
388pub struct PipelineContext<'a> {
389 update_data: &'a mut [u8],
390 template_offsets: &'a [u32],
391 scratch: Option<&'a mut ScratchBuffer>,
392}
393
394#[derive(Debug)]
395pub struct ComputePipeline {
396 raw: vk::Pipeline,
397 layout: PipelineLayout,
398 wg_size: [u32; 3],
399}
400
401#[hidden_trait::expose]
402impl crate::traits::ComputePipelineBase for ComputePipeline {
403 fn get_workgroup_size(&self) -> [u32; 3] {
404 self.wg_size
405 }
406}
407
408#[derive(Debug)]
409pub struct RenderPipeline {
410 raw: vk::Pipeline,
411 layout: PipelineLayout,
412}
413
414#[derive(Debug)]
415struct CommandBuffer {
416 raw: vk::CommandBuffer,
417 descriptor_pool: descriptor::DescriptorPool,
418 query_pool: vk::QueryPool,
419 timed_pass_names: Vec<String>,
420 scratch: Option<ScratchBuffer>,
421}
422
423struct CrashHandler {
424 name: String,
425 marker_buf: Buffer,
426 raw_string: Box<[u8]>,
427 next_offset: usize,
428}
429
430pub struct CommandEncoder {
431 pool: vk::CommandPool,
432 buffers: Box<[CommandBuffer]>,
433 device: Device,
434 update_data: Vec<u8>,
435 present: Option<Presentation>,
436 crash_handler: Option<CrashHandler>,
437 temp_label: Vec<u8>,
438 timings: crate::Timings,
439}
440pub struct TransferCommandEncoder<'a> {
441 raw: vk::CommandBuffer,
442 device: &'a Device,
443}
444pub struct AccelerationStructureCommandEncoder<'a> {
445 raw: vk::CommandBuffer,
446 device: &'a Device,
447}
448pub struct ComputeCommandEncoder<'a> {
449 cmd_buf: &'a mut CommandBuffer,
450 device: &'a Device,
451 update_data: &'a mut Vec<u8>,
452}
453pub struct RenderCommandEncoder<'a> {
457 cmd_buf: &'a mut CommandBuffer,
458 device: &'a Device,
459 update_data: &'a mut Vec<u8>,
460}
461
462pub struct PipelineEncoder<'a, 'p> {
463 cmd_buf: &'a mut CommandBuffer,
464 layout: &'p PipelineLayout,
465 bind_point: vk::PipelineBindPoint,
466 device: &'a Device,
467 update_data: &'a mut Vec<u8>,
468}
469
470#[derive(Clone, Debug)]
471pub struct SyncPoint {
472 progress: u64,
473}
474
475#[hidden_trait::expose]
476impl crate::traits::CommandDevice for Context {
477 type CommandEncoder = CommandEncoder;
478 type SyncPoint = SyncPoint;
479
480 fn create_command_encoder(&self, desc: super::CommandEncoderDesc) -> CommandEncoder {
481 let pool_info = vk::CommandPoolCreateInfo {
482 flags: vk::CommandPoolCreateFlags::RESET_COMMAND_BUFFER,
483 ..Default::default()
484 };
485 let pool = unsafe {
486 self.device
487 .core
488 .create_command_pool(&pool_info, None)
489 .unwrap()
490 };
491 let cmd_buf_info = vk::CommandBufferAllocateInfo {
492 command_pool: pool,
493 command_buffer_count: desc.buffer_count,
494 ..Default::default()
495 };
496 let cmd_buffers = unsafe {
497 self.device
498 .core
499 .allocate_command_buffers(&cmd_buf_info)
500 .unwrap()
501 };
502
503 let buffers = cmd_buffers
504 .into_iter()
505 .map(|raw| {
506 if !desc.name.is_empty() {
507 self.set_object_name(raw, desc.name);
508 };
509 let descriptor_pool = self.device.create_descriptor_pool();
510 let query_pool = if self.device.timing.is_some() {
511 let query_pool_info = vk::QueryPoolCreateInfo::default()
512 .query_type(vk::QueryType::TIMESTAMP)
513 .query_count(QUERY_POOL_SIZE as u32);
514 unsafe {
515 self.device
516 .core
517 .create_query_pool(&query_pool_info, None)
518 .unwrap()
519 }
520 } else {
521 vk::QueryPool::null()
522 };
523 let scratch = if !self.device.inline_uniform_blocks {
524 const SCRATCH_SIZE: u64 = 1 << 20; let buf = self.create_buffer(crate::BufferDesc {
526 name: "_scratch",
527 size: SCRATCH_SIZE,
528 memory: crate::Memory::Shared,
529 });
530 Some(ScratchBuffer {
531 raw: buf.raw,
532 memory_handle: buf.memory_handle,
533 mapped: buf.mapped_data,
534 capacity: SCRATCH_SIZE,
535 offset: 0,
536 alignment: self.min_uniform_buffer_offset_alignment,
537 })
538 } else {
539 None
540 };
541 CommandBuffer {
542 raw,
543 descriptor_pool,
544 query_pool,
545 timed_pass_names: Vec::new(),
546 scratch,
547 }
548 })
549 .collect();
550
551 let crash_handler = if self.device.buffer_marker.is_some() {
552 Some(CrashHandler {
553 name: desc.name.to_string(),
554 marker_buf: self.create_buffer(crate::BufferDesc {
555 name: "_marker",
556 size: 4,
557 memory: crate::Memory::Shared,
558 }),
559 raw_string: vec![0; 0x1000].into_boxed_slice(),
560 next_offset: 0,
561 })
562 } else {
563 None
564 };
565
566 CommandEncoder {
567 pool,
568 buffers,
569 device: self.device.clone(),
570 update_data: Vec::new(),
571 present: None,
572 crash_handler,
573 temp_label: Vec::new(),
574 timings: Default::default(),
575 }
576 }
577
578 fn destroy_command_encoder(&self, command_encoder: &mut CommandEncoder) {
579 for cmd_buf in command_encoder.buffers.iter_mut() {
580 let raw_cmd_buffers = [cmd_buf.raw];
581 unsafe {
582 self.device
583 .core
584 .free_command_buffers(command_encoder.pool, &raw_cmd_buffers);
585 }
586 self.device
587 .destroy_descriptor_pool(&mut cmd_buf.descriptor_pool);
588 if self.device.timing.is_some() {
589 unsafe {
590 self.device
591 .core
592 .destroy_query_pool(cmd_buf.query_pool, None);
593 }
594 }
595 if let Some(ref scratch) = cmd_buf.scratch {
596 self.destroy_buffer(super::Buffer {
597 raw: scratch.raw,
598 memory_handle: scratch.memory_handle,
599 mapped_data: scratch.mapped,
600 size: 0,
601 external: None,
602 });
603 }
604 }
605 unsafe {
606 self.device
607 .core
608 .destroy_command_pool(mem::take(&mut command_encoder.pool), None)
609 };
610 if let Some(crash_handler) = command_encoder.crash_handler.take() {
611 self.destroy_buffer(crash_handler.marker_buf);
612 };
613 }
614
615 fn submit(&self, encoder: &mut CommandEncoder) -> SyncPoint {
616 let raw_cmd_buf = encoder.finish();
617 let mut queue = self.queue.lock().unwrap();
618 queue.last_progress += 1;
619 let progress = queue.last_progress;
620 let command_buffers = [raw_cmd_buf];
621 let wait_values_all = [0];
622 let mut wait_semaphores_all = [vk::Semaphore::null()];
623 let wait_stages = [vk::PipelineStageFlags::ALL_COMMANDS];
624 let mut signal_semaphores_all = [queue.timeline_semaphore, vk::Semaphore::null()];
625 let signal_values_all = [progress, 0];
626 let (num_wait_semaphores, num_signal_sepahores) = match encoder.present {
627 Some(Presentation::Window {
628 acquire_semaphore,
629 present_semaphore,
630 ..
631 }) => {
632 wait_semaphores_all[0] = acquire_semaphore;
633 signal_semaphores_all[1] = present_semaphore;
634 (1, 2)
635 }
636 Some(Presentation::Xr { .. }) | None => (0, 1),
637 };
638 let mut timeline_info = vk::TimelineSemaphoreSubmitInfo::default()
639 .wait_semaphore_values(&wait_values_all[..num_wait_semaphores])
640 .signal_semaphore_values(&signal_values_all[..num_signal_sepahores]);
641 let vk_info = vk::SubmitInfo::default()
642 .command_buffers(&command_buffers)
643 .wait_semaphores(&wait_semaphores_all[..num_wait_semaphores])
644 .wait_dst_stage_mask(&wait_stages[..num_wait_semaphores])
645 .signal_semaphores(&signal_semaphores_all[..num_signal_sepahores])
646 .push_next(&mut timeline_info);
647 let ret = unsafe {
648 self.device
649 .core
650 .queue_submit(queue.raw, &[vk_info], vk::Fence::null())
651 };
652 encoder.check_gpu_crash(ret);
653
654 if let Some(presentation) = encoder.present.take() {
655 match presentation {
656 Presentation::Window {
657 swapchain,
658 image_index,
659 present_semaphore,
660 ..
661 } => {
662 let khr_swapchain = self.device.swapchain.as_ref().unwrap();
663 let swapchains = [swapchain];
664 let image_indices = [image_index];
665 let wait_semaphores = [present_semaphore];
666 let present_info = vk::PresentInfoKHR::default()
667 .swapchains(&swapchains)
668 .image_indices(&image_indices)
669 .wait_semaphores(&wait_semaphores);
670 let ret = unsafe { khr_swapchain.queue_present(queue.raw, &present_info) };
671 let _ = encoder.check_gpu_crash(ret);
672 }
673 Presentation::Xr {
674 swapchain,
675 view_count,
676 target_size,
677 views,
678 } => {
679 let semaphores = [queue.timeline_semaphore];
680 let semaphore_values = [progress];
681 let wait_info = vk::SemaphoreWaitInfoKHR::default()
682 .semaphores(&semaphores)
683 .values(&semaphore_values);
684 unsafe {
685 self.device
686 .timeline_semaphore
687 .wait_semaphores(&wait_info, !0)
688 .unwrap();
689 }
690 let swapchain = unsafe { &mut *(swapchain as *mut xr::Swapchain<xr::Vulkan>) };
691 swapchain.release_image().unwrap();
692
693 let xr_state = self.xr.as_ref().expect("XR is not enabled in this context");
694 let mut xr_state = xr_state.lock().unwrap();
695 let environment_blend_mode = xr_state.environment_blend_mode;
696 let space = xr_state.space.take().expect("XR space is not initialized");
697 let predicted_display_time = xr_state
698 .predicted_display_time
699 .expect("XR frame timing is not initialized");
700 let rect = xr::Rect2Di {
701 offset: xr::Offset2Di { x: 0, y: 0 },
702 extent: xr::Extent2Di {
703 width: target_size[0] as _,
704 height: target_size[1] as _,
705 },
706 };
707 let projection_views = views[..view_count as usize]
708 .iter()
709 .enumerate()
710 .map(|(i, view)| {
711 xr::CompositionLayerProjectionView::new()
712 .pose(xr::Posef {
713 orientation: xr::Quaternionf {
714 x: view.pose.orientation[0],
715 y: view.pose.orientation[1],
716 z: view.pose.orientation[2],
717 w: view.pose.orientation[3],
718 },
719 position: xr::Vector3f {
720 x: view.pose.position[0],
721 y: view.pose.position[1],
722 z: view.pose.position[2],
723 },
724 })
725 .fov(xr::Fovf {
726 angle_left: view.fov.angle_left,
727 angle_right: view.fov.angle_right,
728 angle_up: view.fov.angle_up,
729 angle_down: view.fov.angle_down,
730 })
731 .sub_image(
732 xr::SwapchainSubImage::new()
733 .swapchain(swapchain)
734 .image_array_index(i as u32)
735 .image_rect(rect),
736 )
737 })
738 .collect::<Vec<_>>();
739 match xr_state.frame_stream.end(
740 predicted_display_time,
741 environment_blend_mode,
742 &[&xr::CompositionLayerProjection::new()
743 .space(&space)
744 .views(&projection_views)],
745 ) {
746 Ok(()) => {}
747 Err(xr::sys::Result::ERROR_POSE_INVALID) => {
748 log::warn!("XR frame end: pose invalid (tracking lost?)");
751 }
752 Err(e) => panic!("XR frame end failed: {e}"),
753 }
754 xr_state.space = Some(space);
755 }
756 }
757 }
758
759 SyncPoint { progress }
760 }
761
762 fn wait_for(&self, sp: &SyncPoint, timeout_ms: u32) -> Result<bool, crate::DeviceError> {
763 let timeline_semaphore = self.queue.lock().unwrap().timeline_semaphore;
766 let semaphores = [timeline_semaphore];
767 let semaphore_values = [sp.progress];
768 let wait_info = vk::SemaphoreWaitInfoKHR::default()
769 .semaphores(&semaphores)
770 .values(&semaphore_values);
771 let timeout_ns = map_timeout(timeout_ms);
772 match unsafe {
773 self.device
774 .timeline_semaphore
775 .wait_semaphores(&wait_info, timeout_ns)
776 } {
777 Ok(()) => Ok(true),
778 Err(vk::Result::TIMEOUT) => Ok(false),
779 Err(vk::Result::ERROR_DEVICE_LOST) => Err(crate::DeviceError::DeviceLost),
780 Err(vk::Result::ERROR_OUT_OF_DEVICE_MEMORY)
781 | Err(vk::Result::ERROR_OUT_OF_HOST_MEMORY) => Err(crate::DeviceError::OutOfMemory),
782 Err(other) => {
783 log::error!("Unexpected wait_semaphores error: {:?}", other);
784 Err(crate::DeviceError::DeviceLost)
785 }
786 }
787 }
788}
789
790fn map_texture_format(format: crate::TextureFormat) -> vk::Format {
791 use crate::TextureFormat as Tf;
792 match format {
793 Tf::R8Unorm => vk::Format::R8_UNORM,
794 Tf::Rg8Unorm => vk::Format::R8G8_UNORM,
795 Tf::Rg8Snorm => vk::Format::R8G8_SNORM,
796 Tf::Rgba8Unorm => vk::Format::R8G8B8A8_UNORM,
797 Tf::Rgba8UnormSrgb => vk::Format::R8G8B8A8_SRGB,
798 Tf::Bgra8Unorm => vk::Format::B8G8R8A8_UNORM,
799 Tf::Bgra8UnormSrgb => vk::Format::B8G8R8A8_SRGB,
800 Tf::Rgba8Snorm => vk::Format::R8G8B8A8_SNORM,
801 Tf::R16Float => vk::Format::R16_SFLOAT,
802 Tf::Rg16Float => vk::Format::R16G16_SFLOAT,
803 Tf::Rgba16Float => vk::Format::R16G16B16A16_SFLOAT,
804 Tf::R32Float => vk::Format::R32_SFLOAT,
805 Tf::Rg32Float => vk::Format::R32G32_SFLOAT,
806 Tf::Rgba32Float => vk::Format::R32G32B32A32_SFLOAT,
807 Tf::R32Uint => vk::Format::R32_UINT,
808 Tf::Rg32Uint => vk::Format::R32G32_UINT,
809 Tf::Rgba32Uint => vk::Format::R32G32B32A32_UINT,
810 Tf::Depth32Float => vk::Format::D32_SFLOAT,
811 Tf::Depth32FloatStencil8Uint => vk::Format::D32_SFLOAT_S8_UINT,
812 Tf::Stencil8Uint => vk::Format::S8_UINT,
813 Tf::Bc1Unorm => vk::Format::BC1_RGBA_SRGB_BLOCK,
814 Tf::Bc1UnormSrgb => vk::Format::BC1_RGBA_UNORM_BLOCK,
815 Tf::Bc2Unorm => vk::Format::BC2_UNORM_BLOCK,
816 Tf::Bc2UnormSrgb => vk::Format::BC2_SRGB_BLOCK,
817 Tf::Bc3Unorm => vk::Format::BC3_UNORM_BLOCK,
818 Tf::Bc3UnormSrgb => vk::Format::BC3_SRGB_BLOCK,
819 Tf::Bc4Unorm => vk::Format::BC4_UNORM_BLOCK,
820 Tf::Bc4Snorm => vk::Format::BC4_SNORM_BLOCK,
821 Tf::Bc5Unorm => vk::Format::BC5_UNORM_BLOCK,
822 Tf::Bc5Snorm => vk::Format::BC5_SNORM_BLOCK,
823 Tf::Bc6hUfloat => vk::Format::BC6H_UFLOAT_BLOCK,
824 Tf::Bc6hFloat => vk::Format::BC6H_SFLOAT_BLOCK,
825 Tf::Bc7Unorm => vk::Format::BC7_UNORM_BLOCK,
826 Tf::Bc7UnormSrgb => vk::Format::BC7_SRGB_BLOCK,
827 Tf::Rgb10a2Unorm => vk::Format::A2B10G10R10_UNORM_PACK32,
828 Tf::Rg11b10Ufloat => vk::Format::B10G11R11_UFLOAT_PACK32,
829 Tf::Rgb9e5Ufloat => vk::Format::E5B9G9R9_UFLOAT_PACK32,
830 }
831}
832
833fn map_aspects(aspects: crate::TexelAspects) -> vk::ImageAspectFlags {
834 let mut flags = vk::ImageAspectFlags::empty();
835 if aspects.contains(crate::TexelAspects::COLOR) {
836 flags |= vk::ImageAspectFlags::COLOR;
837 }
838 if aspects.contains(crate::TexelAspects::DEPTH) {
839 flags |= vk::ImageAspectFlags::DEPTH;
840 }
841 if aspects.contains(crate::TexelAspects::STENCIL) {
842 flags |= vk::ImageAspectFlags::STENCIL;
843 }
844 flags
845}
846
847fn map_extent_3d(extent: &crate::Extent) -> vk::Extent3D {
848 vk::Extent3D {
849 width: extent.width,
850 height: extent.height,
851 depth: extent.depth,
852 }
853}
854
855fn map_subresource_range(
856 subresources: &crate::TextureSubresources,
857 aspects: crate::TexelAspects,
858) -> vk::ImageSubresourceRange {
859 vk::ImageSubresourceRange {
860 aspect_mask: map_aspects(aspects),
861 base_mip_level: subresources.base_mip_level,
862 level_count: subresources
863 .mip_level_count
864 .map_or(vk::REMAINING_MIP_LEVELS, NonZeroU32::get),
865 base_array_layer: subresources.base_array_layer,
866 layer_count: subresources
867 .array_layer_count
868 .map_or(vk::REMAINING_ARRAY_LAYERS, NonZeroU32::get),
869 }
870}
871
872fn map_comparison(fun: crate::CompareFunction) -> vk::CompareOp {
873 use crate::CompareFunction as Cf;
874 match fun {
875 Cf::Never => vk::CompareOp::NEVER,
876 Cf::Less => vk::CompareOp::LESS,
877 Cf::LessEqual => vk::CompareOp::LESS_OR_EQUAL,
878 Cf::Equal => vk::CompareOp::EQUAL,
879 Cf::GreaterEqual => vk::CompareOp::GREATER_OR_EQUAL,
880 Cf::Greater => vk::CompareOp::GREATER,
881 Cf::NotEqual => vk::CompareOp::NOT_EQUAL,
882 Cf::Always => vk::CompareOp::ALWAYS,
883 }
884}
885
886fn map_index_type(index_type: crate::IndexType) -> vk::IndexType {
887 match index_type {
888 crate::IndexType::U16 => vk::IndexType::UINT16,
889 crate::IndexType::U32 => vk::IndexType::UINT32,
890 }
891}
892
893fn map_vertex_format(vertex_format: crate::VertexFormat) -> vk::Format {
894 use crate::VertexFormat as Vf;
895 match vertex_format {
896 Vf::F32 => vk::Format::R32_SFLOAT,
897 Vf::F32Vec2 => vk::Format::R32G32_SFLOAT,
898 Vf::F32Vec3 => vk::Format::R32G32B32_SFLOAT,
899 Vf::F32Vec4 => vk::Format::R32G32B32A32_SFLOAT,
900 Vf::U32 => vk::Format::R32_UINT,
901 Vf::U32Vec2 => vk::Format::R32G32_UINT,
902 Vf::U32Vec3 => vk::Format::R32G32B32_UINT,
903 Vf::U32Vec4 => vk::Format::R32G32B32A32_UINT,
904 Vf::I32 => vk::Format::R32_SINT,
905 Vf::I32Vec2 => vk::Format::R32G32_SINT,
906 Vf::I32Vec3 => vk::Format::R32G32B32_SINT,
907 Vf::I32Vec4 => vk::Format::R32G32B32A32_SINT,
908 }
909}
910
911struct BottomLevelAccelerationStructureInput<'a> {
912 max_primitive_counts: Box<[u32]>,
913 build_range_infos: Box<[vk::AccelerationStructureBuildRangeInfoKHR]>,
914 _geometries: Box<[vk::AccelerationStructureGeometryKHR<'a>]>,
915 build_info: vk::AccelerationStructureBuildGeometryInfoKHR<'a>,
916}
917
918impl Device {
919 fn get_device_address(&self, piece: &crate::BufferPiece) -> u64 {
920 let vk_info = vk::BufferDeviceAddressInfo {
921 buffer: piece.buffer.raw,
922 ..Default::default()
923 };
924 let base = unsafe { self.core.get_buffer_device_address(&vk_info) };
925 base + piece.offset
926 }
927
928 fn map_acceleration_structure_meshes(
929 &self,
930 meshes: &[crate::AccelerationStructureMesh],
931 ) -> BottomLevelAccelerationStructureInput<'_> {
932 let mut total_primitive_count = 0;
933 let mut max_primitive_counts = Vec::with_capacity(meshes.len());
934 let mut build_range_infos = Vec::with_capacity(meshes.len());
935 let mut geometries = Vec::with_capacity(meshes.len());
936 for mesh in meshes {
937 total_primitive_count += mesh.triangle_count;
938 max_primitive_counts.push(mesh.triangle_count);
939 build_range_infos.push(vk::AccelerationStructureBuildRangeInfoKHR {
940 primitive_count: mesh.triangle_count,
941 primitive_offset: 0,
942 first_vertex: 0,
943 transform_offset: 0,
944 });
945
946 let mut triangles = vk::AccelerationStructureGeometryTrianglesDataKHR {
947 vertex_format: map_vertex_format(mesh.vertex_format),
948 vertex_data: {
949 let device_address = self.get_device_address(&mesh.vertex_data);
950 assert!(
951 device_address & 0x3 == 0,
952 "Vertex data address {device_address} is not aligned"
953 );
954 vk::DeviceOrHostAddressConstKHR { device_address }
955 },
956 vertex_stride: mesh.vertex_stride as u64,
957 max_vertex: mesh.vertex_count.saturating_sub(1),
958 ..Default::default()
959 };
960 if let Some(index_type) = mesh.index_type {
961 let device_address = self.get_device_address(&mesh.index_data);
962 assert!(
963 device_address & 0x3 == 0,
964 "Index data address {device_address} is not aligned"
965 );
966 triangles.index_type = map_index_type(index_type);
967 triangles.index_data = vk::DeviceOrHostAddressConstKHR { device_address };
968 }
969 if mesh.transform_data.buffer.raw != vk::Buffer::null() {
970 let device_address = self.get_device_address(&mesh.transform_data);
971 assert!(
972 device_address & 0xF == 0,
973 "Transform data address {device_address} is not aligned"
974 );
975 triangles.transform_data = vk::DeviceOrHostAddressConstKHR { device_address };
976 }
977
978 let geometry = vk::AccelerationStructureGeometryKHR {
979 geometry_type: vk::GeometryTypeKHR::TRIANGLES,
980 geometry: vk::AccelerationStructureGeometryDataKHR { triangles },
981 flags: if mesh.is_opaque {
982 vk::GeometryFlagsKHR::OPAQUE
983 } else {
984 vk::GeometryFlagsKHR::empty()
985 },
986 ..Default::default()
987 };
988 geometries.push(geometry);
989 }
990 let build_info = vk::AccelerationStructureBuildGeometryInfoKHR {
991 ty: vk::AccelerationStructureTypeKHR::BOTTOM_LEVEL,
992 flags: vk::BuildAccelerationStructureFlagsKHR::PREFER_FAST_TRACE,
993 mode: vk::BuildAccelerationStructureModeKHR::BUILD,
994 geometry_count: geometries.len() as u32,
995 p_geometries: geometries.as_ptr(),
996 ..Default::default()
997 };
998
999 log::debug!(
1000 "BLAS total {} primitives in {} geometries",
1001 total_primitive_count,
1002 geometries.len()
1003 );
1004 BottomLevelAccelerationStructureInput {
1005 max_primitive_counts: max_primitive_counts.into_boxed_slice(),
1006 build_range_infos: build_range_infos.into_boxed_slice(),
1007 _geometries: geometries.into_boxed_slice(),
1008 build_info,
1009 }
1010 }
1011}