Rendering godrays with monte carlo path tracing on the GPU

Noah Pitts | Isak Karlsson

1. Introduction

The god ray is a visually appealing and intense lighting effect that can be seen in the rendering of interior architectural scenes with limited direct illumination through a portal. The paper proposes a technique for using the the GPU to efficiently and progressively render god rays in these types of scenes using brute force Monte Carlo integration of a volumetric rendering equation. A CUDA rendering application called godRay was produced with the ability to control sun and sky lighting and participating media parameters in a interior temple scene. The discussion is focused on the caparison of these parameters with respect to the physically based rendering of god rays.

Occulus of the Pantheon with Crepuscular Ray

1.1 Crepuscular Rays

God rays, also known as Crepuscular Rays in atmospheric optics, are light beams that appear to radiate from an intense light source in the scene and are particularly apparent when a considerable portion of the scene is occluded from the this light source. In exterior scenes god rays may be seen as beams of light streaming through dense clouds or physical openings in geometry such as between buildings or trees. Interior scenes can also exhibit god rays such as in the image of the pantheon, a classical roman architecture that is day-lit by a single occulus at the top of the dome. In these cases, the viewer of the image is drawn to the phenomenological qualities of the space and the connection to this exterior light source. In the physical environment, god rays are usually produced by the intensity sun of the sun as a single light source in contrast to the much less intense sky dome or skylight. The rays appear as if they converge to a point, but in the case of sunlight are near parallel. The tapering or slight conical appearance of these light rays is an effect often caused by perspective convergence or in the case of computer rendering linear perspective.

1.2 Participating Media

God rays are made visible in the scene through the interaction of light with participating media or atmospheric particles such as smoke, fog, or haze. It is often the case that in architectural renderings of an interior scene, god rays appear in a homogeneous media as the interacting particles in the volume of the space are well mixed. However interactions with media of heterogeneous density are possible as well. In this paper we will be considering only the homogeneous case due to the efficiency it affords when sampling the media.

1.3 GPU Rendering

For interior architectural scenes with significant indirect lighting and global illumination path tracing techniques can provided excellent results. In order to maintain an unbiased solution we have used a brute force Monte Carlo integrator to numerically solve the rendering equations. The brute force approach to the rendering equation requires a significantly greater amount of pixel samples to resolve the scene, especially in areas only lit by indirect illumination. The increase in sampling can greatly increase the rendering times of the scene. In scenes with participating media, the sampling and rendering times can increase even further. To account for this, we have developed the rendering application on the GPU using Nvidia CUDA. Both the direct illumination and indirect illumination integrators are developed in CUDA kernels to take advantage of the massive core count on the GPU. The test images for this paper were rendered with two Nvidia Titan X GPUs with a combined count of 7680 CUDA cores. The scene bounding volume hierarchy (BVH) was built using the Nvidia OptiX SDK.

2. Mathematical Model

The rendering equation for godRay is formaly based on the rendering equation proposed by Kajiya Reference with adaptations for volumetric rendering in homogeneous participating media discussed in chapter 4 of Wojciech Jarosz thesis Reference. This project maintains an unbiased approach using monte carlo techniques as presented by Veach Reference.

2.1 Participating media

Absorbtion
Out Scatter
In Scatter
For rendering participating media in the scene we consider three differential effects of light transport along the length of the ray path segments: absorbtion ( σa\\sigma\_a ) , out-scattering ( σo\\sigma\_o ) , and in-scattering ( σs\\sigma\_s ) . However absorbtion and out-scattering will be combined to form a single extinction coefficient ( σt\\sigma\_t ).
σt=σa+σo\\sigma\_t = \\sigma\_a + \\sigma\_o

where the differential equation of extinction can be described by

dLo(x,ω)=σt(x,ω)Li(x,ω)dtdL \_o(x, \\omega) = -\\sigma\_t(x, \\omega)L\_i(x, \\omega)dt

The fraction of radiance that is transmitted between two points along the ray path, transmittance( Tr(x1,x2)T_r(x_1, x_2) ) is therefore the solution to the differential equation of extinction.

Tr(x1,x2)=e0dσt(x1+tω,ω)dtT\_r(x\_1, x\_2) = e^{-\\int\_0^d{\\sigma\_t(x\_1 + t\\omega, \\omega)dt}}

However, since we are considering our godRay scenes to contain only homogenuos media where the extintion coefficient is constant, we can significanly simplify the tranmittance equation to beer's law where dd is representing the path length between points.

Tr(x1,x2)=eσtdT\_r(x\_1, x\_2) = e^{-\\sigma\_td}

The model for in-scattering can be described by the differential equation where incoming radiation from all solid angles ω\omega' is scattered in the direction of the ray ω\omega .

dLo(x,ω)=σs(x,ω)S2ρp(x,ω,ω)Li(x,ω)dωdtdL\_o(x, \\omega) = \\sigma\_s(x, \\omega)\\int\_{S\_2}\\rho\_p(x, \\omega', \\omega)Li(x, \\omega')d\\omega'dt

2.2 Phase functions and BSDF

The phase function, similar to the BSDF function for surface interactions, ρp\rho_p represents the fraction of light that is scattered into the ray direction ω\omega . For godRay we are using the Henyey Greenstien phase function reference which allows for a single asymmetry value gg between the range [-1.0, 1.0] to paramaterize the scattering angle. When g=1.0g = -1.0 there is perfect back scattering while when g=1.0g = 1.0 there is perfect forward scattering. When g=0.0g = 0.0 scattering is isotropic with uniform probability of scattering in every direction.

ρp(x,ω,ω)=14π1g2(1+g22gcosθ)3/2\\rho\_p(x, \\omega', \\omega) = {1\\over4\\pi}{1-g^2\\over(1+g^2-2g\\cos\\theta)^{3/2}}

In godRay, in order to focus on the volumetric effects of the god ray we are only considering a basic Lambertian bidirectional reflectance distribution function or BRDF reference representing an ideal matte surface. While the Lambertian model is not physically based it provides a significant simplification of the model. For a more physically based diffuse surface, the microfacet model reference could be considered. In our case however ρr\rho_r represents the BRDF while sigmarsigma_r represents the diffuse reflectance coefficient of the surface interaction.

ρr(x,ω,ω)=σrπ\\rho\_r(x, \\omega, \\omega') = {\\sigma\_r\\over\\pi}

2.3 Volume Rendering Equation

The volume rendering equation for radiance L(x,ω)L(x, \omega) must consider the different effects of either scattering at a surface or within the media. Therefore along a given path segment we will sample a scattering event along the path and test whether or not the media intersection occurred before a surface intersection. If the scattering event occurred in the media, radiance, Latmos(x,ω)L_{atmos}(x, \omega) , will be calculated based off of the participating media equations and parameters set for the globally defined homogeneous atmosphere. If the scattering event occurs at a surface, the radiance, Lsurf(x,ω)L_{surf}(x, \omega) ,for the path will be calculated as per the Kajiya rendering equation reference. The radiance for the path is then scaled according to the transmittance function for the atmospheric media along the path length. The following volume rendering equation adapts the Kajiya model to account for scattering events that occur within the globally defined atmospheric media.

L(x,ω)=0[tatmos<tsurf?Latmos(x+sω,ω):Lsurf(x+dω,ω)]σteσtdsL(x,\\omega) = \\int\_{0}^{\\infty}\\left[t\_{atmos} < t\_{surf}\\quad?\\quad L\_{atmos}(x + s\\omega, -\\omega)\\quad:\\quad L\_{surf}(x + d\\omega, -\\omega)\\right]\\sigma\_{t}e^{-\\sigma\_{t}d\_s}
Latmos(x,ω)=S2σsσtρp(x,ω,ω)L(x,ω)dωL\_{atmos}(x, \\omega) = \\int\_{S\_2}{\\sigma\_s\\over\\sigma\_t}\\rho\_p(x, \\omega, \\omega')L(x, \\omega')d\\omega'
Lsurf(x,ω)=Le(x,ω)+Sρr(x,ω,ω)L(x,ω)cosθdωL\_{surf}(x, \\omega) = L\_e(x, \\omega) + \\int\_{S}\\rho\_r(x, \\omega, \\omega')L(x, \\omega')|\\cos{\\theta'}|d\\omega'

3. Scene Construction

A sample scene was constructed for godRay where the interior of a faux classical temple interior receives illumination from a procedurally generated exterior sun and sky light model. The only light on the interior passes through a single occulus at the top of a dome centered above a sculpture.

3.1 Sun and SKy Lighting

The lighting for the sample temple scene uses two light sources procedurally generated from a Preetham Sun and Sky model reference. While the preetham model is not physically acurate, it provides a copmutationaly cheap method of calculating an infinite skylight that can be parameterized through a sun azimuth and sun altitude. The sunlight was modeled as an infinte directional light with a small radius based on the suns solid angle.

3.2 Temple Model Geometry

The temple geometry was modeled in NURBS and exported into an OBJ mesh format for import into the godRay rendering engine. A section of the temple scene geometry is diagramed in figure. Triangle intersection test are used in the BVH ray intersection accelerator. A Venus de milo mesh file was used as the central piece under the dome and occulus to allow for the god ray to cast onto. The mesh file is based on a 3d scan of the original sculpture reference.

Model of the temple scene

4. Monte Calro path tracing

The rendering equation for the godRay engine was solved through brute force Monte Carlo integration. Pixel samples were distributed across the CUDA cores and collected into an accumulation buffer. godRay uses progressive rendering without tiling, where pixel samples will continue to accumulate when running the application in OpenGL with the GUI, or can be specified though arguments if run without the GUI on the command line. Figure describes the general path tracing procedure. Direct illumination from the sun was sampled at all scatter events while the skylight contribution was included only in the indirect illumination path when an intersection miss occurred with the geometry.

Direct and Indirect Illumination paths

4.1 Monte Carlo Sampling

Sampleing of the of the global illumination path is done through russian roulette termination of the resursive ray depth. The technique used follows Veach's discription closly reference.

Sampling of the scatter events in the atmospheric media first require an intersection test of the ray with the scene geometry where tsurft_{surf} represents the parametric value of the intersection point along the ray. tatmost_{atmos} represent the sampled atmospheric scattering point along the same ray. tatmost_{atmos} derived from the equation describing the extintion in the media where ξ\xi is a uniformly distrubuted random variable.

tatmos=ln(1ξ)σtt\_{atmos} = {ln(1 - \\xi)\\over\\sigma\_t}

with a PDF

pt(t)=σteσttp\_t(t) = \\sigma\_te^{-\\sigma\_tt}

Sampling of the solid angle for surface scatter events uses cosine weighted hemisphere sampling so that more sampling occurs in areas of the hemisphere where more total irrandiance is likely to come from. The pdf is: p(x,ω,ω)=cosθ/πp(x, \omega, \omega') = {\cos\theta/\pi} . Sampling of the solid angle for non terminated indirect paths for atmospheric scatter events follows from solving the Henyey-Greenstien phase function for cosθ\cos\theta where given some uniformly distributed random variable ξ\xi

cosθ=12g(1+g2(1g21g+2gξ)2)\\cos\\theta = {1\\over2g}\\left( 1 + g^2 - \\left( {1-g^2\\over1-g+2g\\xi} \\right)^2 \\right)

Given that the phase function is normalized the PDF is the evaluation of the function ρp(x,ω,ω)\rho_p(x, \omega', \omega) .

When considering the direct illumination sampling of the sunlight is done through uniform disk sampling based off of the radius of the modeled sun.

4.2 Pathtracing in CUDA

Due to limits on the stack depth in CUDA, implementing the rendering equation for godRay in requires that the path tracing be done iteratively rather than recursively. This requires that the throughput of atmospheric attenuation, tr and global illumination path weight, beta, be separately tracked through each iteration of the main global illumination rendering loop. The algorithm described is written in generalized pseudo code, however you can refer to the CUDA source code for specific implementation syntax and details of all functions.

Main rendering loop in pathtracer.cu

// return radiance of the pixel sample float3 renderPixel(float2 pixel_index) { unsigned int seed; // Sample the Current Pixel float2 pixel_sample = samplePixel(seed); // Generate Camera Ray for current Sample float3 ray_origin; float3 ray_dir; genPinholeCameraRay(&ray_origin, &ray_dir, pixel_sample); // Per Ray Data Structure PerRayData_radiance prd; prd.beta = float3(1.0f); prd.tr = float3(1.0f); prd.radiance = float3(0.0f); // Next ray to be traced prd.origin = make_float3(0.0f); prd.direction = make_float3(0.0f); // Accumulated Radiance for the Sample float3 L = make_float3(0.0f); // Main Global Illumination Pathtrace for (int i = 0; i < max_depth; ++i) { // Generate Ray for current ray segment Ray ray(ray_origin, ray_dir, scene_epsilon); // Trace ray and intersect with scene geometry rtTrace(top_object, ray, prd); // rtTrace calls a new CUDA material kernel depending on ray intersection. This kernel is defined in material.cu // prd struct is updated on return from rtTrace // Add transmittance radiance from path segment to L L += prd.tr * prd.radiance; // russian roulette termination | pbrt 879 if (prd.depth > min_depth) { float q = 1.0f - prd.beta.y; if (q < 0.05f) q = 0.05f; if (rnd(prd.seed) < q) break; // update beta to account for russian roulette termination probability prd.beta /= 1.0f - q; } // Update ray data for the next path segment ray_origin = prd.origin; ray_dir = prd.direction; } return L; }

material.cu defines the routines for updating per ray data. All materials in godRay are Lambertian diffuse materials. Atmospheric scattering is accounted for as a conditional test in the material shader. If atmospheric scattering occurs, the intersection is updated and estimated direct lighting is computed according to the intersection type (atmospheric or diffuse).

// Lambertian Diffuse Intersection Kernel void diffuse_hit_radiance() { // Find the distance to the closest intersection const float3 fhp = ray.origin + t_hit * ray.direction; float isect_dist = length(fhp - ray.origin); // Determine if scatter occured in atmosphere if (atmos_scatter(prd_radiance.seed, isect_dist)) { // Handle Atmosphere scatter // ------------------------- // Update Intersection float3 isect = ray.origin + isect_dist * ray.direction; // Esitmate Direct Lighting from Sun prd.radiance = prd.beta * estimate_direct_light(isect, ATMOS, -prd_.direction); // Compute transmittance and atmopsheric PDF float3 transmittance = expf(-atmos_sigma_t * isect_dist); float3 density = atmos_sigma_t * transmittance; float atmos_pdf = (density.x + density.y + density.z) / 3.0f; // Set weighting for atmospheric transmittance prd.tr *= transmittance * atmos_sigma_s / atmos_pdf; // Sample solid angle of atmos scatter bounce float3 w_in = float3(0.0f); float p *= hg_sample_phase(atmos_g, -ray.direction, w_in); // Set next ray bounce prd.origin = isect; prd.direction = w_in; } else { // Handle scatting at point on diffuse surface // ------------------------------------------- // Compute the transmittance and sampleing density float3 transmittance = expf(-atmos_sigma_t * isect_dist); float atmos_pdf = (transmittance.x + transmittance.y + transmittance.z) / 3.0f; // Return weighting factor for scattering from surface and atmosphere prd.tr *= transmittance / atmos_pdf; // Calculate world surface normal const float3 ffnormal = faceforward(world_shading_normal, -ray.direction, world_geometric_normal); // Sample surface solid angle float3 bsdf_f = Kd / M_PIf; float bsdf_pdf; float3 w_in = cwh_sample(bsdf_pdf); // Set next ray bounce prd.origin = fhp; prd.direction = w_in; // Esitmate Direct Lighting prd.radiance = prd.beta * estimate_direct_light(fhp, DIFFUSE, -prd_radiance.direction); prd.beta *= bsdf_f * dot(ffnormal, w_in) / bsdf_pdf; }

5. Results

Images were generated for all of the parameters concerning the rendering of the god ray in the scene. The following image sequences describe the variation of parameter values and the effect on the scene image.

5.1 Extinction Coefficient

The extinction coefficient increases the probability of scattering, as well as increases the attenuation rate of rays as they pass through a media. In the figures below we can observe the scene becoming darker as the extinction coefficient increases.

Extinction Coefficient = 0.001
Extinction Coefficient = 0.002
Extinction Coefficient = 0.004
Extinction Coefficient = 0.008

5.2 Scatter coefficient

The Scatter coefficient affects the amount scattered into a differential segment of media. This is different than the out-scatter coefficient which is bundled together with the extinction coefficient. The in-scatter coefficient, as seen below, changes how much light is bouncing around within the scene. A larger scattering coefficient will produce a brighter image, as more light enters the dark room where the statue is.

Scatter Coefficient = 0.001
Scatter Coefficient = 0.002
Scatter Coefficient = 0.004
Scatter Coefficient = 0.008

5.3 Phase Function

The scattering phase function coefficient is the coefficient gg in Henyey-Greensteein's phase function. Observe the effect of gg as it increases from 0.1 to 0.99 and -0.1 to -0.99. Specifically, when the coefficient is close to 0 we observe isotropic scattering. A larger negative value produces backscattering while a larger positive value produces forward scattering. The effects of each can be observed when focusing on the face of the statue and the disk of light at its feet. Backscattering will illuminate the face while forward scattering will make the disk at the base brighter.

Scatter Phase Coefficient (G) = 0.1
Scatter Phase Coefficient (G) = 0.5
Scatter Phase Coefficient (G) = 0.99
Scatter Phase Coefficient (G) = -0.1
Scatter Phase Coefficient (G) = -0.5
Scatter Phase Coefficient (G) = -0.99

5.4 Helios coefficient

The helios coefficient is an atmospheric radiance amplifier we added to the monte carlo integration. This helps produce a sharper contrast from areas where scattering is not taking place as often. The below images demonstrates the effect, where the coefficient is set to 1 we have a nearly dark room as scattering events in the atmosphere contribute very little radiance. A higher helios coefficient will bring out the god ray effect, as scattering events become more apparent.

Helios Coefficient = 1
Helios Coefficient = 2
Helios Coefficient = 4
Helios Coefficient = 8
Helios Coefficient = 16
Helios Coefficient = 32
Helios Coefficient = 64
Helios Coefficient = 100

5.5 Sun elevation and rotation

We can also observe the effects of the god ray at various angles throughout the scene by rotating the sun and changing the sun's elevation. Observe the following video clips for pre-rendered scenes of the light changes.

6. Conclusion

Overall, this has been a fantastic project to work on. Accelerating ray tracing by means of a graphics card allowed us to achieve great rendering results. A computationally difficult problem becomes much more feasible, which resulted in a faster turn around with testing parameters. We were able to produce many different results, with high variation and low latency between tests. The following video demonstrates all of the features of the godRay software and the different effects that can be achieved.

7. Contributions

The authors would like to thank Professor Ren Ng of UC Berkeley’s Foundations of Computer Graphics course.

8. References

[1] Wojciech Jarosz. 2008. Efcient Monte Carlo Methods for Light Transport in Scattering Media. Ph.D. Dissertation. UC San Diego, La Jolla, CA, USA. Advisor(s). Henrik Wann Jensen and Mathias Zwicker.

[2] James T. Kajiya. 1986. Te Rendering Equation. SIGGRAPH Comput. Graph. 20, 4 (Aug. 1986), 143–150. htps://doi.org/10.1145/15886.15902

[3] A.J.Preetham, PeterShirley, andBrianSmits.1999. APracticalAnalyticModelfor Daylight. In Proceedings of the 26th Annual Conference on Computer Graphics and Interactive Techniques (SIGGRAPH ’99). ACM Press/Addison-Wesley Publishing Co., New York, NY, USA, 91–100. htps://doi.org/10.1145/311535.311545

[4] Dominique Toublanc. 1996. Henyey–Greenstein and Mie phase functions in Monte Carlo radiative transfer computations. Appl. Opt. 35, 18 (Jun 1996), 3270–3274. htps://doi.org/10.1364/AO.35.003270

[5] Eric Veach and Leonidas J. Guibas. 1997. Metropolis Light Transport. In Computer Graphics (SIGGRAPH ’97 Proceedings. Addison Wesley, 65–76.