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.
39 set_viewport_no_notify(struct gl_context
*ctx
, unsigned idx
,
41 GLfloat width
, GLfloat height
)
43 /* clamp width and height to the implementation dependent range */
44 width
= MIN2(width
, (GLfloat
) ctx
->Const
.MaxViewportWidth
);
45 height
= MIN2(height
, (GLfloat
) ctx
->Const
.MaxViewportHeight
);
47 /* The GL_ARB_viewport_array spec says:
49 * "The location of the viewport's bottom-left corner, given by (x,y),
50 * are clamped to be within the implementation-dependent viewport
51 * bounds range. The viewport bounds range [min, max] tuple may be
52 * determined by calling GetFloatv with the symbolic constant
53 * VIEWPORT_BOUNDS_RANGE (see section 6.1)."
55 if (ctx
->Extensions
.ARB_viewport_array
||
56 (ctx
->Extensions
.OES_viewport_array
&&
57 _mesa_is_gles31(ctx
))) {
59 ctx
->Const
.ViewportBounds
.Min
, ctx
->Const
.ViewportBounds
.Max
);
61 ctx
->Const
.ViewportBounds
.Min
, ctx
->Const
.ViewportBounds
.Max
);
64 if (ctx
->ViewportArray
[idx
].X
== x
&&
65 ctx
->ViewportArray
[idx
].Width
== width
&&
66 ctx
->ViewportArray
[idx
].Y
== y
&&
67 ctx
->ViewportArray
[idx
].Height
== height
)
70 FLUSH_VERTICES(ctx
, ctx
->DriverFlags
.NewViewport
? 0 : _NEW_VIEWPORT
);
71 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewViewport
;
73 ctx
->ViewportArray
[idx
].X
= x
;
74 ctx
->ViewportArray
[idx
].Width
= width
;
75 ctx
->ViewportArray
[idx
].Y
= y
;
76 ctx
->ViewportArray
[idx
].Height
= height
;
79 struct gl_viewport_inputs
{
80 GLfloat X
, Y
; /**< position */
81 GLfloat Width
, Height
; /**< size */
84 struct gl_depthrange_inputs
{
85 GLdouble Near
, Far
; /**< Depth buffer range */
89 viewport(struct gl_context
*ctx
, GLint x
, GLint y
, GLsizei width
,
92 /* The GL_ARB_viewport_array spec says:
94 * "Viewport sets the parameters for all viewports to the same values
95 * and is equivalent (assuming no errors are generated) to:
97 * for (uint i = 0; i < MAX_VIEWPORTS; i++)
98 * ViewportIndexedf(i, 1, (float)x, (float)y, (float)w, (float)h);"
100 * Set all of the viewports supported by the implementation, but only
101 * signal the driver once at the end.
103 for (unsigned i
= 0; i
< ctx
->Const
.MaxViewports
; i
++)
104 set_viewport_no_notify(ctx
, i
, x
, y
, width
, height
);
106 if (ctx
->Driver
.Viewport
)
107 ctx
->Driver
.Viewport(ctx
);
112 * \sa Called via glViewport() or display list execution.
114 * Flushes the vertices and calls _mesa_set_viewport() with the given
118 _mesa_Viewport_no_error(GLint x
, GLint y
, GLsizei width
, GLsizei height
)
120 GET_CURRENT_CONTEXT(ctx
);
121 viewport(ctx
, x
, y
, width
, height
);
125 _mesa_Viewport(GLint x
, GLint y
, GLsizei width
, GLsizei height
)
127 GET_CURRENT_CONTEXT(ctx
);
129 if (MESA_VERBOSE
& VERBOSE_API
)
130 _mesa_debug(ctx
, "glViewport %d %d %d %d\n", x
, y
, width
, height
);
132 if (width
< 0 || height
< 0) {
133 _mesa_error(ctx
, GL_INVALID_VALUE
,
134 "glViewport(%d, %d, %d, %d)", x
, y
, width
, height
);
138 viewport(ctx
, x
, y
, width
, height
);
143 * Set new viewport parameters and update derived state.
144 * Usually called from _mesa_Viewport().
146 * \param ctx GL context.
147 * \param idx Index of the viewport to be updated.
148 * \param x, y coordinates of the lower left corner of the viewport rectangle.
149 * \param width width of the viewport rectangle.
150 * \param height height of the viewport rectangle.
153 _mesa_set_viewport(struct gl_context
*ctx
, unsigned idx
, GLfloat x
, GLfloat y
,
154 GLfloat width
, GLfloat height
)
156 set_viewport_no_notify(ctx
, idx
, x
, y
, width
, height
);
158 if (ctx
->Driver
.Viewport
)
159 ctx
->Driver
.Viewport(ctx
);
163 viewport_array(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
164 const struct gl_viewport_inputs
*inputs
)
166 for (GLsizei i
= 0; i
< count
; i
++) {
167 set_viewport_no_notify(ctx
, i
+ first
, inputs
[i
].X
, inputs
[i
].Y
,
168 inputs
[i
].Width
, inputs
[i
].Height
);
171 if (ctx
->Driver
.Viewport
)
172 ctx
->Driver
.Viewport(ctx
);
176 _mesa_ViewportArrayv_no_error(GLuint first
, GLsizei count
, const GLfloat
*v
)
178 GET_CURRENT_CONTEXT(ctx
);
180 const struct gl_viewport_inputs
*const p
= (struct gl_viewport_inputs
*)v
;
181 viewport_array(ctx
, first
, count
, p
);
185 _mesa_ViewportArrayv(GLuint first
, GLsizei count
, const GLfloat
*v
)
188 const struct gl_viewport_inputs
*const p
= (struct gl_viewport_inputs
*) v
;
189 GET_CURRENT_CONTEXT(ctx
);
191 if (MESA_VERBOSE
& VERBOSE_API
)
192 _mesa_debug(ctx
, "glViewportArrayv %d %d\n", first
, count
);
194 if ((first
+ count
) > ctx
->Const
.MaxViewports
) {
195 _mesa_error(ctx
, GL_INVALID_VALUE
,
196 "glViewportArrayv: first (%d) + count (%d) > MaxViewports "
198 first
, count
, ctx
->Const
.MaxViewports
);
202 /* Verify width & height */
203 for (i
= 0; i
< count
; i
++) {
204 if (p
[i
].Width
< 0 || p
[i
].Height
< 0) {
205 _mesa_error(ctx
, GL_INVALID_VALUE
,
206 "glViewportArrayv: index (%d) width or height < 0 "
208 i
+ first
, p
[i
].Width
, p
[i
].Height
);
213 viewport_array(ctx
, first
, count
, p
);
217 viewport_indexed_err(struct gl_context
*ctx
, GLuint index
, GLfloat x
, GLfloat y
,
218 GLfloat w
, GLfloat h
, const char *function
)
220 if (MESA_VERBOSE
& VERBOSE_API
)
221 _mesa_debug(ctx
, "%s(%d, %f, %f, %f, %f)\n",
222 function
, index
, x
, y
, w
, h
);
224 if (index
>= ctx
->Const
.MaxViewports
) {
225 _mesa_error(ctx
, GL_INVALID_VALUE
,
226 "%s: index (%d) >= MaxViewports (%d)",
227 function
, index
, ctx
->Const
.MaxViewports
);
231 /* Verify width & height */
232 if (w
< 0 || h
< 0) {
233 _mesa_error(ctx
, GL_INVALID_VALUE
,
234 "%s: index (%d) width or height < 0 (%f, %f)",
235 function
, index
, w
, h
);
239 _mesa_set_viewport(ctx
, index
, x
, y
, w
, h
);
243 _mesa_ViewportIndexedf_no_error(GLuint index
, GLfloat x
, GLfloat y
,
244 GLfloat w
, GLfloat h
)
246 GET_CURRENT_CONTEXT(ctx
);
247 _mesa_set_viewport(ctx
, index
, x
, y
, w
, h
);
251 _mesa_ViewportIndexedf(GLuint index
, GLfloat x
, GLfloat y
,
252 GLfloat w
, GLfloat h
)
254 GET_CURRENT_CONTEXT(ctx
);
255 viewport_indexed_err(ctx
, index
, x
, y
, w
, h
, "glViewportIndexedf");
259 _mesa_ViewportIndexedfv_no_error(GLuint index
, const GLfloat
*v
)
261 GET_CURRENT_CONTEXT(ctx
);
262 _mesa_set_viewport(ctx
, index
, v
[0], v
[1], v
[2], v
[3]);
266 _mesa_ViewportIndexedfv(GLuint index
, const GLfloat
*v
)
268 GET_CURRENT_CONTEXT(ctx
);
269 viewport_indexed_err(ctx
, index
, v
[0], v
[1], v
[2], v
[3],
270 "glViewportIndexedfv");
274 set_depth_range_no_notify(struct gl_context
*ctx
, unsigned idx
,
275 GLclampd nearval
, GLclampd farval
)
277 if (ctx
->ViewportArray
[idx
].Near
== nearval
&&
278 ctx
->ViewportArray
[idx
].Far
== farval
)
281 /* The depth range is needed by program state constants. */
282 FLUSH_VERTICES(ctx
, _NEW_VIEWPORT
);
283 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewViewport
;
285 ctx
->ViewportArray
[idx
].Near
= CLAMP(nearval
, 0.0, 1.0);
286 ctx
->ViewportArray
[idx
].Far
= CLAMP(farval
, 0.0, 1.0);
290 _mesa_set_depth_range(struct gl_context
*ctx
, unsigned idx
,
291 GLclampd nearval
, GLclampd farval
)
293 set_depth_range_no_notify(ctx
, idx
, nearval
, farval
);
295 if (ctx
->Driver
.DepthRange
)
296 ctx
->Driver
.DepthRange(ctx
);
300 * Called by glDepthRange
302 * \param nearval specifies the Z buffer value which should correspond to
303 * the near clip plane
304 * \param farval specifies the Z buffer value which should correspond to
308 _mesa_DepthRange(GLclampd nearval
, GLclampd farval
)
311 GET_CURRENT_CONTEXT(ctx
);
313 if (MESA_VERBOSE
&VERBOSE_API
)
314 _mesa_debug(ctx
, "glDepthRange %f %f\n", nearval
, farval
);
316 /* The GL_ARB_viewport_array spec says:
318 * "DepthRange sets the depth range for all viewports to the same
319 * values and is equivalent (assuming no errors are generated) to:
321 * for (uint i = 0; i < MAX_VIEWPORTS; i++)
322 * DepthRangeIndexed(i, n, f);"
324 * Set the depth range for all of the viewports supported by the
325 * implementation, but only signal the driver once at the end.
327 for (i
= 0; i
< ctx
->Const
.MaxViewports
; i
++)
328 set_depth_range_no_notify(ctx
, i
, nearval
, farval
);
330 if (ctx
->Driver
.DepthRange
) {
331 ctx
->Driver
.DepthRange(ctx
);
336 _mesa_DepthRangef(GLclampf nearval
, GLclampf farval
)
338 _mesa_DepthRange(nearval
, farval
);
342 * Update a range DepthRange values
344 * \param first starting array index
345 * \param count count of DepthRange items to update
346 * \param v pointer to memory containing
347 * GLclampd near and far clip-plane values
350 _mesa_DepthRangeArrayv(GLuint first
, GLsizei count
, const GLclampd
*v
)
353 const struct gl_depthrange_inputs
*const p
=
354 (struct gl_depthrange_inputs
*) v
;
355 GET_CURRENT_CONTEXT(ctx
);
357 if (MESA_VERBOSE
& VERBOSE_API
)
358 _mesa_debug(ctx
, "glDepthRangeArrayv %d %d\n", first
, count
);
360 if ((first
+ count
) > ctx
->Const
.MaxViewports
) {
361 _mesa_error(ctx
, GL_INVALID_VALUE
,
362 "glDepthRangev: first (%d) + count (%d) >= MaxViewports (%d)",
363 first
, count
, ctx
->Const
.MaxViewports
);
367 for (i
= 0; i
< count
; i
++)
368 set_depth_range_no_notify(ctx
, i
+ first
, p
[i
].Near
, p
[i
].Far
);
370 if (ctx
->Driver
.DepthRange
)
371 ctx
->Driver
.DepthRange(ctx
);
375 _mesa_DepthRangeArrayfvOES(GLuint first
, GLsizei count
, const GLfloat
*v
)
378 GET_CURRENT_CONTEXT(ctx
);
380 if (MESA_VERBOSE
& VERBOSE_API
)
381 _mesa_debug(ctx
, "glDepthRangeArrayfv %d %d\n", first
, count
);
383 if ((first
+ count
) > ctx
->Const
.MaxViewports
) {
384 _mesa_error(ctx
, GL_INVALID_VALUE
,
385 "glDepthRangeArrayfv: first (%d) + count (%d) >= MaxViewports (%d)",
386 first
, count
, ctx
->Const
.MaxViewports
);
390 for (i
= 0; i
< count
; i
++)
391 set_depth_range_no_notify(ctx
, i
+ first
, v
[i
* 2], v
[i
* 2 + 1]);
393 if (ctx
->Driver
.DepthRange
)
394 ctx
->Driver
.DepthRange(ctx
);
398 * Update a single DepthRange
400 * \param index array index to update
401 * \param nearval specifies the Z buffer value which should correspond to
402 * the near clip plane
403 * \param farval specifies the Z buffer value which should correspond to
407 _mesa_DepthRangeIndexed(GLuint index
, GLclampd nearval
, GLclampd farval
)
409 GET_CURRENT_CONTEXT(ctx
);
411 if (MESA_VERBOSE
& VERBOSE_API
)
412 _mesa_debug(ctx
, "glDepthRangeIndexed(%d, %f, %f)\n",
413 index
, nearval
, farval
);
415 if (index
>= ctx
->Const
.MaxViewports
) {
416 _mesa_error(ctx
, GL_INVALID_VALUE
,
417 "glDepthRangeIndexed: index (%d) >= MaxViewports (%d)",
418 index
, ctx
->Const
.MaxViewports
);
422 _mesa_set_depth_range(ctx
, index
, nearval
, farval
);
426 _mesa_DepthRangeIndexedfOES(GLuint index
, GLfloat nearval
, GLfloat farval
)
428 _mesa_DepthRangeIndexed(index
, nearval
, farval
);
432 * Initialize the context viewport attribute group.
433 * \param ctx the GL context.
435 void _mesa_init_viewport(struct gl_context
*ctx
)
439 ctx
->Transform
.ClipOrigin
= GL_LOWER_LEFT
;
440 ctx
->Transform
.ClipDepthMode
= GL_NEGATIVE_ONE_TO_ONE
;
442 /* Note: ctx->Const.MaxViewports may not have been set by the driver yet,
443 * so just initialize all of them.
445 for (i
= 0; i
< MAX_VIEWPORTS
; i
++) {
447 ctx
->ViewportArray
[i
].X
= 0;
448 ctx
->ViewportArray
[i
].Y
= 0;
449 ctx
->ViewportArray
[i
].Width
= 0;
450 ctx
->ViewportArray
[i
].Height
= 0;
451 ctx
->ViewportArray
[i
].Near
= 0.0;
452 ctx
->ViewportArray
[i
].Far
= 1.0;
458 clip_control(struct gl_context
*ctx
, GLenum origin
, GLenum depth
)
460 if (ctx
->Transform
.ClipOrigin
== origin
&&
461 ctx
->Transform
.ClipDepthMode
== depth
)
464 /* Affects transform state and the viewport transform */
465 FLUSH_VERTICES(ctx
, ctx
->DriverFlags
.NewClipControl
? 0 :
466 _NEW_TRANSFORM
| _NEW_VIEWPORT
);
467 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewClipControl
;
469 if (ctx
->Transform
.ClipOrigin
!= origin
) {
470 ctx
->Transform
.ClipOrigin
= origin
;
472 /* Affects the winding order of the front face. */
473 if (ctx
->DriverFlags
.NewPolygonState
)
474 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewPolygonState
;
476 ctx
->NewState
|= _NEW_POLYGON
;
478 if (ctx
->Driver
.FrontFace
)
479 ctx
->Driver
.FrontFace(ctx
, ctx
->Polygon
.FrontFace
);
482 if (ctx
->Transform
.ClipDepthMode
!= depth
) {
483 ctx
->Transform
.ClipDepthMode
= depth
;
485 if (ctx
->Driver
.DepthRange
)
486 ctx
->Driver
.DepthRange(ctx
);
492 _mesa_ClipControl_no_error(GLenum origin
, GLenum depth
)
494 GET_CURRENT_CONTEXT(ctx
);
495 clip_control(ctx
, origin
, depth
);
500 _mesa_ClipControl(GLenum origin
, GLenum depth
)
502 GET_CURRENT_CONTEXT(ctx
);
504 if (MESA_VERBOSE
& VERBOSE_API
)
505 _mesa_debug(ctx
, "glClipControl(%s, %s)\n",
506 _mesa_enum_to_string(origin
),
507 _mesa_enum_to_string(depth
));
509 ASSERT_OUTSIDE_BEGIN_END(ctx
);
511 if (!ctx
->Extensions
.ARB_clip_control
) {
512 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glClipControl");
516 if (origin
!= GL_LOWER_LEFT
&& origin
!= GL_UPPER_LEFT
) {
517 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClipControl");
521 if (depth
!= GL_NEGATIVE_ONE_TO_ONE
&& depth
!= GL_ZERO_TO_ONE
) {
522 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClipControl");
526 clip_control(ctx
, origin
, depth
);
530 * Computes the scaling and the translation part of the
531 * viewport transform matrix of the \param i-th viewport
532 * and writes that into \param scale and \param translate.
535 _mesa_get_viewport_xform(struct gl_context
*ctx
, unsigned i
,
536 float scale
[3], float translate
[3])
538 float x
= ctx
->ViewportArray
[i
].X
;
539 float y
= ctx
->ViewportArray
[i
].Y
;
540 float half_width
= 0.5f
* ctx
->ViewportArray
[i
].Width
;
541 float half_height
= 0.5f
* ctx
->ViewportArray
[i
].Height
;
542 double n
= ctx
->ViewportArray
[i
].Near
;
543 double f
= ctx
->ViewportArray
[i
].Far
;
545 scale
[0] = half_width
;
546 translate
[0] = half_width
+ x
;
547 if (ctx
->Transform
.ClipOrigin
== GL_UPPER_LEFT
) {
548 scale
[1] = -half_height
;
550 scale
[1] = half_height
;
552 translate
[1] = half_height
+ y
;
554 if (ctx
->Transform
.ClipDepthMode
== GL_NEGATIVE_ONE_TO_ONE
) {
555 scale
[2] = 0.5 * (f
- n
);
556 translate
[2] = 0.5 * (n
+ f
);