Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-gpu

Source of release 0.5.1

0.5.1 / tests / programs / vulkan / batching.lucb

from luce_gpu import gpu
import memory
import thread
import depth_slope
import mesh_pixels

## Vulkan batching contracts: texture passes and uploads do not wait, results are
## still ordered for later passes and reads, images destroyed while work that
## uses them is in flight stay valid until it completes, and pooled memory pages
## are all released again.
pub func main(arguments: str[]) -> i32!:
    _ = arguments
    var device = gpu.Device.open() catch failure:
        if failure.code == gpu.unavailable:
            print("skip no Vulkan device")
            return 0
        error(failure.code, failure.message)
    defer device.destroy()
    try depth_slope.pixels(device)
    try mesh_pixels.pixels(device)
    try ordering(device)
    try batched_reads(device)
    try stress(device, 20000, 256)
    # 1536 live tiles are 768 MiB: several pages, filled, emptied and freed.
    try stress(device, 6000, 1536)
    print("ok Vulkan batching")
    return 0

func fill(texture: gpu.Texture*, color: gpu.Color) -> !:
    let frame = try texture.frame()
    defer frame.release()
    assert((try frame.value().present(color)) == gpu.PresentResult.submitted)

## A chain of passes without waits: each samples the previous texture, which was
## destroyed right after being drawn from; only the final read waits.
func ordering(device: gpu.Device) -> !:
    var first = try gpu.Texture.create(device, 4, 4, gpu.Format.rgba8_linear)
    var red: u8[64]
    for index in 0..<(usize)16:
        red[index * 4] = 200
        red[index * 4 + 3] = 255
    try first.upload(red)
    var previous = first
    for step in 0..<(usize)64:
        var next = try gpu.Texture.create(device, 4, 4, gpu.Format.rgba8_linear)
        let frame = try next.frame()
        let root = try frame.value().target()
        try gpu.copy_image(root.value(), previous, gpu.Rect(width = 4.0, height = 4.0))
        root.release()
        assert((try frame.value().present(gpu.Color(0.0, 0.0, 0.0, 0.0))) == gpu.PresentResult.submitted)
        frame.release()
        # The copy that samples `previous` may still be in flight.
        previous.destroy()
        previous = next
        _ = step
    var result: u8[64]
    try previous.read(result)
    for index in 0..<(usize)16:
        assert(result[index * 4] == 200 and result[index * 4 + 1] == 0 and result[index * 4 + 3] == 255)
    previous.destroy()

## `count` 256x256 half-float tiles, rendered in a rolling window of `window` live
## ones and destroyed while their passes may be in flight.
func stress(device: gpu.Device, count: usize, window: usize) -> !:
    let baseline = try device.memory_pages()
    let live = try new gpu.Texture[window] --- in memory.heap
    defer free(live) in memory.heap
    var peak: usize = 0
    for index in 0..<count:
        let at = index % window
        if index >= window:
            live[at].destroy()
        live[at] = try gpu.Texture.create(device, 256, 256, gpu.Format.rgba16_float)
        try fill(&live[at], gpu.Color((f64)(index % 7) / 7.0, 0.5, 0.25, 1.0))
        let pages = try device.memory_pages()
        if pages > peak:
            peak = pages
    # One read waits for everything submitted before it.
    var texel: u8[8]
    try live[(count - 1) % window].read(texel, gpu.Region(0, 0, 1, 1))
    for at in 0..<window:
        live[at].destroy()
    let after = try device.memory_pages()
    print(f"pages baseline {baseline} peak {peak} after {after}")
    # Tiles of 512 KiB share pages of at least 64 MiB, not one allocation each.
    assert(peak <= baseline + window * 512 // 65536 + 2)
    if window * 512 > 256 * 1024:
        assert(peak > baseline)
    assert(after == baseline)

## A read batch on Vulkan: two tiles side by side in one strip at a row pitch,
## read with one submission; then uploads through the ring, many more than it
## holds at once, each read back as uploaded.
func batched_reads(device: gpu.Device) -> !:
    var tiles: gpu.Texture[2] = [try gpu.Texture.create(device, 256, 256, gpu.Format.rgba16_float), try gpu.Texture.create(device, 256, 256, gpu.Format.rgba16_float)]
    defer tiles[0].destroy()
    defer tiles[1].destroy()
    try fill(&tiles[0], gpu.Color(1.0, 0.0, 0.0, 1.0))
    try fill(&tiles[1], gpu.Color(0.0, 1.0, 0.0, 1.0))
    var batch = try gpu.ReadBatch.begin(device)
    defer batch.finish()
    let strip = (try batch.reserve(512 * 256 * 8)) else trap("a strip fits")
    try batch.copy(tiles[0], gpu.Region(0, 0, 256, 256), strip, row_texels = 512)
    try batch.copy(tiles[1], gpu.Region(0, 0, 256, 256), strip + 256 * 8, row_texels = 512)
    try batch.submit()
    try batch.wait()
    let bytes = try batch.bytes(strip, 512 * 256 * 8)
    # Half-float 1.0 is 0x3c00: red on the left, green on the right, every row.
    for y in [(usize)0, 100, 255]:
        assert(bytes[(y * 512 + 5) * 8 + 1] == 0x3c and bytes[(y * 512 + 5) * 8 + 3] == 0)
        assert(bytes[(y * 512 + 300) * 8 + 1] == 0 and bytes[(y * 512 + 300) * 8 + 3] == 0x3c)
    var texels: u8[1024]
    var back: u8[1024]
    var target = try gpu.Texture.create(device, 16, 16, gpu.Format.rgba8_linear)
    defer target.destroy()
    for round in 0..<(usize)300:
        for index in 0..<(usize)1024:
            texels[index] = (u8)((index + round) % 256)
        try target.upload(texels)
        if round % 50 == 49:
            try target.read(back)
            for index in 0..<(usize)1024:
                assert(back[index] == (u8)((index + round) % 256))