Silence compiler warnings about implicit casts or conversions by supplying explicit...
[mesa.git] / src / mesa / main / depth.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 5.1
4 *
5 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #include "glheader.h"
27 #include "imports.h"
28 #include "context.h"
29 #include "depth.h"
30 #include "enums.h"
31 #include "hash.h"
32 #include "macros.h"
33 #include "mtypes.h"
34
35
36 /**********************************************************************/
37 /***** API Functions *****/
38 /**********************************************************************/
39
40
41
42 void
43 _mesa_ClearDepth( GLclampd depth )
44 {
45 GET_CURRENT_CONTEXT(ctx);
46 GLfloat tmp = (GLfloat) CLAMP( depth, 0.0, 1.0 );
47 ASSERT_OUTSIDE_BEGIN_END(ctx);
48
49 if (ctx->Depth.Clear == tmp)
50 return;
51
52 FLUSH_VERTICES(ctx, _NEW_DEPTH);
53 ctx->Depth.Clear = tmp;
54 if (ctx->Driver.ClearDepth)
55 (*ctx->Driver.ClearDepth)( ctx, ctx->Depth.Clear );
56 }
57
58
59
60 void
61 _mesa_DepthFunc( GLenum func )
62 {
63 GET_CURRENT_CONTEXT(ctx);
64 ASSERT_OUTSIDE_BEGIN_END(ctx);
65
66 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
67 _mesa_debug(ctx, "glDepthFunc %s\n", _mesa_lookup_enum_by_nr(func));
68
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 if (ctx->Depth.Func == func)
85 return;
86
87 FLUSH_VERTICES(ctx, _NEW_DEPTH);
88 ctx->Depth.Func = func;
89
90 if (ctx->Driver.DepthFunc)
91 ctx->Driver.DepthFunc( ctx, func );
92 }
93
94
95
96 void
97 _mesa_DepthMask( GLboolean flag )
98 {
99 GET_CURRENT_CONTEXT(ctx);
100 ASSERT_OUTSIDE_BEGIN_END(ctx);
101
102 if (MESA_VERBOSE & (VERBOSE_API|VERBOSE_TEXTURE))
103 _mesa_debug(ctx, "glDepthMask %d\n", flag);
104
105 /*
106 * GL_TRUE indicates depth buffer writing is enabled (default)
107 * GL_FALSE indicates depth buffer writing is disabled
108 */
109 if (ctx->Depth.Mask == flag)
110 return;
111
112 FLUSH_VERTICES(ctx, _NEW_DEPTH);
113 ctx->Depth.Mask = flag;
114
115 if (ctx->Driver.DepthMask)
116 ctx->Driver.DepthMask( ctx, flag );
117 }
118
119
120
121 /* GL_EXT_depth_bounds_test */
122 void
123 _mesa_DepthBoundsEXT( GLclampd zmin, GLclampd zmax )
124 {
125 GET_CURRENT_CONTEXT(ctx);
126 ASSERT_OUTSIDE_BEGIN_END(ctx);
127
128 if (zmin > zmax) {
129 _mesa_error(ctx, GL_INVALID_VALUE, "glDepthBoundsEXT(zmin > zmax)");
130 return;
131 }
132
133 zmin = CLAMP(zmin, 0.0, 1.0);
134 zmax = CLAMP(zmax, 0.0, 1.0);
135
136 if (ctx->Depth.BoundsMin == zmin && ctx->Depth.BoundsMax == zmax)
137 return;
138
139 FLUSH_VERTICES(ctx, _NEW_DEPTH);
140 ctx->Depth.BoundsMin = (GLfloat) zmin;
141 ctx->Depth.BoundsMax = (GLfloat) zmax;
142 }
143
144
145 /**********************************************************************/
146 /***** Initialization *****/
147 /**********************************************************************/
148
149 void _mesa_init_depth( GLcontext * ctx )
150 {
151 /* Depth buffer group */
152 ctx->Depth.Test = GL_FALSE;
153 ctx->Depth.Clear = 1.0;
154 ctx->Depth.Func = GL_LESS;
155 ctx->Depth.Mask = GL_TRUE;
156 ctx->Depth.OcclusionTest = GL_FALSE;
157
158 /* Z buffer stuff */
159 if (ctx->Visual.depthBits == 0) {
160 /* Special case. Even if we don't have a depth buffer we need
161 * good values for DepthMax for Z vertex transformation purposes
162 * and for per-fragment fog computation.
163 */
164 ctx->DepthMax = 1 << 16;
165 ctx->DepthMaxF = (GLfloat) ctx->DepthMax;
166 }
167 else if (ctx->Visual.depthBits < 32) {
168 ctx->DepthMax = (1 << ctx->Visual.depthBits) - 1;
169 ctx->DepthMaxF = (GLfloat) ctx->DepthMax;
170 }
171 else {
172 /* Special case since shift values greater than or equal to the
173 * number of bits in the left hand expression's type are undefined.
174 */
175 ctx->DepthMax = 0xffffffff;
176 ctx->DepthMaxF = (GLfloat) ctx->DepthMax;
177 }
178 ctx->MRD = 1.0; /* Minimum resolvable depth value, for polygon offset */
179
180 #if FEATURE_ARB_occlusion_query
181 ctx->Occlusion.QueryObjects = _mesa_NewHashTable();
182 #endif
183 ctx->OcclusionResult = GL_FALSE;
184 ctx->OcclusionResultSaved = GL_FALSE;
185 }