expose window alpha mode (#6331)

# Objective

- Being able to set the `CompositeAlphaMode`

## Solution

- Expose it on `WindowDescriptor`, in the same way as `PresentMode` is exposed
This commit is contained in:
François 2022-10-24 14:53:19 +00:00
parent c9888a969c
commit 0cbd1bbe43
2 changed files with 53 additions and 2 deletions

View File

@ -6,7 +6,9 @@ use crate::{
use bevy_app::{App, Plugin}; use bevy_app::{App, Plugin};
use bevy_ecs::prelude::*; use bevy_ecs::prelude::*;
use bevy_utils::{tracing::debug, HashMap, HashSet}; use bevy_utils::{tracing::debug, HashMap, HashSet};
use bevy_window::{PresentMode, RawHandleWrapper, WindowClosed, WindowId, Windows}; use bevy_window::{
CompositeAlphaMode, PresentMode, RawHandleWrapper, WindowClosed, WindowId, Windows,
};
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
/// Token to ensure a system runs on the main thread. /// Token to ensure a system runs on the main thread.
@ -45,6 +47,7 @@ pub struct ExtractedWindow {
pub swap_chain_texture: Option<TextureView>, pub swap_chain_texture: Option<TextureView>,
pub size_changed: bool, pub size_changed: bool,
pub present_mode_changed: bool, pub present_mode_changed: bool,
pub alpha_mode: CompositeAlphaMode,
} }
#[derive(Default, Resource)] #[derive(Default, Resource)]
@ -90,6 +93,7 @@ fn extract_windows(
swap_chain_texture: None, swap_chain_texture: None,
size_changed: false, size_changed: false,
present_mode_changed: false, present_mode_changed: false,
alpha_mode: window.alpha_mode(),
}); });
// NOTE: Drop the swap chain frame here // NOTE: Drop the swap chain frame here
@ -196,7 +200,13 @@ pub fn prepare_windows(
PresentMode::AutoVsync => wgpu::PresentMode::AutoVsync, PresentMode::AutoVsync => wgpu::PresentMode::AutoVsync,
PresentMode::AutoNoVsync => wgpu::PresentMode::AutoNoVsync, PresentMode::AutoNoVsync => wgpu::PresentMode::AutoNoVsync,
}, },
alpha_mode: wgpu::CompositeAlphaMode::Auto, alpha_mode: match window.alpha_mode {
CompositeAlphaMode::Auto => wgpu::CompositeAlphaMode::Auto,
CompositeAlphaMode::Opaque => wgpu::CompositeAlphaMode::Opaque,
CompositeAlphaMode::PreMultiplied => wgpu::CompositeAlphaMode::PreMultiplied,
CompositeAlphaMode::PostMultiplied => wgpu::CompositeAlphaMode::PostMultiplied,
CompositeAlphaMode::Inherit => wgpu::CompositeAlphaMode::Inherit,
},
}; };
// Do the initial surface configuration if it hasn't been configured yet. Or if size or // Do the initial surface configuration if it hasn't been configured yet. Or if size or

View File

@ -57,6 +57,36 @@ pub enum PresentMode {
Fifo = 4, // NOTE: The explicit ordinal values mirror wgpu. Fifo = 4, // NOTE: The explicit ordinal values mirror wgpu.
} }
/// Specifies how the alpha channel of the textures should be handled during compositing.
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub enum CompositeAlphaMode {
/// Chooses either `Opaque` or `Inherit` automaticallydepending on the
/// `alpha_mode` that the current surface can support.
Auto = 0,
/// The alpha channel, if it exists, of the textures is ignored in the
/// compositing process. Instead, the textures is treated as if it has a
/// constant alpha of 1.0.
Opaque = 1,
/// The alpha channel, if it exists, of the textures is respected in the
/// compositing process. The non-alpha channels of the textures are
/// expected to already be multiplied by the alpha channel by the
/// application.
PreMultiplied = 2,
/// The alpha channel, if it exists, of the textures is respected in the
/// compositing process. The non-alpha channels of the textures are not
/// expected to already be multiplied by the alpha channel by the
/// application; instead, the compositor will multiply the non-alpha
/// channels of the texture by the alpha channel during compositing.
PostMultiplied = 3,
/// The alpha channel, if it exists, of the textures is unknown for processing
/// during compositing. Instead, the application is responsible for setting
/// the composite alpha blending mode using native WSI command. If not set,
/// then a platform-specific default will be used.
Inherit = 4,
}
impl WindowId { impl WindowId {
/// Creates a new [`WindowId`]. /// Creates a new [`WindowId`].
pub fn new() -> Self { pub fn new() -> Self {
@ -264,6 +294,7 @@ pub struct Window {
canvas: Option<String>, canvas: Option<String>,
fit_canvas_to_parent: bool, fit_canvas_to_parent: bool,
command_queue: Vec<WindowCommand>, command_queue: Vec<WindowCommand>,
alpha_mode: CompositeAlphaMode,
} }
/// A command to be sent to a window. /// A command to be sent to a window.
/// ///
@ -407,6 +438,7 @@ impl Window {
canvas: window_descriptor.canvas.clone(), canvas: window_descriptor.canvas.clone(),
fit_canvas_to_parent: window_descriptor.fit_canvas_to_parent, fit_canvas_to_parent: window_descriptor.fit_canvas_to_parent,
command_queue: Vec::new(), command_queue: Vec::new(),
alpha_mode: window_descriptor.alpha_mode,
} }
} }
/// Get the window's [`WindowId`]. /// Get the window's [`WindowId`].
@ -616,6 +648,12 @@ impl Window {
self.present_mode self.present_mode
} }
#[inline]
/// Get the window's [`CompositeAlphaMode`].
pub fn alpha_mode(&self) -> CompositeAlphaMode {
self.alpha_mode
}
#[inline] #[inline]
#[doc(alias = "set_vsync")] #[doc(alias = "set_vsync")]
/// Set the window's [`PresentMode`]. /// Set the window's [`PresentMode`].
@ -933,6 +971,8 @@ pub struct WindowDescriptor {
/// ///
/// This value has no effect on non-web platforms. /// This value has no effect on non-web platforms.
pub fit_canvas_to_parent: bool, pub fit_canvas_to_parent: bool,
/// Specifies how the alpha channel of the textures should be handled during compositing.
pub alpha_mode: CompositeAlphaMode,
} }
impl Default for WindowDescriptor { impl Default for WindowDescriptor {
@ -954,6 +994,7 @@ impl Default for WindowDescriptor {
transparent: false, transparent: false,
canvas: None, canvas: None,
fit_canvas_to_parent: false, fit_canvas_to_parent: false,
alpha_mode: CompositeAlphaMode::Auto,
} }
} }
} }