Make StagingBelt available on no_std (#10699)

This commit is contained in:
Horu authored and GitHub committed 2026-11-06 02:47:31 +00:00
1 parent 2bd544d896
commit a55352911b
5 files changed
+19 -41

No files matched your search

Generated
+1
View File
@@ -4799,6 +4799,7 @@ dependencies = [
"bytemuck",
"cfg-if",
"cfg_aliases",
"concurrent-queue",
"document-features",
"hashbrown 0.17.1",
"js-sys",
+1
View File
@@ -132,6 +132,7 @@ cargo_metadata = "0.23"
cfg_aliases = "0.2.2"
cfg-if = "1"
codespan-reporting = { version = "0.13", default-features = false }
concurrent-queue = { version = "2.5.0", default-features = false }
diff = "0.1"
document-features = "0.2.10"
encase = "0.13"
+2
View File
@@ -177,6 +177,7 @@ web = ["dep:wasm-bindgen", "dep:js-sys", "dep:web-sys", "wgpu-types/web"]
## This can allow for better error reporting and for improved multithreading
## support.
std = [
"concurrent-queue/std",
"raw-window-handle/std",
"wgpu-sync/std",
"wgpu-types/std",
@@ -206,6 +207,7 @@ arrayvec.workspace = true
bitflags.workspace = true
bytemuck.workspace = true
cfg-if.workspace = true
concurrent-queue.workspace = true
document-features.workspace = true
log.workspace = true
hashbrown.workspace = true
+15 -37
View File
@@ -2,13 +2,17 @@ use crate::{
util::align_to, Buffer, BufferAddress, BufferDescriptor, BufferSize, BufferSlice, BufferUsages,
BufferViewMut, CommandEncoder, Device, MapMode,
};
use alloc::sync::Arc;
use alloc::vec::Vec;
use concurrent_queue::ConcurrentQueue;
use core::fmt;
use std::sync::mpsc;
use wgt::Features;
use crate::COPY_BUFFER_ALIGNMENT;
#[cfg(send_sync)]
static_assertions::assert_impl_all!(StagingBelt: Send, Sync);
/// Efficiently performs many buffer writes by sharing and reusing temporary buffers.
///
/// Internally it uses a ring-buffer of staging buffers that are sub-allocated.
@@ -45,9 +49,8 @@ pub struct StagingBelt {
/// into `active_chunks`.
free_chunks: Vec<Chunk>,
/// When closed chunks are mapped again, the map callback sends them here.
sender: Exclusive<mpsc::Sender<Chunk>>,
/// Free chunks are received here to be put on `self.free_chunks`.
receiver: Exclusive<mpsc::Receiver<Chunk>>,
/// Free chunks are retrieved from this to be put on `self.free_chunks`.
queue: Arc<ConcurrentQueue<Chunk>>,
/// Whether the belt has grown since last recall.
grew_since_recall: bool,
}
@@ -94,8 +97,6 @@ impl StagingBelt {
chunk_size: BufferAddress,
mut buffer_usages: BufferUsages,
) -> Self {
let (sender, receiver) = mpsc::channel();
// make sure anything other than MAP_WRITE | COPY_SRC is only allowed with MAPPABLE_PRIMARY_BUFFERS.
let extra_usages =
buffer_usages.difference(BufferUsages::MAP_WRITE | BufferUsages::COPY_SRC);
@@ -116,8 +117,7 @@ impl StagingBelt {
active_chunks: Vec::new(),
closed_chunks: Vec::new(),
free_chunks: Vec::new(),
sender: Exclusive::new(sender),
receiver: Exclusive::new(receiver),
queue: Arc::new(ConcurrentQueue::unbounded()),
grew_since_recall: false,
}
}
@@ -267,13 +267,13 @@ impl StagingBelt {
self.receive_chunks();
for chunk in self.closed_chunks.drain(..) {
let sender = self.sender.get_mut().clone();
let queue = self.queue.clone();
chunk
.buffer
.clone()
.slice(..)
.map_async(MapMode::Write, move |_| {
let _ = sender.send(chunk);
let _ = queue.push(chunk);
});
}
}
@@ -303,18 +303,18 @@ impl StagingBelt {
self.receive_chunks();
for chunk in self.closed_chunks.drain(..) {
let sender = self.sender.get_mut().clone();
let queue = self.queue.clone();
let buffer = chunk.buffer.clone();
encoder.map_buffer_on_submit(&buffer, MapMode::Write, .., move |_| {
let _ = sender.send(chunk);
let _ = queue.push(chunk);
});
}
}
/// Move all chunks that the GPU is done with (and are now mapped again)
/// from `self.receiver` to `self.free_chunks`.
/// from `self.queue` to `self.free_chunks`.
fn receive_chunks(&mut self) {
while let Ok(mut chunk) = self.receiver.get_mut().try_recv() {
while let Ok(mut chunk) = self.queue.pop() {
chunk.offset = 0;
self.free_chunks.push(chunk);
}
@@ -346,8 +346,7 @@ impl fmt::Debug for StagingBelt {
active_chunks,
closed_chunks,
free_chunks,
sender: _,
receiver: _,
queue: _,
grew_since_recall: _,
} = self;
f.debug_struct("StagingBelt")
@@ -384,24 +383,3 @@ impl Chunk {
alloc_start
}
}
use exclusive::Exclusive;
mod exclusive {
/// `Sync` wrapper that works by providing only exclusive access.
///
/// See <https://doc.rust-lang.org/nightly/std/sync/struct.Exclusive.html>
pub(super) struct Exclusive<T>(T);
/// Safety: `&Exclusive` has no operations.
unsafe impl<T> Sync for Exclusive<T> {}
impl<T> Exclusive<T> {
pub fn new(value: T) -> Self {
Self(value)
}
pub fn get_mut(&mut self) -> &mut T {
&mut self.0
}
}
}
-4
View File
@@ -3,9 +3,6 @@
//! Nothing in this module is a part of the WebGPU API specification;
//! they are unique to the `wgpu` library.
// TODO: For [`belt::StagingBelt`] to be available in `no_std` its usage of [`std::sync::mpsc`]
// must be replaced with an appropriate alternative.
#[cfg(std)]
mod belt;
mod device;
mod encoder;
@@ -17,7 +14,6 @@ mod texture_blitter;
use alloc::{format, string::String};
#[cfg(std)]
pub use belt::StagingBelt;
pub use device::{BufferInitDescriptor, DeviceExt};
pub use encoder::RenderEncoder;