Refactor Viewport and Compositor
This commit is contained in:
parent
720e7756f2
commit
a1a5fcfd46
24 changed files with 202 additions and 278 deletions
|
|
@ -1,11 +1,11 @@
|
|||
use crate::quad;
|
||||
use crate::text;
|
||||
use crate::triangle;
|
||||
use crate::{Settings, Target, Transformation};
|
||||
use crate::{Settings, Transformation};
|
||||
use iced_graphics::backend;
|
||||
use iced_graphics::font;
|
||||
use iced_graphics::layer::Layer;
|
||||
use iced_graphics::Primitive;
|
||||
use iced_graphics::{Primitive, Viewport};
|
||||
use iced_native::mouse;
|
||||
use iced_native::{Font, HorizontalAlignment, Size, VerticalAlignment};
|
||||
|
||||
|
|
@ -62,19 +62,19 @@ impl Backend {
|
|||
&mut self,
|
||||
device: &wgpu::Device,
|
||||
encoder: &mut wgpu::CommandEncoder,
|
||||
target: Target<'_>,
|
||||
frame: &wgpu::TextureView,
|
||||
viewport: &Viewport,
|
||||
(primitive, mouse_interaction): &(Primitive, mouse::Interaction),
|
||||
scale_factor: f64,
|
||||
overlay_text: &[T],
|
||||
) -> mouse::Interaction {
|
||||
log::debug!("Drawing");
|
||||
|
||||
let (width, height) = target.viewport.dimensions();
|
||||
let scale_factor = scale_factor as f32;
|
||||
let transformation = target.viewport.transformation();
|
||||
let target_size = viewport.physical_size();
|
||||
let scale_factor = viewport.scale_factor() as f32;
|
||||
let transformation = viewport.projection();
|
||||
|
||||
let mut layers = Layer::generate(primitive, &target.viewport);
|
||||
layers.push(Layer::overlay(overlay_text, &target.viewport));
|
||||
let mut layers = Layer::generate(primitive, viewport);
|
||||
layers.push(Layer::overlay(overlay_text, viewport));
|
||||
|
||||
for layer in layers {
|
||||
self.flush(
|
||||
|
|
@ -83,9 +83,9 @@ impl Backend {
|
|||
transformation,
|
||||
&layer,
|
||||
encoder,
|
||||
target.texture,
|
||||
width,
|
||||
height,
|
||||
&frame,
|
||||
target_size.width,
|
||||
target_size.height,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -106,7 +106,7 @@ impl Backend {
|
|||
target_width: u32,
|
||||
target_height: u32,
|
||||
) {
|
||||
let bounds = layer.bounds * scale_factor;
|
||||
let bounds = (layer.bounds * scale_factor).round();
|
||||
|
||||
if !layer.quads.is_empty() {
|
||||
self.quad_pipeline.draw(
|
||||
|
|
|
|||
|
|
@ -34,7 +34,6 @@ pub mod window;
|
|||
|
||||
mod backend;
|
||||
mod quad;
|
||||
mod target;
|
||||
mod text;
|
||||
|
||||
pub use iced_graphics::{Defaults, Primitive, Viewport};
|
||||
|
|
@ -42,7 +41,6 @@ pub use wgpu;
|
|||
|
||||
pub use backend::Backend;
|
||||
pub use settings::Settings;
|
||||
pub use target::Target;
|
||||
|
||||
#[doc(no_inline)]
|
||||
pub use widget::*;
|
||||
|
|
|
|||
|
|
@ -1,14 +0,0 @@
|
|||
use crate::Viewport;
|
||||
|
||||
/// A rendering target.
|
||||
#[derive(Debug)]
|
||||
pub struct Target<'a> {
|
||||
/// The texture where graphics will be rendered.
|
||||
pub texture: &'a wgpu::TextureView,
|
||||
|
||||
/// The viewport of the target.
|
||||
///
|
||||
/// Most of the time, you will want this to match the dimensions of the
|
||||
/// texture.
|
||||
pub viewport: &'a Viewport,
|
||||
}
|
||||
|
|
@ -326,7 +326,8 @@ impl Pipeline {
|
|||
for (i, (vertex_offset, index_offset, indices)) in
|
||||
offsets.into_iter().enumerate()
|
||||
{
|
||||
let clip_bounds = meshes[i].clip_bounds * scale_factor;
|
||||
let clip_bounds =
|
||||
(meshes[i].clip_bounds * scale_factor).round();
|
||||
|
||||
render_pass.set_scissor_rect(
|
||||
clip_bounds.x,
|
||||
|
|
|
|||
|
|
@ -1,6 +1,4 @@
|
|||
//! Display rendering results on windows.
|
||||
mod compositor;
|
||||
mod swap_chain;
|
||||
|
||||
pub use compositor::Compositor;
|
||||
pub use swap_chain::SwapChain;
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use crate::{window::SwapChain, Renderer, Settings, Target};
|
||||
use crate::{Renderer, Settings};
|
||||
|
||||
use iced_graphics::Viewport;
|
||||
use iced_native::{futures, mouse};
|
||||
use raw_window_handle::HasRawWindowHandle;
|
||||
|
||||
|
|
@ -11,11 +12,11 @@ pub struct Compositor {
|
|||
format: wgpu::TextureFormat,
|
||||
}
|
||||
|
||||
impl iced_native::window::Compositor for Compositor {
|
||||
impl iced_graphics::window::Compositor for Compositor {
|
||||
type Settings = Settings;
|
||||
type Renderer = Renderer;
|
||||
type Surface = wgpu::Surface;
|
||||
type SwapChain = SwapChain;
|
||||
type SwapChain = wgpu::SwapChain;
|
||||
|
||||
fn new(settings: Self::Settings) -> Self {
|
||||
let (device, queue) = futures::executor::block_on(async {
|
||||
|
|
@ -66,19 +67,28 @@ impl iced_native::window::Compositor for Compositor {
|
|||
surface: &Self::Surface,
|
||||
width: u32,
|
||||
height: u32,
|
||||
) -> SwapChain {
|
||||
SwapChain::new(&self.device, surface, self.format, width, height)
|
||||
) -> Self::SwapChain {
|
||||
self.device.create_swap_chain(
|
||||
surface,
|
||||
&wgpu::SwapChainDescriptor {
|
||||
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
|
||||
format: self.format,
|
||||
width,
|
||||
height,
|
||||
present_mode: wgpu::PresentMode::Mailbox,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn draw<T: AsRef<str>>(
|
||||
&mut self,
|
||||
renderer: &mut Self::Renderer,
|
||||
swap_chain: &mut SwapChain,
|
||||
swap_chain: &mut Self::SwapChain,
|
||||
viewport: &Viewport,
|
||||
output: &<Self::Renderer as iced_native::Renderer>::Output,
|
||||
scale_factor: f64,
|
||||
overlay: &[T],
|
||||
) -> mouse::Interaction {
|
||||
let (frame, viewport) = swap_chain.next_frame().expect("Next frame");
|
||||
let frame = swap_chain.get_next_texture().expect("Next frame");
|
||||
|
||||
let mut encoder = self.device.create_command_encoder(
|
||||
&wgpu::CommandEncoderDescriptor { label: None },
|
||||
|
|
@ -103,12 +113,9 @@ impl iced_native::window::Compositor for Compositor {
|
|||
let mouse_interaction = renderer.backend_mut().draw(
|
||||
&mut self.device,
|
||||
&mut encoder,
|
||||
Target {
|
||||
texture: &frame.view,
|
||||
viewport,
|
||||
},
|
||||
&frame.view,
|
||||
viewport,
|
||||
output,
|
||||
scale_factor,
|
||||
overlay,
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,61 +0,0 @@
|
|||
use crate::Viewport;
|
||||
|
||||
/// The rendering target of a window.
|
||||
///
|
||||
/// It represents a series of virtual framebuffers with a scale factor.
|
||||
#[derive(Debug)]
|
||||
pub struct SwapChain {
|
||||
raw: wgpu::SwapChain,
|
||||
viewport: Viewport,
|
||||
}
|
||||
|
||||
impl SwapChain {}
|
||||
|
||||
impl SwapChain {
|
||||
/// Creates a new [`SwapChain`] for the given surface.
|
||||
///
|
||||
/// [`SwapChain`]: struct.SwapChain.html
|
||||
pub fn new(
|
||||
device: &wgpu::Device,
|
||||
surface: &wgpu::Surface,
|
||||
format: wgpu::TextureFormat,
|
||||
width: u32,
|
||||
height: u32,
|
||||
) -> SwapChain {
|
||||
SwapChain {
|
||||
raw: new_swap_chain(surface, format, width, height, device),
|
||||
viewport: Viewport::new(width, height),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the next frame of the [`SwapChain`] alongside its [`Viewport`].
|
||||
///
|
||||
/// [`SwapChain`]: struct.SwapChain.html
|
||||
/// [`Viewport`]: ../struct.Viewport.html
|
||||
pub fn next_frame(
|
||||
&mut self,
|
||||
) -> Result<(wgpu::SwapChainOutput, &Viewport), wgpu::TimeOut> {
|
||||
let viewport = &self.viewport;
|
||||
|
||||
self.raw.get_next_texture().map(|output| (output, viewport))
|
||||
}
|
||||
}
|
||||
|
||||
fn new_swap_chain(
|
||||
surface: &wgpu::Surface,
|
||||
format: wgpu::TextureFormat,
|
||||
width: u32,
|
||||
height: u32,
|
||||
device: &wgpu::Device,
|
||||
) -> wgpu::SwapChain {
|
||||
device.create_swap_chain(
|
||||
&surface,
|
||||
&wgpu::SwapChainDescriptor {
|
||||
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
|
||||
format,
|
||||
width,
|
||||
height,
|
||||
present_mode: wgpu::PresentMode::Mailbox,
|
||||
},
|
||||
)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue