mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-21 15:00:57 +01:00
Naga mesh shader WGSL writer (#8481)
Co-authored-by: Jim Blandy <jimb@red-bean.com> Co-authored-by: Erich Gubler <erichdongubler@gmail.com> Co-authored-by: SupaMaggie70Incorporated <magnus.larsson.mn@gmail.com> Co-authored-by: SupaMaggie70Incorporated <85136135+SupaMaggie70Incorporated@users.noreply.github.com> Co-authored-by: Inner Daemons <85136135+inner-daemons@users.noreply.github.com> Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com>
This commit is contained in:
11 files changed
+337
-29
No files matched your search
@@ -335,6 +335,7 @@ By @cwfitzgerald in [#8609](https://github.com/gfx-rs/wgpu/pull/8609).
|
||||
- The `STORAGE_READ_ONLY` texture usage is now permitted to coexist with other read-only usages. By @andyleiserson in [#8490](https://github.com/gfx-rs/wgpu/pull/8490).
|
||||
- Validate that buffers are unmapped in `write_buffer` calls. By @ErichDonGubler in [#8454](https://github.com/gfx-rs/wgpu/pull/8454).
|
||||
- Add WGSL parsing for mesh shaders. By @inner-daemons in [#8370](https://github.com/gfx-rs/wgpu/pull/8370).
|
||||
- Add WGSL writing for mesh shaders. By @Slightlyclueless in [#8481](https://github.com/gfx-rs/wgpu/pull/8481).
|
||||
- Shorten critical section inside present such that the snatch write lock is no longer held during present, preventing other work happening on other threads. By @cwfitzgerald in [#8608](https://github.com/gfx-rs/wgpu/pull/8608).
|
||||
|
||||
#### naga
|
||||
|
||||
+107
-14
@@ -33,6 +33,9 @@ enum Attribute {
|
||||
BlendSrc(u32),
|
||||
Stage(ShaderStage),
|
||||
WorkGroupSize([u32; 3]),
|
||||
MeshStage(String),
|
||||
TaskPayload(String),
|
||||
PerPrimitive,
|
||||
}
|
||||
|
||||
/// The WGSL form that `write_expr_with_indirection` should use to render a Naga
|
||||
@@ -211,9 +214,37 @@ impl<W: Write> Writer<W> {
|
||||
Attribute::Stage(ShaderStage::Compute),
|
||||
Attribute::WorkGroupSize(ep.workgroup_size),
|
||||
],
|
||||
ShaderStage::Mesh | ShaderStage::Task => unreachable!(),
|
||||
ShaderStage::Mesh => {
|
||||
let mesh_output_name = module.global_variables
|
||||
[ep.mesh_info.as_ref().unwrap().output_variable]
|
||||
.name
|
||||
.clone()
|
||||
.unwrap();
|
||||
let mut mesh_attrs = vec![
|
||||
Attribute::MeshStage(mesh_output_name),
|
||||
Attribute::WorkGroupSize(ep.workgroup_size),
|
||||
];
|
||||
if ep.task_payload.is_some() {
|
||||
let payload_name = module.global_variables[ep.task_payload.unwrap()]
|
||||
.name
|
||||
.clone()
|
||||
.unwrap();
|
||||
mesh_attrs.push(Attribute::TaskPayload(payload_name));
|
||||
}
|
||||
mesh_attrs
|
||||
}
|
||||
ShaderStage::Task => {
|
||||
let payload_name = module.global_variables[ep.task_payload.unwrap()]
|
||||
.name
|
||||
.clone()
|
||||
.unwrap();
|
||||
vec![
|
||||
Attribute::Stage(ShaderStage::Task),
|
||||
Attribute::TaskPayload(payload_name),
|
||||
Attribute::WorkGroupSize(ep.workgroup_size),
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
self.write_attributes(&attributes)?;
|
||||
// Add a newline after attribute
|
||||
writeln!(self.out)?;
|
||||
@@ -247,6 +278,7 @@ impl<W: Write> Writer<W> {
|
||||
let mut needs_f16 = false;
|
||||
let mut needs_dual_source_blending = false;
|
||||
let mut needs_clip_distances = false;
|
||||
let mut needs_mesh_shaders = false;
|
||||
|
||||
// Determine which `enable` declarations are needed
|
||||
for (_, ty) in module.types.iter() {
|
||||
@@ -267,6 +299,25 @@ impl<W: Write> Writer<W> {
|
||||
crate::Binding::BuiltIn(crate::BuiltIn::ClipDistance) => {
|
||||
needs_clip_distances = true;
|
||||
}
|
||||
crate::Binding::Location {
|
||||
per_primitive: true,
|
||||
..
|
||||
} => {
|
||||
needs_mesh_shaders = true;
|
||||
}
|
||||
crate::Binding::BuiltIn(
|
||||
crate::BuiltIn::MeshTaskSize
|
||||
| crate::BuiltIn::CullPrimitive
|
||||
| crate::BuiltIn::PointIndex
|
||||
| crate::BuiltIn::LineIndices
|
||||
| crate::BuiltIn::TriangleIndices
|
||||
| crate::BuiltIn::VertexCount
|
||||
| crate::BuiltIn::Vertices
|
||||
| crate::BuiltIn::PrimitiveCount
|
||||
| crate::BuiltIn::Primitives,
|
||||
) => {
|
||||
needs_mesh_shaders = true;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
@@ -275,6 +326,22 @@ impl<W: Write> Writer<W> {
|
||||
}
|
||||
}
|
||||
|
||||
if module
|
||||
.entry_points
|
||||
.iter()
|
||||
.any(|ep| matches!(ep.stage, ShaderStage::Mesh | ShaderStage::Task))
|
||||
{
|
||||
needs_mesh_shaders = true;
|
||||
}
|
||||
|
||||
if module
|
||||
.global_variables
|
||||
.iter()
|
||||
.any(|gv| gv.1.space == crate::AddressSpace::TaskPayload)
|
||||
{
|
||||
needs_mesh_shaders = true;
|
||||
}
|
||||
|
||||
// Write required declarations
|
||||
let mut any_written = false;
|
||||
if needs_f16 {
|
||||
@@ -289,6 +356,10 @@ impl<W: Write> Writer<W> {
|
||||
writeln!(self.out, "enable clip_distances;")?;
|
||||
any_written = true;
|
||||
}
|
||||
if needs_mesh_shaders {
|
||||
writeln!(self.out, "enable wgpu_mesh_shader;")?;
|
||||
any_written = true;
|
||||
}
|
||||
if any_written {
|
||||
// Empty line for readability
|
||||
writeln!(self.out)?;
|
||||
@@ -407,8 +478,11 @@ impl<W: Write> Writer<W> {
|
||||
ShaderStage::Vertex => "vertex",
|
||||
ShaderStage::Fragment => "fragment",
|
||||
ShaderStage::Compute => "compute",
|
||||
ShaderStage::Task | ShaderStage::Mesh => unreachable!(),
|
||||
ShaderStage::Task => "task",
|
||||
//Handled by another variant in the Attribute enum, so this code should never be hit.
|
||||
ShaderStage::Mesh => unreachable!(),
|
||||
};
|
||||
|
||||
write!(self.out, "@{stage_str} ")?;
|
||||
}
|
||||
Attribute::WorkGroupSize(size) => {
|
||||
@@ -437,6 +511,13 @@ impl<W: Write> Writer<W> {
|
||||
write!(self.out, "@interpolate({interpolation}) ")?;
|
||||
}
|
||||
}
|
||||
Attribute::MeshStage(ref name) => {
|
||||
write!(self.out, "@mesh({name}) ")?;
|
||||
}
|
||||
Attribute::TaskPayload(ref payload_name) => {
|
||||
write!(self.out, "@payload({payload_name}) ")?;
|
||||
}
|
||||
Attribute::PerPrimitive => write!(self.out, "@per_primitive ")?,
|
||||
};
|
||||
}
|
||||
Ok(())
|
||||
@@ -1867,21 +1948,33 @@ fn map_binding_to_attribute(binding: &crate::Binding) -> Vec<Attribute> {
|
||||
interpolation,
|
||||
sampling,
|
||||
blend_src: None,
|
||||
per_primitive: _,
|
||||
} => vec![
|
||||
Attribute::Location(location),
|
||||
Attribute::Interpolate(interpolation, sampling),
|
||||
],
|
||||
per_primitive,
|
||||
} => {
|
||||
let mut attrs = vec![
|
||||
Attribute::Location(location),
|
||||
Attribute::Interpolate(interpolation, sampling),
|
||||
];
|
||||
if per_primitive {
|
||||
attrs.push(Attribute::PerPrimitive);
|
||||
}
|
||||
attrs
|
||||
}
|
||||
crate::Binding::Location {
|
||||
location,
|
||||
interpolation,
|
||||
sampling,
|
||||
blend_src: Some(blend_src),
|
||||
per_primitive: _,
|
||||
} => vec![
|
||||
Attribute::Location(location),
|
||||
Attribute::BlendSrc(blend_src),
|
||||
Attribute::Interpolate(interpolation, sampling),
|
||||
],
|
||||
per_primitive,
|
||||
} => {
|
||||
let mut attrs = vec![
|
||||
Attribute::Location(location),
|
||||
Attribute::BlendSrc(blend_src),
|
||||
Attribute::Interpolate(interpolation, sampling),
|
||||
];
|
||||
if per_primitive {
|
||||
attrs.push(Attribute::PerPrimitive);
|
||||
}
|
||||
attrs
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -183,22 +183,23 @@ impl TryToWgsl for crate::BuiltIn {
|
||||
Bi::SubgroupInvocationId => "subgroup_invocation_id",
|
||||
|
||||
// Non-standard built-ins.
|
||||
Bi::MeshTaskSize => "mesh_task_size",
|
||||
Bi::TriangleIndices => "triangle_indices",
|
||||
Bi::LineIndices => "line_indices",
|
||||
Bi::PointIndex => "point_index",
|
||||
Bi::Vertices => "vertices",
|
||||
Bi::Primitives => "primitives",
|
||||
Bi::VertexCount => "vertex_count",
|
||||
Bi::PrimitiveCount => "primitive_count",
|
||||
Bi::CullPrimitive => "cull_primitive",
|
||||
|
||||
Bi::BaseInstance
|
||||
| Bi::BaseVertex
|
||||
| Bi::CullDistance
|
||||
| Bi::PointSize
|
||||
| Bi::DrawID
|
||||
| Bi::PointCoord
|
||||
| Bi::WorkGroupSize
|
||||
| Bi::CullPrimitive
|
||||
| Bi::TriangleIndices
|
||||
| Bi::LineIndices
|
||||
| Bi::MeshTaskSize
|
||||
| Bi::PointIndex
|
||||
| Bi::VertexCount
|
||||
| Bi::PrimitiveCount
|
||||
| Bi::Vertices
|
||||
| Bi::Primitives => return None,
|
||||
| Bi::WorkGroupSize => return None,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -362,7 +363,7 @@ pub const fn address_space_str(
|
||||
As::WorkGroup => "workgroup",
|
||||
As::Handle => return (None, None),
|
||||
As::Function => "function",
|
||||
As::TaskPayload => return (None, None),
|
||||
As::TaskPayload => "task_payload",
|
||||
}),
|
||||
None,
|
||||
)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
capabilities = "MESH_SHADER"
|
||||
targets = "SPIRV"
|
||||
targets = "WGSL | SPIRV"
|
||||
|
||||
[spv]
|
||||
version = [1, 4]
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
capabilities = "MESH_SHADER"
|
||||
targets = "SPIRV"
|
||||
targets = "WGSL | SPIRV"
|
||||
|
||||
[spv]
|
||||
version = [1, 4]
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
capabilities = "MESH_SHADER | MESH_SHADER_POINT_TOPOLOGY"
|
||||
targets = "SPIRV"
|
||||
targets = "WGSL | SPIRV"
|
||||
|
||||
[spv]
|
||||
version = [1, 4]
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
capabilities = "MESH_SHADER"
|
||||
targets = "SPIRV"
|
||||
targets = "WGSL | SPIRV"
|
||||
|
||||
[spv]
|
||||
version = [1, 4]
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
enable wgpu_mesh_shader;
|
||||
|
||||
struct TaskPayload {
|
||||
dummy: u32,
|
||||
}
|
||||
|
||||
struct VertexOutput {
|
||||
@builtin(position) position: vec4<f32>,
|
||||
}
|
||||
|
||||
struct PrimitiveOutput {
|
||||
@builtin(triangle_indices) indices: vec3<u32>,
|
||||
}
|
||||
|
||||
struct MeshOutput {
|
||||
@builtin(vertices) vertices: array<VertexOutput, 3>,
|
||||
@builtin(primitives) primitives: array<PrimitiveOutput, 1>,
|
||||
@builtin(vertex_count) vertex_count: u32,
|
||||
@builtin(primitive_count) primitive_count: u32,
|
||||
}
|
||||
|
||||
var<task_payload> taskPayload: TaskPayload;
|
||||
var<workgroup> mesh_output: MeshOutput;
|
||||
|
||||
@task @payload(taskPayload) @workgroup_size(1, 1, 1)
|
||||
fn ts_main() -> @builtin(mesh_task_size) vec3<u32> {
|
||||
return vec3<u32>(1u, 1u, 1u);
|
||||
}
|
||||
|
||||
@mesh(mesh_output) @workgroup_size(1, 1, 1) @payload(taskPayload)
|
||||
fn ms_main() {
|
||||
return;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
enable wgpu_mesh_shader;
|
||||
|
||||
struct TaskPayload {
|
||||
dummy: u32,
|
||||
}
|
||||
|
||||
struct VertexOutput {
|
||||
@builtin(position) position: vec4<f32>,
|
||||
}
|
||||
|
||||
struct PrimitiveOutput {
|
||||
@builtin(line_indices) indices: vec2<u32>,
|
||||
}
|
||||
|
||||
struct MeshOutput {
|
||||
@builtin(vertices) vertices: array<VertexOutput, 2>,
|
||||
@builtin(primitives) primitives: array<PrimitiveOutput, 1>,
|
||||
@builtin(vertex_count) vertex_count: u32,
|
||||
@builtin(primitive_count) primitive_count: u32,
|
||||
}
|
||||
|
||||
var<task_payload> taskPayload: TaskPayload;
|
||||
var<workgroup> mesh_output: MeshOutput;
|
||||
|
||||
@task @payload(taskPayload) @workgroup_size(1, 1, 1)
|
||||
fn ts_main() -> @builtin(mesh_task_size) vec3<u32> {
|
||||
return vec3<u32>(1u, 1u, 1u);
|
||||
}
|
||||
|
||||
@mesh(mesh_output) @workgroup_size(1, 1, 1) @payload(taskPayload)
|
||||
fn ms_main() {
|
||||
return;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
enable wgpu_mesh_shader;
|
||||
|
||||
struct TaskPayload {
|
||||
dummy: u32,
|
||||
}
|
||||
|
||||
struct VertexOutput {
|
||||
@builtin(position) position: vec4<f32>,
|
||||
}
|
||||
|
||||
struct PrimitiveOutput {
|
||||
@builtin(point_index) indices: u32,
|
||||
}
|
||||
|
||||
struct MeshOutput {
|
||||
@builtin(vertices) vertices: array<VertexOutput, 1>,
|
||||
@builtin(primitives) primitives: array<PrimitiveOutput, 1>,
|
||||
@builtin(vertex_count) vertex_count: u32,
|
||||
@builtin(primitive_count) primitive_count: u32,
|
||||
}
|
||||
|
||||
var<task_payload> taskPayload: TaskPayload;
|
||||
var<workgroup> mesh_output: MeshOutput;
|
||||
|
||||
@task @payload(taskPayload) @workgroup_size(1, 1, 1)
|
||||
fn ts_main() -> @builtin(mesh_task_size) vec3<u32> {
|
||||
return vec3<u32>(1u, 1u, 1u);
|
||||
}
|
||||
|
||||
@mesh(mesh_output) @workgroup_size(1, 1, 1) @payload(taskPayload)
|
||||
fn ms_main() {
|
||||
return;
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
enable wgpu_mesh_shader;
|
||||
|
||||
struct TaskPayload {
|
||||
colorMask: vec4<f32>,
|
||||
visible: bool,
|
||||
}
|
||||
|
||||
struct VertexOutput {
|
||||
@builtin(position) position: vec4<f32>,
|
||||
@location(0) color: vec4<f32>,
|
||||
}
|
||||
|
||||
struct PrimitiveOutput {
|
||||
@builtin(triangle_indices) indices: vec3<u32>,
|
||||
@builtin(cull_primitive) cull: bool,
|
||||
@location(1) @per_primitive colorMask: vec4<f32>,
|
||||
}
|
||||
|
||||
struct PrimitiveInput {
|
||||
@location(1) @per_primitive colorMask: vec4<f32>,
|
||||
}
|
||||
|
||||
struct MeshOutput {
|
||||
@builtin(vertices) vertices: array<VertexOutput, 3>,
|
||||
@builtin(primitives) primitives: array<PrimitiveOutput, 1>,
|
||||
@builtin(vertex_count) vertex_count: u32,
|
||||
@builtin(primitive_count) primitive_count: u32,
|
||||
}
|
||||
|
||||
var<task_payload> taskPayload: TaskPayload;
|
||||
var<workgroup> workgroupData: f32;
|
||||
var<workgroup> mesh_output: MeshOutput;
|
||||
|
||||
@task @payload(taskPayload) @workgroup_size(1, 1, 1)
|
||||
fn ts_main() -> @builtin(mesh_task_size) vec3<u32> {
|
||||
workgroupData = 1f;
|
||||
taskPayload.colorMask = vec4<f32>(1f, 1f, 0f, 1f);
|
||||
taskPayload.visible = true;
|
||||
return vec3<u32>(1u, 1u, 1u);
|
||||
}
|
||||
|
||||
@task @payload(taskPayload) @workgroup_size(2, 1, 1)
|
||||
fn ts_divergent(@builtin(local_invocation_index) thread_id: u32) -> @builtin(mesh_task_size) vec3<u32> {
|
||||
if (thread_id == 0u) {
|
||||
taskPayload.colorMask = vec4<f32>(1f, 1f, 0f, 1f);
|
||||
taskPayload.visible = true;
|
||||
return vec3<u32>(1u, 1u, 1u);
|
||||
}
|
||||
return vec3<u32>(2u, 2u, 2u);
|
||||
}
|
||||
|
||||
@mesh(mesh_output) @workgroup_size(1, 1, 1) @payload(taskPayload)
|
||||
fn ms_main() {
|
||||
mesh_output.vertex_count = 3u;
|
||||
mesh_output.primitive_count = 1u;
|
||||
workgroupData = 2f;
|
||||
mesh_output.vertices[0].position = vec4<f32>(0f, 1f, 0f, 1f);
|
||||
let _e23 = taskPayload.colorMask;
|
||||
mesh_output.vertices[0].color = (vec4<f32>(0f, 1f, 0f, 1f) * _e23);
|
||||
mesh_output.vertices[1].position = vec4<f32>(-1f, -1f, 0f, 1f);
|
||||
let _e45 = taskPayload.colorMask;
|
||||
mesh_output.vertices[1].color = (vec4<f32>(0f, 0f, 1f, 1f) * _e45);
|
||||
mesh_output.vertices[2].position = vec4<f32>(1f, -1f, 0f, 1f);
|
||||
let _e67 = taskPayload.colorMask;
|
||||
mesh_output.vertices[2].color = (vec4<f32>(1f, 0f, 0f, 1f) * _e67);
|
||||
mesh_output.primitives[0].indices = vec3<u32>(0u, 1u, 2u);
|
||||
let _e88 = taskPayload.visible;
|
||||
mesh_output.primitives[0].cull = !(_e88);
|
||||
mesh_output.primitives[0].colorMask = vec4<f32>(1f, 0f, 1f, 1f);
|
||||
return;
|
||||
}
|
||||
|
||||
@mesh(mesh_output) @workgroup_size(1, 1, 1)
|
||||
fn ms_no_ts() {
|
||||
mesh_output.vertex_count = 3u;
|
||||
mesh_output.primitive_count = 1u;
|
||||
workgroupData = 2f;
|
||||
mesh_output.vertices[0].position = vec4<f32>(0f, 1f, 0f, 1f);
|
||||
mesh_output.vertices[0].color = vec4<f32>(0f, 1f, 0f, 1f);
|
||||
mesh_output.vertices[1].position = vec4<f32>(-1f, -1f, 0f, 1f);
|
||||
mesh_output.vertices[1].color = vec4<f32>(0f, 0f, 1f, 1f);
|
||||
mesh_output.vertices[2].position = vec4<f32>(1f, -1f, 0f, 1f);
|
||||
mesh_output.vertices[2].color = vec4<f32>(1f, 0f, 0f, 1f);
|
||||
mesh_output.primitives[0].indices = vec3<u32>(0u, 1u, 2u);
|
||||
mesh_output.primitives[0].cull = false;
|
||||
mesh_output.primitives[0].colorMask = vec4<f32>(1f, 0f, 1f, 1f);
|
||||
return;
|
||||
}
|
||||
|
||||
@mesh(mesh_output) @workgroup_size(1, 1, 1)
|
||||
fn ms_divergent(@builtin(local_invocation_index) thread_id_1: u32) {
|
||||
if (thread_id_1 == 0u) {
|
||||
mesh_output.vertex_count = 3u;
|
||||
mesh_output.primitive_count = 1u;
|
||||
workgroupData = 2f;
|
||||
mesh_output.vertices[0].position = vec4<f32>(0f, 1f, 0f, 1f);
|
||||
mesh_output.vertices[0].color = vec4<f32>(0f, 1f, 0f, 1f);
|
||||
mesh_output.vertices[1].position = vec4<f32>(-1f, -1f, 0f, 1f);
|
||||
mesh_output.vertices[1].color = vec4<f32>(0f, 0f, 1f, 1f);
|
||||
mesh_output.vertices[2].position = vec4<f32>(1f, -1f, 0f, 1f);
|
||||
mesh_output.vertices[2].color = vec4<f32>(1f, 0f, 0f, 1f);
|
||||
mesh_output.primitives[0].indices = vec3<u32>(0u, 1u, 2u);
|
||||
mesh_output.primitives[0].cull = false;
|
||||
mesh_output.primitives[0].colorMask = vec4<f32>(1f, 0f, 1f, 1f);
|
||||
return;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@fragment
|
||||
fn fs_main(vertex: VertexOutput, primitive: PrimitiveInput) -> @location(0) vec4<f32> {
|
||||
return (vertex.color * primitive.colorMask);
|
||||
}
|
||||
Reference in new issue
Block a user