Ray-Box Intersection Shader Code
•bool
•IntersectBox(Ray r, float3 boxmin, float3 boxmax, out float tnear,
out float tfar)
•{
•    // compute intersection of ray with all six bbox planes
•    float3 invR = 1.0 / r.d;
•    float3 tbot = invR * (boxmin.xyz - r.o);
•    float3 ttop = invR * (boxmax.xyz - r.o);
•
•    // re-order intersections to find smallest and largest on each axis
•    float3 tmin = min (ttop, tbot);
•    float3 tmax = max (ttop, tbot);
•
•    // find the largest tmin and the smallest tmax
•    float2 t0 = max (tmin.xx, tmin.yz);
•    tnear = max (t0.x, t0.y);
•    t0 = min (tmax.xx, tmax.yz);
•    tfar = min (t0.x, t0.y);
•
•    // check for hit
•    bool hit;
•    if ((tnear > tfar))
•        hit = false;
•    else
•        hit = true;
• return hit;
•}