Skip to content

Commit

Permalink
Add From BVecN and BVecNA for all vector types. (#488)
Browse files Browse the repository at this point in the history
Fixes #474
  • Loading branch information
bitshifter authored Mar 11, 2024
1 parent 962d59d commit fb8ef0f
Show file tree
Hide file tree
Showing 42 changed files with 736 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
toolchain: [1.66.1]
toolchain: [1.68.2]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
Expand Down
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ The format is based on [Keep a Changelog], and this project adheres to

### Breaking changes

* Minimum Supported Version of Rust bumped to 1.66.1 for `saturating_add_signed`
support.
* Minimum Supported Rust Version bumped to 1.68.2 for
`impl From<bool> for {f32,f64}` support.

### Fixed

Expand All @@ -35,6 +35,8 @@ The format is based on [Keep a Changelog], and this project adheres to
* Added `normalize_or` method to vector types that returns the specified value
if normalization failed.

* Added `From<BVecN>` support for all vector types.

## [0.25.0] - 2023-12-19

### Breaking changes
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ readme = "README.md"
license = "MIT OR Apache-2.0"
keywords = ["gamedev", "math", "matrix", "vector", "quaternion"]
categories = ["game-engines", "no-std"]
rust-version = "1.66.1"
rust-version = "1.68.2"

[badges]
maintenance = { status = "actively-developed" }
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Build Status]][github-ci] [![Coverage Status]][coveralls.io]
[![Latest Version]][crates.io] [![docs]][docs.rs]
[![Minimum Supported Rust Version]][Rust 1.66.1]
[![Minimum Supported Rust Version]][Rust 1.68.2]

A simple and fast 3D math library for games and graphics.

Expand Down Expand Up @@ -140,7 +140,7 @@ glam = { version = "0.25", default-features = false }

### Minimum Supported Rust Version (MSRV)

The minimum supported version of Rust for `glam` is `1.66.1`.
The minimum supported version of Rust for `glam` is `1.68.2`.

## Conventions

Expand Down Expand Up @@ -262,5 +262,5 @@ See [ATTRIBUTION.md] for details.
[crates.io]: https://crates.io/crates/glam/
[docs]: https://docs.rs/glam/badge.svg
[docs.rs]: https://docs.rs/glam/
[Minimum Supported Rust Version]: https://img.shields.io/badge/Rust-1.66.1-blue?color=fc8d62&logo=rust
[Rust 1.66.1]: https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-1661-2023-01-10
[Minimum Supported Rust Version]: https://img.shields.io/badge/Rust-1.68.2-blue?color=fc8d62&logo=rust
[Rust 1.68.2]: https://github.com/rust-lang/rust/blob/master/RELEASES.md#version-1682-2023-03-28
2 changes: 1 addition & 1 deletion build_all_msrv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e

CARGO='rustup run 1.66.1 cargo'
CARGO='rustup run 1.68.2 cargo'
$CARGO check --features "bytemuck mint rand serde debug-glam-assert" && \
$CARGO check --features "scalar-math bytemuck mint rand serde debug-glam-assert" && \
$CARGO check --no-default-features --features "libm scalar-math bytemuck mint rand serde debug-glam-assert"
2 changes: 1 addition & 1 deletion clippy.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
msrv = "1.66.1"
msrv = "1.68.2"
49 changes: 46 additions & 3 deletions codegen/templates/vec.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@
{% set from_types = ["U16Vec" ~ dim, "UVec" ~ dim] %}
{% set try_from_types = ["I16Vec" ~ dim, "IVec" ~ dim, "I64Vec" ~ dim] %}
{% endif %}
{% set bvec_from_type = "BVec" ~ dim %}
{% if dim > 2 %}
{% set bveca_from_type = "BVec" ~ dim ~ "A" %}
{% endif %}

{% if dim == 2 %}
{% if scalar_t == "i16" or scalar_t == "u16" %}
Expand Down Expand Up @@ -128,15 +132,23 @@
{% set zero = "0" %}
{% endif %}

{% if mask_t == "BVec4A" and scalar_t == "f32" and is_scalar %}
#[cfg(feature = "scalar-math")]
use crate::BVec4 as BVec4A;
{% if bveca_from_type and bveca_from_type == "BVec4A" and is_scalar %}
{% if scalar_t == "f32" %}
#[cfg(feature = "scalar-math")]
use crate::BVec4 as BVec4A;
{% endif %}
#[cfg(not(feature = "scalar-math"))]
use crate::BVec4A;
use crate::{
{% if bveca_from_type and bveca_from_type != mask_t %}
{{ mask_t }},
{% endif %}
{% else %}
use crate::{
{{ mask_t }},
{% if bveca_from_type and bveca_from_type != mask_t %}
{{ bveca_from_type }},
{% endif %}
{% endif %}
{% if self_t != vec2_t %}
{{ vec2_t }},
Expand Down Expand Up @@ -170,6 +182,9 @@
{{ ty }},
{% endfor %}
{% endif %}
{% if bvec_from_type != mask_t %}
{{ bvec_from_type }},
{% endif %}
};

#[cfg(not(target_arch = "spirv"))]
Expand Down Expand Up @@ -3225,3 +3240,31 @@ impl DerefMut for {{ self_t }} {
}
{% endfor %}
{% endif %}

impl From<{{ bvec_from_type }}> for {{ self_t }} {
#[inline]
fn from(v: {{ bvec_from_type }}) -> Self {
Self::new(
{% for c in components %}
{{ scalar_t }}::from(v.{{ c }}),
{% endfor %}
)
}
}

{% if bveca_from_type %}
{% if bveca_from_type == "BVec4A" %}
#[cfg(not(feature = "scalar-math"))]
{% endif %}
impl From<{{ bveca_from_type }}> for {{ self_t }} {
#[inline]
fn from(v: {{ bveca_from_type }}) -> Self {
let bool_array: [bool; {{ dim }}] = v.into();
Self::new(
{% for c in components %}
{{ scalar_t }}::from(bool_array[{{ loop.index0 }}]),
{% endfor %}
)
}
}
{% endif %}
21 changes: 20 additions & 1 deletion src/f32/coresimd/vec3a.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Generated from vec.rs.tera template. Edit the template, not the generated file.

use crate::{coresimd::*, f32::math, BVec3A, Vec2, Vec3, Vec4};
use crate::{coresimd::*, f32::math, BVec3, BVec3A, Vec2, Vec3, Vec4};

#[cfg(not(target_arch = "spirv"))]
use core::fmt;
Expand Down Expand Up @@ -1259,3 +1259,22 @@ impl DerefMut for Vec3A {
unsafe { &mut *(self as *mut Self).cast() }
}
}

impl From<BVec3> for Vec3A {
#[inline]
fn from(v: BVec3) -> Self {
Self::new(f32::from(v.x), f32::from(v.y), f32::from(v.z))
}
}

impl From<BVec3A> for Vec3A {
#[inline]
fn from(v: BVec3A) -> Self {
let bool_array: [bool; 3] = v.into();
Self::new(
f32::from(bool_array[0]),
f32::from(bool_array[1]),
f32::from(bool_array[2]),
)
}
}
29 changes: 28 additions & 1 deletion src/f32/coresimd/vec4.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Generated from vec.rs.tera template. Edit the template, not the generated file.

use crate::{coresimd::*, f32::math, BVec4A, Vec2, Vec3, Vec3A};
use crate::{coresimd::*, f32::math, BVec4, BVec4A, Vec2, Vec3, Vec3A};

#[cfg(not(target_arch = "spirv"))]
use core::fmt;
Expand Down Expand Up @@ -1182,3 +1182,30 @@ impl DerefMut for Vec4 {
unsafe { &mut *(self as *mut Self).cast() }
}
}

impl From<BVec4> for Vec4 {
#[inline]
fn from(v: BVec4) -> Self {
Self::new(
f32::from(v.x),
f32::from(v.y),
f32::from(v.z),
f32::from(v.w),
)
}
}

#[cfg(not(feature = "scalar-math"))]

impl From<BVec4A> for Vec4 {
#[inline]
fn from(v: BVec4A) -> Self {
let bool_array: [bool; 4] = v.into();
Self::new(
f32::from(bool_array[0]),
f32::from(bool_array[1]),
f32::from(bool_array[2]),
f32::from(bool_array[3]),
)
}
}
21 changes: 20 additions & 1 deletion src/f32/scalar/vec3a.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Generated from vec.rs.tera template. Edit the template, not the generated file.

use crate::{f32::math, BVec3A, Vec2, Vec3, Vec4};
use crate::{f32::math, BVec3, BVec3A, Vec2, Vec3, Vec4};

#[cfg(not(target_arch = "spirv"))]
use core::fmt;
Expand Down Expand Up @@ -1370,3 +1370,22 @@ impl From<(Vec2, f32)> for Vec3A {
Self::new(v.x, v.y, z)
}
}

impl From<BVec3> for Vec3A {
#[inline]
fn from(v: BVec3) -> Self {
Self::new(f32::from(v.x), f32::from(v.y), f32::from(v.z))
}
}

impl From<BVec3A> for Vec3A {
#[inline]
fn from(v: BVec3A) -> Self {
let bool_array: [bool; 3] = v.into();
Self::new(
f32::from(bool_array[0]),
f32::from(bool_array[1]),
f32::from(bool_array[2]),
)
}
}
30 changes: 29 additions & 1 deletion src/f32/scalar/vec4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

#[cfg(feature = "scalar-math")]
use crate::BVec4 as BVec4A;

#[cfg(not(feature = "scalar-math"))]
use crate::BVec4A;
use crate::{f32::math, Vec2, Vec3, Vec3A};
use crate::{f32::math, BVec4, Vec2, Vec3, Vec3A};

#[cfg(not(target_arch = "spirv"))]
use core::fmt;
Expand Down Expand Up @@ -1381,3 +1382,30 @@ impl From<(Vec2, Vec2)> for Vec4 {
Self::new(v.x, v.y, u.x, u.y)
}
}

impl From<BVec4> for Vec4 {
#[inline]
fn from(v: BVec4) -> Self {
Self::new(
f32::from(v.x),
f32::from(v.y),
f32::from(v.z),
f32::from(v.w),
)
}
}

#[cfg(not(feature = "scalar-math"))]

impl From<BVec4A> for Vec4 {
#[inline]
fn from(v: BVec4A) -> Self {
let bool_array: [bool; 4] = v.into();
Self::new(
f32::from(bool_array[0]),
f32::from(bool_array[1]),
f32::from(bool_array[2]),
f32::from(bool_array[3]),
)
}
}
21 changes: 20 additions & 1 deletion src/f32/sse2/vec3a.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Generated from vec.rs.tera template. Edit the template, not the generated file.

use crate::{f32::math, sse2::*, BVec3A, Vec2, Vec3, Vec4};
use crate::{f32::math, sse2::*, BVec3, BVec3A, Vec2, Vec3, Vec4};

#[cfg(not(target_arch = "spirv"))]
use core::fmt;
Expand Down Expand Up @@ -1347,3 +1347,22 @@ impl DerefMut for Vec3A {
unsafe { &mut *(self as *mut Self).cast() }
}
}

impl From<BVec3> for Vec3A {
#[inline]
fn from(v: BVec3) -> Self {
Self::new(f32::from(v.x), f32::from(v.y), f32::from(v.z))
}
}

impl From<BVec3A> for Vec3A {
#[inline]
fn from(v: BVec3A) -> Self {
let bool_array: [bool; 3] = v.into();
Self::new(
f32::from(bool_array[0]),
f32::from(bool_array[1]),
f32::from(bool_array[2]),
)
}
}
29 changes: 28 additions & 1 deletion src/f32/sse2/vec4.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Generated from vec.rs.tera template. Edit the template, not the generated file.

use crate::{f32::math, sse2::*, BVec4A, Vec2, Vec3, Vec3A};
use crate::{f32::math, sse2::*, BVec4, BVec4A, Vec2, Vec3, Vec3A};

#[cfg(not(target_arch = "spirv"))]
use core::fmt;
Expand Down Expand Up @@ -1268,3 +1268,30 @@ impl DerefMut for Vec4 {
unsafe { &mut *(self as *mut Self).cast() }
}
}

impl From<BVec4> for Vec4 {
#[inline]
fn from(v: BVec4) -> Self {
Self::new(
f32::from(v.x),
f32::from(v.y),
f32::from(v.z),
f32::from(v.w),
)
}
}

#[cfg(not(feature = "scalar-math"))]

impl From<BVec4A> for Vec4 {
#[inline]
fn from(v: BVec4A) -> Self {
let bool_array: [bool; 4] = v.into();
Self::new(
f32::from(bool_array[0]),
f32::from(bool_array[1]),
f32::from(bool_array[2]),
f32::from(bool_array[3]),
)
}
}
7 changes: 7 additions & 0 deletions src/f32/vec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1232,3 +1232,10 @@ impl From<Vec2> for (f32, f32) {
(v.x, v.y)
}
}

impl From<BVec2> for Vec2 {
#[inline]
fn from(v: BVec2) -> Self {
Self::new(f32::from(v.x), f32::from(v.y))
}
}
Loading

0 comments on commit fb8ef0f

Please sign in to comment.