mirror of
https://github.com/gfx-rs/wgpu.git
synced 2026-04-22 03:02:01 -04:00
175 lines
5.4 KiB
Rust
175 lines
5.4 KiB
Rust
use log::info;
|
|
|
|
#[cfg_attr(rustfmt, rustfmt_skip)]
|
|
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 fn load_glsl(code: &str, stage: ShaderStage) -> Vec<u32> {
|
|
use std::io::Read;
|
|
|
|
let ty = match stage {
|
|
ShaderStage::Vertex => glsl_to_spirv::ShaderType::Vertex,
|
|
ShaderStage::Fragment => glsl_to_spirv::ShaderType::Fragment,
|
|
ShaderStage::Compute => glsl_to_spirv::ShaderType::Compute,
|
|
};
|
|
|
|
let mut output = glsl_to_spirv::compile(&code, ty).unwrap();
|
|
let mut spv_bytes = Vec::new();
|
|
output.read_to_end(&mut spv_bytes).unwrap();
|
|
|
|
let mut spv_words = Vec::new();
|
|
for bytes4 in spv_bytes.chunks(4) {
|
|
spv_words.push(u32::from_le_bytes([bytes4[0], bytes4[1], bytes4[2], bytes4[3]]));
|
|
}
|
|
spv_words
|
|
}
|
|
|
|
pub trait Example {
|
|
fn init(sc_desc: &wgpu::SwapChainDescriptor, device: &mut wgpu::Device) -> Self;
|
|
fn resize(&mut self, sc_desc: &wgpu::SwapChainDescriptor, device: &mut wgpu::Device);
|
|
fn update(&mut self, event: wgpu::winit::WindowEvent);
|
|
fn render(&mut self, frame: &wgpu::SwapChainOutput, device: &mut wgpu::Device);
|
|
}
|
|
|
|
pub fn run<E: Example>(title: &str) {
|
|
use wgpu::winit::{
|
|
ElementState,
|
|
Event,
|
|
EventsLoop,
|
|
KeyboardInput,
|
|
VirtualKeyCode,
|
|
WindowEvent,
|
|
};
|
|
|
|
env_logger::init();
|
|
|
|
let mut events_loop = EventsLoop::new();
|
|
|
|
info!("Initializing the window...");
|
|
|
|
#[cfg(not(feature = "gl"))]
|
|
let (_window, instance, hidpi_factor, size, surface) = {
|
|
use wgpu::winit::Window;
|
|
|
|
let instance = wgpu::Instance::new();
|
|
|
|
let window = Window::new(&events_loop).unwrap();
|
|
window.set_title(title);
|
|
let hidpi_factor = window.get_hidpi_factor();
|
|
let size = window.get_inner_size().unwrap().to_physical(hidpi_factor);
|
|
|
|
let surface = instance.create_surface(&window);
|
|
|
|
(window, instance, hidpi_factor, size, surface)
|
|
};
|
|
|
|
#[cfg(feature = "gl")]
|
|
let (_window, instance, hidpi_factor, size, surface) = {
|
|
let wb = wgpu::winit::WindowBuilder::new();
|
|
let cb = wgpu::glutin::ContextBuilder::new().with_vsync(true);
|
|
let context = cb.build_windowed(wb, &events_loop).unwrap();
|
|
context.window().set_title(title);
|
|
|
|
let hidpi_factor = context.window().get_hidpi_factor();
|
|
let size = context
|
|
.window()
|
|
.get_inner_size()
|
|
.unwrap()
|
|
.to_physical(hidpi_factor);
|
|
|
|
let (context, window) = unsafe { context.make_current().unwrap().split() };
|
|
|
|
let instance = wgpu::Instance::new(context);
|
|
let surface = instance.get_surface();
|
|
|
|
(window, instance, hidpi_factor, size, surface)
|
|
};
|
|
|
|
let adapter = instance.get_adapter(&wgpu::AdapterDescriptor {
|
|
power_preference: wgpu::PowerPreference::LowPower,
|
|
});
|
|
|
|
let mut device = adapter.request_device(&wgpu::DeviceDescriptor {
|
|
extensions: wgpu::Extensions {
|
|
anisotropic_filtering: false,
|
|
},
|
|
limits: wgpu::Limits::default(),
|
|
});
|
|
|
|
let mut sc_desc = wgpu::SwapChainDescriptor {
|
|
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
|
|
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
|
width: size.width.round() as u32,
|
|
height: size.height.round() as u32,
|
|
present_mode: wgpu::PresentMode::Vsync,
|
|
};
|
|
let mut swap_chain = device.create_swap_chain(&surface, &sc_desc);
|
|
|
|
info!("Initializing the example...");
|
|
let mut example = E::init(&sc_desc, &mut device);
|
|
|
|
info!("Entering render loop...");
|
|
let mut running = true;
|
|
while running {
|
|
events_loop.poll_events(|event| match event {
|
|
Event::WindowEvent {
|
|
event: WindowEvent::Resized(size),
|
|
..
|
|
} => {
|
|
let physical = size.to_physical(hidpi_factor);
|
|
info!("Resizing to {:?}", physical);
|
|
sc_desc.width = physical.width.round() as u32;
|
|
sc_desc.height = physical.height.round() as u32;
|
|
swap_chain = device.create_swap_chain(&surface, &sc_desc);
|
|
example.resize(&sc_desc, &mut device);
|
|
}
|
|
Event::WindowEvent { event, .. } => match event {
|
|
WindowEvent::KeyboardInput {
|
|
input:
|
|
KeyboardInput {
|
|
virtual_keycode: Some(VirtualKeyCode::Escape),
|
|
state: ElementState::Pressed,
|
|
..
|
|
},
|
|
..
|
|
}
|
|
| WindowEvent::CloseRequested => {
|
|
running = false;
|
|
}
|
|
_ => {
|
|
example.update(event);
|
|
}
|
|
},
|
|
_ => (),
|
|
});
|
|
|
|
let frame = swap_chain.get_next_texture();
|
|
example.render(&frame, &mut device);
|
|
running &= !cfg!(feature = "metal-auto-capture");
|
|
}
|
|
}
|
|
|
|
// This allows treating the framework as a standalone example,
|
|
// thus avoiding listing the example names in `Cargo.toml`.
|
|
#[allow(dead_code)]
|
|
fn main() {}
|