feat: add debug node

This commit is contained in:
Max Richter
2026-01-23 04:06:04 +01:00
committed by Max Richter
parent 1d1a44324e
commit 30afb30341
14 changed files with 289 additions and 108 deletions

8
Cargo.lock generated
View File

@@ -24,6 +24,14 @@ dependencies = [
"nodarium_utils", "nodarium_utils",
] ]
[[package]]
name = "debug"
version = "0.1.0"
dependencies = [
"nodarium_macros",
"nodarium_utils",
]
[[package]] [[package]]
name = "float" name = "float"
version = "0.1.0" version = "0.1.0"

View File

@@ -16,17 +16,28 @@ import {
import type { RuntimeNode } from './types'; import type { RuntimeNode } from './types';
const log = createLogger('runtime-executor'); const log = createLogger('runtime-executor');
log.mute(); // log.mute(); // Keep logging enabled for debug info
const remoteRegistry = new RemoteNodeRegistry(''); const remoteRegistry = new RemoteNodeRegistry('');
function getValue(input: NodeInput, value?: unknown) { type WasmExecute = (outputPos: number, args: number[]) => number;
function getValue(input: NodeInput, value?: unknown): number | number[] | Int32Array {
if (value === undefined && 'value' in input) { if (value === undefined && 'value' in input) {
value = input.value; value = input.value;
} }
if (input.type === 'float') { switch (input.type) {
case 'float':
return encodeFloat(value as number); return encodeFloat(value as number);
case 'select':
return (value as number) ?? 0;
case 'vec3': {
const arr = Array.isArray(value) ? value : [];
return [0, arr.length + 1, ...arr.map(v => encodeFloat(v)), 1, 1];
}
} }
if (Array.isArray(value)) { if (Array.isArray(value)) {
@@ -42,22 +53,14 @@ function getValue(input: NodeInput, value?: unknown) {
return [0, value.length + 1, ...value, 1, 1] as number[]; return [0, value.length + 1, ...value, 1, 1] as number[];
} }
if (typeof value === 'boolean') { if (typeof value === 'boolean') return value ? 1 : 0;
return value ? 1 : 0; if (typeof value === 'number') return value;
} if (value instanceof Int32Array) return value;
if (typeof value === 'number') { throw new Error(`Unsupported input type: ${input.type}`);
return value;
}
if (value instanceof Int32Array) {
return value;
}
throw new Error(`Unknown input type ${input.type}`);
} }
function compareInt32(a: Int32Array, b: Int32Array) { function compareInt32(a: Int32Array, b: Int32Array): boolean {
if (a.length !== b.length) return false; if (a.length !== b.length) return false;
for (let i = 0; i < a.length; i++) { for (let i = 0; i < a.length; i++) {
if (a[i] !== b[i]) return false; if (a[i] !== b[i]) return false;
@@ -76,47 +79,56 @@ export type Pointer = {
perf?: PerformanceStore; perf?: PerformanceStore;
public getMemory() {
return new Int32Array(this.memory.buffer);
}
constructor( constructor(
private registry: NodeRegistry, private readonly registry: NodeRegistry,
public cache?: SyncCache<Int32Array> public cache?: SyncCache<Int32Array>
) { ) {
this.cache = undefined; this.cache = undefined;
this.refreshView();
log.info('MemoryRuntimeExecutor initialized');
} }
private refreshView(): void {
this.memoryView = new Int32Array(this.memory.buffer);
log.info(`Memory view refreshed, length: ${this.memoryView.length}`);
}
public getMemory(): Int32Array {
return new Int32Array(this.memory.buffer);
}
private map = new Map<string, { definition: NodeDefinition; execute: WasmExecute }>();
private async getNodeDefinitions(graph: Graph) { private async getNodeDefinitions(graph: Graph) {
if (this.registry.status !== 'ready') { if (this.registry.status !== 'ready') {
throw new Error('Node registry is not ready'); throw new Error('Node registry is not ready');
} }
await this.registry.load(graph.nodes.map((node) => node.type)); await this.registry.load(graph.nodes.map(n => n.type));
log.info(`Loaded ${graph.nodes.length} node types from registry`);
const typeMap = new Map<string, { for (const { type } of graph.nodes) {
definition: NodeDefinition; if (this.map.has(type)) continue;
execute: (outputPos: number, args: number[]) => number;
}>(); const def = this.registry.getNode(type);
for (const node of graph.nodes) { if (!def) continue;
if (!typeMap.has(node.type)) {
const type = this.registry.getNode(node.type); log.info(`Fetching WASM for node type: ${type}`);
const buffer = await remoteRegistry.fetchArrayBuffer('nodes/' + node.type + '.wasm'); const buffer = await remoteRegistry.fetchArrayBuffer(`nodes/${type}.wasm`);
const wrapper = createWasmWrapper(buffer, this.memory); const wrapper = createWasmWrapper(buffer, this.memory);
if (type) {
typeMap.set(node.type, { this.map.set(type, {
definition: type, definition: def,
execute: wrapper.execute execute: wrapper.execute
}); });
log.info(`Node type ${type} loaded and wrapped`);
} }
}
} return this.map;
return typeMap;
} }
private async addMetaData(graph: Graph) { private async addMetaData(graph: Graph) {
// First, lets check if all nodes have a definition
this.nodes = await this.getNodeDefinitions(graph); this.nodes = await this.getNodeDefinitions(graph);
log.info(`Metadata added for ${this.nodes.size} nodes`);
const graphNodes = graph.nodes.map(node => { const graphNodes = graph.nodes.map(node => {
const n = node as RuntimeNode; const n = node as RuntimeNode;
@@ -129,37 +141,30 @@ export type Pointer = {
return n; return n;
}); });
const outputNode = graphNodes.find((node) => node.type.endsWith('/output')); const outputNode = graphNodes.find(n => n.type.endsWith('/output') || n.type.endsWith('/debug'))
if (!outputNode) { ?? graphNodes[0];
// throw new Error('No output node found');
console.log('No output node found');
}
const nodeMap = new Map( const nodeMap = new Map(graphNodes.map(n => [n.id, n]));
graphNodes.map((node) => [node.id, node])
);
// loop through all edges and assign the parent and child nodes to each node // loop through all edges and assign the parent and child nodes to each node
for (const edge of graph.edges) { for (const edge of graph.edges) {
const [parentId, /*_parentOutput*/, childId, childInput] = edge; const [parentId, /*_parentOutput*/, childId, childInput] = edge;
const parent = nodeMap.get(parentId); const parent = nodeMap.get(parentId);
const child = nodeMap.get(childId); const child = nodeMap.get(childId);
if (parent && child) { if (!parent || !child) continue;
parent.state.children.push(child); parent.state.children.push(child);
child.state.parents.push(parent); child.state.parents.push(parent);
child.state.inputNodes[childInput] = parent; child.state.inputNodes[childInput] = parent;
} }
}
const nodes = new Map<number, RuntimeNode>(); const nodes = new Map<number, RuntimeNode>();
// loop through all the nodes and assign each nodes its depth // loop through all the nodes and assign each nodes its depth
const stack = [outputNode, ...graphNodes.filter(n => n.type.endsWith('/debug'))]; const stack = [outputNode, ...graphNodes.filter(n => n.type.endsWith('/debug'))];
while (stack.length) { while (stack.length) {
const node = stack.pop(); const node = stack.pop()!;
if (!node) continue;
for (const parent of node.state.parents) { for (const parent of node.state.parents) {
parent.state = parent.state || {};
parent.state.depth = node.state.depth + 1; parent.state.depth = node.state.depth + 1;
stack.push(parent); stack.push(parent);
} }
@@ -183,16 +188,14 @@ export type Pointer = {
return [outputNode, _nodes] as const; return [outputNode, _nodes] as const;
} }
private writeToMemory(v: number | number[] | Int32Array, title?: string) { private writeToMemory(value: number | number[] | Int32Array, title?: string): Pointer {
let length = 1; const start = this.offset;
if (typeof v === 'number') { if (typeof value === 'number') {
this.memoryView[this.offset] = v; this.memoryView[this.offset++] = value;
console.log('MEM: writing number', v, ' to', this.offset);
length = 1;
} else { } else {
this.memoryView.set(v, this.offset); this.memoryView.set(value, this.offset);
length = v.length; this.offset += value.length;
} }
let a = performance.now(); let a = performance.now();
@@ -338,16 +341,122 @@ export type Pointer = {
} }
} }
// const mem = new Int32Array(this.memory.buffer); this.isRunning = true;
// console.log('OUT', mem.slice(0, 10)); log.info('Execution started');
// return the result of the parent of the output node try {
// const res = this.results[outputNode.id]; this.offset = 0;
this.results = {};
this.inputPtrs = {};
this.allPtrs = [];
this.seed += 2;
this.perf?.endPoint('runtime'); this.refreshView();
const [outputNode, nodes] = await this.addMetaData(graph);
const sortedNodes = [...nodes].sort(
(a, b) => (b.state.depth ?? 0) - (a.state.depth ?? 0)
);
const seedPtr = this.writeToMemory(this.seed, 'seed');
const settingPtrs = new Map<string, Pointer>();
for (const [key, value] of Object.entries(settings)) {
const ptr = this.writeToMemory(value as number, `setting.${key}`);
settingPtrs.set(key, ptr);
}
let lastNodePtr: Pointer | undefined = undefined;
for (const node of sortedNodes) {
const nodeType = this.nodes.get(node.type);
if (!nodeType) continue;
log.info(`Executing node: ${node.id} (type: ${node.type})`);
const inputs = Object.entries(nodeType.definition.inputs || {}).map(
([key, input]) => {
if (input.type === 'seed') return seedPtr;
if (input.setting) {
const ptr = settingPtrs.get(input.setting);
if (!ptr) throw new Error(`Missing setting: ${input.setting}`);
return ptr;
}
const src = node.state.inputNodes[key];
if (src) {
const res = this.results[src.id];
if (!res) {
throw new Error(`Missing input from ${src.type}/${src.id}`);
}
return res;
}
if (node.props?.[key] !== undefined) {
return this.writeToMemory(
getValue(input, node.props[key]),
`${node.id}.${key}`
);
}
return this.writeToMemory(getValue(input), `${node.id}.${key}`);
}
);
this.inputPtrs[node.id] = inputs;
const args = inputs.flatMap(p => [p.start * 4, p.end * 4]);
log.info(`Executing node ${node.type}/${node.id}`);
const bytesWritten = nodeType.execute(this.offset * 4, args);
if (bytesWritten === -1) {
throw new Error(`Failed to execute node`);
}
this.refreshView();
const outLen = bytesWritten >> 2;
const outputStart = this.offset;
if (
args.length === 2
&& inputs[0].end - inputs[0].start === outLen
&& compareInt32(
this.memoryView.slice(inputs[0].start, inputs[0].end),
this.memoryView.slice(outputStart, outputStart + outLen)
)
) {
this.results[node.id] = inputs[0];
log.info(`Node ${node.id} result reused input memory`);
} else {
this.results[node.id] = {
start: outputStart,
end: outputStart + outLen,
_title: `${node.id} ->`
};
this.offset += outLen;
lastNodePtr = this.results[node.id];
log.info(
`Node ${node.id} wrote result to memory: start=${outputStart}, end=${outputStart + outLen
}`
);
}
}
const res = this.results[outputNode.id] ?? lastNodePtr;
if (!res) throw new Error('Output node produced no result');
log.info(`Execution finished, output pointer: start=${res.start}, end=${res.end}`);
this.refreshView();
return this.memoryView.slice(res.start, res.end);
} catch (e) {
log.info('Execution error:', e);
console.error(e);
} finally {
this.isRunning = false; this.isRunning = false;
return undefined as unknown as Int32Array; this.perf?.endPoint('runtime');
log.info('Executor state reset');
}
} }
getDebugData() { getDebugData() {

View File

@@ -100,12 +100,13 @@
<tr class="h-[40px] odd:bg-[var(--layer-1)]"> <tr class="h-[40px] odd:bg-[var(--layer-1)]">
<td class="px-4 border-b border-[var(--outline)] w-8">{index}</td> <td class="px-4 border-b border-[var(--outline)] w-8">{index}</td>
<td <td
class="w-[50px] border-b border-[var(--outline)] class="border-b border-[var(--outline)] overflow-hidden text-ellipsis pl-2
{ptr?._title?.includes('->') {ptr?._title?.includes('->')
? 'bg-red-500' ? 'bg-red-500'
: 'bg-blue-500'}" : 'bg-blue-500'}"
style="width: 100px; min-width: 100px; max-width: 100px;"
> >
<span>{ptr?._title}</span> {ptr?._title}
</td> </td>
<td <td
class="px-4 border-b border-[var(--outline)] cursor-pointer text-blue-600 hover:text-blue-800" class="px-4 border-b border-[var(--outline)] cursor-pointer text-blue-600 hover:text-blue-800"

6
nodes/max/plantarium/debug/.gitignore vendored Normal file
View File

@@ -0,0 +1,6 @@
/target
**/*.rs.bk
Cargo.lock
bin/
pkg/
wasm-pack.log

View File

@@ -0,0 +1,12 @@
[package]
name = "debug"
version = "0.1.0"
authors = ["Max Richter <jim-x@web.de>"]
edition = "2018"
[lib]
crate-type = ["cdylib", "rlib"]
[dependencies]
nodarium_macros = { version = "0.1.0", path = "../../../../packages/macros" }
nodarium_utils = { version = "0.1.0", path = "../../../../packages/utils" }

View File

@@ -0,0 +1,22 @@
{
"id": "max/plantarium/debug",
"outputs": [],
"inputs": {
"input": {
"type": "float",
"accepts": [
"*"
],
"external": true
},
"type": {
"type": "select",
"options": [
"float",
"vec3",
"geometry"
],
"internal": true
}
}
}

View File

@@ -0,0 +1,25 @@
use nodarium_macros::nodarium_definition_file;
use nodarium_macros::nodarium_execute;
use nodarium_utils::encode_float;
use nodarium_utils::evaluate_float;
use nodarium_utils::evaluate_vec3;
use nodarium_utils::read_i32;
use nodarium_utils::read_i32_slice;
nodarium_definition_file!("src/input.json");
#[nodarium_execute]
pub fn execute(input: (i32, i32), input_type: (i32, i32)) -> Vec<i32> {
let inp = read_i32_slice(input);
let t = read_i32(input_type.0);
if t == 0 {
let f = evaluate_float(inp.as_slice());
return vec![encode_float(f)];
}
if t == 1 {
let f = evaluate_vec3(inp.as_slice());
return vec![encode_float(f[0]), encode_float(f[1]), encode_float(f[2])];
}
return inp;
}

View File

@@ -1,11 +1,13 @@
use nodarium_macros::nodarium_definition_file; use nodarium_macros::nodarium_definition_file;
use nodarium_macros::nodarium_execute; use nodarium_macros::nodarium_execute;
use nodarium_utils::log;
use nodarium_utils::{concat_arg_vecs, read_i32_slice}; use nodarium_utils::{concat_arg_vecs, read_i32_slice};
nodarium_definition_file!("src/input.json"); nodarium_definition_file!("src/input.json");
#[nodarium_execute] #[nodarium_execute]
pub fn execute(op_type: (i32, i32), a: (i32, i32), b: (i32, i32)) -> Vec<i32> { pub fn execute(op_type: (i32, i32), a: (i32, i32), b: (i32, i32)) -> Vec<i32> {
log!("math.op {:?}", op_type);
let op = read_i32_slice(op_type); let op = read_i32_slice(op_type);
let a_val = read_i32_slice(a); let a_val = read_i32_slice(a);
let b_val = read_i32_slice(b); let b_val = read_i32_slice(b);

View File

@@ -5,7 +5,7 @@
"input": { "input": {
"type": "path", "type": "path",
"accepts": [ "accepts": [
"geometry" "*"
], ],
"external": true "external": true
}, },

View File

@@ -1,9 +1,5 @@
use nodarium_macros::nodarium_definition_file; use nodarium_macros::nodarium_definition_file;
use nodarium_macros::nodarium_execute; use nodarium_macros::nodarium_execute;
use nodarium_utils::encode_float;
use nodarium_utils::evaluate_float;
use nodarium_utils::evaluate_vec3;
use nodarium_utils::log;
use nodarium_utils::read_i32_slice; use nodarium_utils::read_i32_slice;
nodarium_definition_file!("src/input.json"); nodarium_definition_file!("src/input.json");
@@ -11,17 +7,5 @@ nodarium_definition_file!("src/input.json");
#[nodarium_execute] #[nodarium_execute]
pub fn execute(input: (i32, i32), _res: (i32, i32)) -> Vec<i32> { pub fn execute(input: (i32, i32), _res: (i32, i32)) -> Vec<i32> {
let inp = read_i32_slice(input); let inp = read_i32_slice(input);
let length = inp.len();
if length == 1 {
let f = evaluate_float(inp.as_slice());
log!("out.float f={:?}", f);
return vec![encode_float(f)];
}
if length == 3 {
let f = evaluate_vec3(inp.as_slice());
log!("out.vec3 x={:?} y={:?} z={:?}", f[0], f[1], f[2]);
return vec![encode_float(f[0]), encode_float(f[1]), encode_float(f[2])];
}
return inp; return inp;
} }

View File

@@ -99,22 +99,20 @@ pub fn nodarium_execute(_attr: TokenStream, item: TokenStream) -> TokenStream {
#[no_mangle] #[no_mangle]
#fn_vis extern "C" fn execute(output_pos: i32, #( #arg_names: i32 ),*) -> i32 { #fn_vis extern "C" fn execute(output_pos: i32, #( #arg_names: i32 ),*) -> i32 {
// log!("before_fn"); nodarium_utils::log!("before_fn");
let result = #inner_fn_name( let result = #inner_fn_name(
#( #tuple_args ),* #( #tuple_args ),*
); );
// log!("after_fn"); nodarium_utils::log!("after_fn");
let len_bytes = result.len() * 4; let len_bytes = result.len() * 4;
unsafe { unsafe {
let src = result.as_ptr() as *const u8; let src = result.as_ptr() as *const u8;
let dst = output_pos as *mut u8; let dst = output_pos as *mut u8;
// log!("writing output_pos={:?} src={:?} len_bytes={:?}", output_pos, src, len_bytes); // nodarium_utils::log!("writing output_pos={:?} src={:?} len_bytes={:?}", output_pos, src, len_bytes);
dst.copy_from_nonoverlapping(src, len_bytes); dst.copy_from_nonoverlapping(src, len_bytes);
} }
core::mem::forget(result);
len_bytes as i32 len_bytes as i32
} }
}; };

View File

@@ -9,6 +9,7 @@ const DefaultOptionsSchema = z.object({
accepts: z accepts: z
.array( .array(
z.union([ z.union([
z.literal('*'),
z.literal('float'), z.literal('float'),
z.literal('integer'), z.literal('integer'),
z.literal('boolean'), z.literal('boolean'),

View File

@@ -27,22 +27,30 @@ pub fn read_f32(ptr: i32) -> f32 {
} }
#[inline] #[inline]
pub fn read_i32_slice(tuple: (i32, i32)) -> Vec<i32> { pub fn read_i32_slice(range: (i32, i32)) -> Vec<i32> {
let (start, end) = range;
assert!(end >= start);
let byte_len = (end - start) as usize;
assert!(byte_len % 4 == 0);
unsafe { unsafe {
let start = tuple.0 as *const i32; let ptr = start as *const i32;
let end = tuple.1 as *const i32; let len = byte_len / 4;
let len = (end as usize - start as usize) / 4; std::slice::from_raw_parts(ptr, len).to_vec()
std::slice::from_raw_parts(start, len).to_vec()
} }
} }
#[inline] #[inline]
pub fn read_f32_slice(tuple: (i32, i32)) -> Vec<f32> { pub fn read_f32_slice(range: (i32, i32)) -> Vec<f32> {
let (start, end) = range;
assert!(end >= start);
let byte_len = (end - start) as usize;
assert!(byte_len % 4 == 0);
unsafe { unsafe {
let start = tuple.0 as *const f32; let ptr = start as *const f32;
let end = tuple.1 as *const f32; let len = byte_len / 4;
let len = (end as usize - start as usize) / 4; std::slice::from_raw_parts(ptr, len).to_vec()
std::slice::from_raw_parts(start, len).to_vec()
} }
} }

View File

@@ -27,7 +27,12 @@ export function createWasmWrapper(buffer: ArrayBuffer, memory: WebAssembly.Memor
exports = instance.exports as NodariumExports; exports = instance.exports as NodariumExports;
function execute(outputPos: number, args: number[]): number { function execute(outputPos: number, args: number[]): number {
try {
return exports.execute(outputPos, ...args); return exports.execute(outputPos, ...args);
} catch (e) {
console.log(e);
return -1;
}
} }
function get_definition() { function get_definition() {