Share msaa::Blit texture between multiple windows
This commit is contained in:
parent
13289dbd19
commit
288f62bfb6
4 changed files with 112 additions and 69 deletions
|
|
@ -117,7 +117,7 @@ impl Renderer {
|
||||||
) {
|
) {
|
||||||
self.draw_overlay(overlay, viewport);
|
self.draw_overlay(overlay, viewport);
|
||||||
self.prepare(engine, device, queue, format, encoder, viewport);
|
self.prepare(engine, device, queue, format, encoder, viewport);
|
||||||
self.render(engine, device, encoder, frame, clear_color, viewport);
|
self.render(engine, encoder, frame, clear_color, viewport);
|
||||||
|
|
||||||
self.triangle_storage.trim();
|
self.triangle_storage.trim();
|
||||||
self.text_storage.trim();
|
self.text_storage.trim();
|
||||||
|
|
@ -153,7 +153,8 @@ impl Renderer {
|
||||||
&mut engine.staging_belt,
|
&mut engine.staging_belt,
|
||||||
&mut self.triangle_storage,
|
&mut self.triangle_storage,
|
||||||
&layer.triangles,
|
&layer.triangles,
|
||||||
viewport.projection() * Transformation::scale(scale_factor),
|
Transformation::scale(scale_factor),
|
||||||
|
viewport.physical_size(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -187,7 +188,6 @@ impl Renderer {
|
||||||
fn render(
|
fn render(
|
||||||
&mut self,
|
&mut self,
|
||||||
engine: &mut Engine,
|
engine: &mut Engine,
|
||||||
device: &wgpu::Device,
|
|
||||||
encoder: &mut wgpu::CommandEncoder,
|
encoder: &mut wgpu::CommandEncoder,
|
||||||
frame: &wgpu::TextureView,
|
frame: &wgpu::TextureView,
|
||||||
clear_color: Option<Color>,
|
clear_color: Option<Color>,
|
||||||
|
|
@ -264,13 +264,11 @@ impl Renderer {
|
||||||
let _ = ManuallyDrop::into_inner(render_pass);
|
let _ = ManuallyDrop::into_inner(render_pass);
|
||||||
|
|
||||||
mesh_layer += engine.triangle_pipeline.render(
|
mesh_layer += engine.triangle_pipeline.render(
|
||||||
device,
|
|
||||||
encoder,
|
encoder,
|
||||||
frame,
|
frame,
|
||||||
&self.triangle_storage,
|
&self.triangle_storage,
|
||||||
mesh_layer,
|
mesh_layer,
|
||||||
&layer.triangles,
|
&layer.triangles,
|
||||||
viewport.physical_size(),
|
|
||||||
physical_bounds,
|
physical_bounds,
|
||||||
scale,
|
scale,
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,14 @@
|
||||||
var<private> positions: array<vec2<f32>, 6> = array<vec2<f32>, 6>(
|
|
||||||
vec2<f32>(-1.0, 1.0),
|
|
||||||
vec2<f32>(-1.0, -1.0),
|
|
||||||
vec2<f32>(1.0, -1.0),
|
|
||||||
vec2<f32>(-1.0, 1.0),
|
|
||||||
vec2<f32>(1.0, 1.0),
|
|
||||||
vec2<f32>(1.0, -1.0)
|
|
||||||
);
|
|
||||||
|
|
||||||
var<private> uvs: array<vec2<f32>, 6> = array<vec2<f32>, 6>(
|
var<private> uvs: array<vec2<f32>, 6> = array<vec2<f32>, 6>(
|
||||||
vec2<f32>(0.0, 0.0),
|
vec2<f32>(0.0, 0.0),
|
||||||
vec2<f32>(0.0, 1.0),
|
vec2<f32>(1.0, 0.0),
|
||||||
vec2<f32>(1.0, 1.0),
|
vec2<f32>(1.0, 1.0),
|
||||||
vec2<f32>(0.0, 0.0),
|
vec2<f32>(0.0, 0.0),
|
||||||
vec2<f32>(1.0, 0.0),
|
vec2<f32>(0.0, 1.0),
|
||||||
vec2<f32>(1.0, 1.0)
|
vec2<f32>(1.0, 1.0)
|
||||||
);
|
);
|
||||||
|
|
||||||
@group(0) @binding(0) var u_sampler: sampler;
|
@group(0) @binding(0) var u_sampler: sampler;
|
||||||
|
@group(0) @binding(1) var<uniform> u_ratio: vec2<f32>;
|
||||||
@group(1) @binding(0) var u_texture: texture_2d<f32>;
|
@group(1) @binding(0) var u_texture: texture_2d<f32>;
|
||||||
|
|
||||||
struct VertexInput {
|
struct VertexInput {
|
||||||
|
|
@ -30,9 +22,11 @@ struct VertexOutput {
|
||||||
|
|
||||||
@vertex
|
@vertex
|
||||||
fn vs_main(input: VertexInput) -> VertexOutput {
|
fn vs_main(input: VertexInput) -> VertexOutput {
|
||||||
|
let uv = uvs[input.vertex_index];
|
||||||
|
|
||||||
var out: VertexOutput;
|
var out: VertexOutput;
|
||||||
out.uv = uvs[input.vertex_index];
|
out.uv = uv * u_ratio;
|
||||||
out.position = vec4<f32>(positions[input.vertex_index], 0.0, 1.0);
|
out.position = vec4<f32>(uv * vec2(2.0, -2.0) + vec2(-1.0, 1.0), 0.0, 1.0);
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -184,8 +184,16 @@ impl Pipeline {
|
||||||
belt: &mut wgpu::util::StagingBelt,
|
belt: &mut wgpu::util::StagingBelt,
|
||||||
storage: &mut Storage,
|
storage: &mut Storage,
|
||||||
items: &[Item],
|
items: &[Item],
|
||||||
projection: Transformation,
|
scale: Transformation,
|
||||||
|
target_size: Size<u32>,
|
||||||
) {
|
) {
|
||||||
|
let projection = if let Some(blit) = &mut self.blit {
|
||||||
|
blit.prepare(device, encoder, belt, target_size) * scale
|
||||||
|
} else {
|
||||||
|
Transformation::orthographic(target_size.width, target_size.height)
|
||||||
|
* scale
|
||||||
|
};
|
||||||
|
|
||||||
for item in items {
|
for item in items {
|
||||||
match item {
|
match item {
|
||||||
Item::Group {
|
Item::Group {
|
||||||
|
|
@ -233,13 +241,11 @@ impl Pipeline {
|
||||||
|
|
||||||
pub fn render(
|
pub fn render(
|
||||||
&mut self,
|
&mut self,
|
||||||
device: &wgpu::Device,
|
|
||||||
encoder: &mut wgpu::CommandEncoder,
|
encoder: &mut wgpu::CommandEncoder,
|
||||||
target: &wgpu::TextureView,
|
target: &wgpu::TextureView,
|
||||||
storage: &Storage,
|
storage: &Storage,
|
||||||
start: usize,
|
start: usize,
|
||||||
batch: &Batch,
|
batch: &Batch,
|
||||||
target_size: Size<u32>,
|
|
||||||
bounds: Rectangle,
|
bounds: Rectangle,
|
||||||
screen_transformation: Transformation,
|
screen_transformation: Transformation,
|
||||||
) -> usize {
|
) -> usize {
|
||||||
|
|
@ -274,13 +280,11 @@ impl Pipeline {
|
||||||
});
|
});
|
||||||
|
|
||||||
render(
|
render(
|
||||||
device,
|
|
||||||
encoder,
|
encoder,
|
||||||
target,
|
target,
|
||||||
self.blit.as_mut(),
|
self.blit.as_mut(),
|
||||||
&self.solid,
|
&self.solid,
|
||||||
&self.gradient,
|
&self.gradient,
|
||||||
target_size,
|
|
||||||
bounds,
|
bounds,
|
||||||
items,
|
items,
|
||||||
);
|
);
|
||||||
|
|
@ -294,20 +298,17 @@ impl Pipeline {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render<'a>(
|
fn render<'a>(
|
||||||
device: &wgpu::Device,
|
|
||||||
encoder: &mut wgpu::CommandEncoder,
|
encoder: &mut wgpu::CommandEncoder,
|
||||||
target: &wgpu::TextureView,
|
target: &wgpu::TextureView,
|
||||||
mut blit: Option<&mut msaa::Blit>,
|
mut blit: Option<&mut msaa::Blit>,
|
||||||
solid: &solid::Pipeline,
|
solid: &solid::Pipeline,
|
||||||
gradient: &gradient::Pipeline,
|
gradient: &gradient::Pipeline,
|
||||||
target_size: Size<u32>,
|
|
||||||
bounds: Rectangle,
|
bounds: Rectangle,
|
||||||
group: impl Iterator<Item = (&'a Layer, &'a [Mesh], Transformation)>,
|
group: impl Iterator<Item = (&'a Layer, &'a [Mesh], Transformation)>,
|
||||||
) {
|
) {
|
||||||
{
|
{
|
||||||
let (attachment, resolve_target, load) = if let Some(blit) = &mut blit {
|
let (attachment, resolve_target, load) = if let Some(blit) = &mut blit {
|
||||||
let (attachment, resolve_target) =
|
let (attachment, resolve_target) = blit.targets();
|
||||||
blit.targets(device, target_size.width, target_size.height);
|
|
||||||
|
|
||||||
(
|
(
|
||||||
attachment,
|
attachment,
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,18 @@
|
||||||
|
use crate::core::{Size, Transformation};
|
||||||
use crate::graphics;
|
use crate::graphics;
|
||||||
|
|
||||||
|
use std::num::NonZeroU64;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Blit {
|
pub struct Blit {
|
||||||
format: wgpu::TextureFormat,
|
format: wgpu::TextureFormat,
|
||||||
pipeline: wgpu::RenderPipeline,
|
pipeline: wgpu::RenderPipeline,
|
||||||
constants: wgpu::BindGroup,
|
constants: wgpu::BindGroup,
|
||||||
|
ratio: wgpu::Buffer,
|
||||||
texture_layout: wgpu::BindGroupLayout,
|
texture_layout: wgpu::BindGroupLayout,
|
||||||
sample_count: u32,
|
sample_count: u32,
|
||||||
targets: Option<Targets>,
|
targets: Option<Targets>,
|
||||||
|
last_region: Option<Size<u32>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Blit {
|
impl Blit {
|
||||||
|
|
@ -19,27 +24,52 @@ impl Blit {
|
||||||
let sampler =
|
let sampler =
|
||||||
device.create_sampler(&wgpu::SamplerDescriptor::default());
|
device.create_sampler(&wgpu::SamplerDescriptor::default());
|
||||||
|
|
||||||
|
let ratio = device.create_buffer(&wgpu::BufferDescriptor {
|
||||||
|
label: Some("iced-wgpu::triangle::msaa ratio"),
|
||||||
|
size: std::mem::size_of::<Ratio>() as u64,
|
||||||
|
usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::UNIFORM,
|
||||||
|
mapped_at_creation: false,
|
||||||
|
});
|
||||||
|
|
||||||
let constant_layout =
|
let constant_layout =
|
||||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||||
label: Some("iced_wgpu::triangle:msaa uniforms layout"),
|
label: Some("iced_wgpu::triangle:msaa uniforms layout"),
|
||||||
entries: &[wgpu::BindGroupLayoutEntry {
|
entries: &[
|
||||||
binding: 0,
|
wgpu::BindGroupLayoutEntry {
|
||||||
visibility: wgpu::ShaderStages::FRAGMENT,
|
binding: 0,
|
||||||
ty: wgpu::BindingType::Sampler(
|
visibility: wgpu::ShaderStages::FRAGMENT,
|
||||||
wgpu::SamplerBindingType::NonFiltering,
|
ty: wgpu::BindingType::Sampler(
|
||||||
),
|
wgpu::SamplerBindingType::NonFiltering,
|
||||||
count: None,
|
),
|
||||||
}],
|
count: None,
|
||||||
|
},
|
||||||
|
wgpu::BindGroupLayoutEntry {
|
||||||
|
binding: 1,
|
||||||
|
visibility: wgpu::ShaderStages::VERTEX,
|
||||||
|
ty: wgpu::BindingType::Buffer {
|
||||||
|
ty: wgpu::BufferBindingType::Uniform,
|
||||||
|
has_dynamic_offset: false,
|
||||||
|
min_binding_size: None,
|
||||||
|
},
|
||||||
|
count: None,
|
||||||
|
},
|
||||||
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
let constant_bind_group =
|
let constant_bind_group =
|
||||||
device.create_bind_group(&wgpu::BindGroupDescriptor {
|
device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||||
label: Some("iced_wgpu::triangle::msaa uniforms bind group"),
|
label: Some("iced_wgpu::triangle::msaa uniforms bind group"),
|
||||||
layout: &constant_layout,
|
layout: &constant_layout,
|
||||||
entries: &[wgpu::BindGroupEntry {
|
entries: &[
|
||||||
binding: 0,
|
wgpu::BindGroupEntry {
|
||||||
resource: wgpu::BindingResource::Sampler(&sampler),
|
binding: 0,
|
||||||
}],
|
resource: wgpu::BindingResource::Sampler(&sampler),
|
||||||
|
},
|
||||||
|
wgpu::BindGroupEntry {
|
||||||
|
binding: 1,
|
||||||
|
resource: ratio.as_entire_binding(),
|
||||||
|
},
|
||||||
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
let texture_layout =
|
let texture_layout =
|
||||||
|
|
@ -88,9 +118,7 @@ impl Blit {
|
||||||
entry_point: "fs_main",
|
entry_point: "fs_main",
|
||||||
targets: &[Some(wgpu::ColorTargetState {
|
targets: &[Some(wgpu::ColorTargetState {
|
||||||
format,
|
format,
|
||||||
blend: Some(
|
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
|
||||||
wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING,
|
|
||||||
),
|
|
||||||
write_mask: wgpu::ColorWrites::ALL,
|
write_mask: wgpu::ColorWrites::ALL,
|
||||||
})],
|
})],
|
||||||
}),
|
}),
|
||||||
|
|
@ -112,43 +140,61 @@ impl Blit {
|
||||||
format,
|
format,
|
||||||
pipeline,
|
pipeline,
|
||||||
constants: constant_bind_group,
|
constants: constant_bind_group,
|
||||||
|
ratio,
|
||||||
texture_layout,
|
texture_layout,
|
||||||
sample_count: antialiasing.sample_count(),
|
sample_count: antialiasing.sample_count(),
|
||||||
targets: None,
|
targets: None,
|
||||||
|
last_region: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn targets(
|
pub fn prepare(
|
||||||
&mut self,
|
&mut self,
|
||||||
device: &wgpu::Device,
|
device: &wgpu::Device,
|
||||||
width: u32,
|
encoder: &mut wgpu::CommandEncoder,
|
||||||
height: u32,
|
belt: &mut wgpu::util::StagingBelt,
|
||||||
) -> (&wgpu::TextureView, &wgpu::TextureView) {
|
region_size: Size<u32>,
|
||||||
|
) -> Transformation {
|
||||||
match &mut self.targets {
|
match &mut self.targets {
|
||||||
None => {
|
Some(targets)
|
||||||
|
if region_size.width <= targets.size.width
|
||||||
|
&& region_size.height <= targets.size.height => {}
|
||||||
|
_ => {
|
||||||
self.targets = Some(Targets::new(
|
self.targets = Some(Targets::new(
|
||||||
device,
|
device,
|
||||||
self.format,
|
self.format,
|
||||||
&self.texture_layout,
|
&self.texture_layout,
|
||||||
self.sample_count,
|
self.sample_count,
|
||||||
width,
|
region_size,
|
||||||
height,
|
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
Some(targets) => {
|
|
||||||
if targets.width != width || targets.height != height {
|
|
||||||
self.targets = Some(Targets::new(
|
|
||||||
device,
|
|
||||||
self.format,
|
|
||||||
&self.texture_layout,
|
|
||||||
self.sample_count,
|
|
||||||
width,
|
|
||||||
height,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let targets = self.targets.as_mut().unwrap();
|
||||||
|
|
||||||
|
if Some(region_size) != self.last_region {
|
||||||
|
let ratio = Ratio {
|
||||||
|
u: region_size.width as f32 / targets.size.width as f32,
|
||||||
|
v: region_size.height as f32 / targets.size.height as f32,
|
||||||
|
};
|
||||||
|
|
||||||
|
belt.write_buffer(
|
||||||
|
encoder,
|
||||||
|
&self.ratio,
|
||||||
|
0,
|
||||||
|
NonZeroU64::new(std::mem::size_of::<Ratio>() as u64)
|
||||||
|
.expect("non-empty ratio"),
|
||||||
|
device,
|
||||||
|
)
|
||||||
|
.copy_from_slice(bytemuck::bytes_of(&ratio));
|
||||||
|
|
||||||
|
self.last_region = Some(region_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
Transformation::orthographic(targets.size.width, targets.size.height)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn targets(&self) -> (&wgpu::TextureView, &wgpu::TextureView) {
|
||||||
let targets = self.targets.as_ref().unwrap();
|
let targets = self.targets.as_ref().unwrap();
|
||||||
|
|
||||||
(&targets.attachment, &targets.resolve)
|
(&targets.attachment, &targets.resolve)
|
||||||
|
|
@ -191,8 +237,7 @@ struct Targets {
|
||||||
attachment: wgpu::TextureView,
|
attachment: wgpu::TextureView,
|
||||||
resolve: wgpu::TextureView,
|
resolve: wgpu::TextureView,
|
||||||
bind_group: wgpu::BindGroup,
|
bind_group: wgpu::BindGroup,
|
||||||
width: u32,
|
size: Size<u32>,
|
||||||
height: u32,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Targets {
|
impl Targets {
|
||||||
|
|
@ -201,12 +246,11 @@ impl Targets {
|
||||||
format: wgpu::TextureFormat,
|
format: wgpu::TextureFormat,
|
||||||
texture_layout: &wgpu::BindGroupLayout,
|
texture_layout: &wgpu::BindGroupLayout,
|
||||||
sample_count: u32,
|
sample_count: u32,
|
||||||
width: u32,
|
size: Size<u32>,
|
||||||
height: u32,
|
|
||||||
) -> Targets {
|
) -> Targets {
|
||||||
let extent = wgpu::Extent3d {
|
let extent = wgpu::Extent3d {
|
||||||
width,
|
width: size.width,
|
||||||
height,
|
height: size.height,
|
||||||
depth_or_array_layers: 1,
|
depth_or_array_layers: 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -252,8 +296,14 @@ impl Targets {
|
||||||
attachment,
|
attachment,
|
||||||
resolve,
|
resolve,
|
||||||
bind_group,
|
bind_group,
|
||||||
width,
|
size,
|
||||||
height,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
|
||||||
|
#[repr(C)]
|
||||||
|
struct Ratio {
|
||||||
|
u: f32,
|
||||||
|
v: f32,
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue