From 6cb18ed07255c4f11b64223338f5404027cdda58 Mon Sep 17 00:00:00 2001 From: teoxoy <28601907+teoxoy@users.noreply.github.com> Date: Tue, 25 Jun 2024 16:01:18 +0200 Subject: [PATCH] extract `set_vertex_buffer` from `render_pass_end_impl` --- wgpu-core/src/command/render.rs | 128 +++++++++++++++++--------------- 1 file changed, 67 insertions(+), 61 deletions(-) diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index 2bff56d5f..67627f3e6 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -1552,69 +1552,9 @@ impl Global { offset, size, } => { - api_log!( - "RenderPass::set_vertex_buffer {slot} {}", - buffer.error_ident() - ); - let scope = PassErrorScope::SetVertexBuffer(buffer.as_info().id()); - - state - .info - .usage_scope - .buffers - .merge_single(&buffer, hal::BufferUses::VERTEX) + set_vertex_buffer(&mut state, &cmd_buf, slot, buffer, offset, size) .map_pass_err(scope)?; - - buffer - .same_device_as(cmd_buf.as_ref()) - .map_pass_err(scope)?; - - let max_vertex_buffers = state.device.limits.max_vertex_buffers; - if slot >= max_vertex_buffers { - return Err(RenderCommandError::VertexBufferIndexOutOfRange { - index: slot, - max: max_vertex_buffers, - }) - .map_pass_err(scope); - } - - buffer - .check_usage(BufferUsages::VERTEX) - .map_pass_err(scope)?; - let buf_raw = buffer.try_raw(state.snatch_guard).map_pass_err(scope)?; - - let empty_slots = - (1 + slot as usize).saturating_sub(state.vertex.inputs.len()); - state - .vertex - .inputs - .extend(iter::repeat(VertexBufferState::EMPTY).take(empty_slots)); - let vertex_state = &mut state.vertex.inputs[slot as usize]; - //TODO: where are we checking that the offset is in bound? - vertex_state.total_size = match size { - Some(s) => s.get(), - None => buffer.size - offset, - }; - vertex_state.bound = true; - - state.buffer_memory_init_actions.extend( - buffer.initialization_status.read().create_action( - &buffer, - offset..(offset + vertex_state.total_size), - MemoryInitKind::NeedsInitializedMemory, - ), - ); - - let bb = hal::BufferBinding { - buffer: buf_raw, - offset, - size, - }; - unsafe { - state.raw_encoder.set_vertex_buffer(slot, bb); - } - state.vertex.update_limits(); } ArcRenderCommand::SetBlendConstant(ref color) => { api_log!("RenderPass::set_blend_constant"); @@ -2559,6 +2499,72 @@ fn set_index_buffer( Ok(()) } +fn set_vertex_buffer( + state: &mut State, + cmd_buf: &Arc>, + slot: u32, + buffer: Arc>, + offset: u64, + size: Option, +) -> Result<(), RenderPassErrorInner> { + api_log!( + "RenderPass::set_vertex_buffer {slot} {}", + buffer.error_ident() + ); + + state + .info + .usage_scope + .buffers + .merge_single(&buffer, hal::BufferUses::VERTEX)?; + + buffer.same_device_as(cmd_buf.as_ref())?; + + let max_vertex_buffers = state.device.limits.max_vertex_buffers; + if slot >= max_vertex_buffers { + return Err(RenderCommandError::VertexBufferIndexOutOfRange { + index: slot, + max: max_vertex_buffers, + } + .into()); + } + + buffer.check_usage(BufferUsages::VERTEX)?; + let buf_raw = buffer.try_raw(state.snatch_guard)?; + + let empty_slots = (1 + slot as usize).saturating_sub(state.vertex.inputs.len()); + state + .vertex + .inputs + .extend(iter::repeat(VertexBufferState::EMPTY).take(empty_slots)); + let vertex_state = &mut state.vertex.inputs[slot as usize]; + //TODO: where are we checking that the offset is in bound? + vertex_state.total_size = match size { + Some(s) => s.get(), + None => buffer.size - offset, + }; + vertex_state.bound = true; + + state + .buffer_memory_init_actions + .extend(buffer.initialization_status.read().create_action( + &buffer, + offset..(offset + vertex_state.total_size), + MemoryInitKind::NeedsInitializedMemory, + )); + + let bb = hal::BufferBinding { + buffer: buf_raw, + offset, + size, + }; + unsafe { + state.raw_encoder.set_vertex_buffer(slot, bb); + } + state.vertex.update_limits(); + Ok(()) +} + impl Global { pub fn render_pass_set_bind_group( &self,