Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add no_std support to bevy_mikktspace #15528

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea to keep the cfg feature hell in one place. This does remind me of the functionality of bevy_math::ops, though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think it would make sense to move this into bevy_math, but that's a much harder change since it includes (comparatively) lots of dependencies. For now I think this is an acceptable mess, but highlights the need for possible changes.

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