Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / mesa / main / depth.c
1 /*
2  * Mesa 3-D graphics library
3  * Version:  7.1
4  *
5  * Copyright (C) 1999-2007  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 "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    ASSERT_OUTSIDE_BEGIN_END(ctx);
46
47    if (MESA_VERBOSE & VERBOSE_API)
48       _mesa_debug(ctx, "glClearDepth(%f)\n", depth);
49
50    depth = CLAMP( depth, 0.0, 1.0 );
51
52    if (ctx->Depth.Clear == depth)
53       return;
54
55    FLUSH_VERTICES(ctx, _NEW_DEPTH);
56    ctx->Depth.Clear = depth;
57    if (ctx->Driver.ClearDepth)
58       (*ctx->Driver.ClearDepth)( ctx, ctx->Depth.Clear );
59 }
60
61
62 void GLAPIENTRY
63 _mesa_ClearDepthf( GLclampf depth )
64 {
65    _mesa_ClearDepth(depth);
66 }
67
68
69 void GLAPIENTRY
70 _mesa_DepthFunc( GLenum func )
71 {
72    GET_CURRENT_CONTEXT(ctx);
73    ASSERT_OUTSIDE_BEGIN_END(ctx);
74
75    if (MESA_VERBOSE & VERBOSE_API)
76       _mesa_debug(ctx, "glDepthFunc %s\n", _mesa_lookup_enum_by_nr(func));
77
78    switch (func) {
79    case GL_LESS:    /* (default) pass if incoming z < stored z */
80    case GL_GEQUAL:
81    case GL_LEQUAL:
82    case GL_GREATER:
83    case GL_NOTEQUAL:
84    case GL_EQUAL:
85    case GL_ALWAYS:
86    case GL_NEVER:
87       break;
88    default:
89       _mesa_error( ctx, GL_INVALID_ENUM, "glDepth.Func" );
90       return;
91    }
92
93    if (ctx->Depth.Func == func)
94       return;
95
96    FLUSH_VERTICES(ctx, _NEW_DEPTH);
97    ctx->Depth.Func = func;
98
99    if (ctx->Driver.DepthFunc)
100       ctx->Driver.DepthFunc( ctx, func );
101 }
102
103
104
105 void GLAPIENTRY
106 _mesa_DepthMask( GLboolean flag )
107 {
108    GET_CURRENT_CONTEXT(ctx);
109    ASSERT_OUTSIDE_BEGIN_END(ctx);
110
111    if (MESA_VERBOSE & VERBOSE_API)
112       _mesa_debug(ctx, "glDepthMask %d\n", flag);
113
114    /*
115     * GL_TRUE indicates depth buffer writing is enabled (default)
116     * GL_FALSE indicates depth buffer writing is disabled
117     */
118    if (ctx->Depth.Mask == flag)
119       return;
120
121    FLUSH_VERTICES(ctx, _NEW_DEPTH);
122    ctx->Depth.Mask = flag;
123
124    if (ctx->Driver.DepthMask)
125       ctx->Driver.DepthMask( ctx, flag );
126 }
127
128
129
130 /**
131  * Specified by the GL_EXT_depth_bounds_test extension.
132  */
133 void GLAPIENTRY
134 _mesa_DepthBoundsEXT( GLclampd zmin, GLclampd zmax )
135 {
136    GET_CURRENT_CONTEXT(ctx);
137    ASSERT_OUTSIDE_BEGIN_END(ctx);
138
139    if (MESA_VERBOSE & VERBOSE_API)
140       _mesa_debug(ctx, "glDepthBounds(%f, %f)\n", zmin, zmax);
141
142    if (zmin > zmax) {
143       _mesa_error(ctx, GL_INVALID_VALUE, "glDepthBoundsEXT(zmin > zmax)");
144       return;
145    }
146
147    zmin = CLAMP(zmin, 0.0, 1.0);
148    zmax = CLAMP(zmax, 0.0, 1.0);
149
150    if (ctx->Depth.BoundsMin == zmin && ctx->Depth.BoundsMax == zmax)
151       return;
152
153    FLUSH_VERTICES(ctx, _NEW_DEPTH);
154    ctx->Depth.BoundsMin = (GLfloat) zmin;
155    ctx->Depth.BoundsMax = (GLfloat) zmax;
156 }
157
158
159 /**********************************************************************/
160 /*****                      Initialization                        *****/
161 /**********************************************************************/
162
163
164 /**
165  * Initialize the depth buffer attribute group in the given context.
166  */
167 void
168 _mesa_init_depth(struct gl_context *ctx)
169 {
170    ctx->Depth.Test = GL_FALSE;
171    ctx->Depth.Clear = 1.0;
172    ctx->Depth.Func = GL_LESS;
173    ctx->Depth.Mask = GL_TRUE;
174 }