dymokomi/luced-3d
Source of release 0.11.1
## Content stamps: a node's stamp names its cooked result. It covers what
## compute reads (kind, bypass, parameters, texts, Edit recipes, the file
## epoch and every input's stamp), never names, positions, display, output or
## visibility flags, selection or the Edit tool amount. Those affect only the
## parent Group, whose stamp lists its exposed children and their visibility.
from network import Network, Node
from cache_node import reads_file, cache_stamp
from edit_kinds import is_edit
## Stamp every node. Computed on the UI thread before each request; cheap,
## since Edit recipes carry their own precomputed content keys.
pub func stamp_graph(graph: Network) -> unit!:
let done: map[int, int] = {}
for node in graph.nodes:
node.stamp = stamp_of(graph, node, done, 0)
func stamp_of(graph: Network, node: Node, done: map[int, int], depth: int) -> int!:
if let known = done[node.id]:
return known
if depth > 256:
trap("node stamps exceed the graph depth budget")
let inputs: list[int] = []
for input in node.inputs:
if let upstream = graph.find(input):
inputs.append(stamp_of(graph, upstream, done, depth + 1))
else:
inputs.append(0)
let children: list[(int, bool)] = []
if node.kind == "Group":
for child in graph.outputs(node.id):
children.append((stamp_of(graph, child, done, depth + 1), child.visible))
var stamp = 0
if is_edit(node.kind) and node.enabled:
stamp = prefix_stamp(node, inputs[0], node.edits.length)
elif is_edit(node.kind):
# A bypassed Edit passes its input through; the tool amount is UI state.
stamp = nonzero((node.kind, false, inputs).hash())
elif node.kind == "Cache":
stamp = stamp_cache(node, inputs)
else:
stamp = nonzero((node.kind, node.enabled, node.values, node.texts, inputs, children, node.source_revision, node.guard).hash())
done[node.id] = stamp
return stamp
## A Cache node's stamp: from its path and input while it writes (Load from
## disk off, or bypassed); from its file while it reads, so upstream edits
## stop there and downstream results survive the switch (cache_node.luc).
func stamp_cache(node: Node, inputs: list[int]) -> int!:
let written = nonzero(("Cache", node.enabled, node.texts[0].trim(), inputs, node.source_revision).hash())
return cache_stamp(reads_file(node.kind, node.enabled, node.values), node.texts[0], node.source_revision, written)
## An enabled Edit node's result after its first `count` recipes. The full
## count is the node's own stamp, so the state before a new recipe (or a
## gizmo drag's preview recipe) is already stored under count - 1.
pub func prefix_stamp(node: Node, input: int, count: int) -> int:
let recipes: list[int] = []
for at in 0..<count:
recipes.append(node.edits[at].key)
return nonzero((node.kind, input, recipes).hash())
## Zero means "never cooked" on both sides of the worker boundary.
func nonzero(value: int) -> int:
return value if value != 0 else 1