home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 5 / MA_Cover_5.iso / ppc / mesa / src / depth.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-31  |  19.0 KB  |  935 lines

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