Skip to content

Commit

Permalink
Add no_std support to bevy_mikktspace (#15528)
Browse files Browse the repository at this point in the history
# Objective

- Contributes to #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`
  • Loading branch information
bushrat011899 authored Sep 30, 2024
1 parent 0d2eb3d commit cedd0c5
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
9 changes: 8 additions & 1 deletion crates/bevy_mikktspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@ license = "Zlib AND (MIT OR Apache-2.0)"
keywords = ["bevy", "3D", "graphics", "algorithm", "tangent"]
rust-version = "1.76.0"

[features]
default = ["std"]

std = ["glam/std"]
libm = ["glam/libm", "dep:libm"]

[dependencies]
glam = "0.29"
glam = { version = "0.29.0", default-features = false }
libm = { version = "0.2", default-features = false, optional = true }

[[example]]
name = "generate"
Expand Down
69 changes: 65 additions & 4 deletions crates/bevy_mikktspace/src/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
unsafe_code
)]

use alloc::{vec, vec::Vec};
use core::ptr::{self, null_mut};

use glam::Vec3;
Expand Down Expand Up @@ -211,7 +212,7 @@ pub unsafe fn genTangSpace<I: Geometry>(geometry: &mut I, fAngularThreshold: f32
let mut index = 0;
let iNrFaces = geometry.num_faces();
let mut bRes: bool = false;
let fThresCos = fAngularThreshold.to_radians().cos();
let fThresCos = cos(fAngularThreshold.to_radians());
f = 0;
while f < iNrFaces {
let verts = geometry.num_vertices_of_face(f);
Expand Down Expand Up @@ -630,7 +631,7 @@ unsafe fn VNotZero(v: Vec3) -> bool {
}

unsafe fn NotZero(fX: f32) -> bool {
fX.abs() > 1.17549435e-38f32
abs(fX) > 1.17549435e-38f32
}

unsafe fn EvalTspace<I: Geometry>(
Expand Down Expand Up @@ -724,7 +725,7 @@ unsafe fn EvalTspace<I: Geometry>(
} else {
fCos
};
fAngle = (fCos as f64).acos() as f32;
fAngle = acosf64(fCos as f64) as f32;
fMagS = (*pTriInfos.offset(f as isize)).fMagS;
fMagT = (*pTriInfos.offset(f as isize)).fMagT;
res.vOs = res.vOs + (fAngle * vOs);
Expand Down Expand Up @@ -1010,7 +1011,7 @@ unsafe fn InitTriInfo<I: Geometry>(
0i32
};
if NotZero(fSignedAreaSTx2) {
let fAbsArea: f32 = fSignedAreaSTx2.abs();
let fAbsArea: f32 = abs(fSignedAreaSTx2);
let fLenOs: f32 = vOs.length();
let fLenOt: f32 = vOt.length();
let fS: f32 = if (*pTriInfos.offset(f as isize)).iFlag & 8i32 == 0i32 {
Expand Down Expand Up @@ -1808,3 +1809,63 @@ unsafe fn GenerateInitialVerticesIndexList<I: Geometry>(
}
return iTSpacesOffs;
}

fn cos(value: f32) -> f32 {
#[cfg(feature = "std")]
{
value.cos()
}
#[cfg(all(not(feature = "std"), feature = "libm"))]
{
libm::cosf(value)
}
#[cfg(all(not(feature = "std"), not(feature = "libm")))]
{
compile_error!("Require either 'libm' or 'std' for `cos`")
}
}

fn acos(value: f32) -> f32 {
#[cfg(feature = "std")]
{
value.acos()
}
#[cfg(all(not(feature = "std"), feature = "libm"))]
{
libm::acosf(value)
}
#[cfg(all(not(feature = "std"), not(feature = "libm")))]
{
compile_error!("Require either 'libm' or 'std' for `acos`")
}
}

fn abs(value: f32) -> f32 {
#[cfg(feature = "std")]
{
value.abs()
}
#[cfg(all(not(feature = "std"), feature = "libm"))]
{
libm::fabsf(value)
}
#[cfg(all(not(feature = "std"), not(feature = "libm")))]
{
compile_error!("Require either 'libm' or 'std' for `abs`")
}
}

fn acosf64(value: f64) -> f64 {
#[cfg(feature = "std")]
{
value.acos()
}
#[cfg(all(not(feature = "std"), feature = "libm"))]
{
libm::acos(value)
}
#[cfg(all(not(feature = "std"), not(feature = "libm")))]
{
compile_error!("Require either 'libm' or 'std' for `acos`")
}
}
3 changes: 3 additions & 0 deletions crates/bevy_mikktspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
html_logo_url = "https://bevyengine.org/assets/icon.png",
html_favicon_url = "https://bevyengine.org/assets/icon.png"
)]
#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;

use glam::{Vec2, Vec3};

Expand Down

0 comments on commit cedd0c5

Please sign in to comment.