Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.4

0.11.4 / src / stamps.luc

## Content stamps: a node's stamp names its cooked result. It covers what
## compute reads (kind, bypass, parameters, texts, Edit recipes, the file
## epoch, every input's stamp and the object paths it hides), 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, frozen_stamp
from edit_kinds import is_edit
import node_catalog
import script_cook

## 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_catalog.places_children(node.kind):
        for child in graph.outputs(node.id):
            children.append((stamp_of(graph, child, done, depth + 1), child.visible))
    # A Material reads every node inside it.
    if node.kind == "Material":
        for child in graph.children(node.id):
            children.append((stamp_of(graph, child, done, depth + 1), child.enabled))
    var stamp = 0
    if node.frozen != "":
        stamp = frozen_stamp(node.frozen)
    elif 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; its own values are 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, node.sources, children, node.source_revision, node.guard).hash())
    # A Script node reads its file parameters' files: an edited file recooks it.
    if node.kind == script_cook.kind_name and node.frozen == "":
        stamp = nonzero((stamp, script_cook.file_stamps(node.texts, graph.folder)).hash())
    # A result that follows the timeline is a new result each frame.
    if node.time_dependent and node.frozen == "":
        stamp = nonzero((stamp, graph.frame, graph.fps).hash())
    # A Group's own visibility is in its result (its instance's).
    if node_catalog.places_children(node.kind):
        stamp = nonzero((stamp, node.visible).hash())
    # Hidden objects leave the result after the cook (an Edit node's recipe
    # prefixes keep the whole result).
    if node.hidden.length > 0:
        stamp = nonzero((stamp, node.hidden).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