TWO ALGORITHMS OF THE FIREFLY FOREST DEMO This demo shows a night forest lit by up to 255 small moving lights (fireflies) and one large light (the moon). Two algorithms keep that scene fast. One answers "which lights touch this pixel?". The other answers "how much detail is this light's shadow worth?". Both spend the frame's budget where the eye is, and skip it where the eye is not. 1. THE LIGHT INDEX TEXTURE THE PROBLEM The renderer draws the scene in a single pass (a "forward" renderer). For every pixel on the screen it must add the light from every light that reaches that pixel. With 255 lights the naive cost is 255 light tests per pixel. On a 1000 by 800 screen that is over 200 million light tests per frame. That is too slow. THE IDEA Each firefly's light fades out at a fixed distance. Its reach is a ball in space. A small patch of screen (a "cell") is reached by only a few of those balls, and the same few reach every pixel in the cell. So the decision can be made once per cell, not once per pixel. THE ALGORITHM - Split the screen into a grid of 128 by 128 cells (16,384 cells). - Once per frame a small GPU pass fills a 128 by 128 texture. Each texel of that texture holds the answer for one cell. - For one cell, the shader takes the ray from the camera through the center of the cell and tests the ray against every light's ball (a ray-sphere test). The lights whose balls the ray hits are the lights that can reach the cell. - The shader stores two numbers: the smallest light index that hits, and the largest. THE USE The world pass draws the forest. For each pixel it looks up the pixel's cell in the texture and reads the two numbers. It then loops over the lights between those numbers (a "range"). For each light in the range it does the real test: is this pixel inside this light's ball? A light that is in the range but too far away for this pixel fails that test and costs almost nothing. WHY MINIMUM AND MAXIMUM, NOT A LIST The common version of this idea — the one used by Forward+ and other forward renderers — stores a list of light indices per cell. A list needs storage, a maximum length per cell, and a rule for what happens when a cell holds more lights than the list can take. Storing only the smallest and largest index drops all three. The price is the "holes": lights that sit inside the index range but do not reach the cell. In this scene the ranges stay short (the debug overlay saturates at 64 lights wide and rarely gets close), so the price is small. A brute mode (key B) skips the texture and tests all lights for every pixel. It is slower. It exists as the reference side of the A/B comparison. WHY THE INDEX TEXTURE WORKS The first reason is that the decision is nearly exact. A firefly's light is a ball 4 to 8 meters in radius: seen from the camera it covers a limited part of the screen. A cell is a few pixels, so the lights that touch a cell are the same for every pixel in it, and one test per cell answers the question for the whole cell. The second reason is that the window is tight. The lights are numbered by screen position (a clump sort), so the candidates for one cell sit in a short run of numbers. The minimum and the maximum wrap the lights that matter, and the few extras in between fail a cheap distance test and cost almost nothing. The third reason is that the cost is fixed. The pass is always 128 by 128, whatever the screen size. A bigger screen saves more per-pixel work and pays the same index cost. 2. THE TIERED SHADOWMAP ATLAS THE PROBLEM A point light casts a shadow in every direction. The standard tool is a cube shadow map: six images, one per direction, each rendered from the light's position. Six images per light, times 255 lights, at a usable size, is far beyond any frame budget, in memory or in time. THE IDEA Give every light its own place in one big texture, and let the size of the place depend on how important the light is. The texture (the "atlas") is 4096 by 4096 texels by default, 16 bits deep, no color. It is cut into 256 fixed regions, one per light. Each region holds the light's six faces, laid back to back. The cut is a precomputed table: no regions overlap, and nothing is allocated at run time. THE TIERS The regions come in five sizes plus one last resort (face size, at the default atlas): 4 slots 512 by 512 per face the closest lights 12 slots 256 by 256 per face 32 slots 128 by 128 per face 73 slots 64 by 64 per face 134 slots 32 by 32 per face 1 slot 20 by 32 per face the overflow slot The big slots go to the lights that matter most. A light's rank is how far it sits from the edge of its reach. A light the player is close to sits deep inside its own reach; that is the light whose shadow the player sees, and it takes a big slot. A light that is almost past its range matters less and can live in a small one. THE ASSIGNMENT Every frame the system hands out slots. A light with no slot takes the first empty slot. If no slot is empty it may steal the slot of a light the camera cannot see right now (that light's shadow is not on screen, so the loss is not seen). If there is nothing to steal, the light gets no shadow at all ("slotless"). It still lights the scene; it only stops casting. THE RENDER A slot is filled by drawing the scene from the light's position. For each of the six faces, the renderer clears that face's region and then draws the casters (tree trunks, canopies, ground) whose bounding boxes overlap the light's reach. Only those boxes are drawn; the rest of the forest is skipped for this light. The map stores depth only: for a given direction, the distance from the light to the nearest surface. THE BUDGET Not every light re-renders its slot every frame. That would be 255 six-face draws per frame. Instead each slot carries an "error" value that grows every frame while its light is moving. A big slot grows faster than a small one, so the big slots age quickly. Once per frame the system sorts the dirty slots by error and re-renders the oldest ones, up to a budget (32 slots per frame, or the number of slot swaps if that is larger). The four nearest slots sort first, so if they are dirty they are redrawn. The result: the big nearby slots stay fresh, the tiny far slots update slowly, and a completely still scene costs zero shadow work per frame after the first fill. THE SWAPS When the camera moves, lights cross the tier boundaries. A light that is now closer may deserve a bigger slot than the one it holds. The system schedules a swap between the two slot records (the big slot goes to the light that is now closer). An upgrade (a bigger slot) acts now; a downgrade waits half a second and asks for 2 meters of camera movement first. The frame executes at most 36 swaps. Until the budget re-renders a swapped slot, its region still holds the previous occupant's faces. That stale state can last a frame or two under heavy motion; in normal use it is not seen. RECAP: ONE FRAME OF THE ATLAS In order, every frame: 1. List the lights the camera can see, and rank them by how much each one matters. 2. Give each light a slot: an empty one, a stolen one, or none. 3. Let the lights that crossed a tier boundary swap slots. 4. Age the slots whose lights moved (the big slots age faster). 5. Re-render the oldest slots, up to the budget. 6. The world pass reads the stored depths and shadows the pixels. Steps 1 to 3 decide who owns which region. Steps 4 and 5 decide how much of the map is redrawn. Step 6 is the only place the map is used. A still scene: nothing ages, nothing is redrawn, the frame pays nothing. WHY AN ATLAS One texture instead of 255: one allocation, one bind, one place for the consumer to read. The world pass asks the atlas for a shadow the same way for every light: find the light's region, pick the face that points at the pixel, read the depth, and compare it with the pixel's distance to the light. 16 bits of depth is enough for these small maps. The fixed layout means the atlas never grows, never fragments, and never allocates mid-frame. HOW THE TWO WORK TOGETHER The index texture answers "which lights touch this pixel?" with two integers per screen cell. The atlas answers "how good is this light's shadow?" with a budget that spends itself where the eye is. The same pattern runs through both: decide once for a region, skip what changed little, and let the eye decide what is worth recomputing.