Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-cad

Source of release 0.9.5

0.9.5 / src / tessellation / face_jobs.lucb

## Bounded native face jobs on the CAD worker pool. Workers borrow immutable
## CAD/edge data; each owns its native scratch and ARC temporaries. Only copied
## raw arrays cross back.

pub type FaceMesher = func(MeshBuilder*, BrepFace, Surface, const Curve[], const Samples[], GridLayout, Density, f64, f64) -> unit!

## Faces left out of the tessellation: edited faces that no longer meshed
## (`dropped`), and other faces no mesher could follow (`skipped`, one
## "Skipped CAD face i: reason" line each in `lines`); and the points shared
## by faces (vertices, edge samples, layout rows), which come before every
## face's own points.
pub struct FaceNotes:
    pub var dropped: usize = 0
    pub var skipped: usize = 0
    pub var lines: Growing[u8] = Growing[u8]()
    pub var shared_points: usize = 0

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

## Note face `index` of `faces` that failed with `message` ("CAD face i:
## reason"): an edited one is dropped, any other skipped with a line.
pub func note_failure(notes: FaceNotes*, edited: bool, message: str) -> !:
    if edited:
        notes.dropped += 1
        return
    notes.skipped += 1
    try append_text(&notes.lines, "Skipped ")
    try append_text(&notes.lines, message)
    try notes.lines.push(10)

struct Product:
    var points: Vector3[]
    var sizes: i64[]
    var corners: i64[]
    var display: i64[]
    var normals: f64[]
    var owns_normals: bool = true
    var failure: interop.Packet[str]? = none
    ## The failure is not the face's own (running out of memory): it fails
    ## the tessellation with this code.
    var fatal: bool = false
    var code: ErrorCode = invalid

    func close():
        if self.points.length > 0: free(self.points) in memory.heap
        if self.sizes.length > 0: free(self.sizes) in memory.heap
        if self.corners.length > 0: free(self.corners) in memory.heap
        if self.display.length > 0: free(self.display) in memory.heap
        if self.owns_normals and self.normals.length > 0: free(self.normals) in memory.heap
        if let failure = self.failure: failure.release()

struct Jobs:
    var lock: sync.Mutex
    var next: usize = 0
    var completed: usize = 0
    var stop: bool = false
    var failure: interop.Packet[str]? = none
    let points: const Vector3[]
    let edges: const Curve[]
    let surfaces: const Surface[]
    let faces: const BrepFace[]
    let samples: const Samples[]
    let plans: const GridLayout[]
    let divisions: Density
    let tolerance: f64
    let edge_size: f64
    let products: Product[]
    let mesher: FaceMesher
    ## The faces to mesh (the rest come from the cache).
    let order: const usize[]

## One pool item per worker: each keeps one scratch builder for all its faces.
func run(raw: void*, slot: usize, worker: usize):
    _ = slot
    let jobs = (Jobs*)raw
    run_jobs(jobs) catch failure:
        jobs.lock.lock()
        defer jobs.lock.unlock()
        if jobs.failure == none:
            jobs.failure = interop.text_transfer.copy(failure.message) catch exhausted: trap(exhausted.message)
        jobs.stop = true

func run_jobs(jobs: Jobs*) -> !:
    var scratch = try MeshBuilder(precise = true)
    defer scratch.close()
    for point in jobs.points:
        _ = try scratch.point(point)
    while true:
        jobs.lock.lock()
        if jobs.stop or jobs.next == jobs.order.length:
            jobs.lock.unlock()
            return
        let index = jobs.order[jobs.next]
        jobs.next += 1
        jobs.lock.unlock()
        scratch.np = jobs.points.length
        scratch.nf = 0
        scratch.nc = 0
        prepare(jobs, index, &scratch) catch failure:
            var context: u8[1024]
            let message = strings.format(context, f"CAD face {index}: {failure.message}") catch exhausted: recover failure.message
            jobs.products[index].failure = interop.text_transfer.copy(message) catch exhausted: trap(exhausted.message)
            # A face no mesher follows is left out (mesh_faces); running out
            # of memory stops the rest. Lower indices have already been
            # assigned; joining their in-flight jobs keeps the reported first
            # failure deterministic across schedules.
            if failure.code != invalid:
                jobs.products[index].fatal = true
                jobs.products[index].code = failure.code
                jobs.lock.lock()
                jobs.stop = true
                jobs.lock.unlock()
        jobs.lock.lock()
        jobs.completed += 1
        jobs.lock.unlock()

func prepare(jobs: Jobs*, index: usize, scratch: MeshBuilder*) -> !:
    let face = jobs.faces[index]
    let plan = jobs.plans[index]
    try jobs.mesher(scratch, face, jobs.surfaces[(usize)face.surface], jobs.edges, jobs.samples, plan, jobs.divisions, jobs.tolerance, jobs.edge_size)
    let product = &jobs.products[index]
    product.points = try new Vector3[scratch.np - jobs.points.length] --- in memory.heap
    product.sizes = try new i64[scratch.nf] --- in memory.heap
    product.corners = try new i64[scratch.nc] --- in memory.heap
    memory.copy(product.points, scratch.points[jobs.points.length..<scratch.np], product.points.length)
    memory.copy(product.sizes, scratch.sizes, scratch.nf)
    memory.copy(product.corners, scratch.corners, scratch.nc)
    if scratch.display.length > 0:
        product.display = try new i64[(scratch.nc - scratch.nf * 2) * 3] --- in memory.heap
        memory.copy(product.display, scratch.display, product.display.length)
    product.owns_normals = not (plan.applied.length > 0 and plan.applied[0])
    product.normals = (try face_normals(scratch, 0, scratch.nc, face, jobs.surfaces[(usize)face.surface], jobs.tolerance)) if product.owns_normals else plan.normals[0].values

## The caller invokes progress on its own thread. Results are assembled in face
## order, so parallel scheduling cannot change point IDs, topology or attributes.
## With a cache, faces whose inputs are unchanged reuse their pieces and the
## rest are kept for next time (face_cache.lucb).
## A face that no longer meshes does not fail the model: it is left out
## and `notes` records it (note_failure): dropped when its surface's cvs
## were moved (Surface.edited, its trims pulled into a shape the mesher
## rejects), else skipped with its reason. Only running out of memory, or
## every face failing, fails the tessellation.
pub func mesh_faces(mesh: MeshBuilder*, edges: const Curve[], surfaces: const Surface[], faces: const BrepFace[], samples: const Samples[], plans: const GridLayout[], divisions: Density, tolerance: f64, edge_size: f64, progress: interop.Callback[f64, unit]?, mesher: FaceMesher, notes: FaceNotes*, cache: FaceCache*? = none) -> f64[]!:
    let products = try new Product[faces.length] --- in memory.heap
    defer free(products) in memory.heap
    for index in 0..<products.length:
        products[index] = Product()
    defer close_products(products)
    # Cached faces: their keys, tables and pieces.
    var tables = Tables()
    defer tables.close()
    let keys = try new u64[faces.length] --- in memory.heap
    defer free(keys) in memory.heap
    let found = try new Piece*?[faces.length] --- in memory.heap
    defer free(found) in memory.heap
    for index in 0..<faces.length: found[index] = none
    defer release_found(found)
    var order = Growing[usize]()
    defer order.close()
    if let store = cache:
        try face_tables(faces, samples, plans, &tables)
        var keying = Keying(faces, surfaces, edges, samples, plans, mesh.points[..<mesh.np], &tables, divisions, tolerance, edge_size, keys)
        try parallel.for_each_range(faces.length, 16, (void*)&keying, key_faces)
        for index in 0..<faces.length: found[index] = store.find(index, keys[index])
    for index in 0..<faces.length:
        if found[index] == none: try order.push(index)
    var jobs = Jobs(points = mesh.points[..<mesh.np], edges = edges, surfaces = surfaces, faces = faces, samples = samples, plans = plans, divisions = divisions, tolerance = tolerance, edge_size = edge_size, products = products, mesher = mesher, order = order.view())
    defer release_failure(&jobs)
    let threads = job_threads()
    let wanted = order.count
    var job = try parallel.start(threads if threads < wanted else wanted, (void*)&jobs, run)
    var running = true
    defer:
        if running: stop_jobs(&jobs, &job)
    var reported: usize = 0
    while true:
        jobs.lock.lock()
        let completed = jobs.completed
        let stopped = jobs.stop
        jobs.lock.unlock()
        if completed != reported:
            if let report = progress:
                let outcome = report.invoke((f64)completed / (f64)math.max(1.0, (f64)wanted) * 0.9)
                defer outcome.release()
                try outcome.get()
            reported = completed
        if completed == wanted or stopped:
            break
        thread.sleep(2)
    stop_jobs(&jobs, &job)
    running = false
    if let failure = jobs.failure:
        let owned = try interop.copy_text(failure.value)
        if let owner = owned.owner: _ = ownership.pool_object(owner)
        error(invalid, owned.value)
    # Check before allocating or assembling partial products. Unscheduled
    # faces are empty, while every assigned worker has now been joined.
    var failed: usize = 0
    var first_failed: usize = products.length
    for index in 0..<products.length:
        let failure = products[index].failure else continue
        if products[index].fatal: try fail(failure.value, products[index].code)
        failed += 1
        if first_failed == products.length: first_failed = index
        try note_failure(notes, surfaces[(usize)faces[index].surface].edited, failure.value)
    if failed == faces.length and first_failed < products.length:
        try fail((products[first_failed].failure else error(invalid, "every CAD face failed")).value)
    let boundary = mesh.np
    # New faces become pieces for next time (unless a corner falls outside
    # the face's boundary table).
    if let store = cache:
        for index in order.view():
            if products[index].failure != none: continue
            if let piece = try made_piece(&products[index], index, boundary, &tables, keys[index], plans[index]): try store.put(index, piece)
    var normal_count: usize = 0
    for index in 0..<products.length:
        if let piece = found[index]: normal_count += piece.normals.length
        elif products[index].failure == none: normal_count += products[index].normals.length
    let normals = try new f64[normal_count] --- in memory.heap
    errdefer free(normals) in memory.heap
    # One face's display triangles, sized by the largest face (any size).
    var largest: i64 = 3
    for index in 0..<products.length:
        if let piece = found[index]:
            for size in piece.sizes:
                if size > largest: largest = size
        elif products[index].failure == none:
            for size in products[index].sizes:
                if size > largest: largest = size
    let local_indices = try new i64[(usize)(largest - 2) * 3] --- in memory.heap
    defer free(local_indices) in memory.heap
    var normal_offset: usize = 0
    for index in 0..<products.length:
        if let piece = found[index]:
            try place_piece(mesh, piece, index, &tables, plans[index], local_indices)
            memory.copy(normals[normal_offset..<normal_offset + piece.normals.length], piece.normals, piece.normals.length)
            normal_offset += piece.normals.length
            continue
        let product = products[index]
        if product.failure != none: continue
        let base = mesh.np
        for point in product.points:
            _ = try mesh.point(point)
        var corner: usize = 0
        var display: usize = 0
        for size in product.sizes:
            let first = corner
            try mesh.face(size, (i64)index)
            for at in 0..<size:
                let id = product.corners[corner]
                try mesh.corner(id if (usize)id < boundary else (i64)(base + (usize)id - boundary))
                corner += 1
            let count = ((usize)size - 2) * 3
            if product.display.length > 0 and product.display[display] >= 0:
                for at in 0..<count: local_indices[at] = product.display[display + at] - (i64)first
                try mesh.set_last_display(local_indices[..<count])
            display += count
        memory.copy(normals[normal_offset..<normal_offset + product.normals.length], product.normals, product.normals.length)
        normal_offset += product.normals.length
    return normals

## Keys of every face, in parallel (pure reads).
struct Keying:
    let faces: const BrepFace[]
    let surfaces: const Surface[]
    let edges: const Curve[]
    let samples: const Samples[]
    let plans: const GridLayout[]
    let points: const Vector3[]
    let tables: const Tables*
    let divisions: Density
    let tolerance: f64
    let edge_size: f64
    let keys: u64[]

func key_faces(context: void*, first: usize, end: usize, worker: usize):
    let keying = (Keying*)context
    for index in first..<end:
        keying.keys[index] = face_key(index, keying.faces, keying.surfaces, keying.edges, keying.samples, keying.plans, keying.points, keying.tables, keying.divisions, keying.tolerance, keying.edge_size)

## Face `index`'s product as a piece: boundary corners by table entry, or
## none when one is not in the table.
func made_piece(product: Product*, index: usize, boundary: usize, tables: const Tables*, key: u64, plan: GridLayout) -> Piece*?!:
    let corners = try new i64[product.corners.length] --- in memory.heap
    for at in 0..<corners.length:
        let id = product.corners[at]
        if (usize)id >= boundary:
            corners[at] = id - (i64)boundary
            continue
        let entry = tables.entry(index, id)
        if entry < 0:
            free(corners) in memory.heap
            return none
        corners[at] = -entry - 1
    var points: Vector3[]
    var sizes: i64[]
    var display: i64[]
    var normals: f64[]
    errdefer:
        free(corners) in memory.heap
        if points.length > 0: free(points) in memory.heap
        if sizes.length > 0: free(sizes) in memory.heap
        if display.length > 0: free(display) in memory.heap
        if normals.length > 0: free(normals) in memory.heap
    points = try copied(product.points)
    sizes = try copied(product.sizes)
    display = try copied(product.display)
    normals = try copied(product.normals)
    return try new_piece(key, points, sizes, corners, display, normals, plan.method[0] if plan.method.length > 0 else 0, plan.applied.length > 0 and plan.applied[0])

## Append a cached face: its own points, then faces over them and its
## boundary table's current ids; its plan gets the strategy it records.
func place_piece(mesh: MeshBuilder*, piece: const Piece*, index: usize, tables: const Tables*, plan: GridLayout, local_indices: i64[]) -> !:
    if plan.method.length > 0: plan.method[0] = piece.method
    if plan.applied.length > 0: plan.applied[0] = piece.applied
    let table = tables.of(index)
    let base = mesh.np
    for point in piece.points: _ = try mesh.point(point)
    var corner: usize = 0
    var display: usize = 0
    for size in piece.sizes:
        let first = corner
        try mesh.face(size, (i64)index)
        for at in 0..<size:
            let id = piece.corners[corner]
            try mesh.corner((i64)base + id if id >= 0 else table[(usize)(-id - 1)])
            corner += 1
        let count = ((usize)size - 2) * 3
        if piece.display.length > 0 and piece.display[display] >= 0:
            for at in 0..<count: local_indices[at] = piece.display[display + at] - (i64)first
            try mesh.set_last_display(local_indices[..<count])
        display += count

func release_found(found: Piece*?[]):
    for piece in found:
        if let held = piece: release_piece(held)

func stop_jobs(jobs: Jobs*, job: parallel.Job*):
    jobs.lock.lock()
    jobs.stop = true
    jobs.lock.unlock()
    # Items catch their own failures (jobs.failure): the job itself has none to give.
    job.wait() catch failure: _ = failure

## Fail with a copy of a face's failure `message`.
func fail(message: str, code: ErrorCode = invalid) -> !:
    let owned = try interop.copy_text(message)
    if let owner = owned.owner: _ = ownership.pool_object(owner)
    error(code, owned.value)

func close_products(products: const Product[]):
    for product in products: product.close()

func release_failure(jobs: Jobs*):
    if let failure = jobs.failure: failure.release()

## Faces meshed at once: one long-running item per pool thread but one (a job
## runs on the pool's threads; this one polls progress), at least one.
pub func job_threads() -> usize:
    let width = parallel.workers(most_job_threads)
    return width - 1 if width > 1 else 1

## More long-running face items than this never help.
let most_job_threads: usize = 256