depth.c revision 01e04c3f
17117f1b4Smrg/*
27117f1b4Smrg * Mesa 3-D graphics library
37117f1b4Smrg *
4c1f859d4Smrg * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
57117f1b4Smrg *
67117f1b4Smrg * Permission is hereby granted, free of charge, to any person obtaining a
77117f1b4Smrg * copy of this software and associated documentation files (the "Software"),
87117f1b4Smrg * to deal in the Software without restriction, including without limitation
97117f1b4Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
107117f1b4Smrg * and/or sell copies of the Software, and to permit persons to whom the
117117f1b4Smrg * Software is furnished to do so, subject to the following conditions:
127117f1b4Smrg *
137117f1b4Smrg * The above copyright notice and this permission notice shall be included
147117f1b4Smrg * in all copies or substantial portions of the Software.
157117f1b4Smrg *
167117f1b4Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
177117f1b4Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
187117f1b4Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19af69d88dSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20af69d88dSmrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21af69d88dSmrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22af69d88dSmrg * 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 "macros.h"
327117f1b4Smrg#include "mtypes.h"
337117f1b4Smrg
347117f1b4Smrg
357117f1b4Smrg/**********************************************************************/
367117f1b4Smrg/*****                          API Functions                     *****/
377117f1b4Smrg/**********************************************************************/
387117f1b4Smrg
397117f1b4Smrg
407117f1b4Smrg
417117f1b4Smrgvoid GLAPIENTRY
427117f1b4Smrg_mesa_ClearDepth( GLclampd depth )
437117f1b4Smrg{
447117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
457117f1b4Smrg
463464ebd5Sriastradh   if (MESA_VERBOSE & VERBOSE_API)
473464ebd5Sriastradh      _mesa_debug(ctx, "glClearDepth(%f)\n", depth);
483464ebd5Sriastradh
49af69d88dSmrg   ctx->Depth.Clear = CLAMP( depth, 0.0, 1.0 );
507117f1b4Smrg}
517117f1b4Smrg
527117f1b4Smrg
533464ebd5Sriastradhvoid GLAPIENTRY
543464ebd5Sriastradh_mesa_ClearDepthf( GLclampf depth )
553464ebd5Sriastradh{
563464ebd5Sriastradh   _mesa_ClearDepth(depth);
573464ebd5Sriastradh}
583464ebd5Sriastradh
597117f1b4Smrg
6001e04c3fSmrgstatic ALWAYS_INLINE void
6101e04c3fSmrgdepth_func(struct gl_context *ctx, GLenum func, bool no_error)
627117f1b4Smrg{
637117f1b4Smrg   if (ctx->Depth.Func == func)
647117f1b4Smrg      return;
657117f1b4Smrg
6601e04c3fSmrg   if (!no_error) {
6701e04c3fSmrg      switch (func) {
6801e04c3fSmrg      case GL_LESS:    /* (default) pass if incoming z < stored z */
6901e04c3fSmrg      case GL_GEQUAL:
7001e04c3fSmrg      case GL_LEQUAL:
7101e04c3fSmrg      case GL_GREATER:
7201e04c3fSmrg      case GL_NOTEQUAL:
7301e04c3fSmrg      case GL_EQUAL:
7401e04c3fSmrg      case GL_ALWAYS:
7501e04c3fSmrg      case GL_NEVER:
7601e04c3fSmrg         break;
7701e04c3fSmrg      default:
7801e04c3fSmrg         _mesa_error(ctx, GL_INVALID_ENUM, "glDepth.Func");
7901e04c3fSmrg         return;
8001e04c3fSmrg      }
8101e04c3fSmrg   }
8201e04c3fSmrg
8301e04c3fSmrg   FLUSH_VERTICES(ctx, ctx->DriverFlags.NewDepth ? 0 : _NEW_DEPTH);
8401e04c3fSmrg   ctx->NewDriverState |= ctx->DriverFlags.NewDepth;
857117f1b4Smrg   ctx->Depth.Func = func;
867117f1b4Smrg
877117f1b4Smrg   if (ctx->Driver.DepthFunc)
8801e04c3fSmrg      ctx->Driver.DepthFunc(ctx, func);
8901e04c3fSmrg}
9001e04c3fSmrg
9101e04c3fSmrg
9201e04c3fSmrgvoid GLAPIENTRY
9301e04c3fSmrg_mesa_DepthFunc_no_error(GLenum func)
9401e04c3fSmrg{
9501e04c3fSmrg   GET_CURRENT_CONTEXT(ctx);
9601e04c3fSmrg   depth_func(ctx, func, true);
9701e04c3fSmrg}
9801e04c3fSmrg
9901e04c3fSmrg
10001e04c3fSmrgvoid GLAPIENTRY
10101e04c3fSmrg_mesa_DepthFunc(GLenum func)
10201e04c3fSmrg{
10301e04c3fSmrg   GET_CURRENT_CONTEXT(ctx);
10401e04c3fSmrg
10501e04c3fSmrg   if (MESA_VERBOSE & VERBOSE_API)
10601e04c3fSmrg      _mesa_debug(ctx, "glDepthFunc %s\n", _mesa_enum_to_string(func));
10701e04c3fSmrg
10801e04c3fSmrg   depth_func(ctx, func, false);
1097117f1b4Smrg}
1107117f1b4Smrg
1117117f1b4Smrg
1127117f1b4Smrg
1137117f1b4Smrgvoid GLAPIENTRY
1147117f1b4Smrg_mesa_DepthMask( GLboolean flag )
1157117f1b4Smrg{
1167117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
1177117f1b4Smrg
1184a49301eSmrg   if (MESA_VERBOSE & VERBOSE_API)
1197117f1b4Smrg      _mesa_debug(ctx, "glDepthMask %d\n", flag);
1207117f1b4Smrg
1217117f1b4Smrg   /*
1227117f1b4Smrg    * GL_TRUE indicates depth buffer writing is enabled (default)
1237117f1b4Smrg    * GL_FALSE indicates depth buffer writing is disabled
1247117f1b4Smrg    */
1257117f1b4Smrg   if (ctx->Depth.Mask == flag)
1267117f1b4Smrg      return;
1277117f1b4Smrg
12801e04c3fSmrg   FLUSH_VERTICES(ctx, ctx->DriverFlags.NewDepth ? 0 : _NEW_DEPTH);
12901e04c3fSmrg   ctx->NewDriverState |= ctx->DriverFlags.NewDepth;
1307117f1b4Smrg   ctx->Depth.Mask = flag;
1317117f1b4Smrg
1327117f1b4Smrg   if (ctx->Driver.DepthMask)
1337117f1b4Smrg      ctx->Driver.DepthMask( ctx, flag );
1347117f1b4Smrg}
1357117f1b4Smrg
1367117f1b4Smrg
1377117f1b4Smrg
1387117f1b4Smrg/**
1397117f1b4Smrg * Specified by the GL_EXT_depth_bounds_test extension.
1407117f1b4Smrg */
1417117f1b4Smrgvoid GLAPIENTRY
1427117f1b4Smrg_mesa_DepthBoundsEXT( GLclampd zmin, GLclampd zmax )
1437117f1b4Smrg{
1447117f1b4Smrg   GET_CURRENT_CONTEXT(ctx);
1457117f1b4Smrg
1463464ebd5Sriastradh   if (MESA_VERBOSE & VERBOSE_API)
1473464ebd5Sriastradh      _mesa_debug(ctx, "glDepthBounds(%f, %f)\n", zmin, zmax);
1483464ebd5Sriastradh
1497117f1b4Smrg   if (zmin > zmax) {
1507117f1b4Smrg      _mesa_error(ctx, GL_INVALID_VALUE, "glDepthBoundsEXT(zmin > zmax)");
1517117f1b4Smrg      return;
1527117f1b4Smrg   }
1537117f1b4Smrg
1547117f1b4Smrg   zmin = CLAMP(zmin, 0.0, 1.0);
1557117f1b4Smrg   zmax = CLAMP(zmax, 0.0, 1.0);
1567117f1b4Smrg
1577117f1b4Smrg   if (ctx->Depth.BoundsMin == zmin && ctx->Depth.BoundsMax == zmax)
1587117f1b4Smrg      return;
1597117f1b4Smrg
16001e04c3fSmrg   FLUSH_VERTICES(ctx, ctx->DriverFlags.NewDepth ? 0 : _NEW_DEPTH);
16101e04c3fSmrg   ctx->NewDriverState |= ctx->DriverFlags.NewDepth;
1627117f1b4Smrg   ctx->Depth.BoundsMin = (GLfloat) zmin;
1637117f1b4Smrg   ctx->Depth.BoundsMax = (GLfloat) zmax;
1647117f1b4Smrg}
1657117f1b4Smrg
1667117f1b4Smrg
1677117f1b4Smrg/**********************************************************************/
1687117f1b4Smrg/*****                      Initialization                        *****/
1697117f1b4Smrg/**********************************************************************/
1707117f1b4Smrg
1717117f1b4Smrg
1727117f1b4Smrg/**
1737117f1b4Smrg * Initialize the depth buffer attribute group in the given context.
1747117f1b4Smrg */
175c1f859d4Smrgvoid
1763464ebd5Sriastradh_mesa_init_depth(struct gl_context *ctx)
1777117f1b4Smrg{
1787117f1b4Smrg   ctx->Depth.Test = GL_FALSE;
1797117f1b4Smrg   ctx->Depth.Clear = 1.0;
1807117f1b4Smrg   ctx->Depth.Func = GL_LESS;
1817117f1b4Smrg   ctx->Depth.Mask = GL_TRUE;
1827117f1b4Smrg}
183