Graphics Reference
In-Depth Information
4.5.3 Ray-Tracing Pass
The implementation in Listing 4.5 is the Hi-Z ray-tracing code in Microsoft HLSL.
The code snippet is heavily commented and should be easy to follow once the
algorithm presented in Section 4.4.3 is clear.
float3
hiZTrace
(
float3
p
,
float3
v
)
{
const float
rootLevel
=
mipCount
−
1.0
f
;
// Convert to 0
// based indexing
float
level
=
HIZ_START_LEVEL
;
// HIZ START LEVEL was
// set to 2 in the demo
float
iterations
=0.0
f
;
// Get the cell cross direction and a small offset to enter
// the next cell when doing cell crossing.
float2
crossStep
,
crossOffset
;
crossStep
.
x
=(
v
.
x
>
=0 ) ? 1.
f
:
−
1.
f
;
crossStep
.
y
=(
v
.
y
>
=0 ) ? 1.
f
:
−
1.
f
;
crossOffset
.
xy
=
crossStep
.
xy
HIZ_CROSS_EPSILON
.
xy
;
crossStep
.
xy
=
saturate
(
crossStep
.
xy
);
// Set current ray to the original screen coordinate and
// depth.
float3
ray
=
p
.
xyz
;
// Scale the vector such that z is 1.0f
// (maximum depth) .
float3
d
=
v
.
xyz
/=
v
.
z
;
// Set starting point to the point where z equals 0.0f (
←
minimum depth) .
float3
o
=
intersectDepthPlane
(
p
.
xy
,
d
.
xy
,
−
p
.
z
);
// Cross to next cell so that we don
tgetaself
−
// intersection immediately.
float2
rayCell
=
getCell
(
ray
.
xy
,
hiZSize
.
xy
);
rayCell
.
xy
,
hiZSize
.
xy
,
crossStep
.
xy
,
crossOffset
.
xy
);
=
intersectCellBoundary
(
o
.
xy
,
d
.
xy
,
←
ray
// The algorithm loop HIZ STOP LEVEL was set to 2 in the
// demo; going too high can create artifacts .
[
loop
]
while
(
level
>
=
HIZ_STOP_LEVEL
&&
iterations
<
MAX_ITERATIONS
←
)
{
// Get the minimum depth plane in which the current ray
// resides .
float
minZ
=
getMinimumDepthPlane
(
ray
.
xy
,
level
,
←
rootLevel
);
// Get the cel l number of our current ray .
const float2
cellCount
=
getCellCount
(
level
,
rootLevel
);
const float2
oldCellIdx
=
getCell
(
ray
.
xy
,
cellCount
);
// Intersect only if ray depth is below the minimum depth
// plane .
float3
tmpRay
=
intersectDepthPlane
(
o
.
xy
,
d
.
xy
←
,
max
(
ray
.
z
,
minZ
));