Graphics Reference
In-Depth Information
// Distance of the new hit point ,
1
// asuint() retains the order of positive floats
2
int newDist = asuint ( floatingPointHitDistance );
3
4
5
int lastDistOrLocked =0;
do
6
{
7
// The absolute value always corresponds to the current
8
// closest hit. Immediately discard farther hits
9
if ( newDist > = abs ( RayDistBuffer [ rayIdx ]))
10
break ;
11
12
13
// Atomically compare new hit to the current closest hit
// and check if the ray is locked at the same time
14
InterlockedMin (
15
RayDistBuffer [ rayIdx ], newDist , lastDistOrLocked );
16
17
18
// Only entered if ray is unlocked (lastDistOrLocked > =0)
// and new distance is less than old distance
19
if ( newDist < lastDistOrLocked )
20
{
21
// Atomically lock ray via the distance buffer
22
// (= set distance to a negative value)
23
int lastDist =0;
24
InterlockedCompareExchange (
25
RayDistBuffer [ rayIdx ], newDist ,
newDist , lastDist );
26
27
28
// Check if exchg successful and new distance still closest
if ( lastDist == newDist )
29
{
30
< Update hit data >
31
// Unlock the ray by updating the distance buffer
32
InterlockedExchange ( RayDistBuffer [ rayIdx ], newDist );
33
}
34
}
35
// Re
iterate until the ray has been unlocked
36
}
while ( lastDistOrLocked < 0) ;
37
Listing 2.1. HLSL code for the ray update synchronization.
always corresponds to the current distance to the closest hit point. This enables
us to both atomically compare the distance of a new hit point against the current
closest distance and check the respective record's lock state at the same time.
Ray update synchronization. We make use of the atomic operations on unordered
access views that have become available in shader model 5.0 to implement a
spin lock mechanism that allows for the atomic updating of more than just one
unsigned integer quantity. Listing 2.1 shows the HLSL code skeleton.
We first check whether the distance of the new hit point is actually smaller
than the current absolute value of the record's distance attribute (line 8). Hits
with greater distances are skipped right away. We then use InterlockedMin() in
its signed integer version to atomically update the distance attribute with the
positive distance of the new hit point (line 13). If the record is currently locked,
Search WWH ::




Custom Search