feat: implement debug node
All checks were successful
🚀 Lint & Test & Deploy / release (pull_request) Successful in 3m53s

Closes #39
This commit is contained in:
release-bot
2026-02-12 21:33:47 +01:00
parent 48cee58ad3
commit 15e08a8163
11 changed files with 234 additions and 111 deletions

View File

@@ -0,0 +1,19 @@
<script lang="ts">
import { T } from '@threlte/core';
import type { Group } from 'three';
import { updateDebugScene } from './debug';
type Props = {
debugData?: Record<number, { type: string; data: Int32Array }>;
};
let group = $state<Group>(null!);
const { debugData }: Props = $props();
$effect(() => {
if (!group || !debugData) return;
updateDebugScene(group, $state.snapshot(debugData));
});
</script>
<T.Group bind:ref={group} />

View File

@@ -1,33 +1,26 @@
<script lang="ts">
import { colors } from '$lib/graph-interface/graph/colors.svelte';
import { T, useTask, useThrelte } from '@threlte/core';
import { Grid, MeshLineGeometry, MeshLineMaterial, Text } from '@threlte/extras';
import {
Box3,
type BufferGeometry,
type Group,
Mesh,
MeshBasicMaterial,
Vector3,
type Vector3Tuple
} from 'three';
import { Grid } from '@threlte/extras';
import { Box3, type BufferGeometry, type Group, Mesh, MeshBasicMaterial, Vector3 } from 'three';
import { appSettings } from '../settings/app-settings.svelte';
import Camera from './Camera.svelte';
import Debug from './Debug.svelte';
const { renderStage, invalidate: _invalidate } = useThrelte();
type Props = {
fps: number[];
lines: Vector3[][];
debugData?: Record<number, { type: string; data: Int32Array }>;
scene: Group;
centerCamera: boolean;
};
let {
lines,
centerCamera,
fps = $bindable(),
scene = $bindable()
scene = $bindable(),
debugData
}: Props = $props();
let geometries = $state.raw<BufferGeometry[]>([]);
@@ -91,18 +84,12 @@
});
_invalidate();
});
function getPosition(geo: BufferGeometry, i: number) {
return [
geo.attributes.position.array[i],
geo.attributes.position.array[i + 1],
geo.attributes.position.array[i + 2]
] as Vector3Tuple;
}
</script>
<Camera {center} {centerCamera} />
<Debug {debugData} />
{#if appSettings.value.showGrid}
<Grid
cellColor={colors['outline']}
@@ -116,35 +103,4 @@
fadeOrigin={new Vector3(0, 0, 0)}
/>
{/if}
<T.Group>
{#if geometries}
{#each geometries as geo (geo.id)}
{#if appSettings.value.debug.showIndices}
{#each geo.attributes.position.array, i (i)}
{#if i % 3 === 0}
<Text fontSize={0.25} position={getPosition(geo, i)} />
{/if}
{/each}
{/if}
{#if appSettings.value.debug.showVertices}
<T.Points visible={true}>
<T is={geo} />
<T.PointsMaterial size={0.25} />
</T.Points>
{/if}
{/each}
{/if}
<T.Group bind:ref={scene}></T.Group>
</T.Group>
{#if appSettings.value.debug.showStemLines && lines}
{#each lines as line (line[0].x + '-' + line[0].y + '-' + '' + line[0].z)}
<T.Mesh>
<MeshLineGeometry points={line} />
<MeshLineMaterial width={0.1} color="red" depthTest={false} />
</T.Mesh>
{/each}
{/if}
<T.Group bind:ref={scene}></T.Group>

View File

@@ -1,10 +1,10 @@
<script lang="ts">
import SmallPerformanceViewer from '$lib/performance/SmallPerformanceViewer.svelte';
import { appSettings } from '$lib/settings/app-settings.svelte';
import { decodeFloat, splitNestedArray } from '@nodarium/utils';
import { splitNestedArray } from '@nodarium/utils';
import type { PerformanceStore } from '@nodarium/utils';
import { Canvas } from '@threlte/core';
import { DoubleSide, Vector3 } from 'three';
import { DoubleSide } from 'three';
import { type Group, MeshMatcapMaterial, TextureLoader } from 'three';
import { createGeometryPool, createInstancedGeometryPool } from './geometryPool';
import Scene from './Scene.svelte';
@@ -23,6 +23,7 @@
let geometryPool: ReturnType<typeof createGeometryPool>;
let instancePool: ReturnType<typeof createInstancedGeometryPool>;
export function updateGeometries(inputs: Int32Array[], group: Group) {
geometryPool = geometryPool || createGeometryPool(group, material);
instancePool = instancePool || createInstancedGeometryPool(group, material);
@@ -40,44 +41,16 @@
scene: Group;
centerCamera: boolean;
perf: PerformanceStore;
debugData?: Record<number, { type: string; data: Int32Array }>;
};
let { scene = $bindable(), centerCamera, perf }: Props = $props();
let lines = $state<Vector3[][]>([]);
function createLineGeometryFromEncodedData(encodedData: Int32Array) {
const positions: Vector3[] = [];
const amount = (encodedData.length - 1) / 4;
for (let i = 0; i < amount; i++) {
const x = decodeFloat(encodedData[2 + i * 4 + 0]);
const y = decodeFloat(encodedData[2 + i * 4 + 1]);
const z = decodeFloat(encodedData[2 + i * 4 + 2]);
positions.push(new Vector3(x, y, z));
}
return positions;
}
let { scene = $bindable(), centerCamera, debugData, perf }: Props = $props();
export const update = function update(result: Int32Array) {
perf.addPoint('split-result');
const inputs = splitNestedArray(result);
perf.endPoint();
if (appSettings.value.debug.showStemLines) {
perf.addPoint('create-lines');
lines = inputs
.map((input) => {
if (input[0] === 0) {
return createLineGeometryFromEncodedData(input);
}
})
.filter(Boolean) as Vector3[][];
perf.endPoint();
}
perf.addPoint('update-geometries');
const { totalVertices, totalFaces } = updateGeometries(inputs, scene);
@@ -97,8 +70,8 @@
<Canvas>
<Scene
bind:this={sceneComponent}
{lines}
{centerCamera}
{debugData}
bind:scene
bind:fps
/>

View File

@@ -0,0 +1,90 @@
import { splitNestedArray } from '@nodarium/utils';
import {
BufferGeometry,
type Group,
InstancedMesh,
Line,
LineBasicMaterial,
Matrix4,
MeshBasicMaterial,
SphereGeometry,
Vector3
} from 'three';
function writePath(scene: Group, data: Int32Array): Vector3[] {
const positions: Vector3[] = [];
const f32 = new Float32Array(data.buffer);
for (let i = 2; i + 2 < f32.length; i += 4) {
const vec = new Vector3(f32[i], f32[i + 1], f32[i + 2]);
positions.push(vec);
}
// Path line
if (positions.length >= 2) {
const geometry = new BufferGeometry().setFromPoints(positions);
const line = new Line(
geometry,
new LineBasicMaterial({ color: 0xff0000, depthTest: false })
);
scene.add(line);
}
// Instanced spheres at points
if (positions.length > 0) {
const sphereGeometry = new SphereGeometry(0.05, 8, 8); // keep low-poly
const sphereMaterial = new MeshBasicMaterial({
color: 0xff0000,
depthTest: false
});
const spheres = new InstancedMesh(
sphereGeometry,
sphereMaterial,
positions.length
);
const matrix = new Matrix4();
for (let i = 0; i < positions.length; i++) {
matrix.makeTranslation(
positions[i].x,
positions[i].y,
positions[i].z
);
spheres.setMatrixAt(i, matrix);
}
spheres.instanceMatrix.needsUpdate = true;
scene.add(spheres);
}
return positions;
}
function clearGroup(group: Group) {
for (let i = group.children.length - 1; i >= 0; i--) {
const child = group.children[i];
group.remove(child);
// optional but correct: free GPU memory
// @ts-expect-error three.js runtime fields
child.geometry?.dispose?.();
// @ts-expect-error three.js runtime fields
child.material?.dispose?.();
}
}
export function updateDebugScene(
group: Group,
data: Record<number, { type: string; data: Int32Array }>
) {
clearGroup(group);
return Object.entries(data || {}).map(([, d]) => {
switch (d.type) {
case 'path':
splitNestedArray(d.data)
.forEach(p => writePath(group, p));
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
return (_g: Group) => {};
}).flat();
}

View File

@@ -59,7 +59,7 @@ export class MemoryRuntimeExecutor implements RuntimeExecutor {
private definitionMap: Map<string, NodeDefinition> = new Map();
private seed = Math.floor(Math.random() * 100000000);
private debugData: Record<string, Int32Array> = {};
private debugData: Record<number, { type: string; data: Int32Array }> = {};
perf?: PerformanceStore;
@@ -143,8 +143,8 @@ export class MemoryRuntimeExecutor implements RuntimeExecutor {
for (const node of graphNodes) {
if (node.type.endsWith('/debug')) {
node.state = node.state || {};
node.state.depth = Math.min(...node.state.parents.map(s => s.state.depth), 1) - 1;
nodes.push(node);
const parent = node.state.parents[0];
parent.state.debugNode = true;
}
}
@@ -247,6 +247,12 @@ export class MemoryRuntimeExecutor implements RuntimeExecutor {
log.log(`Using cached value for ${node_type.id || node.id}`);
this.perf?.addPoint('cache-hit', 1);
results[node.id] = cachedValue as Int32Array;
if (node.state.debugNode && node_type.outputs) {
this.debugData[node.id] = {
type: node_type.outputs[0],
data: cachedValue
};
}
continue;
}
this.perf?.addPoint('cache-hit', 0);
@@ -255,8 +261,11 @@ export class MemoryRuntimeExecutor implements RuntimeExecutor {
log.log(`Inputs:`, inputs);
a = performance.now();
results[node.id] = node_type.execute(encoded_inputs);
if (node_type.id.endsWith('/debug')) {
this.debugData[node.id] = results[node.id];
if (node.state.debugNode && node_type.outputs) {
this.debugData[node.id] = {
type: node_type.outputs[0],
data: results[node.id]
};
}
log.log('Executed', node.type, node.id);
b = performance.now();

View File

@@ -5,6 +5,7 @@ type RuntimeState = {
parents: RuntimeNode[];
children: RuntimeNode[];
inputNodes: Record<string, RuntimeNode>;
debugNode?: boolean;
};
export type RuntimeNode = SerializedNode & { state: RuntimeState };

View File

@@ -6,12 +6,15 @@ export class WorkerRuntimeExecutor implements RuntimeExecutor {
new URL(`./worker-runtime-executor-backend.ts`, import.meta.url)
);
async execute(graph: Graph, settings: Record<string, unknown>) {
execute(graph: Graph, settings: Record<string, unknown>) {
return this.worker.executeGraph(graph, settings);
}
async getPerformanceData() {
getPerformanceData() {
return this.worker.getPerformanceData();
}
getDebugData() {
return this.worker.getDebugData();
}
set useRuntimeCache(useCache: boolean) {
this.worker.setUseRuntimeCache(useCache);
}

View File

@@ -59,26 +59,11 @@ export const AppSettingTypes = {
label: 'Execute in WebWorker',
value: true
},
showIndices: {
type: 'boolean',
label: 'Show Indices',
value: false
},
advancedMode: {
type: 'boolean',
label: 'Advanced Mode',
value: false
},
showVertices: {
type: 'boolean',
label: 'Show Vertices',
value: false
},
showStemLines: {
type: 'boolean',
label: 'Show Stem Lines',
value: false
},
cache: {
title: 'Cache',
useRuntimeCache: {

View File

@@ -42,11 +42,13 @@
const store: Store = {};
Object.keys(inputs).forEach((key) => {
if (props) {
const value = props[key] || inputs[key].value;
const value = props[key] !== undefined ? props[key] : inputs[key].value;
if (Array.isArray(value) || typeof value === 'number') {
store[key] = value;
} else if (typeof value === 'boolean') {
store[key] = value ? 1 : 0;
} else {
console.error('Wrong error');
console.error('Wrong error', { value });
}
}
});

View File

@@ -68,6 +68,7 @@
let sidebarOpen = $state(false);
let graphInterface = $state<ReturnType<typeof GraphInterface>>(null!);
let viewerComponent = $state<ReturnType<typeof Viewer>>();
let debugData = $state<Record<number, { type: string; data: Int32Array }>>();
const manager = $derived(graphInterface?.manager);
async function randomGenerate() {
@@ -107,6 +108,7 @@
if (appSettings.value.debug.useWorker) {
let perfData = await runtime.getPerformanceData();
debugData = await runtime.getDebugData();
let lastRun = perfData?.at(-1);
if (lastRun?.total) {
lastRun.runtime = lastRun.total;
@@ -165,6 +167,7 @@
bind:scene
bind:this={viewerComponent}
perf={performanceStore}
debugData={debugData}
centerCamera={appSettings.value.centerCamera}
/>
</Grid.Cell>

View File

@@ -1,5 +1,10 @@
import { expect, test } from 'vitest';
import { concatEncodedArrays, decodeNestedArray, encodeNestedArray } from './flatTree';
import {
concatEncodedArrays,
decodeNestedArray,
encodeNestedArray,
splitNestedArray
} from './flatTree';
test('it correctly concats nested arrays', () => {
const input_a = encodeNestedArray([1, 2, 3]);
@@ -82,3 +87,80 @@ test('it correctly handles arrays with mixed data types', () => {
const decoded = decodeNestedArray(encodeNestedArray(input));
expect(decoded).toEqual(input);
});
// Test splitNestedArray function
test('it splits nested array into segments based on structure', () => {
const input = [[1, 2], [3, 4]];
const encoded = new Int32Array(encodeNestedArray(input));
const split = splitNestedArray(encoded);
// Based on the actual behavior, splitNestedArray returns segments
// but the specific behavior needs to match the implementation
expect(Array.isArray(split)).toBe(true);
expect(split.length).toBe(2);
expect(split[0][0]).toBe(1);
expect(split[0][1]).toBe(2);
expect(split[1][0]).toBe(3);
expect(split[1][1]).toBe(4);
});
// Test splitNestedArray function
test('it splits nested array into segments based on structure 2', () => {
// dprint-ignore
const encoded = new Int32Array([
0, 1,
0, 19,
0, 1,
0, 0, 0, 1060487823,
1067592955, 1079491492, -1086248132, 1056069822,
-1078247113, 1086620820, 1073133800, 1047681214,
-1068353940, 1094067306, 1078792112, 0,
1, 1,
0, 19,
0, 1,
0, 0, 0, 1060487823,
-1089446963, 1080524584, 1041006274, 1056069822,
-1092176382, 1087031528, -1088851934, 1047681214,
1081482392, 1094426140, -1107842261, 0,
1, 1,
1, 1
]);
// Should be split into two seperate arrays
const split = splitNestedArray(encoded);
// Based on the actual behavior, splitNestedArray returns segments
// but the specific behavior needs to match the implementation
expect(Array.isArray(split)).toBe(true);
expect(split.length).toBe(2);
expect(split[0][0]).toBe(0);
expect(split[0][1]).toBe(1);
expect(split[1][0]).toBe(0);
expect(split[1][1]).toBe(1);
});
// Test splitNestedArray function
test('it splits nested array into segments based on structure 2', () => {
// dprint-ignore
const encoded = new Int32Array( [
0, 1,
0, 27,
0, 1,
0, 0, 0, 1065353216,
0, 1067757391, 0, 1061997773,
0, 1076145999, 0, 1058642330,
0, 1081542391, 0, 1053609164,
0, 1084534607, 0, 1045220556,
0, 1087232803, 0, 0,
1, 1,
1, 1
]);
// Should be split into two seperate arrays
const split = splitNestedArray(encoded);
// Based on the actual behavior, splitNestedArray returns segments
// but the specific behavior needs to match the implementation
expect(Array.isArray(split)).toBe(true);
expect(split.length).toBe(1);
});