Skip to content

Commit

Permalink
Merge pull request #978 from ATiltedTree/use-preffered-format
Browse files Browse the repository at this point in the history
wgpu: Use the preferred texture format of the surface
  • Loading branch information
hecrj authored Aug 4, 2021
2 parents cdd2f24 + f3916de commit 63bdbf8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 33 deletions.
35 changes: 19 additions & 16 deletions examples/integration/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn main() {
let instance = wgpu::Instance::new(wgpu::BackendBit::PRIMARY);
let surface = unsafe { instance.create_surface(&window) };

let (mut device, queue) = futures::executor::block_on(async {
let (format, (mut device, queue)) = futures::executor::block_on(async {
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::HighPerformance,
Expand All @@ -43,29 +43,32 @@ pub fn main() {
.await
.expect("Request adapter");

adapter
.request_device(
&wgpu::DeviceDescriptor {
label: None,
features: wgpu::Features::empty(),
limits: wgpu::Limits::default(),
},
None,
)
.await
.expect("Request device")
(
adapter
.get_swap_chain_preferred_format(&surface)
.expect("Get preferred format"),
adapter
.request_device(
&wgpu::DeviceDescriptor {
label: None,
features: wgpu::Features::empty(),
limits: wgpu::Limits::default(),
},
None,
)
.await
.expect("Request device"),
)
});

let format = wgpu::TextureFormat::Bgra8UnormSrgb;

let mut swap_chain = {
let size = window.inner_size();

device.create_swap_chain(
&surface,
&wgpu::SwapChainDescriptor {
usage: wgpu::TextureUsage::RENDER_ATTACHMENT,
format: format,
format,
width: size.width,
height: size.height,
present_mode: wgpu::PresentMode::Mailbox,
Expand All @@ -85,7 +88,7 @@ pub fn main() {
// Initialize iced
let mut debug = Debug::new();
let mut renderer =
Renderer::new(Backend::new(&mut device, Settings::default()));
Renderer::new(Backend::new(&mut device, Settings::default(), format));

let mut state = program::State::new(
controls,
Expand Down
19 changes: 10 additions & 9 deletions wgpu/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,24 @@ pub struct Backend {

impl Backend {
/// Creates a new [`Backend`].
pub fn new(device: &wgpu::Device, settings: Settings) -> Self {
pub fn new(
device: &wgpu::Device,
settings: Settings,
format: wgpu::TextureFormat,
) -> Self {
let text_pipeline = text::Pipeline::new(
device,
settings.format,
format,
settings.default_font,
settings.text_multithreading,
);

let quad_pipeline = quad::Pipeline::new(device, settings.format);
let triangle_pipeline = triangle::Pipeline::new(
device,
settings.format,
settings.antialiasing,
);
let quad_pipeline = quad::Pipeline::new(device, format);
let triangle_pipeline =
triangle::Pipeline::new(device, format, settings.antialiasing);

#[cfg(any(feature = "image_rs", feature = "svg"))]
let image_pipeline = image::Pipeline::new(device, settings.format);
let image_pipeline = image::Pipeline::new(device, format);

Self {
quad_pipeline,
Expand Down
6 changes: 0 additions & 6 deletions wgpu/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ pub use crate::Antialiasing;
/// [`Backend`]: crate::Backend
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Settings {
/// The output format of the [`Backend`].
///
/// [`Backend`]: crate::Backend
pub format: wgpu::TextureFormat,

/// The present mode of the [`Backend`].
///
/// [`Backend`]: crate::Backend
Expand Down Expand Up @@ -68,7 +63,6 @@ impl Settings {
impl Default for Settings {
fn default() -> Settings {
Settings {
format: wgpu::TextureFormat::Bgra8UnormSrgb,
present_mode: wgpu::PresentMode::Mailbox,
internal_backend: wgpu::BackendBit::PRIMARY,
default_font: None,
Expand Down
10 changes: 8 additions & 2 deletions wgpu/src/window/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct Compositor {
queue: wgpu::Queue,
staging_belt: wgpu::util::StagingBelt,
local_pool: futures::executor::LocalPool,
format: wgpu::TextureFormat,
}

impl Compositor {
Expand Down Expand Up @@ -42,6 +43,10 @@ impl Compositor {
})
.await?;

let format = compatible_surface
.as_ref()
.and_then(|surf| adapter.get_swap_chain_preferred_format(surf))?;

let (device, queue) = adapter
.request_device(
&wgpu::DeviceDescriptor {
Expand Down Expand Up @@ -69,12 +74,13 @@ impl Compositor {
queue,
staging_belt,
local_pool,
format,
})
}

/// Creates a new rendering [`Backend`] for this [`Compositor`].
pub fn create_backend(&self) -> Backend {
Backend::new(&self.device, self.settings)
Backend::new(&self.device, self.settings, self.format)
}
}

Expand Down Expand Up @@ -119,7 +125,7 @@ impl iced_graphics::window::Compositor for Compositor {
surface,
&wgpu::SwapChainDescriptor {
usage: wgpu::TextureUsage::RENDER_ATTACHMENT,
format: self.settings.format,
format: self.format,
present_mode: self.settings.present_mode,
width,
height,
Expand Down

0 comments on commit 63bdbf8

Please sign in to comment.