depth.c revision 7117f1b4
17117f1b4Smrg/* 27117f1b4Smrg * Mesa 3-D graphics library 37117f1b4Smrg * Version: 6.5 47117f1b4Smrg * 57117f1b4Smrg * Copyright (C) 1999-2005 Brian Paul All Rights Reserved. 67117f1b4Smrg * 77117f1b4Smrg * Permission is hereby granted, free of charge, to any person obtaining a 87117f1b4Smrg * copy of this software and associated documentation files (the "Software"), 97117f1b4Smrg * to deal in the Software without restriction, including without limitation 107117f1b4Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 117117f1b4Smrg * and/or sell copies of the Software, and to permit persons to whom the 127117f1b4Smrg * Software is furnished to do so, subject to the following conditions: 137117f1b4Smrg * 147117f1b4Smrg * The above copyright notice and this permission notice shall be included 157117f1b4Smrg * in all copies or substantial portions of the Software. 167117f1b4Smrg * 177117f1b4Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 187117f1b4Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 197117f1b4Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 207117f1b4Smrg * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 217117f1b4Smrg * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 227117f1b4Smrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 237117f1b4Smrg */ 247117f1b4Smrg 257117f1b4Smrg 267117f1b4Smrg#include "glheader.h" 277117f1b4Smrg#include "imports.h" 287117f1b4Smrg#include "context.h" 297117f1b4Smrg#include "depth.h" 307117f1b4Smrg#include "enums.h" 317117f1b4Smrg#include "hash.h" 327117f1b4Smrg#include "macros.h" 337117f1b4Smrg#include "mtypes.h" 347117f1b4Smrg 357117f1b4Smrg 367117f1b4Smrg/**********************************************************************/ 377117f1b4Smrg/***** API Functions *****/ 387117f1b4Smrg/**********************************************************************/ 397117f1b4Smrg 407117f1b4Smrg 417117f1b4Smrg 427117f1b4Smrgvoid GLAPIENTRY 437117f1b4Smrg_mesa_ClearDepth( GLclampd depth ) 447117f1b4Smrg{ 457117f1b4Smrg GET_CURRENT_CONTEXT(ctx); 467117f1b4Smrg ASSERT_OUTSIDE_BEGIN_END(ctx); 477117f1b4Smrg 487117f1b4Smrg depth = CLAMP( depth, 0.0, 1.0 ); 497117f1b4Smrg 507117f1b4Smrg if (ctx->Depth.Clear == depth) 517117f1b4Smrg return; 527117f1b4Smrg 537117f1b4Smrg FLUSH_VERTICES(ctx, _NEW_DEPTH); 547117f1b4Smrg ctx->Depth.Clear = depth; 557117f1b4Smrg if (ctx->Driver.ClearDepth) 567117f1b4Smrg (*ctx->Driver.ClearDepth)( ctx, ctx->Depth.Clear ); 577117f1b4Smrg} 587117f1b4Smrg 597117f1b4Smrg 607117f1b4Smrg 617117f1b4Smrgvoid GLAPIENTRY 627117f1b4Smrg_mesa_DepthFunc( GLenum func ) 637117f1b4Smrg{ 647117f1b4Smrg GET_CURRENT_CONTEXT(ctx); 657117f1b4Smrg ASSERT_OUTSIDE_BEGIN_END(ctx); 667117f1b4Smrg 677117f1b4Smrg if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) 687117f1b4Smrg _mesa_debug(ctx, "glDepthFunc %s\n", _mesa_lookup_enum_by_nr(func)); 697117f1b4Smrg 707117f1b4Smrg switch (func) { 717117f1b4Smrg case GL_LESS: /* (default) pass if incoming z < stored z */ 727117f1b4Smrg case GL_GEQUAL: 737117f1b4Smrg case GL_LEQUAL: 747117f1b4Smrg case GL_GREATER: 757117f1b4Smrg case GL_NOTEQUAL: 767117f1b4Smrg case GL_EQUAL: 777117f1b4Smrg case GL_ALWAYS: 787117f1b4Smrg case GL_NEVER: 797117f1b4Smrg break; 807117f1b4Smrg default: 817117f1b4Smrg _mesa_error( ctx, GL_INVALID_ENUM, "glDepth.Func" ); 827117f1b4Smrg return; 837117f1b4Smrg } 847117f1b4Smrg 857117f1b4Smrg if (ctx->Depth.Func == func) 867117f1b4Smrg return; 877117f1b4Smrg 887117f1b4Smrg FLUSH_VERTICES(ctx, _NEW_DEPTH); 897117f1b4Smrg ctx->Depth.Func = func; 907117f1b4Smrg 917117f1b4Smrg if (ctx->Driver.DepthFunc) 927117f1b4Smrg ctx->Driver.DepthFunc( ctx, func ); 937117f1b4Smrg} 947117f1b4Smrg 957117f1b4Smrg 967117f1b4Smrg 977117f1b4Smrgvoid GLAPIENTRY 987117f1b4Smrg_mesa_DepthMask( GLboolean flag ) 997117f1b4Smrg{ 1007117f1b4Smrg GET_CURRENT_CONTEXT(ctx); 1017117f1b4Smrg ASSERT_OUTSIDE_BEGIN_END(ctx); 1027117f1b4Smrg 1037117f1b4Smrg if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE)) 1047117f1b4Smrg _mesa_debug(ctx, "glDepthMask %d\n", flag); 1057117f1b4Smrg 1067117f1b4Smrg /* 1077117f1b4Smrg * GL_TRUE indicates depth buffer writing is enabled (default) 1087117f1b4Smrg * GL_FALSE indicates depth buffer writing is disabled 1097117f1b4Smrg */ 1107117f1b4Smrg if (ctx->Depth.Mask == flag) 1117117f1b4Smrg return; 1127117f1b4Smrg 1137117f1b4Smrg FLUSH_VERTICES(ctx, _NEW_DEPTH); 1147117f1b4Smrg ctx->Depth.Mask = flag; 1157117f1b4Smrg 1167117f1b4Smrg if (ctx->Driver.DepthMask) 1177117f1b4Smrg ctx->Driver.DepthMask( ctx, flag ); 1187117f1b4Smrg} 1197117f1b4Smrg 1207117f1b4Smrg 1217117f1b4Smrg 1227117f1b4Smrg/** 1237117f1b4Smrg * Specified by the GL_EXT_depth_bounds_test extension. 1247117f1b4Smrg */ 1257117f1b4Smrgvoid GLAPIENTRY 1267117f1b4Smrg_mesa_DepthBoundsEXT( GLclampd zmin, GLclampd zmax ) 1277117f1b4Smrg{ 1287117f1b4Smrg GET_CURRENT_CONTEXT(ctx); 1297117f1b4Smrg ASSERT_OUTSIDE_BEGIN_END(ctx); 1307117f1b4Smrg 1317117f1b4Smrg if (zmin > zmax) { 1327117f1b4Smrg _mesa_error(ctx, GL_INVALID_VALUE, "glDepthBoundsEXT(zmin > zmax)"); 1337117f1b4Smrg return; 1347117f1b4Smrg } 1357117f1b4Smrg 1367117f1b4Smrg zmin = CLAMP(zmin, 0.0, 1.0); 1377117f1b4Smrg zmax = CLAMP(zmax, 0.0, 1.0); 1387117f1b4Smrg 1397117f1b4Smrg if (ctx->Depth.BoundsMin == zmin && ctx->Depth.BoundsMax == zmax) 1407117f1b4Smrg return; 1417117f1b4Smrg 1427117f1b4Smrg FLUSH_VERTICES(ctx, _NEW_DEPTH); 1437117f1b4Smrg ctx->Depth.BoundsMin = (GLfloat) zmin; 1447117f1b4Smrg ctx->Depth.BoundsMax = (GLfloat) zmax; 1457117f1b4Smrg} 1467117f1b4Smrg 1477117f1b4Smrg 1487117f1b4Smrg/**********************************************************************/ 1497117f1b4Smrg/***** Initialization *****/ 1507117f1b4Smrg/**********************************************************************/ 1517117f1b4Smrg 1527117f1b4Smrg 1537117f1b4Smrg/** 1547117f1b4Smrg * Initialize the depth buffer attribute group in the given context. 1557117f1b4Smrg */ 1567117f1b4Smrgvoid _mesa_init_depth( GLcontext * ctx ) 1577117f1b4Smrg{ 1587117f1b4Smrg /* Depth buffer group */ 1597117f1b4Smrg ctx->Depth.Test = GL_FALSE; 1607117f1b4Smrg ctx->Depth.Clear = 1.0; 1617117f1b4Smrg ctx->Depth.Func = GL_LESS; 1627117f1b4Smrg ctx->Depth.Mask = GL_TRUE; 1637117f1b4Smrg 1647117f1b4Smrg /* XXX this is now per-framebuffer state */ 1657117f1b4Smrg#if 00 1667117f1b4Smrg /* Z buffer stuff */ 1677117f1b4Smrg if (ctx->Visual.depthBits == 0) { 1687117f1b4Smrg /* Special case. Even if we don't have a depth buffer we need 1697117f1b4Smrg * good values for DepthMax for Z vertex transformation purposes 1707117f1b4Smrg * and for per-fragment fog computation. 1717117f1b4Smrg */ 1727117f1b4Smrg ctx->DepthMax = (1 << 16) - 1; 1737117f1b4Smrg ctx->DepthMaxF = (GLfloat) ctx->DepthMax; 1747117f1b4Smrg } 1757117f1b4Smrg else if (ctx->Visual.depthBits < 32) { 1767117f1b4Smrg ctx->DepthMax = (1 << ctx->Visual.depthBits) - 1; 1777117f1b4Smrg ctx->DepthMaxF = (GLfloat) ctx->DepthMax; 1787117f1b4Smrg } 1797117f1b4Smrg else { 1807117f1b4Smrg /* Special case since shift values greater than or equal to the 1817117f1b4Smrg * number of bits in the left hand expression's type are undefined. 1827117f1b4Smrg */ 1837117f1b4Smrg ctx->DepthMax = 0xffffffff; 1847117f1b4Smrg ctx->DepthMaxF = (GLfloat) ctx->DepthMax; 1857117f1b4Smrg } 1867117f1b4Smrg ctx->MRD = 1.0; /* Minimum resolvable depth value, for polygon offset */ 1877117f1b4Smrg#endif 1887117f1b4Smrg} 189