Skip to content

Commit c635290

Browse files
authored
Merge pull request #2231 from Koranir/transparency-fix
Fix alpha mode misconfiguration in wgpu renderer
2 parents 7ee00e7 + 0bcdefe commit c635290

6 files changed

Lines changed: 106 additions & 30 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
103103
- `Subscription::map` using unreliable function pointer hash to identify mappers. [#2237](https://github.com/iced-rs/iced/pull/2237)
104104
- Missing feature flag docs for `time::every`. [#2188](https://github.com/iced-rs/iced/pull/2188)
105105
- Event loop not being resumed on Windows while resizing. [#2214](https://github.com/iced-rs/iced/pull/2214)
106+
- Alpha mode misconfiguration in `iced_wgpu`. [#2231](https://github.com/iced-rs/iced/pull/2231)
106107

107108
Many thanks to...
108109

@@ -134,6 +135,7 @@ Many thanks to...
134135
- @jim-ec
135136
- @joshuamegnauth54
136137
- @jpttrssn
138+
- @Koranir
137139
- @lufte
138140
- @matze
139141
- @MichalLebeda

examples/gradient/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ edition = "2021"
55
publish = false
66

77
[dependencies]
8-
iced = { path = "../.." }
8+
iced.workspace = true
9+
iced.features = ["debug"]
10+
11+
tracing-subscriber = "0.3"

examples/gradient/src/main.rs

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,39 @@
1-
use iced::gradient;
2-
use iced::widget::{column, container, horizontal_space, row, slider, text};
1+
use iced::application;
2+
use iced::theme::{self, Theme};
3+
use iced::widget::{
4+
checkbox, column, container, horizontal_space, row, slider, text,
5+
};
6+
use iced::{gradient, window};
37
use iced::{
48
Alignment, Background, Color, Element, Length, Radians, Sandbox, Settings,
59
};
610

711
pub fn main() -> iced::Result {
8-
Gradient::run(Settings::default())
12+
tracing_subscriber::fmt::init();
13+
14+
Gradient::run(Settings {
15+
window: window::Settings {
16+
transparent: true,
17+
..Default::default()
18+
},
19+
..Default::default()
20+
})
921
}
1022

1123
#[derive(Debug, Clone, Copy)]
1224
struct Gradient {
1325
start: Color,
1426
end: Color,
1527
angle: Radians,
28+
transparent: bool,
1629
}
1730

1831
#[derive(Debug, Clone, Copy)]
1932
enum Message {
2033
StartChanged(Color),
2134
EndChanged(Color),
2235
AngleChanged(Radians),
36+
TransparentToggled(bool),
2337
}
2438

2539
impl Sandbox for Gradient {
@@ -30,6 +44,7 @@ impl Sandbox for Gradient {
3044
start: Color::WHITE,
3145
end: Color::new(0.0, 0.0, 1.0, 1.0),
3246
angle: Radians(0.0),
47+
transparent: false,
3348
}
3449
}
3550

@@ -42,11 +57,19 @@ impl Sandbox for Gradient {
4257
Message::StartChanged(color) => self.start = color,
4358
Message::EndChanged(color) => self.end = color,
4459
Message::AngleChanged(angle) => self.angle = angle,
60+
Message::TransparentToggled(transparent) => {
61+
self.transparent = transparent;
62+
}
4563
}
4664
}
4765

4866
fn view(&self) -> Element<Message> {
49-
let Self { start, end, angle } = *self;
67+
let Self {
68+
start,
69+
end,
70+
angle,
71+
transparent,
72+
} = *self;
5073

5174
let gradient_box = container(horizontal_space(Length::Fill))
5275
.width(Length::Fill)
@@ -72,14 +95,34 @@ impl Sandbox for Gradient {
7295
.padding(8)
7396
.align_items(Alignment::Center);
7497

98+
let transparency_toggle = iced::widget::Container::new(
99+
checkbox("Transparent window", transparent)
100+
.on_toggle(Message::TransparentToggled),
101+
)
102+
.padding(8);
103+
75104
column![
76105
color_picker("Start", self.start).map(Message::StartChanged),
77106
color_picker("End", self.end).map(Message::EndChanged),
78107
angle_picker,
79-
gradient_box
108+
transparency_toggle,
109+
gradient_box,
80110
]
81111
.into()
82112
}
113+
114+
fn style(&self) -> theme::Application {
115+
if self.transparent {
116+
theme::Application::custom(|theme: &Theme| {
117+
application::Appearance {
118+
background_color: Color::TRANSPARENT,
119+
text_color: theme.palette().text,
120+
}
121+
})
122+
} else {
123+
theme::Application::Default
124+
}
125+
}
83126
}
84127

85128
fn color_picker(label: &str, color: Color) -> Element<'_, Color> {
@@ -91,6 +134,8 @@ fn color_picker(label: &str, color: Color) -> Element<'_, Color> {
91134
.step(0.01),
92135
slider(0.0..=1.0, color.b, move |b| { Color { b, ..color } })
93136
.step(0.01),
137+
slider(0.0..=1.0, color.a, move |a| { Color { a, ..color } })
138+
.step(0.01),
94139
]
95140
.spacing(8)
96141
.padding(8)

examples/solar_system/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl Application for SolarSystem {
8888
}
8989
}
9090

91-
theme::Application::from(dark_background as fn(&Theme) -> _)
91+
theme::Application::custom(dark_background)
9292
}
9393

9494
fn subscription(&self) -> Subscription<Message> {

style/src/theme.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,15 @@ pub enum Application {
172172
Custom(Box<dyn application::StyleSheet<Style = Theme>>),
173173
}
174174

175+
impl Application {
176+
/// Creates a custom [`Application`] style.
177+
pub fn custom(
178+
custom: impl application::StyleSheet<Style = Theme> + 'static,
179+
) -> Self {
180+
Self::Custom(Box::new(custom))
181+
}
182+
}
183+
175184
impl application::StyleSheet for Theme {
176185
type Style = Application;
177186

@@ -196,14 +205,6 @@ impl<T: Fn(&Theme) -> application::Appearance> application::StyleSheet for T {
196205
}
197206
}
198207

199-
impl<T: Fn(&Theme) -> application::Appearance + 'static> From<T>
200-
for Application
201-
{
202-
fn from(f: T) -> Self {
203-
Self::Custom(Box::new(f))
204-
}
205-
}
206-
207208
/// The style of a button.
208209
#[derive(Default)]
209210
pub enum Button {

wgpu/src/window/compositor.rs

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub struct Compositor {
1515
device: wgpu::Device,
1616
queue: wgpu::Queue,
1717
format: wgpu::TextureFormat,
18+
alpha_mode: wgpu::CompositeAlphaMode,
1819
}
1920

2021
impl Compositor {
@@ -61,25 +62,48 @@ impl Compositor {
6162

6263
log::info!("Selected: {:#?}", adapter.get_info());
6364

64-
let format = compatible_surface.as_ref().and_then(|surface| {
65-
let capabilities = surface.get_capabilities(&adapter);
65+
let (format, alpha_mode) =
66+
compatible_surface.as_ref().and_then(|surface| {
67+
let capabilities = surface.get_capabilities(&adapter);
6668

67-
let mut formats = capabilities.formats.iter().copied();
69+
let mut formats = capabilities.formats.iter().copied();
6870

69-
let format = if color::GAMMA_CORRECTION {
70-
formats.find(wgpu::TextureFormat::is_srgb)
71-
} else {
72-
formats.find(|format| !wgpu::TextureFormat::is_srgb(format))
73-
};
71+
log::info!("Available formats: {formats:#?}");
7472

75-
format.or_else(|| {
76-
log::warn!("No format found!");
73+
let format = if color::GAMMA_CORRECTION {
74+
formats.find(wgpu::TextureFormat::is_srgb)
75+
} else {
76+
formats.find(|format| !wgpu::TextureFormat::is_srgb(format))
77+
};
7778

78-
capabilities.formats.first().copied()
79-
})
80-
})?;
79+
let format = format.or_else(|| {
80+
log::warn!("No format found!");
81+
82+
capabilities.formats.first().copied()
83+
});
84+
85+
let alpha_modes = capabilities.alpha_modes;
86+
87+
log::info!("Available alpha modes: {alpha_modes:#?}");
8188

82-
log::info!("Selected format: {format:?}");
89+
let preferred_alpha = if alpha_modes
90+
.contains(&wgpu::CompositeAlphaMode::PostMultiplied)
91+
{
92+
wgpu::CompositeAlphaMode::PostMultiplied
93+
} else if alpha_modes
94+
.contains(&wgpu::CompositeAlphaMode::PreMultiplied)
95+
{
96+
wgpu::CompositeAlphaMode::PreMultiplied
97+
} else {
98+
wgpu::CompositeAlphaMode::Auto
99+
};
100+
101+
format.zip(Some(preferred_alpha))
102+
})?;
103+
104+
log::info!(
105+
"Selected format: {format:?} with alpha mode: {alpha_mode:?}"
106+
);
83107

84108
#[cfg(target_arch = "wasm32")]
85109
let limits = [wgpu::Limits::downlevel_webgl2_defaults()
@@ -120,6 +144,7 @@ impl Compositor {
120144
device,
121145
queue,
122146
format,
147+
alpha_mode,
123148
})
124149
}
125150

@@ -249,7 +274,7 @@ impl graphics::Compositor for Compositor {
249274
present_mode: self.settings.present_mode,
250275
width,
251276
height,
252-
alpha_mode: wgpu::CompositeAlphaMode::Auto,
277+
alpha_mode: self.alpha_mode,
253278
view_formats: vec![],
254279
desired_maximum_frame_latency: 2,
255280
},

0 commit comments

Comments
 (0)