Skip to content

Commit

Permalink
upgrade dep: wgpu -> 22.1. winit -> 0.30
Browse files Browse the repository at this point in the history
  • Loading branch information
fralonra committed Sep 2, 2024
1 parent 63dfb11 commit 29f1caa
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 110 deletions.
5 changes: 2 additions & 3 deletions crates/wgs_runtime_wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ opt-level = 'z'
anyhow = "1.0"
bytemuck = { version = "1.12", features = ["derive"] }
futures = "0.3"
raw-window-handle = "0.5"
wgpu = "0.17"
wgpu = "22.1"
wgs_core = { version = "0.1", path = "../wgs_core" }
wgs_runtime_base = { version = "0.1", path = "../wgs_runtime_base" }

Expand All @@ -36,4 +35,4 @@ web-sys = { version = "0.3", features = ["HtmlCanvasElement"] }
wgpu = { version = "0.16", features = ["webgl"] }

[dev-dependencies]
winit = "0.28"
winit = "0.30"
124 changes: 71 additions & 53 deletions crates/wgs_runtime_wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,85 @@
//!
//! ## Examples
//!
//! ### Integrate with Winit
//! ### Integrate with Winit 0.30
//!
//! ```no_run
//! use wgs_core::WgsData;
//! use wgs_runtime_wgpu::{Runtime, RuntimeExt};
//! use winit::{
//! event::{Event, WindowEvent},
//! event_loop::{ControlFlow, EventLoop},
//! window::Window,
//! };
//! use winit::event_loop::{ControlFlow, EventLoop};
//!
//! fn main() {
//! let event_loop = EventLoop::new();
//! fn main() -> Result<(), impl std::error::Error> {
//! let event_loop = EventLoop::new()?;
//! event_loop.set_control_flow(ControlFlow::Wait);
//!
//! let window = Window::new(&event_loop).unwrap();
//!
//! let mut runtime =
//! futures::executor::block_on(Runtime::new(&window, WgsData::default(), None)).unwrap();
//!
//! let size = window.inner_size();
//!
//! /// Needs to set the width and height of the runtime before rendering.
//! runtime.resize(size.width as f32, size.height as f32);
//!
//! event_loop.run(move |event, _, control_flow| {
//! *control_flow = ControlFlow::Wait;
//!
//! match event {
//! Event::RedrawEventsCleared => window.request_redraw(),
//! Event::WindowEvent {
//! event: WindowEvent::Resized(size),
//! ..
//! } => {
//! runtime.resize(size.width as f32, size.height as f32);
//!
//! window.request_redraw();
//! }
//! Event::RedrawRequested(_) => {
//! /// Starts a new frame before doing the actual rendering.
//! runtime.frame_start().unwrap();
//!
//! /// The actual rendering for wgs.
//! runtime.render().unwrap();
//!
//! /// To render other stuff on the target surface besides the wgs content.
//! // runtime.render_with(|_device, _queue, _view| {
//! // // Other rendering like ui etc.
//! // }).unwrap();
//! let mut app = app::App::default();
//! event_loop.run_app(&mut app)
//! }
//!
//! /// Remember to finish the current working frame.
//! runtime.frame_finish().unwrap();
//! mod app {
//! use std::sync::Arc;
//! use wgs_core::WgsData;
//! use wgs_runtime_wgpu::{Runtime, RuntimeExt};
//! use winit::{
//! application::ApplicationHandler,
//! event::WindowEvent,
//! event_loop::ActiveEventLoop,
//! window::{Window, WindowId},
//! };
//!
//! #[derive(Default)]
//! pub struct App<'a> {
//! runtime: Option<Runtime<'a>>,
//! window: Option<Arc<Window>>,
//! }
//!
//! impl<'a> ApplicationHandler for App<'a> {
//! fn resumed(&mut self, event_loop: &ActiveEventLoop) {
//! let window = event_loop
//! .create_window(Window::default_attributes())
//! .unwrap();
//! let window = Arc::new(window);
//!
//! let mut runtime =
//! futures::executor::block_on(Runtime::new(window.clone(), WgsData::default(), None))
//! .unwrap();
//! let size = window.inner_size();
//! runtime.resize(size.width as f32, size.height as f32);
//!
//! self.runtime = Some(runtime);
//! self.window = Some(window)
//! }
//!
//! window.request_redraw();
//! fn window_event(
//! &mut self,
//! event_loop: &ActiveEventLoop,
//! _id: WindowId,
//! event: WindowEvent,
//! ) {
//! match event {
//! WindowEvent::CloseRequested => {
//! event_loop.exit();
//! }
//! WindowEvent::RedrawRequested => {
//! if let Some(runtime) = &mut self.runtime {
//! runtime.frame_start().unwrap();
//!
//! runtime.render().unwrap();
//!
//! runtime.frame_finish().unwrap();
//! }
//!
//! if let Some(window) = &self.window {
//! window.request_redraw();
//! }
//! }
//! WindowEvent::Resized(size) => {
//! if let Some(runtime) = &mut self.runtime {
//! runtime.resize(size.width as f32, size.height as f32);
//! }
//! }
//! _ => (),
//! }
//! Event::WindowEvent {
//! event: WindowEvent::CloseRequested,
//! ..
//! } => *control_flow = ControlFlow::Exit,
//! _ => {}
//! }
//! });
//! }
//! }
//! ```
//!
Expand Down
43 changes: 26 additions & 17 deletions crates/wgs_runtime_wgpu/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const U8_SIZE: u32 = std::mem::size_of::<u8>() as u32;
const UNIFORM_GROUP_ID: u32 = 0;

/// The wgpu wgs runtime.
pub struct Runtime {
pub struct Runtime<'w> {
#[cfg(not(target_arch = "wasm32"))]
captured_callback: Option<(Viewport, Box<dyn FnOnce(&mut Self, u32, u32, Vec<u8>)>)>,
device: wgpu::Device,
Expand All @@ -24,7 +24,7 @@ pub struct Runtime {
queue: wgpu::Queue,
sampler: wgpu::Sampler,
shader_vert: String,
surface: wgpu::Surface,
surface: wgpu::Surface<'w>,
surface_configuration: wgpu::SurfaceConfiguration,
surface_texture: Option<wgpu::SurfaceTexture>,
texture_bind_groups: Vec<(wgpu::BindGroupLayout, wgpu::BindGroup)>,
Expand All @@ -39,7 +39,7 @@ pub struct Runtime {
width: f32,
}

impl RuntimeExt for Runtime {
impl RuntimeExt for Runtime<'_> {
fn add_texture(&mut self, width: u32, height: u32, buffer: Vec<u8>) {
self.texture_bind_groups.push(create_texture(
&self.device,
Expand Down Expand Up @@ -149,10 +149,12 @@ impl RuntimeExt for Runtime {
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Load,
store: true,
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
});

if let Some(viewport) = &self.viewport {
Expand Down Expand Up @@ -255,7 +257,7 @@ impl RuntimeExt for Runtime {
}
}

impl Runtime {
impl<'w> Runtime<'w> {
#[cfg(target_arch = "wasm32")]
pub async fn new(
canvas: web_sys::HtmlCanvasElement,
Expand Down Expand Up @@ -294,9 +296,9 @@ impl Runtime {
/// }
/// ```
#[cfg(not(target_arch = "wasm32"))]
pub async fn new<W>(w: &W, wgs: WgsData, viewport: Option<Viewport>) -> Result<Self>
pub async fn new<W>(w: W, wgs: WgsData, viewport: Option<Viewport>) -> Result<Self>
where
W: raw_window_handle::HasRawWindowHandle + raw_window_handle::HasRawDisplayHandle,
W: wgpu::WindowHandle + 'w,
{
let instance = init_instance();

Expand All @@ -310,7 +312,7 @@ impl Runtime {
wgs: WgsData,
viewport: Option<Viewport>,
instance: wgpu::Instance,
surface: wgpu::Surface,
surface: wgpu::Surface<'w>,
) -> Result<Self> {
let (surface_configuration, device, queue) = init_adapter(&instance, &surface).await?;

Expand Down Expand Up @@ -606,17 +608,20 @@ fn build_pipeline(
vertex: wgpu::VertexState {
module: &vs_module,
entry_point: "main",
compilation_options: wgpu::PipelineCompilationOptions::default(),
buffers: &[],
},
fragment: Some(wgpu::FragmentState {
module: &fs_module,
entry_point: "main",
compilation_options: wgpu::PipelineCompilationOptions::default(),
targets: &[Some(format.into())],
}),
primitive: wgpu::PrimitiveState::default(),
depth_stencil: None,
multisample: wgpu::MultisampleState::default(),
multiview: None,
cache: None,
});

Ok(pipeline)
Expand Down Expand Up @@ -706,9 +711,9 @@ fn create_texture(
(texture_bind_group_layout, texture_bind_group)
}

async fn init_adapter(
async fn init_adapter<'w>(
instance: &wgpu::Instance,
surface: &wgpu::Surface,
surface: &wgpu::Surface<'w>,
) -> Result<(wgpu::SurfaceConfiguration, wgpu::Device, wgpu::Queue)> {
if let Some(adapter) = instance
.request_adapter(&wgpu::RequestAdapterOptions {
Expand All @@ -725,11 +730,12 @@ async fn init_adapter(
.request_device(
&wgpu::DeviceDescriptor {
label: Some("Device Descriptor"),
features: adapter_features & wgpu::Features::default(),
required_features: adapter_features & wgpu::Features::default(),
#[cfg(target_arch = "wasm32")]
limits: wgpu::Limits::downlevel_webgl2_defaults(),
required_limits: wgpu::Limits::downlevel_webgl2_defaults(),
#[cfg(not(target_arch = "wasm32"))]
limits: wgpu::Limits::downlevel_defaults(),
required_limits: wgpu::Limits::downlevel_defaults(),
memory_hints: wgpu::MemoryHints::default(),
},
None,
)
Expand All @@ -746,7 +752,9 @@ fn init_instance() -> wgpu::Instance {

let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends,
flags: wgpu::InstanceFlags::debugging(),
dx12_shader_compiler: wgpu::Dx12Compiler::default(),
gles_minor_version: wgpu::Gles3MinorVersion::Automatic,
});

instance
Expand All @@ -763,11 +771,11 @@ fn init_surface(
}

#[cfg(not(target_arch = "wasm32"))]
fn init_surface<W>(instance: &wgpu::Instance, w: &W) -> Result<wgpu::Surface>
fn init_surface<'w, W>(instance: &wgpu::Instance, w: W) -> Result<wgpu::Surface<'w>>
where
W: raw_window_handle::HasRawWindowHandle + raw_window_handle::HasRawDisplayHandle,
W: wgpu::WindowHandle + 'w,
{
let surface = unsafe { instance.create_surface(w)? };
let surface = instance.create_surface(w)?;

Ok(surface)
}
Expand Down Expand Up @@ -803,6 +811,7 @@ fn init_surface_configuration(
width: 0,
height: 0,
present_mode: wgpu::PresentMode::AutoVsync,
desired_maximum_frame_latency: 2,
alpha_mode: wgpu::CompositeAlphaMode::Auto,
view_formats: vec![format],
}
Expand Down Expand Up @@ -957,7 +966,7 @@ fn trim_image_buffer(viewport: &Viewport, align_width: usize, buffer: &[u8]) ->
async fn view_into_buffer(
device: &wgpu::Device,
viewport: &Viewport,
raw_width: u32,
_raw_width: u32,
raw_height: u32,
raw_buffer: &wgpu::Buffer,
) -> Result<Vec<u8>> {
Expand Down
2 changes: 1 addition & 1 deletion crates/winit_demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ publish = false
futures = "0.3"
wgs_core = { path = "../wgs_core" }
wgs_runtime_wgpu = { path = "../wgs_runtime_wgpu" }
winit = "0.28"
winit = "0.30"
Loading

0 comments on commit 29f1caa

Please sign in to comment.