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 allocating during queue submit #4947

Merged
merged 2 commits into from
Jan 3, 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
6 changes: 2 additions & 4 deletions wgpu/src/backend/direct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3040,13 +3040,11 @@ impl crate::Context for Context {
wgpu_render_pass_end_pipeline_statistics_query(pass_data)
}

fn render_pass_execute_bundles<'a>(
fn render_pass_execute_bundles(
&self,
_pass: &mut Self::RenderPassId,
pass_data: &mut Self::RenderPassData,
render_bundles: Box<
dyn Iterator<Item = (Self::RenderBundleId, &'a Self::RenderBundleData)> + 'a,
>,
render_bundles: &mut dyn Iterator<Item = (Self::RenderBundleId, &Self::RenderBundleData)>,
) {
let temp_render_bundles = render_bundles.map(|(i, _)| i).collect::<SmallVec<[_; 4]>>();
unsafe {
Expand Down
6 changes: 2 additions & 4 deletions wgpu/src/backend/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3451,13 +3451,11 @@ impl crate::context::Context for Context {
// Not available in gecko yet
}

fn render_pass_execute_bundles<'a>(
fn render_pass_execute_bundles(
&self,
_pass: &mut Self::RenderPassId,
pass_data: &mut Self::RenderPassData,
render_bundles: Box<
dyn Iterator<Item = (Self::RenderBundleId, &'a Self::RenderBundleData)> + 'a,
>,
render_bundles: &mut dyn Iterator<Item = (Self::RenderBundleId, &Self::RenderBundleData)>,
) {
let mapped = render_bundles
.map(|(_, bundle_data)| &bundle_data.0)
Expand Down
30 changes: 14 additions & 16 deletions wgpu/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1018,13 +1018,11 @@ pub trait Context: Debug + WasmNotSendSync + Sized {
pass: &mut Self::RenderPassId,
pass_data: &mut Self::RenderPassData,
);
fn render_pass_execute_bundles<'a>(
fn render_pass_execute_bundles(
&self,
pass: &mut Self::RenderPassId,
pass_data: &mut Self::RenderPassData,
render_bundles: Box<
dyn Iterator<Item = (Self::RenderBundleId, &'a Self::RenderBundleData)> + 'a,
>,
render_bundles: &mut dyn Iterator<Item = (Self::RenderBundleId, &Self::RenderBundleData)>,
);
}

Expand Down Expand Up @@ -1660,11 +1658,11 @@ pub(crate) trait DynContext: Debug + WasmNotSendSync {
dest: crate::ImageCopyTextureTagged<'_>,
size: wgt::Extent3d,
);
fn queue_submit<'a>(
fn queue_submit(
&self,
queue: &ObjectId,
queue_data: &crate::Data,
command_buffers: Box<dyn Iterator<Item = (ObjectId, Box<crate::Data>)> + 'a>,
command_buffers: &mut dyn Iterator<Item = (ObjectId, Box<crate::Data>)>,
) -> (ObjectId, Arc<crate::Data>);
fn queue_get_timestamp_period(&self, queue: &ObjectId, queue_data: &crate::Data) -> f32;
fn queue_on_submitted_work_done(
Expand Down Expand Up @@ -2068,11 +2066,11 @@ pub(crate) trait DynContext: Debug + WasmNotSendSync {
pass: &mut ObjectId,
pass_data: &mut crate::Data,
);
fn render_pass_execute_bundles<'a>(
fn render_pass_execute_bundles(
&self,
pass: &mut ObjectId,
pass_data: &mut crate::Data,
render_bundles: Box<dyn Iterator<Item = (&'a ObjectId, &'a crate::Data)> + 'a>,
render_bundles: &mut dyn Iterator<Item = (&ObjectId, &crate::Data)>,
);
}

Expand Down Expand Up @@ -3112,15 +3110,15 @@ where
Context::queue_copy_external_image_to_texture(self, &queue, queue_data, source, dest, size)
}

fn queue_submit<'a>(
fn queue_submit(
&self,
queue: &ObjectId,
queue_data: &crate::Data,
command_buffers: Box<dyn Iterator<Item = (ObjectId, Box<crate::Data>)> + 'a>,
command_buffers: &mut dyn Iterator<Item = (ObjectId, Box<crate::Data>)>,
) -> (ObjectId, Arc<crate::Data>) {
let queue = <T::QueueId>::from(*queue);
let queue_data = downcast_ref(queue_data);
let command_buffers = command_buffers.into_iter().map(|(id, data)| {
let command_buffers = command_buffers.map(|(id, data)| {
let command_buffer_data: <T as Context>::CommandBufferData = *data.downcast().unwrap();
(<T::CommandBufferId>::from(id), command_buffer_data)
});
Expand Down Expand Up @@ -4073,19 +4071,19 @@ where
Context::render_pass_end_pipeline_statistics_query(self, &mut pass, pass_data)
}

fn render_pass_execute_bundles<'a>(
fn render_pass_execute_bundles(
&self,
pass: &mut ObjectId,
pass_data: &mut crate::Data,
render_bundles: Box<dyn Iterator<Item = (&'a ObjectId, &'a crate::Data)> + 'a>,
render_bundles: &mut dyn Iterator<Item = (&ObjectId, &crate::Data)>,
) {
let mut pass = <T::RenderPassId>::from(*pass);
let pass_data = downcast_mut::<T::RenderPassData>(pass_data);
let render_bundles = Box::new(render_bundles.into_iter().map(|(id, data)| {
let mut render_bundles = render_bundles.map(|(id, data)| {
let render_bundle_data: &<T as Context>::RenderBundleData = downcast_ref(data);
(<T::RenderBundleId>::from(*id), render_bundle_data)
}));
Context::render_pass_execute_bundles(self, &mut pass, pass_data, render_bundles)
});
Context::render_pass_execute_bundles(self, &mut pass, pass_data, &mut render_bundles)
}
}

Expand Down
25 changes: 11 additions & 14 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4112,19 +4112,16 @@ impl<'a> RenderPass<'a> {
///
/// Commands in the bundle do not inherit this render pass's current render state, and after the
/// bundle has executed, the state is **cleared** (reset to defaults, not the previous state).
pub fn execute_bundles<I: IntoIterator<Item = &'a RenderBundle> + 'a>(
&mut self,
render_bundles: I,
) {
pub fn execute_bundles<I: IntoIterator<Item = &'a RenderBundle>>(&mut self, render_bundles: I) {
let mut render_bundles = render_bundles
.into_iter()
.map(|rb| (&rb.id, rb.data.as_ref()));

DynContext::render_pass_execute_bundles(
&*self.parent.context,
&mut self.id,
self.data.as_mut(),
Box::new(
render_bundles
.into_iter()
.map(|rb| (&rb.id, rb.data.as_ref())),
),
&mut render_bundles,
)
}
}
Expand Down Expand Up @@ -4998,15 +4995,15 @@ impl Queue {
&self,
command_buffers: I,
) -> SubmissionIndex {
let mut command_buffers = command_buffers
.into_iter()
.map(|mut comb| (comb.id.take().unwrap(), comb.data.take().unwrap()));

let (raw, data) = DynContext::queue_submit(
&*self.context,
&self.id,
self.data.as_ref(),
Box::new(
command_buffers
.into_iter()
.map(|mut comb| (comb.id.take().unwrap(), comb.data.take().unwrap())),
),
&mut command_buffers,
);

SubmissionIndex(raw, data)
Expand Down
Loading