Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-image

Source of release 0.45.0

0.45.0 / src / mesh_render.lucb

#==============================================================================================
#
#   mesh_render - A mesh-warped layer's cells on the GPU
#
#   DESCRIPTION:
#       Warp and Puppet Warp draw a layer through a `MeshMap` a cell at a time with mesh.frag:
#       at the level a view draws while the mesh is dragged (the canvas shows the layer
#       through a stand-in), and once at full size, for the cells the mesh came from or
#       lands on, when the warp is kept. A cell's field has a sample for each of its
#       texels, so the map is followed as finely as the cell shows it; the window it reads
#       is gathered at the cell's level, or coarser where the cell shrinks a large area.
#
#==============================================================================================

from luce_gpu import gpu
from luce_std import math
import memory
from luce_canvas import pool
from luce_raster.errors import limit
from luce_canvas.tiles import Tiles, tile_size, tile_texture
from luce_canvas.pyramid import Pyramid
from transform_window import Footprint, gather_tiles, level_for, Window
from liquify_grid import Reach
from mesh_map import MeshMap
from shaders import mesh_frag_words, mesh_frag_msl

## The widest window a full-size cell reads, in pixels a side; one reaching
## farther (a cell that gathers a huge area) reads a coarser level.
let full_window: f64 = 4096.0

## Draws mesh-warped cells: mesh.frag's pipeline.
pub struct Mesher:
    var owner: gpu.Device
    var shader: gpu.Shader
    var pipeline: gpu.Pipeline
    ## Whether samples mix as sRGB-encoded values: the document's blending, set
    ## for each warp (it cannot change while one is under way).
    pub var srgb: bool = false

    pub static func create(device: gpu.Device) -> Mesher!:
        var shader = try gpu.Shader.create(device, mesh_frag_words, mesh_frag_msl)
        errdefer shader.destroy()
        var pipeline = try gpu.Pipeline.create(device, shader, gpu.Blend.replace, gpu.Format.rgba16_float)
        return Mesher(owner = device, shader = shader, pipeline = pipeline)

    pub func destroy():
        self.pipeline.destroy()
        self.shader.destroy()

    ## Cell (`column`, `row`) of pyramid `level` of `tiles` (as the warp began;
    ## `pyramid` their levels) drawn through `mesh` for a preview: its field a
    ## sample every other texel, a quarter of the work, the rim a texel softer.
    ## None where nothing lands.
    pub func cell(mesh: const MeshMap*, tiles: const Tiles*, pyramid: Pyramid*, column: u32, row: u32, level: u32) -> gpu.Texture?!:
        var made = try prepare(mesh, column, row, level, 2)
        defer made.destroy()
        return try self.draw(&made, tiles, pyramid)

    ## Cells `first`..`last` of row `row` at full size, their fields made on
    ## `threads` threads at once and drawn one by one; `each` takes each cell's
    ## texture (none where nothing lands) with `context`.
    pub func row_of_cells(mesh: const MeshMap*, tiles: const Tiles*, pyramid: Pyramid*, row: u32, first: u32, last: u32, threads: usize,
                          context: void*, each: func(void*, u32, u32, gpu.Texture?) -> unit!) -> !:
        let count = (usize)(last - first + 1)
        let fields = try new CellField[count] --- in memory.heap
        for index in 0..<count:
            fields[index] = CellField(bytes = none, wide = none, reach = nowhere())
        defer:
            for index in 0..<fields.length:
                fields[index].destroy()
            free(fields) in memory.heap
        var batch = Batch(mesh = mesh, row = row, first = first, fields = fields)
        try prepare_all(&batch, threads)
        for index in 0..<count:
            let texture = try self.draw(&fields[index], tiles, pyramid)
            try each(context, first + (u32)index, row, texture)

    ## A made field drawn: the window it reads gathered, the field and its
    ## coverage uploaded and mesh.frag run over the cell.
    func draw(made: const CellField*, tiles: const Tiles*, pyramid: Pyramid*) -> gpu.Texture?!:
        let bytes = made.bytes else return none
        let wide = made.wide else return none
        let reach = made.reach
        let level = made.level
        let scale = (f64)(1u32 << level)
        let n = made.n
        # The window: every place a covered sample reads, two texels of its
        # level around it for the tap, within the layer.
        var at = level
        if level == 0:
            while at < 12 and math.max(reach.right - reach.left, reach.bottom - reach.top) / (f64)(1u32 << at) > full_window:
                at += 1
        else:
            let coarsest = level_for(source_box(reach, tiles, scale) else return none)
            at = coarsest if coarsest > level else level
        let box = source_box(reach, tiles, (f64)(1u32 << at)) else return none
        let clear = gpu.Color(0.0, 0.0, 0.0, 0.0)
        var window = try gather_tiles(self.owner, tiles, pyramid, box, at, false, clear)
        defer window.destroy()
        var field = try gpu.Texture.create(self.owner, (u32)n, (u32)n, gpu.Format.rgba16_float)
        defer field.destroy()
        try field.upload(bytes)
        var coverage = try gpu.Texture.create(self.owner, (u32)n, (u32)n, gpu.Format.rgba8_linear)
        defer coverage.destroy()
        try coverage.upload(wide)
        var params: f32[16] = [(f32)made.x0, (f32)made.y0, (f32)scale, 0.0, (f32)window.left, (f32)window.top, (f32)(try window.texture.width()), (f32)(try window.texture.height()),
                               (f32)(1u32 << at), (f32)made.spacing, (f32)n, 0.0, 1.0 if self.srgb else 0.0, 0.0, 0.0, 0.0]
        var fresh = try tile_texture(self.owner)
        errdefer fresh.destroy()
        let frame = try fresh.frame()
        defer frame.release()
        let area = try frame.value().target()
        defer area.release()
        try gpu.shade(area.value(), self.pipeline, gpu.Rect(width = (f64)tile_size, height = (f64)tile_size), u8[]((u8*)&params[0], 64),
                      [window.texture, field, coverage], gpu.Filter.nearest)
        _ = try frame.value().present(clear)
        return fresh

## A cell's field made on the CPU, for the GPU: each texel's displacement
## (liquify's split halves), its coverage as rgba8 (red), and where they
## read. No bytes where nothing lands or nothing moves.
struct CellField:
    var bytes: u8[]?
    var wide: u8[]?
    var reach: Reach
    var x0: f64 = 0.0
    var y0: f64 = 0.0
    var level: u32 = 0
    ## Document pixels between samples, and samples a side.
    var spacing: f64 = 1.0
    var n: i64 = 0

    func destroy():
        if let bytes = self.bytes:
            free(bytes) in memory.heap
        if let wide = self.wide:
            free(wide) in memory.heap
        self.bytes = none
        self.wide = none

func nowhere() -> Reach:
    return Reach(moved = false, left = 0.0, top = 0.0, right = 0.0, bottom = 0.0)

## Cell (`column`, `row`) of `level`'s field through `mesh`, a sample every
## `step` texels: CPU only, so several cells' are made at once.
func prepare(mesh: const MeshMap*, column: u32, row: u32, level: u32, step: i64) -> CellField!:
    var made = CellField(bytes = none, wide = none, reach = nowhere(), level = level)
    let scale = (f64)(1u32 << level)
    let side = (f64)tile_size * scale
    made.x0 = (f64)column * side
    made.y0 = (f64)row * side
    if not mesh.touches(made.x0 - scale, made.y0 - scale, made.x0 + side + scale, made.y0 + side + scale):
        return made
    # A sample each `step` texels of the cell, and one more along each side for the tap.
    let n = (i64)tile_size // step + 1
    made.spacing = scale * (f64)step
    made.n = n
    let count = (usize)(n * n)
    let bytes = try new u8[count * 8] --- in memory.heap
    errdefer free(bytes) in memory.heap
    let cover = try new u8[count] --- in memory.heap
    defer free(cover) in memory.heap
    made.reach = mesh.field(made.x0, made.y0, made.spacing, n, scale, bytes, cover)
    if not made.reach.moved:
        free(bytes) in memory.heap
        return made
    # As rgba8, coverage in red: rows of whole words, whatever the width.
    let wide = try new u8[count * 4] --- in memory.heap
    for index in 0..<count:
        wide[index * 4] = cover[index]
        wide[index * 4 + 1] = 0
        wide[index * 4 + 2] = 0
        wide[index * 4 + 3] = 255
    made.bytes = bytes
    made.wide = wide
    return made

## A row of cells whose fields are made on several threads.
struct Batch:
    var mesh: const MeshMap*
    var row: u32
    var first: u32
    var fields: CellField[]
    var failed: @bool

func prepare_one(raw: void*?, index: usize):
    let batch = (Batch*)(raw else return)
    if batch.failed.load(.acquire):
        return
    batch.fields[index] = prepare(batch.mesh, batch.first + (u32)index, batch.row, 0, 1) catch failure:
        batch.failed.store(true, .release)
        return

## Every field of `batch`, on the worker pool with up to `threads` threads (this one among them).
func prepare_all(batch: Batch*, threads: usize) -> !:
    pool.run(batch.fields.length, prepare_one, (void*)batch, limit = threads if threads < 16 else 16)
    if batch.failed.load(.acquire):
        error(limit, "out of memory making a warp's cells")

## Where a cell's samples read (`reach`), two texels of `scale` pixels around
## for the tap, within `tiles`' layer; none when that is past it.
func source_box(reach: Reach, tiles: const Tiles*, scale: f64) -> Footprint?:
    let box = Footprint(left = math.max(0.0, math.floor(reach.left) - 2.0 * scale), top = math.max(0.0, math.floor(reach.top) - 2.0 * scale),
                        right = math.min((f64)tiles.width(), math.ceil(reach.right) + 2.0 * scale), bottom = math.min((f64)tiles.height(), math.ceil(reach.bottom) + 2.0 * scale))
    if box.right <= box.left or box.bottom <= box.top:
        return none
    return box