-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Atomic counter for render resource ids and Render to Gpu rename #2895
Changes from all commits
a6b3bcd
a8d6f11
eb44c42
ec90b60
0d4ff23
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::{parse_macro_input, DeriveInput}; | ||
|
||
pub fn derive_id(input: TokenStream) -> TokenStream { | ||
let ast = parse_macro_input!(input as DeriveInput); | ||
|
||
let struct_name = &ast.ident; | ||
let error_message = format!("The system ran out of unique `{}`s.", struct_name); | ||
let (_impl_generics, type_generics, where_clause) = &ast.generics.split_for_impl(); | ||
|
||
TokenStream::from(quote! { | ||
impl #struct_name #type_generics #where_clause { | ||
pub fn new() -> Self { | ||
use std::sync::atomic::{AtomicU64, Ordering}; | ||
static COUNTER: AtomicU64 = AtomicU64::new(0); | ||
COUNTER | ||
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |val| { | ||
val.checked_add(1) | ||
}) | ||
.map(Self) | ||
.expect(#error_message) | ||
} | ||
} | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub use bevy_derive::Id; | ||
pub type IdType = u64; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,13 @@ | ||
use bevy_math::{IVec2, Vec2}; | ||
use bevy_utils::{tracing::warn, Uuid}; | ||
use bevy_utils::{tracing::warn, Id, IdType}; | ||
use raw_window_handle::RawWindowHandle; | ||
|
||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] | ||
pub struct WindowId(Uuid); | ||
#[derive(Id, Copy, Clone, Hash, Eq, PartialEq, Debug)] | ||
pub struct WindowId(pub IdType); | ||
|
||
impl WindowId { | ||
pub fn new() -> Self { | ||
WindowId(Uuid::new_v4()) | ||
} | ||
|
||
pub fn primary() -> Self { | ||
WindowId(Uuid::from_u128(0)) | ||
WindowId(0) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is problematic, because the first WindowId generated via new() will be the primary window. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe use |
||
|
||
pub fn is_primary(&self) -> bool { | ||
|
@@ -25,7 +21,7 @@ use crate::raw_window_handle::RawWindowHandleWrapper; | |
|
||
impl fmt::Display for WindowId { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
self.0.to_simple().fmt(f) | ||
self.0.fmt(f) | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe make it a regular macro? Something like
define_id!(pub SystemId)
which defines the struct and add all appropriate derives.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point! But this makes it also harder to extend those types, right? And a derive trait seems like the more idiomatic choice?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is not really anything that can or should be extended about those ids. Using more than one field or a non-integer field doesn't make sense for an id. Also you can still use
impl SystemId {}
of you want.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, that makes sense. I will try it :)
Thank you
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mhh I am not sure whether defining a new type inside a macro is even possible, because my generated struct is not visible across files and thus can't be imported. Any ideas on how to make the compiler know this struct before expanding the macro?