---
source: wgsl_to_wgpu/src/lib.rs
---
pub struct OverrideConstants {
pub b1: Option<bool>,
pub b2: Option<bool>,
pub b3: bool,
pub f1: Option<f32>,
pub f2: f32,
pub f3: Option<f64>,
pub f4: f64,
pub i1: Option<i32>,
pub i2: i32,
pub i3: Option<i32>,
pub a: Option<f32>,
pub b: Option<f32>,
}
impl OverrideConstants {
pub fn constants(&self) -> Vec<(&'static str, f64)> {
let mut entries = vec![
("b3", if self.b3 { 1.0 } else { 0.0 }), ("f2", self.f2 as f64), ("f4", self
.f4 as f64), ("i2", self.i2 as f64)
];
if let Some(value) = self.b1 {
entries.push(("b1", if value { 1.0 } else { 0.0 }));
}
if let Some(value) = self.b2 {
entries.push(("b2", if value { 1.0 } else { 0.0 }));
}
if let Some(value) = self.f1 {
entries.push(("f1", value as f64));
}
if let Some(value) = self.f3 {
entries.push(("f3", value as f64));
}
if let Some(value) = self.i1 {
entries.push(("i1", value as f64));
}
if let Some(value) = self.i3 {
entries.push(("i3", value as f64));
}
if let Some(value) = self.a {
entries.push(("0", value as f64));
}
if let Some(value) = self.b {
entries.push(("35", value as f64));
}
entries
}
}
pub const ENTRY_MAIN: &str = "main";
#[derive(Debug)]
pub struct FragmentEntry<const N: usize> {
pub entry_point: &'static str,
pub targets: [Option<wgpu::ColorTargetState>; N],
pub constants: Vec<(&'static str, f64)>,
}
pub fn fragment_state<'a, const N: usize>(
module: &'a wgpu::ShaderModule,
entry: &'a FragmentEntry<N>,
) -> wgpu::FragmentState<'a> {
wgpu::FragmentState {
module,
entry_point: Some(entry.entry_point),
targets: &entry.targets,
compilation_options: wgpu::PipelineCompilationOptions {
constants: &entry.constants,
..Default::default()
},
}
}
pub fn main_entry(
targets: [Option<wgpu::ColorTargetState>; 0],
overrides: &OverrideConstants,
) -> FragmentEntry<0> {
FragmentEntry {
entry_point: ENTRY_MAIN,
targets,
constants: overrides.constants(),
}
}
pub const SOURCE: &str = include_str!("shader.wgsl");
pub fn create_shader_module(device: &wgpu::Device) -> wgpu::ShaderModule {
let source = std::borrow::Cow::Borrowed(SOURCE);
device
.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Wgsl(source),
})
}
pub fn create_pipeline_layout(device: &wgpu::Device) -> wgpu::PipelineLayout {
device
.create_pipeline_layout(
&wgpu::PipelineLayoutDescriptor {
label: None,
bind_group_layouts: &[],
immediate_size: 0,
},
)
}