2 * Mesa 3-D graphics library
4 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
28 * glViewport and glDepthRange functions.
38 set_viewport_no_notify(struct gl_context
*ctx
, unsigned idx
,
40 GLfloat width
, GLfloat height
)
42 double scale
[3], translate
[3];
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
);
48 /* The GL_ARB_viewport_array spec says:
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)."
56 if (ctx
->Extensions
.ARB_viewport_array
) {
58 ctx
->Const
.ViewportBounds
.Min
, ctx
->Const
.ViewportBounds
.Max
);
60 ctx
->Const
.ViewportBounds
.Min
, ctx
->Const
.ViewportBounds
.Max
);
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
)
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
;
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.
80 _mesa_get_viewport_xform(ctx
, idx
, scale
, translate
);
81 _math_matrix_viewport(&ctx
->ViewportArray
[idx
]._WindowMap
,
82 scale
, translate
, ctx
->DrawBuffer
->_DepthMaxF
);
86 struct gl_viewport_inputs
{
87 GLfloat X
, Y
; /**< position */
88 GLfloat Width
, Height
; /**< size */
91 struct gl_depthrange_inputs
{
92 GLdouble Near
, Far
; /**< Depth buffer range */
97 * \sa Called via glViewport() or display list execution.
99 * Flushes the vertices and calls _mesa_set_viewport() with the given
103 _mesa_Viewport(GLint x
, GLint y
, GLsizei width
, GLsizei height
)
106 GET_CURRENT_CONTEXT(ctx
);
107 FLUSH_VERTICES(ctx
, 0);
109 if (MESA_VERBOSE
& VERBOSE_API
)
110 _mesa_debug(ctx
, "glViewport %d %d %d %d\n", x
, y
, width
, height
);
112 if (width
< 0 || height
< 0) {
113 _mesa_error(ctx
, GL_INVALID_VALUE
,
114 "glViewport(%d, %d, %d, %d)", x
, y
, width
, height
);
118 /* The GL_ARB_viewport_array spec says:
120 * "Viewport sets the parameters for all viewports to the same values
121 * and is equivalent (assuming no errors are generated) to:
123 * for (uint i = 0; i < MAX_VIEWPORTS; i++)
124 * ViewportIndexedf(i, 1, (float)x, (float)y, (float)w, (float)h);"
126 * Set all of the viewports supported by the implementation, but only
127 * signal the driver once at the end.
129 for (i
= 0; i
< ctx
->Const
.MaxViewports
; i
++)
130 set_viewport_no_notify(ctx
, i
, x
, y
, width
, height
);
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.
136 ctx
->Driver
.Viewport(ctx
);
142 * Set new viewport parameters and update derived state (the _WindowMap
143 * matrix). Usually called from _mesa_Viewport().
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.
152 _mesa_set_viewport(struct gl_context
*ctx
, unsigned idx
, GLfloat x
, GLfloat y
,
153 GLfloat width
, GLfloat height
)
155 set_viewport_no_notify(ctx
, idx
, x
, y
, width
, height
);
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.
161 ctx
->Driver
.Viewport(ctx
);
166 _mesa_ViewportArrayv(GLuint first
, GLsizei count
, const GLfloat
*v
)
169 const struct gl_viewport_inputs
*const p
= (struct gl_viewport_inputs
*) v
;
170 GET_CURRENT_CONTEXT(ctx
);
172 if (MESA_VERBOSE
& VERBOSE_API
)
173 _mesa_debug(ctx
, "glViewportArrayv %d %d\n", first
, count
);
175 if ((first
+ count
) > ctx
->Const
.MaxViewports
) {
176 _mesa_error(ctx
, GL_INVALID_VALUE
,
177 "glViewportArrayv: first (%d) + count (%d) > MaxViewports "
179 first
, count
, ctx
->Const
.MaxViewports
);
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 "
189 i
+ first
, p
[i
].Width
, p
[i
].Height
);
194 for (i
= 0; i
< count
; i
++)
195 set_viewport_no_notify(ctx
, i
+ first
,
197 p
[i
].Width
, p
[i
].Height
);
199 if (ctx
->Driver
.Viewport
)
200 ctx
->Driver
.Viewport(ctx
);
204 ViewportIndexedf(GLuint index
, GLfloat x
, GLfloat y
,
205 GLfloat w
, GLfloat h
, const char *function
)
207 GET_CURRENT_CONTEXT(ctx
);
209 if (MESA_VERBOSE
& VERBOSE_API
)
210 _mesa_debug(ctx
, "%s(%d, %f, %f, %f, %f)\n",
211 function
, index
, x
, y
, w
, h
);
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
);
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
);
228 _mesa_set_viewport(ctx
, index
, x
, y
, w
, h
);
232 _mesa_ViewportIndexedf(GLuint index
, GLfloat x
, GLfloat y
,
233 GLfloat w
, GLfloat h
)
235 ViewportIndexedf(index
, x
, y
, w
, h
, "glViewportIndexedf");
239 _mesa_ViewportIndexedfv(GLuint index
, const GLfloat
*v
)
241 ViewportIndexedf(index
, v
[0], v
[1], v
[2], v
[3], "glViewportIndexedfv");
245 set_depth_range_no_notify(struct gl_context
*ctx
, unsigned idx
,
246 GLclampd nearval
, GLclampd farval
)
248 double scale
[3], translate
[3];
250 if (ctx
->ViewportArray
[idx
].Near
== nearval
&&
251 ctx
->ViewportArray
[idx
].Far
== farval
)
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
;
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.
263 _mesa_get_viewport_xform(ctx
, idx
, scale
, translate
);
264 _math_matrix_viewport(&ctx
->ViewportArray
[idx
]._WindowMap
,
265 scale
, translate
, ctx
->DrawBuffer
->_DepthMaxF
);
270 _mesa_set_depth_range(struct gl_context
*ctx
, unsigned idx
,
271 GLclampd nearval
, GLclampd farval
)
273 set_depth_range_no_notify(ctx
, idx
, nearval
, farval
);
275 if (ctx
->Driver
.DepthRange
)
276 ctx
->Driver
.DepthRange(ctx
);
280 * Called by glDepthRange
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
288 _mesa_DepthRange(GLclampd nearval
, GLclampd farval
)
291 GET_CURRENT_CONTEXT(ctx
);
293 FLUSH_VERTICES(ctx
, 0);
295 if (MESA_VERBOSE
&VERBOSE_API
)
296 _mesa_debug(ctx
, "glDepthRange %f %f\n", nearval
, farval
);
298 /* The GL_ARB_viewport_array spec says:
300 * "DepthRange sets the depth range for all viewports to the same
301 * values and is equivalent (assuming no errors are generated) to:
303 * for (uint i = 0; i < MAX_VIEWPORTS; i++)
304 * DepthRangeIndexed(i, n, f);"
306 * Set the depth range for all of the viewports supported by the
307 * implementation, but only signal the driver once at the end.
309 for (i
= 0; i
< ctx
->Const
.MaxViewports
; i
++)
310 set_depth_range_no_notify(ctx
, i
, nearval
, farval
);
312 if (ctx
->Driver
.DepthRange
) {
313 ctx
->Driver
.DepthRange(ctx
);
318 _mesa_DepthRangef(GLclampf nearval
, GLclampf farval
)
320 _mesa_DepthRange(nearval
, farval
);
324 * Update a range DepthRange values
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
332 _mesa_DepthRangeArrayv(GLuint first
, GLsizei count
, const GLclampd
*v
)
335 const struct gl_depthrange_inputs
*const p
=
336 (struct gl_depthrange_inputs
*) v
;
337 GET_CURRENT_CONTEXT(ctx
);
339 if (MESA_VERBOSE
& VERBOSE_API
)
340 _mesa_debug(ctx
, "glDepthRangeArrayv %d %d\n", first
, count
);
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
);
349 for (i
= 0; i
< count
; i
++)
350 set_depth_range_no_notify(ctx
, i
+ first
, p
[i
].Near
, p
[i
].Far
);
352 if (ctx
->Driver
.DepthRange
)
353 ctx
->Driver
.DepthRange(ctx
);
357 * Update a single DepthRange
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
366 _mesa_DepthRangeIndexed(GLuint index
, GLclampd nearval
, GLclampd farval
)
368 GET_CURRENT_CONTEXT(ctx
);
370 if (MESA_VERBOSE
& VERBOSE_API
)
371 _mesa_debug(ctx
, "glDepthRangeIndexed(%d, %f, %f)\n",
372 index
, nearval
, farval
);
374 if (index
>= ctx
->Const
.MaxViewports
) {
375 _mesa_error(ctx
, GL_INVALID_VALUE
,
376 "glDepthRangeIndexed: index (%d) >= MaxViewports (%d)",
377 index
, ctx
->Const
.MaxViewports
);
381 _mesa_set_depth_range(ctx
, index
, nearval
, farval
);
385 * Initialize the context viewport attribute group.
386 * \param ctx the GL context.
388 void _mesa_init_viewport(struct gl_context
*ctx
)
390 GLfloat depthMax
= 65535.0F
; /* sorf of arbitrary */
393 /* Note: ctx->Const.MaxViewports may not have been set by the driver yet,
394 * so just initialize all of them.
396 for (i
= 0; i
< MAX_VIEWPORTS
; i
++) {
397 double scale
[3], translate
[3];
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
);
408 _mesa_get_viewport_xform(ctx
, i
, scale
, translate
);
409 _math_matrix_viewport(&ctx
->ViewportArray
[i
]._WindowMap
,
410 scale
, translate
, depthMax
);
416 * Free the context viewport attribute group data.
417 * \param ctx the GL context.
419 void _mesa_free_viewport_data(struct gl_context
*ctx
)
423 for (i
= 0; i
< MAX_VIEWPORTS
; i
++)
424 _math_matrix_dtr(&ctx
->ViewportArray
[i
]._WindowMap
);
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.
433 _mesa_get_viewport_xform(struct gl_context
*ctx
, unsigned i
,
434 double scale
[3], double translate
[3])
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
;
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
);