home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / System / Mesa-3.1 / src / depth.c < prev    next >
C/C++ Source or Header  |  2000-01-07  |  20KB  |  893 lines

  1. /* $Id: depth.c,v 1.8 1999/11/08 07:36:43 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  3.1
  6.  * 
  7.  * Copyright (C) 1999  Brian Paul   All Rights Reserved.
  8.  * 
  9.  * Permission is hereby granted, free of charge, to any person obtaining a
  10.  * copy of this software and associated documentation files (the "Software"),
  11.  * to deal in the Software without restriction, including without limitation
  12.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13.  * and/or sell copies of the Software, and to permit persons to whom the
  14.  * Software is furnished to do so, subject to the following conditions:
  15.  * 
  16.  * The above copyright notice and this permission notice shall be included
  17.  * in all copies or substantial portions of the Software.
  18.  * 
  19.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  22.  * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  23.  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  24.  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.  */
  26.  
  27.  
  28. /* $XFree86: xc/lib/GL/mesa/src/depth.c,v 1.3 1999/04/04 00:20:22 dawes Exp $ */
  29.  
  30. /*
  31.  * Depth buffer functions
  32.  */
  33.  
  34. #include <stdlib.h>
  35.  
  36. #ifdef PC_HEADER
  37. #include "all.h"
  38. #else
  39. #ifndef XFree86Server
  40. #include <stdio.h>
  41. #include <string.h>
  42. #else
  43. #include "GL/xf86glx.h"
  44. #endif
  45. #include "context.h"
  46. #include "enums.h"
  47. #include "depth.h"
  48. #include "macros.h"
  49. #include "types.h"
  50. #endif
  51.  
  52.  
  53.  
  54. /**********************************************************************/
  55. /*****                          API Functions                     *****/
  56. /**********************************************************************/
  57.  
  58.  
  59.  
  60. void gl_ClearDepth( GLcontext* ctx, GLclampd depth )
  61. {
  62.    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glClearDepth");
  63.    ctx->Depth.Clear = (GLfloat) CLAMP( depth, 0.0, 1.0 );
  64.    if (ctx->Driver.ClearDepth)
  65.       (*ctx->Driver.ClearDepth)( ctx, ctx->Depth.Clear );
  66. }
  67.  
  68.  
  69.  
  70. void gl_DepthFunc( GLcontext* ctx, GLenum func )
  71. {
  72.    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glDepthFunc");
  73.  
  74.    if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
  75.       fprintf(stderr, "glDepthFunc %s\n", gl_lookup_enum_by_nr(func));
  76.  
  77.    switch (func) {
  78.       case GL_LESS:    /* (default) pass if incoming z < stored z */
  79.       case GL_GEQUAL:
  80.       case GL_LEQUAL:
  81.       case GL_GREATER:
  82.       case GL_NOTEQUAL:
  83.       case GL_EQUAL:
  84.       case GL_ALWAYS:
  85.      if (ctx->Depth.Func != func) {
  86.         ctx->Depth.Func = func;
  87.         ctx->NewState |= NEW_RASTER_OPS;
  88.         ctx->TriangleCaps &= ~DD_Z_NEVER;
  89.         if (ctx->Driver.DepthFunc) {
  90.            (*ctx->Driver.DepthFunc)( ctx, func );
  91.         }
  92.      }
  93.          break;
  94.       case GL_NEVER:
  95.      if (ctx->Depth.Func != func) {
  96.         ctx->Depth.Func = func;
  97.         ctx->NewState |= NEW_RASTER_OPS;
  98.         ctx->TriangleCaps |= DD_Z_NEVER;
  99.         if (ctx->Driver.DepthFunc) {
  100.            (*ctx->Driver.DepthFunc)( ctx, func );
  101.         }
  102.      }
  103.          break;
  104.       default:
  105.          gl_error( ctx, GL_INVALID_ENUM, "glDepth.Func" );
  106.    }
  107. }
  108.  
  109.  
  110.  
  111. void gl_DepthMask( GLcontext* ctx, GLboolean flag )
  112. {
  113.    ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glDepthMask");
  114.  
  115.    if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
  116.       fprintf(stderr, "glDepthMask %d\n", flag);
  117.  
  118.    /*
  119.     * GL_TRUE indicates depth buffer writing is enabled (default)
  120.     * GL_FALSE indicates depth buffer writing is disabled
  121.     */
  122.    if (ctx->Depth.Mask != flag) {
  123.       ctx->Depth.Mask = flag;
  124.       ctx->NewState |= NEW_RASTER_OPS;
  125.       if (ctx->Driver.DepthMask) {
  126.      (*ctx->Driver.DepthMask)( ctx, flag );
  127.       }
  128.    }
  129. }
  130.  
  131.  
  132.  
  133. /**********************************************************************/
  134. /*****                   Depth Testing Functions                  *****/
  135. /**********************************************************************/
  136.  
  137.  
  138. /*
  139.  * Depth test horizontal spans of fragments.  These functions are called
  140.  * via ctx->Driver.depth_test_span only.
  141.  *
  142.  * Input:  n - number of pixels in the span
  143.  *         x, y - location of leftmost pixel in span in window coords
  144.  *         z - array [n] of integer depth values
  145.  * In/Out:  mask - array [n] of flags (1=draw pixel, 0=don't draw) 
  146.  * Return:  number of pixels which passed depth test
  147.  */
  148.  
  149.  
  150. /*
  151.  * glDepthFunc( any ) and glDepthMask( GL_TRUE or GL_FALSE ).
  152.  */
  153. GLuint gl_depth_test_span_generic( GLcontext* ctx,
  154.                                    GLuint n, GLint x, GLint y,
  155.                                    const GLdepth z[],
  156.                                    GLubyte mask[] )
  157. {
  158.    GLdepth *zptr = Z_ADDRESS( ctx, x, y );
  159.    GLubyte *m = mask;
  160.    GLuint i;
  161.    GLuint passed = 0;
  162.  
  163.    /* switch cases ordered from most frequent to less frequent */
  164.    switch (ctx->Depth.Func) {
  165.       case GL_LESS:
  166.          if (ctx->Depth.Mask) {
  167.         /* Update Z buffer */
  168.         for (i=0; i<n; i++,zptr++,m++) {
  169.            if (*m) {
  170.           if (z[i] < *zptr) {
  171.              /* pass */
  172.              *zptr = z[i];
  173.              passed++;
  174.           }
  175.           else {
  176.              /* fail */
  177.              *m = 0;
  178.           }
  179.            }
  180.         }
  181.      }
  182.      else {
  183.         /* Don't update Z buffer */
  184.         for (i=0; i<n; i++,zptr++,m++) {
  185.            if (*m) {
  186.           if (z[i] < *zptr) {
  187.              /* pass */
  188.              passed++;
  189.           }
  190.           else {
  191.              *m = 0;
  192.           }
  193.            }
  194.         }
  195.      }
  196.      break;
  197.       case GL_LEQUAL:
  198.      if (ctx->Depth.Mask) {
  199.         /* Update Z buffer */
  200.         for (i=0;i<n;i++,zptr++,m++) {
  201.            if (*m) {
  202.           if (z[i] <= *zptr) {
  203.              *zptr = z[i];
  204.              passed++;
  205.           }
  206.           else {
  207.              *m = 0;
  208.           }
  209.            }
  210.         }
  211.      }
  212.      else {
  213.         /* Don't update Z buffer */
  214.         for (i=0;i<n;i++,zptr++,m++) {
  215.            if (*m) {
  216.           if (z[i] <= *zptr) {
  217.              /* pass */
  218.              passed++;
  219.           }
  220.           else {
  221.              *m = 0;
  222.           }
  223.            }
  224.         }
  225.      }
  226.      break;
  227.       case GL_GEQUAL:
  228.      if (ctx->Depth.Mask) {
  229.         /* Update Z buffer */
  230.         for (i=0;i<n;i++,zptr++,m++) {
  231.            if (*m) {
  232.           if (z[i] >= *zptr) {
  233.              *zptr = z[i];
  234.              passed++;
  235.           }
  236.           else {
  237.              *m = 0;
  238.           }
  239.            }
  240.         }
  241.      }
  242.      else {
  243.         /* Don't update Z buffer */
  244.         for (i=0;i<n;i++,zptr++,m++) {
  245.            if (*m) {
  246.           if (z[i] >= *zptr) {
  247.              /* pass */
  248.              passed++;
  249.           }
  250.           else {
  251.              *m = 0;
  252.           }
  253.            }
  254.         }
  255.      }
  256.      break;
  257.       case GL_GREATER:
  258.      if (ctx->Depth.Mask) {
  259.         /* Update Z buffer */
  260.         for (i=0;i<n;i++,zptr++,m++) {
  261.            if (*m) {
  262.           if (z[i] > *zptr) {
  263.              *zptr = z[i];
  264.              passed++;
  265.           }
  266.           else {
  267.              *m = 0;
  268.           }
  269.            }
  270.         }
  271.      }
  272.      else {
  273.         /* Don't update Z buffer */
  274.         for (i=0;i<n;i++,zptr++,m++) {
  275.            if (*m) {
  276.           if (z[i] > *zptr) {
  277.              /* pass */
  278.              passed++;
  279.           }
  280.           else {
  281.              *m = 0;
  282.           }
  283.            }
  284.         }
  285.      }
  286.      break;
  287.       case GL_NOTEQUAL:
  288.      if (ctx->Depth.Mask) {
  289.         /* Update Z buffer */
  290.         for (i=0;i<n;i++,zptr++,m++) {
  291.            if (*m) {
  292.           if (z[i] != *zptr) {
  293.              *zptr = z[i];
  294.              passed++;
  295.           }
  296.           else {
  297.              *m = 0;
  298.           }
  299.            }
  300.         }
  301.      }
  302.      else {
  303.         /* Don't update Z buffer */
  304.         for (i=0;i<n;i++,zptr++,m++) {
  305.            if (*m) {
  306.           if (z[i] != *zptr) {
  307.              /* pass */
  308.              passed++;
  309.           }
  310.           else {
  311.              *m = 0;
  312.           }
  313.            }
  314.         }
  315.      }
  316.      break;
  317.       case GL_EQUAL:
  318.      if (ctx->Depth.Mask) {
  319.         /* Update Z buffer */
  320.         for (i=0;i<n;i++,zptr++,m++) {
  321.            if (*m) {
  322.           if (z[i] == *zptr) {
  323.              *zptr = z[i];
  324.              passed++;
  325.           }
  326.           else {
  327.              *m =0;
  328.           }
  329.            }
  330.         }
  331.      }
  332.      else {
  333.         /* Don't update Z buffer */
  334.         for (i=0;i<n;i++,zptr++,m++) {
  335.            if (*m) {
  336.           if (z[i] == *zptr) {
  337.              /* pass */
  338.              passed++;
  339.           }
  340.           else {
  341.              *m =0;
  342.           }
  343.            }
  344.         }
  345.      }
  346.      break;
  347.       case GL_ALWAYS:
  348.      if (ctx->Depth.Mask) {
  349.         /* Update Z buffer */
  350.         for (i=0;i<n;i++,zptr++,m++) {
  351.            if (*m) {
  352.           *zptr = z[i];
  353.           passed++;
  354.            }
  355.         }
  356.      }
  357.      else {
  358.         /* Don't update Z buffer or mask */
  359.         passed = n;
  360.      }
  361.      break;
  362.       case GL_NEVER:
  363.      for (i=0;i<n;i++) {
  364.         mask[i] = 0;
  365.      }
  366.      break;
  367.       default:
  368.          gl_problem(ctx, "Bad depth func in gl_depth_test_span_generic");
  369.    } /*switch*/
  370.  
  371.    return passed;
  372. }
  373.  
  374.  
  375.  
  376. /*
  377.  * glDepthFunc(GL_LESS) and glDepthMask(GL_TRUE).
  378.  */
  379. GLuint gl_depth_test_span_less( GLcontext* ctx,
  380.                                 GLuint n, GLint x, GLint y, const GLdepth z[],
  381.                                 GLubyte mask[] )
  382. {
  383.    GLdepth *zptr = Z_ADDRESS( ctx, x, y );
  384.    GLuint i;
  385.    GLuint passed = 0;
  386.  
  387.    for (i=0; i<n; i++) {
  388.       if (mask[i]) {
  389.          if (z[i] < zptr[i]) {
  390.             /* pass */
  391.             zptr[i] = z[i];
  392.             passed++;
  393.          }
  394.          else {
  395.             /* fail */
  396.             mask[i] = 0;
  397.          }
  398.       }
  399.    }
  400.    return passed;
  401. }
  402.  
  403.  
  404. /*
  405.  * glDepthFunc(GL_GREATER) and glDepthMask(GL_TRUE).
  406.  */
  407. GLuint gl_depth_test_span_greater( GLcontext* ctx,
  408.                                    GLuint n, GLint x, GLint y,
  409.                                    const GLdepth z[],
  410.                                    GLubyte mask[] )
  411. {
  412.    GLdepth *zptr = Z_ADDRESS( ctx, x, y );
  413.    GLuint i;
  414.    GLuint passed = 0;
  415.  
  416.    for (i=0; i<n; i++) {
  417.       if (mask[i]) {
  418.          if (z[i] > zptr[i]) {
  419.             /* pass */
  420.             zptr[i] = z[i];
  421.             passed++;
  422.          }
  423.          else {
  424.             /* fail */
  425.             mask[i] = 0;
  426.          }
  427.       }
  428.    }
  429.    return passed;
  430. }
  431.  
  432.  
  433.  
  434. /*
  435.  * Depth test an array of randomly positioned fragments.
  436.  */
  437.  
  438.  
  439. #define ZADDR_SETUP   GLdepth *depthbuffer = ctx->Buffer->Depth; \
  440.                       GLint width = ctx->Buffer->Width;
  441.  
  442. #define ZADDR( X, Y )   (depthbuffer + (Y) * width + (X) )
  443.  
  444.  
  445.  
  446. /*
  447.  * glDepthFunc( any ) and glDepthMask( GL_TRUE or GL_FALSE ).
  448.  */
  449. void gl_depth_test_pixels_generic( GLcontext* ctx,
  450.                                    GLuint n, const GLint x[], const GLint y[],
  451.                                    const GLdepth z[], GLubyte mask[] )
  452. {
  453.    register GLdepth *zptr;
  454.    register GLuint i;
  455.  
  456.    /* switch cases ordered from most frequent to less frequent */
  457.    switch (ctx->Depth.Func) {
  458.       case GL_LESS:
  459.          if (ctx->Depth.Mask) {
  460.         /* Update Z buffer */
  461.         for (i=0; i<n; i++) {
  462.            if (mask[i]) {
  463.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  464.           if (z[i] < *zptr) {
  465.              /* pass */
  466.              *zptr = z[i];
  467.           }
  468.           else {
  469.              /* fail */
  470.              mask[i] = 0;
  471.           }
  472.            }
  473.         }
  474.      }
  475.      else {
  476.         /* Don't update Z buffer */
  477.         for (i=0; i<n; i++) {
  478.            if (mask[i]) {
  479.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  480.           if (z[i] < *zptr) {
  481.              /* pass */
  482.           }
  483.           else {
  484.              /* fail */
  485.              mask[i] = 0;
  486.           }
  487.            }
  488.         }
  489.      }
  490.      break;
  491.       case GL_LEQUAL:
  492.          if (ctx->Depth.Mask) {
  493.         /* Update Z buffer */
  494.         for (i=0; i<n; i++) {
  495.            if (mask[i]) {
  496.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  497.           if (z[i] <= *zptr) {
  498.              /* pass */
  499.              *zptr = z[i];
  500.           }
  501.           else {
  502.              /* fail */
  503.              mask[i] = 0;
  504.           }
  505.            }
  506.         }
  507.      }
  508.      else {
  509.         /* Don't update Z buffer */
  510.         for (i=0; i<n; i++) {
  511.            if (mask[i]) {
  512.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  513.           if (z[i] <= *zptr) {
  514.              /* pass */
  515.           }
  516.           else {
  517.              /* fail */
  518.              mask[i] = 0;
  519.           }
  520.            }
  521.         }
  522.      }
  523.      break;
  524.       case GL_GEQUAL:
  525.          if (ctx->Depth.Mask) {
  526.         /* Update Z buffer */
  527.         for (i=0; i<n; i++) {
  528.            if (mask[i]) {
  529.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  530.           if (z[i] >= *zptr) {
  531.              /* pass */
  532.              *zptr = z[i];
  533.           }
  534.           else {
  535.              /* fail */
  536.              mask[i] = 0;
  537.           }
  538.            }
  539.         }
  540.      }
  541.      else {
  542.         /* Don't update Z buffer */
  543.         for (i=0; i<n; i++) {
  544.            if (mask[i]) {
  545.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  546.           if (z[i] >= *zptr) {
  547.              /* pass */
  548.           }
  549.           else {
  550.              /* fail */
  551.              mask[i] = 0;
  552.           }
  553.            }
  554.         }
  555.      }
  556.      break;
  557.       case GL_GREATER:
  558.          if (ctx->Depth.Mask) {
  559.         /* Update Z buffer */
  560.         for (i=0; i<n; i++) {
  561.            if (mask[i]) {
  562.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  563.           if (z[i] > *zptr) {
  564.              /* pass */
  565.              *zptr = z[i];
  566.           }
  567.           else {
  568.              /* fail */
  569.              mask[i] = 0;
  570.           }
  571.            }
  572.         }
  573.      }
  574.      else {
  575.         /* Don't update Z buffer */
  576.         for (i=0; i<n; i++) {
  577.            if (mask[i]) {
  578.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  579.           if (z[i] > *zptr) {
  580.              /* pass */
  581.           }
  582.           else {
  583.              /* fail */
  584.              mask[i] = 0;
  585.           }
  586.            }
  587.         }
  588.      }
  589.      break;
  590.       case GL_NOTEQUAL:
  591.          if (ctx->Depth.Mask) {
  592.         /* Update Z buffer */
  593.         for (i=0; i<n; i++) {
  594.            if (mask[i]) {
  595.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  596.           if (z[i] != *zptr) {
  597.              /* pass */
  598.              *zptr = z[i];
  599.           }
  600.           else {
  601.              /* fail */
  602.              mask[i] = 0;
  603.           }
  604.            }
  605.         }
  606.      }
  607.      else {
  608.         /* Don't update Z buffer */
  609.         for (i=0; i<n; i++) {
  610.            if (mask[i]) {
  611.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  612.           if (z[i] != *zptr) {
  613.              /* pass */
  614.           }
  615.           else {
  616.              /* fail */
  617.              mask[i] = 0;
  618.           }
  619.            }
  620.         }
  621.      }
  622.      break;
  623.       case GL_EQUAL:
  624.          if (ctx->Depth.Mask) {
  625.         /* Update Z buffer */
  626.         for (i=0; i<n; i++) {
  627.            if (mask[i]) {
  628.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  629.           if (z[i] == *zptr) {
  630.              /* pass */
  631.              *zptr = z[i];
  632.           }
  633.           else {
  634.              /* fail */
  635.              mask[i] = 0;
  636.           }
  637.            }
  638.         }
  639.      }
  640.      else {
  641.         /* Don't update Z buffer */
  642.         for (i=0; i<n; i++) {
  643.            if (mask[i]) {
  644.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  645.           if (z[i] == *zptr) {
  646.              /* pass */
  647.           }
  648.           else {
  649.              /* fail */
  650.              mask[i] = 0;
  651.           }
  652.            }
  653.         }
  654.      }
  655.      break;
  656.       case GL_ALWAYS:
  657.      if (ctx->Depth.Mask) {
  658.         /* Update Z buffer */
  659.         for (i=0; i<n; i++) {
  660.            if (mask[i]) {
  661.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  662.           *zptr = z[i];
  663.            }
  664.         }
  665.      }
  666.      else {
  667.         /* Don't update Z buffer or mask */
  668.      }
  669.      break;
  670.       case GL_NEVER:
  671.      /* depth test never passes */
  672.      for (i=0;i<n;i++) {
  673.         mask[i] = 0;
  674.      }
  675.      break;
  676.       default:
  677.          gl_problem(ctx, "Bad depth func in gl_depth_test_pixels_generic");
  678.    } /*switch*/
  679. }
  680.  
  681.  
  682.  
  683. /*
  684.  * glDepthFunc( GL_LESS ) and glDepthMask( GL_TRUE ).
  685.  */
  686. void gl_depth_test_pixels_less( GLcontext* ctx,
  687.                                 GLuint n, const GLint x[], const GLint y[],
  688.                                 const GLdepth z[], GLubyte mask[] )
  689. {
  690.    GLdepth *zptr;
  691.    GLuint i;
  692.  
  693.    for (i=0; i<n; i++) {
  694.       if (mask[i]) {
  695.          zptr = Z_ADDRESS(ctx,x[i],y[i]);
  696.          if (z[i] < *zptr) {
  697.             /* pass */
  698.             *zptr = z[i];
  699.          }
  700.          else {
  701.             /* fail */
  702.             mask[i] = 0;
  703.          }
  704.       }
  705.    }
  706. }
  707.  
  708.  
  709. /*
  710.  * glDepthFunc( GL_GREATER ) and glDepthMask( GL_TRUE ).
  711.  */
  712. void gl_depth_test_pixels_greater( GLcontext* ctx,
  713.                                    GLuint n, const GLint x[], const GLint y[],
  714.                                    const GLdepth z[], GLubyte mask[] )
  715. {
  716.    GLdepth *zptr;
  717.    GLuint i;
  718.  
  719.    for (i=0; i<n; i++) {
  720.       if (mask[i]) {
  721.          zptr = Z_ADDRESS(ctx,x[i],y[i]);
  722.          if (z[i] > *zptr) {
  723.             /* pass */
  724.             *zptr = z[i];
  725.          }
  726.          else {
  727.             /* fail */
  728.             mask[i] = 0;
  729.          }
  730.       }
  731.    }
  732. }
  733.  
  734.  
  735.  
  736.  
  737. /**********************************************************************/
  738. /*****                      Read Depth Buffer                     *****/
  739. /**********************************************************************/
  740.  
  741.  
  742. /*
  743.  * Return a span of depth values from the depth buffer as floats in [0,1].
  744.  * This function is only called through Driver.read_depth_span_float()
  745.  * Input:  n - how many pixels
  746.  *         x,y - location of first pixel
  747.  * Output:  depth - the array of depth values
  748.  */
  749. void gl_read_depth_span_float( GLcontext* ctx,
  750.                                GLuint n, GLint x, GLint y, GLfloat depth[] )
  751. {
  752.    GLdepth *zptr;
  753.    GLfloat scale;
  754.    GLuint i;
  755.  
  756.    scale = 1.0F / DEPTH_SCALE;
  757.  
  758.    if (ctx->Buffer->Depth) {
  759.       zptr = Z_ADDRESS( ctx, x, y );
  760.       for (i=0;i<n;i++) {
  761.      depth[i] = (GLfloat) zptr[i] * scale;
  762.       }
  763.    }
  764.    else {
  765.       for (i=0;i<n;i++) {
  766.      depth[i] = 0.0F;
  767.       }
  768.    }
  769. }
  770.  
  771.  
  772. /*
  773.  * Return a span of depth values from the depth buffer as integers in
  774.  * [0,MAX_DEPTH].
  775.  * This function is only called through Driver.read_depth_span_int()
  776.  * Input:  n - how many pixels
  777.  *         x,y - location of first pixel
  778.  * Output:  depth - the array of depth values
  779.  */
  780. void gl_read_depth_span_int( GLcontext* ctx,
  781.                              GLuint n, GLint x, GLint y, GLdepth depth[] )
  782. {
  783.    if (ctx->Buffer->Depth) {
  784.       GLdepth *zptr = Z_ADDRESS( ctx, x, y );
  785.       MEMCPY( depth, zptr, n * sizeof(GLdepth) );
  786.    }
  787.    else {
  788.       GLuint i;
  789.       for (i=0;i<n;i++) {
  790.      depth[i] = 0;
  791.       }
  792.    }
  793. }
  794.  
  795.  
  796.  
  797. /**********************************************************************/
  798. /*****                Allocate and Clear Depth Buffer             *****/
  799. /**********************************************************************/
  800.  
  801.  
  802.  
  803. /*
  804.  * Allocate a new depth buffer.  If there's already a depth buffer allocated
  805.  * it will be free()'d.  The new depth buffer will be uniniitalized.
  806.  * This function is only called through Driver.alloc_depth_buffer.
  807.  */
  808. void gl_alloc_depth_buffer( GLcontext* ctx )
  809. {
  810.    /* deallocate current depth buffer if present */
  811.    if (ctx->Buffer->Depth) {
  812.       FREE(ctx->Buffer->Depth);
  813.       ctx->Buffer->Depth = NULL;
  814.    }
  815.  
  816.    /* allocate new depth buffer, but don't initialize it */
  817.    ctx->Buffer->Depth = (GLdepth *) MALLOC( ctx->Buffer->Width
  818.                                             * ctx->Buffer->Height
  819.                                             * sizeof(GLdepth) );
  820.    if (!ctx->Buffer->Depth) {
  821.       /* out of memory */
  822.       ctx->Depth.Test = GL_FALSE;
  823.       ctx->NewState |= NEW_RASTER_OPS;
  824.       gl_error( ctx, GL_OUT_OF_MEMORY, "Couldn't allocate depth buffer" );
  825.    }
  826. }
  827.  
  828.  
  829.  
  830.  
  831. /*
  832.  * Clear the depth buffer.  If the depth buffer doesn't exist yet we'll
  833.  * allocate it now.
  834.  * This function is only called through Driver.clear_depth_buffer.
  835.  */
  836. void gl_clear_depth_buffer( GLcontext* ctx )
  837. {
  838.    GLdepth clear_value = (GLdepth) (ctx->Depth.Clear * DEPTH_SCALE);
  839.    
  840.    if (ctx->Visual->DepthBits==0 || !ctx->Buffer->Depth || !ctx->Depth.Mask) {
  841.       /* no depth buffer, or writing to it is disabled */
  842.       return;
  843.    }
  844.  
  845.    /* The loops in this function have been written so the IRIX 5.3
  846.     * C compiler can unroll them.  Hopefully other compilers can too!
  847.     */
  848.  
  849.    if (ctx->Scissor.Enabled) {
  850.       /* only clear scissor region */
  851.       GLint y;
  852.       for (y=ctx->Buffer->Ymin; y<=ctx->Buffer->Ymax; y++) {
  853.          GLdepth *d = Z_ADDRESS( ctx, ctx->Buffer->Xmin, y );
  854.          GLint n = ctx->Buffer->Xmax - ctx->Buffer->Xmin + 1;
  855.          do {
  856.             *d++ = clear_value;
  857.             n--;
  858.          } while (n);
  859.       }
  860.    }
  861.    else {
  862.       /* clear whole buffer */
  863.       if (sizeof(GLdepth)==2 && (clear_value&0xff)==(clear_value>>8)) {
  864.          /* lower and upper bytes of clear_value are same, use MEMSET */
  865.          MEMSET( ctx->Buffer->Depth, clear_value&0xff,
  866.                  2*ctx->Buffer->Width*ctx->Buffer->Height);
  867.       }
  868.       else {
  869.          GLdepth *d = ctx->Buffer->Depth;
  870.          GLint n = ctx->Buffer->Width * ctx->Buffer->Height;
  871.          while (n>=16) {
  872.             d[0] = clear_value;    d[1] = clear_value;
  873.             d[2] = clear_value;    d[3] = clear_value;
  874.             d[4] = clear_value;    d[5] = clear_value;
  875.             d[6] = clear_value;    d[7] = clear_value;
  876.             d[8] = clear_value;    d[9] = clear_value;
  877.             d[10] = clear_value;   d[11] = clear_value;
  878.             d[12] = clear_value;   d[13] = clear_value;
  879.             d[14] = clear_value;   d[15] = clear_value;
  880.             d += 16;
  881.             n -= 16;
  882.          }
  883.          while (n>0) {
  884.             *d++ = clear_value;
  885.             n--;
  886.          }
  887.       }
  888.    }
  889. }
  890.  
  891.  
  892.  
  893.