Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-usd

Source of release 0.9.6

0.9.6 / src / usda / writer / arrays.lucb

## Numeric arrays, the bulk of geometry layers: elements formatted straight
## into the output, and arrays of more than `parallel_elements` elements
## formatted in chunks on luce-geocore's pool, each into its own buffer,
## then joined in order.
from luce_std import parallel

## Arrays longer than this format in parallel.
let parallel_elements: usize = 32768
## Elements per parallel chunk.
let chunk_elements: usize = 16384

## Whether elements of `kind` are numbers this module formats alone.
func numeric(kind: ValueKind) -> bool:
    match kind:
        .boolean, .uchar, .int, .uint, .int64, .uint64, .half, .float, .double, .time_code: return true
        .vec2d, .vec3d, .vec4d, .vec2f, .vec3f, .vec4f, .vec2h, .vec3h, .vec4h, .vec2i, .vec3i, .vec4i: return true
        .quatd, .quatf, .quath, .matrix2d, .matrix3d, .matrix4d: return true
        _: return false

## Append elements first..<last of a numeric array, ", " between them (and
## before the first when `first` > 0).
func format_elements(kind: ValueKind, bytes: const u8[], first: usize, last: usize, out: Growing[u8]*) -> !:
    let size = element_bytes(kind)
    for index in first..<last:
        if index > 0:
            try out.push(',')
            try out.push(' ')
        try format_numeric(kind, &bytes[index * size], out)

## One numeric element: a scalar, a tuple, a quaternion or a matrix.
func format_numeric(kind: ValueKind, at: const u8*, out: Growing[u8]*) -> !:
    var text: u8[32] = ---
    match kind:
        .float:
            let length = try numbers.format_float(memory.read[f32]((void*)at), text)
            try put_bytes(out, text[..<length])
            return
        .double, .time_code:
            let length = try numbers.format_double(memory.read[f64]((void*)at), text)
            try put_bytes(out, text[..<length])
            return
        .int:
            try put_integer(out, (i64)memory.read[i32]((void*)at))
            return
        .uint:
            try put_integer(out, (i64)memory.read[u32]((void*)at))
            return
        .int64:
            try put_integer(out, memory.read[i64]((void*)at))
            return
        .uint64:
            try put_unsigned(out, memory.read[u64]((void*)at))
            return
        .uchar:
            try put_integer(out, (i64)memory.read[u8]((void*)at))
            return
        .boolean:
            try out.push('1' if memory.read[u8]((void*)at) != 0 else '0')
            return
        .half:
            try numbers.write_half(out, memory.read[u16]((void*)at))
            return
        .matrix2d, .matrix3d, .matrix4d:
            let rows: usize = 2 if kind == ValueKind.matrix2d else (3 if kind == ValueKind.matrix3d else 4)
            try put_bytes(out, "( ".bytes)
            for row in 0..<rows:
                if row > 0: try put_bytes(out, ", ".bytes)
                try out.push('(')
                for column in 0..<rows:
                    if column > 0: try put_bytes(out, ", ".bytes)
                    let length = try numbers.format_double(memory.read[f64]((void*)(at + (row * rows + column) * 8)), text)
                    try put_bytes(out, text[..<length])
                try out.push(')')
            try put_bytes(out, " )".bytes)
            return
        _: _ = 0
    let count = components(kind)
    let scalar = scalar_of(kind)
    let size = element_bytes(scalar)
    let quaternion = kind == ValueKind.quatd or kind == ValueKind.quatf or kind == ValueKind.quath
    try out.push('(')
    for index in 0..<count:
        if index > 0:
            try out.push(',')
            try out.push(' ')
        # Quaternions print (real, i, j, k); memory holds i, j, k, real.
        let source = (index + 3) % 4 if quaternion else index
        try format_numeric(scalar, at + source * size, out)
    try out.push(')')

func put_bytes(out: Growing[u8]*, bytes: const u8[]) -> !:
    try out.reserve(out.count + bytes.length)
    memory.copy(out.items[out.count..], bytes, bytes.length)
    out.count += bytes.length

func put_integer(out: Growing[u8]*, value: i64) -> !:
    if value < 0:
        try out.push('-')
        try put_unsigned(out, (u64)(-%value))
        return
    try put_unsigned(out, (u64)value)

func put_unsigned(out: Growing[u8]*, value: u64) -> !:
    var digits: u8[20] = ---
    var count: usize = 0
    var rest = value
    while true:
        digits[count] = (u8)(48 + rest % 10)
        count += 1
        rest = rest // 10
        if rest == 0: break
    try out.reserve(out.count + count)
    while count > 0:
        count -= 1
        out.items[out.count] = digits[count]
        out.count += 1

## One parallel array format: its chunks' buffers and whether one failed.
struct ArrayJob:
    var kind: ValueKind
    var bytes: const u8[]
    var count: usize
    var outputs: Growing[u8][]
    var failed: @bool

func format_chunk(context: void*, index: usize, worker: usize):
    let raw = context
    let job = (ArrayJob*)raw
    let first = index * chunk_elements
    let last = first + chunk_elements if first + chunk_elements < job.count else job.count
    format_elements(job.kind, job.bytes, first, last, &job.outputs[index]) catch failure:
        job.failed.store(true)

## Append `[a, b, ...]` for a numeric array of `count` elements.
func format_array(kind: ValueKind, bytes: const u8[], count: usize, out: Growing[u8]*) -> !:
    try out.push('[')
    if count <= parallel_elements:
        try format_elements(kind, bytes, 0, count, out)
        try out.push(']')
        return
    let chunks = (count + chunk_elements - 1) // chunk_elements
    let outputs = try new Growing[u8][chunks] --- in memory.heap
    defer free(outputs) in memory.heap
    for index in 0..<chunks: outputs[index] = Growing[u8]()
    defer:
        for index in 0..<chunks: outputs[index].close()
    var job = ArrayJob(kind = kind, bytes = bytes, count = count, outputs = outputs, failed = false)
    try parallel.for_each(chunks, (void*)&job, format_chunk)
    if job.failed.load(): error(memory.exhausted, "out of memory formatting an array")
    var total = out.count + 1
    for index in 0..<chunks: total += outputs[index].count
    try out.reserve(total)
    for index in 0..<chunks: try put_bytes(out, outputs[index].view())
    try out.push(']')