Ignore:
Timestamp:
May 23, 2000, 10:41:28 PM (25 years ago)
Author:
jeroen
Message:

* empty log message *

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/opengl/mesa/depth.c

    r2962 r3598  
    1 /* $Id: depth.c,v 1.2 2000-03-01 18:49:26 jeroen Exp $ */
     1/* $Id: depth.c,v 1.3 2000-05-23 20:40:30 jeroen Exp $ */
    22
    33/*
    44 * Mesa 3-D graphics library
    5  * Version:  3.1
     5 * Version:  3.3
    66 *
    77 * Copyright (C) 1999  Brian Paul   All Rights Reserved.
     
    3737#include "all.h"
    3838#else
    39 #ifndef XFree86Server
    40 #include <stdio.h>
    41 #include <string.h>
    42 #else
    43 #include "GL/xf86glx.h"
    44 #endif
     39#include "glheader.h"
    4540#include "types.h"
    4641#include "context.h"
    4742#include "enums.h"
    4843#include "depth.h"
     44#include "mem.h"
     45#include "pb.h"
    4946#include "macros.h"
    5047#endif
     
    5855
    5956
    60 void gl_ClearDepth( GLcontext* ctx, GLclampd depth )
     57void
     58_mesa_ClearDepth( GLclampd depth )
    6159{
     60   GET_CURRENT_CONTEXT(ctx);
    6261   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glClearDepth");
    6362   ctx->Depth.Clear = (GLfloat) CLAMP( depth, 0.0, 1.0 );
     
    6867
    6968
    70 void gl_DepthFunc( GLcontext* ctx, GLenum func )
     69void
     70_mesa_DepthFunc( GLenum func )
    7171{
     72   GET_CURRENT_CONTEXT(ctx);
    7273   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glDepthFunc");
    7374
     
    8384      case GL_EQUAL:
    8485      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         }
     86        if (ctx->Depth.Func != func) {
     87            ctx->Depth.Func = func;
     88            ctx->NewState |= NEW_RASTER_OPS;
     89            ctx->TriangleCaps &= ~DD_Z_NEVER;
     90            if (ctx->Driver.DepthFunc) {
     91               (*ctx->Driver.DepthFunc)( ctx, func );
     92            }
     93        }
    9394         break;
    9495      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         }
     96        if (ctx->Depth.Func != func) {
     97            ctx->Depth.Func = func;
     98            ctx->NewState |= NEW_RASTER_OPS;
     99            ctx->TriangleCaps |= DD_Z_NEVER;
     100            if (ctx->Driver.DepthFunc) {
     101               (*ctx->Driver.DepthFunc)( ctx, func );
     102            }
     103        }
    103104         break;
    104105      default:
     
    109110
    110111
    111 void gl_DepthMask( GLcontext* ctx, GLboolean flag )
     112void
     113_mesa_DepthMask( GLboolean flag )
    112114{
     115   GET_CURRENT_CONTEXT(ctx);
    113116   ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx, "glDepthMask");
    114117
     
    124127      ctx->NewState |= NEW_RASTER_OPS;
    125128      if (ctx->Driver.DepthMask) {
    126         (*ctx->Driver.DepthMask)( ctx, flag );
     129        (*ctx->Driver.DepthMask)( ctx, flag );
    127130      }
    128131   }
    129132}
     133
     134
     135
     136/**********************************************************************/
     137/*****                           Misc                             *****/
     138/**********************************************************************/
     139
     140/*
     141 * Return address of depth buffer value for given window coord.
     142 */
     143GLvoid *
     144_mesa_zbuffer_address(GLcontext *ctx, GLint x, GLint y)
     145{
     146   if (ctx->Visual->DepthBits <= 16)
     147      return (GLushort *) ctx->DrawBuffer->DepthBuffer + ctx->DrawBuffer->Width * y + x;
     148   else
     149      return (GLuint *) ctx->DrawBuffer->DepthBuffer + ctx->DrawBuffer->Width * y + x;
     150}
     151
     152
     153#define Z_ADDRESS16( CTX, X, Y )                                \
     154            ( ((GLushort *) (CTX)->DrawBuffer->DepthBuffer)     \
     155              + (CTX)->DrawBuffer->Width * (Y) + (X) )
     156
     157#define Z_ADDRESS32( CTX, X, Y )                                \
     158            ( ((GLuint *) (CTX)->DrawBuffer->DepthBuffer)       \
     159              + (CTX)->DrawBuffer->Width * (Y) + (X) )
    130160
    131161
     
    137167
    138168/*
    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
     169 * Do depth test for an array of fragments.  This is used both for
     170 * software and hardware Z buffers.
     171 * Input:  zbuffer - array of z values in the zbuffer
     172 *         z - array of fragment z values
     173 * Return:  number of fragments which pass the test.
    147174 */
    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[] )
     175static GLuint
     176depth_test_span16( GLcontext *ctx, GLuint n, GLint x, GLint y,
     177                   GLushort zbuffer[], const GLdepth z[], GLubyte mask[] )
    157178{
    158    GLdepth *zptr = Z_ADDRESS( ctx, x, y );
    159    GLubyte *m = mask;
    160    GLuint i;
    161179   GLuint passed = 0;
    162180
     
    165183      case GL_LESS:
    166184         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;
     185            /* Update Z buffer */
     186            GLuint i;
     187            for (i=0; i<n; i++) {
     188               if (mask[i]) {
     189                  if (z[i] < zbuffer[i]) {
     190                     /* pass */
     191                     zbuffer[i] = z[i];
     192                     passed++;
     193                  }
     194                  else {
     195                     /* fail */
     196                     mask[i] = 0;
     197                  }
     198               }
     199            }
     200         }
     201         else {
     202            /* Don't update Z buffer */
     203            GLuint i;
     204            for (i=0; i<n; i++) {
     205               if (mask[i]) {
     206                  if (z[i] < zbuffer[i]) {
     207                     /* pass */
     208                     passed++;
     209                  }
     210                  else {
     211                     mask[i] = 0;
     212                  }
     213               }
     214            }
     215         }
     216         break;
    197217      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;
     218         if (ctx->Depth.Mask) {
     219            /* Update Z buffer */
     220            GLuint i;
     221            for (i=0;i<n;i++) {
     222               if (mask[i]) {
     223                  if (z[i] <= zbuffer[i]) {
     224                     zbuffer[i] = z[i];
     225                     passed++;
     226                  }
     227                  else {
     228                     mask[i] = 0;
     229                  }
     230               }
     231            }
     232         }
     233         else {
     234            /* Don't update Z buffer */
     235            GLuint i;
     236            for (i=0;i<n;i++) {
     237               if (mask[i]) {
     238                  if (z[i] <= zbuffer[i]) {
     239                     /* pass */
     240                     passed++;
     241                  }
     242                  else {
     243                     mask[i] = 0;
     244                  }
     245               }
     246            }
     247         }
     248         break;
    227249      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;
     250         if (ctx->Depth.Mask) {
     251            /* Update Z buffer */
     252            GLuint i;
     253            for (i=0;i<n;i++) {
     254               if (mask[i]) {
     255                  if (z[i] >= zbuffer[i]) {
     256                     zbuffer[i] = z[i];
     257                     passed++;
     258                  }
     259                  else {
     260                     mask[i] = 0;
     261                  }
     262               }
     263            }
     264         }
     265         else {
     266            /* Don't update Z buffer */
     267            GLuint i;
     268            for (i=0;i<n;i++) {
     269               if (mask[i]) {
     270                  if (z[i] >= zbuffer[i]) {
     271                     /* pass */
     272                     passed++;
     273                  }
     274                  else {
     275                     mask[i] = 0;
     276                  }
     277               }
     278            }
     279         }
     280         break;
    257281      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;
     282         if (ctx->Depth.Mask) {
     283            /* Update Z buffer */
     284            GLuint i;
     285            for (i=0;i<n;i++) {
     286               if (mask[i]) {
     287                  if (z[i] > zbuffer[i]) {
     288                     zbuffer[i] = z[i];
     289                     passed++;
     290                  }
     291                  else {
     292                     mask[i] = 0;
     293                  }
     294               }
     295            }
     296         }
     297         else {
     298            /* Don't update Z buffer */
     299            GLuint i;
     300            for (i=0;i<n;i++) {
     301               if (mask[i]) {
     302                  if (z[i] > zbuffer[i]) {
     303                     /* pass */
     304                     passed++;
     305                  }
     306                  else {
     307                     mask[i] = 0;
     308                  }
     309               }
     310            }
     311         }
     312         break;
    287313      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;
     314         if (ctx->Depth.Mask) {
     315            /* Update Z buffer */
     316            GLuint i;
     317            for (i=0;i<n;i++) {
     318               if (mask[i]) {
     319                  if (z[i] != zbuffer[i]) {
     320                     zbuffer[i] = z[i];
     321                     passed++;
     322                  }
     323                  else {
     324                     mask[i] = 0;
     325                  }
     326               }
     327            }
     328         }
     329         else {
     330            /* Don't update Z buffer */
     331            GLuint i;
     332            for (i=0;i<n;i++) {
     333               if (mask[i]) {
     334                  if (z[i] != zbuffer[i]) {
     335                     /* pass */
     336                     passed++;
     337                  }
     338                  else {
     339                     mask[i] = 0;
     340                  }
     341               }
     342            }
     343         }
     344         break;
    317345      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;
     346         if (ctx->Depth.Mask) {
     347            /* Update Z buffer */
     348            GLuint i;
     349            for (i=0;i<n;i++) {
     350               if (mask[i]) {
     351                  if (z[i] == zbuffer[i]) {
     352                     zbuffer[i] = z[i];
     353                     passed++;
     354                  }
     355                  else {
     356                     mask[i] = 0;
     357                  }
     358               }
     359            }
     360         }
     361         else {
     362            /* Don't update Z buffer */
     363            GLuint i;
     364            for (i=0;i<n;i++) {
     365               if (mask[i]) {
     366                  if (z[i] == zbuffer[i]) {
     367                     /* pass */
     368                     passed++;
     369                  }
     370                  else {
     371                     mask[i] = 0;
     372                  }
     373               }
     374            }
     375         }
     376         break;
    347377      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;
     378         if (ctx->Depth.Mask) {
     379            /* Update Z buffer */
     380            GLuint i;
     381            for (i=0;i<n;i++) {
     382               if (mask[i]) {
     383                  zbuffer[i] = z[i];
     384                  passed++;
     385               }
     386            }
     387         }
     388         else {
     389            /* Don't update Z buffer or mask */
     390            passed = n;
     391         }
     392         break;
    362393      case GL_NEVER:
    363          for (i=0;i<n;i++) {
    364             mask[i] = 0;
    365          }
    366          break;
     394         MEMSET(mask, 0, n * sizeof(GLubyte));
     395         break;
    367396      default:
    368          gl_problem(ctx, "Bad depth func in gl_depth_test_span_generic");
    369    } /*switch*/
     397         gl_problem(ctx, "Bad depth func in depth_test_span16");
     398   }
    370399
    371400   return passed;
     
    373402
    374403
    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[] )
     404static GLuint
     405depth_test_span32( GLcontext *ctx, GLuint n, GLint x, GLint y,
     406                   GLuint zbuffer[], const GLdepth z[], GLubyte mask[] )
    382407{
    383    GLdepth *zptr = Z_ADDRESS( ctx, x, y );
    384    GLuint i;
    385408   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;
    455409
    456410   /* switch cases ordered from most frequent to less frequent */
     
    458412      case GL_LESS:
    459413         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;
     414            /* Update Z buffer */
     415            GLuint i;
     416            for (i=0; i<n; i++) {
     417               if (mask[i]) {
     418                  if (z[i] < zbuffer[i]) {
     419                     /* pass */
     420                     zbuffer[i] = z[i];
     421                     passed++;
     422                  }
     423                  else {
     424                     /* fail */
     425                     mask[i] = 0;
     426                  }
     427               }
     428            }
     429         }
     430         else {
     431            /* Don't update Z buffer */
     432            GLuint i;
     433            for (i=0; i<n; i++) {
     434               if (mask[i]) {
     435                  if (z[i] < zbuffer[i]) {
     436                     /* pass */
     437                     passed++;
     438                  }
     439                  else {
     440                     mask[i] = 0;
     441                  }
     442               }
     443            }
     444         }
     445         break;
    491446      case GL_LEQUAL:
    492447         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;
     448            /* Update Z buffer */
     449            GLuint i;
     450            for (i=0;i<n;i++) {
     451               if (mask[i]) {
     452                  if (z[i] <= zbuffer[i]) {
     453                     zbuffer[i] = z[i];
     454                     passed++;
     455                  }
     456                  else {
     457                     mask[i] = 0;
     458                  }
     459               }
     460            }
     461         }
     462         else {
     463            /* Don't update Z buffer */
     464            GLuint i;
     465            for (i=0;i<n;i++) {
     466               if (mask[i]) {
     467                  if (z[i] <= zbuffer[i]) {
     468                     /* pass */
     469                     passed++;
     470                  }
     471                  else {
     472                     mask[i] = 0;
     473                  }
     474               }
     475            }
     476         }
     477         break;
    524478      case GL_GEQUAL:
    525479         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;
     480            /* Update Z buffer */
     481            GLuint i;
     482            for (i=0;i<n;i++) {
     483               if (mask[i]) {
     484                  if (z[i] >= zbuffer[i]) {
     485                     zbuffer[i] = z[i];
     486                     passed++;
     487                  }
     488                  else {
     489                     mask[i] = 0;
     490                  }
     491               }
     492            }
     493         }
     494         else {
     495            /* Don't update Z buffer */
     496            GLuint i;
     497            for (i=0;i<n;i++) {
     498               if (mask[i]) {
     499                  if (z[i] >= zbuffer[i]) {
     500                     /* pass */
     501                     passed++;
     502                  }
     503                  else {
     504                     mask[i] = 0;
     505                  }
     506               }
     507            }
     508         }
     509         break;
    557510      case GL_GREATER:
    558511         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;
     512            /* Update Z buffer */
     513            GLuint i;
     514            for (i=0;i<n;i++) {
     515               if (mask[i]) {
     516                  if (z[i] > zbuffer[i]) {
     517                     zbuffer[i] = z[i];
     518                     passed++;
     519                  }
     520                  else {
     521                     mask[i] = 0;
     522                  }
     523               }
     524            }
     525         }
     526         else {
     527            /* Don't update Z buffer */
     528            GLuint i;
     529            for (i=0;i<n;i++) {
     530               if (mask[i]) {
     531                  if (z[i] > zbuffer[i]) {
     532                     /* pass */
     533                     passed++;
     534                  }
     535                  else {
     536                     mask[i] = 0;
     537                  }
     538               }
     539            }
     540         }
     541         break;
    590542      case GL_NOTEQUAL:
    591543         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;
     544            /* Update Z buffer */
     545            GLuint i;
     546            for (i=0;i<n;i++) {
     547               if (mask[i]) {
     548                  if (z[i] != zbuffer[i]) {
     549                     zbuffer[i] = z[i];
     550                     passed++;
     551                  }
     552                  else {
     553                     mask[i] = 0;
     554                  }
     555               }
     556            }
     557         }
     558         else {
     559            /* Don't update Z buffer */
     560            GLuint i;
     561            for (i=0;i<n;i++) {
     562               if (mask[i]) {
     563                  if (z[i] != zbuffer[i]) {
     564                     /* pass */
     565                     passed++;
     566                  }
     567                  else {
     568                     mask[i] = 0;
     569                  }
     570               }
     571            }
     572         }
     573         break;
    623574      case GL_EQUAL:
    624575         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;
     576            /* Update Z buffer */
     577            GLuint i;
     578            for (i=0;i<n;i++) {
     579               if (mask[i]) {
     580                  if (z[i] == zbuffer[i]) {
     581                     zbuffer[i] = z[i];
     582                     passed++;
     583                  }
     584                  else {
     585                     mask[i] = 0;
     586                  }
     587               }
     588            }
     589         }
     590         else {
     591            /* Don't update Z buffer */
     592            GLuint i;
     593            for (i=0;i<n;i++) {
     594               if (mask[i]) {
     595                  if (z[i] == zbuffer[i]) {
     596                     /* pass */
     597                     passed++;
     598                  }
     599                  else {
     600                     mask[i] = 0;
     601                  }
     602               }
     603            }
     604         }
     605         break;
    656606      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;
     607         if (ctx->Depth.Mask) {
     608            /* Update Z buffer */
     609            GLuint i;
     610            for (i=0;i<n;i++) {
     611               if (mask[i]) {
     612                  zbuffer[i] = z[i];
     613                  passed++;
     614               }
     615            }
     616         }
     617         else {
     618            /* Don't update Z buffer or mask */
     619            passed = n;
     620         }
     621         break;
    670622      case GL_NEVER:
    671          /* depth test never passes */
    672          for (i=0;i<n;i++) {
    673             mask[i] = 0;
    674          }
    675          break;
     623         MEMSET(mask, 0, n * sizeof(GLubyte));
     624         break;
    676625      default:
    677          gl_problem(ctx, "Bad depth func in gl_depth_test_pixels_generic");
    678    } /*switch*/
     626         gl_problem(ctx, "Bad depth func in depth_test_span32");
     627   }
     628
     629   return passed;
    679630}
    680631
     
    682633
    683634/*
    684  * glDepthFunc( GL_LESS ) and glDepthMask( GL_TRUE ).
     635 * Apply depth test to span of fragments.  Hardware or software z buffer.
    685636 */
    686 void gl_depth_test_pixels_less( GLcontext* ctx,
    687                                 GLuint n, const GLint x[], const GLint y[],
    688                                 const GLdepth z[], GLubyte mask[] )
     637GLuint
     638_mesa_depth_test_span( GLcontext *ctx, GLuint n, GLint x, GLint y,
     639                       const GLdepth z[], GLubyte mask[] )
    689640{
    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          }
     641   if (ctx->Driver.ReadDepthSpan) {
     642      /* hardware-based depth buffer */
     643      GLdepth zbuffer[MAX_WIDTH];
     644      GLuint passed;
     645      (*ctx->Driver.ReadDepthSpan)(ctx, n, x, y, zbuffer);
     646      passed = depth_test_span32(ctx, n, x, y, zbuffer, z, mask);
     647      ASSERT(ctx->Driver.WriteDepthSpan);
     648      (*ctx->Driver.WriteDepthSpan)(ctx, n, x, y, zbuffer, mask);
     649      return passed;
     650   }
     651   else {
     652      /* software depth buffer */
     653      if (ctx->Visual->DepthBits <= 16) {
     654         GLushort *zptr = (GLushort *) Z_ADDRESS16(ctx, x, y);
     655         GLuint passed = depth_test_span16(ctx, n, x, y, zptr, z, mask);
     656         return passed;
    704657      }
     658      else {
     659         GLuint *zptr = (GLuint *) Z_ADDRESS32(ctx, x, y);
     660         GLuint passed = depth_test_span32(ctx, n, x, y, zptr, z, mask);
     661         return passed;
     662      }
    705663   }
    706664}
    707665
    708666
     667
     668
    709669/*
    710  * glDepthFunc( GL_GREATER ) and glDepthMask( GL_TRUE ).
     670 * Do depth testing for an array of fragments using software Z buffer.
    711671 */
    712 void gl_depth_test_pixels_greater( GLcontext* ctx,
    713                                    GLuint n, const GLint x[], const GLint y[],
    714                                    const GLdepth z[], GLubyte mask[] )
     672static void
     673software_depth_test_pixels16( GLcontext *ctx, GLuint n,
     674                              const GLint x[], const GLint y[],
     675                              const GLdepth z[], GLubyte mask[] )
    715676{
    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       }
     677   /* switch cases ordered from most frequent to less frequent */
     678   switch (ctx->Depth.Func) {
     679      case GL_LESS:
     680         if (ctx->Depth.Mask) {
     681            /* Update Z buffer */
     682            GLuint i;
     683            for (i=0; i<n; i++) {
     684               if (mask[i]) {
     685                  GLushort *zptr = Z_ADDRESS16(ctx,x[i],y[i]);
     686                  if (z[i] < *zptr) {
     687                     /* pass */
     688                     *zptr = z[i];
     689                  }
     690                  else {
     691                     /* fail */
     692                     mask[i] = 0;
     693                  }
     694               }
     695            }
     696         }
     697         else {
     698            /* Don't update Z buffer */
     699            GLuint i;
     700            for (i=0; i<n; i++) {
     701               if (mask[i]) {
     702                  GLushort *zptr = Z_ADDRESS16(ctx,x[i],y[i]);
     703                  if (z[i] < *zptr) {
     704                     /* pass */
     705                  }
     706                  else {
     707                     /* fail */
     708                     mask[i] = 0;
     709                  }
     710               }
     711            }
     712         }
     713         break;
     714      case GL_LEQUAL:
     715         if (ctx->Depth.Mask) {
     716            /* Update Z buffer */
     717            GLuint i;
     718            for (i=0; i<n; i++) {
     719               if (mask[i]) {
     720                  GLushort *zptr = Z_ADDRESS16(ctx,x[i],y[i]);
     721                  if (z[i] <= *zptr) {
     722                     /* pass */
     723                     *zptr = z[i];
     724                  }
     725                  else {
     726                     /* fail */
     727                     mask[i] = 0;
     728                  }
     729               }
     730            }
     731         }
     732         else {
     733            /* Don't update Z buffer */
     734            GLuint i;
     735            for (i=0; i<n; i++) {
     736               if (mask[i]) {
     737                  GLushort *zptr = Z_ADDRESS16(ctx,x[i],y[i]);
     738                  if (z[i] <= *zptr) {
     739                     /* pass */
     740                  }
     741                  else {
     742                     /* fail */
     743                     mask[i] = 0;
     744                  }
     745               }
     746            }
     747         }
     748         break;
     749      case GL_GEQUAL:
     750         if (ctx->Depth.Mask) {
     751            /* Update Z buffer */
     752            GLuint i;
     753            for (i=0; i<n; i++) {
     754               if (mask[i]) {
     755                  GLushort *zptr = Z_ADDRESS16(ctx,x[i],y[i]);
     756                  if (z[i] >= *zptr) {
     757                     /* pass */
     758                     *zptr = z[i];
     759                  }
     760                  else {
     761                     /* fail */
     762                     mask[i] = 0;
     763                  }
     764               }
     765            }
     766         }
     767         else {
     768            /* Don't update Z buffer */
     769            GLuint i;
     770            for (i=0; i<n; i++) {
     771               if (mask[i]) {
     772                  GLushort *zptr = Z_ADDRESS16(ctx,x[i],y[i]);
     773                  if (z[i] >= *zptr) {
     774                     /* pass */
     775                  }
     776                  else {
     777                     /* fail */
     778                     mask[i] = 0;
     779                  }
     780               }
     781            }
     782         }
     783         break;
     784      case GL_GREATER:
     785         if (ctx->Depth.Mask) {
     786            /* Update Z buffer */
     787            GLuint i;
     788            for (i=0; i<n; i++) {
     789               if (mask[i]) {
     790                  GLushort *zptr = Z_ADDRESS16(ctx,x[i],y[i]);
     791                  if (z[i] > *zptr) {
     792                     /* pass */
     793                     *zptr = z[i];
     794                  }
     795                  else {
     796                     /* fail */
     797                     mask[i] = 0;
     798                  }
     799               }
     800            }
     801         }
     802         else {
     803            /* Don't update Z buffer */
     804            GLuint i;
     805            for (i=0; i<n; i++) {
     806               if (mask[i]) {
     807                  GLushort *zptr = Z_ADDRESS16(ctx,x[i],y[i]);
     808                  if (z[i] > *zptr) {
     809                     /* pass */
     810                  }
     811                  else {
     812                     /* fail */
     813                     mask[i] = 0;
     814                  }
     815               }
     816            }
     817         }
     818         break;
     819      case GL_NOTEQUAL:
     820         if (ctx->Depth.Mask) {
     821            /* Update Z buffer */
     822            GLuint i;
     823            for (i=0; i<n; i++) {
     824               if (mask[i]) {
     825                  GLushort *zptr = Z_ADDRESS16(ctx,x[i],y[i]);
     826                  if (z[i] != *zptr) {
     827                     /* pass */
     828                     *zptr = z[i];
     829                  }
     830                  else {
     831                     /* fail */
     832                     mask[i] = 0;
     833                  }
     834               }
     835            }
     836         }
     837         else {
     838            /* Don't update Z buffer */
     839            GLuint i;
     840            for (i=0; i<n; i++) {
     841               if (mask[i]) {
     842                  GLushort *zptr = Z_ADDRESS16(ctx,x[i],y[i]);
     843                  if (z[i] != *zptr) {
     844                     /* pass */
     845                  }
     846                  else {
     847                     /* fail */
     848                     mask[i] = 0;
     849                  }
     850               }
     851            }
     852         }
     853         break;
     854      case GL_EQUAL:
     855         if (ctx->Depth.Mask) {
     856            /* Update Z buffer */
     857            GLuint i;
     858            for (i=0; i<n; i++) {
     859               if (mask[i]) {
     860                  GLushort *zptr = Z_ADDRESS16(ctx,x[i],y[i]);
     861                  if (z[i] == *zptr) {
     862                     /* pass */
     863                     *zptr = z[i];
     864                  }
     865                  else {
     866                     /* fail */
     867                     mask[i] = 0;
     868                  }
     869               }
     870            }
     871         }
     872         else {
     873            /* Don't update Z buffer */
     874            GLuint i;
     875            for (i=0; i<n; i++) {
     876               if (mask[i]) {
     877                  GLushort *zptr = Z_ADDRESS16(ctx,x[i],y[i]);
     878                  if (z[i] == *zptr) {
     879                     /* pass */
     880                  }
     881                  else {
     882                     /* fail */
     883                     mask[i] = 0;
     884                  }
     885               }
     886            }
     887         }
     888         break;
     889      case GL_ALWAYS:
     890         if (ctx->Depth.Mask) {
     891            /* Update Z buffer */
     892            GLuint i;
     893            for (i=0; i<n; i++) {
     894               if (mask[i]) {
     895                  GLushort *zptr = Z_ADDRESS16(ctx,x[i],y[i]);
     896                  *zptr = z[i];
     897               }
     898            }
     899         }
     900         else {
     901            /* Don't update Z buffer or mask */
     902         }
     903         break;
     904      case GL_NEVER:
     905         /* depth test never passes */
     906         MEMSET(mask, 0, n * sizeof(GLubyte));
     907         break;
     908      default:
     909         gl_problem(ctx, "Bad depth func in software_depth_test_pixels");
    731910   }
    732911}
     912
     913
     914
     915/*
     916 * Do depth testing for an array of fragments using software Z buffer.
     917 */
     918static void
     919software_depth_test_pixels32( GLcontext *ctx, GLuint n,
     920                              const GLint x[], const GLint y[],
     921                              const GLdepth z[], GLubyte mask[] )
     922{
     923   /* switch cases ordered from most frequent to less frequent */
     924   switch (ctx->Depth.Func) {
     925      case GL_LESS:
     926         if (ctx->Depth.Mask) {
     927            /* Update Z buffer */
     928            GLuint i;
     929            for (i=0; i<n; i++) {
     930               if (mask[i]) {
     931                  GLuint *zptr = Z_ADDRESS32(ctx,x[i],y[i]);
     932                  if (z[i] < *zptr) {
     933                     /* pass */
     934                     *zptr = z[i];
     935                  }
     936                  else {
     937                     /* fail */
     938                     mask[i] = 0;
     939                  }
     940               }
     941            }
     942         }
     943         else {
     944            /* Don't update Z buffer */
     945            GLuint i;
     946            for (i=0; i<n; i++) {
     947               if (mask[i]) {
     948                  GLuint *zptr = Z_ADDRESS32(ctx,x[i],y[i]);
     949                  if (z[i] < *zptr) {
     950                     /* pass */
     951                  }
     952                  else {
     953                     /* fail */
     954                     mask[i] = 0;
     955                  }
     956               }
     957            }
     958         }
     959         break;
     960      case GL_LEQUAL:
     961         if (ctx->Depth.Mask) {
     962            /* Update Z buffer */
     963            GLuint i;
     964            for (i=0; i<n; i++) {
     965               if (mask[i]) {
     966                  GLuint *zptr = Z_ADDRESS32(ctx,x[i],y[i]);
     967                  if (z[i] <= *zptr) {
     968                     /* pass */
     969                     *zptr = z[i];
     970                  }
     971                  else {
     972                     /* fail */
     973                     mask[i] = 0;
     974                  }
     975               }
     976            }
     977         }
     978         else {
     979            /* Don't update Z buffer */
     980            GLuint i;
     981            for (i=0; i<n; i++) {
     982               if (mask[i]) {
     983                  GLuint *zptr = Z_ADDRESS32(ctx,x[i],y[i]);
     984                  if (z[i] <= *zptr) {
     985                     /* pass */
     986                  }
     987                  else {
     988                     /* fail */
     989                     mask[i] = 0;
     990                  }
     991               }
     992            }
     993         }
     994         break;
     995      case GL_GEQUAL:
     996         if (ctx->Depth.Mask) {
     997            /* Update Z buffer */
     998            GLuint i;
     999            for (i=0; i<n; i++) {
     1000               if (mask[i]) {
     1001                  GLuint *zptr = Z_ADDRESS32(ctx,x[i],y[i]);
     1002                  if (z[i] >= *zptr) {
     1003                     /* pass */
     1004                     *zptr = z[i];
     1005                  }
     1006                  else {
     1007                     /* fail */
     1008                     mask[i] = 0;
     1009                  }
     1010               }
     1011            }
     1012         }
     1013         else {
     1014            /* Don't update Z buffer */
     1015            GLuint i;
     1016            for (i=0; i<n; i++) {
     1017               if (mask[i]) {
     1018                  GLuint *zptr = Z_ADDRESS32(ctx,x[i],y[i]);
     1019                  if (z[i] >= *zptr) {
     1020                     /* pass */
     1021                  }
     1022                  else {
     1023                     /* fail */
     1024                     mask[i] = 0;
     1025                  }
     1026               }
     1027            }
     1028         }
     1029         break;
     1030      case GL_GREATER:
     1031         if (ctx->Depth.Mask) {
     1032            /* Update Z buffer */
     1033            GLuint i;
     1034            for (i=0; i<n; i++) {
     1035               if (mask[i]) {
     1036                  GLuint *zptr = Z_ADDRESS32(ctx,x[i],y[i]);
     1037                  if (z[i] > *zptr) {
     1038                     /* pass */
     1039                     *zptr = z[i];
     1040                  }
     1041                  else {
     1042                     /* fail */
     1043                     mask[i] = 0;
     1044                  }
     1045               }
     1046            }
     1047         }
     1048         else {
     1049            /* Don't update Z buffer */
     1050            GLuint i;
     1051            for (i=0; i<n; i++) {
     1052               if (mask[i]) {
     1053                  GLuint *zptr = Z_ADDRESS32(ctx,x[i],y[i]);
     1054                  if (z[i] > *zptr) {
     1055                     /* pass */
     1056                  }
     1057                  else {
     1058                     /* fail */
     1059                     mask[i] = 0;
     1060                  }
     1061               }
     1062            }
     1063         }
     1064         break;
     1065      case GL_NOTEQUAL:
     1066         if (ctx->Depth.Mask) {
     1067            /* Update Z buffer */
     1068            GLuint i;
     1069            for (i=0; i<n; i++) {
     1070               if (mask[i]) {
     1071                  GLuint *zptr = Z_ADDRESS32(ctx,x[i],y[i]);
     1072                  if (z[i] != *zptr) {
     1073                     /* pass */
     1074                     *zptr = z[i];
     1075                  }
     1076                  else {
     1077                     /* fail */
     1078                     mask[i] = 0;
     1079                  }
     1080               }
     1081            }
     1082         }
     1083         else {
     1084            /* Don't update Z buffer */
     1085            GLuint i;
     1086            for (i=0; i<n; i++) {
     1087               if (mask[i]) {
     1088                  GLuint *zptr = Z_ADDRESS32(ctx,x[i],y[i]);
     1089                  if (z[i] != *zptr) {
     1090                     /* pass */
     1091                  }
     1092                  else {
     1093                     /* fail */
     1094                     mask[i] = 0;
     1095                  }
     1096               }
     1097            }
     1098         }
     1099         break;
     1100      case GL_EQUAL:
     1101         if (ctx->Depth.Mask) {
     1102            /* Update Z buffer */
     1103            GLuint i;
     1104            for (i=0; i<n; i++) {
     1105               if (mask[i]) {
     1106                  GLuint *zptr = Z_ADDRESS32(ctx,x[i],y[i]);
     1107                  if (z[i] == *zptr) {
     1108                     /* pass */
     1109                     *zptr = z[i];
     1110                  }
     1111                  else {
     1112                     /* fail */
     1113                     mask[i] = 0;
     1114                  }
     1115               }
     1116            }
     1117         }
     1118         else {
     1119            /* Don't update Z buffer */
     1120            GLuint i;
     1121            for (i=0; i<n; i++) {
     1122               if (mask[i]) {
     1123                  GLuint *zptr = Z_ADDRESS32(ctx,x[i],y[i]);
     1124                  if (z[i] == *zptr) {
     1125                     /* pass */
     1126                  }
     1127                  else {
     1128                     /* fail */
     1129                     mask[i] = 0;
     1130                  }
     1131               }
     1132            }
     1133         }
     1134         break;
     1135      case GL_ALWAYS:
     1136         if (ctx->Depth.Mask) {
     1137            /* Update Z buffer */
     1138            GLuint i;
     1139            for (i=0; i<n; i++) {
     1140               if (mask[i]) {
     1141                  GLuint *zptr = Z_ADDRESS32(ctx,x[i],y[i]);
     1142                  *zptr = z[i];
     1143               }
     1144            }
     1145         }
     1146         else {
     1147            /* Don't update Z buffer or mask */
     1148         }
     1149         break;
     1150      case GL_NEVER:
     1151         /* depth test never passes */
     1152         MEMSET(mask, 0, n * sizeof(GLubyte));
     1153         break;
     1154      default:
     1155         gl_problem(ctx, "Bad depth func in software_depth_test_pixels");
     1156   }
     1157}
     1158
     1159
     1160
     1161/*
     1162 * Do depth testing for an array of pixels using hardware Z buffer.
     1163 * Input/output:  zbuffer - array of depth values from Z buffer
     1164 * Input:  z - array of fragment z values.
     1165 */
     1166static void
     1167hardware_depth_test_pixels( GLcontext *ctx, GLuint n, GLdepth zbuffer[],
     1168                            const GLdepth z[], GLubyte mask[] )
     1169{
     1170   /* switch cases ordered from most frequent to less frequent */
     1171   switch (ctx->Depth.Func) {
     1172      case GL_LESS:
     1173         if (ctx->Depth.Mask) {
     1174            /* Update Z buffer */
     1175            GLuint i;
     1176            for (i=0; i<n; i++) {
     1177               if (mask[i]) {
     1178                  if (z[i] < zbuffer[i]) {
     1179                     /* pass */
     1180                     zbuffer[i] = z[i];
     1181                  }
     1182                  else {
     1183                     /* fail */
     1184                     mask[i] = 0;
     1185                  }
     1186               }
     1187            }
     1188         }
     1189         else {
     1190            /* Don't update Z buffer */
     1191            GLuint i;
     1192            for (i=0; i<n; i++) {
     1193               if (mask[i]) {
     1194                  if (z[i] < zbuffer[i]) {
     1195                     /* pass */
     1196                  }
     1197                  else {
     1198                     /* fail */
     1199                     mask[i] = 0;
     1200                  }
     1201               }
     1202            }
     1203         }
     1204         break;
     1205      case GL_LEQUAL:
     1206         if (ctx->Depth.Mask) {
     1207            /* Update Z buffer */
     1208            GLuint i;
     1209            for (i=0; i<n; i++) {
     1210               if (mask[i]) {
     1211                  if (z[i] <= zbuffer[i]) {
     1212                     /* pass */
     1213                     zbuffer[i] = z[i];
     1214                  }
     1215                  else {
     1216                     /* fail */
     1217                     mask[i] = 0;
     1218                  }
     1219               }
     1220            }
     1221         }
     1222         else {
     1223            /* Don't update Z buffer */
     1224            GLuint i;
     1225            for (i=0; i<n; i++) {
     1226               if (mask[i]) {
     1227                  if (z[i] <= zbuffer[i]) {
     1228                     /* pass */
     1229                  }
     1230                  else {
     1231                     /* fail */
     1232                     mask[i] = 0;
     1233                  }
     1234               }
     1235            }
     1236         }
     1237         break;
     1238      case GL_GEQUAL:
     1239         if (ctx->Depth.Mask) {
     1240            /* Update Z buffer */
     1241            GLuint i;
     1242            for (i=0; i<n; i++) {
     1243               if (mask[i]) {
     1244                  if (z[i] >= zbuffer[i]) {
     1245                     /* pass */
     1246                     zbuffer[i] = z[i];
     1247                  }
     1248                  else {
     1249                     /* fail */
     1250                     mask[i] = 0;
     1251                  }
     1252               }
     1253            }
     1254         }
     1255         else {
     1256            /* Don't update Z buffer */
     1257            GLuint i;
     1258            for (i=0; i<n; i++) {
     1259               if (mask[i]) {
     1260                  if (z[i] >= zbuffer[i]) {
     1261                     /* pass */
     1262                  }
     1263                  else {
     1264                     /* fail */
     1265                     mask[i] = 0;
     1266                  }
     1267               }
     1268            }
     1269         }
     1270         break;
     1271      case GL_GREATER:
     1272         if (ctx->Depth.Mask) {
     1273            /* Update Z buffer */
     1274            GLuint i;
     1275            for (i=0; i<n; i++) {
     1276               if (mask[i]) {
     1277                  if (z[i] > zbuffer[i]) {
     1278                     /* pass */
     1279                     zbuffer[i] = z[i];
     1280                  }
     1281                  else {
     1282                     /* fail */
     1283                     mask[i] = 0;
     1284                  }
     1285               }
     1286            }
     1287         }
     1288         else {
     1289            /* Don't update Z buffer */
     1290            GLuint i;
     1291            for (i=0; i<n; i++) {
     1292               if (mask[i]) {
     1293                  if (z[i] > zbuffer[i]) {
     1294                     /* pass */
     1295                  }
     1296                  else {
     1297                     /* fail */
     1298                     mask[i] = 0;
     1299                  }
     1300               }
     1301            }
     1302         }
     1303         break;
     1304      case GL_NOTEQUAL:
     1305         if (ctx->Depth.Mask) {
     1306            /* Update Z buffer */
     1307            GLuint i;
     1308            for (i=0; i<n; i++) {
     1309               if (mask[i]) {
     1310                  if (z[i] != zbuffer[i]) {
     1311                     /* pass */
     1312                     zbuffer[i] = z[i];
     1313                  }
     1314                  else {
     1315                     /* fail */
     1316                     mask[i] = 0;
     1317                  }
     1318               }
     1319            }
     1320         }
     1321         else {
     1322            /* Don't update Z buffer */
     1323            GLuint i;
     1324            for (i=0; i<n; i++) {
     1325               if (mask[i]) {
     1326                  if (z[i] != zbuffer[i]) {
     1327                     /* pass */
     1328                  }
     1329                  else {
     1330                     /* fail */
     1331                     mask[i] = 0;
     1332                  }
     1333               }
     1334            }
     1335         }
     1336         break;
     1337      case GL_EQUAL:
     1338         if (ctx->Depth.Mask) {
     1339            /* Update Z buffer */
     1340            GLuint i;
     1341            for (i=0; i<n; i++) {
     1342               if (mask[i]) {
     1343                  if (z[i] == zbuffer[i]) {
     1344                     /* pass */
     1345                     zbuffer[i] = z[i];
     1346                  }
     1347                  else {
     1348                     /* fail */
     1349                     mask[i] = 0;
     1350                  }
     1351               }
     1352            }
     1353         }
     1354         else {
     1355            /* Don't update Z buffer */
     1356            GLuint i;
     1357            for (i=0; i<n; i++) {
     1358               if (mask[i]) {
     1359                  if (z[i] == zbuffer[i]) {
     1360                     /* pass */
     1361                  }
     1362                  else {
     1363                     /* fail */
     1364                     mask[i] = 0;
     1365                  }
     1366               }
     1367            }
     1368         }
     1369         break;
     1370      case GL_ALWAYS:
     1371         if (ctx->Depth.Mask) {
     1372            /* Update Z buffer */
     1373            GLuint i;
     1374            for (i=0; i<n; i++) {
     1375               if (mask[i]) {
     1376                  zbuffer[i] = z[i];
     1377               }
     1378            }
     1379         }
     1380         else {
     1381            /* Don't update Z buffer or mask */
     1382         }
     1383         break;
     1384      case GL_NEVER:
     1385         /* depth test never passes */
     1386         MEMSET(mask, 0, n * sizeof(GLubyte));
     1387         break;
     1388      default:
     1389         gl_problem(ctx, "Bad depth func in hardware_depth_test_pixels");
     1390   }
     1391}
     1392
     1393
     1394
     1395void
     1396_mesa_depth_test_pixels( GLcontext *ctx,
     1397                         GLuint n, const GLint x[], const GLint y[],
     1398                         const GLdepth z[], GLubyte mask[] )
     1399{
     1400   if (ctx->Driver.ReadDepthPixels) {
     1401      /* read depth values from hardware Z buffer */
     1402      GLdepth zbuffer[PB_SIZE];
     1403      (*ctx->Driver.ReadDepthPixels)(ctx, n, x, y, zbuffer);
     1404
     1405      hardware_depth_test_pixels( ctx, n, zbuffer, z, mask );
     1406
     1407      /* update hardware Z buffer with new values */
     1408      ASSERT(ctx->Driver.WriteDepthPixels);
     1409      (*ctx->Driver.WriteDepthPixels)(ctx, n, x, y, zbuffer, mask );
     1410   }
     1411   else {
     1412      /* software depth testing */
     1413      if (ctx->Visual->DepthBits <= 16)
     1414         software_depth_test_pixels16(ctx, n, x, y, z, mask);
     1415      else
     1416         software_depth_test_pixels32(ctx, n, x, y, z, mask);
     1417   }
     1418}
     1419
    7331420
    7341421
     
    7421429/*
    7431430 * 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()
     1431 * This is used for both hardware and software depth buffers.
    7451432 * Input:  n - how many pixels
    7461433 *         x,y - location of first pixel
    7471434 * Output:  depth - the array of depth values
    7481435 */
    749 void gl_read_depth_span_float( GLcontext* ctx,
    750                                GLuint n, GLint x, GLint y, GLfloat depth[] )
     1436void
     1437_mesa_read_depth_span_float( GLcontext* ctx,
     1438                             GLuint n, GLint x, GLint y, GLfloat depth[] )
    7511439{
    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;
     1440   const GLfloat scale = 1.0F / ctx->Visual->DepthMaxF;
     1441
     1442   if (ctx->DrawBuffer->DepthBuffer) {
     1443      /* read from software depth buffer */
     1444      if (ctx->Visual->DepthBits <= 16) {
     1445         const GLushort *zptr = Z_ADDRESS16( ctx, x, y );
     1446         GLuint i;
     1447         for (i = 0; i < n; i++) {
     1448            depth[i] = (GLfloat) zptr[i] * scale;
     1449         }
    7621450      }
     1451      else {
     1452         const GLuint *zptr = Z_ADDRESS32( ctx, x, y );
     1453         GLuint i;
     1454         for (i = 0; i < n; i++) {
     1455            depth[i] = (GLfloat) zptr[i] * scale;
     1456         }
     1457      }
     1458   }
     1459   else if (ctx->Driver.ReadDepthSpan) {
     1460      /* read from hardware depth buffer */
     1461      GLdepth d[MAX_WIDTH];
     1462      GLuint i;
     1463      ASSERT(n <= MAX_WIDTH);
     1464      (*ctx->Driver.ReadDepthSpan)( ctx, n, x, y, d );
     1465      for (i = 0; i < n; i++) {
     1466         depth[i] = d[i] * scale;
     1467      }
    7631468   }
    7641469   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       }
     1470      /* no depth buffer */
     1471      MEMSET(depth, 0, n * sizeof(GLfloat));
    7921472   }
    7931473}
     
    8061486 * This function is only called through Driver.alloc_depth_buffer.
    8071487 */
    808 void gl_alloc_depth_buffer( GLcontext* ctx )
     1488void
     1489_mesa_alloc_depth_buffer( GLcontext *ctx )
    8091490{
    8101491   /* 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" );
     1492   if (ctx->DrawBuffer->UseSoftwareDepthBuffer) {
     1493      GLint bytesPerValue;
     1494
     1495      if (ctx->DrawBuffer->DepthBuffer) {
     1496         FREE(ctx->DrawBuffer->DepthBuffer);
     1497         ctx->DrawBuffer->DepthBuffer = NULL;
     1498      }
     1499
     1500      /* allocate new depth buffer, but don't initialize it */
     1501      if (ctx->Visual->DepthBits <= 16)
     1502         bytesPerValue = sizeof(GLushort);
     1503      else
     1504         bytesPerValue = sizeof(GLuint);
     1505
     1506      ctx->DrawBuffer->DepthBuffer = MALLOC( ctx->DrawBuffer->Width
     1507                                             * ctx->DrawBuffer->Height
     1508                                             * bytesPerValue );
     1509
     1510      if (!ctx->DrawBuffer->DepthBuffer) {
     1511         /* out of memory */
     1512         ctx->Depth.Test = GL_FALSE;
     1513         ctx->NewState |= NEW_RASTER_OPS;
     1514         gl_error( ctx, GL_OUT_OF_MEMORY, "Couldn't allocate depth buffer" );
     1515      }
    8251516   }
    8261517}
     
    8341525 * This function is only called through Driver.clear_depth_buffer.
    8351526 */
    836 void gl_clear_depth_buffer( GLcontext* ctx )
     1527void
     1528_mesa_clear_depth_buffer( GLcontext *ctx )
    8371529{
    838    GLdepth clear_value = (GLdepth) (ctx->Depth.Clear * DEPTH_SCALE);
    839 
    840    if (ctx->Visual->DepthBits==0 || !ctx->Buffer->Depth || !ctx->Depth.Mask) {
     1530   if (ctx->Visual->DepthBits == 0
     1531       || !ctx->DrawBuffer->DepthBuffer
     1532       || !ctx->Depth.Mask) {
    8411533      /* no depth buffer, or writing to it is disabled */
    8421534      return;
     
    8491541   if (ctx->Scissor.Enabled) {
    8501542      /* 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);
     1543      if (ctx->Visual->DepthBits <= 16) {
     1544         const GLushort clearValue = (GLushort) (ctx->Depth.Clear * ctx->Visual->DepthMax);
     1545         const GLint rows = ctx->DrawBuffer->Ymax - ctx->DrawBuffer->Ymin + 1;
     1546         const GLint width = ctx->DrawBuffer->Width;
     1547         GLushort *dRow = (GLushort *) ctx->DrawBuffer->DepthBuffer
     1548            + ctx->DrawBuffer->Ymin * width + ctx->DrawBuffer->Xmin;
     1549         GLint i, j;
     1550         for (i = 0; i < rows; i++) {
     1551            for (j = 0; j < width; j++) {
     1552               dRow[j] = clearValue;
     1553            }
     1554            dRow += width;
     1555         }
     1556      }
     1557      else {
     1558         const GLuint clearValue = (GLuint) (ctx->Depth.Clear * ctx->Visual->DepthMax);
     1559         const GLint rows = ctx->DrawBuffer->Ymax - ctx->DrawBuffer->Ymin + 1;
     1560         const GLint width = ctx->DrawBuffer->Width;
     1561         GLuint *dRow = (GLuint *) ctx->DrawBuffer->DepthBuffer
     1562            + ctx->DrawBuffer->Ymin * width + ctx->DrawBuffer->Xmin;
     1563         GLint i, j;
     1564         for (i = 0; i < rows; i++) {
     1565            for (j = 0; j < width; j++) {
     1566               dRow[j] = clearValue;
     1567            }
     1568            dRow += width;
     1569         }
    8591570      }
    8601571   }
    8611572   else {
    8621573      /* 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);
     1574      if (ctx->Visual->DepthBits <= 16) {
     1575         const GLushort clearValue = (GLushort) (ctx->Depth.Clear * ctx->Visual->DepthMax);
     1576         if ((clearValue & 0xff) == (clearValue >> 8)) {
     1577            /* lower and upper bytes of clear_value are same, use MEMSET */
     1578            MEMSET( ctx->DrawBuffer->DepthBuffer, clearValue & 0xff,
     1579                    2 * ctx->DrawBuffer->Width * ctx->DrawBuffer->Height);
     1580         }
     1581         else {
     1582            GLushort *d = (GLushort *) ctx->DrawBuffer->DepthBuffer;
     1583            GLint n = ctx->DrawBuffer->Width * ctx->DrawBuffer->Height;
     1584            while (n >= 16) {
     1585               d[0] = clearValue;    d[1] = clearValue;
     1586               d[2] = clearValue;    d[3] = clearValue;
     1587               d[4] = clearValue;    d[5] = clearValue;
     1588               d[6] = clearValue;    d[7] = clearValue;
     1589               d[8] = clearValue;    d[9] = clearValue;
     1590               d[10] = clearValue;   d[11] = clearValue;
     1591               d[12] = clearValue;   d[13] = clearValue;
     1592               d[14] = clearValue;   d[15] = clearValue;
     1593               d += 16;
     1594               n -= 16;
     1595            }
     1596            while (n > 0) {
     1597               *d++ = clearValue;
     1598               n--;
     1599            }
     1600         }
    8671601      }
    8681602      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;
     1603         /* >16 bit depth buffer */
     1604         GLuint *d = (GLuint *) ctx->DrawBuffer->DepthBuffer;
     1605         const GLuint clearValue = (GLuint) (ctx->Depth.Clear * ctx->Visual->DepthMax);
     1606         GLint n = ctx->DrawBuffer->Width * ctx->DrawBuffer->Height;
     1607         while (n >= 16) {
     1608            d[0] = clearValue;    d[1] = clearValue;
     1609            d[2] = clearValue;    d[3] = clearValue;
     1610            d[4] = clearValue;    d[5] = clearValue;
     1611            d[6] = clearValue;    d[7] = clearValue;
     1612            d[8] = clearValue;    d[9] = clearValue;
     1613            d[10] = clearValue;   d[11] = clearValue;
     1614            d[12] = clearValue;   d[13] = clearValue;
     1615            d[14] = clearValue;   d[15] = clearValue;
    8801616            d += 16;
    8811617            n -= 16;
    8821618         }
    883          while (n>0) {
    884             *d++ = clear_value;
     1619         while (n > 0) {
     1620            *d++ = clearValue;
    8851621            n--;
    8861622         }
     
    8881624   }
    8891625}
    890 
    891 
    892 
Note: See TracChangeset for help on using the changeset viewer.