Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-gpu

Source of release 0.5.6

0.5.6 / src / gpu / mask.lucb

#==============================================================================================
#
#   mask - Backend-neutral coverage commands
#
#   DESCRIPTION:
#       A backend-neutral coverage command. Storage-buffer sampling keeps this small primitive
#       independent of texture formats and descriptor ownership in callers. Each frame owns at
#       most 16 MiB of packed coverage; regular triangles use none. The parameters a draw
#       carries are in params.lucb.
#
#==============================================================================================

func canvas_mask(canvas: Canvas*, pixels: const u8[], width: u32, height: u32,
                 rectangle: Rect, color: Color, clip: Clip, target_width: f64, target_height: f64) -> !:
    try check_color(color)
    if width == 0 or height == 0 or width > 16384 or height > 16384 or pixels.length != (usize)width * (usize)height:
        error(invalid_geometry, "coverage dimensions must match tightly packed pixels, in 1..16384")
    if rectangle.width == 0.0 or rectangle.height == 0.0 or clip.width == 0 or clip.height == 0:
        return
    let words = (pixels.length + 3) // 4
    if words > 4194304 - canvas.coverage_count:
        error(command_limit, "a frame supports at most 16 MiB of coverage images")
    let needed = canvas.coverage_count + words
    if needed > canvas.coverage.length:
        let capacity = canvas_max(needed, canvas_min(4194304, canvas_max(1024, canvas.coverage.length * 2)))
        let grown = try new u32[capacity] --- in memory.heap
        if canvas.coverage_count > 0:
            memory.copy(grown, canvas.coverage, canvas.coverage_count)
        if canvas.coverage.length > 0:
            free(canvas.coverage) in memory.heap
        canvas.coverage = grown
    try canvas.triangles(quad_vertices(rectangle, Color(color.red, color.green, color.blue, color.alpha), target_width, target_height), clip)
    # Vulkan and Metal read coverage as uint words, byte 0 lowest: on the
    # little-endian hosts both run on that is the bytes as they are, so they
    # are copied whole. Complete the append only after allocations.
    let packed = u8[]((u8*)&canvas.coverage[canvas.coverage_count], words * 4)
    memory.copy(packed, pixels, pixels.length)
    for tail in pixels.length..<words * 4:
        packed[tail] = 0
    canvas.draws[canvas.draw_count - 1].params = DrawParams((f32)rectangle.x, (f32)rectangle.y, (f32)rectangle.width, (f32)rectangle.height,
                                                            width, height, (u32)(canvas.coverage_count * 4), draw_coverage, 0.0, 0.0, 0.0, 0.0)
    canvas.coverage_count = needed