Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-canvas

Source of release 0.5.1

0.5.1 / src / tiles / tests.lucb

test "tiles share, replace, write and read on a device":
    var device = gpu.Device.open() catch failure:
        assert(failure.code == gpu.unavailable)
        return
    defer device.destroy()
    var tiles = try Tiles.create(device, 300, 260)
    defer tiles.destroy()
    assert(tiles.columns() == 2 and tiles.rows() == 2 and tiles.region(1, 1).width == 44 and tiles.region(1, 1).height == 4)
    let count = (usize)300 * 260 * 4
    let pixels = try new f32[count] --- in memory.heap
    defer free(pixels) in memory.heap
    for index in 0..<count:
        pixels[index] = 0.0
    # One red pixel in the top-left tile, one half-transparent green in the bottom-right one.
    pixels[(10 * 300 + 20) * 4] = 1.0
    pixels[(10 * 300 + 20) * 4 + 3] = 1.0
    pixels[(258 * 300 + 270) * 4 + 1] = 0.25
    pixels[(258 * 300 + 270) * 4 + 3] = 0.5
    try tiles.write(pixels)
    assert(tiles.painted(0, 0) and not tiles.painted(1, 0) and not tiles.painted(0, 1) and tiles.painted(1, 1))
    var copy = try tiles.clone()
    defer copy.destroy()
    assert((tiles.tile(0, 0) else trap("missing tile")).references == 2)
    let fresh = try tiles.blank()
    try tiles.set(0, 0, fresh)
    assert((copy.tile(0, 0) else trap("missing tile")).references == 1)
    let back = try new f32[count] --- in memory.heap
    defer free(back) in memory.heap
    try copy.read(back)
    assert(back[(10 * 300 + 20) * 4] == 1.0 and back[(10 * 300 + 20) * 4 + 3] == 1.0)
    assert(back[(258 * 300 + 270) * 4 + 1] == 0.25 and back[(258 * 300 + 270) * 4 + 3] == 0.5)
    assert(back[0] == 0.0 and back[count - 1] == 0.0)
    try tiles.read(back)
    assert(back[(10 * 300 + 20) * 4] == 0.0 and back[(258 * 300 + 270) * 4 + 3] == 0.5)
    # A shared cell holds the same tile, and stamp, as its source.
    try copy.share(1, 0, 0, 0)
    assert(copy.stamp(1, 0) == copy.stamp(0, 0) and copy.painted(1, 0))
    var rejected = false
    tiles.set(5, 0, try tiles.blank()) catch failure:
        assert(failure.code == invalid)
        rejected = true
    assert(rejected)

test "content bounds read from the outside in, and still find inner pixels":
    var device = gpu.Device.open() catch failure:
        assert(failure.code == gpu.unavailable)
        return
    defer device.destroy()
    # 5 by 4 cells, the last column and row partial.
    var tiles = try Tiles.create(device, 1200, 1000)
    defer tiles.destroy()
    assert(try tiles.content_bounds() == none)
    let count = (usize)1200 * 1000 * 4
    let pixels = try new f32[count] --- in memory.heap
    defer free(pixels) in memory.heap
    for index in 0..<count:
        pixels[index] = 0.0
    # One pixel in a corner cell, one in a cell of the inner ring.
    pixels[(5 * 1200 + 5) * 4 + 3] = 1.0
    pixels[(600 * 1200 + 600) * 4 + 3] = 0.25
    try tiles.write(pixels)
    let two = try tiles.content_bounds() else trap("no bounds")
    assert(two.left == 5 and two.top == 5 and two.right == 601 and two.bottom == 601)
    # Everything painted: the outer ring alone gives the whole canvas.
    for index in 0..<count // 4:
        pixels[index * 4 + 3] = 1.0
    try tiles.write(pixels)
    let whole = try tiles.content_bounds() else trap("no bounds")
    assert(whole.left == 0 and whole.top == 0 and whole.right == 1200 and whole.bottom == 1000)
    # Only the middle: nothing on the outside to skip against.
    for index in 0..<count // 4:
        pixels[index * 4 + 3] = 0.0
    pixels[(520 * 1200 + 700) * 4 + 3] = 1.0
    try tiles.write(pixels)
    let middle = try tiles.content_bounds() else trap("no bounds")
    assert(middle.left == 700 and middle.top == 520 and middle.right == 701 and middle.bottom == 521)

test "a cell reads back straight and writes back from premultiplied samples; a clear one drops":
    var device = gpu.Device.open() catch failure:
        assert(failure.code == gpu.unavailable)
        return
    defer device.destroy()
    var tiles = try Tiles.create(device, 300, 260)
    defer tiles.destroy()
    let cell = try new f32[(usize)tile_size * (usize)tile_size * 4] --- in memory.heap
    defer free(cell) in memory.heap
    try tiles.read_cell(1, 1, cell)
    assert(cell[0] == 0.0 and cell[3] == 0.0)
    # Premultiplied half-transparent red at the last cell's first pixel.
    cell[0] = 0.5
    cell[3] = 0.5
    try tiles.write_cell_premultiplied(1, 1, cell)
    assert(tiles.painted(1, 1))
    try tiles.read_cell(1, 1, cell)
    assert(cell[0] == 1.0 and cell[3] == 0.5 and cell[7] == 0.0)
    for index in 0..<cell.length:
        cell[index] = 0.0
    try tiles.write_cell_premultiplied(1, 1, cell)
    assert(not tiles.painted(1, 1))

test "tiles_alive counts each tile once, shared or not, until its last holder lets go":
    var device = gpu.Device.open() catch failure:
        assert(failure.code == gpu.unavailable)
        return
    defer device.destroy()
    let start = tiles_alive()
    var tiles = try Tiles.create(device, 600, 300)
    try tiles.set(0, 0, try tiles.blank())
    try tiles.share(1, 0, 0, 0)
    try tiles.share(2, 1, 0, 0)
    assert(tiles_alive() == start + 1)
    var copy = try tiles.clone()
    try tiles.set(1, 1, try tiles.blank())
    assert(tiles_alive() == start + 2)
    tiles.destroy()
    assert(tiles_alive() == start + 1)
    copy.destroy()
    assert(tiles_alive() == start)

## What the read_cells test's visitor saw.
struct Seen:
    var order: Cell[128]
    var count: usize
    var empty: usize
    var wrong: usize

func record_cell(context: void*?, cell: Cell, bytes: const u8[]) -> !:
    let seen = (Seen*)(context else trap("lost the record"))
    seen.order[seen.count] = cell
    seen.count += 1
    if bytes.length == 0:
        seen.empty += 1
        return
    # Each painted cell's first texel has red = column / 16 and green = row / 16.
    let red = from_half((u16)bytes[0] | ((u16)bytes[1] << 8))
    let green = from_half((u16)bytes[2] | ((u16)bytes[3] << 8))
    if red != (f32)cell.column / 16.0 or green != (f32)cell.row / 16.0:
        seen.wrong += 1

test "read_cells reads more cells than a batch holds, in order, empty ones as nothing":
    var device = gpu.Device.open() catch failure:
        assert(failure.code == gpu.unavailable)
        return
    defer device.destroy()
    # 12 by 8 cells, the last row partial; every third cell left empty.
    var tiles = try Tiles.create(device, 3000, 2000)
    defer tiles.destroy()
    let count = (usize)3000 * 2000 * 4
    let pixels = try new f32[count] --- in memory.heap
    defer free(pixels) in memory.heap
    for index in 0..<count:
        pixels[index] = 0.0
    var cells: Cell[96] = ---
    var listed: usize = 0
    for row in 0..<(u32)8:
        for column in 0..<(u32)12:
            cells[listed] = Cell(column = column, row = row)
            listed += 1
            if (row * 12 + column) % 3 == 0:
                continue
            let at = ((usize)(row * 256) * 3000 + (usize)(column * 256)) * 4
            pixels[at] = (f32)column / 16.0
            pixels[at + 1] = (f32)row / 16.0
            pixels[at + 3] = 1.0
    try tiles.write(pixels)
    var seen: Seen = ---
    seen.count = 0
    seen.empty = 0
    seen.wrong = 0
    try tiles.read_many(cells, (void*)&seen, record_cell)
    assert(seen.count == 96 and seen.empty == 32 and seen.wrong == 0)
    for index in 0..<(usize)96:
        assert(seen.order[index].column == cells[index].column and seen.order[index].row == cells[index].row)
    # The whole read agrees with what was written.
    let back = try new f32[count] --- in memory.heap
    defer free(back) in memory.heap
    try tiles.read(back)
    for index in 0..<count:
        assert(back[index] == pixels[index])

test "writing band by band makes the same tiles as writing the whole picture":
    var device = gpu.Device.open() catch failure:
        assert(failure.code == gpu.unavailable)
        return
    defer device.destroy()
    let width: usize = 600
    let height: usize = 530
    let pixels = try new f32[width * height * 4] --- in memory.heap
    defer free(pixels) in memory.heap
    for at in 0..<width * height:
        let x = at % width
        let y = at // width
        pixels[at * 4] = (f32)x / (f32)width
        pixels[at * 4 + 1] = (f32)y / (f32)height
        pixels[at * 4 + 2] = 0.25
        # A clear cell at the top-left, to be dropped both ways.
        pixels[at * 4 + 3] = 0.0 if x < 256 and y < 256 else 0.75
    var whole = try Tiles.create(device, (u32)width, (u32)height)
    defer whole.destroy()
    try whole.write(pixels)
    var banded = try Tiles.create(device, (u32)width, (u32)height)
    defer banded.destroy()
    for row in 0..<banded.rows():
        let top = (usize)row * (usize)tile_size
        let rows = min_usize((usize)tile_size, height - top)
        try banded.write_band(row, pixels[top * width * 4..<(top + rows) * width * 4])
    let a = try new f32[width * height * 4] --- in memory.heap
    defer free(a) in memory.heap
    let b = try new f32[width * height * 4] --- in memory.heap
    defer free(b) in memory.heap
    try whole.read(a)
    try banded.read(b)
    for index in 0..<a.length:
        assert(a[index] == b[index])
    assert(not banded.painted(0, 0) and banded.painted(1, 0))

test "a band packed from encoded samples through tables matches one packed from floats":
    # No device: packing is CPU work. 8-bit and 16-bit samples over a partial last row and column.
    let width: usize = 600
    let height: usize = 530
    for deep in [false, true]:
        let codes: usize = 65536 if deep else 256
        let color = try new u16[codes] --- in memory.heap
        defer free(color) in memory.heap
        let alpha = try new u16[codes] --- in memory.heap
        defer free(alpha) in memory.heap
        for code in 0..<codes:
            color[code] = to_half((f32)code / (f32)(codes - 1) * (f32)code / (f32)(codes - 1))
            alpha[code] = to_half((f32)code / (f32)(codes - 1))
        let columns = (usize)tile_columns((u32)width)
        let coded = try new u8[packed_band_bytes((u32)width)] --- in memory.heap
        defer free(coded) in memory.heap
        let floated = try new u8[packed_band_bytes((u32)width)] --- in memory.heap
        defer free(floated) in memory.heap
        let coded_painted = try new bool[columns] --- in memory.heap
        defer free(coded_painted) in memory.heap
        let floated_painted = try new bool[columns] --- in memory.heap
        defer free(floated_painted) in memory.heap
        for row in 0..<tile_columns((u32)height):
            let rows = min_usize((usize)tile_size, height - (usize)row * (usize)tile_size)
            let samples = try new u8[width * rows * 4 * (2 if deep else 1)] --- in memory.heap
            defer free(samples) in memory.heap
            let pixels = try new f32[width * rows * 4] --- in memory.heap
            defer free(pixels) in memory.heap
            for at in 0..<width * rows * 4:
                let x = (at // 4) % width
                # Clear in the first column of cells.
                var code = (at * 37 + (usize)row * 11) % codes
                if at % 4 == 3 and x < 256:
                    code = 0
                if deep:
                    u16[]((u16*)samples.data, samples.length // 2)[at] = (u16)code
                else:
                    samples[at] = (u8)code
                pixels[at] = from_half(alpha[code] if at % 4 == 3 else color[code])
            try pack_band_coded((u32)width, (u32)height, row, samples, deep, color, alpha, coded, coded_painted)
            try pack_band((u32)width, (u32)height, row, pixels, floated, floated_painted)
            for index in 0..<coded.length:
                assert(coded[index] == floated[index])
            for column in 0..<columns:
                assert(coded_painted[column] == floated_painted[column])
            assert(not coded_painted[0] and coded_painted[1])