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

Avoid unnecessary copying with BufferVec::swap #4902

Closed
wants to merge 3 commits into from
Closed
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
9 changes: 5 additions & 4 deletions crates/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ impl Default for SkinnedMeshUniform {
pub fn prepare_skinned_meshes(
render_device: Res<RenderDevice>,
render_queue: Res<RenderQueue>,
extracted_joints: Res<ExtractedJoints>,
mut extracted_joints: ResMut<ExtractedJoints>,
mut skinned_mesh_uniform: ResMut<SkinnedMeshUniform>,
) {
if extracted_joints.buffer.is_empty() {
Expand All @@ -752,9 +752,10 @@ pub fn prepare_skinned_meshes(
skinned_mesh_uniform
.buffer
.reserve(extracted_joints.buffer.len(), &render_device);
for joint in &extracted_joints.buffer {
skinned_mesh_uniform.buffer.push(*joint);
}
skinned_mesh_uniform
.buffer
.swap(&mut extracted_joints.buffer);
extracted_joints.buffer.clear();
skinned_mesh_uniform
.buffer
.write_buffer(&render_device, &render_queue);
Expand Down
9 changes: 9 additions & 0 deletions crates/bevy_render/src/render_resource/buffer_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use crate::{
renderer::{RenderDevice, RenderQueue},
};
use bevy_core::{cast_slice, Pod};
#[cfg(feature = "trace")]
use bevy_utils::tracing::info_span;
use copyless::VecHelper;
use wgpu::BufferUsages;

Expand Down Expand Up @@ -51,6 +53,11 @@ impl<T: Pod> BufferVec<T> {
index
}

/// Swaps the internal [`Vec`] with another of the same type.
pub fn swap(&mut self, other: &mut Vec<T>) {
std::mem::swap(&mut self.values, other);
}

pub fn reserve(&mut self, capacity: usize, device: &RenderDevice) {
if capacity > self.capacity {
self.capacity = capacity;
Expand All @@ -72,6 +79,8 @@ impl<T: Pod> BufferVec<T> {
if let Some(buffer) = &self.buffer {
let range = 0..self.item_size * self.values.len();
let bytes: &[u8] = cast_slice(&self.values);
#[cfg(feature = "trace")]
let _span = info_span!("BufferVec: write buffer").entered();
queue.write_buffer(buffer, 0, &bytes[range]);
}
}
Expand Down
6 changes: 6 additions & 0 deletions crates/bevy_render/src/render_resource/storage_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::Buffer;
use crate::renderer::{RenderDevice, RenderQueue};
#[cfg(feature = "trace")]
use bevy_utils::tracing::info_span;
use encase::{
internal::WriteInto, DynamicStorageBuffer as DynamicStorageBufferWrapper, ShaderType,
StorageBuffer as StorageBufferWrapper,
Expand Down Expand Up @@ -73,6 +75,8 @@ impl<T: ShaderType + WriteInto> StorageBuffer<T> {
}));
self.capacity = size;
} else if let Some(buffer) = &self.buffer {
#[cfg(feature = "trace")]
let _span = info_span!("StorageBuffer: write buffer").entered();
queue.write_buffer(buffer, 0, self.scratch.as_ref());
}
}
Expand Down Expand Up @@ -140,6 +144,8 @@ impl<T: ShaderType + WriteInto> DynamicStorageBuffer<T> {
}));
self.capacity = size;
} else if let Some(buffer) = &self.buffer {
#[cfg(feature = "trace")]
let _span = info_span!("DynamicStorageBuffer: write buffer").entered();
queue.write_buffer(buffer, 0, self.scratch.as_ref());
}
}
Expand Down
10 changes: 9 additions & 1 deletion crates/bevy_render/src/render_resource/uniform_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::{
render_resource::Buffer,
renderer::{RenderDevice, RenderQueue},
};
#[cfg(feature = "trace")]
use bevy_utils::tracing::info_span;
use encase::{
internal::WriteInto, DynamicUniformBuffer as DynamicUniformBufferWrapper, ShaderType,
UniformBuffer as UniformBufferWrapper,
Expand Down Expand Up @@ -63,7 +65,11 @@ impl<T: ShaderType + WriteInto> UniformBuffer<T> {
self.scratch.write(&self.value).unwrap();

match &self.buffer {
Some(buffer) => queue.write_buffer(buffer, 0, self.scratch.as_ref()),
Some(buffer) => {
#[cfg(feature = "trace")]
let _span = info_span!("UniformBuffer: write buffer").entered();
queue.write_buffer(buffer, 0, self.scratch.as_ref());
}
None => {
self.buffer = Some(device.create_buffer_with_data(&BufferInitDescriptor {
label: None,
Expand Down Expand Up @@ -137,6 +143,8 @@ impl<T: ShaderType + WriteInto> DynamicUniformBuffer<T> {
}));
self.capacity = size;
} else if let Some(buffer) = &self.buffer {
#[cfg(feature = "trace")]
let _span = info_span!("DynamicUniformBuffer: write buffer").entered();
queue.write_buffer(buffer, 0, self.scratch.as_ref());
}
}
Expand Down