1848b8605Smrg/*
2848b8605Smrg * Mesa 3-D graphics library
3848b8605Smrg *
4848b8605Smrg * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
5848b8605Smrg *
6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg * copy of this software and associated documentation files (the "Software"),
8848b8605Smrg * to deal in the Software without restriction, including without limitation
9848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the
11848b8605Smrg * Software is furnished to do so, subject to the following conditions:
12848b8605Smrg *
13848b8605Smrg * The above copyright notice and this permission notice shall be included
14848b8605Smrg * in all copies or substantial portions of the Software.
15848b8605Smrg *
16848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19848b8605Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20848b8605Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21848b8605Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22848b8605Smrg * OTHER DEALINGS IN THE SOFTWARE.
23848b8605Smrg */
24848b8605Smrg
25848b8605Smrg
26848b8605Smrg#include "glheader.h"
27848b8605Smrg#include "imports.h"
28848b8605Smrg#include "context.h"
29848b8605Smrg#include "depth.h"
30848b8605Smrg#include "enums.h"
31848b8605Smrg#include "macros.h"
32848b8605Smrg#include "mtypes.h"
33848b8605Smrg
34848b8605Smrg
35848b8605Smrg/**********************************************************************/
36848b8605Smrg/*****                          API Functions                     *****/
37848b8605Smrg/**********************************************************************/
38848b8605Smrg
39848b8605Smrg
40848b8605Smrg
41848b8605Smrgvoid GLAPIENTRY
42848b8605Smrg_mesa_ClearDepth( GLclampd depth )
43848b8605Smrg{
44848b8605Smrg   GET_CURRENT_CONTEXT(ctx);
45848b8605Smrg
46848b8605Smrg   if (MESA_VERBOSE & VERBOSE_API)
47848b8605Smrg      _mesa_debug(ctx, "glClearDepth(%f)\n", depth);
48848b8605Smrg
49848b8605Smrg   ctx->Depth.Clear = CLAMP( depth, 0.0, 1.0 );
50848b8605Smrg}
51848b8605Smrg
52848b8605Smrg
53848b8605Smrgvoid GLAPIENTRY
54848b8605Smrg_mesa_ClearDepthf( GLclampf depth )
55848b8605Smrg{
56848b8605Smrg   _mesa_ClearDepth(depth);
57848b8605Smrg}
58848b8605Smrg
59848b8605Smrg
60b8e80941Smrgstatic ALWAYS_INLINE void
61b8e80941Smrgdepth_func(struct gl_context *ctx, GLenum func, bool no_error)
62848b8605Smrg{
63848b8605Smrg   if (ctx->Depth.Func == func)
64848b8605Smrg      return;
65848b8605Smrg
66b8e80941Smrg   if (!no_error) {
67b8e80941Smrg      switch (func) {
68b8e80941Smrg      case GL_LESS:    /* (default) pass if incoming z < stored z */
69b8e80941Smrg      case GL_GEQUAL:
70b8e80941Smrg      case GL_LEQUAL:
71b8e80941Smrg      case GL_GREATER:
72b8e80941Smrg      case GL_NOTEQUAL:
73b8e80941Smrg      case GL_EQUAL:
74b8e80941Smrg      case GL_ALWAYS:
75b8e80941Smrg      case GL_NEVER:
76b8e80941Smrg         break;
77b8e80941Smrg      default:
78b8e80941Smrg         _mesa_error(ctx, GL_INVALID_ENUM, "glDepth.Func");
79b8e80941Smrg         return;
80b8e80941Smrg      }
81b8e80941Smrg   }
82b8e80941Smrg
83b8e80941Smrg   FLUSH_VERTICES(ctx, ctx->DriverFlags.NewDepth ? 0 : _NEW_DEPTH);
84b8e80941Smrg   ctx->NewDriverState |= ctx->DriverFlags.NewDepth;
85848b8605Smrg   ctx->Depth.Func = func;
86848b8605Smrg
87848b8605Smrg   if (ctx->Driver.DepthFunc)
88b8e80941Smrg      ctx->Driver.DepthFunc(ctx, func);
89b8e80941Smrg}
90b8e80941Smrg
91b8e80941Smrg
92b8e80941Smrgvoid GLAPIENTRY
93b8e80941Smrg_mesa_DepthFunc_no_error(GLenum func)
94b8e80941Smrg{
95b8e80941Smrg   GET_CURRENT_CONTEXT(ctx);
96b8e80941Smrg   depth_func(ctx, func, true);
97b8e80941Smrg}
98b8e80941Smrg
99b8e80941Smrg
100b8e80941Smrgvoid GLAPIENTRY
101b8e80941Smrg_mesa_DepthFunc(GLenum func)
102b8e80941Smrg{
103b8e80941Smrg   GET_CURRENT_CONTEXT(ctx);
104b8e80941Smrg
105b8e80941Smrg   if (MESA_VERBOSE & VERBOSE_API)
106b8e80941Smrg      _mesa_debug(ctx, "glDepthFunc %s\n", _mesa_enum_to_string(func));
107b8e80941Smrg
108b8e80941Smrg   depth_func(ctx, func, false);
109848b8605Smrg}
110848b8605Smrg
111848b8605Smrg
112848b8605Smrg
113848b8605Smrgvoid GLAPIENTRY
114848b8605Smrg_mesa_DepthMask( GLboolean flag )
115848b8605Smrg{
116848b8605Smrg   GET_CURRENT_CONTEXT(ctx);
117848b8605Smrg
118848b8605Smrg   if (MESA_VERBOSE & VERBOSE_API)
119848b8605Smrg      _mesa_debug(ctx, "glDepthMask %d\n", flag);
120848b8605Smrg
121848b8605Smrg   /*
122848b8605Smrg    * GL_TRUE indicates depth buffer writing is enabled (default)
123848b8605Smrg    * GL_FALSE indicates depth buffer writing is disabled
124848b8605Smrg    */
125848b8605Smrg   if (ctx->Depth.Mask == flag)
126848b8605Smrg      return;
127848b8605Smrg
128b8e80941Smrg   FLUSH_VERTICES(ctx, ctx->DriverFlags.NewDepth ? 0 : _NEW_DEPTH);
129b8e80941Smrg   ctx->NewDriverState |= ctx->DriverFlags.NewDepth;
130848b8605Smrg   ctx->Depth.Mask = flag;
131848b8605Smrg
132848b8605Smrg   if (ctx->Driver.DepthMask)
133848b8605Smrg      ctx->Driver.DepthMask( ctx, flag );
134848b8605Smrg}
135848b8605Smrg
136848b8605Smrg
137848b8605Smrg
138848b8605Smrg/**
139848b8605Smrg * Specified by the GL_EXT_depth_bounds_test extension.
140848b8605Smrg */
141848b8605Smrgvoid GLAPIENTRY
142848b8605Smrg_mesa_DepthBoundsEXT( GLclampd zmin, GLclampd zmax )
143848b8605Smrg{
144848b8605Smrg   GET_CURRENT_CONTEXT(ctx);
145848b8605Smrg
146848b8605Smrg   if (MESA_VERBOSE & VERBOSE_API)
147848b8605Smrg      _mesa_debug(ctx, "glDepthBounds(%f, %f)\n", zmin, zmax);
148848b8605Smrg
149848b8605Smrg   if (zmin > zmax) {
150848b8605Smrg      _mesa_error(ctx, GL_INVALID_VALUE, "glDepthBoundsEXT(zmin > zmax)");
151848b8605Smrg      return;
152848b8605Smrg   }
153848b8605Smrg
154848b8605Smrg   zmin = CLAMP(zmin, 0.0, 1.0);
155848b8605Smrg   zmax = CLAMP(zmax, 0.0, 1.0);
156848b8605Smrg
157848b8605Smrg   if (ctx->Depth.BoundsMin == zmin && ctx->Depth.BoundsMax == zmax)
158848b8605Smrg      return;
159848b8605Smrg
160b8e80941Smrg   FLUSH_VERTICES(ctx, ctx->DriverFlags.NewDepth ? 0 : _NEW_DEPTH);
161b8e80941Smrg   ctx->NewDriverState |= ctx->DriverFlags.NewDepth;
162848b8605Smrg   ctx->Depth.BoundsMin = (GLfloat) zmin;
163848b8605Smrg   ctx->Depth.BoundsMax = (GLfloat) zmax;
164848b8605Smrg}
165848b8605Smrg
166848b8605Smrg
167848b8605Smrg/**********************************************************************/
168848b8605Smrg/*****                      Initialization                        *****/
169848b8605Smrg/**********************************************************************/
170848b8605Smrg
171848b8605Smrg
172848b8605Smrg/**
173848b8605Smrg * Initialize the depth buffer attribute group in the given context.
174848b8605Smrg */
175848b8605Smrgvoid
176848b8605Smrg_mesa_init_depth(struct gl_context *ctx)
177848b8605Smrg{
178848b8605Smrg   ctx->Depth.Test = GL_FALSE;
179848b8605Smrg   ctx->Depth.Clear = 1.0;
180848b8605Smrg   ctx->Depth.Func = GL_LESS;
181848b8605Smrg   ctx->Depth.Mask = GL_TRUE;
182848b8605Smrg}
183