feat: compose interactive world ui across nested 3d viewports
This commit is contained in:
+15
-2
@@ -259,7 +259,12 @@ pub fn occurrence_constraints(
|
||||
child: &UiNode,
|
||||
mut base: [f32; 4],
|
||||
offer: Size,
|
||||
child_index: usize,
|
||||
) -> [f32; 4] {
|
||||
if let UiKind::Viewport3D { panels, .. } = &parent.kind {
|
||||
let size = panels[child_index];
|
||||
return [size.width, size.height, size.width, size.height];
|
||||
}
|
||||
let inset = insets(parent);
|
||||
for (axis, dimension, available, padding) in [
|
||||
(0, child.sizing.width, offer.width, inset.horizontal()),
|
||||
@@ -280,6 +285,7 @@ pub fn initial_child_constraints(
|
||||
parent: &UiNode,
|
||||
child: &UiNode,
|
||||
constraints: [f32; 4],
|
||||
child_index: usize,
|
||||
) -> [f32; 4] {
|
||||
let offer = parent.sizing.available(constraints);
|
||||
occurrence_constraints(
|
||||
@@ -290,6 +296,7 @@ pub fn initial_child_constraints(
|
||||
width: offer[2],
|
||||
height: offer[3],
|
||||
},
|
||||
child_index,
|
||||
)
|
||||
}
|
||||
/// Scroll cross-axis constraints come from this occurrence's actual viewport,
|
||||
@@ -752,6 +759,9 @@ pub fn arrange_native(
|
||||
bounds: Size,
|
||||
children: &[Size],
|
||||
) -> Result<Vec<(f32, f32)>> {
|
||||
if matches!(node.kind, UiKind::Viewport3D { .. }) {
|
||||
return Ok(node.children().iter().map(|_| (0., 0.)).collect());
|
||||
}
|
||||
if let UiKind::Overlay { .. } = node.kind {
|
||||
let padding = insets(node);
|
||||
let horizontal = match node.main_align {
|
||||
@@ -977,12 +987,14 @@ impl LayoutRuntime {
|
||||
id,
|
||||
constraints,
|
||||
});
|
||||
for (child, slot) in node.children().iter().zip(&node.slots).rev() {
|
||||
for (child_index, (child, slot)) in
|
||||
node.children().iter().zip(&node.slots).enumerate().rev()
|
||||
{
|
||||
work.push((
|
||||
child.as_ref(),
|
||||
Some(index),
|
||||
*slot,
|
||||
initial_child_constraints(node, child, constraints),
|
||||
initial_child_constraints(node, child, constraints, child_index),
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -1017,6 +1029,7 @@ impl LayoutRuntime {
|
||||
coordinate(x + size.width)?;
|
||||
coordinate(y + size.height)?;
|
||||
mounts.push(MountGeometry {
|
||||
panel: None,
|
||||
canvas_viewport: matches!(item.node.kind, UiKind::CanvasViewport { .. }),
|
||||
scene3d_viewport: matches!(item.node.kind, UiKind::Viewport3D { .. }),
|
||||
camera_controls: matches!(
|
||||
|
||||
@@ -214,7 +214,8 @@ impl NativeCallable for Method {
|
||||
} else {
|
||||
node.children()
|
||||
.iter()
|
||||
.map(|child| {
|
||||
.enumerate()
|
||||
.map(|(child_index, child)| {
|
||||
size(
|
||||
child,
|
||||
layout::occurrence_constraints(
|
||||
@@ -226,6 +227,7 @@ impl NativeCallable for Method {
|
||||
Size { width, height },
|
||||
),
|
||||
Size { width, height },
|
||||
child_index,
|
||||
),
|
||||
)
|
||||
})
|
||||
|
||||
+127
-11
@@ -1,6 +1,7 @@
|
||||
//! Software 3D surfaces share the application's existing CPU compositor. Mesh
|
||||
//! visibility and picking use the same camera and transformed world triangles.
|
||||
pub mod math;
|
||||
pub mod panel;
|
||||
use math::{Camera, Matrix, Projection, Vec3};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::sync::Arc;
|
||||
@@ -12,10 +13,15 @@ pub const MAX_VERTICES: usize = 32_768;
|
||||
pub struct Geometry {
|
||||
pub vertices: Vec<Vec3>,
|
||||
pub triangles: Vec<[u32; 3]>,
|
||||
#[serde(default)]
|
||||
pub uv: Vec<[f32; 2]>,
|
||||
}
|
||||
impl Geometry {
|
||||
pub fn valid(&self) -> bool {
|
||||
self.vertices.len() <= MAX_VERTICES
|
||||
&& (self.uv.is_empty()
|
||||
|| self.uv.len() == self.vertices.len()
|
||||
&& self.uv.iter().flatten().all(|value| value.is_finite()))
|
||||
&& self.triangles.len() <= MAX_TRIANGLES
|
||||
&& self.vertices.iter().all(|point| point.valid())
|
||||
&& self
|
||||
@@ -27,6 +33,7 @@ impl Geometry {
|
||||
pub fn cuboid(width: f32, height: f32, depth: f32) -> Self {
|
||||
let (x, y, z) = (width / 2., height / 2., depth / 2.);
|
||||
Self {
|
||||
uv: vec![],
|
||||
vertices: vec![
|
||||
Vec3::new(-x, -y, -z),
|
||||
Vec3::new(x, -y, -z),
|
||||
@@ -60,6 +67,22 @@ pub struct Mesh {
|
||||
pub geometry: Arc<Geometry>,
|
||||
pub world: Matrix,
|
||||
pub color: [f32; 3],
|
||||
pub panel: Option<Panel>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct UiSurface {
|
||||
pub commands: Vec<crate::document::RenderCommand>,
|
||||
pub clips: Vec<Option<[f32; 4]>>,
|
||||
pub views: Vec<Option<crate::viewport::Transform>>,
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Panel {
|
||||
pub key: u64,
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub background: [f32; 3],
|
||||
pub surface: Option<Arc<UiSurface>>,
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Light {
|
||||
@@ -91,6 +114,7 @@ impl endor_core::ObjectPayload for PaintView {
|
||||
}
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Hit {
|
||||
pub instance: usize,
|
||||
pub identity: String,
|
||||
pub point: Vec3,
|
||||
pub normal: Vec3,
|
||||
@@ -101,6 +125,24 @@ pub struct Hit {
|
||||
|
||||
impl View {
|
||||
pub fn valid(&self) -> bool {
|
||||
thread_local! { static DEPTH:std::cell::Cell<usize>=const { std::cell::Cell::new(0) }; }
|
||||
struct Guard;
|
||||
impl Drop for Guard {
|
||||
fn drop(&mut self) {
|
||||
DEPTH.with(|depth| depth.set(depth.get() - 1));
|
||||
}
|
||||
}
|
||||
if !DEPTH.with(|depth| {
|
||||
if depth.get() >= 8 {
|
||||
false
|
||||
} else {
|
||||
depth.set(depth.get() + 1);
|
||||
true
|
||||
}
|
||||
}) {
|
||||
return false;
|
||||
}
|
||||
let _guard = Guard;
|
||||
let color_valid = |color: [f32; 3]| {
|
||||
color
|
||||
.iter()
|
||||
@@ -121,6 +163,22 @@ impl View {
|
||||
.vertices
|
||||
.iter()
|
||||
.all(|point| mesh.world.point(*point).valid())
|
||||
&& mesh.panel.as_ref().is_none_or(|panel| {
|
||||
panel.width > 0
|
||||
&& panel.height > 0
|
||||
&& panel.width <= 2048
|
||||
&& panel.height <= 2048
|
||||
&& color_valid(panel.background)
|
||||
&& panel.surface.as_ref().is_none_or(|surface| {
|
||||
surface.commands.len() <= 10001
|
||||
&& surface.commands.len() == surface.views.len()
|
||||
&& surface.commands.len() == surface.clips.len()
|
||||
&& surface
|
||||
.commands
|
||||
.iter()
|
||||
.all(|command| command.validate().is_ok())
|
||||
})
|
||||
})
|
||||
})
|
||||
&& self.scene.lights.iter().all(|light| {
|
||||
color_valid(light.color)
|
||||
@@ -156,7 +214,7 @@ impl View {
|
||||
let ray = self.camera.ray(u, v, aspect)?;
|
||||
let basis = self.camera.basis()?;
|
||||
let mut hit: Option<Hit> = None;
|
||||
for mesh in &self.scene.meshes {
|
||||
for (instance, mesh) in self.scene.meshes.iter().enumerate() {
|
||||
for (triangle, indices) in mesh.geometry.triangles.iter().enumerate() {
|
||||
let [a, b, c] =
|
||||
indices.map(|i| mesh.world.point(mesh.geometry.vertices[i as usize]));
|
||||
@@ -175,6 +233,7 @@ impl View {
|
||||
continue;
|
||||
};
|
||||
hit = Some(Hit {
|
||||
instance,
|
||||
identity: mesh.identity.clone(),
|
||||
point,
|
||||
normal,
|
||||
@@ -240,7 +299,23 @@ pub fn surface(view: &Arc<View>, width: u32, height: u32) -> Result<Arc<[u32]>,
|
||||
|
||||
/// Homogeneous frustum planes expressed in view space. Clipping before division
|
||||
/// handles triangles crossing the near plane without huge screen coordinates.
|
||||
fn clip(mut polygon: Vec<Vec3>, camera: Camera, aspect: f32) -> Vec<Vec3> {
|
||||
#[derive(Clone, Copy)]
|
||||
struct Vertex {
|
||||
position: Vec3,
|
||||
uv: [f32; 2],
|
||||
}
|
||||
impl Vertex {
|
||||
fn lerp(self, other: Self, t: f32) -> Self {
|
||||
Self {
|
||||
position: self.position + (other.position - self.position) * t,
|
||||
uv: [
|
||||
self.uv[0] + (other.uv[0] - self.uv[0]) * t,
|
||||
self.uv[1] + (other.uv[1] - self.uv[1]) * t,
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
||||
fn clip(mut polygon: Vec<Vertex>, camera: Camera, aspect: f32) -> Vec<Vertex> {
|
||||
let (x, y, slope) = match camera.projection {
|
||||
Projection::Perspective { fov } => {
|
||||
let y = (fov.to_radians() / 2.).tan();
|
||||
@@ -260,14 +335,17 @@ fn clip(mut polygon: Vec<Vec3>, camera: Camera, aspect: f32) -> Vec<Vec3> {
|
||||
if polygon.is_empty() {
|
||||
break;
|
||||
}
|
||||
let distance = |p: Vec3| a * p.x + b * p.y + c * p.z + d;
|
||||
let distance = |vertex: Vertex| {
|
||||
let p = vertex.position;
|
||||
a * p.x + b * p.y + c * p.z + d
|
||||
};
|
||||
let mut output = Vec::new();
|
||||
let mut previous = *polygon.last().unwrap();
|
||||
let mut before = distance(previous);
|
||||
for current in polygon {
|
||||
let after = distance(current);
|
||||
if (before >= 0.) != (after >= 0.) {
|
||||
output.push(previous + (current - previous) * (before / (before - after)));
|
||||
output.push(previous.lerp(current, before / (before - after)));
|
||||
}
|
||||
if after >= 0. {
|
||||
output.push(current);
|
||||
@@ -281,11 +359,13 @@ fn clip(mut polygon: Vec<Vec3>, camera: Camera, aspect: f32) -> Vec<Vec3> {
|
||||
}
|
||||
|
||||
pub fn render(view: &View, width: u32, height: u32) -> Result<Vec<u32>, &'static str> {
|
||||
let _scope = crate::software_renderer::RenderScope::enter()?;
|
||||
let area = width as usize * height as usize;
|
||||
// Offscreen targets have a hard per-surface resource bound.
|
||||
if area > 4_194_304 || width == 0 || height == 0 || !view.valid() {
|
||||
return Err("invalid 3D surface or resource budget exceeded");
|
||||
}
|
||||
crate::software_renderer::reserve_pixels(area.saturating_mul(2))?;
|
||||
let mut pixels = vec![rgb(view.scene.background); area];
|
||||
let mut depths = vec![f32::INFINITY; area];
|
||||
let camera = view.camera;
|
||||
@@ -305,6 +385,7 @@ pub fn render(view: &View, width: u32, height: u32) -> Result<Vec<u32>, &'static
|
||||
};
|
||||
let mut work = 0usize;
|
||||
for mesh in &view.scene.meshes {
|
||||
let texture = mesh.panel.as_ref().map(panel::texture).transpose()?;
|
||||
for indices in &mesh.geometry.triangles {
|
||||
let [a, b, c] = indices.map(|i| mesh.world.point(mesh.geometry.vertices[i as usize]));
|
||||
let Some(normal) = (b - a).cross(c - a).normalized() else {
|
||||
@@ -319,11 +400,20 @@ pub fn render(view: &View, width: u32, height: u32) -> Result<Vec<u32>, &'static
|
||||
}
|
||||
let color = rgb(std::array::from_fn(|i| mesh.color[i] * light[i]));
|
||||
let polygon = clip(
|
||||
vec![
|
||||
camera.view(basis, a),
|
||||
camera.view(basis, b),
|
||||
camera.view(basis, c),
|
||||
],
|
||||
indices
|
||||
.map(|index| Vertex {
|
||||
position: camera.view(
|
||||
basis,
|
||||
mesh.world.point(mesh.geometry.vertices[index as usize]),
|
||||
),
|
||||
uv: mesh
|
||||
.geometry
|
||||
.uv
|
||||
.get(index as usize)
|
||||
.copied()
|
||||
.unwrap_or([0., 0.]),
|
||||
})
|
||||
.to_vec(),
|
||||
camera,
|
||||
aspect,
|
||||
);
|
||||
@@ -331,7 +421,8 @@ pub fn render(view: &View, width: u32, height: u32) -> Result<Vec<u32>, &'static
|
||||
continue;
|
||||
}
|
||||
for index in 1..polygon.len() - 1 {
|
||||
let [a, b, c] = [polygon[0], polygon[index], polygon[index + 1]].map(project);
|
||||
let vertices = [polygon[0], polygon[index], polygon[index + 1]];
|
||||
let [a, b, c] = vertices.map(|vertex| project(vertex.position));
|
||||
let min_x = a.x.min(b.x).min(c.x).floor().max(0.) as u32;
|
||||
let max_x = a.x.max(b.x).max(c.x).ceil().min(width as f32) as u32;
|
||||
let min_y = a.y.min(b.y).min(c.y).floor().max(0.) as u32;
|
||||
@@ -367,7 +458,30 @@ pub fn render(view: &View, width: u32, height: u32) -> Result<Vec<u32>, &'static
|
||||
let index = y as usize * width as usize + x as usize;
|
||||
if depth < depths[index] {
|
||||
depths[index] = depth;
|
||||
pixels[index] = color;
|
||||
pixels[index] =
|
||||
if let (Some(texture), Some(panel)) = (&texture, &mesh.panel) {
|
||||
let interpolated = |axis| {
|
||||
if perspective {
|
||||
depth
|
||||
* (weights[0] * vertices[0].uv[axis] / a.z
|
||||
+ weights[1] * vertices[1].uv[axis] / b.z
|
||||
+ weights[2] * vertices[2].uv[axis] / c.z)
|
||||
} else {
|
||||
weights[0] * vertices[0].uv[axis]
|
||||
+ weights[1] * vertices[1].uv[axis]
|
||||
+ weights[2] * vertices[2].uv[axis]
|
||||
}
|
||||
};
|
||||
let u = (interpolated(0).clamp(0., 1.) * panel.width as f32)
|
||||
.min((panel.width - 1) as f32)
|
||||
as usize;
|
||||
let v = (interpolated(1).clamp(0., 1.) * panel.height as f32)
|
||||
.min((panel.height - 1) as f32)
|
||||
as usize;
|
||||
texture[v * panel.width as usize + u]
|
||||
} else {
|
||||
color
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -385,6 +499,7 @@ mod tests {
|
||||
camera: Camera::default(),
|
||||
scene: Arc::new(Scene {
|
||||
meshes: vec![Mesh {
|
||||
panel: None,
|
||||
identity: "mesh".into(),
|
||||
geometry: Arc::new(geometry),
|
||||
world: Matrix::default(),
|
||||
@@ -399,6 +514,7 @@ mod tests {
|
||||
#[test]
|
||||
fn triangles_crossing_near_plane_are_clipped_and_pick_uses_the_same_range() {
|
||||
let mut view = view(Geometry {
|
||||
uv: vec![],
|
||||
vertices: vec![
|
||||
Vec3::new(-1., -1., 0.5),
|
||||
Vec3::new(1., -1., 2.),
|
||||
|
||||
@@ -63,6 +63,38 @@ impl Default for Matrix {
|
||||
}
|
||||
}
|
||||
impl Matrix {
|
||||
pub fn inverse(self) -> Option<Self> {
|
||||
let m = self.0;
|
||||
let determinant = m[0] * (m[5] * m[10] - m[6] * m[9]) - m[1] * (m[4] * m[10] - m[6] * m[8])
|
||||
+ m[2] * (m[4] * m[9] - m[5] * m[8]);
|
||||
if !determinant.is_finite() || determinant.abs() < 1e-9 {
|
||||
return None;
|
||||
}
|
||||
let d = 1. / determinant;
|
||||
let mut out = Self([
|
||||
(m[5] * m[10] - m[6] * m[9]) * d,
|
||||
(m[2] * m[9] - m[1] * m[10]) * d,
|
||||
(m[1] * m[6] - m[2] * m[5]) * d,
|
||||
0.,
|
||||
(m[6] * m[8] - m[4] * m[10]) * d,
|
||||
(m[0] * m[10] - m[2] * m[8]) * d,
|
||||
(m[2] * m[4] - m[0] * m[6]) * d,
|
||||
0.,
|
||||
(m[4] * m[9] - m[5] * m[8]) * d,
|
||||
(m[1] * m[8] - m[0] * m[9]) * d,
|
||||
(m[0] * m[5] - m[1] * m[4]) * d,
|
||||
0.,
|
||||
0.,
|
||||
0.,
|
||||
0.,
|
||||
1.,
|
||||
]);
|
||||
let translation = out.point(Vec3::new(m[3], m[7], m[11])) * -1.;
|
||||
out.0[3] = translation.x;
|
||||
out.0[7] = translation.y;
|
||||
out.0[11] = translation.z;
|
||||
Some(out)
|
||||
}
|
||||
pub fn compose(self, local: Self) -> Self {
|
||||
let mut out = [0.; 16];
|
||||
for row in 0..4 {
|
||||
|
||||
@@ -0,0 +1,255 @@
|
||||
//! Perspective UI mapping. The same world plane supplies texture coordinates
|
||||
//! during rendering and inverse pointer coordinates during event dispatch.
|
||||
use super::{
|
||||
View,
|
||||
math::{Projection, Vec3},
|
||||
};
|
||||
use crate::{
|
||||
geometry::{Point, Rect},
|
||||
viewport::Transform,
|
||||
};
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
|
||||
pub struct Mapping {
|
||||
pub owner: u64,
|
||||
pub key: u64,
|
||||
pub view: Arc<View>,
|
||||
pub rect: [f32; 4],
|
||||
pub outer: Transform,
|
||||
pub clip: Option<[f32; 4]>,
|
||||
pub parent: Option<Arc<Mapping>>,
|
||||
}
|
||||
impl Mapping {
|
||||
pub fn texture_point(&self, point: Point, clipped: bool) -> Option<Point> {
|
||||
let point = if let Some(parent) = &self.parent {
|
||||
parent.texture_point(point, clipped)?
|
||||
} else {
|
||||
point
|
||||
};
|
||||
if clipped
|
||||
&& self
|
||||
.clip
|
||||
.is_some_and(|clip| !Rect::new(clip[0], clip[1], clip[2], clip[3]).contains(point))
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let point = self.outer.inverse(point);
|
||||
let [x, y, width, height] = self.rect;
|
||||
if width <= 0. || height <= 0. {
|
||||
return None;
|
||||
}
|
||||
let (u, v) = ((point.x - x) / width, (point.y - y) / height);
|
||||
let mesh = self.view.scene.meshes.iter().find(|mesh| {
|
||||
mesh.panel
|
||||
.as_ref()
|
||||
.is_some_and(|panel| panel.key == self.key)
|
||||
})?;
|
||||
let panel = mesh.panel.as_ref()?;
|
||||
if clipped {
|
||||
let hit = self.view.pick(u, v, width / height)?;
|
||||
if self.view.scene.meshes[hit.instance]
|
||||
.panel
|
||||
.as_ref()
|
||||
.is_none_or(|panel| panel.key != self.key)
|
||||
{
|
||||
return None;
|
||||
}
|
||||
}
|
||||
let ray = self.view.camera.ray(u, v, width / height)?;
|
||||
let center = mesh.world.point(Vec3::default());
|
||||
let normal = (mesh.world.point(Vec3::new(1., 0., 0.)) - center)
|
||||
.cross(mesh.world.point(Vec3::new(0., 1., 0.)) - center);
|
||||
let denominator = normal.dot(ray.direction);
|
||||
if denominator.abs() < 1e-7 {
|
||||
return None;
|
||||
}
|
||||
let distance = (center - ray.origin).dot(normal) / denominator;
|
||||
if !distance.is_finite() || distance <= 0. {
|
||||
return None;
|
||||
}
|
||||
let local = mesh
|
||||
.world
|
||||
.inverse()?
|
||||
.point(ray.origin + ray.direction * distance);
|
||||
let world_width = mesh.geometry.vertices[1].x - mesh.geometry.vertices[0].x;
|
||||
let world_height = mesh.geometry.vertices[0].y - mesh.geometry.vertices[2].y;
|
||||
let point = Point::new(
|
||||
(local.x / world_width + 0.5) * panel.width as f32,
|
||||
(0.5 - local.y / world_height) * panel.height as f32,
|
||||
);
|
||||
(point.x.is_finite() && point.y.is_finite()).then_some(point)
|
||||
}
|
||||
|
||||
pub fn project_point(&self, point: Point) -> Option<Point> {
|
||||
let mesh = self.view.scene.meshes.iter().find(|mesh| {
|
||||
mesh.panel
|
||||
.as_ref()
|
||||
.is_some_and(|panel| panel.key == self.key)
|
||||
})?;
|
||||
let panel = mesh.panel.as_ref()?;
|
||||
let width = mesh.geometry.vertices[1].x - mesh.geometry.vertices[0].x;
|
||||
let height = mesh.geometry.vertices[0].y - mesh.geometry.vertices[2].y;
|
||||
let world = mesh.world.point(Vec3::new(
|
||||
(point.x / panel.width as f32 - 0.5) * width,
|
||||
(0.5 - point.y / panel.height as f32) * height,
|
||||
0.,
|
||||
));
|
||||
let camera = self.view.camera;
|
||||
let local = camera.view(camera.basis()?, world);
|
||||
if local.z <= camera.near || local.z >= camera.far {
|
||||
return None;
|
||||
}
|
||||
let [x, y, w, h] = self.rect;
|
||||
let half = match camera.projection {
|
||||
Projection::Perspective { fov } => local.z * (fov.to_radians() / 2.).tan(),
|
||||
Projection::Orthographic { height } => height / 2.,
|
||||
};
|
||||
let point = self.outer.point(Point::new(
|
||||
x + (local.x / (half * w / h) + 1.) * w / 2.,
|
||||
y + (1. - local.y / half) * h / 2.,
|
||||
));
|
||||
if let Some(parent) = &self.parent {
|
||||
parent.project_point(point)
|
||||
} else {
|
||||
Some(point)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct Entry {
|
||||
command: crate::document::RenderCommand,
|
||||
clip: Option<[f32; 4]>,
|
||||
view: Option<Transform>,
|
||||
owner: u64,
|
||||
}
|
||||
|
||||
/// Collect a panel's ordinary paint subtree into its texture. Nested 3D
|
||||
/// commands are assembled recursively before their containing texture.
|
||||
pub fn compose(frame: &mut crate::document::Frame) -> anyhow::Result<()> {
|
||||
use crate::document::RenderCommand;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
let mounts: HashMap<_, _> = frame.mounts.iter().map(|mount| (mount.id, mount)).collect();
|
||||
let mut groups = HashMap::<(u64, u64), Vec<Entry>>::new();
|
||||
let mut roots = Vec::new();
|
||||
for (index, command) in frame.commands.iter().enumerate() {
|
||||
let owner = frame.command_mounts[index];
|
||||
let mount = mounts[&owner];
|
||||
let entry = Entry {
|
||||
command: command.clone(),
|
||||
clip: frame.command_clips[index],
|
||||
view: frame.command_views[index],
|
||||
owner,
|
||||
};
|
||||
if let Some(panel) = &mount.panel {
|
||||
groups
|
||||
.entry((panel.owner, panel.key))
|
||||
.or_default()
|
||||
.push(entry);
|
||||
} else {
|
||||
roots.push(entry);
|
||||
}
|
||||
}
|
||||
if let Some(mount) = frame.focus.and_then(|id| mounts.get(&id))
|
||||
&& let Some(panel) = &mount.panel
|
||||
{
|
||||
groups
|
||||
.entry((panel.owner, panel.key))
|
||||
.or_default()
|
||||
.push(Entry {
|
||||
command: RenderCommand::StrokeRoundedRect {
|
||||
x: mount.x,
|
||||
y: mount.y,
|
||||
width: mount.width,
|
||||
height: mount.height,
|
||||
radius: 7.,
|
||||
stroke_width: 2.,
|
||||
color: "#4F6FDB".into(),
|
||||
},
|
||||
clip: mount.clip,
|
||||
view: mount.view,
|
||||
owner: mount.id,
|
||||
});
|
||||
}
|
||||
fn assemble(
|
||||
entry: Entry,
|
||||
groups: &HashMap<(u64, u64), Vec<Entry>>,
|
||||
path: &mut HashSet<u64>,
|
||||
depth: usize,
|
||||
) -> anyhow::Result<Entry> {
|
||||
let mut entry = entry;
|
||||
if let RenderCommand::Scene3D { view, .. } = &mut entry.command
|
||||
&& view.scene.meshes.iter().any(|mesh| mesh.panel.is_some())
|
||||
{
|
||||
anyhow::ensure!(
|
||||
depth < 8 && path.insert(entry.owner),
|
||||
"3D UI paint nesting cycle or depth budget exceeded"
|
||||
);
|
||||
let mut scene = (*view.scene).clone();
|
||||
for mesh in &mut scene.meshes {
|
||||
if let Some(panel) = &mut mesh.panel {
|
||||
let mut surface = super::UiSurface {
|
||||
commands: Vec::new(),
|
||||
clips: Vec::new(),
|
||||
views: Vec::new(),
|
||||
};
|
||||
for child in groups.get(&(entry.owner, panel.key)).into_iter().flatten() {
|
||||
let child = assemble(child.clone(), groups, path, depth + 1)?;
|
||||
surface.commands.push(child.command);
|
||||
surface.clips.push(child.clip);
|
||||
surface.views.push(child.view);
|
||||
}
|
||||
panel.surface = Some(Arc::new(surface));
|
||||
}
|
||||
}
|
||||
*view = Arc::new(View {
|
||||
scene: Arc::new(scene),
|
||||
camera: view.camera,
|
||||
});
|
||||
path.remove(&entry.owner);
|
||||
}
|
||||
Ok(entry)
|
||||
}
|
||||
let mut path = HashSet::new();
|
||||
frame.commands.clear();
|
||||
frame.command_clips.clear();
|
||||
frame.command_views.clear();
|
||||
frame.command_mounts.clear();
|
||||
for entry in roots {
|
||||
let entry = assemble(entry, &groups, &mut path, 0)?;
|
||||
frame.commands.push(entry.command);
|
||||
frame.command_clips.push(entry.clip);
|
||||
frame.command_views.push(entry.view);
|
||||
frame.command_mounts.push(entry.owner);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn texture(panel: &super::Panel) -> Result<Vec<u32>, &'static str> {
|
||||
crate::software_renderer::reserve_pixels(panel.width as usize * panel.height as usize)?;
|
||||
let mut pixels =
|
||||
vec![super::rgb(panel.background); panel.width as usize * panel.height as usize];
|
||||
if let Some(surface) = &panel.surface {
|
||||
let document = crate::document::RenderDocument {
|
||||
frame: crate::document::Frame {
|
||||
background: format!("#{:06X}", super::rgb(panel.background)),
|
||||
commands: surface.commands.clone(),
|
||||
command_views: surface.views.clone(),
|
||||
command_clips: surface.clips.clone(),
|
||||
..Default::default()
|
||||
},
|
||||
..Default::default()
|
||||
};
|
||||
let list = document
|
||||
.display_list(1.)
|
||||
.map_err(|_| "invalid 3D UI texture commands")?;
|
||||
crate::software_renderer::SoftwareRenderer::render(
|
||||
&list,
|
||||
&mut pixels,
|
||||
panel.width,
|
||||
panel.height,
|
||||
)?;
|
||||
}
|
||||
Ok(pixels)
|
||||
}
|
||||
+16
-2
@@ -76,7 +76,12 @@ pub fn project(vm: &mut Vm, table: &MountTable, root: &UiNode, frame: &mut Frame
|
||||
.mounts
|
||||
.iter()
|
||||
.map(|m| {
|
||||
let inset = layout::insets(nodes[m.block_id.as_str()]);
|
||||
let node = nodes[m.block_id.as_str()];
|
||||
let inset = if matches!(node.kind, UiKind::Scroll { .. }) {
|
||||
layout::insets(node)
|
||||
} else {
|
||||
layout::Insets::default()
|
||||
};
|
||||
(m.x + m.width - inset.right, m.y + m.height - inset.bottom)
|
||||
})
|
||||
.collect();
|
||||
@@ -84,6 +89,12 @@ pub fn project(vm: &mut Vm, table: &MountTable, root: &UiNode, frame: &mut Frame
|
||||
let m = &frame.mounts[i];
|
||||
if let Some(parent) = m.parent {
|
||||
let parent = indices[&parent];
|
||||
if matches!(
|
||||
nodes[frame.mounts[parent].block_id.as_str()].kind,
|
||||
UiKind::CanvasViewport { .. } | UiKind::Viewport3D { .. }
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
let e = if scrolling[i] {
|
||||
(m.x + m.width, m.y + m.height)
|
||||
} else {
|
||||
@@ -166,6 +177,9 @@ pub fn project(vm: &mut Vm, table: &MountTable, root: &UiNode, frame: &mut Frame
|
||||
),
|
||||
);
|
||||
}
|
||||
if matches!(node.kind, UiKind::Viewport3D { .. }) {
|
||||
child = (0., 0., None);
|
||||
}
|
||||
contexts.insert(m.id, child);
|
||||
for command in layout::paint_commands(
|
||||
nodes[m.block_id.as_str()],
|
||||
@@ -259,7 +273,7 @@ pub fn into_view(
|
||||
};
|
||||
let mount = cx.store().payload::<Mount>(owner, "Mount")?.clone();
|
||||
parent = mount.parent;
|
||||
if mount.geometry.canvas_viewport {
|
||||
if mount.geometry.canvas_viewport || mount.geometry.scene3d_viewport {
|
||||
rect = [
|
||||
mount.geometry.x,
|
||||
mount.geometry.y,
|
||||
|
||||
@@ -6,6 +6,42 @@ use crate::{
|
||||
|
||||
pub struct SoftwareRenderer;
|
||||
|
||||
thread_local! {
|
||||
static RENDER_DEPTH:std::cell::Cell<usize>=const { std::cell::Cell::new(0) };
|
||||
static PIXEL_UNITS:std::cell::Cell<usize>=const { std::cell::Cell::new(0) };
|
||||
}
|
||||
pub(crate) struct RenderScope;
|
||||
impl RenderScope {
|
||||
pub fn enter() -> Result<Self, &'static str> {
|
||||
RENDER_DEPTH.with(|depth| {
|
||||
let current = depth.get();
|
||||
if current >= 18 {
|
||||
return Err("render nesting budget exceeded");
|
||||
}
|
||||
if current == 0 {
|
||||
PIXEL_UNITS.with(|pixels| pixels.set(0));
|
||||
}
|
||||
depth.set(current + 1);
|
||||
Ok(Self)
|
||||
})
|
||||
}
|
||||
}
|
||||
impl Drop for RenderScope {
|
||||
fn drop(&mut self) {
|
||||
RENDER_DEPTH.with(|depth| depth.set(depth.get() - 1));
|
||||
}
|
||||
}
|
||||
pub(crate) fn reserve_pixels(units: usize) -> Result<(), &'static str> {
|
||||
PIXEL_UNITS.with(|pixels| {
|
||||
let next = pixels.get().saturating_add(units);
|
||||
if next > 16_777_216 {
|
||||
return Err("nested render pixel budget exceeded");
|
||||
}
|
||||
pixels.set(next);
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
impl SoftwareRenderer {
|
||||
pub fn render(
|
||||
list: &DisplayList,
|
||||
@@ -13,6 +49,7 @@ impl SoftwareRenderer {
|
||||
width: u32,
|
||||
height: u32,
|
||||
) -> Result<(), &'static str> {
|
||||
let _scope = RenderScope::enter()?;
|
||||
let mut canvas = Canvas {
|
||||
pixels: target,
|
||||
width,
|
||||
|
||||
@@ -80,7 +80,9 @@ fn selector(
|
||||
}
|
||||
let geometry = &target.geometry;
|
||||
let point = crate::geometry::Point::new(point[0], point[1]);
|
||||
let point = geometry.view.map_or(point, |view| view.inverse(point));
|
||||
let Some(point) = geometry.logical_point(point) else {
|
||||
continue;
|
||||
};
|
||||
let x = point.x - geometry.x;
|
||||
let y = point.y - geometry.y;
|
||||
if x < 0.0 || y < 0.0 || x > geometry.width || y > geometry.height {
|
||||
|
||||
@@ -534,12 +534,13 @@ impl NativeCallable for Settle {
|
||||
available,
|
||||
Size { width, height },
|
||||
);
|
||||
for child in children {
|
||||
for (child_index, child) in children.into_iter().enumerate() {
|
||||
let natural = layout::occurrence_constraints(
|
||||
&plan.nodes[index],
|
||||
&plan.nodes[child],
|
||||
base,
|
||||
Size { width, height },
|
||||
child_index,
|
||||
);
|
||||
let mut constraints = natural;
|
||||
if let Some(width) = plan.allocations[child][0] {
|
||||
@@ -887,11 +888,12 @@ pub fn start(
|
||||
}
|
||||
let mut constraints = vec![runtime.root_constraints(); nodes.len()];
|
||||
for index in 0..nodes.len() {
|
||||
for child in &children[index] {
|
||||
for (child_index, child) in children[index].iter().enumerate() {
|
||||
constraints[*child] = layout::initial_child_constraints(
|
||||
&nodes[index],
|
||||
&nodes[*child],
|
||||
constraints[index],
|
||||
child_index,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -416,7 +416,8 @@ fn finish(vm: &mut Vm, mut work: Work, reads: Vec<endor_core::ReadDependency>) -
|
||||
.output
|
||||
.lock()
|
||||
.map_err(|_| anyhow::anyhow!("paint output poisoned"))?;
|
||||
if output.commands.is_empty() {
|
||||
let native_only = output.commands.is_empty();
|
||||
if work.root.is_none() {
|
||||
return Ok(Progress::Complete(Box::new(Draft {
|
||||
frame: work.frame,
|
||||
reads: Vec::new(),
|
||||
@@ -465,12 +466,14 @@ fn finish(vm: &mut Vm, mut work: Work, reads: Vec<endor_core::ReadDependency>) -
|
||||
frame.command_clips = clips;
|
||||
frame.command_views = views;
|
||||
frame.command_mounts = owners;
|
||||
crate::scene3d::panel::compose(frame)?;
|
||||
let registry = object(vm.get_attr(desktop, "types")?)?;
|
||||
let reads = reads
|
||||
.into_iter()
|
||||
.filter(|dependency| {
|
||||
let object = dependency.object();
|
||||
object != desktop
|
||||
!native_only
|
||||
&& object != desktop
|
||||
&& object != registry
|
||||
&& vm
|
||||
.object(object)
|
||||
|
||||
+62
-6
@@ -194,24 +194,46 @@ pub fn project(
|
||||
) -> anyhow::Result<()> {
|
||||
use crate::ui_bridge::UiKind;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
let mut nodes = HashMap::new();
|
||||
let mut work = vec![root];
|
||||
while let Some(node) = work.pop() {
|
||||
nodes.insert(node.block_id.as_str(), node);
|
||||
work.extend(node.children().iter().map(|node| node.as_ref()));
|
||||
}
|
||||
let mut contexts = HashMap::<u64, (Transform, Option<[f32; 4]>)>::new();
|
||||
type Scope = (
|
||||
Transform,
|
||||
Option<[f32; 4]>,
|
||||
Option<Arc<crate::scene3d::panel::Mapping>>,
|
||||
);
|
||||
let mut contexts = HashMap::<u64, Scope>::new();
|
||||
let next: HashMap<_, _> = frame
|
||||
.mounts
|
||||
.iter()
|
||||
.map(|mount| (mount.id, mount.next_sibling))
|
||||
.collect();
|
||||
let mut panels =
|
||||
HashMap::<u64, (Arc<crate::scene3d::panel::Mapping>, crate::layout::Size)>::new();
|
||||
for mount in &mut frame.mounts {
|
||||
let node = nodes[mount.block_id.as_str()];
|
||||
let (view, clip) = mount
|
||||
.parent
|
||||
.and_then(|parent| contexts.get(&parent).copied())
|
||||
.unwrap_or_default();
|
||||
let (view, clip, panel) = if let Some((panel, size)) = panels.get(&mount.id) {
|
||||
(
|
||||
Transform::default(),
|
||||
Some([0., 0., size.width, size.height]),
|
||||
Some(panel.clone()),
|
||||
)
|
||||
} else {
|
||||
mount
|
||||
.parent
|
||||
.and_then(|parent| contexts.get(&parent).cloned())
|
||||
.unwrap_or_default()
|
||||
};
|
||||
anyhow::ensure!(
|
||||
view.valid(),
|
||||
"nested viewport transform exceeds numeric budget"
|
||||
);
|
||||
mount.view = (view != Transform::default()).then_some(view);
|
||||
mount.panel = panel.clone();
|
||||
mount.clip = clip;
|
||||
mount.viewport_clip = None;
|
||||
let mut child_view = view;
|
||||
@@ -260,7 +282,41 @@ pub fn project(
|
||||
mount.viewport_clip = child_clip;
|
||||
}
|
||||
}
|
||||
contexts.insert(mount.id, (child_view, child_clip));
|
||||
if let UiKind::Viewport3D {
|
||||
view: scene,
|
||||
panels: sizes,
|
||||
..
|
||||
} = &node.kind
|
||||
{
|
||||
let mut depth = 0;
|
||||
let mut ancestor = panel.clone();
|
||||
while let Some(mapping) = ancestor {
|
||||
depth += 1;
|
||||
ancestor = mapping.parent.clone();
|
||||
}
|
||||
anyhow::ensure!(depth < 8, "3D UI viewport nesting exceeds 8 surfaces");
|
||||
let mut child = mount.first_child;
|
||||
for (key, size) in node.slots.iter().zip(sizes) {
|
||||
let id = child.ok_or_else(|| anyhow::anyhow!("missing 3D UI root occurrence"))?;
|
||||
let mapping = Arc::new(crate::scene3d::panel::Mapping {
|
||||
owner: mount.id,
|
||||
key: *key,
|
||||
view: scene.clone(),
|
||||
rect: [
|
||||
mount.x + inset.left,
|
||||
mount.y + inset.top,
|
||||
(mount.width - inset.horizontal()).max(0.),
|
||||
(mount.height - inset.vertical()).max(0.),
|
||||
],
|
||||
outer: view,
|
||||
clip: mount.viewport_clip,
|
||||
parent: panel.clone(),
|
||||
});
|
||||
panels.insert(id, (mapping, *size));
|
||||
child = next[&id];
|
||||
}
|
||||
}
|
||||
contexts.insert(mount.id, (child_view, child_clip, panel));
|
||||
}
|
||||
let mounts: HashMap<_, _> = frame.mounts.iter().map(|mount| (mount.id, mount)).collect();
|
||||
frame.command_views.clear();
|
||||
|
||||
Reference in New Issue
Block a user