Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-develop

Source of release 0.1.0

0.1.0 / tests / parity / main.lucb

## The GPU renderer held to the CPU develop: each file on the command line (by
## default a Leica DNG, a Fuji X-Trans RAF and a Sony ARW from luce-raw's sample
## raws checked out beside this package; skipped when they are not) is developed
## both ways with several settings into 8-bit sRGB, and the pictures
## must agree: the mean difference under 1 level and fewer than 0.1% of values
## off by more than 6. The Standard and Linear views are computed the same way on
## both, so no value may be off by 3 (8 with film, which runs nine half-float
## passes first); ACES and Print go through a table on the GPU, so their limit
## is 40.
import c
import memory
import strings
from luce_std import files
from luce_gpu import gpu
from luce_std import math
from luce_std import paths
from luce_develop import scene
from luce_develop import process
from luce_develop import renderer
from luce_develop.settings import parse, compile, Settings
from luce_develop import film

let cases: str[28] = [
    "",
    "temperature=7000;tint=10;exposure=0.7;contrast=40;shadows=50;vibrance=60",
    "exposure=-1;highlights=-80;whites=30;blacks=-40;saturation=-50",
    "view=Linear;exposure=1.5",
    "view=ACES 2.0;exposure=1;contrast=20;vibrance=40",
    "view=Standard;orange_hue=40;orange_saturation=-60;blue_luminance=-50;green_saturation=80;grade_shadows_hue=200;grade_shadows_saturation=40;grade_highlights_hue=40;grade_highlights_saturation=30;grade_global_luminance=10",
    "bw=1;bw_tint_hue=40;bw_tint_amount=30;bw_red=120;bw_blue=-80;levels_black=0.05;levels_gamma=1.3;curve_rgb=0,0 0.25,0.2 0.75,0.85 1,1;curve_blue=0,0.05 1,0.95;curve_luma=0,0 0.5,0.55 1,1",
    "lens_vignetting=60;lens_midpoint=30;vignette_amount=-60;vignette_feather=70;vignette_roundness=40",
    "view=Standard;geometry_vertical=40;geometry_rotate=4;geometry_aspect=20;guides=0.1,0.1,0.12,0.9",
    "view=Standard;geometry_rotate=3;crop_left=0.1;crop_top=0.2;crop_right=0.8;crop_bottom=0.9;vignette_amount=-40",
    "view=Standard;film_stock=Kodak EXR 500T 5298;film_grain=0",
    "view=Standard;film_stock=Kodak EXR 500T 5298;develop_time=4.5;film_interimage=180;film_warmth=40;film_tint=-20;film_contrast=30;film_black=60;film_format=16 mm",
    "view=Standard;film_stock=Kodak Vision 500T 5279;film_halation=300;film_grain=0;exposure=2",
    "view=Print;exposure=0.5;saturation=20",
    "view=Print;film_stock=Kodak EXR 500T 5298;film_grain=0",
    "view=Standard;film_stock=Fujifilm Velvia 50;film_grain=0",
    "view=Standard;film_stock=Fujifilm Velvia 50;film_grain=80",
    "view=Standard;film_stock=Fujifilm Neopan 100 Acros II;film_grain=120;develop_time=10;develop_temperature=22",
    "view=Standard;film_stock=Kodak Vision 500T 5279;film_grain=0;develop_silver=100;exposure=-1",
    "view=Standard;film_stock=Fujifilm Neopan 100 Acros II;film_grain=0;develop_process=Rodinal 1+100 stand",
    "view=Standard;film_stock=Fujifilm Velvia 50;film_grain=0;develop_process=C-41",
    "view=Print;film_stock=Kodak EXR 500T 5298;film_grain=0;print_silver=2.4",
    "view=Standard;film_stock=Kodak EXR 200T 5293;film_grain=0;film_flash=15;film_filter=½ Coral",
    "recipe=telecine|lens;film_stock=None;lens_coating=Uncoated;lens_diffusion=Low Contrast 3",
    "recipe=telecine|lens;view=Standard;film_stock=Kodak Vision 500T 5279;film_grain=0;lens_coating=Uncoated;lens_diffusion=Low Contrast 2",
    "recipe=paper_print|filter;film_stock=Kodak Tri-X 400;film_grain=0;paper_grade=3.5;paper_enlarger=Condenser",
    "recipe=paper_print|white_balance;film_stock=Kodak Portra 400;film_grain=0;paper_stock=Kodak Endura Premier;paper_yellow=20;paper_exposure=0.5",
    "recipe=release_print|white_balance;film_stock=Kodak Vision 500T 5279;film_grain=60;print_generations=2;print_optical=1"]

## luce-raw's samples, as tests are run from this package's folder.
let samples: str[3] = ["../luce-raw/build/samples/leica_m240.dng", "../luce-raw/build/samples/fuji_xt2_xtrans_14c.raf",
                       "../luce-raw/build/samples/sony_a7m2_14c.arw"]

## Draw `drawer` into all of `texture` until what it shows is what it was
## asked for: the settings are prepared on the renderer's own thread, and each
## draw takes what it has finished (as a window's frames do).
func develop_into(drawer: renderer.Renderer*, texture: gpu.Texture, width: u32, height: u32) -> !:
    var draws = 0
    while true:
        let frame = try texture.frame()
        defer frame.release()
        let target = try frame.value().target()
        defer target.release()
        try drawer.draw(target, 0.0, 0.0, (f64)width, (f64)height)
        _ = try frame.value().present(gpu.Color(0.0, 0.0, 0.0, 1.0))
        draws += 1
        if not drawer.busy() or draws > 20:
            return
        drawer.settle()

## Develop the file at `path` both ways with every case; how many failed.
func check_file(device: gpu.Device, path: str) -> i64!:
    var failed: i64 = 0
    for wanted in cases:
        let settings = parse(wanted)
        var cpu_scene = try scene.open(path, 384)
        defer cpu_scene.close()
        var expected = try process.render(&cpu_scene, settings, cpu = true)
        defer expected.close()
        let width = (u32)expected.width()
        let height = (u32)expected.height()
        var drawer = try renderer.create(gpu.Format.rgba8)
        defer drawer.close()
        drawer.show(try scene.open(path, 384))
        drawer.set_settings(settings)
        var texture = try gpu.Texture.create(device, width, height, gpu.Format.rgba8)
        defer texture.destroy()
        try develop_into(&drawer, texture, width, height)
        let shown = try new u8[(usize)(width * height * 4)] in memory.heap
        defer free(shown) in memory.heap
        try texture.read(shown)
        var total = 0.0
        var largest = 0
        var outliers = 0
        let want = expected.pixels()
        for at in 0..<shown.length:
            if at % 4 == 3:
                continue
            let difference = (i64)shown[at] - (i64)want[at]
            let size = difference if difference >= 0 else -difference
            total += (f64)size
            if size > largest:
                largest = size
            if size > 6:
                outliers += 1
        let mean = total / (f64)(shown.length // 4 * 3)
        # Interpolating the view's table across a sharp corner (the Linear
        # view's clip, the gamut's edge) misses by a few levels, rarely.
        let share = (f64)outliers / (f64)(shown.length // 4 * 3)
        # The view the recipe renders with (a Project stage makes it the print's);
        # only ACES goes through a table, the print and paper are per pixel.
        let view = compile(&settings).get(.view)
        let tabled = view == 0.0
        let limit = 40 if tabled else 3
        let good = mean < 1.0 and share < 0.001 and largest < limit
        print(f"{"ok  " if good else "FAIL"}  {paths.base(path)}  [{wanted}]  mean {mean}  over 6: {outliers}  largest {largest}")
        if not good:
            failed += 1
    return failed

## The stochastic render (film_grains.lucb) on the GPU, drawn until it has
## refined to 64 samples a pixel or more, against the CPU's with as
## many samples: the same grains, the same samples. The GPU computes the
## effective log H itself (half floats, rounded a little differently), so a
## grain right at its threshold can develop on one and not the other, and a
## silver grain is nearly opaque: a mean within 1.5 levels, under 3 % over 6.
let stochastic_cases: str[3] = ["film_stock=Kodak Vision 500T 5279", "film_stock=Fujifilm Velvia 50;film_grain=150", "film_stock=Kodak Tri-X 400;develop_process=D-76"]

func check_stochastic(device: gpu.Device, path: str) -> i64!:
    var failed: i64 = 0
    for wanted in stochastic_cases:
        let settings = parse(wanted)
        var drawer = try renderer.create(gpu.Format.rgba8)
        defer drawer.close()
        drawer.show(try scene.open(path, 128))
        drawer.set_settings(settings)
        let width = (u32)drawer.width()
        let height = (u32)drawer.height()
        var texture = try gpu.Texture.create(device, width, height, gpu.Format.rgba8)
        defer texture.destroy()
        var draws = 0
        while drawer.refined_samples() < 64 and draws < 400:
            # The grains are fitted on the renderer's thread first.
            drawer.settle()
            let frame = try texture.frame()
            defer frame.release()
            let target = try frame.value().target()
            defer target.release()
            try drawer.draw(target, 0.0, 0.0, (f64)width, (f64)height)
            _ = try frame.value().present(gpu.Color(0.0, 0.0, 0.0, 1.0))
            draws += 1
        let refined = drawer.refined_samples()
        let shown = try new u8[(usize)(width * height * 4)] in memory.heap
        defer free(shown) in memory.heap
        try texture.read(shown)
        var cpu_scene = try scene.open(path, 128)
        defer cpu_scene.close()
        var expected = try process.render(&cpu_scene, settings, samples = (usize)refined, cpu = true)
        defer expected.close()
        failed += try against_export(path, settings, wanted, (usize)refined, expected.pixels())
        let want = expected.pixels()
        var total = 0.0
        var largest = 0
        var outliers = 0
        for at in 0..<shown.length:
            if at % 4 == 3:
                continue
            let difference = (i64)shown[at] - (i64)want[at]
            let size = difference if difference >= 0 else -difference
            total += (f64)size
            largest = size if size > largest else largest
            outliers += 1 if size > 6 else 0
        let values = (f64)(shown.length // 4 * 3)
        let mean = total / values
        let good = refined >= 64 and mean < 1.5 and (f64)outliers / values < 0.03
        print(f"{"ok  " if good else "FAIL"}  {paths.base(path)}  [stochastic {wanted}, {refined} samples]  mean {mean}  over 6: {outliers}  largest {largest}")
        if not good:
            failed += 1
    return failed

## The export's film (process.lucb on the GPU, film_gpu.lucb's
## `filmed_on_gpu`) against the CPU's `want`, as many samples.
func against_export(path: str, settings: Settings, wanted: str, refined: usize, want: const u8[]) -> i64!:
    var opened = try scene.open(path, 128)
    defer opened.close()
    var exported = try process.render(&opened, settings, samples = refined)
    defer exported.close()
    let shown = exported.pixels()
    var total = 0.0
    var outliers = 0
    for at in 0..<shown.length:
        if at % 4 == 3:
            continue
        let difference = (i64)shown[at] - (i64)want[at]
        let size = difference if difference >= 0 else -difference
        total += (f64)size
        outliers += 1 if size > 6 else 0
    let values = (f64)(shown.length // 4 * 3)
    let mean = total / values
    let good = mean < 1.5 and (f64)outliers / values < 0.03
    print(f"{"ok  " if good else "FAIL"}  {paths.base(path)}  [export on the GPU {wanted}, {refined} samples]  mean {mean}  over 6: {outliers}")
    return 0 if good else 1

## Whether a file is at `path`.
func present(path: str) -> bool!:
    let terminated = try strings.copy(path)
    defer strings.release(terminated)
    return files.exists((c.str)terminated)

pub func main(arguments: str[]) -> i32!:
    var device = gpu.Device.open() catch failure:
        print("skip: no GPU")
        return 0
    defer device.destroy()
    var failed: i64 = 0
    if arguments.length > 1:
        for path in arguments[1..]:
            failed += try check_file(device, path)
            failed += try check_stochastic(device, path)
        return 1 if failed > 0 else 0
    for sample in samples:
        if not try present(sample):
            print("skip: no sample raws in ../luce-raw/build/samples (luce-raw/tests/run.py fetches them)")
            return 0
    for sample in samples:
        failed += try check_file(device, sample)
        failed += try check_stochastic(device, sample)
    return 1 if failed > 0 else 0