use crate::content::transient::{Glow, GlowKind};
pub(crate) trait Attribute {
const FORMAT: wgpu::VertexFormat;
}
impl Attribute for u32 {
const FORMAT: wgpu::VertexFormat = wgpu::VertexFormat::Uint32;
}
impl Attribute for f32 {
const FORMAT: wgpu::VertexFormat = wgpu::VertexFormat::Float32;
}
impl Attribute for [f32; 2] {
const FORMAT: wgpu::VertexFormat = wgpu::VertexFormat::Float32x2;
}
impl Attribute for [f32; 3] {
const FORMAT: wgpu::VertexFormat = wgpu::VertexFormat::Float32x3;
}
impl Attribute for [f32; 4] {
const FORMAT: wgpu::VertexFormat = wgpu::VertexFormat::Float32x4;
}
pub(crate) const fn format<S, T: Attribute>(_field: fn(&S) -> &T) -> wgpu::VertexFormat {
T::FORMAT
}
pub(crate) struct VertexLayout {
pub buffer: wgpu::VertexBufferLayout<'static>,
#[cfg(test)]
pub fields: &'static [&'static str],
}
macro_rules! vertex_layout {
($struct:ty, [$($field:ident),* $(,)?]) => {
$crate::shading::vertex::vertex_layout!($struct, Vertex, 0, [$($field),*])
};
($struct:ty, $step:ident, $first:expr, [$($field:ident),* $(,)?]) => {
$crate::shading::vertex::VertexLayout {
buffer: wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<$struct>() as u64,
step_mode: wgpu::VertexStepMode::$step,
attributes: &{
let mut attributes = [$(wgpu::VertexAttribute {
format: $crate::shading::vertex::format(|vertex: &$struct| &vertex.$field),
offset: std::mem::offset_of!($struct, $field) as u64,
shader_location: 0,
}),*];
let mut index = 0;
while index < attributes.len() {
attributes[index].shader_location = $first + index as u32;
index += 1;
}
attributes
},
},
#[cfg(test)]
fields: &[$(stringify!($field)),*],
}
};
}
pub(crate) use vertex_layout;
#[repr(C)]
#[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)]
pub(crate) struct CasterVertex {
pub position: [f32; 3],
}
pub(crate) const CASTER_LAYOUT: VertexLayout = vertex_layout!(CasterVertex, [position]);
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, bytemuck::Pod, bytemuck::Zeroable)]
pub(crate) struct DrawInstance {
pub object: u32,
pub mesh: u32,
pub first_index: u32,
pub triangles: u32,
pub first_vertex: u32,
}
pub(crate) const NO_POSITIONS: u32 = u32::MAX;
pub(crate) const DRAW_INSTANCE_LAYOUT: VertexLayout = vertex_layout!(
DrawInstance,
Instance,
1,
[object, mesh, first_index, triangles, first_vertex]
);
pub(crate) const DRAW_INSTANCE_SLOT: u32 = 0;
pub(crate) const CASTER_SLOT: u32 = 1;
pub(crate) const GEOMETRY_BUFFERS: [Option<wgpu::VertexBufferLayout<'static>>; 2] = [
Some(DRAW_INSTANCE_LAYOUT.buffer),
Some(CASTER_LAYOUT.buffer),
];
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
pub(crate) struct GlowVertex {
pub position: [f32; 3],
pub color: [f32; 4],
pub kind: u32,
pub soft_distance: f32,
pub uv: [f32; 2],
pub taper: f32,
pub ripple_frequency: [f32; 2],
pub ripple_amplitude: f32,
pub other: [f32; 3],
pub offset: f32,
}
pub(crate) const GLOW_UNIFORM: u32 = 0;
pub(crate) const GLOW_TAPERED: u32 = 1;
pub(crate) const GLOW_LINE: u32 = 2;
impl GlowVertex {
pub fn new(glow: &Glow) -> Self {
let vertex = Self {
position: glow.position,
color: glow.color,
kind: GLOW_UNIFORM,
soft_distance: glow.soft_distance,
..bytemuck::Zeroable::zeroed()
};
match glow.kind {
GlowKind::Uniform => vertex,
GlowKind::Tapered { uv, profile } => Self {
kind: GLOW_TAPERED,
uv,
taper: profile.taper.max(0.),
ripple_frequency: profile.ripple_frequency,
ripple_amplitude: profile.ripple_amplitude.clamp(0., 0.5),
..vertex
},
GlowKind::Line { other, offset } => Self {
kind: GLOW_LINE,
other,
offset,
..vertex
},
}
}
}
#[cfg(test)]
pub(crate) fn constants() -> [crate::shading::layout_tests::Constant; 5] {
use crate::shading::layout_tests::Constant;
use naga::Literal::U32;
[
Constant::new("glow", "GLOW_TAPERED", U32(GLOW_TAPERED)),
Constant::new("glow", "GLOW_LINE", U32(GLOW_LINE)),
Constant::new(
"caster",
"CASTER_VERTEX_WORDS",
U32((std::mem::size_of::<CasterVertex>() / 4) as u32),
),
Constant::new("caster", "NO_POSITIONS", U32(NO_POSITIONS)),
Constant::new("cull", "NO_POSITIONS", U32(NO_POSITIONS)),
]
}