Skip to content

Allow reading from buffers#320

Open
madsmtm wants to merge 1 commit intomasterfrom
madsmtm/allow-reading-buffer
Open

Allow reading from buffers#320
madsmtm wants to merge 1 commit intomasterfrom
madsmtm/allow-reading-buffer

Conversation

@madsmtm
Copy link
Member

@madsmtm madsmtm commented Jan 23, 2026

In #313, I added a note that reading from the pixel buffer is discouraged, because I wanted to set kIOSurfaceMapWriteCombineCache on macOS once I got around to implementing proper IOSurface support. See e.g. the following links for a few places where it's used in the wild:

I thought this was fine, because I couldn't see a use-case where you'd need to read from the pixel buffer, but that was perhaps a bit hasty, I found at least one use-case: converting the alpha mode (premultiplying components or making the alpha channel opaque) by modifying the buffer in-place in a separate step before presenting it buffer.

This means that the argument for only having the mutable version instead of distinct pixels/pixels_mut, pixel_rows/pixel_rows_mut and pixels_iter/pixels_iter_mut methods is a bit weaker.

WDYT? Should we provide both immutable and mutable methods on Buffer<'_>?

@madsmtm madsmtm added documentation Improvements or additions to documentation enhancement New feature or request DS - CoreGraphics macOS/iOS/tvOS/watchOS/visionOS backend labels Jan 23, 2026
@madsmtm madsmtm changed the title Add API for making buffers write-only and allow reading from them by default Add API for making buffers write-only, and allow reading from them by default Jan 23, 2026
src/lib.rs Outdated
/// Set the buffer as optimized for only being written to.
///
/// Setting this allows the underlying storage to bypass certain caches and reduce cache
/// pollution. In turn, this may make reading from the buffer data perform very poorly.
Copy link
Member

Choose a reason for hiding this comment

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

So it's not really "write only", but more optimized for not reading? Though I guess it should generally be treated the same...

Not sure if any other name would be more appropriate for this method. Maybe not.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe write_optimized? Or read_penalized?

@madsmtm
Copy link
Member Author

madsmtm commented Feb 12, 2026

I just found gfx-rs/wgpu#8897, we might have to make this method unsafe because the write-combined memory seems to work weirdly with atomics? I'm unfamiliar with how write-combined memory actually works.

Disallowing reading is overly restrictive, and we don't use
write-combined memory anywhere yet anyhow.
@madsmtm madsmtm force-pushed the madsmtm/allow-reading-buffer branch from 2ac7aa8 to 51f29e0 Compare March 18, 2026 14:32
@madsmtm madsmtm changed the title Add API for making buffers write-only, and allow reading from them by default Allow reading from buffers Mar 18, 2026
@madsmtm
Copy link
Member Author

madsmtm commented Mar 18, 2026

I've changed this PR to only be a docs update to allow reading from the buffer, we can reconsider the set_write_only API later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation DS - CoreGraphics macOS/iOS/tvOS/watchOS/visionOS backend enhancement New feature or request

Development

Successfully merging this pull request may close these issues.

2 participants