Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-gpu

Source of release 0.5.7

0.5.7 / src / gpu / metal / transfer.lucb

#==============================================================================================
#
#   transfer - The Metal readback ring
#
#   DESCRIPTION:
#       Shared-storage buffers made once per device and kept: on unified memory their contents
#       are the host's view of the bytes, with no copy across a bus. A read batch holds one
#       slot, blits every region into it in one command buffer, and waits for that once.
#
#==============================================================================================

## One shared buffer of the ring, and the command buffer still writing it.
struct MetalRingSlot:
    var buffer: void*?
    var data: void*?
    var size: u64
    var busy: bool

let metal_read_slot_count: usize = 3
let metal_read_slot_bytes: u64 = 32u64 << 20

## A free readback slot, made on first use.
func metal_read_acquire(device: MetalDevice*) -> ReadSlot!:
    for index in 0..<metal_read_slot_count:
        let slot = &device.reads[index]
        if slot.busy:
            continue
        if slot.buffer == none:
            let pool = objc_autoreleasePoolPush()
            defer objc_autoreleasePoolPop(pool)
            # MTLResourceStorageModeShared: the CPU reads the buffer in place.
            let buffer = metal_new_buffer(metal_required(device.device), metal_sel("newBufferWithLength:options:"), metal_read_slot_bytes, 0) else error(failed, "could not allocate GPU readback memory")
            slot.buffer = buffer
            slot.data = metal_ptr(buffer, metal_sel("contents"))
            slot.size = metal_read_slot_bytes
            if slot.data == none:
                error(failed, "Metal readback storage is inaccessible")
        slot.busy = true
        let data = slot.data else trap("a Metal ring slot lost its contents")
        return ReadSlot(index = index, data = (u8*)data, capacity = (usize)slot.size)
    error(failed, "every Metal readback slot is held; finish a read batch first")

## Copy every region of a batch into slot `index` (render passes whose
## fragments write the slot) and commit; the ticket holds the command buffer
## until it is waited for.
func metal_read_submit(device: MetalDevice*, index: usize, entries: const ReadEntry[]) -> Ticket!:
    let pool = objc_autoreleasePoolPush()
    defer objc_autoreleasePoolPop(pool)
    let ring = &device.reads[index]
    # The copies join the open command buffer, committed now: what they read was
    # encoded before them.
    let command = try metal_open(device)
    for entry in entries:
        if let buffer = entry.buffer:
            try metal_copy_words(device, command, buffer.native, (u64)entry.source_offset, metal_required(ring.buffer), (u64)entry.offset, (u64)entry.bytes, 0)
            continue
        let source = entry.texture else trap("a read entry has no source")
        let texture = (MetalTexture*)source.native
        let texel = (u64)texel_bytes(texture.format)
        let row = (u64)(entry.row_texels if entry.row_texels > 0 else entry.region.width) * texel
        try metal_copy_read_texture(device, command, texture, entry.region, metal_required(ring.buffer), (u64)entry.offset, row)
    # Kept past the pool: the wait needs it.
    metal_void(command, metal_sel("retain"))
    metal_flush(device)
    return Ticket(command = command)

## Wait for a batch's command buffer and let it go, reporting execution failure.
func metal_read_wait(ticket: Ticket) -> !:
    let command = ticket.command else return
    let pool = objc_autoreleasePoolPush()
    defer objc_autoreleasePoolPop(pool)
    metal_void(command, metal_sel("waitUntilCompleted"))
    let status = metal_uint(command, metal_sel("status"))
    let code = metal_failure_code(command) if status != 4 else 0
    metal_void(command, metal_sel("release"))
    if status != 4:
        error(execution_failed, metal_failure_reason(code))

## Whether a batch's command buffer has finished, completed or failed
## (MTLCommandBufferStatus 4 or 5), without waiting.
func metal_read_ready(ticket: Ticket) -> bool:
    let command = ticket.command else return true
    return metal_uint(command, metal_sel("status")) >= 4

## Give slot `index` back; a batch still on the GPU is waited for first, so the
## next one never shares its bytes.
func metal_read_release(device: MetalDevice*, index: usize, ticket: Ticket, pending: bool):
    if pending:
        metal_read_wait(ticket) catch failure:
            _ = failure
    device.reads[index].busy = false

func metal_rings_destroy(device: MetalDevice*):
    for index in 0..<metal_read_slot_count:
        if let buffer = device.reads[index].buffer:
            metal_void(buffer, metal_sel("release"))
        device.reads[index] = MetalRingSlot()

## Write every staged copy of an upload batch from `staged`, copied into the
## upload ring, with render passes in the open command buffer.
func metal_upload_submit(device: MetalDevice*, entries: const UploadEntry[], staged: const u8[]) -> !:
    let pool = objc_autoreleasePoolPush()
    defer objc_autoreleasePoolPop(pool)
    let command = try metal_open(device)
    let staging = try metal_ring_stage(device, staged)
    for entry in entries:
        try metal_copy_upload_texture(device, command, (MetalTexture*)entry.texture.native, entry.region, staging.buffer, staging.offset + (u64)entry.offset)
    metal_encoded(device)