Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-geocore

Source of release 0.7.6

0.7.6 / src / code / bindings.lucb

# What the API's members mean, per Run Over mode, as a luce-kernel `Host`. Column names
# a program uses: an attribute's own name on the mode's domain, `P` for positions,
# `group:<name>` for a group, `detail:<name>` for a detail attribute, and `@area`,
# `@center`, `@normal`, `@point` for values geocore computes or keeps outside attributes;
# for voxels `value`, `grid:<name>`, `@ix`, `@iy`, `@iz` (voxels.lucb).

## The accessor kinds: method suffix and lanes.
let accessor_names: str[7] = ["f32", "f64", "i32", "i64", "vec2", "vec3", "vec4"]
let accessor_lanes: usize[7] = [1, 1, 1, 1, 2, 3, 4]

## The bindings of `mode`. With `layout`, the input's columns are declared and reads of
## missing ones give 0 (`orient` the identity); without it (checking with no geometry) every read is allowed.
func make_host(mode: RunOver, layout: const Layout*?) -> Host!:
    var host = Host()
    errdefer host.destroy()
    let owner = mode.owner()
    match mode:
        .points:
            try host.field("Point", "P", "P", 3)
            try host.field("Point", "N", "N", 3)
            try host.field("Point", "Cd", "Cd", 3)
            try host.field("Point", "uv", "uv", 2)
            try host.element("Point", "ptnum")
            try host.uniform("Point", "numpt", "numpt")
            try host.field("Point", "orient", "orient", 4)
            try host.field("Point", "scale", "scale", 3)
            try host.field("Point", "opacity", "opacity", 1)
        .primitives:
            try host.field("Prim", "Cd", "Cd", 3)
            try host.element("Prim", "primnum")
            try host.uniform("Prim", "numprim", "numprim")
            try host.computed("Prim", "area", "@area", 1)
            try host.computed("Prim", "center", "@center", 3)
            try host.computed("Prim", "normal", "@normal", 3)
        .vertices:
            try host.field("Vertex", "uv", "uv", 2)
            try host.field("Vertex", "N", "N", 3)
            try host.element("Vertex", "vtxnum")
            try host.uniform("Vertex", "numvtx", "numvtx")
            try host.field("Vertex", "ptnum", "@point", 1)
        .voxels:
            try host.field("Voxel", "P", "P", 3)
            try host.field("Voxel", "value", "value", 1)
            try host.field("Voxel", "ix", "@ix", 1)
            try host.field("Voxel", "iy", "@iy", 1)
            try host.field("Voxel", "iz", "@iz", 1)
            try host.element("Voxel", "voxnum")
            try host.uniform("Voxel", "res", "res")
            for (name, lanes) in [("f32", (usize)1), ("vec3", (usize)3)]:
                var room: u8[32]
                try host.getter("Voxel", name, "grid:", lanes)
                try host.setter("Voxel", try strings.format(room, f"set_{name}"), "grid:", lanes)
        _:
            _ = 0
    if mode == .points or mode == .primitives or mode == .vertices:
        for (at, name) in accessor_names.indexed():
            var room: u8[32]
            let setter = try strings.format(room, f"set_{name}")
            try host.getter(owner, name, "", accessor_lanes[at])
            try host.setter(owner, setter, "", accessor_lanes[at])
        try host.getter(owner, "in_group", "group:", 1)
        try host.setter(owner, "set_group", "group:", 1)
    try host.uniform("Kernel", "frame", "frame")
    try host.uniform("Kernel", "time", "time")
    try host.uniform("Kernel", "fps", "fps")
    for (at, name) in accessor_names.indexed():
        try host.parameter("Kernel", name, accessor_lanes[at])
        var reader_room: u8[32]
        let reader = try strings.format(reader_room, f"detail_{name}")
        var setter_room: u8[32]
        let setter = try strings.format(setter_room, f"set_detail_{name}")
        # Run Over Detail reads and writes the detail as its one element; the others read
        # it as a uniform, and Numbers writes it from its lanes (the last writer wins).
        if mode == .detail:
            try host.getter("Kernel", reader, "detail:", accessor_lanes[at])
        else:
            try host.reader("Kernel", reader, "detail:")
        try host.setter("Kernel", setter, "detail:", accessor_lanes[at])
    try bind_geometry(&host)
    try bind_edits(&host)
    try bind_ramps(&host)
    try bind_topology(&host)
    try bind_named(&host)
    try bind_measure(&host)
    try bind_winding(&host)
    try bind_clouds(&host)
    try bind_volumes(&host)
    try bind_log(&host)
    try bind_texts(&host)
    try bind_text_sets(&host)
    try bind_arrays(&host)
    try bind_array_attributes(&host)
    try bind_splats(&host)
    if let given = layout:
        host.missing_reads_zero()
        # A point without a rotation is unrotated: `p.orient` reads the identity.
        try host.missing_reads("orient", [0.0, 0.0, 0.0, 1.0])
        for column in given.columns.view():
            try host.column(column.name, column.kind, column.components)
    return host