|
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,72 @@ 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 = |
| 615 | + wgpu::rwh::RawDisplayHandle::Windows(wgpu::rwh::WindowsDisplayHandle::new()); |
| 616 | + let window_handle = wgpu::rwh::RawWindowHandle::Win32( |
| 617 | + wgpu::rwh::Win32WindowHandle::new(std::num::NonZeroIsize::new(1).unwrap()), |
| 618 | + ); |
| 619 | + instance |
| 620 | + .create_surface_unsafe(wgpu::SurfaceTargetUnsafe::RawHandle { |
| 621 | + raw_display_handle: handle, |
| 622 | + raw_window_handle: window_handle, |
| 623 | + }) |
| 624 | + .unwrap() |
| 625 | + }; |
| 626 | + |
| 627 | + Self { |
| 628 | + surface: dummy_surface, |
| 629 | + device, |
| 630 | + queue, |
| 631 | + surface_config, |
| 632 | + surface_format, |
| 633 | + } |
| 634 | + } |
566 | 635 | } |
567 | 636 |
|
568 | | -struct Scene { |
| 637 | +pub struct Scene { |
569 | 638 | pub model: nalgebra_glm::Mat4, |
570 | 639 | pub vertex_buffer: wgpu::Buffer, |
571 | 640 | pub index_buffer: wgpu::Buffer, |
@@ -723,7 +792,7 @@ struct UniformBuffer { |
723 | 792 | mvp: nalgebra_glm::Mat4, |
724 | 793 | } |
725 | 794 |
|
726 | | -struct UniformBinding { |
| 795 | +pub struct UniformBinding { |
727 | 796 | pub buffer: wgpu::Buffer, |
728 | 797 | pub bind_group: wgpu::BindGroup, |
729 | 798 | pub bind_group_layout: wgpu::BindGroupLayout, |
@@ -801,6 +870,26 @@ const VERTICES: [Vertex; 3] = [ |
801 | 870 |
|
802 | 871 | const INDICES: [u32; 3] = [0, 1, 2]; |
803 | 872 |
|
| 873 | +pub const CUBE_VERTICES: [Vertex; 8] = [ |
| 874 | + Vertex { position: [-0.05, -0.05, -0.05, 1.0], color: [1.0, 1.0, 1.0, 1.0] }, |
| 875 | + Vertex { position: [0.05, -0.05, -0.05, 1.0], color: [1.0, 1.0, 1.0, 1.0] }, |
| 876 | + Vertex { position: [0.05, 0.05, -0.05, 1.0], color: [1.0, 1.0, 1.0, 1.0] }, |
| 877 | + Vertex { position: [-0.05, 0.05, -0.05, 1.0], color: [1.0, 1.0, 1.0, 1.0] }, |
| 878 | + Vertex { position: [-0.05, -0.05, 0.05, 1.0], color: [1.0, 1.0, 1.0, 1.0] }, |
| 879 | + Vertex { position: [0.05, -0.05, 0.05, 1.0], color: [1.0, 1.0, 1.0, 1.0] }, |
| 880 | + Vertex { position: [0.05, 0.05, 0.05, 1.0], color: [1.0, 1.0, 1.0, 1.0] }, |
| 881 | + Vertex { position: [-0.05, 0.05, 0.05, 1.0], color: [1.0, 1.0, 1.0, 1.0] }, |
| 882 | +]; |
| 883 | + |
| 884 | +pub const CUBE_INDICES: [u32; 36] = [ |
| 885 | + 0, 1, 2, 2, 3, 0, |
| 886 | + 1, 5, 6, 6, 2, 1, |
| 887 | + 5, 4, 7, 7, 6, 5, |
| 888 | + 4, 0, 3, 3, 7, 4, |
| 889 | + 3, 2, 6, 6, 7, 3, |
| 890 | + 4, 5, 1, 1, 0, 4, |
| 891 | +]; |
| 892 | + |
804 | 893 | const SHADER_SOURCE: &str = " |
805 | 894 | struct Uniform { |
806 | 895 | mvp: mat4x4<f32>, |
|
0 commit comments