|
@@ -0,0 +1,1671 @@
|
|
|
|
|
+//! Graph-to-RDP Compiler & Software Reference Combiner Evaluator.
|
|
|
|
|
+//!
|
|
|
|
|
+//! Compiles a `NodeGraph` into a real 64-bit Nintendo 64 `G_SETCOMBINE` (`0xFC`)
|
|
|
|
|
+//! command word plus pipeline state (Cycle 1 vs Cycle 2, PRIM/ENV colors,
|
|
|
|
|
+//! texture configurations, Fresnel vertex alpha, and blender mode).
|
|
|
|
|
+
|
|
|
|
|
+use crate::graph::{BlendMode, NodeGraph, NodeId, NodeType, PinId, TexPattern};
|
|
|
|
|
+use heapless::{String, Vec};
|
|
|
|
|
+
|
|
|
|
|
+#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
|
+pub enum CycleCount {
|
|
|
|
|
+ OneCycle,
|
|
|
|
|
+ TwoCycle,
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
|
+pub struct TexConfig {
|
|
|
|
|
+ pub pattern: TexPattern,
|
|
|
|
|
+ pub scroll_x: i8,
|
|
|
|
|
+ pub scroll_y: i8,
|
|
|
|
|
+ pub env_map: bool,
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+#[derive(Clone, Debug, PartialEq, Eq)]
|
|
|
|
|
+pub struct CompiledShader {
|
|
|
|
|
+ pub combine_cmd: u64,
|
|
|
|
|
+ pub cycles: CycleCount,
|
|
|
|
|
+ pub prim_color: [u8; 4],
|
|
|
|
|
+ pub prim_lod_frac: u8,
|
|
|
|
|
+ pub env_color: [u8; 4],
|
|
|
|
|
+ pub tex0: Option<TexConfig>,
|
|
|
|
|
+ pub tex1: Option<TexConfig>,
|
|
|
|
|
+ pub uses_shade: bool,
|
|
|
|
|
+ pub uses_fresnel: bool,
|
|
|
|
|
+ pub fresnel_power: u8,
|
|
|
|
|
+ pub fresnel_bias: u8,
|
|
|
|
|
+ pub blend_mode: BlendMode,
|
|
|
|
|
+ pub cycle1_text: String<40>,
|
|
|
|
|
+ pub cycle2_text: String<40>,
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
|
+pub enum CompileError {
|
|
|
|
|
+ MissingOutput,
|
|
|
|
|
+ UnconnectedOutput(NodeId),
|
|
|
|
|
+ CycleDetected(NodeId),
|
|
|
|
|
+ ExceedsTwoCycles(NodeId),
|
|
|
|
|
+ Cycle2Tex0Quirk(NodeId),
|
|
|
|
|
+ InvalidSlot { node: NodeId, reason: &'static str },
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+impl CompileError {
|
|
|
|
|
+ pub fn offending_node(&self) -> Option<NodeId> {
|
|
|
|
|
+ match *self {
|
|
|
|
|
+ Self::MissingOutput => None,
|
|
|
|
|
+ Self::UnconnectedOutput(n)
|
|
|
|
|
+ | Self::CycleDetected(n)
|
|
|
|
|
+ | Self::ExceedsTwoCycles(n)
|
|
|
|
|
+ | Self::Cycle2Tex0Quirk(n)
|
|
|
|
|
+ | Self::InvalidSlot { node: n, .. } => Some(n),
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ pub const fn summary(&self) -> &'static str {
|
|
|
|
|
+ match self {
|
|
|
|
|
+ Self::MissingOutput => "MISSING OUTPUT NODE",
|
|
|
|
|
+ Self::UnconnectedOutput(_) => "OUTPUT COL NOT WIRED",
|
|
|
|
|
+ Self::CycleDetected(_) => "GRAPH CYCLE DETECTED",
|
|
|
|
|
+ Self::ExceedsTwoCycles(_) => "EXCEEDS 2 RDP CYCLES",
|
|
|
|
|
+ Self::Cycle2Tex0Quirk(_) => "CYC2 TEX0 HW QUIRK",
|
|
|
|
|
+ Self::InvalidSlot { reason, .. } => reason,
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/// Hardware mux source tokens (abstracted before Cycle-1 vs Cycle-2 code encoding).
|
|
|
|
|
+#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
|
+enum RgbSrc {
|
|
|
|
|
+ Combined,
|
|
|
|
|
+ Tex0,
|
|
|
|
|
+ Tex1,
|
|
|
|
|
+ Prim,
|
|
|
|
|
+ Shade,
|
|
|
|
|
+ Env,
|
|
|
|
|
+ One,
|
|
|
|
|
+ Noise,
|
|
|
|
|
+ Zero,
|
|
|
|
|
+ Tex0Alpha,
|
|
|
|
|
+ Tex1Alpha,
|
|
|
|
|
+ PrimAlpha,
|
|
|
|
|
+ ShadeAlpha,
|
|
|
|
|
+ EnvAlpha,
|
|
|
|
|
+ PrimLodFrac,
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
|
+enum AlphaSrc {
|
|
|
|
|
+ Combined,
|
|
|
|
|
+ Tex0,
|
|
|
|
|
+ Tex1,
|
|
|
|
|
+ Prim,
|
|
|
|
|
+ Shade,
|
|
|
|
|
+ Env,
|
|
|
|
|
+ One,
|
|
|
|
|
+ Zero,
|
|
|
|
|
+ PrimLodFrac,
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
|
+struct RgbStage {
|
|
|
|
|
+ a: RgbSrc,
|
|
|
|
|
+ b: RgbSrc,
|
|
|
|
|
+ c: RgbSrc,
|
|
|
|
|
+ d: RgbSrc,
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+impl RgbStage {
|
|
|
|
|
+ const fn pass_combined() -> Self {
|
|
|
|
|
+ Self {
|
|
|
|
|
+ a: RgbSrc::Zero,
|
|
|
|
|
+ b: RgbSrc::Zero,
|
|
|
|
|
+ c: RgbSrc::Zero,
|
|
|
|
|
+ d: RgbSrc::Combined,
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
|
+struct AlphaStage {
|
|
|
|
|
+ a: AlphaSrc,
|
|
|
|
|
+ b: AlphaSrc,
|
|
|
|
|
+ c: AlphaSrc,
|
|
|
|
|
+ d: AlphaSrc,
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+impl AlphaStage {
|
|
|
|
|
+ const fn pass_one() -> Self {
|
|
|
|
|
+ Self {
|
|
|
|
|
+ a: AlphaSrc::Zero,
|
|
|
|
|
+ b: AlphaSrc::Zero,
|
|
|
|
|
+ c: AlphaSrc::Zero,
|
|
|
|
|
+ d: AlphaSrc::One,
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const fn pass_combined() -> Self {
|
|
|
|
|
+ Self {
|
|
|
|
|
+ a: AlphaSrc::Zero,
|
|
|
|
|
+ b: AlphaSrc::Zero,
|
|
|
|
|
+ c: AlphaSrc::Zero,
|
|
|
|
|
+ d: AlphaSrc::Combined,
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+impl RgbSrc {
|
|
|
|
|
+ fn is_alpha_only(self) -> bool {
|
|
|
|
|
+ matches!(
|
|
|
|
|
+ self,
|
|
|
|
|
+ Self::Tex0Alpha
|
|
|
|
|
+ | Self::Tex1Alpha
|
|
|
|
|
+ | Self::PrimAlpha
|
|
|
|
|
+ | Self::ShadeAlpha
|
|
|
|
|
+ | Self::EnvAlpha
|
|
|
|
|
+ | Self::PrimLodFrac
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ fn label(self) -> &'static str {
|
|
|
|
|
+ match self {
|
|
|
|
|
+ Self::Combined => "CMB",
|
|
|
|
|
+ Self::Tex0 => "T0",
|
|
|
|
|
+ Self::Tex1 => "T1",
|
|
|
|
|
+ Self::Prim => "PRM",
|
|
|
|
|
+ Self::Shade => "SHD",
|
|
|
|
|
+ Self::Env => "ENV",
|
|
|
|
|
+ Self::One => "1",
|
|
|
|
|
+ Self::Noise => "NSE",
|
|
|
|
|
+ Self::Zero => "0",
|
|
|
|
|
+ Self::Tex0Alpha => "T0a",
|
|
|
|
|
+ Self::Tex1Alpha => "T1a",
|
|
|
|
|
+ Self::PrimAlpha => "PRMa",
|
|
|
|
|
+ Self::ShadeAlpha => "SHDa",
|
|
|
|
|
+ Self::EnvAlpha => "ENVa",
|
|
|
|
|
+ Self::PrimLodFrac => "PLOD",
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ fn encode_a(self, cyc2: bool, node: NodeId) -> Result<u64, CompileError> {
|
|
|
|
|
+ match (cyc2, self) {
|
|
|
|
|
+ (false, Self::Combined) => Err(CompileError::InvalidSlot {
|
|
|
|
|
+ node,
|
|
|
|
|
+ reason: "COMBINED ONLY IN CYC2",
|
|
|
|
|
+ }),
|
|
|
|
|
+ (true, Self::Tex0) | (true, Self::Tex0Alpha) => Err(CompileError::Cycle2Tex0Quirk(node)),
|
|
|
|
|
+ (_, Self::Combined) => Ok(0),
|
|
|
|
|
+ (false, Self::Tex0) => Ok(1),
|
|
|
|
|
+ (false, Self::Tex1) => Ok(2),
|
|
|
|
|
+ (true, Self::Tex1) => Ok(1),
|
|
|
|
|
+ (_, Self::Prim) => Ok(3),
|
|
|
|
|
+ (_, Self::Shade) => Ok(4),
|
|
|
|
|
+ (_, Self::Env) => Ok(5),
|
|
|
|
|
+ (_, Self::One) => Ok(6),
|
|
|
|
|
+ (_, Self::Noise) => Ok(7),
|
|
|
|
|
+ (_, Self::Zero) => Ok(8),
|
|
|
|
|
+ _ => Err(CompileError::InvalidSlot {
|
|
|
|
|
+ node,
|
|
|
|
|
+ reason: "BAD SLOT A INPUT",
|
|
|
|
|
+ }),
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ fn encode_b(self, cyc2: bool, node: NodeId) -> Result<u64, CompileError> {
|
|
|
|
|
+ match (cyc2, self) {
|
|
|
|
|
+ (false, Self::Combined) => Err(CompileError::InvalidSlot {
|
|
|
|
|
+ node,
|
|
|
|
|
+ reason: "COMBINED ONLY IN CYC2",
|
|
|
|
|
+ }),
|
|
|
|
|
+ (true, Self::Tex0) | (true, Self::Tex0Alpha) => Err(CompileError::Cycle2Tex0Quirk(node)),
|
|
|
|
|
+ (_, Self::Combined) => Ok(0),
|
|
|
|
|
+ (false, Self::Tex0) => Ok(1),
|
|
|
|
|
+ (false, Self::Tex1) => Ok(2),
|
|
|
|
|
+ (true, Self::Tex1) => Ok(1),
|
|
|
|
|
+ (_, Self::Prim) => Ok(3),
|
|
|
|
|
+ (_, Self::Shade) => Ok(4),
|
|
|
|
|
+ (_, Self::Env) => Ok(5),
|
|
|
|
|
+ (_, Self::Zero) => Ok(8),
|
|
|
|
|
+ _ => Err(CompileError::InvalidSlot {
|
|
|
|
|
+ node,
|
|
|
|
|
+ reason: "BAD SLOT B INPUT",
|
|
|
|
|
+ }),
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ fn encode_c(self, cyc2: bool, node: NodeId) -> Result<u64, CompileError> {
|
|
|
|
|
+ match (cyc2, self) {
|
|
|
|
|
+ (false, Self::Combined) => Err(CompileError::InvalidSlot {
|
|
|
|
|
+ node,
|
|
|
|
|
+ reason: "COMBINED ONLY IN CYC2",
|
|
|
|
|
+ }),
|
|
|
|
|
+ (true, Self::Tex0) | (true, Self::Tex0Alpha) => Err(CompileError::Cycle2Tex0Quirk(node)),
|
|
|
|
|
+ (_, Self::Combined) => Ok(0),
|
|
|
|
|
+ (false, Self::Tex0) => Ok(1),
|
|
|
|
|
+ (false, Self::Tex1) => Ok(2),
|
|
|
|
|
+ (true, Self::Tex1) => Ok(1),
|
|
|
|
|
+ (_, Self::Prim) => Ok(3),
|
|
|
|
|
+ (_, Self::Shade) => Ok(4),
|
|
|
|
|
+ (_, Self::Env) => Ok(5),
|
|
|
|
|
+ (false, Self::Tex0Alpha) => Ok(8),
|
|
|
|
|
+ (false, Self::Tex1Alpha) => Ok(9),
|
|
|
|
|
+ (true, Self::Tex1Alpha) => Ok(8),
|
|
|
|
|
+ (_, Self::PrimAlpha) => Ok(10),
|
|
|
|
|
+ (_, Self::ShadeAlpha) => Ok(11),
|
|
|
|
|
+ (_, Self::EnvAlpha) => Ok(12),
|
|
|
|
|
+ (_, Self::PrimLodFrac) => Ok(14),
|
|
|
|
|
+ (_, Self::Zero) => Ok(16),
|
|
|
|
|
+ _ => Err(CompileError::InvalidSlot {
|
|
|
|
|
+ node,
|
|
|
|
|
+ reason: "BAD SLOT C INPUT",
|
|
|
|
|
+ }),
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ fn encode_d(self, cyc2: bool, node: NodeId) -> Result<u64, CompileError> {
|
|
|
|
|
+ match (cyc2, self) {
|
|
|
|
|
+ (false, Self::Combined) => Err(CompileError::InvalidSlot {
|
|
|
|
|
+ node,
|
|
|
|
|
+ reason: "COMBINED ONLY IN CYC2",
|
|
|
|
|
+ }),
|
|
|
|
|
+ (true, Self::Tex0) | (true, Self::Tex0Alpha) => Err(CompileError::Cycle2Tex0Quirk(node)),
|
|
|
|
|
+ (_, Self::Combined) => Ok(0),
|
|
|
|
|
+ (false, Self::Tex0) => Ok(1),
|
|
|
|
|
+ (false, Self::Tex1) => Ok(2),
|
|
|
|
|
+ (true, Self::Tex1) => Ok(1),
|
|
|
|
|
+ (_, Self::Prim) => Ok(3),
|
|
|
|
|
+ (_, Self::Shade) => Ok(4),
|
|
|
|
|
+ (_, Self::Env) => Ok(5),
|
|
|
|
|
+ (_, Self::One) => Ok(6),
|
|
|
|
|
+ (_, Self::Zero) => Ok(7),
|
|
|
|
|
+ _ => Err(CompileError::InvalidSlot {
|
|
|
|
|
+ node,
|
|
|
|
|
+ reason: "BAD SLOT D INPUT",
|
|
|
|
|
+ }),
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+impl AlphaSrc {
|
|
|
|
|
+ fn encode_abd(self, cyc2: bool, node: NodeId) -> Result<u64, CompileError> {
|
|
|
|
|
+ match (cyc2, self) {
|
|
|
|
|
+ (false, Self::Combined) => Err(CompileError::InvalidSlot {
|
|
|
|
|
+ node,
|
|
|
|
|
+ reason: "COMBINED ONLY IN CYC2",
|
|
|
|
|
+ }),
|
|
|
|
|
+ (true, Self::Tex0) => Err(CompileError::Cycle2Tex0Quirk(node)),
|
|
|
|
|
+ (_, Self::Combined) => Ok(0),
|
|
|
|
|
+ (false, Self::Tex0) => Ok(1),
|
|
|
|
|
+ (false, Self::Tex1) => Ok(2),
|
|
|
|
|
+ (true, Self::Tex1) => Ok(1),
|
|
|
|
|
+ (_, Self::Prim) => Ok(3),
|
|
|
|
|
+ (_, Self::Shade) => Ok(4),
|
|
|
|
|
+ (_, Self::Env) => Ok(5),
|
|
|
|
|
+ (_, Self::One) => Ok(6),
|
|
|
|
|
+ (_, Self::Zero) => Ok(7),
|
|
|
|
|
+ (_, Self::PrimLodFrac) => Err(CompileError::InvalidSlot {
|
|
|
|
|
+ node,
|
|
|
|
|
+ reason: "PLOD ONLY IN ALPHA C",
|
|
|
|
|
+ }),
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ fn encode_c(self, cyc2: bool, node: NodeId) -> Result<u64, CompileError> {
|
|
|
|
|
+ match (cyc2, self) {
|
|
|
|
|
+ (true, Self::Tex0) => Err(CompileError::Cycle2Tex0Quirk(node)),
|
|
|
|
|
+ (false, Self::Tex0) => Ok(1),
|
|
|
|
|
+ (false, Self::Tex1) => Ok(2),
|
|
|
|
|
+ (true, Self::Tex1) => Ok(1),
|
|
|
|
|
+ (_, Self::Prim) => Ok(3),
|
|
|
|
|
+ (_, Self::Shade) => Ok(4),
|
|
|
|
|
+ (_, Self::Env) => Ok(5),
|
|
|
|
|
+ (_, Self::PrimLodFrac) => Ok(6),
|
|
|
|
|
+ (_, Self::Zero) => Ok(7),
|
|
|
|
|
+ _ => Err(CompileError::InvalidSlot {
|
|
|
|
|
+ node,
|
|
|
|
|
+ reason: "BAD ALPHA C INPUT",
|
|
|
|
|
+ }),
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+struct CompileCtx {
|
|
|
|
|
+ prim_color: Option<[u8; 4]>,
|
|
|
|
|
+ prim_lod_frac: Option<u8>,
|
|
|
|
|
+ env_color: Option<[u8; 4]>,
|
|
|
|
|
+ tex0: Option<TexConfig>,
|
|
|
|
|
+ tex1: Option<TexConfig>,
|
|
|
|
|
+ uses_shade: bool,
|
|
|
|
|
+ fresnel: Option<(u8, u8)>,
|
|
|
|
|
+ visiting: Vec<NodeId, 32>,
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+impl CompileCtx {
|
|
|
|
|
+ fn new() -> Self {
|
|
|
|
|
+ Self {
|
|
|
|
|
+ prim_color: None,
|
|
|
|
|
+ prim_lod_frac: None,
|
|
|
|
|
+ env_color: None,
|
|
|
|
|
+ tex0: None,
|
|
|
|
|
+ tex1: None,
|
|
|
|
|
+ uses_shade: false,
|
|
|
|
|
+ fresnel: None,
|
|
|
|
|
+ visiting: Vec::new(),
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ fn set_prim(&mut self, rgba: [u8; 4], node: NodeId) -> Result<(), CompileError> {
|
|
|
|
|
+ if let Some(prev) = self.prim_color {
|
|
|
|
|
+ if prev != rgba {
|
|
|
|
|
+ return Err(CompileError::InvalidSlot {
|
|
|
|
|
+ node,
|
|
|
|
|
+ reason: "CONFLICTING PRIM COLOR",
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ self.prim_color = Some(rgba);
|
|
|
|
|
+ Ok(())
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ fn set_env(&mut self, rgba: [u8; 4], node: NodeId) -> Result<(), CompileError> {
|
|
|
|
|
+ if let Some(prev) = self.env_color {
|
|
|
|
|
+ if prev != rgba {
|
|
|
|
|
+ return Err(CompileError::InvalidSlot {
|
|
|
|
|
+ node,
|
|
|
|
|
+ reason: "CONFLICTING ENV COLOR",
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ self.env_color = Some(rgba);
|
|
|
|
|
+ Ok(())
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ fn set_lod_frac(&mut self, val: u8, node: NodeId) -> Result<(), CompileError> {
|
|
|
|
|
+ if let Some(prev) = self.prim_lod_frac {
|
|
|
|
|
+ if prev != val {
|
|
|
|
|
+ return Err(CompileError::InvalidSlot {
|
|
|
|
|
+ node,
|
|
|
|
|
+ reason: "TWO MIX FACTORS USED",
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ self.prim_lod_frac = Some(val);
|
|
|
|
|
+ Ok(())
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+#[derive(Copy, Clone, Debug)]
|
|
|
|
|
+enum Expr {
|
|
|
|
|
+ Leaf(RgbSrc, NodeId),
|
|
|
|
|
+ Stage {
|
|
|
|
|
+ node: NodeId,
|
|
|
|
|
+ stage: RgbStage,
|
|
|
|
|
+ child: Option<(NodeId, RgbStage)>,
|
|
|
|
|
+ },
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+#[derive(Copy, Clone, Debug)]
|
|
|
|
|
+enum AlphaExpr {
|
|
|
|
|
+ Leaf(AlphaSrc, NodeId),
|
|
|
|
|
+ Stage {
|
|
|
|
|
+ node: NodeId,
|
|
|
|
|
+ stage: AlphaStage,
|
|
|
|
|
+ child: Option<(NodeId, AlphaStage)>,
|
|
|
|
|
+ },
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+fn pin_to_rgb_leaf(graph: &NodeGraph, pin: PinId, ctx: &mut CompileCtx) -> Result<RgbSrc, CompileError> {
|
|
|
|
|
+ let node = graph.node(pin.node).ok_or(CompileError::MissingOutput)?;
|
|
|
|
|
+ match node.kind {
|
|
|
|
|
+ NodeType::Texture0 {
|
|
|
|
|
+ pattern,
|
|
|
|
|
+ scroll_x,
|
|
|
|
|
+ scroll_y,
|
|
|
|
|
+ env_map,
|
|
|
|
|
+ } => {
|
|
|
|
|
+ ctx.tex0 = Some(TexConfig {
|
|
|
|
|
+ pattern,
|
|
|
|
|
+ scroll_x,
|
|
|
|
|
+ scroll_y,
|
|
|
|
|
+ env_map,
|
|
|
|
|
+ });
|
|
|
|
|
+ Ok(if pin.slot == 1 {
|
|
|
|
|
+ RgbSrc::Tex0Alpha
|
|
|
|
|
+ } else {
|
|
|
|
|
+ RgbSrc::Tex0
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ NodeType::Texture1 {
|
|
|
|
|
+ pattern,
|
|
|
|
|
+ scroll_x,
|
|
|
|
|
+ scroll_y,
|
|
|
|
|
+ env_map,
|
|
|
|
|
+ } => {
|
|
|
|
|
+ ctx.tex1 = Some(TexConfig {
|
|
|
|
|
+ pattern,
|
|
|
|
|
+ scroll_x,
|
|
|
|
|
+ scroll_y,
|
|
|
|
|
+ env_map,
|
|
|
|
|
+ });
|
|
|
|
|
+ Ok(if pin.slot == 1 {
|
|
|
|
|
+ RgbSrc::Tex1Alpha
|
|
|
|
|
+ } else {
|
|
|
|
|
+ RgbSrc::Tex1
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ NodeType::VertexColor => {
|
|
|
|
|
+ ctx.uses_shade = true;
|
|
|
|
|
+ Ok(if pin.slot == 1 {
|
|
|
|
|
+ RgbSrc::ShadeAlpha
|
|
|
|
|
+ } else {
|
|
|
|
|
+ RgbSrc::Shade
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ NodeType::PrimColor { rgba } => {
|
|
|
|
|
+ ctx.set_prim(rgba, node.id)?;
|
|
|
|
|
+ Ok(if pin.slot == 1 {
|
|
|
|
|
+ RgbSrc::PrimAlpha
|
|
|
|
|
+ } else {
|
|
|
|
|
+ RgbSrc::Prim
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ NodeType::EnvColor { rgba } => {
|
|
|
|
|
+ ctx.set_env(rgba, node.id)?;
|
|
|
|
|
+ Ok(if pin.slot == 1 {
|
|
|
|
|
+ RgbSrc::EnvAlpha
|
|
|
|
|
+ } else {
|
|
|
|
|
+ RgbSrc::Env
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ NodeType::Noise => Ok(RgbSrc::Noise),
|
|
|
|
|
+ NodeType::Fresnel { power, bias } => {
|
|
|
|
|
+ ctx.uses_shade = true;
|
|
|
|
|
+ ctx.fresnel = Some((power, bias));
|
|
|
|
|
+ Ok(RgbSrc::ShadeAlpha)
|
|
|
|
|
+ }
|
|
|
|
|
+ _ => Err(CompileError::InvalidSlot {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ reason: "UNEXPECTED LEAF NODE",
|
|
|
|
|
+ }),
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+fn pin_to_alpha_leaf(graph: &NodeGraph, pin: PinId, ctx: &mut CompileCtx) -> Result<AlphaSrc, CompileError> {
|
|
|
|
|
+ let node = graph.node(pin.node).ok_or(CompileError::MissingOutput)?;
|
|
|
|
|
+ match node.kind {
|
|
|
|
|
+ NodeType::Texture0 {
|
|
|
|
|
+ pattern,
|
|
|
|
|
+ scroll_x,
|
|
|
|
|
+ scroll_y,
|
|
|
|
|
+ env_map,
|
|
|
|
|
+ } => {
|
|
|
|
|
+ ctx.tex0 = Some(TexConfig {
|
|
|
|
|
+ pattern,
|
|
|
|
|
+ scroll_x,
|
|
|
|
|
+ scroll_y,
|
|
|
|
|
+ env_map,
|
|
|
|
|
+ });
|
|
|
|
|
+ Ok(AlphaSrc::Tex0)
|
|
|
|
|
+ }
|
|
|
|
|
+ NodeType::Texture1 {
|
|
|
|
|
+ pattern,
|
|
|
|
|
+ scroll_x,
|
|
|
|
|
+ scroll_y,
|
|
|
|
|
+ env_map,
|
|
|
|
|
+ } => {
|
|
|
|
|
+ ctx.tex1 = Some(TexConfig {
|
|
|
|
|
+ pattern,
|
|
|
|
|
+ scroll_x,
|
|
|
|
|
+ scroll_y,
|
|
|
|
|
+ env_map,
|
|
|
|
|
+ });
|
|
|
|
|
+ Ok(AlphaSrc::Tex1)
|
|
|
|
|
+ }
|
|
|
|
|
+ NodeType::VertexColor => {
|
|
|
|
|
+ ctx.uses_shade = true;
|
|
|
|
|
+ Ok(AlphaSrc::Shade)
|
|
|
|
|
+ }
|
|
|
|
|
+ NodeType::PrimColor { rgba } => {
|
|
|
|
|
+ ctx.set_prim(rgba, node.id)?;
|
|
|
|
|
+ Ok(AlphaSrc::Prim)
|
|
|
|
|
+ }
|
|
|
|
|
+ NodeType::EnvColor { rgba } => {
|
|
|
|
|
+ ctx.set_env(rgba, node.id)?;
|
|
|
|
|
+ Ok(AlphaSrc::Env)
|
|
|
|
|
+ }
|
|
|
|
|
+ NodeType::Fresnel { power, bias } => {
|
|
|
|
|
+ ctx.uses_shade = true;
|
|
|
|
|
+ ctx.fresnel = Some((power, bias));
|
|
|
|
|
+ Ok(AlphaSrc::Shade)
|
|
|
|
|
+ }
|
|
|
|
|
+ _ => Err(CompileError::InvalidSlot {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ reason: "INVALID ALPHA INPUT",
|
|
|
|
|
+ }),
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+fn stage_mul_rgb(x: RgbSrc, y: RgbSrc, node: NodeId) -> Result<RgbStage, CompileError> {
|
|
|
|
|
+ // `(A - 0) * C + 0`: Noise and normal colors prefer `A`; alpha-only sources MUST go in `C`.
|
|
|
|
|
+ let (a, c) = if x.is_alpha_only() && !y.is_alpha_only() {
|
|
|
|
|
+ (y, x)
|
|
|
|
|
+ } else if y == RgbSrc::Noise && x != RgbSrc::Noise && !x.is_alpha_only() {
|
|
|
|
|
+ (y, x)
|
|
|
|
|
+ } else if x.is_alpha_only() && y.is_alpha_only() {
|
|
|
|
|
+ return Err(CompileError::InvalidSlot {
|
|
|
|
|
+ node,
|
|
|
|
|
+ reason: "CANNOT MUL TWO ALPHAS",
|
|
|
|
|
+ });
|
|
|
|
|
+ } else if x == RgbSrc::Noise && y == RgbSrc::Noise {
|
|
|
|
|
+ return Err(CompileError::InvalidSlot {
|
|
|
|
|
+ node,
|
|
|
|
|
+ reason: "CANNOT MUL NOISE*NOISE",
|
|
|
|
|
+ });
|
|
|
|
|
+ } else {
|
|
|
|
|
+ (x, y)
|
|
|
|
|
+ };
|
|
|
|
|
+ Ok(RgbStage {
|
|
|
|
|
+ a,
|
|
|
|
|
+ b: RgbSrc::Zero,
|
|
|
|
|
+ c,
|
|
|
|
|
+ d: RgbSrc::Zero,
|
|
|
|
|
+ })
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+fn stage_add_rgb(x: RgbSrc, y: RgbSrc, node: NodeId) -> Result<RgbStage, CompileError> {
|
|
|
|
|
+ // `(1 - 0) * C + D`: `C` can hold alpha sources or colors (not Noise), `D` can hold colors (not Noise or alpha-only).
|
|
|
|
|
+ let (c, d) = if (y.is_alpha_only() || x == RgbSrc::Noise) && !x.is_alpha_only() && y != RgbSrc::Noise {
|
|
|
|
|
+ (y, x)
|
|
|
|
|
+ } else if x == RgbSrc::Noise && y == RgbSrc::Noise {
|
|
|
|
|
+ return Err(CompileError::InvalidSlot {
|
|
|
|
|
+ node,
|
|
|
|
|
+ reason: "CANNOT ADD NOISE+NOISE",
|
|
|
|
|
+ });
|
|
|
|
|
+ } else if x.is_alpha_only() && y.is_alpha_only() {
|
|
|
|
|
+ return Err(CompileError::InvalidSlot {
|
|
|
|
|
+ node,
|
|
|
|
|
+ reason: "CANNOT ADD TWO ALPHAS",
|
|
|
|
|
+ });
|
|
|
|
|
+ } else {
|
|
|
|
|
+ (x, y)
|
|
|
|
|
+ };
|
|
|
|
|
+ if c == RgbSrc::Noise || d == RgbSrc::Noise || d.is_alpha_only() {
|
|
|
|
|
+ return Err(CompileError::InvalidSlot {
|
|
|
|
|
+ node,
|
|
|
|
|
+ reason: "INVALID ADD OPERANDS",
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ Ok(RgbStage {
|
|
|
|
|
+ a: RgbSrc::One,
|
|
|
|
|
+ b: RgbSrc::Zero,
|
|
|
|
|
+ c,
|
|
|
|
|
+ d,
|
|
|
|
|
+ })
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+fn stage_fma_rgb(
|
|
|
|
|
+ mul_x: RgbSrc,
|
|
|
|
|
+ mul_y: RgbSrc,
|
|
|
|
|
+ add_z: RgbSrc,
|
|
|
|
|
+ node: NodeId,
|
|
|
|
|
+) -> Result<RgbStage, CompileError> {
|
|
|
|
|
+ let mul = stage_mul_rgb(mul_x, mul_y, node)?;
|
|
|
|
|
+ if add_z == RgbSrc::Noise || add_z.is_alpha_only() {
|
|
|
|
|
+ return Err(CompileError::InvalidSlot {
|
|
|
|
|
+ node,
|
|
|
|
|
+ reason: "INVALID FMA ADDEND",
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ Ok(RgbStage {
|
|
|
|
|
+ a: mul.a,
|
|
|
|
|
+ b: RgbSrc::Zero,
|
|
|
|
|
+ c: mul.c,
|
|
|
|
|
+ d: add_z,
|
|
|
|
|
+ })
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+fn stage_mix_rgb(
|
|
|
|
|
+ a_val: RgbSrc,
|
|
|
|
|
+ b_val: RgbSrc,
|
|
|
|
|
+ t_val: RgbSrc,
|
|
|
|
|
+ const_factor: Option<u8>,
|
|
|
|
|
+ ctx: &mut CompileCtx,
|
|
|
|
|
+ node: NodeId,
|
|
|
|
|
+) -> Result<RgbStage, CompileError> {
|
|
|
|
|
+ // Standard lerp: `(B - A) * T + A`.
|
|
|
|
|
+ // If `A == Noise` (only valid in slot A) and `T` is constant, swap to `(Noise - B) * (1 - T) + B`.
|
|
|
|
|
+ if a_val == RgbSrc::Noise {
|
|
|
|
|
+ if let Some(f) = const_factor {
|
|
|
|
|
+ ctx.set_lod_frac(255u8.saturating_sub(f), node)?;
|
|
|
|
|
+ return Ok(RgbStage {
|
|
|
|
|
+ a: RgbSrc::Noise,
|
|
|
|
|
+ b: b_val,
|
|
|
|
|
+ c: RgbSrc::PrimLodFrac,
|
|
|
|
|
+ d: b_val,
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ return Err(CompileError::InvalidSlot {
|
|
|
|
|
+ node,
|
|
|
|
|
+ reason: "NOISE INVALID IN MIX A",
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ if a_val.is_alpha_only() || b_val.is_alpha_only() {
|
|
|
|
|
+ return Err(CompileError::InvalidSlot {
|
|
|
|
|
+ node,
|
|
|
|
|
+ reason: "ALPHA ONLY IN MIX INPUT",
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ if let Some(f) = const_factor {
|
|
|
|
|
+ ctx.set_lod_frac(f, node)?;
|
|
|
|
|
+ }
|
|
|
|
|
+ Ok(RgbStage {
|
|
|
|
|
+ a: b_val,
|
|
|
|
|
+ b: a_val,
|
|
|
|
|
+ c: t_val,
|
|
|
|
|
+ d: a_val,
|
|
|
|
|
+ })
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+fn compile_rgb_expr(
|
|
|
|
|
+ graph: &NodeGraph,
|
|
|
|
|
+ pin: PinId,
|
|
|
|
|
+ ctx: &mut CompileCtx,
|
|
|
|
|
+) -> Result<Expr, CompileError> {
|
|
|
|
|
+ if ctx.visiting.contains(&pin.node) {
|
|
|
|
|
+ return Err(CompileError::CycleDetected(pin.node));
|
|
|
|
|
+ }
|
|
|
|
|
+ let node = graph.node(pin.node).ok_or(CompileError::MissingOutput)?;
|
|
|
|
|
+ ctx.visiting
|
|
|
|
|
+ .push(node.id)
|
|
|
|
|
+ .map_err(|_| CompileError::ExceedsTwoCycles(node.id))?;
|
|
|
|
|
+
|
|
|
|
|
+ let res = match node.kind {
|
|
|
|
|
+ NodeType::Texture0 { .. }
|
|
|
|
|
+ | NodeType::Texture1 { .. }
|
|
|
|
|
+ | NodeType::VertexColor
|
|
|
|
|
+ | NodeType::PrimColor { .. }
|
|
|
|
|
+ | NodeType::EnvColor { .. }
|
|
|
|
|
+ | NodeType::Noise
|
|
|
|
|
+ | NodeType::Fresnel { .. } => {
|
|
|
|
|
+ let src = pin_to_rgb_leaf(graph, pin, ctx)?;
|
|
|
|
|
+ Ok(Expr::Leaf(src, node.id))
|
|
|
|
|
+ }
|
|
|
|
|
+ NodeType::Multiply => {
|
|
|
|
|
+ let p0 = graph
|
|
|
|
|
+ .input_source(node.id, 0)
|
|
|
|
|
+ .ok_or(CompileError::InvalidSlot {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ reason: "MUL INPUT A UNWIRED",
|
|
|
|
|
+ })?;
|
|
|
|
|
+ let p1 = graph
|
|
|
|
|
+ .input_source(node.id, 1)
|
|
|
|
|
+ .ok_or(CompileError::InvalidSlot {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ reason: "MUL INPUT B UNWIRED",
|
|
|
|
|
+ })?;
|
|
|
|
|
+ let e0 = compile_rgb_expr(graph, p0, ctx)?;
|
|
|
|
|
+ let e1 = compile_rgb_expr(graph, p1, ctx)?;
|
|
|
|
|
+ match (e0, e1) {
|
|
|
|
|
+ (Expr::Leaf(x, _), Expr::Leaf(y, _)) => Ok(Expr::Stage {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ stage: stage_mul_rgb(x, y, node.id)?,
|
|
|
|
|
+ child: None,
|
|
|
|
|
+ }),
|
|
|
|
|
+ (
|
|
|
|
|
+ Expr::Stage {
|
|
|
|
|
+ node: cnode,
|
|
|
|
|
+ stage: cstage,
|
|
|
|
|
+ child: None,
|
|
|
|
|
+ },
|
|
|
|
|
+ Expr::Leaf(y, _),
|
|
|
|
|
+ ) => Ok(Expr::Stage {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ stage: stage_mul_rgb(RgbSrc::Combined, y, node.id)?,
|
|
|
|
|
+ child: Some((cnode, cstage)),
|
|
|
|
|
+ }),
|
|
|
|
|
+ (
|
|
|
|
|
+ Expr::Leaf(x, _),
|
|
|
|
|
+ Expr::Stage {
|
|
|
|
|
+ node: cnode,
|
|
|
|
|
+ stage: cstage,
|
|
|
|
|
+ child: None,
|
|
|
|
|
+ },
|
|
|
|
|
+ ) => Ok(Expr::Stage {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ stage: stage_mul_rgb(x, RgbSrc::Combined, node.id)?,
|
|
|
|
|
+ child: Some((cnode, cstage)),
|
|
|
|
|
+ }),
|
|
|
|
|
+ _ => Err(CompileError::ExceedsTwoCycles(node.id)),
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ NodeType::Add => {
|
|
|
|
|
+ let p0 = graph
|
|
|
|
|
+ .input_source(node.id, 0)
|
|
|
|
|
+ .ok_or(CompileError::InvalidSlot {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ reason: "ADD INPUT A UNWIRED",
|
|
|
|
|
+ })?;
|
|
|
|
|
+ let p1 = graph
|
|
|
|
|
+ .input_source(node.id, 1)
|
|
|
|
|
+ .ok_or(CompileError::InvalidSlot {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ reason: "ADD INPUT B UNWIRED",
|
|
|
|
|
+ })?;
|
|
|
|
|
+ let e0 = compile_rgb_expr(graph, p0, ctx)?;
|
|
|
|
|
+ let e1 = compile_rgb_expr(graph, p1, ctx)?;
|
|
|
|
|
+ match (e0, e1) {
|
|
|
|
|
+ (Expr::Leaf(x, _), Expr::Leaf(y, _)) => Ok(Expr::Stage {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ stage: stage_add_rgb(x, y, node.id)?,
|
|
|
|
|
+ child: None,
|
|
|
|
|
+ }),
|
|
|
|
|
+ // Fuse `Add(Multiply(x, y), z)` into 1 cycle if `Multiply` has no child!
|
|
|
|
|
+ (
|
|
|
|
|
+ Expr::Stage {
|
|
|
|
|
+ node: cnode,
|
|
|
|
|
+ stage: cstage,
|
|
|
|
|
+ child: None,
|
|
|
|
|
+ },
|
|
|
|
|
+ Expr::Leaf(z, _),
|
|
|
|
|
+ ) => {
|
|
|
|
|
+ let cnode_ref = graph.node(cnode).ok_or(CompileError::MissingOutput)?;
|
|
|
|
|
+ if matches!(cnode_ref.kind, NodeType::Multiply)
|
|
|
|
|
+ && cstage.d == RgbSrc::Zero
|
|
|
|
|
+ && cstage.b == RgbSrc::Zero
|
|
|
|
|
+ {
|
|
|
|
|
+ if let Ok(fma) = stage_fma_rgb(cstage.a, cstage.c, z, node.id) {
|
|
|
|
|
+ return {
|
|
|
|
|
+ ctx.visiting.pop();
|
|
|
|
|
+ Ok(Expr::Stage {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ stage: fma,
|
|
|
|
|
+ child: None,
|
|
|
|
|
+ })
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ Ok(Expr::Stage {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ stage: stage_add_rgb(RgbSrc::Combined, z, node.id)?,
|
|
|
|
|
+ child: Some((cnode, cstage)),
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ (
|
|
|
|
|
+ Expr::Leaf(z, _),
|
|
|
|
|
+ Expr::Stage {
|
|
|
|
|
+ node: cnode,
|
|
|
|
|
+ stage: cstage,
|
|
|
|
|
+ child: None,
|
|
|
|
|
+ },
|
|
|
|
|
+ ) => {
|
|
|
|
|
+ let cnode_ref = graph.node(cnode).ok_or(CompileError::MissingOutput)?;
|
|
|
|
|
+ if matches!(cnode_ref.kind, NodeType::Multiply)
|
|
|
|
|
+ && cstage.d == RgbSrc::Zero
|
|
|
|
|
+ && cstage.b == RgbSrc::Zero
|
|
|
|
|
+ {
|
|
|
|
|
+ if let Ok(fma) = stage_fma_rgb(cstage.a, cstage.c, z, node.id) {
|
|
|
|
|
+ return {
|
|
|
|
|
+ ctx.visiting.pop();
|
|
|
|
|
+ Ok(Expr::Stage {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ stage: fma,
|
|
|
|
|
+ child: None,
|
|
|
|
|
+ })
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ Ok(Expr::Stage {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ stage: stage_add_rgb(z, RgbSrc::Combined, node.id)?,
|
|
|
|
|
+ child: Some((cnode, cstage)),
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ _ => Err(CompileError::ExceedsTwoCycles(node.id)),
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ NodeType::Mix { factor } => {
|
|
|
|
|
+ let p0 = graph
|
|
|
|
|
+ .input_source(node.id, 0)
|
|
|
|
|
+ .ok_or(CompileError::InvalidSlot {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ reason: "MIX INPUT A UNWIRED",
|
|
|
|
|
+ })?;
|
|
|
|
|
+ let p1 = graph
|
|
|
|
|
+ .input_source(node.id, 1)
|
|
|
|
|
+ .ok_or(CompileError::InvalidSlot {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ reason: "MIX INPUT B UNWIRED",
|
|
|
|
|
+ })?;
|
|
|
|
|
+ let e0 = compile_rgb_expr(graph, p0, ctx)?;
|
|
|
|
|
+ let e1 = compile_rgb_expr(graph, p1, ctx)?;
|
|
|
|
|
+
|
|
|
|
|
+ let (t_src, const_fac) = if let Some(pt) = graph.input_source(node.id, 2) {
|
|
|
|
|
+ match compile_rgb_expr(graph, pt, ctx)? {
|
|
|
|
|
+ Expr::Leaf(t, _) => (t, None),
|
|
|
|
|
+ Expr::Stage { .. } => return Err(CompileError::ExceedsTwoCycles(node.id)),
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ (RgbSrc::PrimLodFrac, Some(factor))
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ match (e0, e1) {
|
|
|
|
|
+ (Expr::Leaf(a_val, _), Expr::Leaf(b_val, _)) => Ok(Expr::Stage {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ stage: stage_mix_rgb(a_val, b_val, t_src, const_fac, ctx, node.id)?,
|
|
|
|
|
+ child: None,
|
|
|
|
|
+ }),
|
|
|
|
|
+ (
|
|
|
|
|
+ Expr::Stage {
|
|
|
|
|
+ node: cnode,
|
|
|
|
|
+ stage: cstage,
|
|
|
|
|
+ child: None,
|
|
|
|
|
+ },
|
|
|
|
|
+ Expr::Leaf(b_val, _),
|
|
|
|
|
+ ) => Ok(Expr::Stage {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ stage: stage_mix_rgb(RgbSrc::Combined, b_val, t_src, const_fac, ctx, node.id)?,
|
|
|
|
|
+ child: Some((cnode, cstage)),
|
|
|
|
|
+ }),
|
|
|
|
|
+ (
|
|
|
|
|
+ Expr::Leaf(a_val, _),
|
|
|
|
|
+ Expr::Stage {
|
|
|
|
|
+ node: cnode,
|
|
|
|
|
+ stage: cstage,
|
|
|
|
|
+ child: None,
|
|
|
|
|
+ },
|
|
|
|
|
+ ) => Ok(Expr::Stage {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ stage: stage_mix_rgb(a_val, RgbSrc::Combined, t_src, const_fac, ctx, node.id)?,
|
|
|
|
|
+ child: Some((cnode, cstage)),
|
|
|
|
|
+ }),
|
|
|
|
|
+ _ => Err(CompileError::ExceedsTwoCycles(node.id)),
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ NodeType::Output { .. } => Err(CompileError::CycleDetected(node.id)),
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ ctx.visiting.pop();
|
|
|
|
|
+ res
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+fn compile_alpha_expr(
|
|
|
|
|
+ graph: &NodeGraph,
|
|
|
|
|
+ pin: PinId,
|
|
|
|
|
+ ctx: &mut CompileCtx,
|
|
|
|
|
+) -> Result<AlphaExpr, CompileError> {
|
|
|
|
|
+ if ctx.visiting.contains(&pin.node) {
|
|
|
|
|
+ return Err(CompileError::CycleDetected(pin.node));
|
|
|
|
|
+ }
|
|
|
|
|
+ let node = graph.node(pin.node).ok_or(CompileError::MissingOutput)?;
|
|
|
|
|
+ ctx.visiting
|
|
|
|
|
+ .push(node.id)
|
|
|
|
|
+ .map_err(|_| CompileError::ExceedsTwoCycles(node.id))?;
|
|
|
|
|
+
|
|
|
|
|
+ let res = match node.kind {
|
|
|
|
|
+ NodeType::Texture0 { .. }
|
|
|
|
|
+ | NodeType::Texture1 { .. }
|
|
|
|
|
+ | NodeType::VertexColor
|
|
|
|
|
+ | NodeType::PrimColor { .. }
|
|
|
|
|
+ | NodeType::EnvColor { .. }
|
|
|
|
|
+ | NodeType::Fresnel { .. } => Ok(AlphaExpr::Leaf(pin_to_alpha_leaf(graph, pin, ctx)?, node.id)),
|
|
|
|
|
+ NodeType::Multiply => {
|
|
|
|
|
+ let p0 = graph
|
|
|
|
|
+ .input_source(node.id, 0)
|
|
|
|
|
+ .ok_or(CompileError::InvalidSlot {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ reason: "ALPHA MUL A UNWIRED",
|
|
|
|
|
+ })?;
|
|
|
|
|
+ let p1 = graph
|
|
|
|
|
+ .input_source(node.id, 1)
|
|
|
|
|
+ .ok_or(CompileError::InvalidSlot {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ reason: "ALPHA MUL B UNWIRED",
|
|
|
|
|
+ })?;
|
|
|
|
|
+ let e0 = compile_alpha_expr(graph, p0, ctx)?;
|
|
|
|
|
+ let e1 = compile_alpha_expr(graph, p1, ctx)?;
|
|
|
|
|
+ match (e0, e1) {
|
|
|
|
|
+ (AlphaExpr::Leaf(x, _), AlphaExpr::Leaf(y, _)) => Ok(AlphaExpr::Stage {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ stage: AlphaStage {
|
|
|
|
|
+ a: x,
|
|
|
|
|
+ b: AlphaSrc::Zero,
|
|
|
|
|
+ c: y,
|
|
|
|
|
+ d: AlphaSrc::Zero,
|
|
|
|
|
+ },
|
|
|
|
|
+ child: None,
|
|
|
|
|
+ }),
|
|
|
|
|
+ (
|
|
|
|
|
+ AlphaExpr::Stage {
|
|
|
|
|
+ node: cnode,
|
|
|
|
|
+ stage: cstage,
|
|
|
|
|
+ child: None,
|
|
|
|
|
+ },
|
|
|
|
|
+ AlphaExpr::Leaf(y, _),
|
|
|
|
|
+ ) => Ok(AlphaExpr::Stage {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ stage: AlphaStage {
|
|
|
|
|
+ a: AlphaSrc::Combined,
|
|
|
|
|
+ b: AlphaSrc::Zero,
|
|
|
|
|
+ c: y,
|
|
|
|
|
+ d: AlphaSrc::Zero,
|
|
|
|
|
+ },
|
|
|
|
|
+ child: Some((cnode, cstage)),
|
|
|
|
|
+ }),
|
|
|
|
|
+ _ => Err(CompileError::ExceedsTwoCycles(node.id)),
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ NodeType::Add => {
|
|
|
|
|
+ let p0 = graph
|
|
|
|
|
+ .input_source(node.id, 0)
|
|
|
|
|
+ .ok_or(CompileError::InvalidSlot {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ reason: "ALPHA ADD A UNWIRED",
|
|
|
|
|
+ })?;
|
|
|
|
|
+ let p1 = graph
|
|
|
|
|
+ .input_source(node.id, 1)
|
|
|
|
|
+ .ok_or(CompileError::InvalidSlot {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ reason: "ALPHA ADD B UNWIRED",
|
|
|
|
|
+ })?;
|
|
|
|
|
+ let e0 = compile_alpha_expr(graph, p0, ctx)?;
|
|
|
|
|
+ let e1 = compile_alpha_expr(graph, p1, ctx)?;
|
|
|
|
|
+ match (e0, e1) {
|
|
|
|
|
+ (AlphaExpr::Leaf(x, _), AlphaExpr::Leaf(y, _)) => Ok(AlphaExpr::Stage {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ stage: AlphaStage {
|
|
|
|
|
+ a: AlphaSrc::One,
|
|
|
|
|
+ b: AlphaSrc::Zero,
|
|
|
|
|
+ c: x,
|
|
|
|
|
+ d: y,
|
|
|
|
|
+ },
|
|
|
|
|
+ child: None,
|
|
|
|
|
+ }),
|
|
|
|
|
+ _ => Err(CompileError::ExceedsTwoCycles(node.id)),
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ NodeType::Mix { factor } => {
|
|
|
|
|
+ let p0 = graph
|
|
|
|
|
+ .input_source(node.id, 0)
|
|
|
|
|
+ .ok_or(CompileError::InvalidSlot {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ reason: "ALPHA MIX A UNWIRED",
|
|
|
|
|
+ })?;
|
|
|
|
|
+ let p1 = graph
|
|
|
|
|
+ .input_source(node.id, 1)
|
|
|
|
|
+ .ok_or(CompileError::InvalidSlot {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ reason: "ALPHA MIX B UNWIRED",
|
|
|
|
|
+ })?;
|
|
|
|
|
+ let e0 = compile_alpha_expr(graph, p0, ctx)?;
|
|
|
|
|
+ let e1 = compile_alpha_expr(graph, p1, ctx)?;
|
|
|
|
|
+ ctx.set_lod_frac(factor, node.id)?;
|
|
|
|
|
+ match (e0, e1) {
|
|
|
|
|
+ (AlphaExpr::Leaf(a_val, _), AlphaExpr::Leaf(b_val, _)) => Ok(AlphaExpr::Stage {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ stage: AlphaStage {
|
|
|
|
|
+ a: b_val,
|
|
|
|
|
+ b: a_val,
|
|
|
|
|
+ c: AlphaSrc::PrimLodFrac,
|
|
|
|
|
+ d: a_val,
|
|
|
|
|
+ },
|
|
|
|
|
+ child: None,
|
|
|
|
|
+ }),
|
|
|
|
|
+ _ => Err(CompileError::ExceedsTwoCycles(node.id)),
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ _ => Err(CompileError::InvalidSlot {
|
|
|
|
|
+ node: node.id,
|
|
|
|
|
+ reason: "INVALID ALPHA NODE",
|
|
|
|
|
+ }),
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ ctx.visiting.pop();
|
|
|
|
|
+ res
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+fn format_stage(stage: RgbStage) -> String<40> {
|
|
|
|
|
+ use core::fmt::Write;
|
|
|
|
|
+ let mut s = String::new();
|
|
|
|
|
+ let _ = write!(
|
|
|
|
|
+ s,
|
|
|
|
|
+ "({}-{})*{}+{}",
|
|
|
|
|
+ stage.a.label(),
|
|
|
|
|
+ stage.b.label(),
|
|
|
|
|
+ stage.c.label(),
|
|
|
|
|
+ stage.d.label()
|
|
|
|
|
+ );
|
|
|
|
|
+ s
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+#[allow(clippy::too_many_arguments)]
|
|
|
|
|
+fn pack_set_combine(
|
|
|
|
|
+ rgb0: RgbStage,
|
|
|
|
|
+ n_rgb0: NodeId,
|
|
|
|
|
+ alp0: AlphaStage,
|
|
|
|
|
+ n_alp0: NodeId,
|
|
|
|
|
+ rgb1: RgbStage,
|
|
|
|
|
+ n_rgb1: NodeId,
|
|
|
|
|
+ alp1: AlphaStage,
|
|
|
|
|
+ n_alp1: NodeId,
|
|
|
|
|
+ two_cycle: bool,
|
|
|
|
|
+) -> Result<u64, CompileError> {
|
|
|
|
|
+ let a0 = rgb0.a.encode_a(false, n_rgb0)?;
|
|
|
|
|
+ let b0 = rgb0.b.encode_b(false, n_rgb0)?;
|
|
|
|
|
+ let c0 = rgb0.c.encode_c(false, n_rgb0)?;
|
|
|
|
|
+ let d0 = rgb0.d.encode_d(false, n_rgb0)?;
|
|
|
|
|
+ let aa0 = alp0.a.encode_abd(false, n_alp0)?;
|
|
|
|
|
+ let ba0 = alp0.b.encode_abd(false, n_alp0)?;
|
|
|
|
|
+ let ca0 = alp0.c.encode_c(false, n_alp0)?;
|
|
|
|
|
+ let da0 = alp0.d.encode_abd(false, n_alp0)?;
|
|
|
|
|
+
|
|
|
|
|
+ let (a1, b1, c1, d1, aa1, ba1, ca1, da1) = if two_cycle {
|
|
|
|
|
+ (
|
|
|
|
|
+ rgb1.a.encode_a(true, n_rgb1)?,
|
|
|
|
|
+ rgb1.b.encode_b(true, n_rgb1)?,
|
|
|
|
|
+ rgb1.c.encode_c(true, n_rgb1)?,
|
|
|
|
|
+ rgb1.d.encode_d(true, n_rgb1)?,
|
|
|
|
|
+ alp1.a.encode_abd(true, n_alp1)?,
|
|
|
|
|
+ alp1.b.encode_abd(true, n_alp1)?,
|
|
|
|
|
+ alp1.c.encode_c(true, n_alp1)?,
|
|
|
|
|
+ alp1.d.encode_abd(true, n_alp1)?,
|
|
|
|
|
+ )
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // Real hardware requires Cycle 2 fields to mirror Cycle 1 in 1-cycle mode.
|
|
|
|
|
+ (a0, b0, c0, d0, aa0, ba0, ca0, da0)
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ Ok((0xFCu64 << 56)
|
|
|
|
|
+ | (a0 << 52)
|
|
|
|
|
+ | (c0 << 47)
|
|
|
|
|
+ | (aa0 << 44)
|
|
|
|
|
+ | (ca0 << 41)
|
|
|
|
|
+ | (a1 << 37)
|
|
|
|
|
+ | (c1 << 32)
|
|
|
|
|
+ | (b0 << 28)
|
|
|
|
|
+ | (b1 << 24)
|
|
|
|
|
+ | (aa1 << 21)
|
|
|
|
|
+ | (ca1 << 18)
|
|
|
|
|
+ | (d0 << 15)
|
|
|
|
|
+ | (ba0 << 12)
|
|
|
|
|
+ | (da0 << 9)
|
|
|
|
|
+ | (d1 << 6)
|
|
|
|
|
+ | (ba1 << 3)
|
|
|
|
|
+ | da1)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/// Compile a `NodeGraph` into a hardware N64 RDP shader configuration.
|
|
|
|
|
+pub fn compile(graph: &NodeGraph) -> Result<CompiledShader, CompileError> {
|
|
|
|
|
+ let out_id = graph.find_output().ok_or(CompileError::MissingOutput)?;
|
|
|
|
|
+ let out_node = graph.node(out_id).ok_or(CompileError::MissingOutput)?;
|
|
|
|
|
+ let blend_mode = match out_node.kind {
|
|
|
|
|
+ NodeType::Output { blend_mode } => blend_mode,
|
|
|
|
|
+ _ => BlendMode::Opaque,
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ let col_pin = graph
|
|
|
|
|
+ .input_source(out_id, 0)
|
|
|
|
|
+ .ok_or(CompileError::UnconnectedOutput(out_id))?;
|
|
|
|
|
+
|
|
|
|
|
+ let mut ctx = CompileCtx::new();
|
|
|
|
|
+ let rgb_expr = compile_rgb_expr(graph, col_pin, &mut ctx)?;
|
|
|
|
|
+
|
|
|
|
|
+ let alpha_expr = if let Some(alp_pin) = graph.input_source(out_id, 1) {
|
|
|
|
|
+ Some(compile_alpha_expr(graph, alp_pin, &mut ctx)?)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ None
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ // Determine whether 2-cycle mode is required.
|
|
|
|
|
+ let rgb_needs_2cyc = matches!(
|
|
|
|
|
+ rgb_expr,
|
|
|
|
|
+ Expr::Stage {
|
|
|
|
|
+ child: Some(_),
|
|
|
|
|
+ ..
|
|
|
|
|
+ }
|
|
|
|
|
+ );
|
|
|
|
|
+ let alpha_needs_2cyc = matches!(
|
|
|
|
|
+ alpha_expr,
|
|
|
|
|
+ Some(AlphaExpr::Stage {
|
|
|
|
|
+ child: Some(_),
|
|
|
|
|
+ ..
|
|
|
|
|
+ })
|
|
|
|
|
+ );
|
|
|
|
|
+ let two_cycle = rgb_needs_2cyc || alpha_needs_2cyc;
|
|
|
|
|
+
|
|
|
|
|
+ // Lower RGB stages.
|
|
|
|
|
+ let (rgb0, n_rgb0, rgb1, n_rgb1) = match rgb_expr {
|
|
|
|
|
+ Expr::Leaf(src, n) => {
|
|
|
|
|
+ let st = if src == RgbSrc::Noise {
|
|
|
|
|
+ // Noise is only in A; if PrimColor unused, set Prim=white to multiply by 1.0.
|
|
|
|
|
+ if ctx.prim_color.is_none() {
|
|
|
|
|
+ ctx.prim_color = Some([255, 255, 255, 255]);
|
|
|
|
|
+ }
|
|
|
|
|
+ RgbStage {
|
|
|
|
|
+ a: RgbSrc::Noise,
|
|
|
|
|
+ b: RgbSrc::Zero,
|
|
|
|
|
+ c: RgbSrc::Prim,
|
|
|
|
|
+ d: RgbSrc::Zero,
|
|
|
|
|
+ }
|
|
|
|
|
+ } else if src.is_alpha_only() {
|
|
|
|
|
+ RgbStage {
|
|
|
|
|
+ a: RgbSrc::One,
|
|
|
|
|
+ b: RgbSrc::Zero,
|
|
|
|
|
+ c: src,
|
|
|
|
|
+ d: RgbSrc::Zero,
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ RgbStage {
|
|
|
|
|
+ a: RgbSrc::Zero,
|
|
|
|
|
+ b: RgbSrc::Zero,
|
|
|
|
|
+ c: RgbSrc::Zero,
|
|
|
|
|
+ d: src,
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+ if two_cycle {
|
|
|
|
|
+ // Put leaf in Cycle 1 and pass Combined in Cycle 2 (avoids TEX0 bug in Cyc2!).
|
|
|
|
|
+ (st, n, RgbStage::pass_combined(), out_id)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ (st, n, st, n)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ Expr::Stage {
|
|
|
|
|
+ node,
|
|
|
|
|
+ stage,
|
|
|
|
|
+ child: None,
|
|
|
|
|
+ } => {
|
|
|
|
|
+ if two_cycle {
|
|
|
|
|
+ (stage, node, RgbStage::pass_combined(), out_id)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ (stage, node, stage, node)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ Expr::Stage {
|
|
|
|
|
+ node,
|
|
|
|
|
+ stage,
|
|
|
|
|
+ child: Some((cnode, cstage)),
|
|
|
|
|
+ } => (cstage, cnode, stage, node),
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ // Lower Alpha stages.
|
|
|
|
|
+ let (alp0, n_alp0, alp1, n_alp1) = match alpha_expr {
|
|
|
|
|
+ None => {
|
|
|
|
|
+ let st = AlphaStage::pass_one();
|
|
|
|
|
+ (st, out_id, st, out_id)
|
|
|
|
|
+ }
|
|
|
|
|
+ Some(AlphaExpr::Leaf(src, n)) => {
|
|
|
|
|
+ let st = AlphaStage {
|
|
|
|
|
+ a: AlphaSrc::Zero,
|
|
|
|
|
+ b: AlphaSrc::Zero,
|
|
|
|
|
+ c: AlphaSrc::Zero,
|
|
|
|
|
+ d: src,
|
|
|
|
|
+ };
|
|
|
|
|
+ if two_cycle {
|
|
|
|
|
+ (st, n, AlphaStage::pass_combined(), out_id)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ (st, n, st, n)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ Some(AlphaExpr::Stage {
|
|
|
|
|
+ node,
|
|
|
|
|
+ stage,
|
|
|
|
|
+ child: None,
|
|
|
|
|
+ }) => {
|
|
|
|
|
+ if two_cycle {
|
|
|
|
|
+ (stage, node, AlphaStage::pass_combined(), out_id)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ (stage, node, stage, node)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ Some(AlphaExpr::Stage {
|
|
|
|
|
+ node,
|
|
|
|
|
+ stage,
|
|
|
|
|
+ child: Some((cnode, cstage)),
|
|
|
|
|
+ }) => (cstage, cnode, stage, node),
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ let combine_cmd = pack_set_combine(
|
|
|
|
|
+ rgb0, n_rgb0, alp0, n_alp0, rgb1, n_rgb1, alp1, n_alp1, two_cycle,
|
|
|
|
|
+ )?;
|
|
|
|
|
+
|
|
|
|
|
+ let cycle1_text = format_stage(rgb0);
|
|
|
|
|
+ let mut cycle2_text = String::new();
|
|
|
|
|
+ if two_cycle {
|
|
|
|
|
+ cycle2_text = format_stage(rgb1);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ let _ = cycle2_text.push_str("1-CYC PASS");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ let (fresnel_power, fresnel_bias) = ctx.fresnel.unwrap_or((2, 0));
|
|
|
|
|
+
|
|
|
|
|
+ Ok(CompiledShader {
|
|
|
|
|
+ combine_cmd,
|
|
|
|
|
+ cycles: if two_cycle {
|
|
|
|
|
+ CycleCount::TwoCycle
|
|
|
|
|
+ } else {
|
|
|
|
|
+ CycleCount::OneCycle
|
|
|
|
|
+ },
|
|
|
|
|
+ prim_color: ctx.prim_color.unwrap_or([255, 255, 255, 255]),
|
|
|
|
|
+ prim_lod_frac: ctx.prim_lod_frac.unwrap_or(128),
|
|
|
|
|
+ env_color: ctx.env_color.unwrap_or([255, 255, 255, 255]),
|
|
|
|
|
+ tex0: ctx.tex0,
|
|
|
|
|
+ tex1: ctx.tex1,
|
|
|
|
|
+ uses_shade: ctx.uses_shade,
|
|
|
|
|
+ uses_fresnel: ctx.fresnel.is_some(),
|
|
|
|
|
+ fresnel_power,
|
|
|
|
|
+ fresnel_bias,
|
|
|
|
|
+ blend_mode,
|
|
|
|
|
+ cycle1_text,
|
|
|
|
|
+ cycle2_text,
|
|
|
|
|
+ })
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/// Software reference evaluator for raw 64-bit `G_SETCOMBINE` (`0xFC`) words.
|
|
|
|
|
+/// Used by unit tests to verify the packed hardware bitfields end-to-end.
|
|
|
|
|
+#[derive(Copy, Clone, Debug)]
|
|
|
|
|
+pub struct CombinerInputs {
|
|
|
|
|
+ pub tex0: [u8; 4],
|
|
|
|
|
+ pub tex1: [u8; 4],
|
|
|
|
|
+ pub prim: [u8; 4],
|
|
|
|
|
+ pub env: [u8; 4],
|
|
|
|
|
+ pub shade: [u8; 4],
|
|
|
|
|
+ pub noise: [u8; 4],
|
|
|
|
|
+ pub prim_lod_frac: u8,
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+pub fn eval_combine_word(combine_cmd: u64, two_cycle: bool, inp: &CombinerInputs) -> [u8; 4] {
|
|
|
|
|
+ let a0 = ((combine_cmd >> 52) & 0xF) as u8;
|
|
|
|
|
+ let c0 = ((combine_cmd >> 47) & 0x1F) as u8;
|
|
|
|
|
+ let aa0 = ((combine_cmd >> 44) & 0x7) as u8;
|
|
|
|
|
+ let ca0 = ((combine_cmd >> 41) & 0x7) as u8;
|
|
|
|
|
+ let a1 = ((combine_cmd >> 37) & 0xF) as u8;
|
|
|
|
|
+ let c1 = ((combine_cmd >> 32) & 0x1F) as u8;
|
|
|
|
|
+ let b0 = ((combine_cmd >> 28) & 0xF) as u8;
|
|
|
|
|
+ let b1 = ((combine_cmd >> 24) & 0xF) as u8;
|
|
|
|
|
+ let aa1 = ((combine_cmd >> 21) & 0x7) as u8;
|
|
|
|
|
+ let ca1 = ((combine_cmd >> 18) & 0x7) as u8;
|
|
|
|
|
+ let d0 = ((combine_cmd >> 15) & 0x7) as u8;
|
|
|
|
|
+ let ba0 = ((combine_cmd >> 12) & 0x7) as u8;
|
|
|
|
|
+ let da0 = ((combine_cmd >> 9) & 0x7) as u8;
|
|
|
|
|
+ let d1 = ((combine_cmd >> 6) & 0x7) as u8;
|
|
|
|
|
+ let ba1 = ((combine_cmd >> 3) & 0x7) as u8;
|
|
|
|
|
+ let da1 = (combine_cmd & 0x7) as u8;
|
|
|
|
|
+
|
|
|
|
|
+ let eval_rgb_code = |code: u8, cyc2: bool, comb: [i32; 4]| -> [i32; 3] {
|
|
|
|
|
+ match (cyc2, code) {
|
|
|
|
|
+ (true, 0) => [comb[0], comb[1], comb[2]],
|
|
|
|
|
+ (false, 1) => [inp.tex0[0] as i32, inp.tex0[1] as i32, inp.tex0[2] as i32],
|
|
|
|
|
+ (false, 2) | (true, 1) => [inp.tex1[0] as i32, inp.tex1[1] as i32, inp.tex1[2] as i32],
|
|
|
|
|
+ (_, 3) => [inp.prim[0] as i32, inp.prim[1] as i32, inp.prim[2] as i32],
|
|
|
|
|
+ (_, 4) => [inp.shade[0] as i32, inp.shade[1] as i32, inp.shade[2] as i32],
|
|
|
|
|
+ (_, 5) => [inp.env[0] as i32, inp.env[1] as i32, inp.env[2] as i32],
|
|
|
|
|
+ (_, 6) => [255, 255, 255],
|
|
|
|
|
+ (_, 7) if !cyc2 => [inp.noise[0] as i32, inp.noise[1] as i32, inp.noise[2] as i32],
|
|
|
|
|
+ (true, 7) => [comb[3], comb[3], comb[3]], // COMBINED_ALPHA in C
|
|
|
|
|
+ (false, 8) => [inp.tex0[3] as i32; 3],
|
|
|
|
|
+ (false, 9) | (true, 8) => [inp.tex1[3] as i32; 3],
|
|
|
|
|
+ (_, 10) => [inp.prim[3] as i32; 3],
|
|
|
|
|
+ (_, 11) => [inp.shade[3] as i32; 3],
|
|
|
|
|
+ (_, 12) => [inp.env[3] as i32; 3],
|
|
|
|
|
+ (_, 14) => [inp.prim_lod_frac as i32; 3],
|
|
|
|
|
+ _ => [0, 0, 0],
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ let eval_rgb_ab = |code: u8, cyc2: bool, comb: [i32; 4]| -> [i32; 3] {
|
|
|
|
|
+ match (cyc2, code) {
|
|
|
|
|
+ (_, 7) => [inp.noise[0] as i32, inp.noise[1] as i32, inp.noise[2] as i32],
|
|
|
|
|
+ (_, 8) => [0, 0, 0],
|
|
|
|
|
+ _ => eval_rgb_code(code, cyc2, comb),
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ let eval_rgb_d = |code: u8, cyc2: bool, comb: [i32; 4]| -> [i32; 3] {
|
|
|
|
|
+ if code == 7 {
|
|
|
|
|
+ [0, 0, 0]
|
|
|
|
|
+ } else {
|
|
|
|
|
+ eval_rgb_code(code, cyc2, comb)
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ let eval_alp = |code: u8, is_c: bool, cyc2: bool, comb: [i32; 4]| -> i32 {
|
|
|
|
|
+ match (cyc2, is_c, code) {
|
|
|
|
|
+ (true, false, 0) => comb[3],
|
|
|
|
|
+ (false, _, 1) => inp.tex0[3] as i32,
|
|
|
|
|
+ (false, _, 2) | (true, _, 1) => inp.tex1[3] as i32,
|
|
|
|
|
+ (_, _, 3) => inp.prim[3] as i32,
|
|
|
|
|
+ (_, _, 4) => inp.shade[3] as i32,
|
|
|
|
|
+ (_, _, 5) => inp.env[3] as i32,
|
|
|
|
|
+ (_, false, 6) => 255,
|
|
|
|
|
+ (_, true, 6) => inp.prim_lod_frac as i32,
|
|
|
|
|
+ (_, _, 7) => 0,
|
|
|
|
|
+ _ => 0,
|
|
|
|
|
+ }
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ let step = |cyc2: bool,
|
|
|
|
|
+ a: u8,
|
|
|
|
|
+ b: u8,
|
|
|
|
|
+ c: u8,
|
|
|
|
|
+ d: u8,
|
|
|
|
|
+ aa: u8,
|
|
|
|
|
+ ba: u8,
|
|
|
|
|
+ ca: u8,
|
|
|
|
|
+ da: u8,
|
|
|
|
|
+ comb: [i32; 4]|
|
|
|
|
|
+ -> [i32; 4] {
|
|
|
|
|
+ let va = eval_rgb_ab(a, cyc2, comb);
|
|
|
|
|
+ let vb = eval_rgb_ab(b, cyc2, comb);
|
|
|
|
|
+ let vc = eval_rgb_code(c, cyc2, comb);
|
|
|
|
|
+ let vd = eval_rgb_d(d, cyc2, comb);
|
|
|
|
|
+ let mut out = [0i32; 4];
|
|
|
|
|
+ for i in 0..3 {
|
|
|
|
|
+ let v = ((va[i] - vb[i]) * vc[i]) / 255 + vd[i];
|
|
|
|
|
+ out[i] = v.clamp(0, 255);
|
|
|
|
|
+ }
|
|
|
|
|
+ let vaa = eval_alp(aa, false, cyc2, comb);
|
|
|
|
|
+ let vba = eval_alp(ba, false, cyc2, comb);
|
|
|
|
|
+ let vca = eval_alp(ca, true, cyc2, comb);
|
|
|
|
|
+ let vda = eval_alp(da, false, cyc2, comb);
|
|
|
|
|
+ out[3] = (((vaa - vba) * vca) / 255 + vda).clamp(0, 255);
|
|
|
|
|
+ out
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ let cyc1_out = step(false, a0, b0, c0, d0, aa0, ba0, ca0, da0, [0; 4]);
|
|
|
|
|
+ let fin = if two_cycle {
|
|
|
|
|
+ step(true, a1, b1, c1, d1, aa1, ba1, ca1, da1, cyc1_out)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ cyc1_out
|
|
|
|
|
+ };
|
|
|
|
|
+ [fin[0] as u8, fin[1] as u8, fin[2] as u8, fin[3] as u8]
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+#[cfg(test)]
|
|
|
|
|
+mod tests {
|
|
|
|
|
+ use super::*;
|
|
|
|
|
+ use crate::graph::PinDir;
|
|
|
|
|
+
|
|
|
|
|
+ fn test_inputs() -> CombinerInputs {
|
|
|
|
|
+ CombinerInputs {
|
|
|
|
|
+ tex0: [200, 100, 50, 255],
|
|
|
|
|
+ tex1: [40, 80, 160, 200],
|
|
|
|
|
+ prim: [128, 255, 64, 180],
|
|
|
|
|
+ env: [10, 20, 240, 100],
|
|
|
|
|
+ shade: [200, 200, 200, 128],
|
|
|
|
|
+ noise: [90, 90, 90, 255],
|
|
|
|
|
+ prim_lod_frac: 128,
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn compile_one_cycle_mul_and_eval() {
|
|
|
|
|
+ let mut g = NodeGraph::new();
|
|
|
|
|
+ let t0 = g
|
|
|
|
|
+ .add_node(
|
|
|
|
|
+ NodeType::Texture0 {
|
|
|
|
|
+ pattern: TexPattern::ChromeEnv,
|
|
|
|
|
+ scroll_x: 0,
|
|
|
|
|
+ scroll_y: 0,
|
|
|
|
|
+ env_map: false,
|
|
|
|
|
+ },
|
|
|
|
|
+ 10,
|
|
|
|
|
+ 20,
|
|
|
|
|
+ )
|
|
|
|
|
+ .unwrap();
|
|
|
|
|
+ let prim = g
|
|
|
|
|
+ .add_node(
|
|
|
|
|
+ NodeType::PrimColor {
|
|
|
|
|
+ rgba: [128, 255, 64, 180],
|
|
|
|
|
+ },
|
|
|
|
|
+ 10,
|
|
|
|
|
+ 70,
|
|
|
|
|
+ )
|
|
|
|
|
+ .unwrap();
|
|
|
|
|
+ let mul = g.add_node(NodeType::Multiply, 70, 40).unwrap();
|
|
|
|
|
+ let out = g
|
|
|
|
|
+ .add_node(
|
|
|
|
|
+ NodeType::Output {
|
|
|
|
|
+ blend_mode: BlendMode::Opaque,
|
|
|
|
|
+ },
|
|
|
|
|
+ 140,
|
|
|
|
|
+ 40,
|
|
|
|
|
+ )
|
|
|
|
|
+ .unwrap();
|
|
|
|
|
+
|
|
|
|
|
+ g.connect(
|
|
|
|
|
+ PinId { node: t0, slot: 0, dir: PinDir::Output },
|
|
|
|
|
+ PinId { node: mul, slot: 0, dir: PinDir::Input },
|
|
|
|
|
+ );
|
|
|
|
|
+ g.connect(
|
|
|
|
|
+ PinId { node: prim, slot: 0, dir: PinDir::Output },
|
|
|
|
|
+ PinId { node: mul, slot: 1, dir: PinDir::Input },
|
|
|
|
|
+ );
|
|
|
|
|
+ g.connect(
|
|
|
|
|
+ PinId { node: mul, slot: 0, dir: PinDir::Output },
|
|
|
|
|
+ PinId { node: out, slot: 0, dir: PinDir::Input },
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ let compiled = compile(&g).expect("should compile");
|
|
|
|
|
+ assert_eq!(compiled.cycles, CycleCount::OneCycle);
|
|
|
|
|
+ assert_eq!(compiled.combine_cmd >> 56, 0xFC);
|
|
|
|
|
+
|
|
|
|
|
+ let px = eval_combine_word(compiled.combine_cmd, false, &test_inputs());
|
|
|
|
|
+ // (200 * 128)/255 = 100, (100 * 255)/255 = 100, (50 * 64)/255 = 12
|
|
|
|
|
+ assert!((px[0] as i32 - 100).abs() <= 1);
|
|
|
|
|
+ assert!((px[1] as i32 - 100).abs() <= 1);
|
|
|
|
|
+ assert!((px[2] as i32 - 12).abs() <= 1);
|
|
|
|
|
+ assert_eq!(px[3], 255);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn compile_two_cycle_chrome_fresnel_mix() {
|
|
|
|
|
+ let mut g = NodeGraph::new();
|
|
|
|
|
+ let t0 = g
|
|
|
|
|
+ .add_node(
|
|
|
|
|
+ NodeType::Texture0 {
|
|
|
|
|
+ pattern: TexPattern::ChromeEnv,
|
|
|
|
|
+ scroll_x: 0,
|
|
|
|
|
+ scroll_y: 0,
|
|
|
|
|
+ env_map: true,
|
|
|
|
|
+ },
|
|
|
|
|
+ 10,
|
|
|
|
|
+ 20,
|
|
|
|
|
+ )
|
|
|
|
|
+ .unwrap();
|
|
|
|
|
+ let vtx = g.add_node(NodeType::VertexColor, 10, 60).unwrap();
|
|
|
|
|
+ let env = g
|
|
|
|
|
+ .add_node(
|
|
|
|
|
+ NodeType::EnvColor {
|
|
|
|
|
+ rgba: [10, 20, 240, 255],
|
|
|
|
|
+ },
|
|
|
|
|
+ 10,
|
|
|
|
|
+ 100,
|
|
|
|
|
+ )
|
|
|
|
|
+ .unwrap();
|
|
|
|
|
+ let frs = g
|
|
|
|
|
+ .add_node(NodeType::Fresnel { power: 2, bias: 16 }, 10, 140)
|
|
|
|
|
+ .unwrap();
|
|
|
|
|
+ let mul = g.add_node(NodeType::Multiply, 70, 40).unwrap();
|
|
|
|
|
+ let mix = g.add_node(NodeType::Mix { factor: 128 }, 120, 80).unwrap();
|
|
|
|
|
+ let out = g
|
|
|
|
|
+ .add_node(
|
|
|
|
|
+ NodeType::Output {
|
|
|
|
|
+ blend_mode: BlendMode::Opaque,
|
|
|
|
|
+ },
|
|
|
|
|
+ 170,
|
|
|
|
|
+ 80,
|
|
|
|
|
+ )
|
|
|
|
|
+ .unwrap();
|
|
|
|
|
+
|
|
|
|
|
+ g.connect(
|
|
|
|
|
+ PinId { node: t0, slot: 0, dir: PinDir::Output },
|
|
|
|
|
+ PinId { node: mul, slot: 0, dir: PinDir::Input },
|
|
|
|
|
+ );
|
|
|
|
|
+ g.connect(
|
|
|
|
|
+ PinId { node: vtx, slot: 0, dir: PinDir::Output },
|
|
|
|
|
+ PinId { node: mul, slot: 1, dir: PinDir::Input },
|
|
|
|
|
+ );
|
|
|
|
|
+ g.connect(
|
|
|
|
|
+ PinId { node: mul, slot: 0, dir: PinDir::Output },
|
|
|
|
|
+ PinId { node: mix, slot: 0, dir: PinDir::Input },
|
|
|
|
|
+ );
|
|
|
|
|
+ g.connect(
|
|
|
|
|
+ PinId { node: env, slot: 0, dir: PinDir::Output },
|
|
|
|
|
+ PinId { node: mix, slot: 1, dir: PinDir::Input },
|
|
|
|
|
+ );
|
|
|
|
|
+ g.connect(
|
|
|
|
|
+ PinId { node: frs, slot: 0, dir: PinDir::Output },
|
|
|
|
|
+ PinId { node: mix, slot: 2, dir: PinDir::Input },
|
|
|
|
|
+ );
|
|
|
|
|
+ g.connect(
|
|
|
|
|
+ PinId { node: mix, slot: 0, dir: PinDir::Output },
|
|
|
|
|
+ PinId { node: out, slot: 0, dir: PinDir::Input },
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ let compiled = compile(&g).expect("should compile");
|
|
|
|
|
+ assert_eq!(compiled.cycles, CycleCount::TwoCycle);
|
|
|
|
|
+ assert!(compiled.uses_fresnel);
|
|
|
|
|
+
|
|
|
|
|
+ let px = eval_combine_word(compiled.combine_cmd, true, &test_inputs());
|
|
|
|
|
+ // Cycle 1: tex0 * shade = (200*200/255, 100*200/255, 50*200/255) ~ (156, 78, 39)
|
|
|
|
|
+ // Cycle 2: lerp((156,78,39), (10,20,240), 128/255) ~ (83, 49, 139)
|
|
|
|
|
+ assert!((px[0] as i32 - 83).abs() <= 2);
|
|
|
|
|
+ assert!((px[1] as i32 - 49).abs() <= 2);
|
|
|
|
|
+ assert!((px[2] as i32 - 139).abs() <= 2);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn rejects_three_cycle_chain_with_exact_node() {
|
|
|
|
|
+ let mut g = NodeGraph::new();
|
|
|
|
|
+ let t0 = g
|
|
|
|
|
+ .add_node(
|
|
|
|
|
+ NodeType::Texture0 {
|
|
|
|
|
+ pattern: TexPattern::ChromeEnv,
|
|
|
|
|
+ scroll_x: 0,
|
|
|
|
|
+ scroll_y: 0,
|
|
|
|
|
+ env_map: false,
|
|
|
|
|
+ },
|
|
|
|
|
+ 0,
|
|
|
|
|
+ 0,
|
|
|
|
|
+ )
|
|
|
|
|
+ .unwrap();
|
|
|
|
|
+ let t1 = g
|
|
|
|
|
+ .add_node(
|
|
|
|
|
+ NodeType::Texture1 {
|
|
|
|
|
+ pattern: TexPattern::LavaMagma,
|
|
|
|
|
+ scroll_x: 0,
|
|
|
|
|
+ scroll_y: 0,
|
|
|
|
|
+ env_map: false,
|
|
|
|
|
+ },
|
|
|
|
|
+ 0,
|
|
|
|
|
+ 40,
|
|
|
|
|
+ )
|
|
|
|
|
+ .unwrap();
|
|
|
|
|
+ let p = g
|
|
|
|
|
+ .add_node(NodeType::PrimColor { rgba: [255; 4] }, 0, 80)
|
|
|
|
|
+ .unwrap();
|
|
|
|
|
+ let e = g
|
|
|
|
|
+ .add_node(NodeType::EnvColor { rgba: [255; 4] }, 0, 120)
|
|
|
|
|
+ .unwrap();
|
|
|
|
|
+ let m1 = g.add_node(NodeType::Multiply, 50, 20).unwrap();
|
|
|
|
|
+ let m2 = g.add_node(NodeType::Multiply, 90, 40).unwrap();
|
|
|
|
|
+ let m3 = g.add_node(NodeType::Multiply, 130, 60).unwrap();
|
|
|
|
|
+ let out = g
|
|
|
|
|
+ .add_node(
|
|
|
|
|
+ NodeType::Output {
|
|
|
|
|
+ blend_mode: BlendMode::Opaque,
|
|
|
|
|
+ },
|
|
|
|
|
+ 170,
|
|
|
|
|
+ 60,
|
|
|
|
|
+ )
|
|
|
|
|
+ .unwrap();
|
|
|
|
|
+
|
|
|
|
|
+ g.connect(
|
|
|
|
|
+ PinId { node: t0, slot: 0, dir: PinDir::Output },
|
|
|
|
|
+ PinId { node: m1, slot: 0, dir: PinDir::Input },
|
|
|
|
|
+ );
|
|
|
|
|
+ g.connect(
|
|
|
|
|
+ PinId { node: t1, slot: 0, dir: PinDir::Output },
|
|
|
|
|
+ PinId { node: m1, slot: 1, dir: PinDir::Input },
|
|
|
|
|
+ );
|
|
|
|
|
+ g.connect(
|
|
|
|
|
+ PinId { node: m1, slot: 0, dir: PinDir::Output },
|
|
|
|
|
+ PinId { node: m2, slot: 0, dir: PinDir::Input },
|
|
|
|
|
+ );
|
|
|
|
|
+ g.connect(
|
|
|
|
|
+ PinId { node: p, slot: 0, dir: PinDir::Output },
|
|
|
|
|
+ PinId { node: m2, slot: 1, dir: PinDir::Input },
|
|
|
|
|
+ );
|
|
|
|
|
+ g.connect(
|
|
|
|
|
+ PinId { node: m2, slot: 0, dir: PinDir::Output },
|
|
|
|
|
+ PinId { node: m3, slot: 0, dir: PinDir::Input },
|
|
|
|
|
+ );
|
|
|
|
|
+ g.connect(
|
|
|
|
|
+ PinId { node: e, slot: 0, dir: PinDir::Output },
|
|
|
|
|
+ PinId { node: m3, slot: 1, dir: PinDir::Input },
|
|
|
|
|
+ );
|
|
|
|
|
+ g.connect(
|
|
|
|
|
+ PinId { node: m3, slot: 0, dir: PinDir::Output },
|
|
|
|
|
+ PinId { node: out, slot: 0, dir: PinDir::Input },
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ let err = compile(&g).unwrap_err();
|
|
|
|
|
+ assert_eq!(err, CompileError::ExceedsTwoCycles(m3));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn detects_cycle2_tex0_silicon_quirk() {
|
|
|
|
|
+ let mut g = NodeGraph::new();
|
|
|
|
|
+ let p = g
|
|
|
|
|
+ .add_node(NodeType::PrimColor { rgba: [200; 4] }, 0, 0)
|
|
|
|
|
+ .unwrap();
|
|
|
|
|
+ let e = g
|
|
|
|
|
+ .add_node(NodeType::EnvColor { rgba: [100; 4] }, 0, 40)
|
|
|
|
|
+ .unwrap();
|
|
|
|
|
+ let t0 = g
|
|
|
|
|
+ .add_node(
|
|
|
|
|
+ NodeType::Texture0 {
|
|
|
|
|
+ pattern: TexPattern::ChromeEnv,
|
|
|
|
|
+ scroll_x: 0,
|
|
|
|
|
+ scroll_y: 0,
|
|
|
|
|
+ env_map: false,
|
|
|
|
|
+ },
|
|
|
|
|
+ 0,
|
|
|
|
|
+ 80,
|
|
|
|
|
+ )
|
|
|
|
|
+ .unwrap();
|
|
|
|
|
+ let mix = g.add_node(NodeType::Mix { factor: 128 }, 60, 20).unwrap();
|
|
|
|
|
+ let mul = g.add_node(NodeType::Multiply, 110, 50).unwrap();
|
|
|
|
|
+ let out = g
|
|
|
|
|
+ .add_node(
|
|
|
|
|
+ NodeType::Output {
|
|
|
|
|
+ blend_mode: BlendMode::Opaque,
|
|
|
|
|
+ },
|
|
|
|
|
+ 160,
|
|
|
|
|
+ 50,
|
|
|
|
|
+ )
|
|
|
|
|
+ .unwrap();
|
|
|
|
|
+
|
|
|
|
|
+ g.connect(
|
|
|
|
|
+ PinId { node: p, slot: 0, dir: PinDir::Output },
|
|
|
|
|
+ PinId { node: mix, slot: 0, dir: PinDir::Input },
|
|
|
|
|
+ );
|
|
|
|
|
+ g.connect(
|
|
|
|
|
+ PinId { node: e, slot: 0, dir: PinDir::Output },
|
|
|
|
|
+ PinId { node: mix, slot: 1, dir: PinDir::Input },
|
|
|
|
|
+ );
|
|
|
|
|
+ g.connect(
|
|
|
|
|
+ PinId { node: mix, slot: 0, dir: PinDir::Output },
|
|
|
|
|
+ PinId { node: mul, slot: 0, dir: PinDir::Input },
|
|
|
|
|
+ );
|
|
|
|
|
+ g.connect(
|
|
|
|
|
+ PinId { node: t0, slot: 0, dir: PinDir::Output },
|
|
|
|
|
+ PinId { node: mul, slot: 1, dir: PinDir::Input },
|
|
|
|
|
+ );
|
|
|
|
|
+ g.connect(
|
|
|
|
|
+ PinId { node: mul, slot: 0, dir: PinDir::Output },
|
|
|
|
|
+ PinId { node: out, slot: 0, dir: PinDir::Input },
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ let err = compile(&g).unwrap_err();
|
|
|
|
|
+ assert_eq!(err, CompileError::Cycle2Tex0Quirk(mul));
|
|
|
|
|
+ }
|
|
|
|
|
+}
|