diff --git a/CHANGELOG.md b/CHANGELOG.md index 050305c89..881bb3f76 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,6 +54,7 @@ Bottom level categories: #### Metal - Unconditionally enable `Features::CLIP_DISTANCES`. By @ErichDonGubler in [#9270](https://github.com/gfx-rs/wgpu/pull/9270). +- Added full support for mesh shaders, including in WGSL shaders. By @inner-daemons in [#8739](https://github.com/gfx-rs/wgpu/pull/8739). #### DX12 @@ -68,6 +69,7 @@ Bottom level categories: #### General - `Features::CLIP_DISTANCE`, `naga::Capabilities::CLIP_DISTANCE`, and `naga::BuiltIn::ClipDistance` have been renamed to `CLIP_DISTANCES` and `ClipDistances` (viz., pluralized) as appropriate, to match the WebGPU spec. By @ErichDonGubler in [#9267](https://github.com/gfx-rs/wgpu/pull/9267). +- Added more granular limits for mesh shaders. By @inner-daemons in [#8739](https://github.com/gfx-rs/wgpu/pull/8739). - Added new `InvalidWorkgroupSizeError`, which is now used by `DrawError::InvalidGroupSize` and `StageError::InvalidWorkgroupSize`. By @andyleiserson in [#9357](https://github.com/gfx-rs/wgpu/pull/9357). #### Validation diff --git a/examples/features/src/mesh_shader/mod.rs b/examples/features/src/mesh_shader/mod.rs index a6ea95e59..c9bd085ec 100644 --- a/examples/features/src/mesh_shader/mod.rs +++ b/examples/features/src/mesh_shader/mod.rs @@ -12,17 +12,6 @@ fn compile_wgsl(device: &wgpu::Device) -> wgpu::ShaderModule { } } -fn compile_msl(device: &wgpu::Device) -> wgpu::ShaderModule { - unsafe { - device.create_shader_module_passthrough(wgpu::ShaderModuleDescriptorPassthrough { - label: None, - msl: Some(std::borrow::Cow::Borrowed(include_str!("shader.metal"))), - num_workgroups: (1, 1, 1), - ..Default::default() - }) - } -} - struct Shaders { ts: wgpu::ShaderModule, ms: wgpu::ShaderModule, @@ -32,33 +21,15 @@ struct Shaders { fs_name: &'static str, } -fn get_shaders(device: &wgpu::Device, backend: wgpu::Backend) -> Shaders { - // In the case that the platform does support mesh shaders, the dummy - // shader is used to avoid requiring PASSTHROUGH_SHADERS. - match backend { - wgpu::Backend::Vulkan | wgpu::Backend::Dx12 => { - let compiled = compile_wgsl(device); - Shaders { - ts: compiled.clone(), - ms: compiled.clone(), - fs: compiled.clone(), - ts_name: "ts_main", - ms_name: "ms_main", - fs_name: "fs_main", - } - } - wgpu::Backend::Metal => { - let compiled = compile_msl(device); - Shaders { - ts: compiled.clone(), - ms: compiled.clone(), - fs: compiled.clone(), - ts_name: "taskShader", - ms_name: "meshShader", - fs_name: "fragShader", - } - } - _ => unreachable!(), +fn get_shaders(device: &wgpu::Device) -> Shaders { + let compiled = compile_wgsl(device); + Shaders { + ts: compiled.clone(), + ms: compiled.clone(), + fs: compiled.clone(), + ts_name: "ts_main", + ms_name: "ms_main", + fs_name: "fs_main", } } @@ -68,7 +39,7 @@ pub struct Example { impl crate::framework::Example for Example { fn init( config: &wgpu::SurfaceConfiguration, - adapter: &wgpu::Adapter, + _adapter: &wgpu::Adapter, device: &wgpu::Device, _queue: &wgpu::Queue, ) -> Self { @@ -79,7 +50,7 @@ impl crate::framework::Example for Example { ts_name, ms_name, fs_name, - } = get_shaders(device, adapter.get_info().backend); + } = get_shaders(device); let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: None, bind_group_layouts: &[], diff --git a/examples/features/src/mesh_shader/shader.metal b/examples/features/src/mesh_shader/shader.metal deleted file mode 100644 index 4c7da5038..000000000 --- a/examples/features/src/mesh_shader/shader.metal +++ /dev/null @@ -1,77 +0,0 @@ -using namespace metal; - -struct OutVertex { - float4 Position [[position]]; - float4 Color [[user(locn0)]]; -}; - -struct OutPrimitive { - float4 ColorMask [[flat]] [[user(locn1)]]; - bool CullPrimitive [[primitive_culled]]; -}; - -struct InVertex { -}; - -struct InPrimitive { - float4 ColorMask [[flat]] [[user(locn1)]]; -}; - -struct FragmentIn { - float4 Color [[user(locn0)]]; - float4 ColorMask [[flat]] [[user(locn1)]]; -}; - -struct PayloadData { - float4 ColorMask; - bool Visible; -}; - -using Meshlet = metal::mesh; - - -constant float4 positions[3] = { - float4(0.0, 1.0, 0.0, 1.0), - float4(-1.0, -1.0, 0.0, 1.0), - float4(1.0, -1.0, 0.0, 1.0) -}; - -constant float4 colors[3] = { - float4(0.0, 1.0, 0.0, 1.0), - float4(0.0, 0.0, 1.0, 1.0), - float4(1.0, 0.0, 0.0, 1.0) -}; - - -[[object]] -void taskShader(uint3 tid [[thread_position_in_grid]], object_data PayloadData &outPayload [[payload]], mesh_grid_properties grid) { - outPayload.ColorMask = float4(1.0, 1.0, 0.0, 1.0); - outPayload.Visible = true; - grid.set_threadgroups_per_grid(uint3(3, 1, 1)); -} - -[[mesh]] -void meshShader( - object_data PayloadData const& payload [[payload]], - Meshlet out -) -{ - out.set_primitive_count(1); - - for(int i = 0;i < 3;i++) { - OutVertex vert; - vert.Position = positions[i]; - vert.Color = colors[i] * payload.ColorMask; - out.set_vertex(i, vert); - out.set_index(i, i); - } - - OutPrimitive prim; - prim.ColorMask = float4(1.0, 0.0, 0.0, 1.0); - prim.CullPrimitive = !payload.Visible; - out.set_primitive(0, prim); -} - -fragment float4 fragShader(FragmentIn data [[stage_in]]) { - return data.Color * data.ColorMask; -} diff --git a/naga-cli/src/bin/naga.rs b/naga-cli/src/bin/naga.rs index 73731a3b5..5d5383b21 100644 --- a/naga-cli/src/bin/naga.rs +++ b/naga-cli/src/bin/naga.rs @@ -575,6 +575,8 @@ fn run() -> anyhow::Result<()> { params.spv_out.mesh_shader_primitive_indices_clamp = args.validate_mesh_output; params.spv_out.task_dispatch_limits = args.task_limits.0; + params.msl.mesh_shader_primitive_indices_clamp = args.validate_mesh_output; + params.msl.task_dispatch_limits = args.task_limits.0; params.hlsl.mesh_shader_primitive_indices_clamp = args.validate_mesh_output; params.hlsl.task_dispatch_limits = args.task_limits.0; diff --git a/naga/src/back/msl/keywords.rs b/naga/src/back/msl/keywords.rs index f0ed9154d..0f35b424b 100644 --- a/naga/src/back/msl/keywords.rs +++ b/naga/src/back/msl/keywords.rs @@ -344,7 +344,7 @@ const RESERVED: &[&str] = &[ // Naga utilities "DefaultConstructible", // Naga builtin names - "__local_invocation_id", + "__local_invocation_index", super::writer::FREXP_FUNCTION, super::writer::MODF_FUNCTION, super::writer::ABS_FUNCTION, diff --git a/naga/src/back/msl/mesh_shader.rs b/naga/src/back/msl/mesh_shader.rs new file mode 100644 index 000000000..87a2b52ac --- /dev/null +++ b/naga/src/back/msl/mesh_shader.rs @@ -0,0 +1,442 @@ +use alloc::{ + format, + string::{String, ToString}, + vec::Vec, +}; + +use crate::{ + back::{ + self, + msl::{ + writer::{TypeContext, TypedGlobalVariable}, + BackendResult, EntryPointArgument, Error, NAMESPACE, WRAPPED_ARRAY_FIELD, + }, + }, + proc::NameKey, +}; + +pub(super) struct MeshOutputInfo { + out_vertex_ty_name: String, + out_primitive_ty_name: String, + out_vertex_member_names: Vec>, + out_primitive_member_names: Vec>, +} + +pub(super) struct NestedFunctionInfo<'a> { + pub(super) options: &'a super::Options, + pub(super) ep: &'a crate::EntryPoint, + pub(super) module: &'a crate::Module, + pub(super) mod_info: &'a crate::valid::ModuleInfo, + pub(super) fun_info: &'a crate::valid::FunctionInfo, + pub(super) args: Vec, + pub(super) local_invocation_index: Option<&'a NameKey>, + pub(super) nested_name: &'a str, + pub(super) outer_name: &'a str, + pub(super) out_mesh_info: Option, +} + +impl super::Writer { + /// This writes the output vertex and primitive structs given the reflection information about them. + pub(super) fn write_mesh_output_types( + &mut self, + mesh_info: &crate::MeshStageInfo, + fun_name: &str, + module: &crate::Module, + // See `PipelineOptions::allow_and_force_point_size` + allow_and_force_point_size: bool, + options: &super::Options, + ) -> Result { + let mut vertex_member_names = Vec::new(); + let mut primitive_member_names = Vec::new(); + let vertex_out_name = self.namer.call(&format!("{fun_name}VertexOutput")); + let primitive_out_name = self.namer.call(&format!("{fun_name}PrimitiveOutput")); + let mut existing_names = Vec::new(); + for (out_name, struct_ty, is_primitive, member_names) in [ + ( + &vertex_out_name, + mesh_info.vertex_output_type, + false, + &mut vertex_member_names, + ), + ( + &primitive_out_name, + mesh_info.primitive_output_type, + true, + &mut primitive_member_names, + ), + ] { + writeln!(self.out, "struct {out_name} {{")?; + // Mesh output types are guaranteed to be user defined structs. This is validated by naga. + let crate::TypeInner::Struct { ref members, .. } = module.types[struct_ty].inner else { + unreachable!() + }; + let mut has_point_size = false; + for (index, member) in members.iter().enumerate() { + member_names.push(None); + let ty_name = TypeContext { + handle: member.ty, + gctx: module.to_ctx(), + names: &self.names, + access: crate::StorageAccess::empty(), + first_time: true, + }; + let binding = member + .binding + .clone() + .ok_or_else(|| Error::GenericValidation("Expected binding, got None".into()))?; + + if let crate::Binding::BuiltIn(crate::BuiltIn::PointSize) = binding { + has_point_size = true; + if !allow_and_force_point_size { + continue; + } + } + if let crate::Binding::BuiltIn( + crate::BuiltIn::PointIndex + | crate::BuiltIn::LineIndices + | crate::BuiltIn::TriangleIndices, + ) = binding + { + continue; + } + + // Names of struct members must be unique across vertex and primitive output. + // Therefore, when writing the primitive output struct, we might need to rename some fields. + let mut name = self.names[&NameKey::StructMember(struct_ty, index as u32)].clone(); + if existing_names.contains(&name) { + name = self.namer.call(&name); + } else { + // Let the namer know this is illegal to use again + let _ = self.namer.call(&name); + } + + let array_len = match module.types[member.ty].inner { + crate::TypeInner::Array { + size: crate::ArraySize::Constant(size), + .. + } => Some(size), + _ => None, + }; + let resolved = + options.resolve_local_binding(&binding, back::msl::LocationMode::MeshOutput)?; + write!(self.out, "{}{} {}", back::INDENT, ty_name, name)?; + if let Some(array_len) = array_len { + write!(self.out, " [{array_len}]")?; + } + resolved.try_fmt(&mut self.out)?; + writeln!(self.out, ";")?; + *member_names.last_mut().unwrap() = Some(name.clone()); + existing_names.push(name); + } + if allow_and_force_point_size && !has_point_size && !is_primitive { + // inject the point size output last + writeln!( + self.out, + "{}float _point_size [[point_size]];", + back::INDENT + )?; + } + writeln!(self.out, "}};")?; + } + Ok(MeshOutputInfo { + out_vertex_ty_name: vertex_out_name, + out_primitive_ty_name: primitive_out_name, + out_vertex_member_names: vertex_member_names, + out_primitive_member_names: primitive_member_names, + }) + } + + pub(super) fn write_wrapper_function(&mut self, info: NestedFunctionInfo<'_>) -> BackendResult { + let NestedFunctionInfo { + options, + ep, + module, + mod_info, + fun_info, + args, + local_invocation_index: local_invocation_index_key, + nested_name, + outer_name, + out_mesh_info, + } = info; + let indent = back::INDENT; + + let em_str = match ep.stage { + crate::ShaderStage::Mesh => "[[mesh]]", + crate::ShaderStage::Task => "[[object]]", + _ => unreachable!(), + }; + writeln!(self.out, "{em_str} void {outer_name}(")?; + + // Arguments + + let mut mesh_out_name: Option = None; + let mut mesh_variable_name = None; + let mut task_grid_name = None; + if let Some(ref info) = ep.mesh_info { + let mesh_out = out_mesh_info.as_ref().unwrap(); + let mesh_name = self.namer.call("meshOutput"); + let topology_name = match info.topology { + crate::MeshOutputTopology::Points => "point", + crate::MeshOutputTopology::Lines => "line", + crate::MeshOutputTopology::Triangles => "triangle", + }; + let num_verts = info.max_vertices; + let num_prims = info.max_primitives; + writeln!(self.out, + " {NAMESPACE}::mesh<{}, {}, {num_verts}, {num_prims}, metal::topology::{topology_name}> {mesh_name}", + mesh_out.out_vertex_ty_name, + mesh_out.out_primitive_ty_name, + )?; + mesh_out_name = Some(mesh_name); + mesh_variable_name = Some( + self.names + [&NameKey::GlobalVariable(ep.mesh_info.as_ref().unwrap().output_variable)] + .clone(), + ); + } else if ep.stage == crate::ShaderStage::Task { + let grid_name = self.namer.call("nagaMeshGrid"); + writeln!(self.out, " {NAMESPACE}::mesh_grid_properties {grid_name}")?; + task_grid_name = Some(grid_name); + } + let local_invocation_index = if let Some(key) = local_invocation_index_key { + self.names[key].clone() + } else { + "__local_invocation_index".to_string() + }; + + for arg in &args { + write!(self.out, ", {} {}{}", arg.ty_name, arg.name, arg.binding)?; + if let Some(init) = arg.init { + write!(self.out, " = ")?; + self.put_const_expression(init, module, mod_info, &module.global_expressions)?; + } + writeln!(self.out)?; + } + + writeln!(self.out, ") {{")?; + + // Function body + if ep.stage == crate::ShaderStage::Mesh { + for (handle, var) in module.global_variables.iter() { + if var.space != crate::AddressSpace::WorkGroup || fun_info[handle].is_empty() { + continue; + } + let tyvar = TypedGlobalVariable { + module, + names: &self.names, + handle, + usage: crate::valid::GlobalUse::WRITE | crate::valid::GlobalUse::READ, + reference: false, + }; + write!(self.out, "{}", back::INDENT)?; + tyvar.try_fmt(&mut self.out)?; + writeln!(self.out, ";")?; + } + } + write!(self.out, "{indent}")?; + let result_name = if ep.stage == crate::ShaderStage::Task { + let name = self.namer.call("nagaGridSize"); + write!(self.out, "uint3 {} = ", name)?; + Some(name) + } else { + None + }; + write!(self.out, "{nested_name}(")?; + { + let mut is_first = true; + for arg in &args { + if !is_first { + write!(self.out, ", ")?; + } + is_first = false; + write!(self.out, "{}", arg.name)?; + } + if ep.stage == crate::ShaderStage::Mesh { + for (handle, var) in module.global_variables.iter() { + if var.space != crate::AddressSpace::WorkGroup || fun_info[handle].is_empty() { + continue; + } + if !is_first { + write!(self.out, ", ")?; + } + let name = &self.names[&NameKey::GlobalVariable(handle)]; + write!(self.out, "{name}")?; + } + } + } + writeln!(self.out, ");")?; + self.write_barrier( + crate::Barrier::WORK_GROUP, + back::Level(1), + info.options.lang_version, + )?; + + if let Some(grid_name) = task_grid_name { + let result_name = result_name.unwrap(); + writeln!(self.out, "{indent}if ({local_invocation_index} == 0u) {{")?; + { + let level2 = back::Level(2); + if let Some(limits) = options.task_dispatch_limits { + let level3 = back::Level(3); + let max_per_dim = limits.max_mesh_workgroups_per_dim; + let max_total = limits.max_mesh_workgroups_total; + writeln!(self.out, "{level2}if (")?; + + writeln!(self.out, "{level3}{result_name}.x > {max_per_dim}u ||")?; + writeln!(self.out, "{level3}{result_name}.y > {max_per_dim}u ||")?; + writeln!(self.out, "{level3}{result_name}.z > {max_per_dim}u ||")?; + writeln!( + self.out, + "{level3}{NAMESPACE}::mulhi({result_name}.x, {result_name}.y) != 0u ||" + )?; + writeln!( + self.out, + "{level3}{NAMESPACE}::mulhi({result_name}.x * {result_name}.y, {result_name}.z) != 0u ||" + )?; + writeln!(self.out, "{level3}({result_name}.x * {result_name}.y * {result_name}.z) > {max_total}u")?; + + writeln!(self.out, "{level2}) {{")?; + writeln!(self.out, "{level3}{result_name} = {NAMESPACE}::uint3(0u);")?; + writeln!(self.out, "{level2}}}")?; + } + writeln!( + self.out, + "{level2}{grid_name}.set_threadgroups_per_grid({result_name});" + )?; + } + writeln!(self.out, "{indent}}}")?; + writeln!(self.out, "{indent}return;")?; + } else if let Some(ref info) = ep.mesh_info { + let mesh_out = out_mesh_info.as_ref().unwrap(); + let out_ty = module.global_variables[info.output_variable].ty; + let mesh_out_name = mesh_out_name.unwrap(); + let mesh_variable_name = mesh_variable_name.unwrap(); + // The output type is guaranteed to be a struct with exactly 4 members + let crate::TypeInner::Struct { ref members, .. } = module.types[out_ty].inner else { + unreachable!(); + }; + let get_out_value = |bi| { + let member_idx = members + .iter() + .position(|a| a.binding == Some(crate::Binding::BuiltIn(bi))) + .unwrap() as u32; + format!( + "{}.{}", + mesh_variable_name, + self.names[&NameKey::StructMember(out_ty, member_idx)] + ) + }; + let vert_count = format!( + "{NAMESPACE}::min({}, {}u)", + get_out_value(crate::BuiltIn::VertexCount), + info.max_vertices + ); + let prim_count = format!( + "{NAMESPACE}::min({}, {}u)", + get_out_value(crate::BuiltIn::PrimitiveCount), + info.max_primitives + ); + let workgroup_size: u32 = ep.workgroup_size.iter().product(); + { + let vert_index = self.namer.call("vertexIndex"); + let in_array = get_out_value(crate::BuiltIn::Vertices); + writeln!( + self.out, + "{indent}for(uint {vert_index} = {local_invocation_index}; {vert_index} < {vert_count}; {vert_index} += {workgroup_size}) {{" + )?; + let out_vert = self.namer.call("vertex"); + writeln!( + self.out, + "{indent}{indent}{} {out_vert};", + mesh_out.out_vertex_ty_name, + )?; + for (member_idx, new_name) in mesh_out.out_vertex_member_names.iter().enumerate() { + let in_value = format!( + "{in_array}.{WRAPPED_ARRAY_FIELD}[{vert_index}].{}", + self.names + [&NameKey::StructMember(info.vertex_output_type, member_idx as u32)] + ); + let out_value = format!("{out_vert}.{}", new_name.as_ref().unwrap()); + writeln!(self.out, "{indent}{indent}{out_value} = {in_value};")?; + } + writeln!( + self.out, + "{indent}{indent}{}.set_vertex({vert_index}, {out_vert});", + mesh_out_name + )?; + writeln!(self.out, "{indent}}}")?; + } + { + let prim_index = self.namer.call("primitiveIndex"); + let in_array = get_out_value(crate::BuiltIn::Primitives); + writeln!( + self.out, + "{indent}for(uint {prim_index} = {local_invocation_index}; {prim_index} < {prim_count}; {prim_index} += {workgroup_size}) {{" + )?; + let out_prim = self.namer.call("primitive"); + writeln!( + self.out, + "{indent}{indent}{} {out_prim};", + mesh_out.out_primitive_ty_name + )?; + for (member_idx, new_name) in mesh_out.out_primitive_member_names.iter().enumerate() + { + let in_value = format!( + "{in_array}.{WRAPPED_ARRAY_FIELD}[{prim_index}].{}", + self.names + [&NameKey::StructMember(info.primitive_output_type, member_idx as u32)] + ); + if let Some(new_name) = new_name.as_ref() { + let out_value = format!("{out_prim}.{new_name}"); + writeln!( + self.out, + "{indent}{}{out_value} = {in_value};", + back::INDENT + )?; + } else { + let num_indices = match info.topology { + crate::MeshOutputTopology::Points => 1, + crate::MeshOutputTopology::Lines => 2, + crate::MeshOutputTopology::Triangles => 3, + }; + for i in 0..num_indices { + let component = if num_indices == 1 { + "".to_string() + } else { + format!(".{}", back::COMPONENTS[i]) + }; + writeln!( + self.out, + "{indent}{}{}.set_index({prim_index} * {num_indices} + {i}, {in_value}{component});", + back::INDENT, + mesh_out_name, + )?; + } + } + } + writeln!( + self.out, + "{indent}{}{}.set_primitive({prim_index}, {out_prim});", + back::INDENT, + mesh_out_name + )?; + writeln!(self.out, "{indent}}}")?; + } + + writeln!(self.out, "{indent}if ({local_invocation_index} == 0u) {{")?; + writeln!( + self.out, + "{indent}{indent}{}.set_primitive_count({prim_count});", + mesh_out_name, + )?; + writeln!(self.out, "{indent}}}")?; + } else { + // Must either have task output grid (task shader) or mesh output info (mesh shader) + unreachable!() + } + + writeln!(self.out, "}}")?; + Ok(()) + } +} diff --git a/naga/src/back/msl/mod.rs b/naga/src/back/msl/mod.rs index 9541f4800..54f755941 100644 --- a/naga/src/back/msl/mod.rs +++ b/naga/src/back/msl/mod.rs @@ -75,9 +75,10 @@ use alloc::{ }; use core::fmt::{Error as FmtError, Write}; -use crate::{arena::Handle, ir, proc::index, valid::ModuleInfo}; +use crate::{arena::Handle, back::TaskDispatchLimits, ir, proc::index, valid::ModuleInfo}; mod keywords; +mod mesh_shader; pub mod sampler; mod writer; @@ -178,6 +179,7 @@ enum ResolvedBinding { interpolation: Option, }, Resource(BindTarget), + Payload, } #[derive(Copy, Clone)] @@ -240,6 +242,8 @@ pub enum Error { ResolveArraySizeError(#[from] crate::proc::ResolveArraySizeError), #[error("entry point with stage {0:?} and name '{1}' not found")] EntryPointNotFound(ir::ShaderStage, String), + #[error("Cannot use mesh shader syntax prior to MSL 3.0")] + UnsupportedMeshShader, #[error("Per vertex fragment inputs are not supported prior to MSL 4.0")] PerVertexNotSupported, } @@ -280,6 +284,9 @@ enum LocationMode { /// Output from the fragment shader. FragmentOutput, + /// Output from the mesh shader. + MeshOutput, + /// Compute shader input or output. Uniform, } @@ -306,6 +313,11 @@ pub struct Options { /// If set, loops will have code injected into them, forcing the compiler /// to think the number of iterations is bounded. pub force_loop_bounding: bool, + /// Whether and how checks in the task shader should verify the dispatched + /// mesh grid size. + pub task_dispatch_limits: Option, + /// Whether to validate the output of a mesh shader workgroup. + pub mesh_shader_primitive_indices_clamp: bool, } impl Default for Options { @@ -319,6 +331,8 @@ impl Default for Options { bounds_check_policies: index::BoundsCheckPolicies::default(), zero_initialize_workgroup_memory: true, force_loop_bounding: true, + task_dispatch_limits: None, + mesh_shader_primitive_indices_clamp: true, } } } @@ -487,7 +501,7 @@ pub struct PipelineOptions { /// Metal doesn't like this for non-point primitive topologies and requires it for /// point primitive topologies. /// - /// Enable this for vertex shaders with point primitive topologies. + /// Enable this for vertex/mesh shaders with point primitive topologies. pub allow_and_force_point_size: bool, /// If set, when generating the Metal vertex shader, transform it @@ -556,7 +570,7 @@ impl Options { interpolation, sampling, blend_src, - per_primitive: _, + per_primitive, } => match mode { LocationMode::VertexInput => Ok(ResolvedBinding::Attribute(location)), LocationMode::FragmentOutput => { @@ -568,7 +582,9 @@ impl Options { blend_src, }) } - LocationMode::VertexOutput | LocationMode::FragmentInput => { + LocationMode::VertexOutput + | LocationMode::FragmentInput + | LocationMode::MeshOutput => { Ok(ResolvedBinding::User { prefix: if self.spirv_cross_compatibility { "locn" @@ -582,7 +598,11 @@ impl Options { // sampling is `None` only for Flat interpolation. let interpolation = interpolation.unwrap(); let sampling = sampling.unwrap_or(crate::Sampling::Center); - Some(ResolvedInterpolation::from_binding(interpolation, sampling)) + Some(ResolvedInterpolation::from_binding( + interpolation, + sampling, + per_primitive, + )) }, }) } @@ -722,6 +742,8 @@ impl ResolvedBinding { Bi::CullPrimitive => "primitive_culled", // TODO: figure out how to make this written as a function call Bi::PointIndex | Bi::LineIndices | Bi::TriangleIndices => unimplemented!(), + // These aren't real builtins passed into MSL. They are extracted by the + // wrapper function which actually sets the outputs. Bi::MeshTaskSize | Bi::VertexCount | Bi::PrimitiveCount @@ -776,6 +798,7 @@ impl ResolvedBinding { return Err(Error::UnimplementedBindTarget(target.clone())); } } + Self::Payload => write!(out, "payload")?, } write!(out, "]]")?; Ok(()) @@ -783,10 +806,18 @@ impl ResolvedBinding { } impl ResolvedInterpolation { - const fn from_binding(interpolation: crate::Interpolation, sampling: crate::Sampling) -> Self { + const fn from_binding( + interpolation: crate::Interpolation, + sampling: crate::Sampling, + per_primitive: bool, + ) -> Self { use crate::Interpolation as I; use crate::Sampling as S; + if per_primitive { + return Self::Flat; + } + match (interpolation, sampling) { (I::Perspective, S::Center) => Self::CenterPerspective, (I::Perspective, S::Centroid) => Self::CentroidPerspective, @@ -816,6 +847,23 @@ impl ResolvedInterpolation { } } +struct EntryPointArgument { + ty_name: String, + name: String, + binding: String, + init: Option>, +} + +/// Shorthand result used internally by the backend +type BackendResult = Result<(), Error>; + +const NAMESPACE: &str = "metal"; + +// The name of the array member of the Metal struct types we generate to +// represent Naga `Array` types. See the comments in `Writer::write_type_defs` +// for details. +const WRAPPED_ARRAY_FIELD: &str = "inner"; + /// Information about a translated module that is required /// for the use of the result. pub struct TranslationInfo { @@ -869,8 +917,8 @@ pub fn supported_capabilities() -> crate::valid::Capabilities { | Caps::TEXTURE_EXTERNAL | Caps::SHADER_FLOAT16_IN_FLOAT32 | Caps::SHADER_BARYCENTRICS - // No MESH_SHADER - // No MESH_SHADER_POINT_TOPOLOGY + | Caps::MESH_SHADER + | Caps::MESH_SHADER_POINT_TOPOLOGY | Caps::TEXTURE_AND_SAMPLER_BINDING_ARRAY_NON_UNIFORM_INDEXING // No BUFFER_BINDING_ARRAY_NON_UNIFORM_INDEXING | Caps::STORAGE_TEXTURE_BINDING_ARRAY_NON_UNIFORM_INDEXING diff --git a/naga/src/back/msl/writer.rs b/naga/src/back/msl/writer.rs index bfb2c6aa9..f855456ed 100644 --- a/naga/src/back/msl/writer.rs +++ b/naga/src/back/msl/writer.rs @@ -13,10 +13,17 @@ use num_traits::real::Real as _; use half::f16; -use super::{sampler as sm, Error, LocationMode, Options, PipelineOptions, TranslationInfo}; +use super::{ + sampler as sm, Error, LocationMode, Options, PipelineOptions, TranslationInfo, NAMESPACE, + WRAPPED_ARRAY_FIELD, +}; use crate::{ arena::{Handle, HandleSet}, - back::{self, get_entry_points, Baked}, + back::{ + self, get_entry_points, + msl::{mesh_shader::NestedFunctionInfo, BackendResult, EntryPointArgument}, + Baked, + }, common, proc::{ self, concrete_int_scalars, @@ -29,14 +36,6 @@ use crate::{ #[cfg(test)] use core::ptr; -/// Shorthand result used internally by the backend -type BackendResult = Result<(), Error>; - -const NAMESPACE: &str = "metal"; -// The name of the array member of the Metal struct types we generate to -// represent Naga `Array` types. See the comments in `Writer::write_type_defs` -// for details. -const WRAPPED_ARRAY_FIELD: &str = "inner"; // This is a hack: we need to pass a pointer to an atomic, // but generally the backend isn't putting "&" in front of every pointer. // Some more general handling of pointers is needed to be implemented here. @@ -195,12 +194,12 @@ impl Display for Reinterpreted<'_> { } } -struct TypeContext<'a> { - handle: Handle, - gctx: proc::GlobalCtx<'a>, - names: &'a FastHashMap, - access: crate::StorageAccess, - first_time: bool, +pub(super) struct TypeContext<'a> { + pub handle: Handle, + pub gctx: proc::GlobalCtx<'a>, + pub names: &'a FastHashMap, + pub access: crate::StorageAccess, + pub first_time: bool, } impl TypeContext<'_> { @@ -397,16 +396,21 @@ impl Display for TypeContext<'_> { } } -struct TypedGlobalVariable<'a> { - module: &'a crate::Module, - names: &'a FastHashMap, - handle: Handle, - usage: valid::GlobalUse, - reference: bool, +pub(super) struct TypedGlobalVariable<'a> { + pub module: &'a crate::Module, + pub names: &'a FastHashMap, + pub handle: Handle, + pub usage: valid::GlobalUse, + pub reference: bool, +} + +struct TypedGlobalVariableParts { + ty_name: String, + var_name: String, } impl TypedGlobalVariable<'_> { - fn try_fmt(&self, out: &mut W) -> BackendResult { + fn to_parts(&self) -> Result { let var = &self.module.global_variables[self.handle]; let name = &self.names[&NameKey::GlobalVariable(self.handle)]; @@ -437,8 +441,18 @@ impl TypedGlobalVariable<'_> { first_time: false, }; - let (coherent, space, access, reference) = match var.space.to_msl_name() { - Some(space) if self.reference => { + let access = if var.space.needs_access_qualifier() + && !self.usage.intersects(valid::GlobalUse::WRITE) + { + "const" + } else { + "" + }; + let (coherent, space, access, reference) = match (var.space.to_msl_name(), var.space) { + (Some(space), crate::AddressSpace::WorkGroup) => { + ("", space, access, if self.reference { "&" } else { "" }) + } + (Some(space), _) if self.reference => { let coherent = if var .memory_decorations .contains(crate::MemoryDecorations::COHERENT) @@ -447,35 +461,31 @@ impl TypedGlobalVariable<'_> { } else { "" }; - let access = if var.space.needs_access_qualifier() - && !self.usage.intersects(valid::GlobalUse::WRITE) - { - "const" - } else { - "" - }; (coherent, space, access, "&") } _ => ("", "", "", ""), }; - Ok(write!( - out, - "{}{}{}{}{}{}{} {}", - coherent, - space, + let ty = format!( + "{coherent}{space}{}{ty_name}{}{access}{reference}", if space.is_empty() { "" } else { " " }, - ty_name, if access.is_empty() { "" } else { " " }, - access, - reference, - name, - )?) + ); + + Ok(TypedGlobalVariableParts { + ty_name: ty, + var_name: name.clone(), + }) + } + pub(super) fn try_fmt(&self, out: &mut W) -> BackendResult { + let parts = self.to_parts()?; + + Ok(write!(out, "{} {}", parts.ty_name, parts.var_name)?) } } #[derive(Eq, PartialEq, Hash)] -enum WrappedFunction { +pub(super) enum WrappedFunction { UnaryOp { op: crate::UnaryOperator, ty: (Option, crate::Scalar), @@ -520,20 +530,20 @@ enum WrappedFunction { } pub struct Writer { - out: W, - names: FastHashMap, - named_expressions: crate::NamedExpressions, + pub(super) out: W, + pub(super) names: FastHashMap, + pub(super) named_expressions: crate::NamedExpressions, /// Set of expressions that need to be baked to avoid unnecessary repetition in output - need_bake_expressions: back::NeedBakeExpressions, - namer: proc::Namer, - wrapped_functions: FastHashSet, + pub(super) need_bake_expressions: back::NeedBakeExpressions, + pub(super) namer: proc::Namer, + pub(super) wrapped_functions: FastHashSet, #[cfg(test)] - put_expression_stack_pointers: FastHashSet<*const ()>, + pub(super) put_expression_stack_pointers: FastHashSet<*const ()>, #[cfg(test)] - put_block_stack_pointers: FastHashSet<*const ()>, + pub(super) put_block_stack_pointers: FastHashSet<*const ()>, /// Set of (struct type, struct field index) denoting which fields require /// padding inserted **before** them (i.e. between fields at index - 1 and index) - struct_member_pads: FastHashSet<(Handle, u32)>, + pub(super) struct_member_pads: FastHashSet<(Handle, u32)>, } impl crate::Scalar { @@ -658,7 +668,8 @@ impl crate::AddressSpace { // may end up with "const" even if the binding is read-write, // and that should be OK. Self::Storage { .. } => true, - Self::TaskPayload | Self::RayPayload | Self::IncomingRayPayload => unimplemented!(), + Self::TaskPayload => true, + Self::RayPayload | Self::IncomingRayPayload => unimplemented!(), // These should always be read-write. Self::Private | Self::WorkGroup => false, // These translate to `constant` address space, no need for qualifiers. @@ -769,7 +780,7 @@ struct TexelAddress { level: Option, } -struct ExpressionContext<'a> { +pub(super) struct ExpressionContext<'a> { function: &'a crate::Function, origin: FunctionOrigin, info: &'a valid::FunctionInfo, @@ -1738,7 +1749,7 @@ impl Writer { Ok(()) } - fn put_const_expression( + pub(super) fn put_const_expression( &mut self, expr_handle: Handle, module: &crate::Module, @@ -1952,7 +1963,7 @@ impl Writer { /// /// - Pass `false` if it is an operand of a `?:` operator, a `[]`, or really /// almost anything else. - fn put_expression( + pub(super) fn put_expression( &mut self, expr_handle: Handle, context: &ExpressionContext, @@ -3916,7 +3927,7 @@ impl Writer { } crate::Statement::ControlBarrier(flags) | crate::Statement::MemoryBarrier(flags) => { - self.write_barrier(flags, level)?; + self.write_barrier(flags, level, context.expression.lang_version)?; } crate::Statement::Store { pointer, value } => { self.put_store(pointer, value, level, context)? @@ -4068,7 +4079,11 @@ impl Writer { self.put_image_atomic(level, image, &address, fun, value, context)? } crate::Statement::WorkGroupUniformLoad { pointer, result } => { - self.write_barrier(crate::Barrier::WORK_GROUP, level)?; + self.write_barrier( + crate::Barrier::WORK_GROUP, + level, + context.expression.lang_version, + )?; write!(self.out, "{level}")?; let name = self.namer.call(""); @@ -4077,7 +4092,11 @@ impl Writer { self.named_expressions.insert(result, name); writeln!(self.out, ";")?; - self.write_barrier(crate::Barrier::WORK_GROUP, level)?; + self.write_barrier( + crate::Barrier::WORK_GROUP, + level, + context.expression.lang_version, + )?; } crate::Statement::RayQuery { query, ref fun } => { if context.expression.lang_version < (2, 4) { @@ -4459,6 +4478,10 @@ impl Writer { // Work around Metal bug where `uint` is not available by default writeln!(self.out, "using {NAMESPACE}::uint;")?; + if module.uses_mesh_shaders() && options.lang_version < (3, 0) { + return Err(Error::UnsupportedMeshShader); + } + let mut uses_ray_query = false; for (_, ty) in module.types.iter() { match ty.inner { @@ -6943,24 +6966,29 @@ template let (em_str, in_mode, out_mode, can_vertex_pull) = match ep.stage { crate::ShaderStage::Vertex => ( - "vertex", + Some("vertex"), LocationMode::VertexInput, LocationMode::VertexOutput, true, ), crate::ShaderStage::Fragment => ( - "fragment", + Some("fragment"), LocationMode::FragmentInput, LocationMode::FragmentOutput, false, ), crate::ShaderStage::Compute => ( - "kernel", + Some("kernel"), LocationMode::Uniform, LocationMode::Uniform, false, ), - crate::ShaderStage::Task | crate::ShaderStage::Mesh => unimplemented!(), + crate::ShaderStage::Task => { + (None, LocationMode::Uniform, LocationMode::Uniform, false) + } + crate::ShaderStage::Mesh => { + (None, LocationMode::Uniform, LocationMode::MeshOutput, false) + } crate::ShaderStage::RayGeneration | crate::ShaderStage::AnyHit | crate::ShaderStage::ClosestHit @@ -7031,12 +7059,10 @@ template break; } } - crate::AddressSpace::TaskPayload => { - unimplemented!() - } crate::AddressSpace::Function | crate::AddressSpace::Private - | crate::AddressSpace::WorkGroup => {} + | crate::AddressSpace::WorkGroup + | crate::AddressSpace::TaskPayload => {} crate::AddressSpace::RayPayload | crate::AddressSpace::IncomingRayPayload => unimplemented!(), } @@ -7052,7 +7078,7 @@ template info.entry_point_names.push(Err(err)); continue; } - let fun_name = &self.names[&NameKey::EntryPoint(ep_index as _)]; + let fun_name = self.names[&NameKey::EntryPoint(ep_index as _)].clone(); info.entry_point_names.push(Ok(fun_name.clone())); writeln!(self.out)?; @@ -7184,7 +7210,7 @@ template let stage_out_name = self.namer.call(&format!("{fun_name}Output")); let result_member_name = self.namer.call("member"); let result_type_name = match fun.result { - Some(ref result) => { + Some(ref result) if ep.stage != crate::ShaderStage::Task => { let mut result_members = Vec::new(); if let crate::TypeInner::Struct { ref members, .. } = module.types[result.ty].inner @@ -7255,7 +7281,30 @@ template writeln!(self.out, "}};")?; &stage_out_name } - None => "void", + Some(ref result) if ep.stage == crate::ShaderStage::Task => { + assert_eq!( + module.types[result.ty].inner, + crate::TypeInner::Vector { + size: crate::VectorSize::Tri, + scalar: crate::Scalar::U32 + } + ); + + "metal::uint3" + } + _ => "void", + }; + + let out_mesh_info = if let Some(ref mesh_info) = ep.mesh_info { + Some(self.write_mesh_output_types( + mesh_info, + &fun_name, + module, + pipeline_options.allow_and_force_point_size, + options, + )?) + } else { + None }; // If we're doing a vertex pulling transform, define the buffer @@ -7275,6 +7324,17 @@ template } } + let is_wrapped = matches!( + ep.stage, + crate::ShaderStage::Task | crate::ShaderStage::Mesh + ); + let fun_name = fun_name.clone(); + let nested_fun_name = if is_wrapped { + self.namer.call(&format!("_{fun_name}")) + } else { + fun_name.clone() + }; + // https://web.archive.org/web/20181029003926/https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf if ep.stage == crate::ShaderStage::Compute && options.lang_version >= (2, 1) { let total_threads = @@ -7284,30 +7344,27 @@ template "[[max_total_threads_per_threadgroup({total_threads})]] " )?; } - // Write the entry point function's name, and begin its argument list. - writeln!(self.out, "{em_str} {result_type_name} {fun_name}(")?; - let mut is_first_argument = true; - let mut separator = || { - if is_first_argument { - is_first_argument = false; - ' ' - } else { - ',' - } - }; + // Write the entry point function's name, and begin its argument list. + if let Some(em_str) = em_str { + write!(self.out, "{em_str} ")?; + } + writeln!(self.out, "{result_type_name} {nested_fun_name}(")?; + + let mut args = Vec::new(); // If we have produced a struct holding the `EntryPoint`'s // `Function`'s arguments' varyings, pass that struct first. if has_varyings { - writeln!( - self.out, - "{} {stage_in_name} {varyings_member_name} [[stage_in]]", - separator() - )?; + args.push(EntryPointArgument { + ty_name: stage_in_name, + name: varyings_member_name.clone(), + binding: " [[stage_in]]".to_string(), + init: None, + }); } - let mut local_invocation_id = None; + let mut local_invocation_index = None; // Then pass the remaining arguments not included in the varyings // struct. @@ -7322,8 +7379,8 @@ template _ => &self.names[name_key], }; - if binding == &crate::Binding::BuiltIn(crate::BuiltIn::LocalInvocationId) { - local_invocation_id = Some(name_key); + if binding == &crate::Binding::BuiltIn(crate::BuiltIn::LocalInvocationIndex) { + local_invocation_index = Some(name_key); } let ty_name = TypeContext { @@ -7345,19 +7402,31 @@ template }; let resolved = options.resolve_local_binding(binding, in_mode)?; - write!(self.out, "{} {ty_name} {name}", separator())?; - resolved.try_fmt(&mut self.out)?; - writeln!(self.out)?; + let mut binding = String::new(); + resolved.try_fmt(&mut binding)?; + + args.push(EntryPointArgument { + ty_name: format!("{ty_name}"), + name: name.clone(), + binding, + init: None, + }); } let need_workgroup_variables_initialization = self.need_workgroup_variables_initialization(options, ep, module, fun_info); - if need_workgroup_variables_initialization && local_invocation_id.is_none() { - writeln!( - self.out, - "{} {NAMESPACE}::uint3 __local_invocation_id [[thread_position_in_threadgroup]]", separator() - )?; + if local_invocation_index.is_none() + && (need_workgroup_variables_initialization + || ep.stage == crate::ShaderStage::Task + || ep.stage == crate::ShaderStage::Mesh) + { + args.push(EntryPointArgument { + ty_name: "uint".to_string(), + name: "__local_invocation_index".to_string(), + binding: " [[thread_index_in_threadgroup]]".to_string(), + init: None, + }); } // Those global variables used by this entry point and its callees @@ -7494,6 +7563,7 @@ template let resolved = match var.space { crate::AddressSpace::Immediate => options.resolve_immediates(ep).ok(), crate::AddressSpace::WorkGroup => None, + crate::AddressSpace::TaskPayload => Some(back::msl::ResolvedBinding::Payload), _ => options .resolve_resource_binding(ep, var.binding.as_ref().unwrap()) .ok(), @@ -7521,20 +7591,25 @@ template }; for i in 0..3 { - write!(self.out, "{} ", separator())?; - let plane_name = &self.names[&NameKey::ExternalTextureGlobalVariable( handle, ExternalTextureNameKey::Plane(i), )]; - write!( - self.out, - "{NAMESPACE}::texture2d {plane_name}" - )?; - if let Some(ref target) = target { - write!(self.out, " [[texture({})]]", target.planes[i])?; - } - writeln!(self.out)?; + let ty_name = format!( + "{NAMESPACE}::texture2d" + ); + let name = plane_name.clone(); + let binding = if let Some(ref target) = target { + format!(" [[texture({})]]", target.planes[i]) + } else { + String::new() + }; + args.push(EntryPointArgument { + ty_name, + name, + binding, + init: None, + }); } let params_ty_name = &self.names [&NameKey::Type(module.special_types.external_texture_params.unwrap())]; @@ -7542,13 +7617,25 @@ template handle, ExternalTextureNameKey::Params, )]; - write!(self.out, "{} ", separator())?; - write!(self.out, "constant {params_ty_name}& {params_name}")?; - if let Some(ref target) = target { - write!(self.out, " [[buffer({})]]", target.params)?; - } + let binding = if let Some(ref target) = target { + format!(" [[buffer({})]]", target.params) + } else { + String::new() + }; + + args.push(EntryPointArgument { + ty_name: format!("constant {params_ty_name}&"), + name: params_name.clone(), + binding, + init: None, + }); } _ => { + if var.space == crate::AddressSpace::WorkGroup + && ep.stage == crate::ShaderStage::Mesh + { + continue; + } let tyvar = TypedGlobalVariable { module, names: &self.names, @@ -7556,33 +7643,39 @@ template usage, reference: true, }; - write!(self.out, "{} ", separator())?; - tyvar.try_fmt(&mut self.out)?; + let parts = tyvar.to_parts()?; + let mut binding = String::new(); if let Some(resolved) = resolved { - resolved.try_fmt(&mut self.out)?; - } - if let Some(value) = var.init { - write!(self.out, " = ")?; - self.put_const_expression( - value, - module, - mod_info, - &module.global_expressions, - )?; + resolved.try_fmt(&mut binding)?; } + args.push(EntryPointArgument { + ty_name: parts.ty_name, + name: parts.var_name, + binding, + init: var.init, + }); } } - writeln!(self.out)?; } if do_vertex_pulling { if needs_vertex_id && v_existing_id.is_none() { // Write the [[vertex_id]] argument. - writeln!(self.out, "{} uint {v_id} [[vertex_id]]", separator())?; + args.push(EntryPointArgument { + ty_name: "uint".to_string(), + name: v_id.clone(), + binding: " [[vertex_id]]".to_string(), + init: None, + }); } if needs_instance_id && i_existing_id.is_none() { - writeln!(self.out, "{} uint {i_id} [[instance_id]]", separator())?; + args.push(EntryPointArgument { + ty_name: "uint".to_string(), + name: i_id.clone(), + binding: " [[instance_id]]".to_string(), + init: None, + }); } // Iterate vbm_resolved, output one argument for every vertex buffer, @@ -7591,11 +7684,12 @@ template let id = &vbm.id; let ty_name = &vbm.ty_name; let param_name = &vbm.param_name; - writeln!( - self.out, - "{} const device {ty_name}* {param_name} [[buffer({id})]]", - separator() - )?; + args.push(EntryPointArgument { + ty_name: format!("const device {ty_name}*"), + name: param_name.clone(), + binding: format!(" [[buffer({id})]]"), + init: None, + }); } } @@ -7604,14 +7698,63 @@ template if needs_buffer_sizes { // this is checked earlier let resolved = options.resolve_sizes_buffer(ep).unwrap(); - write!( - self.out, - "{} constant _mslBufferSizes& _buffer_sizes", - separator() - )?; - resolved.try_fmt(&mut self.out)?; + let mut binding = String::new(); + resolved.try_fmt(&mut binding)?; + args.push(EntryPointArgument { + ty_name: "constant _mslBufferSizes&".to_string(), + name: "_buffer_sizes".to_string(), + binding, + init: None, + }); + } + + let mut is_first_arg = true; + for arg in &args { + if is_first_arg { + write!(self.out, " ")?; + } else { + write!(self.out, ", ")?; + } + is_first_arg = false; + write!(self.out, "{} {}", arg.ty_name, arg.name)?; + if !is_wrapped { + write!(self.out, "{}", arg.binding)?; + if let Some(init) = arg.init { + write!(self.out, " = ")?; + self.put_const_expression( + init, + module, + mod_info, + &module.global_expressions, + )?; + } + } writeln!(self.out)?; } + if ep.stage == crate::ShaderStage::Mesh { + for (handle, var) in module.global_variables.iter() { + if var.space != crate::AddressSpace::WorkGroup || fun_info[handle].is_empty() { + continue; + } + if is_first_arg { + write!(self.out, " ")?; + } else { + write!(self.out, ", ")?; + } + let ty_context = TypeContext { + handle: module.global_variables[handle].ty, + gctx: module.to_ctx(), + names: &self.names, + access: crate::StorageAccess::empty(), + first_time: false, + }; + writeln!( + self.out, + "threadgroup {ty_context}& {}", + self.names[&NameKey::GlobalVariable(handle)] + )?; + } + } // end of the entry point argument list writeln!(self.out, ") {{")?; @@ -7782,15 +7925,6 @@ template } } - if need_workgroup_variables_initialization { - self.write_workgroup_variables_initialization( - module, - mod_info, - fun_info, - local_invocation_id, - )?; - } - // Metal doesn't support private mutable variables outside of functions, // so we put them here, just like the locals. for (handle, var) in module.global_variables.iter() { @@ -7869,6 +8003,17 @@ template } } + if need_workgroup_variables_initialization { + self.write_workgroup_variables_initialization( + module, + mod_info, + fun_info, + local_invocation_index, + ep.stage, + options.lang_version, + )?; + } + // Now take the arguments that we gathered into structs, and the // structs that we flattened into arguments, and emit local // variables with initializers that put everything back the way the @@ -7984,7 +8129,11 @@ template pipeline_options, force_loop_bounding: options.force_loop_bounding, }, - result_struct: Some(&stage_out_name), + result_struct: if ep.stage == crate::ShaderStage::Task { + None + } else { + Some(&stage_out_name) + }, }; // Finally, declare all the local variables that we need @@ -7997,12 +8146,32 @@ template writeln!(self.out)?; } self.named_expressions.clear(); + + if is_wrapped { + self.write_wrapper_function(NestedFunctionInfo { + options, + ep, + module, + mod_info, + fun_info, + args, + local_invocation_index, + nested_name: &nested_fun_name, + outer_name: &fun_name, + out_mesh_info, + })?; + } } Ok(info) } - fn write_barrier(&mut self, flags: crate::Barrier, level: back::Level) -> BackendResult { + pub(super) fn write_barrier( + &mut self, + flags: crate::Barrier, + level: back::Level, + lang_version: (u8, u8), + ) -> BackendResult { // Note: OR-ring bitflags requires `__HAVE_MEMFLAG_OPERATORS__`, // so we try to avoid it here. if flags.is_empty() { @@ -8022,6 +8191,12 @@ template self.out, "{level}{NAMESPACE}::threadgroup_barrier({NAMESPACE}::mem_flags::mem_threadgroup);", )?; + if lang_version >= (3, 0) { + writeln!( + self.out, + "{level}{NAMESPACE}::threadgroup_barrier({NAMESPACE}::mem_flags::mem_object_data);", + )?; + } } if flags.contains(crate::Barrier::SUB_GROUP) { writeln!( @@ -8120,37 +8295,43 @@ mod workgroup_mem_init { module: &crate::Module, fun_info: &valid::FunctionInfo, ) -> bool { + let is_task = ep.stage == crate::ShaderStage::Task; options.zero_initialize_workgroup_memory && ep.stage.compute_like() && module.global_variables.iter().any(|(handle, var)| { - !fun_info[handle].is_empty() && var.space == crate::AddressSpace::WorkGroup + let is_right_address_space = var.space == crate::AddressSpace::WorkGroup + || (var.space == crate::AddressSpace::TaskPayload && is_task); + !fun_info[handle].is_empty() && is_right_address_space }) } - pub(super) fn write_workgroup_variables_initialization( + pub fn write_workgroup_variables_initialization( &mut self, module: &crate::Module, module_info: &valid::ModuleInfo, fun_info: &valid::FunctionInfo, - local_invocation_id: Option<&NameKey>, + local_invocation_index: Option<&NameKey>, + stage: crate::ShaderStage, + lang_version: (u8, u8), ) -> BackendResult { let level = back::Level(1); writeln!( self.out, - "{}if ({}::all({} == {}::uint3(0u))) {{", + "{}if ({} == 0u) {{", level, - NAMESPACE, - local_invocation_id + local_invocation_index .map(|name_key| self.names[name_key].as_str()) - .unwrap_or("__local_invocation_id"), - NAMESPACE, + .unwrap_or("__local_invocation_index"), )?; let mut access_stack = AccessStack::new(); + let is_task = stage == crate::ShaderStage::Task; let vars = module.global_variables.iter().filter(|&(handle, var)| { - !fun_info[handle].is_empty() && var.space == crate::AddressSpace::WorkGroup + let is_right_address_space = var.space == crate::AddressSpace::WorkGroup + || (var.space == crate::AddressSpace::TaskPayload && is_task); + !fun_info[handle].is_empty() && is_right_address_space }); for (handle, var) in vars { @@ -8166,7 +8347,7 @@ mod workgroup_mem_init { } writeln!(self.out, "{level}}}")?; - self.write_barrier(crate::Barrier::WORK_GROUP, level) + self.write_barrier(crate::Barrier::WORK_GROUP, level, lang_version) } fn write_workgroup_variable_initialization( diff --git a/naga/src/ir/mod.rs b/naga/src/ir/mod.rs index a1ff7b0d7..7cfe810e4 100644 --- a/naga/src/ir/mod.rs +++ b/naga/src/ir/mod.rs @@ -1579,7 +1579,7 @@ bitflags::bitflags! { pub struct Barrier: u32 { /// Barrier affects all [`AddressSpace::Storage`] accesses. const STORAGE = 1 << 0; - /// Barrier affects all [`AddressSpace::WorkGroup`] accesses. + /// Barrier affects all [`AddressSpace::WorkGroup`] and [`AddressSpace::TaskPayload`] accesses. const WORK_GROUP = 1 << 1; /// Barrier synchronizes execution across all invocations within a subgroup that execute this instruction. const SUB_GROUP = 1 << 2; diff --git a/naga/tests/in/wgsl/mesh-shader-empty.toml b/naga/tests/in/wgsl/mesh-shader-empty.toml index 24e00ffee..45ccfecbd 100644 --- a/naga/tests/in/wgsl/mesh-shader-empty.toml +++ b/naga/tests/in/wgsl/mesh-shader-empty.toml @@ -1,9 +1,12 @@ capabilities = "MESH_SHADER" -targets = "WGSL | SPIRV | HLSL" +targets = "WGSL | SPIRV | HLSL | METAL" [spv] version = [1, 4] capabilities = ["MeshShadingEXT"] +[msl] +lang_version = [3, 0] + [hlsl] shader_model = "V6_5" diff --git a/naga/tests/in/wgsl/mesh-shader-lines.toml b/naga/tests/in/wgsl/mesh-shader-lines.toml index 24e00ffee..45ccfecbd 100644 --- a/naga/tests/in/wgsl/mesh-shader-lines.toml +++ b/naga/tests/in/wgsl/mesh-shader-lines.toml @@ -1,9 +1,12 @@ capabilities = "MESH_SHADER" -targets = "WGSL | SPIRV | HLSL" +targets = "WGSL | SPIRV | HLSL | METAL" [spv] version = [1, 4] capabilities = ["MeshShadingEXT"] +[msl] +lang_version = [3, 0] + [hlsl] shader_model = "V6_5" diff --git a/naga/tests/in/wgsl/mesh-shader-points.toml b/naga/tests/in/wgsl/mesh-shader-points.toml index fce646e62..37752b390 100644 --- a/naga/tests/in/wgsl/mesh-shader-points.toml +++ b/naga/tests/in/wgsl/mesh-shader-points.toml @@ -1,6 +1,9 @@ capabilities = "MESH_SHADER | MESH_SHADER_POINT_TOPOLOGY" -targets = "WGSL | SPIRV" +targets = "WGSL | SPIRV | METAL" [spv] version = [1, 4] capabilities = ["MeshShadingEXT"] + +[msl] +lang_version = [3, 0] diff --git a/naga/tests/in/wgsl/mesh-shader.toml b/naga/tests/in/wgsl/mesh-shader.toml index 24e00ffee..45ccfecbd 100644 --- a/naga/tests/in/wgsl/mesh-shader.toml +++ b/naga/tests/in/wgsl/mesh-shader.toml @@ -1,9 +1,12 @@ capabilities = "MESH_SHADER" -targets = "WGSL | SPIRV | HLSL" +targets = "WGSL | SPIRV | HLSL | METAL" [spv] version = [1, 4] capabilities = ["MeshShadingEXT"] +[msl] +lang_version = [3, 0] + [hlsl] shader_model = "V6_5" diff --git a/naga/tests/naga/snapshots.rs b/naga/tests/naga/snapshots.rs index 0f1a0f1f9..2bc29591b 100644 --- a/naga/tests/naga/snapshots.rs +++ b/naga/tests/naga/snapshots.rs @@ -131,8 +131,8 @@ Note: this is an issue with snapshot configuration, not code. If you added a new &info, ¶ms.msl, ¶ms.msl_pipeline, - params.bounds_check_policies, ¶ms.pipeline_constants, + &shared_info, ); } @@ -267,8 +267,8 @@ fn write_output_msl( info: &naga::valid::ModuleInfo, options: &naga::back::msl::Options, pipeline_options: &naga::back::msl::PipelineOptions, - bounds_check_policies: naga::proc::BoundsCheckPolicies, pipeline_constants: &naga::back::PipelineConstants, + shared_options: &WriterSharedOptions, ) { use naga::back::msl; @@ -279,7 +279,9 @@ fn write_output_msl( .expect("override evaluation failed"); let mut options = options.clone(); - options.bounds_check_policies = bounds_check_policies; + options.bounds_check_policies = shared_options.bounds_checks_policies; + options.mesh_shader_primitive_indices_clamp = shared_options.mesh_output_validation; + options.task_dispatch_limits = shared_options.task_limits; let (string, tr_info) = msl::write_string(&module, &info, &options, pipeline_options) .unwrap_or_else(|err| panic!("Metal write failed: {err}")); diff --git a/naga/tests/out/msl/wgsl-8820-multiple-local-invocation-index-id.metal b/naga/tests/out/msl/wgsl-8820-multiple-local-invocation-index-id.metal index 45b85b79c..90411265c 100644 --- a/naga/tests/out/msl/wgsl-8820-multiple-local-invocation-index-id.metal +++ b/naga/tests/out/msl/wgsl-8820-multiple-local-invocation-index-id.metal @@ -16,7 +16,7 @@ kernel void compute1_( , uint local_invocation_index [[thread_index_in_threadgroup]] , threadgroup uint& wg_var ) { - if (metal::all(local_invocation_id == metal::uint3(0u))) { + if (local_invocation_index == 0u) { wg_var = {}; } metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); diff --git a/naga/tests/out/msl/wgsl-abstract-types-operators.metal b/naga/tests/out/msl/wgsl-abstract-types-operators.metal index e0188351c..bed023c04 100644 --- a/naga/tests/out/msl/wgsl-abstract-types-operators.metal +++ b/naga/tests/out/msl/wgsl-abstract-types-operators.metal @@ -107,10 +107,10 @@ void wgpu_4435_( } kernel void main_( - metal::uint3 __local_invocation_id [[thread_position_in_threadgroup]] + uint __local_invocation_index [[thread_index_in_threadgroup]] , threadgroup type_3& a ) { - if (metal::all(__local_invocation_id == metal::uint3(0u))) { + if (__local_invocation_index == 0u) { a = {}; } metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); diff --git a/naga/tests/out/msl/wgsl-atomicOps-float32.metal b/naga/tests/out/msl/wgsl-atomicOps-float32.metal index 0bdc3e6b4..efc9a1514 100644 --- a/naga/tests/out/msl/wgsl-atomicOps-float32.metal +++ b/naga/tests/out/msl/wgsl-atomicOps-float32.metal @@ -25,16 +25,19 @@ struct cs_mainInput { metal::atomic_store_explicit(&storage_struct.atomic_scalar, 1.5, metal::memory_order_relaxed); metal::atomic_store_explicit(&storage_struct.atomic_arr.inner[1], 1.5, metal::memory_order_relaxed); metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); + metal::threadgroup_barrier(metal::mem_flags::mem_object_data); float l0_ = metal::atomic_load_explicit(&storage_atomic_scalar, metal::memory_order_relaxed); float l1_ = metal::atomic_load_explicit(&storage_atomic_arr.inner[1], metal::memory_order_relaxed); float l2_ = metal::atomic_load_explicit(&storage_struct.atomic_scalar, metal::memory_order_relaxed); float l3_ = metal::atomic_load_explicit(&storage_struct.atomic_arr.inner[1], metal::memory_order_relaxed); metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); + metal::threadgroup_barrier(metal::mem_flags::mem_object_data); float _e27 = metal::atomic_fetch_add_explicit(&storage_atomic_scalar, 1.5, metal::memory_order_relaxed); float _e31 = metal::atomic_fetch_add_explicit(&storage_atomic_arr.inner[1], 1.5, metal::memory_order_relaxed); float _e35 = metal::atomic_fetch_add_explicit(&storage_struct.atomic_scalar, 1.5, metal::memory_order_relaxed); float _e40 = metal::atomic_fetch_add_explicit(&storage_struct.atomic_arr.inner[1], 1.5, metal::memory_order_relaxed); metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); + metal::threadgroup_barrier(metal::mem_flags::mem_object_data); float _e43 = metal::atomic_exchange_explicit(&storage_atomic_scalar, 1.5, metal::memory_order_relaxed); float _e47 = metal::atomic_exchange_explicit(&storage_atomic_arr.inner[1], 1.5, metal::memory_order_relaxed); float _e51 = metal::atomic_exchange_explicit(&storage_struct.atomic_scalar, 1.5, metal::memory_order_relaxed); diff --git a/naga/tests/out/msl/wgsl-atomicOps.metal b/naga/tests/out/msl/wgsl-atomicOps.metal index ea82237e7..26f384cf1 100644 --- a/naga/tests/out/msl/wgsl-atomicOps.metal +++ b/naga/tests/out/msl/wgsl-atomicOps.metal @@ -76,6 +76,7 @@ struct cs_mainInput { }; kernel void cs_main( metal::uint3 id [[thread_position_in_threadgroup]] +, uint __local_invocation_index [[thread_index_in_threadgroup]] , device metal::atomic_uint& storage_atomic_scalar [[user(fake0)]] , device type_4& storage_atomic_arr [[user(fake0)]] , device Struct& storage_struct [[user(fake0)]] @@ -83,7 +84,7 @@ kernel void cs_main( , threadgroup type_4& workgroup_atomic_arr , threadgroup Struct& workgroup_struct ) { - if (metal::all(id == metal::uint3(0u))) { + if (__local_invocation_index == 0u) { metal::atomic_store_explicit(&workgroup_atomic_scalar, 0, metal::memory_order_relaxed); for (int __i0 = 0; __i0 < 2; __i0++) { metal::atomic_store_explicit(&workgroup_atomic_arr.inner[__i0], 0, metal::memory_order_relaxed); diff --git a/naga/tests/out/msl/wgsl-atomicTexture-int64.metal b/naga/tests/out/msl/wgsl-atomicTexture-int64.metal index fb70ca59e..0c7578629 100644 --- a/naga/tests/out/msl/wgsl-atomicTexture-int64.metal +++ b/naga/tests/out/msl/wgsl-atomicTexture-int64.metal @@ -14,6 +14,7 @@ struct cs_mainInput { image.atomic_max(metal::uint2(metal::int2(0, 0)), 1uL); if (metal::int2(0, 0).x == -99999) { image.write(ulong4(0uL), metal::uint2(metal::int2(0, 0))); } metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); + metal::threadgroup_barrier(metal::mem_flags::mem_object_data); image.atomic_min(metal::uint2(metal::int2(0, 0)), 1uL); if (metal::int2(0, 0).x == -99999) { image.write(ulong4(0uL), metal::uint2(metal::int2(0, 0))); } return; diff --git a/naga/tests/out/msl/wgsl-globals.metal b/naga/tests/out/msl/wgsl-globals.metal index b976cc201..8bd1a5cd3 100644 --- a/naga/tests/out/msl/wgsl-globals.metal +++ b/naga/tests/out/msl/wgsl-globals.metal @@ -60,7 +60,7 @@ void test_msl_packed_vec3_( } kernel void main_( - metal::uint3 __local_invocation_id [[thread_position_in_threadgroup]] + uint __local_invocation_index [[thread_index_in_threadgroup]] , threadgroup type_2& wg , threadgroup metal::atomic_uint& at_1 , device FooStruct& alignment [[user(fake0)]] @@ -72,7 +72,7 @@ kernel void main_( , constant type_15& global_nested_arrays_of_matrices_4x2_ [[user(fake0)]] , constant _mslBufferSizes& _buffer_sizes [[user(fake0)]] ) { - if (metal::all(__local_invocation_id == metal::uint3(0u))) { + if (__local_invocation_index == 0u) { wg = {}; metal::atomic_store_explicit(&at_1, 0, metal::memory_order_relaxed); } diff --git a/naga/tests/out/msl/wgsl-interface.metal b/naga/tests/out/msl/wgsl-interface.metal index 4ebc7870c..c4f63ca9a 100644 --- a/naga/tests/out/msl/wgsl-interface.metal +++ b/naga/tests/out/msl/wgsl-interface.metal @@ -77,7 +77,7 @@ struct computeInput { , metal::uint3 num_wgs [[threadgroups_per_grid]] , threadgroup type_4& output ) { - if (metal::all(local_id == metal::uint3(0u))) { + if (local_index == 0u) { output = {}; } metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); diff --git a/naga/tests/out/msl/wgsl-mesh-shader-empty.metal b/naga/tests/out/msl/wgsl-mesh-shader-empty.metal new file mode 100644 index 000000000..61e3b1d9b --- /dev/null +++ b/naga/tests/out/msl/wgsl-mesh-shader-empty.metal @@ -0,0 +1,107 @@ +// language: metal3.0 +#include +#include + +using metal::uint; + +struct TaskPayload { + uint dummy; +}; +struct VertexOutput { + metal::float4 position; +}; +struct PrimitiveOutput { + metal::uint3 indices; +}; +struct type_3 { + VertexOutput inner[3]; +}; +struct type_4 { + PrimitiveOutput inner[1]; +}; +struct MeshOutput { + type_3 vertices; + type_4 primitives; + uint vertex_count; + uint primitive_count; + char _pad4[8]; +}; + +metal::uint3 _ts_main( + uint __local_invocation_index +, object_data TaskPayload& taskPayload +) { + if (__local_invocation_index == 0u) { + taskPayload = {}; + } + metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); + metal::threadgroup_barrier(metal::mem_flags::mem_object_data); + return metal::uint3(1u, 1u, 1u); +} + +[[object]] void ts_main( + metal::mesh_grid_properties nagaMeshGrid +, uint __local_invocation_index [[thread_index_in_threadgroup]] +, object_data TaskPayload& taskPayload [[payload]] +) { + uint3 nagaGridSize = _ts_main(__local_invocation_index, taskPayload); + metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); + metal::threadgroup_barrier(metal::mem_flags::mem_object_data); + if (__local_invocation_index == 0u) { + if ( + nagaGridSize.x > 256u || + nagaGridSize.y > 256u || + nagaGridSize.z > 256u || + metal::mulhi(nagaGridSize.x, nagaGridSize.y) != 0u || + metal::mulhi(nagaGridSize.x * nagaGridSize.y, nagaGridSize.z) != 0u || + (nagaGridSize.x * nagaGridSize.y * nagaGridSize.z) > 1024u + ) { + nagaGridSize = metal::uint3(0u); + } + nagaMeshGrid.set_threadgroups_per_grid(nagaGridSize); + } + return; +} + +struct ms_mainVertexOutput { + metal::float4 position [[position]]; +}; +struct ms_mainPrimitiveOutput { +}; +void _ms_main( + uint __local_invocation_index +, object_data TaskPayload const& taskPayload +, threadgroup MeshOutput& mesh_output +) { + if (__local_invocation_index == 0u) { + mesh_output = {}; + } + metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); + metal::threadgroup_barrier(metal::mem_flags::mem_object_data); + return; +} +[[mesh]] void ms_main( + metal::mesh meshOutput +, uint __local_invocation_index [[thread_index_in_threadgroup]] +, object_data TaskPayload const& taskPayload [[payload]] +) { + threadgroup MeshOutput mesh_output; + _ms_main(__local_invocation_index, taskPayload, mesh_output); + metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); + metal::threadgroup_barrier(metal::mem_flags::mem_object_data); + for(uint vertexIndex = __local_invocation_index; vertexIndex < metal::min(mesh_output.vertex_count, 3u); vertexIndex += 64) { + ms_mainVertexOutput vertex_; + vertex_.position = mesh_output.vertices.inner[vertexIndex].position; + meshOutput.set_vertex(vertexIndex, vertex_); + } + for(uint primitiveIndex = __local_invocation_index; primitiveIndex < metal::min(mesh_output.primitive_count, 1u); primitiveIndex += 64) { + ms_mainPrimitiveOutput primitive; + meshOutput.set_index(primitiveIndex * 3 + 0, mesh_output.primitives.inner[primitiveIndex].indices.x); + meshOutput.set_index(primitiveIndex * 3 + 1, mesh_output.primitives.inner[primitiveIndex].indices.y); + meshOutput.set_index(primitiveIndex * 3 + 2, mesh_output.primitives.inner[primitiveIndex].indices.z); + meshOutput.set_primitive(primitiveIndex, primitive); + } + if (__local_invocation_index == 0u) { + meshOutput.set_primitive_count(metal::min(mesh_output.primitive_count, 1u)); + } +} diff --git a/naga/tests/out/msl/wgsl-mesh-shader-lines.metal b/naga/tests/out/msl/wgsl-mesh-shader-lines.metal new file mode 100644 index 000000000..504527a97 --- /dev/null +++ b/naga/tests/out/msl/wgsl-mesh-shader-lines.metal @@ -0,0 +1,105 @@ +// language: metal3.0 +#include +#include + +using metal::uint; + +struct TaskPayload { + uint dummy; +}; +struct VertexOutput { + metal::float4 position; +}; +struct PrimitiveOutput { + metal::uint2 indices; +}; +struct type_4 { + VertexOutput inner[2]; +}; +struct type_5 { + PrimitiveOutput inner[1]; +}; +struct MeshOutput { + type_4 vertices; + type_5 primitives; + uint vertex_count; + uint primitive_count; +}; + +metal::uint3 _ts_main( + uint __local_invocation_index +, object_data TaskPayload& taskPayload +) { + if (__local_invocation_index == 0u) { + taskPayload = {}; + } + metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); + metal::threadgroup_barrier(metal::mem_flags::mem_object_data); + return metal::uint3(1u, 1u, 1u); +} + +[[object]] void ts_main( + metal::mesh_grid_properties nagaMeshGrid +, uint __local_invocation_index [[thread_index_in_threadgroup]] +, object_data TaskPayload& taskPayload [[payload]] +) { + uint3 nagaGridSize = _ts_main(__local_invocation_index, taskPayload); + metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); + metal::threadgroup_barrier(metal::mem_flags::mem_object_data); + if (__local_invocation_index == 0u) { + if ( + nagaGridSize.x > 256u || + nagaGridSize.y > 256u || + nagaGridSize.z > 256u || + metal::mulhi(nagaGridSize.x, nagaGridSize.y) != 0u || + metal::mulhi(nagaGridSize.x * nagaGridSize.y, nagaGridSize.z) != 0u || + (nagaGridSize.x * nagaGridSize.y * nagaGridSize.z) > 1024u + ) { + nagaGridSize = metal::uint3(0u); + } + nagaMeshGrid.set_threadgroups_per_grid(nagaGridSize); + } + return; +} + +struct ms_mainVertexOutput { + metal::float4 position [[position]]; +}; +struct ms_mainPrimitiveOutput { +}; +void _ms_main( + uint __local_invocation_index +, object_data TaskPayload const& taskPayload +, threadgroup MeshOutput& mesh_output +) { + if (__local_invocation_index == 0u) { + mesh_output = {}; + } + metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); + metal::threadgroup_barrier(metal::mem_flags::mem_object_data); + return; +} +[[mesh]] void ms_main( + metal::mesh meshOutput +, uint __local_invocation_index [[thread_index_in_threadgroup]] +, object_data TaskPayload const& taskPayload [[payload]] +) { + threadgroup MeshOutput mesh_output; + _ms_main(__local_invocation_index, taskPayload, mesh_output); + metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); + metal::threadgroup_barrier(metal::mem_flags::mem_object_data); + for(uint vertexIndex = __local_invocation_index; vertexIndex < metal::min(mesh_output.vertex_count, 2u); vertexIndex += 64) { + ms_mainVertexOutput vertex_; + vertex_.position = mesh_output.vertices.inner[vertexIndex].position; + meshOutput.set_vertex(vertexIndex, vertex_); + } + for(uint primitiveIndex = __local_invocation_index; primitiveIndex < metal::min(mesh_output.primitive_count, 1u); primitiveIndex += 64) { + ms_mainPrimitiveOutput primitive; + meshOutput.set_index(primitiveIndex * 2 + 0, mesh_output.primitives.inner[primitiveIndex].indices.x); + meshOutput.set_index(primitiveIndex * 2 + 1, mesh_output.primitives.inner[primitiveIndex].indices.y); + meshOutput.set_primitive(primitiveIndex, primitive); + } + if (__local_invocation_index == 0u) { + meshOutput.set_primitive_count(metal::min(mesh_output.primitive_count, 1u)); + } +} diff --git a/naga/tests/out/msl/wgsl-mesh-shader-points.metal b/naga/tests/out/msl/wgsl-mesh-shader-points.metal new file mode 100644 index 000000000..761e83d56 --- /dev/null +++ b/naga/tests/out/msl/wgsl-mesh-shader-points.metal @@ -0,0 +1,105 @@ +// language: metal3.0 +#include +#include + +using metal::uint; + +struct TaskPayload { + uint dummy; +}; +struct VertexOutput { + metal::float4 position; +}; +struct PrimitiveOutput { + uint indices; +}; +struct type_3 { + VertexOutput inner[1]; +}; +struct type_4 { + PrimitiveOutput inner[1]; +}; +struct MeshOutput { + type_3 vertices; + type_4 primitives; + uint vertex_count; + uint primitive_count; + char _pad4[4]; +}; + +metal::uint3 _ts_main( + uint __local_invocation_index +, object_data TaskPayload& taskPayload +) { + if (__local_invocation_index == 0u) { + taskPayload = {}; + } + metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); + metal::threadgroup_barrier(metal::mem_flags::mem_object_data); + return metal::uint3(1u, 1u, 1u); +} + +[[object]] void ts_main( + metal::mesh_grid_properties nagaMeshGrid +, uint __local_invocation_index [[thread_index_in_threadgroup]] +, object_data TaskPayload& taskPayload [[payload]] +) { + uint3 nagaGridSize = _ts_main(__local_invocation_index, taskPayload); + metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); + metal::threadgroup_barrier(metal::mem_flags::mem_object_data); + if (__local_invocation_index == 0u) { + if ( + nagaGridSize.x > 256u || + nagaGridSize.y > 256u || + nagaGridSize.z > 256u || + metal::mulhi(nagaGridSize.x, nagaGridSize.y) != 0u || + metal::mulhi(nagaGridSize.x * nagaGridSize.y, nagaGridSize.z) != 0u || + (nagaGridSize.x * nagaGridSize.y * nagaGridSize.z) > 1024u + ) { + nagaGridSize = metal::uint3(0u); + } + nagaMeshGrid.set_threadgroups_per_grid(nagaGridSize); + } + return; +} + +struct ms_mainVertexOutput { + metal::float4 position [[position]]; +}; +struct ms_mainPrimitiveOutput { +}; +void _ms_main( + uint __local_invocation_index +, object_data TaskPayload const& taskPayload +, threadgroup MeshOutput& mesh_output +) { + if (__local_invocation_index == 0u) { + mesh_output = {}; + } + metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); + metal::threadgroup_barrier(metal::mem_flags::mem_object_data); + return; +} +[[mesh]] void ms_main( + metal::mesh meshOutput +, uint __local_invocation_index [[thread_index_in_threadgroup]] +, object_data TaskPayload const& taskPayload [[payload]] +) { + threadgroup MeshOutput mesh_output; + _ms_main(__local_invocation_index, taskPayload, mesh_output); + metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); + metal::threadgroup_barrier(metal::mem_flags::mem_object_data); + for(uint vertexIndex = __local_invocation_index; vertexIndex < metal::min(mesh_output.vertex_count, 1u); vertexIndex += 64) { + ms_mainVertexOutput vertex_; + vertex_.position = mesh_output.vertices.inner[vertexIndex].position; + meshOutput.set_vertex(vertexIndex, vertex_); + } + for(uint primitiveIndex = __local_invocation_index; primitiveIndex < metal::min(mesh_output.primitive_count, 1u); primitiveIndex += 64) { + ms_mainPrimitiveOutput primitive; + meshOutput.set_index(primitiveIndex * 1 + 0, mesh_output.primitives.inner[primitiveIndex].indices); + meshOutput.set_primitive(primitiveIndex, primitive); + } + if (__local_invocation_index == 0u) { + meshOutput.set_primitive_count(metal::min(mesh_output.primitive_count, 1u)); + } +} diff --git a/naga/tests/out/msl/wgsl-mesh-shader.metal b/naga/tests/out/msl/wgsl-mesh-shader.metal new file mode 100644 index 000000000..a359dd091 --- /dev/null +++ b/naga/tests/out/msl/wgsl-mesh-shader.metal @@ -0,0 +1,360 @@ +// language: metal3.0 +#include +#include + +using metal::uint; + +struct TaskPayload { + metal::float4 colorMask; + bool visible; + char _pad2[15]; +}; +struct VertexOutput { + metal::float4 position; + metal::float4 color; +}; +struct PrimitiveOutput { + metal::packed_uint3 indices; + bool cull; + char _pad2[3]; + metal::float4 colorMask; +}; +struct PrimitiveInput { + metal::float4 colorMask; +}; +struct type_5 { + VertexOutput inner[3]; +}; +struct type_6 { + PrimitiveOutput inner[1]; +}; +struct MeshOutput { + type_5 vertices; + type_6 primitives; + uint vertex_count; + uint primitive_count; + char _pad4[8]; +}; + +bool helper_reader( + object_data TaskPayload const& taskPayload +) { + bool _e2 = taskPayload.visible; + return _e2; +} + +void helper_writer( + bool value, + object_data TaskPayload& taskPayload +) { + taskPayload.visible = value; + return; +} + +metal::uint3 _ts_main( + uint __local_invocation_index +, object_data TaskPayload& taskPayload +, threadgroup float& workgroupData +) { + if (__local_invocation_index == 0u) { + taskPayload = {}; + workgroupData = {}; + } + metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); + metal::threadgroup_barrier(metal::mem_flags::mem_object_data); + workgroupData = 1.0; + taskPayload.colorMask = metal::float4(1.0, 1.0, 0.0, 1.0); + helper_writer(true, taskPayload); + bool _e12 = helper_reader(taskPayload); + taskPayload.visible = _e12; + return metal::uint3(1u, 1u, 1u); +} + +[[object]] void ts_main( + metal::mesh_grid_properties nagaMeshGrid +, uint __local_invocation_index [[thread_index_in_threadgroup]] +, object_data TaskPayload& taskPayload [[payload]] +, threadgroup float& workgroupData +) { + uint3 nagaGridSize = _ts_main(__local_invocation_index, taskPayload, workgroupData); + metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); + metal::threadgroup_barrier(metal::mem_flags::mem_object_data); + if (__local_invocation_index == 0u) { + if ( + nagaGridSize.x > 256u || + nagaGridSize.y > 256u || + nagaGridSize.z > 256u || + metal::mulhi(nagaGridSize.x, nagaGridSize.y) != 0u || + metal::mulhi(nagaGridSize.x * nagaGridSize.y, nagaGridSize.z) != 0u || + (nagaGridSize.x * nagaGridSize.y * nagaGridSize.z) > 1024u + ) { + nagaGridSize = metal::uint3(0u); + } + nagaMeshGrid.set_threadgroups_per_grid(nagaGridSize); + } + return; +} + +struct ts_divergentInput { +}; +metal::uint3 _ts_divergent( + metal::uint3 thread_id +, uint __local_invocation_index +, object_data TaskPayload& taskPayload +) { + if (__local_invocation_index == 0u) { + taskPayload = {}; + } + metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); + metal::threadgroup_barrier(metal::mem_flags::mem_object_data); + if (thread_id.x == 0u) { + taskPayload.colorMask = metal::float4(1.0, 1.0, 0.0, 1.0); + taskPayload.visible = true; + return metal::uint3(1u, 1u, 1u); + } + return metal::uint3(2u, 2u, 2u); +} + +[[object]] void ts_divergent( + metal::mesh_grid_properties nagaMeshGrid_1 +, metal::uint3 thread_id [[thread_position_in_threadgroup]] +, uint __local_invocation_index [[thread_index_in_threadgroup]] +, object_data TaskPayload& taskPayload [[payload]] +) { + uint3 nagaGridSize_1 = _ts_divergent(thread_id, __local_invocation_index, taskPayload); + metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); + metal::threadgroup_barrier(metal::mem_flags::mem_object_data); + if (__local_invocation_index == 0u) { + if ( + nagaGridSize_1.x > 256u || + nagaGridSize_1.y > 256u || + nagaGridSize_1.z > 256u || + metal::mulhi(nagaGridSize_1.x, nagaGridSize_1.y) != 0u || + metal::mulhi(nagaGridSize_1.x * nagaGridSize_1.y, nagaGridSize_1.z) != 0u || + (nagaGridSize_1.x * nagaGridSize_1.y * nagaGridSize_1.z) > 1024u + ) { + nagaGridSize_1 = metal::uint3(0u); + } + nagaMeshGrid_1.set_threadgroups_per_grid(nagaGridSize_1); + } + return; +} + +struct ms_mainVertexOutput { + metal::float4 position [[position]]; + metal::float4 color [[user(loc0), center_perspective]]; +}; +struct ms_mainPrimitiveOutput { + bool cull [[primitive_culled]]; + metal::float4 colorMask [[user(loc1), flat]]; +}; +void _ms_main( + uint __local_invocation_index +, object_data TaskPayload const& taskPayload +, threadgroup float& workgroupData +, threadgroup MeshOutput& mesh_output +) { + if (__local_invocation_index == 0u) { + workgroupData = {}; + mesh_output = {}; + } + metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); + metal::threadgroup_barrier(metal::mem_flags::mem_object_data); + mesh_output.vertex_count = 3u; + mesh_output.primitive_count = 1u; + workgroupData = 2.0; + mesh_output.vertices.inner[0].position = metal::float4(0.0, 1.0, 0.0, 1.0); + metal::float4 _e23 = taskPayload.colorMask; + mesh_output.vertices.inner[0].color = metal::float4(0.0, 1.0, 0.0, 1.0) * _e23; + mesh_output.vertices.inner[1].position = metal::float4(-1.0, -1.0, 0.0, 1.0); + metal::float4 _e45 = taskPayload.colorMask; + mesh_output.vertices.inner[1].color = metal::float4(0.0, 0.0, 1.0, 1.0) * _e45; + mesh_output.vertices.inner[2].position = metal::float4(1.0, -1.0, 0.0, 1.0); + metal::float4 _e67 = taskPayload.colorMask; + mesh_output.vertices.inner[2].color = metal::float4(1.0, 0.0, 0.0, 1.0) * _e67; + mesh_output.primitives.inner[0].indices = metal::uint3(0u, 1u, 2u); + bool _e86 = helper_reader(taskPayload); + mesh_output.primitives.inner[0].cull = !(_e86); + mesh_output.primitives.inner[0].colorMask = metal::float4(1.0, 0.0, 1.0, 1.0); + return; +} + +[[mesh]] void ms_main( + metal::mesh meshOutput +, uint __local_invocation_index [[thread_index_in_threadgroup]] +, object_data TaskPayload const& taskPayload [[payload]] +) { + threadgroup float workgroupData; + threadgroup MeshOutput mesh_output; + _ms_main(__local_invocation_index, taskPayload, workgroupData, mesh_output); + metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); + metal::threadgroup_barrier(metal::mem_flags::mem_object_data); + for(uint vertexIndex = __local_invocation_index; vertexIndex < metal::min(mesh_output.vertex_count, 3u); vertexIndex += 1) { + ms_mainVertexOutput vertex_1; + vertex_1.position = mesh_output.vertices.inner[vertexIndex].position; + vertex_1.color = mesh_output.vertices.inner[vertexIndex].color; + meshOutput.set_vertex(vertexIndex, vertex_1); + } + for(uint primitiveIndex = __local_invocation_index; primitiveIndex < metal::min(mesh_output.primitive_count, 1u); primitiveIndex += 1) { + ms_mainPrimitiveOutput primitive_1; + meshOutput.set_index(primitiveIndex * 3 + 0, mesh_output.primitives.inner[primitiveIndex].indices.x); + meshOutput.set_index(primitiveIndex * 3 + 1, mesh_output.primitives.inner[primitiveIndex].indices.y); + meshOutput.set_index(primitiveIndex * 3 + 2, mesh_output.primitives.inner[primitiveIndex].indices.z); + primitive_1.cull = mesh_output.primitives.inner[primitiveIndex].cull; + primitive_1.colorMask = mesh_output.primitives.inner[primitiveIndex].colorMask; + meshOutput.set_primitive(primitiveIndex, primitive_1); + } + if (__local_invocation_index == 0u) { + meshOutput.set_primitive_count(metal::min(mesh_output.primitive_count, 1u)); + } +} + +struct ms_no_tsVertexOutput { + metal::float4 position [[position]]; + metal::float4 color [[user(loc0), center_perspective]]; +}; +struct ms_no_tsPrimitiveOutput { + bool cull [[primitive_culled]]; + metal::float4 colorMask [[user(loc1), flat]]; +}; +void _ms_no_ts( + uint __local_invocation_index +, threadgroup float& workgroupData +, threadgroup MeshOutput& mesh_output +) { + if (__local_invocation_index == 0u) { + workgroupData = {}; + mesh_output = {}; + } + metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); + metal::threadgroup_barrier(metal::mem_flags::mem_object_data); + mesh_output.vertex_count = 3u; + mesh_output.primitive_count = 1u; + workgroupData = 2.0; + mesh_output.vertices.inner[0].position = metal::float4(0.0, 1.0, 0.0, 1.0); + mesh_output.vertices.inner[0].color = metal::float4(0.0, 1.0, 0.0, 1.0); + mesh_output.vertices.inner[1].position = metal::float4(-1.0, -1.0, 0.0, 1.0); + mesh_output.vertices.inner[1].color = metal::float4(0.0, 0.0, 1.0, 1.0); + mesh_output.vertices.inner[2].position = metal::float4(1.0, -1.0, 0.0, 1.0); + mesh_output.vertices.inner[2].color = metal::float4(1.0, 0.0, 0.0, 1.0); + mesh_output.primitives.inner[0].indices = metal::uint3(0u, 1u, 2u); + mesh_output.primitives.inner[0].cull = false; + mesh_output.primitives.inner[0].colorMask = metal::float4(1.0, 0.0, 1.0, 1.0); + return; +} + +[[mesh]] void ms_no_ts( + metal::mesh meshOutput_1 +, uint __local_invocation_index [[thread_index_in_threadgroup]] +) { + threadgroup float workgroupData; + threadgroup MeshOutput mesh_output; + _ms_no_ts(__local_invocation_index, workgroupData, mesh_output); + metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); + metal::threadgroup_barrier(metal::mem_flags::mem_object_data); + for(uint vertexIndex_1 = __local_invocation_index; vertexIndex_1 < metal::min(mesh_output.vertex_count, 3u); vertexIndex_1 += 1) { + ms_no_tsVertexOutput vertex_2; + vertex_2.position = mesh_output.vertices.inner[vertexIndex_1].position; + vertex_2.color = mesh_output.vertices.inner[vertexIndex_1].color; + meshOutput_1.set_vertex(vertexIndex_1, vertex_2); + } + for(uint primitiveIndex_1 = __local_invocation_index; primitiveIndex_1 < metal::min(mesh_output.primitive_count, 1u); primitiveIndex_1 += 1) { + ms_no_tsPrimitiveOutput primitive_2; + meshOutput_1.set_index(primitiveIndex_1 * 3 + 0, mesh_output.primitives.inner[primitiveIndex_1].indices.x); + meshOutput_1.set_index(primitiveIndex_1 * 3 + 1, mesh_output.primitives.inner[primitiveIndex_1].indices.y); + meshOutput_1.set_index(primitiveIndex_1 * 3 + 2, mesh_output.primitives.inner[primitiveIndex_1].indices.z); + primitive_2.cull = mesh_output.primitives.inner[primitiveIndex_1].cull; + primitive_2.colorMask = mesh_output.primitives.inner[primitiveIndex_1].colorMask; + meshOutput_1.set_primitive(primitiveIndex_1, primitive_2); + } + if (__local_invocation_index == 0u) { + meshOutput_1.set_primitive_count(metal::min(mesh_output.primitive_count, 1u)); + } +} + +struct ms_divergentInput { +}; +struct ms_divergentVertexOutput { + metal::float4 position [[position]]; + metal::float4 color [[user(loc0), center_perspective]]; +}; +struct ms_divergentPrimitiveOutput { + bool cull [[primitive_culled]]; + metal::float4 colorMask [[user(loc1), flat]]; +}; +void _ms_divergent( + metal::uint3 thread_id_1 +, uint __local_invocation_index +, threadgroup float& workgroupData +, threadgroup MeshOutput& mesh_output +) { + if (__local_invocation_index == 0u) { + workgroupData = {}; + mesh_output = {}; + } + metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); + metal::threadgroup_barrier(metal::mem_flags::mem_object_data); + if (thread_id_1.x == 0u) { + mesh_output.vertex_count = 3u; + mesh_output.primitive_count = 1u; + workgroupData = 2.0; + mesh_output.vertices.inner[0].position = metal::float4(0.0, 1.0, 0.0, 1.0); + mesh_output.vertices.inner[0].color = metal::float4(0.0, 1.0, 0.0, 1.0); + mesh_output.vertices.inner[1].position = metal::float4(-1.0, -1.0, 0.0, 1.0); + mesh_output.vertices.inner[1].color = metal::float4(0.0, 0.0, 1.0, 1.0); + mesh_output.vertices.inner[2].position = metal::float4(1.0, -1.0, 0.0, 1.0); + mesh_output.vertices.inner[2].color = metal::float4(1.0, 0.0, 0.0, 1.0); + mesh_output.primitives.inner[0].indices = metal::uint3(0u, 1u, 2u); + mesh_output.primitives.inner[0].cull = false; + mesh_output.primitives.inner[0].colorMask = metal::float4(1.0, 0.0, 1.0, 1.0); + return; + } else { + return; + } +} + +[[mesh]] void ms_divergent( + metal::mesh meshOutput_2 +, metal::uint3 thread_id_1 [[thread_position_in_threadgroup]] +, uint __local_invocation_index [[thread_index_in_threadgroup]] +) { + threadgroup float workgroupData; + threadgroup MeshOutput mesh_output; + _ms_divergent(thread_id_1, __local_invocation_index, workgroupData, mesh_output); + metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); + metal::threadgroup_barrier(metal::mem_flags::mem_object_data); + for(uint vertexIndex_2 = __local_invocation_index; vertexIndex_2 < metal::min(mesh_output.vertex_count, 3u); vertexIndex_2 += 2) { + ms_divergentVertexOutput vertex_3; + vertex_3.position = mesh_output.vertices.inner[vertexIndex_2].position; + vertex_3.color = mesh_output.vertices.inner[vertexIndex_2].color; + meshOutput_2.set_vertex(vertexIndex_2, vertex_3); + } + for(uint primitiveIndex_2 = __local_invocation_index; primitiveIndex_2 < metal::min(mesh_output.primitive_count, 1u); primitiveIndex_2 += 2) { + ms_divergentPrimitiveOutput primitive_3; + meshOutput_2.set_index(primitiveIndex_2 * 3 + 0, mesh_output.primitives.inner[primitiveIndex_2].indices.x); + meshOutput_2.set_index(primitiveIndex_2 * 3 + 1, mesh_output.primitives.inner[primitiveIndex_2].indices.y); + meshOutput_2.set_index(primitiveIndex_2 * 3 + 2, mesh_output.primitives.inner[primitiveIndex_2].indices.z); + primitive_3.cull = mesh_output.primitives.inner[primitiveIndex_2].cull; + primitive_3.colorMask = mesh_output.primitives.inner[primitiveIndex_2].colorMask; + meshOutput_2.set_primitive(primitiveIndex_2, primitive_3); + } + if (__local_invocation_index == 0u) { + meshOutput_2.set_primitive_count(metal::min(mesh_output.primitive_count, 1u)); + } +} + +struct fs_mainInput { + metal::float4 color [[user(loc0), center_perspective]]; + metal::float4 colorMask [[user(loc1), flat]]; +}; +struct fs_mainOutput { + metal::float4 member_5 [[color(0)]]; +}; +fragment fs_mainOutput fs_main( + fs_mainInput varyings_5 [[stage_in]] +, metal::float4 position_3 [[position]] +) { + const VertexOutput vertex_ = { position_3, varyings_5.color }; + const PrimitiveInput primitive = { varyings_5.colorMask }; + return fs_mainOutput { vertex_.color * primitive.colorMask }; +} diff --git a/naga/tests/out/msl/wgsl-overrides-atomicCompareExchangeWeak.metal b/naga/tests/out/msl/wgsl-overrides-atomicCompareExchangeWeak.metal index 42b93fdfc..8380883e3 100644 --- a/naga/tests/out/msl/wgsl-overrides-atomicCompareExchangeWeak.metal +++ b/naga/tests/out/msl/wgsl-overrides-atomicCompareExchangeWeak.metal @@ -37,10 +37,10 @@ _atomic_compare_exchange_result_Uint_4_ naga_atomic_compare_exchange_weak_explic constant int o = 2; kernel void f( - metal::uint3 __local_invocation_id [[thread_position_in_threadgroup]] + uint __local_invocation_index [[thread_index_in_threadgroup]] , threadgroup metal::atomic_uint& a ) { - if (metal::all(__local_invocation_id == metal::uint3(0u))) { + if (__local_invocation_index == 0u) { metal::atomic_store_explicit(&a, 0, metal::memory_order_relaxed); } metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); diff --git a/naga/tests/out/msl/wgsl-policy-mix.metal b/naga/tests/out/msl/wgsl-policy-mix.metal index 5130ad1e5..a31d80398 100644 --- a/naga/tests/out/msl/wgsl-policy-mix.metal +++ b/naga/tests/out/msl/wgsl-policy-mix.metal @@ -53,17 +53,17 @@ metal::float4 mock_function( } kernel void main_( - metal::uint3 __local_invocation_id [[thread_position_in_threadgroup]] + uint __local_invocation_index [[thread_index_in_threadgroup]] , device InStorage const& in_storage [[user(fake0)]] , constant InUniform& in_uniform [[user(fake0)]] , metal::texture2d_array image_2d_array [[user(fake0)]] , threadgroup type_5& in_workgroup ) { - if (metal::all(__local_invocation_id == metal::uint3(0u))) { + type_6 in_private = {}; + if (__local_invocation_index == 0u) { in_workgroup = {}; } metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); - type_6 in_private = {}; metal::float4 _e5 = mock_function(metal::int2(1, 2), 3, 4, in_storage, in_uniform, image_2d_array, in_workgroup, in_private); return; } diff --git a/naga/tests/out/msl/wgsl-workgroup-uniform-load-atomic.metal b/naga/tests/out/msl/wgsl-workgroup-uniform-load-atomic.metal index 0fcdebded..ed4b60096 100644 --- a/naga/tests/out/msl/wgsl-workgroup-uniform-load-atomic.metal +++ b/naga/tests/out/msl/wgsl-workgroup-uniform-load-atomic.metal @@ -17,11 +17,12 @@ struct test_atomic_workgroup_uniform_loadInput { kernel void test_atomic_workgroup_uniform_load( metal::uint3 workgroup_id [[threadgroup_position_in_grid]] , metal::uint3 local_id [[thread_position_in_threadgroup]] +, uint __local_invocation_index [[thread_index_in_threadgroup]] , threadgroup metal::atomic_uint& wg_scalar , threadgroup metal::atomic_int& wg_signed , threadgroup AtomicStruct& wg_struct ) { - if (metal::all(local_id == metal::uint3(0u))) { + if (__local_invocation_index == 0u) { metal::atomic_store_explicit(&wg_scalar, 0, metal::memory_order_relaxed); metal::atomic_store_explicit(&wg_signed, 0, metal::memory_order_relaxed); metal::atomic_store_explicit(&wg_struct.atomic_scalar, 0, metal::memory_order_relaxed); diff --git a/naga/tests/out/msl/wgsl-workgroup-uniform-load.metal b/naga/tests/out/msl/wgsl-workgroup-uniform-load.metal index 32495c198..5b8b513c3 100644 --- a/naga/tests/out/msl/wgsl-workgroup-uniform-load.metal +++ b/naga/tests/out/msl/wgsl-workgroup-uniform-load.metal @@ -13,10 +13,10 @@ struct test_workgroupUniformLoadInput { }; kernel void test_workgroupUniformLoad( metal::uint3 workgroup_id [[threadgroup_position_in_grid]] -, metal::uint3 __local_invocation_id [[thread_position_in_threadgroup]] +, uint __local_invocation_index [[thread_index_in_threadgroup]] , threadgroup type_2& arr_i32_ ) { - if (metal::all(__local_invocation_id == metal::uint3(0u))) { + if (__local_invocation_index == 0u) { arr_i32_ = {}; } metal::threadgroup_barrier(metal::mem_flags::mem_threadgroup); diff --git a/naga/tests/out/msl/wgsl-workgroup-var-init.metal b/naga/tests/out/msl/wgsl-workgroup-var-init.metal index 991c8b014..6bb6bf96f 100644 --- a/naga/tests/out/msl/wgsl-workgroup-var-init.metal +++ b/naga/tests/out/msl/wgsl-workgroup-var-init.metal @@ -20,11 +20,11 @@ struct WStruct { }; kernel void main_( - metal::uint3 __local_invocation_id [[thread_position_in_threadgroup]] + uint __local_invocation_index [[thread_index_in_threadgroup]] , threadgroup WStruct& w_mem , device type_1& output [[buffer(0)]] ) { - if (metal::all(__local_invocation_id == metal::uint3(0u))) { + if (__local_invocation_index == 0u) { w_mem.arr = {}; metal::atomic_store_explicit(&w_mem.atom, 0, metal::memory_order_relaxed); for (int __i0 = 0; __i0 < 8; __i0++) { diff --git a/tests/tests/wgpu-gpu/mesh_shader/mod.rs b/tests/tests/wgpu-gpu/mesh_shader/mod.rs index 622005b6e..0f24fd5c1 100644 --- a/tests/tests/wgpu-gpu/mesh_shader/mod.rs +++ b/tests/tests/wgpu-gpu/mesh_shader/mod.rs @@ -28,16 +28,6 @@ fn compile_wgsl(device: &wgpu::Device) -> wgpu::ShaderModule { }) } -fn compile_msl(device: &wgpu::Device) -> wgpu::ShaderModule { - unsafe { - device.create_shader_module_passthrough(wgpu::ShaderModuleDescriptorPassthrough { - label: None, - msl: Some(std::borrow::Cow::Borrowed(include_str!("shader.metal"))), - num_workgroups: (1, 1, 1), - ..Default::default() - }) - } -} struct Shaders { ts: Option, ms: wgpu::ShaderModule, @@ -46,50 +36,24 @@ struct Shaders { ms_name: &'static str, fs_name: &'static str, } -fn get_shaders( - device: &wgpu::Device, - backend: wgpu::Backend, - info: &MeshPipelineTestInfo, -) -> Shaders { +fn get_shaders(device: &wgpu::Device, info: &MeshPipelineTestInfo) -> Shaders { if info.divergent && info.use_task { unreachable!(); } - // In the case that the platform does support mesh shaders, the dummy - // shader is used to avoid requiring PASSTHROUGH_SHADERS. - match backend { - wgpu::Backend::Vulkan | wgpu::Backend::Dx12 => { - let compiled = compile_wgsl(device); - Shaders { - ts: info.use_task.then_some(compiled.clone()), - ms: compiled.clone(), - fs: info.use_frag.then_some(compiled), - ts_name: "ts_main", - ms_name: if info.divergent { - "ms_divergent" - } else if info.use_task { - "ms_main" - } else { - "ms_no_ts" - }, - fs_name: "fs_main", - } - } - wgpu::Backend::Metal => { - let compiled = compile_msl(device); - Shaders { - ts: info.use_task.then_some(compiled.clone()), - ms: compiled.clone(), - fs: info.use_frag.then_some(compiled), - ts_name: "taskShader", - ms_name: if info.use_task { - "meshShader" - } else { - "meshNoTaskShader" - }, - fs_name: "fragShader", - } - } - _ => unreachable!(), + let compiled = compile_wgsl(device); + Shaders { + ts: info.use_task.then_some(compiled.clone()), + ms: compiled.clone(), + fs: info.use_frag.then_some(compiled), + ts_name: "ts_main", + ms_name: if info.divergent { + "ms_divergent" + } else if info.use_task { + "ms_main" + } else { + "ms_no_ts" + }, + fs_name: "fs_main", } } @@ -130,7 +94,6 @@ struct MeshPipelineTestInfo { } fn mesh_pipeline_build(ctx: &TestingContext, info: MeshPipelineTestInfo) { - let backend = ctx.adapter.get_info().backend; let device = &ctx.device; let (_depth_image, depth_view, depth_state) = create_depth(device); @@ -141,7 +104,7 @@ fn mesh_pipeline_build(ctx: &TestingContext, info: MeshPipelineTestInfo) { ts_name, ms_name, fs_name, - } = get_shaders(device, backend, &info); + } = get_shaders(device, &info); let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: None, bind_group_layouts: &[], @@ -214,7 +177,6 @@ pub enum DrawType { } fn mesh_draw(ctx: &TestingContext, draw_type: DrawType, info: MeshPipelineTestInfo) { - let backend = ctx.adapter.get_info().backend; let device = &ctx.device; let (_depth_image, depth_view, depth_state) = create_depth(device); @@ -225,7 +187,7 @@ fn mesh_draw(ctx: &TestingContext, draw_type: DrawType, info: MeshPipelineTestIn ts_name, ms_name, fs_name, - } = get_shaders(device, backend, &info); + } = get_shaders(device, &info); let frag = fs.unwrap(); let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: None, diff --git a/tests/tests/wgpu-gpu/mesh_shader/shader.metal b/tests/tests/wgpu-gpu/mesh_shader/shader.metal deleted file mode 100644 index f935030c7..000000000 --- a/tests/tests/wgpu-gpu/mesh_shader/shader.metal +++ /dev/null @@ -1,98 +0,0 @@ -using namespace metal; - -struct OutVertex { - float4 Position [[position]]; - float4 Color [[user(locn0)]]; -}; - -struct OutPrimitive { - float4 ColorMask [[flat]] [[user(locn1)]]; - bool CullPrimitive [[primitive_culled]]; -}; - -struct InVertex { -}; - -struct InPrimitive { - float4 ColorMask [[flat]] [[user(locn1)]]; -}; - -struct FragmentIn { - float4 Color [[user(locn0)]]; - float4 ColorMask [[flat]] [[user(locn1)]]; -}; - -struct PayloadData { - float4 ColorMask; - bool Visible; -}; - -using Meshlet = metal::mesh; - - -constant float4 positions[3] = { - float4(0.0, 1.0, 0.0, 1.0), - float4(-1.0, -1.0, 0.0, 1.0), - float4(1.0, -1.0, 0.0, 1.0) -}; - -constant float4 colors[3] = { - float4(0.0, 1.0, 0.0, 1.0), - float4(0.0, 0.0, 1.0, 1.0), - float4(1.0, 0.0, 0.0, 1.0) -}; - - -[[object]] -void taskShader(uint3 tid [[thread_position_in_grid]], object_data PayloadData &outPayload [[payload]], mesh_grid_properties grid) { - outPayload.ColorMask = float4(1.0, 1.0, 0.0, 1.0); - outPayload.Visible = true; - grid.set_threadgroups_per_grid(uint3(3, 1, 1)); -} - -[[mesh]] -void meshShader( - object_data PayloadData const& payload [[payload]], - Meshlet out -) -{ - out.set_primitive_count(1); - - for(int i = 0;i < 3;i++) { - OutVertex vert; - vert.Position = positions[i]; - vert.Color = colors[i] * payload.ColorMask; - out.set_vertex(i, vert); - out.set_index(i, i); - } - - OutPrimitive prim; - prim.ColorMask = float4(1.0, 0.0, 0.0, 1.0); - prim.CullPrimitive = !payload.Visible; - out.set_primitive(0, prim); -} - -[[mesh]] -void meshNoTaskShader( - Meshlet out -) -{ - out.set_primitive_count(1); - - for(int i = 0;i < 3;i++) { - OutVertex vert; - vert.Position = positions[i]; - vert.Color = colors[i]; - out.set_vertex(i, vert); - out.set_index(i, i); - } - - OutPrimitive prim; - prim.ColorMask = float4(1.0, 0.0, 0.0, 1.0); - prim.CullPrimitive = false; - out.set_primitive(0, prim); -} - -fragment float4 fragShader(FragmentIn data [[stage_in]]) { - return data.Color * data.ColorMask; -} diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index 9b5ec99c6..2ce7d59e4 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -851,15 +851,25 @@ fn draw_mesh_tasks( ) -> Result<(), RenderBundleErrorInner> { state.is_ready(DrawCommandFamily::DrawMeshTasks)?; + let limits = &state.device.limits; + let (groups_size_limit, max_groups) = + if state.pipeline.as_ref().unwrap().pipeline.has_task_shader { + ( + limits.max_task_workgroups_per_dimension, + limits.max_task_workgroup_total_count, + ) + } else { + ( + limits.max_mesh_workgroups_per_dimension, + limits.max_mesh_workgroup_total_count, + ) + }; + let total_count = check_workgroup_sizes( &[group_count_x, group_count_y, group_count_z], - &[ - state.device.limits.max_task_mesh_workgroups_per_dimension, - state.device.limits.max_task_mesh_workgroups_per_dimension, - state.device.limits.max_task_mesh_workgroups_per_dimension, - ], + &[groups_size_limit, groups_size_limit, groups_size_limit], "max_task_mesh_workgroups_per_dimension", - state.device.limits.max_task_mesh_workgroup_total_count, + max_groups, "max_task_mesh_workgroup_total_count", ) .map_err(|err| RenderBundleErrorInner::Draw(err.into()))?; diff --git a/wgpu-core/src/command/render.rs b/wgpu-core/src/command/render.rs index 16ed3c4a4..8ecb88bdd 100644 --- a/wgpu-core/src/command/render.rs +++ b/wgpu-core/src/command/render.rs @@ -2728,18 +2728,19 @@ fn draw_mesh_tasks( state.flush_bindings()?; validate_mesh_draw_multiview(state)?; - let groups_size_limit = state - .pass - .base - .device - .limits - .max_task_mesh_workgroups_per_dimension; - let max_groups = state - .pass - .base - .device - .limits - .max_task_mesh_workgroup_total_count; + let limits = &state.pass.base.device.limits; + let (groups_size_limit, max_groups) = if state.pipeline.as_ref().unwrap().has_task_shader { + ( + limits.max_task_workgroups_per_dimension, + limits.max_task_workgroup_total_count, + ) + } else { + ( + limits.max_mesh_workgroups_per_dimension, + limits.max_mesh_workgroup_total_count, + ) + }; + let total_count = check_workgroup_sizes( &[group_count_x, group_count_y, group_count_z], &[groups_size_limit, groups_size_limit, groups_size_limit], diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index 3988faea0..b3304ecd7 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -4642,6 +4642,7 @@ impl Device { }; let is_mesh = mesh_stage.is_some(); + let has_task_shader = task_stage.is_some(); let raw = { let pipeline_desc = hal::RenderPipelineDescriptor { label: desc.label.to_hal(self.instance_flags), @@ -4748,6 +4749,7 @@ impl Device { label: desc.label.to_string(), tracking_data: TrackingData::new(self.tracker_indices.render_pipelines.clone()), is_mesh, + has_task_shader, }; let pipeline = Arc::new(pipeline); diff --git a/wgpu-core/src/pipeline.rs b/wgpu-core/src/pipeline.rs index 08be9e710..5af0b6ec7 100644 --- a/wgpu-core/src/pipeline.rs +++ b/wgpu-core/src/pipeline.rs @@ -820,6 +820,7 @@ pub struct RenderPipeline { pub(crate) tracking_data: TrackingData, /// Whether this is a mesh shader pipeline pub(crate) is_mesh: bool, + pub(crate) has_task_shader: bool, } impl Drop for RenderPipeline { diff --git a/wgpu-hal/src/dx12/adapter.rs b/wgpu-hal/src/dx12/adapter.rs index 7d0858c52..da312bbe5 100644 --- a/wgpu-hal/src/dx12/adapter.rs +++ b/wgpu-hal/src/dx12/adapter.rs @@ -852,6 +852,19 @@ impl super::Adapter { ); } + // Source: https://microsoft.github.io/DirectX-Specs/d3d/MeshShader.html#dispatchmesh-api + let max_task_mesh_workgroup_total_count = if mesh_shader_supported { + 2u32.pow(22) + } else { + 0 + }; + // Technically it says "64k" but I highly doubt they want 65536 for compute and exactly 64,000 for task workgroups + let max_task_mesh_workgroups_per_dimension = if mesh_shader_supported { + Direct3D12::D3D12_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION + } else { + 0 + }; + Some(crate::ExposedAdapter { adapter: super::Adapter { raw: adapter, @@ -940,23 +953,16 @@ impl super::Adapter { // NATIVE (Non-WebGPU) LIMITS: // max_non_sampler_bindings: 1_000_000, - max_binding_array_elements_per_shader_stage: full_heap_count, max_binding_array_sampler_elements_per_shader_stage: Direct3D12::D3D12_MAX_SHADER_VISIBLE_SAMPLER_HEAP_SIZE, + max_task_workgroup_total_count: max_task_mesh_workgroup_total_count, + max_task_workgroups_per_dimension: max_task_mesh_workgroups_per_dimension, + max_mesh_workgroup_total_count: max_task_mesh_workgroup_total_count, + max_mesh_workgroups_per_dimension: max_task_mesh_workgroups_per_dimension, + // Source: https://microsoft.github.io/DirectX-Specs/d3d/MeshShader.html#dispatchmesh-api - max_task_mesh_workgroup_total_count: if mesh_shader_supported { - 2u32.pow(22) - } else { - 0 - }, - // Technically it says "64k" but I highly doubt they want 65536 for compute and exactly 64,000 for task workgroups - max_task_mesh_workgroups_per_dimension: if mesh_shader_supported { - Direct3D12::D3D12_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION - } else { - 0 - }, // Assume this inherits from compute shaders max_task_invocations_per_workgroup: if mesh_shader_supported { Direct3D12::D3D12_CS_4_X_THREAD_GROUP_MAX_THREADS_PER_GROUP diff --git a/wgpu-hal/src/dx12/device.rs b/wgpu-hal/src/dx12/device.rs index c23c14f98..2ca1c8039 100644 --- a/wgpu-hal/src/dx12/device.rs +++ b/wgpu-hal/src/dx12/device.rs @@ -1523,8 +1523,8 @@ impl crate::Device for super::Device { external_texture_binding_map, force_loop_bounding: true, task_dispatch_limits: Some(naga::back::TaskDispatchLimits { - max_mesh_workgroups_per_dim: self.limits.max_task_mesh_workgroups_per_dimension, - max_mesh_workgroups_total: self.limits.max_task_mesh_workgroup_total_count, + max_mesh_workgroups_per_dim: self.limits.max_mesh_workgroups_per_dimension, + max_mesh_workgroups_total: self.limits.max_mesh_workgroup_total_count, }), mesh_shader_primitive_indices_clamp: true, ray_query_initialization_tracking: true, diff --git a/wgpu-hal/src/gles/adapter.rs b/wgpu-hal/src/gles/adapter.rs index 11e7a4b17..c1e4ff25f 100644 --- a/wgpu-hal/src/gles/adapter.rs +++ b/wgpu-hal/src/gles/adapter.rs @@ -819,8 +819,10 @@ impl super::Adapter { max_buffer_size: i32::MAX as u64, max_non_sampler_bindings: u32::MAX, - max_task_mesh_workgroup_total_count: 0, - max_task_mesh_workgroups_per_dimension: 0, + max_task_workgroup_total_count: 0, + max_task_workgroups_per_dimension: 0, + max_mesh_workgroup_total_count: 0, + max_mesh_workgroups_per_dimension: 0, max_task_invocations_per_workgroup: 0, max_task_invocations_per_dimension: 0, max_mesh_invocations_per_workgroup: 0, diff --git a/wgpu-hal/src/metal/adapter.rs b/wgpu-hal/src/metal/adapter.rs index 23d35c1a9..0de4a1421 100644 --- a/wgpu-hal/src/metal/adapter.rs +++ b/wgpu-hal/src/metal/adapter.rs @@ -76,7 +76,7 @@ impl crate::Adapter for super::Adapter { unsafe fn open( &self, features: wgt::Features, - _limits: &wgt::Limits, + limits: &wgt::Limits, _memory_hints: &wgt::MemoryHints, ) -> Result, crate::DeviceError> { let queue = self @@ -116,6 +116,7 @@ impl crate::Adapter for super::Adapter { shared: Arc::clone(&self.shared), features, counters: Default::default(), + limits: limits.clone(), }, queue: super::Queue { shared: Arc::new(QueueShared { @@ -1067,7 +1068,26 @@ impl super::CapabilitiesQuery { }, // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf#page=4 mesh_shaders, - max_mesh_task_workgroup_count: if mesh_shaders { 1024 } else { 0 }, + max_task_workgroup_count: if mesh_shaders + && (metal4 || device.supportsFamily(MTLGPUFamily::Apple2)) + { + u32::MAX + } else if mesh_shaders { + 1024 + } else { + 0 + }, + max_mesh_workgroup_count: if mesh_shaders + && device.supportsFamily(MTLGPUFamily::Apple10) + { + 2u32.pow(22) + } else if mesh_shaders && device.supportsFamily(MTLGPUFamily::Apple9) { + 2u32.pow(20) + } else if mesh_shaders { + 1024 + } else { + 0 + }, max_task_payload_size: if mesh_shaders { 16384 - 32 } else { 0 }, supports_cooperative_matrix: family_check && (device.supportsFamily(MTLGPUFamily::Apple7) @@ -1201,7 +1221,14 @@ impl super::CapabilitiesQuery { features.insert(F::SUBGROUP | F::SUBGROUP_BARRIER); } - features.set(F::EXPERIMENTAL_MESH_SHADER, self.mesh_shaders); + features.set( + F::EXPERIMENTAL_MESH_SHADER | F::EXPERIMENTAL_MESH_SHADER_POINTS, + self.mesh_shaders, + ); + features.set( + F::EXPERIMENTAL_MESH_SHADER_MULTIVIEW, + self.supported_vertex_amplification_factor > 1 && self.mesh_shaders, + ); // Cooperative matrix (simdgroup matrix) requires MSL 2.3+ features.set( @@ -1326,8 +1353,10 @@ impl super::CapabilitiesQuery { }, // Should be not too large - max_task_mesh_workgroup_total_count: self.max_mesh_task_workgroup_count, - max_task_mesh_workgroups_per_dimension: self.max_mesh_task_workgroup_count, + max_task_workgroup_total_count: self.max_task_workgroup_count, + max_task_workgroups_per_dimension: self.max_task_workgroup_count, + max_mesh_workgroup_total_count: self.max_mesh_workgroup_count, + max_mesh_workgroups_per_dimension: self.max_mesh_workgroup_count, max_task_invocations_per_workgroup: if self.mesh_shaders { 1024 } else { 0 }, max_task_invocations_per_dimension: if self.mesh_shaders { 1024 } else { 0 }, max_mesh_invocations_per_workgroup: if self.mesh_shaders { 1024 } else { 0 }, diff --git a/wgpu-hal/src/metal/device.rs b/wgpu-hal/src/metal/device.rs index 586143ea3..032cfaef7 100644 --- a/wgpu-hal/src/metal/device.rs +++ b/wgpu-hal/src/metal/device.rs @@ -158,7 +158,7 @@ impl super::Device { let ep_resources = &layout.per_stage_map[naga_stage]; - let bounds_check_policy = if stage.module.bounds_checks.bounds_checks { + let bounds_check_policy = if stage.module.runtime_checks.bounds_checks { naga::proc::BoundsCheckPolicy::Restrict } else { naga::proc::BoundsCheckPolicy::Unchecked @@ -197,7 +197,21 @@ impl super::Device { binding_array: naga::proc::BoundsCheckPolicy::Unchecked, }, zero_initialize_workgroup_memory: stage.zero_initialize_workgroup_memory, - force_loop_bounding: stage.module.bounds_checks.force_loop_bounding, + force_loop_bounding: stage.module.runtime_checks.force_loop_bounding, + task_dispatch_limits: stage + .module + .runtime_checks + .task_shader_dispatch_tracking + .then_some(naga::back::TaskDispatchLimits { + max_mesh_workgroups_per_dim: self + .limits + .max_mesh_workgroups_per_dimension, + max_mesh_workgroups_total: self.limits.max_mesh_workgroup_total_count, + }), + mesh_shader_primitive_indices_clamp: stage + .module + .runtime_checks + .mesh_shader_primitive_indices_clamp, }; let pipeline_options = naga::back::msl::PipelineOptions { @@ -377,6 +391,7 @@ impl super::Device { pub unsafe fn device_from_raw( raw: Retained>, features: wgt::Features, + limits: &wgt::Limits, ) -> super::Device { let capabilities_query = super::CapabilitiesQuery::new(&raw); let shared = super::AdapterShared::new(raw, &capabilities_query); @@ -384,6 +399,7 @@ impl super::Device { shared: Arc::new(shared), features, counters: Default::default(), + limits: limits.clone(), } } @@ -1137,7 +1153,7 @@ impl crate::Device for super::Device { match shader { crate::ShaderInput::Naga(naga) => Ok(super::ShaderModule { source: ShaderModuleSource::Naga(naga), - bounds_checks: desc.runtime_checks, + runtime_checks: desc.runtime_checks, }), crate::ShaderInput::MetalLib { file, @@ -1155,7 +1171,7 @@ impl crate::Device for super::Device { num_workgroups, }), // This goes unused for passthrough shaders - bounds_checks: wgt::ShaderRuntimeChecks::unchecked(), + runtime_checks: wgt::ShaderRuntimeChecks::unchecked(), }) } crate::ShaderInput::Msl { @@ -1174,8 +1190,7 @@ impl crate::Device for super::Device { library, num_workgroups, }), - // This goes unused for passthrough shaders - bounds_checks: wgt::ShaderRuntimeChecks::unchecked(), + runtime_checks: desc.runtime_checks, }) } crate::ShaderInput::SpirV(_) diff --git a/wgpu-hal/src/metal/mod.rs b/wgpu-hal/src/metal/mod.rs index bec4cae6c..3ef5795da 100644 --- a/wgpu-hal/src/metal/mod.rs +++ b/wgpu-hal/src/metal/mod.rs @@ -310,7 +310,8 @@ struct CapabilitiesQuery { int64_atomics: bool, float_atomics: bool, mesh_shaders: bool, - max_mesh_task_workgroup_count: u32, + max_task_workgroup_count: u32, + max_mesh_workgroup_count: u32, max_task_payload_size: u32, supported_vertex_amplification_factor: u32, shader_barycentrics: bool, @@ -478,6 +479,7 @@ pub struct Device { shared: Arc, features: wgt::Features, counters: Arc, + limits: wgt::Limits, } pub struct Surface { @@ -870,7 +872,7 @@ unsafe impl Sync for PassthroughShader {} #[derive(Debug)] pub struct ShaderModule { source: ShaderModuleSource, - bounds_checks: wgt::ShaderRuntimeChecks, + runtime_checks: wgt::ShaderRuntimeChecks, } impl crate::DynShaderModule for ShaderModule {} diff --git a/wgpu-hal/src/vulkan/adapter.rs b/wgpu-hal/src/vulkan/adapter.rs index 8c236920d..0fc1ddc0d 100644 --- a/wgpu-hal/src/vulkan/adapter.rs +++ b/wgpu-hal/src/vulkan/adapter.rs @@ -1376,9 +1376,14 @@ impl PhysicalDeviceProperties { fn to_wgpu_limits(&self) -> wgt::Limits { let limits = &self.properties.limits; + // Default is only implemented for tuples up to a certain size. + let ( + mut max_task_workgroup_total_count, + mut max_task_workgroups_per_dimension, + mut max_mesh_workgroup_total_count, + mut max_mesh_workgroups_per_dimension, + ) = Default::default(); let ( - mut max_task_mesh_workgroup_total_count, - mut max_task_mesh_workgroups_per_dimension, mut max_task_invocations_per_workgroup, mut max_task_invocations_per_dimension, mut max_mesh_invocations_per_workgroup, @@ -1390,15 +1395,12 @@ impl PhysicalDeviceProperties { mut max_mesh_multiview_view_count, ) = Default::default(); if let Some(m) = self.mesh_shader { - max_task_mesh_workgroup_total_count = m - .max_task_work_group_total_count - .min(m.max_mesh_work_group_total_count); - max_task_mesh_workgroups_per_dimension = m - .max_task_work_group_count - .into_iter() - .chain(m.max_mesh_work_group_count) - .min() - .unwrap(); + max_task_workgroup_total_count = m.max_task_work_group_total_count; + max_task_workgroups_per_dimension = + m.max_task_work_group_count.into_iter().min().unwrap(); + max_mesh_workgroup_total_count = m.max_mesh_work_group_total_count; + max_mesh_workgroups_per_dimension = + m.max_mesh_work_group_count.into_iter().min().unwrap(); max_task_invocations_per_workgroup = m.max_task_work_group_invocations; max_task_invocations_per_dimension = m.max_task_work_group_size.into_iter().min().unwrap(); @@ -1666,8 +1668,11 @@ impl PhysicalDeviceProperties { 0 }, - max_task_mesh_workgroup_total_count, - max_task_mesh_workgroups_per_dimension, + max_task_workgroup_total_count, + max_task_workgroups_per_dimension, + max_mesh_workgroup_total_count, + max_mesh_workgroups_per_dimension, + max_task_invocations_per_workgroup, max_task_invocations_per_dimension, @@ -2690,8 +2695,8 @@ impl super::Adapter { binding_map: BTreeMap::default(), debug_info: None, task_dispatch_limits: Some(naga::back::TaskDispatchLimits { - max_mesh_workgroups_per_dim: limits.max_task_mesh_workgroups_per_dimension, - max_mesh_workgroups_total: limits.max_task_mesh_workgroup_total_count, + max_mesh_workgroups_per_dim: limits.max_mesh_workgroups_per_dimension, + max_mesh_workgroups_total: limits.max_mesh_workgroup_total_count, }), mesh_shader_primitive_indices_clamp: true, } diff --git a/wgpu-info/src/human.rs b/wgpu-info/src/human.rs index d12589fa2..5a406153e 100644 --- a/wgpu-info/src/human.rs +++ b/wgpu-info/src/human.rs @@ -182,8 +182,10 @@ fn print_adapter(output: &mut impl io::Write, report: &AdapterReport, idx: usize max_immediate_size, max_non_sampler_bindings, - max_task_mesh_workgroup_total_count, - max_task_mesh_workgroups_per_dimension, + max_task_workgroup_total_count, + max_task_workgroups_per_dimension, + max_mesh_workgroup_total_count, + max_mesh_workgroups_per_dimension, max_task_invocations_per_workgroup, max_task_invocations_per_dimension, max_mesh_invocations_per_workgroup, @@ -236,8 +238,10 @@ fn print_adapter(output: &mut impl io::Write, report: &AdapterReport, idx: usize writeln!(output, "\t\t Max Compute Workgroup Size Z: {max_compute_workgroup_size_z}")?; writeln!(output, "\t\t Max Compute Workgroups Per Dimension: {max_compute_workgroups_per_dimension}")?; - writeln!(output, "\t\t Max Task/Mesh Workgroup Total Count: {max_task_mesh_workgroup_total_count}")?; - writeln!(output, "\t\t Max Task/Mesh Workgroups Per Dimension: {max_task_mesh_workgroups_per_dimension}")?; + writeln!(output, "\t\t Max Task Workgroup Total Count: {max_task_workgroup_total_count}")?; + writeln!(output, "\t\t Max Task Workgroups Per Dimension: {max_task_workgroups_per_dimension}")?; + writeln!(output, "\t\t Max Mesh Workgroup Total Count: {max_mesh_workgroup_total_count}")?; + writeln!(output, "\t\t Max Mesh Workgroups Per Dimension: {max_mesh_workgroups_per_dimension}")?; writeln!(output, "\t\t Max Task Invocations Per Workgroup: {max_task_invocations_per_workgroup}")?; writeln!(output, "\t\t Max Task Invocations Per Dimension: {max_task_invocations_per_dimension}")?; writeln!(output, "\t\t Max Mesh Invocations Per Workgroup: {max_mesh_invocations_per_workgroup}")?; diff --git a/wgpu-types/src/limits.rs b/wgpu-types/src/limits.rs index a3f732fc9..c5b453746 100644 --- a/wgpu-types/src/limits.rs +++ b/wgpu-types/src/limits.rs @@ -67,8 +67,10 @@ macro_rules! with_limits { $macro_name!(max_immediate_size, Ordering::Less); $macro_name!(max_non_sampler_bindings, Ordering::Less); - $macro_name!(max_task_mesh_workgroup_total_count, Ordering::Less); - $macro_name!(max_task_mesh_workgroups_per_dimension, Ordering::Less); + $macro_name!(max_task_workgroup_total_count, Ordering::Less); + $macro_name!(max_task_workgroups_per_dimension, Ordering::Less); + $macro_name!(max_mesh_workgroup_total_count, Ordering::Less); + $macro_name!(max_mesh_workgroups_per_dimension, Ordering::Less); $macro_name!(max_task_invocations_per_workgroup, Ordering::Less); $macro_name!(max_task_invocations_per_dimension, Ordering::Less); $macro_name!(max_mesh_invocations_per_workgroup, Ordering::Less); @@ -254,12 +256,20 @@ pub struct Limits { /// to create many bind groups at the cost of a large up-front allocation at device creation. pub max_non_sampler_bindings: u32, - /// The maximum total value for a `RenderPass::draw_mesh_tasks(x, y, z)` operation or the - /// `@builtin(mesh_task_size)` returned from a task shader. Higher is "better". - pub max_task_mesh_workgroup_total_count: u32, - /// The maximum value for each dimension of a `RenderPass::draw_mesh_tasks(x, y, z)` operation. + /// The maximum total value for a `RenderPass::draw_mesh_tasks(x, y, z)` call on a mesh pipeline with a task shader. + /// Higher is "better". + pub max_task_workgroup_total_count: u32, + /// The maximum value for each dimension of a `RenderPass::draw_mesh_tasks(x, y, z)` call on a mesh pipeline with a task shader. + /// Higher is "better". + pub max_task_workgroups_per_dimension: u32, + /// The maximum product of arguments of a `RenderPass::draw_mesh_tasks(x, y, z)` operation on a mesh shader pipeline + /// without task shaders. /// Also for task shader outputs. Higher is "better". - pub max_task_mesh_workgroups_per_dimension: u32, + pub max_mesh_workgroup_total_count: u32, + /// The maximum value for each dimension of a `RenderPass::draw_mesh_tasks(x, y, z)` operation on a mesh shader pipeline + /// without task shaders. + /// Also for task shader outputs. Higher is "better". + pub max_mesh_workgroups_per_dimension: u32, // These are fundamentally different. It is very common for limits on mesh shaders to be much lower. /// Maximum total number of invocations, or threads, per task shader workgroup. Higher is "better". pub max_task_invocations_per_workgroup: u32, @@ -353,8 +363,10 @@ impl Limits { /// max_compute_workgroups_per_dimension: 65535, /// max_immediate_size: 0, /// max_non_sampler_bindings: 1_000_000, - /// max_task_mesh_workgroup_total_count: 0, - /// max_task_mesh_workgroups_per_dimension: 0, + /// max_task_workgroup_total_count: 0, + /// max_task_workgroups_per_dimension: 0, + /// max_mesh_workgroup_total_count: 0, + /// max_mesh_workgroups_per_dimension: 0, /// max_task_invocations_per_workgroup: 0, /// max_task_invocations_per_dimension: 0, /// max_mesh_invocations_per_workgroup: 0, @@ -413,8 +425,10 @@ impl Limits { max_immediate_size: 0, max_non_sampler_bindings: 1_000_000, - max_task_mesh_workgroup_total_count: 0, - max_task_mesh_workgroups_per_dimension: 0, + max_task_workgroup_total_count: 0, + max_task_workgroups_per_dimension: 0, + max_mesh_workgroup_total_count: 0, + max_mesh_workgroups_per_dimension: 0, max_task_invocations_per_workgroup: 0, max_task_invocations_per_dimension: 0, max_mesh_invocations_per_workgroup: 0, @@ -476,8 +490,10 @@ impl Limits { /// max_buffer_size: 256 << 20, // (256 MiB) /// max_non_sampler_bindings: 1_000_000, /// - /// max_task_mesh_workgroup_total_count: 0, - /// max_task_mesh_workgroups_per_dimension: 0, + /// max_task_workgroup_total_count: 0, + /// max_task_workgroups_per_dimension: 0, + /// max_mesh_workgroup_total_count: 0, + /// max_mesh_workgroups_per_dimension: 0, /// max_task_invocations_per_workgroup: 0, /// max_task_invocations_per_dimension: 0, /// max_mesh_invocations_per_workgroup: 0, @@ -555,8 +571,10 @@ impl Limits { /// max_buffer_size: 256 << 20, // (256 MiB), /// max_non_sampler_bindings: 1_000_000, /// - /// max_task_mesh_workgroup_total_count: 0, - /// max_task_mesh_workgroups_per_dimension: 0, + /// max_task_workgroup_total_count: 0, + /// max_task_workgroups_per_dimension: 0, + /// max_mesh_workgroup_total_count: 0, + /// max_mesh_workgroups_per_dimension: 0, /// max_task_invocations_per_workgroup: 0, /// max_task_invocations_per_dimension: 0, /// max_mesh_invocations_per_workgroup: 0, @@ -650,8 +668,10 @@ impl Limits { max_immediate_size: ALLOC_MAX_U32, max_non_sampler_bindings: ALLOC_MAX_U32, - max_task_mesh_workgroup_total_count: ALLOC_MAX_U32, - max_task_mesh_workgroups_per_dimension: ALLOC_MAX_U32, + max_task_workgroup_total_count: ALLOC_MAX_U32, + max_task_workgroups_per_dimension: ALLOC_MAX_U32, + max_mesh_workgroup_total_count: ALLOC_MAX_U32, + max_mesh_workgroups_per_dimension: ALLOC_MAX_U32, max_task_invocations_per_workgroup: ALLOC_MAX_U32, max_task_invocations_per_dimension: ALLOC_MAX_U32, max_mesh_invocations_per_workgroup: ALLOC_MAX_U32, @@ -731,10 +751,17 @@ impl Limits { #[must_use] pub const fn using_recommended_minimum_mesh_shader_values(self) -> Self { Self { - // This limitation comes from metal - max_task_mesh_workgroup_total_count: 1024, - // This is a DirectX limitation - max_task_mesh_workgroups_per_dimension: 256, + // These are DirectX limitations (both nvidia and AMD match these exactly on vulkan) + // Note that Mac2 (newest intel macs) support up to 1024, but this is low enough, + // to make use of mesh shaders nonviable in most cases. + // We therefore, don't expose mesh shading on these devices. + // In contrast, here is no limit for any A-series or M-series chip. + max_task_workgroup_total_count: 2u32.pow(22), + max_task_workgroups_per_dimension: 65535, + // These are metal limitations + // M3 ups both of these to 1M + max_mesh_workgroup_total_count: 1024, + max_mesh_workgroups_per_dimension: 1024, // Nvidia limit on vulkan max_task_invocations_per_workgroup: 128, max_task_invocations_per_dimension: 64, diff --git a/wgpu/src/backend/webgpu.rs b/wgpu/src/backend/webgpu.rs index 060868f2a..a7229b057 100644 --- a/wgpu/src/backend/webgpu.rs +++ b/wgpu/src/backend/webgpu.rs @@ -829,10 +829,10 @@ fn map_wgt_limits(limits: webgpu_sys::GpuSupportedLimits) -> wgt::Limits { max_immediate_size: wgt::Limits::default().max_immediate_size, max_non_sampler_bindings: wgt::Limits::default().max_non_sampler_bindings, - max_task_mesh_workgroup_total_count: wgt::Limits::default() - .max_task_mesh_workgroup_total_count, - max_task_mesh_workgroups_per_dimension: wgt::Limits::default() - .max_task_mesh_workgroups_per_dimension, + max_task_workgroup_total_count: wgt::Limits::default().max_task_workgroup_total_count, + max_task_workgroups_per_dimension: wgt::Limits::default().max_task_workgroups_per_dimension, + max_mesh_workgroup_total_count: wgt::Limits::default().max_mesh_workgroup_total_count, + max_mesh_workgroups_per_dimension: wgt::Limits::default().max_mesh_workgroups_per_dimension, max_task_invocations_per_workgroup: wgt::Limits::default() .max_task_invocations_per_workgroup, max_task_invocations_per_dimension: wgt::Limits::default()