Skip to content

Commit

Permalink
Update wgpu to 0.15
Browse files Browse the repository at this point in the history
  • Loading branch information
ElhamAryanpur authored and hecrj committed Apr 13, 2023
1 parent adb70d2 commit d5453c6
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 20 deletions.
37 changes: 23 additions & 14 deletions examples/integration_wgpu/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@ use web_sys::HtmlCanvasElement;
#[cfg(target_arch = "wasm32")]
use winit::platform::web::WindowBuilderExtWebSys;

pub fn main() {
pub fn main() -> Result<(), Box<dyn std::error::Error>> {
#[cfg(target_arch = "wasm32")]
let canvas_element = {
console_log::init_with_level(log::Level::Debug)
.expect("could not initialize logger");
console_log::init_with_level(log::Level::Debug)?;

std::panic::set_hook(Box::new(console_error_panic_hook::hook));

web_sys::window()
.and_then(|win| win.document())
.and_then(|doc| doc.get_element_by_id("iced_canvas"))
.and_then(|element| element.dyn_into::<HtmlCanvasElement>().ok())
.expect("Canvas with id `iced_canvas` is missing")
.and_then(|element| element.dyn_into::<HtmlCanvasElement>().ok())?
};
#[cfg(not(target_arch = "wasm32"))]
env_logger::init();
Expand All @@ -45,11 +44,10 @@ pub fn main() {
#[cfg(target_arch = "wasm32")]
let window = winit::window::WindowBuilder::new()
.with_canvas(Some(canvas_element))
.build(&event_loop)
.expect("Failed to build winit window");
.build(&event_loop)?;

#[cfg(not(target_arch = "wasm32"))]
let window = winit::window::Window::new(&event_loop).unwrap();
let window = winit::window::Window::new(&event_loop)?;

let physical_size = window.inner_size();
let mut viewport = Viewport::with_physical_size(
Expand All @@ -61,7 +59,6 @@ pub fn main() {
let mut clipboard = Clipboard::connect(&window);

// Initialize wgpu

#[cfg(target_arch = "wasm32")]
let default_backend = wgpu::Backends::GL;
#[cfg(not(target_arch = "wasm32"))]
Expand All @@ -70,8 +67,12 @@ pub fn main() {
let backend =
wgpu::util::backend_bits_from_env().unwrap_or(default_backend);

let instance = wgpu::Instance::new(backend);
let surface = unsafe { instance.create_surface(&window) };
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends: backend,
..Default::default()
});

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

let (format, (device, queue)) = futures::executor::block_on(async {
let adapter = wgpu::util::initialize_adapter_from_env_or_default(
Expand All @@ -93,9 +94,15 @@ pub fn main() {

(
surface
.get_supported_formats(&adapter)
.first()
.get_capabilities(&adapter)
.formats
.iter()
.filter(|format| format.describe().srgb)
.copied()
.next()
.or_else(|| {
surface.get_capabilities(&adapter).formats.first().copied()
})
.expect("Get preferred format"),
adapter
.request_device(
Expand All @@ -120,6 +127,7 @@ pub fn main() {
height: physical_size.height,
present_mode: wgpu::PresentMode::AutoVsync,
alpha_mode: wgpu::CompositeAlphaMode::Auto,
view_formats: vec![],
},
);

Expand Down Expand Up @@ -214,7 +222,8 @@ pub fn main() {
width: size.width,
height: size.height,
present_mode: wgpu::PresentMode::AutoVsync,
alpha_mode: wgpu::CompositeAlphaMode::Auto
alpha_mode: wgpu::CompositeAlphaMode::Auto,
view_formats: vec![]
},
);

Expand Down
4 changes: 2 additions & 2 deletions wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ spirv = ["wgpu/spirv"]
webgl = ["wgpu/webgl"]

[dependencies]
wgpu = "0.14"
wgpu_glyph = "0.18"
wgpu = "0.15"
wgpu_glyph = "0.19"
glyph_brush = "0.7"
raw-window-handle = "0.5"
log = "0.4"
Expand Down
2 changes: 2 additions & 0 deletions wgpu/src/image/atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl Atlas {
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8UnormSrgb,
view_formats: &[],
usage: wgpu::TextureUsages::COPY_DST
| wgpu::TextureUsages::COPY_SRC
| wgpu::TextureUsages::TEXTURE_BINDING,
Expand Down Expand Up @@ -247,6 +248,7 @@ impl Atlas {
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8UnormSrgb,
view_formats: &[],
usage: wgpu::TextureUsages::COPY_DST
| wgpu::TextureUsages::COPY_SRC
| wgpu::TextureUsages::TEXTURE_BINDING,
Expand Down
2 changes: 2 additions & 0 deletions wgpu/src/triangle/msaa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ impl Targets {
sample_count,
dimension: wgpu::TextureDimension::D2,
format,
view_formats: &[],
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
});

Expand All @@ -232,6 +233,7 @@ impl Targets {
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format,
view_formats: &[],
usage: wgpu::TextureUsages::RENDER_ATTACHMENT
| wgpu::TextureUsages::TEXTURE_BINDING,
});
Expand Down
25 changes: 21 additions & 4 deletions wgpu/src/window/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ impl<Theme> Compositor<Theme> {
settings: Settings,
compatible_window: Option<&W>,
) -> Option<Self> {
let instance = wgpu::Instance::new(settings.internal_backend);
let instance = wgpu::Instance::new(wgpu::InstanceDescriptor {
backends: settings.internal_backend,
..Default::default()
});

log::info!("{:#?}", settings);

Expand All @@ -46,7 +49,7 @@ impl<Theme> Compositor<Theme> {

#[allow(unsafe_code)]
let compatible_surface = compatible_window
.map(|window| unsafe { instance.create_surface(window) });
.and_then(|window| unsafe { instance.create_surface(window).ok() });

let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
Expand All @@ -63,7 +66,18 @@ impl<Theme> Compositor<Theme> {
log::info!("Selected: {:#?}", adapter.get_info());

let format = compatible_surface.as_ref().and_then(|surface| {
surface.get_supported_formats(&adapter).first().copied()
surface
.get_capabilities(&adapter)
.formats
.iter()
.filter(|format| format.describe().srgb)
.copied()
.next()
.or_else(|| {
log::warn!("No sRGB format found!");

surface.get_capabilities(&adapter).formats.first().copied()
})
})?;

log::info!("Selected format: {:?}", format);
Expand Down Expand Up @@ -144,7 +158,9 @@ impl<Theme> iced_graphics::window::Compositor for Compositor<Theme> {
) -> wgpu::Surface {
#[allow(unsafe_code)]
unsafe {
self.instance.create_surface(window)
self.instance
.create_surface(window)
.expect("Create surface")
}
}

Expand All @@ -163,6 +179,7 @@ impl<Theme> iced_graphics::window::Compositor for Compositor<Theme> {
width,
height,
alpha_mode: wgpu::CompositeAlphaMode::Auto,
view_formats: vec![],
},
);
}
Expand Down

0 comments on commit d5453c6

Please sign in to comment.