mesa: Set all viewports from _mesa_Viewport and _mesa_DepthRange
[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 static void
38 set_viewport_no_notify(struct gl_context *ctx, unsigned idx, GLint x, GLint y,
39 GLsizei width, GLsizei height)
40 {
41 /* clamp width and height to the implementation dependent range */
42 width = MIN2(width, (GLsizei) ctx->Const.MaxViewportWidth);
43 height = MIN2(height, (GLsizei) ctx->Const.MaxViewportHeight);
44
45 ctx->ViewportArray[idx].X = x;
46 ctx->ViewportArray[idx].Width = width;
47 ctx->ViewportArray[idx].Y = y;
48 ctx->ViewportArray[idx].Height = height;
49 ctx->NewState |= _NEW_VIEWPORT;
50
51 #if 1
52 /* XXX remove this someday. Currently the DRI drivers rely on
53 * the WindowMap matrix being up to date in the driver's Viewport
54 * and DepthRange functions.
55 */
56 _math_matrix_viewport(&ctx->ViewportArray[idx]._WindowMap,
57 ctx->ViewportArray[idx].X,
58 ctx->ViewportArray[idx].Y,
59 ctx->ViewportArray[idx].Width,
60 ctx->ViewportArray[idx].Height,
61 ctx->ViewportArray[idx].Near,
62 ctx->ViewportArray[idx].Far,
63 ctx->DrawBuffer->_DepthMaxF);
64 #endif
65 }
66
67 /**
68 * Set the viewport.
69 * \sa Called via glViewport() or display list execution.
70 *
71 * Flushes the vertices and calls _mesa_set_viewport() with the given
72 * parameters.
73 */
74 void GLAPIENTRY
75 _mesa_Viewport(GLint x, GLint y, GLsizei width, GLsizei height)
76 {
77 unsigned i;
78 GET_CURRENT_CONTEXT(ctx);
79 FLUSH_VERTICES(ctx, 0);
80
81 if (MESA_VERBOSE & VERBOSE_API)
82 _mesa_debug(ctx, "glViewport %d %d %d %d\n", x, y, width, height);
83
84 if (width < 0 || height < 0) {
85 _mesa_error(ctx, GL_INVALID_VALUE,
86 "glViewport(%d, %d, %d, %d)", x, y, width, height);
87 return;
88 }
89
90 /* The GL_ARB_viewport_array spec says:
91 *
92 * "Viewport sets the parameters for all viewports to the same values
93 * and is equivalent (assuming no errors are generated) to:
94 *
95 * for (uint i = 0; i < MAX_VIEWPORTS; i++)
96 * ViewportIndexedf(i, 1, (float)x, (float)y, (float)w, (float)h);"
97 *
98 * Set all of the viewports supported by the implementation, but only
99 * signal the driver once at the end.
100 */
101 for (i = 0; i < ctx->Const.MaxViewports; i++)
102 set_viewport_no_notify(ctx, i, x, y, width, height);
103
104 if (ctx->Driver.Viewport) {
105 /* Many drivers will use this call to check for window size changes
106 * and reallocate the z/stencil/accum/etc buffers if needed.
107 */
108 ctx->Driver.Viewport(ctx);
109 }
110 }
111
112
113 /**
114 * Set new viewport parameters and update derived state (the _WindowMap
115 * matrix). Usually called from _mesa_Viewport().
116 *
117 * \param ctx GL context.
118 * \param idx Index of the viewport to be updated.
119 * \param x, y coordinates of the lower left corner of the viewport rectangle.
120 * \param width width of the viewport rectangle.
121 * \param height height of the viewport rectangle.
122 */
123 void
124 _mesa_set_viewport(struct gl_context *ctx, unsigned idx, GLint x, GLint y,
125 GLsizei width, GLsizei height)
126 {
127 set_viewport_no_notify(ctx, idx, x, y, width, height);
128
129 if (ctx->Driver.Viewport) {
130 /* Many drivers will use this call to check for window size changes
131 * and reallocate the z/stencil/accum/etc buffers if needed.
132 */
133 ctx->Driver.Viewport(ctx);
134 }
135 }
136
137 static void
138 set_depth_range_no_notify(struct gl_context *ctx, unsigned idx,
139 GLclampd nearval, GLclampd farval)
140 {
141 if (ctx->ViewportArray[idx].Near == nearval &&
142 ctx->ViewportArray[idx].Far == farval)
143 return;
144
145 ctx->ViewportArray[idx].Near = CLAMP(nearval, 0.0, 1.0);
146 ctx->ViewportArray[idx].Far = CLAMP(farval, 0.0, 1.0);
147 ctx->NewState |= _NEW_VIEWPORT;
148
149 #if 1
150 /* XXX remove this someday. Currently the DRI drivers rely on
151 * the WindowMap matrix being up to date in the driver's Viewport
152 * and DepthRange functions.
153 */
154 _math_matrix_viewport(&ctx->ViewportArray[idx]._WindowMap,
155 ctx->ViewportArray[idx].X,
156 ctx->ViewportArray[idx].Y,
157 ctx->ViewportArray[idx].Width,
158 ctx->ViewportArray[idx].Height,
159 ctx->ViewportArray[idx].Near,
160 ctx->ViewportArray[idx].Far,
161 ctx->DrawBuffer->_DepthMaxF);
162 #endif
163 }
164
165 void
166 _mesa_set_depth_range(struct gl_context *ctx, unsigned idx,
167 GLclampd nearval, GLclampd farval)
168 {
169 set_depth_range_no_notify(ctx, idx, nearval, farval);
170
171 if (ctx->Driver.DepthRange)
172 ctx->Driver.DepthRange(ctx);
173 }
174
175 /**
176 * Called by glDepthRange
177 *
178 * \param nearval specifies the Z buffer value which should correspond to
179 * the near clip plane
180 * \param farval specifies the Z buffer value which should correspond to
181 * the far clip plane
182 */
183 void GLAPIENTRY
184 _mesa_DepthRange(GLclampd nearval, GLclampd farval)
185 {
186 unsigned i;
187 GET_CURRENT_CONTEXT(ctx);
188
189 FLUSH_VERTICES(ctx, 0);
190
191 if (MESA_VERBOSE&VERBOSE_API)
192 _mesa_debug(ctx, "glDepthRange %f %f\n", nearval, farval);
193
194 /* The GL_ARB_viewport_array spec says:
195 *
196 * "DepthRange sets the depth range for all viewports to the same
197 * values and is equivalent (assuming no errors are generated) to:
198 *
199 * for (uint i = 0; i < MAX_VIEWPORTS; i++)
200 * DepthRangeIndexed(i, n, f);"
201 *
202 * Set the depth range for all of the viewports supported by the
203 * implementation, but only signal the driver once at the end.
204 */
205 for (i = 0; i < ctx->Const.MaxViewports; i++)
206 set_depth_range_no_notify(ctx, i, nearval, farval);
207
208 if (ctx->Driver.DepthRange) {
209 ctx->Driver.DepthRange(ctx);
210 }
211 }
212
213 void GLAPIENTRY
214 _mesa_DepthRangef(GLclampf nearval, GLclampf farval)
215 {
216 _mesa_DepthRange(nearval, farval);
217 }
218
219 /**
220 * Initialize the context viewport attribute group.
221 * \param ctx the GL context.
222 */
223 void _mesa_init_viewport(struct gl_context *ctx)
224 {
225 GLfloat depthMax = 65535.0F; /* sorf of arbitrary */
226 unsigned i;
227
228 /* Note: ctx->Const.MaxViewports may not have been set by the driver yet,
229 * so just initialize all of them.
230 */
231 for (i = 0; i < MAX_VIEWPORTS; i++) {
232 /* Viewport group */
233 ctx->ViewportArray[i].X = 0;
234 ctx->ViewportArray[i].Y = 0;
235 ctx->ViewportArray[i].Width = 0;
236 ctx->ViewportArray[i].Height = 0;
237 ctx->ViewportArray[i].Near = 0.0;
238 ctx->ViewportArray[i].Far = 1.0;
239 _math_matrix_ctr(&ctx->ViewportArray[i]._WindowMap);
240
241 _math_matrix_viewport(&ctx->ViewportArray[i]._WindowMap, 0, 0, 0, 0,
242 0.0F, 1.0F, depthMax);
243 }
244 }
245
246
247 /**
248 * Free the context viewport attribute group data.
249 * \param ctx the GL context.
250 */
251 void _mesa_free_viewport_data(struct gl_context *ctx)
252 {
253 unsigned i;
254
255 for (i = 0; i < MAX_VIEWPORTS; i++)
256 _math_matrix_dtr(&ctx->ViewportArray[i]._WindowMap);
257 }
258