dymokomi/luce-render
Source of release 0.1.2
0.1.2 / 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. Gaussian splats on the way (splats.glsl) add their radiance to a
// camera path's and keep their transmittance of its throughput; a light path
// is only attenuated. A path the splats stop entirely (Russian roulette) ends
// as a miss with no throughput left. 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"
#include "splats.glsl"
// Indirect light is clamped like the surfaces' (shade_surface's `clamped`).
Spec clamped_indirect(Spec contribution, uint total) {
float clamp_value = constants[K_INFO].z;
if (total == 0u || clamp_value <= 0.0) return contribution;
float largest = spec_max(contribution);
return largest > clamp_value ? contribution * (clamp_value / largest) : contribution;
}
// The splats before the hit at `reach`: false when they stopped the path.
bool through_splats(uint path, vec3 origin, vec3 direction, float reach) {
uint bounces = get_u(SU_BOUNCE, path);
uint seed = splat_seed(path, bounces, 0u);
Spec throughput = get_v(SV_THROUGHPUT, path);
float kept;
if (is_light_path(path)) {
kept = splat_transmittance(origin, direction, 0.0, reach, seed);
} else {
Spec added;
splat_composite(origin, direction, reach, get_v(SV_LAMBDA, path), seed, added, kept);
set_v(SV_RADIANCE, path, get_v(SV_RADIANCE, path) + clamped_indirect(throughput * added, bounce_total(bounces)));
}
if (kept == 1.0) return true;
set_v(SV_THROUGHPUT, path, throughput * kept);
return kept > 0.0;
}
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 (splats_present() && !through_splats(path, origin, direction, what == NONE ? INFINITY : hit.t)) {
queue_push(Q_MISS, path);
return;
}
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;
}
}