use wgpu::{
include_wgsl, AddressMode, BindGroupDescriptor, BindGroupEntry, BindGroupLayout,
BindGroupLayoutDescriptor, BindGroupLayoutEntry, BindingResource, BindingType, BlendState,
ColorTargetState, ColorWrites, CommandEncoder, Device, FilterMode, FragmentState,
MultisampleState, Operations, PrimitiveState, RenderPassColorAttachment, RenderPassDescriptor,
RenderPipeline, Sampler, SamplerBindingType, SamplerDescriptor, ShaderStages, TextureFormat,
TextureSampleType, TextureView, TextureViewDimension, VertexState,
};
pub struct BlitPipeline {
pub source_bind_group_layout: BindGroupLayout,
pub sampler: Sampler,
pub pipeline: RenderPipeline,
}
impl BlitPipeline {
pub fn new(device: &Device, format: TextureFormat) -> Self {
let bind_group_layout = device.create_bind_group_layout(&BindGroupLayoutDescriptor {
label: Some("Blit Source Bind Group Layout"),
entries: &[
BindGroupLayoutEntry {
binding: 0,
visibility: ShaderStages::FRAGMENT,
ty: BindingType::Texture {
sample_type: TextureSampleType::Float { filterable: true },
view_dimension: TextureViewDimension::D2,
multisampled: false,
},
count: None,
},
BindGroupLayoutEntry {
binding: 1,
visibility: ShaderStages::FRAGMENT,
ty: BindingType::Sampler(SamplerBindingType::Filtering),
count: None,
},
],
});
let sampler = device.create_sampler(&SamplerDescriptor {
label: Some("Blit Sampler"),
address_mode_u: AddressMode::ClampToEdge,
address_mode_v: AddressMode::ClampToEdge,
address_mode_w: AddressMode::ClampToEdge,
mag_filter: FilterMode::Linear,
min_filter: FilterMode::Linear,
..Default::default()
});
let shader = device.create_shader_module(include_wgsl!("blit.wgsl"));
let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("Blit Pipeline"),
layout: Some(
&device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("Blit Pipeline Layout"),
bind_group_layouts: &[&bind_group_layout],
push_constant_ranges: &[],
}),
),
vertex: VertexState {
module: &shader,
entry_point: "vertex",
buffers: &[],
},
fragment: Some(FragmentState {
module: &shader,
entry_point: "fragment",
targets: &[Some(ColorTargetState {
format,
blend: Some(BlendState::REPLACE),
write_mask: ColorWrites::ALL,
})],
}),
primitive: PrimitiveState::default(),
multisample: MultisampleState {
count: 4,
..Default::default()
},
depth_stencil: None,
multiview: None,
});
Self {
source_bind_group_layout: bind_group_layout,
sampler,
pipeline,
}
}
pub fn blit(
&self,
device: &Device,
encoder: &mut CommandEncoder,
source: &TextureView,
target: &TextureView,
) {
let bind_group = device.create_bind_group(&BindGroupDescriptor {
label: Some("Blit Source Bind Group"),
layout: &self.source_bind_group_layout,
entries: &[
BindGroupEntry {
binding: 0,
resource: BindingResource::TextureView(source),
},
BindGroupEntry {
binding: 1,
resource: BindingResource::Sampler(&self.sampler),
},
],
});
let mut render_pass = encoder.begin_render_pass(&RenderPassDescriptor {
label: Some("Blit Render Pass"),
color_attachments: &[Some(RenderPassColorAttachment {
view: target,
resolve_target: None,
ops: Operations {
load: wgpu::LoadOp::Load,
store: true,
},
})],
depth_stencil_attachment: None,
});
render_pass.set_pipeline(&self.pipeline);
render_pass.set_bind_group(0, &bind_group, &[]);
render_pass.draw(0..6, 0..1);
}
}