From a55352911bb26a1b33bc8d5fc03bdebb0a967f5a Mon Sep 17 00:00:00 2001 From: Horu <73709188+HigherOrderLogic@users.noreply.github.com> Date: Fri, 6 Nov 2026 09:47:31 +0700 Subject: [PATCH] Make `StagingBelt` available on `no_std` (#10699) --- Cargo.lock | 1 + Cargo.toml | 1 + wgpu/Cargo.toml | 2 ++ wgpu/src/util/belt.rs | 52 +++++++++++++------------------------------ wgpu/src/util/mod.rs | 4 ---- 5 files changed, 19 insertions(+), 41 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 406f4548f..ba4073fbd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4799,6 +4799,7 @@ dependencies = [ "bytemuck", "cfg-if", "cfg_aliases", + "concurrent-queue", "document-features", "hashbrown 0.17.1", "js-sys", diff --git a/Cargo.toml b/Cargo.toml index 24fd561c7..23fd5a961 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index f175f0dec..81cf64447 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -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 diff --git a/wgpu/src/util/belt.rs b/wgpu/src/util/belt.rs index 7d0ff3535..424b95728 100644 --- a/wgpu/src/util/belt.rs +++ b/wgpu/src/util/belt.rs @@ -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, /// When closed chunks are mapped again, the map callback sends them here. - sender: Exclusive>, - /// Free chunks are received here to be put on `self.free_chunks`. - receiver: Exclusive>, + /// Free chunks are retrieved from this to be put on `self.free_chunks`. + queue: Arc>, /// 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 - pub(super) struct Exclusive(T); - - /// Safety: `&Exclusive` has no operations. - unsafe impl Sync for Exclusive {} - - impl Exclusive { - pub fn new(value: T) -> Self { - Self(value) - } - - pub fn get_mut(&mut self) -> &mut T { - &mut self.0 - } - } -} diff --git a/wgpu/src/util/mod.rs b/wgpu/src/util/mod.rs index 6284d329e..40a59055b 100644 --- a/wgpu/src/util/mod.rs +++ b/wgpu/src/util/mod.rs @@ -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;