mesa: add ARB_texture_buffer_range glTextureBufferRangeEXT function
[mesa.git] / src / mesa / main / depth.c
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 #include "imports.h"
28 #include "context.h"
29 #include "depth.h"
30 #include "enums.h"
31 #include "macros.h"
32 #include "mtypes.h"
33
34
35 /**********************************************************************/
36 /***** API Functions *****/
37 /**********************************************************************/
38
39
40
41 void GLAPIENTRY
42 _mesa_ClearDepth( GLclampd depth )
43 {
44 GET_CURRENT_CONTEXT(ctx);
45
46 if (MESA_VERBOSE & VERBOSE_API)
47 _mesa_debug(ctx, "glClearDepth(%f)\n", depth);
48
49 ctx->Depth.Clear = CLAMP( depth, 0.0, 1.0 );
50 }
51
52
53 void GLAPIENTRY
54 _mesa_ClearDepthf( GLclampf depth )
55 {
56 _mesa_ClearDepth(depth);
57 }
58
59
60 static ALWAYS_INLINE void
61 depth_func(struct gl_context *ctx, GLenum func, bool no_error)
62 {
63 if (ctx->Depth.Func == func)
64 return;
65
66 if (!no_error) {
67 switch (func) {
68 case GL_LESS: /* (default) pass if incoming z < stored z */
69 case GL_GEQUAL:
70 case GL_LEQUAL:
71 case GL_GREATER:
72 case GL_NOTEQUAL:
73 case GL_EQUAL:
74 case GL_ALWAYS:
75 case GL_NEVER:
76 break;
77 default:
78 _mesa_error(ctx, GL_INVALID_ENUM, "glDepth.Func");
79 return;
80 }
81 }
82
83 FLUSH_VERTICES(ctx, ctx->DriverFlags.NewDepth ? 0 : _NEW_DEPTH);
84 ctx->NewDriverState |= ctx->DriverFlags.NewDepth;
85 ctx->Depth.Func = func;
86
87 if (ctx->Driver.DepthFunc)
88 ctx->Driver.DepthFunc(ctx, func);
89 }
90
91
92 void GLAPIENTRY
93 _mesa_DepthFunc_no_error(GLenum func)
94 {
95 GET_CURRENT_CONTEXT(ctx);
96 depth_func(ctx, func, true);
97 }
98
99
100 void GLAPIENTRY
101 _mesa_DepthFunc(GLenum func)
102 {
103 GET_CURRENT_CONTEXT(ctx);
104
105 if (MESA_VERBOSE & VERBOSE_API)
106 _mesa_debug(ctx, "glDepthFunc %s\n", _mesa_enum_to_string(func));
107
108 depth_func(ctx, func, false);
109 }
110
111
112
113 void GLAPIENTRY
114 _mesa_DepthMask( GLboolean flag )
115 {
116 GET_CURRENT_CONTEXT(ctx);
117
118 if (MESA_VERBOSE & VERBOSE_API)
119 _mesa_debug(ctx, "glDepthMask %d\n", flag);
120
121 /*
122 * GL_TRUE indicates depth buffer writing is enabled (default)
123 * GL_FALSE indicates depth buffer writing is disabled
124 */
125 if (ctx->Depth.Mask == flag)
126 return;
127
128 FLUSH_VERTICES(ctx, ctx->DriverFlags.NewDepth ? 0 : _NEW_DEPTH);
129 ctx->NewDriverState |= ctx->DriverFlags.NewDepth;
130 ctx->Depth.Mask = flag;
131
132 if (ctx->Driver.DepthMask)
133 ctx->Driver.DepthMask( ctx, flag );
134 }
135
136
137
138 /**
139 * Specified by the GL_EXT_depth_bounds_test extension.
140 */
141 void 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 = CLAMP(zmin, 0.0, 1.0);
155 zmax = CLAMP(zmax, 0.0, 1.0);
156
157 if (ctx->Depth.BoundsMin == zmin && ctx->Depth.BoundsMax == zmax)
158 return;
159
160 FLUSH_VERTICES(ctx, ctx->DriverFlags.NewDepth ? 0 : _NEW_DEPTH);
161 ctx->NewDriverState |= ctx->DriverFlags.NewDepth;
162 ctx->Depth.BoundsMin = (GLfloat) zmin;
163 ctx->Depth.BoundsMax = (GLfloat) zmax;
164 }
165
166
167 /**********************************************************************/
168 /***** Initialization *****/
169 /**********************************************************************/
170
171
172 /**
173 * Initialize the depth buffer attribute group in the given context.
174 */
175 void
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 }