mesa: Refactor depth range setting even more
[mesa.git] / src / mesa / main / viewport.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2009 VMware, Inc. 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 /**
27 * \file viewport.c
28 * glViewport and glDepthRange functions.
29 */
30
31
32 #include "context.h"
33 #include "macros.h"
34 #include "mtypes.h"
35 #include "viewport.h"
36
37
38 /**
39 * Set the viewport.
40 * \sa Called via glViewport() or display list execution.
41 *
42 * Flushes the vertices and calls _mesa_set_viewport() with the given
43 * parameters.
44 */
45 void GLAPIENTRY
46 _mesa_Viewport(GLint x, GLint y, GLsizei width, GLsizei height)
47 {
48 GET_CURRENT_CONTEXT(ctx);
49 FLUSH_VERTICES(ctx, 0);
50
51 if (MESA_VERBOSE & VERBOSE_API)
52 _mesa_debug(ctx, "glViewport %d %d %d %d\n", x, y, width, height);
53
54 if (width < 0 || height < 0) {
55 _mesa_error(ctx, GL_INVALID_VALUE,
56 "glViewport(%d, %d, %d, %d)", x, y, width, height);
57 return;
58 }
59
60 _mesa_set_viewport(ctx, 0, x, y, width, height);
61 }
62
63
64 /**
65 * Set new viewport parameters and update derived state (the _WindowMap
66 * matrix). Usually called from _mesa_Viewport().
67 *
68 * \param ctx GL context.
69 * \param idx Index of the viewport to be updated.
70 * \param x, y coordinates of the lower left corner of the viewport rectangle.
71 * \param width width of the viewport rectangle.
72 * \param height height of the viewport rectangle.
73 */
74 void
75 _mesa_set_viewport(struct gl_context *ctx, unsigned idx, GLint x, GLint y,
76 GLsizei width, GLsizei height)
77 {
78 /* clamp width and height to the implementation dependent range */
79 width = MIN2(width, (GLsizei) ctx->Const.MaxViewportWidth);
80 height = MIN2(height, (GLsizei) ctx->Const.MaxViewportHeight);
81
82 ctx->ViewportArray[idx].X = x;
83 ctx->ViewportArray[idx].Width = width;
84 ctx->ViewportArray[idx].Y = y;
85 ctx->ViewportArray[idx].Height = height;
86 ctx->NewState |= _NEW_VIEWPORT;
87
88 #if 1
89 /* XXX remove this someday. Currently the DRI drivers rely on
90 * the WindowMap matrix being up to date in the driver's Viewport
91 * and DepthRange functions.
92 */
93 _math_matrix_viewport(&ctx->ViewportArray[idx]._WindowMap,
94 ctx->ViewportArray[idx].X,
95 ctx->ViewportArray[idx].Y,
96 ctx->ViewportArray[idx].Width,
97 ctx->ViewportArray[idx].Height,
98 ctx->ViewportArray[idx].Near,
99 ctx->ViewportArray[idx].Far,
100 ctx->DrawBuffer->_DepthMaxF);
101 #endif
102
103 if (ctx->Driver.Viewport) {
104 /* Many drivers will use this call to check for window size changes
105 * and reallocate the z/stencil/accum/etc buffers if needed.
106 */
107 ctx->Driver.Viewport(ctx);
108 }
109 }
110
111 static void
112 set_depth_range_no_notify(struct gl_context *ctx, unsigned idx,
113 GLclampd nearval, GLclampd farval)
114 {
115 if (ctx->ViewportArray[idx].Near == nearval &&
116 ctx->ViewportArray[idx].Far == farval)
117 return;
118
119 ctx->ViewportArray[idx].Near = CLAMP(nearval, 0.0, 1.0);
120 ctx->ViewportArray[idx].Far = CLAMP(farval, 0.0, 1.0);
121 ctx->NewState |= _NEW_VIEWPORT;
122
123 #if 1
124 /* XXX remove this someday. Currently the DRI drivers rely on
125 * the WindowMap matrix being up to date in the driver's Viewport
126 * and DepthRange functions.
127 */
128 _math_matrix_viewport(&ctx->ViewportArray[idx]._WindowMap,
129 ctx->ViewportArray[idx].X,
130 ctx->ViewportArray[idx].Y,
131 ctx->ViewportArray[idx].Width,
132 ctx->ViewportArray[idx].Height,
133 ctx->ViewportArray[idx].Near,
134 ctx->ViewportArray[idx].Far,
135 ctx->DrawBuffer->_DepthMaxF);
136 #endif
137 }
138
139 void
140 _mesa_set_depth_range(struct gl_context *ctx, unsigned idx,
141 GLclampd nearval, GLclampd farval)
142 {
143 set_depth_range_no_notify(ctx, idx, nearval, farval);
144
145 if (ctx->Driver.DepthRange)
146 ctx->Driver.DepthRange(ctx);
147 }
148
149 /**
150 * Called by glDepthRange
151 *
152 * \param nearval specifies the Z buffer value which should correspond to
153 * the near clip plane
154 * \param farval specifies the Z buffer value which should correspond to
155 * the far clip plane
156 */
157 void GLAPIENTRY
158 _mesa_DepthRange(GLclampd nearval, GLclampd farval)
159 {
160 GET_CURRENT_CONTEXT(ctx);
161
162 FLUSH_VERTICES(ctx, 0);
163
164 if (MESA_VERBOSE&VERBOSE_API)
165 _mesa_debug(ctx, "glDepthRange %f %f\n", nearval, farval);
166
167 set_depth_range_no_notify(ctx, 0, nearval, farval);
168
169 if (ctx->Driver.DepthRange) {
170 ctx->Driver.DepthRange(ctx);
171 }
172 }
173
174 void GLAPIENTRY
175 _mesa_DepthRangef(GLclampf nearval, GLclampf farval)
176 {
177 _mesa_DepthRange(nearval, farval);
178 }
179
180 /**
181 * Initialize the context viewport attribute group.
182 * \param ctx the GL context.
183 */
184 void _mesa_init_viewport(struct gl_context *ctx)
185 {
186 GLfloat depthMax = 65535.0F; /* sorf of arbitrary */
187
188 /* Viewport group */
189 ctx->ViewportArray[0].X = 0;
190 ctx->ViewportArray[0].Y = 0;
191 ctx->ViewportArray[0].Width = 0;
192 ctx->ViewportArray[0].Height = 0;
193 ctx->ViewportArray[0].Near = 0.0;
194 ctx->ViewportArray[0].Far = 1.0;
195 _math_matrix_ctr(&ctx->ViewportArray[0]._WindowMap);
196
197 _math_matrix_viewport(&ctx->ViewportArray[0]._WindowMap, 0, 0, 0, 0,
198 0.0F, 1.0F, depthMax);
199 }
200
201
202 /**
203 * Free the context viewport attribute group data.
204 * \param ctx the GL context.
205 */
206 void _mesa_free_viewport_data(struct gl_context *ctx)
207 {
208 _math_matrix_dtr(&ctx->ViewportArray[0]._WindowMap);
209 }
210