Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.9

0.11.9 / src / compute_session.luc

## UI-side scheduling: stamp the graph, send the delta, poll progress and
## adopt reports. A node is dirty while its stamp has no reported result.
from compute_bridge import ComputeService
from compute_worker import compute_factory
from compute_request import RequestWriter, read_report
from network import Network
from stamps import stamp_graph

pub class ComputeSession:
    pub let service: ComputeService
    pub var generation: int = 0
    pub var applied: int = 0
    var sequence: int = -1
    pub var busy: bool = false
    ## Set when the worker asked for a full request; the caller resubmits once.
    pub var resync: bool = false
    ## Size of the last request text and Edit recipes the last cook applied.
    pub var request_size: int = 0
    pub var recipes: int = 0
    ## What the worker's result store held after the last cook: results and
    ## their bytes.
    pub var results: int = 0
    pub var result_bytes: int = 0
    ## The last report's Project shadows and the Sketch stamp they are for.
    pub var shadows: str = ""
    pub var shadows_stamp: int = 0
    ## The last report's Slice cap and the Sketch stamp it is for.
    pub var section: str = ""
    pub var section_stamp: int = 0
    let writer: RequestWriter
    var resyncs: int = 0
    ## The graph a request is wanted for while another waits (see submit).
    var deferred: Network? = none
    var deferred_mode: int = 0
    var deferred_normals: bool = false

    pub func init(self) -> unit!:
        self.service = ComputeService(compute_factory)
        self.writer = RequestWriter()

    ## Ask the worker for `graph` as it is. Requests coalesce: while one
    ## waits for the worker to take it, a newer one is not sent but kept as
    ## wanted (`poll_progress` sends it once the worker takes the waiting
    ## one, encoded from the graph as it is then), so however fast moves
    ## come and however slow the worker is, at most one request waits and
    ## the worker cooks only the newest state. (Each request is a delta
    ## against the last one sent, so skipping states loses nothing.)
    pub func submit(self, graph: Network, mode: int, normals: bool = false) -> unit!:
        stamp_graph(graph)
        self.generation += 1
        self.busy = true
        let wanted: list[int] = []
        # A drag's frames cook in a few milliseconds each: their nodes show
        # no Queued or Computing, which would flicker on every pointer move
        # (the network view and the parameters' line).
        let quiet = graph.quiet
        for node in graph.nodes:
            wanted.append(node.stamp)
            node.dirty = node.stamp != node.cooked
            # An Edit action the worker runs (edit_held.luc) shows its
            # progress even inside a gesture: it may take a while.
            let shown = not quiet or node.held_base != 0
            node.computing = node.dirty and shown
            node.progress = 0.0
            node.phase = "Queued" if node.dirty and shown else ""
        if self.service.queued() > 0:
            self.deferred = graph
            self.deferred_mode = mode
            self.deferred_normals = normals
            return
        self.send(graph, mode, normals, wanted)

    func send(self, graph: Network, mode: int, normals: bool, wanted: list[int]) -> unit!:
        self.deferred = none
        let request = self.writer.encode(graph, mode, normals)
        self.request_size = request.byte_count
        self.service.submit(request, self.generation, wanted)

    ## Send the request kept as wanted once the worker took the one waiting.
    func send_deferred(self) -> unit!:
        let graph = self.deferred else return
        if self.service.queued() > 0:
            return
        let stamps: list[int] = []
        for node in graph.nodes:
            stamps.append(node.stamp)
        self.send(graph, self.deferred_mode, self.deferred_normals, stamps)

    pub func poll_progress(self, graph: Network) -> bool!:
        self.send_deferred()
        let sequence = self.service.sequence()
        if sequence == self.sequence:
            return false
        self.sequence = sequence
        for node in graph.nodes:
            if node.phase != "Queued":
                node.computing = false
        if let node = graph.find(self.service.active_node()):
            node.computing = self.busy and (not graph.quiet or node.held_base != 0)
            node.progress = self.service.progress()
            node.phase = self.service.phase()
        return true

    pub func read_report(self, graph: Network) -> str!:
        self.busy = false
        self.applied = self.generation
        self.resync = false
        if self.service.failed():
            # The worker's mirror may hold part of a failed delta.
            self.writer.reset()
            for node in graph.nodes:
                node.computing = false
                node.phase = ""
            return self.service.report()
        let report = read_report(self.service.report())
        self.recipes = report.recipes
        self.results = report.results
        self.result_bytes = report.result_bytes
        self.shadows = report.shadows
        self.shadows_stamp = report.shadows_stamp
        self.section = report.section
        self.section_stamp = report.section_stamp
        if report.resync:
            # One full resend; a second refusal is reported, not retried.
            self.writer.reset()
            self.resyncs += 1
            self.resync = self.resyncs == 1
            return report.notice
        self.resyncs = 0
        for outcome in report.outcomes:
            let node = graph.find(outcome.id) else continue
            node.cooked = outcome.cooked
            node.dirty = node.stamp != outcome.cooked
            node.failure = outcome.failure
            node.warning = outcome.warning
            node.console = outcome.console
            node.sketch_state = outcome.sketch_state
            node.automatic = outcome.automatic
            # Handles stay where the last good cook put them while one is in
            # flight or fails (a radius dragged too large keeps its arrow).
            if outcome.cooked != 0 and outcome.failure == "":
                node.handles = outcome.handles
            # An Edit node holding its result made it here: its sketches,
            # handles and the last action's aids are that result's.
            if let held = node.held:
                node.sketch_state = held.result.sketch_state
                node.handles = held.result.handles
            node.summary = outcome.summary
            node.evaluations = outcome.evaluations
            node.group_paths = outcome.paths
            node.curve_paths = outcome.curves
            node.computing = false
            node.phase = ""
            node.cache = none
        return report.notice

    pub func close(self):
        self.service.close()