Benedikt Mandelkow 2020-10-13 19:59:59 +02:00 committed by 4bb4
parent 99123d7b8d
commit bce1ad26f4
2 changed files with 52 additions and 50 deletions

View file

@ -1,5 +1,5 @@
[package] [package]
name = "wgpu_plotting" name = "implot-wgpu-examples"
version = "0.1.0" version = "0.1.0"
authors = ["Benedikt Mandelkow <benedikt.mandelkow@rwth-aachen.de>", "imgui-wgpu contributors"] authors = ["Benedikt Mandelkow <benedikt.mandelkow@rwth-aachen.de>", "imgui-wgpu contributors"]
edition = "2018" edition = "2018"

View file

@ -1,7 +1,9 @@
use imgui::{im_str, CollapsingHeader, Condition, FontSource}; use futures::executor::block_on;
use imgui::*;
use imgui_wgpu::RendererConfig; use imgui_wgpu::RendererConfig;
use std::time::Instant; use std::time::Instant;
use winit::{ use winit::{
dpi::LogicalSize,
event::{Event, WindowEvent}, event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop}, event_loop::{ControlFlow, EventLoop},
window::Window, window::Window,
@ -10,41 +12,45 @@ use winit::{
// the actual implot samples are in there // the actual implot samples are in there
mod ui; mod ui;
async fn run(event_loop: EventLoop<()>, window: Window, swapchain_format: wgpu::TextureFormat) { fn main() {
let size = window.inner_size(); // Set up window and GPU
let event_loop = EventLoop::new();
let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY); let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY);
let surface = unsafe { instance.create_surface(&window) };
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::Default,
// Request an adapter which can render to our surface
compatible_surface: Some(&surface),
})
.await
.expect("Failed to find an appropiate adapter");
// Create the logical device and command queue let (window, size, surface) = {
let (device, queue) = adapter let window = Window::new(&event_loop).unwrap();
.request_device( window.set_inner_size(LogicalSize {
width: 1280.0,
height: 720.0,
});
window.set_title(&"implot-wgpu".to_string());
let size = window.inner_size();
let surface = unsafe { instance.create_surface(&window) };
(window, size, surface)
};
let adapter = block_on(instance.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::HighPerformance,
compatible_surface: Some(&surface),
}))
.unwrap();
let (device, queue) = block_on(adapter.request_device(
&wgpu::DeviceDescriptor { &wgpu::DeviceDescriptor {
features: wgpu::Features::empty(), features: wgpu::Features::empty(),
limits: wgpu::Limits::default(), limits: wgpu::Limits::default(),
shader_validation: true, shader_validation: false,
}, },
None, None,
) ))
.await .unwrap();
.expect("Failed to create device");
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: None,
bind_group_layouts: &[],
push_constant_ranges: &[],
});
// Set up swap chain
let mut sc_desc = wgpu::SwapChainDescriptor { let mut sc_desc = wgpu::SwapChainDescriptor {
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT, usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
format: swapchain_format, format: wgpu::TextureFormat::Bgra8Unorm,
width: size.width, width: size.width,
height: size.height, height: size.height,
present_mode: wgpu::PresentMode::Mailbox, present_mode: wgpu::PresentMode::Mailbox,
@ -64,7 +70,7 @@ async fn run(event_loop: EventLoop<()>, window: Window, swapchain_format: wgpu::
); );
imgui.set_ini_filename(None); imgui.set_ini_filename(None);
let hidpi_factor = window.scale_factor(); let mut hidpi_factor = window.scale_factor();
let font_size = (13.0 * hidpi_factor) as f32; let font_size = (13.0 * hidpi_factor) as f32;
imgui.io_mut().font_global_scale = (1.0 / hidpi_factor) as f32; imgui.io_mut().font_global_scale = (1.0 / hidpi_factor) as f32;
@ -78,29 +84,32 @@ async fn run(event_loop: EventLoop<()>, window: Window, swapchain_format: wgpu::
}), }),
}]); }]);
let style = imgui.style_mut(); //
style.use_classic_colors(); // Set up dear imgui wgpu renderer
//
let mut renderer = RendererConfig::new() let mut renderer = RendererConfig::new()
.set_texture_format(sc_desc.format) .set_texture_format(sc_desc.format)
.build(&mut imgui, &device, &queue); .build(&mut imgui, &device, &queue);
let mut last_frame = Instant::now(); let mut last_frame = Instant::now();
let mut last_cursor = None; let mut last_cursor = None;
let mut showing_demo = false; let mut showing_demo = false;
let mut make_fullscreen = false; let mut make_fullscreen = false;
// Event loop
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
// Have the closure take ownership of the resources. *control_flow = ControlFlow::Poll;
// `event_loop.run` never returns, therefore we must do this to ensure
// the resources are properly cleaned up.
let _ = (&instance, &adapter, &pipeline_layout);
let plot_ui = implot.get_plot_ui(); let plot_ui = implot.get_plot_ui();
*control_flow = ControlFlow::Poll;
match event { match event {
Event::WindowEvent {
event: WindowEvent::ScaleFactorChanged { scale_factor, .. },
..
} => {
hidpi_factor = scale_factor;
}
Event::WindowEvent { Event::WindowEvent {
event: WindowEvent::Resized(size), event: WindowEvent::Resized(size),
.. ..
@ -202,7 +211,7 @@ async fn run(event_loop: EventLoop<()>, window: Window, swapchain_format: wgpu::
ops: wgpu::Operations { ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color { load: wgpu::LoadOp::Clear(wgpu::Color {
r: 0.1, r: 0.1,
g: 0.2, g: 0.4,
b: 0.3, b: 0.3,
a: 1.0, a: 1.0,
}), }),
@ -220,16 +229,9 @@ async fn run(event_loop: EventLoop<()>, window: Window, swapchain_format: wgpu::
queue.submit(Some(encoder.finish())); queue.submit(Some(encoder.finish()));
} }
_ => {} _ => (),
} }
platform.handle_event(imgui.io_mut(), &window, &event); platform.handle_event(imgui.io_mut(), &window, &event);
}); });
} }
fn main() {
let event_loop = EventLoop::new();
let window = winit::window::Window::new(&event_loop).unwrap();
// Temporarily avoid srgb formats for the swapchain on the web
futures::executor::block_on(run(event_loop, window, wgpu::TextureFormat::Bgra8UnormSrgb));
}