Skip to content

Commit ae54187

Browse files
committed
Demonstrate how transparent windows can be used
1 parent 0b22871 commit ae54187

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

examples/scenes/src/test_scenes.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export_scenes!(
7676
labyrinth(labyrinth),
7777
robust_paths(robust_paths),
7878
base_color_test(base_color_test: animated),
79+
translucent_base(translucent_base),
7980
clip_test(clip_test: animated),
8081
longpathdash_butt(impls::longpathdash(Cap::Butt), "longpathdash (butt caps)", false),
8182
longpathdash_round(impls::longpathdash(Cap::Round), "longpathdash (round caps)", false),
@@ -1539,6 +1540,20 @@ mod impls {
15391540
);
15401541
}
15411542

1543+
pub(super) fn translucent_base(scene: &mut Scene, params: &mut SceneParams<'_>) {
1544+
let background_color = Color::TRANSPARENT;
1545+
params.base_color = Some(background_color);
1546+
1547+
// Blend a white square over it.
1548+
scene.fill(
1549+
Fill::NonZero,
1550+
Affine::IDENTITY,
1551+
palette::css::WHITE.with_alpha(0.5),
1552+
None,
1553+
&Rect::new(50.0, 50.0, 500.0, 500.0),
1554+
);
1555+
}
1556+
15421557
pub(super) fn clip_test(scene: &mut Scene, params: &mut SceneParams<'_>) {
15431558
let clip = {
15441559
const X0: f64 = 50.0;

examples/with_winit/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,7 @@ fn window_attributes() -> WindowAttributes {
782782
Window::default_attributes()
783783
.with_inner_size(LogicalSize::new(1044, 800))
784784
.with_resizable(true)
785+
.with_transparent(true)
785786
.with_title("Vello demo")
786787
}
787788

vello/src/util.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
use std::future::Future;
77

88
use wgpu::{
9-
util::TextureBlitter, Adapter, Device, Instance, Limits, Queue, Surface, SurfaceConfiguration,
10-
SurfaceTarget, Texture, TextureFormat, TextureView,
9+
util::{TextureBlitter, TextureBlitterBuilder},
10+
Adapter, BlendComponent, BlendFactor, BlendOperation, BlendState, Device, Instance, Limits,
11+
Queue, Surface, SurfaceConfiguration, SurfaceTarget, Texture, TextureFormat, TextureView,
1112
};
1213

1314
use crate::{Error, Result};
@@ -89,18 +90,28 @@ impl RenderContext {
8990
height,
9091
present_mode,
9192
desired_maximum_frame_latency: 2,
92-
alpha_mode: wgpu::CompositeAlphaMode::Auto,
93+
alpha_mode: wgpu::CompositeAlphaMode::PreMultiplied,
9394
view_formats: vec![],
9495
};
9596
let (target_texture, target_view) = create_targets(width, height, &device_handle.device);
97+
let premul_blitter = TextureBlitterBuilder::new(&device_handle.device, format)
98+
.blend_state(BlendState {
99+
alpha: BlendComponent::REPLACE,
100+
color: BlendComponent {
101+
src_factor: BlendFactor::SrcAlpha,
102+
dst_factor: BlendFactor::Zero,
103+
operation: BlendOperation::Add,
104+
},
105+
})
106+
.build();
96107
let surface = RenderSurface {
97108
surface,
98109
config,
99110
dev_id,
100111
format,
101112
target_texture,
102113
target_view,
103-
blitter: TextureBlitter::new(&device_handle.device, format),
114+
blitter: premul_blitter,
104115
};
105116
self.configure_surface(&surface);
106117
Ok(surface)

0 commit comments

Comments
 (0)