Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-3d

Source of release 0.9.6

0.9.6 / src / renderers / retained.lucb

## Bound retained batches below Vulkan's guaranteed 128 MiB storage-buffer range.
## Chunking also makes large imported meshes independent of UI frame budgets.
from luce_gpu import gpu
import memory

pub struct RetainedBatch:
    var buffers: gpu.Geometry[]
    var count: usize = 0

    pub func is_open() -> bool:
        return self.count > 0

    pub func upload(device: gpu.Device, vertices: const gpu.Vertex[], lines: bool = false) -> !:
        self.close()
        let chunk: usize = 3145728
        self.buffers = try new gpu.Geometry[(vertices.length + chunk - 1) // chunk] --- in memory.heap
        self.count = 0
        errdefer self.close()
        var offset: usize = 0
        while offset < vertices.length:
            let end = offset + chunk if vertices.length - offset > chunk else vertices.length
            self.buffers[self.count] = try gpu.Geometry.create(device, vertices[offset..<end], lines)
            self.count += 1
            offset = end

    pub func draw(target: const gpu.RenderTarget*, matrix: const f32[], color: gpu.Color = gpu.Color(1.0, 1.0, 1.0), width: f64 = 1.0, slope_bias: f64 = 0.0, bias: f64 = 0.000001) -> !:
        for buffer in self.buffers[..<self.count]: try gpu.draw_geometry(target, buffer, matrix, color, width, bias, slope_bias = slope_bias)

    pub func close():
        for at in 0..<self.count: self.buffers[at].destroy()
        if self.buffers.length > 0: free(self.buffers) in memory.heap
        var empty: gpu.Geometry[]
        self.buffers = empty
        self.count = 0