2020-10-14 18:25:55 +00:00
|
|
|
use crate::{Error, GPUVertex, MaskState};
|
|
|
|
use enum_map::{enum_map, EnumMap};
|
2020-04-28 14:10:45 +00:00
|
|
|
use wgpu::vertex_attr_array;
|
|
|
|
|
2020-04-28 14:34:07 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ShapePipeline {
|
2020-10-14 18:25:55 +00:00
|
|
|
pub mask_pipelines: EnumMap<MaskState, wgpu::RenderPipeline>,
|
2020-04-28 14:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Pipelines {
|
2020-10-17 21:39:24 +00:00
|
|
|
pub mesh_layout: wgpu::BindGroupLayout,
|
|
|
|
|
2020-10-17 19:05:16 +00:00
|
|
|
pub color_pipelines: ShapePipeline,
|
2020-10-17 21:39:24 +00:00
|
|
|
|
2020-10-17 19:05:16 +00:00
|
|
|
pub bitmap_pipelines: ShapePipeline,
|
|
|
|
pub bitmap_layout: wgpu::BindGroupLayout,
|
2020-10-17 21:39:24 +00:00
|
|
|
|
2020-10-17 19:05:16 +00:00
|
|
|
pub gradient_pipelines: ShapePipeline,
|
|
|
|
pub gradient_layout: wgpu::BindGroupLayout,
|
2020-04-28 14:34:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 20:48:17 +00:00
|
|
|
impl ShapePipeline {
|
2020-10-14 18:25:55 +00:00
|
|
|
pub fn pipeline_for(&self, mask_state: MaskState) -> &wgpu::RenderPipeline {
|
|
|
|
&self.mask_pipelines[mask_state]
|
2020-04-28 20:48:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 14:34:07 +00:00
|
|
|
impl Pipelines {
|
2020-10-15 22:55:19 +00:00
|
|
|
pub fn new(
|
|
|
|
device: &wgpu::Device,
|
|
|
|
msaa_sample_count: u32,
|
|
|
|
sampler_layout: &wgpu::BindGroupLayout,
|
2020-10-16 15:46:40 +00:00
|
|
|
globals_layout: &wgpu::BindGroupLayout,
|
2020-10-15 22:55:19 +00:00
|
|
|
) -> Result<Self, Error> {
|
2020-08-27 10:32:41 +00:00
|
|
|
let color_vs =
|
|
|
|
device.create_shader_module(wgpu::include_spirv!("../shaders/color.vert.spv"));
|
|
|
|
let color_fs =
|
|
|
|
device.create_shader_module(wgpu::include_spirv!("../shaders/color.frag.spv"));
|
|
|
|
let texture_vs =
|
|
|
|
device.create_shader_module(wgpu::include_spirv!("../shaders/texture.vert.spv"));
|
|
|
|
let gradient_fs =
|
|
|
|
device.create_shader_module(wgpu::include_spirv!("../shaders/gradient.frag.spv"));
|
|
|
|
let bitmap_fs =
|
|
|
|
device.create_shader_module(wgpu::include_spirv!("../shaders/bitmap.frag.spv"));
|
|
|
|
|
|
|
|
let vertex_buffers_description = [wgpu::VertexBufferDescriptor {
|
|
|
|
stride: std::mem::size_of::<GPUVertex>() as u64,
|
|
|
|
step_mode: wgpu::InputStepMode::Vertex,
|
|
|
|
attributes: &vertex_attr_array![
|
|
|
|
0 => Float2,
|
|
|
|
1 => Float4
|
|
|
|
],
|
|
|
|
}];
|
2020-04-28 14:34:07 +00:00
|
|
|
|
2020-10-17 21:39:24 +00:00
|
|
|
let mesh_bind_layout_label = create_debug_label!("Mesh bind group layout");
|
|
|
|
let mesh_bind_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
2020-10-17 19:05:16 +00:00
|
|
|
entries: &[
|
|
|
|
wgpu::BindGroupLayoutEntry {
|
|
|
|
binding: 0,
|
|
|
|
visibility: wgpu::ShaderStage::VERTEX,
|
|
|
|
ty: wgpu::BindingType::UniformBuffer {
|
|
|
|
dynamic: false,
|
|
|
|
min_binding_size: None,
|
|
|
|
},
|
|
|
|
count: None,
|
|
|
|
},
|
|
|
|
wgpu::BindGroupLayoutEntry {
|
|
|
|
binding: 1,
|
2020-10-17 21:39:24 +00:00
|
|
|
visibility: wgpu::ShaderStage::FRAGMENT,
|
2020-10-17 19:05:16 +00:00
|
|
|
ty: wgpu::BindingType::UniformBuffer {
|
|
|
|
dynamic: false,
|
|
|
|
min_binding_size: None,
|
|
|
|
},
|
|
|
|
count: None,
|
|
|
|
},
|
|
|
|
],
|
2020-10-17 21:39:24 +00:00
|
|
|
label: mesh_bind_layout_label.as_deref(),
|
2020-10-17 19:05:16 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
let color_pipelines = create_color_pipelines(
|
|
|
|
&device,
|
|
|
|
&color_vs,
|
|
|
|
&color_fs,
|
|
|
|
msaa_sample_count,
|
|
|
|
&vertex_buffers_description,
|
|
|
|
globals_layout,
|
2020-10-17 21:39:24 +00:00
|
|
|
&mesh_bind_layout,
|
2020-10-17 19:05:16 +00:00
|
|
|
);
|
|
|
|
|
2020-10-17 21:39:24 +00:00
|
|
|
let bitmap_bind_layout_label = create_debug_label!("Bitmap shape bind group layout");
|
2020-10-17 19:05:16 +00:00
|
|
|
let bitmap_bind_layout =
|
|
|
|
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
|
|
|
entries: &[
|
|
|
|
wgpu::BindGroupLayoutEntry {
|
|
|
|
binding: 0,
|
|
|
|
visibility: wgpu::ShaderStage::VERTEX,
|
|
|
|
ty: wgpu::BindingType::UniformBuffer {
|
|
|
|
dynamic: false,
|
|
|
|
min_binding_size: None,
|
|
|
|
},
|
|
|
|
count: None,
|
|
|
|
},
|
|
|
|
wgpu::BindGroupLayoutEntry {
|
|
|
|
binding: 1,
|
|
|
|
visibility: wgpu::ShaderStage::FRAGMENT,
|
|
|
|
ty: wgpu::BindingType::SampledTexture {
|
|
|
|
multisampled: false,
|
|
|
|
component_type: wgpu::TextureComponentType::Float,
|
|
|
|
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,
|
2020-10-17 21:39:24 +00:00
|
|
|
&mesh_bind_layout,
|
2020-10-17 19:05:16 +00:00
|
|
|
&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::UniformBuffer {
|
|
|
|
dynamic: false,
|
|
|
|
min_binding_size: None,
|
|
|
|
},
|
|
|
|
count: None,
|
|
|
|
},
|
|
|
|
wgpu::BindGroupLayoutEntry {
|
|
|
|
binding: 1,
|
|
|
|
visibility: wgpu::ShaderStage::FRAGMENT,
|
|
|
|
ty: wgpu::BindingType::StorageBuffer {
|
|
|
|
dynamic: false,
|
|
|
|
min_binding_size: None,
|
|
|
|
readonly: true,
|
|
|
|
},
|
|
|
|
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,
|
2020-10-17 21:39:24 +00:00
|
|
|
&mesh_bind_layout,
|
2020-10-17 19:05:16 +00:00
|
|
|
&gradient_bind_layout,
|
|
|
|
);
|
|
|
|
|
2020-04-28 14:34:07 +00:00
|
|
|
Ok(Self {
|
2020-10-17 21:39:24 +00:00
|
|
|
mesh_layout: mesh_bind_layout,
|
2020-10-17 19:05:16 +00:00
|
|
|
color_pipelines,
|
|
|
|
bitmap_pipelines,
|
|
|
|
bitmap_layout: bitmap_bind_layout,
|
|
|
|
gradient_pipelines,
|
|
|
|
gradient_layout: gradient_bind_layout,
|
2020-04-28 14:34:07 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 10:40:56 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2020-04-28 14:34:07 +00:00
|
|
|
fn create_pipeline_descriptor<'a>(
|
2020-08-27 10:40:56 +00:00
|
|
|
label: Option<&'a str>,
|
2020-04-28 14:10:45 +00:00
|
|
|
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>],
|
2020-05-01 02:56:07 +00:00
|
|
|
msaa_sample_count: u32,
|
2020-04-28 14:10:45 +00:00
|
|
|
) -> wgpu::RenderPipelineDescriptor<'a> {
|
|
|
|
wgpu::RenderPipelineDescriptor {
|
2020-08-27 10:40:56 +00:00
|
|
|
label,
|
2020-08-27 10:32:41 +00:00
|
|
|
layout: Some(&pipeline_layout),
|
2020-04-28 14:10:45 +00:00
|
|
|
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,
|
2020-10-13 22:29:24 +00:00
|
|
|
polygon_mode: Default::default(),
|
2020-08-27 10:32:41 +00:00
|
|
|
clamp_depth: false,
|
2020-04-28 14:10:45 +00:00
|
|
|
depth_bias: 0,
|
|
|
|
depth_bias_slope_scale: 0.0,
|
|
|
|
depth_bias_clamp: 0.0,
|
|
|
|
}),
|
|
|
|
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
|
|
|
|
color_states,
|
|
|
|
depth_stencil_state,
|
2020-05-01 02:56:07 +00:00
|
|
|
sample_count: msaa_sample_count,
|
2020-04-28 14:10:45 +00:00
|
|
|
sample_mask: !0,
|
|
|
|
alpha_to_coverage_enabled: false,
|
|
|
|
vertex_state: wgpu::VertexStateDescriptor {
|
|
|
|
index_format: wgpu::IndexFormat::Uint16,
|
2020-08-27 10:32:41 +00:00
|
|
|
vertex_buffers: vertex_buffers_description,
|
2020-04-28 14:10:45 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 20:48:17 +00:00
|
|
|
fn create_color_pipelines(
|
2020-04-28 14:10:45 +00:00
|
|
|
device: &wgpu::Device,
|
|
|
|
vertex_shader: &wgpu::ShaderModule,
|
|
|
|
fragment_shader: &wgpu::ShaderModule,
|
2020-05-01 02:56:07 +00:00
|
|
|
msaa_sample_count: u32,
|
2020-08-27 10:32:41 +00:00
|
|
|
vertex_buffers_description: &[wgpu::VertexBufferDescriptor<'_>],
|
2020-10-16 15:46:40 +00:00
|
|
|
globals_layout: &wgpu::BindGroupLayout,
|
2020-10-17 21:39:24 +00:00
|
|
|
mesh_bind_layout: &wgpu::BindGroupLayout,
|
2020-04-28 20:48:17 +00:00
|
|
|
) -> ShapePipeline {
|
2020-08-27 10:40:56 +00:00
|
|
|
let pipeline_layout_label = create_debug_label!("Color shape pipeline layout");
|
2020-04-28 14:10:45 +00:00
|
|
|
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
2020-08-27 10:40:56 +00:00
|
|
|
label: pipeline_layout_label.as_deref(),
|
2020-10-17 21:39:24 +00:00
|
|
|
bind_group_layouts: &[globals_layout, &mesh_bind_layout],
|
2020-08-27 10:32:41 +00:00
|
|
|
push_constant_ranges: &[],
|
2020-04-28 14:10:45 +00:00
|
|
|
});
|
|
|
|
|
2020-10-14 18:25:55 +00:00
|
|
|
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,
|
|
|
|
))
|
|
|
|
},
|
2020-04-28 20:48:17 +00:00
|
|
|
|
2020-10-14 18:25:55 +00:00
|
|
|
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
|
|
|
},
|
2020-10-14 18:25:55 +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
|
|
|
},
|
2020-10-14 18:25:55 +00:00
|
|
|
write_mask,
|
|
|
|
}],
|
|
|
|
vertex_buffers_description,
|
|
|
|
msaa_sample_count,
|
|
|
|
))
|
|
|
|
},
|
2020-04-28 14:10:45 +00:00
|
|
|
|
2020-10-14 18:25:55 +00:00
|
|
|
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
|
|
|
},
|
2020-10-14 18:25:55 +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
|
|
|
},
|
2020-10-14 18:25:55 +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,
|
|
|
|
))
|
|
|
|
},
|
|
|
|
};
|
2020-04-28 20:48:17 +00:00
|
|
|
|
2020-10-17 19:05:16 +00:00
|
|
|
ShapePipeline { mask_pipelines }
|
2020-04-28 14:10:45 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 19:05:16 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2020-04-28 14:34:07 +00:00
|
|
|
fn create_bitmap_pipeline(
|
2020-04-28 14:10:45 +00:00
|
|
|
device: &wgpu::Device,
|
|
|
|
vertex_shader: &wgpu::ShaderModule,
|
|
|
|
fragment_shader: &wgpu::ShaderModule,
|
2020-05-01 02:56:07 +00:00
|
|
|
msaa_sample_count: u32,
|
2020-08-27 10:32:41 +00:00
|
|
|
vertex_buffers_description: &[wgpu::VertexBufferDescriptor<'_>],
|
2020-10-15 22:55:19 +00:00
|
|
|
sampler_layout: &wgpu::BindGroupLayout,
|
2020-10-16 15:46:40 +00:00
|
|
|
globals_layout: &wgpu::BindGroupLayout,
|
2020-10-17 21:39:24 +00:00
|
|
|
mesh_bind_layout: &wgpu::BindGroupLayout,
|
2020-10-17 19:05:16 +00:00
|
|
|
bitmap_bind_layout: &wgpu::BindGroupLayout,
|
2020-04-28 20:48:17 +00:00
|
|
|
) -> ShapePipeline {
|
2020-08-27 10:40:56 +00:00
|
|
|
let pipeline_layout_label = create_debug_label!("Bitmap shape pipeline layout");
|
2020-04-28 14:10:45 +00:00
|
|
|
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
2020-08-27 10:40:56 +00:00
|
|
|
label: pipeline_layout_label.as_deref(),
|
2020-10-17 21:39:24 +00:00
|
|
|
bind_group_layouts: &[
|
|
|
|
globals_layout,
|
|
|
|
mesh_bind_layout,
|
|
|
|
bitmap_bind_layout,
|
|
|
|
sampler_layout,
|
|
|
|
],
|
2020-08-27 10:32:41 +00:00
|
|
|
push_constant_ranges: &[],
|
2020-04-28 14:10:45 +00:00
|
|
|
});
|
|
|
|
|
2020-10-14 18:25:55 +00:00
|
|
|
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 {
|
2020-10-15 22:28:17 +00:00
|
|
|
src_factor: wgpu::BlendFactor::One,
|
2020-10-14 18:25:55 +00:00
|
|
|
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,
|
|
|
|
))
|
|
|
|
},
|
2020-04-28 14:10:45 +00:00
|
|
|
|
2020-10-14 18:25:55 +00:00
|
|
|
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
|
|
|
},
|
2020-10-14 18:25:55 +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
|
|
|
},
|
2020-10-14 18:25:55 +00:00
|
|
|
write_mask,
|
|
|
|
}],
|
|
|
|
vertex_buffers_description,
|
|
|
|
msaa_sample_count,
|
|
|
|
))
|
|
|
|
},
|
2020-04-28 20:48:17 +00:00
|
|
|
|
2020-10-14 18:25:55 +00:00
|
|
|
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 {
|
2020-10-15 22:28:17 +00:00
|
|
|
src_factor: wgpu::BlendFactor::One,
|
2020-10-14 18:25:55 +00:00
|
|
|
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
|
|
|
|
operation: wgpu::BlendOperation::Add,
|
2020-08-27 10:32:41 +00:00
|
|
|
},
|
2020-10-14 18:25:55 +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
|
|
|
},
|
2020-10-14 18:25:55 +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,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
2020-04-28 20:48:17 +00:00
|
|
|
|
2020-10-17 19:05:16 +00:00
|
|
|
ShapePipeline { mask_pipelines }
|
2020-04-28 14:10:45 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 21:39:24 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2020-04-28 14:34:07 +00:00
|
|
|
fn create_gradient_pipeline(
|
2020-04-28 14:10:45 +00:00
|
|
|
device: &wgpu::Device,
|
|
|
|
vertex_shader: &wgpu::ShaderModule,
|
|
|
|
fragment_shader: &wgpu::ShaderModule,
|
2020-05-01 02:56:07 +00:00
|
|
|
msaa_sample_count: u32,
|
2020-08-27 10:32:41 +00:00
|
|
|
vertex_buffers_description: &[wgpu::VertexBufferDescriptor<'_>],
|
2020-10-16 15:46:40 +00:00
|
|
|
globals_layout: &wgpu::BindGroupLayout,
|
2020-10-17 21:39:24 +00:00
|
|
|
mesh_bind_layout: &wgpu::BindGroupLayout,
|
2020-10-17 19:05:16 +00:00
|
|
|
gradient_bind_layout: &wgpu::BindGroupLayout,
|
2020-04-28 20:48:17 +00:00
|
|
|
) -> ShapePipeline {
|
2020-08-27 10:40:56 +00:00
|
|
|
let pipeline_layout_label = create_debug_label!("Gradient shape pipeline layout");
|
2020-04-28 14:10:45 +00:00
|
|
|
let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
|
2020-08-27 10:40:56 +00:00
|
|
|
label: pipeline_layout_label.as_deref(),
|
2020-10-17 21:39:24 +00:00
|
|
|
bind_group_layouts: &[globals_layout, mesh_bind_layout, gradient_bind_layout],
|
2020-08-27 10:32:41 +00:00
|
|
|
push_constant_ranges: &[],
|
2020-04-28 14:10:45 +00:00
|
|
|
});
|
|
|
|
|
2020-10-14 18:25:55 +00:00
|
|
|
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,
|
|
|
|
))
|
|
|
|
},
|
2020-04-28 20:48:17 +00:00
|
|
|
|
2020-10-14 18:25:55 +00:00
|
|
|
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
|
|
|
},
|
2020-10-14 18:25:55 +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
|
|
|
},
|
2020-10-14 18:25:55 +00:00
|
|
|
write_mask,
|
|
|
|
}],
|
|
|
|
vertex_buffers_description,
|
|
|
|
msaa_sample_count,
|
|
|
|
))
|
|
|
|
},
|
2020-04-28 14:10:45 +00:00
|
|
|
|
2020-10-14 18:25:55 +00:00
|
|
|
|
|
|
|
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
|
|
|
},
|
2020-10-14 18:25:55 +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
|
|
|
},
|
2020-10-14 18:25:55 +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,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
2020-04-28 20:48:17 +00:00
|
|
|
|
2020-10-17 19:05:16 +00:00
|
|
|
ShapePipeline { mask_pipelines }
|
2020-04-28 14:10:45 +00:00
|
|
|
}
|
2020-10-14 18:25:55 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
}
|