Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.1

0.11.1 / src / compute_worker.luc

## Worker-local DAG mirror, stamp-keyed result store and publishing. No UI
## objects are created here. Cooking resolves each node by stamp: a stored
## result is reused, and an Edit node applies only the recipes after its
## longest stored prefix.
from compute_bridge import ComputeChannel, ComputeHandler, memory_budget
import compute_bridge
from compute_request import read_request, write_report, Outcome
from network import Network, Node
from node_evaluation import SourceCache
from result_store import ResultStore
from stamps import prefix_stamp
from cache_node import reads_file
from geometry_data import GeometryData
from scene_projection import append_overlays, append_patch_boundaries, append_cad_hulls
from luce_geocore.geocore import Mesh, Subdivision
from overlay_lines import OverlayLines
from luce_3d.graphics import Matrix4
import node_catalog
from luce_ui import ui
from edit_kinds import is_edit, edit_kind

class Computation:
    let channel: ComputeChannel
    var graph: Network
    let store: ResultStore
    let sources: SourceCache
    ## Cooks per node ID, reported as the node's evaluation count.
    let cooks: map[int, int]
    ## Failures met while cooking this request, with node names.
    let failures: map[int, str]
    var preview_warning: str = ""
    var recipes: int = 0
    ## Meshes published under keys the UI may hold, kept alive so an identity
    ## match never sees a recycled object. A mesh reaching several nodes (an
    ## Edit without recipes, a Null, a bypass) keeps one key, so the UI keeps
    ## its copy, scene object and renderer data.
    var meshes: list[Mesh]
    var keys: list[int]

    pub func init(self, channel: ComputeChannel) -> unit!:
        self.channel = channel
        self.graph = Network(false)
        self.store = ResultStore(memory_budget())
        self.sources = SourceCache()
        self.cooks = {}
        self.failures = {}
        self.meshes = []
        self.keys = []

    pub func compute(self, message: str) -> unit!:
        self.graph = read_request(message, self.graph) catch failure:
            # The mirror is unusable: ask the UI for a full request.
            self.graph = Network(false)
            self.channel.report(write_report(failure.message, true, []))
            return
        # A newer delta is queued: apply this one, cook only the newest.
        if self.channel.superseded():
            return
        let graph = self.graph
        self.preview_warning = ""
        self.recipes = 0
        self.failures.clear()
        var notice = ""
        var outputs = graph.outputs(graph.scope)
        if graph.preview:
            if let node = graph.find(graph.display):
                outputs = [node]
        let meshes: list[Mesh] = []
        let keys: list[int] = []
        for at in 0..<self.keys.length:
            if self.channel.holds(self.keys[at]):
                meshes.append(self.meshes[at])
                keys.append(self.keys[at])
        self.meshes = meshes
        self.keys = keys
        for node in outputs:
            if not node.visible:
                continue
            let data = self.evaluate(node) catch failure:
                if failure.message == "Computation superseded":
                    error(failure.code, failure.message)
                notice = failure.message
                continue
            self.channel.progress(node.id, 0.95, "Preparing viewport")
            self.publish(node, data)
        if let node = graph.find(graph.selected):
            let data = self.evaluate(node) catch failure:
                if failure.message == "Computation superseded":
                    error(failure.code, failure.message)
                recover GeometryData()
            # An Edit node's kind says what its level picks on.
            let kind = edit_kind(node.kind) if node.enabled else none
            if let editor = kind:
                let picked = editor.pick(data, graph.level) catch failure:
                    recover none
                if let mesh = picked:
                    self.inspect(node, mesh, graph.level)
            elif let mesh = data.mesh:
                self.inspect(node, mesh)
        if notice == "":
            notice = self.preview_warning
        self.store.trim(self.pinned())
        self.channel.report(write_report(notice, false, self.outcomes(), self.recipes))

    ## Stored result for the node's stamp, or cook it. Failures from inputs
    ## propagate without being stored under this node's stamp.
    func evaluate(self, node: Node, depth: int = 0) -> GeometryData!:
        if depth > 64:
            error(ui.invalid, "Cycle detected during evaluation")
        if let entry = self.store.get(node.stamp):
            if entry.failure != "":
                error(ui.invalid, self.failed(node, entry.failure))
            return entry.data else GeometryData()
        self.channel.begin(node.stamp)
        let result = self.cook(node, depth) catch failure:
            self.channel.end()
            error(failure.code, failure.message)
        self.channel.end()
        return result

    func cook(self, node: Node, depth: int) -> GeometryData!:
        let graph = self.graph
        var input = GeometryData()
        var second: GeometryData? = none
        if node.kind == "Group":
            input = GeometryData()
            if node.enabled:
                for child in graph.outputs(node.id):
                    input = input.with_instance(self.upstream(node, child, depth), visible = child.visible)
        elif not (node.kind in node_catalog.sources()) and not reads_file(node.kind, node.enabled, node.values):
            let port = int(node.values[0]) if node.kind == "Switch" and node.enabled else 0
            # An Edit node starts from nothing when unconnected (it can create).
            let unconnected = is_edit(node.kind) and node.inputs[0] < 0
            if not unconnected and (port < 0 or port >= node.inputs.length or node.inputs[port] < 0):
                return self.own(node, ui.invalid, "Connect a geometry input")
            if not unconnected:
                input = self.upstream(node, graph.require(node.inputs[port]), depth)
            if not unconnected and node.enabled and node.inputs.length == 2 and not (node.kind in ["Switch", "Null"]):
                if node.inputs[1] >= 0:
                    second = self.upstream(node, graph.require(node.inputs[1]), depth)
                elif node_catalog.requires_second(node.kind):
                    return self.own(node, ui.invalid, "Connect the second geometry input")
        self.channel.progress(node.id, 0.0, "Computing")
        self.cooks[node.id] = (self.cooks[node.id] else 0) + 1
        var result = GeometryData()
        if is_edit(node.kind) and node.enabled:
            result = self.edit(node, input) catch failure:
                return self.own(node, failure.code, failure.message)
        else:
            let progress = func (fraction: float) -> unit!:
                self.channel.progress(node.id, fraction, "Tessellating" if node.kind == "Tessellate" else "Computing")
            result = node.compute(input, second, progress, self.sources) catch failure:
                return self.own(node, failure.code, failure.message)
        _ = self.store.put(node.stamp, result)
        self.channel.progress(node.id, 1.0, "Complete")
        return result

    ## Apply only the recipes after the longest stored prefix. Prefixes at the
    ## checkpoints (the last eight, every sixteenth) are stored as they are
    ## made: undo inside that window, a gizmo drag and the next recipe start
    ## from a stored result. The node's Edit kind runs each step.
    func edit(self, node: Node, input: GeometryData) -> GeometryData!:
        let kind = edit_kind(node.kind) else error(ui.invalid, f"{node.kind} is not an Edit node")
        var base = 0
        if let upstream = self.graph.find(node.inputs[0]):
            base = upstream.stamp
        var result = kind.start(input)
        var start = 0
        var count = node.edits.length - 1
        while count > 0 and start == 0:
            if let entry = self.store.get(prefix_stamp(node, base, count)):
                if let data = entry.data:
                    result = data
                    start = count
            count -= 1
        for at in start..<node.edits.length:
            result = kind.run(node.edits[at], result)
            self.recipes += 1
            self.channel.check()
            if at + 1 < node.edits.length and checkpoint(at + 1, node.edits.length):
                _ = self.store.put(prefix_stamp(node, base, at + 1), result)
        return result

    func upstream(self, node: Node, input: Node, depth: int) -> GeometryData!:
        let data = self.evaluate(input, depth + 1) catch failure:
            if failure.message == "Computation superseded":
                error(failure.code, failure.message)
            error(failure.code, self.failed(node, failure.message))
        return data

    ## A failure of this node's own recipe, stored under its stamp.
    func own(self, node: Node, code: ErrorCode, message: str) -> GeometryData!:
        if message == "Computation superseded":
            error(code, message)
        _ = self.store.fail(node.stamp, message)
        error(code, self.failed(node, message))

    func failed(self, node: Node, message: str) -> str:
        let text = f"{node.name}: {message}"
        self.failures[node.id] = text
        return text

    ## Displayed and selected stamps and everything they read stay resident.
    func pinned(self) -> set[int]!:
        let result: set[int] = {}
        let pending: list[int] = [self.graph.display, self.graph.selected]
        for node in self.graph.outputs(self.graph.scope):
            pending.append(node.id)
        while pending.length > 0:
            let node = self.graph.find(pending.pop()) else continue
            if node.stamp in result:
                continue
            result.insert(node.stamp)
            if is_edit(node.kind) and node.edits.length > 0:
                if let input = self.graph.find(node.inputs[0]):
                    for count in 1..<node.edits.length:
                        if checkpoint(count, node.edits.length):
                            result.insert(prefix_stamp(node, input.stamp, count))
            for input in node.inputs:
                pending.append(input)
            if node.kind == "Group":
                for child in self.graph.outputs(node.id):
                    pending.append(child.id)
        return result

    func outcomes(self) -> list[Outcome]:
        let result: list[Outcome] = []
        for node in self.graph.nodes:
            var cooked = 0
            var summary = ""
            var paths: list[str] = []
            var failure = self.failures[node.id] else ""
            var warning = ""
            if let entry = self.store.peek(node.stamp):
                cooked = node.stamp
                summary = entry.summary
                paths = entry.paths
                warning = entry.warning
                if entry.failure != "":
                    failure = f"{node.name}: {entry.failure}"
            elif failure != "":
                cooked = node.stamp
            let outcome = Outcome(node.id, cooked, failure, summary, self.cooks[node.id] else 0, paths)
            outcome.warning = warning
            result.append(outcome)
        return result

    ## Publish a displayed node's meshes and overlays by content key; keys the
    ## UI still holds cost nothing. CAD previews build under the node's stamp,
    ## so a newer request that still wants it lets the preview finish.
    func publish(self, node: Node, data: GeometryData) -> unit!:
        self.channel.begin(node.stamp)
        self.prepare(data, node) catch failure:
            self.channel.end()
            error(failure.code, failure.message)
        self.channel.end()
        self.store.refresh(node.stamp)
        _ = self.display(data, Matrix4(), node, 0)
        # CAD edges are keyed by what they are drawn from, so a CV drag keeps
        # them held; normal guides follow the result.
        let edges = data.edges_key()
        if edges != 0:
            self.publish_lines((edges, "cad-edges").hash(), node, data, true, false)
            if self.graph.show_normals:
                self.publish_lines((node.stamp, "guides").hash(), node, data, false, true)
        else:
            self.publish_lines((node.stamp, "overlays", self.graph.show_normals).hash(), node, data, true, self.graph.show_normals)
        self.publish_curves(node, data)
        self.publish_cad_hulls(node, data)
        self.publish_guides(node, data)

    ## The displayed Edit node's guides (Edit Sketch's plane grid), with the
    ## hull lines. Guides are drawn from the set's detail attributes, so
    ## their content key holds through a drag and the batch is kept.
    func publish_guides(self, node: Node, data: GeometryData) -> unit!:
        if node.id != self.graph.display or not node.enabled:
            return
        let kind = edit_kind(node.kind) else return
        let guides = kind.guides else return
        let key = (data.geometry.detail_key(), node.kind, "edit-guides").hash()
        if self.channel.holds(key):
            self.channel.reuse(key, node.id, false, false)
            return
        let lines = OverlayLines.make()
        guides(data, lines)
        if lines.count() > 0:
            self.channel.hull(key, node.id, lines)

    ## Overlay lines under `key`: the CAD edges (as wires and patch borders)
    ## with `edges`, the normal guides with `normals`.
    func publish_lines(self, key: int, node: Node, data: GeometryData, edges: bool, normals: bool) -> unit!:
        if self.channel.holds(key):
            self.channel.reuse(key, node.id, false, false)
            return
        let wires = OverlayLines.make()
        let guides = OverlayLines.make()
        let patches = OverlayLines.make()
        append_overlays(data, Matrix4(), wires if edges else OverlayLines.make(), guides if normals else OverlayLines.make(), polygons = false)
        if edges:
            append_patch_boundaries(data, Matrix4(), patches)
        if wires.count() + guides.count() + patches.count() > 0:
            self.channel.overlay(key, node.id, wires, guides, patches)

    ## CAD control nets go to the shared hull product, under their own key.
    func publish_cad_hulls(self, node: Node, data: GeometryData) -> unit!:
        let key = (node.stamp, "cad-hull").hash()
        if self.channel.holds(key):
            self.channel.reuse(key, node.id, false, false)
            return
        let lines = OverlayLines.make()
        append_cad_hulls(data, Matrix4(), lines)
        if lines.count() > 0:
            self.channel.hull(key, node.id, lines)

    ## Curves are drawn as lines, published under their own content key.
    func publish_curves(self, node: Node, data: GeometryData) -> unit!:
        let key = (node.stamp, "curves").hash()
        if self.channel.holds(key):
            self.channel.reuse(key, node.id, false, false)
            return
        self.channel.curves(key, node.id, data.geometry)

    func prepare(self, data: GeometryData, node: Node) -> unit!:
        if data.has_cad():
            # A drag's frames show moved control vertices' quick previews.
            let report = func (fraction: float) -> unit!:
                self.channel.progress(node.id, fraction * 0.9, "Tessellating CAD")
            let preview = data.preview(report, self.graph.gesture)
            if preview.failed_faces > 0:
                self.preview_warning = f"{preview.failed_faces} CAD preview patches failed: {preview.first_error}"
                self.channel.progress(node.id, 0.9, f"{preview.failed_faces} preview failures")
            elif preview.cv_warning != "":
                self.preview_warning = preview.cv_warning
        for index in 0..<data.instance_count():
            if data.instance_visible(index):
                self.prepare(data.instance(index), node)

    ## Deterministic traversal: a stamp always yields the same ordinals.
    func display(self, data: GeometryData, matrix: Matrix4, node: Node, ordinal: int) -> int!:
        self.channel.check()
        var next = ordinal
        if let mesh = data.mesh:
            self.show(mesh, matrix, node, next, true)
            next += 1
        # A drag's frames show a coarse SDF surface; the release, the full one.
        if let preview = data.field_preview(self.graph.gesture):
            self.show(preview, matrix, node, next, false)
            next += 1
        if data.has_cad():
            for mesh in data.preview().meshes:
                self.show(mesh, matrix, node, next, false)
                next += 1
        for index in 0..<data.instance_count():
            if data.instance_visible(index):
                next = self.display(data.instance(index), matrix.multiply(data.instance_matrix(index)), node, next)
        return next

    func show(self, mesh: Mesh, matrix: Matrix4, node: Node, ordinal: int, overlays: bool) -> unit!:
        let identity = matrix.is_identity()
        let key = self.key_of(mesh, (node.stamp, ordinal).hash()) if identity else (node.stamp, ordinal).hash()
        # Picking runs on the GPU; a query index is built by its first query.
        var shown: Mesh? = none
        if self.channel.holds(key):
            self.channel.reuse(key, node.id, true, false)
        else:
            let placed = mesh if identity else mesh.placed(matrix)
            shown = placed
            # A subdivided mesh's display is made here, off the UI thread: the
            # renderer then finds it cached (by connectivity and positions).
            _ = Subdivision.display(placed)
            self.channel.mesh(placed, key, node.id, true, false)
            if identity:
                self.meshes.append(mesh)
                self.keys.append(key)
        if overlays and self.graph.show_normals:
            let guides = (key, "guides").hash()
            if self.channel.holds(guides):
                self.channel.reuse(guides, node.id, false, false)
            else:
                self.channel.guides(guides, node.id, shown else (mesh if identity else mesh.placed(matrix)))

    ## The selected node's mesh for the spreadsheet and component picking. A
    ## mesh already published (displayed, or held from before) doubles as it.
    ## `level`: the Edit node's level whose pick geometry `mesh` is (-1 a
    ## node's own mesh), part of the key since levels pick on other meshes.
    func inspect(self, node: Node, mesh: Mesh, level: int = -1) -> unit!:
        let key = self.key_of(mesh, (node.stamp, "inspect", level).hash())
        if self.channel.holds(key) or self.listed(key):
            self.channel.reuse(key, node.id, false, true)
            return
        self.channel.mesh(mesh, key, node.id, false, true)
        self.meshes.append(mesh)
        self.keys.append(key)

    func key_of(self, mesh: Mesh, fallback: int) -> int:
        for at in 0..<self.meshes.length:
            if self.meshes[at] is mesh:
                return self.keys[at]
        return fallback

    func listed(self, key: int) -> bool:
        return key in self.keys

## Whether the result after `count` of `total` steps is kept: the last eight
## (instant undo and redo) and every sixteenth (a replay from any point costs
## at most fifteen steps), per D18.
pub func checkpoint(count: int, total: int) -> bool:
    return count >= total - 8 or count % 16 == 0

pub func compute_factory(token: int) -> ComputeHandler!:
    let computation = Computation(compute_bridge.channel(token))
    return computation.compute