   {
     float3 depth = make_float3(0.0f, 0.0f, 0.0f);
     // convert position to window-coordinates
-    const float4 depthWin = mulPost(c_MvPrMatrix, make_float4(ibrDepth  .x, ibrDepth  .y, ibrDepth  .z, 1.0f));
+    const float4 depthWin = mulPost(c_MvPrMatrix, make_float4(ibrDepth.x, ibrDepth.y, ibrDepth.z, 1.0f));
     depth = perspectiveDivide(depthWin);
 
-    depth.z++;
-    depth.z = depth.z/2.0f;
-
-    if(ibrPlanes.x != 0.0f && ibrPlanes.y != 0.0f) // SCALED depth mode
-    {
-      depth.z = (depth.z - ibrPlanes.x) / (ibrPlanes.y - ibrPlanes.x);
-    }
-
-    if     (depth.z > 1.0f) depth.z = 1.0f;
-    else if(depth.z < 0.0f) depth.z = 0.0f;
+    // Scale to 0.0 - 1.0
+    depth.z += 1.0f;
+    depth.z *= 0.5f;
+
+    depth.z = (depth.z - ibrPlanes.x) / (ibrPlanes.y - ibrPlanes.x);
+    clamp(depth.z);
 
     switch(dp)
     {

#endif

  // Ensure that dist is big enough
  bool infinite = false;
  if(tnear+dist != tnear && tfar+dist != tfar)
    infinite = true;

  while(infinite)
  {

      switch(dp)
      {
      case 8:
        ((uchar*)(d_depth))[y * texwidth + x] = 0;
        break;
      case 16:
        ((ushort*)(d_depth))[y * texwidth + x] = 0;
        break;
      case 32:
        ((float*)(d_depth))[y * texwidth + x] = 0.f;
