-
Notifications
You must be signed in to change notification settings - Fork 69
Add AlphaMode and transparency
#321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
madsmtm
wants to merge
1
commit into
master
Choose a base branch
from
madsmtm/alpha-mode
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+677
−138
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| //! An example to test transparent rendering. | ||
| //! | ||
| //! Press `o`, `i`, `m` or `t` to change the alpha mode to `Opaque`, `Ignored`, `Premultiplied` and | ||
| //! `Postmultiplied` respectively. | ||
| //! | ||
| //! This should render 6 rectangular areas. For details on the terminology, see: | ||
| //! <https://html.spec.whatwg.org/multipage/canvas.html#premultiplied-alpha-and-the-2d-rendering-context> | ||
| //! | ||
| //! (255, 127, 0, 255): | ||
| //! - Opaque/Ignored: Completely-opaque orange. | ||
| //! - Postmultiplied: Completely-opaque orange. | ||
| //! - Premultiplied: Completely-opaque orange. | ||
| //! | ||
| //! (255, 255, 0, 127): | ||
| //! - Opaque/Ignored: Completely-opaque yellow. | ||
| //! - Postmultiplied: Halfway-opaque yellow. | ||
| //! - Premultiplied: Additive halfway-opaque yellow. | ||
| //! | ||
| //! (127, 127, 0, 127): | ||
| //! - Opaque/Ignored: Completely-opaque dark yellow. | ||
| //! - Postmultiplied: Halfway-opaque dark yellow. | ||
| //! - Premultiplied: Halfway-opaque yellow. | ||
| //! | ||
| //! (255, 127, 0, 127): | ||
| //! - Opaque/Ignored: Completely-opaque orange. | ||
| //! - Postmultiplied: Halfway-opaque orange. | ||
| //! - Premultiplied: Additive halfway-opaque orange. | ||
| //! | ||
| //! (255, 127, 0, 0): | ||
| //! - Opaque/Ignored: Completely-opaque orange. | ||
| //! - Postmultiplied: Fully-transparent orange. | ||
| //! - Premultiplied: Additive fully-transparent orange. | ||
| //! | ||
| //! (0, 0, 0, 0): | ||
| //! - Opaque/Ignored: Completely-opaque black. | ||
| //! - Postmultiplied: Fully-transparent. | ||
| //! - Premultiplied: Fully-transparent. | ||
| use softbuffer::{AlphaMode, Context, Pixel, Surface}; | ||
| use std::num::NonZeroU32; | ||
| use winit::event::{ElementState, KeyEvent, WindowEvent}; | ||
| use winit::event_loop::{ControlFlow, EventLoop}; | ||
| use winit::keyboard::{Key, NamedKey}; | ||
|
|
||
| mod util; | ||
|
|
||
| fn main() { | ||
| util::setup(); | ||
|
|
||
| let event_loop = EventLoop::new().unwrap(); | ||
|
|
||
| let context = Context::new(event_loop.owned_display_handle()).unwrap(); | ||
|
|
||
| let app = util::WinitAppBuilder::with_init( | ||
| |elwt| util::make_window(elwt, |w| w), | ||
| move |_elwt, window| Surface::new(&context, window.clone()).unwrap(), | ||
| ) | ||
| .with_event_handler(|window, surface, window_id, event, elwt| { | ||
| elwt.set_control_flow(ControlFlow::Wait); | ||
|
|
||
| if window_id != window.id() { | ||
| return; | ||
| } | ||
|
|
||
| match event { | ||
| WindowEvent::Resized(size) => { | ||
| let Some(surface) = surface else { | ||
| tracing::error!("Resized fired before Resumed or after Suspended"); | ||
| return; | ||
| }; | ||
|
|
||
| if let (Some(width), Some(height)) = | ||
| (NonZeroU32::new(size.width), NonZeroU32::new(size.height)) | ||
| { | ||
| surface.resize(width, height).unwrap(); | ||
| } | ||
| } | ||
| WindowEvent::RedrawRequested => { | ||
| let Some(surface) = surface else { | ||
| tracing::error!("RedrawRequested fired before Resumed or after Suspended"); | ||
| return; | ||
| }; | ||
|
|
||
| tracing::info!(alpha_mode = ?surface.alpha_mode(), "redraw"); | ||
|
|
||
| let alpha_mode = surface.alpha_mode(); | ||
| let mut buffer = surface.buffer_mut().unwrap(); | ||
| let width = buffer.width().get(); | ||
| for (x, _, pixel) in buffer.pixels_iter() { | ||
| let rectangle_number = (x * 6) / width; | ||
| *pixel = match rectangle_number { | ||
| 0 => Pixel::new_rgba(255, 127, 0, 255), | ||
| 1 => Pixel::new_rgba(255, 255, 0, 127), | ||
| 2 => Pixel::new_rgba(127, 127, 0, 127), | ||
| 3 => Pixel::new_rgba(255, 127, 0, 127), | ||
| 4 => Pixel::new_rgba(255, 127, 0, 0), | ||
| _ => Pixel::new_rgba(0, 0, 0, 0), | ||
| }; | ||
|
|
||
| // Convert `AlphaMode::Opaque` -> `AlphaMode::Ignored`. | ||
| if alpha_mode == AlphaMode::Opaque { | ||
| pixel.a = 255; | ||
| }; | ||
| } | ||
|
|
||
| buffer.present().unwrap(); | ||
| } | ||
| WindowEvent::CloseRequested | ||
| | WindowEvent::KeyboardInput { | ||
| event: | ||
| KeyEvent { | ||
| logical_key: Key::Named(NamedKey::Escape), | ||
| repeat: false, | ||
| .. | ||
| }, | ||
| .. | ||
| } => { | ||
| elwt.exit(); | ||
| } | ||
| WindowEvent::KeyboardInput { | ||
| event: | ||
| KeyEvent { | ||
| logical_key, | ||
| repeat: false, | ||
| state: ElementState::Pressed, | ||
| .. | ||
| }, | ||
| .. | ||
| } => { | ||
| let Some(surface) = surface else { | ||
| tracing::error!("KeyboardInput fired before Resumed or after Suspended"); | ||
| return; | ||
| }; | ||
|
|
||
| let alpha_mode = match logical_key.to_text() { | ||
| Some("o") => AlphaMode::Opaque, | ||
| Some("i") => AlphaMode::Ignored, | ||
| Some("m") => AlphaMode::Premultiplied, | ||
| Some("t") => AlphaMode::Postmultiplied, | ||
| _ => return, | ||
| }; | ||
|
|
||
| if !surface.supports_alpha_mode(alpha_mode) { | ||
| tracing::warn!(?alpha_mode, "not supported by the backend"); | ||
| return; | ||
| } | ||
|
|
||
| tracing::info!(?alpha_mode, "set alpha"); | ||
| let size = window.inner_size(); | ||
| let width = NonZeroU32::new(size.width).unwrap(); | ||
| let height = NonZeroU32::new(size.height).unwrap(); | ||
| surface.configure(width, height, alpha_mode).unwrap(); | ||
| assert_eq!(surface.alpha_mode(), alpha_mode); | ||
|
|
||
| window.set_transparent(matches!( | ||
| alpha_mode, | ||
| AlphaMode::Premultiplied | AlphaMode::Postmultiplied | ||
| )); | ||
|
|
||
| window.request_redraw(); | ||
| } | ||
| _ => {} | ||
| } | ||
| }); | ||
|
|
||
| util::run_app(event_loop, app); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be https://docs.rs/winit/0.30.12/winit/window/struct.WindowAttributes.html#method.with_transparent, right?
So something that's left to
winit.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or looking at this differently: if Vulkan, OpenGL, or wgpu won't set this property, then it makes sense that softbuffer won't either.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
wgpucallssetOpaqueon the layer they're working with, which might be the root layer if it was created fromCAMetalLayer, and otherwise it is a sublayer.In any case, Winit doesn't currently set this property on its content view/layer, only on the window itself.