Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-render

Source of release 0.1.2

0.1.2 / shaders / shade_miss.comp

#version 450
// Rays that left the scene: the background, distant lights with a size and
// dome lights, MIS-weighted against their light samples. The path ends.
#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"

void work() {
    uint at = gl_GlobalInvocationID.x;
    if (at >= queue_count(Q_MISS)) return;
    uint path = queue_path(Q_MISS, at);
    // Light paths that leave the scene just end.
    if (is_light_path(path)) return;
    uint bounce = bounce_total(get_u(SU_BOUNCE, path));
    vec4 ray = get_v(SV_DIRECTION, path);
    Spec throughput = get_v(SV_THROUGHPUT, path);
    vec4 lambda = get_v(SV_LAMBDA, path);
    Spec radiance = spec_of(constants[K_BACKGROUND_FIT], constants[K_BACKGROUND_RGB].xyz, lambda);
    uint lights_count = light_count();
    for (uint light = 0u; light < lights_count; light++) {
        uint type = light_type(light);
        if (type < LIGHT_DISTANT) continue;
        if (type == LIGHT_DISTANT) {
            float cos_half = light_at(light, 2u).w;
            if (cos_half >= 1.0 - 1e-7 || dot(ray.xyz, -light_at(light, 3u).xyz) < cos_half) continue;
        }
        float weight = 1.0;
        if (bounce == 0u) {
            if (!light_visible(light)) continue;
        } else if (ray.w > 0.0) {
            // A direction's pdf: no origin needed. With light tracing, paths
            // from this light's disk weigh in too (lights.glsl).
            float traced = 0.0;
            vec4 mis = get_v(SV_MIS, path);
            if (light_tracing() && emits_from_afar(light) && mis.y > 0.0)
                traced = mis.x * (afar_first_density(get_v(SV_VERTEX, path).xyz, ray.xyz, mis.z) / mis.y) * (afar_emission_pdf(light, ray.xyz) / ray.w);
            weight = mis_weight(light_hit_pdf(light, vec3(0.0), ray.xyz, INFINITY) * light_select_pdf(light) / ray.w, traced);
        }
        radiance += (type == LIGHT_DOME ? dome_radiance(light, ray.xyz, lambda) : light_radiance(light, lambda)) * weight;
    }
    Spec contribution = throughput * radiance;
    // Indirect light is clamped like the surfaces' (shade_surface's `clamped`).
    float clamp_value = constants[K_INFO].z;
    float largest = spec_max(contribution);
    if (bounce > 0u && clamp_value > 0.0 && largest > clamp_value) contribution *= clamp_value / largest;
    set_v(SV_RADIANCE, path, get_v(SV_RADIANCE, path) + contribution);
}

void main() {
    work();
    if (last_group(queues[arguments_slot(Q_MISS)])) {
        // Shading is done: shadow rays and the next round's rays are ready.
        queues[Q_MISS] = 0u;
        arguments(Q_SHADOW);
        arguments(Q_CLOSEST + 1u - params.current);
    }
}