Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-render

Source of release 0.1.0

0.1.0 / shaders / intersect_closest.comp

#version 460
// The nearest triangle or light along each queued ray: hits go to the surface
// queue (camera paths) or the light queue (light paths), misses to the miss
// queue. The last workgroup sizes the shading kernels.
#extension GL_GOOGLE_include_directive : require
#extension GL_EXT_buffer_reference : require
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
layout(local_size_x = 64) in;
#include "common.glsl"
#include "spectrum.glsl"
#include "lights.glsl"
#include "trace.glsl"

void work() {
    uint queue = Q_CLOSEST + params.current;
    uint at = gl_GlobalInvocationID.x;
    if (at >= queue_count(queue)) return;
    uint path = queue_path(queue, at);
    vec3 origin = get_v(SV_ORIGIN, path).xyz;
    vec3 direction = get_v(SV_DIRECTION, path).xyz;
    Hit hit = trace_closest(origin, direction, INFINITY);
    uint what = hit.triangle;
    uint lights_count = light_count();
    // Camera rays pass through lights that are not visible to the camera.
    bool camera_ray = bounce_total(get_u(SU_BOUNCE, path)) == 0u;
    for (uint light = 0u; light < lights_count; light++) {
        if (camera_ray && !light_visible(light)) continue;
        float t = intersect_light(light, origin, direction, hit.t, is_light_path(path));
        if (t < hit.t) {
            hit.t = t;
            what = LIGHT_HIT | light;
        }
    }
    if (what == NONE) {
        queue_push(Q_MISS, path);
        return;
    }
    set_v(SV_HIT, path, vec4(hit.t, 0.0, hit.uv));
    set_u(SU_HIT, path, what);
    queue_push(is_light_path(path) ? Q_LIGHT : Q_SURFACE, path);
}

void main() {
    work();
    if (last_group(queues[arguments_slot(Q_CLOSEST + params.current)])) {
        // Hits are shaded, misses taken by shade_miss; this round's rays are done.
        // shade_surface takes both kinds of hits.
        arguments_for(Q_SURFACE, atomicAdd(queues[Q_SURFACE], 0u) + atomicAdd(queues[Q_LIGHT], 0u));
        arguments(Q_MISS);
        queues[Q_CLOSEST + params.current] = 0u;
    }
}