Make BlendState configurable (#260)
This commit is contained in:
parent
ebf7179a89
commit
94a2cc2dbd
|
@ -17,6 +17,7 @@ pub struct PixelsBuilder<'req, 'dev, 'win, W: HasRawWindowHandle> {
|
||||||
render_texture_format: Option<wgpu::TextureFormat>,
|
render_texture_format: Option<wgpu::TextureFormat>,
|
||||||
surface_texture_format: Option<wgpu::TextureFormat>,
|
surface_texture_format: Option<wgpu::TextureFormat>,
|
||||||
clear_color: wgpu::Color,
|
clear_color: wgpu::Color,
|
||||||
|
blend_state: wgpu::BlendState,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'req, 'dev, 'win, W: HasRawWindowHandle> PixelsBuilder<'req, 'dev, 'win, W> {
|
impl<'req, 'dev, 'win, W: HasRawWindowHandle> PixelsBuilder<'req, 'dev, 'win, W> {
|
||||||
|
@ -71,6 +72,7 @@ impl<'req, 'dev, 'win, W: HasRawWindowHandle> PixelsBuilder<'req, 'dev, 'win, W>
|
||||||
render_texture_format: None,
|
render_texture_format: None,
|
||||||
surface_texture_format: None,
|
surface_texture_format: None,
|
||||||
clear_color: wgpu::Color::BLACK,
|
clear_color: wgpu::Color::BLACK,
|
||||||
|
blend_state: wgpu::BlendState::ALPHA_BLENDING,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -189,11 +191,37 @@ impl<'req, 'dev, 'win, W: HasRawWindowHandle> PixelsBuilder<'req, 'dev, 'win, W>
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set the default clear color.
|
/// Set the blend state.
|
||||||
|
///
|
||||||
|
/// Allows customization of how to mix the new and existing pixels in a texture
|
||||||
|
/// when rendering.
|
||||||
|
///
|
||||||
|
/// The default blend state is alpha blending with non-premultiplied alpha.
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// use pixels::wgpu::BlendState;
|
||||||
|
///
|
||||||
|
/// # use pixels::PixelsBuilder;
|
||||||
|
/// # let window = pixels_mocks::Rwh;
|
||||||
|
/// # let surface_texture = pixels::SurfaceTexture::new(320, 240, &window);
|
||||||
|
/// // Replace the old pixels with the new without mixing.
|
||||||
|
/// let mut pixels = PixelsBuilder::new(320, 240, surface_texture)
|
||||||
|
/// .blend_state(wgpu::BlendState::REPLACE)
|
||||||
|
/// .build()?;
|
||||||
|
/// # Ok::<(), pixels::Error>(())
|
||||||
|
/// ```
|
||||||
|
pub fn blend_state(mut self, blend_state: wgpu::BlendState) -> Self {
|
||||||
|
self.blend_state = blend_state;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set the clear color.
|
||||||
///
|
///
|
||||||
/// Allows customization of the background color and the border drawn for non-integer scale
|
/// Allows customization of the background color and the border drawn for non-integer scale
|
||||||
/// values.
|
/// values.
|
||||||
///
|
///
|
||||||
|
/// The default value is pure black.
|
||||||
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// use pixels::wgpu::Color;
|
/// use pixels::wgpu::Color;
|
||||||
///
|
///
|
||||||
|
@ -277,6 +305,7 @@ impl<'req, 'dev, 'win, W: HasRawWindowHandle> PixelsBuilder<'req, 'dev, 'win, W>
|
||||||
// Create the backing texture
|
// Create the backing texture
|
||||||
let surface_size = self.surface_texture.size;
|
let surface_size = self.surface_texture.size;
|
||||||
let clear_color = self.clear_color;
|
let clear_color = self.clear_color;
|
||||||
|
let blend_state = self.blend_state;
|
||||||
let (scaling_matrix_inverse, texture_extent, texture, scaling_renderer, pixels_buffer_size) =
|
let (scaling_matrix_inverse, texture_extent, texture, scaling_renderer, pixels_buffer_size) =
|
||||||
create_backing_texture(
|
create_backing_texture(
|
||||||
&device,
|
&device,
|
||||||
|
@ -288,6 +317,7 @@ impl<'req, 'dev, 'win, W: HasRawWindowHandle> PixelsBuilder<'req, 'dev, 'win, W>
|
||||||
&surface_size,
|
&surface_size,
|
||||||
render_texture_format,
|
render_texture_format,
|
||||||
clear_color,
|
clear_color,
|
||||||
|
blend_state,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Create the pixel buffer
|
// Create the pixel buffer
|
||||||
|
@ -312,6 +342,7 @@ impl<'req, 'dev, 'win, W: HasRawWindowHandle> PixelsBuilder<'req, 'dev, 'win, W>
|
||||||
present_mode,
|
present_mode,
|
||||||
render_texture_format,
|
render_texture_format,
|
||||||
surface_texture_format,
|
surface_texture_format,
|
||||||
|
blend_state,
|
||||||
pixels,
|
pixels,
|
||||||
scaling_matrix_inverse,
|
scaling_matrix_inverse,
|
||||||
};
|
};
|
||||||
|
@ -360,6 +391,7 @@ impl<'req, 'dev, 'win, W: HasRawWindowHandle> PixelsBuilder<'req, 'dev, 'win, W>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub(crate) fn create_backing_texture(
|
pub(crate) fn create_backing_texture(
|
||||||
device: &wgpu::Device,
|
device: &wgpu::Device,
|
||||||
width: u32,
|
width: u32,
|
||||||
|
@ -368,6 +400,7 @@ pub(crate) fn create_backing_texture(
|
||||||
surface_size: &SurfaceSize,
|
surface_size: &SurfaceSize,
|
||||||
render_texture_format: wgpu::TextureFormat,
|
render_texture_format: wgpu::TextureFormat,
|
||||||
clear_color: wgpu::Color,
|
clear_color: wgpu::Color,
|
||||||
|
blend_state: wgpu::BlendState,
|
||||||
) -> (
|
) -> (
|
||||||
ultraviolet::Mat4,
|
ultraviolet::Mat4,
|
||||||
wgpu::Extent3d,
|
wgpu::Extent3d,
|
||||||
|
@ -406,6 +439,7 @@ pub(crate) fn create_backing_texture(
|
||||||
surface_size,
|
surface_size,
|
||||||
render_texture_format,
|
render_texture_format,
|
||||||
clear_color,
|
clear_color,
|
||||||
|
blend_state,
|
||||||
);
|
);
|
||||||
|
|
||||||
let texture_format_size = get_texture_format_size(backing_texture_format);
|
let texture_format_size = get_texture_format_size(backing_texture_format);
|
||||||
|
|
|
@ -98,6 +98,7 @@ pub struct Pixels {
|
||||||
present_mode: wgpu::PresentMode,
|
present_mode: wgpu::PresentMode,
|
||||||
render_texture_format: wgpu::TextureFormat,
|
render_texture_format: wgpu::TextureFormat,
|
||||||
surface_texture_format: wgpu::TextureFormat,
|
surface_texture_format: wgpu::TextureFormat,
|
||||||
|
blend_state: wgpu::BlendState,
|
||||||
|
|
||||||
// Pixel buffer
|
// Pixel buffer
|
||||||
pixels: Vec<u8>,
|
pixels: Vec<u8>,
|
||||||
|
@ -285,6 +286,7 @@ impl Pixels {
|
||||||
&self.surface_size,
|
&self.surface_size,
|
||||||
self.render_texture_format,
|
self.render_texture_format,
|
||||||
self.context.scaling_renderer.clear_color,
|
self.context.scaling_renderer.clear_color,
|
||||||
|
self.blend_state,
|
||||||
);
|
);
|
||||||
|
|
||||||
self.scaling_matrix_inverse = scaling_matrix_inverse;
|
self.scaling_matrix_inverse = scaling_matrix_inverse;
|
||||||
|
|
|
@ -23,6 +23,7 @@ impl ScalingRenderer {
|
||||||
surface_size: &SurfaceSize,
|
surface_size: &SurfaceSize,
|
||||||
render_texture_format: wgpu::TextureFormat,
|
render_texture_format: wgpu::TextureFormat,
|
||||||
clear_color: wgpu::Color,
|
clear_color: wgpu::Color,
|
||||||
|
blend_state: wgpu::BlendState,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let shader = wgpu::include_wgsl!("../shaders/scale.wgsl");
|
let shader = wgpu::include_wgsl!("../shaders/scale.wgsl");
|
||||||
let module = device.create_shader_module(&shader);
|
let module = device.create_shader_module(&shader);
|
||||||
|
@ -152,7 +153,7 @@ impl ScalingRenderer {
|
||||||
entry_point: "fs_main",
|
entry_point: "fs_main",
|
||||||
targets: &[wgpu::ColorTargetState {
|
targets: &[wgpu::ColorTargetState {
|
||||||
format: render_texture_format,
|
format: render_texture_format,
|
||||||
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
|
blend: Some(blend_state),
|
||||||
write_mask: wgpu::ColorWrites::ALL,
|
write_mask: wgpu::ColorWrites::ALL,
|
||||||
}],
|
}],
|
||||||
}),
|
}),
|
||||||
|
|
Loading…
Reference in a new issue