Path Tracing
Loading...
Searching...
No Matches
path_trace_core.glsl File Reference

Path Tracer Core Shader. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void camera_axes (out vec3 cam_x, out vec3 cam_y, out vec3 cam_z)
 Calculates the base of the camera in world-space.
vec3 cameraRay (vec2 uv, vec3 cam_x, vec3 cam_y, vec3 cam_z, float fov_scale)
float polygonAperture (float angle, int blades)
 Max radius of a regular N-gon at a given angle.
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/CAM_ANAMORPHIC_SQUEEZE, and is biased towards the frame edges (behaviour like mechanical vignette) by CAM_CATEYE_STRENGTH.
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.
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.
Ray cameraRayDOF (vec2 uv, int spp_index, uvec2 px)
 Creates a ray with depth of field.
vec3 sampleEnvMap (vec3 dir)
float rayEps ()
 Scene-relative epsilon for ray origins, avoids self-intersection.
vec3 surfaceOrigin (Hit h)
 Origin for any ray leaving an opaque surface on the side it was hit from (diffuse/mirror bounces and every NEE shadow ray). Replaces old hardcoded fixed EPS Lifted off the flat triangle (shadow terminator) and pushed along the geometric normal.
vec3 sampleAnalyticLight (Hit h, int bounce, int spp_index, uvec2 px)
vec3 sampleEmissiveTriangles (Hit h, int bounce, int spp_index, uvec2 px)
vec3 sampleEnvLight (Hit h, int bounce, int spp_index, uvec2 px)
vec4 pathTraceDebug (vec2 uv, int spp_index, uvec2 px)
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. Extracted from pathTrace() so the exact same bounce logic can be called per color channel, hero-sampling style.
vec4 pathTrace (vec2 uv, int spp_index, uvec2 px)

Detailed Description

Path Tracer Core Shader.

Author
Leonel Matos
Date
2026-04-22

Function Documentation

◆ camera_axes()

void camera_axes ( out vec3 cam_x,
out vec3 cam_y,
out vec3 cam_z )

Calculates the base of the camera in world-space.

Parameters
vec3All camera axes

◆ cameraRay()

vec3 cameraRay ( vec2 uv,
vec3 cam_x,
vec3 cam_y,
vec3 cam_z,
float fov_scale )

Calculates the direction of a ray for a pixel

Parameters
uvPixel coordinates
vec3All camera axes
fov_scaleMultiplier on CAM_FOV, so callers can perturb it per color/colour channel
Returns
Normalized direction of ray in worldspace
See also
CAM_DISTORTION_K1, CAM_PROJECTION_MODE

◆ cameraRayDOF()

Ray cameraRayDOF ( vec2 uv,
int spp_index,
uvec2 px )

Creates a ray with depth of field.

Parameters
uvpixel coordinates [0,1]
Returns
Ray with origin at the lens directed to the focal point
Note
random jitter generated here, for each spp
This is a wrapper around cameraRayDOFChannel(...1) so every caller keeps the pre-chromatic dispersion
See also
CAM_APERTURE, CAM_FOCAL_DISTANCE

◆ cameraRayDOFChannel()

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.

Returns
Ray with origin at the lens directed to the disp_coefficient's directed focal point
See also
cameraRayDOF, CAM_LATERAL_CA, CAM_AXIAL_CA

◆ focalPlanePoint()

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.

Note
CAM_TILT==0 keeps the original sphere-approx behaviour
See also
CAM_TILT

◆ pathTrace()

vec4 pathTrace ( vec2 uv,
int spp_index,
uvec2 px )

◆ pathTraceDebug()

vec4 pathTraceDebug ( vec2 uv,
int spp_index,
uvec2 px )

Used to debug the brute-force path tracer for only the primary ray, no bounces, no materials, no optimizations, no NEE

Returns
hit normal in color

◆ pathTraceFromRay()

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. Extracted from pathTrace() so the exact same bounce logic can be called per color channel, hero-sampling style.

Parameters
channelto which this ray is dedicated to (0=R, 1=G, 2=B), for MAT_GLASS
Returns
vec3 color
Note
Uses cosine-weighted sampling for diffuse materials and Fresnel+Snell for glass materials
See also
pathTrace, BACKGROUND, FOCAL_DEBUG, RR_MAX_SURVIVAL

PDF of the direction the BSDF sampler chose at the previous vertex. < 0 == no valid competing NEE strategy Means that emission hit directly so it gets full weight instead of MIS-weighted

DEPRECATED

◆ polygonAperture()

float polygonAperture ( float angle,
int blades )

Max radius of a regular N-gon at a given angle.

Parameters
bladesnumber of aperture blades, <3 returns 1.0 (circular)
Note
Standart "polygon bokeh": r(θ) = cos(π/n) / cos(θ' - π/n), where θ' is θ measured relative to the nearest edge's midpoint.

◆ rayEps()

float rayEps ( )

Scene-relative epsilon for ray origins, avoids self-intersection.

Note
EPS_TRI (1e-10) is far below float precision at scene coordinates, it can't be used as an offset

◆ sampleAnalyticLight()

vec3 sampleAnalyticLight ( Hit h,
int bounce,
int spp_index,
uvec2 px )

Adaptive EPS for shadow rays to avoid self-intersection

MIS only applies where a BSDF sampled ray could land here Like a directional light with real angular extent. Delta light (point, spot, 0-radius sun) get full weight

◆ sampleAperture()

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/CAM_ANAMORPHIC_SQUEEZE, and is biased towards the frame edges (behaviour like mechanical vignette) by CAM_CATEYE_STRENGTH.

Parameters
uvpixel coordinates [0,1], used only for cat's-eye
anglerandom angle around the lens
radius_randrandom [0,1[ for the radius, distributed in uniform disk density using the sqrt
Returns
world-space offset from camera_position, on the lens plane

◆ sampleEmissiveTriangles()

vec3 sampleEmissiveTriangles ( Hit h,
int bounce,
int spp_index,
uvec2 px )

Adaptive EPS for shadow rays to avoid self-intersection

◆ sampleEnvLight()

vec3 sampleEnvLight ( Hit h,
int bounce,
int spp_index,
uvec2 px )
Note
Because cos_theta / (PI * pdf) is 1.0, the last return value can be return h.albedo * env_color But I kept the entire formula

◆ sampleEnvMap()

vec3 sampleEnvMap ( vec3 dir)

◆ surfaceOrigin()

vec3 surfaceOrigin ( Hit h)

Origin for any ray leaving an opaque surface on the side it was hit from (diffuse/mirror bounces and every NEE shadow ray). Replaces old hardcoded fixed EPS Lifted off the flat triangle (shadow terminator) and pushed along the geometric normal.

See also
terminatorLift, rayEps