dymokomi/luce-3d
Source of release 0.9.5
0.9.5 / src / renderers / gpu_arrays.lucb
## GPU copies of mesh arrays, keyed by the change ids of the arrays they were
## made from. Equal ids mean equal contents, so a mesh drawn again, a snapshot,
## or a moved mesh sharing its topology finds its buffers here and uploads only
## what changed: a Move re-uploads positions (8 MB for 700k points), not the
## whole mesh. Copies unused for a while are dropped.
from luce_gpu import gpu
import memory
from luce_geocore.core.growing import Growing
## Frames an unused copy survives, and the bytes over which unused copies
## go at once.
let keep_frames: u64 = 64
let budget: usize = 1073741824
## What a buffer holds: the source array's id, which array (tag) and which
## chunk of it (part).
pub struct ArrayKey:
pub var id: u64
pub var tag: u8
pub var part: u32
## An auto range the curvature map found for a source and value kind.
struct Range:
var key: u64
var kind: i64
var value: f64
struct Entry:
var key: ArrayKey
var buffer: gpu.Buffer
var bytes: usize
var used: u64
pub struct GpuArrays:
var entries: Growing[Entry]
var scratch: Growing[u8]
var device: gpu.Device
var frame: u64
## Recent curvature ranges, oldest overwritten.
var ranges: Range[8]
var next_range: usize
## Uploads and bytes so far, for tests and benchmarks.
pub var uploads: u64
pub var uploaded: u64
pub func init():
self.entries = Growing[Entry]()
self.scratch = Growing[u8]()
self.device = gpu.Device()
self.frame = 0
self.ranges = [Range(0, 0, 0.0), Range(0, 0, 0.0), Range(0, 0, 0.0), Range(0, 0, 0.0), Range(0, 0, 0.0), Range(0, 0, 0.0), Range(0, 0, 0.0), Range(0, 0, 0.0)]
self.next_range = 0
self.uploads = 0
self.uploaded = 0
## Start a frame on `device`; a different device drops every copy.
pub func begin(device: gpu.Device) -> !:
if self.device.is_open() and not self.device.same(&device):
self.clear()
if not self.device.is_open():
self.device = try device.share()
self.frame += 1
## A remembered curvature range of `key` for value `kind`, and a new one.
pub func range(key: u64, kind: i64) -> f64?:
for entry in self.ranges:
if entry.key == key and entry.kind == kind and entry.value > 0.0: return entry.value
return none
pub func remember_range(key: u64, kind: i64, value: f64):
self.ranges[self.next_range] = Range(key, kind, value)
self.next_range = (self.next_range + 1) % self.ranges.length
## Whether a frame has named the device the copies live on; and a share
## of it (the caller releases it).
pub func has_device() -> bool:
return self.device.is_open()
pub func device_handle() -> gpu.Device:
return self.device
## The copy of `key`, marked used this frame.
pub func find(key: ArrayKey) -> gpu.Buffer?:
for at in 0..<self.entries.count:
let entry = &self.entries.items[at]
if entry.key == key:
entry.used = self.frame
return entry.buffer
return none
## Room to pack `bytes` bytes before `store`; reused between arrays.
pub func staging(bytes: usize) -> u8[]!:
try self.scratch.reserve(bytes)
return self.scratch.items[..<bytes]
## Upload `data` as the copy of `key`. When the copy of `parent` (the
## array `key`'s was derived from) has the same size and no draw of this
## frame reads it, it is rewritten in place: the device orders the copy
## after earlier frames, which keep the old contents.
## Rewrite bytes `offset`.. of the copy of `parent` (same tag and part,
## `total` bytes, unused this frame) with `data` and rename it `key`: a
## local edit uploads its neighbourhood. None when there is no such copy.
pub func patch(key: ArrayKey, parent: u64, total: usize, offset: usize, data: const u8[]) -> gpu.Buffer?!:
for at in 0..<self.entries.count:
let entry = &self.entries.items[at]
if entry.key.id == parent and entry.key.tag == key.tag and entry.key.part == key.part and entry.bytes == total and entry.used != self.frame:
if data.length > 0: try entry.buffer.upload(offset, data)
self.uploads += 1
self.uploaded += (u64)data.length
entry.key = key
entry.used = self.frame
return entry.buffer
return none
pub func store(key: ArrayKey, data: const u8[], parent: u64 = 0) -> gpu.Buffer!:
self.uploads += 1
self.uploaded += (u64)data.length
if parent != 0:
for at in 0..<self.entries.count:
let entry = &self.entries.items[at]
if entry.key.id == parent and entry.key.tag == key.tag and entry.key.part == key.part and entry.bytes == data.length and entry.used != self.frame:
try entry.buffer.upload(0, data)
entry.key = key
entry.used = self.frame
return entry.buffer
let buffer = try gpu.Buffer.create(self.device, data)
self.entries.push(Entry(key, buffer, data.length, self.frame)) catch failure:
var created = buffer
created.destroy()
error(failure.code, failure.message)
return buffer
## Drop copies unused for `keep_frames` frames, or unused this frame when
## the copies exceed the budget.
pub func trim():
var total: usize = 0
for entry in self.entries.view(): total += entry.bytes
var kept: usize = 0
for at in 0..<self.entries.count:
var entry = self.entries.items[at]
let stale = entry.used + keep_frames < self.frame or (total > budget and entry.used != self.frame)
if stale:
total -= entry.bytes
entry.buffer.destroy()
continue
self.entries.items[kept] = entry
kept += 1
self.entries.count = kept
## Buffers held and their bytes.
pub func count() -> usize:
return self.entries.count
pub func clear():
for at in 0..<self.entries.count: self.entries.items[at].buffer.destroy()
self.entries.clear()
self.device.destroy()
pub func close():
self.clear()
self.entries.close()
self.scratch.close()