use crate::config::RasterConfig;
use crate::RenderError;
pub struct RasterPipelines {
pub preprocess: wgpu::ComputePipeline,
pub prefix_sum: wgpu::ComputePipeline,
pub prefix_sum_add: wgpu::ComputePipeline,
pub tile_assign: wgpu::ComputePipeline,
pub tile_ranges: wgpu::ComputePipeline,
pub rasterize_fwd: wgpu::ComputePipeline,
pub rasterize_bwd: wgpu::ComputePipeline,
pub atomic_to_f32: wgpu::ComputePipeline,
pub preprocess_bwd: wgpu::ComputePipeline,
pub flame_binding_bwd: wgpu::ComputePipeline,
pub preprocess_bgl: wgpu::BindGroupLayout,
pub prefix_sum_bgl: wgpu::BindGroupLayout,
pub prefix_sum_add_bgl: wgpu::BindGroupLayout,
pub tile_assign_bgl: wgpu::BindGroupLayout,
pub tile_ranges_bgl: wgpu::BindGroupLayout,
pub rasterize_fwd_bgl: wgpu::BindGroupLayout,
pub rasterize_bwd_bgl: wgpu::BindGroupLayout,
pub atomic_to_f32_bgl: wgpu::BindGroupLayout,
pub preprocess_bwd_bgl: wgpu::BindGroupLayout,
pub flame_binding_bwd_bgl: wgpu::BindGroupLayout,
}
fn get_preprocess_shader_source(config: &RasterConfig) -> &'static str {
if config.sh_optimization && config.use_sh_variants {
match config.sh_degree {
0 => include_str!("../shaders/preprocess_sh0.wgsl"),
1 => include_str!("../shaders/preprocess_sh1.wgsl"),
2 => include_str!("../shaders/preprocess_sh2.wgsl"),
3 => include_str!("../shaders/preprocess_sh3.wgsl"),
_ => include_str!("../shaders/preprocess.wgsl"),
}
} else {
include_str!("../shaders/preprocess.wgsl")
}
}
fn get_preprocess_shader_label(config: &RasterConfig) -> &'static str {
if config.sh_optimization && config.use_sh_variants {
match config.sh_degree {
0 => "preprocess_sh0",
1 => "preprocess_sh1",
2 => "preprocess_sh2",
3 => "preprocess_sh3",
_ => "preprocess",
}
} else {
"preprocess"
}
}
impl RasterPipelines {
pub fn new(device: &wgpu::Device, config: &RasterConfig) -> Result<Self, RenderError> {
let preprocess_bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("preprocess_bgl"),
entries: &[
uniform_entry(0), storage_ro_entry(1), storage_ro_entry(2), storage_ro_entry(3), storage_ro_entry(4), storage_rw_entry(5), storage_rw_entry(6), storage_rw_entry(7), storage_rw_entry(8), storage_rw_entry(9), storage_rw_entry(10), storage_ro_entry(11), storage_rw_entry(12), storage_rw_entry(13), ],
});
let preprocess_source = get_preprocess_shader_source(config);
let preprocess_label = get_preprocess_shader_label(config);
let preprocess = compile_pipeline(
device,
preprocess_label,
preprocess_source,
"preprocess",
&preprocess_bgl,
);
let prefix_sum_bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("prefix_sum_bgl"),
entries: &[
storage_ro_entry(0), storage_rw_entry(1), uniform_entry(2), storage_rw_entry(3), ],
});
let prefix_sum = compile_pipeline(
device,
"prefix_sum",
include_str!("../shaders/prefix_sum.wgsl"),
"prefix_sum",
&prefix_sum_bgl,
);
let prefix_sum_add_bgl =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("prefix_sum_add_bgl"),
entries: &[
storage_rw_entry(0), storage_ro_entry(1), uniform_entry(2), ],
});
let prefix_sum_add = compile_pipeline(
device,
"prefix_sum_add",
include_str!("../shaders/prefix_sum_add.wgsl"),
"prefix_sum_add",
&prefix_sum_add_bgl,
);
let tile_assign_bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("tile_assign_bgl"),
entries: &[
uniform_entry(0), storage_ro_entry(1), storage_ro_entry(2), storage_ro_entry(3), storage_ro_entry(4), storage_rw_entry(5), storage_rw_entry(6), ],
});
let tile_assign = compile_pipeline(
device,
"tile_assign",
include_str!("../shaders/tile_assign.wgsl"),
"tile_assign",
&tile_assign_bgl,
);
let tile_ranges_bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("tile_ranges_bgl"),
entries: &[
storage_ro_entry(0), storage_rw_entry(1), uniform_entry(2), ],
});
let tile_ranges = compile_pipeline(
device,
"tile_ranges",
include_str!("../shaders/tile_ranges.wgsl"),
"tile_ranges_kernel",
&tile_ranges_bgl,
);
let rasterize_fwd_bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("rasterize_fwd_bgl"),
entries: &[
uniform_entry(0), storage_ro_entry(1), storage_ro_entry(2), storage_ro_entry(3), storage_ro_entry(4), storage_ro_entry(5), storage_ro_entry(6), storage_ro_entry(7), storage_rw_entry(8), storage_rw_entry(9), storage_rw_entry(10), storage_rw_entry(11), storage_ro_entry(12), storage_rw_entry(13), ],
});
let rasterize_fwd = compile_pipeline(
device,
"rasterize_fwd",
include_str!("../shaders/rasterize_fwd.wgsl"),
"rasterize_forward",
&rasterize_fwd_bgl,
);
let rasterize_bwd_bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("rasterize_bwd_bgl"),
entries: &[
uniform_entry(0), storage_ro_entry(1), storage_ro_entry(2), storage_ro_entry(3), storage_ro_entry(4), storage_ro_entry(5), storage_ro_entry(6), storage_ro_entry(7), storage_ro_entry(8), storage_ro_entry(9), storage_ro_entry(10), storage_rw_entry(11), storage_rw_entry(12), storage_rw_entry(13), storage_rw_entry(14), ],
});
let rasterize_bwd = compile_pipeline(
device,
"rasterize_bwd",
include_str!("../shaders/rasterize_bwd.wgsl"),
"rasterize_backward",
&rasterize_bwd_bgl,
);
let atomic_to_f32_bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("atomic_to_f32_bgl"),
entries: &[
uniform_entry(0), storage_rw_entry(1), storage_rw_entry(2), ],
});
let atomic_to_f32 = compile_pipeline(
device,
"atomic_to_f32",
include_str!("../shaders/atomic_to_f32.wgsl"),
"atomic_to_f32",
&atomic_to_f32_bgl,
);
let preprocess_bwd_bgl =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("preprocess_bwd_bgl"),
entries: &[
uniform_entry(0), storage_ro_entry(1), storage_ro_entry(2), storage_ro_entry(3), storage_ro_entry(4), storage_ro_entry(5), storage_ro_entry(6), storage_ro_entry(7), storage_ro_entry(8), storage_ro_entry(9), storage_rw_entry(10), storage_rw_entry(11), storage_rw_entry(12), storage_rw_entry(13), ],
});
let preprocess_bwd = compile_pipeline(
device,
"preprocess_bwd",
include_str!("../shaders/preprocess_bwd.wgsl"),
"preprocess_backward",
&preprocess_bwd_bgl,
);
let flame_binding_bwd_bgl =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("flame_binding_bwd_bgl"),
entries: &[
uniform_entry(0), storage_ro_entry(1), storage_ro_entry(2), storage_ro_entry(3), storage_rw_entry(4), ],
});
let flame_binding_bwd = compile_pipeline(
device,
"flame_binding_bwd",
include_str!("../shaders/flame_binding_bwd.wgsl"),
"flame_binding_backward",
&flame_binding_bwd_bgl,
);
Ok(Self {
preprocess,
prefix_sum,
prefix_sum_add,
tile_assign,
tile_ranges,
rasterize_fwd,
rasterize_bwd,
atomic_to_f32,
preprocess_bwd,
flame_binding_bwd,
preprocess_bgl,
prefix_sum_bgl,
prefix_sum_add_bgl,
tile_assign_bgl,
tile_ranges_bgl,
rasterize_fwd_bgl,
rasterize_bwd_bgl,
atomic_to_f32_bgl,
preprocess_bwd_bgl,
flame_binding_bwd_bgl,
})
}
}
fn compile_pipeline(
device: &wgpu::Device,
label: &str,
source: &str,
entry_point: &str,
bgl: &wgpu::BindGroupLayout,
) -> wgpu::ComputePipeline {
let module = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some(label),
source: wgpu::ShaderSource::Wgsl(source.into()),
});
let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some(&format!("{label}_layout")),
bind_group_layouts: &[bgl],
immediate_size: 0,
});
device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
label: Some(&format!("{label}_pipeline")),
layout: Some(&layout),
module: &module,
entry_point: Some(entry_point),
compilation_options: Default::default(),
cache: None,
})
}
fn uniform_entry(binding: u32) -> wgpu::BindGroupLayoutEntry {
wgpu::BindGroupLayoutEntry {
binding,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
}
}
fn storage_ro_entry(binding: u32) -> wgpu::BindGroupLayoutEntry {
wgpu::BindGroupLayoutEntry {
binding,
visibility: wgpu::ShaderStages::COMPUTE,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: true },
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
}
}
fn storage_rw_entry(binding: u32) -> wgpu::BindGroupLayoutEntry {
wgpu::BindGroupLayoutEntry {
binding,
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,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_preprocess_shader_label() {
let mut config = RasterConfig {
sh_optimization: true,
use_sh_variants: true,
..RasterConfig::default()
};
config.sh_degree = 0;
assert_eq!(get_preprocess_shader_label(&config), "preprocess_sh0");
config.sh_degree = 1;
assert_eq!(get_preprocess_shader_label(&config), "preprocess_sh1");
config.sh_degree = 2;
assert_eq!(get_preprocess_shader_label(&config), "preprocess_sh2");
config.sh_degree = 3;
assert_eq!(get_preprocess_shader_label(&config), "preprocess_sh3");
config.use_sh_variants = false;
config.sh_degree = 0;
assert_eq!(get_preprocess_shader_label(&config), "preprocess");
}
}