diff --git a/cts_runner/fail.lst b/cts_runner/fail.lst index eeddcbb60..82bc7079d 100644 --- a/cts_runner/fail.lst +++ b/cts_runner/fail.lst @@ -39,13 +39,15 @@ 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:* // 0%, feature not implemented +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 webgpu:api,validation,layout_shader_compat:pipeline_layout_shader_exact_match:* // dx12, https://bugzilla.mozilla.org/show_bug.cgi?id=2017725 webgpu:api,validation,non_filterable_texture:non_filterable_texture_with_filtering_sampler:* // 80%, depth textures with filtering samplers +webgpu:api,validation,pipeline,immediates:* // 0%, https://github.com/gfx-rs/wgpu/issues/8556 webgpu:api,validation,query_set,create:count:* // 0%, wgpu incorrectly rejects zero-count query sets webgpu:api,validation,queue,destroyed,* // 71%, writeBuffer/writeTexture return value, destroyed query set webgpu:api,validation,queue,writeBuffer:ranges:* // 0%, missing OperationError for invalid ranges diff --git a/cts_runner/test.lst b/cts_runner/test.lst index beb282a2c..f7faff6c6 100644 --- a/cts_runner/test.lst +++ b/cts_runner/test.lst @@ -32,6 +32,8 @@ webgpu:api,operation,command_buffer,image_copy:undefined_params:initMethod="Writ webgpu:api,operation,command_buffer,image_copy:undefined_params:initMethod="WriteTexture";checkMethod="PartialCopyT2B";dimension="1d" fails-if(dx12) webgpu:api,operation,command_buffer,image_copy:undefined_params:initMethod="WriteTexture";checkMethod="PartialCopyT2B";dimension="2d" webgpu:api,operation,command_buffer,image_copy:undefined_params:initMethod="WriteTexture";checkMethod="PartialCopyT2B";dimension="3d" +// https://github.com/gfx-rs/wgpu/issues/8556 +fails-if(dx12) webgpu:api,operation,command_buffer,programmable,immediate:* webgpu:api,operation,compute_pipeline,overrides:* //FAIL: webgpu:api,operation,compute,basic:large_dispatch:* webgpu:api,operation,compute,basic:memcpy:* @@ -185,11 +187,40 @@ webgpu:api,validation,encoding,cmds,setBindGroup:state_and_binding_index:encoder 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,createRenderBundleEncoder:* -webgpu:api,validation,encoding,encoder_open_state:compute_pass_commands:* +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:* +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_state:* webgpu:api,validation,encoding,programmable,pipeline_bind_group_compat:* webgpu:api,validation,encoding,queries,begin_end:nesting:* diff --git a/deno_webgpu/adapter.rs b/deno_webgpu/adapter.rs index 348eb72d5..19d2cc166 100644 --- a/deno_webgpu/adapter.rs +++ b/deno_webgpu/adapter.rs @@ -457,6 +457,11 @@ impl GPUSupportedLimits { fn maxComputeWorkgroupsPerDimension(&self) -> u32 { self.0.max_compute_workgroups_per_dimension } + + #[getter] + fn maxImmediateSize(&self) -> u32 { + self.0.max_immediate_size + } } pub struct GPUSupportedFeatures(v8::Global); diff --git a/deno_webgpu/compute_pass.rs b/deno_webgpu/compute_pass.rs index 50dffae29..fae338e68 100644 --- a/deno_webgpu/compute_pass.rs +++ b/deno_webgpu/compute_pass.rs @@ -12,8 +12,10 @@ use deno_core::webidl::WebIdlConverter; use deno_core::webidl::WebIdlError; use deno_core::GarbageCollected; use deno_core::WebIDL; +use deno_error::JsErrorBox; use crate::error::GPUGenericError; +use crate::get_data_slice; use crate::Instance; pub struct GPUComputePassEncoder { @@ -230,6 +232,30 @@ impl GPUComputePassEncoder { Ok(()) } + + #[required(2)] + #[undefined] + fn set_immediates<'a>( + &self, + scope: &mut v8::HandleScope<'a>, + #[webidl(options(enforce_range = true))] offset: u32, + data_arg: v8::Local<'a, v8::Value>, + #[webidl(default = 0, options(enforce_range = true))] data_offset: u64, + #[webidl(options(enforce_range = true))] data_size: Option, + ) -> Result<(), JsErrorBox> { + let data = get_data_slice(scope, data_arg, data_offset, data_size)?; + + let err = self + .instance + .compute_pass_set_immediates( + &mut self.compute_pass.borrow_mut(), + offset, + data, + ) + .err(); + self.error_handler.push_error(err); + Ok(()) + } } #[derive(WebIDL)] diff --git a/deno_webgpu/device.rs b/deno_webgpu/device.rs index c7d48e6e5..50e4f3cc6 100644 --- a/deno_webgpu/device.rs +++ b/deno_webgpu/device.rs @@ -394,7 +394,7 @@ impl GPUDevice { let wgpu_descriptor = wgpu_core::binding_model::PipelineLayoutDescriptor { label: crate::transform_label(descriptor.label.clone()), bind_group_layouts: Cow::Owned(bind_group_layouts), - immediate_size: 0, + immediate_size: descriptor.immediate_size, }; let (id, err) = self.instance.device_create_pipeline_layout( diff --git a/deno_webgpu/lib.rs b/deno_webgpu/lib.rs index fa34aba9d..1f409710b 100644 --- a/deno_webgpu/lib.rs +++ b/deno_webgpu/lib.rs @@ -11,6 +11,7 @@ use deno_core::op2; use deno_core::v8; use deno_core::GarbageCollected; use deno_core::OpState; +use deno_error::JsErrorBox; use serde::de::IntoDeserializer; use serde::Deserialize as _; pub use wgpu_core; @@ -308,3 +309,130 @@ fn transform_label<'a>(label: String) -> Option> { Some(std::borrow::Cow::Owned(label)) } } + +fn operation_error( + message: impl Into>, +) -> JsErrorBox { + JsErrorBox::new("DOMExceptionOperationError", message) +} + +/// Validate the input data (AllowSharedBufferSource) and return the slice that applied the offset and size, +/// or return `Err` if validation fails. +/// +/// See also the content timeline requirements of +/// and +fn get_data_slice<'a>( + scope: &mut v8::HandleScope, + data_arg: v8::Local<'a, v8::Value>, + data_offset: u64, + data_size: Option, +) -> Result<&'a [u8], JsErrorBox> { + const EMPTY: &[u8] = &[]; + // Per the WebGPU spec, dataOffset and size are in elements (not bytes) + // when data is a TypedArray, and in bytes otherwise. + let (buf, bytes_per_element) = if let Ok(typed_array) = + v8::Local::::try_from(data_arg) + { + let len = typed_array.length(); + // Avoid panicking as data of zero length array is `None`. + if len == 0 { + (EMPTY, 1) + } else { + let bpe = typed_array.byte_length() / len; + let byte_offset = typed_array.byte_offset(); + let byte_len = typed_array.byte_length(); + let ab = typed_array.buffer(scope).unwrap(); + // SAFETY: Pointer is non-null, and V8 guarantees that the + // byte_offset is within the buffer backing store. + let ptr = unsafe { ab.data().unwrap().as_ptr().add(byte_offset) }; + let buf = + // SAFETY: the slice is within the bounds of the backing store + unsafe { std::slice::from_raw_parts(ptr as *const u8, byte_len) }; + (buf, bpe) + } + } else if let Ok(ab) = v8::Local::::try_from(data_arg) { + let byte_len = ab.byte_length(); + // Avoid panicking as data of zero length array is `None`. + if byte_len == 0 { + (EMPTY, 1) + } else { + let ptr = ab.data().unwrap().as_ptr(); + let buf = + // SAFETY: Pointer is non-null and byte_len is within the backing store. + unsafe { std::slice::from_raw_parts(ptr as *const u8, byte_len) }; + (buf, 1) + } + } else if let Ok(ab) = v8::Local::::try_from(data_arg) + { + let byte_len = ab.byte_length(); + // Avoid panicking as data of zero length array is `None`. + if byte_len == 0 { + (EMPTY, 1) + } else { + let ptr = ab.get_backing_store().data().unwrap().as_ptr(); + let buf = + // SAFETY: Pointer is non-null and byte_len is within the backing store. + unsafe { std::slice::from_raw_parts(ptr as *const u8, byte_len) }; + (buf, 1) + } + } else if let Ok(view) = v8::Local::::try_from(data_arg) + { + let byte_offset = view.byte_offset(); + let byte_len = view.byte_length(); + if byte_len == 0 { + (EMPTY, 1) + } else { + let ab = view.buffer(scope).unwrap(); + // SAFETY: Pointer is non-null, and V8 guarantees that the + // byte_offset is within the buffer backing store. + let ptr = unsafe { ab.data().unwrap().as_ptr().add(byte_offset) }; + // SAFETY: the slice is within the bounds of the backing store + let buf = + unsafe { std::slice::from_raw_parts(ptr as *const u8, byte_len) }; + (buf, 1) + } + } else { + return Err(JsErrorBox::type_error( + "data must be an ArrayBuffer, SharedArrayBuffer or ArrayBufferView", + )); + }; + + let data_offset_bytes = data_offset + .checked_mul(bytes_per_element as u64) + .ok_or(operation_error("data offset in bytes overflows a `u64`"))?; + + let content_size_bytes = if let Some(data_size) = data_size { + let data_size_bytes = data_size + .checked_mul(bytes_per_element as u64) + .ok_or(operation_error("data size in bytes overflows a `u64`"))?; + if data_offset_bytes + .checked_add(data_size_bytes) + .ok_or(operation_error("data size + offset overflows a `u64`"))? + > buf.len() as u64 + { + return Err(operation_error("data size + offset is out of bounds")); + } + data_size_bytes + } else { + (buf.len() as u64) + .checked_sub(data_offset_bytes) + .ok_or(operation_error("data offset is out of bounds"))? + }; + + // Both `Queue::write_buffer` and `set_immediates` require content size to be a multiple of 4 + const { + assert!(wgpu_types::COPY_BUFFER_ALIGNMENT == 4); + assert!(wgpu_types::IMMEDIATE_DATA_ALIGNMENT == 4); + } + if !content_size_bytes.is_multiple_of(4) { + return Err(operation_error( + "content size in bytes is not a multiple of 4", + )); + } + + // We have validated data offset and content size are within the bounds. + let data = &buf[(data_offset_bytes as usize) + ..((data_offset_bytes + content_size_bytes) as usize)]; + + Ok(data) +} diff --git a/deno_webgpu/pipeline_layout.rs b/deno_webgpu/pipeline_layout.rs index 11890c6e0..250aed4f5 100644 --- a/deno_webgpu/pipeline_layout.rs +++ b/deno_webgpu/pipeline_layout.rs @@ -60,4 +60,6 @@ pub(crate) struct GPUPipelineLayoutDescriptor { pub bind_group_layouts: Vec>>, + #[webidl(default = 0)] + pub immediate_size: u32, } diff --git a/deno_webgpu/queue.rs b/deno_webgpu/queue.rs index dee47927f..10604ad67 100644 --- a/deno_webgpu/queue.rs +++ b/deno_webgpu/queue.rs @@ -7,6 +7,7 @@ use std::time::Duration; use deno_core::cppgc::Ptr; use deno_core::futures::channel::oneshot; use deno_core::op2; +use deno_core::v8; use deno_core::GarbageCollected; use deno_core::WebIDL; use deno_error::JsErrorBox; @@ -14,6 +15,7 @@ use deno_error::JsErrorBox; use crate::buffer::GPUBuffer; use crate::command_buffer::GPUCommandBuffer; use crate::error::GPUGenericError; +use crate::get_data_slice; use crate::texture::GPUTexture; use crate::texture::GPUTextureAspect; use crate::webidl::GPUExtent3D; @@ -127,20 +129,16 @@ impl GPUQueue { #[required(3)] #[undefined] - fn write_buffer( + fn write_buffer<'a>( &self, + scope: &mut v8::HandleScope<'a>, #[webidl] buffer: Ptr, #[webidl(options(enforce_range = true))] buffer_offset: u64, - #[anybuffer] buf: &[u8], + data_arg: v8::Local<'a, v8::Value>, #[webidl(default = 0, options(enforce_range = true))] data_offset: u64, #[webidl(options(enforce_range = true))] size: Option, - ) { - let data = match size { - Some(size) => { - &buf[(data_offset as usize)..((data_offset + size) as usize)] - } - None => &buf[(data_offset as usize)..], - }; + ) -> Result<(), JsErrorBox> { + let data = get_data_slice(scope, data_arg, data_offset, size)?; let err = self .instance @@ -148,6 +146,8 @@ impl GPUQueue { .err(); self.error_handler.push_error(err); + + Ok(()) } #[required(4)] diff --git a/deno_webgpu/render_bundle.rs b/deno_webgpu/render_bundle.rs index c7cee6949..02aec7b15 100644 --- a/deno_webgpu/render_bundle.rs +++ b/deno_webgpu/render_bundle.rs @@ -18,6 +18,7 @@ use deno_error::JsErrorBox; use crate::buffer::GPUBuffer; use crate::error::GPUGenericError; +use crate::get_data_slice; use crate::texture::GPUTextureFormat; use crate::Instance; @@ -383,6 +384,34 @@ impl GPURenderBundleEncoder { ); Ok(()) } + + #[required(2)] + #[undefined] + fn set_immediates<'a>( + &self, + scope: &mut v8::HandleScope<'a>, + #[webidl(options(enforce_range = true))] offset: u32, + data_arg: v8::Local<'a, v8::Value>, + #[webidl(default = 0, options(enforce_range = true))] data_offset: u64, + #[webidl(options(enforce_range = true))] data_size: Option, + ) -> Result<(), JsErrorBox> { + let data = get_data_slice(scope, data_arg, data_offset, data_size)?; + + let mut encoder = self.encoder.borrow_mut(); + let encoder = encoder.as_mut().ok_or_else(|| { + JsErrorBox::generic("Encoder has already been finished") + })?; + + unsafe { + wgpu_core::command::bundle_ffi::wgpu_render_bundle_set_immediates( + encoder, + offset, + data.len().try_into().unwrap(), + data.as_ptr(), + ); + } + Ok(()) + } } #[derive(WebIDL)] diff --git a/deno_webgpu/render_pass.rs b/deno_webgpu/render_pass.rs index 3728992dc..d62615da3 100644 --- a/deno_webgpu/render_pass.rs +++ b/deno_webgpu/render_pass.rs @@ -17,9 +17,11 @@ use deno_core::webidl::WebIdlConverter; use deno_core::webidl::WebIdlError; use deno_core::GarbageCollected; use deno_core::WebIDL; +use deno_error::JsErrorBox; use crate::buffer::GPUBuffer; use crate::error::GPUGenericError; +use crate::get_data_slice; use crate::render_bundle::GPURenderBundle; use crate::texture::GPUTexture; use crate::texture::GPUTextureView; @@ -445,6 +447,30 @@ impl GPURenderPassEncoder { .err(); self.error_handler.push_error(err); } + + #[required(2)] + #[undefined] + fn set_immediates<'a>( + &self, + scope: &mut v8::HandleScope<'a>, + #[webidl(options(enforce_range = true))] offset: u32, + data_arg: v8::Local<'a, v8::Value>, + #[webidl(default = 0, options(enforce_range = true))] data_offset: u64, + #[webidl(options(enforce_range = true))] data_size: Option, + ) -> Result<(), JsErrorBox> { + let data = get_data_slice(scope, data_arg, data_offset, data_size)?; + + let err = self + .instance + .render_pass_set_immediates( + &mut self.render_pass.borrow_mut(), + offset, + data, + ) + .err(); + self.error_handler.push_error(err); + Ok(()) + } } #[derive(WebIDL)] diff --git a/wgpu-core/src/binding_model.rs b/wgpu-core/src/binding_model.rs index e50b9d1b7..74e3b7121 100644 --- a/wgpu-core/src/binding_model.rs +++ b/wgpu-core/src/binding_model.rs @@ -873,8 +873,10 @@ 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( - "Start offset {start_offset} overruns the immediate data range with a size of {immediate_size}" + "Provided immediate data start offset {start_offset} overruns the range with a size of {immediate_size}" )] StartOffsetOverrun { start_offset: u32, @@ -904,20 +906,6 @@ pub enum ImmediateUploadError { size: u32, immediate_size: u32, }, - #[error("Start index {start_index} overruns the value data range with {data_size} element(s)")] - ValueStartIndexOverrun { start_index: u32, data_size: usize }, - #[error( - "Start index {} + count of {} overruns the value data range \ - with {} element(s)", - start_index, - count, - data_size - )] - ValueEndIndexOverrun { - start_index: u32, - count: u32, - data_size: usize, - }, } impl WebGpuError for ImmediateUploadError { @@ -1028,14 +1016,6 @@ impl PipelineLayout { // as immediate data ranges are already validated to be within bounds, // and we validate that they are within the ranges. - if !offset.is_multiple_of(wgt::IMMEDIATE_DATA_ALIGNMENT) { - return Err(ImmediateUploadError::StartOffsetUnaligned(offset)); - } - - if !size_bytes.is_multiple_of(wgt::IMMEDIATE_DATA_ALIGNMENT) { - return Err(ImmediateUploadError::SizeUnaligned(offset)); - } - if offset > self.immediate_size { return Err(ImmediateUploadError::StartOffsetOverrun { start_offset: offset, diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index a87a31aee..7bc7e7469 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -101,8 +101,9 @@ use crate::command::ArcReferences; use crate::{ binding_model::{BindError, BindGroup, PipelineLayout}, command::{ - bind::Binder, BasePass, BindGroupStateChange, ColorAttachmentError, DrawError, - IdReferences, MapPassErr, PassErrorScope, RenderCommand, RenderCommandError, StateChange, + bind::Binder, pass::validate_immediates_alignment, BasePass, BindGroupStateChange, + ColorAttachmentError, DrawError, IdReferences, MapPassErr, PassErrorScope, RenderCommand, + RenderCommandError, StateChange, }, device::{ AttachmentData, Device, DeviceError, MissingDownlevelFlags, MissingFeatures, @@ -798,6 +799,8 @@ fn set_immediates( size_bytes: u32, values_offset: Option, ) -> Result<(), RenderBundleErrorInner> { + validate_immediates_alignment(offset, size_bytes)?; + let pipeline = state .pipeline .as_deref() @@ -1594,7 +1597,7 @@ where pub mod bundle_ffi { use super::{RenderBundleEncoder, RenderCommand}; use crate::{command::DrawCommandFamily, id, RawString}; - use core::{convert::TryInto, slice}; + use core::slice; use wgt::{BufferAddress, BufferSize, DynamicOffset, IndexFormat}; /// # Safety @@ -1677,21 +1680,10 @@ pub mod bundle_ffi { size_bytes: u32, data: *const u8, ) { - assert_eq!( - offset & (wgt::IMMEDIATE_DATA_ALIGNMENT - 1), - 0, - "Immediate data offset must be aligned to 4 bytes." - ); - assert_eq!( - size_bytes & (wgt::IMMEDIATE_DATA_ALIGNMENT - 1), - 0, - "Immediate data size must be aligned to 4 bytes." - ); let data_slice = unsafe { slice::from_raw_parts(data, size_bytes as usize) }; let value_offset = pass.base.immediates_data.len().try_into().expect( "Ran out of immediate data space. Don't set 4gb of immediates per RenderBundle.", ); - pass.base.immediates_data.extend( data_slice .chunks_exact(wgt::IMMEDIATE_DATA_ALIGNMENT as usize) diff --git a/wgpu-core/src/command/compute.rs b/wgpu-core/src/command/compute.rs index 871bca98d..0ba412c31 100644 --- a/wgpu-core/src/command/compute.rs +++ b/wgpu-core/src/command/compute.rs @@ -178,12 +178,6 @@ pub enum ComputePassErrorInner { Bind(#[from] BindError), #[error(transparent)] ImmediateData(#[from] ImmediateUploadError), - #[error("Immediate data offset must be aligned to 4 bytes")] - ImmediateOffsetAlignment, - #[error("Immediate data size must be aligned to 4 bytes")] - ImmediateDataizeAlignment, - #[error("Ran out of immediate data space. Don't set 4gb of immediates per ComputePass.")] - ImmediateOutOfMemory, #[error(transparent)] QueryUse(#[from] QueryUseError), #[error(transparent)] @@ -256,9 +250,6 @@ impl WebGpuError for ComputePassError { | ComputePassErrorInner::BindGroupIndexOutOfRange { .. } | ComputePassErrorInner::UnalignedIndirectBufferOffset(_) | ComputePassErrorInner::IndirectBufferOverrun { .. } - | ComputePassErrorInner::ImmediateOffsetAlignment - | ComputePassErrorInner::ImmediateDataizeAlignment - | ComputePassErrorInner::ImmediateOutOfMemory | ComputePassErrorInner::PassEnded => ErrorType::Validation, } } @@ -1259,29 +1250,18 @@ impl Global { let scope = PassErrorScope::SetImmediate; let base = pass_base!(pass, scope); - if offset & (wgt::IMMEDIATE_DATA_ALIGNMENT - 1) != 0 { - pass_try!( - base, - scope, - Err(ComputePassErrorInner::ImmediateOffsetAlignment), - ); - } - - if data.len() as u32 & (wgt::IMMEDIATE_DATA_ALIGNMENT - 1) != 0 { - pass_try!( - base, - scope, - Err(ComputePassErrorInner::ImmediateDataizeAlignment), - ) - } - let value_offset = pass_try!( + let size_bytes = pass_try!( base, scope, - base.immediates_data - .len() - .try_into() - .map_err(|_| ComputePassErrorInner::ImmediateOutOfMemory) + 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) @@ -1291,7 +1271,7 @@ impl Global { base.commands.push(ArcComputeCommand::SetImmediate { offset, size_bytes: data.len() as u32, - values_offset: value_offset, + values_offset, }); Ok(()) diff --git a/wgpu-core/src/command/pass.rs b/wgpu-core/src/command/pass.rs index 22b52568e..e73f78618 100644 --- a/wgpu-core/src/command/pass.rs +++ b/wgpu-core/src/command/pass.rs @@ -217,6 +217,21 @@ where Ok(()) } +pub(crate) fn validate_immediates_alignment( + offset: u32, + size_bytes: u32, +) -> 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) { + return Err(ImmediateUploadError::SizeUnaligned(size_bytes)); + } + + Ok(()) +} + pub(crate) fn set_immediates( state: &mut PassState, immediates_data: &[u32], @@ -230,6 +245,8 @@ where { 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 @@ -243,24 +260,25 @@ where 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() { - return Err(ImmediateUploadError::ValueStartIndexOverrun { - start_index: values_offset, - data_size: immediates_data.len(), - } - .into()); + panic!( + "Internal error: `set_immediates` values offset ({}) \ + overruns the immediates data length ({})", + values_offset, + immediates_data.len() + ); } - // NOTE: The `validate_immediates_ranges` call above validates `size_bytes` is aligned. 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 { - return Err(ImmediateUploadError::ValueEndIndexOverrun { - start_index: values_offset, - count: size_immediate_elements, - data_size: immediates_data.len(), - } - .into()); + 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. diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index 19b06244b..fd5913bb2 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -961,12 +961,6 @@ pub enum RenderPassErrorInner { Draw(#[from] DrawError), #[error(transparent)] Bind(#[from] BindError), - #[error("Immediate data offset must be aligned to 4 bytes")] - ImmediateOffsetAlignment, - #[error("Immediate data size must be aligned to 4 bytes")] - ImmediateDataizeAlignment, - #[error("Ran out of immediate data space. Don't set 4gb of immediates per ComputePass.")] - ImmediateOutOfMemory, #[error(transparent)] QueryUse(#[from] QueryUseError), #[error("Multiview layer count must match")] @@ -1073,9 +1067,6 @@ impl WebGpuError for RenderPassError { | RenderPassErrorInner::IndirectCountBufferOverrun { .. } | RenderPassErrorInner::ResourceUsageCompatibility(..) | RenderPassErrorInner::IncompatibleBundleReadOnlyDepthStencil { .. } - | RenderPassErrorInner::ImmediateOffsetAlignment - | RenderPassErrorInner::ImmediateDataizeAlignment - | RenderPassErrorInner::ImmediateOutOfMemory | RenderPassErrorInner::MultiViewMismatch | RenderPassErrorInner::MultiViewDimensionMismatch | RenderPassErrorInner::TooManyMultiviewViews @@ -3617,29 +3608,18 @@ impl Global { let scope = PassErrorScope::SetImmediate; let base = pass_base!(pass, scope); - if offset & (wgt::IMMEDIATE_DATA_ALIGNMENT - 1) != 0 { - pass_try!( - base, - scope, - Err(RenderPassErrorInner::ImmediateOffsetAlignment) - ); - } - if data.len() as u32 & (wgt::IMMEDIATE_DATA_ALIGNMENT - 1) != 0 { - pass_try!( - base, - scope, - Err(RenderPassErrorInner::ImmediateDataizeAlignment) - ); - } - - let value_offset = pass_try!( + let size_bytes = pass_try!( base, scope, - base.immediates_data - .len() - .try_into() - .map_err(|_| RenderPassErrorInner::ImmediateOutOfMemory), + 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) @@ -3649,7 +3629,7 @@ impl Global { base.commands.push(ArcRenderCommand::SetImmediate { offset, size_bytes: data.len() as u32, - values_offset: Some(value_offset), + values_offset: Some(values_offset), }); Ok(()) diff --git a/wgpu/src/backend/wgpu_core.rs b/wgpu/src/backend/wgpu_core.rs index 927451afe..c894c5c19 100644 --- a/wgpu/src/backend/wgpu_core.rs +++ b/wgpu/src/backend/wgpu_core.rs @@ -3844,12 +3844,12 @@ impl dispatch::RenderBundleEncoderInterface for CoreRenderBundleEncoder { fn set_immediates(&mut self, offset: u32, data: &[u8]) { unsafe { - wgpu_render_bundle_set_immediates( + wgpu_core::command::bundle_ffi::wgpu_render_bundle_set_immediates( &mut self.encoder, offset, data.len().try_into().unwrap(), data.as_ptr(), - ) + ); } }