Why is rendering shadows so slow?
From my understanding GPUs can rasterize polygons and fill extremely fast.
Isn't a shadow just a projected surface (from the perspective of the light source) on everything? Is it rendered once at the beginning or do most game engines render shadows every frame?
Posted Mar 31, 2019, 4:24 AM UTC
46
Yea typically nowadays shadows are rendered using "shadow maps", which basically just means we render a depth map from the perspective of the shadow casting light. Then, when we render the lighting, we can transform a pixel from world space back into that shadow map's space. There, we can compare its transformed depth value to the depth value stored in the shadow map to determine if the surface is in shadow or not.
If your light source isn't moving, then you most likely will not need to re-render your shadow maps the next frame. However, if there are moving objects present in the scene that you want casting shadows, you will have to either A) render a separate shadow map just for the moving objects (faster), and then use two shadow maps (moving and non-moving) for the shadow test (a little bit more expensive). Or B), re render the whole shadow map to include the objects that moved (expensive).
There's lots of tricks to make this less expensive, like prioritizing which shadow maps render on a frame based on how much of the screen they take up, (eg ones in the background can probably get away with not updating every frame), or changing the resolution of shadow maps over distance (cascaded shadow maps).
I hope this has shed some light on how shadows are rendered, though this was just a super simple explanation. There's lots of complexity involved (eg what resolution should the shadow map be? How do you do transparent shadows? Can you make shadows softer as they get further away? What about ray tracing? etc).
How fast is it to render a decal? If you project a decal on a mushroom for example. It covers the ground around the mushroom, not under the cap, and on top of the cap.
Is this possible?
Or does the decal just wrap around the entire mushroom and look really shitty. No trimming.
Yeah I was playing Prey. The beginning scene was using the sun at a set time of day but the shadow was super shitty. Couldn't they just render it as a static shadow map at the beginning in higher quality? It didn't go far into the horizon at all.
Or they could have just baked fake shadows into the objects. The sun doesn't move. It wouldn't matter. It's just a 3D diorama not even playable area.
edit: reread my first part. didn't make sense. removed.
When rendering a light, you can think of the shadow map as being a representation of how far the light can go before it hits something. Transforming the fragment into light space and comparing the depth doesn't take very much time at all. The most expensive step is the actual rendering of the shadow map, since it requires not only rendering many objects, but also the setup required every time you render something (eg calculating which objects are visible)
[deleted]
Okay. What if it wasn't a shadow but a "light map decal".
Like this? The yellow part is the "light map" and everything is default dark or something.
https://www.opengl-tutorial.org/assets/images/tuto-16-shadow-mapping/shadowmapping.png
That's exactly how it works! :) when lights are rendered, their resultant value is added to a light buffer. So it's not really rendering shadows as much as it is just accumulating light where the shadows aren't.