Skip to content

Commit 5ef994c

Browse files
bushrat011899robtfm
authored andcommitted
Add no_std support to bevy_mikktspace (bevyengine#15528)
# Objective - Contributes to bevyengine#15460 - Allows `bevy_mikktspace` to be used in `no_std` contexts. ## Solution - Added `std` (default) and `libm` features which control the inclusion of the standard library. To use `bevy_mikktspace` in `no_std` environments, enable the `libm` feature. ## Testing - CI - `cargo clippy -p bevy_mikktspace --target "x86_64-unknown-none" --no-default-features --features libm`
1 parent a0f94aa commit 5ef994c

File tree

3 files changed

+76
-5
lines changed

3 files changed

+76
-5
lines changed

crates/bevy_mikktspace/Cargo.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,15 @@ license = "Zlib AND (MIT OR Apache-2.0)"
1515
keywords = ["bevy", "3D", "graphics", "algorithm", "tangent"]
1616
rust-version = "1.76.0"
1717

18+
[features]
19+
default = ["std"]
20+
21+
std = ["glam/std"]
22+
libm = ["glam/libm", "dep:libm"]
23+
1824
[dependencies]
19-
glam = "0.29"
25+
glam = { version = "0.29.0", default-features = false }
26+
libm = { version = "0.2", default-features = false, optional = true }
2027

2128
[[example]]
2229
name = "generate"

crates/bevy_mikktspace/src/generated.rs

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
unsafe_code
4646
)]
4747

48+
use alloc::{vec, vec::Vec};
4849
use core::ptr::{self, null_mut};
4950

5051
use glam::Vec3;
@@ -211,7 +212,7 @@ pub unsafe fn genTangSpace<I: Geometry>(geometry: &mut I, fAngularThreshold: f32
211212
let mut index = 0;
212213
let iNrFaces = geometry.num_faces();
213214
let mut bRes: bool = false;
214-
let fThresCos = fAngularThreshold.to_radians().cos();
215+
let fThresCos = cos(fAngularThreshold.to_radians());
215216
f = 0;
216217
while f < iNrFaces {
217218
let verts = geometry.num_vertices_of_face(f);
@@ -630,7 +631,7 @@ unsafe fn VNotZero(v: Vec3) -> bool {
630631
}
631632

632633
unsafe fn NotZero(fX: f32) -> bool {
633-
fX.abs() > 1.17549435e-38f32
634+
abs(fX) > 1.17549435e-38f32
634635
}
635636

636637
unsafe fn EvalTspace<I: Geometry>(
@@ -724,7 +725,7 @@ unsafe fn EvalTspace<I: Geometry>(
724725
} else {
725726
fCos
726727
};
727-
fAngle = (fCos as f64).acos() as f32;
728+
fAngle = acosf64(fCos as f64) as f32;
728729
fMagS = (*pTriInfos.offset(f as isize)).fMagS;
729730
fMagT = (*pTriInfos.offset(f as isize)).fMagT;
730731
res.vOs = res.vOs + (fAngle * vOs);
@@ -1010,7 +1011,7 @@ unsafe fn InitTriInfo<I: Geometry>(
10101011
0i32
10111012
};
10121013
if NotZero(fSignedAreaSTx2) {
1013-
let fAbsArea: f32 = fSignedAreaSTx2.abs();
1014+
let fAbsArea: f32 = abs(fSignedAreaSTx2);
10141015
let fLenOs: f32 = vOs.length();
10151016
let fLenOt: f32 = vOt.length();
10161017
let fS: f32 = if (*pTriInfos.offset(f as isize)).iFlag & 8i32 == 0i32 {
@@ -1808,3 +1809,63 @@ unsafe fn GenerateInitialVerticesIndexList<I: Geometry>(
18081809
}
18091810
return iTSpacesOffs;
18101811
}
1812+
1813+
fn cos(value: f32) -> f32 {
1814+
#[cfg(feature = "std")]
1815+
{
1816+
value.cos()
1817+
}
1818+
#[cfg(all(not(feature = "std"), feature = "libm"))]
1819+
{
1820+
libm::cosf(value)
1821+
}
1822+
#[cfg(all(not(feature = "std"), not(feature = "libm")))]
1823+
{
1824+
compile_error!("Require either 'libm' or 'std' for `cos`")
1825+
}
1826+
}
1827+
1828+
fn acos(value: f32) -> f32 {
1829+
#[cfg(feature = "std")]
1830+
{
1831+
value.acos()
1832+
}
1833+
#[cfg(all(not(feature = "std"), feature = "libm"))]
1834+
{
1835+
libm::acosf(value)
1836+
}
1837+
#[cfg(all(not(feature = "std"), not(feature = "libm")))]
1838+
{
1839+
compile_error!("Require either 'libm' or 'std' for `acos`")
1840+
}
1841+
}
1842+
1843+
fn abs(value: f32) -> f32 {
1844+
#[cfg(feature = "std")]
1845+
{
1846+
value.abs()
1847+
}
1848+
#[cfg(all(not(feature = "std"), feature = "libm"))]
1849+
{
1850+
libm::fabsf(value)
1851+
}
1852+
#[cfg(all(not(feature = "std"), not(feature = "libm")))]
1853+
{
1854+
compile_error!("Require either 'libm' or 'std' for `abs`")
1855+
}
1856+
}
1857+
1858+
fn acosf64(value: f64) -> f64 {
1859+
#[cfg(feature = "std")]
1860+
{
1861+
value.acos()
1862+
}
1863+
#[cfg(all(not(feature = "std"), feature = "libm"))]
1864+
{
1865+
libm::acos(value)
1866+
}
1867+
#[cfg(all(not(feature = "std"), not(feature = "libm")))]
1868+
{
1869+
compile_error!("Require either 'libm' or 'std' for `acos`")
1870+
}
1871+
}

crates/bevy_mikktspace/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
html_logo_url = "https://bevyengine.org/assets/icon.png",
1212
html_favicon_url = "https://bevyengine.org/assets/icon.png"
1313
)]
14+
#![cfg_attr(not(feature = "std"), no_std)]
15+
16+
extern crate alloc;
1417

1518
use glam::{Vec2, Vec3};
1619

0 commit comments

Comments
 (0)