Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-develop

Source of release 0.1.0

0.1.0 / src / film_gpu.lucb

## Film on the negative, on the GPU: shaders/film.comp's stages
## (film_stages.glsl), in the order film_passes.lucb runs them on the CPU, as
## compute dispatches into single-float textures at the scene's size (the
## chemistry's fields are fine differences; half floats rounded them). The
## renderer runs them when the scene or the settings change and then draws the
## filmed scene in place of the camera's. Then, frame by frame, the stochastic
## render (`refine`): its traced pixels, run by run.
from luce_gpu import gpu
from film import Film
from luce_std import math
import film
import film_passes
from film_passes import Plan, Blur
from shaders import film_comp_words, film_comp_msl, film_comp_group, film_comp_bindings
import memory
from scene import Scene
from tables import Tables, table_width, table_rows
import tables as develop_tables

## The films' textures: single floats a kernel writes.
let field_format: gpu.Format = gpu.Format.rgba32_float

pub struct FilmPasses:
    var kernel: gpu.Kernel?
    ## The device, and the dispatches being recorded (submitted at the end of
    ## a run, of a call to `refine`, or before a read).
    var device: gpu.Device?
    var compute: gpu.Compute?
    var exposed: gpu.Texture?
    var one: gpu.Texture?
    var two: gpu.Texture?
    var three: gpu.Texture?
    ## The halation's blocks and its gathered halo, and their block size.
    var low: gpu.Texture?
    var gathered: gpu.Texture?
    var block: i32
    var width: u32
    var height: u32
    ## The lens's veiling glare (lens.lucb), added in the expose pass.
    var veil: f64
    ## How much of the development is still to come in what is shown (0: none,
    ## the developed film; 1: none of it yet), for watching it develop.
    pub var remaining: f64
    ## The stochastic render: the effective log H (`run` leaves it), the
    ## traced pixels' mean light, what it reads back to, and the samples its
    ## traced pixels have once all are traced (0 before). What the last run
    ## used, for the passes after it.
    var effective: gpu.Texture?
    var sums: gpu.Texture?
    var resolved: gpu.Texture?
    pub var samples: u32
    ## The last submission of the last batch: the next batch waits for it, so
    ## batches never pile up on the GPU's queue.
    var submitted: gpu.Submission?
    ## Where the stochastic render traces grains: runs of 16-pixel tiles with
    ## any pixel traced, one tile row tall (x, y, width, height in pixels,
    ## four a run), how many, and how many pixels they cover; the pass in
    ## progress: its next run, and its samples a pixel.
    var runs: f64[]
    var run_count: usize
    pub var traced_pixels: f64
    var run_at: usize
    var tiles: gpu.Texture?
    var last_plan: Plan
    var last_matrix: f64[9]
    var last_aspect: f64

    ## Film `scene` (the camera's values, `width` × `height`): `matrix` into AP1,
    ## `tables` and `aspect` for the lens vignetting, `atlas` the film's. The
    ## filmed scene, a texture these passes own until the next run.
    ## With `statistical` false the statistical film is not made (the
    ## stochastic render will replace it: an export's); what is returned then
    ## is not yet the film.
    pub func run(device: gpu.Device, scene: gpu.Texture, tables: gpu.Texture, atlas: gpu.Texture, source: const Film*,
                 matrix: f64[9], aspect: f64, width: u32, height: u32, veil: f64 = 0.0, statistical: bool = true) -> gpu.Texture!:
        self.veil = veil
        self.device = device
        if self.kernel == none:
            self.kernel = try gpu.Kernel.create(device, film_comp_words, film_comp_msl, film_comp_group, film_comp_bindings)
        if self.width != width or self.height != height or self.exposed == none:
            self.release_textures()
            self.exposed = try gpu.Texture.create(device, width, height, field_format, storage = true)
            self.one = try gpu.Texture.create(device, width, height, field_format, storage = true)
            self.two = try gpu.Texture.create(device, width, height, field_format, storage = true)
            self.three = try gpu.Texture.create(device, width, height, field_format, storage = true)
            self.effective = try gpu.Texture.create(device, width, height, field_format, storage = true)
            self.resolved = try gpu.Texture.create(device, width, height, field_format, storage = true)
            self.sums = try gpu.Texture.create(device, width, height, field_format, storage = true)
            self.width = width
            self.height = height
        let exposed = self.exposed else return scene
        let one = self.one else return scene
        let two = self.two else return scene
        let three = self.three else return scene
        let plan = film_passes.plan_for(source, (usize)width)
        if film_passes.has_halation(&plan) and (self.low == none or self.block != plan.halation_block):
            self.release_low()
            let low_width = (u32)film_passes.blocks((usize)width, plan.halation_block)
            let low_height = (u32)film_passes.blocks((usize)height, plan.halation_block)
            self.low = try gpu.Texture.create(device, low_width, low_height, field_format, storage = true)
            self.gathered = try gpu.Texture.create(device, low_width, low_height, field_format, storage = true)
            self.block = plan.halation_block
        let none_blur = Blur(sigma = [0.0f32, 0.0f32, 0.0f32], radius = 0, exponential = false, alpha = 0.0f32)
        try self.pass(exposed, 0, scene, scene, tables, atlas, 0, 0, none_blur, plan, matrix, aspect)
        try self.pass(one, 1, exposed, exposed, tables, atlas, 1, 0, plan.scatter, plan, matrix, aspect)
        try self.pass(two, 1, one, one, tables, atlas, 0, 1, plan.scatter, plan, matrix, aspect)
        if film_passes.has_halation(&plan):
            let low = self.low else return scene
            let gathered = self.gathered else return scene
            # Halation: blocks of the exposure, gathered at the kernel's samples,
            # added back onto the scatter (film_passes.lucb's stages 5–7).
            let blocked = Blur(sigma = [0.0f32, 0.0f32, 0.0f32], radius = plan.halation_block, exponential = false, alpha = 0.0f32)
            try self.pass(low, 5, exposed, exposed, tables, atlas, 0, 0, blocked, plan, matrix, aspect)
            try self.pass(gathered, 6, low, low, tables, atlas, 0, 0, blocked, plan, matrix, aspect)
            try self.pass(exposed, 7, gathered, two, tables, atlas, 0, 0, blocked, plan, matrix, aspect)
        else:
            # Unblurred, the scattered exposure is the exposure.
            try self.pass(exposed, 1, two, two, tables, atlas, 0, 0, none_blur, plan, matrix, aspect)
        # Development, step by step (film_passes.lucb's `develop_steps`): each
        # step's release added to `field` (into `scratch`), the field diffused
        # one step across into `spare` and down into `field` again.
        let field = one
        let spare = two
        let scratch = three
        let n = film.chemistry_steps
        let spread = film_passes.step_blur(&plan)
        for step in 1..=n:
            let shares: f32[4] = [(f32)(step - 1) / (f32)n, (f32)step / (f32)n, (f32)math.pow(plan.keep, (f64)(n - step)),
                                  1.0f32 if step == 1 else 0.0f32]
            try self.pass(scratch, 8, exposed, field, tables, atlas, 0, 0, none_blur, plan, matrix, aspect, shares)
            try self.pass(spare, 1, scratch, scratch, tables, atlas, 1, 0, spread, plan, matrix, aspect)
            try self.pass(field, 1, spare, spare, tables, atlas, 0, 1, spread, plan, matrix, aspect)
        let shown: f32[4] = [(f32)(1.0 - self.remaining), 0.0f32, 0.0f32, 0.0f32]
        # Where the film developed, for the stochastic render; its samples start over.
        if let effective = self.effective:
            try self.pass(effective, 10, exposed, field, tables, atlas, 0, 0, none_blur, plan, matrix, aspect, shown)
        self.samples = 0
        self.run_at = 0
        try self.find_runs(device, tables, atlas, plan, matrix, aspect)
        if let sums = self.sums:
            if let effective = self.effective:
                try self.pass(sums, 15, effective, effective, tables, atlas, 0, 0, none_blur, plan, matrix, aspect, shown)
        self.last_plan = plan
        self.last_matrix = matrix
        self.last_aspect = aspect
        if statistical:
            try self.pass(spare, 4, exposed, field, tables, atlas, 0, 0, none_blur, plan, matrix, aspect, shown)
            # The duplicates between negative and print soften the developed image.
            if plan.duplicated.radius > 0:
                try self.pass(field, 1, spare, spare, tables, atlas, 1, 0, plan.duplicated, plan, matrix, aspect)
                try self.pass(spare, 1, field, field, tables, atlas, 0, 1, plan.duplicated, plan, matrix, aspect)
        try self.submit()
        return spare

    ## Go on with the stochastic render of the film last `run` (its grains
    ## fitted in `atlas`: Film.fit_grains): trace about `budget` more pixels
    ## this call (the runs of tiles with traced pixels, in order), each with
    ## `samples` samples among the grains gathered for it, so a frame stays
    ## short; then, unless only `at_end` and there is more to trace, read the
    ## render back (and duplicate it as `run` does): the pixels drawn from
    ## their distribution are final at once, the traced ones as they are
    ## traced. A texture these passes own until the next run, or none.
    pub func refine(device: gpu.Device, tables: gpu.Texture, atlas: gpu.Texture, samples: u32, budget: f64, at_end: bool = false) -> gpu.Texture?!:
        # The last call's work done first: calls never pile up on the queue.
        if let before = self.submitted:
            try device.wait(before)
        let effective = self.effective else return none
        let sums = self.sums else return none
        let resolved = self.resolved else return none
        let one = self.one else return none
        let none_blur = Blur(sigma = [0.0f32, 0.0f32, 0.0f32], radius = 0, exponential = false, alpha = 0.0f32)
        let plan = self.last_plan
        let matrix = self.last_matrix
        let aspect = self.last_aspect
        let share = (f32)(1.0 - self.remaining)
        let trace: f32[4] = [share, 0.0f32, (f32)samples, 0.0f32]
        var done = 0.0
        while self.run_at < self.run_count and done < budget:
            let r = self.run_at * 4
            try self.pass(sums, 11, effective, effective, tables, atlas, 0, 0, none_blur, plan, matrix, aspect, trace,
                          self.runs[r + 1], self.runs[r + 3], self.runs[r], self.runs[r + 2])
            done += self.runs[r + 2] * self.runs[r + 3]
            self.run_at += 1
        if self.run_at >= self.run_count:
            self.samples = samples
        elif at_end:
            try self.submit()
            return none
        let read: f32[4] = [share, 0.0f32, 0.0f32, 0.0f32]
        try self.pass(resolved, 12, effective, sums, tables, atlas, 0, 0, none_blur, plan, matrix, aspect, read)
        if plan.duplicated.radius > 0:
            try self.pass(one, 1, resolved, resolved, tables, atlas, 1, 0, plan.duplicated, plan, matrix, aspect)
            try self.pass(resolved, 1, one, one, tables, atlas, 0, 1, plan.duplicated, plan, matrix, aspect)
        try self.submit()
        return resolved

    ## How much of the tracing is done (0..1).
    pub func traced_share() -> f64:
        return (f64)self.run_at / (f64)self.run_count if self.run_count > 0 else 1.0

    ## Which 16-pixel tiles trace grains (film_stages.glsl's stage 14, read back), as
    ## runs along each row of tiles.
    func find_runs(device: gpu.Device, tables: gpu.Texture, atlas: gpu.Texture, plan: Plan, matrix: f64[9], aspect: f64) -> !:
        let effective = self.effective else return
        let across = (self.width + 15) // 16
        let down = (self.height + 15) // 16
        if let held = self.tiles:
            var gone = held
            gone.destroy()
        self.tiles = try gpu.Texture.create(device, across, down, field_format, storage = true)
        let tiles = self.tiles else return
        let none_blur = Blur(sigma = [0.0f32, 0.0f32, 0.0f32], radius = 0, exponential = false, alpha = 0.0f32)
        let shown: f32[4] = [(f32)(1.0 - self.remaining), 0.0f32, 0.0f32, 0.0f32]
        try self.pass(tiles, 14, effective, effective, tables, atlas, 0, 0, none_blur, plan, matrix, aspect, shown)
        try self.submit()
        let flags = try new f32[(usize)(across * down) * 4] --- in memory.heap
        defer free(flags) in memory.heap
        try tiles.read(u8[]((u8*)flags.data, flags.length * 4))
        if self.runs.length > 0:
            free(self.runs) in memory.heap
        self.runs = try new f64[(usize)(across * down) * 4] --- in memory.heap
        self.run_count = 0
        self.traced_pixels = 0.0
        for ty in 0..<(usize)down:
            var tx: usize = 0
            while tx < (usize)across:
                if (f64)flags[(ty * (usize)across + tx) * 4] <= 0.0:
                    tx += 1
                    continue
                let first = tx
                while tx < (usize)across and (f64)flags[(ty * (usize)across + tx) * 4] > 0.0:
                    tx += 1
                let x = (f64)(first * 16)
                let y = (f64)(ty * 16)
                let w = math.min((f64)(tx * 16), (f64)self.width) - x
                let h = math.min(y + 16.0, (f64)self.height) - y
                let at = self.run_count * 4
                self.runs[at] = x
                self.runs[at + 1] = y
                self.runs[at + 2] = w
                self.runs[at + 3] = h
                self.run_count += 1
                self.traced_pixels += w * h

    ## One stage into `target` (rows `top` on, `rows` of them, from `left`,
    ## `across` wide; all of it when `rows` is 0), a dispatch of the pass being
    ## recorded. `row0` replaces the matrix's first row for development's
    ## stages (8: the step's shares; 4: the share shown).
    func pass(target: gpu.Texture, stage: i64, first: gpu.Texture, second: gpu.Texture, tables: gpu.Texture, atlas: gpu.Texture,
              dx: i64, dy: i64, blur: Blur, plan: Plan, matrix: f64[9], aspect: f64, row0: f32[4]? = none, top: f64 = 0.0, rows: f64 = 0.0,
              left: f64 = 0.0, across: f64 = 0.0) -> !:
        let kernel = self.kernel else return
        let device = self.device else return
        if self.compute == none:
            self.compute = try gpu.Compute.begin(device)
        let w = (f64)(try target.width())
        let h = (f64)(try target.height())
        let x0 = left if rows > 0.0 else 0.0
        let y0 = top if rows > 0.0 else 0.0
        let x1 = (left + (across if across > 0.0 else w)) if rows > 0.0 else w
        let y1 = (top + rows) if rows > 0.0 else h
        let weights = plan.halation_strength
        var uniforms: f32[32] = [
            (f32)x0, (f32)y0, (f32)x1, (f32)y1,
            (f32)stage, (f32)dx, (f32)dy, (f32)blur.radius,
            blur.sigma[0], blur.sigma[1], blur.sigma[2], 1.0f32 if blur.exponential else 0.0f32,
            weights[0], weights[1], weights[2], plan.pitch,
            (f32)matrix[0], (f32)matrix[1], (f32)matrix[2], 0.0f32,
            (f32)matrix[3], (f32)matrix[4], (f32)matrix[5], 0.0f32,
            (f32)matrix[6], (f32)matrix[7], (f32)matrix[8], 0.0f32,
            (f32)aspect, blur.alpha, (f32)self.veil, 0.0f32]
        if let row = row0:
            for k in 0..<(usize)4:
                uniforms[16 + k] = row[k]
        let bindings: gpu.Binding[6] = [gpu.Binding(), gpu.Binding(texture = first), gpu.Binding(texture = second), gpu.Binding(texture = tables),
                                        gpu.Binding(texture = atlas), gpu.Binding(texture = target)]
        let groups_x = (u32)math.ceil((x1 - x0) / 8.0)
        let groups_y = (u32)math.ceil((y1 - y0) / 8.0)
        let compute = self.compute else return
        try compute.dispatch(kernel, bindings, u8[]((u8*)&uniforms[0], 128), groups_x, groups_y)

    ## Send what has been recorded; the next call's dispatches wait for it.
    func submit() -> !:
        if let compute = self.compute:
            var sent = compute
            try sent.submit()
            self.submitted = try sent.submission()
            sent.close()
        self.compute = none

    func release_low():
        if let held = self.low:
            var gone = held
            gone.destroy()
        if let held = self.gathered:
            var gone = held
            gone.destroy()
        self.low = none
        self.gathered = none

    func release_textures():
        self.release_low()
        if self.runs.length > 0:
            free(self.runs) in memory.heap
        var no_runs: f64[]
        self.runs = no_runs
        self.run_count = 0
        for slot in [&self.effective, &self.sums, &self.resolved, &self.tiles]:
            if let held = *slot:
                var gone = held
                gone.destroy()
            *slot = none
        if let held = self.exposed:
            var gone = held
            gone.destroy()
        if let held = self.one:
            var gone = held
            gone.destroy()
        if let held = self.two:
            var gone = held
            gone.destroy()
        if let held = self.three:
            var gone = held
            gone.destroy()
        self.exposed = none
        self.one = none
        self.two = none
        self.three = none

    pub func release():
        if let held = self.compute:
            var gone = held
            gone.close()
        self.compute = none
        self.release_textures()
        if let held = self.kernel:
            var gone = held
            gone.destroy()
        self.kernel = none

## The most pixels an export's call to `refine` traces: its submissions stay short.
let export_work: u32 = 262144

## `scene` (camera values) through the film on the GPU at its own size, for an
## export: the passes as the renderer runs them (`matrix` into AP1, the lens
## from `built` and `aspect`, its `veil`), and with the grain on, the stochastic
## render refined to `samples` a pixel. The filmed scene, which the caller
## closes. Fails where no GPU opens (the caller films on the CPU then).
pub func filmed_on_gpu(source: Film*, scene: const Scene*, matrix: f64[9], built: const Tables*, aspect: f64, veil: f64, samples: u32) -> Scene!:
    var device = try gpu.Device.open()
    defer device.destroy()
    let width = (u32)scene.width()
    let height = (u32)scene.height()
    var scene_texture = try gpu.Texture.create(device, width, height, gpu.Format.rgba16_float)
    defer scene_texture.destroy()
    try scene_texture.upload(scene.bytes())
    let half_tables = try new f16[table_width * table_rows * 4] --- in memory.heap
    defer free(half_tables) in memory.heap
    develop_tables.half(built, half_tables)
    var tables_texture = try gpu.Texture.create(device, (u32)table_width, (u32)table_rows, gpu.Format.rgba16_float)
    defer tables_texture.destroy()
    try tables_texture.upload(u8[]((u8*)half_tables.data, half_tables.length * 2))
    let grained = samples > 0 and source.value(6)[3] > 0.0
    if grained:
        try source.fit_grains()
    let atlas = try new f16[film.atlas_width * film.atlas_rows * 4] --- in memory.heap
    defer free(atlas) in memory.heap
    for at in 0..<atlas.length:
        atlas[at] = (f16)source.texels[at]
    var atlas_texture = try gpu.Texture.create(device, (u32)film.atlas_width, (u32)film.atlas_rows, gpu.Format.rgba16_float)
    defer atlas_texture.destroy()
    try atlas_texture.upload(u8[]((u8*)atlas.data, atlas.length * 2))
    var passes = FilmPasses()
    defer passes.release()
    var out = try passes.run(device, scene_texture, tables_texture, atlas_texture, source, matrix, aspect, width, height, veil,
                             statistical = not grained)
    if grained:
        while true:
            if let resolved = try passes.refine(device, tables_texture, atlas_texture, samples, (f64)export_work, at_end = true):
                out = resolved
            if passes.samples >= samples:
                break
    let read = try new f32[(usize)width * (usize)height * 4] --- in memory.heap
    defer free(read) in memory.heap
    try out.read(u8[]((u8*)read.data, read.length * 4))
    let pixels = try new f16[read.length] --- in memory.heap
    errdefer free(pixels) in memory.heap
    for at in 0..<read.length:
        pixels[at] = (f16)read[at]
    return Scene.from_pixels((usize)width, (usize)height, pixels)