Path Tracing
Loading...
Searching...
No Matches
path_trace_core.glsl
Go to the documentation of this file.
1
8
9//----------------------------------------------------------
10//Camera Functions
11
14void camera_axes(out vec3 cam_x, out vec3 cam_y, out vec3 cam_z) {
15 cam_z = normalize(camera_position - camera_lookat);
16 cam_x = normalize(cross(camera_up, cam_z));
17 cam_y = cross(cam_z, cam_x);
18}
19
27vec3 cameraRay(vec2 uv, vec3 cam_x, vec3 cam_y, vec3 cam_z, float fov_scale) {
28 float aspect = resolution.x / resolution.y;
29 vec2 p = (2.0 * uv - 1.0) * vec2(aspect, 1.0);
30
31 //Radial lens distortion, applied in aspect-corrected space
32 if(CAM_DISTORTION_K1 != 0.0 || CAM_DISTORTION_K2 != 0.0) {
33 float r2 = dot(p, p);
34 p *= 1.0 + CAM_DISTORTION_K1 * r2 + CAM_DISTORTION_K2 * r2 * r2;
35 }
36
37 float half_fov = 0.5 * CAM_FOV * fov_scale;
38
40 float f_len = 1.0 / tan(half_fov);
41 vec3 ray_cam = vec3(p.x, p.y, -f_len);
42 return normalize(cam_x * ray_cam.x + cam_y * ray_cam.y + cam_z * ray_cam.z);
43 }
44
45 //Fisheye projection map image-plane
46 float r = length(p);
47 if(r < EPS) return -cam_z;
48
49 float theta;
51 theta = r * half_fov;
52 }
54 theta = 2.0 * atan(r * tan(half_fov * 0.5));
55 }
56 else { //PROJ_FISHEYE_EQUISOLID
57 theta = 2.0 * asin(clamp(r * sin(half_fov * 0.5), -1.0, 1.0));
58 }
59
60 vec2 dir2d = p / r;
61 vec3 local_dir = vec3(dir2d * sin(theta), -cos(theta));
62
63 return normalize(cam_x * local_dir.x + cam_y * local_dir.y + cam_z * local_dir.z);
64}
65
70float polygonAperture(float angle, int blades) {
71 if(blades < 3) return 1.0;
72 float corner = TWO_PI / float(blades);
73 float a = mod(angle, corner) - corner * 0.5;
74 return cos(corner * 0.5) / cos(a);
75}
76
85vec3 sampleAperture(vec3 cam_x, vec3 cam_y, vec2 uv, float angle, float radius_rand) {
87 float radius = sqrt(radius_rand) * CAM_APERTURE * shape;
88
89 vec2 disk = vec2(cos(angle), sin(angle)) * radius;
90 disk.x *= CAM_ANAMORPHIC_SQUEEZE;
91
92 //Cat's-eye: biases the sample towards the center, proportional to how far off-axis the pixel is.
93 //This method is an approximation, not a real occlusion test, but it's cheap and doesn't add noise
94 if(CAM_CATEYE_STRENGTH > 0.0) {
95 vec2 frame_offset = (uv - 0.5) * 2.0;
96 disk -= frame_offset * length(frame_offset) * CAM_CATEYE_STRENGTH * CAM_APERTURE;
97 }
98
99 return cam_x * disk.x + cam_y * disk.y;
100}
101
106vec3 focalPlanePoint(vec3 cam_x, vec3 cam_y, vec3 cam_z, vec3 ray_dir, float focal_distance) {
107 if(CAM_TILT == 0.0) {
108 return camera_position + ray_dir * focal_distance;
109 }
110
111 vec3 plane_point = camera_position + cam_z * focal_distance;
112 vec3 plane_normal = cam_z * cos(CAM_TILT) + cam_y * sin(CAM_TILT);
113
114 float denom = dot(ray_dir, plane_normal);
115 if(abs(denom) < EPS)
116 return camera_position + ray_dir * focal_distance;
117
118 float t = dot(plane_point - camera_position, plane_normal) / denom;
119 return camera_position + ray_dir * t;
120}
121
126Ray cameraRayDOFChannel(vec2 uv, int spp_index, uvec2 px, float disp_coeff) {
127 vec3 cam_x, cam_y, cam_z;
128 camera_axes(cam_x, cam_y, cam_z);
129
130 float fov_scale = 1.0 + CAM_LATERAL_CA * disp_coeff;
131 float focal_distance = CAM_FOCAL_DISTANCE * (1.0 + CAM_AXIAL_CA * disp_coeff);
132
133 vec3 rj = rand3(-2, spp_index, px);
134 vec2 jitter = (rj.xz - 0.5) / resolution;
135
136 //Focal point definition
137 vec3 base_dir = cameraRay(uv + jitter, cam_x, cam_y, cam_z, fov_scale);
138 vec3 focal_point = focalPlanePoint(cam_x, cam_y, cam_z, base_dir, focal_distance);
139
140 //Lens disk
141 float angle = rj.x * TWO_PI;
142 vec3 rDOF = rand3(-3, spp_index, px); //needs different random seed
143 vec3 lens_offset = sampleAperture(cam_x, cam_y, uv, angle, rDOF.x);
144
145 vec3 origin = camera_position + lens_offset;
146 return Ray(origin, normalize(focal_point - origin));
147}
148
157Ray cameraRayDOF(vec2 uv, int spp_index, uvec2 px) {
158 return cameraRayDOFChannel(uv, spp_index, px, 0.0);
159}
160
161//----------------------------------------------------------
162//Environment Mapping
163
164vec3 sampleEnvMap(vec3 dir) {
165 float phi = atan(dir.y, dir.x);
166 float theta = acos(clamp(dir.z, -1.0, 1.0));
167 vec2 uv = vec2(phi / TWO_PI + 0.5, theta / PI);
168 return texture(env_map, uv).rgb;
169}
170
171//----------------------------------------------------------
172//Ray origins
173
178float rayEps() {
180}
181
189 return h.pos + h.lift + h.geom_normal * rayEps();
190}
191
192//----------------------------------------------------------
193//Next Event Estimation
194
195vec3 sampleAnalyticLight(Hit h, int bounce, int spp_index, uvec2 px) {
196 if (analytic_light_count == 0) return vec3(0);
197
198 //pick random light from buffer
199 vec3 rnd = rand3(bounce + 200, spp_index, px);
200 int light_idx = int(rnd.x * float(analytic_light_count));
201 GPULight light = analytic_lights[light_idx];
202
203 //calculates the distance and direction
204 vec3 to_light;
205 float dist;
206 float attenuation = 1.0;
207 float pdf_dir = 1.0; //Directional-density PDF. 1.0 = delta (point/spot or directional with radius=0)
208
209 switch (light.type) {
210 case LIGHT_POINT: {
211 vec3 offset = vec3(0);
212 if(light.radius > 0.0) {
213 float r = light.radius * pow(rnd.y, 1.0 / 3.0);
214 float phi = TWO_PI * rnd.z;
215 float cos_t = 2.0 * rnd.x - 1.0;
216 float sin_t = sqrt(1.0 - cos_t * cos_t);
217 offset = r * vec3(sin_t * cos(phi), sin_t * sin(phi), cos_t);
218 }
219 to_light = light.position.xyz + offset - h.pos;
220 dist = length(to_light);
221 attenuation = 1.0 / (dist * dist);
222 break;
223 }
224 case LIGHT_DIRECTIONAL: {
225 vec3 sun_dir = -light.direction.xyz;
226 if(light.radius > 0.0) {
227 to_light = sampleCone(sun_dir, light.radius, rnd.yz);
228 pdf_dir = conePDF(light.radius);
229 }
230 else {
231 to_light = sun_dir;
232 }
233 dist = 1e10;
234 attenuation = 1.0;
235 break;
236 }
237 case LIGHT_SPOT: {
238 to_light = light.position.xyz - h.pos;
239 dist = length(to_light);
240 attenuation = 1.0 / (dist * dist);
241 vec3 dir_to_dir = -normalize(to_light);
242 float cos_angle = dot(dir_to_dir, light.direction.xyz);
243 float t = smoothstep(light.spot_outer, light.spot_inner, cos_angle);
244 attenuation *= t;
245 if (attenuation <= 0) return vec3(0);
246 break;
247 }
248 default:
249 return vec3(0);
250 }
251
252 vec3 dir_light = normalize(to_light);
253 float cos_surface = dot(h.normal, dir_light);
254 if (cos_surface <= 0.0) return vec3(0);
255
257 float shadow_eps = length(mesh_aabb_max - mesh_aabb_min) * EPS_SHADOW;
258
259 Ray shadow_ray;
260 shadow_ray.origin = surfaceOrigin(h);
261 shadow_ray.direction = dir_light;
262
263 Hit shadow_hit;
264 bool occluded = intersects(shadow_ray, shadow_hit) && shadow_hit.t < dist - shadow_eps && shadow_hit.material != MAT_SHADOW_CATCHER;
265
266 if (occluded) return vec3(0);
267
268 float pdf_select = 1.0 / float(analytic_light_count);
269 float pdf_light = pdf_select * pdf_dir;
270 if (pdf_light <= 0.0) return vec3(0);
271
275 float weight = 1.0;
276 if(light.type == LIGHT_DIRECTIONAL && light.radius > 0.0) {
277 float pdf_bsdf = bsdfPDF_diffuse(h.normal, dir_light);
278 weight = powerHeuristic(pdf_light, pdf_bsdf);
279 }
280
281 //Direct contribution
282 return h.albedo * light.emission.rgb * cos_surface * weight / (PI * pdf_light);
283}
284
285vec3 sampleEmissiveTriangles(Hit h, int bounce, int spp_index, uvec2 px) {
286 if(light_count == 0) return vec3(0);
287
288 vec3 rnd = rand3(bounce + 100, spp_index, px);
289 int light_idx = light_indices[int(rnd.x * float(light_count))];
290
291 GPUTriangle light_tri = triangles[light_idx];
292 int m_id = light_tri.material_id;
293 vec3 emission = gpu_materials[m_id].emission.rgb;
294
295 if (dot(emission, emission) == 0.0) return vec3(0);
296
297 //Point sample on light surface
298 vec3 light_pos = sampleTriangle(light_tri.v0.position, light_tri.v1.position, light_tri.v2.position, rnd.yz);
299
300 //Shadow rays
301 vec3 to_light = light_pos - h.pos;
302 float dist = length(to_light);
303 vec3 dir_light = to_light / dist;
304
305 //Check if light is in the correct surface side
306 float cos_surface = dot(h.normal, dir_light);
307 if (cos_surface <= 0.0) return vec3(0);
308
309 //Light normal
310 vec3 light_normal = normalize(cross(light_tri.v1.position - light_tri.v0.position, light_tri.v2.position - light_tri.v0.position));
311 float cos_light = dot(-dir_light, light_normal);
312 if(cos_light <= 0.0) return vec3(0);
313
315 float shadow_eps = length(mesh_aabb_max - mesh_aabb_min) * EPS_SHADOW;
316
317 //check occlusion of shadow rays
318 Ray shadow_ray;
319 shadow_ray.origin = surfaceOrigin(h);
320 shadow_ray.direction = dir_light;
321
322 Hit shadow_hit;
323
324 bool occluded = intersects(shadow_ray, shadow_hit) && shadow_hit.t < dist - shadow_eps && shadow_hit.material != MAT_SHADOW_CATCHER;
325
326 if (occluded) return vec3(0);
327
328 //PDF sampling
329 float area = triangleArea(light_tri.v0.position, light_tri.v1.position, light_tri.v2.position);
330 float pdf_light = trianglelightPDF(dist, cos_light, area);
331 if(pdf_light <= 0.0) return vec3(0);
332
333 //MIS weight
334 float pdf_bsdf = bsdfPDF_diffuse(h.normal, dir_light);
335 float weight = powerHeuristic(pdf_light, pdf_bsdf);
336
337 //Direct contribution
338 return h.albedo * emission * cos_surface * weight / (PI * pdf_light);
339}
340
345vec3 sampleEnvLight(Hit h, int bounce, int spp_index, uvec2 px) {
346 if(USE_ENV_MAP == 0) return vec3(0);
347
348 vec3 r = rand3(bounce + 300, spp_index, px);
349
350 //Cosine-weighted hemisphere sampling
351 float cosT = sqrt(r.x);
352 float sinT = sqrt(1.0 - r.x);
353 float phi = TWO_PI * r.y;
354 vec3 local_dir = vec3(sinT * cos(phi), sinT * sin(phi), cosT);
355 vec3 world_dir = onb(h.normal) * local_dir;
356
357 //Shadow ray to check occlusion
358 Ray shadow_ray;
359 shadow_ray.origin = surfaceOrigin(h);
360 shadow_ray.direction = world_dir;
361
362 Hit shadow_hit;
363
364 bool occluded = intersects(shadow_ray, shadow_hit) && shadow_hit.material != MAT_SHADOW_CATCHER;
365
366 if (occluded) return vec3(0);
367
368 vec3 env_color = sampleEnvMap(world_dir);
369
370 //NEE and the diffuse BSDF sample the same cosine-weight hemisphere, so their PDFs are the same
371 //Means that the powerHeuristic is always 0.5 here
372 return h.albedo * env_color * 0.5;
373}
374
375//----------------------------------------------------------
376//Path Tracer
377
383vec4 pathTraceDebug(vec2 uv, int spp_index, uvec2 px) {
384 Ray ray = cameraRayDOF(uv, spp_index, px);
385 Hit h;
386 if(!intersects(ray, h)) return vec4(0.0, 0.0, 0.0, 1.0);
387
388 vec3 n = normalize(DEBUG_VIEW == DEBUG_VIEW_GEOM_NORMALS ? h.geom_normal : h.normal);
389 return vec4(n * 0.5 + 0.5, 1.0);
390}
391
392
402vec4 pathTraceFromRay(Ray ray, int spp_index, uvec2 px, float disp_coeff) {
403 vec3 color = vec3(0);
404 vec3 throughput = vec3(1);
405 float alpha = 1.0;
406
407 int diffuse_bounces = 0;
408
411 float prev_bsdf_pdf = -1.0;
412
413 //foreach ray bounce
414 for (int b = 0; b < DEPTH; b++) {
415 Hit h;
416 if (!intersects(ray, h)) {
417 for (int li = 0; li < analytic_light_count; li++) {
418 GPULight light = analytic_lights[li];
419 if(light.type != LIGHT_DIRECTIONAL) continue;
420 float cos_angle = dot(ray.direction, -light.direction.xyz);
421 if(cos_angle > cos(light.radius)) {
422 float mis_weight = 1.0;
423 if (light.radius > 0.0 && prev_bsdf_pdf >= 0.0) {
424 float pdf_light = conePDF(light.radius) / float(analytic_light_count);
425 mis_weight = powerHeuristic(prev_bsdf_pdf, pdf_light);
426 }
427 color += throughput * light.emission.rgb * mis_weight;
428 }
429 }
430
431 //Environment Mapping
432 if(USE_ENV_MAP == 1) {
433 float mis_weight = (prev_bsdf_pdf >= 0.0 && b < 3) ? 0.5 : 1.0;
434 color += throughput * sampleEnvMap(ray.direction) * mis_weight;
435 }
436 else {
437 //Background Alternative Colors
438 switch(BACKGROUND) {
439 case BG_BLACK:
440 break;
441 case BG_WHITE:
442 color += throughput * vec3(0.9);
443 break;
444 case BG_GRADIENT: //skybox-like
445 float t = clamp(ray.direction.z * 0.5 + 0.5, 0.0, 1.0);
446 color += throughput * mix(vec3(0), vec3(1), t);
447 break;
448 }
449 }
450 break;
451 }
452
453 //Focal debug only on 1st bounce
454 if(b == 0 && FOCAL_DEBUG) {
455 float dist_to_focal = abs(h.t - CAM_FOCAL_DISTANCE);
456 if(dist_to_focal < FOCAL_BAND_DEBUG)
457 color += vec3(0.0, 1.0, 0.0) * 0.5;
458 }
459
460 //Avoids double counting with NEE for emission
461 //MIS, full weight for the primary ray or after specular bounce
462 //otherwise weighted against what NEE PDF would be for this hit
463 if (dot(h.emission, h.emission) > 0.0) {
464 float mis_weight = 1.0;
465 if(prev_bsdf_pdf >= 0.0) {
466 float cos_light = dot(h.geom_normal, -ray.direction);
467 float pdf_light = trianglelightPDF(h.t, cos_light, h.light_area);
468 mis_weight = (pdf_light > 0.0) ? powerHeuristic(prev_bsdf_pdf, pdf_light) : 1.0;
469 }
470 color += throughput * h.emission * mis_weight;
471 break;
472 }
473
474 //NEE
475 //Only analytic lights without geometry (point, directional, spot)
476 if(USE_NEE == 1 && h.material == MAT_DIFFUSE && analytic_light_count > 0) {
477 color += throughput * sampleAnalyticLight(h, b, spp_index, px);
478 }
479
480 //NEE on emissive triangles
481 if (USE_NEE == 1 && h.material == MAT_DIFFUSE && light_count > 0) {
482 color += throughput * sampleEmissiveTriangles(h, b, spp_index, px);
483 }
484
485 //NEE on Environment Map HDRI
486 //Env map lighting only affects up-to 2 bounces for performance
487 if (USE_NEE == 1 && h.material == MAT_DIFFUSE && USE_ENV_MAP == 1 && b < 2) {
488 color += throughput * sampleEnvLight(h, b, spp_index, px);
489 }
490
491 vec3 r = rand3(b, spp_index, px);
492
493 //Used by MAT_DIFFUSE when the bounce cap is reached
494 //\note break inside the switch only exits the switch, not the for-loop,
495 //the end_path flag is used to terminate the path instead of leaving to re-hit the same point
496 bool end_path = false;
497
498 switch(h.material) {
499 //Diffuse Materials
500 case MAT_DIFFUSE: {
501 //Reduces diffuse bounces without losing quality because of NEE
502 if(diffuse_bounces++ >= 2 && USE_NEE == 1) {
503 end_path = true;
504 }
505
506 //Cosine-weighted hemisphere
507 float cosT = sqrt(r.x);
508 float sinT = sqrt(1.0 - r.x);
509 float phi = TWO_PI * r.y;
510 ray.direction = onb(h.normal) * vec3(sinT*cos(phi), sinT*sin(phi), cosT);
511 throughput *= h.albedo;
512 ray.origin = surfaceOrigin(h);
513 prev_bsdf_pdf = bsdfPDF_diffuse(h.normal, ray.direction);
514 break;
515 }
516 case MAT_MIRROR: {
517 ray.direction = reflect(ray.direction, h.normal);
518 throughput *= h.albedo;
519 ray.origin = surfaceOrigin(h);
520 prev_bsdf_pdf = -1.0;
521 break;
522 }
523 case MAT_GLASS: {
524 bool h_entering = dot(ray.direction, h.normal) < 0.0;
525 vec3 normal = h_entering ? h.normal : -h.normal;
526 //Geometric normal facing the incoming ray, offsets the new origin to the correct side
527 vec3 ng_in = dot(h.geom_normal, ray.direction) < 0.0 ? h.geom_normal : -h.geom_normal;
528
529 //Dispersion. This trace is dedicated to a channel's wavelength
530 //so every refraction it does uses the respective channel's IOR, same idea as cameraRayDOFChannel(),
531 //but applied to glass mat.
532 float ior = h.ior * (1.0 + GLASS_DISPERSION * disp_coeff);
533 float eta = h_entering ? (1.0 / ior) : ior;
534
535 float cos_theta = abs(dot(-ray.direction, normal));
536 float r0 = (1.0 - eta) / (1.0 + eta);
537 r0 *= r0; //r0²
538 float fresnel = r0 + (1.0 - r0) * pow(1.0 - cos_theta, 5.0);
539
540 float sin_theta_sq = eta * eta * (1.0 - cos_theta * cos_theta);
541 bool total_reflect = sin_theta_sq > 1.0;
542
543 if (total_reflect || r.z < fresnel) {
544 ray.direction = reflect(ray.direction, normal);
545 ray.origin = h.pos + ng_in * rayEps();
546 }
547 else {
548 ray.direction = refract(ray.direction, normal, eta);
549 ray.origin = h.pos - ng_in * rayEps();
550 }
551 throughput *= h.albedo;
552 prev_bsdf_pdf = -1.0;
553 break;
554 }
556 case MAT_TINTED_GLASS: { //ray passes directly, no Fresnel reflection
557 throughput *= h.albedo;
558 //Continues through the surface: start on the far side of the geometric plane
559 vec3 ng_in = dot(h.geom_normal, ray.direction) < 0.0 ? h.geom_normal : -h.geom_normal;
560 ray.origin = h.pos - ng_in * rayEps();
561 prev_bsdf_pdf = -1.0;
562 break;
563 }
564 case MAT_SHADOW_CATCHER: {
565 float shadow_eps = max(EPS, length(h.pos) * EPS_SHADOW);
566 bool in_shadow = false;
567
568 for (int li = 0; li < analytic_light_count; li++) {
569 GPULight light = analytic_lights[li];
570 vec3 to_light = (light.type == LIGHT_DIRECTIONAL) ? -light.direction.xyz : normalize(light.position.xyz - h.pos);
571 float dist = (light.type == LIGHT_DIRECTIONAL) ? 1e10 : length(light.position.xyz - h.pos);
572
573 Ray shadow_ray;
574 shadow_ray.origin = h.pos + h.geom_normal * shadow_eps;
575 shadow_ray.direction = to_light;
576 Hit shadow_h;
577
578 if(intersects(shadow_ray, shadow_h) && shadow_h.t < dist - shadow_eps && shadow_h.material != MAT_SHADOW_CATCHER) {
579 in_shadow = true;
580 break;
581 }
582 }
583 if(in_shadow) {
584 color = vec3(0.0);
586 }
587 else {
588 alpha = 0.0;
589 }
590 return vec4(color, alpha);
591 }
592 }
593
594 if (end_path) break;
595
596 // Russian Roulette
597 if (RR_MIN_BOUNCES > 0 && b >= RR_MIN_BOUNCES) {
598 float survival = max(throughput.r, max(throughput.g, throughput.b));
599
600 survival = min(survival, RR_MAX_SURVIVAL);
601
602 //Ends path with P = 1 - survival
603 if (rand3(b + DEPTH, spp_index, px).x > survival) break;
604
605 throughput /= survival;
606 }
607
608 // Early Termination
609 //Mostly works as a safeguard for the RR (removes rays with < 0.1% intensity)
610 if(max(throughput.r, max(throughput.g, throughput.b)) < 0.001) break;
611 }
612
613 // Firefly Clamp
614 if (FIREFLY_CLAMP > 0.0) {
615 float lum = dot(color, vec3(0.2126, 0.7152, 0.0722));
616 if (lum > FIREFLY_CLAMP)
617 color *= FIREFLY_CLAMP / lum;
618 }
619
620 return vec4(color, alpha);
621}
622
623vec4 pathTrace(vec2 uv, int spp_index, uvec2 px) {
624 //Uniform branch: same for every invocation, no divergence
626 return pathTraceDebug(uv, spp_index, px);
627
628 if(CAM_LATERAL_CA <= 0.0 && CAM_AXIAL_CA <= 0.0 && GLASS_DISPERSION <= 0.0) {
629 Ray ray = cameraRayDOF(uv, spp_index, px);
630 return pathTraceFromRay(ray, spp_index, px, 0.0);
631 }
632
633 vec3 traces[3];
634 float final_alpha = 0.0;
635 for(int c = 0; c < 3; c++) {
636 float jitter = (rand3(-10 - c, spp_index, px).x - 0.5) * DISPERSION_JITTER_WIDTH;
637 float disp_coeff = DISPERSION_COEFF[c] + jitter;
638
639 Ray ray = cameraRayDOFChannel(uv, spp_index, px, disp_coeff);
640 vec4 result = pathTraceFromRay(ray, spp_index, px, disp_coeff);
641 traces[c] = result.rgb;
642 final_alpha += result.a;
643 }
644
645 vec3 rc = traces[0], gc = traces[1], bc = traces[2];
646 vec3 final_color;
647 final_color.r = 0.70 * rc.r + 0.25 * gc.r + 0.05 * bc.r;
648 final_color.g = 0.15 * rc.g + 0.70 * gc.g + 0.16 * bc.g;
649 final_color.b = 0.05 * rc.b + 0.25 * gc.b + 0.70 * bc.b;
650
651 return vec4(final_color, final_alpha / 3.0);
652}
const float EPS_SHADOW
Definition globals.glsl:8
const float TWO_PI
Definition globals.glsl:3
const float EPS
Definition globals.glsl:6
const float PI
Definition globals.glsl:2
uniform int BACKGROUND
Color result if the ray doesn't reach the light.
Definition globals.glsl:118
uniform int USE_ENV_MAP
Definition globals.glsl:121
const int BG_GRADIENT
Definition globals.glsl:115
uniform sampler2D env_map
Definition globals.glsl:120
uniform float GROUND_SHADOW_OPACITY
Definition globals.glsl:131
const int BG_BLACK
Definition globals.glsl:113
const int BG_WHITE
Definition globals.glsl:114
uniform int CAM_APERTURE_BLADES
Definition globals.glsl:69
const float DISPERSION_COEFF[3]
Definition globals.glsl:59
uniform float CAM_APERTURE
Definition globals.glsl:15
uniform vec3 camera_position
Camera position in world space.
Definition globals.glsl:29
uniform float CAM_CATEYE_STRENGTH
Definition globals.glsl:84
const int PROJ_FISHEYE_EQUIDISTANT
Definition globals.glsl:94
uniform float CAM_TILT
Definition globals.glsl:104
uniform float CAM_DISTORTION_K2
Definition globals.glsl:90
uniform vec3 camera_lookat
Camera direction.
Definition globals.glsl:32
uniform float FOCAL_BAND_DEBUG
Thickness of the focal plane line.
Definition globals.glsl:24
const int PROJ_FISHEYE_STEREOGRAPHIC
Definition globals.glsl:95
const float DISPERSION_JITTER_WIDTH
Definition globals.glsl:65
uniform float CAM_FOCAL_DISTANCE
Worldspace distance between camera and focal plane.
Definition globals.glsl:18
uniform float CAM_BLADE_ROTATION
Rotates the polygon aperture shape (in radians).
Definition globals.glsl:72
uniform float CAM_AXIAL_CA
Definition globals.glsl:51
uniform float CAM_ANAMORPHIC_SQUEEZE
Definition globals.glsl:78
uniform bool FOCAL_DEBUG
Enables debug focal plane viewer.
Definition globals.glsl:21
uniform float CAM_DISTORTION_K1
Definition globals.glsl:89
uniform vec3 camera_up
Camera's up vector.
Definition globals.glsl:35
uniform float CAM_LATERAL_CA
Definition globals.glsl:43
const int PROJ_RECTILINEAR
Definition globals.glsl:93
uniform int CAM_PROJECTION_MODE
Camera projection model, see PROJ_* constants.
Definition globals.glsl:99
uniform float CAM_FOV
Definition globals.glsl:26
uniform int DEBUG_VIEW
Definition globals.glsl:355
const int DEBUG_VIEW_OFF
Definition globals.glsl:356
const int DEBUG_VIEW_GEOM_NORMALS
flat face normal (h.geom_normal)
Definition globals.glsl:362
const int MAT_SHADOW_CATCHER
Definition globals.glsl:210
uniform float GLASS_DISPERSION
Glass dispersion strength (prism effect).
Definition globals.glsl:219
uniform vec3 mesh_aabb_min
Bounding box of the loaded mesh to skip the triangle loop for rays that don't interact with the model...
Definition globals.glsl:310
uniform vec3 mesh_aabb_max
Definition globals.glsl:311
uniform int analytic_light_count
Definition globals.glsl:298
uniform int light_count
Definition globals.glsl:293
uniform float FIREFLY_CLAMP
Definition globals.glsl:199
uniform int DEPTH
Definition globals.glsl:174
uniform float RR_MAX_SURVIVAL
Definition globals.glsl:191
uniform int USE_NEE
Enables NEE.
Definition globals.glsl:194
uniform int RR_MIN_BOUNCES
Russian Roulette, minimum bounces before enabling. Changing to more or less gives minimal performance...
Definition globals.glsl:185
GLsizei GLsizei * length
Definition imgui_impl_opengl3_loader.h:319
GLfloat GLfloat GLfloat alpha
Definition imgui_impl_opengl3_loader.h:195
GLuint texture
Definition imgui_impl_opengl3_loader.h:231
GLuint GLuint64EXT * result
Definition imgui_impl_opengl3_loader.h:457
GLintptr offset
Definition imgui_impl_opengl3_loader.h:278
vector< GPULight > analytic_lights
Definition main.cpp:96
#define MAT_DIFFUSE
Definition mesh.cpp:14
#define MAT_MIRROR
Definition mesh.cpp:15
#define MAT_TINTED_GLASS
Definition mesh.cpp:17
#define MAT_GLASS
Definition mesh.cpp:16
#define LIGHT_POINT
Definition mesh.hpp:122
#define LIGHT_DIRECTIONAL
Definition mesh.hpp:123
#define LIGHT_SPOT
Definition mesh.hpp:124
uniform vec2 resolution
Definition path_trace.frag:14
float rayEps()
Scene-relative epsilon for ray origins, avoids self-intersection.
Definition path_trace_core.glsl:178
void camera_axes(out vec3 cam_x, out vec3 cam_y, out vec3 cam_z)
Calculates the base of the camera in world-space.
Definition path_trace_core.glsl:14
vec3 cameraRay(vec2 uv, vec3 cam_x, vec3 cam_y, vec3 cam_z, float fov_scale)
Definition path_trace_core.glsl:27
vec3 sampleAperture(vec3 cam_x, vec3 cam_y, vec2 uv, float angle, float radius_rand)
Samples a point on the camera's aperture (lens disk) Shaped by CAM_APERTURE_BLADES/CAM_BLADE_ROTATION...
Definition path_trace_core.glsl:85
vec3 sampleEmissiveTriangles(Hit h, int bounce, int spp_index, uvec2 px)
Definition path_trace_core.glsl:285
vec3 sampleEnvMap(vec3 dir)
Definition path_trace_core.glsl:164
vec4 pathTraceFromRay(Ray ray, int spp_index, uvec2 px, float disp_coeff)
Traces a single already-generated path through the bounce loop and returns the radiance....
Definition path_trace_core.glsl:402
vec3 sampleAnalyticLight(Hit h, int bounce, int spp_index, uvec2 px)
Definition path_trace_core.glsl:195
Ray cameraRayDOF(vec2 uv, int spp_index, uvec2 px)
Creates a ray with depth of field.
Definition path_trace_core.glsl:157
vec4 pathTrace(vec2 uv, int spp_index, uvec2 px)
Definition path_trace_core.glsl:623
Ray cameraRayDOFChannel(vec2 uv, int spp_index, uvec2 px, float disp_coeff)
Creates a ray with depth of field, using one channel's lens parameters.
Definition path_trace_core.glsl:126
vec4 pathTraceDebug(vec2 uv, int spp_index, uvec2 px)
Definition path_trace_core.glsl:383
float polygonAperture(float angle, int blades)
Max radius of a regular N-gon at a given angle.
Definition path_trace_core.glsl:70
vec3 focalPlanePoint(vec3 cam_x, vec3 cam_y, vec3 cam_z, vec3 ray_dir, float focal_distance)
Point on the (maybe tilted) focal plane that a given ray focuses on.
Definition path_trace_core.glsl:106
vec3 surfaceOrigin(Hit h)
Origin for any ray leaving an opaque surface on the side it was hit from (diffuse/mirror bounces and ...
Definition path_trace_core.glsl:188
vec3 sampleEnvLight(Hit h, int bounce, int spp_index, uvec2 px)
Definition path_trace_core.glsl:345
vec3 rand3(int bounce, int path_id, uvec2 px)
Definition sampling.glsl:13
vec3 sampleCone(vec3 axis, float theta_max, vec2 rnd)
Definition sampling.glsl:46
float conePDF(float theta_max)
Definition sampling.glsl:55
float triangleArea(vec3 v0, vec3 v1, vec3 v2)
Definition sampling.glsl:37
float bsdfPDF_diffuse(vec3 normal, vec3 dir)
Definition sampling.glsl:71
mat3 onb(vec3 n)
Definition sampling.glsl:23
vec3 sampleTriangle(vec3 v0, vec3 v1, vec3 v2, vec2 rnd)
Samples a random point in the triangle surface uniformly distributed, based on R. Osada,...
Definition sampling.glsl:30
float trianglelightPDF(float dist, float cos_light, float area)
Definition sampling.glsl:77
float powerHeuristic(float pdf_a, float pdf_b)
Definition sampling.glsl:62
bool intersects(const Ray ray, out Hit h)
Definition scene.glsl:179
Definition globals.glsl:267
float radius
Definition globals.glsl:272
int type
Definition globals.glsl:271
vec4 direction
Definition globals.glsl:270
vec4 emission
Definition globals.glsl:269
vec4 position
Definition globals.glsl:268
float spot_outer
Definition globals.glsl:274
float spot_inner
Definition globals.glsl:273
Definition globals.glsl:245
GPUVertex v2
Definition globals.glsl:246
int material_id
Definition globals.glsl:247
GPUVertex v1
Definition globals.glsl:246
GPUVertex v0
Definition globals.glsl:246
vec3 position
Definition globals.glsl:236
ior = index of refraction (only used with MAT_GLASS)
Definition globals.glsl:164
vec3 normal
Definition globals.glsl:166
vec3 lift
Offset from the flat triangle to the smooth surface, for rays leaving it.
Definition globals.glsl:171
float t
Definition globals.glsl:165
vec3 geom_normal
Definition globals.glsl:166
int material
Definition globals.glsl:168
float ior
Definition globals.glsl:169
vec3 albedo
Definition globals.glsl:167
vec3 pos
Definition globals.glsl:166
float light_area
Used for MIS.
Definition globals.glsl:170
vec3 emission
Definition globals.glsl:167
Definition globals.glsl:158
vec3 direction
Definition globals.glsl:160
vec3 origin
Definition globals.glsl:159