diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e07c6bc6..08bdbcaa8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,6 +60,7 @@ Bottom level categories: - `front::wgsl::Frontend::set_options` - `ir::Block::is_empty` - `ir::Block::len` +- Changed passthrough shaders to not require an entry point parameter, so that the same shader module may be used in multiple entry points. Also added support for metallib passthrough. By @inner-daemons in #8886. #### naga diff --git a/Cargo.lock b/Cargo.lock index 8fcec0601..0cda6a5b9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1178,6 +1178,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89a09f22a6c6069a18470eb92d2298acf25463f14256d24778e1230d789a2aec" dependencies = [ "bitflags 2.10.0", + "block2 0.6.2", + "libc", "objc2 0.6.3", ] @@ -2843,6 +2845,7 @@ checksum = "a0125f776a10d00af4152d74616409f0d4a2053a6f57fa5b7d6aa2854ac04794" dependencies = [ "bitflags 2.10.0", "block2 0.6.2", + "dispatch2", "objc2 0.6.3", "objc2-foundation 0.3.2", ] @@ -4875,6 +4878,7 @@ dependencies = [ "bytemuck", "cfg-if", "cfg_aliases 0.2.1", + "dispatch2", "env_logger", "glam", "glow", diff --git a/Cargo.toml b/Cargo.toml index 20b8dce42..9813a3d02 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -203,6 +203,7 @@ xshell = "0.2.2" # Metal dependencies block2 = "0.6.2" +dispatch2 = "0.3.0" objc2 = "0.6.3" objc2-core-foundation = { version = "0.3.2", default-features = false, features = [ "std", @@ -218,6 +219,7 @@ objc2-foundation = { version = "0.3.2", default-features = false, features = [ objc2-metal = { version = "0.3.2", default-features = false, features = [ "std", "block2", + "dispatch2", "MTLAllocation", "MTLBlitCommandEncoder", "MTLBlitPass", diff --git a/examples/features/src/mesh_shader/mod.rs b/examples/features/src/mesh_shader/mod.rs index 20f069e9a..bd36ed97f 100644 --- a/examples/features/src/mesh_shader/mod.rs +++ b/examples/features/src/mesh_shader/mod.rs @@ -29,7 +29,6 @@ fn compile_hlsl(device: &wgpu::Device, entry: &str, stage_str: &str) -> wgpu::Sh std::fs::remove_file(out_path).unwrap(); unsafe { device.create_shader_module_passthrough(wgpu::ShaderModuleDescriptorPassthrough { - entry_point: entry.to_owned(), label: None, num_workgroups: (1, 1, 1), dxil: Some(std::borrow::Cow::Owned(file)), @@ -38,10 +37,9 @@ fn compile_hlsl(device: &wgpu::Device, entry: &str, stage_str: &str) -> wgpu::Sh } } -fn compile_msl(device: &wgpu::Device, entry: &str) -> wgpu::ShaderModule { +fn compile_msl(device: &wgpu::Device) -> wgpu::ShaderModule { unsafe { device.create_shader_module_passthrough(wgpu::ShaderModuleDescriptorPassthrough { - entry_point: entry.to_owned(), label: None, msl: Some(std::borrow::Cow::Borrowed(include_str!("shader.metal"))), num_workgroups: (1, 1, 1), @@ -50,6 +48,53 @@ fn compile_msl(device: &wgpu::Device, entry: &str) -> wgpu::ShaderModule { } } +struct Shaders { + ts: wgpu::ShaderModule, + ms: wgpu::ShaderModule, + fs: wgpu::ShaderModule, + ts_name: &'static str, + ms_name: &'static str, + 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 EXPERIMENTAL_PASSTHROUGH_SHADERS. + match backend { + wgpu::Backend::Vulkan => { + 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::Dx12 => Shaders { + ts: compile_hlsl(device, "Task", "as"), + ms: compile_hlsl(device, "Mesh", "ms"), + fs: compile_hlsl(device, "Frag", "ps"), + ts_name: "main", + ms_name: "main", + fs_name: "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!(), + } +} + pub struct Example { pipeline: wgpu::RenderPipeline, } @@ -60,33 +105,14 @@ impl crate::framework::Example for Example { device: &wgpu::Device, _queue: &wgpu::Queue, ) -> Self { - let (ts, ms, fs, ts_name, ms_name, fs_name) = match adapter.get_info().backend { - wgpu::Backend::Vulkan => ( - compile_wgsl(device), - compile_wgsl(device), - compile_wgsl(device), - "ts_main", - "ms_main", - "fs_main", - ), - wgpu::Backend::Dx12 => ( - compile_hlsl(device, "Task", "as"), - compile_hlsl(device, "Mesh", "ms"), - compile_hlsl(device, "Frag", "ps"), - "main", - "main", - "main", - ), - wgpu::Backend::Metal => ( - compile_msl(device, "taskShader"), - compile_msl(device, "meshShader"), - compile_msl(device, "fragShader"), - "main", - "main", - "main", - ), - _ => panic!("Example can currently only run on vulkan, dx12 or metal"), - }; + let Shaders { + ts, + ms, + fs, + ts_name, + ms_name, + fs_name, + } = get_shaders(device, adapter.get_info().backend); let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: None, bind_group_layouts: &[], diff --git a/player/src/lib.rs b/player/src/lib.rs index 1de984635..2092c1756 100644 --- a/player/src/lib.rs +++ b/player/src/lib.rs @@ -268,10 +268,8 @@ impl Player { Action::CreateShaderModulePassthrough { id, data, - entry_point, label, num_workgroups, - runtime_checks, } => { let spirv = data.iter().find_map(|a| { if a.kind() == DataKind::Spv { @@ -289,6 +287,9 @@ impl Player { let hlsl = data .iter() .find_map(|a| (a.kind() == DataKind::Hlsl).then(|| loader.load_utf8(a))); + let metallib = data + .iter() + .find_map(|a| (a.kind() == DataKind::MetalLib).then(|| loader.load(a))); let msl = data .iter() .find_map(|a| (a.kind() == DataKind::Msl).then(|| loader.load_utf8(a))); @@ -299,14 +300,13 @@ impl Player { .iter() .find_map(|a| (a.kind() == DataKind::Wgsl).then(|| loader.load_utf8(a))); let desc = wgt::CreateShaderModuleDescriptorPassthrough { - entry_point, label, num_workgroups, - runtime_checks, spirv, dxil, hlsl, + metallib, msl, glsl, wgsl, diff --git a/tests/tests/wgpu-gpu/main.rs b/tests/tests/wgpu-gpu/main.rs index 235de5091..2689e8410 100644 --- a/tests/tests/wgpu-gpu/main.rs +++ b/tests/tests/wgpu-gpu/main.rs @@ -42,6 +42,7 @@ mod occlusion_query; mod oob_indexing; mod oom; mod pass_ops; +mod passthrough; mod per_vertex; mod pipeline; mod pipeline_cache; @@ -107,6 +108,7 @@ fn all_tests() -> Vec { oob_indexing::all_tests(&mut tests); oom::all_tests(&mut tests); pass_ops::all_tests(&mut tests); + passthrough::all_tests(&mut tests); per_vertex::all_tests(&mut tests); pipeline_cache::all_tests(&mut tests); pipeline::all_tests(&mut tests); diff --git a/tests/tests/wgpu-gpu/mesh_shader/mod.rs b/tests/tests/wgpu-gpu/mesh_shader/mod.rs index f188e742d..6cb3e0468 100644 --- a/tests/tests/wgpu-gpu/mesh_shader/mod.rs +++ b/tests/tests/wgpu-gpu/mesh_shader/mod.rs @@ -29,6 +29,7 @@ fn compile_wgsl(device: &wgpu::Device) -> wgpu::ShaderModule { source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()), }) } + fn compile_hlsl( device: &wgpu::Device, entry: &str, @@ -62,7 +63,6 @@ fn compile_hlsl( std::fs::remove_file(out_path).unwrap(); unsafe { device.create_shader_module_passthrough(wgpu::ShaderModuleDescriptorPassthrough { - entry_point: entry.to_owned(), label: None, num_workgroups: (1, 1, 1), dxil: Some(std::borrow::Cow::Owned(file)), @@ -71,10 +71,9 @@ fn compile_hlsl( } } -fn compile_msl(device: &wgpu::Device, entry: &str) -> wgpu::ShaderModule { +fn compile_msl(device: &wgpu::Device) -> wgpu::ShaderModule { unsafe { device.create_shader_module_passthrough(wgpu::ShaderModuleDescriptorPassthrough { - entry_point: entry.to_owned(), label: None, msl: Some(std::borrow::Cow::Borrowed(include_str!("shader.metal"))), num_workgroups: (1, 1, 1), @@ -82,70 +81,75 @@ fn compile_msl(device: &wgpu::Device, entry: &str) -> wgpu::ShaderModule { }) } } - +struct Shaders { + ts: Option, + ms: wgpu::ShaderModule, + fs: Option, + ts_name: &'static str, + ms_name: &'static str, + fs_name: &'static str, +} fn get_shaders( device: &wgpu::Device, backend: wgpu::Backend, test_name: &str, info: &MeshPipelineTestInfo, -) -> ( - Option, - wgpu::ShaderModule, - Option, - &'static str, - &'static str, - &'static str, -) { +) -> 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 EXPERIMENTAL_PASSTHROUGH_SHADERS. match backend { - wgpu::Backend::Vulkan => ( - info.use_task.then(|| compile_wgsl(device)), - compile_wgsl(device), - info.use_frag.then(|| compile_wgsl(device)), - "ts_main", - if info.divergent { - "ms_divergent" - } else if info.use_task { - "ms_main" - } else { - "ms_no_ts" - }, - "fs_main", - ), - wgpu::Backend::Dx12 => ( - info.use_task + wgpu::Backend::Vulkan => { + 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::Dx12 => Shaders { + ts: info + .use_task .then(|| compile_hlsl(device, "Task", "as", test_name)), - compile_hlsl( + ms: compile_hlsl( device, if info.use_task { "Mesh" } else { "MeshNoTask" }, "ms", test_name, ), - info.use_frag + fs: info + .use_frag .then(|| compile_hlsl(device, "Frag", "ps", test_name)), - "main", - "main", - "main", - ), - wgpu::Backend::Metal => ( - info.use_task.then(|| compile_msl(device, "taskShader")), - compile_msl( - device, - if info.use_task { + ts_name: "main", + ms_name: "main", + fs_name: "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" }, - ), - info.use_frag.then(|| compile_msl(device, "fragShader")), - "main", - "main", - "main", - ), + fs_name: "fragShader", + } + } _ => unreachable!(), } } @@ -198,8 +202,14 @@ fn mesh_pipeline_build(ctx: &TestingContext, info: MeshPipelineTestInfo) { let (_depth_image, depth_view, depth_state) = create_depth(device); let test_hash = hash_testing_context(ctx).to_string(); - let (task, mesh, frag, ts_name, ms_name, fs_name) = - get_shaders(device, backend, &test_hash, &info); + let Shaders { + ts, + ms, + fs, + ts_name, + ms_name, + fs_name, + } = get_shaders(device, backend, &test_hash, &info); let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: None, bind_group_layouts: &[], @@ -208,17 +218,17 @@ fn mesh_pipeline_build(ctx: &TestingContext, info: MeshPipelineTestInfo) { let pipeline = device.create_mesh_pipeline(&wgpu::MeshPipelineDescriptor { label: None, layout: Some(&layout), - task: task.as_ref().map(|task| wgpu::TaskState { + task: ts.as_ref().map(|task| wgpu::TaskState { module: task, entry_point: Some(ts_name), compilation_options: Default::default(), }), mesh: wgpu::MeshState { - module: &mesh, + module: &ms, entry_point: Some(ms_name), compilation_options: Default::default(), }, - fragment: frag.as_ref().map(|frag| wgpu::FragmentState { + fragment: fs.as_ref().map(|frag| wgpu::FragmentState { module: frag, entry_point: Some(fs_name), targets: &[], @@ -277,9 +287,15 @@ fn mesh_draw(ctx: &TestingContext, draw_type: DrawType, info: MeshPipelineTestIn let (_depth_image, depth_view, depth_state) = create_depth(device); let test_hash = hash_testing_context(ctx).to_string(); - let (task, mesh, frag, ts_name, ms_name, fs_name) = - get_shaders(device, backend, &test_hash, &info); - let frag = frag.unwrap(); + let Shaders { + ts, + ms, + fs, + ts_name, + ms_name, + fs_name, + } = get_shaders(device, backend, &test_hash, &info); + let frag = fs.unwrap(); let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: None, bind_group_layouts: &[], @@ -288,13 +304,13 @@ fn mesh_draw(ctx: &TestingContext, draw_type: DrawType, info: MeshPipelineTestIn let pipeline = device.create_mesh_pipeline(&wgpu::MeshPipelineDescriptor { label: None, layout: Some(&layout), - task: task.as_ref().map(|task| wgpu::TaskState { + task: ts.as_ref().map(|task| wgpu::TaskState { module: task, entry_point: Some(ts_name), compilation_options: Default::default(), }), mesh: wgpu::MeshState { - module: &mesh, + module: &ms, entry_point: Some(ms_name), compilation_options: Default::default(), }, diff --git a/tests/tests/wgpu-gpu/passthrough/mod.rs b/tests/tests/wgpu-gpu/passthrough/mod.rs new file mode 100644 index 000000000..6b353c979 --- /dev/null +++ b/tests/tests/wgpu-gpu/passthrough/mod.rs @@ -0,0 +1,545 @@ +use std::{ + borrow::Cow, + hash::{DefaultHasher, Hash, Hasher}, +}; + +use wgpu::{ + Backends, ColorTargetState, ColorWrites, Features, FragmentState, MultisampleState, + PipelineLayoutDescriptor, RenderPipelineDescriptor, VertexState, +}; +use wgpu_test::{ + gpu_test, FailureCase, GpuTestConfiguration, GpuTestInitializer, TestParameters, TestingContext, +}; +use wgpu_types::CreateShaderModuleDescriptorPassthrough; + +pub fn all_tests(tests: &mut Vec) { + tests.push(METAL_PASSTHROUGH_SHADER); + tests.push(METALLIB_PASSTHROUGH_SHADER); + tests.push(HLSL_PASSTHROUGH_SHADER); + tests.push(DXIL_PASSTHROUGH_SHADER); + tests.push(SPIRV_PASSTHROUGH_SHADER); + tests.push(GLSL_PASSTHROUGH_SHADER); + tests.push(WGSL_PASSTHROUGH_SHADER); + tests.push(ALL_PASSTHROUGH_SHADERS_BINARY); + tests.push(ALL_PASSTHROUGH_SHADERS_SOURCE); + tests.push(PASSTHROUGH_SHADERS_EXPLICIT_LAYOUT_VALIDATION); +} + +fn test_hash(ctx: &TestingContext, name: &str) -> u64 { + let mut hasher = DefaultHasher::new(); + ctx.hash(&mut hasher); + name.hash(&mut hasher); + hasher.finish() +} + +fn test_with_module(ctx: TestingContext, vertex: wgpu::ShaderModule, fragment: wgpu::ShaderModule) { + let layout = ctx + .device + .create_pipeline_layout(&PipelineLayoutDescriptor { + label: None, + bind_group_layouts: &[], + immediate_size: 0, + }); + let _pipeline = ctx + .device + .create_render_pipeline(&RenderPipelineDescriptor { + label: None, + layout: Some(&layout), + vertex: VertexState { + module: &vertex, + entry_point: Some("vertex_main"), + compilation_options: Default::default(), + buffers: &[], + }, + primitive: Default::default(), + depth_stencil: None, + multisample: MultisampleState::default(), + fragment: Some(FragmentState { + module: &fragment, + entry_point: Some("fragment_main"), + compilation_options: Default::default(), + targets: &[Some(ColorTargetState { + format: wgpu::TextureFormat::Rgba8Unorm, + blend: None, + write_mask: ColorWrites::all(), + })], + }), + multiview_mask: None, + cache: None, + }); +} + +fn metal_source() -> Cow<'static, str> { + Cow::Borrowed(include_str!("shader.metal")) +} + +fn metal_test(ctx: TestingContext) { + let module = unsafe { + ctx.device + .create_shader_module_passthrough(CreateShaderModuleDescriptorPassthrough { + label: None, + num_workgroups: (0, 0, 0), + msl: Some(metal_source()), + ..Default::default() + }) + }; + test_with_module(ctx, module.clone(), module); +} + +#[gpu_test] +static METAL_PASSTHROUGH_SHADER: GpuTestConfiguration = GpuTestConfiguration::new() + .parameters( + TestParameters::default() + .features(Features::EXPERIMENTAL_PASSTHROUGH_SHADERS) + .skip(FailureCase::backend(!Backends::METAL)), + ) + .run_sync(metal_test); + +fn metallib_source(test_hash: u64) -> Cow<'static, [u8]> { + if cfg!(not(target_vendor = "apple")) { + return Cow::Borrowed(&[]); + } + let metal_compiler = std::process::Command::new("xcrun") + .args(["--find", "metal"]) + .status() + .is_ok_and(|a| a.success()); + let metallib_linker = std::process::Command::new("xcrun") + .args(["--find", "metallib"]) + .status() + .is_ok_and(|a| a.success()); + if !metal_compiler || !metallib_linker { + panic!("Metal compiler or metallib linker not present. Most users can safely ignore this."); + } + let air_name = format!( + "{}/tests/wgpu-gpu/passthrough/shader{test_hash}.air", + env!("CARGO_MANIFEST_DIR") + ); + let output_name = format!( + "{}/tests/wgpu-gpu/passthrough/shader{test_hash}.metallib", + env!("CARGO_MANIFEST_DIR") + ); + + { + let output = std::process::Command::new("xcrun") + .args([ + "metal", + "-c", + &format!( + "{}/tests/wgpu-gpu/passthrough/shader.metal", + env!("CARGO_MANIFEST_DIR") + ), + "-o", + &air_name, + ]) + .output() + .unwrap(); + if !output.status.success() { + panic!( + "Failed to compile .metal into .air: {}", + String::from_utf8(output.stderr).unwrap() + ); + } + } + { + let output = std::process::Command::new("xcrun") + .args(["metallib", &air_name, "-o", &output_name]) + .output() + .unwrap(); + if !output.status.success() { + panic!( + "Failed to compile .air into .metallib: {}", + String::from_utf8(output.stderr).unwrap() + ); + } + } + let source = std::fs::read(&output_name).unwrap(); + Cow::Owned(source) +} + +fn metallib_test(ctx: TestingContext) { + let test_hash = test_hash(&ctx, "metallib_test"); + let source = metallib_source(test_hash); + let module = unsafe { + ctx.device + .create_shader_module_passthrough(CreateShaderModuleDescriptorPassthrough { + label: None, + num_workgroups: (0, 0, 0), + metallib: Some(std::borrow::Cow::Borrowed(&source)), + ..Default::default() + }) + }; + test_with_module(ctx, module.clone(), module); +} + +#[gpu_test] +static METALLIB_PASSTHROUGH_SHADER: GpuTestConfiguration = GpuTestConfiguration::new() + .parameters( + TestParameters::default() + .features(Features::EXPERIMENTAL_PASSTHROUGH_SHADERS) + .skip(FailureCase::backend(!Backends::METAL)), + ) + .run_sync(metallib_test); + +fn hlsl_source() -> Cow<'static, str> { + std::borrow::Cow::Borrowed(include_str!("shader.hlsl")) +} + +fn hlsl_test(ctx: TestingContext) { + let module = unsafe { + ctx.device + .create_shader_module_passthrough(CreateShaderModuleDescriptorPassthrough { + hlsl: Some(hlsl_source()), + ..Default::default() + }) + }; + test_with_module(ctx, module.clone(), module); +} + +#[gpu_test] +static HLSL_PASSTHROUGH_SHADER: GpuTestConfiguration = GpuTestConfiguration::new() + .parameters( + TestParameters::default() + .features(Features::EXPERIMENTAL_PASSTHROUGH_SHADERS) + .skip(FailureCase::backend(!Backends::DX12)), + ) + .run_sync(hlsl_test); + +fn compile_dxil(entry: &str, stage_str: &str, test_hash: u64) -> Cow<'static, [u8]> { + let out_path = format!( + "{}/tests/wgpu-gpu/passthrough/shader{test_hash}.{stage_str}.cso", + env!("CARGO_MANIFEST_DIR") + ); + let cmd = std::process::Command::new("dxc") + .args([ + "-T", + &format!("{stage_str}_6_3"), + "-E", + entry, + &format!( + "{}/tests/wgpu-gpu/passthrough/shader.hlsl", + env!("CARGO_MANIFEST_DIR") + ), + "-Fo", + &out_path, + ]) + .output() + .unwrap(); + let file = std::fs::read(&out_path); + let _ = std::fs::remove_file(out_path); + // Remove the file before checking for status + if !cmd.status.success() { + panic!("DXC failed:\n{}", String::from_utf8(cmd.stderr).unwrap()); + } + let file = file.unwrap(); + Cow::Owned(file) +} + +fn dxil_vertex_source(test_hash: u64) -> Cow<'static, [u8]> { + if cfg!(target_os = "windows") { + compile_dxil("vertex_main", "vs", test_hash) + } else { + Cow::Borrowed(&[]) + } +} + +fn dxil_fragment_source(test_hash: u64) -> Cow<'static, [u8]> { + if cfg!(target_os = "windows") { + compile_dxil("fragment_main", "ps", test_hash) + } else { + Cow::Borrowed(&[]) + } +} + +fn dxil_test(ctx: TestingContext) { + let test_hash = test_hash(&ctx, "dxil_test"); + let vertex_source = dxil_vertex_source(test_hash); + let vertex = unsafe { + ctx.device + .create_shader_module_passthrough(wgpu::ShaderModuleDescriptorPassthrough { + label: None, + num_workgroups: (1, 1, 1), + dxil: Some(vertex_source), + ..Default::default() + }) + }; + let fragment_source = dxil_fragment_source(test_hash); + let fragment = unsafe { + ctx.device + .create_shader_module_passthrough(wgpu::ShaderModuleDescriptorPassthrough { + label: None, + num_workgroups: (1, 1, 1), + dxil: Some(fragment_source), + ..Default::default() + }) + }; + test_with_module(ctx, vertex, fragment); +} + +#[gpu_test] +static DXIL_PASSTHROUGH_SHADER: GpuTestConfiguration = GpuTestConfiguration::new() + .parameters( + TestParameters::default() + .features(Features::EXPERIMENTAL_PASSTHROUGH_SHADERS) + .skip(FailureCase::backend(!Backends::DX12)), + ) + .run_sync(dxil_test); + +fn spirv_source(test_hash: u64) -> Cow<'static, [u32]> { + let out_path = format!( + "{}/tests/wgpu-gpu/passthrough/shade{test_hash}.spv", + env!("CARGO_MANIFEST_DIR") + ); + let cmd = std::process::Command::new("dxc") + .args([ + "-spirv", + "-T", + "lib_6_3", + "-fspv-target-env=vulkan1.0", + // We need to tell it to compile for SPIRV which requires different info + "-D", + "SPIRV", + &format!( + "{}/tests/wgpu-gpu/passthrough/shader.hlsl", + env!("CARGO_MANIFEST_DIR") + ), + "-Fo", + &out_path, + ]) + .output() + .unwrap(); + let file = std::fs::read(&out_path); + let _ = std::fs::remove_file(out_path); + // Remove the file before checking for status + if !cmd.status.success() { + panic!("DXC failed:\n{}", String::from_utf8(cmd.stderr).unwrap()); + } + let file = file.unwrap(); + let spirv = bytemuck::pod_collect_to_vec::(&file); + Cow::Owned(spirv) +} + +fn spirv_test(ctx: TestingContext) { + let test_hash = test_hash(&ctx, "spirv_test"); + let module = unsafe { + ctx.device + .create_shader_module_passthrough(CreateShaderModuleDescriptorPassthrough { + spirv: Some(spirv_source(test_hash)), + ..Default::default() + }) + }; + test_with_module(ctx, module.clone(), module); +} + +#[gpu_test] +static SPIRV_PASSTHROUGH_SHADER: GpuTestConfiguration = GpuTestConfiguration::new() + .parameters( + TestParameters::default() + .features(Features::EXPERIMENTAL_PASSTHROUGH_SHADERS) + .skip(FailureCase::backend(!Backends::VULKAN)), + ) + .run_sync(spirv_test); + +fn glsl_vertex_source() -> Cow<'static, str> { + std::borrow::Cow::Borrowed(include_str!("shader.vert")) +} + +fn glsl_fragment_source() -> Cow<'static, str> { + std::borrow::Cow::Borrowed(include_str!("shader.frag")) +} + +fn glsl_test(ctx: TestingContext) { + let vertex = unsafe { + ctx.device + .create_shader_module_passthrough(CreateShaderModuleDescriptorPassthrough { + glsl: Some(glsl_vertex_source()), + ..Default::default() + }) + }; + let fragment = unsafe { + ctx.device + .create_shader_module_passthrough(CreateShaderModuleDescriptorPassthrough { + glsl: Some(glsl_fragment_source()), + ..Default::default() + }) + }; + test_with_module(ctx, vertex, fragment); +} + +#[gpu_test] +static GLSL_PASSTHROUGH_SHADER: GpuTestConfiguration = GpuTestConfiguration::new() + .parameters( + TestParameters::default() + .features(Features::EXPERIMENTAL_PASSTHROUGH_SHADERS) + .skip(FailureCase::backend(!Backends::GL)), + ) + .run_sync(glsl_test); + +fn wgsl_source() -> Cow<'static, str> { + std::borrow::Cow::Borrowed(include_str!("shader.wgsl")) +} + +fn wgsl_test(ctx: TestingContext) { + let module = unsafe { + ctx.device + .create_shader_module_passthrough(CreateShaderModuleDescriptorPassthrough { + wgsl: Some(wgsl_source()), + ..Default::default() + }) + }; + test_with_module(ctx, module.clone(), module); +} + +#[gpu_test] +static WGSL_PASSTHROUGH_SHADER: GpuTestConfiguration = GpuTestConfiguration::new() + .parameters( + TestParameters::default() + .features(Features::EXPERIMENTAL_PASSTHROUGH_SHADERS) + .skip(FailureCase::backend(!Backends::BROWSER_WEBGPU)), + ) + .run_sync(wgsl_test); + +fn all_passthrough_shaders_binary(ctx: TestingContext) { + let test_hash = test_hash(&ctx, "all_passthrough_binary"); + let vertex = unsafe { + ctx.device + .create_shader_module_passthrough(CreateShaderModuleDescriptorPassthrough { + label: None, + num_workgroups: (0, 0, 0), + spirv: Some(spirv_source(test_hash)), + dxil: Some(dxil_vertex_source(test_hash)), + hlsl: None, + metallib: Some(metallib_source(test_hash)), + msl: None, + glsl: Some(glsl_vertex_source()), + wgsl: Some(wgsl_source()), + }) + }; + let fragment = unsafe { + ctx.device + .create_shader_module_passthrough(CreateShaderModuleDescriptorPassthrough { + label: None, + num_workgroups: (0, 0, 0), + spirv: Some(spirv_source(test_hash)), + dxil: Some(dxil_fragment_source(test_hash)), + hlsl: None, + metallib: Some(metallib_source(test_hash)), + msl: None, + glsl: Some(glsl_fragment_source()), + wgsl: Some(wgsl_source()), + }) + }; + test_with_module(ctx, vertex, fragment); +} + +#[gpu_test] +static ALL_PASSTHROUGH_SHADERS_BINARY: GpuTestConfiguration = GpuTestConfiguration::new() + .parameters(TestParameters::default().features(Features::EXPERIMENTAL_PASSTHROUGH_SHADERS)) + .run_sync(all_passthrough_shaders_binary); + +fn all_passthrough_shader_source(ctx: TestingContext) { + let test_hash = test_hash(&ctx, "all_passthrough_source"); + let vertex = unsafe { + ctx.device + .create_shader_module_passthrough(CreateShaderModuleDescriptorPassthrough { + label: None, + num_workgroups: (0, 0, 0), + spirv: Some(spirv_source(test_hash)), + dxil: None, + hlsl: Some(hlsl_source()), + metallib: None, + msl: Some(metal_source()), + glsl: Some(glsl_vertex_source()), + wgsl: Some(wgsl_source()), + }) + }; + let fragment = unsafe { + ctx.device + .create_shader_module_passthrough(CreateShaderModuleDescriptorPassthrough { + label: None, + num_workgroups: (0, 0, 0), + spirv: Some(spirv_source(test_hash)), + dxil: None, + hlsl: Some(hlsl_source()), + metallib: None, + msl: Some(metal_source()), + glsl: Some(glsl_fragment_source()), + wgsl: Some(wgsl_source()), + }) + }; + test_with_module(ctx, vertex, fragment); +} + +#[gpu_test] +static ALL_PASSTHROUGH_SHADERS_SOURCE: GpuTestConfiguration = GpuTestConfiguration::new() + .parameters(TestParameters::default().features(Features::EXPERIMENTAL_PASSTHROUGH_SHADERS)) + .run_sync(all_passthrough_shader_source); + +fn explicit_layout_validation(ctx: TestingContext) { + let test_hash = test_hash(&ctx, "explicit_layout_validation"); + let vertex = unsafe { + ctx.device + .create_shader_module_passthrough(CreateShaderModuleDescriptorPassthrough { + label: None, + num_workgroups: (0, 0, 0), + spirv: Some(spirv_source(test_hash)), + dxil: None, + hlsl: Some(hlsl_source()), + metallib: None, + msl: Some(metal_source()), + glsl: Some(glsl_vertex_source()), + wgsl: Some(wgsl_source()), + }) + }; + let fragment = unsafe { + ctx.device + .create_shader_module_passthrough(CreateShaderModuleDescriptorPassthrough { + label: None, + num_workgroups: (0, 0, 0), + spirv: Some(spirv_source(test_hash)), + dxil: None, + hlsl: Some(hlsl_source()), + metallib: None, + msl: Some(metal_source()), + glsl: Some(glsl_fragment_source()), + wgsl: Some(wgsl_source()), + }) + }; + + let _pipeline = ctx + .device + .create_render_pipeline(&RenderPipelineDescriptor { + label: None, + layout: None, + vertex: VertexState { + module: &vertex, + entry_point: Some("vertex_main"), + compilation_options: Default::default(), + buffers: &[], + }, + primitive: Default::default(), + depth_stencil: None, + multisample: MultisampleState::default(), + fragment: Some(FragmentState { + module: &fragment, + entry_point: Some("fragment_main"), + compilation_options: Default::default(), + targets: &[Some(ColorTargetState { + format: wgpu::TextureFormat::Rgba8Unorm, + blend: None, + write_mask: ColorWrites::all(), + })], + }), + multiview_mask: None, + cache: None, + }); +} + +#[gpu_test] +static PASSTHROUGH_SHADERS_EXPLICIT_LAYOUT_VALIDATION: GpuTestConfiguration = + GpuTestConfiguration::new() + .parameters( + TestParameters::default() + .features(Features::EXPERIMENTAL_PASSTHROUGH_SHADERS) + .expect_fail(FailureCase::always()), + ) + .run_sync(explicit_layout_validation); diff --git a/tests/tests/wgpu-gpu/passthrough/shader.frag b/tests/tests/wgpu-gpu/passthrough/shader.frag new file mode 100644 index 000000000..bb406fd46 --- /dev/null +++ b/tests/tests/wgpu-gpu/passthrough/shader.frag @@ -0,0 +1,8 @@ +#version 300 es +precision mediump float; + +out vec4 fragColor; + +void main() { + fragColor = vec4(1.0, 1.0, 1.0, 1.0); +} \ No newline at end of file diff --git a/tests/tests/wgpu-gpu/passthrough/shader.hlsl b/tests/tests/wgpu-gpu/passthrough/shader.hlsl new file mode 100644 index 000000000..4a1917c7b --- /dev/null +++ b/tests/tests/wgpu-gpu/passthrough/shader.hlsl @@ -0,0 +1,31 @@ +// Used for HLSL, DXIL, and SPIRV passthrough + +struct VSOut +{ + float4 position : SV_POSITION; +}; + +#ifdef SPIRV +[shader("vertex")] +#endif +VSOut vertex_main(uint vid: SV_VertexID) +{ + VSOut output; + + float2 positions[3] = { + float2(0.0, 0.5), + float2(-0.5, -0.5), + float2(0.5, -0.5), + }; + + output.position = float4(positions[vid], 0.0, 1.0); + return output; +} + +#ifdef SPIRV +[shader("pixel")] +#endif +float4 fragment_main(VSOut input) : SV_TARGET +{ + return float4(1.0, 1.0, 1.0, 1.0); +} diff --git a/tests/tests/wgpu-gpu/passthrough/shader.metal b/tests/tests/wgpu-gpu/passthrough/shader.metal new file mode 100644 index 000000000..3fcc4dc63 --- /dev/null +++ b/tests/tests/wgpu-gpu/passthrough/shader.metal @@ -0,0 +1,25 @@ +// Used for metal and metallib passthrough + +#include +using namespace metal; + +struct VSOut { + float4 position [[position]]; +}; + +vertex VSOut vertex_main(uint vid [[vertex_id]]) { + VSOut out; + + float2 positions[3] = { + float2( 0.0, 0.5), + float2(-0.5, -0.5), + float2( 0.5, -0.5), + }; + + out.position = float4(positions[vid], 0.0, 1.0); + return out; +} + +fragment float4 fragment_main(VSOut in [[stage_in]]) { + return float4(1.0, 1.0, 1.0, 1.0); +} diff --git a/tests/tests/wgpu-gpu/passthrough/shader.vert b/tests/tests/wgpu-gpu/passthrough/shader.vert new file mode 100644 index 000000000..d6227dec6 --- /dev/null +++ b/tests/tests/wgpu-gpu/passthrough/shader.vert @@ -0,0 +1,13 @@ +#version 300 es + +out vec4 v_position; + +void main() { + vec2 positions[3] = vec2[3]( + vec2( 0.0, 0.5), + vec2(-0.5, -0.5), + vec2( 0.5, -0.5) + ); + + gl_Position = vec4(positions[gl_VertexID], 0.0, 1.0); +} \ No newline at end of file diff --git a/tests/tests/wgpu-gpu/passthrough/shader.wgsl b/tests/tests/wgpu-gpu/passthrough/shader.wgsl new file mode 100644 index 000000000..b5cdc8805 --- /dev/null +++ b/tests/tests/wgpu-gpu/passthrough/shader.wgsl @@ -0,0 +1,23 @@ +struct VSOut { + @builtin(position) position: vec4, +}; + +@vertex +fn vertex_main(@builtin(vertex_index) vid: u32) -> VSOut { + var out: VSOut; + + // Hardcoded triangle in clip space + var positions = array, 3>( + vec2( 0.0, 0.5), + vec2(-0.5, -0.5), + vec2( 0.5, -0.5), + ); + + out.position = vec4(positions[vid], 0.0, 1.0); + return out; +} + +@fragment +fn fragment_main(in: VSOut) -> @location(0) vec4 { + return vec4(1.0, 1.0, 1.0, 1.0); +} \ No newline at end of file diff --git a/wgpu-core/src/device/global.rs b/wgpu-core/src/device/global.rs index a0f61ca96..f1536fa03 100644 --- a/wgpu-core/src/device/global.rs +++ b/wgpu-core/src/device/global.rs @@ -1099,6 +1099,7 @@ impl Global { ), (desc.dxil.as_deref(), DataKind::Dxil), (desc.hlsl.as_ref().map(|a| a.as_bytes()), DataKind::Hlsl), + (desc.metallib.as_deref(), DataKind::MetalLib), (desc.msl.as_ref().map(|a| a.as_bytes()), DataKind::Msl), (desc.glsl.as_ref().map(|a| a.as_bytes()), DataKind::Glsl), (desc.wgsl.as_ref().map(|a| a.as_bytes()), DataKind::Wgsl), @@ -1110,11 +1111,8 @@ impl Global { trace.add(trace::Action::CreateShaderModulePassthrough { id: shader.to_trace(), data: file_names, - - entry_point: desc.entry_point.clone(), label: desc.label.clone(), num_workgroups: desc.num_workgroups, - runtime_checks: desc.runtime_checks, }); }; diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index b606485cc..78e9e4538 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -2368,33 +2368,37 @@ impl Device { if let Some(dxil) = &descriptor.dxil { hal::ShaderInput::Dxil { shader: dxil, - entry_point: descriptor.entry_point.clone(), num_workgroups: descriptor.num_workgroups, } } else if let Some(hlsl) = &descriptor.hlsl { hal::ShaderInput::Hlsl { shader: hlsl, - entry_point: descriptor.entry_point.clone(), num_workgroups: descriptor.num_workgroups, } } else { return Err(pipeline::CreateShaderModuleError::NotCompiledForBackend); } } - wgt::Backend::Metal => hal::ShaderInput::Msl { - shader: descriptor - .msl - .as_ref() - .ok_or(pipeline::CreateShaderModuleError::NotCompiledForBackend)?, - entry_point: descriptor.entry_point.clone(), - num_workgroups: descriptor.num_workgroups, - }, + wgt::Backend::Metal => { + if let Some(metallib) = &descriptor.metallib { + hal::ShaderInput::MetalLib { + file: metallib, + num_workgroups: descriptor.num_workgroups, + } + } else if let Some(msl) = &descriptor.msl { + hal::ShaderInput::Msl { + shader: msl, + num_workgroups: descriptor.num_workgroups, + } + } else { + return Err(pipeline::CreateShaderModuleError::NotCompiledForBackend); + } + } wgt::Backend::Gl => hal::ShaderInput::Glsl { shader: descriptor .glsl .as_ref() .ok_or(pipeline::CreateShaderModuleError::NotCompiledForBackend)?, - entry_point: descriptor.entry_point.clone(), num_workgroups: descriptor.num_workgroups, }, wgt::Backend::Noop => { diff --git a/wgpu-core/src/device/trace.rs b/wgpu-core/src/device/trace.rs index dcf908c97..529cc07bc 100644 --- a/wgpu-core/src/device/trace.rs +++ b/wgpu-core/src/device/trace.rs @@ -46,6 +46,7 @@ pub enum DataKind { Spv, Dxil, Hlsl, + MetalLib, Msl, Glsl, } @@ -59,6 +60,7 @@ impl core::fmt::Display for DataKind { DataKind::Spv => "spv", DataKind::Dxil => "dxil", DataKind::Hlsl => "hlsl", + DataKind::MetalLib => "metallib", DataKind::Msl => "metal", DataKind::Glsl => "glsl", }; @@ -73,7 +75,7 @@ impl DataKind { DataKind::Wgsl | DataKind::Ron | DataKind::Hlsl | DataKind::Msl | DataKind::Glsl => { true } - DataKind::Bin | DataKind::Spv | DataKind::Dxil => false, + DataKind::Bin | DataKind::Spv | DataKind::Dxil | DataKind::MetalLib => false, } } } @@ -94,6 +96,8 @@ impl Data { DataKind::Dxil } else if file.ends_with(".hlsl") { DataKind::Hlsl + } else if file.ends_with(".metallib") { + DataKind::MetalLib } else if file.ends_with(".metal") { DataKind::Msl } else if file.ends_with(".glsl") { @@ -184,10 +188,8 @@ pub enum Action<'a, R: ReferenceType> { id: PointerId, data: Vec, - entry_point: String, label: crate::Label<'a>, num_workgroups: (u32, u32, u32), - runtime_checks: wgt::ShaderRuntimeChecks, }, DestroyShaderModule(PointerId), CreateComputePipeline { diff --git a/wgpu-hal/Cargo.toml b/wgpu-hal/Cargo.toml index f99ab01a4..8f2fa873a 100644 --- a/wgpu-hal/Cargo.toml +++ b/wgpu-hal/Cargo.toml @@ -75,6 +75,7 @@ metal = [ "naga/msl-out", "dep:arrayvec", "dep:block2", + "dep:dispatch2", "dep:hashbrown", "dep:libc", "dep:log", @@ -283,6 +284,7 @@ mach-dxcompiler-rs = { workspace = true, optional = true } [target.'cfg(target_vendor = "apple")'.dependencies] # Backend: Metal block2 = { workspace = true, optional = true } +dispatch2 = { workspace = true, optional = true } objc2 = { workspace = true, optional = true } objc2-core-foundation = { workspace = true, optional = true } objc2-foundation = { workspace = true, optional = true } diff --git a/wgpu-hal/src/dx12/device.rs b/wgpu-hal/src/dx12/device.rs index a3df7fe04..d8c00d387 100644 --- a/wgpu-hal/src/dx12/device.rs +++ b/wgpu-hal/src/dx12/device.rs @@ -383,11 +383,10 @@ impl super::Device { } super::ShaderModuleSource::HlslPassthrough(passthrough) => ShaderCacheKey { source: passthrough.shader.clone(), - entry_point: passthrough.entry_point.clone(), + entry_point: stage.entry_point.to_string(), stage: naga_stage, shader_model: naga_options.shader_model, }, - super::ShaderModuleSource::DxilPassthrough(passthrough) => { return Ok(super::CompiledShader::Precompiled( passthrough.shader.clone(), @@ -1831,12 +1830,10 @@ impl crate::Device for super::Device { }), crate::ShaderInput::Dxil { shader, - entry_point, num_workgroups, } => Ok(super::ShaderModule { source: super::ShaderModuleSource::DxilPassthrough(super::DxilPassthroughShader { shader: shader.to_vec(), - entry_point, num_workgroups, }), raw_name, @@ -1844,18 +1841,17 @@ impl crate::Device for super::Device { }), crate::ShaderInput::Hlsl { shader, - entry_point, num_workgroups, } => Ok(super::ShaderModule { source: super::ShaderModuleSource::HlslPassthrough(super::HlslPassthroughShader { shader: shader.to_owned(), - entry_point, num_workgroups, }), raw_name, runtime_checks: desc.runtime_checks, }), crate::ShaderInput::SpirV(_) + | crate::ShaderInput::MetalLib { .. } | crate::ShaderInput::Msl { .. } | crate::ShaderInput::Glsl { .. } => { unreachable!() diff --git a/wgpu-hal/src/dx12/mod.rs b/wgpu-hal/src/dx12/mod.rs index 51f24eb9f..330b104f9 100644 --- a/wgpu-hal/src/dx12/mod.rs +++ b/wgpu-hal/src/dx12/mod.rs @@ -1601,14 +1601,12 @@ impl crate::Queue for Queue { #[derive(Debug)] pub struct DxilPassthroughShader { pub shader: Vec, - pub entry_point: String, pub num_workgroups: (u32, u32, u32), } #[derive(Debug)] pub struct HlslPassthroughShader { pub shader: String, - pub entry_point: String, pub num_workgroups: (u32, u32, u32), } diff --git a/wgpu-hal/src/gles/device.rs b/wgpu-hal/src/gles/device.rs index f2192fcd3..720a7d84e 100644 --- a/wgpu-hal/src/gles/device.rs +++ b/wgpu-hal/src/gles/device.rs @@ -1352,6 +1352,7 @@ impl crate::Device for super::Device { // The backend doesn't yet expose this feature so it should be fine crate::ShaderInput::Glsl { .. } => unimplemented!(), crate::ShaderInput::SpirV(_) + | crate::ShaderInput::MetalLib { .. } | crate::ShaderInput::Msl { .. } | crate::ShaderInput::Dxil { .. } | crate::ShaderInput::Hlsl { .. } => { diff --git a/wgpu-hal/src/lib.rs b/wgpu-hal/src/lib.rs index b8f48f759..8bafcfe80 100644 --- a/wgpu-hal/src/lib.rs +++ b/wgpu-hal/src/lib.rs @@ -2338,25 +2338,25 @@ impl fmt::Debug for NagaShader { /// Shader input. pub enum ShaderInput<'a> { Naga(NagaShader), + MetalLib { + file: &'a [u8], + num_workgroups: (u32, u32, u32), + }, Msl { shader: &'a str, - entry_point: String, num_workgroups: (u32, u32, u32), }, SpirV(&'a [u32]), Dxil { shader: &'a [u8], - entry_point: String, num_workgroups: (u32, u32, u32), }, Hlsl { shader: &'a str, - entry_point: String, num_workgroups: (u32, u32, u32), }, Glsl { shader: &'a str, - entry_point: String, num_workgroups: (u32, u32, u32), }, } diff --git a/wgpu-hal/src/metal/device.rs b/wgpu-hal/src/metal/device.rs index ce6386f88..7dc84d3f6 100644 --- a/wgpu-hal/src/metal/device.rs +++ b/wgpu-hal/src/metal/device.rs @@ -1,5 +1,10 @@ use alloc::{borrow::ToOwned as _, sync::Arc, vec::Vec}; -use core::{ptr::NonNull, sync::atomic}; +use core::{ + ffi::c_void, + ptr::{self, NonNull}, + sync::atomic, +}; +use dispatch2::DispatchData; use std::{thread, time}; use objc2::{ @@ -320,7 +325,10 @@ impl super::Device { } ShaderModuleSource::Passthrough(ref shader) => Ok(CompiledShader { library: shader.library.clone(), - function: shader.function.clone(), + function: shader + .library + .newFunctionWithName(&NSString::from_str(stage.entry_point)) + .ok_or(crate::PipelineError::EntryPoint(naga_stage))?, wg_size: MTLSize { width: shader.num_workgroups.0 as usize, height: shader.num_workgroups.1 as usize, @@ -1089,9 +1097,36 @@ impl crate::Device for super::Device { source: ShaderModuleSource::Naga(naga), bounds_checks: desc.runtime_checks, }), + crate::ShaderInput::MetalLib { + file, + num_workgroups, + } => { + // SAFETY: this creates a reference to `file` that is dropped before `file` is dropped. + let data = unsafe { + DispatchData::new( + NonNull::new(file.as_ptr() as *mut c_void).unwrap(), + file.len(), + None, + ptr::null_mut(), + ) + }; + let library = self + .shared + .device + .newLibraryWithData_error(&data) + .map_err(|e| crate::ShaderError::Compilation(format!("Metallib: {e:?}")))?; + drop(data); + Ok(super::ShaderModule { + source: ShaderModuleSource::Passthrough(PassthroughShader { + library, + num_workgroups, + }), + // This goes unused for passthrough shaders + bounds_checks: wgt::ShaderRuntimeChecks::unchecked(), + }) + } crate::ShaderInput::Msl { shader: source, - entry_point, num_workgroups, } => { let options = MTLCompileOptions::new(); @@ -1100,22 +1135,14 @@ impl crate::Device for super::Device { let library = device .newLibraryWithSource_options_error(&NSString::from_str(source), Some(&options)) .map_err(|e| crate::ShaderError::Compilation(format!("MSL: {e:?}")))?; - let function = library - .newFunctionWithName(&NSString::from_str(&entry_point)) - .ok_or_else(|| { - crate::ShaderError::Compilation(format!( - "Entry point '{entry_point}' not found" - )) - })?; Ok(super::ShaderModule { source: ShaderModuleSource::Passthrough(PassthroughShader { library, - function, - entry_point, num_workgroups, }), - bounds_checks: desc.runtime_checks, + // This goes unused for passthrough shaders + bounds_checks: wgt::ShaderRuntimeChecks::unchecked(), }) } crate::ShaderInput::SpirV(_) @@ -1603,14 +1630,17 @@ impl crate::Device for super::Device { let descriptor = MTLComputePipelineDescriptor::new(); let module = desc.stage.module; - let cs = if let ShaderModuleSource::Passthrough(desc) = &module.source { + let cs = if let ShaderModuleSource::Passthrough(passthrough_desc) = &module.source { CompiledShader { - library: desc.library.clone(), - function: desc.function.clone(), + library: passthrough_desc.library.clone(), + function: passthrough_desc + .library + .newFunctionWithName(&NSString::from_str(desc.stage.entry_point)) + .ok_or(crate::PipelineError::EntryPoint(naga::ShaderStage::Compute))?, wg_size: MTLSize { - width: desc.num_workgroups.0 as usize, - height: desc.num_workgroups.1 as usize, - depth: desc.num_workgroups.2 as usize, + width: passthrough_desc.num_workgroups.0 as usize, + height: passthrough_desc.num_workgroups.1 as usize, + depth: passthrough_desc.num_workgroups.2 as usize, }, wg_memory_sizes: vec![], sized_bindings: vec![], diff --git a/wgpu-hal/src/metal/mod.rs b/wgpu-hal/src/metal/mod.rs index 9c04a31ef..a4ceff9ca 100644 --- a/wgpu-hal/src/metal/mod.rs +++ b/wgpu-hal/src/metal/mod.rs @@ -45,7 +45,7 @@ use objc2_metal::{ MTLArgumentBuffersTier, MTLBlitCommandEncoder, MTLBuffer, MTLCommandBuffer, MTLCommandBufferStatus, MTLCommandQueue, MTLComputeCommandEncoder, MTLComputePipelineState, MTLCounterSampleBuffer, MTLCullMode, MTLDepthClipMode, MTLDepthStencilState, MTLDevice, - MTLDrawable, MTLFunction, MTLIndexType, MTLLanguageVersion, MTLLibrary, MTLPrimitiveType, + MTLDrawable, MTLIndexType, MTLLanguageVersion, MTLLibrary, MTLPrimitiveType, MTLReadWriteTextureTier, MTLRenderCommandEncoder, MTLRenderPipelineState, MTLRenderStages, MTLResource, MTLResourceUsage, MTLSamplerState, MTLSharedEvent, MTLSize, MTLTexture, MTLTextureType, MTLTriangleFillMode, MTLWinding, @@ -792,8 +792,6 @@ pub enum ShaderModuleSource { #[derive(Debug)] pub struct PassthroughShader { pub library: Retained>, - pub function: Retained>, - pub entry_point: String, pub num_workgroups: (u32, u32, u32), } diff --git a/wgpu-hal/src/vulkan/device.rs b/wgpu-hal/src/vulkan/device.rs index f804a208f..415841d6a 100644 --- a/wgpu-hal/src/vulkan/device.rs +++ b/wgpu-hal/src/vulkan/device.rs @@ -1747,7 +1747,8 @@ impl crate::Device for super::Device { crate::ShaderInput::SpirV(data) => { super::ShaderModule::Raw(self.create_shader_module_impl(data, &desc.label)?) } - crate::ShaderInput::Msl { .. } + crate::ShaderInput::MetalLib { .. } + | crate::ShaderInput::Msl { .. } | crate::ShaderInput::Dxil { .. } | crate::ShaderInput::Hlsl { .. } | crate::ShaderInput::Glsl { .. } => unreachable!(), diff --git a/wgpu-types/src/shader.rs b/wgpu-types/src/shader.rs index cbeaef737..83102f84d 100644 --- a/wgpu-types/src/shader.rs +++ b/wgpu-types/src/shader.rs @@ -1,4 +1,4 @@ -use alloc::{borrow::Cow, string::String}; +use alloc::borrow::Cow; /// Describes how shader bound checks should be performed. #[derive(Copy, Clone, Debug)] @@ -92,23 +92,21 @@ impl Default for ShaderRuntimeChecks { #[derive(Debug, Clone)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct CreateShaderModuleDescriptorPassthrough<'a, L> { - /// Entrypoint. Unused for Spir-V. - pub entry_point: String, /// Debug label of the shader module. This will show up in graphics debuggers for easy identification. pub label: L, /// Number of workgroups in each dimension x, y and z. Unused for Spir-V. pub num_workgroups: (u32, u32, u32), - /// Runtime checks that should be enabled. - pub runtime_checks: ShaderRuntimeChecks, /// Binary SPIR-V data, in 4-byte words. pub spirv: Option>, /// Shader DXIL source. pub dxil: Option>, - /// Shader MSL source. - pub msl: Option>, /// Shader HLSL source. pub hlsl: Option>, + /// Shader MetalLib source. + pub metallib: Option>, + /// Shader MSL source. + pub msl: Option>, /// Shader GLSL source (currently unused). pub glsl: Option>, /// Shader WGSL source. @@ -120,12 +118,11 @@ pub struct CreateShaderModuleDescriptorPassthrough<'a, L> { impl<'a, L: Default> Default for CreateShaderModuleDescriptorPassthrough<'a, L> { fn default() -> Self { Self { - entry_point: "".into(), label: Default::default(), num_workgroups: (0, 0, 0), - runtime_checks: ShaderRuntimeChecks::unchecked(), spirv: None, dxil: None, + metallib: None, msl: None, hlsl: None, glsl: None, @@ -141,11 +138,10 @@ impl<'a, L> CreateShaderModuleDescriptorPassthrough<'a, L> { fun: impl FnOnce(&L) -> K, ) -> CreateShaderModuleDescriptorPassthrough<'a, K> { CreateShaderModuleDescriptorPassthrough { - entry_point: self.entry_point.clone(), label: fun(&self.label), num_workgroups: self.num_workgroups, - runtime_checks: self.runtime_checks, spirv: self.spirv.clone(), + metallib: self.metallib.clone(), dxil: self.dxil.clone(), msl: self.msl.clone(), hlsl: self.hlsl.clone(), @@ -159,10 +155,18 @@ impl<'a, L> CreateShaderModuleDescriptorPassthrough<'a, L> { pub fn trace_data(&self) -> &[u8] { if let Some(spirv) = &self.spirv { bytemuck::cast_slice(spirv) + } else if let Some(metallib) = &self.metallib { + metallib } else if let Some(msl) = &self.msl { msl.as_bytes() } else if let Some(dxil) = &self.dxil { dxil + } else if let Some(hlsl) = &self.hlsl { + hlsl.as_bytes() + } else if let Some(glsl) = &self.glsl { + glsl.as_bytes() + } else if let Some(wgsl) = &self.wgsl { + wgsl.as_bytes() } else { panic!("No binary data provided to `ShaderModuleDescriptorGeneric`") } @@ -173,10 +177,18 @@ impl<'a, L> CreateShaderModuleDescriptorPassthrough<'a, L> { pub fn trace_binary_ext(&self) -> &'static str { if self.spirv.is_some() { "spv" + } else if self.metallib.is_some() { + "metallib" } else if self.msl.is_some() { "metal" } else if self.dxil.is_some() { "dxil" + } else if self.hlsl.is_some() { + "hlsl" + } else if self.glsl.is_some() { + "glsl" + } else if self.wgsl.is_some() { + "wgsl" } else { panic!("No binary data provided to `ShaderModuleDescriptorGeneric`") } diff --git a/wgpu/src/macros/mod.rs b/wgpu/src/macros/mod.rs index ec28f604a..067ae9d43 100644 --- a/wgpu/src/macros/mod.rs +++ b/wgpu/src/macros/mod.rs @@ -141,11 +141,10 @@ macro_rules! include_spirv_raw { $crate::ShaderModuleDescriptorPassthrough { label: $crate::__macro_helpers::Some($($token)*), spirv: Some($crate::__macro_helpers::Cow::Borrowed($crate::include_spirv_source!($($token)*))), - entry_point: $crate::__macro_helpers::String::new(), // This is unused for SPIR-V num_workgroups: (0, 0, 0), - runtime_checks: $crate::ShaderRuntimeChecks::unchecked(), dxil: None, + metallib: None, msl: None, hlsl: None, glsl: None,