feat(ir): replace Rpl with GraphIr
Semi-broken as atm the CLI just does nothing except printing the parsed IR, instead of actually executing it.
This commit is contained in:
parent
ccbccfb11b
commit
fcf7e909ee
14 changed files with 198 additions and 143 deletions
14
crates/ir/Cargo.toml
Normal file
14
crates/ir/Cargo.toml
Normal file
|
@ -0,0 +1,14 @@
|
|||
[package]
|
||||
name = "ir"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
ahash = { version = "0.8.7", features = ["serde"] }
|
||||
ron = "0.8"
|
||||
serde = { version = "1.0.193", features = ["derive"] }
|
||||
|
||||
[lints]
|
||||
workspace = true
|
47
crates/ir/src/instruction/mod.rs
Normal file
47
crates/ir/src/instruction/mod.rs
Normal file
|
@ -0,0 +1,47 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub mod read;
|
||||
pub mod write;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum Kind {
|
||||
Read(read::Read),
|
||||
Write(write::Write),
|
||||
Math(Math),
|
||||
Blend(Blend),
|
||||
Noise(Noise),
|
||||
Filter(Filter),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum Math {
|
||||
Add,
|
||||
Subtract,
|
||||
Multiply,
|
||||
Divide,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum Blend {
|
||||
Normal,
|
||||
Multiply,
|
||||
Additive,
|
||||
Overlay,
|
||||
Screen,
|
||||
Subtractive,
|
||||
Difference,
|
||||
Darken,
|
||||
Lighten,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum Noise {
|
||||
Perlin,
|
||||
Simplex,
|
||||
Voronoi,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum Filter {
|
||||
Invert,
|
||||
}
|
19
crates/ir/src/instruction/read.rs
Normal file
19
crates/ir/src/instruction/read.rs
Normal file
|
@ -0,0 +1,19 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Read {
|
||||
pub source: SourceType,
|
||||
pub format: SourceFormat,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum SourceType {
|
||||
File(PathBuf),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum SourceFormat {
|
||||
Jpeg,
|
||||
Png,
|
||||
}
|
19
crates/ir/src/instruction/write.rs
Normal file
19
crates/ir/src/instruction/write.rs
Normal file
|
@ -0,0 +1,19 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Write {
|
||||
pub target: TargetType,
|
||||
pub format: TargetFormat,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum TargetType {
|
||||
File(PathBuf),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum TargetFormat {
|
||||
Jpeg,
|
||||
Png,
|
||||
}
|
108
crates/ir/src/lib.rs
Normal file
108
crates/ir/src/lib.rs
Normal file
|
@ -0,0 +1,108 @@
|
|||
use std::ops::RangeInclusive;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub mod instruction;
|
||||
|
||||
pub type Map<K, V> = ahash::AHashMap<K, V>;
|
||||
pub type Set<V> = ahash::AHashSet<V>;
|
||||
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns an error if the parsed source is not a valid graph IR.
|
||||
pub fn from_ron(source: &str) -> ron::error::SpannedResult<GraphIr> {
|
||||
ron::from_str(source)
|
||||
}
|
||||
|
||||
/// The toplevel representation of a whole pipeline.
|
||||
///
|
||||
/// Pipelines may not be fully linear. They may branch out and recombine later on.
|
||||
/// As such, the representation for them which is currently used is a
|
||||
/// [**D**irected **A**cyclic **G**raph](https://en.wikipedia.org/wiki/Directed_acyclic_graph)
|
||||
/// .
|
||||
///
|
||||
/// For those who are already familiar with graphs, a DAG is one, except that:
|
||||
///
|
||||
/// - It is **directed**: Edges have a direction they point to.
|
||||
/// In this case, edges point from the outputs of streamers to inputs of consumers.
|
||||
/// - It is **acyclic**: Those directed edges may not form loops.
|
||||
/// In other words, if one follows edges only in their direction, it must be impossible
|
||||
/// to come back to an already visited node.
|
||||
///
|
||||
/// Here, if an edge points from _A_ to _B_ (`A --> B`),
|
||||
/// then _A_ is called a **dependency** of _B_,
|
||||
/// and _B_ is called a **dependent** of _A_.
|
||||
///
|
||||
/// The DAG also enables another neat operation:
|
||||
/// [Topological sorting](https://en.wikipedia.org/wiki/Topological_sorting).
|
||||
/// This allows to put the entire graph into a linear list,
|
||||
/// where it's guaranteed that once a vertex is visited,
|
||||
/// all dependencies of it will have been visited already as well.
|
||||
///
|
||||
/// The representation used here in specific is a bit more complicated,
|
||||
/// since **instructions** directly aren't just connected to one another,
|
||||
/// but their **sockets** are instead.
|
||||
///
|
||||
/// So the vertices of the DAG are the **sockets**
|
||||
/// (which are either [`id::Input`] or [`id::Output`] depending on the direction),
|
||||
/// and each **socket** in turn belongs to an **instruction**.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
|
||||
pub struct GraphIr {
|
||||
/// "Backbone" storage of all **instruction** IDs to
|
||||
/// what **kind of instruction** they are.
|
||||
instructions: Map<id::Instruction, instruction::Kind>,
|
||||
|
||||
/// How the data flows forward. **Dependencies** map to **dependents** here.
|
||||
edges: Map<id::Output, Set<id::Input>>,
|
||||
/// How the data flows backward. **Dependents** map to **dependencies** here.
|
||||
rev_edges: Map<id::Input, Set<id::Output>>,
|
||||
}
|
||||
|
||||
pub mod id {
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::Span;
|
||||
|
||||
/// One specific instruction, and where it is found in code.
|
||||
///
|
||||
/// It does **not** contain what kind of instruction this is.
|
||||
/// Refer to [`crate::instruction::Kind`] for this instead.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
|
||||
pub struct Instruction(Span);
|
||||
|
||||
impl Instruction {
|
||||
/// Where this instruction is written down.
|
||||
pub fn span(&self) -> &Span {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
/// On an **instruction**, accepts incoming data.
|
||||
///
|
||||
/// An **instruction** cannot run if any of these are not connected.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
|
||||
pub struct Input(Socket);
|
||||
|
||||
/// On an **instruction**, returns outgoing data to be fed to [`Input`]s.
|
||||
///
|
||||
/// In contrast to [`Input`]s, [`Output`]s may be used or unused.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
|
||||
pub struct Output(Socket);
|
||||
|
||||
/// An unspecified socket on a specific **instruction**,
|
||||
/// and where it is on that **instruction**.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
|
||||
pub struct Socket {
|
||||
pub belongs_to: Instruction,
|
||||
pub index: SocketIdx,
|
||||
}
|
||||
|
||||
/// Where a [`Socket`] is on an **instruction**.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
|
||||
pub struct SocketIdx(pub u16);
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
|
||||
pub struct Span {
|
||||
range: RangeInclusive<usize>,
|
||||
}
|
3
crates/ir/src/value/mod.rs
Normal file
3
crates/ir/src/value/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
pub enum DynamicValue {
|
||||
Image(DynamicImage),
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue