Ray tracing Developers

Implementing hybrid ray tracing in a rasterized game engine

Picture of Imagination Technologies
May 6, 2014  |  4 min read

For the past month, many of you have been asking on social media or in the comments section of our blog for a video recording of our hybrid rendering demo. Since some of our readers have missed our GDC 2014 sessions or didn’t get a chance to drop by and see our PowerVR Ray Tracing demos running at our booth, I have asked my colleagues in the PowerVR Ray Tracing group to put together short clips that present the work they’ve done to highlight some use cases for ray tracing.

The first example we’ll be analysing today is the hybrid rendering demo. Before we begin, there is a caveat: please keep mind that this demo is running on a previous generation of the PowerVR Ray Tracing technology. The PowerVR GR6500 ray tracing GPU we’ve announced at GDC 2014 is designed to be integrated in a System-on-Chip and therefore can provide much better raw performance (a higher ray budget) and tighter/faster memory coupling while consuming lower power. Therefore the performance, framerate and resolution in the video isn’t representative of the PowerVR GR6500 or any other Wizard GPU.

 

 

The hybrid rendering demo running on our PowerVR Ray Tracing IP, as shown at GDC 2014

For this ray tracing demo, we’ve started with an experimental version of the Unity game engine and implemented OpenRL support which enables us to use the G-Buffer from the rasterizer to initialize rays (see my recent Gamasutra article for more details). Then, the results of the ray tracing are used by Unity’s final lighting pass and fed right back into the game engine.

Ray tracing alongside rasterization opens up many possibilities and enables effects that are either difficult or impossible to do without artefacts using raster techniques alone.

Hard and soft shadows

The first scene presents hard shadows. We are using the properties of the G-Buffer (world-space positions, normal and material IDs) to cast shadow rays. Zooming in on the tree in this scene reveals how every leaf is casting and receiving a very accurate shadow.

Hybrid ray tracing in a rasterized game engine - hard shadows (2)Ray tracing can be used to generate very accurate hard shadows

The second example in this scene introduces a similar situation where the complex geometry of the bridge will cause resolution issues when casting shadows in a traditional game engine. However, ray tracing doesn’t have any resolution problems because the rays are cast for each screen pixel.  Developers do not have to worry about selecting the appropriate shadow map resolutions for various positions on the game level, and rigorously testing the game looking for artefacts. Ray traced shadows look realistic from afar (check out the beginning of the scene) and maintain fidelity all the way to the end. We can go as close to the wall as we want and we will never run out of shadow map resolution.

Hybrid ray tracing in a rasterized game engine - hard shadows (3)Ray traced shadows will not have resolution issues

The next scene focuses on soft shadows. While hard shadows simulate sunlight on a clear day at noon, soft shadows appear when the light source has area (e.g. a lamp with a shade) or there is a medium to scatter the light between the light source and the scene (e.g. clouds). In our hybrid rendering example, by simply adding some random noise to the shadow ray directions, we can obtain the appearance of a cloudier day. This technique simulates the effect where the penumbra size is dependent on the distance between the occlude and the object receiving the shadow. In the video example, the head of the lamp has a very soft shadow while the base retains its hard edges, just like in real life. This would be very difficult to achieve using conventional techniques.

Additionally, implementing ray traced shadows in game engines enables you to have many shadows casting lights because sampling decisions are made on a pixel-by-pixel basis. In the scene above, half of the pixels don’t need to sample the headlights of the car because those lights can be rejected by cheap cone-test. There is no need for a separate pass to handle each dynamic shadow casting light.

Reflections, refractions and transparency

Another use case for ray tracing is reflections, which can be handled in a similar fashion to shadows. Instead of directing the ray towards a known light source, we send the ray in the direction calculated by performing the mathematical reflect function between the vector from the viewpoint and the normal vector at the surface. Ray traced reflections have several advantages over raster techniques like reflection maps.  Not only do they reflect dynamic objects without needing separate rendering passes, nearby objects and self-reflections are handled perfectly.

Hybrid ray tracing in a rasterized game engine - reflectionWhen using ray tracing in your game engine, reflections look crisp clear

Additionally, transparency is also handled by casting rays. In our example, the windshield is tinting the rays and continuing them on to provide order-independent transparency. If you look closely at the car, you can notice we have implemented both transparency and reflections techniques for the windshield glass material; this is easy to do and produces a realistic effect where the trees are visible in the glass. The proportion of the blending between the reflection and the transparency takes into account the Fresnel term calculated from the viewing angle, and produces a convincing model of real life.

Another example of ray traced transparency is the fence. It combines transparent objects with shadows to achieve an interesting visual effect. The fence is made of quads with an alpha texture applied to create the wire mesh. Because the rays respect the texture’s mip level, the shadow just looks like a faint tint from a reasonable distance. Once we get closer, the texture is starting to come out and the shadows gradually become more detailed, right up to the resolution limit of the wire mesh texture itself.

Hybrid ray tracing in a rasterized game engine - hard shadowsIn ray tracing, you can combine transparent objects with shadows

A texture can also be used to tint a ray traced shadow for a quick and easy way to get perspective textures. In our example, this effect is noticeable on the pavement as the light cast from inside the diner is combined with the window decal to produce a coloured shadow.

Finally, this wouldn’t be a ray tracing demo if we didn’t show some refraction. The open sign visible through the neck of the bottle or the bottle’s label is visible through the mug. We apply the refraction equation to bend the ray based on the difference of the index of refraction between glass and air, and then apply it again in reverse when the ray exits the material.

Hybrid ray tracing in a rasterized game engine - refractionRefraction and glossy reflections

Glossy reflections also work with this hybrid method. The normal map on this table means that the reflection rays are scattered a little bit. We can faintly see the reflection of the open sign and the beer label in the table, although it is too fuzzy to make it out clearly.

Final words

In conclusion, we’ve seen how adding just a few rays-per-pixel to a rasterized game engine can add a lot of effects that would be difficult or impossible to do with rasterization alone. Ray tracing can do a better job approximating the physical light transport phenomena, and using ray traced effects can avoid the artefacts of complicated raster techniques and often work out to be even be more efficient.

Thank you for watching! I hope this article has come a little way towards quenching your thirst for ray tracing demos. If you have any suggestions or questions, please don’t hesitate to leave a comment in the box below.

Come back to our blog for a second article where we focus on another example where PowerVR Ray Tracing is used inside the Unity 5 lightmap editor and make sure you follow us on Twitter (@ImaginationTech) for the latest news and announcements on PowerVR and ray tracing.

Share this post

About the Author
Picture of Imagination Technologies

Here at Imagination, our vision is to be the undisputed leader in semiconductor IP solutions that transform billions of lives.

More from Imagination Technologies

Read Next