Drawing
Shadows of spot and point lights
A spot light and a point light sit at one point and shine outward. Their shadow maps are drawn by a perspective camera at the light: one camera along the cone for a spot light, six cameras in a cube for a point light. This page explains the cone, the cube map and how a direction picks its face, why the cube's cameras are mirrored, and why the bias must grow with distance. It builds on Shadow mapping. The last part shows how RASM does it.
In short
- Rays leave the light from one point, so the light's camera is a perspective camera at the light, not an orthographic box.
- A spot light has a cone: full light inside the inner angle, a smooth fade to 0 at the outer angle. Its shadow is one square map along the cone's axis, with a field of view of 2 × the outer angle.
- A point light shines everywhere. Its shadow is a cube map: 6 faces, each a 90° view. A direction belongs to the face of its biggest axis, and the depth to compare is the distance along that axis.
- The cube faces' cameras are mirrored, so the face passes swap back-face and front-face culling.
- In a perspective map a texel gets bigger with distance, so the bias grows with distance too.
1The problem: rays from one point
The sun is far away, so its rays are parallel and an orthographic box fits them. A lamp is close. Its rays fan out from one point, so the camera that draws its shadow map must fan out the same way.
Spot
One perspective map along the cone's axis (sections 2 and 3).
1 map
Point
A cube map: six perspective maps of 90°, one per face (sections 4 to 6).
6 maps in one cube
2The spot light's cone
A spot light shines inside a cone around its direction. Two angles, measured from the axis, shape it: inside the inner angle the light is full; between inner and outer it fades; outside the outer angle there is no light.
The rule is the one of the glTF lights extension (KHR_lights_punctual). Two numbers per light, worked out once on the CPU, turn the cosine into the cone factor:
cone = saturate(cos θ · scale + offset)
scale = 1 / max(cos(inner) − cos(outer), 0.001)
offset = −cos(outer) · scale
light = color · intensity · cone² / distance²
- cos θ
- dot(light direction, −l): the cosine of the angle between the axis and the ray to the point
- saturate
- clamp to 0..1: 1 at cos(inner), 0 at cos(outer)
- 0.001
- keeps the division finite when inner and outer are equal: the fade becomes a hard edge
- distance²
- the usual inverse-square falloff of a light at a point, often with a smooth window that ends at the light's range
3The spot light's shadow map
A spot light needs only one map. Its camera sits at the light and looks along the axis. The field of view is 2 × the outer angle, so the cone just fits. The map is square, so the cone is the circle inside the square.
Try the cone and its map. The light is at the top and shines down. Drag the point P, and change the two angles.
4The point light's cube map
A point light shines in all directions. No single perspective camera sees everything: a field of view must stay well below 180°. Six cameras of 90° do it, one along each axis: +X, −X, +Y, −Y, +Z, −Z. Their images are the six faces of a cube map, one texture with six square layers.
The GPU reads a cube map with a direction r, not with texture coordinates. Vulkan's cube map face table says which face and which texel a direction falls on. ma is the biggest of r's three parts, the major axis:
| Biggest part | Face | Layer | sc | tc | ma |
|---|---|---|---|---|---|
| |rx|, rx > 0 | +X | 0 | −rz | −ry | rx |
| |rx|, rx < 0 | −X | 1 | +rz | −ry | rx |
| |ry|, ry > 0 | +Y | 2 | +rx | +rz | ry |
| |ry|, ry < 0 | −Y | 3 | +rx | −rz | ry |
| |rz|, rz > 0 | +Z | 4 | +rx | −ry | rz |
| |rz|, rz < 0 | −Z | 5 | −rx | −ry | rz |
s = (sc / |ma| + 1) / 2 0 at the left edge, 1 at the right edge
t = (tc / |ma| + 1) / 2 0 at the top row, 1 at the bottom row
5Reading the cube: face and depth
To test a point P, take the vector from the light, d = P − light. Its biggest part picks the face. The 90° faces meet at the cube's diagonals, so the face is the one whose axis is nearest to d.
Compare the distance |d|
3.13 > 3: P seems behind the wall itself and shadows itself. The error grows toward the face's edges, up to √3 at the corners.
Compare the depth along the axis
3 = 3: P is the first surface, so it is lit, like every other point of the wall.
Some engines write the distance |d| into the faces from a fragment shader, and compare |d| when reading. That works too. The two sides must only agree. Storing the depth the hardware writes needs no fragment shader in the shadow pass.
Try it. The picture is the slice through the light at its height, seen from above. Drag P, and move it up or down with the height slider: when |d.y| is the biggest, P falls on the +Y or −Y face.
6Drawing a face: a mirrored camera
To draw a face so that the lookup above finds each depth, the face's camera must put texel (s, t) where the table expects it. So the camera looks along the face's axis F, its image's right is S (where s grows), and its image's up is −T (t grows down). That camera is not a normal one:
+X face: F = (1, 0, 0) S = (0, 0, −1) T = (0, −1, 0)
rows: S = (0, 0, −1) −T = (0, 1, 0) −F = (−1, 0, 0)
det = (S × −T) · (−F) = (1, 0, 0) · (−1, 0, 0) = −1
A mirror reverses the winding of every triangle on screen (Face culling, section 5). Through a face's camera, the triangles that face the light turn clockwise and look like back faces. The fix is cheap: in a face's pass, swap the cull mode.
Cull back faces, as usual
The side toward the light is culled. The map holds the far side of closed objects, and open meshes (a plane, a leaf) cast no shadow.
Swap the cull mode
Cull the faces the GPU calls front: through the mirror, they are the real back faces. The faces toward the light are drawn, as in every other view.
Do not keep a list of mirrored views by hand. A view is mirrored when the determinant of its 3×3 part is negative. Test that once per view, and swap the culling when it is.
7The bias grows with distance
The fixes for shadow acne (Shadow mapping, section 5) are best measured in texels. In an orthographic map, a texel has the same size everywhere. In a perspective map it does not: the texels' rays fan out, so one texel covers more of the world the farther it is from the light.
So compute the texel's size at the point being shaded:
texel ≈ 2 · depth · tan(fov / 2) / size
cube face (fov = 90°, tan 45° = 1): texel = 2 · depth / size
from the matrix: texel = 2 · w / (|row 0 of M| · size)
- depth
- the point's distance from the light along the map's axis
- fov
- the map's field of view; size its width in texels
- M
- the light's view-projection matrix. The length of its first row is the projection's x scale: 1 / tan(fov / 2) for a square perspective map, 2 / box width for an orthographic one
- w
- the point's clip-space w: 1 in an orthographic map, the depth from the light in a perspective one. So the last line is right for both kinds
Then the lookup point leaves the surface by about one texel, in two directions:
p' = p + n · texel · (1 + 2 · (1 − n·l)) + l · texel
- n · texel · …
- along the normal: 1 texel where the surface faces the light, up to 3 where the light grazes it
- l · texel
- toward the light: 1 texel
Both terms use the texel at p, so a far point moves more than a near one. For a cube, depth is the biggest part of d, the depth on the point's face.
8Soft edges across faces
PCF (Shadow mapping, section 6) reads a block of texels around the lookup. Near a face's edge, part of that block is on the next face. A cube map handles this by itself when it is read with directions: GPUs sample cube maps seamlessly, so a filter at a face's edge reads the neighbour face's texels. Those texels hold the depth along the neighbour's axis, so each part of the filter compares with the point's depth along the axis of the face it reads. Near the edge the two depths are almost equal, but not quite, and comparing with the wrong one gives a thin line of light or acne along the cube's edges.
Each face as a 2D image
The block stops at the face's edge. The result jumps there: a seam along every cube edge.
Read through the cube
Turn each gather's texel corner back into a direction and sample the cube: the block crosses the edge with no seam.
9Cost: renders per light
Each map is one more render of the scene's depth. A point light is the most expensive light to shadow.
Sun, 4 cascades
One orthographic render per cascade.
4 renders
Spot light
One perspective render along the cone.
1 render
Point light
One 90° render per face.
6 renders
Six renders do not mean six times the triangles. Each face is culled with its own 90° frustum, so an object is drawn only by the faces that see it: most objects fall in one or two faces. What does add up six times is the fixed cost of a view: its culling pass, its draw calls and its barriers.
| Way to make it cheaper | Idea |
|---|---|
| One pass for all faces | Cull once against all six frusta and draw into the six layers in one pass (layered rendering or multiview). |
| Smaller maps for far lights | A light that covers few pixels on screen needs fewer texels. |
| Update less often | When nothing near a light moves, last frame's cube is still right. |
| Skip faces | A light on a wall or a ceiling has faces that see only the wall. |
10Worked example
A point light with a 1024 × 1024 cube, and a point P at d = (3, 0.4, −1.4) from it:
So the normal offset moves the lookup by 0.006 units where the surface faces the light, and up to 0.018 where the light grazes it.
A spot light with an inner angle of 14°, an outer angle of 22° and a 2048 × 2048 map, and a point 18° off the axis, 10 units along it:
With an outer angle of 80°, the same texel is 14 times bigger: tan 80° / tan 22° = 14.
11Pitfalls
- Acne that grows toward the cube's edges and corners
- The lookup compares the distance |d| against a depth stored along the face's axis. |d| is up to √3 times larger at a corner. Compare the depth along the face's axis, or store distances.
- A point light's shadows vanish, or objects shadow themselves
- The faces' mirrored cameras were not handled: back-face culling removes the faces toward the light. Swap the cull mode when the view's determinant is negative.
- Shadows are mirrored, or sit on the wrong side of the light
- A face's camera does not match the cube map table, so a face is drawn turned or flipped. Build each camera from the table's S and T, and test every face against the table.
- An object very close to the light casts no shadow
- It is nearer than the near plane, so it is clipped away. Make the near plane small; reversed-Z keeps the depth precise down to it.
- A very wide spot light has blurry, blocky shadows
- A field of view near 180° spreads the map's texels thin: tan(fov / 2) grows without end. Limit the field of view, and accept that light outside the map is not shadowed; or use a cube for a very wide cone.
- Acne far from the light, peter-panning near it
- The offsets are fixed in world units. In a perspective map, compute them from the texel size at the point.
- Seams along the cube's edges
- The PCF reads each face as a 2D image. Read the cube with directions, so the GPU filters across faces.
12In RASM
RASM shadows spot and point lights with the same two calls as the sun. Renderer::addShadowPasses takes a ShadowDesc for any light and returns a SceneShadow; Renderer::addScenePasses takes up to MAX_VIEW_SHADOWS (4) of them, each of a different light. For a spot or point light, ShadowDesc::distance is the far plane, measured from the light, and the near plane is that distance / 200. Each map is drawn by a depth-only scene view, so it is culled on the GPU like any view, in ordinary render graph passes. examples/07_lights shows it: a weak moon with cascades, a spot lamp and a point lantern, with a spinning cube whose shadow moves. A View lists the same ShadowDescs in View::shadows; each view draws a spot map or a cube from its own camera, since the maps take that camera's LODs, so a cube costs six scene views per view that lists it (examples/22_c_api gives its main view the point light's cube).
const rasm::LightHandle lamp = engine.createLight(rasm::LightType::SPOT, glm::vec3(1.0f, 0.95f, 0.8f), lampPosition, lampDirection, 900.0f, 40.0f, glm::radians(14.0f), glm::radians(22.0f));
const rasm::LightHandle lantern = engine.createLight(rasm::LightType::POINT, glm::vec3(1.0f, 0.6f, 0.3f), lanternPosition, glm::vec3(0.0f), 25.0f, 14.0f);
const rasm::SceneShadow shadows[] = {
engine.addShadowPasses(graph, scene, camera, rasm::ShadowDesc{.light = lamp, .size = 2048, .distance = 40.0f}),
engine.addShadowPasses(graph, scene, camera, rasm::ShadowDesc{.light = lantern, .size = 1024, .distance = 14.0f}),
};
engine.addScenePasses(graph, scene, camera, sceneTarget, shadows, 2);
The cone angles are in radians. The outer angle's default is SPOT_OUTER_CONE_ANGLE (π/4, glTF's default), and Renderer::createLight rejects a spot light unless 0 ≤ inner < outer ≤ π/2.
AttachmentOps::firstWrite); the later face passes keep the faces already drawn.Where RASM follows this page, and where it differs
| Part | This page | RASM |
|---|---|---|
| Cone | glTF's inner and outer angles, cone² | The same. setSpotCone computes spotScale and spotOffset on the CPU every frame; shadePBR multiplies the light by cone², after the inverse square and the range window. |
| Spot map | one perspective map, fov 2 × outer | One depth-only render target shadow_spot_<light index>, ShadowDesc::size squared. getSpotShadowView (a world-space glm::dmat4, from the light's double position; each view moves it into its render space) and getSpotShadowProjection build its matrices; the field of view is clamped to at most 160° (MAX_SPOT_HALF_ANGLE), so light more than 80° off the axis is not shadowed. |
| Near plane | small, behind every caster | distance / 200 (NEAR_PART), with reversed-Z, like every RASM projection. |
| Cube | 6 faces in the API's order | One cube render target shadow_cube_<light index>, made with RenderTargetDesc::cube. Its depth texture has TextureDesc::cube: CUBE_FACES (6) layers, a cube view for sampling and one 2D view per face for drawing. Each face's pass draws one face with PassBuilder::write(target, face). |
| Face cameras | from the cube map table | getCubeFaceView builds rows S, −T, −F from the tables CUBE_FORWARD, CUBE_S, CUBE_T; getCubeFaceProjection is the shared 90° projection. tests/localShadowsTests.cpp checks every face against Vulkan's table. |
| Mirrored faces | swap the cull mode | SceneView::mirrored is set when the view's 3×3 determinant is negative, and Renderer::drawSceneView swaps CullMode::BACK and CullMode::FRONT. |
| Depth compared | along the face's axis | The depth the hardware writes. sampleShadowCube turns the depth on the face's axis into clip depth with the face projection, which is the map's matrix for a point light. |
| Bias | about one texel, from the texel size at the point | offsetLookup: along the normal 1 to 3 texels, and 1 texel toward the light. sampleShadowMap takes the texel from 2 · w / (|row 0| · size), right for cascades and spot maps; sampleShadowCube from 2 · depth / size. |
| Filtering | PCF through the cube, seamless | 3x3 PCF with bilinear weights as four 2x2 Gathers (getPcfWeights). For the cube, each gather's corner becomes a direction into gCubeTextures, a SamplerCube array on the same binding as gTextures, and each gather compares with the depth along the axis of the face it reads (getCubeFace of the corner, getCubeDepth). |
| Cost | 1 render per spot, 6 per point light | One scene view per map: a sun with 4 cascades, a spot light, a point light and the shaded view take 12 of the MAX_SCENE_VIEWS (32). Drawing all faces in one view is D20 in docs/code_review.md. |
Where it lives in code
| What | Where |
|---|---|
| The API | Renderer::createLight in src/scene/light.cpp; SPOT_OUTER_CONE_ANGLE in include/rasm/light.h; ShadowDesc, SceneShadow, MAX_VIEW_SHADOWS, MAX_SCENE_VIEWS in include/rasm/scene.h; Renderer::addShadowPasses, Renderer::addScenePasses in src/renderer/gpuScene.cpp |
| The maps' matrices | getSpotShadowView, getSpotShadowProjection, getCubeFaceView, getCubeFaceProjection in src/renderer/localShadows.cpp; getShadowMaps in src/renderer/gpuScene.cpp |
| The views, mirrored culling | Renderer::createSceneView, Renderer::prepareSceneView, Renderer::drawSceneView in src/renderer/gpuScene.cpp; SceneView in src/renderer/gpuScene.h |
| Cube targets and textures | CUBE_FACES, TextureDesc, RenderTargetDesc, AttachmentOps in include/rasm/render/resource.h; the image and its views in src/rhi/vulkan/vulkanDevice.cpp |
| Drawing one face, keeping the others | PassBuilder::write in include/rasm/render/renderGraph.h; RenderGraph::assignAttachmentOps in src/renderer/renderGraph.cpp |
| GPU fields | gfx::ShadowMap, gfx::Light, SceneData::shadowMaps in src/renderer/uberMaterialTypes.h and shaders/lib/core.slang |
| The lookup | sampleShadow, sampleShadowMap, sampleShadowCube, offsetLookup, getPcfWeights in shaders/lib/shadow.slang; getCubeFace and the cube tables in shaders/lib/cubeMap.slang, which the environment bake shares; gCubeTextures and fragMain in shaders/passes/rasm.slang |
| The cone, applying the shadow | shadePBR and PBRInfo::shadows in shaders/lib/pbr.slang |
| Tests | tests/localShadowsTests.cpp |
| An example | examples/07_lights/main.cpp |
CUBE_FORWARD,CUBE_SandCUBE_Texist twice, insrc/renderer/localShadows.cppandshaders/lib/cubeMap.slang. They must stay equal and match Vulkan's cube map face table;tests/localShadowsTests.cppchecks the C++ side.- A mirrored view must swap its culling.
SceneView::mirroredcomes from the view matrix, so a new kind of view gets it for free; a pipeline that sets its own cull mode does not. - A cube render target is depth-only and square, with a fixed size.
addScenePassesdoes not take one as its color target. - Only the cube's first pass of the frame may drop its contents (
AttachmentOps::firstWrite). One barrier covers the whole cube, so the later face passes must keep the faces drawn before them. - A point light's
gfx::ShadowMapholds only the faces' projection. The shader builds the face's view from the light's position, so it must use the same tables asgetCubeFaceView. - Every index into
gTexturesandgCubeTexturescomes from the scene data and is the same for every pixel of the draw.
Further reading
- The Vulkan specification, section "Cube Map Face Selection" (in "Image Operations"): the face table used on this page.
- Khronos, the glTF extension KHR_lights_punctual: the spot light's cone and its falloff.
- Microsoft, "Common Techniques to Improve Shadow Depth Maps" (Windows DirectX documentation).
- Elmar Eisemann and others, Real-Time Shadows (2011): omnidirectional shadow maps.
- RenderMan's cube face convention, which the APIs inherited: the reason the table is left-handed.