From e66457c8933aef3555fadcc4ed44f52bbe30f1c2 Mon Sep 17 00:00:00 2001 From: james7132 Date: Wed, 3 Apr 2024 14:41:57 -0700 Subject: [PATCH 1/2] Relax BufferVec's constraints --- crates/bevy_pbr/src/meshlet/gpu_scene.rs | 2 +- crates/bevy_pbr/src/render/morph.rs | 4 ++-- crates/bevy_render/Cargo.toml | 2 +- crates/bevy_render/src/render_resource/buffer_vec.rs | 12 ++++++------ 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/crates/bevy_pbr/src/meshlet/gpu_scene.rs b/crates/bevy_pbr/src/meshlet/gpu_scene.rs index 492179dc2bcfe..1ec7227c86aea 100644 --- a/crates/bevy_pbr/src/meshlet/gpu_scene.rs +++ b/crates/bevy_pbr/src/meshlet/gpu_scene.rs @@ -174,7 +174,7 @@ pub fn queue_material_meshlet_meshes( } // TODO: Try using Queue::write_buffer_with() in queue_meshlet_mesh_upload() to reduce copies -fn upload_storage_buffer( +fn upload_storage_buffer( buffer: &mut StorageBuffer>, render_device: &RenderDevice, render_queue: &RenderQueue, diff --git a/crates/bevy_pbr/src/render/morph.rs b/crates/bevy_pbr/src/render/morph.rs index cdc9c599ea24b..b53cf62f2f8e9 100644 --- a/crates/bevy_pbr/src/render/morph.rs +++ b/crates/bevy_pbr/src/render/morph.rs @@ -11,7 +11,7 @@ use bevy_render::{ view::ViewVisibility, Extract, }; -use bytemuck::Pod; +use bytemuck::NoUninit; #[derive(Component)] pub struct MorphIndex { @@ -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(buffer: &mut BufferVec) { +fn add_to_alignment(buffer: &mut BufferVec) { let n = WGPU_MIN_ALIGN; let t_size = mem::size_of::(); if !can_align(n, t_size) { diff --git a/crates/bevy_render/Cargo.toml b/crates/bevy_render/Cargo.toml index cbc25b9146b56..cec4260c60b27 100644 --- a/crates/bevy_render/Cargo.toml +++ b/crates/bevy_render/Cargo.toml @@ -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" diff --git a/crates/bevy_render/src/render_resource/buffer_vec.rs b/crates/bevy_render/src/render_resource/buffer_vec.rs index 26656c58ed983..f330d52dea50a 100644 --- a/crates/bevy_render/src/render_resource/buffer_vec.rs +++ b/crates/bevy_render/src/render_resource/buffer_vec.rs @@ -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 @@ -28,7 +28,7 @@ use wgpu::BufferUsages; /// * [`GpuArrayBuffer`](crate::render_resource::GpuArrayBuffer) /// * [`BufferVec`] /// * [`Texture`](crate::render_resource::Texture) -pub struct BufferVec { +pub struct BufferVec { values: Vec, buffer: Option, capacity: usize, @@ -38,7 +38,7 @@ pub struct BufferVec { label_changed: bool, } -impl BufferVec { +impl BufferVec { pub const fn new(buffer_usage: BufferUsages) -> Self { Self { values: Vec::new(), @@ -132,7 +132,7 @@ impl BufferVec { 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]); } } @@ -154,7 +154,7 @@ impl BufferVec { } } -impl Extend for BufferVec { +impl Extend for BufferVec { #[inline] fn extend>(&mut self, iter: I) { self.values.extend(iter); From 597a47632dac1e461c51526bf16635b39fcc5514 Mon Sep 17 00:00:00 2001 From: james7132 Date: Thu, 4 Apr 2024 19:01:04 -0700 Subject: [PATCH 2/2] must_cast_slice in bevy_pbr --- crates/bevy_pbr/Cargo.toml | 2 +- crates/bevy_pbr/src/meshlet/gpu_scene.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_pbr/Cargo.toml b/crates/bevy_pbr/Cargo.toml index d75be9f3c9d07..dc5b35a97b997 100644 --- a/crates/bevy_pbr/Cargo.toml +++ b/crates/bevy_pbr/Cargo.toml @@ -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"] } diff --git a/crates/bevy_pbr/src/meshlet/gpu_scene.rs b/crates/bevy_pbr/src/meshlet/gpu_scene.rs index 1ec7227c86aea..9944f853d3f85 100644 --- a/crates/bevy_pbr/src/meshlet/gpu_scene.rs +++ b/crates/bevy_pbr/src/meshlet/gpu_scene.rs @@ -187,7 +187,7 @@ fn upload_storage_buffer( 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);