
Implementing fast, ray-traced soft shadows in a game engine - alexvoica
http://blog.imgtec.com/multimedia/implementing-fast-ray-traced-soft-shadows-in-a-game-engine
======
cousin_it
This reminds me of Inigo Quilez's work on rendering soft shadows by using
distance fields.

The main idea is that you represent a 3D object using not triangles, but a
function f(x,y,z) stored on the GPU. The function should return the
approximate distance from point (x,y,z) to the object in question, so it's
zero on the object's surface and negative on the inside.

It's easy to construct such functions for spheres, boxes, etc. [1] It's also
easy to define combinators for moving, adding and subtracting different
objects, adding rounded corners, etc.

That representation is well suited for raymarching [2], because the value of
f(x,y,z) at your current position along the ray can be safely used as the
length of the next raymarching step, so you automatically get larger steps
when you're far from the object.

It's also well suited for rendering shadows with penumbras, because the
minimum value of f(x,y,z) as you march along the ray gives you the distance
between the ray and the object [3]. A slight modification of that trick can
give you ambient occlusion as well.

[1]
[http://iquilezles.org/www/articles/distfunctions/distfunctio...](http://iquilezles.org/www/articles/distfunctions/distfunctions.htm)

[2]
[http://iquilezles.org/www/material/nvscene2008/rwwtt.pdf](http://iquilezles.org/www/material/nvscene2008/rwwtt.pdf)

[3]
[http://iquilezles.org/www/articles/rmshadows/rmshadows.htm](http://iquilezles.org/www/articles/rmshadows/rmshadows.htm)

------
prewett
I like the article, but it seems to be missing a pretty major component: how
do I cast the ray? It seems like the hardware won't have the geometry
available so I can't do it on hardware. (What if this is the first pixel
rendered? and even if it does store up all the geometry, OpenGL shaders don't
have a ray casting call) And if I do it in software, I can't use a vertex
shader (unless I duplicate what it is doing, which kind of defeats the
purpose).

~~~
s_tec
This is a marketing piece for their new GPU, which has ray casting built into
the silicon. It's not clear how to access this new hardware (some OpenGL
extension?), but they do mention that their chip gives shaders the ability to
cast rays.

~~~
alexvoica
Yes, we are using ray tracing extensions implemented for OpenGL.

You can find the sample code here [http://cdn.imgtec.com/OpenRLSDK/OpenRL-
Hybrid-Example.zip](http://cdn.imgtec.com/OpenRLSDK/OpenRL-Hybrid-Example.zip)

~~~
Arelius
Any chance Imagination is considering a Workstation-level product? I
understand that mobile is your money maker, but if the hardware is as fast as
it sounds, there are some roles that might be really nice to be able to
offload onto this sort of hardware.

And how well is the hardware currently handling highly divergent rays? Such as
those used in GI or reflections?

~~~
alexvoica
The PowerVR Wizard architecture scales from mobile to desktop/console. We will
be bringing out dev kits in the near future that will deliver very (very) high
performance.

~~~
Arelius
Awesome, I'm very (very) interested.

------
sonium
The author chose an outside scene with blue sky as a demonstration. This is
exactly where one in reality does NOT have soft shadows due the the small
apparent size of the sun. A indoor scene with an extended light source would
have been a better choice.

~~~
Florin_Andrei
I disagree - and it would be perhaps helpful to actually go outside and watch
physical reality a little bit for tons of counter-examples. Yes, the gradient
at the edge of shadows is washed out by the "flood light" of the sky, but it's
still visible. This is very obvious with shadows from tall objects such as
buildings or towers - just go to the edge of the shadow and observe.

------
MakeMake
This technique is limited to "unrealistic" point lights (except for stars). To
improve the shadow quality it is necessary to work with light fields. This can
be done either by voxel fields (I think nvidia tried something like this) or
depth fields. I use the later one with the advantage that it can be easily
combined with other techniques and can be updated in real time. For almost
static environments like the one in the example I would advise to use
precomputed emission maps. There is also something wrong with the shadow map
(right image) I suspect a blur step is missing.

~~~
bhouston
It is just as unrealistic as the blurring that happens in Cascading shadow
maps but it is more efficient.

------
zamalek
The original article got me all hot and flustered and this one didn't fail to
impress either. I just wish we had widespread access to this kind of pipeline
on the desktop (alongside conventional shader units).

Edit for those that are out of the loop: shadows are notoriously finicky.
There are a bunch of approaches to them that, while a heroic effort, suck.
They all compromise on different things and yet fail to be really good at what
they are supposed to be perfect at. You can spend a week tweaking constants
only to get something passable for your engine. They are the bane of an engine
dev's life.

This approach is so clean and, in theory, comes very close to a one-size-fits-
all-golden-hammer. Not perfect, but worlds apart from what games are doing
today. Catch is: this specific clean implementation of raytraced shadows can
only be done with PowerVR (AFAIK).

------
aarw
Looks like Caustic's technology at work:

[http://www.techeye.net/chips/imagination-to-buy-caustic-
grap...](http://www.techeye.net/chips/imagination-to-buy-caustic-graphics-
for-27-million)

~~~
alexvoica
Yes, we've integrated Caustic's ray tracing tech into the PowerVR
architecture.

------
otis_inf
Can't you draw the shadow as a blended polygon where you gouraud shade from
full opaque to full alpha the penumbra area? (so you thus have to draw
triangles in that area)? This will produce soft shadows (due to the shading)
and will take not much calculations (per vertex in the model casting the
shadow, you have to cast its shadow point on the surfaces the shadows are
casted at from the lightsource which isn't a point anymore (so you get the
penumbra, like in the image in the article)

Not sure if this will work, I thought about this years ago when I was still in
the demoscene but never had the time to implement it.

~~~
Arelius
With shadow buffers this will not work, since you are not in fact rendering a
shadow mask, but instead a depth buffer with distance-to-occluder, which
cannot be blended as it'd be entirely incorrect.

Even if you could, the penumbra size is a function of not only the light size
but also the distance from the shadowed surface to the occluding edge, which
is fundamentally something you cannot know during shadow occluder rendering
time.

------
megrimlock
Neat stuff. It's worth noting that shadow rays don't entirely avoid precision
issues either. You still need to handle self-intersections. One option is to
add a bias epsilon (which introduces other artifacts, depending on the scale);
another is to reject local self-shadowing (which means you can't get finely
shadowed surface cracks); another is to give artists explicit control over
which sets of objects shadow which others (which adds artist time). Offline
rendered movies use all of these.

~~~
kayamon
Generally speaking, the biases needed for ray-tracing are much smaller than
the biases needed for shadow mapping. Ray-tracing tests directly against the
geometry so only needs a bias proportional to the numerical accuracy used,
whereas shadow mapping requires bias proportional to the texel resolution of
the map.

------
alexvoica
We've posted a follow-up article where we analyze further optimizations and
compare memory traffic and rendering speed for ray traced shadows vs. cascaded
shadow maps: [http://blog.imgtec.com/multimedia/ray-traced-shadows-vs-
casc...](http://blog.imgtec.com/multimedia/ray-traced-shadows-vs-cascaded-
shadow-maps)

------
s_tec
How does one actually access this special ray-tracing hardware? Some sort of
OpenGL extension? I looked around, but couldn't find any answers. None of
their press seems targeted at developers.

~~~
kelvin0
I don't think there is any 'special ray-tracing hardware' they are simply
leveraging the existing GPU with some algorithms and be able to to have
faster, better looking shadows compared to the usual algos. Someone correct me
if I'm wrong.

~~~
s_tec
Their new GPU comes with something called an "RTU", which accelerates the ray
intersections. This article is showing off one way to use their fancy new toy:
[http://blog.imgtec.com/powervr-developers/powervr-
gr6500-ray...](http://blog.imgtec.com/powervr-developers/powervr-gr6500-ray-
tracing)

~~~
kelvin0
I stand corrected. Thanks!

