Skip to content

Commit 7f74e3c

Browse files
authored
Fix depth_bias and build errors on less capable platforms (#17079)
# Objective - I'm compiling (parts of) bevy for an embedded platform with no 64bit atomic and ctrlc handler support. Some compilation errors came up. This PR contains the fixes for those. - Fix depth_bias casting in PBR material (Fixes #14169) - Negative depth_bias values were casted to 0 before this PR - f32::INFINITY depth_bias value was casted to -1 before this PR ## Solutions - Restrict 64bit atomic reflection to supported platforms - Restrict ctrlc handler to supported platforms (linux, windows or macos instead of "not wasm") - The depth bias value (f32) is first casted to i32 then u64 in order to preserve negative values ## Testing - This version compiles on a platform with no 64bit atomic support, and no ctrlc support - CtrlC handler still works on Linux and Windows (I can't test on Macos) - depth_bias: ```rust println!("{}",f32::INFINITY as u64 as i32); // Prints: -1 (old implementation) println!("{}",f32::INFINITY as i32 as u64 as i32); // Prints: 2147483647 (expected, new implementation) ``` Also ran a modified version of 3d_scene example with the following results: RED cube depth_bias: -1000.0 BLUE cube depth_bias: 0.0 ![image](https://github.com/user-attachments/assets/d5a96759-dd3c-4a0a-97ff-821163873a0d) RED cube depth_bias: -INF BLUE cube depth_bias: 0.0 ![image](https://github.com/user-attachments/assets/e4de22b4-0c31-4dea-8be1-12b700e440b9) RED cube depth_bias: INF (case reported in #14169) BLUE cube depth_bias: 0.0 (Im not completely sure whats going on with the shadows here, it seems like depth_bias has some affect to those aswell, if this is unintentional this issue was not introduced by this PR) ![image](https://github.com/user-attachments/assets/52d9348f-df27-468f-a001-2d3d3ff6b553)
1 parent a8f15bd commit 7f74e3c

File tree

6 files changed

+10
-6
lines changed

6 files changed

+10
-6
lines changed

crates/bevy_app/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ portable-atomic-util = { version = "0.2.4", features = [
9393
"alloc",
9494
], optional = true }
9595

96-
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
96+
[target.'cfg(any(unix, windows))'.dependencies]
9797
ctrlc = { version = "3.4.4", optional = true }
9898

9999
[target.'cfg(target_arch = "wasm32")'.dependencies]

crates/bevy_app/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ mod schedule_runner;
3434
mod sub_app;
3535
#[cfg(feature = "bevy_tasks")]
3636
mod task_pool_plugin;
37-
#[cfg(all(not(target_arch = "wasm32"), feature = "std"))]
37+
#[cfg(all(any(unix, windows), feature = "std"))]
3838
mod terminal_ctrl_c_handler;
3939

4040
pub use app::*;
@@ -46,7 +46,7 @@ pub use schedule_runner::*;
4646
pub use sub_app::*;
4747
#[cfg(feature = "bevy_tasks")]
4848
pub use task_pool_plugin::*;
49-
#[cfg(all(not(target_arch = "wasm32"), feature = "std"))]
49+
#[cfg(all(any(unix, windows), feature = "std"))]
5050
pub use terminal_ctrl_c_handler::*;
5151

5252
/// The app prelude.

crates/bevy_dev_tools/src/ci_testing/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl Plugin for CiTestingPlugin {
6262

6363
// The offending system does not exist in the wasm32 target.
6464
// As a result, we must conditionally order the two systems using a system set.
65-
#[cfg(not(target_arch = "wasm32"))]
65+
#[cfg(any(unix, windows))]
6666
app.configure_sets(
6767
Update,
6868
SendEvents.before(bevy_app::TerminalCtrlCHandlerPlugin::exit_on_flag),

crates/bevy_internal/src/default_plugins.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ plugin_group! {
1818
bevy_window:::WindowPlugin,
1919
#[cfg(feature = "bevy_window")]
2020
bevy_a11y:::AccessibilityPlugin,
21-
#[custom(cfg(not(target_arch = "wasm32")))]
21+
#[custom(cfg(any(unix, windows)))]
2222
bevy_app:::TerminalCtrlCHandlerPlugin,
2323
#[cfg(feature = "bevy_asset")]
2424
bevy_asset:::AssetPlugin,

crates/bevy_pbr/src/pbr_material.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,9 @@ impl From<&StandardMaterial> for StandardMaterialKey {
12381238
}
12391239

12401240
key.insert(StandardMaterialKey::from_bits_retain(
1241-
(material.depth_bias as u64) << STANDARD_MATERIAL_KEY_DEPTH_BIAS_SHIFT,
1241+
// Casting to i32 first to ensure the full i32 range is preserved.
1242+
// (wgpu expects the depth_bias as an i32 when this is extracted in a later step)
1243+
(material.depth_bias as i32 as u64) << STANDARD_MATERIAL_KEY_DEPTH_BIAS_SHIFT,
12421244
));
12431245
key
12441246
}

crates/bevy_reflect/src/impls/std.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,12 @@ impl_reflect_for_atomic!(
367367
::core::sync::atomic::AtomicUsize,
368368
::core::sync::atomic::Ordering::SeqCst
369369
);
370+
#[cfg(target_has_atomic = "64")]
370371
impl_reflect_for_atomic!(
371372
::core::sync::atomic::AtomicI64,
372373
::core::sync::atomic::Ordering::SeqCst
373374
);
375+
#[cfg(target_has_atomic = "64")]
374376
impl_reflect_for_atomic!(
375377
::core::sync::atomic::AtomicU64,
376378
::core::sync::atomic::Ordering::SeqCst

0 commit comments

Comments
 (0)