Make output format of iced_wgpu
configurable
This commit is contained in:
parent
e680fd27e7
commit
be14aca075
@ -40,11 +40,12 @@ pub fn main() {
|
||||
});
|
||||
|
||||
let surface = wgpu::Surface::create(&window);
|
||||
let format = wgpu::TextureFormat::Bgra8UnormSrgb;
|
||||
|
||||
let mut swap_chain = {
|
||||
let size = window.inner_size();
|
||||
|
||||
SwapChain::new(&device, &surface, size.width, size.height)
|
||||
SwapChain::new(&device, &surface, format, size.width, size.height)
|
||||
};
|
||||
let mut resized = false;
|
||||
|
||||
@ -163,6 +164,7 @@ pub fn main() {
|
||||
swap_chain = SwapChain::new(
|
||||
&device,
|
||||
&surface,
|
||||
format,
|
||||
size.width,
|
||||
size.height,
|
||||
);
|
||||
|
@ -183,6 +183,7 @@ pub trait Application: Sized {
|
||||
} else {
|
||||
None
|
||||
},
|
||||
..iced_wgpu::Settings::default()
|
||||
},
|
||||
);
|
||||
|
||||
|
@ -28,7 +28,7 @@ pub struct Pipeline {
|
||||
}
|
||||
|
||||
impl Pipeline {
|
||||
pub fn new(device: &wgpu::Device) -> Self {
|
||||
pub fn new(device: &wgpu::Device, format: wgpu::TextureFormat) -> Self {
|
||||
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
|
||||
address_mode_u: wgpu::AddressMode::ClampToEdge,
|
||||
address_mode_v: wgpu::AddressMode::ClampToEdge,
|
||||
@ -135,7 +135,7 @@ impl Pipeline {
|
||||
}),
|
||||
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
|
||||
color_states: &[wgpu::ColorStateDescriptor {
|
||||
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
||||
format,
|
||||
color_blend: wgpu::BlendDescriptor {
|
||||
src_factor: wgpu::BlendFactor::SrcAlpha,
|
||||
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
|
||||
|
@ -14,7 +14,10 @@ pub struct Pipeline {
|
||||
}
|
||||
|
||||
impl Pipeline {
|
||||
pub fn new(device: &mut wgpu::Device) -> Pipeline {
|
||||
pub fn new(
|
||||
device: &mut wgpu::Device,
|
||||
format: wgpu::TextureFormat,
|
||||
) -> Pipeline {
|
||||
let constant_layout =
|
||||
device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
|
||||
bindings: &[wgpu::BindGroupLayoutBinding {
|
||||
@ -79,7 +82,7 @@ impl Pipeline {
|
||||
}),
|
||||
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
|
||||
color_states: &[wgpu::ColorStateDescriptor {
|
||||
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
||||
format,
|
||||
color_blend: wgpu::BlendDescriptor {
|
||||
src_factor: wgpu::BlendFactor::SrcAlpha,
|
||||
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
|
||||
|
@ -48,11 +48,16 @@ impl Renderer {
|
||||
///
|
||||
/// [`Renderer`]: struct.Renderer.html
|
||||
pub fn new(device: &mut wgpu::Device, settings: Settings) -> Self {
|
||||
let text_pipeline = text::Pipeline::new(device, settings.default_font);
|
||||
let quad_pipeline = quad::Pipeline::new(device);
|
||||
let image_pipeline = crate::image::Pipeline::new(device);
|
||||
let triangle_pipeline =
|
||||
triangle::Pipeline::new(device, settings.antialiasing);
|
||||
let text_pipeline =
|
||||
text::Pipeline::new(device, settings.format, settings.default_font);
|
||||
let quad_pipeline = quad::Pipeline::new(device, settings.format);
|
||||
let image_pipeline =
|
||||
crate::image::Pipeline::new(device, settings.format);
|
||||
let triangle_pipeline = triangle::Pipeline::new(
|
||||
device,
|
||||
settings.format,
|
||||
settings.antialiasing,
|
||||
);
|
||||
|
||||
Self {
|
||||
quad_pipeline,
|
||||
|
@ -5,8 +5,13 @@
|
||||
/// The settings of a [`Renderer`].
|
||||
///
|
||||
/// [`Renderer`]: ../struct.Renderer.html
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Settings {
|
||||
/// The output format of the [`Renderer`].
|
||||
///
|
||||
/// [`Renderer`]: ../struct.Renderer.html
|
||||
pub format: wgpu::TextureFormat,
|
||||
|
||||
/// The bytes of the font that will be used by default.
|
||||
///
|
||||
/// If `None` is provided, a default system font will be chosen.
|
||||
@ -16,6 +21,16 @@ pub struct Settings {
|
||||
pub antialiasing: Option<Antialiasing>,
|
||||
}
|
||||
|
||||
impl Default for Settings {
|
||||
fn default() -> Settings {
|
||||
Settings {
|
||||
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
||||
default_font: None,
|
||||
antialiasing: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An antialiasing strategy.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Antialiasing {
|
||||
|
@ -22,7 +22,11 @@ pub struct Pipeline {
|
||||
}
|
||||
|
||||
impl Pipeline {
|
||||
pub fn new(device: &mut wgpu::Device, default_font: Option<&[u8]>) -> Self {
|
||||
pub fn new(
|
||||
device: &mut wgpu::Device,
|
||||
format: wgpu::TextureFormat,
|
||||
default_font: Option<&[u8]>,
|
||||
) -> Self {
|
||||
// TODO: Font customization
|
||||
let font_source = font::Source::new();
|
||||
|
||||
@ -54,7 +58,7 @@ impl Pipeline {
|
||||
|
||||
let draw_brush = brush_builder
|
||||
.initial_cache_size((2048, 2048))
|
||||
.build(device, wgpu::TextureFormat::Bgra8UnormSrgb);
|
||||
.build(device, format);
|
||||
|
||||
Pipeline {
|
||||
draw_brush: RefCell::new(draw_brush),
|
||||
|
@ -61,6 +61,7 @@ impl<T> Buffer<T> {
|
||||
impl Pipeline {
|
||||
pub fn new(
|
||||
device: &mut wgpu::Device,
|
||||
format: wgpu::TextureFormat,
|
||||
antialiasing: Option<settings::Antialiasing>,
|
||||
) -> Pipeline {
|
||||
let constant_layout =
|
||||
@ -127,7 +128,7 @@ impl Pipeline {
|
||||
}),
|
||||
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
|
||||
color_states: &[wgpu::ColorStateDescriptor {
|
||||
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
||||
format,
|
||||
color_blend: wgpu::BlendDescriptor {
|
||||
src_factor: wgpu::BlendFactor::SrcAlpha,
|
||||
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
|
||||
@ -169,7 +170,7 @@ impl Pipeline {
|
||||
|
||||
Pipeline {
|
||||
pipeline,
|
||||
blit: antialiasing.map(|a| msaa::Blit::new(device, a)),
|
||||
blit: antialiasing.map(|a| msaa::Blit::new(device, format, a)),
|
||||
constants: constant_bind_group,
|
||||
uniforms_buffer: constants_buffer,
|
||||
vertex_buffer: Buffer::new(
|
||||
|
@ -2,6 +2,7 @@ use crate::settings;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Blit {
|
||||
format: wgpu::TextureFormat,
|
||||
pipeline: wgpu::RenderPipeline,
|
||||
constants: wgpu::BindGroup,
|
||||
texture_layout: wgpu::BindGroupLayout,
|
||||
@ -12,6 +13,7 @@ pub struct Blit {
|
||||
impl Blit {
|
||||
pub fn new(
|
||||
device: &wgpu::Device,
|
||||
format: wgpu::TextureFormat,
|
||||
antialiasing: settings::Antialiasing,
|
||||
) -> Blit {
|
||||
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
|
||||
@ -93,7 +95,7 @@ impl Blit {
|
||||
}),
|
||||
primitive_topology: wgpu::PrimitiveTopology::TriangleList,
|
||||
color_states: &[wgpu::ColorStateDescriptor {
|
||||
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
||||
format,
|
||||
color_blend: wgpu::BlendDescriptor {
|
||||
src_factor: wgpu::BlendFactor::SrcAlpha,
|
||||
dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha,
|
||||
@ -115,6 +117,7 @@ impl Blit {
|
||||
});
|
||||
|
||||
Blit {
|
||||
format,
|
||||
pipeline,
|
||||
constants: constant_bind_group,
|
||||
texture_layout: texture_layout,
|
||||
@ -133,6 +136,7 @@ impl Blit {
|
||||
None => {
|
||||
self.targets = Some(Targets::new(
|
||||
&device,
|
||||
self.format,
|
||||
&self.texture_layout,
|
||||
self.sample_count,
|
||||
width,
|
||||
@ -143,6 +147,7 @@ impl Blit {
|
||||
if targets.width != width || targets.height != height {
|
||||
self.targets = Some(Targets::new(
|
||||
&device,
|
||||
self.format,
|
||||
&self.texture_layout,
|
||||
self.sample_count,
|
||||
width,
|
||||
@ -204,6 +209,7 @@ struct Targets {
|
||||
impl Targets {
|
||||
pub fn new(
|
||||
device: &wgpu::Device,
|
||||
format: wgpu::TextureFormat,
|
||||
texture_layout: &wgpu::BindGroupLayout,
|
||||
sample_count: u32,
|
||||
width: u32,
|
||||
@ -221,7 +227,7 @@ impl Targets {
|
||||
mip_level_count: 1,
|
||||
sample_count,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
||||
format,
|
||||
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
|
||||
});
|
||||
|
||||
@ -231,7 +237,7 @@ impl Targets {
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
||||
format,
|
||||
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT
|
||||
| wgpu::TextureUsage::SAMPLED,
|
||||
});
|
||||
|
@ -8,6 +8,7 @@ use raw_window_handle::HasRawWindowHandle;
|
||||
pub struct Backend {
|
||||
device: wgpu::Device,
|
||||
queue: wgpu::Queue,
|
||||
format: wgpu::TextureFormat,
|
||||
}
|
||||
|
||||
impl iced_native::window::Backend for Backend {
|
||||
@ -37,7 +38,14 @@ impl iced_native::window::Backend for Backend {
|
||||
|
||||
let renderer = Renderer::new(&mut device, settings);
|
||||
|
||||
(Backend { device, queue }, renderer)
|
||||
(
|
||||
Backend {
|
||||
device,
|
||||
queue,
|
||||
format: settings.format,
|
||||
},
|
||||
renderer,
|
||||
)
|
||||
}
|
||||
|
||||
fn create_surface<W: HasRawWindowHandle>(
|
||||
@ -53,7 +61,7 @@ impl iced_native::window::Backend for Backend {
|
||||
width: u32,
|
||||
height: u32,
|
||||
) -> SwapChain {
|
||||
SwapChain::new(&self.device, surface, width, height)
|
||||
SwapChain::new(&self.device, surface, self.format, width, height)
|
||||
}
|
||||
|
||||
fn draw<T: AsRef<str>>(
|
||||
|
@ -18,11 +18,12 @@ impl SwapChain {
|
||||
pub fn new(
|
||||
device: &wgpu::Device,
|
||||
surface: &wgpu::Surface,
|
||||
format: wgpu::TextureFormat,
|
||||
width: u32,
|
||||
height: u32,
|
||||
) -> SwapChain {
|
||||
SwapChain {
|
||||
raw: new_swap_chain(surface, width, height, device),
|
||||
raw: new_swap_chain(surface, format, width, height, device),
|
||||
viewport: Viewport::new(width, height),
|
||||
}
|
||||
}
|
||||
@ -38,6 +39,7 @@ impl SwapChain {
|
||||
|
||||
fn new_swap_chain(
|
||||
surface: &wgpu::Surface,
|
||||
format: wgpu::TextureFormat,
|
||||
width: u32,
|
||||
height: u32,
|
||||
device: &wgpu::Device,
|
||||
@ -46,7 +48,7 @@ fn new_swap_chain(
|
||||
&surface,
|
||||
&wgpu::SwapChainDescriptor {
|
||||
usage: wgpu::TextureUsage::OUTPUT_ATTACHMENT,
|
||||
format: wgpu::TextureFormat::Bgra8UnormSrgb,
|
||||
format,
|
||||
width,
|
||||
height,
|
||||
present_mode: wgpu::PresentMode::Vsync,
|
||||
|
Loading…
Reference in New Issue
Block a user