mesa: Refactor viewport transform computation.
[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,
39 GLfloat x, GLfloat y,
40 GLfloat width, GLfloat height)
41 {
42 double scale[3], translate[3];
43
44 /* clamp width and height to the implementation dependent range */
45 width = MIN2(width, (GLfloat) ctx->Const.MaxViewportWidth);
46 height = MIN2(height, (GLfloat) ctx->Const.MaxViewportHeight);
47
48 /* The GL_ARB_viewport_array spec says:
49 *
50 * "The location of the viewport's bottom-left corner, given by (x,y),
51 * are clamped to be within the implementation-dependent viewport
52 * bounds range. The viewport bounds range [min, max] tuple may be
53 * determined by calling GetFloatv with the symbolic constant
54 * VIEWPORT_BOUNDS_RANGE (see section 6.1)."
55 */
56 if (ctx->Extensions.ARB_viewport_array) {
57 x = CLAMP(x,
58 ctx->Const.ViewportBounds.Min, ctx->Const.ViewportBounds.Max);
59 y = CLAMP(y,
60 ctx->Const.ViewportBounds.Min, ctx->Const.ViewportBounds.Max);
61 }
62
63 if (ctx->ViewportArray[idx].X == x &&
64 ctx->ViewportArray[idx].Width == width &&
65 ctx->ViewportArray[idx].Y == y &&
66 ctx->ViewportArray[idx].Height == height)
67 return;
68
69 ctx->ViewportArray[idx].X = x;
70 ctx->ViewportArray[idx].Width = width;
71 ctx->ViewportArray[idx].Y = y;
72 ctx->ViewportArray[idx].Height = height;
73 ctx->NewState |= _NEW_VIEWPORT;
74
75 #if 1
76 /* XXX remove this someday. Currently the DRI drivers rely on
77 * the WindowMap matrix being up to date in the driver's Viewport
78 * and DepthRange functions.
79 */
80 _mesa_get_viewport_xform(ctx, idx, scale, translate);
81 _math_matrix_viewport(&ctx->ViewportArray[idx]._WindowMap,
82 scale, translate, ctx->DrawBuffer->_DepthMaxF);
83 #endif
84 }
85
86 struct gl_viewport_inputs {
87 GLfloat X, Y; /**< position */
88 GLfloat Width, Height; /**< size */
89 };
90
91 struct gl_depthrange_inputs {
92 GLdouble Near, Far; /**< Depth buffer range */
93 };
94
95 /**
96 * Set the viewport.
97 * \sa Called via glViewport() or display list execution.
98 *
99 * Flushes the vertices and calls _mesa_set_viewport() with the given
100 * parameters.
101 */
102 void GLAPIENTRY
103 _mesa_Viewport(GLint x, GLint y, GLsizei width, GLsizei height)
104 {
105 unsigned i;
106 GET_CURRENT_CONTEXT(ctx);
107 FLUSH_VERTICES(ctx, 0);
108
109 if (MESA_VERBOSE & VERBOSE_API)
110 _mesa_debug(ctx, "glViewport %d %d %d %d\n", x, y, width, height);
111
112 if (width < 0 || height < 0) {
113 _mesa_error(ctx, GL_INVALID_VALUE,
114 "glViewport(%d, %d, %d, %d)", x, y, width, height);
115 return;
116 }
117
118 /* The GL_ARB_viewport_array spec says:
119 *
120 * "Viewport sets the parameters for all viewports to the same values
121 * and is equivalent (assuming no errors are generated) to:
122 *
123 * for (uint i = 0; i < MAX_VIEWPORTS; i++)
124 * ViewportIndexedf(i, 1, (float)x, (float)y, (float)w, (float)h);"
125 *
126 * Set all of the viewports supported by the implementation, but only
127 * signal the driver once at the end.
128 */
129 for (i = 0; i < ctx->Const.MaxViewports; i++)
130 set_viewport_no_notify(ctx, i, x, y, width, height);
131
132 if (ctx->Driver.Viewport) {
133 /* Many drivers will use this call to check for window size changes
134 * and reallocate the z/stencil/accum/etc buffers if needed.
135 */
136 ctx->Driver.Viewport(ctx);
137 }
138 }
139
140
141 /**
142 * Set new viewport parameters and update derived state (the _WindowMap
143 * matrix). Usually called from _mesa_Viewport().
144 *
145 * \param ctx GL context.
146 * \param idx Index of the viewport to be updated.
147 * \param x, y coordinates of the lower left corner of the viewport rectangle.
148 * \param width width of the viewport rectangle.
149 * \param height height of the viewport rectangle.
150 */
151 void
152 _mesa_set_viewport(struct gl_context *ctx, unsigned idx, GLfloat x, GLfloat y,
153 GLfloat width, GLfloat height)
154 {
155 set_viewport_no_notify(ctx, idx, x, y, width, height);
156
157 if (ctx->Driver.Viewport) {
158 /* Many drivers will use this call to check for window size changes
159 * and reallocate the z/stencil/accum/etc buffers if needed.
160 */
161 ctx->Driver.Viewport(ctx);
162 }
163 }
164
165 void GLAPIENTRY
166 _mesa_ViewportArrayv(GLuint first, GLsizei count, const GLfloat *v)
167 {
168 int i;
169 const struct gl_viewport_inputs *const p = (struct gl_viewport_inputs *) v;
170 GET_CURRENT_CONTEXT(ctx);
171
172 if (MESA_VERBOSE & VERBOSE_API)
173 _mesa_debug(ctx, "glViewportArrayv %d %d\n", first, count);
174
175 if ((first + count) > ctx->Const.MaxViewports) {
176 _mesa_error(ctx, GL_INVALID_VALUE,
177 "glViewportArrayv: first (%d) + count (%d) > MaxViewports "
178 "(%d)",
179 first, count, ctx->Const.MaxViewports);
180 return;
181 }
182
183 /* Verify width & height */
184 for (i = 0; i < count; i++) {
185 if (p[i].Width < 0 || p[i].Height < 0) {
186 _mesa_error(ctx, GL_INVALID_VALUE,
187 "glViewportArrayv: index (%d) width or height < 0 "
188 "(%f, %f)",
189 i + first, p[i].Width, p[i].Height);
190 return;
191 }
192 }
193
194 for (i = 0; i < count; i++)
195 set_viewport_no_notify(ctx, i + first,
196 p[i].X, p[i].Y,
197 p[i].Width, p[i].Height);
198
199 if (ctx->Driver.Viewport)
200 ctx->Driver.Viewport(ctx);
201 }
202
203 static void
204 ViewportIndexedf(GLuint index, GLfloat x, GLfloat y,
205 GLfloat w, GLfloat h, const char *function)
206 {
207 GET_CURRENT_CONTEXT(ctx);
208
209 if (MESA_VERBOSE & VERBOSE_API)
210 _mesa_debug(ctx, "%s(%d, %f, %f, %f, %f)\n",
211 function, index, x, y, w, h);
212
213 if (index >= ctx->Const.MaxViewports) {
214 _mesa_error(ctx, GL_INVALID_VALUE,
215 "%s: index (%d) >= MaxViewports (%d)",
216 function, index, ctx->Const.MaxViewports);
217 return;
218 }
219
220 /* Verify width & height */
221 if (w < 0 || h < 0) {
222 _mesa_error(ctx, GL_INVALID_VALUE,
223 "%s: index (%d) width or height < 0 (%f, %f)",
224 function, index, w, h);
225 return;
226 }
227
228 _mesa_set_viewport(ctx, index, x, y, w, h);
229 }
230
231 void GLAPIENTRY
232 _mesa_ViewportIndexedf(GLuint index, GLfloat x, GLfloat y,
233 GLfloat w, GLfloat h)
234 {
235 ViewportIndexedf(index, x, y, w, h, "glViewportIndexedf");
236 }
237
238 void GLAPIENTRY
239 _mesa_ViewportIndexedfv(GLuint index, const GLfloat *v)
240 {
241 ViewportIndexedf(index, v[0], v[1], v[2], v[3], "glViewportIndexedfv");
242 }
243
244 static void
245 set_depth_range_no_notify(struct gl_context *ctx, unsigned idx,
246 GLclampd nearval, GLclampd farval)
247 {
248 double scale[3], translate[3];
249
250 if (ctx->ViewportArray[idx].Near == nearval &&
251 ctx->ViewportArray[idx].Far == farval)
252 return;
253
254 ctx->ViewportArray[idx].Near = CLAMP(nearval, 0.0, 1.0);
255 ctx->ViewportArray[idx].Far = CLAMP(farval, 0.0, 1.0);
256 ctx->NewState |= _NEW_VIEWPORT;
257
258 #if 1
259 /* XXX remove this someday. Currently the DRI drivers rely on
260 * the WindowMap matrix being up to date in the driver's Viewport
261 * and DepthRange functions.
262 */
263 _mesa_get_viewport_xform(ctx, idx, scale, translate);
264 _math_matrix_viewport(&ctx->ViewportArray[idx]._WindowMap,
265 scale, translate, ctx->DrawBuffer->_DepthMaxF);
266 #endif
267 }
268
269 void
270 _mesa_set_depth_range(struct gl_context *ctx, unsigned idx,
271 GLclampd nearval, GLclampd farval)
272 {
273 set_depth_range_no_notify(ctx, idx, nearval, farval);
274
275 if (ctx->Driver.DepthRange)
276 ctx->Driver.DepthRange(ctx);
277 }
278
279 /**
280 * Called by glDepthRange
281 *
282 * \param nearval specifies the Z buffer value which should correspond to
283 * the near clip plane
284 * \param farval specifies the Z buffer value which should correspond to
285 * the far clip plane
286 */
287 void GLAPIENTRY
288 _mesa_DepthRange(GLclampd nearval, GLclampd farval)
289 {
290 unsigned i;
291 GET_CURRENT_CONTEXT(ctx);
292
293 FLUSH_VERTICES(ctx, 0);
294
295 if (MESA_VERBOSE&VERBOSE_API)
296 _mesa_debug(ctx, "glDepthRange %f %f\n", nearval, farval);
297
298 /* The GL_ARB_viewport_array spec says:
299 *
300 * "DepthRange sets the depth range for all viewports to the same
301 * values and is equivalent (assuming no errors are generated) to:
302 *
303 * for (uint i = 0; i < MAX_VIEWPORTS; i++)
304 * DepthRangeIndexed(i, n, f);"
305 *
306 * Set the depth range for all of the viewports supported by the
307 * implementation, but only signal the driver once at the end.
308 */
309 for (i = 0; i < ctx->Const.MaxViewports; i++)
310 set_depth_range_no_notify(ctx, i, nearval, farval);
311
312 if (ctx->Driver.DepthRange) {
313 ctx->Driver.DepthRange(ctx);
314 }
315 }
316
317 void GLAPIENTRY
318 _mesa_DepthRangef(GLclampf nearval, GLclampf farval)
319 {
320 _mesa_DepthRange(nearval, farval);
321 }
322
323 /**
324 * Update a range DepthRange values
325 *
326 * \param first starting array index
327 * \param count count of DepthRange items to update
328 * \param v pointer to memory containing
329 * GLclampd near and far clip-plane values
330 */
331 void GLAPIENTRY
332 _mesa_DepthRangeArrayv(GLuint first, GLsizei count, const GLclampd *v)
333 {
334 int i;
335 const struct gl_depthrange_inputs *const p =
336 (struct gl_depthrange_inputs *) v;
337 GET_CURRENT_CONTEXT(ctx);
338
339 if (MESA_VERBOSE & VERBOSE_API)
340 _mesa_debug(ctx, "glDepthRangeArrayv %d %d\n", first, count);
341
342 if ((first + count) > ctx->Const.MaxViewports) {
343 _mesa_error(ctx, GL_INVALID_VALUE,
344 "glDepthRangev: first (%d) + count (%d) >= MaxViewports (%d)",
345 first, count, ctx->Const.MaxViewports);
346 return;
347 }
348
349 for (i = 0; i < count; i++)
350 set_depth_range_no_notify(ctx, i + first, p[i].Near, p[i].Far);
351
352 if (ctx->Driver.DepthRange)
353 ctx->Driver.DepthRange(ctx);
354 }
355
356 /**
357 * Update a single DepthRange
358 *
359 * \param index array index to update
360 * \param nearval specifies the Z buffer value which should correspond to
361 * the near clip plane
362 * \param farval specifies the Z buffer value which should correspond to
363 * the far clip plane
364 */
365 void GLAPIENTRY
366 _mesa_DepthRangeIndexed(GLuint index, GLclampd nearval, GLclampd farval)
367 {
368 GET_CURRENT_CONTEXT(ctx);
369
370 if (MESA_VERBOSE & VERBOSE_API)
371 _mesa_debug(ctx, "glDepthRangeIndexed(%d, %f, %f)\n",
372 index, nearval, farval);
373
374 if (index >= ctx->Const.MaxViewports) {
375 _mesa_error(ctx, GL_INVALID_VALUE,
376 "glDepthRangeIndexed: index (%d) >= MaxViewports (%d)",
377 index, ctx->Const.MaxViewports);
378 return;
379 }
380
381 _mesa_set_depth_range(ctx, index, nearval, farval);
382 }
383
384 /**
385 * Initialize the context viewport attribute group.
386 * \param ctx the GL context.
387 */
388 void _mesa_init_viewport(struct gl_context *ctx)
389 {
390 GLfloat depthMax = 65535.0F; /* sorf of arbitrary */
391 unsigned i;
392
393 /* Note: ctx->Const.MaxViewports may not have been set by the driver yet,
394 * so just initialize all of them.
395 */
396 for (i = 0; i < MAX_VIEWPORTS; i++) {
397 double scale[3], translate[3];
398
399 /* Viewport group */
400 ctx->ViewportArray[i].X = 0;
401 ctx->ViewportArray[i].Y = 0;
402 ctx->ViewportArray[i].Width = 0;
403 ctx->ViewportArray[i].Height = 0;
404 ctx->ViewportArray[i].Near = 0.0;
405 ctx->ViewportArray[i].Far = 1.0;
406 _math_matrix_ctr(&ctx->ViewportArray[i]._WindowMap);
407
408 _mesa_get_viewport_xform(ctx, i, scale, translate);
409 _math_matrix_viewport(&ctx->ViewportArray[i]._WindowMap,
410 scale, translate, depthMax);
411 }
412 }
413
414
415 /**
416 * Free the context viewport attribute group data.
417 * \param ctx the GL context.
418 */
419 void _mesa_free_viewport_data(struct gl_context *ctx)
420 {
421 unsigned i;
422
423 for (i = 0; i < MAX_VIEWPORTS; i++)
424 _math_matrix_dtr(&ctx->ViewportArray[i]._WindowMap);
425 }
426
427 /**
428 * Computes the scaling and the translation part of the
429 * viewport transform matrix of the \param i-th viewport
430 * and writes that into \param scale and \param translate.
431 */
432 void
433 _mesa_get_viewport_xform(struct gl_context *ctx, unsigned i,
434 double scale[3], double translate[3])
435 {
436 double x = ctx->ViewportArray[i].X;
437 double y = ctx->ViewportArray[i].Y;
438 double half_width = 0.5*ctx->ViewportArray[i].Width;
439 double half_height = 0.5*ctx->ViewportArray[i].Height;
440 double n = ctx->ViewportArray[i].Near;
441 double f = ctx->ViewportArray[i].Far;
442
443 scale[0] = half_width;
444 translate[0] = half_width + x;
445 scale[1] = half_height;
446 translate[1] = half_height + y;
447 scale[2] = 0.5*(f - n);
448 translate[2] = 0.5*(n + f);
449 }