diff --git a/Cargo.lock b/Cargo.lock index d633f8153b..9cdcedba6d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -41,15 +41,6 @@ version = "1.0.52" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "84450d0b4a8bd1ba4144ce8ce718fbc5d071358b1e5384bace6536b3d1f2d5b3" -[[package]] -name = "approx" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f2a05fd1bd10b2527e20a2cd32d8873d115b8b39fe219ee25f42a8aca6ba278" -dependencies = [ - "num-traits 0.2.14", -] - [[package]] name = "arrayvec" version = "0.7.1" @@ -250,16 +241,6 @@ dependencies = [ "libc", ] -[[package]] -name = "cgmath" -version = "0.18.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a98d30140e3296250832bbaaff83b27dcd6fa3cc70fb6f1f3e5c9c0023b5317" -dependencies = [ - "approx", - "num-traits 0.2.14", -] - [[package]] name = "cocoa" version = "0.24.0" @@ -672,6 +653,12 @@ dependencies = [ "xml-rs", ] +[[package]] +name = "glam" +version = "0.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4fa84eead97d5412b2a20aed4d66612a97a9e41e08eababdb9ae2bf88667490" + [[package]] name = "glow" version = "0.11.1" @@ -2070,11 +2057,11 @@ dependencies = [ "async-executor", "bitflags", "bytemuck", - "cgmath", "console_error_panic_hook", "console_log", "ddsfile", "env_logger", + "glam", "js-sys", "log", "naga", diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index 4f8a51ad80..895b76262d 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -121,7 +121,7 @@ smallvec = "1" [dev-dependencies] bitflags = "1" bytemuck = { version = "1.4", features = ["derive"] } -cgmath = "0.18" +glam = "0.20.2" ddsfile = "0.5" log = "0.4" # Opt out of noise's "default-features" to avoid "image" feature as a dependency count optimization. diff --git a/wgpu/examples/bunnymark/main.rs b/wgpu/examples/bunnymark/main.rs index e7b1a464dc..d6605172ec 100644 --- a/wgpu/examples/bunnymark/main.rs +++ b/wgpu/examples/bunnymark/main.rs @@ -176,7 +176,7 @@ impl framework::Example for Example { }); let globals = Globals { - mvp: cgmath::ortho( + mvp: glam::Mat4::orthographic_rh( 0.0, config.width as f32, 0.0, @@ -184,7 +184,7 @@ impl framework::Example for Example { -1.0, 1.0, ) - .into(), + .to_cols_array_2d(), size: [BUNNY_SIZE; 2], pad: [0.0; 2], }; diff --git a/wgpu/examples/cube/main.rs b/wgpu/examples/cube/main.rs index 60a4954270..b1d8c48931 100644 --- a/wgpu/examples/cube/main.rs +++ b/wgpu/examples/cube/main.rs @@ -2,7 +2,7 @@ mod framework; use bytemuck::{Pod, Zeroable}; -use std::{borrow::Cow, future::Future, mem, pin::Pin, task}; +use std::{borrow::Cow, f32::consts, future::Future, mem, pin::Pin, task}; use wgpu::util::DeviceExt; #[repr(C)] @@ -111,15 +111,14 @@ struct Example { } impl Example { - fn generate_matrix(aspect_ratio: f32) -> cgmath::Matrix4 { - let mx_projection = cgmath::perspective(cgmath::Deg(45f32), aspect_ratio, 1.0, 10.0); - let mx_view = cgmath::Matrix4::look_at_rh( - cgmath::Point3::new(1.5f32, -5.0, 3.0), - cgmath::Point3::new(0f32, 0.0, 0.0), - cgmath::Vector3::unit_z(), + fn generate_matrix(aspect_ratio: f32) -> glam::Mat4 { + let projection = glam::Mat4::perspective_rh(consts::FRAC_PI_4, aspect_ratio, 1.0, 10.0); + let view = glam::Mat4::look_at_rh( + glam::Vec3::new(1.5f32, -5.0, 3.0), + glam::Vec3::ZERO, + glam::Vec3::Z, ); - let mx_correction = framework::OPENGL_TO_WGPU_MATRIX; - mx_correction * mx_projection * mx_view + projection * view } } diff --git a/wgpu/examples/framework.rs b/wgpu/examples/framework.rs index 3a50656a4c..08dc16ad72 100644 --- a/wgpu/examples/framework.rs +++ b/wgpu/examples/framework.rs @@ -10,15 +10,6 @@ use winit::{ #[path = "../tests/common/mod.rs"] pub mod test_common; -#[rustfmt::skip] -#[allow(unused)] -pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4 = cgmath::Matrix4::new( - 1.0, 0.0, 0.0, 0.0, - 0.0, 1.0, 0.0, 0.0, - 0.0, 0.0, 0.5, 0.0, - 0.0, 0.0, 0.5, 1.0, -); - #[allow(dead_code)] pub fn cast_slice(data: &[T]) -> &[u8] { use std::{mem::size_of, slice::from_raw_parts}; diff --git a/wgpu/examples/mipmap/main.rs b/wgpu/examples/mipmap/main.rs index 2be93475a0..cdab3690c0 100644 --- a/wgpu/examples/mipmap/main.rs +++ b/wgpu/examples/mipmap/main.rs @@ -2,7 +2,7 @@ mod framework; use bytemuck::{Pod, Zeroable}; -use std::{borrow::Cow, mem, num::NonZeroU32}; +use std::{borrow::Cow, f32::consts, mem, num::NonZeroU32}; use wgpu::util::DeviceExt; const TEXTURE_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8UnormSrgb; @@ -61,15 +61,14 @@ struct Example { } impl Example { - fn generate_matrix(aspect_ratio: f32) -> cgmath::Matrix4 { - let mx_projection = cgmath::perspective(cgmath::Deg(45f32), aspect_ratio, 1.0, 1000.0); - let mx_view = cgmath::Matrix4::look_at_rh( - cgmath::Point3::new(0f32, 0.0, 10.0), - cgmath::Point3::new(0f32, 50.0, 0.0), - cgmath::Vector3::unit_z(), + fn generate_matrix(aspect_ratio: f32) -> glam::Mat4 { + let projection = glam::Mat4::perspective_rh(consts::FRAC_PI_4, aspect_ratio, 1.0, 1000.0); + let view = glam::Mat4::look_at_rh( + glam::Vec3::new(0f32, 0.0, 10.0), + glam::Vec3::new(0f32, 50.0, 0.0), + glam::Vec3::Z, ); - let mx_correction = framework::OPENGL_TO_WGPU_MATRIX; - mx_correction * mx_projection * mx_view + projection * view } fn generate_mipmaps( diff --git a/wgpu/examples/shadow/main.rs b/wgpu/examples/shadow/main.rs index b6219f6b71..287c18b6bb 100644 --- a/wgpu/examples/shadow/main.rs +++ b/wgpu/examples/shadow/main.rs @@ -1,4 +1,4 @@ -use std::{borrow::Cow, iter, mem, num::NonZeroU32, ops::Range, rc::Rc}; +use std::{borrow::Cow, f32::consts, iter, mem, num::NonZeroU32, ops::Range, rc::Rc}; #[path = "../framework.rs"] mod framework; @@ -80,7 +80,7 @@ fn create_plane(size: i8) -> (Vec, Vec) { } struct Entity { - mx_world: cgmath::Matrix4, + mx_world: glam::Mat4, rotation_speed: f32, color: wgpu::Color, vertex_buf: Rc, @@ -91,7 +91,7 @@ struct Entity { } struct Light { - pos: cgmath::Point3, + pos: glam::Vec3, color: wgpu::Color, fov: f32, depth: Range, @@ -108,20 +108,16 @@ struct LightRaw { impl Light { fn to_raw(&self) -> LightRaw { - use cgmath::{Deg, EuclideanSpace, Matrix4, PerspectiveFov, Point3, Vector3}; - - let mx_view = Matrix4::look_at_rh(self.pos, Point3::origin(), Vector3::unit_z()); - let projection = PerspectiveFov { - fovy: Deg(self.fov).into(), - aspect: 1.0, - near: self.depth.start, - far: self.depth.end, - }; - let mx_correction = framework::OPENGL_TO_WGPU_MATRIX; - let mx_view_proj = - mx_correction * cgmath::Matrix4::from(projection.to_perspective()) * mx_view; + let view = glam::Mat4::look_at_rh(self.pos, glam::Vec3::ZERO, glam::Vec3::Z); + let projection = glam::Mat4::perspective_rh( + self.fov * consts::PI / 180., + 1.0, + self.depth.start, + self.depth.end, + ); + let view_proj = projection * view; LightRaw { - proj: *mx_view_proj.as_ref(), + proj: view_proj.to_cols_array_2d(), pos: [self.pos.x, self.pos.y, self.pos.z, 1.0], color: [ self.color.r as f32, @@ -175,15 +171,14 @@ impl Example { }; const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float; - fn generate_matrix(aspect_ratio: f32) -> cgmath::Matrix4 { - let mx_projection = cgmath::perspective(cgmath::Deg(45f32), aspect_ratio, 1.0, 20.0); - let mx_view = cgmath::Matrix4::look_at_rh( - cgmath::Point3::new(3.0f32, -10.0, 6.0), - cgmath::Point3::new(0f32, 0.0, 0.0), - cgmath::Vector3::unit_z(), + fn generate_matrix(aspect_ratio: f32) -> glam::Mat4 { + let projection = glam::Mat4::perspective_rh(consts::FRAC_PI_4, aspect_ratio, 1.0, 20.0); + let view = glam::Mat4::look_at_rh( + glam::Vec3::new(3.0f32, -10.0, 6.0), + glam::Vec3::new(0f32, 0.0, 0.0), + glam::Vec3::Z, ); - let mx_correction = framework::OPENGL_TO_WGPU_MATRIX; - mx_correction * mx_projection * mx_view + projection * view } fn create_depth_texture( @@ -258,32 +253,32 @@ impl framework::Example for Example { }); struct CubeDesc { - offset: cgmath::Vector3, + offset: glam::Vec3, angle: f32, scale: f32, rotation: f32, } let cube_descs = [ CubeDesc { - offset: cgmath::vec3(-2.0, -2.0, 2.0), + offset: glam::Vec3::new(-2.0, -2.0, 2.0), angle: 10.0, scale: 0.7, rotation: 0.1, }, CubeDesc { - offset: cgmath::vec3(2.0, -2.0, 2.0), + offset: glam::Vec3::new(2.0, -2.0, 2.0), angle: 50.0, scale: 1.3, rotation: 0.2, }, CubeDesc { - offset: cgmath::vec3(-2.0, 2.0, 2.0), + offset: glam::Vec3::new(-2.0, 2.0, 2.0), angle: 140.0, scale: 1.1, rotation: 0.3, }, CubeDesc { - offset: cgmath::vec3(2.0, 2.0, 2.0), + offset: glam::Vec3::new(2.0, 2.0, 2.0), angle: 210.0, scale: 0.9, rotation: 0.4, @@ -314,9 +309,8 @@ impl framework::Example for Example { let index_format = wgpu::IndexFormat::Uint16; let mut entities = vec![{ - use cgmath::SquareMatrix; Entity { - mx_world: cgmath::Matrix4::identity(), + mx_world: glam::Mat4::IDENTITY, rotation_speed: 0.0, color: wgpu::Color::WHITE, vertex_buf: Rc::new(plane_vertex_buf), @@ -328,15 +322,16 @@ impl framework::Example for Example { }]; for (i, cube) in cube_descs.iter().enumerate() { - use cgmath::{Decomposed, Deg, InnerSpace, Quaternion, Rotation3}; - - let transform = Decomposed { - disp: cube.offset, - rot: Quaternion::from_axis_angle(cube.offset.normalize(), Deg(cube.angle)), - scale: cube.scale, - }; + let mx_world = glam::Mat4::from_scale_rotation_translation( + glam::Vec3::splat(cube.scale), + glam::Quat::from_axis_angle( + cube.offset.normalize(), + cube.angle * consts::PI / 180., + ), + cube.offset, + ); entities.push(Entity { - mx_world: cgmath::Matrix4::from(transform), + mx_world, rotation_speed: cube.rotation, color: wgpu::Color::GREEN, vertex_buf: Rc::clone(&cube_vertex_buf), @@ -414,7 +409,7 @@ impl framework::Example for Example { .collect::>(); let lights = vec![ Light { - pos: cgmath::Point3::new(7.0, -5.0, 10.0), + pos: glam::Vec3::new(7.0, -5.0, 10.0), color: wgpu::Color { r: 0.5, g: 1.0, @@ -426,7 +421,7 @@ impl framework::Example for Example { target_view: shadow_target_views[0].take().unwrap(), }, Light { - pos: cgmath::Point3::new(-5.0, 7.0, 10.0), + pos: glam::Vec3::new(-5.0, 7.0, 10.0), color: wgpu::Color { r: 1.0, g: 0.5, @@ -603,7 +598,7 @@ impl framework::Example for Example { let mx_total = Self::generate_matrix(sc_desc.width as f32 / sc_desc.height as f32); let forward_uniforms = GlobalUniforms { - proj: *mx_total.as_ref(), + proj: mx_total.to_cols_array_2d(), num_lights: [lights.len() as u32, 0, 0, 0], }; let uniform_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { @@ -724,11 +719,12 @@ impl framework::Example for Example { // update uniforms for entity in self.entities.iter_mut() { if entity.rotation_speed != 0.0 { - let rotation = cgmath::Matrix4::from_angle_x(cgmath::Deg(entity.rotation_speed)); - entity.mx_world = entity.mx_world * rotation; + let rotation = + glam::Mat4::from_rotation_x(entity.rotation_speed * consts::PI / 180.); + entity.mx_world *= rotation; } let data = EntityUniforms { - model: entity.mx_world.into(), + model: entity.mx_world.to_cols_array_2d(), color: [ entity.color.r as f32, entity.color.g as f32, diff --git a/wgpu/examples/skybox/main.rs b/wgpu/examples/skybox/main.rs index de2de44a6a..9dcd3ece82 100644 --- a/wgpu/examples/skybox/main.rs +++ b/wgpu/examples/skybox/main.rs @@ -2,8 +2,7 @@ mod framework; use bytemuck::{Pod, Zeroable}; -use cgmath::SquareMatrix; -use std::borrow::Cow; +use std::{borrow::Cow, f32::consts}; use wgpu::{util::DeviceExt, AstcBlock, AstcChannel}; const IMAGE_SIZE: u32 = 128; @@ -33,20 +32,18 @@ const MODEL_CENTER_Y: f32 = 2.0; impl Camera { fn to_uniform_data(&self) -> [f32; 16 * 3 + 4] { let aspect = self.screen_size.0 as f32 / self.screen_size.1 as f32; - let mx_projection = cgmath::perspective(cgmath::Deg(45f32), aspect, 1.0, 50.0); - let cam_pos = cgmath::Point3::new( + let proj = glam::Mat4::perspective_rh(consts::FRAC_PI_4, aspect, 1.0, 50.0); + let cam_pos = glam::Vec3::new( self.angle_xz.cos() * self.angle_y.sin() * self.dist, self.angle_xz.sin() * self.dist + MODEL_CENTER_Y, self.angle_xz.cos() * self.angle_y.cos() * self.dist, ); - let mx_view = cgmath::Matrix4::look_at_rh( + let view = glam::Mat4::look_at_rh( cam_pos, - cgmath::Point3::new(0f32, MODEL_CENTER_Y, 0.0), - cgmath::Vector3::unit_y(), + glam::Vec3::new(0f32, MODEL_CENTER_Y, 0.0), + glam::Vec3::Y, ); - let proj = framework::OPENGL_TO_WGPU_MATRIX * mx_projection; - let proj_inv = proj.invert().unwrap(); - let view = framework::OPENGL_TO_WGPU_MATRIX * mx_view; + let proj_inv = proj.inverse(); let mut raw = [0f32; 16 * 3 + 4]; raw[..16].copy_from_slice(&AsRef::<[f32; 16]>::as_ref(&proj)[..]); @@ -182,7 +179,7 @@ impl framework::Example for Skybox { screen_size: (config.width, config.height), angle_xz: 0.2, angle_y: 0.2, - dist: 30.0, + dist: 20.0, }; let raw_uniforms = camera.to_uniform_data(); let uniform_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { diff --git a/wgpu/examples/skybox/screenshot-astc.png b/wgpu/examples/skybox/screenshot-astc.png index a3dc335ab7..38515fada4 100644 Binary files a/wgpu/examples/skybox/screenshot-astc.png and b/wgpu/examples/skybox/screenshot-astc.png differ diff --git a/wgpu/examples/skybox/screenshot-bc1.png b/wgpu/examples/skybox/screenshot-bc1.png index d111bc5122..efba0b5c8c 100644 Binary files a/wgpu/examples/skybox/screenshot-bc1.png and b/wgpu/examples/skybox/screenshot-bc1.png differ diff --git a/wgpu/examples/skybox/screenshot-etc2.png b/wgpu/examples/skybox/screenshot-etc2.png index d2280935a5..46ae8d3198 100644 Binary files a/wgpu/examples/skybox/screenshot-etc2.png and b/wgpu/examples/skybox/screenshot-etc2.png differ diff --git a/wgpu/examples/skybox/screenshot.png b/wgpu/examples/skybox/screenshot.png index 362c921f96..d90debe148 100644 Binary files a/wgpu/examples/skybox/screenshot.png and b/wgpu/examples/skybox/screenshot.png differ diff --git a/wgpu/examples/water/main.rs b/wgpu/examples/water/main.rs index 9209c4b6b1..7ff7c7e465 100644 --- a/wgpu/examples/water/main.rs +++ b/wgpu/examples/water/main.rs @@ -4,9 +4,9 @@ mod framework; mod point_gen; use bytemuck::{Pod, Zeroable}; -use cgmath::Point3; +use glam::Vec3; use rand::SeedableRng; -use std::{borrow::Cow, iter, mem}; +use std::{borrow::Cow, f32::consts, iter, mem}; use wgpu::util::DeviceExt; /// @@ -23,16 +23,12 @@ const SIZE: f32 = 29.0; /// Location of the camera. /// Location of light is in terrain/water shaders. /// -const CAMERA: Point3 = Point3 { - x: -200.0, - y: 70.0, - z: 200.0, -}; +const CAMERA: Vec3 = glam::const_vec3!([-200.0, 70.0, 200.0,]); struct Matrices { - view: cgmath::Matrix4, - flipped_view: cgmath::Matrix4, - projection: cgmath::Matrix4, + view: glam::Mat4, + flipped_view: glam::Mat4, + projection: glam::Mat4, } #[repr(C)] @@ -99,31 +95,29 @@ impl Example { /// Creates the view matrices, and the corrected projection matrix. /// fn generate_matrices(aspect_ratio: f32) -> Matrices { - let projection = cgmath::perspective(cgmath::Deg(45f32), aspect_ratio, 10.0, 400.0); - let reg_view = cgmath::Matrix4::look_at_rh( + let projection = glam::Mat4::perspective_rh(consts::FRAC_PI_4, aspect_ratio, 10.0, 400.0); + let reg_view = glam::Mat4::look_at_rh( CAMERA, - cgmath::Point3::new(0f32, 0.0, 0.0), - cgmath::Vector3::unit_y(), //Note that y is up. Differs from other examples. + glam::Vec3::new(0f32, 0.0, 0.0), + glam::Vec3::Y, //Note that y is up. Differs from other examples. ); - let scale = cgmath::Matrix4::from_nonuniform_scale(8.0, 1.5, 8.0); + let scale = glam::Mat4::from_scale(glam::Vec3::new(8.0, 1.5, 8.0)); let reg_view = reg_view * scale; - let flipped_view = cgmath::Matrix4::look_at_rh( - cgmath::Point3::new(CAMERA.x, -CAMERA.y, CAMERA.z), - cgmath::Point3::new(0f32, 0.0, 0.0), - cgmath::Vector3::unit_y(), + let flipped_view = glam::Mat4::look_at_rh( + glam::Vec3::new(CAMERA.x, -CAMERA.y, CAMERA.z), + glam::Vec3::ZERO, + glam::Vec3::Y, ); - let correction = framework::OPENGL_TO_WGPU_MATRIX; - let flipped_view = flipped_view * scale; Matrices { view: reg_view, flipped_view, - projection: correction * projection, + projection, } } @@ -331,11 +325,7 @@ impl framework::Example for Example { SNOW }; point_gen::TerrainVertex { - position: Point3 { - x: point[0], - y, - z: point[1], - }, + position: Vec3::new(point[0], y, point[1]), colour: mul_arr(colour, random), } }); diff --git a/wgpu/examples/water/point_gen.rs b/wgpu/examples/water/point_gen.rs index d14f99751f..21465e6ea8 100644 --- a/wgpu/examples/water/point_gen.rs +++ b/wgpu/examples/water/point_gen.rs @@ -3,7 +3,6 @@ //! use bytemuck::{Pod, Zeroable}; -use cgmath::{InnerSpace, Point3, Vector3}; use std::collections::HashMap; // The following constants are used in calculations. @@ -52,7 +51,7 @@ pub struct WaterVertexAttributes { /// #[derive(Copy, Clone, Debug)] pub struct TerrainVertex { - pub position: Point3, + pub position: glam::Vec3, pub colour: [u8; 4], } @@ -96,7 +95,7 @@ fn surrounding_point_values_iter( /// /// Used in calculating terrain normals. /// -pub fn calculate_normal(a: Point3, b: Point3, c: Point3) -> Vector3 { +pub fn calculate_normal(a: glam::Vec3, b: glam::Vec3, c: glam::Vec3) -> glam::Vec3 { (b - a).normalize().cross((c - a).normalize()).normalize() } @@ -152,19 +151,11 @@ impl HexTerrainMesh { /// pub fn make_buffer_data(&self) -> Vec { let mut vertices = Vec::new(); - fn middle(p1: &TerrainVertex, p2: &TerrainVertex, p: &TerrainVertex) -> Point3 { - Point3 { - x: (p1.position.x + p2.position.x + p.position.x) / 3.0, - y: (p1.position.y + p2.position.y + p.position.y) / 3.0, - z: (p1.position.z + p2.position.z + p.position.z) / 3.0, - } + fn middle(p1: &TerrainVertex, p2: &TerrainVertex, p: &TerrainVertex) -> glam::Vec3 { + (p1.position + p2.position + p.position) / 3.0 } - fn half(p1: &TerrainVertex, p2: &TerrainVertex) -> Point3 { - Point3 { - x: (p1.position.x + p2.position.x) / 2.0, - y: (p1.position.y + p2.position.y) / 2.0, - z: (p1.position.z + p2.position.z) / 2.0, - } + fn half(p1: &TerrainVertex, p2: &TerrainVertex) -> glam::Vec3 { + (p1.position + p2.position) / 2.0 } let mut push_triangle = |p1: &TerrainVertex, p2: &TerrainVertex,