threers 0.1.0

Drop-in three.js replacement for Rust and WebAssembly (wgpu) — THREE.* API, scene graph, PBR, post-fx, mesh BVH, CSG, an OpenSCAD front end with a watertight exact-CSG kernel, headless render, and native media codecs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
//! Rendering into a native window or child view someone else owns — a Tauri
//! or Wry child window beside a web view, a tao or winit window, an HWND or
//! an X11/Wayland surface handed over raw — on one device shared by every
//! viewport, from any thread.
//!
//! [`SurfaceGpu`] is the device: open it once, and every [`SurfaceRenderer`]
//! on it shares meshes, shaders and pipelines, so the second viewport costs
//! no compilation the first did not. Each renderer is `Send`: make it where
//! the window is, then move it to the thread that draws.
//!
//! ```ignore
//! // The cache goes anywhere: a directory (`PathBuf`), a `FileStore`, a
//! // `MemoryStore`, a `CallbackStore` into the app's own storage.
//! let gpu = SurfaceGpu::new()?.with_pipeline_cache(app_cache_dir.join("gpu"));
//! // On the main thread (macOS requires it): `window` is anything with
//! // raw-window-handle 0.6 window and display handles — a `tauri::Window`,
//! // a tao or winit window in an `Arc`.
//! let mut view = SurfaceRenderer::new(&gpu, window.clone(), w, h, SurfaceOptions::default().lazy(true))?;
//! view.renderer().warm_up().run();       // or on worker threads
//! gpu.save_pipeline_cache()?;
//! std::thread::spawn(move || loop { view.render(&mut scene, &camera); });
//! ```
//!
//! The device comes from [`GpuContext::headless`](crate::gpu::GpuContext),
//! with no display handle: right for Vulkan, Metal and DX12, which are what
//! desktop GPUs get. GL on Wayland needs one; open a
//! [`crate::gpu::GpuContext`] yourself and use
//! [`SurfaceGpu::from_context`].

use std::sync::{Arc, RwLock};

use super::pipeline_cache::{PipelineCache, PipelineCacheStore};
use super::{Renderer, RendererOptions};
use crate::cameras::Camera;
use crate::gpu::GpuContext;
use crate::scene::Scene;

/// The device every [`SurfaceRenderer`] draws with. `Send + Sync`.
pub struct SurfaceGpu {
    gpu: GpuContext,
    /// Frames hold it shared; a surface reconfigure holds it alone — wgpu's
    /// `Surface::configure` waits for the queue to empty and fails if another
    /// thread submits meanwhile (see `layer.rs`, which this follows).
    frames: Arc<RwLock<()>>,
    cache: Option<PipelineCache>,
}

impl SurfaceGpu {
    /// The system's high-performance adapter. Blocks briefly.
    pub fn new() -> Result<Self, String> {
        Ok(Self::from_context(GpuContext::headless_blocking()?))
    }

    /// A device you opened yourself — with a display handle, other limits or
    /// features. Ask for `Features::PIPELINE_CACHE` if you want
    /// [`with_pipeline_cache`](Self::with_pipeline_cache) to do anything
    /// (`GpuContext` asks for it wherever there is one).
    pub fn from_context(gpu: GpuContext) -> Self {
        Self {
            gpu,
            frames: Arc::new(RwLock::new(())),
            cache: None,
        }
    }

    /// Build every viewport's pipelines through the cache `store` holds — a
    /// directory (`PathBuf`), [`FileStore`](super::FileStore),
    /// [`MemoryStore`](super::MemoryStore),
    /// [`CallbackStore`](super::CallbackStore) or your own
    /// [`PipelineCacheStore`]. Written on
    /// [`save_pipeline_cache`](Self::save_pipeline_cache). Nothing changes
    /// where the backend has no pipeline cache. Set it before making the
    /// viewports: each takes the cache when it is made.
    pub fn with_pipeline_cache(mut self, store: impl PipelineCacheStore + 'static) -> Self {
        self.cache = PipelineCache::open(&self.gpu.device, &self.gpu.adapter.get_info(), store);
        self
    }

    /// A [`PipelineCache`] opened yourself (it must be on this device).
    pub fn with_opened_pipeline_cache(mut self, cache: Option<PipelineCache>) -> Self {
        self.cache = cache;
        self
    }

    /// Write the pipeline cache — after a warm-up, or on exit. `Ok(false)`
    /// with no cache, or nothing in it.
    pub fn save_pipeline_cache(&self) -> std::io::Result<bool> {
        match &self.cache {
            Some(cache) => cache.save(),
            None => Ok(false),
        }
    }

    /// The cache, when there is one: for its [`data`](PipelineCache::data),
    /// or to build pipelines of your own through it.
    pub fn pipeline_cache(&self) -> Option<&PipelineCache> {
        self.cache.as_ref()
    }

    pub fn context(&self) -> &GpuContext {
        &self.gpu
    }

    pub fn device(&self) -> &Arc<wgpu::Device> {
        &self.gpu.device
    }

    pub fn queue(&self) -> &Arc<wgpu::Queue> {
        &self.gpu.queue
    }
}

/// How a [`SurfaceRenderer`] draws.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct SurfaceOptions {
    /// 1 (off) or 4.
    pub msaa: u32,
    /// Build pipelines on first use; see [`Renderer::new_lazy`] and
    /// [`Renderer::warm_up`].
    pub lazy: bool,
    /// `AutoVsync` by default: display-paced, so a render thread drawing
    /// several viewports blocks on each one's next image and never spins.
    pub present_mode: wgpu::PresentMode,
    /// Composite with what is behind the window (premultiplied alpha), where
    /// the platform offers it.
    pub transparent: bool,
}

impl Default for SurfaceOptions {
    fn default() -> Self {
        Self {
            msaa: 4,
            lazy: false,
            present_mode: wgpu::PresentMode::AutoVsync,
            transparent: false,
        }
    }
}

impl SurfaceOptions {
    pub fn msaa(mut self, samples: u32) -> Self {
        self.msaa = samples;
        self
    }

    pub fn lazy(mut self, lazy: bool) -> Self {
        self.lazy = lazy;
        self
    }

    pub fn present_mode(mut self, mode: wgpu::PresentMode) -> Self {
        self.present_mode = mode;
        self
    }

    pub fn transparent(mut self, transparent: bool) -> Self {
        self.transparent = transparent;
        self
    }
}

/// How to make the surface again when the platform reports it lost.
enum Target {
    Safe(Box<dyn Fn() -> wgpu::SurfaceTarget<'static> + Send>),
    Raw(RawHandles),
}

struct RawHandles {
    display: Option<wgpu::rwh::RawDisplayHandle>,
    window: wgpu::rwh::RawWindowHandle,
}

// SAFETY: the handles are only used to create a surface on the window
// `SurfaceRenderer::from_raw` was given, which its contract keeps alive; the
// raw pointers inside are what wgpu's own surface already holds and uses from
// whichever thread renders.
unsafe impl Send for RawHandles {}

/// A [`Renderer`] presenting to one window or view.
pub struct SurfaceRenderer {
    // Dropped first: the surface before the window it may own (`Target::Safe`).
    renderer: Renderer,
    surface: wgpu::Surface<'static>,
    target: Target,
    config: wgpu::SurfaceConfiguration,
    gpu: GpuContext,
    frames: Arc<RwLock<()>>,
    /// The last configure failed; try again before the next frame.
    unconfigured: bool,
    /// A warm-up [`render`](Self::render) waits for.
    warming: Option<super::PipelineWarmup>,
}

impl SurfaceRenderer {
    /// Draw into `window` at `width × height` physical pixels.
    ///
    /// `window` is anything with raw-window-handle 0.6 window and display
    /// handles that can be cloned — a `tauri::Window`, an `Arc` of a tao or
    /// winit window. The renderer keeps a clone, so the window outlives it.
    ///
    /// On macOS, call this on the main thread (wgpu reads the view's layer);
    /// the renderer can then move to any thread.
    pub fn new<W>(
        gpu: &SurfaceGpu,
        window: W,
        width: u32,
        height: u32,
        options: SurfaceOptions,
    ) -> Result<Self, String>
    where
        W: wgpu::WindowHandle + wgpu::rwh::HasDisplayHandle + Clone + 'static,
    {
        let make = move || wgpu::SurfaceTarget::from(window.clone());
        let surface = gpu
            .gpu
            .instance
            .create_surface(make())
            .map_err(|e| format!("window surface: {e}"))?;
        Self::finish(
            gpu,
            surface,
            Target::Safe(Box::new(make)),
            width,
            height,
            options,
        )
    }

    /// [`new`](Self::new) from raw handles — what a framework that does not
    /// expose its window type, or a C host, hands over.
    ///
    /// # Safety
    /// The handles must be valid and the window they name must outlive the
    /// renderer. On macOS, call this on the main thread.
    pub unsafe fn from_raw(
        gpu: &SurfaceGpu,
        display: Option<wgpu::rwh::RawDisplayHandle>,
        window: wgpu::rwh::RawWindowHandle,
        width: u32,
        height: u32,
        options: SurfaceOptions,
    ) -> Result<Self, String> {
        let target = wgpu::SurfaceTargetUnsafe::RawHandle {
            raw_display_handle: display,
            raw_window_handle: window,
        };
        // SAFETY: the caller's contract.
        let surface = unsafe { gpu.gpu.instance.create_surface_unsafe(target) }
            .map_err(|e| format!("window surface: {e}"))?;
        Self::finish(
            gpu,
            surface,
            Target::Raw(RawHandles { display, window }),
            width,
            height,
            options,
        )
    }

    fn finish(
        gpu: &SurfaceGpu,
        surface: wgpu::Surface<'static>,
        target: Target,
        width: u32,
        height: u32,
        options: SurfaceOptions,
    ) -> Result<Self, String> {
        #[cfg(any(
            target_os = "macos",
            target_os = "ios",
            target_os = "tvos",
            target_os = "visionos"
        ))]
        let _pool = super::layer::Pool::new();
        let (width, height) = (width.max(1), height.max(1));
        let caps = surface.get_capabilities(&gpu.gpu.adapter);
        if caps.formats.is_empty() {
            return Err(format!(
                "the adapter ({}) cannot present to this window — open a GpuContext with a \
                 compatible surface and use SurfaceGpu::from_context",
                gpu.gpu.adapter.get_info().name
            ));
        }
        let format = caps
            .formats
            .iter()
            .copied()
            .find(|f| f.is_srgb())
            .unwrap_or(caps.formats[0]);
        let alpha_preference: &[wgpu::CompositeAlphaMode] = if options.transparent {
            &[
                wgpu::CompositeAlphaMode::PreMultiplied,
                wgpu::CompositeAlphaMode::PostMultiplied,
            ]
        } else {
            &[wgpu::CompositeAlphaMode::Opaque]
        };
        let alpha_mode = alpha_preference
            .iter()
            .chain(&[wgpu::CompositeAlphaMode::Auto])
            .copied()
            .find(|m| caps.alpha_modes.contains(m))
            .unwrap_or(caps.alpha_modes[0]);
        let present_mode = if caps.present_modes.contains(&options.present_mode)
            || matches!(
                options.present_mode,
                wgpu::PresentMode::AutoVsync | wgpu::PresentMode::AutoNoVsync
            ) {
            options.present_mode
        } else {
            wgpu::PresentMode::Fifo
        };
        let config = wgpu::SurfaceConfiguration {
            usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
            format,
            width,
            height,
            present_mode,
            alpha_mode,
            view_formats: vec![],
            desired_maximum_frame_latency: 2,
            color_space: wgpu::SurfaceColorSpace::Srgb,
        };
        let renderer_options = RendererOptions::default()
            .lazy(options.lazy)
            .pipeline_cache(&gpu.cache);
        let mut renderer = Renderer::with_options(
            Arc::clone(&gpu.gpu.device),
            Arc::clone(&gpu.gpu.queue),
            format,
            width,
            height,
            renderer_options,
        );
        renderer.set_msaa(options.msaa);
        let mut this = Self {
            renderer,
            surface,
            target,
            config,
            gpu: GpuContext {
                instance: gpu.gpu.instance.clone(),
                adapter: gpu.gpu.adapter.clone(),
                device: Arc::clone(&gpu.gpu.device),
                queue: Arc::clone(&gpu.gpu.queue),
            },
            frames: gpu.frames.clone(),
            unconfigured: true,
            warming: None,
        };
        this.configure();
        Ok(this)
    }

    /// New size in physical pixels; no-op when unchanged.
    pub fn resize(&mut self, width: u32, height: u32) {
        let (width, height) = (width.max(1), height.max(1));
        if (width, height) == (self.config.width, self.config.height) {
            return;
        }
        #[cfg(any(
            target_os = "macos",
            target_os = "ios",
            target_os = "tvos",
            target_os = "visionos"
        ))]
        let _pool = super::layer::Pool::new();
        self.config.width = width;
        self.config.height = height;
        self.configure();
        self.renderer.resize(width, height);
    }

    /// Apply `config`, with every other viewport on the device between
    /// frames. A failure is caught rather than left to wgpu's panic, and
    /// retried before the next frame.
    fn configure(&mut self) -> bool {
        let _alone = self.frames.write().unwrap_or_else(|e| e.into_inner());
        // Error scopes are per thread in wgpu, so this catches this
        // configure's error and nobody else's.
        let scope = self
            .gpu
            .device
            .push_error_scope(wgpu::ErrorFilter::Validation);
        self.surface.configure(&self.gpu.device, &self.config);
        self.unconfigured = pollster::block_on(scope.pop()).is_some();
        !self.unconfigured
    }

    pub fn size(&self) -> (u32, u32) {
        (self.config.width, self.config.height)
    }

    /// The surface's format, which the renderer draws in.
    pub fn format(&self) -> wgpu::TextureFormat {
        self.config.format
    }

    /// Build the pipelines on `threads` background threads, and make
    /// [`render`](Self::render) return `false` until they are done, so the
    /// first frame anyone sees does not stall on compilation. The caller shows
    /// its own loading state meanwhile; `on_progress` on the returned job
    /// feeds it. `states` are the material states the app draws with (see
    /// [`Renderer::warm_up_states`]); an eager renderer has nothing to warm
    /// and is not held. After the job, check
    /// [`errors`](super::PipelineWarmup::errors).
    #[cfg(not(target_arch = "wasm32"))]
    pub fn warm_in_background(
        &mut self,
        threads: usize,
        states: &[crate::materials::RenderState],
    ) -> super::PipelineWarmup {
        let warmup = self.renderer.warm_up_states(states);
        warmup.spawn(threads);
        self.warming = (!warmup.is_ready()).then(|| warmup.clone());
        warmup
    }

    /// Whether [`render`](Self::render) is waiting on a
    /// [`warm_in_background`](Self::warm_in_background) job.
    pub fn is_warming(&self) -> bool {
        self.warming.as_ref().is_some_and(|w| !w.is_ready())
    }

    /// Draw before the background warm-up has finished, taking the stall on
    /// the frames that need a pipeline the job has not reached.
    pub fn stop_waiting_for_warm_up(&mut self) {
        self.warming = None;
    }

    /// Render and present one frame. `false`: no image this time (minimised,
    /// occluded, every image in flight, the surface was just remade, or a
    /// [`warm_in_background`](Self::warm_in_background) job is still
    /// running), and the next call draws. Do not spin on `false`: sleep a
    /// frame (or wait for the window's next redraw request) before trying
    /// again, or a minimised window costs a core.
    pub fn render(&mut self, scene: &mut Scene, camera: &dyn Camera) -> bool {
        use wgpu::CurrentSurfaceTexture as Acquired;
        if self.is_warming() {
            return false;
        }
        self.warming = None;
        #[cfg(any(
            target_os = "macos",
            target_os = "ios",
            target_os = "tvos",
            target_os = "visionos"
        ))]
        let _pool = super::layer::Pool::new();
        if self.unconfigured && !self.configure() {
            return false;
        }
        match self.surface.get_current_texture() {
            Acquired::Success(frame) => {
                self.draw(scene, camera, frame);
                true
            }
            Acquired::Suboptimal(frame) => {
                self.draw(scene, camera, frame);
                self.unconfigured = true;
                true
            }
            Acquired::Timeout | Acquired::Occluded => false,
            Acquired::Outdated => {
                self.configure();
                false
            }
            Acquired::Lost => {
                self.remake();
                self.configure();
                false
            }
            _ => false,
        }
    }

    /// A new surface on the same window. Not on Apple platforms, where it
    /// has to happen on the main thread and this may be a render thread; a
    /// reconfigure is what Metal needs there.
    fn remake(&mut self) {
        if cfg!(any(
            target_os = "macos",
            target_os = "ios",
            target_os = "tvos",
            target_os = "visionos"
        )) {
            return;
        }
        let surface = match &self.target {
            Target::Safe(make) => self.gpu.instance.create_surface(make()),
            Target::Raw(raw) => {
                let target = wgpu::SurfaceTargetUnsafe::RawHandle {
                    raw_display_handle: raw.display,
                    raw_window_handle: raw.window,
                };
                // SAFETY: the handles `from_raw` was given, which its
                // contract keeps valid for as long as this renderer.
                unsafe { self.gpu.instance.create_surface_unsafe(target) }
            }
        };
        if let Ok(surface) = surface {
            self.surface = surface;
        }
    }

    fn draw(&mut self, scene: &mut Scene, camera: &dyn Camera, frame: wgpu::SurfaceTexture) {
        // Shared: viewports draw in parallel, and only a reconfigure waits.
        let _frame = self.frames.read().unwrap_or_else(|e| e.into_inner());
        let view = frame
            .texture
            .create_view(&wgpu::TextureViewDescriptor::default());
        self.renderer.render(scene, camera, &view, false);
        self.gpu.queue.present(frame);
    }

    /// The renderer, for its settings, [`warm_up`](Renderer::warm_up) and
    /// [`warm_scene`](Renderer::warm_scene).
    pub fn renderer(&mut self) -> &mut Renderer {
        &mut self.renderer
    }
}

// Compile-time: what this module promises about threads.
const _: () = {
    const fn send_sync<T: Send + Sync>() {}
    const fn send<T: Send>() {}
    send_sync::<SurfaceGpu>();
    send::<SurfaceRenderer>();
};