Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-render

Source of release 0.1.1

0.1.1 / src / render / textures.lucb

#==============================================================================================
#
#   render/textures - Materials' textures and node-graph programs
#
#   DESCRIPTION:
#       A material may carry a node graph (shader_graph.lucb). Its Image nodes name
#       files, each read once per use: colors as sRGB-encoded 8-bit (PNG, JPEG)
#       sampled as linear light, data (roughness, normals) as stored; OpenEXR files as
#       half floats either way. The kernels sample them through a texture table
#       (shaders/openpbr.glsl) at the point's texture coordinates; images are linear
#       Rec.709 once sampled, made ACEScg by the program. Every material's program
#       sits in one u32 buffer, each at its entry (-1: none).
#
#==============================================================================================

pub struct MaterialTextures:
    ## Each material's program entry (-1 none), the default material's first.
    pub var entries: Growing[f32]
    pub var programs: Growing[u32]
    pub var textures: Growing[gpu.Texture]
    pub var table: gpu.TextureTable
    var device: gpu.Device
    ## The file and use of each texture, to read each once.
    var keys: Growing[u8]

    pub func destroy():
        self.table.destroy()
        for texture in self.textures.view():
            var held = texture
            held.destroy()
        self.textures.close()
        self.entries.close()
        self.programs.close()
        self.keys.close()

## The programs of `materials`' node graphs and the textures they read, on
## `device`; the table always has at least one texture (kernels that sample
## need one bound).
pub func load_textures(device: gpu.Device, materials: const Materials*) -> MaterialTextures!:
    var assets = MaterialTextures(Growing[f32](), Growing[u32](), Growing[gpu.Texture](), gpu.TextureTable(), device, Growing[u8]())
    errdefer:
        for texture in assets.textures.view():
            var held = texture
            held.destroy()
        assets.textures.close()
        assets.entries.close()
        assets.programs.close()
        assets.keys.close()
    try assets.entries.push(-1.0)
    try assets.programs.push(0)
    for index in 0..<materials.count():
        let graph = materials.find_text(index, "shader_graph")
        var entry: i64 = -1
        if graph.length > 0: entry = try compile_graph(graph, ImageSlots(image_slot, (void*)&assets), &assets.programs)
        try assets.entries.push((f32)entry)
    if assets.textures.count == 0:
        var white = try gpu.Texture.create(device, 1, 1, gpu.Format.rgba8_linear)
        errdefer white.destroy()
        let texel: u8[4] = [255, 255, 255, 255]
        try white.upload(u8[]((u8*)&texel[0], 4))
        try assets.textures.push(white)
    assets.table = try gpu.TextureTable.create(device, (u32)assets.textures.count)
    for at in 0..<assets.textures.count: try assets.table.set((u32)at, assets.textures.items[at])
    return assets

func image_slot(context: void*, file: str, color: bool) -> f64!:
    let assets = (MaterialTextures*)context
    return (f64)(try texture_slot(assets.device, &assets.keys, &assets.textures, file, color))

## The slot of `file` read for its use (`color`: sRGB), read now if it is new.
func texture_slot(device: gpu.Device, keys: Growing[u8]*, textures: Growing[gpu.Texture]*, file: str, color: bool) -> usize!:
    # Keys are "c" or "d", the file, a newline: one per texture, in order.
    var at: usize = 0
    var slot: usize = 0
    while at < keys.count:
        var end = at
        while end < keys.count and keys.items[end] != 10: end += 1
        let key = (str)keys.items[at..<end]
        if key.length == file.length + 1 and key.bytes[0] == ('c' if color else 'd') and (str)key.bytes[1..] == file: return slot
        slot += 1
        at = end + 1
    let texture = try read_texture(device, file, color)
    try textures.push(texture)
    try keys.push('c' if color else 'd')
    try append_text(keys, file)
    try keys.push(10)
    return textures.count - 1

## Image file `file` as a texture: 8-bit RGBA from PNG or JPEG (sRGB for a
## color), half-float RGBA from OpenEXR.
func read_texture(device: gpu.Device, file: str, color: bool) -> gpu.Texture!:
    var name: str
    with memory.heap:
        name = try strings.copy(file)
    defer:
        with memory.heap: strings.release(name)
    let bytes = try files.read((c.str)name)
    defer bytes.release()
    let data = bytes.value
    if data.length >= 2 and data[0] == 'v' and data[1] == '/': return try read_exr_texture(device, data)
    var width: usize = 0
    var height: usize = 0
    let is_png = data.length >= 8 and data[0] == 137 and data[1] == 'P'
    let is_jpeg = data.length >= 2 and data[0] == 255 and data[1] == 216
    if is_png:
        let info = try png.info(data)
        width = info.width
        height = info.height
    elif is_jpeg:
        let info = try jpeg.info(data)
        width = info.width
        height = info.height
    else:
        error(invalid, "a texture is a PNG, JPEG or OpenEXR file")
    if width == 0 or height == 0 or width > 16384 or height > 16384: error(invalid, "a texture is 1 to 16384 texels on a side")
    let pixels = try new u8[width * height * 4] --- in memory.heap
    defer free(pixels) in memory.heap
    if is_png: try png.decode_rgba8(data, pixels)
    else: try jpeg.decode_rgba8(data, pixels)
    var texture = try gpu.Texture.create(device, (u32)width, (u32)height, gpu.Format.rgba8 if color else gpu.Format.rgba8_linear)
    errdefer texture.destroy()
    try texture.upload(pixels)
    return texture

func read_exr_texture(device: gpu.Device, data: const u8[]) -> gpu.Texture!:
    var raster = Raster(max_pixels = 67108864)
    defer raster.close()
    raster.encoded = try new u8[data.length] --- in memory.heap
    memory.copy(raster.encoded, data, data.length)
    try exr.probe(&raster)
    try raster.allocate()
    for chunk in 0..<raster.chunks.length: try exr.decode_chunk(&raster, chunk)
    var channel: i64[4] = [-1, -1, -1, -1]
    for at in 0..<raster.channels.length:
        let n = raster.channels[at].name
        if n == "R" or n == "Y": channel[0] = (i64)at
        if n == "G" or n == "Y": channel[1] = (i64)at
        if n == "B" or n == "Y": channel[2] = (i64)at
        if n == "A": channel[3] = (i64)at
    if channel[0] < 0 or channel[1] < 0 or channel[2] < 0: error(invalid, "a texture's OpenEXR needs R, G and B channels, or Y")
    let width = raster.width
    let height = raster.height
    if width > 16384 or height > 16384: error(invalid, "a texture is 1 to 16384 texels on a side")
    let stride = raster.channels.length
    let halves = try new u16[width * height * 4] --- in memory.heap
    defer free(halves) in memory.heap
    for texel in 0..<width * height:
        for k in 0..<(usize)4:
            let value = raster.pixels[texel * stride + (usize)channel[k]] if channel[k] >= 0 else 1.0
            halves[texel * 4 + k] = to_half(value)
    var texture = try gpu.Texture.create(device, (u32)width, (u32)height, gpu.Format.rgba16_float)
    errdefer texture.destroy()
    try texture.upload(u8[]((u8*)halves.data, halves.length * 2))
    return texture