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

Relax BufferVec's type constraints #12866

Merged
merged 2 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion crates/bevy_pbr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ thiserror = { version = "1", optional = true }
bitflags = "2.3"
fixedbitset = "0.5"
# direct dependency required for derive macro
bytemuck = { version = "1", features = ["derive"] }
bytemuck = { version = "1", features = ["derive", "must_cast"] }
radsort = "0.1"
smallvec = "1.6"
serde = { version = "1", features = ["derive", "rc"] }
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_pbr/src/meshlet/gpu_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ pub fn queue_material_meshlet_meshes<M: Material>(
}

// TODO: Try using Queue::write_buffer_with() in queue_meshlet_mesh_upload() to reduce copies
fn upload_storage_buffer<T: ShaderSize + bytemuck::Pod>(
fn upload_storage_buffer<T: ShaderSize + bytemuck::NoUninit>(
buffer: &mut StorageBuffer<Vec<T>>,
render_device: &RenderDevice,
render_queue: &RenderQueue,
Expand All @@ -187,7 +187,7 @@ fn upload_storage_buffer<T: ShaderSize + bytemuck::Pod>(

if capacity >= size {
let inner = inner.unwrap();
let bytes = bytemuck::cast_slice(buffer.get().as_slice());
let bytes = bytemuck::must_cast_slice(buffer.get().as_slice());
render_queue.write_buffer(inner, 0, bytes);
} else {
buffer.write_buffer(render_device, render_queue);
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_pbr/src/render/morph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use bevy_render::{
view::ViewVisibility,
Extract,
};
use bytemuck::Pod;
use bytemuck::NoUninit;

#[derive(Component)]
pub struct MorphIndex {
Expand Down Expand Up @@ -54,7 +54,7 @@ const fn can_align(step: usize, target: usize) -> bool {
const WGPU_MIN_ALIGN: usize = 256;

/// Align a [`BufferVec`] to `N` bytes by padding the end with `T::default()` values.
fn add_to_alignment<T: Pod + Default>(buffer: &mut BufferVec<T>) {
fn add_to_alignment<T: NoUninit + Default>(buffer: &mut BufferVec<T>) {
let n = WGPU_MIN_ALIGN;
let t_size = mem::size_of::<T>();
if !can_align(n, t_size) {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ wgpu = { version = "0.19.3", default-features = false, features = [
naga = { version = "0.19", features = ["wgsl-in"] }
serde = { version = "1", features = ["derive"] }
bitflags = { version = "2.3", features = ["serde"] }
bytemuck = { version = "1.5", features = ["derive"] }
bytemuck = { version = "1.5", features = ["derive", "must_cast"] }
downcast-rs = "1.2.0"
thread_local = "1.1"
thiserror = "1.0"
Expand Down
12 changes: 6 additions & 6 deletions crates/bevy_render/src/render_resource/buffer_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use crate::{
render_resource::Buffer,
renderer::{RenderDevice, RenderQueue},
};
use bytemuck::{cast_slice, Pod};
use bytemuck::{must_cast_slice, NoUninit};
use wgpu::BufferUsages;

/// A structure for storing raw bytes that have already been properly formatted
/// for use by the GPU.
///
/// "Properly formatted" means that item data already meets the alignment and padding
/// requirements for how it will be used on the GPU. The item type must implement [`Pod`]
/// requirements for how it will be used on the GPU. The item type must implement [`NoUninit`]
/// for its data representation to be directly copyable.
///
/// Index, vertex, and instance-rate vertex buffers have no alignment nor padding requirements and
Expand All @@ -28,7 +28,7 @@ use wgpu::BufferUsages;
/// * [`GpuArrayBuffer`](crate::render_resource::GpuArrayBuffer)
/// * [`BufferVec`]
/// * [`Texture`](crate::render_resource::Texture)
pub struct BufferVec<T: Pod> {
pub struct BufferVec<T: NoUninit> {
values: Vec<T>,
buffer: Option<Buffer>,
capacity: usize,
Expand All @@ -38,7 +38,7 @@ pub struct BufferVec<T: Pod> {
label_changed: bool,
}

impl<T: Pod> BufferVec<T> {
impl<T: NoUninit> BufferVec<T> {
pub const fn new(buffer_usage: BufferUsages) -> Self {
Self {
values: Vec::new(),
Expand Down Expand Up @@ -132,7 +132,7 @@ impl<T: Pod> BufferVec<T> {
self.reserve(self.values.len(), device);
if let Some(buffer) = &self.buffer {
let range = 0..self.item_size * self.values.len();
let bytes: &[u8] = cast_slice(&self.values);
let bytes: &[u8] = must_cast_slice(&self.values);
queue.write_buffer(buffer, 0, &bytes[range]);
}
}
Expand All @@ -154,7 +154,7 @@ impl<T: Pod> BufferVec<T> {
}
}

impl<T: Pod> Extend<T> for BufferVec<T> {
impl<T: NoUninit> Extend<T> for BufferVec<T> {
#[inline]
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
self.values.extend(iter);
Expand Down