Skip to content

Commit 10e947c

Browse files
committed
Add support for GLSL passthrough
1 parent 35db26b commit 10e947c

File tree

32 files changed

+617
-610
lines changed

32 files changed

+617
-610
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ depth_stencil: Some(wgpu::DepthStencilState::stencil(
158158
- Added support for `insert_debug_marker`, `push_debug_group` and `pop_debug_group`. By @evilpie in [#9017](https://github.com/gfx-rs/wgpu/pull/9017).
159159
- Added support for `begin_occlusion_query` and `end_occlusion_query`. By @evilpie in [#9039](https://github.com/gfx-rs/wgpu/pull/9039).
160160

161+
#### GLES
162+
- Added support for GLSL passthrough. By @inner-daemons in [#9064](https://github.com/gfx-rs/wgpu/pull/9064).
163+
161164
### Changes
162165

163166
#### General
@@ -247,6 +250,7 @@ depth_stencil: Some(wgpu::DepthStencilState::stencil(
247250
#### General
248251

249252
- Tracing now uses the `.metal` extension for metal source files, instead of `.msl`. By @inner-daemons in #8880.
253+
- 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).
250254

251255
#### naga
252256

examples/features/src/mesh_shader/mod.rs

Lines changed: 39 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,43 @@
1-
// Same as in mesh shader tests
2-
fn compile_wgsl(device: &wgpu::Device) -> wgpu::ShaderModule {
3-
// Workgroup memory zero initialization can be expensive for mesh shaders
4-
unsafe {
5-
device.create_shader_module_trusted(
6-
wgpu::ShaderModuleDescriptor {
7-
label: None,
8-
source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()),
9-
},
10-
wgpu::ShaderRuntimeChecks::unchecked(),
11-
)
12-
}
13-
}
14-
fn compile_hlsl(device: &wgpu::Device, entry: &str, stage_str: &str) -> wgpu::ShaderModule {
15-
let out_path = format!(
16-
"{}/src/mesh_shader/shader.{stage_str}.cso",
17-
env!("CARGO_MANIFEST_DIR")
18-
);
19-
let cmd = std::process::Command::new("dxc")
20-
.args([
21-
"-T",
22-
&format!("{stage_str}_6_5"),
23-
"-E",
24-
entry,
25-
&format!("{}/src/mesh_shader/shader.hlsl", env!("CARGO_MANIFEST_DIR")),
26-
"-Fo",
27-
&out_path,
28-
])
29-
.output()
30-
.unwrap();
31-
if !cmd.status.success() {
32-
panic!("DXC failed:\n{}", String::from_utf8(cmd.stderr).unwrap());
33-
}
34-
let file = std::fs::read(&out_path).unwrap();
35-
std::fs::remove_file(out_path).unwrap();
36-
unsafe {
37-
device.create_shader_module_passthrough(wgpu::ShaderModuleDescriptorPassthrough {
38-
label: None,
39-
num_workgroups: (1, 1, 1),
40-
dxil: Some(std::borrow::Cow::Owned(file)),
41-
..Default::default()
42-
})
43-
}
44-
}
45-
46-
fn compile_msl(device: &wgpu::Device) -> wgpu::ShaderModule {
47-
unsafe {
48-
device.create_shader_module_passthrough(wgpu::ShaderModuleDescriptorPassthrough {
49-
label: None,
50-
msl: Some(std::borrow::Cow::Borrowed(include_str!("shader.metal"))),
51-
num_workgroups: (1, 1, 1),
52-
..Default::default()
53-
})
54-
}
55-
}
56-
57-
struct Shaders {
58-
ts: wgpu::ShaderModule,
59-
ms: wgpu::ShaderModule,
60-
fs: wgpu::ShaderModule,
61-
ts_name: &'static str,
62-
ms_name: &'static str,
63-
fs_name: &'static str,
64-
}
1+
use std::borrow::Cow;
652

66-
fn get_shaders(device: &wgpu::Device, backend: wgpu::Backend) -> Shaders {
3+
fn get_shader(device: &wgpu::Device, backend: wgpu::Backend) -> wgpu::ShaderModule {
674
// In the case that the platform does support mesh shaders, the dummy
685
// shader is used to avoid requiring PASSTHROUGH_SHADERS.
696
match backend {
707
wgpu::Backend::Vulkan => {
71-
let compiled = compile_wgsl(device);
72-
Shaders {
73-
ts: compiled.clone(),
74-
ms: compiled.clone(),
75-
fs: compiled.clone(),
76-
ts_name: "ts_main",
77-
ms_name: "ms_main",
78-
fs_name: "fs_main",
8+
// Workgroup memory zero initialization can be expensive for mesh shaders
9+
unsafe {
10+
device.create_shader_module_trusted(
11+
wgpu::ShaderModuleDescriptor {
12+
label: None,
13+
source: wgpu::ShaderSource::Wgsl(include_str!("shader.wgsl").into()),
14+
},
15+
wgpu::ShaderRuntimeChecks::unchecked(),
16+
)
7917
}
8018
}
81-
wgpu::Backend::Dx12 => Shaders {
82-
ts: compile_hlsl(device, "Task", "as"),
83-
ms: compile_hlsl(device, "Mesh", "ms"),
84-
fs: compile_hlsl(device, "Frag", "ps"),
85-
ts_name: "main",
86-
ms_name: "main",
87-
fs_name: "main",
19+
wgpu::Backend::Dx12 | wgpu::Backend::Metal => unsafe {
20+
device.create_shader_module_passthrough(wgpu::ShaderModuleDescriptorPassthrough {
21+
label: None,
22+
entry_points: Cow::Borrowed(&[
23+
wgpu::PassthroughShaderEntryPoint {
24+
name: "ts_main".into(),
25+
workgroup_size: (1, 1, 1),
26+
},
27+
wgpu::PassthroughShaderEntryPoint {
28+
name: "ms_main".into(),
29+
workgroup_size: (1, 1, 1),
30+
},
31+
wgpu::PassthroughShaderEntryPoint {
32+
name: "fs_main".into(),
33+
workgroup_size: (0, 0, 0),
34+
},
35+
]),
36+
hlsl: Some(Cow::Borrowed(include_str!("shader.hlsl"))),
37+
msl: Some(Cow::Borrowed(include_str!("shader.metal"))),
38+
..Default::default()
39+
})
8840
},
89-
wgpu::Backend::Metal => {
90-
let compiled = compile_msl(device);
91-
Shaders {
92-
ts: compiled.clone(),
93-
ms: compiled.clone(),
94-
fs: compiled.clone(),
95-
ts_name: "taskShader",
96-
ms_name: "meshShader",
97-
fs_name: "fragShader",
98-
}
99-
}
10041
_ => unreachable!(),
10142
}
10243
}
@@ -111,14 +52,7 @@ impl crate::framework::Example for Example {
11152
device: &wgpu::Device,
11253
_queue: &wgpu::Queue,
11354
) -> Self {
114-
let Shaders {
115-
ts,
116-
ms,
117-
fs,
118-
ts_name,
119-
ms_name,
120-
fs_name,
121-
} = get_shaders(device, adapter.get_info().backend);
55+
let shader = get_shader(device, adapter.get_info().backend);
12256
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
12357
label: None,
12458
bind_group_layouts: &[],
@@ -128,18 +62,18 @@ impl crate::framework::Example for Example {
12862
label: None,
12963
layout: Some(&pipeline_layout),
13064
task: Some(wgpu::TaskState {
131-
module: &ts,
132-
entry_point: Some(ts_name),
65+
module: &shader,
66+
entry_point: Some("ts_main"),
13367
compilation_options: Default::default(),
13468
}),
13569
mesh: wgpu::MeshState {
136-
module: &ms,
137-
entry_point: Some(ms_name),
70+
module: &shader,
71+
entry_point: Some("ms_main"),
13872
compilation_options: Default::default(),
13973
},
14074
fragment: Some(wgpu::FragmentState {
141-
module: &fs,
142-
entry_point: Some(fs_name),
75+
module: &shader,
76+
entry_point: Some("fs_main"),
14377
compilation_options: Default::default(),
14478
targets: &[Some(config.view_formats[0].into())],
14579
}),
Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,50 @@
1-
struct OutVertex {
1+
struct OutVertex
2+
{
23
float4 Position : SV_POSITION;
3-
float4 Color: COLOR;
4+
float4 Color : COLOR;
45
};
5-
struct OutPrimitive {
6+
struct OutPrimitive
7+
{
68
float4 ColorMask : COLOR_MASK : PRIMITIVE;
7-
bool CullPrimitive: SV_CullPrimitive;
9+
bool CullPrimitive : SV_CullPrimitive;
810
};
9-
struct InVertex {
10-
float4 Color: COLOR;
11+
struct InVertex
12+
{
13+
float4 Color : COLOR;
1114
};
12-
struct InPrimitive {
15+
struct InPrimitive
16+
{
1317
float4 ColorMask : COLOR_MASK : PRIMITIVE;
1418
};
15-
struct PayloadData {
19+
struct PayloadData
20+
{
1621
float4 ColorMask;
1722
bool Visible;
1823
};
1924

20-
21-
static const float4 positions[3] = {float4(0., 1.0, 0., 1.0), float4(-1.0, -1.0, 0., 1.0), float4(1.0, -1.0, 0., 1.0)};
22-
static const float4 colors[3] = {float4(0., 1., 0., 1.), float4(0., 0., 1., 1.), float4(1., 0., 0., 1.)};
25+
static const float4 positions[3] = { float4(0., 1.0, 0., 1.0), float4(-1.0, -1.0, 0., 1.0), float4(1.0, -1.0, 0., 1.0) };
26+
static const float4 colors[3] = { float4(0., 1., 0., 1.), float4(0., 0., 1., 1.), float4(1., 0., 0., 1.) };
2327

2428
groupshared PayloadData outPayload;
2529

2630
[numthreads(1, 1, 1)]
27-
void Task() {
31+
void ts_main()
32+
{
2833
outPayload.ColorMask = float4(1.0, 1.0, 0.0, 1.0);
2934
outPayload.Visible = true;
3035
DispatchMesh(3, 1, 1, outPayload);
3136
}
3237

3338
[outputtopology("triangle")]
3439
[numthreads(1, 1, 1)]
35-
void Mesh(out indices uint3 triangles[1], out vertices OutVertex vertices[3], out primitives OutPrimitive primitives[1], in payload PayloadData payload) {
40+
void ms_main(out indices uint3 triangles[1], out vertices OutVertex vertices[3], out primitives OutPrimitive primitives[1], in payload PayloadData payload)
41+
{
3642
SetMeshOutputCounts(3, 1);
3743

3844
vertices[0].Position = positions[0];
3945
vertices[1].Position = positions[1];
4046
vertices[2].Position = positions[2];
41-
47+
4248
vertices[0].Color = colors[0] * payload.ColorMask;
4349
vertices[1].Color = colors[1] * payload.ColorMask;
4450
vertices[2].Color = colors[2] * payload.ColorMask;
@@ -48,6 +54,7 @@ void Mesh(out indices uint3 triangles[1], out vertices OutVertex vertices[3], ou
4854
primitives[0].CullPrimitive = !payload.Visible;
4955
}
5056

51-
float4 Frag(InVertex vertex, InPrimitive primitive) : SV_Target {
57+
float4 fs_main(InVertex vertex, InPrimitive primitive) : SV_Target
58+
{
5259
return vertex.Color * primitive.ColorMask;
5360
}

examples/features/src/mesh_shader/shader.metal

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ constant float4 colors[3] = {
4444

4545

4646
[[object]]
47-
void taskShader(uint3 tid [[thread_position_in_grid]], object_data PayloadData &outPayload [[payload]], mesh_grid_properties grid) {
47+
void ts_main(uint3 tid [[thread_position_in_grid]], object_data PayloadData &outPayload [[payload]], mesh_grid_properties grid) {
4848
outPayload.ColorMask = float4(1.0, 1.0, 0.0, 1.0);
4949
outPayload.Visible = true;
5050
grid.set_threadgroups_per_grid(uint3(3, 1, 1));
5151
}
5252

5353
[[mesh]]
54-
void meshShader(
54+
void ms_main(
5555
object_data PayloadData const& payload [[payload]],
5656
Meshlet out
5757
)
@@ -72,6 +72,6 @@ void meshShader(
7272
out.set_primitive(0, prim);
7373
}
7474

75-
fragment float4 fragShader(FragmentIn data [[stage_in]]) {
75+
fragment float4 fs_main(FragmentIn data [[stage_in]]) {
7676
return data.Color * data.ColorMask;
7777
}

player/src/lib.rs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ impl Player {
269269
id,
270270
data,
271271
label,
272-
num_workgroups,
272+
entry_points,
273273
} => {
274274
let spirv = data.iter().find_map(|a| {
275275
if a.kind() == DataKind::Spv {
@@ -281,27 +281,33 @@ impl Player {
281281
None
282282
}
283283
});
284-
let dxil = data
285-
.iter()
286-
.find_map(|a| (a.kind() == DataKind::Dxil).then(|| loader.load(a)));
287-
let hlsl = data
288-
.iter()
289-
.find_map(|a| (a.kind() == DataKind::Hlsl).then(|| loader.load_utf8(a)));
290-
let metallib = data
291-
.iter()
292-
.find_map(|a| (a.kind() == DataKind::MetalLib).then(|| loader.load(a)));
293-
let msl = data
294-
.iter()
295-
.find_map(|a| (a.kind() == DataKind::Msl).then(|| loader.load_utf8(a)));
296-
let glsl = data
297-
.iter()
298-
.find_map(|a| (a.kind() == DataKind::Glsl).then(|| loader.load_utf8(a)));
299-
let wgsl = data
300-
.iter()
301-
.find_map(|a| (a.kind() == DataKind::Wgsl).then(|| loader.load_utf8(a)));
284+
let dxil = data.iter().find_map(|a| {
285+
(a.kind() == DataKind::Dxil).then(|| Cow::Owned(loader.load(a).into_owned()))
286+
});
287+
let hlsl = data.iter().find_map(|a| {
288+
(a.kind() == DataKind::Hlsl)
289+
.then(|| Cow::Owned(loader.load_utf8(a).into_owned()))
290+
});
291+
let metallib = data.iter().find_map(|a| {
292+
(a.kind() == DataKind::MetalLib)
293+
.then(|| Cow::Owned(loader.load(a).into_owned()))
294+
});
295+
let msl = data.iter().find_map(|a| {
296+
(a.kind() == DataKind::Msl)
297+
.then(|| Cow::Owned(loader.load_utf8(a).into_owned()))
298+
});
299+
let glsl = data.iter().find_map(|a| {
300+
(a.kind() == DataKind::Glsl)
301+
.then(|| Cow::Owned(loader.load_utf8(a).into_owned()))
302+
});
303+
let wgsl = data.iter().find_map(|a| {
304+
(a.kind() == DataKind::Wgsl)
305+
.then(|| Cow::Owned(loader.load_utf8(a).into_owned()))
306+
});
307+
302308
let desc = wgt::CreateShaderModuleDescriptorPassthrough {
303309
label,
304-
num_workgroups,
310+
entry_points,
305311

306312
spirv,
307313
dxil,

0 commit comments

Comments
 (0)