Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.1

0.11.1 / src / compute_bridge.lucb

## App-owned worker boundary. Luce graphs and ARC owners never cross threads.
## Requests are copied text, applied in order: each is a delta against the one
## before, so none is dropped, but only the newest is cooked and published. A
## cook whose stamp the newest request still wants runs to completion into the
## worker's result store; stale publishing stops at the next check.
## Published products are keyed by content: the UI keeps the meshes it adopted,
## and the worker re-publishes a held key without copying, indexing or
## re-extracting overlays.
import interop
import memory
import thread
import sync
import c
from luce_geocore import geocore
from luce_geocore.geocore import detached_mesh, mesh_overlay_points
from luce_3d.graphics import Vector3, Matrix4, invalid, WireBatch, wire_batch_type, append_curve_segments
from luce_geocore.geocore import GeometrySet
from luce_geocore.core.growing import Growing
from overlay_lines import OverlayLines, line_points
import platform

pub type ComputeHandler = interop.Callback[str, unit]

struct Request:
    let message: interop.Packet[str]
    let generation: i64
    let wanted: i64[]

## A product of the current result. Fresh products own a deep mesh copy for
## the UI and freshly extracted overlays; others name a key the UI holds.
struct Product:
    let key: i64
    var mesh: geocore.Mesh*?
    let node: i64
    let display: bool
    var inspect: bool
    let fresh: bool
    var wires: Vector3[]
    var normals: Vector3[]
    var patches: Vector3[]
    ## Evaluated curves and their control hulls.
    var curves: Vector3[]
    var hulls: Vector3[]

## Overlays of a key the UI holds, kept so unchanged keys need no extraction.
struct Held:
    let key: i64
    var wires: Vector3[]
    var normals: Vector3[]
    var patches: Vector3[]
    var curves: Vector3[]
    var hulls: Vector3[]

struct State:
    var lock: sync.Mutex
    var changed: sync.Condition
    var stopping: bool = false
    var queue: Request[]
    var queued: usize = 0
    var wanted: i64[]
    var cooking: i64[64]
    var depth: usize = 0
    var requested: i64 = 0
    var active: i64 = 0
    var completed: i64 = 0
    var sequence: i64 = 0
    var node: i64 = -1
    var fraction: f64 = 0.0
    var phase: interop.Packet[str]? = none
    var report: interop.Packet[str]? = none
    var failed: bool = false
    var products: Product[1024]
    var count: usize = 0
    var held: Held[1024]
    var held_count: usize = 0
    var handle: thread.Handle
    let entry: interop.WorkerEntry[i64, str, unit]

    ## Drop the current result; untaken fresh copies are freed.
    func clear():
        for product in self.products[..<self.count]:
            if let mesh = product.mesh:
                mesh.close()
                free(mesh) in memory.heap
            release_points(product.wires)
            release_points(product.normals)
            release_points(product.patches)
            release_points(product.curves)
            release_points(product.hulls)
        self.count = 0
        if let report = self.report:
            report.release()
        self.report = none
        self.failed = false

    func holds(key: i64) -> bool:
        for held in self.held[..<self.held_count]:
            if held.key == key:
                return true
        return false

    func wants(stamp: i64) -> bool:
        for wanted in self.wanted:
            if wanted == stamp:
                return true
        return false

func release_points(points: Vector3[]):
    if points.length > 0:
        free(points) in memory.heap

func copy_points(points: const Vector3[]) -> Vector3[]!:
    var empty: Vector3[]
    if points.length == 0:
        return empty
    let output = try new Vector3[points.length] --- in memory.heap
    memory.copy(output, points, points.length)
    return output

## Borrowed synchronized channel valid only while the service's worker lives.
pub struct ComputeChannel:
    let state: State*

    pub func close():
        _ = self.state

    ## Cancellation point. A newer request cancels publishing, and a cook only
    ## when that request no longer wants the stamp being cooked.
    pub func check() -> !:
        self.state.lock.lock()
        defer self.state.lock.unlock()
        if self.state.stopping:
            error(invalid, "Computation superseded")
        if self.state.queued == 0:
            return
        if self.state.depth == 0 or not self.state.wants(self.state.cooking[self.state.depth - 1]):
            error(invalid, "Computation superseded")

    ## Whether a newer request waits; its delta must be applied before cooking.
    pub func superseded() -> bool:
        self.state.lock.lock()
        defer self.state.lock.unlock()
        return self.state.queued > 0 or self.state.stopping

    pub func begin(stamp: i64) -> !:
        self.state.lock.lock()
        defer self.state.lock.unlock()
        if self.state.depth == self.state.cooking.length:
            error(invalid, "Node cooks nest too deeply")
        self.state.cooking[self.state.depth] = stamp
        self.state.depth += 1

    pub func end():
        self.state.lock.lock()
        defer self.state.lock.unlock()
        if self.state.depth > 0:
            self.state.depth -= 1

    pub func progress(node: i64, fraction: f64, phase: str) -> !:
        try self.check()
        let copied = try interop.text_transfer.copy(phase)
        self.state.lock.lock()
        defer self.state.lock.unlock()
        if let previous = self.state.phase:
            previous.release()
        self.state.phase = copied
        self.state.node = node
        self.state.fraction = fraction
        self.state.sequence += 1

    ## True when the UI already holds this product key from the last result.
    pub func holds(key: i64) -> bool:
        self.state.lock.lock()
        defer self.state.lock.unlock()
        return self.state.holds(key)

    ## Publish a key the UI holds: no copy, index or overlay work.
    pub func reuse(key: i64, node: i64, display: bool, inspect: bool) -> !:
        try self.check()
        if self.merge(key, inspect):
            return
        try self.add(Product(key, none, node, display, inspect, false))

    ## Publish a new mesh product: a new owner of the worker's immutable storage
    ## (its caches included; the UI picks on the GPU, so nothing is warmed).
    pub func mesh(source: interop.Reference[geocore.Mesh], key: i64, node: i64, display: bool, inspect: bool) -> !:
        try self.check()
        if self.merge(key, inspect):
            return
        let copy = try detached_mesh(source)
        var product = Product(key, copy, node, display, inspect, true)
        errdefer:
            copy.close()
            free(copy) in memory.heap
            release_points(product.wires)
            release_points(product.normals)
        try self.check()
        try self.add(product)

    ## Publish `source`'s per-face normal guides under their own key; the
    ## worker makes them only while the viewport shows normals.
    pub func guides(key: i64, node: i64, source: interop.Reference[geocore.Mesh]) -> !:
        try self.check()
        if self.merge(key, false):
            return
        if self.holds(key):
            try self.add(Product(key, none, node, false, false, false))
            return
        var product = Product(key, none, node, false, false, true)
        errdefer release_points(product.normals)
        product.normals = try mesh_overlay_points(try source.get(), Matrix4(), true)
        try self.check()
        try self.add(product)

    ## Publish analytic CAD boundaries and normal guides under a content key.
    pub func overlay(key: i64, node: i64, wires: interop.Reference[OverlayLines], normals: interop.Reference[OverlayLines], patches: interop.Reference[OverlayLines]) -> !:
        try self.check()
        var product = Product(key, none, node, false, false, true)
        errdefer:
            release_points(product.wires)
            release_points(product.normals)
            release_points(product.patches)
        product.wires = try copy_points(line_points(try wires.get()))
        product.normals = try copy_points(line_points(try normals.get()))
        product.patches = try copy_points(line_points(try patches.get()))
        try self.add(product)

    ## Publish the curves of `geometry` (evaluated lines and control hulls)
    ## under a content key; nothing when it has no curves.
    pub func curves(key: i64, node: i64, geometry: interop.Reference[GeometrySet]) -> !:
        try self.check()
        var lines = Growing[Vector3]()
        defer lines.close()
        var hulls = Growing[Vector3]()
        defer hulls.close()
        try append_curve_segments(try geometry.get(), Matrix4(), &lines, &hulls)
        if lines.count + hulls.count == 0:
            return
        var product = Product(key, none, node, false, false, true)
        errdefer:
            release_points(product.curves)
            release_points(product.hulls)
        product.curves = try copy_points(lines.view())
        product.hulls = try copy_points(hulls.view())
        try self.add(product)

    ## Publish control hulls (world-space endpoint pairs: control polygons,
    ## CV nets, handle arms) under a content key. Any family's hulls go here;
    ## the viewport draws them together, behind a visibility toggle.
    pub func hull(key: i64, node: i64, lines: interop.Reference[OverlayLines]) -> !:
        try self.check()
        let points = line_points(try lines.get())
        if points.length == 0:
            return
        var product = Product(key, none, node, false, false, true)
        errdefer release_points(product.hulls)
        product.hulls = try copy_points(points)
        try self.add(product)

    ## A key already in this result gains the inspect role instead of a copy.
    func merge(key: i64, inspect: bool) -> bool:
        for index in 0..<self.state.count:
            if self.state.products[index].key == key:
                self.state.products[index].inspect = self.state.products[index].inspect or inspect
                return true
        return false

    func add(product: Product) -> !:
        if self.state.count == self.state.products.length:
            error(invalid, "Computed scene exceeds 1024 mesh batches")
        self.state.products[self.state.count] = product
        self.state.count += 1

    pub func report(text: str) -> !:
        let copied = try interop.text_transfer.copy(text)
        if let previous = self.state.report:
            previous.release()
        self.state.report = copied

pub let compute_channel_type: interop.Type[ComputeChannel] = interop.Type[ComputeChannel]("Worker compute channel", ComputeChannel.close)

## Called only by the named worker factory with its service-provided token.
pub func channel(token: i64) -> interop.Reference[ComputeChannel]!:
    return try interop.Reference[ComputeChannel].adopt(new ComputeChannel((State*)(usize)token) in memory.heap, compute_channel_type, memory.heap)

## A quarter of physical memory for cooked results; 4 GiB when unknown.
pub func memory_budget() -> i64:
    let total = physical_memory()
    return (i64)(total // 4) if total > 0 else 4294967296

## Main-thread owner. poll never waits for geometry work; close cancels and joins.
pub struct ComputeService:
    var state: State*?

    pub func init(entry: interop.WorkerEntry[i64, str, unit]) -> !:
        let state = try new State(entry = entry) in memory.heap
        errdefer free(state) in memory.heap
        state.queue = try new Request[16] --- in memory.heap
        errdefer free(state.queue) in memory.heap
        state.handle = try thread.spawn(run, (void*)state, name = "luced-3d compute")
        self.state = state

    ## Queue a delta request and the stamps it wants kept cooking.
    pub func submit(text: str, generation: i64, wanted: const i64[]) -> !:
        let state = self.state else error(invalid, "Compute service closed")
        let message = try interop.text_transfer.copy(text)
        errdefer message.release()
        let stamps = try new i64[wanted.length + 1] --- in memory.heap
        errdefer free(stamps) in memory.heap
        memory.copy(stamps, wanted, wanted.length)
        stamps[wanted.length] = 0
        state.lock.lock()
        defer state.lock.unlock()
        if state.queued == state.queue.length:
            let grown = try new Request[state.queue.length * 2] --- in memory.heap
            memory.copy(grown, state.queue, state.queued)
            free(state.queue) in memory.heap
            state.queue = grown
        let current = try new i64[wanted.length + 1] --- in memory.heap
        memory.copy(current, stamps, stamps.length)
        if state.wanted.length > 0:
            free(state.wanted) in memory.heap
        state.wanted = current
        state.queue[state.queued] = Request(message, generation, stamps)
        state.queued += 1
        state.requested = generation
        state.changed.broadcast()

    pub func sequence() -> i64:
        let state = self.state else return 0
        state.lock.lock()
        defer state.lock.unlock()
        return state.sequence

    pub func active_node() -> i64:
        let state = self.state else return -1
        state.lock.lock()
        defer state.lock.unlock()
        return state.node

    pub func progress() -> f64:
        let state = self.state else return 0.0
        state.lock.lock()
        defer state.lock.unlock()
        return state.fraction

    pub func phase() -> interop.Outcome[str]:
        let state = self.state else return interop.Outcome[str].success(interop.Owned[str](""))
        state.lock.lock()
        defer state.lock.unlock()
        let value = (state.phase else interop.Packet[str]("")).value
        return interop.Outcome[str].success(interop.copy_text(value) catch failure: trap(failure.message))

    ## Claim completed data. The worker waits for release_result before reusing
    ## this slot, so readers never race a pending replacement job.
    pub func completed() -> i64:
        let state = self.state else return 0
        state.lock.lock()
        defer state.lock.unlock()
        return state.completed

    pub func mesh_count() -> i64:
        return (i64)(self.state else return 0).count

    pub func mesh_key(index: i64) -> i64:
        return (self.state else return 0).products[(usize)index].key

    pub func mesh_node(index: i64) -> i64:
        return (self.state else return -1).products[(usize)index].node

    pub func mesh_display(index: i64) -> bool:
        return (self.state else return false).products[(usize)index].display

    pub func mesh_inspect(index: i64) -> bool:
        return (self.state else return false).products[(usize)index].inspect

    ## Whether the product carries a new mesh to adopt (else the UI holds it).
    pub func mesh_fresh(index: i64) -> bool:
        return (self.state else return false).products[(usize)index].mesh != none

    pub func take_mesh(index: i64) -> interop.Reference[geocore.Mesh]!:
        let state = self.state else error(invalid, "Compute service closed")
        if index < 0 or (usize)index >= state.count or state.completed == 0:
            error(invalid, "No completed mesh")
        let mesh = state.products[(usize)index].mesh else error(invalid, "Mesh already consumed")
        state.products[(usize)index].mesh = none
        return try interop.Reference[geocore.Mesh].adopt(mesh, geocore.mesh_type, memory.heap)

    ## Combined overlays of the current result: fresh and held keys.
    pub func wires() -> interop.Reference[WireBatch]!:
        return try self.batch(0)

    pub func normals() -> interop.Reference[WireBatch]!:
        return try self.batch(1)

    pub func patches() -> interop.Reference[WireBatch]!:
        return try self.batch(2)

    pub func curve_lines() -> interop.Reference[WireBatch]!:
        return try self.batch(3)

    ## Every published hull: curve control polygons and handles, CV nets.
    pub func hulls() -> interop.Reference[WireBatch]!:
        return try self.batch(4)

    ## What overlay `which`'s batch holds: the keys of the products with
    ## some of it (equal keys, equal lines), so an unchanged batch is kept.
    pub func batch_key(which: i64) -> i64!:
        let state = self.state else error(invalid, "Compute service closed")
        var key: u64 = 0x9E3779B97F4A7C15 ^ (u64)which
        for product in state.products[..<state.count]:
            if overlay_of(state, product, which).length > 0: key = (key ^ (u64)product.key) *% 0x100000001B3
        return (i64)(key >> 1)

    func batch(which: i64) -> interop.Reference[WireBatch]!:
        let state = self.state else error(invalid, "Compute service closed")
        var total: usize = 0
        for product in state.products[..<state.count]:
            total += overlay_of(state, product, which).length
        let points = try new Vector3[total] --- in memory.heap
        defer free(points) in memory.heap
        var at: usize = 0
        for product in state.products[..<state.count]:
            let part = overlay_of(state, product, which)
            memory.copy(points[at..], part, part.length)
            at += part.length
        return try interop.Reference[WireBatch].adopt(try new WireBatch(points) in memory.heap, wire_batch_type, memory.heap)

    pub func report() -> str:
        return ((self.state else trap("closed compute service")).report else interop.Packet[str]("")).value

    pub func failed() -> bool:
        return (self.state else trap("closed compute service")).failed

    ## Release the result. `kept` lists every key the UI holds from now on:
    ## held keys it names stay, and so do this result's fresh products the UI
    ## adopted (or that carry only overlays). All other held data is freed.
    pub func release_result(kept: const i64[]):
        let state = self.state else return
        state.lock.lock()
        defer state.lock.unlock()
        var count: usize = 0
        for index in 0..<state.held_count:
            if listed(kept, state.held[index].key):
                state.held[count] = state.held[index]
                count += 1
            else:
                release_points(state.held[index].wires)
                release_points(state.held[index].normals)
                release_points(state.held[index].patches)
                release_points(state.held[index].curves)
                release_points(state.held[index].hulls)
        state.held_count = count
        for index in 0..<state.count:
            let product = &state.products[index]
            if not product.fresh or product.mesh != none or not listed(kept, product.key) or state.held_count == state.held.length:
                continue
            state.held[state.held_count] = Held(product.key, product.wires, product.normals, product.patches, product.curves, product.hulls)
            state.held_count += 1
            var empty: Vector3[]
            product.wires = empty
            product.normals = empty
            product.patches = empty
            product.curves = empty
            product.hulls = empty
        state.clear()
        state.completed = 0
        state.changed.broadcast()

    pub func close():
        let state = self.state else return
        state.lock.lock()
        state.stopping = true
        state.changed.broadcast()
        state.lock.unlock()
        state.handle.join() catch failure: trap(failure.message)
        state.clear()
        for request in state.queue[..<state.queued]:
            request.message.release()
            free(request.wanted) in memory.heap
        free(state.queue) in memory.heap
        if state.wanted.length > 0:
            free(state.wanted) in memory.heap
        for held in state.held[..<state.held_count]:
            release_points(held.wires)
            release_points(held.normals)
            release_points(held.patches)
            release_points(held.curves)
            release_points(held.hulls)
        if let phase = state.phase:
            phase.release()
        free(state) in memory.heap
        self.state = none

pub let compute_service_type: interop.Type[ComputeService] = interop.Type[ComputeService]("3D compute service", ComputeService.close, closeable = true, main_thread = true)

func listed(keys: const i64[], key: i64) -> bool:
    for candidate in keys:
        if candidate == key:
            return true
    return false

func overlay_of(state: const State*, product: const Product, which: i64) -> const Vector3[]:
    if product.fresh:
        return pick(which, product.wires, product.normals, product.patches, product.curves, product.hulls)
    for held in state.held[..<state.held_count]:
        if held.key == product.key:
            return pick(which, held.wires, held.normals, held.patches, held.curves, held.hulls)
    var empty: Vector3[]
    return empty

## Overlay `which`: 0 wires, 1 normals, 2 patches, 3 curves, 4 curve hulls.
func pick(which: i64, wires: const Vector3[], normals: const Vector3[], patches: const Vector3[], curves: const Vector3[], hulls: const Vector3[]) -> const Vector3[]:
    match which:
        0: return wires
        1: return normals
        2: return patches
        3: return curves
        _: return hulls

func run(raw: void*?):
    let state = (State*)(raw else return)
    state.entry.enter()
    defer state.entry.leave()
    let opened = state.entry.open(state.entry.context, (i64)(usize)state)
    defer opened.release()
    let handler = opened.get() catch failure: trap(failure.message)
    while true:
        state.lock.lock()
        while (state.queued == 0 or state.completed != 0) and not state.stopping:
            state.changed.wait(&state.lock)
        if state.stopping:
            state.lock.unlock()
            return
        let request = state.queue[0]
        state.queued -= 1
        memory.move(state.queue, state.queue[1..], state.queued)
        state.active = request.generation
        state.depth = 0
        state.lock.unlock()
        let result = handler.invoke(request.message.value)
        result.get() catch failure:
            state.failed = true
            if let previous = state.report:
                previous.release()
            state.report = interop.text_transfer.copy(failure.message) catch exhausted: trap(exhausted.message)
            recover ()
        result.release()
        request.message.release()
        free(request.wanted) in memory.heap
        state.lock.lock()
        if state.queued == 0 and state.active == state.requested and not state.stopping:
            state.completed = state.active
        else:
            state.clear()
        state.sequence += 1
        state.lock.unlock()

## The machine's physical memory in bytes; 0 when it cannot be told.
func physical_memory() -> u64:
    if platform.macos:
        var value: u64 = 0
        var length: usize = 8
        if sysctlbyname((c.str)"hw.memsize", (void*)&value, &length, none, 0) != 0:
            return 0
        return value
    elif platform.windows:
        # MEMORYSTATUSEX: dwLength first, ullTotalPhys at byte 8.
        var status: u8[64]
        memory.set(status, 0)
        status[0] = 64
        if GlobalMemoryStatusEx((void*)&status[0]) == 0:
            return 0
        var total: u64 = 0
        for index in 0..<(usize)8:
            total |= (u64)status[8 + index] << (u64)(index * 8)
        return total
    else:
        # _SC_PHYS_PAGES and _SC_PAGESIZE on Linux.
        let pages = sysconf(85)
        let size = sysconf(30)
        return (u64)pages * (u64)size if pages > 0 and size > 0 else 0

extern func sysctlbyname(name: c.str, value: void*, length: usize*, new_value: void*?, new_length: usize) -> i32
extern func sysconf(name: i32) -> i64
extern func GlobalMemoryStatusEx(status: void*) -> i32