ruffle/render/wgpu/src/pipelines.rs

724 lines
30 KiB
Rust
Raw Normal View History

use crate::{Error, GpuVertex, MaskState};
use enum_map::{enum_map, EnumMap};
use wgpu::vertex_attr_array;
#[derive(Debug)]
pub struct ShapePipeline {
pub mask_pipelines: EnumMap<MaskState, wgpu::RenderPipeline>,
}
#[derive(Debug)]
pub struct Pipelines {
pub color_pipelines: ShapePipeline,
pub bitmap_pipelines: ShapePipeline,
pub bitmap_layout: wgpu::BindGroupLayout,
pub gradient_pipelines: ShapePipeline,
pub gradient_layout: wgpu::BindGroupLayout,
}
impl ShapePipeline {
pub fn pipeline_for(&self, mask_state: MaskState) -> &wgpu::RenderPipeline {
&self.mask_pipelines[mask_state]
}
}
impl Pipelines {
pub fn new(
device: &wgpu::Device,
msaa_sample_count: u32,
sampler_layout: &wgpu::BindGroupLayout,
globals_layout: &wgpu::BindGroupLayout,
) -> Result<Self, Error> {
2020-08-27 10:32:41 +00:00
let color_vs =
2020-12-27 06:33:38 +00:00
device.create_shader_module(&wgpu::include_spirv!("../shaders/color.vert.spv"));
2020-08-27 10:32:41 +00:00
let color_fs =
2020-12-27 06:33:38 +00:00
device.create_shader_module(&wgpu::include_spirv!("../shaders/color.frag.spv"));
2020-08-27 10:32:41 +00:00
let texture_vs =
2020-12-27 06:33:38 +00:00
device.create_shader_module(&wgpu::include_spirv!("../shaders/texture.vert.spv"));
2020-08-27 10:32:41 +00:00
let gradient_fs =
2020-12-27 06:33:38 +00:00
device.create_shader_module(&wgpu::include_spirv!("../shaders/gradient.frag.spv"));
2020-08-27 10:32:41 +00:00
let bitmap_fs =
2020-12-27 06:33:38 +00:00
device.create_shader_module(&wgpu::include_spirv!("../shaders/bitmap.frag.spv"));
2020-08-27 10:32:41 +00:00
let vertex_buffers_description = [wgpu::VertexBufferDescriptor {
stride: std::mem::size_of::<GpuVertex>() as u64,
2020-08-27 10:32:41 +00:00
step_mode: wgpu::InputStepMode::Vertex,
attributes: &vertex_attr_array![
0 => Float2,
1 => Float4
],
}];
let color_pipelines = create_color_pipelines(
&device,
&color_vs,
&color_fs,
msaa_sample_count,
&vertex_buffers_description,
globals_layout,
);
let bitmap_bind_layout_label = create_debug_label!("Bitmap shape bind group layout");
let bitmap_bind_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStage::VERTEX,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::Texture {
multisampled: false,
sample_type: wgpu::TextureSampleType::Float { filterable: true },
view_dimension: wgpu::TextureViewDimension::D2,
},
count: None,
},
],
label: bitmap_bind_layout_label.as_deref(),
});
let bitmap_pipelines = create_bitmap_pipeline(
&device,
&texture_vs,
&bitmap_fs,
msaa_sample_count,
&vertex_buffers_description,
sampler_layout,
globals_layout,
&bitmap_bind_layout,
);
let gradient_bind_layout_label = create_debug_label!("Gradient shape bind group");
let gradient_bind_layout =
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
entries: &[
wgpu::BindGroupLayoutEntry {
binding: 0,
visibility: wgpu::ShaderStage::VERTEX,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
wgpu::BindGroupLayoutEntry {
binding: 1,
visibility: wgpu::ShaderStage::FRAGMENT,
ty: wgpu::BindingType::Buffer {
ty: wgpu::BufferBindingType::Storage { read_only: true },
has_dynamic_offset: false,
min_binding_size: None,
},
count: None,
},
],
label: gradient_bind_layout_label.as_deref(),
});
let gradient_pipelines = create_gradient_pipeline(
&device,
&texture_vs,
&gradient_fs,
msaa_sample_count,
&vertex_buffers_description,
globals_layout,
&gradient_bind_layout,
);
Ok(Self {
color_pipelines,
bitmap_pipelines,
bitmap_layout: bitmap_bind_layout,
gradient_pipelines,
gradient_layout: gradient_bind_layout,
})
}
}
#[allow(clippy::too_many_arguments)]
fn create_pipeline_descriptor<'a>(
label: Option<&'a str>,
vertex_shader: &'a wgpu::ShaderModule,
fragment_shader: &'a wgpu::ShaderModule,
pipeline_layout: &'a wgpu::PipelineLayout,
depth_stencil_state: Option<wgpu::DepthStencilStateDescriptor>,
color_states: &'a [wgpu::ColorStateDescriptor],
2020-08-27 10:32:41 +00:00
vertex_buffers_description: &'a [wgpu::VertexBufferDescriptor<'a>],
msaa_sample_count: u32,
) -> wgpu::RenderPipelineDescriptor<'a> {
wgpu::RenderPipelineDescriptor {
label,
2020-08-27 10:32:41 +00:00
layout: Some(&pipeline_layout),
vertex_stage: wgpu::ProgrammableStageDescriptor {
module: &vertex_shader,
entry_point: "main",
},
fragment_stage: Some(wgpu::ProgrammableStageDescriptor {
module: &fragment_shader,
entry_point: "main",
}),
rasterization_state: Some(wgpu::RasterizationStateDescriptor {
front_face: wgpu::FrontFace::Ccw,
cull_mode: wgpu::CullMode::None,
polygon_mode: Default::default(),
2020-08-27 10:32:41 +00:00
clamp_depth: false,
depth_bias: 0,
depth_bias_slope_scale: 0.0,
depth_bias_clamp: 0.0,
}),
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
color_states,
depth_stencil_state,
sample_count: msaa_sample_count,
sample_mask: !0,
alpha_to_coverage_enabled: false,
vertex_state: wgpu::VertexStateDescriptor {
2021-01-04 23:45:40 +00:00
index_format: Some(wgpu::IndexFormat::Uint32),
2020-08-27 10:32:41 +00:00
vertex_buffers: vertex_buffers_description,
},
}
}
fn create_color_pipelines(
device: &wgpu::Device,
vertex_shader: &wgpu::ShaderModule,
fragment_shader: &wgpu::ShaderModule,
msaa_sample_count: u32,
2020-08-27 10:32:41 +00:00
vertex_buffers_description: &[wgpu::VertexBufferDescriptor<'_>],
globals_layout: &wgpu::BindGroupLayout,
) -> ShapePipeline {
let transforms_size = std::mem::size_of::<crate::Transforms>() as u32;
let colors_size = std::mem::size_of::<crate::ColorAdjustments>() as u32;
let pipeline_layout_label = create_debug_label!("Color shape pipeline layout");
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: pipeline_layout_label.as_deref(),
bind_group_layouts: &[globals_layout],
push_constant_ranges: &[
wgpu::PushConstantRange {
stages: wgpu::ShaderStage::VERTEX,
range: 0..transforms_size,
},
wgpu::PushConstantRange {
stages: wgpu::ShaderStage::FRAGMENT,
range: transforms_size..transforms_size + colors_size,
},
],
});
let mask_pipelines = enum_map! {
MaskState::NoMask => {
let (stencil, write_mask) = mask_render_state(MaskState::NoMask);
device.create_render_pipeline(&create_pipeline_descriptor(
create_debug_label!("Color pipeline no mask").as_deref(),
vertex_shader,
fragment_shader,
&pipeline_layout,
Some(wgpu::DepthStencilStateDescriptor {
format: wgpu::TextureFormat::Depth24PlusStencil8,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Always,
stencil,
}),
&[wgpu::ColorStateDescriptor {
format: wgpu::TextureFormat::Bgra8Unorm,
color_blend: wgpu::BlendDescriptor {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
},
alpha_blend: wgpu::BlendDescriptor {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
},
write_mask,
}],
vertex_buffers_description,
msaa_sample_count,
))
},
MaskState::DrawMaskStencil => {
let (stencil, write_mask) = mask_render_state(MaskState::DrawMaskStencil);
device.create_render_pipeline(&create_pipeline_descriptor(
create_debug_label!("Color pipeline draw mask stencil").as_deref(),
vertex_shader,
fragment_shader,
&pipeline_layout,
Some(wgpu::DepthStencilStateDescriptor {
format: wgpu::TextureFormat::Depth24PlusStencil8,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Always,
stencil,
}),
&[wgpu::ColorStateDescriptor {
format: wgpu::TextureFormat::Bgra8Unorm,
color_blend: wgpu::BlendDescriptor {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
2020-08-27 10:32:41 +00:00
},
alpha_blend: wgpu::BlendDescriptor {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
2020-08-27 10:32:41 +00:00
},
write_mask,
}],
vertex_buffers_description,
msaa_sample_count,
))
},
MaskState::DrawMaskedContent => {
let (stencil, write_mask) = mask_render_state(MaskState::DrawMaskedContent);
device.create_render_pipeline(&create_pipeline_descriptor(
create_debug_label!("Color pipeline draw masked content").as_deref(),
vertex_shader,
fragment_shader,
&pipeline_layout,
Some(wgpu::DepthStencilStateDescriptor {
format: wgpu::TextureFormat::Depth24PlusStencil8,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Always,
stencil,
}),
&[wgpu::ColorStateDescriptor {
format: wgpu::TextureFormat::Bgra8Unorm,
color_blend: wgpu::BlendDescriptor {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
2020-08-27 10:32:41 +00:00
},
alpha_blend: wgpu::BlendDescriptor {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
2020-08-27 10:32:41 +00:00
},
write_mask,
}],
vertex_buffers_description,
msaa_sample_count,
))
},
MaskState::ClearMaskStencil => {
let (stencil, write_mask) = mask_render_state(MaskState::ClearMaskStencil);
device.create_render_pipeline(&create_pipeline_descriptor(
create_debug_label!("Color pipeline clear mask stencil").as_deref(),
vertex_shader,
fragment_shader,
&pipeline_layout,
Some(wgpu::DepthStencilStateDescriptor {
format: wgpu::TextureFormat::Depth24PlusStencil8,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Always,
stencil,
}),
&[wgpu::ColorStateDescriptor {
format: wgpu::TextureFormat::Bgra8Unorm,
color_blend: wgpu::BlendDescriptor {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
},
alpha_blend: wgpu::BlendDescriptor {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
},
write_mask,
}],
vertex_buffers_description,
msaa_sample_count,
))
},
};
ShapePipeline { mask_pipelines }
}
#[allow(clippy::too_many_arguments)]
fn create_bitmap_pipeline(
device: &wgpu::Device,
vertex_shader: &wgpu::ShaderModule,
fragment_shader: &wgpu::ShaderModule,
msaa_sample_count: u32,
2020-08-27 10:32:41 +00:00
vertex_buffers_description: &[wgpu::VertexBufferDescriptor<'_>],
sampler_layout: &wgpu::BindGroupLayout,
globals_layout: &wgpu::BindGroupLayout,
bitmap_bind_layout: &wgpu::BindGroupLayout,
) -> ShapePipeline {
let pipeline_layout_label = create_debug_label!("Bitmap shape pipeline layout");
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: pipeline_layout_label.as_deref(),
bind_group_layouts: &[globals_layout, bitmap_bind_layout, sampler_layout],
push_constant_ranges: &[
wgpu::PushConstantRange {
stages: wgpu::ShaderStage::VERTEX,
range: 0..64,
},
wgpu::PushConstantRange {
stages: wgpu::ShaderStage::FRAGMENT,
range: 64..96,
},
],
});
let mask_pipelines = enum_map! {
MaskState::NoMask => {
let (stencil, write_mask) = mask_render_state(MaskState::NoMask);
device.create_render_pipeline(&create_pipeline_descriptor(
create_debug_label!("Bitmap pipeline no mask").as_deref(),
vertex_shader,
fragment_shader,
&pipeline_layout,
Some(wgpu::DepthStencilStateDescriptor {
format: wgpu::TextureFormat::Depth24PlusStencil8,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Always,
stencil,
}),
&[wgpu::ColorStateDescriptor {
format: wgpu::TextureFormat::Bgra8Unorm,
color_blend: wgpu::BlendDescriptor {
src_factor: wgpu::BlendFactor::One,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
},
alpha_blend: wgpu::BlendDescriptor {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
},
write_mask,
}],
vertex_buffers_description,
msaa_sample_count,
))
},
MaskState::DrawMaskStencil => {
let (stencil, write_mask) = mask_render_state(MaskState::DrawMaskStencil);
device.create_render_pipeline(&create_pipeline_descriptor(
create_debug_label!("Bitmap pipeline draw mask stencil").as_deref(),
vertex_shader,
fragment_shader,
&pipeline_layout,
Some(wgpu::DepthStencilStateDescriptor {
format: wgpu::TextureFormat::Depth24PlusStencil8,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Always,
stencil,
}),
&[wgpu::ColorStateDescriptor {
format: wgpu::TextureFormat::Bgra8Unorm,
color_blend: wgpu::BlendDescriptor {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
2020-08-27 10:32:41 +00:00
},
alpha_blend: wgpu::BlendDescriptor {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
2020-08-27 10:32:41 +00:00
},
write_mask,
}],
vertex_buffers_description,
msaa_sample_count,
))
},
MaskState::DrawMaskedContent => {
let (stencil, write_mask) = mask_render_state(MaskState::DrawMaskedContent);
device.create_render_pipeline(&create_pipeline_descriptor(
create_debug_label!("Bitmap pipeline draw masked content").as_deref(),
vertex_shader,
fragment_shader,
&pipeline_layout,
Some(wgpu::DepthStencilStateDescriptor {
format: wgpu::TextureFormat::Depth24PlusStencil8,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Equal,
stencil,
}),
&[wgpu::ColorStateDescriptor {
format: wgpu::TextureFormat::Bgra8Unorm,
color_blend: wgpu::BlendDescriptor {
src_factor: wgpu::BlendFactor::One,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
2020-08-27 10:32:41 +00:00
},
alpha_blend: wgpu::BlendDescriptor {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
2020-08-27 10:32:41 +00:00
},
write_mask,
}],
vertex_buffers_description,
msaa_sample_count,
))
},
MaskState::ClearMaskStencil => {
let (stencil, write_mask) = mask_render_state(MaskState::ClearMaskStencil);
device.create_render_pipeline(&create_pipeline_descriptor(
create_debug_label!("Bitmap pipeline clear mask stencil").as_deref(),
vertex_shader,
fragment_shader,
&pipeline_layout,
Some(wgpu::DepthStencilStateDescriptor {
format: wgpu::TextureFormat::Depth24PlusStencil8,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Always,
stencil,
}),
&[wgpu::ColorStateDescriptor {
format: wgpu::TextureFormat::Bgra8Unorm,
color_blend: wgpu::BlendDescriptor {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
},
alpha_blend: wgpu::BlendDescriptor {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
},
write_mask,
}],
vertex_buffers_description,
msaa_sample_count,
))
}
};
ShapePipeline { mask_pipelines }
}
#[allow(clippy::too_many_arguments)]
fn create_gradient_pipeline(
device: &wgpu::Device,
vertex_shader: &wgpu::ShaderModule,
fragment_shader: &wgpu::ShaderModule,
msaa_sample_count: u32,
2020-08-27 10:32:41 +00:00
vertex_buffers_description: &[wgpu::VertexBufferDescriptor<'_>],
globals_layout: &wgpu::BindGroupLayout,
gradient_bind_layout: &wgpu::BindGroupLayout,
) -> ShapePipeline {
let pipeline_layout_label = create_debug_label!("Gradient shape pipeline layout");
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: pipeline_layout_label.as_deref(),
bind_group_layouts: &[globals_layout, gradient_bind_layout],
push_constant_ranges: &[
wgpu::PushConstantRange {
stages: wgpu::ShaderStage::VERTEX,
range: 0..64,
},
wgpu::PushConstantRange {
stages: wgpu::ShaderStage::FRAGMENT,
range: 64..96,
},
],
});
let mask_pipelines = enum_map! {
MaskState::NoMask => {
let (stencil, write_mask) = mask_render_state(MaskState::NoMask);
device.create_render_pipeline(&create_pipeline_descriptor(
create_debug_label!("Gradient pipeline no mask").as_deref(),
vertex_shader,
fragment_shader,
&pipeline_layout,
Some(wgpu::DepthStencilStateDescriptor {
format: wgpu::TextureFormat::Depth24PlusStencil8,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Always,
stencil,
}),
&[wgpu::ColorStateDescriptor {
format: wgpu::TextureFormat::Bgra8Unorm,
color_blend: wgpu::BlendDescriptor {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
},
alpha_blend: wgpu::BlendDescriptor {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
},
write_mask,
}],
vertex_buffers_description,
msaa_sample_count,
))
},
MaskState::DrawMaskStencil => {
let (stencil, write_mask) = mask_render_state(MaskState::DrawMaskStencil);
device.create_render_pipeline(&create_pipeline_descriptor(
create_debug_label!("Gradient pipeline draw mask stencil").as_deref(),
vertex_shader,
fragment_shader,
&pipeline_layout,
Some(wgpu::DepthStencilStateDescriptor {
format: wgpu::TextureFormat::Depth24PlusStencil8,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Always,
stencil,
}),
&[wgpu::ColorStateDescriptor {
format: wgpu::TextureFormat::Bgra8Unorm,
color_blend: wgpu::BlendDescriptor {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
2020-08-27 10:32:41 +00:00
},
alpha_blend: wgpu::BlendDescriptor {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
2020-08-27 10:32:41 +00:00
},
write_mask,
}],
vertex_buffers_description,
msaa_sample_count,
))
},
MaskState::DrawMaskedContent => {
let (stencil, write_mask) = mask_render_state(MaskState::DrawMaskedContent);
device.create_render_pipeline(&create_pipeline_descriptor(
create_debug_label!("Gradient pipeline draw masked content").as_deref(),
vertex_shader,
fragment_shader,
&pipeline_layout,
Some(wgpu::DepthStencilStateDescriptor {
format: wgpu::TextureFormat::Depth24PlusStencil8,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Equal,
stencil,
}),
&[wgpu::ColorStateDescriptor {
format: wgpu::TextureFormat::Bgra8Unorm,
color_blend: wgpu::BlendDescriptor {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
2020-08-27 10:32:41 +00:00
},
alpha_blend: wgpu::BlendDescriptor {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
2020-08-27 10:32:41 +00:00
},
write_mask,
}],
vertex_buffers_description,
msaa_sample_count,
))
},
MaskState::ClearMaskStencil => {
let (stencil, write_mask) = mask_render_state(MaskState::ClearMaskStencil);
device.create_render_pipeline(&create_pipeline_descriptor(
create_debug_label!("Gradient pipeline clear mask stencil").as_deref(),
vertex_shader,
fragment_shader,
&pipeline_layout,
Some(wgpu::DepthStencilStateDescriptor {
format: wgpu::TextureFormat::Depth24PlusStencil8,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Always,
stencil,
}),
&[wgpu::ColorStateDescriptor {
format: wgpu::TextureFormat::Bgra8Unorm,
color_blend: wgpu::BlendDescriptor {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
},
alpha_blend: wgpu::BlendDescriptor {
src_factor: wgpu::BlendFactor::SrcAlpha,
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
operation: wgpu::BlendOperation::Add,
},
write_mask,
}],
vertex_buffers_description,
msaa_sample_count,
))
}
};
ShapePipeline { mask_pipelines }
}
fn mask_render_state(state: MaskState) -> (wgpu::StencilStateDescriptor, wgpu::ColorWrite) {
let (stencil_state, color_write) = match state {
MaskState::NoMask => (
wgpu::StencilStateFaceDescriptor {
compare: wgpu::CompareFunction::Always,
fail_op: wgpu::StencilOperation::Keep,
depth_fail_op: wgpu::StencilOperation::Keep,
pass_op: wgpu::StencilOperation::Keep,
},
wgpu::ColorWrite::ALL,
),
MaskState::DrawMaskStencil => (
wgpu::StencilStateFaceDescriptor {
compare: wgpu::CompareFunction::Equal,
fail_op: wgpu::StencilOperation::Keep,
depth_fail_op: wgpu::StencilOperation::Keep,
pass_op: wgpu::StencilOperation::IncrementClamp,
},
wgpu::ColorWrite::empty(),
),
MaskState::DrawMaskedContent => (
wgpu::StencilStateFaceDescriptor {
compare: wgpu::CompareFunction::Equal,
fail_op: wgpu::StencilOperation::Keep,
depth_fail_op: wgpu::StencilOperation::Keep,
pass_op: wgpu::StencilOperation::Keep,
},
wgpu::ColorWrite::ALL,
),
MaskState::ClearMaskStencil => (
wgpu::StencilStateFaceDescriptor {
compare: wgpu::CompareFunction::Equal,
fail_op: wgpu::StencilOperation::Keep,
depth_fail_op: wgpu::StencilOperation::Keep,
pass_op: wgpu::StencilOperation::DecrementClamp,
},
wgpu::ColorWrite::empty(),
),
};
(
wgpu::StencilStateDescriptor {
front: stencil_state.clone(),
back: stencil_state,
read_mask: 0xff,
write_mask: 0xff,
},
color_write,
)
}