From d47534ed9a46956cc8a192b878b7204fedfaafbd Mon Sep 17 00:00:00 2001 From: Nicolas Silva Date: Thu, 25 Jan 2024 09:06:29 +0100 Subject: [PATCH] Discard draws when the instance/vertex/index count is zero (#5137) --- wgpu-core/src/command/bundle.rs | 20 +++++++++++++------- wgpu-core/src/command/render.rs | 25 +++++++++++++++++-------- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index 40e652fef..aec42a493 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -514,9 +514,12 @@ impl RenderBundleEncoder { }) .map_pass_err(scope); } - commands.extend(state.flush_vertices()); - commands.extend(state.flush_binds(used_bind_groups, base.dynamic_offsets)); - commands.push(command); + + if instance_count > 0 && vertex_count > 0 { + commands.extend(state.flush_vertices()); + commands.extend(state.flush_binds(used_bind_groups, base.dynamic_offsets)); + commands.push(command); + } } RenderCommand::DrawIndexed { index_count, @@ -556,10 +559,13 @@ impl RenderBundleEncoder { }) .map_pass_err(scope); } - commands.extend(state.flush_index()); - commands.extend(state.flush_vertices()); - commands.extend(state.flush_binds(used_bind_groups, base.dynamic_offsets)); - commands.push(command); + + if instance_count > 0 && index_count > 0 { + commands.extend(state.flush_index()); + commands.extend(state.flush_vertices()); + commands.extend(state.flush_binds(used_bind_groups, base.dynamic_offsets)); + commands.push(command); + } } RenderCommand::MultiDrawIndirect { buffer_id, diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index bf5bf9ad5..f4933d898 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -1931,7 +1931,14 @@ impl Global { } unsafe { - raw.draw(first_vertex, vertex_count, first_instance, instance_count); + if instance_count > 0 && vertex_count > 0 { + raw.draw( + first_vertex, + vertex_count, + first_instance, + instance_count, + ); + } } } RenderCommand::DrawIndexed { @@ -1972,13 +1979,15 @@ impl Global { } unsafe { - raw.draw_indexed( - first_index, - index_count, - base_vertex, - first_instance, - instance_count, - ); + if instance_count > 0 && index_count > 0 { + raw.draw_indexed( + first_index, + index_count, + base_vertex, + first_instance, + instance_count, + ); + } } } RenderCommand::MultiDrawIndirect {