onedrop_renderer/pipeline_helpers.rs
1//! Small helpers shared by the renderer's overlay pipelines (waveform,
2//! custom waves, custom shapes, borders, motion vectors).
3//!
4//! Kept deliberately minimal — each pipeline still owns its own pipeline
5//! descriptor end-to-end so the bindings, vertex layout, and topology
6//! stay obvious at the call site. These helpers just remove the two
7//! pieces of literal duplication that contributed nothing to readability:
8//! the boilerplate around [`wgpu::ShaderModuleDescriptor`] and the
9//! additive-vs-alpha blend-state switch.
10
11/// Compile an inline WGSL source string into a [`wgpu::ShaderModule`]
12/// with the supplied label.
13///
14/// The source is taken as `'static` to encourage call sites to pass
15/// `include_str!("../shaders/<name>.wgsl")` (the dominant pattern) rather
16/// than building a string at runtime.
17pub(crate) fn load_wgsl(
18 device: &wgpu::Device,
19 label: &str,
20 source: &'static str,
21) -> wgpu::ShaderModule {
22 device.create_shader_module(wgpu::ShaderModuleDescriptor {
23 label: Some(label),
24 source: wgpu::ShaderSource::Wgsl(source.into()),
25 })
26}
27
28/// Standard MD2-style blend state for an overlay pipeline.
29///
30/// - `additive = false` → straight alpha blending (preset opacity composes
31/// over the warp output).
32/// - `additive = true` → MD2's additive mode where `src.rgb * src.alpha`
33/// is added on top of the destination. Used by `b_additive_waves`,
34/// `b_additive` (custom waves), and the additive variant of the
35/// custom-shape pipeline.
36pub(crate) fn blend_state_for(additive: bool) -> wgpu::BlendState {
37 if additive {
38 wgpu::BlendState {
39 color: wgpu::BlendComponent {
40 src_factor: wgpu::BlendFactor::SrcAlpha,
41 dst_factor: wgpu::BlendFactor::One,
42 operation: wgpu::BlendOperation::Add,
43 },
44 alpha: wgpu::BlendComponent {
45 src_factor: wgpu::BlendFactor::One,
46 dst_factor: wgpu::BlendFactor::One,
47 operation: wgpu::BlendOperation::Add,
48 },
49 }
50 } else {
51 wgpu::BlendState::ALPHA_BLENDING
52 }
53}