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

[Merged by Bors] - Constify SpritePipelineKey implementation. #6976

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
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
16 changes: 11 additions & 5 deletions crates/bevy_sprite/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,25 +160,31 @@ impl SpritePipelineKey {
const MSAA_MASK_BITS: u32 = 0b111;
const MSAA_SHIFT_BITS: u32 = 32 - Self::MSAA_MASK_BITS.count_ones();

pub fn from_msaa_samples(msaa_samples: u32) -> Self {
#[inline]
pub const fn from_msaa_samples(msaa_samples: u32) -> Self {
let msaa_bits =
(msaa_samples.trailing_zeros() & Self::MSAA_MASK_BITS) << Self::MSAA_SHIFT_BITS;
Self::from_bits(msaa_bits).unwrap()
// SAFETY: The input is the correct type and the mask will ensure the output range is
// capped
unsafe { Self::from_bits_unchecked(msaa_bits) }
Copy link
Contributor

Choose a reason for hiding this comment

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

For other reviewers here's the safety comment from the bitflags crate

https://docs.rs/bitflags/latest/src/bitflags/lib.rs.html#575-577

/// The caller of `from_bits_unchecked()` has to ensure that
/// all bits correspond to a defined flag or that extra bits
/// are valid for this bitflags type.

Copy link
Member

@cart cart Dec 20, 2022

Choose a reason for hiding this comment

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

Idk if this is a good enough reason to introduce unsafe code. Does const-ing this unlock specific scenarios we need? Are there performance benefits (that we have measured)?

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
unsafe { Self::from_bits_unchecked(msaa_bits) }
Self::from_bits_truncate(msaa_bits)

This should be const and safe.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I overlooked that, will fix it shortly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

}

pub fn msaa_samples(&self) -> u32 {
#[inline]
pub const fn msaa_samples(&self) -> u32 {
1 << ((self.bits >> Self::MSAA_SHIFT_BITS) & Self::MSAA_MASK_BITS)
}

pub fn from_colored(colored: bool) -> Self {
#[inline]
pub const fn from_colored(colored: bool) -> Self {
if colored {
SpritePipelineKey::COLORED
} else {
SpritePipelineKey::NONE
}
}

pub fn from_hdr(hdr: bool) -> Self {
#[inline]
pub const fn from_hdr(hdr: bool) -> Self {
if hdr {
SpritePipelineKey::HDR
} else {
Expand Down