Game Development Reference
In-Depth Information
layout(rgba32ui) uniform uimageBuffer uShaderCache;
layout(r32ui) uniform volatile uimageBuffer uBucketLocks;
int getCachedAddress(uint ssID, inout bool needStore){
int hash = hashSSID(ssID);
uvec4 bucket = imageLoad(uShaderCache , hash);
int address = searchBucket(ssID, bucket);
// cache miss
while (address < 0 && iAttempt++ < MAX_ATTEMPTS){
// this thread is competing for storing a sample
uint lock = imageAtomicCompSwap(uBucketLocks , hash, FREE, LOCKED
if (lock == FREE){
address = int (atomicCounterIncrement(bufferTail));
// update the cache
bucket = storeBucket(ssID, hash, bucket);
imageStore(uShaderCache , hash, bucket);
needStore = true;
memoryBarrier(); // release the lock
imageStore(uBucketLocks , hash, FREE);
if (lock == LOCKED){
while (lock == LOCKED && lockAttempt++ < MAX_LOCK_ATTEMPTS)
lock = imageLoad(uBucketLocks , hash).x;
// now try to get the address again
bucket = imageLoad(uShaderCache , hash);
address = searchBucket(ssID, bucket);
// if everything failed, store a the data redundantly
if (address < 0){
address = int (atomicCounterIncrement(bufferTail));
needStore = true;
Listing 3.4. Implementation of the global shading cache.
list behavior and pack all buffers in a single vector. However, this optimization
only works if the hash function uniformly distributes cache requests (like ours),
and the number of buckets is high.
In our examples we use a bucket count of
Shading and Resolving
While the sampling stage described above successfully eliminates all duplicate
shading samples, the resulting CG-buffer might still hold redundant information.
Depending on the rasterization order of triangles, several samples in the compact
Search Nedrilad ::

Custom Search