Skip to content

Commit

Permalink
Add wasm-bindgen feature
Browse files Browse the repository at this point in the history
  • Loading branch information
AThilenius committed Aug 22, 2024
1 parent d827651 commit cf51252
Show file tree
Hide file tree
Showing 60 changed files with 928 additions and 3 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ core-simd = []

[dependencies]
approx = { version = "0.5", optional = true, default-features = false }
bytecheck = { version = "0.7", optional = true, default-features = false }
bytemuck = { version = "1.9", optional = true, default-features = false }
libm = { version = "0.2", optional = true, default-features = false}
mint = { version = "0.5.8", optional = true, default-features = false }
rand = { version = "0.8", optional = true, default-features = false }
serde = { version = "1.0", optional = true, default-features = false }
rkyv = { version = "0.7", optional = true, default-features = false }
bytecheck = { version = "0.7", optional = true, default-features = false }
libm = { version = "0.2", optional = true, default-features = false}
serde = { version = "1.0", optional = true, default-features = false }
wasm-bindgen = { version = "0.2", optional = true, default-features = false}

[dev-dependencies]
# rand_xoshiro is required for tests if rand is enabled
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ glam = { version = "0.29.0", default-features = false }
without the `scalar-math` feature. It should work between all other builds of
`glam`. Endian conversion is currently not supported
* [`bytecheck`] - to perform archive validation when using the `rkyv` feature
* [`wasm-bindgen`] - adds `#[wasm_bindgen]` to types and constructors

[`approx`]: https://docs.rs/approx
[`bytemuck`]: https://docs.rs/bytemuck
Expand Down
46 changes: 46 additions & 0 deletions codegen/templates/mat.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@
{% set axes = ["x_axis", "y_axis", "z_axis", "w_axis"] | slice(end = dim) %}
{% set dimension_in_full = ["zero", "one", "two", "three", "four"] | nth(n = dim) %}

{%- if not is_sse2 %}
#[cfg(feature = "wasm-bindgen")]
use wasm_bindgen::prelude::*;
{%- endif %}

use crate::{
{% if scalar_t == "f32" %}
DMat{{ dim }},
Expand Down Expand Up @@ -216,9 +221,15 @@ pub const fn {{ self_t | lower }}(
{%- endif %}
{%- if self_t == "Mat2" and not is_scalar %}
#[repr(transparent)]
{%- if not is_sse2 %}
#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen)]
{%- endif %}
pub struct {{ self_t }}(pub(crate) {{ simd_t }});
{%- else %}
#[repr(C)]
{%- if not is_sse2 %}
#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen)]
{%- endif %}
pub struct {{ self_t }}
{
{% for axis in axes %}
Expand All @@ -227,6 +238,41 @@ pub struct {{ self_t }}
}
{% endif %}

{%- if not is_sse2 %}
#[cfg(feature = "wasm-bindgen")]
#[wasm_bindgen]
impl {{ self_t }} {
#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen(constructor))]
pub fn wasm_bindgen_ctor(
{% for i in range(end = dim) %}
{%- for j in range(end = dim) %}
m{{ i }}{{ j }}: {{ scalar_t }},
{%- endfor %}
{%- endfor %}
) -> Self {
{% if self_t == "Mat2" and is_sse2 %}
unsafe { UnionCast { a: [m00, m01, m10, m11] }.v }
{% elif self_t == "Mat2" and is_wasm32 %}
Self(f32x4(m00, m01, m10, m11))
{% elif self_t == "Mat2" and is_coresimd %}
Self(f32x4::from_array([m00, m01, m10, m11]))
{% elif self_t == "Mat2" and is_neon %}
unsafe { UnionCast { a: [m00, m01, m10, m11] }.v }
{% else %}
Self {
{% for i in range(end = dim) %}
{{ axes[i] }}: {{ col_t}}::new(
{% for j in range(end = dim) %}
m{{ i }}{{ j }},
{% endfor %}
),
{%- endfor %}
}
{% endif %}
}
}
{%- endif %}

impl {{ self_t }} {
/// A {{ nxn }} matrix with all elements set to `0.0`.
pub const ZERO: Self = Self::from_cols(
Expand Down
5 changes: 5 additions & 0 deletions codegen/templates/quat.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
{% set mat4_t = "DMat4" %}
{% endif %}

#[cfg(feature = "wasm-bindgen")]
use wasm_bindgen::prelude::*;

use crate::{
{{ scalar_t }}::math,
euler::{EulerRot, FromEuler, ToEuler},
Expand Down Expand Up @@ -109,6 +112,7 @@ pub const fn {{ self_t | lower }}(x: {{ scalar_t }}, y: {{ scalar_t }}, z: {{ sc
{%- endif %}
#[cfg_attr(not(target_arch = "spirv"), repr(C))]
#[cfg_attr(target_arch = "spirv", repr(simd))]
#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen)]
pub struct {{ self_t }}{
pub x: {{ scalar_t }},
pub y: {{ scalar_t }},
Expand All @@ -117,6 +121,7 @@ pub struct {{ self_t }}{
}
{%- else %}
#[repr(transparent)]
#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen)]
pub struct {{ self_t }}(pub(crate) {{ simd_t }});
{%- endif %}

Expand Down
58 changes: 58 additions & 0 deletions codegen/templates/vec.rs.tera
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@
{% set zero = "0" %}
{% endif %}

#[cfg(feature = "wasm-bindgen")]
use wasm_bindgen::prelude::*;

{% if bveca_from_type and bveca_from_type == "BVec4A" and is_scalar %}
{% if scalar_t == "f32" %}
#[cfg(feature = "scalar-math")]
Expand Down Expand Up @@ -273,6 +276,7 @@ pub const fn {{ self_t | lower }}(
{%- if is_scalar %}
#[cfg_attr(not(target_arch = "spirv"), repr(C))]
#[cfg_attr(target_arch = "spirv", repr(simd))]
#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen)]
pub struct {{ self_t }}
{
{% for c in components %}
Expand All @@ -281,9 +285,63 @@ pub struct {{ self_t }}
}
{% else %}
#[repr(transparent)]
#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen)]
pub struct {{ self_t }}(pub(crate) {{ simd_t }});
{% endif %}

{%- if not is_sse2 %}
#[cfg(feature = "wasm-bindgen")]
#[wasm_bindgen]
impl {{ self_t }} {
#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen(constructor))]
pub fn wasm_bindgen_ctor(
{% for c in components %}
{{ c }}: {{ scalar_t }},
{% endfor %}
) -> Self {
{% if is_scalar %}
Self {
{% for c in components %}
{{ c }},
{%- endfor %}
}
{% elif is_sse2 %}
unsafe {
UnionCast { a: [
{% if dim == 3 %}
x, y, z, z
{% elif dim == 4 %}
x, y, z, w
{% endif %}
] }.v
}
{% elif is_wasm32 %}
Self(f32x4(
{% if dim == 3 %}
x, y, z, z
{% elif dim == 4 %}
x, y, z, w
{% endif %}
))
{% elif is_coresimd %}
Self(f32x4::from_array([
x, y, z,
{% if dim == 3 %}
z
{% elif dim == 4 %}
w
{% endif %}
]))
{% elif is_neon %}
{% if dim == 3 %}
unsafe { UnionCast { a: [x, y, z, z] }.v }
{% elif dim == 4 %}
unsafe { UnionCast { a: [x, y, z, w] }.v }
{% endif %}
{% endif %}
}
}
{%- endif %}
impl {{ self_t }} {
/// All zeroes.
pub const ZERO: Self = Self::splat({{ zero }});
Expand Down
12 changes: 12 additions & 0 deletions src/f32/coresimd/mat2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Generated from mat.rs.tera template. Edit the template, not the generated file.

#[cfg(feature = "wasm-bindgen")]
use wasm_bindgen::prelude::*;

use crate::{f32::math, swizzles::*, DMat2, Mat3, Mat3A, Vec2};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
Expand All @@ -22,7 +25,16 @@ pub const fn mat2(x_axis: Vec2, y_axis: Vec2) -> Mat2 {
/// This type is 16 byte aligned.
#[derive(Clone, Copy)]
#[repr(transparent)]
#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen)]
pub struct Mat2(pub(crate) f32x4);
#[cfg(feature = "wasm-bindgen")]
#[wasm_bindgen]
impl Mat2 {
#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen(constructor))]
pub fn wasm_bindgen_ctor(m00: f32, m01: f32, m10: f32, m11: f32) -> Self {
Self(f32x4::from_array([m00, m01, m10, m11]))
}
}

impl Mat2 {
/// A 2x2 matrix with all elements set to `0.0`.
Expand Down
27 changes: 27 additions & 0 deletions src/f32/coresimd/mat3a.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Generated from mat.rs.tera template. Edit the template, not the generated file.

#[cfg(feature = "wasm-bindgen")]
use wasm_bindgen::prelude::*;

use crate::{
euler::{FromEuler, ToEuler},
f32::math,
Expand Down Expand Up @@ -46,12 +49,36 @@ pub const fn mat3a(x_axis: Vec3A, y_axis: Vec3A, z_axis: Vec3A) -> Mat3A {
/// transform.
#[derive(Clone, Copy)]
#[repr(C)]
#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen)]
pub struct Mat3A {
pub x_axis: Vec3A,
pub y_axis: Vec3A,
pub z_axis: Vec3A,
}

#[cfg(feature = "wasm-bindgen")]
#[wasm_bindgen]
impl Mat3A {
#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen(constructor))]
pub fn wasm_bindgen_ctor(
m00: f32,
m01: f32,
m02: f32,
m10: f32,
m11: f32,
m12: f32,
m20: f32,
m21: f32,
m22: f32,
) -> Self {
Self {
x_axis: Vec3A::new(m00, m01, m02),
y_axis: Vec3A::new(m10, m11, m12),
z_axis: Vec3A::new(m20, m21, m22),
}
}
}

impl Mat3A {
/// A 3x3 matrix with all elements set to `0.0`.
pub const ZERO: Self = Self::from_cols(Vec3A::ZERO, Vec3A::ZERO, Vec3A::ZERO);
Expand Down
35 changes: 35 additions & 0 deletions src/f32/coresimd/mat4.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Generated from mat.rs.tera template. Edit the template, not the generated file.

#[cfg(feature = "wasm-bindgen")]
use wasm_bindgen::prelude::*;

use crate::{
coresimd::*,
euler::{FromEuler, ToEuler},
Expand Down Expand Up @@ -52,13 +55,45 @@ pub const fn mat4(x_axis: Vec4, y_axis: Vec4, z_axis: Vec4, w_axis: Vec4) -> Mat
/// perspective correction using the [`Self::project_point3()`] convenience method.
#[derive(Clone, Copy)]
#[repr(C)]
#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen)]
pub struct Mat4 {
pub x_axis: Vec4,
pub y_axis: Vec4,
pub z_axis: Vec4,
pub w_axis: Vec4,
}

#[cfg(feature = "wasm-bindgen")]
#[wasm_bindgen]
impl Mat4 {
#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen(constructor))]
pub fn wasm_bindgen_ctor(
m00: f32,
m01: f32,
m02: f32,
m03: f32,
m10: f32,
m11: f32,
m12: f32,
m13: f32,
m20: f32,
m21: f32,
m22: f32,
m23: f32,
m30: f32,
m31: f32,
m32: f32,
m33: f32,
) -> Self {
Self {
x_axis: Vec4::new(m00, m01, m02, m03),
y_axis: Vec4::new(m10, m11, m12, m13),
z_axis: Vec4::new(m20, m21, m22, m23),
w_axis: Vec4::new(m30, m31, m32, m33),
}
}
}

impl Mat4 {
/// A 4x4 matrix with all elements set to `0.0`.
pub const ZERO: Self = Self::from_cols(Vec4::ZERO, Vec4::ZERO, Vec4::ZERO, Vec4::ZERO);
Expand Down
4 changes: 4 additions & 0 deletions src/f32/coresimd/quat.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Generated from quat.rs.tera template. Edit the template, not the generated file.

#[cfg(feature = "wasm-bindgen")]
use wasm_bindgen::prelude::*;

use crate::{
coresimd::*,
euler::{EulerRot, FromEuler, ToEuler},
Expand Down Expand Up @@ -35,6 +38,7 @@ pub const fn quat(x: f32, y: f32, z: f32, w: f32) -> Quat {
/// This type is 16 byte aligned.
#[derive(Clone, Copy)]
#[repr(transparent)]
#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen)]
pub struct Quat(pub(crate) f32x4);

impl Quat {
Expand Down
12 changes: 12 additions & 0 deletions src/f32/coresimd/vec3a.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Generated from vec.rs.tera template. Edit the template, not the generated file.

#[cfg(feature = "wasm-bindgen")]
use wasm_bindgen::prelude::*;

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

#[cfg(not(target_arch = "spirv"))]
Expand Down Expand Up @@ -28,8 +31,17 @@ pub const fn vec3a(x: f32, y: f32, z: f32) -> Vec3A {
/// This type is 16 byte aligned.
#[derive(Clone, Copy)]
#[repr(transparent)]
#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen)]
pub struct Vec3A(pub(crate) f32x4);

#[cfg(feature = "wasm-bindgen")]
#[wasm_bindgen]
impl Vec3A {
#[cfg_attr(feature = "wasm-bindgen", wasm_bindgen(constructor))]
pub fn wasm_bindgen_ctor(x: f32, y: f32, z: f32) -> Self {
Self(f32x4::from_array([x, y, z, z]))
}
}
impl Vec3A {
/// All zeroes.
pub const ZERO: Self = Self::splat(0.0);
Expand Down
Loading

0 comments on commit cf51252

Please sign in to comment.