pub struct OverrideConstants {
pub dt: f32,
}
impl OverrideConstants {
pub fn constants(&self) -> Vec<(&'static str, f64)> {
let entries = vec![("dt", self.dt as f64)];
entries
}
}
pub mod bind_groups {
#[derive(Debug, Clone)]
pub struct BindGroup0(wgpu::BindGroup);
#[derive(Debug)]
pub struct BindGroupLayout0<'a> {
pub uniforms: wgpu::BufferBinding<'a>,
}
const LAYOUT_DESCRIPTOR0: wgpu::BindGroupLayoutDescriptor = wgpu::BindGroupLayoutDescriptor {
label: Some("LayoutDescriptor0"),
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage {
read_only: false,
},
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
],
};
impl BindGroup0 {
pub fn get_bind_group_layout(device: &wgpu::Device) -> wgpu::BindGroupLayout {
device.create_bind_group_layout(&LAYOUT_DESCRIPTOR0)
}
pub fn from_bindings(device: &wgpu::Device, bindings: BindGroupLayout0) -> Self {
let bind_group_layout = device.create_bind_group_layout(&LAYOUT_DESCRIPTOR0);
let bind_group = device
.create_bind_group(
&wgpu::BindGroupDescriptor {
layout: &bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::Buffer(bindings.uniforms),
},
],
label: Some("BindGroup0"),
},
);
Self(bind_group)
}
pub fn set<P: super::SetBindGroup>(&self, pass: &mut P) {
pass.set_bind_group(0, &self.0, &[]);
}
pub fn inner(&self) -> &wgpu::BindGroup {
&self.0
}
}
#[derive(Debug, Copy, Clone)]
pub struct BindGroups<'a> {
pub bind_group0: &'a BindGroup0,
}
impl BindGroups<'_> {
pub fn set<P: super::SetBindGroup>(&self, pass: &mut P) {
self.bind_group0.set(pass);
}
}
}
pub fn set_bind_groups<P: SetBindGroup>(
pass: &mut P,
bind_group0: &bind_groups::BindGroup0,
) {
bind_group0.set(pass);
}
pub mod compute {
pub const MAIN_WORKGROUP_SIZE: [u32; 3] = [1, 1, 1];
pub fn create_main_pipeline(
device: &wgpu::Device,
overrides: &super::OverrideConstants,
) -> wgpu::ComputePipeline {
let module = super::create_shader_module(device);
let layout = super::create_pipeline_layout(device);
device
.create_compute_pipeline(
&wgpu::ComputePipelineDescriptor {
label: Some("Compute Pipeline main"),
layout: Some(&layout),
module: &module,
entry_point: Some("main"),
compilation_options: wgpu::PipelineCompilationOptions {
constants: &overrides.constants(),
..Default::default()
},
cache: Default::default(),
},
)
}
}
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: &[
Some(&bind_groups::BindGroup0::get_bind_group_layout(device)),
],
immediate_size: 0,
},
)
}
pub const ENTRY_MAIN: &str = "main";
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Uniforms {
pub color_rgb: [f32; 3],
}
pub trait SetBindGroup {
fn set_bind_group(
&mut self,
index: u32,
bind_group: &wgpu::BindGroup,
offsets: &[wgpu::DynamicOffset],
);
}
impl SetBindGroup for wgpu::ComputePass<'_> {
fn set_bind_group(
&mut self,
index: u32,
bind_group: &wgpu::BindGroup,
offsets: &[wgpu::DynamicOffset],
) {
self.set_bind_group(index, bind_group, offsets);
}
}
impl SetBindGroup for wgpu::RenderPass<'_> {
fn set_bind_group(
&mut self,
index: u32,
bind_group: &wgpu::BindGroup,
offsets: &[wgpu::DynamicOffset],
) {
self.set_bind_group(index, bind_group, offsets);
}
}
impl SetBindGroup for wgpu::RenderBundleEncoder<'_> {
fn set_bind_group(
&mut self,
index: u32,
bind_group: &wgpu::BindGroup,
offsets: &[wgpu::DynamicOffset],
) {
self.set_bind_group(index, bind_group, offsets);
}
}