How to make transparent window in rust using winit + wgpu?

1.2k views Asked by At

I wanted to create something like rainmeter gadgets for windows. I need to create a partially-transparent window without header and borders.
Some parts of my code:

// winit

let event_loop  = EventLoop::new();
let main_window = WindowBuilder::new()
    .with_decorations(false)
    .with_transparent(true)
    .with_inner_size(LogicalSize::new(640, 480))
    .build(&event_loop)
    .expect("Cant create winit window");

...
// wgpu

let attachments = [RenderPassColorAttachment {
    view           : &view,
    resolve_target : None,
    ops            : Operations 
    { 
        load  : LoadOp::Clear(Color { r: 0.8, g: 0.0, b: 0.3, a: 1.0 }), 
        store : true, 
    },
}];
{
    let mut pass = encoder.begin_render_pass(&RenderPassDescriptor { 
        label                    : None, 
        depth_stencil_attachment : None,
        color_attachments        : &attachments, 
    });
}

renderer.queue.submit([encoder.finish()]);
texture.present();

It just fills window by a solid color. But there is the problem. Window is not fully opaque, but color alpha is 1.0.

Image

If i set color (0,0,0,1) - window begins fully transparent. It looks like window has blend mode add, but i need normal alpha mode. How to fix this? Should i use winapi? Thanks.

1

There are 1 answers

0
Jinlei Li On

wgpu 0.14 already provides an alpha_mode field to support window transparency, which works perfectly on vulkan and metal devices. But on dx devices there are still problems.

https://github.com/gfx-rs/wgpu/pull/2836