From 2b92ae160d4a3fee55977f2a5bbf62ef26858216 Mon Sep 17 00:00:00 2001 From: Luo Zhihao Date: Sat, 11 Jul 2026 06:52:20 +0800 Subject: [PATCH] Defer `set_immediates` to draw/dispatch call time (#9597) --- CHANGELOG.md | 4 + cts_runner/fail.lst | 2 - cts_runner/revision.txt | 2 +- cts_runner/skip.lst | 4 +- cts_runner/test.lst | 44 ++----- player/src/lib.rs | 24 +--- wgpu-core/src/binding_model.rs | 45 +------ wgpu-core/src/command/bundle.rs | 142 ++++++++------------ wgpu-core/src/command/compute.rs | 126 +++++++----------- wgpu-core/src/command/compute_command.rs | 11 +- wgpu-core/src/command/mod.rs | 33 +---- wgpu-core/src/command/pass.rs | 158 ++++++++++------------- wgpu-core/src/command/render.rs | 90 +++++++------ wgpu-core/src/command/render_command.rs | 20 +-- wgpu-core/src/device/trace/record.rs | 21 +-- wgpu/src/backend/wgpu_core.rs | 24 +++- 16 files changed, 261 insertions(+), 489 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ffb153210..acf614cf7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,6 +54,10 @@ Bottom level categories: ### Changes +#### General + +- Allow `set_immediates` before `set_pipeline` by deferring actual setting of immediates to draw/dispatch call time. By @beicause in [#9597](https://github.com/gfx-rs/wgpu/pull/9597). + #### naga - `naga::valid::ValidationError` is now always returned boxed, to avoid `clippy::large_result_err` warning. By @beicause in [#9612](https://github.com/gfx-rs/wgpu/pull/9612) diff --git a/cts_runner/fail.lst b/cts_runner/fail.lst index 67438ba22..8b78ea66a 100644 --- a/cts_runner/fail.lst +++ b/cts_runner/fail.lst @@ -39,9 +39,7 @@ webgpu:api,validation,encoding,cmds,setBindGroup:state_and_binding_index:encoder webgpu:api,validation,encoding,cmds,setBindGroup:state_and_binding_index:encoderType="render%20bundle";state="destroyed";* // https://github.com/gfx-rs/wgpu/issues/7881 webgpu:api,validation,encoding,cmds,setBindGroup:state_and_binding_index:encoderType="render%20pass";state="destroyed";* // https://github.com/gfx-rs/wgpu/issues/7881 webgpu:api,validation,encoding,cmds,setBindGroup:u32array_start_and_length:* // deno unwrap -webgpu:api,validation,encoding,cmds,setImmediates:* // 62%, https://github.com/gfx-rs/wgpu/issues/8556 webgpu:api,validation,encoding,encoder_open_state:* // https://github.com/gfx-rs/wgpu/issues/7857 -webgpu:api,validation,encoding,programmable,pipeline_immediate:* // 27%, https://github.com/gfx-rs/wgpu/issues/8556 webgpu:api,validation,encoding,queries,resolveQuerySet:* // 93%, https://github.com/gfx-rs/wgpu/issues/7881 webgpu:api,validation,encoding,render_bundle:* // 81%, readonly flag normalization mismatch webgpu:api,validation,image_copy,buffer_texture_copies:* // https://github.com/gfx-rs/wgpu/issues/7946 diff --git a/cts_runner/revision.txt b/cts_runner/revision.txt index c8c74e765..8689e781b 100644 --- a/cts_runner/revision.txt +++ b/cts_runner/revision.txt @@ -1 +1 @@ -3fdd32363410f2bd4e3e2f046980967e6abc27e6 +2ed28f0047be2800a8da9a701fc492477c3f7cee diff --git a/cts_runner/skip.lst b/cts_runner/skip.lst index 3c1b21a45..a02bda445 100644 --- a/cts_runner/skip.lst +++ b/cts_runner/skip.lst @@ -3,13 +3,11 @@ // The `cts_runner` integration test `lst_files_are_sorted` verifies that test selectors // appear in this file in order -- including ones in comments. -webgpu:api,validation,createPipelineLayout:immediate_data_size:* // immediates, https://github.com/gfx-rs/wgpu/issues/8556 webgpu:api,validation,dispatch:* // linear indexing, https://github.com/gfx-rs/wgpu/issues/9296 -webgpu:api,validation,encoding,programmable,pipeline_immediate:* // immediates, https://github.com/gfx-rs/wgpu/issues/8556 webgpu:api,validation,gpu_external_texture_expiration:* // external texture -webgpu:api,validation,pipeline,immediates:pipeline_creation_immediate_size_mismatch:* // immediates, https://github.com/gfx-rs/wgpu/issues/8556 webgpu:api,validation,queue,copyToTexture,CopyExternalImageToTexture:* // external texture +webgpu:shader,validation,decl,immediate:* // WGSL language extension 'immediate_address_space' isn't added, https://github.com/gfx-rs/wgpu/issues/8556 webgpu:shader,validation,expression,call,builtin,quadBroadcast:* // subgroups, https://github.com/gfx-rs/wgpu/issues/8722 webgpu:shader,validation,expression,call,builtin,quadSwap:* // ibid. webgpu:shader,validation,expression,call,builtin,subgroupAdd:* // ibid. diff --git a/cts_runner/test.lst b/cts_runner/test.lst index d41474432..21c3537d2 100644 --- a/cts_runner/test.lst +++ b/cts_runner/test.lst @@ -190,43 +190,19 @@ webgpu:api,validation,encoding,cmds,setBindGroup:state_and_binding_index:encoder webgpu:api,validation,encoding,cmds,setBindGroup:state_and_binding_index:encoderType="render%20bundle";state="valid";* webgpu:api,validation,encoding,cmds,setBindGroup:state_and_binding_index:encoderType="render%20pass";state="invalid";* webgpu:api,validation,encoding,cmds,setBindGroup:state_and_binding_index:encoderType="render%20pass";state="valid";* +webgpu:api,validation,encoding,cmds,setImmediates:* webgpu:api,validation,encoding,createRenderBundleEncoder:* -webgpu:api,validation,encoding,encoder_open_state:compute_pass_commands:command="dispatchWorkgroups" -webgpu:api,validation,encoding,encoder_open_state:compute_pass_commands:command="dispatchWorkgroupsIndirect" -webgpu:api,validation,encoding,encoder_open_state:compute_pass_commands:command="insertDebugMarker" -webgpu:api,validation,encoding,encoder_open_state:compute_pass_commands:command="popDebugGroup" -webgpu:api,validation,encoding,encoder_open_state:compute_pass_commands:command="pushDebugGroup" -webgpu:api,validation,encoding,encoder_open_state:compute_pass_commands:command="setBindGroup" -//FAIL: webgpu:api,validation,encoding,encoder_open_state:compute_pass_commands:command="setImmediates" -// https://github.com/gfx-rs/wgpu/issues/8556 -webgpu:api,validation,encoding,encoder_open_state:compute_pass_commands:command="setPipeline" -webgpu:api,validation,encoding,encoder_open_state:non_pass_commands:* -//FAIL: webgpu:api,validation,encoding,encoder_open_state:render_bundle_commands:* -// https://github.com/gfx-rs/wgpu/issues/7857 -webgpu:api,validation,encoding,encoder_open_state:render_pass_commands:command="beginOcclusionQuery" -webgpu:api,validation,encoding,encoder_open_state:render_pass_commands:command="draw" -webgpu:api,validation,encoding,encoder_open_state:render_pass_commands:command="drawIndexed" -webgpu:api,validation,encoding,encoder_open_state:render_pass_commands:command="drawIndexedIndirect" -webgpu:api,validation,encoding,encoder_open_state:render_pass_commands:command="drawIndirect" -webgpu:api,validation,encoding,encoder_open_state:render_pass_commands:command="endOcclusionQuery" -webgpu:api,validation,encoding,encoder_open_state:render_pass_commands:command="executeBundles" -webgpu:api,validation,encoding,encoder_open_state:render_pass_commands:command="insertDebugMarker" -webgpu:api,validation,encoding,encoder_open_state:render_pass_commands:command="multiDrawIndexedIndirect" -webgpu:api,validation,encoding,encoder_open_state:render_pass_commands:command="multiDrawIndirect" -webgpu:api,validation,encoding,encoder_open_state:render_pass_commands:command="popDebugGroup" -webgpu:api,validation,encoding,encoder_open_state:render_pass_commands:command="pushDebugGroup" -webgpu:api,validation,encoding,encoder_open_state:render_pass_commands:command="setBindGroup" -webgpu:api,validation,encoding,encoder_open_state:render_pass_commands:command="setBlendConstant" -//FAIL: webgpu:api,validation,encoding,encoder_open_state:render_pass_commands:command="setImmediates" -// https://github.com/gfx-rs/wgpu/issues/8556 -webgpu:api,validation,encoding,encoder_open_state:render_pass_commands:command="setIndexBuffer" -webgpu:api,validation,encoding,encoder_open_state:render_pass_commands:command="setPipeline" -webgpu:api,validation,encoding,encoder_open_state:render_pass_commands:command="setScissorRect" -webgpu:api,validation,encoding,encoder_open_state:render_pass_commands:command="setStencilReference" -webgpu:api,validation,encoding,encoder_open_state:render_pass_commands:command="setVertexBuffer" -webgpu:api,validation,encoding,encoder_open_state:render_pass_commands:command="setViewport" +webgpu:api,validation,encoding,encoder_open_state:* webgpu:api,validation,encoding,encoder_state:* webgpu:api,validation,encoding,programmable,pipeline_bind_group_compat:* +// On dx12, Error: Unexpected internal error occurred: undefined +// https://github.com/gfx-rs/wgpu/issues/8556 +fails-if(dx12) webgpu:api,validation,encoding,programmable,pipeline_immediate:overprovisioned_immediate_data:* +fails-if(dx12) webgpu:api,validation,encoding,programmable,pipeline_immediate:render_bundle_execution_state_invalidation:* +//FAIL: webgpu:api,validation,encoding,programmable,pipeline_immediate:required_slots_set:* +// Naga `immediate_slots_required` is not quite correct. +// https://github.com/gfx-rs/wgpu/issues/9724 +fails-if(dx12) webgpu:api,validation,encoding,programmable,pipeline_immediate:unused_variable:* webgpu:api,validation,encoding,queries,begin_end:nesting:* webgpu:api,validation,encoding,queries,begin_end:occlusion_query,* webgpu:api,validation,encoding,queries,general:occlusion_query,* diff --git a/player/src/lib.rs b/player/src/lib.rs index 75676064a..a4dacb3e1 100644 --- a/player/src/lib.rs +++ b/player/src/lib.rs @@ -948,7 +948,6 @@ impl Player { error, commands, dynamic_offsets, - immediates_data, string_data, } = pass; @@ -960,7 +959,6 @@ impl Player { .map(|cmd| self.resolve_compute_command(cmd)) .collect(), dynamic_offsets, - immediates_data, string_data, } } @@ -974,7 +972,6 @@ impl Player { error, commands, dynamic_offsets, - immediates_data, string_data, } = pass; @@ -986,7 +983,6 @@ impl Player { .map(|cmd| self.resolve_render_command(cmd)) .collect(), dynamic_offsets, - immediates_data, string_data, } } @@ -1007,15 +1003,7 @@ impl Player { bind_group: bind_group.map(|bg| self.resolve_bind_group_id(bg)), }, C::SetPipeline(id) => C::SetPipeline(self.resolve_compute_pipeline_id(id)), - C::SetImmediate { - offset, - size_bytes, - values_offset, - } => C::SetImmediate { - offset, - size_bytes, - values_offset, - }, + C::SetImmediate { offset, data } => C::SetImmediate { offset, data }, C::DispatchWorkgroups(groups) => C::DispatchWorkgroups(groups), C::DispatchWorkgroupsIndirect { buffer, offset } => C::DispatchWorkgroupsIndirect { buffer: self.resolve_buffer_id(buffer), @@ -1112,15 +1100,7 @@ impl Player { depth_max, }, C::SetScissor(rect) => C::SetScissor(rect), - C::SetImmediate { - offset, - size_bytes, - values_offset, - } => C::SetImmediate { - offset, - size_bytes, - values_offset, - }, + C::SetImmediate { offset, data } => C::SetImmediate { offset, data }, C::Draw { vertex_count, instance_count, diff --git a/wgpu-core/src/binding_model.rs b/wgpu-core/src/binding_model.rs index 886b03683..79bbda20e 100644 --- a/wgpu-core/src/binding_model.rs +++ b/wgpu-core/src/binding_model.rs @@ -955,8 +955,6 @@ impl WebGpuError for CreatePipelineLayoutError { #[derive(Clone, Debug, Error)] #[non_exhaustive] pub enum ImmediateUploadError { - #[error("Ran out of immediate data space. Don't set 4gb of immediates per pass")] - ImmediateOutOfMemory, #[error( "Provided immediate data start offset {start_offset} overruns the range with a size of {immediate_size}" )] @@ -975,18 +973,17 @@ pub enum ImmediateUploadError { `IMMEDIATE_DATA_ALIGNMENT` ({ida})", ida = wgt::IMMEDIATE_DATA_ALIGNMENT )] - SizeUnaligned(u32), + SizeUnaligned(usize), #[error( - "Provided immediate data start offset {} + size {} overruns the immediate data range \ - with a size of {}", + "Provided immediate data start offset {} + size {} overruns `max_immediate_size` {}", start_offset, - size, - immediate_size + size_bytes, + limit )] - EndOffsetOverrun { + EndOffsetBeyondLimit { start_offset: u32, - size: u32, - immediate_size: u32, + size_bytes: usize, + limit: u32, }, } @@ -1113,34 +1110,6 @@ impl PipelineLayout { bgl.entries.get(binding) } - /// Validate immediates match up with expected ranges. - pub(crate) fn validate_immediates_ranges( - &self, - offset: u32, - size_bytes: u32, - ) -> Result<(), ImmediateUploadError> { - // Don't need to validate size against the immediate data size limit here, - // as immediate data ranges are already validated to be within bounds, - // and we validate that they are within the ranges. - - if offset > self.immediate_size { - return Err(ImmediateUploadError::StartOffsetOverrun { - start_offset: offset, - immediate_size: self.immediate_size, - }); - } - - if size_bytes > self.immediate_size - offset { - return Err(ImmediateUploadError::EndOffsetOverrun { - start_offset: offset, - size: size_bytes, - immediate_size: self.immediate_size, - }); - } - - Ok(()) - } - pub(crate) fn invalid(device: Arc, label: String) -> Arc { Arc::new(Self { raw: ResourceState::Invalid, diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index 67a1d81d4..db6bf4e84 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -29,11 +29,11 @@ Render passes are also isolated from the effects of bundles. After executing a render bundle, a render pass's pipeline, bind groups, and vertex and index buffers are are unset, so the bundle cannot affect later draw calls in the pass. -A render pass is not fully isolated from a bundle's effects on immediate data -values. Draw calls following a bundle's execution will see whatever values the -bundle writes to immediate data storage. Setting a pipeline initializes any push -constant storage it could access to zero, and this initialization may also be -visible after bundle execution. +A render pass is isolated from a bundle's effects on immediate data +values. When encoding a render bundle, calls to `set_immediates` snapshot the immediate data +content at encoding time, and the immediate values cannot be changed after `finish`. +Before and after executing each individual bundle, all required immediate slots are cleared/reset, +therefore immediate data must be set again. ## Render Bundle Lifecycle @@ -103,11 +103,13 @@ use wgt::error::{ErrorType, WebGpuError}; use crate::command::ArcReferences; use crate::{ api_log, - binding_model::{BindError, BindGroup, PipelineLayout}, + binding_model::{BindError, BindGroup, ImmediateUploadError, PipelineLayout}, command::{ - bind::Binder, pass::validate_immediates_alignment, pass_base, BasePass, - BindGroupStateChange, ColorAttachmentError, DrawError, EncoderStateError, IdReferences, - MapPassErr, PassErrorScope, PassStateError, RenderCommand, RenderCommandError, StateChange, + bind::Binder, + pass::{validate_immediates_alignment, ImmediateState}, + pass_base, BasePass, BindGroupStateChange, ColorAttachmentError, DrawError, + EncoderStateError, IdReferences, MapPassErr, PassErrorScope, PassStateError, RenderCommand, + RenderCommandError, StateChange, }, device::{ AttachmentData, Device, DeviceError, MissingDownlevelFlags, MissingFeatures, @@ -165,7 +167,7 @@ pub struct RenderBundleEncoderDescriptor<'a> { #[derive(Debug)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub struct RenderBundleEncoder { - base: BasePass, Infallible>, + pub(crate) base: BasePass, Infallible>, parent_id: id::DeviceId, /// State of the render bundle encoder. Encoded to be compatible with pass macros. /// @@ -308,6 +310,10 @@ impl RenderBundleEncoder { self.parent_id } + pub fn label(&self) -> Option<&str> { + self.base.label.as_deref() + } + /// Convert this encoder's commands into a [`RenderBundle`]. /// /// We want executing a [`RenderBundle`] to be quick, so we take @@ -417,7 +423,7 @@ impl RenderBundleEncoder { texture_memory_init_actions: Vec::new(), next_dynamic_offset: 0, binder: Binder::new(), - immediate_slots_set: Default::default(), + immediate_state: ImmediateState::default(), }; let indices = &state.device.tracker_indices; @@ -483,14 +489,9 @@ impl RenderBundleEncoder { set_vertex_buffer(&mut state, &buffer_guard, slot, buffer, offset, size) .map_pass_err(scope)?; } - &RenderCommand::SetImmediate { - offset, - size_bytes, - values_offset, - } => { + &RenderCommand::SetImmediate { offset, ref data } => { let scope = PassErrorScope::SetImmediate; - set_immediates(&mut state, offset, size_bytes, values_offset) - .map_pass_err(scope)?; + set_immediates(&mut state, offset, data).map_pass_err(scope)?; } &RenderCommand::Draw { vertex_count, @@ -605,7 +606,6 @@ impl RenderBundleEncoder { .contains(wgt::InstanceFlags::DISCARD_HAL_LABELS); let string_data = mem::take(&mut self.base.string_data); - let immediates_data = mem::take(&mut self.base.immediates_data); let context = mem::take(&mut self.context); let render_bundle = RenderBundle { state: ResourceState::Valid(RenderBundleState { @@ -618,7 +618,6 @@ impl RenderBundleEncoder { commands, dynamic_offsets: flat_dynamic_offsets, string_data, - immediates_data, }, is_depth_read_only: self.is_depth_read_only, is_stencil_read_only: self.is_stencil_read_only, @@ -712,18 +711,16 @@ impl RenderBundleEncoder { pub fn set_immediates(&mut self, offset: u32, data: &[u8]) -> Result<(), PassStateError> { pass_base!(self, PassErrorScope::SetImmediate); - let value_offset = self.base.immediates_data.len().try_into().expect( - "Ran out of immediate data space. Don't set 4gb of immediates per RenderBundle.", - ); - self.base.immediates_data.extend( - data.chunks_exact(wgt::IMMEDIATE_DATA_ALIGNMENT as usize) - .map(|arr| u32::from_ne_bytes([arr[0], arr[1], arr[2], arr[3]])), - ); + + // This should have been validated in content timeline + assert!(data.len().is_multiple_of(4)); self.base.commands.push(RenderCommand::SetImmediate { offset, - size_bytes: data.len() as u32, - values_offset: Some(value_offset), + data: data + .chunks_exact(size_of::()) + .map(|ck| u32::from_le_bytes(ck.try_into().unwrap())) + .collect(), }); Ok(()) } @@ -1052,26 +1049,13 @@ fn set_vertex_buffer( fn set_immediates( state: &mut State, offset: u32, - size_bytes: u32, - values_offset: Option, -) -> Result<(), RenderBundleErrorInner> { - validate_immediates_alignment(offset, size_bytes)?; + data: &[u32], +) -> Result<(), ImmediateUploadError> { + validate_immediates_alignment(offset, size_of_val(data))?; - let pipeline = state - .pipeline - .as_deref() - .ok_or(DrawError::MissingPipeline(pass::MissingPipeline))?; - - pipeline - .layout()? - .validate_immediates_ranges(offset, size_bytes)?; - - state.commands.push(ArcRenderCommand::SetImmediate { - offset, - size_bytes, - values_offset, - }); - state.immediate_slots_set |= naga::valid::ImmediateSlots::from_range(offset, size_bytes); + state + .immediate_state + .set_immediates::(&state.device.limits, offset, data)?; Ok(()) } @@ -1096,6 +1080,7 @@ fn draw( if instance_count > 0 && vertex_count > 0 { state.flush_vertex_buffers(); state.flush_bindings(); + state.flush_immediates(); state.commands.push(ArcRenderCommand::Draw { vertex_count, instance_count, @@ -1136,6 +1121,7 @@ fn draw_indexed( state.flush_index(); state.flush_vertex_buffers(); state.flush_bindings(); + state.flush_immediates(); state.commands.push(ArcRenderCommand::DrawIndexed { index_count, instance_count, @@ -1181,6 +1167,7 @@ fn draw_mesh_tasks( if total_count > 0 { state.flush_bindings(); + state.flush_immediates(); state.commands.push(ArcRenderCommand::DrawMeshTasks { group_count_x, group_count_y, @@ -1242,6 +1229,7 @@ fn multi_draw_indirect( state.flush_vertex_buffers(); state.flush_bindings(); + state.flush_immediates(); state.commands.push(ArcRenderCommand::DrawIndirect { buffer, offset, @@ -1381,7 +1369,6 @@ impl RenderBundle { commands: Vec::new(), dynamic_offsets: Vec::new(), string_data: Vec::new(), - immediates_data: Vec::new(), }, is_depth_read_only: false, is_stencil_read_only: false, @@ -1486,45 +1473,11 @@ impl RenderBundle { let bb = hal::BufferBinding::new_unchecked(buffer, *offset, *size); unsafe { raw.set_vertex_buffer(*slot, bb) }; } - Cmd::SetImmediate { - offset, - size_bytes, - values_offset, - } => { + Cmd::SetImmediate { offset, data } => { let pipeline_layout = pipeline_layout.as_ref().unwrap(); - if let Some(values_offset) = *values_offset { - let values_end_offset = - (values_offset + size_bytes / wgt::IMMEDIATE_DATA_ALIGNMENT) as usize; - let data_slice = - &self.base.immediates_data[(values_offset as usize)..values_end_offset]; - - unsafe { - raw.set_immediates( - pipeline_layout - .raw() - .expect("PipelineLayout should be valid at this point"), - *offset, - data_slice, - ) - } - } else { - super::immediates_clear( - *offset, - *size_bytes, - |clear_offset, clear_data| { - unsafe { - raw.set_immediates( - pipeline_layout - .raw() - .expect("PipelineLayout should be valid at this point"), - clear_offset, - clear_data, - ) - }; - }, - ); - } + // SAFETY: The range of immediates written was validated in `is_ready` before each `flush_immediates`. + unsafe { raw.set_immediates(pipeline_layout.raw().unwrap(), *offset, data) } } Cmd::Draw { vertex_count, @@ -1743,9 +1696,7 @@ struct State { texture_memory_init_actions: Vec, next_dynamic_offset: usize, binder: Binder, - /// A bitmask, tracking which 4-byte slots have been written via `set_immediates`. - /// Checked against the pipeline's required slots before each draw call. - immediate_slots_set: naga::valid::ImmediateSlots, + immediate_state: ImmediateState, } impl State { @@ -1775,6 +1726,16 @@ impl State { }); } + fn flush_immediates(&mut self) { + if !self.immediate_state.immediates.is_empty() && self.immediate_state.immediates_dirty { + self.commands.push(ArcRenderCommand::SetImmediate { + offset: 0, + data: self.immediate_state.immediates.clone(), + }); + self.immediate_state.immediates_dirty = false; + } + } + /// Generate a `SetIndexBuffer` command to prepare for an indexed draw /// command, if needed. fn flush_index(&mut self) { @@ -1822,13 +1783,14 @@ impl State { } if !self + .immediate_state .immediate_slots_set .contains(pipeline.immediate_slots_required) { return Err(DrawError::MissingImmediateData { missing: pipeline .immediate_slots_required - .difference(self.immediate_slots_set), + .difference(self.immediate_state.immediate_slots_set), }); } diff --git a/wgpu-core/src/command/compute.rs b/wgpu-core/src/command/compute.rs index 56aed663d..f4c7d595e 100644 --- a/wgpu-core/src/command/compute.rs +++ b/wgpu-core/src/command/compute.rs @@ -16,7 +16,7 @@ use crate::{ compute_command::ArcComputeCommand, encoder::EncodingState, memory_init::{fixup_discarded_surfaces, SurfacesInDiscardState}, - pass::{self, flush_bindings_helper}, + pass::{self, flush_bindings_helper, ImmediateState}, pass_base, pass_try, query::{ end_pipeline_statistics_query, record_pass_timestamp_writes, @@ -200,9 +200,6 @@ pub enum ComputePassErrorInner { InvalidResource(#[from] InvalidResourceError), #[error(transparent)] TimestampWrites(#[from] TimestampWritesError), - // This one is unreachable, but required for generic pass support - #[error(transparent)] - InvalidValuesOffset(#[from] pass::InvalidValuesOffset), } impl From for ComputePassErrorInner { @@ -261,7 +258,6 @@ impl WebGpuError for ComputePassError { ComputePassErrorInner::MissingDownlevelFlags(e) => e.webgpu_error_type(), ComputePassErrorInner::InvalidResource(e) => e.webgpu_error_type(), ComputePassErrorInner::TimestampWrites(e) => e.webgpu_error_type(), - ComputePassErrorInner::InvalidValuesOffset(e) => e.webgpu_error_type(), ComputePassErrorInner::InvalidParentEncoder | ComputePassErrorInner::BindGroupIndexOutOfRange { .. } @@ -279,12 +275,6 @@ struct State<'scope, 'snatch_guard, 'cmd_enc> { active_query: Option<(Arc, u32)>, - immediates: Vec, - - /// A bitmask, tracking which 4-byte slots have been written via `set_immediates`. - /// Checked against the pipeline's required slots before each dispatch. - immediate_slots_set: naga::valid::ImmediateSlots, - intermediate_trackers: Tracker, } @@ -294,13 +284,15 @@ impl<'scope, 'snatch_guard, 'cmd_enc> State<'scope, 'snatch_guard, 'cmd_enc> { self.pass.binder.check_compatibility(pipeline.as_ref())?; self.pass.binder.check_late_buffer_bindings()?; if !self + .pass + .immediate_state .immediate_slots_set .contains(pipeline.immediate_slots_required) { return Err(DispatchError::MissingImmediateData { missing: pipeline .immediate_slots_required - .difference(self.immediate_slots_set), + .difference(self.pass.immediate_state.immediate_slots_set), }); } Ok(()) @@ -309,6 +301,22 @@ impl<'scope, 'snatch_guard, 'cmd_enc> State<'scope, 'snatch_guard, 'cmd_enc> { } } + fn flush_immediates(&mut self) { + // SAFETY: The range of immediates written was validated in `is_ready`. + unsafe { + self.pass.immediate_state.flush_immediates( + self.pipeline + .as_ref() + .unwrap() + .layout() + .unwrap() + .raw() + .unwrap(), + self.pass.base.raw_encoder, + ); + } + } + /// Flush binding state in preparation for a dispatch. /// /// # Differences between render and compute passes @@ -716,13 +724,10 @@ pub(super) fn encode_compute_pass( pending_discard_init_fixups: SurfacesInDiscardState::new(), scope: device.new_usage_scope(), string_offset: 0, + immediate_state: ImmediateState::default(), }, active_query: None, - immediates: Vec::new(), - - immediate_slots_set: Default::default(), - intermediate_trackers: Tracker::new( device.ordered_buffer_usages, device.ordered_texture_usages, @@ -823,29 +828,17 @@ pub(super) fn encode_compute_pass( let scope = PassErrorScope::SetPipelineCompute; set_pipeline(&mut state, device, pipeline).map_pass_err(scope)?; } - ArcComputeCommand::SetImmediate { - offset, - size_bytes, - values_offset, - } => { + ArcComputeCommand::SetImmediate { offset, data } => { let scope = PassErrorScope::SetImmediate; - pass::set_immediates::( - &mut state.pass, - &base.immediates_data, - offset, - size_bytes, - Some(values_offset), - |data_slice| { - let offset_in_elements = (offset / wgt::IMMEDIATE_DATA_ALIGNMENT) as usize; - let size_in_elements = - (size_bytes / wgt::IMMEDIATE_DATA_ALIGNMENT) as usize; - state.immediates[offset_in_elements..][..size_in_elements] - .copy_from_slice(data_slice); - }, - ) - .map_pass_err(scope)?; - state.immediate_slots_set |= - naga::valid::ImmediateSlots::from_range(offset, size_bytes); + state + .pass + .immediate_state + .set_immediates::( + &state.pass.base.device.limits, + offset, + &data, + ) + .map_pass_err(scope)?; } ArcComputeCommand::DispatchWorkgroups(groups) => { let scope = PassErrorScope::Dispatch { indirect: false }; @@ -1001,22 +994,10 @@ fn set_pipeline( // Rebind resources let pipeline_layout = pipeline.layout()?; - pass::change_pipeline_layout::( + pass::change_pipeline_layout::( &mut state.pass, pipeline_layout, &pipeline.late_sized_buffer_groups, - || { - // This only needs to be here for compute pipelines because they use immediates for - // validating indirect draws. - state.immediates.clear(); - // Note that can only be one range for each stage. See the `MoreThanOneImmediateRangePerStage` error. - if pipeline_layout.immediate_size != 0 { - // Note that non-0 range start doesn't work anyway https://github.com/gfx-rs/wgpu/issues/4502 - let len = pipeline_layout.immediate_size as usize - / wgt::IMMEDIATE_DATA_ALIGNMENT as usize; - state.immediates.extend(core::iter::repeat_n(0, len)); - } - }, ) } @@ -1026,6 +1007,7 @@ fn dispatch_workgroups(state: &mut State, groups: [u32; 3]) -> Result<(), Comput state.is_ready()?; state.flush_bindings(None, false)?; + state.flush_immediates(); let groups_size_limit = state .pass @@ -1172,33 +1154,23 @@ fn dispatch_workgroups_indirect( // reset state { - let pipeline = state.pipeline.as_ref().unwrap(); - unsafe { state .pass .base .raw_encoder - .set_compute_pipeline(pipeline.raw()?); + .set_compute_pipeline(state.pipeline.as_ref().unwrap().raw()?); } - let pipeline_layout = pipeline.layout()?; - - if !state.immediates.is_empty() { - unsafe { - state.pass.base.raw_encoder.set_immediates( - pipeline_layout.raw()?, - 0, - &state.immediates, - ); - } - } + // Immediates are dirty because we used them for the validation pipeline + state.pass.immediate_state.immediates_dirty = true; + state.flush_immediates(); for (i, group, dynamic_offsets) in state.pass.binder.list_valid() { let raw_bg = group.try_raw(state.pass.base.snatch_guard)?; unsafe { state.pass.base.raw_encoder.set_bind_group( - pipeline_layout.raw()?, + state.pipeline.as_ref().unwrap().layout()?.raw()?, i as u32, raw_bg, dynamic_offsets, @@ -1231,7 +1203,7 @@ fn dispatch_workgroups_indirect( } } else { state.flush_bindings(Some(&buffer), true)?; - + state.flush_immediates(); let buf_raw = buffer.try_raw(state.pass.base.snatch_guard)?; unsafe { state @@ -1326,28 +1298,18 @@ impl ComputePass { let scope = PassErrorScope::SetImmediate; let base = pass_base!(self, scope); - let size_bytes = pass_try!( - base, - scope, - u32::try_from(data.len()).map_err(|_| ImmediateUploadError::ImmediateOutOfMemory) - ); pass_try!( base, scope, - pass::validate_immediates_alignment(offset, size_bytes) - ); - - let values_offset = base.immediates_data.len().try_into().unwrap(); - - base.immediates_data.extend( - data.chunks_exact(wgt::IMMEDIATE_DATA_ALIGNMENT as usize) - .map(|arr| u32::from_ne_bytes([arr[0], arr[1], arr[2], arr[3]])), + pass::validate_immediates_alignment(offset, data.len()) ); base.commands.push(ArcComputeCommand::SetImmediate { offset, - size_bytes: data.len() as u32, - values_offset, + data: data + .chunks_exact(size_of::()) + .map(|ck| u32::from_le_bytes(ck.try_into().unwrap())) + .collect(), }); Ok(()) diff --git a/wgpu-core/src/command/compute_command.rs b/wgpu-core/src/command/compute_command.rs index 809274633..b758262bc 100644 --- a/wgpu-core/src/command/compute_command.rs +++ b/wgpu-core/src/command/compute_command.rs @@ -25,15 +25,8 @@ pub enum ComputeCommand { /// must be a multiple of four. offset: u32, - /// The number of bytes to write. This must be a multiple of four. - size_bytes: u32, - - /// Index in `immediates_data` of the start of the data - /// to be written. - /// - /// Note: this is not a byte offset like `offset`. Rather, it is the - /// index of the first `u32` element in `immediates_data` to read. - values_offset: u32, + /// The immediate data to be written. + data: Vec, }, DispatchWorkgroups([u32; 3]), diff --git a/wgpu-core/src/command/mod.rs b/wgpu-core/src/command/mod.rs index fa0b9ec9b..5ec704627 100644 --- a/wgpu-core/src/command/mod.rs +++ b/wgpu-core/src/command/mod.rs @@ -113,8 +113,6 @@ pub type TexelCopyTextureInfo = ffi::TexelCopyTextureInfo; /// cbindgen:ignore pub type CopyExternalImageDestInfo = ffi::CopyExternalImageDestInfo; -const IMMEDIATES_CLEAR_ARRAY: &[u32] = &[0_u32; 64]; - pub(crate) struct EncoderErrorState { error: CommandEncoderError, @@ -1482,12 +1480,6 @@ pub struct BasePass { /// Each successive [`PushDebugGroup`] or [`InsertDebugMarker`] /// instruction consumes the next `len` bytes from this vector. pub string_data: Vec, - - /// Data used by `SetImmediate` instructions. - /// - /// See the documentation for [`RenderCommand::SetImmediate`] - /// and [`ComputeCommand::SetImmediate`] for details. - pub immediates_data: Vec, } impl BasePass { @@ -1498,7 +1490,6 @@ impl BasePass { commands: Vec::new(), dynamic_offsets: Vec::new(), string_data: Vec::new(), - immediates_data: Vec::new(), } } @@ -1509,7 +1500,6 @@ impl BasePass { commands: Vec::new(), dynamic_offsets: Vec::new(), string_data: Vec::new(), - immediates_data: Vec::new(), } } @@ -1528,7 +1518,6 @@ impl BasePass { commands: mem::take(&mut self.commands), dynamic_offsets: mem::take(&mut self.dynamic_offsets), string_data: mem::take(&mut self.string_data), - immediates_data: mem::take(&mut self.immediates_data), }), } } @@ -1912,27 +1901,7 @@ pub(crate) fn pop_debug_group(state: &mut EncodingState) -> Result<(), CommandEn Ok(()) } -fn immediates_clear(offset: u32, size_bytes: u32, mut push_fn: PushFn) -where - PushFn: FnMut(u32, &[u32]), -{ - let mut count_words = 0_u32; - let size_words = size_bytes / wgt::IMMEDIATE_DATA_ALIGNMENT; - while count_words < size_words { - let count_bytes = count_words * wgt::IMMEDIATE_DATA_ALIGNMENT; - let size_to_write_words = - (size_words - count_words).min(IMMEDIATES_CLEAR_ARRAY.len() as u32); - - push_fn( - offset + count_bytes, - &IMMEDIATES_CLEAR_ARRAY[0..size_to_write_words as usize], - ); - - count_words += size_to_write_words; - } -} - -#[derive(Debug, Clone)] +#[derive(Debug, Copy, Clone)] struct StateChange { last_state: Option, } diff --git a/wgpu-core/src/command/pass.rs b/wgpu-core/src/command/pass.rs index da432a5e9..8f614b774 100644 --- a/wgpu-core/src/command/pass.rs +++ b/wgpu-core/src/command/pass.rs @@ -17,7 +17,6 @@ use alloc::sync::Arc; use alloc::vec::Vec; use core::str; use thiserror::Error; -use wgt::error::{ErrorType, WebGpuError}; use wgt::DynamicOffset; #[derive(Clone, Debug, Error)] @@ -33,13 +32,66 @@ pub struct BindGroupIndexOutOfRange { #[error("Pipeline must be set")] pub struct MissingPipeline; -#[derive(Clone, Debug, Error)] -#[error("Setting `values_offset` to be `None` is only for internal use in render bundles")] -pub struct InvalidValuesOffset; +#[derive(Debug, Default)] +pub(crate) struct ImmediateState { + pub(crate) immediates: Vec, + pub(crate) immediates_dirty: bool, + /// A bitmask, tracking which 4-byte slots have been written via `set_immediates`. + /// Checked against the pipeline's required slots before each draw call. + pub(crate) immediate_slots_set: naga::valid::ImmediateSlots, +} -impl WebGpuError for InvalidValuesOffset { - fn webgpu_error_type(&self) -> ErrorType { - ErrorType::Validation +impl ImmediateState { + pub(crate) fn set_immediates( + &mut self, + limits: &wgt::Limits, + offset_bytes: u32, + data: &[u32], + ) -> Result<(), E> + where + E: From, + { + // Alignment has been validated when pushing `SetImmediate` commands. + + let offset_bytes_usize = offset_bytes as usize; + let size_bytes = data.len().saturating_mul(size_of::()); + if size_bytes + .checked_add(offset_bytes_usize) + .is_none_or(|end_offset| end_offset > limits.max_immediate_size as usize) + { + return Err(ImmediateUploadError::EndOffsetBeyondLimit { + start_offset: offset_bytes, + size_bytes, + limit: limits.max_immediate_size, + } + .into()); + } + + let end_offset_bytes = offset_bytes_usize + size_bytes; + let size_per_elem = size_of::(); + if self.immediates.len() < end_offset_bytes / size_per_elem { + self.immediates.resize(end_offset_bytes / size_per_elem, 0); + } + self.immediates[offset_bytes_usize / size_per_elem..end_offset_bytes / size_per_elem] + .copy_from_slice(data); + self.immediate_slots_set |= + naga::valid::ImmediateSlots::from_range(offset_bytes, size_bytes.try_into().unwrap()); + self.immediates_dirty = true; + + Ok(()) + } + + pub(crate) unsafe fn flush_immediates( + &mut self, + raw_layout: &dyn hal::DynPipelineLayout, + raw_encoder: &mut dyn hal::DynCommandEncoder, + ) { + if !self.immediates.is_empty() && self.immediates_dirty { + unsafe { + raw_encoder.set_immediates(raw_layout, 0, &self.immediates); + } + self.immediates_dirty = false; + } } } @@ -59,6 +111,8 @@ pub(crate) struct PassState<'scope, 'snatch_guard, 'cmd_enc> { pub(crate) dynamic_offset_count: usize, pub(crate) string_offset: usize, + + pub(crate) immediate_state: ImmediateState, } pub(crate) fn set_bind_group( @@ -193,11 +247,10 @@ pub(super) fn flush_bindings_helper( Ok(()) } -pub(super) fn change_pipeline_layout( +pub(super) fn change_pipeline_layout( state: &mut PassState, pipeline_layout: &Arc, late_sized_buffer_groups: &[LateSizedBufferGroup], - f: F, ) -> Result<(), E> where E: From, @@ -206,107 +259,26 @@ where .binder .change_pipeline_layout(pipeline_layout, late_sized_buffer_groups) { - f(); - - super::immediates_clear( - 0, - pipeline_layout.immediate_size, - |clear_offset, clear_data| unsafe { - state.base.raw_encoder.set_immediates( - pipeline_layout - .raw() - .expect("Pipeline layout should be valid at this point"), - clear_offset, - clear_data, - ); - }, - ); + state.immediate_state.immediates_dirty = true; } Ok(()) } pub(crate) fn validate_immediates_alignment( offset: u32, - size_bytes: u32, + size_bytes: usize, ) -> Result<(), ImmediateUploadError> { if !offset.is_multiple_of(wgt::IMMEDIATE_DATA_ALIGNMENT) { return Err(ImmediateUploadError::StartOffsetUnaligned(offset)); } - if !size_bytes.is_multiple_of(wgt::IMMEDIATE_DATA_ALIGNMENT) { + if !size_bytes.is_multiple_of(wgt::IMMEDIATE_DATA_ALIGNMENT as usize) { return Err(ImmediateUploadError::SizeUnaligned(size_bytes)); } Ok(()) } -pub(crate) fn set_immediates( - state: &mut PassState, - immediates_data: &[u32], - offset: u32, - size_bytes: u32, - values_offset: Option, - f: F, -) -> Result<(), E> -where - E: From + From + From, -{ - api_log!("Pass::set_immediates"); - - // Alignment has been validated by `validate_immediates_alignment` when pushing `SetImmediate` commands. - - let values_offset = values_offset.ok_or(InvalidValuesOffset)?; - - let pipeline_layout = state - .binder - .pipeline_layout - .as_ref() - .ok_or(MissingPipeline)?; - - pipeline_layout.validate_immediates_ranges(offset, size_bytes)?; - - let values_offset_usize = usize::try_from(values_offset) - .expect("`values_offset` is outside the bounds of `usize` (!?)"); - if values_offset_usize > immediates_data.len() { - panic!( - "Internal error: `set_immediates` values offset ({}) \ - overruns the immediates data length ({})", - values_offset, - immediates_data.len() - ); - } - - let size_immediate_elements = size_bytes / wgt::IMMEDIATE_DATA_ALIGNMENT; - let size_immediate_elements_usize = usize::try_from(size_immediate_elements) - .expect("`size_immediate_elements` is outside the bounds of `usize` (!?)"); - if size_immediate_elements_usize > immediates_data.len() - values_offset_usize { - panic!( - "Internal error: `set_immediates` values offset + count ({} + {}) \ - overruns the immediates data length ({})", - values_offset, - size_immediate_elements, - immediates_data.len() - ); - } - - // NOTE: These additions are will not overflow, because we've validated the range above. - let values_end_offset = values_offset_usize + size_immediate_elements_usize; - let data_slice = &immediates_data[(values_offset_usize)..values_end_offset]; - - f(data_slice); - - unsafe { - state.base.raw_encoder.set_immediates( - pipeline_layout - .raw() - .expect("Pipeline layout should be valid at this point"), - offset, - data_slice, - ) - } - Ok(()) -} - pub(crate) fn write_timestamp( state: &mut PassState, device: &Arc, diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index 9747fc945..880d81ab9 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -17,7 +17,7 @@ use crate::{ command::{ bind::Binder, memory_init::{fixup_discarded_surfaces, SurfacesInDiscardState, TextureSurfaceDiscard}, - pass::{self, flush_bindings_helper}, + pass::{self, flush_bindings_helper, ImmediateState}, pass_base, pass_try, query::{ end_occlusion_query, end_pipeline_statistics_query, record_pass_timestamp_writes, @@ -398,7 +398,6 @@ impl fmt::Debug for RenderPass { .field("depth_stencil_target", &self.depth_stencil_attachment) .field("command count", &self.base.commands.len()) .field("dynamic offset count", &self.base.dynamic_offsets.len()) - .field("immediate data u32 count", &self.base.immediates_data.len()) .field("multiview mask", &self.multiview_mask) .finish() } @@ -682,10 +681,6 @@ struct State<'scope, 'snatch_guard, 'cmd_enc> { pass: pass::PassState<'scope, 'snatch_guard, 'cmd_enc>, - /// A bitmask, tracking which 4-byte slots have been written via `set_immediates`. - /// Checked against the pipeline's required slots before each draw call. - immediate_slots_set: naga::valid::ImmediateSlots, - active_occlusion_query: Option<(Arc, u32)>, active_pipeline_statistics_query: Option<(Arc, u32)>, } @@ -726,13 +721,15 @@ impl<'scope, 'snatch_guard, 'cmd_enc> State<'scope, 'snatch_guard, 'cmd_enc> { }); } if !self + .pass + .immediate_state .immediate_slots_set .contains(pipeline.immediate_slots_required) { return Err(DrawError::MissingImmediateData { missing: pipeline .immediate_slots_required - .difference(self.immediate_slots_set), + .difference(self.pass.immediate_state.immediate_slots_set), }); } Ok(()) @@ -741,6 +738,22 @@ impl<'scope, 'snatch_guard, 'cmd_enc> State<'scope, 'snatch_guard, 'cmd_enc> { } } + fn flush_immediates(&mut self) { + // SAFETY: The range of immediates written was validated in `is_ready`. + unsafe { + self.pass.immediate_state.flush_immediates( + self.pipeline + .as_ref() + .unwrap() + .layout() + .unwrap() + .raw() + .unwrap(), + self.pass.base.raw_encoder, + ); + } + } + /// Flush binding state in preparation for a draw call. /// /// See the compute pass version for an explanation of some ways that @@ -756,7 +769,7 @@ impl<'scope, 'snatch_guard, 'cmd_enc> State<'scope, 'snatch_guard, 'cmd_enc> { self.pipeline = None; self.index.reset(); self.vertex = Default::default(); - self.immediate_slots_set = Default::default(); + self.pass.immediate_state.immediate_slots_set = Default::default(); } /// Flush dirty vertex buffer slots to the HAL encoder in preparation for a draw call. @@ -962,8 +975,6 @@ pub enum RenderPassErrorInner { #[error("Unable to clear non-present/read-only stencil")] InvalidStencilOps, #[error(transparent)] - InvalidValuesOffset(#[from] pass::InvalidValuesOffset), - #[error(transparent)] MissingFeatures(#[from] MissingFeatures), #[error(transparent)] MissingDownlevelFlags(#[from] MissingDownlevelFlags), @@ -1101,7 +1112,6 @@ impl WebGpuError for RenderPassError { RenderPassErrorInner::IncompatibleBundleTargets(e) => e.webgpu_error_type(), RenderPassErrorInner::InvalidAttachment(e) => e.webgpu_error_type(), RenderPassErrorInner::TimestampWrites(e) => e.webgpu_error_type(), - RenderPassErrorInner::InvalidValuesOffset(e) => e.webgpu_error_type(), RenderPassErrorInner::InvalidParentEncoder | RenderPassErrorInner::UnsupportedResolveTargetFormat { .. } @@ -2433,9 +2443,9 @@ pub(super) fn encode_render_pass( dynamic_offset_count: 0, string_offset: 0, - }, - immediate_slots_set: Default::default(), + immediate_state: ImmediateState::default(), + }, active_occlusion_query: None, active_pipeline_statistics_query: None, @@ -2498,23 +2508,17 @@ pub(super) fn encode_render_pass( let scope = PassErrorScope::SetViewport; set_viewport(&mut state, rect, depth_min, depth_max).map_pass_err(scope)?; } - ArcRenderCommand::SetImmediate { - offset, - size_bytes, - values_offset, - } => { + ArcRenderCommand::SetImmediate { offset, data } => { let scope = PassErrorScope::SetImmediate; - pass::set_immediates::( - &mut state.pass, - &base.immediates_data, - offset, - size_bytes, - values_offset, - |_| {}, - ) - .map_pass_err(scope)?; - state.immediate_slots_set |= - naga::valid::ImmediateSlots::from_range(offset, size_bytes); + state + .pass + .immediate_state + .set_immediates::( + &state.pass.base.device.limits, + offset, + &data, + ) + .map_pass_err(scope)?; } ArcRenderCommand::SetScissor(rect) => { let scope = PassErrorScope::SetScissorRect; @@ -2873,11 +2877,10 @@ fn set_pipeline( } // Rebind resource - pass::change_pipeline_layout::( + pass::change_pipeline_layout::( &mut state.pass, pipeline.layout()?, &pipeline.late_sized_buffer_groups, - || {}, )?; // Update vertex buffer limits. @@ -3163,6 +3166,7 @@ fn draw( state.is_ready(DrawCommandFamily::Draw)?; state.flush_vertex_buffers()?; state.flush_bindings()?; + state.flush_immediates(); state .vertex @@ -3199,6 +3203,7 @@ fn draw_indexed( state.is_ready(DrawCommandFamily::DrawIndexed)?; state.flush_vertex_buffers()?; state.flush_bindings()?; + state.flush_immediates(); let last_index = first_index as u64 + index_count as u64; let index_limit = state.index.limit; @@ -3239,6 +3244,7 @@ fn draw_mesh_tasks( state.is_ready(DrawCommandFamily::DrawMeshTasks)?; state.flush_bindings()?; + state.flush_immediates(); validate_mesh_draw_multiview(state)?; let limits = &state.pass.base.device.limits; @@ -3294,6 +3300,7 @@ fn multi_draw_indirect( state.is_ready(family)?; state.flush_vertex_buffers()?; state.flush_bindings()?; + state.flush_immediates(); if family == DrawCommandFamily::DrawMeshTasks { validate_mesh_draw_multiview(state)?; @@ -3489,6 +3496,7 @@ fn multi_draw_indirect_count( state.is_ready(family)?; state.flush_vertex_buffers()?; state.flush_bindings()?; + state.flush_immediates(); if family == DrawCommandFamily::DrawMeshTasks { validate_mesh_draw_multiview(state)?; @@ -3869,28 +3877,18 @@ impl RenderPass { let scope = PassErrorScope::SetImmediate; let base = pass_base!(self, scope); - let size_bytes = pass_try!( - base, - scope, - u32::try_from(data.len()).map_err(|_| ImmediateUploadError::ImmediateOutOfMemory) - ); pass_try!( base, scope, - pass::validate_immediates_alignment(offset, size_bytes) - ); - - let values_offset = base.immediates_data.len().try_into().unwrap(); - - base.immediates_data.extend( - data.chunks_exact(wgt::IMMEDIATE_DATA_ALIGNMENT as usize) - .map(|arr| u32::from_ne_bytes([arr[0], arr[1], arr[2], arr[3]])), + pass::validate_immediates_alignment(offset, data.len()) ); base.commands.push(ArcRenderCommand::SetImmediate { offset, - size_bytes: data.len() as u32, - values_offset: Some(values_offset), + data: data + .chunks_exact(size_of::()) + .map(|ck| u32::from_le_bytes(ck.try_into().unwrap())) + .collect(), }); Ok(()) diff --git a/wgpu-core/src/command/render_command.rs b/wgpu-core/src/command/render_command.rs index 962b3bd55..1e7b00d87 100644 --- a/wgpu-core/src/command/render_command.rs +++ b/wgpu-core/src/command/render_command.rs @@ -1,9 +1,9 @@ -use wgt::{BufferAddress, BufferSize, Color}; - use super::{DrawCommandFamily, Rect}; #[cfg(feature = "serde")] use crate::command::serde_object_reference_struct; use crate::command::{ArcReferences, ReferenceType}; +use alloc::vec::Vec; +use wgt::{BufferAddress, BufferSize, Color}; #[cfg(feature = "serde")] use macro_rules_attribute::apply; @@ -50,20 +50,8 @@ pub enum RenderCommand { /// must be a multiple of four. offset: u32, - /// The number of bytes to write. This must be a multiple of four. - size_bytes: u32, - - /// Index in [`BasePass::immediates_data`] of the start of the data - /// to be written. - /// - /// Note: this is not a byte offset like `offset`. Rather, it is the - /// index of the first `u32` element in `immediates_data` to read. - /// - /// `None` means zeros should be written to the destination range, and - /// there is no corresponding data in `immediates_data`. This is used - /// by render bundles, which explicitly clear out any state that - /// post-bundle code might see. - values_offset: Option, + /// The immediate data to be written. + data: Vec, }, Draw { vertex_count: u32, diff --git a/wgpu-core/src/device/trace/record.rs b/wgpu-core/src/device/trace/record.rs index 553e41a65..c262efda8 100644 --- a/wgpu-core/src/device/trace/record.rs +++ b/wgpu-core/src/device/trace/record.rs @@ -449,7 +449,6 @@ impl IntoTrace for BasePass { .collect(), dynamic_offsets: self.dynamic_offsets, string_data: self.string_data, - immediates_data: self.immediates_data, } } } @@ -469,15 +468,7 @@ impl IntoTrace for ArcComputeCommand { bind_group: bind_group.map(|bg| bg.into_trace()), }, C::SetPipeline(id) => C::SetPipeline(id.into_trace()), - C::SetImmediate { - offset, - size_bytes, - values_offset, - } => C::SetImmediate { - offset, - size_bytes, - values_offset, - }, + C::SetImmediate { offset, data } => C::SetImmediate { offset, data }, C::DispatchWorkgroups(groups) => C::DispatchWorkgroups(groups), C::DispatchWorkgroupsIndirect { buffer, offset } => C::DispatchWorkgroupsIndirect { buffer: buffer.into_trace(), @@ -574,15 +565,7 @@ impl IntoTrace for ArcRenderCommand { depth_max, }, C::SetScissor(rect) => C::SetScissor(rect), - C::SetImmediate { - offset, - size_bytes, - values_offset, - } => C::SetImmediate { - offset, - size_bytes, - values_offset, - }, + C::SetImmediate { offset, data } => C::SetImmediate { offset, data }, C::Draw { vertex_count, instance_count, diff --git a/wgpu/src/backend/wgpu_core.rs b/wgpu/src/backend/wgpu_core.rs index 349d2cff7..9b770c688 100644 --- a/wgpu/src/backend/wgpu_core.rs +++ b/wgpu/src/backend/wgpu_core.rs @@ -548,6 +548,7 @@ pub struct CoreCommandBuffer { #[derive(Debug)] pub struct CoreRenderBundleEncoder { pub(crate) context: ContextWgpuCore, + error_sink: ErrorSink, encoder: Box, id: crate::cmp::Identifier, } @@ -1825,6 +1826,7 @@ impl dispatch::DeviceInterface for CoreDevice { CoreRenderBundleEncoder { context: self.context.clone(), + error_sink: Arc::clone(&self.error_sink), encoder, id: crate::cmp::Identifier::create(), } @@ -3852,6 +3854,19 @@ impl dispatch::RenderBundleEncoderInterface for CoreRenderBundleEncoder { } fn set_immediates(&mut self, offset: u32, data: &[u8]) { + if !data + .len() + .is_multiple_of(wgt::IMMEDIATE_DATA_ALIGNMENT as usize) + { + self.context.handle_error( + &self.error_sink, + wgc::binding_model::ImmediateUploadError::SizeUnaligned(data.len()), + self.encoder.label(), + "RenderBundleEncoder::set_immediates", + ); + return; + } + self.context .0 .render_bundle_encoder_set_immediates(&mut self.encoder, offset, data) @@ -3923,14 +3938,19 @@ impl dispatch::RenderBundleEncoderInterface for CoreRenderBundleEncoder { where Self: Sized, { + let label = self.encoder.label().map(alloc::string::ToString::to_string); let (id, error) = self.context.0.render_bundle_encoder_finish( &mut self.encoder, &desc.map_label(|l| l.map(Borrowed)), None, ); if let Some(err) = error { - self.context - .handle_error_fatal(err, "RenderBundleEncoder::finish"); + self.context.handle_error( + &self.error_sink, + err, + label.as_deref(), + "RenderBundleEncoder::finish", + ); } CoreRenderBundle { context: self.context.clone(),