1/*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26#include "glheader.h"
27
28#include "context.h"
29#include "depth.h"
30#include "enums.h"
31#include "macros.h"
32#include "mtypes.h"
33#include "state.h"
34
35
36/**********************************************************************/
37/*****                          API Functions                     *****/
38/**********************************************************************/
39
40
41
42void GLAPIENTRY
43_mesa_ClearDepth( GLclampd depth )
44{
45   GET_CURRENT_CONTEXT(ctx);
46
47   if (MESA_VERBOSE & VERBOSE_API)
48      _mesa_debug(ctx, "glClearDepth(%f)\n", depth);
49
50   ctx->PopAttribState |= GL_DEPTH_BUFFER_BIT;
51   ctx->Depth.Clear = CLAMP( depth, 0.0, 1.0 );
52}
53
54
55void GLAPIENTRY
56_mesa_ClearDepthf( GLclampf depth )
57{
58   _mesa_ClearDepth(depth);
59}
60
61
62static ALWAYS_INLINE void
63depth_func(struct gl_context *ctx, GLenum func, bool no_error)
64{
65   if (ctx->Depth.Func == func)
66      return;
67
68   if (!no_error) {
69      switch (func) {
70      case GL_LESS:    /* (default) pass if incoming z < stored z */
71      case GL_GEQUAL:
72      case GL_LEQUAL:
73      case GL_GREATER:
74      case GL_NOTEQUAL:
75      case GL_EQUAL:
76      case GL_ALWAYS:
77      case GL_NEVER:
78         break;
79      default:
80         _mesa_error(ctx, GL_INVALID_ENUM, "glDepth.Func");
81         return;
82      }
83   }
84
85   FLUSH_VERTICES(ctx, ctx->DriverFlags.NewDepth ? 0 : _NEW_DEPTH,
86                  GL_DEPTH_BUFFER_BIT);
87   ctx->NewDriverState |= ctx->DriverFlags.NewDepth;
88   ctx->Depth.Func = func;
89   _mesa_update_allow_draw_out_of_order(ctx);
90
91   if (ctx->Driver.DepthFunc)
92      ctx->Driver.DepthFunc(ctx, func);
93}
94
95
96void GLAPIENTRY
97_mesa_DepthFunc_no_error(GLenum func)
98{
99   GET_CURRENT_CONTEXT(ctx);
100   depth_func(ctx, func, true);
101}
102
103
104void GLAPIENTRY
105_mesa_DepthFunc(GLenum func)
106{
107   GET_CURRENT_CONTEXT(ctx);
108
109   if (MESA_VERBOSE & VERBOSE_API)
110      _mesa_debug(ctx, "glDepthFunc %s\n", _mesa_enum_to_string(func));
111
112   depth_func(ctx, func, false);
113}
114
115
116
117void GLAPIENTRY
118_mesa_DepthMask( GLboolean flag )
119{
120   GET_CURRENT_CONTEXT(ctx);
121
122   if (MESA_VERBOSE & VERBOSE_API)
123      _mesa_debug(ctx, "glDepthMask %d\n", flag);
124
125   /*
126    * GL_TRUE indicates depth buffer writing is enabled (default)
127    * GL_FALSE indicates depth buffer writing is disabled
128    */
129   if (ctx->Depth.Mask == flag)
130      return;
131
132   FLUSH_VERTICES(ctx, ctx->DriverFlags.NewDepth ? 0 : _NEW_DEPTH,
133                  GL_DEPTH_BUFFER_BIT);
134   ctx->NewDriverState |= ctx->DriverFlags.NewDepth;
135   ctx->Depth.Mask = flag;
136   _mesa_update_allow_draw_out_of_order(ctx);
137
138   if (ctx->Driver.DepthMask)
139      ctx->Driver.DepthMask( ctx, flag );
140}
141
142
143
144/**
145 * Specified by the GL_EXT_depth_bounds_test extension.
146 */
147void GLAPIENTRY
148_mesa_DepthBoundsEXT( GLclampd zmin, GLclampd zmax )
149{
150   GET_CURRENT_CONTEXT(ctx);
151
152   if (MESA_VERBOSE & VERBOSE_API)
153      _mesa_debug(ctx, "glDepthBounds(%f, %f)\n", zmin, zmax);
154
155   if (zmin > zmax) {
156      _mesa_error(ctx, GL_INVALID_VALUE, "glDepthBoundsEXT(zmin > zmax)");
157      return;
158   }
159
160   zmin = SATURATE(zmin);
161   zmax = SATURATE(zmax);
162
163   if (ctx->Depth.BoundsMin == zmin && ctx->Depth.BoundsMax == zmax)
164      return;
165
166   FLUSH_VERTICES(ctx, ctx->DriverFlags.NewDepth ? 0 : _NEW_DEPTH,
167                  GL_DEPTH_BUFFER_BIT);
168   ctx->NewDriverState |= ctx->DriverFlags.NewDepth;
169   ctx->Depth.BoundsMin = zmin;
170   ctx->Depth.BoundsMax = zmax;
171}
172
173
174/**********************************************************************/
175/*****                      Initialization                        *****/
176/**********************************************************************/
177
178
179/**
180 * Initialize the depth buffer attribute group in the given context.
181 */
182void
183_mesa_init_depth(struct gl_context *ctx)
184{
185   ctx->Depth.Test = GL_FALSE;
186   ctx->Depth.Clear = 1.0;
187   ctx->Depth.Func = GL_LESS;
188   ctx->Depth.Mask = GL_TRUE;
189}
190