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

Configurable clear color #236

Merged
merged 4 commits into from
Dec 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct PixelsBuilder<'req, 'dev, 'win, W: HasRawWindowHandle> {
surface_texture: SurfaceTexture<'win, W>,
texture_format: wgpu::TextureFormat,
render_texture_format: Option<wgpu::TextureFormat>,
clear_color: wgpu::Color,
}

impl<'req, 'dev, 'win, W: HasRawWindowHandle> PixelsBuilder<'req, 'dev, 'win, W> {
Expand Down Expand Up @@ -67,6 +68,7 @@ impl<'req, 'dev, 'win, W: HasRawWindowHandle> PixelsBuilder<'req, 'dev, 'win, W>
surface_texture,
texture_format: wgpu::TextureFormat::Rgba8UnormSrgb,
render_texture_format: None,
clear_color: wgpu::Color::BLACK,
}
}

Expand Down Expand Up @@ -176,6 +178,33 @@ impl<'req, 'dev, 'win, W: HasRawWindowHandle> PixelsBuilder<'req, 'dev, 'win, W>
self
}

/// Set the default clear color.
///
/// Allows customization of the background color and the border drawn for non-integer scale
/// values.
///
/// ```no_run
/// use pixels::wgpu::Color;
///
/// # use pixels::PixelsBuilder;
/// # let window = pixels_mocks::Rwh;
/// # let surface_texture = pixels::SurfaceTexture::new(320, 240, &window);
/// // Set clear color to bright magenta.
/// let mut pixels = PixelsBuilder::new(320, 240, surface_texture)
/// .clear_color(Color {
/// r: 1.0,
/// g: 0.0,
/// b: 1.0,
/// a: 1.0,
/// })
/// .build()?;
/// # Ok::<(), pixels::Error>(())
/// ```
pub fn clear_color(mut self, color: wgpu::Color) -> Self {
self.clear_color = color;
self
}

/// Create a pixel buffer from the options builder.
///
/// This is the private implementation shared by [`PixelsBuilder::build`] and
Expand Down Expand Up @@ -235,6 +264,7 @@ impl<'req, 'dev, 'win, W: HasRawWindowHandle> PixelsBuilder<'req, 'dev, 'win, W>

// Create the backing texture
let surface_size = self.surface_texture.size;
let clear_color = self.clear_color;
let (scaling_matrix_inverse, texture_extent, texture, scaling_renderer, pixels_buffer_size) =
create_backing_texture(
&device,
Expand All @@ -245,6 +275,7 @@ impl<'req, 'dev, 'win, W: HasRawWindowHandle> PixelsBuilder<'req, 'dev, 'win, W>
// Render texture values
&surface_size,
render_texture_format,
clear_color,
);

// Create the pixel buffer
Expand Down Expand Up @@ -323,6 +354,7 @@ pub(crate) fn create_backing_texture(
backing_texture_format: wgpu::TextureFormat,
surface_size: &SurfaceSize,
render_texture_format: wgpu::TextureFormat,
clear_color: wgpu::Color,
) -> (
ultraviolet::Mat4,
wgpu::Extent3d,
Expand Down Expand Up @@ -360,6 +392,7 @@ pub(crate) fn create_backing_texture(
&texture_extent,
surface_size,
render_texture_format,
clear_color,
);

let texture_format_size = get_texture_format_size(backing_texture_format);
Expand Down
22 changes: 22 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,27 @@ impl Pixels {
.await
}

/// Change the clear color.
///
/// Allows customization of the background color and the border drawn for non-integer scale
/// values.
///
/// ```no_run
/// use pixels::wgpu::Color;
///
/// # use pixels::Pixels;
/// # let window = pixels_mocks::Rwh;
/// # let surface_texture = pixels::SurfaceTexture::new(320, 240, &window);
/// let mut pixels = Pixels::new(320, 240, surface_texture)?;
///
/// // Set clear color to red.
/// pixels.set_clear_color(Color::RED);
/// # Ok::<(), pixels::Error>(())
/// ```
pub fn set_clear_color(&mut self, color: wgpu::Color) {
self.context.scaling_renderer.clear_color = color;
}

/// Resize the pixel buffer and zero its contents.
///
/// This does not resize the surface upon which the pixel buffer texture is rendered. Use
Expand Down Expand Up @@ -262,6 +283,7 @@ impl Pixels {
// Render texture values
&self.surface_size,
self.render_texture_format,
self.context.scaling_renderer.clear_color,
);

self.scaling_matrix_inverse = scaling_matrix_inverse;
Expand Down
10 changes: 5 additions & 5 deletions src/renderers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct ScalingRenderer {
uniform_buffer: wgpu::Buffer,
bind_group: wgpu::BindGroup,
render_pipeline: wgpu::RenderPipeline,
pub(crate) clear_color: wgpu::Color,
width: f32,
height: f32,
clip_rect: (u32, u32, u32, u32),
Expand All @@ -21,6 +22,7 @@ impl ScalingRenderer {
texture_size: &wgpu::Extent3d,
surface_size: &SurfaceSize,
render_texture_format: wgpu::TextureFormat,
clear_color: wgpu::Color,
) -> Self {
let shader = wgpu::include_wgsl!("../shaders/scale.wgsl");
let module = device.create_shader_module(&shader);
Expand Down Expand Up @@ -153,10 +155,7 @@ impl ScalingRenderer {
entry_point: "fs_main",
targets: &[wgpu::ColorTargetState {
format: render_texture_format,
blend: Some(wgpu::BlendState {
color: wgpu::BlendComponent::REPLACE,
alpha: wgpu::BlendComponent::REPLACE,
}),
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
write_mask: wgpu::ColorWrites::ALL,
}],
}),
Expand All @@ -170,6 +169,7 @@ impl ScalingRenderer {
uniform_buffer,
bind_group,
render_pipeline,
clear_color,
width: texture_size.width as f32,
height: texture_size.height as f32,
clip_rect,
Expand All @@ -184,7 +184,7 @@ impl ScalingRenderer {
view: render_target,
resolve_target: None,
ops: wgpu::Operations {
load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
load: wgpu::LoadOp::Clear(self.clear_color),
store: true,
},
}],
Expand Down