From 8bc9fc1795daa72ffc3f3fa974f329b0d234cf47 Mon Sep 17 00:00:00 2001 From: Inner Daemons <85136135+inner-daemons@users.noreply.github.com> Date: Wed, 8 Apr 2026 21:03:34 -0500 Subject: [PATCH] Add GLSL passthrough (#9064) Co-authored-by: Connor Fitzgerald --- CHANGELOG.md | 5 + examples/features/src/mesh_shader/mod.rs | 56 +--- player/src/lib.rs | 46 ++-- tests/Cargo.toml | 6 +- tests/tests/wgpu-gpu/mesh_shader/mod.rs | 102 +++---- tests/tests/wgpu-gpu/passthrough/mod.rs | 254 +++++++++++------- tests/tests/wgpu-gpu/passthrough/shader.frag | 8 +- tests/tests/wgpu-gpu/passthrough/shader.hlsl | 4 +- tests/tests/wgpu-gpu/passthrough/shader.metal | 4 +- tests/tests/wgpu-gpu/passthrough/shader.vert | 25 +- tests/tests/wgpu-gpu/passthrough/shader.wgsl | 4 +- wgpu-core/src/device/global.rs | 12 +- wgpu-core/src/device/resource.rs | 52 ++-- wgpu-core/src/device/trace.rs | 4 +- wgpu-core/src/device/trace/replay.rs | 8 +- wgpu-core/src/pipeline.rs | 48 +++- wgpu-core/src/validation.rs | 25 +- wgpu-hal/Cargo.toml | 6 +- wgpu-hal/src/dx12/device.rs | 12 +- wgpu-hal/src/dx12/mod.rs | 2 - wgpu-hal/src/gles/adapter.rs | 7 +- wgpu-hal/src/gles/device.rs | 204 +++++++------- wgpu-hal/src/gles/mod.rs | 8 +- wgpu-hal/src/lib.rs | 7 +- wgpu-hal/src/metal/device.rs | 40 +-- wgpu-hal/src/metal/mod.rs | 8 +- wgpu-types/src/shader.rs | 22 +- wgpu/src/lib.rs | 25 +- wgpu/src/macros/mod.rs | 7 +- 29 files changed, 561 insertions(+), 450 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index da5fe8446..e21b21f69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,6 +58,10 @@ Bottom level categories: - Added full support for mesh shaders, including in WGSL shaders. By @inner-daemons in [#8739](https://github.com/gfx-rs/wgpu/pull/8739). - Fixed structure field names incorrectly ignoring reserved keywords in the Metal (MSL) backend. By @39ali [#9379](https://github.com/gfx-rs/wgpu/pull/9379). +#### GLES + +- Added support for GLSL passthrough. By @inner-daemons in [#9064](https://github.com/gfx-rs/wgpu/pull/9064). + #### DX12 - Added support for mesh shaders in naga's HLSL writer, completing DX12 support for mesh shaders. By @inner-daemons in [#8752](https://github.com/gfx-rs/wgpu/pull/8752). @@ -76,6 +80,7 @@ Bottom level categories: - 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). - Zero-size `Queue::write_buffer` now returns an error if the offset is invalid or the buffer lacks `COPY_DST`. By @39ali in [#9374](https://github.com/gfx-rs/wgpu/pull/9374). - `Buffer::get_mapped_range` and variants now return `Result<_, MapRangeError>>` instead of panicking, in line with WebGPU spec. By @atlv24 in [#9281](https://github.com/gfx-rs/wgpu/pull/9281). +- Passthrough shaders now require a list of entry points when being created. by @inner-daemons in [#9064](https://github.com/gfx-rs/wgpu/pull/9064). #### Validation diff --git a/examples/features/src/mesh_shader/mod.rs b/examples/features/src/mesh_shader/mod.rs index c9bd085ec..874ca65d2 100644 --- a/examples/features/src/mesh_shader/mod.rs +++ b/examples/features/src/mesh_shader/mod.rs @@ -1,38 +1,3 @@ -// Same as in mesh shader tests -fn compile_wgsl(device: &wgpu::Device) -> wgpu::ShaderModule { - // Workgroup memory zero initialization can be expensive for mesh shaders - unsafe { - device.create_shader_module_trusted( - wgpu::ShaderModuleDescriptor { - label: None, - source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()), - }, - wgpu::ShaderRuntimeChecks::unchecked(), - ) - } -} - -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) -> 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", - } -} - pub struct Example { pipeline: wgpu::RenderPipeline, } @@ -43,14 +8,7 @@ impl crate::framework::Example for Example { device: &wgpu::Device, _queue: &wgpu::Queue, ) -> Self { - let Shaders { - ts, - ms, - fs, - ts_name, - ms_name, - fs_name, - } = get_shaders(device); + let shader = device.create_shader_module(wgpu::include_wgsl!("shader.wgsl")); let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: None, bind_group_layouts: &[], @@ -60,18 +18,18 @@ impl crate::framework::Example for Example { label: None, layout: Some(&pipeline_layout), task: Some(wgpu::TaskState { - module: &ts, - entry_point: Some(ts_name), + module: &shader, + entry_point: Some("ts_main"), compilation_options: Default::default(), }), mesh: wgpu::MeshState { - module: &ms, - entry_point: Some(ms_name), + module: &shader, + entry_point: Some("ms_main"), compilation_options: Default::default(), }, fragment: Some(wgpu::FragmentState { - module: &fs, - entry_point: Some(fs_name), + module: &shader, + entry_point: Some("fs_main"), compilation_options: Default::default(), targets: &[Some(config.view_formats[0].into())], }), diff --git a/player/src/lib.rs b/player/src/lib.rs index 8a86c7ae2..5a6d35476 100644 --- a/player/src/lib.rs +++ b/player/src/lib.rs @@ -291,7 +291,7 @@ impl Player { id, data, label, - num_workgroups, + entry_points, } => { let spirv = data.iter().find_map(|a| { if a.kind() == DataKind::Spv { @@ -303,27 +303,33 @@ impl Player { None } }); - let dxil = data - .iter() - .find_map(|a| (a.kind() == DataKind::Dxil).then(|| loader.load(a))); - 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))); - let glsl = data - .iter() - .find_map(|a| (a.kind() == DataKind::Glsl).then(|| loader.load_utf8(a))); - let wgsl = data - .iter() - .find_map(|a| (a.kind() == DataKind::Wgsl).then(|| loader.load_utf8(a))); + let dxil = data.iter().find_map(|a| { + (a.kind() == DataKind::Dxil).then(|| Cow::Owned(loader.load(a).into_owned())) + }); + let hlsl = data.iter().find_map(|a| { + (a.kind() == DataKind::Hlsl) + .then(|| Cow::Owned(loader.load_utf8(a).into_owned())) + }); + let metallib = data.iter().find_map(|a| { + (a.kind() == DataKind::MetalLib) + .then(|| Cow::Owned(loader.load(a).into_owned())) + }); + let msl = data.iter().find_map(|a| { + (a.kind() == DataKind::Msl) + .then(|| Cow::Owned(loader.load_utf8(a).into_owned())) + }); + let glsl = data.iter().find_map(|a| { + (a.kind() == DataKind::Glsl) + .then(|| Cow::Owned(loader.load_utf8(a).into_owned())) + }); + let wgsl = data.iter().find_map(|a| { + (a.kind() == DataKind::Wgsl) + .then(|| Cow::Owned(loader.load_utf8(a).into_owned())) + }); + let desc = wgt::CreateShaderModuleDescriptorPassthrough { label, - num_workgroups, + entry_points, spirv, dxil, diff --git a/tests/Cargo.toml b/tests/Cargo.toml index 5215723f0..7968cef73 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -32,7 +32,7 @@ name = "wgpu-validation" harness = true [features] -webgl = ["wgpu/webgl"] +webgl = [] # This feature is not actually used by this package, but it being present somewhere in the workspace # allows us to force the build to have profiling code enabled so we can test that configuration. test-build-with-profiling = ["profiling/type-check"] @@ -42,7 +42,9 @@ test-build-with-profiling = ["profiling/type-check"] # dev-dependencies will make the dependency graph just a bit shallower, and makes # `cargo clippy -- -Wunused-crate-dependencies` give fewer false positives. [dependencies] -wgpu = { workspace = true, features = ["noop"] } +# Passthrough backend uses as_hal for the GLES backend, and there's not a great way to cfg-gate that. +# These are all enabled for any testing on CI anyway, and won't do anything if no devices are detected. +wgpu = { workspace = true, features = ["noop", "gles", "webgl", "angle"] } wgpu-core = { workspace = true, features = ["trace"] } wgpu-hal = { workspace = true, features = ["validation_canary"] } wgpu-macros.workspace = true diff --git a/tests/tests/wgpu-gpu/mesh_shader/mod.rs b/tests/tests/wgpu-gpu/mesh_shader/mod.rs index 0f24fd5c1..2e774fe4a 100644 --- a/tests/tests/wgpu-gpu/mesh_shader/mod.rs +++ b/tests/tests/wgpu-gpu/mesh_shader/mod.rs @@ -20,43 +20,6 @@ pub fn all_tests(tests: &mut Vec) { ]); } -// Same as in mesh shader example -fn compile_wgsl(device: &wgpu::Device) -> wgpu::ShaderModule { - device.create_shader_module(wgpu::ShaderModuleDescriptor { - label: None, - source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()), - }) -} - -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, info: &MeshPipelineTestInfo) -> Shaders { - if info.divergent && info.use_task { - 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", - } -} - fn create_depth( device: &wgpu::Device, ) -> (wgpu::Texture, wgpu::TextureView, wgpu::DepthStencilState) { @@ -97,14 +60,7 @@ fn mesh_pipeline_build(ctx: &TestingContext, info: MeshPipelineTestInfo) { let device = &ctx.device; let (_depth_image, depth_view, depth_state) = create_depth(device); - let Shaders { - ts, - ms, - fs, - ts_name, - ms_name, - fs_name, - } = get_shaders(device, &info); + let shader = device.create_shader_module(wgpu::include_wgsl!("shader.wgsl")); let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: None, bind_group_layouts: &[], @@ -113,19 +69,25 @@ fn mesh_pipeline_build(ctx: &TestingContext, info: MeshPipelineTestInfo) { let pipeline = device.create_mesh_pipeline(&wgpu::MeshPipelineDescriptor { label: None, layout: Some(&layout), - task: ts.as_ref().map(|task| wgpu::TaskState { - module: task, - entry_point: Some(ts_name), - compilation_options: Default::default(), + task: info.use_task.then_some(wgpu::TaskState { + module: &shader, + entry_point: Some("ts_main"), + compilation_options: wgpu::PipelineCompilationOptions::default(), }), mesh: wgpu::MeshState { - module: &ms, - entry_point: Some(ms_name), + module: &shader, + entry_point: Some(if info.divergent && info.use_task { + "ms_divergent" + } else if info.use_task { + "ms_main" + } else { + "ms_no_ts" + }), compilation_options: Default::default(), }, - fragment: fs.as_ref().map(|frag| wgpu::FragmentState { - module: frag, - entry_point: Some(fs_name), + fragment: info.use_frag.then_some(wgpu::FragmentState { + module: &shader, + entry_point: Some("fs_main"), targets: &[], compilation_options: Default::default(), }), @@ -180,15 +142,7 @@ fn mesh_draw(ctx: &TestingContext, draw_type: DrawType, info: MeshPipelineTestIn let device = &ctx.device; let (_depth_image, depth_view, depth_state) = create_depth(device); - let Shaders { - ts, - ms, - fs, - ts_name, - ms_name, - fs_name, - } = get_shaders(device, &info); - let frag = fs.unwrap(); + let shader = device.create_shader_module(wgpu::include_wgsl!("shader.wgsl")); let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: None, bind_group_layouts: &[], @@ -197,19 +151,25 @@ fn mesh_draw(ctx: &TestingContext, draw_type: DrawType, info: MeshPipelineTestIn let pipeline = device.create_mesh_pipeline(&wgpu::MeshPipelineDescriptor { label: None, layout: Some(&layout), - task: ts.as_ref().map(|task| wgpu::TaskState { - module: task, - entry_point: Some(ts_name), - compilation_options: Default::default(), + task: info.use_task.then_some(wgpu::TaskState { + module: &shader, + entry_point: Some("ts_main"), + compilation_options: wgpu::PipelineCompilationOptions::default(), }), mesh: wgpu::MeshState { - module: &ms, - entry_point: Some(ms_name), + module: &shader, + entry_point: Some(if info.divergent && info.use_task { + "ms_divergent" + } else if info.use_task { + "ms_main" + } else { + "ms_no_ts" + }), compilation_options: Default::default(), }, fragment: Some(wgpu::FragmentState { - module: &frag, - entry_point: Some(fs_name), + module: &shader, + entry_point: Some("fs_main"), targets: &[], compilation_options: Default::default(), }), diff --git a/tests/tests/wgpu-gpu/passthrough/mod.rs b/tests/tests/wgpu-gpu/passthrough/mod.rs index 5b4876006..33e9658b3 100644 --- a/tests/tests/wgpu-gpu/passthrough/mod.rs +++ b/tests/tests/wgpu-gpu/passthrough/mod.rs @@ -47,7 +47,7 @@ fn test_with_module(ctx: TestingContext, vertex: wgpu::ShaderModule, fragment: w layout: Some(&layout), vertex: VertexState { module: &vertex, - entry_point: Some("vertex_main"), + entry_point: Some("vs_main"), compilation_options: Default::default(), buffers: &[], }, @@ -56,7 +56,7 @@ fn test_with_module(ctx: TestingContext, vertex: wgpu::ShaderModule, fragment: w multisample: MultisampleState::default(), fragment: Some(FragmentState { module: &fragment, - entry_point: Some("fragment_main"), + entry_point: Some("fs_main"), compilation_options: Default::default(), targets: &[Some(ColorTargetState { format: wgpu::TextureFormat::Rgba8Unorm, @@ -78,7 +78,16 @@ fn metal_test(ctx: TestingContext) { ctx.device .create_shader_module_passthrough(CreateShaderModuleDescriptorPassthrough { label: None, - num_workgroups: (0, 0, 0), + entry_points: Cow::Borrowed(&[ + wgpu::PassthroughShaderEntryPoint { + name: "vs_main".into(), + workgroup_size: (0, 0, 0), + }, + wgpu::PassthroughShaderEntryPoint { + name: "fs_main".into(), + workgroup_size: (0, 0, 0), + }, + ]), msl: Some(metal_source()), ..Default::default() }) @@ -180,7 +189,16 @@ fn metallib_test(ctx: TestingContext) { ctx.device .create_shader_module_passthrough(CreateShaderModuleDescriptorPassthrough { label: None, - num_workgroups: (0, 0, 0), + entry_points: Cow::Borrowed(&[ + wgpu::PassthroughShaderEntryPoint { + name: "vs_main".into(), + workgroup_size: (0, 0, 0), + }, + wgpu::PassthroughShaderEntryPoint { + name: "fs_main".into(), + workgroup_size: (0, 0, 0), + }, + ]), metallib: Some(std::borrow::Cow::Borrowed(&source)), ..Default::default() }) @@ -205,6 +223,16 @@ fn hlsl_test(ctx: TestingContext) { let module = unsafe { ctx.device .create_shader_module_passthrough(CreateShaderModuleDescriptorPassthrough { + entry_points: Cow::Borrowed(&[ + wgpu::PassthroughShaderEntryPoint { + name: "vs_main".into(), + workgroup_size: (0, 0, 0), + }, + wgpu::PassthroughShaderEntryPoint { + name: "fs_main".into(), + workgroup_size: (0, 0, 0), + }, + ]), hlsl: Some(hlsl_source()), ..Default::default() }) @@ -253,7 +281,7 @@ fn compile_dxil(entry: &str, stage_str: &str, test_hash: u64) -> Cow<'static, [u fn dxil_vertex_source(test_hash: u64) -> Cow<'static, [u8]> { if cfg!(target_os = "windows") { - compile_dxil("vertex_main", "vs", test_hash) + compile_dxil("vs_main", "vs", test_hash) } else { Cow::Borrowed(&[]) } @@ -261,7 +289,7 @@ fn dxil_vertex_source(test_hash: u64) -> Cow<'static, [u8]> { fn dxil_fragment_source(test_hash: u64) -> Cow<'static, [u8]> { if cfg!(target_os = "windows") { - compile_dxil("fragment_main", "ps", test_hash) + compile_dxil("fs_main", "ps", test_hash) } else { Cow::Borrowed(&[]) } @@ -274,7 +302,10 @@ fn dxil_test(ctx: TestingContext) { ctx.device .create_shader_module_passthrough(wgpu::ShaderModuleDescriptorPassthrough { label: None, - num_workgroups: (1, 1, 1), + entry_points: Cow::Borrowed(&[wgpu::PassthroughShaderEntryPoint { + name: "vs_main".into(), + workgroup_size: (0, 0, 0), + }]), dxil: Some(vertex_source), ..Default::default() }) @@ -284,7 +315,10 @@ fn dxil_test(ctx: TestingContext) { ctx.device .create_shader_module_passthrough(wgpu::ShaderModuleDescriptorPassthrough { label: None, - num_workgroups: (1, 1, 1), + entry_points: Cow::Borrowed(&[wgpu::PassthroughShaderEntryPoint { + name: "fs_main".into(), + workgroup_size: (0, 0, 0), + }]), dxil: Some(fragment_source), ..Default::default() }) @@ -303,7 +337,7 @@ static DXIL_PASSTHROUGH_SHADER: GpuTestConfiguration = GpuTestConfiguration::new fn spirv_source(test_hash: u64) -> Cow<'static, [u32]> { let out_path = format!( - "{}/tests/wgpu-gpu/passthrough/shade{test_hash}.spv", + "{}/tests/wgpu-gpu/passthrough/shader{test_hash}.spv", env!("CARGO_MANIFEST_DIR") ); let cmd = std::process::Command::new("dxc") @@ -340,6 +374,16 @@ fn spirv_test(ctx: TestingContext) { let module = unsafe { ctx.device .create_shader_module_passthrough(CreateShaderModuleDescriptorPassthrough { + entry_points: Cow::Borrowed(&[ + wgpu::PassthroughShaderEntryPoint { + name: "vs_main".into(), + workgroup_size: (0, 0, 0), + }, + wgpu::PassthroughShaderEntryPoint { + name: "fs_main".into(), + workgroup_size: (0, 0, 0), + }, + ]), spirv: Some(spirv_source(test_hash)), ..Default::default() }) @@ -356,26 +400,54 @@ static SPIRV_PASSTHROUGH_SHADER: GpuTestConfiguration = GpuTestConfiguration::ne ) .run_sync(spirv_test); -fn glsl_vertex_source() -> Cow<'static, str> { - std::borrow::Cow::Borrowed(include_str!("shader.vert")) +fn is_gles(ctx: &TestingContext) -> bool { + unsafe { + let Some(dev) = ctx.adapter.as_hal::() else { + return false; + }; + let version = dev.get_glsl_version(); + matches!(version, naga::back::glsl::Version::Embedded { .. }) + } } -fn glsl_fragment_source() -> Cow<'static, str> { - std::borrow::Cow::Borrowed(include_str!("shader.frag")) +fn glsl_vertex_source(ctx: &TestingContext) -> Cow<'static, str> { + let version_string = if is_gles(ctx) { + "#version 300 es\nprecision highp float;\n" + } else { + "#version 330 core\n" + }; + std::borrow::Cow::Owned(format!("{version_string}{}", include_str!("shader.vert"))) +} + +fn glsl_fragment_source(ctx: &TestingContext) -> Cow<'static, str> { + let version_string = if is_gles(ctx) { + "#version 300 es\nprecision highp float;\n" + } else { + "#version 330 core\n" + }; + std::borrow::Cow::Owned(format!("{version_string}{}", include_str!("shader.frag"))) } fn glsl_test(ctx: TestingContext) { let vertex = unsafe { ctx.device .create_shader_module_passthrough(CreateShaderModuleDescriptorPassthrough { - glsl: Some(glsl_vertex_source()), + entry_points: Cow::Borrowed(&[wgpu::PassthroughShaderEntryPoint { + name: "vs_main".into(), + workgroup_size: (0, 0, 0), + }]), + glsl: Some(glsl_vertex_source(&ctx)), ..Default::default() }) }; let fragment = unsafe { ctx.device .create_shader_module_passthrough(CreateShaderModuleDescriptorPassthrough { - glsl: Some(glsl_fragment_source()), + entry_points: Cow::Borrowed(&[wgpu::PassthroughShaderEntryPoint { + name: "fs_main".into(), + workgroup_size: (0, 0, 0), + }]), + glsl: Some(glsl_fragment_source(&ctx)), ..Default::default() }) }; @@ -416,35 +488,35 @@ static WGSL_PASSTHROUGH_SHADER: GpuTestConfiguration = GpuTestConfiguration::new .run_sync(wgsl_test); fn all_passthrough_shaders_binary(ctx: TestingContext) { + #[allow(unused_variables)] 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 desc = CreateShaderModuleDescriptorPassthrough { + entry_points: Cow::Borrowed(&[wgpu::PassthroughShaderEntryPoint { + name: "vs_main".into(), + workgroup_size: (0, 0, 0), + }]), + #[cfg(not(target_arch = "wasm32"))] + spirv: Some(spirv_source(test_hash)), + #[cfg(not(target_arch = "wasm32"))] + dxil: Some(dxil_vertex_source(test_hash)), + #[cfg(not(target_arch = "wasm32"))] + metallib: Some(metallib_source(test_hash)), + glsl: Some(glsl_vertex_source(&ctx)), + wgsl: Some(wgsl_source()), + ..Default::default() }; - 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()), - }) + let vertex = unsafe { ctx.device.create_shader_module_passthrough(desc.clone()) }; + let desc = CreateShaderModuleDescriptorPassthrough { + entry_points: Cow::Borrowed(&[wgpu::PassthroughShaderEntryPoint { + name: "fs_main".into(), + workgroup_size: (0, 0, 0), + }]), + #[cfg(not(target_arch = "wasm32"))] + dxil: Some(dxil_fragment_source(test_hash)), + glsl: Some(glsl_fragment_source(&ctx)), + ..desc }; + let fragment = unsafe { ctx.device.create_shader_module_passthrough(desc) }; test_with_module(ctx, vertex, fragment); } @@ -454,35 +526,31 @@ static ALL_PASSTHROUGH_SHADERS_BINARY: GpuTestConfiguration = GpuTestConfigurati .run_sync(all_passthrough_shaders_binary); fn all_passthrough_shader_source(ctx: TestingContext) { + #[allow(unused_variables)] 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 desc = CreateShaderModuleDescriptorPassthrough { + entry_points: Cow::Borrowed(&[wgpu::PassthroughShaderEntryPoint { + name: "vs_main".into(), + workgroup_size: (0, 0, 0), + }]), + #[cfg(not(target_arch = "wasm32"))] + spirv: Some(spirv_source(test_hash)), + hlsl: Some(hlsl_source()), + msl: Some(metal_source()), + glsl: Some(glsl_vertex_source(&ctx)), + wgsl: Some(wgsl_source()), + ..Default::default() }; - 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 vertex = unsafe { ctx.device.create_shader_module_passthrough(desc.clone()) }; + let desc = CreateShaderModuleDescriptorPassthrough { + entry_points: Cow::Borrowed(&[wgpu::PassthroughShaderEntryPoint { + name: "fs_main".into(), + workgroup_size: (0, 0, 0), + }]), + glsl: Some(glsl_fragment_source(&ctx)), + ..desc }; + let fragment = unsafe { ctx.device.create_shader_module_passthrough(desc) }; test_with_module(ctx, vertex, fragment); } @@ -492,35 +560,31 @@ static ALL_PASSTHROUGH_SHADERS_SOURCE: GpuTestConfiguration = GpuTestConfigurati .run_sync(all_passthrough_shader_source); fn explicit_layout_validation(ctx: TestingContext) { + #[allow(unused_variables)] 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 desc = CreateShaderModuleDescriptorPassthrough { + entry_points: Cow::Borrowed(&[wgpu::PassthroughShaderEntryPoint { + name: "vs_main".into(), + workgroup_size: (0, 0, 0), + }]), + #[cfg(not(target_arch = "wasm32"))] + spirv: Some(spirv_source(test_hash)), + hlsl: Some(hlsl_source()), + msl: Some(metal_source()), + glsl: Some(glsl_vertex_source(&ctx)), + wgsl: Some(wgsl_source()), + ..Default::default() }; - 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 vertex = unsafe { ctx.device.create_shader_module_passthrough(desc.clone()) }; + let desc = CreateShaderModuleDescriptorPassthrough { + entry_points: Cow::Borrowed(&[wgpu::PassthroughShaderEntryPoint { + name: "fs_main".into(), + workgroup_size: (0, 0, 0), + }]), + glsl: Some(glsl_fragment_source(&ctx)), + ..desc }; + let fragment = unsafe { ctx.device.create_shader_module_passthrough(desc) }; let _pipeline = ctx .device @@ -529,7 +593,7 @@ fn explicit_layout_validation(ctx: TestingContext) { layout: None, vertex: VertexState { module: &vertex, - entry_point: Some("vertex_main"), + entry_point: Some("vs_main"), compilation_options: Default::default(), buffers: &[], }, @@ -538,7 +602,7 @@ fn explicit_layout_validation(ctx: TestingContext) { multisample: MultisampleState::default(), fragment: Some(FragmentState { module: &fragment, - entry_point: Some("fragment_main"), + entry_point: Some("fs_main"), compilation_options: Default::default(), targets: &[Some(ColorTargetState { format: wgpu::TextureFormat::Rgba8Unorm, diff --git a/tests/tests/wgpu-gpu/passthrough/shader.frag b/tests/tests/wgpu-gpu/passthrough/shader.frag index bb406fd46..969a369d6 100644 --- a/tests/tests/wgpu-gpu/passthrough/shader.frag +++ b/tests/tests/wgpu-gpu/passthrough/shader.frag @@ -1,7 +1,9 @@ -#version 300 es -precision mediump float; +#if __VERSION__ >= 130 + out vec4 fragColor; +#else + #define fragColor gl_FragColor +#endif -out vec4 fragColor; void main() { fragColor = vec4(1.0, 1.0, 1.0, 1.0); diff --git a/tests/tests/wgpu-gpu/passthrough/shader.hlsl b/tests/tests/wgpu-gpu/passthrough/shader.hlsl index 4a1917c7b..27ec6189d 100644 --- a/tests/tests/wgpu-gpu/passthrough/shader.hlsl +++ b/tests/tests/wgpu-gpu/passthrough/shader.hlsl @@ -8,7 +8,7 @@ struct VSOut #ifdef SPIRV [shader("vertex")] #endif -VSOut vertex_main(uint vid: SV_VertexID) +VSOut vs_main(uint vid: SV_VertexID) { VSOut output; @@ -25,7 +25,7 @@ VSOut vertex_main(uint vid: SV_VertexID) #ifdef SPIRV [shader("pixel")] #endif -float4 fragment_main(VSOut input) : SV_TARGET +float4 fs_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 index 3fcc4dc63..aa6309459 100644 --- a/tests/tests/wgpu-gpu/passthrough/shader.metal +++ b/tests/tests/wgpu-gpu/passthrough/shader.metal @@ -7,7 +7,7 @@ struct VSOut { float4 position [[position]]; }; -vertex VSOut vertex_main(uint vid [[vertex_id]]) { +vertex VSOut vs_main(uint vid [[vertex_id]]) { VSOut out; float2 positions[3] = { @@ -20,6 +20,6 @@ vertex VSOut vertex_main(uint vid [[vertex_id]]) { return out; } -fragment float4 fragment_main(VSOut in [[stage_in]]) { +fragment float4 fs_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 index d6227dec6..9572c06e7 100644 --- a/tests/tests/wgpu-gpu/passthrough/shader.vert +++ b/tests/tests/wgpu-gpu/passthrough/shader.vert @@ -1,13 +1,20 @@ -#version 300 es - -out vec4 v_position; +// Fallback: use attribute for older versions +#if __VERSION__ < 130 + attribute float a_vertexId; + #define vertexId int(a_vertexId) +#else + #define vertexId gl_VertexID +#endif void main() { - vec2 positions[3] = vec2[3]( - vec2( 0.0, 0.5), - vec2(-0.5, -0.5), - vec2( 0.5, -0.5) - ); + vec2 pos; + if (vertexId == 0) { + pos = vec2( 0.0, 0.5); + } else if (vertexId == 1) { + pos = vec2(-0.5, -0.5); + } else { + pos = vec2( 0.5, -0.5); + } - gl_Position = vec4(positions[gl_VertexID], 0.0, 1.0); + gl_Position = vec4(pos, 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 index b5cdc8805..438323e93 100644 --- a/tests/tests/wgpu-gpu/passthrough/shader.wgsl +++ b/tests/tests/wgpu-gpu/passthrough/shader.wgsl @@ -3,7 +3,7 @@ struct VSOut { }; @vertex -fn vertex_main(@builtin(vertex_index) vid: u32) -> VSOut { +fn vs_main(@builtin(vertex_index) vid: u32) -> VSOut { var out: VSOut; // Hardcoded triangle in clip space @@ -18,6 +18,6 @@ fn vertex_main(@builtin(vertex_index) vid: u32) -> VSOut { } @fragment -fn fragment_main(in: VSOut) -> @location(0) vec4 { +fn fs_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 e9496b05d..b9b3648f4 100644 --- a/wgpu-core/src/device/global.rs +++ b/wgpu-core/src/device/global.rs @@ -1118,7 +1118,7 @@ impl Global { id: shader.to_trace(), data: file_names, label: desc.label.clone(), - num_workgroups: desc.num_workgroups, + entry_points: desc.entry_points.clone(), }); }; @@ -1413,7 +1413,7 @@ impl Global { Ok(module) => module, Err(e) => break 'error e, }; - if module.interface.is_none() { + if module.interface.interface().is_none() { passthrough_stages |= wgt::ShaderStages::VERTEX; } let stage = ResolvedProgrammableStageDescriptor { @@ -1443,7 +1443,7 @@ impl Global { Ok(module) => module, Err(e) => break 'error e, }; - if module.interface.is_none() { + if module.interface.interface().is_none() { passthrough_stages |= wgt::ShaderStages::TASK; } let state = ResolvedProgrammableStageDescriptor { @@ -1470,7 +1470,7 @@ impl Global { Ok(module) => module, Err(e) => break 'error e, }; - if mesh_module.interface.is_none() { + if mesh_module.interface.interface().is_none() { passthrough_stages |= wgt::ShaderStages::VERTEX; } let mesh_stage = ResolvedProgrammableStageDescriptor { @@ -1501,7 +1501,7 @@ impl Global { Ok(module) => module, Err(e) => break 'error e, }; - if module.interface.is_none() { + if module.interface.interface().is_none() { passthrough_stages |= wgt::ShaderStages::FRAGMENT; } let stage = ResolvedProgrammableStageDescriptor { @@ -1668,7 +1668,7 @@ impl Global { Ok(module) => module, Err(e) => break 'error e.into(), }; - if module.interface.is_none() && layout.is_none() { + if module.interface.interface().is_none() && layout.is_none() { break 'error pipeline::CreateComputePipelineError::Implicit( pipeline::ImplicitLayoutError::Passthrough(wgt::ShaderStages::COMPUTE), ); diff --git a/wgpu-core/src/device/resource.rs b/wgpu-core/src/device/resource.rs index b3304ecd7..ac9e5ddde 100644 --- a/wgpu-core/src/device/resource.rs +++ b/wgpu-core/src/device/resource.rs @@ -54,7 +54,7 @@ use crate::{ snatch::{SnatchGuard, SnatchLock, Snatchable}, timestamp_normalization::TIMESTAMP_NORMALIZATION_BUFFER_USES, track::{BindGroupStates, DeviceTracker, TrackerIndexAllocators, UsageScope, UsageScopePool}, - validation::{self, check_color_attachment_count}, + validation::{self, check_color_attachment_count, PassthroughInterface, ShaderMetaData}, weak_vec::WeakVec, FastHashMap, LabelHelpers, OnceCellOrLock, }; @@ -2383,7 +2383,7 @@ impl Device { let module = pipeline::ShaderModule { raw: ManuallyDrop::new(raw), device: self.clone(), - interface: Some(interface), + interface: ShaderMetaData::Interface(interface), label: desc.label.to_string(), }; @@ -2402,6 +2402,21 @@ impl Device { self.check_is_valid()?; self.require_features(wgt::Features::PASSTHROUGH_SHADERS)?; + // Mainly important for GLSL or SPIR-V or DXIL, which each take exactly 1 entry point. + if (descriptor.dxil.is_some() || descriptor.glsl.is_some()) + && descriptor.entry_points.len() != 1 + { + return Err(pipeline::CreateShaderModuleError::IncorrectPassthroughEntryPointCount); + } + + let entry_point_hashmap = || { + descriptor + .entry_points + .iter() + .map(|e| (e.name.to_string(), e.workgroup_size)) + .collect() + }; + let hal_shader = match self.backend() { wgt::Backend::Vulkan => hal::ShaderInput::SpirV( descriptor @@ -2411,15 +2426,9 @@ impl Device { ), wgt::Backend::Dx12 => { if let Some(dxil) = &descriptor.dxil { - hal::ShaderInput::Dxil { - shader: dxil, - num_workgroups: descriptor.num_workgroups, - } + hal::ShaderInput::Dxil { shader: dxil } } else if let Some(hlsl) = &descriptor.hlsl { - hal::ShaderInput::Hlsl { - shader: hlsl, - num_workgroups: descriptor.num_workgroups, - } + hal::ShaderInput::Hlsl { shader: hlsl } } else { return Err(pipeline::CreateShaderModuleError::NotCompiledForBackend); } @@ -2428,12 +2437,12 @@ impl Device { if let Some(metallib) = &descriptor.metallib { hal::ShaderInput::MetalLib { file: metallib, - num_workgroups: descriptor.num_workgroups, + num_workgroups: entry_point_hashmap(), } } else if let Some(msl) = &descriptor.msl { hal::ShaderInput::Msl { shader: msl, - num_workgroups: descriptor.num_workgroups, + num_workgroups: entry_point_hashmap(), } } else { return Err(pipeline::CreateShaderModuleError::NotCompiledForBackend); @@ -2444,7 +2453,6 @@ impl Device { .glsl .as_ref() .ok_or(pipeline::CreateShaderModuleError::NotCompiledForBackend)?, - num_workgroups: descriptor.num_workgroups, }, wgt::Backend::Noop => { return Err(pipeline::CreateShaderModuleError::NotCompiledForBackend) @@ -2475,7 +2483,13 @@ impl Device { let module = pipeline::ShaderModule { raw: ManuallyDrop::new(raw), device: self.clone(), - interface: None, + interface: ShaderMetaData::Passthrough(PassthroughInterface { + entry_point_names: descriptor + .entry_points + .iter() + .map(|e| e.name.to_string()) + .collect(), + }), label: descriptor.label.to_string(), }; @@ -3869,7 +3883,7 @@ impl Device { desc.stage.entry_point.as_ref().map(|ep| ep.as_ref()), )?; - if let Some(ref interface) = shader_module.interface { + if let Some(interface) = shader_module.interface.interface() { let _ = interface.check_stage( &mut binding_layout_source, &mut shader_binding_sizes, @@ -4399,7 +4413,7 @@ impl Device { ) .map_err(stage_err)?; - if let Some(ref interface) = vertex_shader_module.interface { + if let Some(interface) = vertex_shader_module.interface.interface() { io = interface .check_stage( &mut binding_layout_source, @@ -4443,7 +4457,7 @@ impl Device { ) .map_err(stage_err)?; - if let Some(ref interface) = task_shader_module.interface { + if let Some(interface) = task_shader_module.interface.interface() { io = interface .check_stage( &mut binding_layout_source, @@ -4485,7 +4499,7 @@ impl Device { ) .map_err(stage_err)?; - if let Some(ref interface) = mesh_shader_module.interface { + if let Some(interface) = mesh_shader_module.interface.interface() { io = interface .check_stage( &mut binding_layout_source, @@ -4537,7 +4551,7 @@ impl Device { ) .map_err(stage_err)?; - if let Some(ref interface) = shader_module.interface { + if let Some(interface) = shader_module.interface.interface() { io = interface .check_stage( &mut binding_layout_source, diff --git a/wgpu-core/src/device/trace.rs b/wgpu-core/src/device/trace.rs index 9a64fd216..8852798e2 100644 --- a/wgpu-core/src/device/trace.rs +++ b/wgpu-core/src/device/trace.rs @@ -5,7 +5,7 @@ mod replay; use core::convert::Infallible; -use alloc::{string::String, vec::Vec}; +use alloc::{borrow::Cow, string::String, vec::Vec}; use macro_rules_attribute::apply; use crate::{ @@ -189,7 +189,7 @@ pub enum Action<'a, R: ReferenceType> { data: Vec, label: crate::Label<'a>, - num_workgroups: (u32, u32, u32), + entry_points: Cow<'a, [wgt::PassthroughShaderEntryPoint<'a>]>, }, DestroyShaderModule(PointerId), CreateComputePipeline { diff --git a/wgpu-core/src/device/trace/replay.rs b/wgpu-core/src/device/trace/replay.rs index 17c98c63b..510f6ec64 100644 --- a/wgpu-core/src/device/trace/replay.rs +++ b/wgpu-core/src/device/trace/replay.rs @@ -3,8 +3,8 @@ use alloc::borrow::Cow; use super::Data; pub trait DataLoader { - fn load<'loader, 'data>(&'loader self, data: &'data Data) -> Cow<'data, [u8]>; - fn load_utf8<'loader, 'data>(&'loader self, data: &'data Data) -> Cow<'data, str>; + fn load<'data>(&self, data: &'data Data) -> Cow<'data, [u8]>; + fn load_utf8<'data>(&self, data: &'data Data) -> Cow<'data, str>; } #[cfg(feature = "std")] @@ -18,7 +18,7 @@ impl<'a> DiskTraceLoader<'a> { } impl DataLoader for DiskTraceLoader<'_> { - fn load<'loader, 'data>(&'loader self, data: &'data Data) -> Cow<'data, [u8]> { + fn load<'data>(&self, data: &'data Data) -> Cow<'data, [u8]> { match data { Data::File(file) => { Cow::from(std::fs::read(self.0.join(file)).expect("Failed to read data file")) @@ -33,7 +33,7 @@ impl DataLoader for DiskTraceLoader<'_> { /// # Panics /// /// If the data kind is not a string format or the data is not valid UTF-8 - fn load_utf8<'loader, 'data>(&'loader self, data: &'data Data) -> Cow<'data, str> { + fn load_utf8<'data>(&self, data: &'data Data) -> Cow<'data, str> { match data { Data::File(file) => Cow::from( std::fs::read_to_string(self.0.join(file)).expect("Failed to read data file"), diff --git a/wgpu-core/src/pipeline.rs b/wgpu-core/src/pipeline.rs index 5af0b6ec7..b8b669083 100644 --- a/wgpu-core/src/pipeline.rs +++ b/wgpu-core/src/pipeline.rs @@ -1,7 +1,7 @@ use alloc::{ - borrow::Cow, + borrow::{Cow, ToOwned}, boxed::Box, - string::{String, ToString as _}, + string::String, sync::Arc, vec::Vec, }; @@ -22,7 +22,9 @@ use crate::{ device::{Device, DeviceError, MissingDownlevelFlags, MissingFeatures, RenderPassContext}, id::{PipelineCacheId, PipelineLayoutId, ShaderModuleId}, resource::{InvalidResourceError, Labeled, TrackingData}, - resource_log, validation, Label, + resource_log, + validation::{self, ShaderMetaData}, + Label, }; /// Information about buffer bindings, which @@ -64,7 +66,7 @@ pub type ShaderModuleDescriptorPassthrough<'a> = pub struct ShaderModule { pub(crate) raw: ManuallyDrop>, pub(crate) device: Arc, - pub(crate) interface: Option, + pub(crate) interface: ShaderMetaData, /// The `label` from the descriptor used to create the resource. pub(crate) label: String, } @@ -95,11 +97,29 @@ impl ShaderModule { stage: naga::ShaderStage, entry_point: Option<&str>, ) -> Result { - match &self.interface { - Some(interface) => interface.finalize_entry_point_name(stage, entry_point), - None => entry_point - .map(|ep| ep.to_string()) - .ok_or(validation::StageError::NoEntryPointFound), + match self.interface { + ShaderMetaData::Interface(ref interface) => { + interface.finalize_entry_point_name(stage, entry_point) + } + ShaderMetaData::Passthrough(ref interface) => { + if let Some(ep) = entry_point { + if interface.entry_point_names.contains(ep) { + Ok(ep.to_owned()) + } else { + Err(validation::StageError::MissingEntryPoint(ep.to_owned())) + } + } else { + if interface.entry_point_names.len() != 1 { + return Err(validation::StageError::MultipleEntryPointsFound); + } + Ok(interface + .entry_point_names + .iter() + .next() + .unwrap() + .to_owned()) + } + } } } } @@ -135,6 +155,10 @@ pub enum CreateShaderModuleError { }, #[error("Generic shader passthrough does not contain any code compatible with this backend.")] NotCompiledForBackend, + #[error( + "Generic passthrough shaders which use GLSL or DXIL must contain exactly one entry point." + )] + IncorrectPassthroughEntryPointCount, } impl WebGpuError for CreateShaderModuleError { @@ -145,14 +169,16 @@ impl WebGpuError for CreateShaderModuleError { Self::Generation => ErrorType::Internal, - Self::Validation(..) | Self::InvalidGroupIndex { .. } => ErrorType::Validation, + Self::Validation(..) + | Self::InvalidGroupIndex { .. } + | Self::IncorrectPassthroughEntryPointCount + | Self::NotCompiledForBackend => ErrorType::Validation, #[cfg(feature = "wgsl")] Self::Parsing(..) => ErrorType::Validation, #[cfg(feature = "glsl")] Self::ParsingGlsl(..) => ErrorType::Validation, #[cfg(feature = "spirv")] Self::ParsingSpirV(..) => ErrorType::Validation, - Self::NotCompiledForBackend => ErrorType::Validation, } } } diff --git a/wgpu-core/src/validation.rs b/wgpu-core/src/validation.rs index 22c4c2407..19e856d94 100644 --- a/wgpu-core/src/validation.rs +++ b/wgpu-core/src/validation.rs @@ -7,7 +7,7 @@ use alloc::{ use core::fmt; use arrayvec::ArrayVec; -use hashbrown::hash_map::Entry; +use hashbrown::{hash_map::Entry, HashSet}; use shader_io_deductions::{display_deductions_as_optional_list, MaxVertexShaderOutputDeduction}; use thiserror::Error; use wgt::{ @@ -302,6 +302,29 @@ pub struct Interface { entry_points: FastHashMap<(naga::ShaderStage, String), EntryPoint>, } +#[derive(Debug)] +pub struct PassthroughInterface { + pub entry_point_names: HashSet, +} + +// Most shaders will use a standard interface which is very large. +// Passthrough shaders have a much smaller interface. No reason to +// box the standard interface though. +#[expect(clippy::large_enum_variant)] +#[derive(Debug)] +pub enum ShaderMetaData { + Interface(Interface), + Passthrough(PassthroughInterface), +} +impl ShaderMetaData { + pub fn interface(&self) -> Option<&Interface> { + match self { + Self::Interface(i) => Some(i), + Self::Passthrough(_) => None, + } + } +} + #[derive(Clone, Debug, Error)] #[non_exhaustive] pub enum BindingError { diff --git a/wgpu-hal/Cargo.toml b/wgpu-hal/Cargo.toml index f5ff1ed1c..9d0d5cbed 100644 --- a/wgpu-hal/Cargo.toml +++ b/wgpu-hal/Cargo.toml @@ -76,7 +76,6 @@ metal = [ "dep:arrayvec", "dep:block2", "dep:bytemuck", - "dep:hashbrown", "dep:libc", "dep:objc2", "dep:objc2-core-foundation", @@ -96,7 +95,6 @@ vulkan = [ "dep:bytemuck", "dep:drm", "dep:gpu-descriptor", - "dep:hashbrown", "dep:libc", "dep:libloading", "dep:ordered-float", @@ -113,7 +111,6 @@ gles = [ "dep:bytemuck", "dep:glow", "dep:glutin_wgl_sys", - "dep:hashbrown", "dep:js-sys", "dep:khronos-egl", "dep:libloading", @@ -139,7 +136,6 @@ dx12 = [ "dep:bit-set", "dep:bytemuck", "dep:gpu-allocator", - "dep:hashbrown", "dep:libloading", "dep:once_cell", "dep:ordered-float", @@ -218,7 +214,7 @@ thiserror.workspace = true # Target agnostic dependencies used only in backends. arrayvec = { workspace = true, optional = true } bytemuck = { workspace = true, optional = true, features = ["derive"] } -hashbrown = { workspace = true, optional = true } +hashbrown = { workspace = true } ordered-float = { workspace = true, optional = true } profiling = { workspace = true, optional = true, default-features = false } diff --git a/wgpu-hal/src/dx12/device.rs b/wgpu-hal/src/dx12/device.rs index 2ca1c8039..a1454c128 100644 --- a/wgpu-hal/src/dx12/device.rs +++ b/wgpu-hal/src/dx12/device.rs @@ -1860,24 +1860,16 @@ impl crate::Device for super::Device { raw_name, runtime_checks: desc.runtime_checks, }), - crate::ShaderInput::Dxil { - shader, - num_workgroups, - } => Ok(super::ShaderModule { + crate::ShaderInput::Dxil { shader } => Ok(super::ShaderModule { source: super::ShaderModuleSource::DxilPassthrough(super::DxilPassthroughShader { shader: shader.to_vec(), - num_workgroups, }), raw_name, runtime_checks: desc.runtime_checks, }), - crate::ShaderInput::Hlsl { - shader, - num_workgroups, - } => Ok(super::ShaderModule { + crate::ShaderInput::Hlsl { shader } => Ok(super::ShaderModule { source: super::ShaderModuleSource::HlslPassthrough(super::HlslPassthroughShader { shader: shader.to_owned(), - num_workgroups, }), raw_name, runtime_checks: desc.runtime_checks, diff --git a/wgpu-hal/src/dx12/mod.rs b/wgpu-hal/src/dx12/mod.rs index 8e2ea525c..2afefbe4a 100644 --- a/wgpu-hal/src/dx12/mod.rs +++ b/wgpu-hal/src/dx12/mod.rs @@ -1664,13 +1664,11 @@ impl crate::Queue for Queue { #[derive(Debug)] pub struct DxilPassthroughShader { pub shader: Vec, - pub num_workgroups: (u32, u32, u32), } #[derive(Debug)] pub struct HlslPassthroughShader { pub shader: String, - pub num_workgroups: (u32, u32, u32), } #[derive(Debug)] diff --git a/wgpu-hal/src/gles/adapter.rs b/wgpu-hal/src/gles/adapter.rs index c1e4ff25f..3d793d52e 100644 --- a/wgpu-hal/src/gles/adapter.rs +++ b/wgpu-hal/src/gles/adapter.rs @@ -14,6 +14,10 @@ const GL_UNMASKED_VENDOR_WEBGL: u32 = 0x9245; const GL_UNMASKED_RENDERER_WEBGL: u32 = 0x9246; impl super::Adapter { + pub fn get_glsl_version(&self) -> naga::back::glsl::Version { + self.shared.shading_language_version + } + /// Note that this function is intentionally lenient in regards to parsing, /// and will try to recover at least the first two version numbers without /// resulting in an `Err`. @@ -445,7 +449,8 @@ impl super::Adapter { | wgt::Features::TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES | wgt::Features::CLEAR_TEXTURE | wgt::Features::IMMEDIATES - | wgt::Features::DEPTH32FLOAT_STENCIL8; + | wgt::Features::DEPTH32FLOAT_STENCIL8 + | wgt::Features::PASSTHROUGH_SHADERS; features.set( wgt::Features::ADDRESS_MODE_CLAMP_TO_BORDER | wgt::Features::ADDRESS_MODE_CLAMP_TO_ZERO, extensions.contains("GL_EXT_texture_border_clamp") diff --git a/wgpu-hal/src/gles/device.rs b/wgpu-hal/src/gles/device.rs index c16a2ab93..08ad537ed 100644 --- a/wgpu-hal/src/gles/device.rs +++ b/wgpu-hal/src/gles/device.rs @@ -1,5 +1,11 @@ use alloc::{ - borrow::ToOwned, format, string::String, string::ToString as _, sync::Arc, vec, vec::Vec, + borrow::{Cow, ToOwned}, + format, + string::String, + string::ToString as _, + sync::Arc, + vec, + vec::Vec, }; use core::{cmp::max, convert::TryInto, num::NonZeroU32, ptr, sync::atomic::Ordering}; @@ -224,92 +230,103 @@ impl super::Device { context: CompilationContext, program: glow::Program, ) -> Result { - use naga::back::glsl; - let pipeline_options = glsl::PipelineOptions { - shader_stage: naga_stage, - entry_point: stage.entry_point.to_owned(), - multiview: context - .multiview_mask - .map(|a| NonZeroU32::new(a.get().count_ones()).unwrap()), + let source = 'outer: { + use naga::back::glsl; + let pipeline_options = glsl::PipelineOptions { + shader_stage: naga_stage, + entry_point: stage.entry_point.to_owned(), + multiview: context + .multiview_mask + .map(|a| NonZeroU32::new(a.get().count_ones()).unwrap()), + }; + + let naga = match stage.module.source { + super::ShaderModuleSource::Naga(ref naga) => naga, + super::ShaderModuleSource::Passthrough { ref source } => { + break 'outer Cow::Borrowed(source); + } + }; + + let (module, info) = naga::back::pipeline_constants::process_overrides( + &naga.module, + &naga.info, + Some((naga_stage, stage.entry_point)), + stage.constants, + ) + .map_err(|e| { + let msg = format!("{e}"); + crate::PipelineError::PipelineConstants(map_naga_stage(naga_stage), msg) + })?; + + let entry_point_index = module + .entry_points + .iter() + .position(|ep| ep.name.as_str() == stage.entry_point) + .ok_or(crate::PipelineError::EntryPoint(naga_stage))?; + + use naga::proc::BoundsCheckPolicy; + // The image bounds checks require the TEXTURE_LEVELS feature available in GL core 4.3+. + let version = gl.version(); + let image_check = if !version.is_embedded && (version.major, version.minor) >= (4, 3) { + BoundsCheckPolicy::ReadZeroSkipWrite + } else { + BoundsCheckPolicy::Unchecked + }; + + // Other bounds check are either provided by glsl or not implemented yet. + let policies = naga::proc::BoundsCheckPolicies { + index: BoundsCheckPolicy::Unchecked, + buffer: BoundsCheckPolicy::Unchecked, + image_load: image_check, + binding_array: BoundsCheckPolicy::Unchecked, + }; + + let mut output = String::new(); + let needs_temp_options = stage.zero_initialize_workgroup_memory + != context.layout.naga_options.zero_initialize_workgroup_memory; + let mut temp_options; + let naga_options = if needs_temp_options { + // We use a conditional here, as cloning the naga_options could be expensive + // That is, we want to avoid doing that unless we cannot avoid it + temp_options = context.layout.naga_options.clone(); + temp_options.zero_initialize_workgroup_memory = + stage.zero_initialize_workgroup_memory; + &temp_options + } else { + &context.layout.naga_options + }; + let mut writer = glsl::Writer::new( + &mut output, + &module, + &info, + naga_options, + &pipeline_options, + policies, + ) + .map_err(|e| { + let msg = format!("{e}"); + crate::PipelineError::Linkage(map_naga_stage(naga_stage), msg) + })?; + + let reflection_info = writer.write().map_err(|e| { + let msg = format!("{e}"); + crate::PipelineError::Linkage(map_naga_stage(naga_stage), msg) + })?; + + log::debug!("Naga generated shader:\n{output}"); + + context.consume_reflection( + gl, + &module, + info.get_entry_point(entry_point_index), + reflection_info, + naga_stage, + program, + ); + Cow::Owned(output) }; - let (module, info) = naga::back::pipeline_constants::process_overrides( - &stage.module.source.module, - &stage.module.source.info, - Some((naga_stage, stage.entry_point)), - stage.constants, - ) - .map_err(|e| { - let msg = format!("{e}"); - crate::PipelineError::PipelineConstants(map_naga_stage(naga_stage), msg) - })?; - - let entry_point_index = module - .entry_points - .iter() - .position(|ep| ep.name.as_str() == stage.entry_point) - .ok_or(crate::PipelineError::EntryPoint(naga_stage))?; - - use naga::proc::BoundsCheckPolicy; - // The image bounds checks require the TEXTURE_LEVELS feature available in GL core 4.3+. - let version = gl.version(); - let image_check = if !version.is_embedded && (version.major, version.minor) >= (4, 3) { - BoundsCheckPolicy::ReadZeroSkipWrite - } else { - BoundsCheckPolicy::Unchecked - }; - - // Other bounds check are either provided by glsl or not implemented yet. - let policies = naga::proc::BoundsCheckPolicies { - index: BoundsCheckPolicy::Unchecked, - buffer: BoundsCheckPolicy::Unchecked, - image_load: image_check, - binding_array: BoundsCheckPolicy::Unchecked, - }; - - let mut output = String::new(); - let needs_temp_options = stage.zero_initialize_workgroup_memory - != context.layout.naga_options.zero_initialize_workgroup_memory; - let mut temp_options; - let naga_options = if needs_temp_options { - // We use a conditional here, as cloning the naga_options could be expensive - // That is, we want to avoid doing that unless we cannot avoid it - temp_options = context.layout.naga_options.clone(); - temp_options.zero_initialize_workgroup_memory = stage.zero_initialize_workgroup_memory; - &temp_options - } else { - &context.layout.naga_options - }; - let mut writer = glsl::Writer::new( - &mut output, - &module, - &info, - naga_options, - &pipeline_options, - policies, - ) - .map_err(|e| { - let msg = format!("{e}"); - crate::PipelineError::Linkage(map_naga_stage(naga_stage), msg) - })?; - - let reflection_info = writer.write().map_err(|e| { - let msg = format!("{e}"); - crate::PipelineError::Linkage(map_naga_stage(naga_stage), msg) - })?; - - log::debug!("Naga generated shader:\n{output}"); - - context.consume_reflection( - gl, - &module, - info.get_entry_point(entry_point_index), - reflection_info, - naga_stage, - program, - ); - - unsafe { Self::compile_shader(gl, &output, naga_stage, stage.module.label.as_deref()) } + unsafe { Self::compile_shader(gl, &source, naga_stage, stage.module.label.as_deref()) } } unsafe fn create_pipeline<'a>( @@ -488,8 +505,13 @@ impl super::Device { for (stage_idx, stage_items) in immediates_items.into_iter().enumerate() { for item in stage_items { - let naga_module = &shaders[stage_idx].1.module.source.module; - let type_inner = &naga_module.types[item.ty].inner; + let source = &shaders[stage_idx].1.module.source; + let super::ShaderModuleSource::Naga(naga_module) = source else { + // ImmediateItem can only be constructed given a naga module, as it requires a type handle. + // Passthrough shaders will have immediates_items empty + unreachable!("Passthrough shaders don't currently support immediates on GLES"); + }; + let type_inner = &naga_module.module.types[item.ty].inner; let location = unsafe { gl.get_uniform_location(program, &item.access_path) }; @@ -505,7 +527,7 @@ impl super::Device { uniforms.push(super::ImmediateDesc { location, offset: item.offset, - size_bytes: type_inner.size(naga_module.to_ctx()), + size_bytes: type_inner.size(naga_module.module.to_ctx()), ty: type_inner.clone(), }); } @@ -1366,9 +1388,11 @@ impl crate::Device for super::Device { Ok(super::ShaderModule { source: match shader { - crate::ShaderInput::Naga(naga) => naga, + crate::ShaderInput::Naga(naga) => super::ShaderModuleSource::Naga(naga), // The backend doesn't yet expose this feature so it should be fine - crate::ShaderInput::Glsl { .. } => unimplemented!(), + crate::ShaderInput::Glsl { shader, .. } => super::ShaderModuleSource::Passthrough { + source: shader.to_owned(), + }, crate::ShaderInput::SpirV(_) | crate::ShaderInput::MetalLib { .. } | crate::ShaderInput::Msl { .. } diff --git a/wgpu-hal/src/gles/mod.rs b/wgpu-hal/src/gles/mod.rs index 6f8c865b2..87ae98b8d 100644 --- a/wgpu-hal/src/gles/mod.rs +++ b/wgpu-hal/src/gles/mod.rs @@ -606,9 +606,15 @@ impl crate::DynBindGroup for BindGroup {} type ShaderId = u32; +#[derive(Debug)] +pub enum ShaderModuleSource { + Naga(crate::NagaShader), + Passthrough { source: String }, +} + #[derive(Debug)] pub struct ShaderModule { - source: crate::NagaShader, + source: ShaderModuleSource, label: Option, id: ShaderId, } diff --git a/wgpu-hal/src/lib.rs b/wgpu-hal/src/lib.rs index 641e1fbab..d08ecec67 100644 --- a/wgpu-hal/src/lib.rs +++ b/wgpu-hal/src/lib.rs @@ -2361,24 +2361,21 @@ pub enum ShaderInput<'a> { Naga(NagaShader), MetalLib { file: &'a [u8], - num_workgroups: (u32, u32, u32), + num_workgroups: hashbrown::HashMap, }, Msl { shader: &'a str, - num_workgroups: (u32, u32, u32), + num_workgroups: hashbrown::HashMap, }, SpirV(&'a [u32]), Dxil { shader: &'a [u8], - num_workgroups: (u32, u32, u32), }, Hlsl { shader: &'a str, - num_workgroups: (u32, u32, u32), }, Glsl { shader: &'a str, - num_workgroups: (u32, u32, u32), }, } diff --git a/wgpu-hal/src/metal/device.rs b/wgpu-hal/src/metal/device.rs index 032cfaef7..b026c1701 100644 --- a/wgpu-hal/src/metal/device.rs +++ b/wgpu-hal/src/metal/device.rs @@ -340,21 +340,24 @@ impl super::Device { immutable_buffer_mask, }) } - ShaderModuleSource::Passthrough(ref shader) => Ok(CompiledShader { - library: shader.library.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, - depth: shader.num_workgroups.2 as usize, - }, - wg_memory_sizes: vec![], - sized_bindings: vec![], - immutable_buffer_mask: 0, - }), + ShaderModuleSource::Passthrough(ref shader) => { + let size = shader.num_workgroups[stage.entry_point]; + Ok(CompiledShader { + library: shader.library.clone(), + function: shader + .library + .newFunctionWithName(&NSString::from_str(stage.entry_point)) + .ok_or(crate::PipelineError::EntryPoint(naga_stage))?, + wg_size: MTLSize { + width: size.0 as usize, + height: size.1 as usize, + depth: size.2 as usize, + }, + wg_memory_sizes: vec![], + sized_bindings: vec![], + immutable_buffer_mask: 0, + }) + } } } @@ -1673,6 +1676,7 @@ impl crate::Device for super::Device { let module = desc.stage.module; let cs = if let ShaderModuleSource::Passthrough(passthrough_desc) = &module.source { + let size = passthrough_desc.num_workgroups[desc.stage.entry_point]; CompiledShader { library: passthrough_desc.library.clone(), function: passthrough_desc @@ -1680,9 +1684,9 @@ impl crate::Device for super::Device { .newFunctionWithName(&NSString::from_str(desc.stage.entry_point)) .ok_or(crate::PipelineError::EntryPoint(naga::ShaderStage::Compute))?, wg_size: MTLSize { - width: passthrough_desc.num_workgroups.0 as usize, - height: passthrough_desc.num_workgroups.1 as usize, - depth: passthrough_desc.num_workgroups.2 as usize, + width: size.0 as usize, + height: size.1 as usize, + depth: size.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 3ef5795da..f9e9a4b9f 100644 --- a/wgpu-hal/src/metal/mod.rs +++ b/wgpu-hal/src/metal/mod.rs @@ -28,7 +28,11 @@ mod library_from_metallib; mod surface; mod time; -use alloc::{string::ToString as _, sync::Arc, vec::Vec}; +use alloc::{ + string::{String, ToString as _}, + sync::Arc, + vec::Vec, +}; use core::{fmt, iter, ops, ptr::NonNull, sync::atomic}; use bitflags::bitflags; @@ -863,7 +867,7 @@ pub enum ShaderModuleSource { #[derive(Debug)] pub struct PassthroughShader { pub library: Retained>, - pub num_workgroups: (u32, u32, u32), + pub num_workgroups: HashMap, } unsafe impl Send for PassthroughShader {} diff --git a/wgpu-types/src/shader.rs b/wgpu-types/src/shader.rs index 121b7875b..e4cb2a47b 100644 --- a/wgpu-types/src/shader.rs +++ b/wgpu-types/src/shader.rs @@ -95,16 +95,30 @@ impl Default for ShaderRuntimeChecks { } } +/// Describes a single entry point in a passthrough shader descriptor. +#[derive(Debug, Clone)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +pub struct PassthroughShaderEntryPoint<'a> { + /// The name of the entry point. Only used in validation and for GLSL or DXIL. + pub name: Cow<'a, str>, + /// Number of workgroups in each dimension x, y and z. Only used for metal with + /// compute-like shader stages. + pub workgroup_size: (u32, u32, u32), +} + /// Descriptor for a shader module given by any of several sources. /// These shaders are passed through directly to the underlying api. /// At least one shader type that may be used by the backend must be `Some` or a panic is raised. +/// +/// Note that you shouldn't expect this to work with bindings except on SPIR-V, and even on SPIR-V +/// there will be some caveats. #[derive(Debug, Clone)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct CreateShaderModuleDescriptorPassthrough<'a, L> { /// 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), + /// The list of entry points and their corresponding workgroup sizes. + pub entry_points: Cow<'a, [PassthroughShaderEntryPoint<'a>]>, /// Binary SPIR-V data, in 4-byte words. pub spirv: Option>, @@ -128,7 +142,7 @@ impl<'a, L: Default> Default for CreateShaderModuleDescriptorPassthrough<'a, L> fn default() -> Self { Self { label: Default::default(), - num_workgroups: (0, 0, 0), + entry_points: Cow::Borrowed(&[]), spirv: None, dxil: None, metallib: None, @@ -148,7 +162,7 @@ impl<'a, L> CreateShaderModuleDescriptorPassthrough<'a, L> { ) -> CreateShaderModuleDescriptorPassthrough<'a, K> { CreateShaderModuleDescriptorPassthrough { label: fun(&self.label), - num_workgroups: self.num_workgroups, + entry_points: self.entry_points.clone(), spirv: self.spirv.clone(), metallib: self.metallib.clone(), dxil: self.dxil.clone(), diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 346205026..e17be8f48 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -142,18 +142,19 @@ pub use wgt::{ GlFenceBehavior, Gles3MinorVersion, HalCounters, ImageSubresourceRange, IndexFormat, InstanceDescriptor, InstanceFlags, InternalCounters, Limits, LoadOpDontCare, MemoryBudgetThresholds, MemoryHints, MipmapFilterMode, MultisampleState, NoopBackendOptions, - Origin2d, Origin3d, PipelineStatisticsTypes, PollError, PollStatus, PolygonMode, - PowerPreference, PredefinedColorSpace, PresentMode, PresentationTimestamp, PrimitiveState, - PrimitiveTopology, QueryType, RenderBundleDepthStencil, RequestAdapterError, - SamplerBindingType, SamplerBorderColor, ShaderLocation, ShaderModel, ShaderRuntimeChecks, - ShaderStages, StencilFaceState, StencilOperation, StencilState, StorageTextureAccess, - SurfaceCapabilities, SurfaceStatus, TexelCopyBufferLayout, TextureAspect, TextureChannel, - TextureDimension, TextureFormat, TextureFormatFeatureFlags, TextureFormatFeatures, - TextureSampleType, TextureTransition, TextureUsages, TextureUses, TextureViewDimension, Trace, - VertexAttribute, VertexFormat, VertexStepMode, WasmNotSend, WasmNotSendSync, WasmNotSync, - WriteOnly, WriteOnlyIter, COPY_BUFFER_ALIGNMENT, COPY_BYTES_PER_ROW_ALIGNMENT, - IMMEDIATE_DATA_ALIGNMENT, MAP_ALIGNMENT, MAXIMUM_SUBGROUP_MAX_SIZE, MINIMUM_SUBGROUP_MIN_SIZE, - QUERY_RESOLVE_BUFFER_ALIGNMENT, QUERY_SET_MAX_QUERIES, QUERY_SIZE, VERTEX_ALIGNMENT, + Origin2d, Origin3d, PassthroughShaderEntryPoint, PipelineStatisticsTypes, PollError, + PollStatus, PolygonMode, PowerPreference, PredefinedColorSpace, PresentMode, + PresentationTimestamp, PrimitiveState, PrimitiveTopology, QueryType, RenderBundleDepthStencil, + RequestAdapterError, SamplerBindingType, SamplerBorderColor, ShaderLocation, ShaderModel, + ShaderRuntimeChecks, ShaderStages, StencilFaceState, StencilOperation, StencilState, + StorageTextureAccess, SurfaceCapabilities, SurfaceStatus, TexelCopyBufferLayout, TextureAspect, + TextureChannel, TextureDimension, TextureFormat, TextureFormatFeatureFlags, + TextureFormatFeatures, TextureSampleType, TextureTransition, TextureUsages, TextureUses, + TextureViewDimension, Trace, VertexAttribute, VertexFormat, VertexStepMode, WasmNotSend, + WasmNotSendSync, WasmNotSync, WriteOnly, WriteOnlyIter, COPY_BUFFER_ALIGNMENT, + COPY_BYTES_PER_ROW_ALIGNMENT, IMMEDIATE_DATA_ALIGNMENT, MAP_ALIGNMENT, + MAXIMUM_SUBGROUP_MAX_SIZE, MINIMUM_SUBGROUP_MIN_SIZE, QUERY_RESOLVE_BUFFER_ALIGNMENT, + QUERY_SET_MAX_QUERIES, QUERY_SIZE, VERTEX_ALIGNMENT, }; #[expect(deprecated)] diff --git a/wgpu/src/macros/mod.rs b/wgpu/src/macros/mod.rs index 90bbeea82..7adb6b169 100644 --- a/wgpu/src/macros/mod.rs +++ b/wgpu/src/macros/mod.rs @@ -141,8 +141,11 @@ 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)*))), - // This is unused for SPIR-V - num_workgroups: (0, 0, 0), + entry_points: $crate::__macro_helpers::Cow::Borrowed(&[$crate::PassthroughShaderEntryPoint { + name: $crate::__macro_helpers::Cow::Borrowed("main"), + // This is unused for SPIR-V + workgroup_size: (0, 0, 0), + }]), dxil: None, metallib: None, msl: None,