Skip to content

Commit

Permalink
Update to wgpu 0.20 (#57)
Browse files Browse the repository at this point in the history
* Update to wgpu 0.20
* Update winit API usage
  • Loading branch information
stefnotch authored May 7, 2024
1 parent d9de3c5 commit 3cc4829
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 44 deletions.
10 changes: 5 additions & 5 deletions example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ version = "0.7.0"
edition = "2021"

[dependencies]
winit = "0.29.10"
wgpu = "0.19.0"
winit = "0.30"
wgpu = "0.20"
futures = "0.3"
bytemuck = { version = "1.13", features = [ "derive" ] }
encase = { version = "0.7.0", features = ["glam"] }
glam = { version = "0.25.0", features = ["bytemuck"] }
bytemuck = { version = "1.13", features = ["derive"] }
encase = { version = "0.8.0", features = ["glam"] }
glam = { version = "0.27.0", features = ["bytemuck"] }

[build-dependencies]
wgsl_to_wgpu = { path = "../wgsl_to_wgpu" }
91 changes: 58 additions & 33 deletions example/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
use std::iter;
use std::{iter, sync::Arc};

use crate::shader::ENTRY_FS_MAIN;
use encase::UniformBuffer;
use futures::executor::block_on;
use wgpu::util::DeviceExt;
use winit::{
event::*,
event_loop::EventLoop,
window::{Window, WindowBuilder},
};
use winit::{application::ApplicationHandler, event::*, event_loop::EventLoop, window::Window};

// Include the bindings generated by build.rs.
// Not all of the binding code will be used.
#[allow(dead_code)]
mod shader;

struct State<'a> {
surface: wgpu::Surface<'a>,
struct State {
window: Arc<Window>,
surface: wgpu::Surface<'static>,
device: wgpu::Device,
queue: wgpu::Queue,
size: winit::dpi::PhysicalSize<u32>,
Expand All @@ -27,13 +24,14 @@ struct State<'a> {
vertex_buffer: wgpu::Buffer,
}

impl<'a> State<'a> {
async fn new(window: &'a Window) -> Self {
impl State {
async fn new(window: Window) -> Self {
let window = Arc::new(window);
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends: wgpu::Backends::all(),
..Default::default()
});
let surface = instance.create_surface(window).unwrap();
let surface = instance.create_surface(window.clone()).unwrap();
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::default(),
Expand Down Expand Up @@ -78,6 +76,7 @@ impl<'a> State<'a> {
module: &shader,
entry_point: ENTRY_FS_MAIN,
targets: &[Some(surface_format.into())],
compilation_options: Default::default(),
}),
primitive: wgpu::PrimitiveState::default(),
depth_stencil: None,
Expand Down Expand Up @@ -182,6 +181,7 @@ impl<'a> State<'a> {
});

Self {
window,
surface,
device,
queue,
Expand Down Expand Up @@ -250,38 +250,63 @@ impl<'a> State<'a> {

fn main() {
let event_loop = EventLoop::new().unwrap();
let window = WindowBuilder::new()
.with_title("wgsl_to_wgpu example")
.build(&event_loop)
.unwrap();

let mut state = block_on(State::new(&window));
event_loop
.run(|event, target| match event {
Event::WindowEvent {
ref event,
window_id,
} if window_id == window.id() => match event {
WindowEvent::CloseRequested => target.exit(),
let mut app = App { state: None };
event_loop.run_app(&mut app).unwrap();
}

struct App {
state: Option<State>,
}

impl ApplicationHandler<()> for App {
fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) {
if self.state.is_some() {
return;
}

let window = event_loop
.create_window(Window::default_attributes().with_title("wgsl_to_wgpu"))
.unwrap();

self.state = Some(block_on(State::new(window)));
}

fn window_event(
&mut self,
event_loop: &winit::event_loop::ActiveEventLoop,
window_id: winit::window::WindowId,
event: WindowEvent,
) {
if event == WindowEvent::CloseRequested {
event_loop.exit();
return;
};

// Window specific event handling.
if let Some(state) = self.state.as_mut() {
if window_id != state.window.id() {
return;
}

match event {
WindowEvent::Resized(physical_size) => {
state.resize(*physical_size);
window.request_redraw();
state.resize(physical_size);
state.window.request_redraw();
}
WindowEvent::ScaleFactorChanged { .. } => {}
WindowEvent::RedrawRequested => {
match state.render() {
Ok(_) => {}
Err(wgpu::SurfaceError::Lost) => state.resize(state.size),
Err(wgpu::SurfaceError::OutOfMemory) => target.exit(),
Err(wgpu::SurfaceError::OutOfMemory) => event_loop.exit(),
Err(e) => eprintln!("{e:?}"),
}
window.request_redraw();
state.window.request_redraw();
}
_ => {
window.request_redraw();
state.window.request_redraw();
}
},
_ => (),
})
.unwrap();
}
}
}
}
1 change: 1 addition & 0 deletions example/src/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ pub fn vertex_state<'a, const N: usize>(
module,
entry_point: entry.entry_point,
buffers: &entry.buffers,
compilation_options: Default::default(),
}
}
pub fn vs_main_entry(vertex_input: wgpu::VertexStepMode) -> VertexEntry<1> {
Expand Down
10 changes: 5 additions & 5 deletions wgsl_to_wgpu/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wgsl_to_wgpu"
version = "0.7.0"
version = "0.8.0"
authors = ["ScanMountGoat <>"]
description = "Generate typesafe Rust bindings for wgsl shaders in wgpu"
license = "MIT"
Expand All @@ -10,15 +10,15 @@ readme = "../README.md"
edition = "2021"

[dependencies]
naga = { version = "0.19.0", features = ["wgsl-in"] }
wgpu-types = "0.19.0"
naga = { version = "0.20.0", features = ["wgsl-in"] }
wgpu-types = "0.20.0"
syn = "2.0"
quote = "1.0"
proc-macro2 = "1.0"
prettyplease = "0.2.17"
prettyplease = "0.2"
thiserror = "1.0"
case = "1.0"

[dev-dependencies]
indoc = "1.0"
pretty_assertions = "1.2.1"
pretty_assertions = "1.4"
3 changes: 2 additions & 1 deletion wgsl_to_wgpu/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ pub fn consts(module: &naga::Module) -> Vec<TokenStream> {
let name = Ident::new(t.name.as_ref()?, Span::call_site());

// TODO: Add support for f64 and f16 once naga supports them.
let type_and_value = match &module.const_expressions[t.init] {
let type_and_value = match &module.global_expressions[t.init] {
naga::Expression::Literal(literal) => match literal {
naga::Literal::F64(v) => Some(quote!(f32 = #v)),
naga::Literal::F32(v) => Some(quote!(f32 = #v)),
naga::Literal::U32(v) => Some(quote!(u32 = #v)),
naga::Literal::I32(v) => Some(quote!(i32 = #v)),
naga::Literal::U64(v) => Some(quote!(u64 = #v)),
naga::Literal::Bool(v) => Some(quote!(bool = #v)),
naga::Literal::I64(v) => Some(quote!(i64 = #v)),
naga::Literal::AbstractInt(v) => Some(quote!(i64 = #v)),
Expand Down
7 changes: 7 additions & 0 deletions wgsl_to_wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ fn create_compute_pipeline(e: &naga::EntryPoint) -> TokenStream {
layout: Some(&layout),
module: &module,
entry_point: #entry_point,
compilation_options: Default::default(),
})
}
}
Expand Down Expand Up @@ -420,6 +421,7 @@ fn vertex_states(module: &naga::Module) -> TokenStream {
module,
entry_point: entry.entry_point,
buffers: &entry.buffers,
compilation_options: Default::default(),
}
}

Expand Down Expand Up @@ -909,6 +911,7 @@ mod test {
layout: Some(&layout),
module: &module,
entry_point: "main1",
compilation_options: Default::default(),
},
)
}
Expand All @@ -923,6 +926,7 @@ mod test {
layout: Some(&layout),
module: &module,
entry_point: "main2",
compilation_options: Default::default(),
},
)
}
Expand Down Expand Up @@ -989,6 +993,7 @@ mod test {
module,
entry_point: entry.entry_point,
buffers: &entry.buffers,
compilation_options: Default::default(),
}
}
pub fn vs_main_entry() -> VertexEntry<0> {
Expand Down Expand Up @@ -1034,6 +1039,7 @@ mod test {
module,
entry_point: entry.entry_point,
buffers: &entry.buffers,
compilation_options: Default::default(),
}
}
pub fn vs_main_1_entry(vertex_input: wgpu::VertexStepMode) -> VertexEntry<1> {
Expand Down Expand Up @@ -1085,6 +1091,7 @@ mod test {
module,
entry_point: entry.entry_point,
buffers: &entry.buffers,
compilation_options: Default::default(),
}
}
pub fn vs_main_entry(input0: wgpu::VertexStepMode, input1: wgpu::VertexStepMode) -> VertexEntry<2> {
Expand Down

0 comments on commit 3cc4829

Please sign in to comment.