dymokomi/luce-render
Source of release 0.1.1
0.1.1 / shaders / intersect_shadow.comp
#version 460
// Each queued shadow ray: unblocked, its light sample joins the path's radiance.
// The slot is the path's own, so no atomics are needed. In scenes with cut-out
// materials (constants K_INFO.w), a ray that is blocked walks on through the
// cut-outs it meets, keeping (1 - opacity) of the light at each: the mean of
// the stochastic pass-through shade_surface takes, so cut-outs cast the
// shadows they should. Anything else opaque, glass included, blocks: light
// through glass arrives on BSDF paths, and on light paths (light tracing).
#extension GL_GOOGLE_include_directive : require
#extension GL_EXT_buffer_reference : require
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
#extension GL_EXT_shader_atomic_float : require
layout(local_size_x = 64) in;
#include "common.glsl"
#include "spectrum.glsl"
#include "lights.glsl"
#include "trace.glsl"
layout(set = 0, binding = 11, std430) readonly buffer Shading { uvec4 shading[]; };
layout(set = 0, binding = 12, std430) readonly buffer Materials { vec4 materials[]; };
#define MATERIAL_STRIDE 16u
// What got through: added to the path's radiance, or for a light path's
// camera connection (already in the film's terms) splatted on its pixel.
void deliver(uint path, float kept) {
vec4 value = get_v(SV_SHADOW_RADIANCE, path) * kept;
if (!is_light_path(path)) {
set_v(SV_RADIANCE, path, get_v(SV_RADIANCE, path) + value);
return;
}
if (any(isnan(value.xyz)) || any(isinf(value.xyz))) return;
uint at = FILM_SPLAT * film_pixels() + get_u(SU_SPLAT, path);
atomicAdd(film[at].x, value.x);
atomicAdd(film[at].y, value.y);
atomicAdd(film[at].z, value.z);
}
// Whether a light the camera sees stands in a light path's connection: the
// camera's own ray, back along it, would stop there (lights.glsl's rule for
// rays that follow light).
bool camera_sees_light(vec3 o, vec3 d, float reach) {
uint count = light_count();
for (uint light = 0u; light < count; light++)
if (light_visible(light) && intersect_light(light, o, d, reach, true) < reach) return true;
return false;
}
void work() {
uint at = gl_GlobalInvocationID.x;
if (at >= queue_count(Q_SHADOW)) return;
uint path = queue_path(Q_SHADOW, at);
vec4 origin = get_v(SV_SHADOW_ORIGIN, path);
vec3 direction = get_v(SV_SHADOW_DIRECTION, path).xyz;
if (is_light_path(path) && camera_sees_light(origin.xyz, direction, origin.w)) return;
if (!trace_any(origin.xyz, direction, origin.w)) {
deliver(path, 1.0);
return;
}
if (!CUTOUTS || constants[K_INFO].w < 0.5) return;
vec3 from = origin.xyz;
float remaining = origin.w;
float kept = 1.0;
for (uint hop = 0u; hop < 16u; hop++) {
Hit hit = trace_closest(from, direction, remaining);
if (hit.triangle == NONE) {
deliver(path, kept);
return;
}
float opacity = clamp(materials[shading[hit.triangle].w * MATERIAL_STRIDE + 12u].x, 0.0, 1.0);
kept *= 1.0 - opacity;
if (kept < 1e-4) return;
// Step past the surface along the ray by the margin origins keep.
vec3 p = from + direction * hit.t;
float margin = 1e-4 * max(1.0, max(abs(p.x), max(abs(p.y), abs(p.z))));
from = p + direction * margin;
remaining -= hit.t + margin;
if (remaining <= 0.0) {
deliver(path, kept);
return;
}
}
}
void main() {
work();
if (last_group(queues[arguments_slot(Q_SHADOW)])) {
queues[Q_SHADOW] = 0u;
}
}