Update wgpu in integration example

This commit is contained in:
Héctor Ramón Jiménez 2020-08-27 14:55:41 +02:00
parent bb5f034e08
commit bae0a3e46e
2 changed files with 58 additions and 40 deletions

View File

@ -7,6 +7,7 @@ use scene::Scene;
use iced_wgpu::{wgpu, Backend, Renderer, Settings, Viewport}; use iced_wgpu::{wgpu, Backend, Renderer, Settings, Viewport};
use iced_winit::{conversion, futures, program, winit, Debug, Size}; use iced_winit::{conversion, futures, program, winit, Debug, Size};
use futures::task::SpawnExt;
use winit::{ use winit::{
dpi::PhysicalPosition, dpi::PhysicalPosition,
event::{Event, ModifiersState, WindowEvent}, event::{Event, ModifiersState, WindowEvent},
@ -29,26 +30,29 @@ pub fn main() {
let mut modifiers = ModifiersState::default(); let mut modifiers = ModifiersState::default();
// Initialize wgpu // Initialize wgpu
let surface = wgpu::Surface::create(&window); let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY);
let surface = unsafe { instance.create_surface(&window) };
let (mut device, queue) = futures::executor::block_on(async { let (mut device, queue) = futures::executor::block_on(async {
let adapter = wgpu::Adapter::request( let adapter = instance
&wgpu::RequestAdapterOptions { .request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::Default, power_preference: wgpu::PowerPreference::Default,
compatible_surface: Some(&surface), compatible_surface: Some(&surface),
},
wgpu::BackendBit::PRIMARY,
)
.await
.expect("Request adapter");
adapter
.request_device(&wgpu::DeviceDescriptor {
extensions: wgpu::Extensions {
anisotropic_filtering: false,
},
limits: wgpu::Limits::default(),
}) })
.await .await
.expect("Request adapter");
adapter
.request_device(
&wgpu::DeviceDescriptor {
features: wgpu::Features::empty(),
limits: wgpu::Limits::default(),
shader_validation: false,
},
None,
)
.await
.expect("Request device")
}); });
let format = wgpu::TextureFormat::Bgra8UnormSrgb; let format = wgpu::TextureFormat::Bgra8UnormSrgb;
@ -69,6 +73,10 @@ pub fn main() {
}; };
let mut resized = false; let mut resized = false;
// Initialize staging belt and local pool
let mut staging_belt = wgpu::util::StagingBelt::new(5 * 1024);
let mut local_pool = futures::executor::LocalPool::new();
// Initialize scene and GUI controls // Initialize scene and GUI controls
let scene = Scene::new(&mut device); let scene = Scene::new(&mut device);
let controls = Controls::new(); let controls = Controls::new();
@ -160,7 +168,7 @@ pub fn main() {
resized = false; resized = false;
} }
let frame = swap_chain.get_next_texture().expect("Next frame"); let frame = swap_chain.get_current_frame().expect("Next frame");
let mut encoder = device.create_command_encoder( let mut encoder = device.create_command_encoder(
&wgpu::CommandEncoderDescriptor { label: None }, &wgpu::CommandEncoderDescriptor { label: None },
@ -171,7 +179,7 @@ pub fn main() {
{ {
// We clear the frame // We clear the frame
let mut render_pass = scene.clear( let mut render_pass = scene.clear(
&frame.view, &frame.output.view,
&mut encoder, &mut encoder,
program.background_color(), program.background_color(),
); );
@ -183,22 +191,32 @@ pub fn main() {
// And then iced on top // And then iced on top
let mouse_interaction = renderer.backend_mut().draw( let mouse_interaction = renderer.backend_mut().draw(
&mut device, &mut device,
&mut staging_belt,
&mut encoder, &mut encoder,
&frame.view, &frame.output.view,
&viewport, &viewport,
state.primitive(), state.primitive(),
&debug.overlay(), &debug.overlay(),
); );
// Then we submit the work // Then we submit the work
queue.submit(&[encoder.finish()]); staging_belt.finish();
queue.submit(Some(encoder.finish()));
// And update the mouse cursor // Update the mouse cursor
window.set_cursor_icon( window.set_cursor_icon(
iced_winit::conversion::mouse_interaction( iced_winit::conversion::mouse_interaction(
mouse_interaction, mouse_interaction,
), ),
); );
// And recall staging buffers
local_pool
.spawner()
.spawn(staging_belt.recall())
.expect("Recall staging buffers");
local_pool.run_until_stalled();
} }
_ => {} _ => {}
} }

View File

@ -22,17 +22,18 @@ impl Scene {
color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor {
attachment: target, attachment: target,
resolve_target: None, resolve_target: None,
load_op: wgpu::LoadOp::Clear, ops: wgpu::Operations {
store_op: wgpu::StoreOp::Store, load: wgpu::LoadOp::Clear({
clear_color: { let [r, g, b, a] = background_color.into_linear();
let [r, g, b, a] = background_color.into_linear();
wgpu::Color { wgpu::Color {
r: r as f64, r: r as f64,
g: g as f64, g: g as f64,
b: b as f64, b: b as f64,
a: a as f64, a: a as f64,
} }
}),
store: true,
}, },
}], }],
depth_stencil_attachment: None, depth_stencil_attachment: None,
@ -46,25 +47,23 @@ impl Scene {
} }
fn build_pipeline(device: &wgpu::Device) -> wgpu::RenderPipeline { fn build_pipeline(device: &wgpu::Device) -> wgpu::RenderPipeline {
let vs = include_bytes!("shader/vert.spv"); let vs_module =
let fs = include_bytes!("shader/frag.spv"); device.create_shader_module(wgpu::include_spirv!("shader/vert.spv"));
let vs_module = device.create_shader_module( let fs_module =
&wgpu::read_spirv(std::io::Cursor::new(&vs[..])).unwrap(), device.create_shader_module(wgpu::include_spirv!("shader/frag.spv"));
);
let fs_module = device.create_shader_module(
&wgpu::read_spirv(std::io::Cursor::new(&fs[..])).unwrap(),
);
let pipeline_layout = let pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: None,
push_constant_ranges: &[],
bind_group_layouts: &[], bind_group_layouts: &[],
}); });
let pipeline = let pipeline =
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
layout: &pipeline_layout, label: None,
layout: Some(&pipeline_layout),
vertex_stage: wgpu::ProgrammableStageDescriptor { vertex_stage: wgpu::ProgrammableStageDescriptor {
module: &vs_module, module: &vs_module,
entry_point: "main", entry_point: "main",
@ -79,6 +78,7 @@ fn build_pipeline(device: &wgpu::Device) -> wgpu::RenderPipeline {
depth_bias: 0, depth_bias: 0,
depth_bias_slope_scale: 0.0, depth_bias_slope_scale: 0.0,
depth_bias_clamp: 0.0, depth_bias_clamp: 0.0,
..Default::default()
}), }),
primitive_topology: wgpu::PrimitiveTopology::TriangleList, primitive_topology: wgpu::PrimitiveTopology::TriangleList,
color_states: &[wgpu::ColorStateDescriptor { color_states: &[wgpu::ColorStateDescriptor {