Fall back to synchronous rendering on Wasm

This commit is contained in:
Héctor Ramón Jiménez 2025-10-24 18:14:29 +02:00
parent 47a3fc0b9a
commit 5216253998
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
2 changed files with 25 additions and 11 deletions

View file

@ -198,10 +198,17 @@ impl<P: Program> Application<P> {
#[cfg(feature = "tester")] #[cfg(feature = "tester")]
let program = iced_tester::attach(self); let program = iced_tester::attach(self);
#[cfg(all(feature = "debug", not(feature = "tester")))] #[cfg(all(
feature = "debug",
not(feature = "tester"),
not(target_arch = "wasm32")
))]
let program = iced_devtools::attach(self); let program = iced_devtools::attach(self);
#[cfg(not(any(feature = "tester", feature = "debug")))] #[cfg(not(any(
feature = "tester",
all(feature = "debug", not(target_arch = "wasm32"))
)))]
let program = self; let program = self;
Ok(shell::run(program)?) Ok(shell::run(program)?)

View file

@ -17,7 +17,7 @@ pub struct Cache {
jobs: mpsc::SyncSender<Job>, jobs: mpsc::SyncSender<Job>,
#[cfg(feature = "image")] #[cfg(feature = "image")]
work: mpsc::Receiver<Work>, work: mpsc::Receiver<Work>,
#[cfg(feature = "image")] #[cfg(all(feature = "image", not(target_arch = "wasm32")))]
worker_: Option<thread::JoinHandle<()>>, worker_: Option<thread::JoinHandle<()>>,
} }
@ -29,11 +29,14 @@ impl Cache {
layout: wgpu::BindGroupLayout, layout: wgpu::BindGroupLayout,
shell: &Shell, shell: &Shell,
) -> Self { ) -> Self {
#[cfg(feature = "image")] #[cfg(all(feature = "image", not(target_arch = "wasm32")))]
let (worker, jobs, work) = let (worker, jobs, work) =
Worker::new(device, queue, backend, layout.clone(), shell); Worker::new(device, queue, backend, layout.clone(), shell);
#[cfg(feature = "image")] #[cfg(all(feature = "image", target_arch = "wasm32"))]
let (jobs, work) = (mpsc::sync_channel(0).0, mpsc::sync_channel(0).1);
#[cfg(all(feature = "image", not(target_arch = "wasm32")))]
let handle = thread::spawn(move || worker.run()); let handle = thread::spawn(move || worker.run());
Self { Self {
@ -50,7 +53,7 @@ impl Cache {
jobs, jobs,
#[cfg(feature = "image")] #[cfg(feature = "image")]
work, work,
#[cfg(feature = "image")] #[cfg(all(feature = "image", not(target_arch = "wasm32")))]
worker_: Some(handle), worker_: Some(handle),
} }
} }
@ -107,7 +110,8 @@ impl Cache {
const MAX_SYNC_SIZE: usize = 2 * 1024 * 1024; const MAX_SYNC_SIZE: usize = 2 * 1024 * 1024;
if image.len() < MAX_SYNC_SIZE { // TODO: Concurrent Wasm support
if image.len() < MAX_SYNC_SIZE || cfg!(target_arch = "wasm32") {
let entry = self.atlas.upload( let entry = self.atlas.upload(
device, device,
encoder, encoder,
@ -205,7 +209,7 @@ impl Cache {
} }
} }
#[cfg(feature = "image")] #[cfg(all(feature = "image", not(target_arch = "wasm32")))]
impl Drop for Cache { impl Drop for Cache {
fn drop(&mut self) { fn drop(&mut self) {
// Stop worker gracefully // Stop worker gracefully
@ -235,8 +239,11 @@ fn load_image<'a>(
use crate::image::raster::Memory; use crate::image::raster::Memory;
if !cache.contains(handle) { if !cache.contains(handle) {
// Load RGBA handles synchronously, since it's very cheap if cfg!(target_arch = "wasm32") {
if let core::image::Handle::Rgba { .. } = handle { // TODO: Concurrent support for Wasm
cache.insert(handle, Memory::load(handle));
} else if let core::image::Handle::Rgba { .. } = handle {
// Load RGBA handles synchronously, since it's very cheap
cache.insert(handle, Memory::load(handle)); cache.insert(handle, Memory::load(handle));
} else { } else {
let _ = jobs.send(Job::Load(handle.clone())); let _ = jobs.send(Job::Load(handle.clone()));
@ -284,7 +291,7 @@ struct Worker {
output: mpsc::SyncSender<Work>, output: mpsc::SyncSender<Work>,
} }
#[cfg(feature = "image")] #[cfg(all(feature = "image", not(target_arch = "wasm32")))]
impl Worker { impl Worker {
fn new( fn new(
device: &wgpu::Device, device: &wgpu::Device,