use std::{sync::Arc, time::Duration};
use nsh_core::{
JsonValue, Rgba,
element::{GpuView, Registry},
geometry::{Points, Size},
gpu::GpuSource,
};
pub use wgpu;
pub trait GpuRenderer: Send + 'static {
fn prepare(&mut self, cx: &GpuContext<'_>) {
let _ = cx;
}
fn set_prop(&mut self, name: &str, value: Option<&JsonValue>) {
let _ = (name, value);
}
fn set_animated_number(&mut self, name: &str, value: f64) {
let json = serde_json::Number::from_f64(value).map_or(JsonValue::Null, JsonValue::Number);
self.set_prop(name, Some(&json));
}
fn set_animated_color(&mut self, name: &str, color: Rgba) {
let channel = |c: f32| (c.clamp(0.0, 1.0) * 255.0).round() as u8;
let hex = format!(
"#{:02x}{:02x}{:02x}{:02x}",
channel(color.r),
channel(color.g),
channel(color.b),
channel(color.a)
);
self.set_prop(name, Some(&JsonValue::from(hex)));
}
fn animating(&self) -> bool {
false
}
fn render(&mut self, frame: &mut GpuFrame<'_>);
}
#[non_exhaustive]
pub struct GpuContext<'a> {
device: &'a wgpu::Device,
queue: &'a wgpu::Queue,
}
impl<'a> GpuContext<'a> {
pub fn new(device: &'a wgpu::Device, queue: &'a wgpu::Queue) -> Self {
Self { device, queue }
}
pub fn device(&self) -> &wgpu::Device {
self.device
}
pub fn queue(&self) -> &wgpu::Queue {
self.queue
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct GpuEvent {
pub name: Arc<str>,
pub data: JsonValue,
}
#[non_exhaustive]
pub struct GpuFrame<'a> {
device: &'a wgpu::Device,
queue: &'a wgpu::Queue,
target: &'a wgpu::TextureView,
format: wgpu::TextureFormat,
width: u32,
height: u32,
scale: f32,
time: Duration,
encoder: &'a mut wgpu::CommandEncoder,
events: &'a mut Vec<GpuEvent>,
}
#[non_exhaustive]
pub struct GpuFrameParts<'a> {
pub device: &'a wgpu::Device,
pub queue: &'a wgpu::Queue,
pub target: &'a wgpu::TextureView,
pub format: wgpu::TextureFormat,
pub width: u32,
pub height: u32,
pub scale: f32,
pub time: Duration,
pub encoder: &'a mut wgpu::CommandEncoder,
pub events: &'a mut Vec<GpuEvent>,
}
impl<'a> GpuFrameParts<'a> {
#[allow(clippy::too_many_arguments)]
pub fn new(
device: &'a wgpu::Device,
queue: &'a wgpu::Queue,
target: &'a wgpu::TextureView,
format: wgpu::TextureFormat,
(width, height): (u32, u32),
scale: f32,
time: Duration,
encoder: &'a mut wgpu::CommandEncoder,
events: &'a mut Vec<GpuEvent>,
) -> Self {
Self {
device,
queue,
target,
format,
width,
height,
scale,
time,
encoder,
events,
}
}
}
impl<'a> GpuFrame<'a> {
pub fn new(parts: GpuFrameParts<'a>) -> Self {
let GpuFrameParts {
device,
queue,
target,
format,
width,
height,
scale,
time,
encoder,
events,
} = parts;
Self {
device,
queue,
target,
format,
width,
height,
scale,
time,
encoder,
events,
}
}
pub fn device(&self) -> &wgpu::Device {
self.device
}
pub fn queue(&self) -> &wgpu::Queue {
self.queue
}
pub fn target(&self) -> &wgpu::TextureView {
self.target
}
pub fn format(&self) -> wgpu::TextureFormat {
self.format
}
pub fn width(&self) -> u32 {
self.width
}
pub fn height(&self) -> u32 {
self.height
}
pub fn scale(&self) -> f32 {
self.scale
}
pub fn size(&self) -> Size<Points> {
let scale = self.scale.max(f32::MIN_POSITIVE);
Size {
width: Points(self.width as f32 / scale),
height: Points(self.height as f32 / scale),
}
}
pub fn time(&self) -> Duration {
self.time
}
pub fn encoder(&mut self) -> &mut wgpu::CommandEncoder {
self.encoder
}
pub fn emit(&mut self, name: &str, data: JsonValue) {
self.events.push(GpuEvent {
name: name.into(),
data,
});
}
}
type Make = Arc<dyn Fn() -> Box<dyn GpuRenderer> + Send + Sync>;
pub fn renderer_source<R: GpuRenderer>(make: impl Fn() -> R + Send + Sync + 'static) -> GpuSource {
let make: Make = Arc::new(move || Box::new(make()));
GpuSource::new(make)
}
pub fn make_renderer(source: &GpuSource) -> Option<Box<dyn GpuRenderer>> {
source.downcast_ref::<Make>().map(|make| make())
}
pub trait GpuRegistry {
fn gpu_renderer<R: GpuRenderer>(
&mut self,
name: &'static str,
make: impl Fn() -> R + Send + Sync + 'static,
) -> &mut Self;
}
impl GpuRegistry for Registry {
fn gpu_renderer<R: GpuRenderer>(
&mut self,
name: &'static str,
make: impl Fn() -> R + Send + Sync + 'static,
) -> &mut Self {
self.gpu_renderer_source(name, renderer_source(make))
}
}
pub trait GpuViewExt {
fn renderer<R: GpuRenderer>(self, make: impl Fn() -> R + Send + Sync + 'static) -> Self;
}
impl GpuViewExt for GpuView {
fn renderer<R: GpuRenderer>(self, make: impl Fn() -> R + Send + Sync + 'static) -> Self {
self.source(renderer_source(make))
}
}
#[cfg(test)]
mod tests {
use super::*;
struct Clear;
impl GpuRenderer for Clear {
fn render(&mut self, _: &mut GpuFrame<'_>) {}
}
#[test]
fn a_source_makes_renderers_and_others_make_none() {
let source = renderer_source(|| Clear);
assert!(make_renderer(&source).is_some());
assert!(make_renderer(&GpuSource::new(7_u32)).is_none());
}
#[test]
fn a_registered_renderer_is_found_by_name() {
let mut registry = Registry::new();
registry.gpu_renderer("test/clear", || Clear);
let source = registry
.registered_gpu_renderer("test/clear")
.expect("registered");
assert!(make_renderer(source).is_some());
assert!(registry.registered_gpu_renderer("test/other").is_none());
}
}