Skip to content

Commit

Permalink
Update to wgpu 0.19
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed Jan 17, 2024
1 parent be66fac commit 608d7c8
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 30 deletions.
10 changes: 7 additions & 3 deletions dunge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@ bytemuck = { workspace = true }
glam = { workspace = true }
instant = { version = "0.1", optional = true }
log = { workspace = true }
wgpu = { version = "0.18", default-features = false, features = ["naga"] }

[dependencies.wgpu]
version = "0.19"
default-features = false
features = ["dx12", "naga-ir"]

[dependencies.winit]
version = "0.29"
default-features = false
features = ["rwh_05", "x11"]
features = ["rwh_06", "x11"]
optional = true

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2", features = ["js"] }
wgpu = { version = "0.18", default-features = false, features = ["webgl"] }
wgpu = { version = "0.19", default-features = false, features = ["webgpu"] }

[target.'cfg(target_arch = "wasm32")'.dependencies.web-sys]
version = "0.3"
Expand Down
4 changes: 2 additions & 2 deletions dunge/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ impl Context {
#[derive(Debug)]
pub enum Error {
BackendSelection,
RequestDevice,
RequestDevice(wgpu::RequestDeviceError),
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::BackendSelection => write!(f, "failed to select backend"),
Self::RequestDevice => write!(f, "failed to get device"),
Self::RequestDevice(err) => write!(f, "failed to get device: {err}"),
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions dunge/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ use {
use crate::window::WindowBuilder;

pub(crate) async fn make() -> Result<(Context, Instance), context::Error> {
use wgpu::{Backends, InstanceDescriptor};
use wgpu::{Backends, InstanceDescriptor, InstanceFlags};

let instance = {
let desc = InstanceDescriptor {
backends: Backends::PRIMARY,
backends: Backends::all(),
flags: InstanceFlags::ALLOW_UNDERLYING_NONCOMPLIANT_ADAPTER,
..Default::default()
};

Expand Down
12 changes: 7 additions & 5 deletions dunge/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,20 @@ impl State {
use wgpu::{DeviceDescriptor, Limits};

let desc = DeviceDescriptor {
limits: if cfg!(target_arch = "wasm32") {
Limits::downlevel_webgl2_defaults()
} else {
Limits::default()
required_limits: Limits {
..if cfg!(target_arch = "wasm32") {
Limits::downlevel_webgl2_defaults()
} else {
Limits::default()
}
},
..Default::default()
};

adapter
.request_device(&desc, None)
.await
.map_err(|_| Error::RequestDevice)?
.map_err(Error::RequestDevice)?
};

Ok(Self {
Expand Down
22 changes: 9 additions & 13 deletions dunge/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use {
error, fmt,
future::{Future, IntoFuture},
pin::Pin,
sync::Arc,
task::{self, Poll},
},
wgpu::{
Expand Down Expand Up @@ -72,7 +73,7 @@ impl WindowBuilder {
None => builder.with_fullscreen(Some(Fullscreen::Borderless(None))),
};

builder.build(el.inner())?
Arc::new(builder.build(el.inner())?)
};

let view = View::new(cx.state(), instance, inner)?;
Expand Down Expand Up @@ -138,27 +139,21 @@ impl Window {
}
}

type SharedWindow = Arc<window::Window>;

pub(crate) struct View {
conf: SurfaceConfiguration,
surface: Surface,

// The window must be declared after the surface so
// it gets dropped after it as the surface contains
// unsafe references to the window's resources.
inner: window::Window,
surface: Surface<'static>,
inner: SharedWindow,
}

impl View {
fn new(state: &State, instance: &Instance, inner: window::Window) -> Result<Self, Error> {
fn new(state: &State, instance: &Instance, inner: SharedWindow) -> Result<Self, Error> {
use wgpu::*;

const SUPPORTED_FORMATS: [Format; 2] = [Format::RgbAlpha, Format::BgrAlpha];

// # Safety
//
// The surface needs to live as long as the window that created it.
// State owns the window so this should be safe.
let surface = unsafe { instance.create_surface(&inner)? };
let surface = instance.create_surface(Arc::clone(&inner))?;
let conf = {
let caps = surface.get_capabilities(state.adapter());
let format = SUPPORTED_FORMATS.into_iter().find_map(|format| {
Expand All @@ -178,6 +173,7 @@ impl View {
width: size.width.max(1),
height: size.height.max(1),
present_mode: PresentMode::default(),
desired_maximum_frame_latency: 2,
alpha_mode: CompositeAlphaMode::default(),
view_formats: vec![],
}
Expand Down
2 changes: 1 addition & 1 deletion dunge_shader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ rust-version = { workspace = true }
[dependencies]
glam = { workspace = true }
log = { workspace = true }
naga = "0.14"
naga = "0.19"

[lints]
workspace = true
16 changes: 12 additions & 4 deletions dunge_shader/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,12 @@ impl ScalarType {
}

pub(crate) const fn ty(self) -> Type {
use naga::Scalar;

let (kind, width) = self.inner();
Type {
name: None,
inner: TypeInner::Scalar { kind, width },
inner: TypeInner::Scalar(Scalar { kind, width }),
}
}
}
Expand Down Expand Up @@ -226,12 +228,13 @@ const VEC3I: Type = vec(VectorSize::Tri, ScalarKind::Sint);
const VEC4I: Type = vec(VectorSize::Quad, ScalarKind::Sint);

const fn vec(size: VectorSize, kind: ScalarKind) -> Type {
use naga::Scalar;

Type {
name: None,
inner: TypeInner::Vector {
size,
kind,
width: 4,
scalar: Scalar { kind, width: 4 },
},
}
}
Expand Down Expand Up @@ -298,12 +301,17 @@ const MAT3F: Type = mat(VectorSize::Tri);
const MAT4F: Type = mat(VectorSize::Quad);

const fn mat(size: VectorSize) -> Type {
use naga::Scalar;

Type {
name: None,
inner: TypeInner::Matrix {
columns: size,
rows: size,
width: 4,
scalar: Scalar {
width: 4,
kind: ScalarKind::Float,
},
},
}
}
Expand Down

0 comments on commit 608d7c8

Please sign in to comment.