Skip to content

Commit

Permalink
Merge pull request #107 from mkrasnitski/master
Browse files Browse the repository at this point in the history
Update wgpu to 0.19
  • Loading branch information
Yatekii authored Sep 16, 2024
2 parents 798a498 + 98d015b commit e2e6335
Show file tree
Hide file tree
Showing 5 changed files with 381 additions and 411 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Per Keep a Changelog there are 6 main categories of changes:
## Unreleased

- Internal: Fixed Scissor-Rect to not span across Framebuffersize, by limiting to framebuffer width. @PixelboysTM
- Bump wgpu version to 0.18. @calcoph
- Bump wgpu version to 0.19. @mkrasnitski and @calcoph

## v0.24.0

Expand Down
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,20 @@ min = 0 # allow non-first increment

[dependencies]
bytemuck = "1"
imgui = "0.11"
imgui = "0.12"
log = "0.4"
smallvec = "1"
wgpu = "0.18"
wgpu = "0.19"

[dev-dependencies]
bytemuck = { version = "1.13", features = ["derive"] }
cgmath = "0.18"
env_logger = "0.10"
image = { version = "0.24", default-features = false, features = ["png"] }
imgui-winit-support = "0.11"
imgui-winit-support = "0.12"
pollster = "0.3"
raw-window-handle = "0.5"
winit = "0.27.5"
winit = "0.29"

[package.metadata.docs.rs]
all-features = true
294 changes: 144 additions & 150 deletions examples/cube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use std::time::Instant;
use wgpu::{include_wgsl, util::DeviceExt, Extent3d};
use winit::{
dpi::LogicalSize,
event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::Window,
keyboard::{Key, NamedKey},
window::WindowBuilder,
};

const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = cgmath::Matrix4::new(
Expand Down Expand Up @@ -332,28 +333,25 @@ fn main() {
env_logger::init();

// Set up window and GPU
let event_loop = EventLoop::new();
let event_loop = EventLoop::new().unwrap();

let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends: wgpu::Backends::PRIMARY,
..Default::default()
});

let (window, size, surface) = {
let window = {
let version = env!("CARGO_PKG_VERSION");

let window = Window::new(&event_loop).unwrap();
window.set_inner_size(LogicalSize {
width: 1280.0,
height: 720.0,
});
window.set_title(&format!("imgui-wgpu {version}"));
let size = window.inner_size();

let surface = unsafe { instance.create_surface(&window) }.unwrap();

(window, size, surface)
let size = LogicalSize::new(1280.0, 720.0);
WindowBuilder::new()
.with_inner_size(size)
.with_title(&format!("imgui-wgpu {version}"))
.build(&event_loop)
.unwrap()
};
let size = window.inner_size();
let surface = instance.create_surface(&window).unwrap();

let hidpi_factor = window.scale_factor();

Expand All @@ -374,6 +372,7 @@ fn main() {
width: size.width,
height: size.height,
present_mode: wgpu::PresentMode::Fifo,
desired_maximum_frame_latency: 2,
alpha_mode: wgpu::CompositeAlphaMode::Auto,
view_formats: vec![wgpu::TextureFormat::Bgra8Unorm],
};
Expand Down Expand Up @@ -443,149 +442,144 @@ fn main() {
let example_texture_id = renderer.textures.insert(texture);

// Event loop
event_loop.run(move |event, _, control_flow| {
*control_flow = if cfg!(feature = "metal-auto-capture") {
ControlFlow::Exit
let _ = event_loop.run(|event, elwt| {
if cfg!(feature = "metal-auto-capture") {
elwt.exit();
} else {
ControlFlow::Poll
elwt.set_control_flow(ControlFlow::Poll);
};
match event {
Event::WindowEvent {
event: WindowEvent::Resized(size),
..
} => {
let surface_desc = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
format: wgpu::TextureFormat::Bgra8UnormSrgb,
width: size.width,
height: size.height,
present_mode: wgpu::PresentMode::Fifo,
alpha_mode: wgpu::CompositeAlphaMode::Auto,
view_formats: vec![wgpu::TextureFormat::Bgra8Unorm],
};

surface.configure(&device, &surface_desc);
}
Event::WindowEvent {
event:
WindowEvent::KeyboardInput {
input:
KeyboardInput {
virtual_keycode: Some(VirtualKeyCode::Escape),
state: ElementState::Pressed,
..
},
..
},
..
}
| Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => {
*control_flow = ControlFlow::Exit;
}
Event::MainEventsCleared => window.request_redraw(),
Event::RedrawEventsCleared => {
let now = Instant::now();
imgui.io_mut().update_delta_time(now - last_frame);
last_frame = now;

let frame = match surface.get_current_texture() {
Ok(frame) => frame,
Err(e) => {
eprintln!("dropped frame: {e:?}");
return;
Event::AboutToWait => window.request_redraw(),
Event::WindowEvent { ref event, .. } => {
match event {
WindowEvent::Resized(size) => {
let surface_desc = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
format: wgpu::TextureFormat::Bgra8UnormSrgb,
width: size.width,
height: size.height,
present_mode: wgpu::PresentMode::Fifo,
desired_maximum_frame_latency: 2,
alpha_mode: wgpu::CompositeAlphaMode::Auto,
view_formats: vec![wgpu::TextureFormat::Bgra8Unorm],
};

surface.configure(&device, &surface_desc);
}
WindowEvent::CloseRequested => elwt.exit(),
WindowEvent::KeyboardInput { event, .. } => {
if let Key::Named(NamedKey::Escape) = event.logical_key {
if event.state.is_pressed() {
elwt.exit();
}
}
}
};
platform
.prepare_frame(imgui.io_mut(), &window)
.expect("Failed to prepare frame");
let ui = imgui.frame();

let view = frame
.texture
.create_view(&wgpu::TextureViewDescriptor::default());

// Render example normally at background
example.update(ui.io().delta_time);
example.setup_camera(&queue, ui.io().display_size);
example.render(&view, &device, &queue);

// Store the new size of Image() or None to indicate that the window is collapsed.
let mut new_example_size: Option<[f32; 2]> = None;

ui.window("Cube")
.size([512.0, 512.0], Condition::FirstUseEver)
.build(|| {
new_example_size = Some(ui.content_region_avail());
imgui::Image::new(example_texture_id, new_example_size.unwrap()).build(ui);
});

if let Some(size) = new_example_size {
// Resize render target, which is optional
if size != example_size && size[0] >= 1.0 && size[1] >= 1.0 {
example_size = size;
let scale = &ui.io().display_framebuffer_scale;
let texture_config = TextureConfig {
size: Extent3d {
width: (example_size[0] * scale[0]) as u32,
height: (example_size[1] * scale[1]) as u32,
..Default::default()
},
usage: wgpu::TextureUsages::RENDER_ATTACHMENT
| wgpu::TextureUsages::TEXTURE_BINDING,
..Default::default()
WindowEvent::RedrawRequested => {
let now = Instant::now();
imgui.io_mut().update_delta_time(now - last_frame);
last_frame = now;

let frame = match surface.get_current_texture() {
Ok(frame) => frame,
Err(e) => {
eprintln!("dropped frame: {e:?}");
return;
}
};
renderer.textures.replace(
example_texture_id,
Texture::new(&device, &renderer, texture_config),
);
platform
.prepare_frame(imgui.io_mut(), &window)
.expect("Failed to prepare frame");
let ui = imgui.frame();

let view = frame
.texture
.create_view(&wgpu::TextureViewDescriptor::default());

// Render example normally at background
example.update(ui.io().delta_time);
example.setup_camera(&queue, ui.io().display_size);
example.render(&view, &device, &queue);

// Store the new size of Image() or None to indicate that the window is collapsed.
let mut new_example_size: Option<[f32; 2]> = None;

ui.window("Cube")
.size([512.0, 512.0], Condition::FirstUseEver)
.build(|| {
new_example_size = Some(ui.content_region_avail());
imgui::Image::new(example_texture_id, new_example_size.unwrap())
.build(ui);
});

if let Some(size) = new_example_size {
// Resize render target, which is optional
if size != example_size && size[0] >= 1.0 && size[1] >= 1.0 {
example_size = size;
let scale = &ui.io().display_framebuffer_scale;
let texture_config = TextureConfig {
size: Extent3d {
width: (example_size[0] * scale[0]) as u32,
height: (example_size[1] * scale[1]) as u32,
..Default::default()
},
usage: wgpu::TextureUsages::RENDER_ATTACHMENT
| wgpu::TextureUsages::TEXTURE_BINDING,
..Default::default()
};
renderer.textures.replace(
example_texture_id,
Texture::new(&device, &renderer, texture_config),
);
}

// Only render example to example_texture if thw window is not collapsed
example.setup_camera(&queue, size);
example.render(
renderer.textures.get(example_texture_id).unwrap().view(),
&device,
&queue,
);
}

let mut encoder: wgpu::CommandEncoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: None,
});

if last_cursor != Some(ui.mouse_cursor()) {
last_cursor = Some(ui.mouse_cursor());
platform.prepare_render(ui, &window);
}

let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Load, // Do not clear
// load: wgpu::LoadOp::Clear(clear_color),
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
});

renderer
.render(imgui.render(), &queue, &device, &mut rpass)
.expect("Rendering failed");

drop(rpass);

queue.submit(Some(encoder.finish()));
frame.present();
}

// Only render example to example_texture if thw window is not collapsed
example.setup_camera(&queue, size);
example.render(
renderer.textures.get(example_texture_id).unwrap().view(),
&device,
&queue,
);
_ => {}
}

let mut encoder: wgpu::CommandEncoder =
device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });

if last_cursor != Some(ui.mouse_cursor()) {
last_cursor = Some(ui.mouse_cursor());
platform.prepare_render(ui, &window);
}

let mut rpass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
label: None,
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
view: &view,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Load, // Do not clear
// load: wgpu::LoadOp::Clear(clear_color),
store: wgpu::StoreOp::Store,
},
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
});

renderer
.render(imgui.render(), &queue, &device, &mut rpass)
.expect("Rendering failed");

drop(rpass);

queue.submit(Some(encoder.finish()));
frame.present();
}
_ => (),
_ => {}
}

platform.handle_event(imgui.io_mut(), &window, &event);
Expand Down
Loading

0 comments on commit e2e6335

Please sign in to comment.