Game Development Reference
In-Depth Information
// Encode color into the lower 24 bit of an unsigned integer, using
// 8 bit for each color channel.
uint colorMask = EncodeColor(base.rgb);
// Calculate color -channel contrast of color and write value into the
// highest 8 bit of the color mask.
float contrast = length(base.rrg-base.gbb)/
(sqrt(2.0f)+base.r+base.g+base.b);
int iContrast = int (contrast*255.0f);
colorMask |= iContrast<<24;
// Encode normal into the lower 27 bit of an unsigned integer, using
// for each axis 8 bit for the value and 1 bit for the sign.
float3 normal = normalize(input.normal);
uint normalMask = EncodeNormal(normal.xyz);
// Calculate to which face of a tetrahedron current normal is closest
// and write corresponding dot product into the highest 5 bit of the
// normal mask.
float dotProduct;
int normalIndex = GetNormalIndex(normal,dotProduct);
int iDotProduct = int (saturate(dotProduct)*31.0f);
normalMask |= iDotProduct <<27;
// Get offset into voxel grid.
float3 offset = (input.positionWS-constBuffer.snappedGridCenter)*
constBuffer.invGridCellSize;
offset = round(offset);
// Get position in voxel grid.
int3 voxelPos = int3(16,16,16)+int3(offset);
// Only output voxels that are inside the boundaries of the grid.
if ((voxelPos.x>-1)&&(voxelPos.x<32)&&(voxelPos.y>-1)&&
(voxelPos.y<32)&&(voxelPos.z>-1)&&(voxelPos.z<32))
{
// Get index into voxel grid.
int gridIndex = (voxelPos.z*1024)+(voxelPos.y*32)+voxelPos.x;
// Output color.
InterlockedMax(gridBuffer[gridIndex].colorMask,colorMask);
// Output normal according to normal index.
InterlockedMax(gridBuffer[gridIndex].normalMasks[normalIndex],
normalMask);
// Mark voxel that contains geometry information.
InterlockedMax(gridBuffer[gridIndex].occlusion,1);
}
}
Listing 7.1. Generation of the voxel grid.
To avoid race conditions between multiple threads that write into the same
location, atomic functions have to be used. Since atomic operations are only
supported in DirectX 11 for integer types, all values have to be converted into
integers. Among the variety of DirectX 11 buffers, the RWStructuredBuffer is
chosen, since this is the only way to hold multiple integer variables in one single
buffer and at the same time perform atomic operations on them.
 
Search Nedrilad ::




Custom Search