Adjusted offscreen pass to be a render pass vs compute for compat with web-colors flag.
This commit is contained in:
parent
78c0189824
commit
05e238e9ed
4 changed files with 168 additions and 60 deletions
|
|
@ -1,12 +1,24 @@
|
|||
use std::borrow::Cow;
|
||||
use wgpu::util::DeviceExt;
|
||||
use wgpu::vertex_attr_array;
|
||||
|
||||
/// A simple compute pipeline to convert any texture to Rgba8UnormSrgb.
|
||||
#[derive(Debug)]
|
||||
pub struct Pipeline {
|
||||
pipeline: wgpu::ComputePipeline,
|
||||
pipeline: wgpu::RenderPipeline,
|
||||
vertices: wgpu::Buffer,
|
||||
indices: wgpu::Buffer,
|
||||
sampler: wgpu::Sampler,
|
||||
layout: wgpu::BindGroupLayout,
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
#[repr(C)]
|
||||
struct Vertex {
|
||||
ndc: [f32; 2],
|
||||
texel: [f32; 2],
|
||||
}
|
||||
|
||||
impl Pipeline {
|
||||
pub fn new(device: &wgpu::Device) -> Self {
|
||||
let shader =
|
||||
|
|
@ -17,13 +29,53 @@ impl Pipeline {
|
|||
))),
|
||||
});
|
||||
|
||||
let vertices =
|
||||
device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("iced_wgpu.offscreen.vertex_buffer"),
|
||||
contents: bytemuck::cast_slice(&[
|
||||
//bottom left
|
||||
Vertex {
|
||||
ndc: [-1.0, -1.0],
|
||||
texel: [0.0, 1.0],
|
||||
},
|
||||
//bottom right
|
||||
Vertex {
|
||||
ndc: [1.0, -1.0],
|
||||
texel: [1.0, 1.0],
|
||||
},
|
||||
//top right
|
||||
Vertex {
|
||||
ndc: [1.0, 1.0],
|
||||
texel: [1.0, 0.0],
|
||||
},
|
||||
//top left
|
||||
Vertex {
|
||||
ndc: [-1.0, 1.0],
|
||||
texel: [0.0, 0.0],
|
||||
},
|
||||
]),
|
||||
usage: wgpu::BufferUsages::VERTEX,
|
||||
});
|
||||
|
||||
let indices =
|
||||
device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||
label: Some("iced_wgpu.offscreen.index_buffer"),
|
||||
contents: bytemuck::cast_slice(&[0u16, 1, 2, 2, 3, 0]),
|
||||
usage: wgpu::BufferUsages::INDEX,
|
||||
});
|
||||
|
||||
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
|
||||
label: Some("iced_wgpu.offscreen.sampler"),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
let bind_group_layout =
|
||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: Some("iced_wgpu.offscreen.blit.bind_group_layout"),
|
||||
entries: &[
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 0,
|
||||
visibility: wgpu::ShaderStages::COMPUTE,
|
||||
visibility: wgpu::ShaderStages::FRAGMENT,
|
||||
ty: wgpu::BindingType::Texture {
|
||||
sample_type: wgpu::TextureSampleType::Float {
|
||||
filterable: false,
|
||||
|
|
@ -35,12 +87,10 @@ impl Pipeline {
|
|||
},
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 1,
|
||||
visibility: wgpu::ShaderStages::COMPUTE,
|
||||
ty: wgpu::BindingType::StorageTexture {
|
||||
access: wgpu::StorageTextureAccess::WriteOnly,
|
||||
format: wgpu::TextureFormat::Rgba8Unorm,
|
||||
view_dimension: wgpu::TextureViewDimension::D2,
|
||||
},
|
||||
visibility: wgpu::ShaderStages::FRAGMENT,
|
||||
ty: wgpu::BindingType::Sampler(
|
||||
wgpu::SamplerBindingType::NonFiltering,
|
||||
),
|
||||
count: None,
|
||||
},
|
||||
],
|
||||
|
|
@ -54,15 +104,56 @@ impl Pipeline {
|
|||
});
|
||||
|
||||
let pipeline =
|
||||
device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
|
||||
device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||
label: Some("iced_wgpu.offscreen.blit.pipeline"),
|
||||
layout: Some(&pipeline_layout),
|
||||
module: &shader,
|
||||
entry_point: "main",
|
||||
vertex: wgpu::VertexState {
|
||||
module: &shader,
|
||||
entry_point: "vs_main",
|
||||
buffers: &[wgpu::VertexBufferLayout {
|
||||
array_stride: std::mem::size_of::<Vertex>() as u64,
|
||||
step_mode: wgpu::VertexStepMode::Vertex,
|
||||
attributes: &vertex_attr_array![
|
||||
0 => Float32x2, // quad ndc pos
|
||||
1 => Float32x2, // texture uv
|
||||
],
|
||||
}],
|
||||
},
|
||||
fragment: Some(wgpu::FragmentState {
|
||||
module: &shader,
|
||||
entry_point: "fs_main",
|
||||
targets: &[Some(wgpu::ColorTargetState {
|
||||
format: wgpu::TextureFormat::Rgba8UnormSrgb,
|
||||
blend: Some(wgpu::BlendState {
|
||||
color: wgpu::BlendComponent {
|
||||
src_factor: wgpu::BlendFactor::SrcAlpha,
|
||||
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
|
||||
operation: wgpu::BlendOperation::Add,
|
||||
},
|
||||
alpha: wgpu::BlendComponent {
|
||||
src_factor: wgpu::BlendFactor::One,
|
||||
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
|
||||
operation: wgpu::BlendOperation::Add,
|
||||
},
|
||||
}),
|
||||
write_mask: wgpu::ColorWrites::ALL,
|
||||
})],
|
||||
}),
|
||||
primitive: wgpu::PrimitiveState {
|
||||
topology: wgpu::PrimitiveTopology::TriangleList,
|
||||
front_face: wgpu::FrontFace::Cw,
|
||||
..Default::default()
|
||||
},
|
||||
depth_stencil: None,
|
||||
multisample: Default::default(),
|
||||
multiview: None,
|
||||
});
|
||||
|
||||
Self {
|
||||
pipeline,
|
||||
vertices,
|
||||
indices,
|
||||
sampler,
|
||||
layout: bind_group_layout,
|
||||
}
|
||||
}
|
||||
|
|
@ -70,11 +161,25 @@ impl Pipeline {
|
|||
pub fn convert(
|
||||
&self,
|
||||
device: &wgpu::Device,
|
||||
extent: wgpu::Extent3d,
|
||||
frame: &wgpu::TextureView,
|
||||
view: &wgpu::TextureView,
|
||||
size: wgpu::Extent3d,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
) {
|
||||
) -> wgpu::Texture {
|
||||
let texture = device.create_texture(&wgpu::TextureDescriptor {
|
||||
label: Some("iced_wgpu.offscreen.conversion.source_texture"),
|
||||
size,
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: wgpu::TextureFormat::Rgba8UnormSrgb,
|
||||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT
|
||||
| wgpu::TextureUsages::COPY_SRC,
|
||||
view_formats: &[],
|
||||
});
|
||||
|
||||
let view =
|
||||
&texture.create_view(&wgpu::TextureViewDescriptor::default());
|
||||
|
||||
let bind = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: Some("iced_wgpu.offscreen.blit.bind_group"),
|
||||
layout: &self.layout,
|
||||
|
|
@ -85,18 +190,33 @@ impl Pipeline {
|
|||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 1,
|
||||
resource: wgpu::BindingResource::TextureView(view),
|
||||
resource: wgpu::BindingResource::Sampler(&self.sampler),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
let mut compute_pass =
|
||||
encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
|
||||
label: Some("iced_wgpu.offscreen.blit.compute_pass"),
|
||||
});
|
||||
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||
label: Some("iced_wgpu.offscreen.blit.render_pass"),
|
||||
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
||||
view,
|
||||
resolve_target: None,
|
||||
ops: wgpu::Operations {
|
||||
load: wgpu::LoadOp::Load,
|
||||
store: true,
|
||||
},
|
||||
})],
|
||||
depth_stencil_attachment: None,
|
||||
});
|
||||
|
||||
compute_pass.set_pipeline(&self.pipeline);
|
||||
compute_pass.set_bind_group(0, &bind, &[]);
|
||||
compute_pass.dispatch_workgroups(extent.width, extent.height, 1);
|
||||
pass.set_pipeline(&self.pipeline);
|
||||
pass.set_bind_group(0, &bind, &[]);
|
||||
pass.set_vertex_buffer(0, self.vertices.slice(..));
|
||||
pass.set_index_buffer(
|
||||
self.indices.slice(..),
|
||||
wgpu::IndexFormat::Uint16,
|
||||
);
|
||||
pass.draw_indexed(0..6u32, 0, 0..1);
|
||||
|
||||
texture
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue