Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-render

Source of release 0.1.4

0.1.4 / 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]
    ## The textures the table binds (the store's; the white one its own).
    pub var textures: Growing[gpu.Texture]
    ## Which stored image each texture is (ImageStore's ids).
    var sources: Growing[u64]
    pub var table: gpu.TextureTable
    var device: gpu.Device
    var store: ImageStore*
    var white: gpu.Texture

    pub func destroy():
        self.table.destroy()
        self.white.destroy()
        self.textures.close()
        self.sources.close()
        self.entries.close()
        self.programs.close()

## The image files a renderer's scene reads (materials' textures and the
## dome's environment), kept while the scene uses them: a reload (a light or
## a material changed) reads and sends none of them again unless the file
## changed (its size or modification time). What a load no longer uses goes
## when that load is done (`settle`).
pub struct ImageStore:
    var images: Growing[StoredImage]
    ## The dome's environment, when `domed`.
    var dome: StoredDome
    var domed: bool
    ## The last id given to a stored image, and the files read so far.
    var serial: u64
    pub var reads: i64

    pub func init():
        self.images = Growing[StoredImage]()
        self.dome = StoredDome(Growing[u8](), Stamp(0, 0, 0), Environment(), false)
        self.domed = false
        self.serial = 0
        self.reads = 0

    ## Before a load: nothing is used yet.
    pub func begin():
        for at in 0..<self.images.count: self.images.items[at].used = false
        self.dome.used = false

    ## After a load: free what it did not use.
    pub func settle():
        var kept: usize = 0
        for at in 0..<self.images.count:
            var image = self.images.items[at]
            if not image.used:
                image.close()
                continue
            self.images.items[kept] = image
            kept += 1
        self.images.count = kept
        if self.domed and not self.dome.used: self.drop_dome()

    ## The texture of `file` read for its use (`color`: sRGB), kept or read
    ## now, and its id in the store.
    func texture(device: gpu.Device, file: str, color: bool) -> (gpu.Texture, u64)!:
        let stamp = try stamp_of(file)
        for at in 0..<self.images.count:
            let image = &self.images.items[at]
            if image.color != color or (str)image.path.view() != file: continue
            if image.stamp == stamp:
                image.used = true
                return (image.texture, image.id)
            # Changed on disk: read again.
            var stale = *image
            stale.close()
            self.images.items[at] = self.images.items[self.images.count - 1]
            self.images.count -= 1
            break
        self.serial += 1
        self.reads += 1
        var image = StoredImage(Growing[u8](), color, stamp, try read_texture(device, file, color), true, self.serial)
        errdefer image.close()
        try append_text(&image.path, file)
        try self.images.push(image)
        return (image.texture, image.id)

    ## The environment of dome image `file`: kept, or read now.
    pub func environment(file: str) -> const Environment*!:
        let stamp = try stamp_of(file)
        if self.domed and (str)self.dome.path.view() == file and self.dome.stamp == stamp:
            self.dome.used = true
            return &self.dome.environment
        self.drop_dome()
        self.reads += 1
        let environment = try load_environment(file)
        self.dome = StoredDome(Growing[u8](), stamp, environment, true)
        self.domed = true
        try append_text(&self.dome.path, file)
        return &self.dome.environment

    func drop_dome():
        if self.domed: self.dome.close()
        self.dome = StoredDome(Growing[u8](), Stamp(0, 0, 0), Environment(), false)
        self.domed = false

    pub func close():
        for at in 0..<self.images.count: self.images.items[at].close()
        self.images.close()
        self.drop_dome()

## What tells a file's contents apart without reading them: its size and
## modification time.
struct Stamp:
    var size: i64
    var seconds: i64
    var nanoseconds: u32

func stamp_of(file: str) -> Stamp!:
    var name: str
    with memory.heap:
        name = try strings.copy(file)
    defer:
        with memory.heap: strings.release(name)
    let found = try files.metadata((c.str)name)
    return Stamp(found.size, found.modified.seconds, found.modified.nanoseconds)

struct StoredImage:
    var path: Growing[u8]
    var color: bool
    var stamp: Stamp
    var texture: gpu.Texture
    var used: bool
    var id: u64

    func close():
        self.path.close()
        self.texture.destroy()

struct StoredDome:
    var path: Growing[u8]
    var stamp: Stamp
    var environment: Environment
    var used: bool

    func close():
        self.path.close()
        self.environment.close()

## The programs of `materials`' node graphs and the textures they read
## (from `store`), 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*, store: ImageStore*, used: const bool[]) -> MaterialTextures!:
    var assets = MaterialTextures(Growing[f32](), Growing[u32](), Growing[gpu.Texture](), Growing[u64](), gpu.TextureTable(), device, store, gpu.Texture())
    errdefer:
        assets.white.destroy()
        assets.textures.close()
        assets.sources.close()
        assets.entries.close()
        assets.programs.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
        # A material no triangle uses compiles nothing and reads no image
        # (`used` by material index: the library's are 1 on).
        let wanted = used.length == 0 or (index + 1 < used.length and used[index + 1])
        if graph.length > 0 and wanted: entry = try compile_graph(graph, ImageSlots(image_slot, (void*)&assets), &assets.programs)
        try assets.entries.push((f32)entry)
    if assets.textures.count == 0:
        assets.white = try gpu.Texture.create(device, 1, 1, gpu.Format.rgba8_linear)
        let texel: u8[4] = [255, 255, 255, 255]
        try assets.white.upload(u8[]((u8*)&texel[0], 4))
        try assets.textures.push(assets.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

## The table slot of `file` read for its use, the texture from the store.
func image_slot(context: void*, file: str, color: bool) -> f64!:
    let assets = (MaterialTextures*)context
    let (texture, id) = try assets.store.texture(assets.device, file, color)
    for at in 0..<assets.sources.count:
        if assets.sources.items[at] == id: return (f64)at
    try assets.textures.push(texture)
    try assets.sources.push(id)
    return (f64)(assets.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