dymokomi/luce-render
Source of release 0.1.4
0.1.4 / shaders / shade_surface.comp
#version 450
// Shade each queued hit. A light adds its radiance (MIS-weighted against the
// light sample the previous vertex took) and ends the path. A surface may let
// the ray through (cut-out opacity), adds its emission, takes one light sample
// (a shadow ray in the path's own slot) and one BSDF sample, then continues or
// ends by Russian roulette and the bounce limits. Indirect light is clamped.
// With light tracing, both techniques weigh against light paths too, and the
// path carries the light tracer's density of it (connect.glsl); the same
// dispatch shades the light paths' hits after the camera paths'.
//
// Materials are OpenPBR (shaders/openpbr.glsl).
#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;
// Materials here sample their textures (openpbr.glsl).
#define MATERIAL_TEXTURES 1
#include "common.glsl"
#include "sampler.glsl"
#include "spectrum.glsl"
#include "lights.glsl"
#include "microfacet.glsl"
#include "openpbr.glsl"
#include "lighttree.glsl"
#include "guide.glsl"
#include "connect.glsl"
#include "shader_program.glsl"
vec3 point_of(uint index) {
return vec3(positions[index * 3u], positions[index * 3u + 1u], positions[index * 3u + 2u]);
}
// Clip an indirect contribution to the clamp (Cycles' sample clamp): its
// largest lane at most `clamp`, keeping its color.
Spec clamped(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;
}
void add_radiance(uint path, Spec contribution) {
set_v(SV_RADIANCE, path, get_v(SV_RADIANCE, path) + contribution);
}
#include "light_paths.glsl"
void shade_camera(uint path) {
uint pixel = get_u(SU_PIXEL, path);
uint bounces = get_u(SU_BOUNCE, path);
uint total = bounce_total(bounces);
vec4 hit = get_v(SV_HIT, path);
vec3 origin = get_v(SV_ORIGIN, path).xyz;
vec4 ray = get_v(SV_DIRECTION, path);
Spec throughput = get_v(SV_THROUGHPUT, path);
vec4 lambda = get_v(SV_LAMBDA, path);
uint what = get_u(SU_HIT, path);
// Lights and emitters are weighed from the last scattering point, which
// passes through cut-outs leave in place.
vec4 vertex_row = get_v(SV_VERTEX, path);
vec3 vertex = vertex_row.xyz;
vec4 mis = get_v(SV_MIS, path);
if ((what & LIGHT_HIT) != 0u) {
uint light = what & ~LIGHT_HIT;
float weight = 1.0;
if (total == 0u) {
if (!light_visible(light)) return;
} else if (ray.w > 0.0) {
uint emitter = light_emitter(light);
float reach = distance(vertex, origin + hit.x * ray.xyz);
float chance = (1.0 - constants[K_TREE].z) * tree_pdf(emitter, vertex, previous_normal(path));
float traced = light_tracing() ? camera_hit_ratio(mis, emitter, light_cos(light, vertex, ray.xyz, reach), reach, ray.w) : 0.0;
weight = mis_weight(light_hit_pdf(light, vertex, ray.xyz, reach) * chance / ray.w, traced);
}
add_radiance(path, clamped(throughput * light_radiance(light, lambda) * weight, total));
return;
}
uint triangle = what;
vec3 p0 = point_of(indices[triangle * 3u]);
vec3 geometric = normalize(cross(point_of(indices[triangle * 3u + 1u]) - p0, point_of(indices[triangle * 3u + 2u]) - p0));
// Faces wind counter-clockwise seen from outside: a ray along the normal
// is leaving the object.
bool inside = dot(geometric, ray.xyz) > 0.0;
if (inside) geometric = -geometric;
uvec4 attributes = shading[triangle];
vec3 p = origin + hit.x * ray.xyz;
surface_uv = triangle_uv(triangle, hit.zw);
// The camera's density of this vertex (area), and what turns the light
// sampler's pdf here toward the last vertex into that vertex's density.
float reach = distance(vertex, p);
float density = (total == 0u ? mis.w : ray.w) * abs(dot(ray.xyz, geometric)) / (reach * reach);
float back_reach = mis.z / (reach * reach);
vec4 u_opacity = sample4(pixel, params.sample_index, GROUP_OPACITY(total));
// Cut-out opacity: pass straight through with probability 1 - opacity, at
// the same bounce. The hit distance decorrelates stacked layers.
float opacity = clamp(material_at(attributes.w, 12u).x, 0.0, 1.0);
if (CUTOUTS && opacity < 1.0 && fract(u_opacity.x + float(hash_u32(floatBitsToUint(hit.x))) * (1.0 / 4294967296.0)) >= opacity) {
set_v(SV_ORIGIN, path, vec4(offset_origin(p, -geometric), 0.0));
queue_push(Q_CLOSEST + 1u - params.current, path);
return;
}
// A dispersive material ends the secondary wavelengths (openpbr.glsl);
// from then on dispersive indices are the hero's.
if (disperses(attributes.w)) {
Spec radiance = get_v(SV_RADIANCE, path);
dispersive_hit(throughput, radiance, lambda);
set_v(SV_RADIANCE, path, radiance);
set_v(SV_LAMBDA, path, lambda);
}
hero_wavelength = lambda.y < 0.0 ? lambda.x : 0.0;
// Emission, MIS-weighted against the light sample the previous vertex took
// (emissive triangles are emitters of the light tree).
if (HAS_EMISSION && material_at(attributes.w, 7u).w > 0.0) {
float weight = 1.0;
uint emitter = triangle_emitter(triangle);
if (total > 0u && ray.w > 0.0 && emitter != NONE) {
float chance = (1.0 - constants[K_TREE].z) * tree_pdf(emitter, vertex, previous_normal(path));
float traced = light_tracing() ? camera_hit_ratio(mis, emitter, abs(dot(ray.xyz, geometric)), reach, ray.w) : 0.0;
weight = mis_weight(triangle_hit_pdf(triangle, ray.xyz, reach) * chance / ray.w, traced);
}
add_radiance(path, clamped(throughput * material_color(attributes.w, 6u, lambda) * weight, total));
}
// A surface reached past the bounce limit ends the path. The ray that got
// here was still traced, so its light hits (and their MIS weights) count.
if (total >= uint(constants[K_INFO].y)) return;
vec3 normal = normalize((1.0 - hit.z - hit.w) * octahedral_decode(attributes.x) + hit.z * octahedral_decode(attributes.y) + hit.w * octahedral_decode(attributes.z));
if (dot(normal, geometric) < 0.0) normal = -normal;
// The material's node graph, once for this hit, on the outward normal; it
// may bend the normal.
run_program(attributes.w, triangle, p, inside ? -normal : normal, -ray.xyz);
// A light path through here would have to scatter toward this path's last
// vertex, which a program reading the view cannot (light_paths.glsl): past
// the first vertex the light tracer's density is zero.
bool viewed = program_viewed;
if (driven(9u)) {
normal = normalize(driven3(11u));
if (dot(normal, geometric) < 0.0) normal = -normal;
}
Surface surface = surface_at(normal, geometric, attributes.w, inside);
vec3 wo = to_local(surface, -ray.xyz);
prepare_surface(surface, wo, lambda);
// One light sample, on either side when the surface transmits: a distant
// light (by power), or an emitter of the light tree (by importance at this
// point and normal).
vec3 from = offset_origin(p, geometric);
// Path guiding: in a trained cell, BSDF sampling becomes a one-sample mix
// of the guide (share `guided`) and the BSDF; every pdf is the mixture's.
// Not at transmissive or mirror-like vertices: their lobes are too sharp
// for a guide to help.
uint cell = NONE;
float guided = 0.0;
if (guiding() && total < GUIDE_RING) {
cell = guide_cell(p, geometric);
bool suited = !surface.inside && surface.p_transmission <= 0.0 && (surface.p_diffuse > 0.05 || min(surface.alpha.x, surface.alpha.y) > 0.1);
if (cell != NONE && suited && guide_count(cell) >= constants[K_GUIDE].w) guided = constants[K_GUIDE].z;
}
vec4 u_light = sample4(pixel, params.sample_index, GROUP_LIGHT(total));
float p_distant = constants[K_TREE].z;
LightSample s;
bool pushed_shadow = false;
Spec emitted = Spec(0.0);
float chance = 0.0;
bool sampled = false;
uint emitter = NONE;
uint afar = NONE;
if (u_light.x < p_distant) {
float share;
uint light = pick_distant(u_light.x / p_distant, share);
if (light != NONE && share > 0.0 && sample_light(light, p, u_light.yz, s)) {
afar = light;
chance = p_distant * share;
emitted = light_type(light) == LIGHT_DOME ? dome_radiance(light, s.direction, lambda) : light_radiance(light, lambda);
sampled = true;
}
} else {
float tree_chance;
if (tree_sample((u_light.x - p_distant) / max(1e-7, 1.0 - p_distant), from, normal, emitter, tree_chance)) {
chance = (1.0 - p_distant) * tree_chance;
uint code = emitter_code(emitter);
if ((code & EMITTER_TRIANGLE) != 0u) {
sampled = sample_triangle(code & ~EMITTER_TRIANGLE, p, u_light.yz, lambda, s, emitted);
} else if (sample_light(code, p, u_light.yz, s)) {
emitted = light_radiance(code, lambda);
sampled = true;
}
}
}
if (sampled && chance > 0.0) {
float side = dot(s.direction, geometric);
vec3 wi = to_local(surface, s.direction);
if ((side > 0.0) == (wi.z > 0.0) && (side > 0.0 || surface.p_transmission > 0.0)) {
float bsdf_pdf;
Spec f = evaluate_surface(surface, wi, wo, bsdf_pdf);
// MIS against light paths weighs BSDF pdfs alone: what a light
// path can compute for this vertex (connect.glsl).
if (guided > 0.0 && !light_tracing()) bsdf_pdf = guided * guide_pdf(cell, s.direction) + (1.0 - guided) * bsdf_pdf;
if (bsdf_pdf > 0.0) {
float light_pdf = s.pdf * chance;
// The light tracer's density of this path over the camera
// path's BSDF ray reaching the same light point.
float traced = 0.0;
if (light_tracing() && !s.delta && emitter != NONE && density > 0.0) {
float d2 = s.distance * s.distance;
float at_last = total == 0u ? 1.0 : (viewed ? 0.0 : light_sampler_pdf(surface, s.direction, -ray.xyz) * back_reach / mis.y);
float at_vertex = emission_direction_pdf(emitter, s.cos_light) * abs(side) / d2 / density;
float at_light = emitter_area_pdf(emitter) / (bsdf_pdf * s.cos_light / d2);
traced = mis.x * at_last * at_vertex * at_light;
}
if (light_tracing() && afar != NONE && emits_from_afar(afar) && density > 0.0) {
float at_last = total == 0u ? 1.0 : (viewed ? 0.0 : light_sampler_pdf(surface, s.direction, -ray.xyz) * back_reach / mis.y);
float at_vertex = afar_first_density(p, s.direction, abs(side)) / density;
float at_light = afar_emission_pdf(afar, s.direction) / bsdf_pdf;
traced = mis.x * at_last * at_vertex * at_light;
}
float weight = s.delta ? 1.0 : mis_weight(bsdf_pdf / light_pdf, traced * bsdf_pdf / light_pdf);
Spec contribution = clamped(throughput * f * abs(wi.z) * emitted * (weight / light_pdf), total);
// Stop short of the emitter by the margin origins leave surfaces
// (offset_origin), so a shadow ray to an emissive triangle does
// not find the triangle itself.
vec3 target = p + s.direction * s.distance;
float margin = 4e-4 * max(1.0, max(abs(target.x), max(abs(target.y), abs(target.z))));
float reach = s.distance >= INFINITY ? INFINITY : max(0.0, s.distance - margin);
set_v(SV_SHADOW_ORIGIN, path, vec4(offset_origin(p, side > 0.0 ? geometric : -geometric), reach));
set_v(SV_SHADOW_DIRECTION, path, vec4(s.direction, 0.0));
set_v(SV_SHADOW_RADIANCE, path, contribution);
queue_push(Q_SHADOW, path);
pushed_shadow = true;
}
}
}
// One BSDF sample, or a guide sample.
vec4 u = sample4(pixel, params.sample_index, GROUP_BSDF(total));
vec3 wi;
Spec weight;
float pdf;
uint lobe;
// The unguided throughput's ratio to the guided one: f cos / p_bsdf against
// f cos / p_mixture.
float unguided = 1.0;
float bsdf_only;
if (u_opacity.y < guided) {
wi = to_local(surface, guide_sample(cell, vec3(u_opacity.zw, u.w)));
float bsdf_pdf;
Spec f = evaluate_surface(surface, wi, wo, bsdf_pdf);
pdf = guided * guide_pdf(cell, to_world(surface, wi)) + (1.0 - guided) * bsdf_pdf;
bsdf_only = bsdf_pdf;
if (pdf <= 0.0 || spec_max(f) <= 0.0) return;
unguided = pdf / max(bsdf_pdf, 1e-6 * pdf);
weight = f * (abs(wi.z) / pdf);
lobe = wi.z < 0.0 ? LOBE_TRANSMISSION : (surface.p_diffuse > 0.5 ? LOBE_DIFFUSE : LOBE_GLOSSY);
} else {
if (!sample_surface(surface, wo, u.xyw, wi, weight, pdf, lobe)) return;
bsdf_only = pdf;
if (guided > 0.0) {
float mixed = guided * guide_pdf(cell, to_world(surface, wi)) + (1.0 - guided) * pdf;
unguided = mixed / pdf;
weight *= pdf / mixed;
pdf = mixed;
}
}
vec3 direction = to_world(surface, wi);
// A shading normal can send a ray to the wrong side of the true surface.
float side = dot(direction, geometric);
if ((side > 0.0) != (wi.z > 0.0)) return;
bounces = next_bounce(bounces, lobe);
if (bounces == NONE) return;
throughput *= weight;
float survival_scale = min(vertex_row.w * unguided, 1e6);
if (total + 1u >= 3u) {
float survive = min(0.95, spec_max(throughput) * survival_scale);
if (u.z >= survive) return;
throughput /= survive;
}
set_u(SU_BOUNCE, path, bounces);
set_u(SU_NORMAL, path, octahedral_encode(normal));
// Path guiding learns from this vertex what arrives along `direction`.
if (guiding() && total < GUIDE_RING) {
if (cell == NONE) cell = guide_cell(p, geometric);
set_u(SU_GUIDE_CELL(total), path, cell);
set_u(SU_GUIDE_DIRECTION(total), path, octahedral_encode(direction));
float found = spec_luminance(get_v(SV_RADIANCE, path), lambda) + spec_luminance(get_v(SV_SHADOW_RADIANCE, path), lambda) * (pushed_shadow ? 1.0 : 0.0);
set_v(SV_GUIDE(total), path, vec4(pdf, spec_luminance(throughput, lambda), found, abs(dot(direction, normal))));
if (total + 1u < GUIDE_RING) set_u(SU_GUIDE_CELL(total + 1u), path, NONE);
}
// From `from` on the reflecting side: the point the light tree weighed, so
// an emitter hit's MIS pdf matches the light sample's.
vec3 next = side > 0.0 ? from : offset_origin(p, -geometric);
set_v(SV_ORIGIN, path, vec4(next, 0.0));
set_v(SV_VERTEX, path, vec4(next, survival_scale));
// The ray's pdf for MIS where it ends; with light tracing, the BSDF's
// alone, and this vertex's factor of the light tracer's density, settled
// now that the light sampler's direction here is known.
if (light_tracing()) {
float at_last = total == 0u ? 1.0 : (viewed ? 0.0 : light_sampler_pdf(surface, direction, -ray.xyz) * back_reach / mis.y);
set_v(SV_MIS, path, vec4(mis.x * at_last, density, abs(side), 0.0));
pdf = bsdf_only;
}
set_v(SV_DIRECTION, path, vec4(direction, pdf));
set_v(SV_THROUGHPUT, path, throughput);
queue_push(Q_CLOSEST + 1u - params.current, path);
}
void main() {
// The camera paths' hits, then the light paths' (light_paths.glsl).
uint at = gl_GlobalInvocationID.x;
uint cameras = queue_count(Q_SURFACE);
if (at < cameras) shade_camera(queue_path(Q_SURFACE, at));
else if (LIGHT_TRACING && at - cameras < queue_count(Q_LIGHT)) shade_light(queue_path(Q_LIGHT, at - cameras));
if (last_group(queues[arguments_slot(Q_SURFACE)])) {
queues[Q_SURFACE] = 0u;
queues[Q_LIGHT] = 0u;
}
}