use crate::{Composer, Error, Result};
const BATCH: u32 = 32;
#[derive(Debug)]
pub struct MeshShaderRenderer {
pipeline: wgpu::RenderPipeline,
layout: wgpu::BindGroupLayout,
}
impl MeshShaderRenderer {
#[must_use]
pub fn is_supported(device: &wgpu::Device) -> bool {
device
.features()
.contains(wgpu::Features::EXPERIMENTAL_MESH_SHADER)
&& device.adapter_info().backend == wgpu::Backend::Vulkan
}
pub fn new(
device: &wgpu::Device,
format: wgpu::TextureFormat,
depth: Option<wgpu::DepthStencilState>,
samples: u32,
) -> Result<Self> {
if !Self::is_supported(device) {
return Err(Error::MeshShadersUnavailable);
}
let source = Composer::with_builtins().compose("mesh_render", &[])?;
let module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("isomesh mesh render"),
source: wgpu::ShaderSource::Wgsl(source.into()),
});
let uniform = |binding: u32| wgpu::BindGroupLayoutEntry {
binding,
visibility: wgpu::ShaderStages::MESH,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
};
let storage = |binding: u32| wgpu::BindGroupLayoutEntry {
binding,
visibility: wgpu::ShaderStages::MESH,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: true },
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
};
let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("isomesh mesh render bindings"),
entries: &[uniform(0), uniform(1), storage(2), storage(3)],
});
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("isomesh mesh render layout"),
bind_group_layouts: &[Some(&layout)],
immediate_size: 0,
});
let pipeline = device.create_mesh_pipeline(&wgpu::MeshPipelineDescriptor {
label: Some("isomesh mesh render"),
layout: Some(&pipeline_layout),
task: None,
mesh: wgpu::MeshState {
module: &module,
entry_point: Some("draw_mesh"),
compilation_options: wgpu::PipelineCompilationOptions::default(),
},
primitive: wgpu::PrimitiveState {
front_face: wgpu::FrontFace::Ccw,
cull_mode: Some(wgpu::Face::Back),
..wgpu::PrimitiveState::default()
},
depth_stencil: depth,
multisample: wgpu::MultisampleState {
count: samples,
..wgpu::MultisampleState::default()
},
fragment: Some(wgpu::FragmentState {
module: &module,
entry_point: Some("shade"),
compilation_options: wgpu::PipelineCompilationOptions::default(),
targets: &[Some(wgpu::ColorTargetState {
format,
blend: None,
write_mask: wgpu::ColorWrites::ALL,
})],
}),
multiview: None,
cache: None,
});
Ok(Self { pipeline, layout })
}
#[must_use]
pub const fn bind_group_layout(&self) -> &wgpu::BindGroupLayout {
&self.layout
}
#[must_use]
pub fn bind_group(
&self,
device: &wgpu::Device,
camera: &wgpu::Buffer,
draw: &wgpu::Buffer,
positions: &wgpu::Buffer,
normals: &wgpu::Buffer,
) -> wgpu::BindGroup {
fn entry(binding: u32, buffer: &wgpu::Buffer) -> wgpu::BindGroupEntry<'_> {
wgpu::BindGroupEntry {
binding,
resource: buffer.as_entire_binding(),
}
}
device.create_bind_group(&wgpu::BindGroupDescriptor {
label: Some("isomesh mesh render"),
layout: &self.layout,
entries: &[
entry(0, camera),
entry(1, draw),
entry(2, positions),
entry(3, normals),
],
})
}
pub fn draw_indirect(
&self,
pass: &mut wgpu::RenderPass<'_>,
bind_group: &wgpu::BindGroup,
indirect: &wgpu::Buffer,
) {
pass.set_pipeline(&self.pipeline);
pass.set_bind_group(0, bind_group, &[]);
pass.draw_mesh_tasks_indirect(indirect, 0);
}
pub fn draw(
&self,
pass: &mut wgpu::RenderPass<'_>,
bind_group: &wgpu::BindGroup,
triangles: u32,
) {
pass.set_pipeline(&self.pipeline);
pass.set_bind_group(0, bind_group, &[]);
pass.draw_mesh_tasks(triangles.div_ceil(BATCH), 1, 1);
}
#[must_use]
pub fn camera_bytes(view_proj: [[f32; 4]; 4]) -> [u8; 64] {
let mut out = [0u8; 64];
for (column, chunk) in view_proj.iter().zip(out.as_chunks_mut::<16>().0) {
for (value, slot) in column.iter().zip(chunk.as_chunks_mut::<4>().0) {
slot.copy_from_slice(&value.to_le_bytes());
}
}
out
}
#[must_use]
pub fn draw_bytes(triangles: u32) -> [u8; 16] {
let mut out = [0u8; 16];
out[..4].copy_from_slice(&triangles.to_le_bytes());
out
}
}
#[cfg(test)]
mod tests;