Skip to content

Commit

Permalink
core: size propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
fooker committed Feb 21, 2024
1 parent 613ea57 commit af3d464
Show file tree
Hide file tree
Showing 13 changed files with 267 additions and 116 deletions.
2 changes: 1 addition & 1 deletion core/src/decl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub trait NodeDecl {
pub trait OutputDecl {
type Output: Output;

fn materialize(self, size: usize) -> impl Future<Output = Result<Self::Output>>;
fn materialize(self) -> impl Future<Output = Result<Self::Output>>;
}

pub trait FreeAttrDecl {
Expand Down
2 changes: 2 additions & 0 deletions core/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ pub trait Output: Sized {
type Element;

fn render(&mut self, out: impl BufferReader<Element = Self::Element>) -> impl Future<Output = Result<()>>;

fn size(&self) -> usize;
}
121 changes: 60 additions & 61 deletions core/src/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ struct NodeBufferReader<'b, E, O> {
}

impl<'b, E, O> BufferReader for NodeBufferReader<'b, E, O>
where
E: Copy,
O: Copy + From<E>,
where
E: Copy,
O: Copy + From<E>,
{
type Element = O;

Expand All @@ -47,7 +47,7 @@ where
}

impl<'ctx, Node> ops::Index<NodeRef<Node>> for Context<'ctx>
where Node: self::Node
where Node: self::Node
{
type Output = NodeContainer<Node>;

Expand All @@ -57,7 +57,7 @@ where Node: self::Node
}

pub struct NodeHandle<Decl>
where Decl: NodeDecl
where Decl: NodeDecl
{
/// The scene-wide unique name of the node
pub name: String,
Expand All @@ -67,14 +67,14 @@ where Decl: NodeDecl
}

pub struct NodeContainer<Node>
where Node: self::Node
where Node: self::Node
{
node: Node,
buffer: Buffer<Node::Element>,
}

impl<Node> NodeContainer<Node>
where Node: self::Node
where Node: self::Node
{
pub fn build(builder: &NodeBuilder, node: Node) -> Result<Self> {
let buffer = Buffer::with_default(builder.size);
Expand All @@ -87,7 +87,7 @@ where Node: self::Node
}

impl<Node> BufferReader for NodeContainer<Node>
where Node: self::Node
where Node: self::Node
{
type Element = Node::Element;

Expand All @@ -101,7 +101,7 @@ where Node: self::Node
}

impl<Node> BufferReader for &NodeContainer<Node>
where Node: self::Node
where Node: self::Node
{
type Element = Node::Element;

Expand All @@ -119,21 +119,21 @@ trait NodeHolder {
}

impl<Node> NodeHolder for NodeContainer<Node>
where Node: self::Node
where Node: self::Node
{
fn update(&mut self, ctx: &Context) -> Result<()> {
return self.node.update(ctx, &mut self.buffer);
}
}

pub struct NodeRef<Node>
where Node: self::Node + 'static // TODO: Is static required?
where Node: self::Node + 'static // TODO: Is static required?
{
node: Ref<NodeContainer<Node>, dyn NodeHolder>,
}

impl<Node> Clone for NodeRef<Node>
where Node: self::Node
where Node: self::Node
{
fn clone(&self) -> Self {
return Self {
Expand All @@ -149,7 +149,7 @@ impl<Node> Copy for NodeRef<Node> where Node: self::Node {}
/// The handle represents a input registered in a [`Scene'] and can be used to get the real thing
/// while manifesting the scene.
pub struct InputHandle<V>
where V: InputValue
where V: InputValue
{
/// The scene-wide unique name of the input
pub name: String,
Expand All @@ -158,7 +158,7 @@ where V: InputValue
}

impl<V> InputHandle<V>
where V: InputValue
where V: InputValue
{
/// Returns a sink into the input represented by this handle.
pub fn sink(&self) -> InputSink {
Expand All @@ -169,23 +169,12 @@ where V: InputValue
/// Declaration of a scene.
///
/// This is used to declare nodes, attributes and inputs.
pub struct Scene {
size: usize,
}
pub struct Scene {}

impl Scene {
/// Create a new scene with a given size.
///
/// The size represents the number of lights in the scene.
pub fn new(size: usize) -> Self {
return Self {
size,
};
}

/// Returns the size of the scene.
pub fn size(&self) -> usize {
return self.size;
pub fn new() -> Self {
return Self {};
}

/// Declares a new node in the scene.
Expand All @@ -195,7 +184,7 @@ impl Scene {
/// The returned handle represents the node in the scene and can be used to reference the node
/// in another node.
pub fn node<Decl>(&mut self, name: &str, decl: Decl) -> Result<NodeHandle<Decl>>
where Decl: NodeDecl {
where Decl: NodeDecl {
return Ok(NodeHandle {
name: name.to_owned(),
decl,
Expand All @@ -209,7 +198,7 @@ impl Scene {
/// The returned handle represents the input in the scene and can be used to reference the input
/// in other nodes and attributes.
pub fn input<V>(&mut self, name: &str) -> Result<InputHandle<V>>
where V: InputValue {
where V: InputValue {
return Ok(InputHandle {
name: name.to_owned(),
input: Input::new(),
Expand All @@ -230,22 +219,24 @@ impl Scene {
root: NodeHandle<Node>,
decl: Output,
) -> Result<Loop<Node::Node, Output::Output>>
where
Node: NodeDecl,
Output: OutputDecl,
<Output::Output as self::Output>::Element: FromColor<<Node::Node as self::Node>::Element>,
<<Node as NodeDecl>::Node as self::Node>::Element: Default, // TODO: Remove this constraint
where
Node: NodeDecl,
Output: OutputDecl,
<Output::Output as self::Output>::Element: FromColor<<Node::Node as self::Node>::Element>,
<<Node as NodeDecl>::Node as self::Node>::Element: Default, // TODO: Remove this constraint
{
let output = decl.materialize().await?;

// Materialize the node tree using a builder tracking the info object creation
let (scene, root) = SceneBuilder::build(self.size, root).await?;
let (scene, root) = SceneBuilder::build(output.size(), root).await?;

let introspection = Introspection::with(scene.root);
introspection.log();

return Ok(Loop {
nodes: scene.nodes,
root,
output: decl.materialize(self.size()).await?,
output,
stats: FrameStats::default(),
introspection,
});
Expand All @@ -257,10 +248,10 @@ impl Scene {
/// The rendering loop updates a scene and all its elements and then renders the root node to the
/// output.
pub struct Loop<Node, Output>
where
Node: self::Node + 'static, // TODO: Is static required?
Output: self::Output,
Output::Element: FromColor<Node::Element>,
where
Node: self::Node + 'static, // TODO: Is static required?
Output: self::Output,
Output::Element: FromColor<Node::Element>,
{
nodes: Arena<dyn NodeHolder>,

Expand All @@ -273,10 +264,10 @@ where
}

impl<'a, Node, Output> Loop<Node, Output>
where
Node: self::Node + 'static,
Output: self::Output,
Output::Element: FromColor<Node::Element> + Copy,
where
Node: self::Node + 'static,
Output: self::Output,
Output::Element: FromColor<Node::Element> + Copy,
{
/// Update and render a single frame.
///
Expand Down Expand Up @@ -322,7 +313,7 @@ where
}
}

pub fn serve(&self, interface: impl Interface) -> impl Future<Output = Result<()>> {
pub fn serve(&self, interface: impl Interface) -> impl Future<Output=Result<()>> {
return interface.listen(self.introspection.clone());
}
}
Expand Down Expand Up @@ -371,9 +362,9 @@ pub struct AttrBuilder<'b> {
impl SceneBuilder {
/// Create a node from its handle.
pub async fn build<Node>(size: usize, root: NodeHandle<Node>) -> Result<(Self, NodeRef<Node::Node>)>
where
Node: NodeDecl,
<<Node as NodeDecl>::Node as self::Node>::Element: Default, // TODO: Remove this constraint
where
Node: NodeDecl,
<<Node as NodeDecl>::Node as self::Node>::Element: Default, // TODO: Remove this constraint
{
let mut nodes = Arena::new();

Expand Down Expand Up @@ -417,12 +408,20 @@ impl SceneBuilder {

impl NodeBuilder<'_> {
pub async fn node<Node>(&mut self, key: impl Into<String>, decl: NodeHandle<Node>) -> Result<NodeRef<Node::Node>>
where
Node: NodeDecl,
<<Node as NodeDecl>::Node as self::Node>::Element: Default, // TODO: Remove this constraint
where
Node: NodeDecl,
<<Node as NodeDecl>::Node as self::Node>::Element: Default, // TODO: Remove this constraint
{
return self.node_with_size(key, decl, self.size).await;
}

pub async fn node_with_size<Node>(&mut self, key: impl Into<String>, decl: NodeHandle<Node>, size: usize) -> Result<NodeRef<Node::Node>>
where
Node: NodeDecl,
<<Node as NodeDecl>::Node as self::Node>::Element: Default, // TODO: Remove this constraint
{
let mut builder = NodeBuilder {
size: self.size,
size,

nodes: &mut self.nodes,

Expand All @@ -437,7 +436,7 @@ impl NodeBuilder<'_> {
let node = Node::materialize(decl.decl, &mut builder).await?;
let info = builder.info;

let buffer = Buffer::with_default(self.size);
let buffer = Buffer::with_default(size);

let node = self.nodes.append(NodeContainer {
node,
Expand All @@ -462,8 +461,8 @@ impl NodeBuilder<'_> {
decl: Attr,
bounds: impl Into<Bounds<Attr::Value>>,
) -> Result<Attr::Attr>
where
Attr: BoundAttrDecl,
where
Attr: BoundAttrDecl,
{
let bounds = bounds.into();

Expand Down Expand Up @@ -491,7 +490,7 @@ impl NodeBuilder<'_> {
/// The created attribute is registered as an attribute to the currently built node.
// TODO: Rename to `free_attr`
pub fn unbound_attr<Attr>(&mut self, name: impl Into<String>, decl: Attr) -> Result<Attr::Attr>
where Attr: FreeAttrDecl {
where Attr: FreeAttrDecl {
let mut builder = AttrBuilder {
nodes: self.nodes,
size: self.size,
Expand Down Expand Up @@ -522,8 +521,8 @@ impl<'b> AttrBuilder<'b> {
decl: Attr,
bounds: impl Into<Bounds<Attr::Value>>,
) -> Result<Attr::Attr>
where
Attr: BoundAttrDecl,
where
Attr: BoundAttrDecl,
{
let bounds = bounds.into();

Expand All @@ -550,7 +549,7 @@ impl<'b> AttrBuilder<'b> {
///
/// The created attribute is registered as an attribute to the currently built node.
pub fn unbound_attr<Attr>(&mut self, name: impl Into<String>, decl: Attr) -> Result<Attr::Attr>
where Attr: FreeAttrDecl {
where Attr: FreeAttrDecl {
let mut builder = AttrBuilder {
nodes: self.nodes,
size: self.size,
Expand All @@ -574,7 +573,7 @@ impl<'b> AttrBuilder<'b> {
///
/// The created input is registered as an input to the currently built node.
pub fn input<V>(&mut self, name: impl Into<String>, input: InputHandle<V>) -> Result<Input<V>>
where V: InputValue {
where V: InputValue {
let sink = input.sink();

let info = InputInfo {
Expand Down
20 changes: 15 additions & 5 deletions dyn/src/boxed/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use photonic::{BufferReader, Output, OutputDecl};

#[async_trait(? Send)]
pub trait DynOutputDecl {
async fn materialize(self: Box<Self>, size: usize) -> Result<BoxedOutput>;
async fn materialize(self: Box<Self>) -> Result<BoxedOutput>;
}

#[async_trait(? Send)]
Expand All @@ -19,8 +19,8 @@ where
T: OutputDecl + 'static,
<<T as OutputDecl>::Output as Output>::Element: Copy + FromColor<Rgb>,
{
async fn materialize(self: Box<Self>, size: usize) -> Result<BoxedOutput> {
let output = <T as OutputDecl>::materialize(*self, size).await?;
async fn materialize(self: Box<Self>) -> Result<BoxedOutput> {
let output = <T as OutputDecl>::materialize(*self).await?;
return Ok(Box::new(output) as Box<dyn DynOutput>);
}
}
Expand All @@ -30,14 +30,16 @@ pub type BoxedOutputDecl = Box<dyn DynOutputDecl>;
impl OutputDecl for BoxedOutputDecl {
type Output = BoxedOutput;

fn materialize(self, size: usize) -> impl Future<Output = Result<Self::Output>> {
return DynOutputDecl::materialize(self, size);
fn materialize(self) -> impl Future<Output = Result<Self::Output>> {
return DynOutputDecl::materialize(self);
}
}

#[async_trait(? Send)]
pub trait DynOutput {
async fn render(&mut self, out: &dyn BufferReader<Element = Rgb>) -> Result<()>;

fn size(&self) -> usize;
}

#[async_trait(? Send)]
Expand All @@ -49,6 +51,10 @@ where
async fn render(&mut self, out: &dyn BufferReader<Element = Rgb>) -> Result<()> {
return Output::render(self, OutputBuffer::wrap(out)).await;
}

fn size(&self) -> usize {
return Output::size(self);
}
}

pub type BoxedOutput = Box<dyn DynOutput>;
Expand All @@ -61,6 +67,10 @@ impl Output for BoxedOutput {
async fn render(&mut self, out: impl BufferReader<Element = Self::Element>) -> Result<()> {
return DynOutput::render(self.as_mut(), &out).await;
}

fn size(&self) -> usize {
return DynOutput::size(self.as_ref());
}
}

struct OutputBuffer<'a, E> {
Expand Down
Loading

0 comments on commit af3d464

Please sign in to comment.