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

Optimization: internally track buffer mutations #390

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::renderers::{ScalingMatrix, ScalingRenderer};
use crate::{Error, Pixels, PixelsContext, SurfaceSize, SurfaceTexture, TextureError};
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};
use std::cell::Cell;

/// A builder to help create customized pixel buffers.
pub struct PixelsBuilder<'req, 'dev, 'win, W: HasRawWindowHandle + HasRawDisplayHandle> {
Expand Down Expand Up @@ -342,6 +343,7 @@ impl<'req, 'dev, 'win, W: HasRawWindowHandle + HasRawDisplayHandle>
surface_texture_format,
blend_state,
pixels,
dirty: Cell::new(false),
scaling_matrix_inverse,
alpha_mode,
};
Expand Down
44 changes: 27 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub use crate::builder::{check_texture_size, PixelsBuilder};
pub use crate::renderers::ScalingRenderer;
pub use raw_window_handle;
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle};
use std::cell::Cell;
use thiserror::Error;
pub use wgpu;

Expand Down Expand Up @@ -103,6 +104,7 @@ pub struct Pixels {

// Pixel buffer
pixels: Vec<u8>,
dirty: Cell<bool>,

// The inverse of the scaling matrix used by the renderer
// Used to convert physical coordinates back to pixel coordinates (for the mouse)
Expand Down Expand Up @@ -333,6 +335,7 @@ impl Pixels {
// Resize the pixel buffer
self.pixels
.resize_with(pixels_buffer_size, Default::default);
self.dirty.set(true);

Ok(())
}
Expand Down Expand Up @@ -514,23 +517,27 @@ impl Pixels {
});

// Update the pixel buffer texture view
let bytes_per_row =
(self.context.texture_extent.width as f32 * self.context.texture_format_size) as u32;
self.context.queue.write_texture(
wgpu::ImageCopyTexture {
texture: &self.context.texture,
mip_level: 0,
origin: wgpu::Origin3d { x: 0, y: 0, z: 0 },
aspect: wgpu::TextureAspect::All,
},
&self.pixels,
wgpu::ImageDataLayout {
offset: 0,
bytes_per_row: Some(bytes_per_row),
rows_per_image: Some(self.context.texture_extent.height),
},
self.context.texture_extent,
);
if self.dirty.get() {
self.dirty.set(false);

let bytes_per_row = (self.context.texture_extent.width as f32
* self.context.texture_format_size) as u32;
self.context.queue.write_texture(
wgpu::ImageCopyTexture {
texture: &self.context.texture,
mip_level: 0,
origin: wgpu::Origin3d { x: 0, y: 0, z: 0 },
aspect: wgpu::TextureAspect::All,
},
&self.pixels,
wgpu::ImageDataLayout {
offset: 0,
bytes_per_row: Some(bytes_per_row),
rows_per_image: Some(self.context.texture_extent.height),
},
self.context.texture_extent,
);
}

let view = frame
.texture
Expand Down Expand Up @@ -565,6 +572,9 @@ impl Pixels {
/// Get a mutable byte slice for the pixel buffer. The buffer is _not_ cleared for you; it will
/// retain the previous frame's contents until you clear it yourself.
pub fn frame_mut(&mut self) -> &mut [u8] {
// Optimistically assume the caller will change the buffer when acquiring mutable access.
self.dirty.set(true);

&mut self.pixels
}

Expand Down
Loading