Skip to content

Commit 7a436e3

Browse files
authored
move define_atomic_id to bevy_utils (#22417)
# Objective - when i made bevy_shader, i inlined the define_atomic_id macro because i wasnt sure if one usage was enough justification for kicking it into utils - ~~bevy_material needs this in several places too, so let's kick it out in preparation. 3 usages outside bevy_render is enough justification~~ its not actually needed for bevy_material ## Solution - move file ## Testing - examples, clippy etc.
1 parent 59104ab commit 7a436e3

File tree

11 files changed

+17
-34
lines changed

11 files changed

+17
-34
lines changed

crates/bevy_render/src/render_resource/bind_group.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::{
2-
define_atomic_id,
32
render_asset::RenderAssets,
43
render_resource::{BindGroupLayout, Buffer, PipelineCache, Sampler, TextureView},
54
renderer::{RenderDevice, WgpuWrapper},
@@ -9,6 +8,7 @@ use bevy_derive::{Deref, DerefMut};
98
use bevy_ecs::system::{SystemParam, SystemParamItem};
109
use bevy_render::render_resource::BindGroupLayoutDescriptor;
1110
pub use bevy_render_macros::AsBindGroup;
11+
use bevy_utils::define_atomic_id;
1212
use core::ops::Deref;
1313
use encase::ShaderType;
1414
use thiserror::Error;

crates/bevy_render/src/render_resource/bind_group_layout.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use crate::{define_atomic_id, renderer::WgpuWrapper};
1+
use crate::renderer::WgpuWrapper;
2+
use bevy_utils::define_atomic_id;
23
use core::ops::Deref;
34

45
define_atomic_id!(BindGroupLayoutId);

crates/bevy_render/src/render_resource/buffer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::define_atomic_id;
21
use crate::renderer::WgpuWrapper;
2+
use bevy_utils::define_atomic_id;
33
use core::ops::{Deref, RangeBounds};
44

55
define_atomic_id!(BufferId);

crates/bevy_render/src/render_resource/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ mod gpu_array_buffer;
1010
mod pipeline;
1111
mod pipeline_cache;
1212
mod pipeline_specializer;
13-
pub mod resource_macros;
1413
mod specializer;
1514
mod storage_buffer;
1615
mod texture;

crates/bevy_render/src/render_resource/pipeline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use crate::define_atomic_id;
21
use crate::renderer::WgpuWrapper;
32
use alloc::borrow::Cow;
43
use bevy_asset::Handle;
54
use bevy_mesh::VertexBufferLayout;
65
use bevy_shader::{Shader, ShaderDefVal};
6+
use bevy_utils::define_atomic_id;
77
use core::iter;
88
use core::ops::Deref;
99
use thiserror::Error;

crates/bevy_render/src/render_resource/texture.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::define_atomic_id;
21
use crate::renderer::WgpuWrapper;
32
use bevy_derive::{Deref, DerefMut};
43
use bevy_ecs::resource::Resource;
4+
use bevy_utils::define_atomic_id;
55
use core::ops::Deref;
66

77
define_atomic_id!(TextureId);

crates/bevy_shader/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ keywords = ["bevy", "shader"]
1313
bevy_asset = { path = "../bevy_asset", version = "0.18.0-dev" }
1414
bevy_reflect = { path = "../bevy_reflect", version = "0.18.0-dev" }
1515
bevy_platform = { path = "../bevy_platform", version = "0.18.0-dev" }
16+
bevy_utils = { path = "../bevy_utils", version = "0.18.0-dev" }
1617

1718
# other
1819
wgpu-types = { version = "27", default-features = false }

crates/bevy_shader/src/shader.rs

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,10 @@ use super::ShaderDefVal;
22
use alloc::borrow::Cow;
33
use bevy_asset::{io::Reader, Asset, AssetLoader, AssetPath, Handle, LoadContext};
44
use bevy_reflect::TypePath;
5-
use core::{marker::Copy, num::NonZero};
5+
use bevy_utils::define_atomic_id;
66
use thiserror::Error;
77

8-
#[derive(Copy, Clone, Hash, Eq, PartialEq, PartialOrd, Ord, Debug)]
9-
pub struct ShaderId(NonZero<u32>);
10-
11-
impl ShaderId {
12-
#[expect(
13-
clippy::new_without_default,
14-
reason = "Implementing the `Default` trait on atomic IDs would imply that two `<AtomicIdType>::default()` equal each other. By only implementing `new()`, we indicate that each atomic ID created will be unique."
15-
)]
16-
pub fn new() -> Self {
17-
use core::sync::atomic::{AtomicU32, Ordering};
18-
static COUNTER: AtomicU32 = AtomicU32::new(1);
19-
let counter = COUNTER.fetch_add(1, Ordering::Relaxed);
20-
Self(NonZero::<u32>::new(counter).unwrap_or_else(|| {
21-
panic!("The system ran out of unique `{}`s.", stringify!(ShaderId));
22-
}))
23-
}
24-
}
25-
impl From<ShaderId> for NonZero<u32> {
26-
fn from(value: ShaderId) -> Self {
27-
value.0
28-
}
29-
}
30-
impl From<NonZero<u32>> for ShaderId {
31-
fn from(value: NonZero<u32>) -> Self {
32-
Self(value)
33-
}
34-
}
8+
define_atomic_id!(ShaderId);
359

3610
#[derive(Error, Debug)]
3711
pub enum ShaderReflectError {

crates/bevy_render/src/render_resource/resource_macros.rs renamed to crates/bevy_utils/src/atomic_id.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/// Defines an id type which guarantees global uniqueness via atomics on a static global.
12
#[macro_export]
23
macro_rules! define_atomic_id {
34
($atomic_id_type:ident) => {

crates/bevy_utils/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub mod prelude {
4848
pub use disqualified::ShortName;
4949
}
5050

51+
mod atomic_id;
5152
mod debug_info;
5253
mod default;
5354
mod once;

0 commit comments

Comments
 (0)