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.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.text_storage.trim();
|
||||
|
|
@ -153,7 +153,8 @@ impl Renderer {
|
|||
&mut engine.staging_belt,
|
||||
&mut self.triangle_storage,
|
||||
&layer.triangles,
|
||||
viewport.projection() * Transformation::scale(scale_factor),
|
||||
Transformation::scale(scale_factor),
|
||||
viewport.physical_size(),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -187,7 +188,6 @@ impl Renderer {
|
|||
fn render(
|
||||
&mut self,
|
||||
engine: &mut Engine,
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
frame: &wgpu::TextureView,
|
||||
clear_color: Option<Color>,
|
||||
|
|
@ -264,13 +264,11 @@ impl Renderer {
|
|||
let _ = ManuallyDrop::into_inner(render_pass);
|
||||
|
||||
mesh_layer += engine.triangle_pipeline.render(
|
||||
device,
|
||||
encoder,
|
||||
frame,
|
||||
&self.triangle_storage,
|
||||
mesh_layer,
|
||||
&layer.triangles,
|
||||
viewport.physical_size(),
|
||||
physical_bounds,
|
||||
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>(
|
||||
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>(0.0, 0.0),
|
||||
vec2<f32>(1.0, 0.0),
|
||||
vec2<f32>(0.0, 1.0),
|
||||
vec2<f32>(1.0, 1.0)
|
||||
);
|
||||
|
||||
@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>;
|
||||
|
||||
struct VertexInput {
|
||||
|
|
@ -30,9 +22,11 @@ struct VertexOutput {
|
|||
|
||||
@vertex
|
||||
fn vs_main(input: VertexInput) -> VertexOutput {
|
||||
let uv = uvs[input.vertex_index];
|
||||
|
||||
var out: VertexOutput;
|
||||
out.uv = uvs[input.vertex_index];
|
||||
out.position = vec4<f32>(positions[input.vertex_index], 0.0, 1.0);
|
||||
out.uv = uv * u_ratio;
|
||||
out.position = vec4<f32>(uv * vec2(2.0, -2.0) + vec2(-1.0, 1.0), 0.0, 1.0);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -184,8 +184,16 @@ impl Pipeline {
|
|||
belt: &mut wgpu::util::StagingBelt,
|
||||
storage: &mut Storage,
|
||||
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 {
|
||||
match item {
|
||||
Item::Group {
|
||||
|
|
@ -233,13 +241,11 @@ impl Pipeline {
|
|||
|
||||
pub fn render(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
target: &wgpu::TextureView,
|
||||
storage: &Storage,
|
||||
start: usize,
|
||||
batch: &Batch,
|
||||
target_size: Size<u32>,
|
||||
bounds: Rectangle,
|
||||
screen_transformation: Transformation,
|
||||
) -> usize {
|
||||
|
|
@ -274,13 +280,11 @@ impl Pipeline {
|
|||
});
|
||||
|
||||
render(
|
||||
device,
|
||||
encoder,
|
||||
target,
|
||||
self.blit.as_mut(),
|
||||
&self.solid,
|
||||
&self.gradient,
|
||||
target_size,
|
||||
bounds,
|
||||
items,
|
||||
);
|
||||
|
|
@ -294,20 +298,17 @@ impl Pipeline {
|
|||
}
|
||||
|
||||
fn render<'a>(
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
target: &wgpu::TextureView,
|
||||
mut blit: Option<&mut msaa::Blit>,
|
||||
solid: &solid::Pipeline,
|
||||
gradient: &gradient::Pipeline,
|
||||
target_size: Size<u32>,
|
||||
bounds: Rectangle,
|
||||
group: impl Iterator<Item = (&'a Layer, &'a [Mesh], Transformation)>,
|
||||
) {
|
||||
{
|
||||
let (attachment, resolve_target, load) = if let Some(blit) = &mut blit {
|
||||
let (attachment, resolve_target) =
|
||||
blit.targets(device, target_size.width, target_size.height);
|
||||
let (attachment, resolve_target) = blit.targets();
|
||||
|
||||
(
|
||||
attachment,
|
||||
|
|
|
|||
|
|
@ -1,13 +1,18 @@
|
|||
use crate::core::{Size, Transformation};
|
||||
use crate::graphics;
|
||||
|
||||
use std::num::NonZeroU64;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Blit {
|
||||
format: wgpu::TextureFormat,
|
||||
pipeline: wgpu::RenderPipeline,
|
||||
constants: wgpu::BindGroup,
|
||||
ratio: wgpu::Buffer,
|
||||
texture_layout: wgpu::BindGroupLayout,
|
||||
sample_count: u32,
|
||||
targets: Option<Targets>,
|
||||
last_region: Option<Size<u32>>,
|
||||
}
|
||||
|
||||
impl Blit {
|
||||
|
|
@ -19,27 +24,52 @@ impl Blit {
|
|||
let sampler =
|
||||
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 =
|
||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
label: Some("iced_wgpu::triangle:msaa uniforms layout"),
|
||||
entries: &[wgpu::BindGroupLayoutEntry {
|
||||
entries: &[
|
||||
wgpu::BindGroupLayoutEntry {
|
||||
binding: 0,
|
||||
visibility: wgpu::ShaderStages::FRAGMENT,
|
||||
ty: wgpu::BindingType::Sampler(
|
||||
wgpu::SamplerBindingType::NonFiltering,
|
||||
),
|
||||
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 =
|
||||
device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||
label: Some("iced_wgpu::triangle::msaa uniforms bind group"),
|
||||
layout: &constant_layout,
|
||||
entries: &[wgpu::BindGroupEntry {
|
||||
entries: &[
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 0,
|
||||
resource: wgpu::BindingResource::Sampler(&sampler),
|
||||
}],
|
||||
},
|
||||
wgpu::BindGroupEntry {
|
||||
binding: 1,
|
||||
resource: ratio.as_entire_binding(),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
let texture_layout =
|
||||
|
|
@ -88,9 +118,7 @@ impl Blit {
|
|||
entry_point: "fs_main",
|
||||
targets: &[Some(wgpu::ColorTargetState {
|
||||
format,
|
||||
blend: Some(
|
||||
wgpu::BlendState::PREMULTIPLIED_ALPHA_BLENDING,
|
||||
),
|
||||
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
|
||||
write_mask: wgpu::ColorWrites::ALL,
|
||||
})],
|
||||
}),
|
||||
|
|
@ -112,43 +140,61 @@ impl Blit {
|
|||
format,
|
||||
pipeline,
|
||||
constants: constant_bind_group,
|
||||
ratio,
|
||||
texture_layout,
|
||||
sample_count: antialiasing.sample_count(),
|
||||
targets: None,
|
||||
last_region: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn targets(
|
||||
pub fn prepare(
|
||||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
width: u32,
|
||||
height: u32,
|
||||
) -> (&wgpu::TextureView, &wgpu::TextureView) {
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
belt: &mut wgpu::util::StagingBelt,
|
||||
region_size: Size<u32>,
|
||||
) -> Transformation {
|
||||
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(
|
||||
device,
|
||||
self.format,
|
||||
&self.texture_layout,
|
||||
self.sample_count,
|
||||
width,
|
||||
height,
|
||||
region_size,
|
||||
));
|
||||
}
|
||||
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();
|
||||
|
||||
(&targets.attachment, &targets.resolve)
|
||||
|
|
@ -191,8 +237,7 @@ struct Targets {
|
|||
attachment: wgpu::TextureView,
|
||||
resolve: wgpu::TextureView,
|
||||
bind_group: wgpu::BindGroup,
|
||||
width: u32,
|
||||
height: u32,
|
||||
size: Size<u32>,
|
||||
}
|
||||
|
||||
impl Targets {
|
||||
|
|
@ -201,12 +246,11 @@ impl Targets {
|
|||
format: wgpu::TextureFormat,
|
||||
texture_layout: &wgpu::BindGroupLayout,
|
||||
sample_count: u32,
|
||||
width: u32,
|
||||
height: u32,
|
||||
size: Size<u32>,
|
||||
) -> Targets {
|
||||
let extent = wgpu::Extent3d {
|
||||
width,
|
||||
height,
|
||||
width: size.width,
|
||||
height: size.height,
|
||||
depth_or_array_layers: 1,
|
||||
};
|
||||
|
||||
|
|
@ -252,8 +296,14 @@ impl Targets {
|
|||
attachment,
|
||||
resolve,
|
||||
bind_group,
|
||||
width,
|
||||
height,
|
||||
size,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[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