mirror of
https://github.com/gfx-rs/wgpu.git
synced 2026-04-22 03:02:01 -04:00
222 lines
7.1 KiB
Rust
222 lines
7.1 KiB
Rust
use std::time;
|
|
use winit::{
|
|
event::{self, WindowEvent},
|
|
event_loop::{ControlFlow, EventLoop},
|
|
window::Window,
|
|
};
|
|
|
|
#[cfg_attr(rustfmt, rustfmt_skip)]
|
|
#[allow(unused)]
|
|
pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = cgmath::Matrix4::new(
|
|
1.0, 0.0, 0.0, 0.0,
|
|
0.0, 1.0, 0.0, 0.0,
|
|
0.0, 0.0, 0.5, 0.0,
|
|
0.0, 0.0, 0.5, 1.0,
|
|
);
|
|
|
|
#[allow(dead_code)]
|
|
pub fn cast_slice<T>(data: &[T]) -> &[u8] {
|
|
use std::mem::size_of;
|
|
use std::slice::from_raw_parts;
|
|
|
|
unsafe { from_raw_parts(data.as_ptr() as *const u8, data.len() * size_of::<T>()) }
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
pub enum ShaderStage {
|
|
Vertex,
|
|
Fragment,
|
|
Compute,
|
|
}
|
|
|
|
pub trait Example: 'static + Sized {
|
|
fn init(
|
|
sc_desc: &wgpu::SwapChainDescriptor,
|
|
device: &wgpu::Device,
|
|
) -> (Self, Option<wgpu::CommandBuffer>);
|
|
fn resize(
|
|
&mut self,
|
|
sc_desc: &wgpu::SwapChainDescriptor,
|
|
device: &wgpu::Device,
|
|
queue: &wgpu::Queue,
|
|
);
|
|
fn update(&mut self, event: WindowEvent);
|
|
fn render(
|
|
&mut self,
|
|
frame: &wgpu::SwapChainOutput,
|
|
device: &wgpu::Device,
|
|
queue: &wgpu::Queue,
|
|
) -> wgpu::CommandBuffer;
|
|
}
|
|
|
|
async fn run_async<E: Example>(event_loop: EventLoop<()>, window: Window) {
|
|
log::info!("Initializing the surface...");
|
|
|
|
let instance = wgpu::Instance::new();
|
|
let (size, surface) = unsafe {
|
|
let size = window.inner_size();
|
|
let surface = instance.create_surface(&window);
|
|
(size, surface)
|
|
};
|
|
|
|
let adapter = instance
|
|
.request_adapter(
|
|
&wgpu::RequestAdapterOptions {
|
|
power_preference: wgpu::PowerPreference::Default,
|
|
compatible_surface: Some(&surface),
|
|
},
|
|
wgpu::BackendBit::PRIMARY,
|
|
)
|
|
.await
|
|
.unwrap();
|
|
|
|
let trace_dir = std::env::var("WGPU_TRACE");
|
|
let (device, queue) = adapter
|
|
.request_device(
|
|
&wgpu::DeviceDescriptor {
|
|
extensions: wgpu::Extensions {
|
|
anisotropic_filtering: false,
|
|
},
|
|
limits: wgpu::Limits::default(),
|
|
},
|
|
match trace_dir {
|
|
Ok(ref value) if !cfg!(feature = "trace") => {
|
|
log::error!("Unable to trace into {:?} without \"trace\" feature enabled!", value);
|
|
None
|
|
}
|
|
Ok(ref value) => Some(std::path::Path::new(value)),
|
|
Err(_) => None,
|
|
},
|
|
)
|
|
.await
|
|
.unwrap();
|
|
|
|
let mut sc_desc = wgpu::SwapChainDescriptor {
|
|
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
|
|
// TODO: Allow srgb unconditionally
|
|
format: if cfg!(target_arch = "wasm32") {
|
|
wgpu::TextureFormat::Bgra8Unorm
|
|
} else {
|
|
wgpu::TextureFormat::Bgra8UnormSrgb
|
|
},
|
|
width: size.width,
|
|
height: size.height,
|
|
present_mode: wgpu::PresentMode::Mailbox,
|
|
};
|
|
let mut swap_chain = device.create_swap_chain(&surface, &sc_desc);
|
|
|
|
log::info!("Initializing the example...");
|
|
let (mut example, init_command_buf) = E::init(&sc_desc, &device);
|
|
if init_command_buf.is_some() {
|
|
queue.submit(init_command_buf);
|
|
}
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
let mut last_update_inst = time::Instant::now();
|
|
|
|
log::info!("Entering render loop...");
|
|
event_loop.run(move |event, _, control_flow| {
|
|
let _ = (&instance, &adapter); // force ownership by the closure
|
|
*control_flow = if cfg!(feature = "metal-auto-capture") {
|
|
ControlFlow::Exit
|
|
} else {
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
{
|
|
ControlFlow::WaitUntil(time::Instant::now() + time::Duration::from_millis(10))
|
|
}
|
|
#[cfg(target_arch = "wasm32")]
|
|
{
|
|
ControlFlow::Poll
|
|
}
|
|
};
|
|
match event {
|
|
event::Event::MainEventsCleared => {
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
{
|
|
if last_update_inst.elapsed() > time::Duration::from_millis(20) {
|
|
window.request_redraw();
|
|
last_update_inst = time::Instant::now();
|
|
}
|
|
}
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
window.request_redraw();
|
|
}
|
|
event::Event::WindowEvent {
|
|
event: WindowEvent::Resized(size),
|
|
..
|
|
} => {
|
|
log::info!("Resizing to {:?}", size);
|
|
sc_desc.width = size.width;
|
|
sc_desc.height = size.height;
|
|
swap_chain = device.create_swap_chain(&surface, &sc_desc);
|
|
example.resize(&sc_desc, &device, &queue);
|
|
}
|
|
event::Event::WindowEvent { event, .. } => match event {
|
|
WindowEvent::KeyboardInput {
|
|
input:
|
|
event::KeyboardInput {
|
|
virtual_keycode: Some(event::VirtualKeyCode::Escape),
|
|
state: event::ElementState::Pressed,
|
|
..
|
|
},
|
|
..
|
|
}
|
|
| WindowEvent::CloseRequested => {
|
|
*control_flow = ControlFlow::Exit;
|
|
}
|
|
_ => {
|
|
example.update(event);
|
|
}
|
|
},
|
|
event::Event::RedrawRequested(_) => {
|
|
let frame = swap_chain
|
|
.get_next_texture()
|
|
.expect("Timeout when acquiring next swap chain texture");
|
|
let command_buf = example.render(&frame, &device, &queue);
|
|
queue.submit(Some(command_buf));
|
|
}
|
|
_ => {}
|
|
}
|
|
});
|
|
}
|
|
|
|
pub fn run<E: Example>(title: &str) {
|
|
let event_loop = EventLoop::new();
|
|
let mut builder = winit::window::WindowBuilder::new();
|
|
builder = builder.with_title(title);
|
|
#[cfg(windows_OFF)] //TODO
|
|
{
|
|
use winit::platform::windows::WindowBuilderExtWindows;
|
|
builder = builder.with_no_redirection_bitmap(true);
|
|
}
|
|
let window = builder.build(&event_loop).unwrap();
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
{
|
|
env_logger::init();
|
|
futures::executor::block_on(run_async::<E>(event_loop, window));
|
|
}
|
|
#[cfg(target_arch = "wasm32")]
|
|
{
|
|
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
|
|
console_log::init().expect("could not initialize logger");
|
|
use winit::platform::web::WindowExtWebSys;
|
|
// On wasm, append the canvas to the document body
|
|
web_sys::window()
|
|
.and_then(|win| win.document())
|
|
.and_then(|doc| doc.body())
|
|
.and_then(|body| {
|
|
body.append_child(&web_sys::Element::from(window.canvas()))
|
|
.ok()
|
|
})
|
|
.expect("couldn't append canvas to document body");
|
|
wasm_bindgen_futures::spawn_local(run_async::<E>(event_loop, window));
|
|
}
|
|
}
|
|
|
|
// This allows treating the framework as a standalone example,
|
|
// thus avoiding listing the example names in `Cargo.toml`.
|
|
#[allow(dead_code)]
|
|
fn main() {}
|