The native runtime for three.js: three.js, down to the metal. 45× smaller than Electron, on a Sony PS3, and path-traced at 60 fps on ray-tracing GPUs.
Rust electron-alternativegame-engineglobal-illuminationgraphicsjavascript-runtimemetalpath-tracingray-tracingreal-time-renderingrenderingrustthreejstypescriptvoxelvulkanwebgpuwgpu
//! The window's current drawable as the runtime's primary display.
//!
//! Each frame the host hands the display the texture the window presents next
//! ([`SurfaceDisplay::set_frame`]); the runtime's present then draws the UI's
//! DrawList into it on top of what the game drew (or over a clear colour when
//! the game drew nothing), and the host presents the drawable.
use std::cell::RefCell;
use std::rc::Rc;
use pocket_drivers::target::{RendererCell, TargetSlot};
use pocket_drivers::{
DisplayDescriptor, DisplayError, DisplayOutput, DisplayRole, PixelFormat, Rect, Target,
TargetOwnership, VsyncSource,
};
use pocket_wgpu_render::{TargetConfig, WgpuRenderer};
pub(crate) struct DisplayState {
pub size: (u32, u32),
pub scale: f32,
pub pixel_format: PixelFormat,
pub target: TargetSlot<WgpuRenderer>,
pub ready: bool,
pub pending: Option<u64>,
pub next_token: u64,
}
impl DisplayState {
pub fn new(renderer: &RendererCell<WgpuRenderer>, size: (u32, u32), scale: f32, pixel_format: PixelFormat) -> Self {
DisplayState { size, scale, pixel_format, target: TargetSlot::new(renderer.port()), ready: false, pending: None, next_token: 0 }
}
/// Draw the next present into `view` (`size` device px), loading what it holds or clearing it.
pub fn set_frame(&mut self, view: &wgpu::TextureView, size: (u32, u32), scale: f32, clear: Option<wgpu::Color>) -> Result<(), String> {
if self.pending.is_some() {
return Err("display transfer is still pending".into());
}
self.size = size;
self.scale = scale;
let load = match clear {
Some(color) => wgpu::LoadOp::Clear(color),
None => wgpu::LoadOp::Load,
};
self.target.replace(TargetConfig::new(view, size, scale, load)).map_err(|e| format!("wgpu target: {e:?}"))?;
self.ready = true;
Ok(())
}
}
pub(crate) struct SurfaceDisplay(pub Rc<RefCell<DisplayState>>);
impl DisplayOutput for SurfaceDisplay {
type Token = u64;
fn descriptor(&self) -> DisplayDescriptor {
let state = self.0.borrow();
DisplayDescriptor::new(DisplayRole::Primary, state.size.0, state.size.1, state.pixel_format, state.size.0 as usize * 4, TargetOwnership::DeviceOwned)
}
fn acquire(&mut self, region: Rect) -> Result<Target<'_>, DisplayError> {
let state = self.0.borrow();
if state.pending.is_some() || !state.ready {
return Err(DisplayError::InvalidTarget);
}
let target = state.target.target().map_err(|_| DisplayError::InvalidTarget)?;
if target.region != region {
return Err(DisplayError::InvalidTarget);
}
Ok(Target::DeviceOwned(target))
}
fn flush(&mut self, _: &[Rect]) -> Result<u64, DisplayError> {
let mut state = self.0.borrow_mut();
if state.pending.is_some() || !state.ready {
return Err(DisplayError::InvalidTarget);
}
state.next_token = state.next_token.checked_add(1).ok_or(DisplayError::Failed)?;
let token = state.next_token;
state.pending = Some(token);
Ok(token)
}
fn wait(&mut self, token: u64) -> Result<(), DisplayError> {
let mut state = self.0.borrow_mut();
if state.pending != Some(token) {
return Err(DisplayError::InvalidTarget);
}
state.pending = None;
// the drawable goes to the window after this present; the next frame brings another
state.ready = false;
let _ = state.target.release();
Ok(())
}
fn vsync(&mut self) -> VsyncSource {
VsyncSource::None
}
}