|
2 | 2 | use wasm_bindgen::prelude::*; |
3 | 3 | use wgpu::InstanceDescriptor; |
4 | 4 |
|
| 5 | +#[cfg(all(not(target_arch = "wasm32"), feature = "openxr"))] |
| 6 | +pub mod xr; |
| 7 | + |
| 8 | +#[cfg(all(not(target_arch = "wasm32"), feature = "openxr"))] |
| 9 | +pub use xr::run_xr; |
| 10 | + |
5 | 11 | use std::sync::Arc; |
6 | 12 | use web_time::{Duration, Instant}; |
7 | 13 | use winit::{ |
@@ -563,9 +569,67 @@ impl Gpu { |
563 | 569 | surface_format, |
564 | 570 | } |
565 | 571 | } |
| 572 | + |
| 573 | + #[cfg(not(target_arch = "wasm32"))] |
| 574 | + pub async fn new_async_headless() -> Self { |
| 575 | + let instance = wgpu::Instance::new(&InstanceDescriptor::default()); |
| 576 | + |
| 577 | + let adapter = instance |
| 578 | + .request_adapter(&wgpu::RequestAdapterOptions { |
| 579 | + power_preference: wgpu::PowerPreference::default(), |
| 580 | + compatible_surface: None, |
| 581 | + force_fallback_adapter: false, |
| 582 | + }) |
| 583 | + .await |
| 584 | + .expect("Failed to request adapter!"); |
| 585 | + |
| 586 | + let (device, queue) = { |
| 587 | + log::info!("WGPU Adapter Features: {:#?}", adapter.features()); |
| 588 | + adapter |
| 589 | + .request_device(&wgpu::DeviceDescriptor { |
| 590 | + label: Some("WGPU Device"), |
| 591 | + memory_hints: wgpu::MemoryHints::default(), |
| 592 | + required_features: wgpu::Features::default(), |
| 593 | + required_limits: wgpu::Limits::default().using_resolution(adapter.limits()), |
| 594 | + experimental_features: wgpu::ExperimentalFeatures::disabled(), |
| 595 | + trace: wgpu::Trace::Off, |
| 596 | + }) |
| 597 | + .await |
| 598 | + .expect("Failed to request a device!") |
| 599 | + }; |
| 600 | + |
| 601 | + let surface_format = wgpu::TextureFormat::Rgba8UnormSrgb; |
| 602 | + let surface_config = wgpu::SurfaceConfiguration { |
| 603 | + usage: wgpu::TextureUsages::RENDER_ATTACHMENT, |
| 604 | + format: surface_format, |
| 605 | + width: 1, |
| 606 | + height: 1, |
| 607 | + present_mode: wgpu::PresentMode::Fifo, |
| 608 | + alpha_mode: wgpu::CompositeAlphaMode::Opaque, |
| 609 | + view_formats: vec![], |
| 610 | + desired_maximum_frame_latency: 2, |
| 611 | + }; |
| 612 | + |
| 613 | + let dummy_surface = unsafe { |
| 614 | + let handle = wgpu::rwh::RawDisplayHandle::Windows(wgpu::rwh::WindowsDisplayHandle::new()); |
| 615 | + let window_handle = wgpu::rwh::RawWindowHandle::Win32(wgpu::rwh::Win32WindowHandle::new(std::num::NonZeroIsize::new(1).unwrap())); |
| 616 | + instance.create_surface_unsafe(wgpu::SurfaceTargetUnsafe::RawHandle { |
| 617 | + raw_display_handle: handle, |
| 618 | + raw_window_handle: window_handle, |
| 619 | + }).unwrap() |
| 620 | + }; |
| 621 | + |
| 622 | + Self { |
| 623 | + surface: dummy_surface, |
| 624 | + device, |
| 625 | + queue, |
| 626 | + surface_config, |
| 627 | + surface_format, |
| 628 | + } |
| 629 | + } |
566 | 630 | } |
567 | 631 |
|
568 | | -struct Scene { |
| 632 | +pub struct Scene { |
569 | 633 | pub model: nalgebra_glm::Mat4, |
570 | 634 | pub vertex_buffer: wgpu::Buffer, |
571 | 635 | pub index_buffer: wgpu::Buffer, |
@@ -723,7 +787,7 @@ struct UniformBuffer { |
723 | 787 | mvp: nalgebra_glm::Mat4, |
724 | 788 | } |
725 | 789 |
|
726 | | -struct UniformBinding { |
| 790 | +pub struct UniformBinding { |
727 | 791 | pub buffer: wgpu::Buffer, |
728 | 792 | pub bind_group: wgpu::BindGroup, |
729 | 793 | pub bind_group_layout: wgpu::BindGroupLayout, |
|
0 commit comments