Skip to content
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

Add pixels::PixelsBuilder::alpha_mode #376

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct PixelsBuilder<'req, 'dev, 'win, W: HasRawWindowHandle + HasRawDisplay
surface_texture_format: Option<wgpu::TextureFormat>,
clear_color: wgpu::Color,
blend_state: wgpu::BlendState,
alpha_mode: wgpu::CompositeAlphaMode,
}

impl<'req, 'dev, 'win, W: HasRawWindowHandle + HasRawDisplayHandle>
Expand Down Expand Up @@ -64,6 +65,7 @@ impl<'req, 'dev, 'win, W: HasRawWindowHandle + HasRawDisplayHandle>
surface_texture_format: None,
clear_color: wgpu::Color::BLACK,
blend_state: wgpu::BlendState::ALPHA_BLENDING,
alpha_mode: wgpu::CompositeAlphaMode::Auto,
}
}

Expand Down Expand Up @@ -235,6 +237,22 @@ impl<'req, 'dev, 'win, W: HasRawWindowHandle + HasRawDisplayHandle>
self
}

/// Sets the alpha mode.
///
/// Default value is `Auto`.
///
///```
///use pixels::wgpu::CompositeAlphaMode;
///
///let mut pixels = PixelsBuilder::new(320, 240, surface_texture)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is not passing in CI.

/// .alpha_mode(CompositeAlphaMode::PostMultiplied)
/// .build()?;
///```
pub fn alpha_mode(mut self, alpha_mode: wgpu::CompositeAlphaMode) -> Self {
self.alpha_mode = alpha_mode;
self
}

/// Create a pixel buffer from the options builder.
///
/// This is the private implementation shared by [`PixelsBuilder::build`] and
Expand Down Expand Up @@ -319,7 +337,7 @@ impl<'req, 'dev, 'win, W: HasRawWindowHandle + HasRawDisplayHandle>
let mut pixels = Vec::with_capacity(pixels_buffer_size);
pixels.resize_with(pixels_buffer_size, Default::default);

let alpha_mode = surface_capabilities.alpha_modes[0];
let alpha_mode = self.alpha_mode;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably check whether the selected alpha mode is supported by the surface. Return an error if it is not.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the same be done for Pixels::surface_texture_format and Pixels::render_texture_format then?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that is the case. AlphaMode is for the surface configuration, not for any textures. The original code here used the first alpha mode from the surface capabilities. Even if that isn't a great default choice, it was at least guaranteed to be supported by the surface.


// Instantiate the Pixels struct
let context = PixelsContext {
Expand Down