use oxiui_core::paint::BlendMode;
use crate::gpu::buffer::Vertex;
use crate::gpu::device::TARGET_FORMAT;
pub fn blend_state_for_mode(mode: BlendMode) -> wgpu::BlendState {
match mode {
BlendMode::Normal => wgpu::BlendState::ALPHA_BLENDING,
BlendMode::Multiply => wgpu::BlendState {
color: wgpu::BlendComponent {
src_factor: wgpu::BlendFactor::Dst,
dst_factor: wgpu::BlendFactor::Zero,
operation: wgpu::BlendOperation::Add,
},
alpha: wgpu::BlendComponent {
src_factor: wgpu::BlendFactor::One,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
},
},
BlendMode::Screen => wgpu::BlendState {
color: wgpu::BlendComponent {
src_factor: wgpu::BlendFactor::One,
dst_factor: wgpu::BlendFactor::OneMinusSrc,
operation: wgpu::BlendOperation::Add,
},
alpha: wgpu::BlendComponent {
src_factor: wgpu::BlendFactor::One,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
},
},
BlendMode::Overlay | BlendMode::Darken | BlendMode::Lighten => {
wgpu::BlendState::ALPHA_BLENDING
}
BlendMode::Copy => wgpu::BlendState::REPLACE,
BlendMode::Destination => wgpu::BlendState {
color: wgpu::BlendComponent {
src_factor: wgpu::BlendFactor::Zero,
dst_factor: wgpu::BlendFactor::One,
operation: wgpu::BlendOperation::Add,
},
alpha: wgpu::BlendComponent {
src_factor: wgpu::BlendFactor::Zero,
dst_factor: wgpu::BlendFactor::One,
operation: wgpu::BlendOperation::Add,
},
},
#[allow(unreachable_patterns)]
_ => wgpu::BlendState::ALPHA_BLENDING,
}
}
pub struct BlendPipelineSet {
pub globals_layout: wgpu::BindGroupLayout,
pub normal: wgpu::RenderPipeline,
pub multiply: wgpu::RenderPipeline,
pub screen: wgpu::RenderPipeline,
pub copy: wgpu::RenderPipeline,
pub destination: wgpu::RenderPipeline,
}
impl BlendPipelineSet {
pub fn new(device: &wgpu::Device, sample_count: u32) -> Self {
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
label: Some("oxiui-render-wgpu blend-mode solid.wgsl"),
source: wgpu::ShaderSource::Wgsl(include_str!("../shaders/solid.wgsl").into()),
});
let globals_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
label: Some("oxiui-render-wgpu blend globals layout"),
entries: &[wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStages::VERTEX,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
}],
});
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("oxiui-render-wgpu blend pipeline layout"),
bind_group_layouts: &[Some(&globals_layout)],
immediate_size: 0,
});
let build = |label: &'static str, blend: wgpu::BlendState| {
let attrs = [
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x2,
offset: 0,
shader_location: 0,
},
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x4,
offset: 8,
shader_location: 1,
},
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x2,
offset: 24,
shader_location: 2,
},
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x2,
offset: 32,
shader_location: 3,
},
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32,
offset: 40,
shader_location: 4,
},
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32,
offset: 44,
shader_location: 5,
},
wgpu::VertexAttribute {
format: wgpu::VertexFormat::Float32x2,
offset: 48,
shader_location: 6,
},
];
let vertex_layout = wgpu::VertexBufferLayout {
array_stride: core::mem::size_of::<Vertex>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: &attrs,
};
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some(label),
layout: Some(&pipeline_layout),
vertex: wgpu::VertexState {
module: &shader,
entry_point: Some("vs_main"),
buffers: &[vertex_layout],
compilation_options: wgpu::PipelineCompilationOptions::default(),
},
fragment: Some(wgpu::FragmentState {
module: &shader,
entry_point: Some("fs_main"),
targets: &[Some(wgpu::ColorTargetState {
format: TARGET_FORMAT,
blend: Some(blend),
write_mask: wgpu::ColorWrites::ALL,
})],
compilation_options: wgpu::PipelineCompilationOptions::default(),
}),
primitive: wgpu::PrimitiveState {
topology: wgpu::PrimitiveTopology::TriangleList,
strip_index_format: None,
front_face: wgpu::FrontFace::Ccw,
cull_mode: None,
unclipped_depth: false,
polygon_mode: wgpu::PolygonMode::Fill,
conservative: false,
},
depth_stencil: None,
multisample: wgpu::MultisampleState {
count: sample_count,
mask: !0,
alpha_to_coverage_enabled: false,
},
multiview_mask: None,
cache: None,
})
};
let normal = build(
"oxiui-render-wgpu blend normal",
blend_state_for_mode(BlendMode::Normal),
);
let multiply = build(
"oxiui-render-wgpu blend multiply",
blend_state_for_mode(BlendMode::Multiply),
);
let screen = build(
"oxiui-render-wgpu blend screen",
blend_state_for_mode(BlendMode::Screen),
);
let copy = build(
"oxiui-render-wgpu blend copy",
blend_state_for_mode(BlendMode::Copy),
);
let destination = build(
"oxiui-render-wgpu blend destination",
blend_state_for_mode(BlendMode::Destination),
);
Self {
globals_layout,
normal,
multiply,
screen,
copy,
destination,
}
}
pub fn pipeline_for(&self, mode: BlendMode) -> &wgpu::RenderPipeline {
match mode {
BlendMode::Normal | BlendMode::Overlay | BlendMode::Darken | BlendMode::Lighten => {
&self.normal
}
BlendMode::Multiply => &self.multiply,
BlendMode::Screen => &self.screen,
BlendMode::Copy => &self.copy,
BlendMode::Destination => &self.destination,
#[allow(unreachable_patterns)]
_ => &self.normal,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn blend_state_for_normal_is_alpha_blending() {
let bs = blend_state_for_mode(BlendMode::Normal);
assert_eq!(bs, wgpu::BlendState::ALPHA_BLENDING);
}
#[test]
fn blend_state_for_copy_is_replace() {
let bs = blend_state_for_mode(BlendMode::Copy);
assert_eq!(bs, wgpu::BlendState::REPLACE);
}
#[test]
fn overlay_falls_back_to_normal() {
let overlay = blend_state_for_mode(BlendMode::Overlay);
let normal = blend_state_for_mode(BlendMode::Normal);
assert_eq!(overlay, normal);
}
fn try_device() -> Option<(wgpu::Device, wgpu::Queue)> {
let instance = wgpu::Instance::default();
let adapter = pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::default(),
force_fallback_adapter: false,
compatible_surface: None,
}))
.ok()?;
pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor {
label: Some("blend test device"),
required_features: wgpu::Features::empty(),
required_limits: wgpu::Limits::downlevel_defaults(),
memory_hints: wgpu::MemoryHints::Performance,
experimental_features: wgpu::ExperimentalFeatures::disabled(),
trace: wgpu::Trace::Off,
}))
.ok()
}
#[test]
fn blend_pipeline_set_compiles() {
let Some((device, _)) = try_device() else {
return;
};
let set = BlendPipelineSet::new(&device, 1);
let _normal = set.pipeline_for(BlendMode::Normal);
let _multiply = set.pipeline_for(BlendMode::Multiply);
let _screen = set.pipeline_for(BlendMode::Screen);
let _copy = set.pipeline_for(BlendMode::Copy);
let _ = set.pipeline_for(BlendMode::Overlay);
}
}