rust-windowing / softbuffer

Easily write an image to a window
Apache License 2.0
292 stars 45 forks source link

using external buffer possible? #222

Open ericlangedijk opened 1 week ago

ericlangedijk commented 1 week ago

Currently I am copying my own rgba into the buffer of the softbuffer-surface. I was wondering if that could be faster. And if it would be possible that the softbuffer uses this external buffer as source. I saw that in Windows the drawing is done with BitBlt (which I believe is hardware-accelerated) and also was wondering if StretchDIBits would be an option. The last one does not need a source-handle, but don't know if StretchDIBits is as optimized.

ids1024 commented 1 week ago

Could your program be refactored to write into the softbuffer::Buffer instead of a separate buffer?

At least on some backends, that should perform better than any possible implementation that presents from just a &[u8] or &[u32]. Not sure about the performance of StretchDIBits, BitBlt, and such. (Other than that SDL uses BitBlt with a comment that when implemented it ended up being faster than trying to use DirectX.)

ericlangedijk commented 1 week ago

Yes BitBlt is fast. I was thinking about your suggestion as well. I could probably rewrite my home-made Bitmap (with all kinds of draw functions) to write to a 'backend' instead of its own Vec<Rgba. Lot of work but doable. It saves a copy of some megabytes each frame....

notgull commented 1 week ago

Many drawing interfaces have a way to render to a mutable byte slice. For instance in my own code I use tiny-skia::PixmapMut around the softbuffer buffer in order to render in software to the window.

daxpedda commented 6 days ago

I would be in favor of such an addition, but I don't know the limitations of other backends.

For Web at least, it should be perfectly possible after we implement the pixel format API. However it is not possible with the atomics target feature, where its not possible at all to draw from Wasm memory to begin with.

ericlangedijk commented 6 days ago

Yes I think for certain backends it will not be possible or handy. No clue about other systems. On windows we have a u32 array which happily has exactly the same layout as my inner bitmap on which i perform all my (cpu) drawing logic buffer scaling and rendering.

daxpedda commented 6 days ago

Maybe some platform-specific extensions would be more appropriate then.

ids1024 commented 4 days ago

If you want to write something specifically for Windows and have some very specific requirements, you can also always just do that directly with windows-rs instead of using Softbuffer.

I guess it's an open design question in softbuffer to decide when to offer platform extensions vs suggest using platform APIs directly.