Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-3d

Source of release 0.9.6

0.9.6 / src / renderers / wire_batch.lucb

## World-space line endpoint pairs kept on the GPU: sent once, then a frame
## (a camera move, a hover) draws them from their buffers, depth writing or
## not. A frame without a device draws them through WireRenderer.
from luce_gpu import gpu
import interop
import memory
from luce_std import math
import errors
from luce_geocore.geocore import Vector3, Bounds
from cameras.camera import Camera
from renderers.wire import WireRenderer
from renderers.mesh_batch import draw_lines
from renderers.overlay_layers import Storage

pub struct WireBatch:
    ## The endpoints: `used` of the heap array `storage` (adopted from
    ## their maker, or copied).
    var storage: Vector3[]
    var used: usize
    ## Their GPU copy: f32 offsets from `origin`, and 0, 1, 2, ... as pairs.
    var positions: Storage
    var pairs: Storage
    var origin: Vector3
    var sent: bool

    ## A batch of a copy of `points`.
    pub func init(points: const Vector3[]) -> !:
        var copied: Vector3[]
        if points.length > 0:
            copied = try new Vector3[points.length] --- in memory.heap
            for index in 0..<points.length:
                copied[index] = points[index]
        self.storage = copied
        self.used = points.length
        self.positions = Storage()
        self.pairs = Storage()
        self.origin = Vector3()
        self.sent = false

    ## A batch of the first `count` of `points`, a heap array it takes over
    ## (no copy): Base makers hand theirs on.
    pub static func adopting(points: Vector3[], count: usize) -> WireBatch!:
        var nothing: Vector3[]
        var batch = try WireBatch(nothing)
        batch.storage = points
        batch.used = count
        return batch

    pub func count() -> i64:
        return (i64)self.used // 2

    ## The box around every endpoint (framing); fails when there are none.
    pub func bounds() -> Bounds!:
        if self.used == 0: error(errors.invalid, "an empty wire batch has no bounds")
        var low = self.storage[0]
        var high = low
        for p in self.storage[..<self.used]:
            low = Vector3(math.min(low.x, p.x), math.min(low.y, p.y), math.min(low.z, p.z))
            high = Vector3(math.max(high.x, p.x), math.max(high.y, p.y), math.max(high.z, p.z))
        return Bounds(low, high)

    ## Draw the lines, depth tested, pulled `bias` toward the eye (as
    ## Projection.clip's). With `writes_depth` off they leave the depth buffer
    ## alone (a grid: splats and fog drawn later cover it).
    pub func draw(camera: interop.Reference[Camera], target: interop.View[gpu.RenderTarget], color: gpu.Color, width: f64 = 1.0, writes_depth: bool = true,
                  bias: f64 = 0.000001) -> !:
        if self.used == 0: return
        let native = try target.get()
        var device = gpu.device_of(native) catch failure:
            if failure.code != gpu.unavailable: error(failure.code, failure.message)
            recover gpu.Device()
        defer device.destroy()
        if not device.is_open():
            try WireRenderer.lines(self.storage[..<self.used], camera, target, color, width, bias, writes_depth = writes_depth)
            return
        if not self.sent: try self.send(device)
        let size = try native.size()
        let projection = try camera.value().projection(size.width / size.height)
        try draw_lines(native, self.positions.buffer, self.origin, self.pairs.buffer, self.used // 2, projection, color, width, bias, writes_depth)

    ## Send the endpoints: f32 offsets from the first keep their precision
    ## far from the world origin.
    func send(device: gpu.Device) -> !:
        self.origin = self.storage[0]
        let packed = try new f32[self.used * 3] --- in memory.heap
        defer free(packed) in memory.heap
        let order = try new u32[self.used] --- in memory.heap
        defer free(order) in memory.heap
        for at in 0..<self.used:
            let offset = self.storage[at].subtract(self.origin)
            packed[at * 3] = (f32)offset.x
            packed[at * 3 + 1] = (f32)offset.y
            packed[at * 3 + 2] = (f32)offset.z
            order[at] = (u32)at
        _ = try self.positions.put(device, u8[]((u8*)packed.data, packed.length * 4))
        _ = try self.pairs.put(device, u8[]((u8*)order.data, order.length * 4))
        self.sent = true

    pub func close():
        self.positions.close()
        self.pairs.close()
        if self.storage.length > 0: free(self.storage) in memory.heap

pub let wire_batch_type: interop.Type[WireBatch] = interop.Type[WireBatch]("WireBatch", WireBatch.close)