xref: /third_party/mesa3d/src/mesa/main/depth.c (revision bf215546)
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#include "api_exec_decl.h"
35
36#include "state_tracker/st_context.h"
37
38/**********************************************************************/
39/*****                          API Functions                     *****/
40/**********************************************************************/
41
42
43
44void GLAPIENTRY
45_mesa_ClearDepth( GLclampd depth )
46{
47   GET_CURRENT_CONTEXT(ctx);
48
49   if (MESA_VERBOSE & VERBOSE_API)
50      _mesa_debug(ctx, "glClearDepth(%f)\n", depth);
51
52   ctx->PopAttribState |= GL_DEPTH_BUFFER_BIT;
53   ctx->Depth.Clear = CLAMP( depth, 0.0, 1.0 );
54}
55
56
57void GLAPIENTRY
58_mesa_ClearDepthf( GLclampf depth )
59{
60   _mesa_ClearDepth(depth);
61}
62
63
64static ALWAYS_INLINE void
65depth_func(struct gl_context *ctx, GLenum func, bool no_error)
66{
67   if (ctx->Depth.Func == func)
68      return;
69
70   if (!no_error) {
71      switch (func) {
72      case GL_LESS:    /* (default) pass if incoming z < stored z */
73      case GL_GEQUAL:
74      case GL_LEQUAL:
75      case GL_GREATER:
76      case GL_NOTEQUAL:
77      case GL_EQUAL:
78      case GL_ALWAYS:
79      case GL_NEVER:
80         break;
81      default:
82         _mesa_error(ctx, GL_INVALID_ENUM, "glDepth.Func");
83         return;
84      }
85   }
86
87   FLUSH_VERTICES(ctx, 0, GL_DEPTH_BUFFER_BIT);
88   ctx->NewDriverState |= ST_NEW_DSA;
89   ctx->Depth.Func = func;
90   _mesa_update_allow_draw_out_of_order(ctx);
91}
92
93
94void GLAPIENTRY
95_mesa_DepthFunc_no_error(GLenum func)
96{
97   GET_CURRENT_CONTEXT(ctx);
98   depth_func(ctx, func, true);
99}
100
101
102void GLAPIENTRY
103_mesa_DepthFunc(GLenum func)
104{
105   GET_CURRENT_CONTEXT(ctx);
106
107   if (MESA_VERBOSE & VERBOSE_API)
108      _mesa_debug(ctx, "glDepthFunc %s\n", _mesa_enum_to_string(func));
109
110   depth_func(ctx, func, false);
111}
112
113
114
115void GLAPIENTRY
116_mesa_DepthMask( GLboolean flag )
117{
118   GET_CURRENT_CONTEXT(ctx);
119
120   if (MESA_VERBOSE & VERBOSE_API)
121      _mesa_debug(ctx, "glDepthMask %d\n", flag);
122
123   /*
124    * GL_TRUE indicates depth buffer writing is enabled (default)
125    * GL_FALSE indicates depth buffer writing is disabled
126    */
127   if (ctx->Depth.Mask == flag)
128      return;
129
130   FLUSH_VERTICES(ctx, 0, GL_DEPTH_BUFFER_BIT);
131   ctx->NewDriverState |= ST_NEW_DSA;
132   ctx->Depth.Mask = flag;
133   _mesa_update_allow_draw_out_of_order(ctx);
134}
135
136
137
138/**
139 * Specified by the GL_EXT_depth_bounds_test extension.
140 */
141void GLAPIENTRY
142_mesa_DepthBoundsEXT( GLclampd zmin, GLclampd zmax )
143{
144   GET_CURRENT_CONTEXT(ctx);
145
146   if (MESA_VERBOSE & VERBOSE_API)
147      _mesa_debug(ctx, "glDepthBounds(%f, %f)\n", zmin, zmax);
148
149   if (zmin > zmax) {
150      _mesa_error(ctx, GL_INVALID_VALUE, "glDepthBoundsEXT(zmin > zmax)");
151      return;
152   }
153
154   zmin = SATURATE(zmin);
155   zmax = SATURATE(zmax);
156
157   if (ctx->Depth.BoundsMin == zmin && ctx->Depth.BoundsMax == zmax)
158      return;
159
160   FLUSH_VERTICES(ctx, 0, GL_DEPTH_BUFFER_BIT);
161   ctx->NewDriverState |= ST_NEW_DSA;
162   ctx->Depth.BoundsMin = zmin;
163   ctx->Depth.BoundsMax = zmax;
164}
165
166
167/**********************************************************************/
168/*****                      Initialization                        *****/
169/**********************************************************************/
170
171
172/**
173 * Initialize the depth buffer attribute group in the given context.
174 */
175void
176_mesa_init_depth(struct gl_context *ctx)
177{
178   ctx->Depth.Test = GL_FALSE;
179   ctx->Depth.Clear = 1.0;
180   ctx->Depth.Func = GL_LESS;
181   ctx->Depth.Mask = GL_TRUE;
182}
183