From 4cacb4f235e76e8f5b25276dafa9494b000e93d5 Mon Sep 17 00:00:00 2001 From: BeastLe9enD Date: Sun, 11 Dec 2022 17:24:53 +0100 Subject: [PATCH] Replace FloatOrd with ordered_float crate --- crates/bevy_core_pipeline/src/core_2d/mod.rs | 6 +- crates/bevy_core_pipeline/src/core_3d/mod.rs | 14 ++--- crates/bevy_pbr/src/render/light.rs | 6 +- crates/bevy_sprite/src/mesh2d/material.rs | 4 +- crates/bevy_sprite/src/render/mod.rs | 4 +- crates/bevy_text/src/font_atlas_set.rs | 10 +-- crates/bevy_ui/src/render/mod.rs | 4 +- crates/bevy_ui/src/render/render_pass.rs | 6 +- crates/bevy_utils/Cargo.toml | 1 + crates/bevy_utils/src/float_ord.rs | 66 -------------------- crates/bevy_utils/src/lib.rs | 3 +- 11 files changed, 29 insertions(+), 95 deletions(-) delete mode 100644 crates/bevy_utils/src/float_ord.rs diff --git a/crates/bevy_core_pipeline/src/core_2d/mod.rs b/crates/bevy_core_pipeline/src/core_2d/mod.rs index 49636f267c8a5..d45e371c013de 100644 --- a/crates/bevy_core_pipeline/src/core_2d/mod.rs +++ b/crates/bevy_core_pipeline/src/core_2d/mod.rs @@ -32,7 +32,7 @@ use bevy_render::{ render_resource::CachedRenderPipelineId, Extract, RenderApp, RenderStage, }; -use bevy_utils::FloatOrd; +use bevy_utils::OrderedFloat; use std::ops::Range; use crate::{tonemapping::TonemappingNode, upscaling::UpscalingNode}; @@ -104,7 +104,7 @@ impl Plugin for Core2dPlugin { } pub struct Transparent2d { - pub sort_key: FloatOrd, + pub sort_key: OrderedFloat, pub entity: Entity, pub pipeline: CachedRenderPipelineId, pub draw_function: DrawFunctionId, @@ -113,7 +113,7 @@ pub struct Transparent2d { } impl PhaseItem for Transparent2d { - type SortKey = FloatOrd; + type SortKey = OrderedFloat; #[inline] fn sort_key(&self) -> Self::SortKey { diff --git a/crates/bevy_core_pipeline/src/core_3d/mod.rs b/crates/bevy_core_pipeline/src/core_3d/mod.rs index 8dc51d760ac1c..0e4f9ce3ab29d 100644 --- a/crates/bevy_core_pipeline/src/core_3d/mod.rs +++ b/crates/bevy_core_pipeline/src/core_3d/mod.rs @@ -41,7 +41,7 @@ use bevy_render::{ view::ViewDepthTexture, Extract, RenderApp, RenderStage, }; -use bevy_utils::{FloatOrd, HashMap}; +use bevy_utils::{HashMap, OrderedFloat}; use crate::{tonemapping::TonemappingNode, upscaling::UpscalingNode}; @@ -122,11 +122,11 @@ pub struct Opaque3d { impl PhaseItem for Opaque3d { // NOTE: Values increase towards the camera. Front-to-back ordering for opaque means we need a descending sort. - type SortKey = Reverse; + type SortKey = Reverse>; #[inline] fn sort_key(&self) -> Self::SortKey { - Reverse(FloatOrd(self.distance)) + Reverse(OrderedFloat(self.distance)) } #[inline] @@ -164,11 +164,11 @@ pub struct AlphaMask3d { impl PhaseItem for AlphaMask3d { // NOTE: Values increase towards the camera. Front-to-back ordering for alpha mask means we need a descending sort. - type SortKey = Reverse; + type SortKey = Reverse>; #[inline] fn sort_key(&self) -> Self::SortKey { - Reverse(FloatOrd(self.distance)) + Reverse(OrderedFloat(self.distance)) } #[inline] @@ -206,11 +206,11 @@ pub struct Transparent3d { impl PhaseItem for Transparent3d { // NOTE: Values increase towards the camera. Back-to-front ordering for transparent means we need an ascending sort. - type SortKey = FloatOrd; + type SortKey = OrderedFloat; #[inline] fn sort_key(&self) -> Self::SortKey { - FloatOrd(self.distance) + OrderedFloat(self.distance) } #[inline] diff --git a/crates/bevy_pbr/src/render/light.rs b/crates/bevy_pbr/src/render/light.rs index e23c1c811c4d7..bf5cf9deb3a7a 100644 --- a/crates/bevy_pbr/src/render/light.rs +++ b/crates/bevy_pbr/src/render/light.rs @@ -32,7 +32,7 @@ use bevy_render::{ Extract, }; use bevy_transform::{components::GlobalTransform, prelude::Transform}; -use bevy_utils::FloatOrd; +use bevy_utils::OrderedFloat; use bevy_utils::{ tracing::{error, warn}, HashMap, @@ -1697,11 +1697,11 @@ pub struct Shadow { } impl PhaseItem for Shadow { - type SortKey = FloatOrd; + type SortKey = OrderedFloat; #[inline] fn sort_key(&self) -> Self::SortKey { - FloatOrd(self.distance) + OrderedFloat(self.distance) } #[inline] diff --git a/crates/bevy_sprite/src/mesh2d/material.rs b/crates/bevy_sprite/src/mesh2d/material.rs index 0f3b86bf9e1d5..6558777c42688 100644 --- a/crates/bevy_sprite/src/mesh2d/material.rs +++ b/crates/bevy_sprite/src/mesh2d/material.rs @@ -35,7 +35,7 @@ use bevy_render::{ Extract, RenderApp, RenderStage, }; use bevy_transform::components::{GlobalTransform, Transform}; -use bevy_utils::{FloatOrd, HashMap, HashSet}; +use bevy_utils::{HashMap, HashSet, OrderedFloat}; use std::hash::Hash; use std::marker::PhantomData; @@ -371,7 +371,7 @@ pub fn queue_material2d_meshes( // lowest sort key and getting closer should increase. As we have // -z in front of the camera, the largest distance is -far with values increasing toward the // camera. As such we can just use mesh_z as the distance - sort_key: FloatOrd(mesh_z), + sort_key: OrderedFloat(mesh_z), // This material is not batched batch_range: None, }); diff --git a/crates/bevy_sprite/src/render/mod.rs b/crates/bevy_sprite/src/render/mod.rs index 1cdc6d93782d8..4f3f6ec1763ef 100644 --- a/crates/bevy_sprite/src/render/mod.rs +++ b/crates/bevy_sprite/src/render/mod.rs @@ -31,8 +31,8 @@ use bevy_render::{ Extract, }; use bevy_transform::components::GlobalTransform; -use bevy_utils::FloatOrd; use bevy_utils::HashMap; +use bevy_utils::OrderedFloat; use bytemuck::{Pod, Zeroable}; use fixedbitset::FixedBitSet; @@ -632,7 +632,7 @@ pub fn queue_sprites( }); // These items will be sorted by depth with other phase items - let sort_key = FloatOrd(extracted_sprite.transform.translation().z); + let sort_key = OrderedFloat(extracted_sprite.transform.translation().z); // Store the vertex data and add the item to the render phase if current_batch.colored { diff --git a/crates/bevy_text/src/font_atlas_set.rs b/crates/bevy_text/src/font_atlas_set.rs index 9468a5405fcfb..b1600ae8351d1 100644 --- a/crates/bevy_text/src/font_atlas_set.rs +++ b/crates/bevy_text/src/font_atlas_set.rs @@ -5,10 +5,10 @@ use bevy_math::Vec2; use bevy_reflect::TypeUuid; use bevy_render::texture::Image; use bevy_sprite::TextureAtlas; -use bevy_utils::FloatOrd; use bevy_utils::HashMap; +use bevy_utils::OrderedFloat; -type FontSizeKey = FloatOrd; +type FontSizeKey = OrderedFloat; #[derive(TypeUuid)] #[uuid = "73ba778b-b6b5-4f45-982d-d21b6b86ace2"] @@ -41,7 +41,7 @@ impl FontAtlasSet { pub fn has_glyph(&self, glyph_id: GlyphId, glyph_position: Point, font_size: f32) -> bool { self.font_atlases - .get(&FloatOrd(font_size)) + .get(&OrderedFloat(font_size)) .map_or(false, |font_atlas| { font_atlas .iter() @@ -61,7 +61,7 @@ impl FontAtlasSet { let font_size = glyph.scale.y; let font_atlases = self .font_atlases - .entry(FloatOrd(font_size)) + .entry(OrderedFloat(font_size)) .or_insert_with(|| { vec![FontAtlas::new( textures, @@ -117,7 +117,7 @@ impl FontAtlasSet { position: Point, ) -> Option { self.font_atlases - .get(&FloatOrd(font_size)) + .get(&OrderedFloat(font_size)) .and_then(|font_atlases| { font_atlases .iter() diff --git a/crates/bevy_ui/src/render/mod.rs b/crates/bevy_ui/src/render/mod.rs index 595896c1fdfec..f371674de5044 100644 --- a/crates/bevy_ui/src/render/mod.rs +++ b/crates/bevy_ui/src/render/mod.rs @@ -27,8 +27,8 @@ use bevy_render::{ use bevy_sprite::{SpriteAssetEvents, TextureAtlas}; use bevy_text::{Text, TextLayoutInfo}; use bevy_transform::components::GlobalTransform; -use bevy_utils::FloatOrd; use bevy_utils::HashMap; +use bevy_utils::OrderedFloat; use bevy_window::{WindowId, Windows}; use bytemuck::{Pod, Zeroable}; use std::ops::Range; @@ -620,7 +620,7 @@ pub fn queue_uinodes( draw_function: draw_ui_function, pipeline, entity, - sort_key: FloatOrd(batch.z), + sort_key: OrderedFloat(batch.z), }); } } diff --git a/crates/bevy_ui/src/render/render_pass.rs b/crates/bevy_ui/src/render/render_pass.rs index 527e96538f1f3..97edff952df37 100644 --- a/crates/bevy_ui/src/render/render_pass.rs +++ b/crates/bevy_ui/src/render/render_pass.rs @@ -11,7 +11,7 @@ use bevy_render::{ renderer::*, view::*, }; -use bevy_utils::FloatOrd; +use bevy_utils::OrderedFloat; pub struct UiPassNode { ui_view_query: QueryState< @@ -102,14 +102,14 @@ impl Node for UiPassNode { } pub struct TransparentUi { - pub sort_key: FloatOrd, + pub sort_key: OrderedFloat, pub entity: Entity, pub pipeline: CachedRenderPipelineId, pub draw_function: DrawFunctionId, } impl PhaseItem for TransparentUi { - type SortKey = FloatOrd; + type SortKey = OrderedFloat; #[inline] fn sort_key(&self) -> Self::SortKey { diff --git a/crates/bevy_utils/Cargo.toml b/crates/bevy_utils/Cargo.toml index a61ce8d16594d..287b06bd4d235 100644 --- a/crates/bevy_utils/Cargo.toml +++ b/crates/bevy_utils/Cargo.toml @@ -14,6 +14,7 @@ tracing = { version = "0.1", default-features = false, features = ["std"] } instant = { version = "0.1", features = ["wasm-bindgen"] } uuid = { version = "1.1", features = ["v4", "serde"] } hashbrown = { version = "0.12", features = ["serde"] } +ordered-float = "3.4.0" [target.'cfg(target_arch = "wasm32")'.dependencies] getrandom = {version = "0.2.0", features = ["js"]} diff --git a/crates/bevy_utils/src/float_ord.rs b/crates/bevy_utils/src/float_ord.rs deleted file mode 100644 index 9e7931d2204a3..0000000000000 --- a/crates/bevy_utils/src/float_ord.rs +++ /dev/null @@ -1,66 +0,0 @@ -use std::{ - cmp::Ordering, - hash::{Hash, Hasher}, - ops::Neg, -}; - -/// A wrapper for floats that implements [`Ord`], [`Eq`], and [`Hash`] traits. -/// -/// This is a work around for the fact that the IEEE 754-2008 standard, -/// implemented by Rust's [`f32`] type, -/// doesn't define an ordering for [`NaN`](f32::NAN), -/// and `NaN` is not considered equal to any other `NaN`. -/// -/// Wrapping a float with `FloatOrd` breaks conformance with the standard -/// by sorting `NaN` as less than all other numbers and equal to any other `NaN`. -#[derive(Debug, Copy, Clone, PartialOrd)] -pub struct FloatOrd(pub f32); - -#[allow(clippy::derive_ord_xor_partial_ord)] -impl Ord for FloatOrd { - fn cmp(&self, other: &Self) -> Ordering { - self.0.partial_cmp(&other.0).unwrap_or_else(|| { - if self.0.is_nan() && !other.0.is_nan() { - Ordering::Less - } else if !self.0.is_nan() && other.0.is_nan() { - Ordering::Greater - } else { - Ordering::Equal - } - }) - } -} - -impl PartialEq for FloatOrd { - fn eq(&self, other: &Self) -> bool { - if self.0.is_nan() && other.0.is_nan() { - true - } else { - self.0 == other.0 - } - } -} - -impl Eq for FloatOrd {} - -impl Hash for FloatOrd { - fn hash(&self, state: &mut H) { - if self.0.is_nan() { - // Ensure all NaN representations hash to the same value - state.write(&f32::to_ne_bytes(f32::NAN)); - } else if self.0 == 0.0 { - // Ensure both zeroes hash to the same value - state.write(&f32::to_ne_bytes(0.0f32)); - } else { - state.write(&f32::to_ne_bytes(self.0)); - } - } -} - -impl Neg for FloatOrd { - type Output = FloatOrd; - - fn neg(self) -> Self::Output { - FloatOrd(-self.0) - } -} diff --git a/crates/bevy_utils/src/lib.rs b/crates/bevy_utils/src/lib.rs index d942ad4feda4d..24d7f6804b065 100644 --- a/crates/bevy_utils/src/lib.rs +++ b/crates/bevy_utils/src/lib.rs @@ -9,13 +9,12 @@ pub use short_names::get_short_name; pub mod synccell; mod default; -mod float_ord; pub use ahash::AHasher; pub use default::default; -pub use float_ord::*; pub use hashbrown; pub use instant::{Duration, Instant}; +pub use ordered_float::*; pub use tracing; pub use uuid::Uuid;