Pangram verdict · v3.3
We believe that this entire text is human-written.
AI likelihood · overall
HumanArticle text · 1,576 words · 1 segments analyzed
Intro Radiance cascades is a technique that massively improves global illumination without much extra cost or accumulation. In this article I'll go over the recent technique of Ray Splitting which allows the use of RC (Radiance cascades)in 3D without huge costs, and how to implement it. Note that the code examples are modified for clearability, so might not fully work Radiance Cascades For this article I'll assume you already know the basics of RC, if not I really recommend the interactive tutorial from jason.today or the article from MAX Here is a quick recap as well. RC Recap The main point of RC is combining the samples/rays for our GI in a way that utilizes them most effectively.This results in a more correct output with less samples. The way of combining this relies on the penumbra hypothesis. Which states that, when you are close to a light source you need fewer rays (less angular resolution) to find the light. In addition when you are close to a light you need more probes (spatial resolution) to get correct shadows To interpolate the half shadow, we need the light at both sides. Further away this spacing can be bigger to still get probes on both sides. By merging layers ranging from (high resolution, low direction count) to (low resolution, high direction count) that correspond to an interval of the ray, we get best of both of these in the range that matters. Probes Problems of 3D RC requires storing our light into a grid of probes. This becomes a problem in 3D due to the massive memory cost that introduces. since probe counts scale ^3 Even if we would only put this around the camera it would cost way too much memory to get a decent resolution.Luckily we only need probes where we need light, which is on all visible surfaces.So we need a way to only put probes on visible geometry. Sparse probes To only spawn probes on visible surfaces, we reconstruct world positions from the screen.We round these positions to a grid of cells. To store our light in these cells we need a buffer of probes. To allocate this we use a hashtable. Creating a hash for each cell allows us to quickly allocate a slot for each of these without much fighting between threads.We also store an AppendBuffer with a list of the probes we allocated, this will be used later to launch our next passes indirectly.(this somewhat goes against the use of the hashtable, but the table will be needed more later)We clear and fill this table every frame and with this we get a table of sparsely allocated probes. // HLSL, Compute: "GatherC0Probes.hlsl" //For now most kinds of hash tables are fine #include "HashTable.hlsl" RWStructuredBuffer‹uint› ActiveProbes; RWStructuredBuffer‹uint› ActiveCounter; [numthreads(8, 8, 1)] void MainCS(uint2 DispatchThreadId : SV_DispatchThreadID) { //We run over every pixel, to allocate a probe at its world position uint2 PixelId = DispatchThreadId; // Check that we are in scope if (any(float2(DispatchThreadId) >= ViewportPixelSize)) { return; } float2 UV = (float2(PixelId) + 0.5f) / float2(ViewportPixelSize); float DeviceZ = SampleSceneDepth(UV); if (DeviceZ == 0) { return; } //We can reconstruct the world position from the depthbuffer using our camera view, most engines have a helper for this float3 WorldPos = ReconstructWorldPositionFromDepth(DeviceZ); float VoxelSize = BaseVoxelSize; int3 Cell = int3(floor((WorldPos / VoxelSize))); //For our Key we simply shift the bits of the cell into 1 big uint //Later we store more info in here so we dont use all the bits //Make sure to add + (1u ‹‹ (BitsPerInt - 1)) to each value to make them unsigned //So our key is //Bits: // xxxxxxxxxxxxxxxxyyyyyyyyyyyyyyyyzzzzzzzzzzzzzzzz0000000000000000 uint64_t Key = PackProbeKey(Cell); //Add our key to the table //Get a random index from our Key, most hashing functions work on 32 bit uints so we do this uint Index = Hash(uint(Key) ^ Hash(uint(Key >> 32))); bool Added = HashTableAdd(Key, [&]: Index, Cascade: 0); if (Added) { //Add to active probes for next passes uint Counter; InterlockedAdd(ActiveCounter[0], 1, Counter); ActiveProbes[Counter] = Index; } } We can visualize/debug our probes in a separate pass // HLSL, Compute: "Output.hlsl" #include "HashTable.hlsl" #include "ColorFromHash.hlsl" [numthreads(8, 8, 1)] void MainCS(uint2 DispatchThreadId : SV_DispatchThreadID) { //Go over every pixel and check if the probe is there uint2 PixelId = DispatchThreadId; // Check that we are in scope if (any(float2(DispatchThreadId) >= ViewportPixelSize)) { return; } float VoxelSize = BaseVoxelSize; int3 Cell = int3(floor((WorldPos / VoxelSize))); uint64_t Key = PackProbeKey(Cell); uint Index = Hash(uint(Key) ^ Hash(uint(Key >> 32))); bool Found = HashTableFind(Key, [&]: Index, Cascade: 0 ); if (Found) { //Since our hashtable is unstable, using debugindex will flicker, you can use the key instead for now. Output[PixelId] = float4(ColorFromHash(Hash(Index)), 1.0); }else { Output[PixelId] = float4(1,0,0,1); } } Cascades With this gather pass we now have a single cascade of probes allocated.However we need multiple cascades of lowering resolution to do RC. For this we need to fill separate hashtables for each cascade, we could just re-run our shader for each cascade but that would be very innefficient. We already have our highest detail probes and there are significantly less of them than we have pixels. So instead of running over all pixels again, we can just go over all the probes we just allocated, for each of these we just halve their position to get the cascade above it. Any duplicates we get from this are handled by the hashtable.We run this bottom up for all cascades. The number of cascades we need is somewhat arbitrairy based on your scene size i'll be using 4 // HLSL, Compute: "FillCascades.hlsl" #include "HashTable.hlsl" // Since each pass continues from the last, // this starts off with the counter from the gather, and then is set to the counter we're filling for the next cascade. // So each pass launches the next RWStructuredBuffer‹uint› PrevActiveProbes; RWStructuredBuffer‹uint› PrevActiveCounter; RWStructuredBuffer‹uint› NextActiveProbes; RWStructuredBuffer‹uint› NextActiveCounter; //The cascade we are filling, so this starts at 1 uint Cascade; [numthreads(64, 1, 1)] void MainCS(uint DispatchThreadId : SV_DispatchThreadID) { uint ProbeId = DispatchThreadId; // Check that we are in scope (since the dispatch gets ceiled to group count) if (ProbeId >= PrevActiveCounter[0]) { return; } // Get the actual key out of the active list which just contains hashtable id's // Im adding the cascade since my hashtable stores the separate tables in 1 big buffer uint64_t ProbeKey = HashTable[(Cascade-1) * HashTableSize + PrevActiveProbes[ProbeId]]; // Since our key is just bit shifted positions we can unpack it again. ProbeKeyInfo KeyData = UnpackProbeKey(ProbeKey); // Get parent key by halving the position of our current key uint64_t HighKey = PackProbeKey(KeyData.Cell >> 1); uint Index = Hash(uint(HighKey) ^ Hash(uint(HighKey >> 32))); if (HashTableAdd(HighKey, Cascade, Index)) { // Fill in the active list which we use for next pass. uint Counter; InterlockedAdd(NextActiveCounter[0], 1, Counter); NextActiveProbes[Counter] = Index; } } Note that when visualizing your higher cascades you need to shift your positions when creating the key Rays From probes vs from screen Normally to get the lighting at every probe, we shoot a set amount of rays into random directions from each probe.This however comes with quite a few issues.Ray counts become very big with good enough spatial and angular resolution. But the main problem is probe positioning, since our probes are in a grid they are not aligned with the surfaces which can cause a bunch of rays to shoot below the surface. This can cause overocclusion and light leaking, and also makes it so we cannot shoot rays in just a hemisphere. The 2 red rays in this probe here got unfairly blocked because the probe is too far into the ground This would cause dark splotches in our lighting.So instead we can trace the rays from reconstructed world positions and put these into the probes that are at that spot.This way rays are sent from real geometry avoiding this problem. Here the blue rays are sent from the position gotten from the screen, then moved into the closest probe. Splitting into intervals However, in RC each cascade does not contain the full ray, only a interval/segment of it. Sending multiple rays for each interval at each pixel would be quite inefficient. Instead we do it the other way around, we trace the full ray and pick the interval (and thus cascade) that it would have hit in based on its hit distance. We fill the hit interval's cascade with the lighting, and then mark transparancy / that a ray traveled through them in the cascades below the hit one. The red ray hit inside the second cascade, and thus is binned into the second and first cascade. We store this in multiple bins per probe, one for each direction. We need to lay out these directions evenly and most importantly in a way that scales so that our merging evenly combines them into lower cascades. For shooting each ray we can just pick a random direction in a hemisphere at our hit. // HLSL, Compute: #include "HashTable.hlsl" #include "EncodeDirection.hlsl" RWStructuredBuffer‹uint4› TotalRadiance; RWStructuredBuffer‹uint› Weights; RWStructuredBuffer‹uint› DepositCounts; static const uint BaseDirections = 4; // This means 32 actual direction bins static const uint kNumCascades = 4; static const float kC0Len = 1.5958; // sqrt(2*BaseDirections*BaseDirections / (4*PI)), [shader("raygeneration")] void MainRG() { uint2 PixelCoord = DispatchRaysIndex().xy; float2 UV = (float2(PixelCoord) + 0.5f) / float2(ViewportPixelSize); FGBufferData GBuffer = GetGBufferDataFromSceneTextures(UV); float DeviceZ = SampleSceneDepth(UV); if (DeviceZ ‹= 0) { return; } float3 WorldPos = ReconstructWorldPositionFromDepth(DeviceZ); //Also sample worldnormal from GBuffer, could