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 clamp_viewport(struct gl_context
*ctx
, GLfloat
*x
, GLfloat
*y
,
40 GLfloat
*width
, GLfloat
*height
)
42 /* clamp width and height to the implementation dependent range */
43 *width
= MIN2(*width
, (GLfloat
) ctx
->Const
.MaxViewportWidth
);
44 *height
= MIN2(*height
, (GLfloat
) ctx
->Const
.MaxViewportHeight
);
46 /* The GL_ARB_viewport_array spec says:
48 * "The location of the viewport's bottom-left corner, given by (x,y),
49 * are clamped to be within the implementation-dependent viewport
50 * bounds range. The viewport bounds range [min, max] tuple may be
51 * determined by calling GetFloatv with the symbolic constant
52 * VIEWPORT_BOUNDS_RANGE (see section 6.1)."
54 if (ctx
->Extensions
.ARB_viewport_array
||
55 (ctx
->Extensions
.OES_viewport_array
&&
56 _mesa_is_gles31(ctx
))) {
58 ctx
->Const
.ViewportBounds
.Min
, ctx
->Const
.ViewportBounds
.Max
);
60 ctx
->Const
.ViewportBounds
.Min
, ctx
->Const
.ViewportBounds
.Max
);
65 set_viewport_no_notify(struct gl_context
*ctx
, unsigned idx
,
67 GLfloat width
, GLfloat height
)
69 if (ctx
->ViewportArray
[idx
].X
== x
&&
70 ctx
->ViewportArray
[idx
].Width
== width
&&
71 ctx
->ViewportArray
[idx
].Y
== y
&&
72 ctx
->ViewportArray
[idx
].Height
== height
)
75 FLUSH_VERTICES(ctx
, ctx
->DriverFlags
.NewViewport
? 0 : _NEW_VIEWPORT
);
76 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewViewport
;
78 ctx
->ViewportArray
[idx
].X
= x
;
79 ctx
->ViewportArray
[idx
].Width
= width
;
80 ctx
->ViewportArray
[idx
].Y
= y
;
81 ctx
->ViewportArray
[idx
].Height
= height
;
84 struct gl_viewport_inputs
{
85 GLfloat X
, Y
; /**< position */
86 GLfloat Width
, Height
; /**< size */
89 struct gl_depthrange_inputs
{
90 GLdouble Near
, Far
; /**< Depth buffer range */
94 viewport(struct gl_context
*ctx
, GLint x
, GLint y
, GLsizei width
,
97 /* Clamp the viewport to the implementation dependent values. */
98 clamp_viewport(ctx
, (GLfloat
*)&x
, (GLfloat
*)&y
,
99 (GLfloat
*)&width
, (GLfloat
*)&height
);
101 /* The GL_ARB_viewport_array spec says:
103 * "Viewport sets the parameters for all viewports to the same values
104 * and is equivalent (assuming no errors are generated) to:
106 * for (uint i = 0; i < MAX_VIEWPORTS; i++)
107 * ViewportIndexedf(i, 1, (float)x, (float)y, (float)w, (float)h);"
109 * Set all of the viewports supported by the implementation, but only
110 * signal the driver once at the end.
112 for (unsigned i
= 0; i
< ctx
->Const
.MaxViewports
; i
++)
113 set_viewport_no_notify(ctx
, i
, x
, y
, width
, height
);
115 if (ctx
->Driver
.Viewport
)
116 ctx
->Driver
.Viewport(ctx
);
121 * \sa Called via glViewport() or display list execution.
123 * Flushes the vertices and calls _mesa_set_viewport() with the given
127 _mesa_Viewport_no_error(GLint x
, GLint y
, GLsizei width
, GLsizei height
)
129 GET_CURRENT_CONTEXT(ctx
);
130 viewport(ctx
, x
, y
, width
, height
);
134 _mesa_Viewport(GLint x
, GLint y
, GLsizei width
, GLsizei height
)
136 GET_CURRENT_CONTEXT(ctx
);
138 if (MESA_VERBOSE
& VERBOSE_API
)
139 _mesa_debug(ctx
, "glViewport %d %d %d %d\n", x
, y
, width
, height
);
141 if (width
< 0 || height
< 0) {
142 _mesa_error(ctx
, GL_INVALID_VALUE
,
143 "glViewport(%d, %d, %d, %d)", x
, y
, width
, height
);
147 viewport(ctx
, x
, y
, width
, height
);
152 * Set new viewport parameters and update derived state.
153 * Usually called from _mesa_Viewport().
155 * \param ctx GL context.
156 * \param idx Index of the viewport to be updated.
157 * \param x, y coordinates of the lower left corner of the viewport rectangle.
158 * \param width width of the viewport rectangle.
159 * \param height height of the viewport rectangle.
162 _mesa_set_viewport(struct gl_context
*ctx
, unsigned idx
, GLfloat x
, GLfloat y
,
163 GLfloat width
, GLfloat height
)
165 clamp_viewport(ctx
, &x
, &y
, &width
, &height
);
166 set_viewport_no_notify(ctx
, idx
, x
, y
, width
, height
);
168 if (ctx
->Driver
.Viewport
)
169 ctx
->Driver
.Viewport(ctx
);
173 viewport_array(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
174 struct gl_viewport_inputs
*inputs
)
176 for (GLsizei i
= 0; i
< count
; i
++) {
177 clamp_viewport(ctx
, &inputs
[i
].X
, &inputs
[i
].Y
,
178 &inputs
[i
].Width
, &inputs
[i
].Height
);
180 set_viewport_no_notify(ctx
, i
+ first
, inputs
[i
].X
, inputs
[i
].Y
,
181 inputs
[i
].Width
, inputs
[i
].Height
);
184 if (ctx
->Driver
.Viewport
)
185 ctx
->Driver
.Viewport(ctx
);
189 _mesa_ViewportArrayv_no_error(GLuint first
, GLsizei count
, const GLfloat
*v
)
191 GET_CURRENT_CONTEXT(ctx
);
193 struct gl_viewport_inputs
*p
= (struct gl_viewport_inputs
*)v
;
194 viewport_array(ctx
, first
, count
, p
);
198 _mesa_ViewportArrayv(GLuint first
, GLsizei count
, const GLfloat
*v
)
201 struct gl_viewport_inputs
*p
= (struct gl_viewport_inputs
*)v
;
202 GET_CURRENT_CONTEXT(ctx
);
204 if (MESA_VERBOSE
& VERBOSE_API
)
205 _mesa_debug(ctx
, "glViewportArrayv %d %d\n", first
, count
);
207 if ((first
+ count
) > ctx
->Const
.MaxViewports
) {
208 _mesa_error(ctx
, GL_INVALID_VALUE
,
209 "glViewportArrayv: first (%d) + count (%d) > MaxViewports "
211 first
, count
, ctx
->Const
.MaxViewports
);
215 /* Verify width & height */
216 for (i
= 0; i
< count
; i
++) {
217 if (p
[i
].Width
< 0 || p
[i
].Height
< 0) {
218 _mesa_error(ctx
, GL_INVALID_VALUE
,
219 "glViewportArrayv: index (%d) width or height < 0 "
221 i
+ first
, p
[i
].Width
, p
[i
].Height
);
226 viewport_array(ctx
, first
, count
, p
);
230 viewport_indexed_err(struct gl_context
*ctx
, GLuint index
, GLfloat x
, GLfloat y
,
231 GLfloat w
, GLfloat h
, const char *function
)
233 if (MESA_VERBOSE
& VERBOSE_API
)
234 _mesa_debug(ctx
, "%s(%d, %f, %f, %f, %f)\n",
235 function
, index
, x
, y
, w
, h
);
237 if (index
>= ctx
->Const
.MaxViewports
) {
238 _mesa_error(ctx
, GL_INVALID_VALUE
,
239 "%s: index (%d) >= MaxViewports (%d)",
240 function
, index
, ctx
->Const
.MaxViewports
);
244 /* Verify width & height */
245 if (w
< 0 || h
< 0) {
246 _mesa_error(ctx
, GL_INVALID_VALUE
,
247 "%s: index (%d) width or height < 0 (%f, %f)",
248 function
, index
, w
, h
);
252 _mesa_set_viewport(ctx
, index
, x
, y
, w
, h
);
256 _mesa_ViewportIndexedf_no_error(GLuint index
, GLfloat x
, GLfloat y
,
257 GLfloat w
, GLfloat h
)
259 GET_CURRENT_CONTEXT(ctx
);
260 _mesa_set_viewport(ctx
, index
, x
, y
, w
, h
);
264 _mesa_ViewportIndexedf(GLuint index
, GLfloat x
, GLfloat y
,
265 GLfloat w
, GLfloat h
)
267 GET_CURRENT_CONTEXT(ctx
);
268 viewport_indexed_err(ctx
, index
, x
, y
, w
, h
, "glViewportIndexedf");
272 _mesa_ViewportIndexedfv_no_error(GLuint index
, const GLfloat
*v
)
274 GET_CURRENT_CONTEXT(ctx
);
275 _mesa_set_viewport(ctx
, index
, v
[0], v
[1], v
[2], v
[3]);
279 _mesa_ViewportIndexedfv(GLuint index
, const GLfloat
*v
)
281 GET_CURRENT_CONTEXT(ctx
);
282 viewport_indexed_err(ctx
, index
, v
[0], v
[1], v
[2], v
[3],
283 "glViewportIndexedfv");
287 set_depth_range_no_notify(struct gl_context
*ctx
, unsigned idx
,
288 GLclampd nearval
, GLclampd farval
)
290 if (ctx
->ViewportArray
[idx
].Near
== nearval
&&
291 ctx
->ViewportArray
[idx
].Far
== farval
)
294 /* The depth range is needed by program state constants. */
295 FLUSH_VERTICES(ctx
, _NEW_VIEWPORT
);
296 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewViewport
;
298 ctx
->ViewportArray
[idx
].Near
= CLAMP(nearval
, 0.0, 1.0);
299 ctx
->ViewportArray
[idx
].Far
= CLAMP(farval
, 0.0, 1.0);
303 _mesa_set_depth_range(struct gl_context
*ctx
, unsigned idx
,
304 GLclampd nearval
, GLclampd farval
)
306 set_depth_range_no_notify(ctx
, idx
, nearval
, farval
);
308 if (ctx
->Driver
.DepthRange
)
309 ctx
->Driver
.DepthRange(ctx
);
313 * Called by glDepthRange
315 * \param nearval specifies the Z buffer value which should correspond to
316 * the near clip plane
317 * \param farval specifies the Z buffer value which should correspond to
321 _mesa_DepthRange(GLclampd nearval
, GLclampd farval
)
324 GET_CURRENT_CONTEXT(ctx
);
326 if (MESA_VERBOSE
&VERBOSE_API
)
327 _mesa_debug(ctx
, "glDepthRange %f %f\n", nearval
, farval
);
329 /* The GL_ARB_viewport_array spec says:
331 * "DepthRange sets the depth range for all viewports to the same
332 * values and is equivalent (assuming no errors are generated) to:
334 * for (uint i = 0; i < MAX_VIEWPORTS; i++)
335 * DepthRangeIndexed(i, n, f);"
337 * Set the depth range for all of the viewports supported by the
338 * implementation, but only signal the driver once at the end.
340 for (i
= 0; i
< ctx
->Const
.MaxViewports
; i
++)
341 set_depth_range_no_notify(ctx
, i
, nearval
, farval
);
343 if (ctx
->Driver
.DepthRange
) {
344 ctx
->Driver
.DepthRange(ctx
);
349 _mesa_DepthRangef(GLclampf nearval
, GLclampf farval
)
351 _mesa_DepthRange(nearval
, farval
);
355 * Update a range DepthRange values
357 * \param first starting array index
358 * \param count count of DepthRange items to update
359 * \param v pointer to memory containing
360 * GLclampd near and far clip-plane values
362 static ALWAYS_INLINE
void
363 depth_range_arrayv(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
364 const struct gl_depthrange_inputs
*const inputs
)
366 for (GLsizei i
= 0; i
< count
; i
++)
367 set_depth_range_no_notify(ctx
, i
+ first
, inputs
[i
].Near
, inputs
[i
].Far
);
369 if (ctx
->Driver
.DepthRange
)
370 ctx
->Driver
.DepthRange(ctx
);
374 _mesa_DepthRangeArrayv(GLuint first
, GLsizei count
, const GLclampd
*v
)
376 const struct gl_depthrange_inputs
*const p
=
377 (struct gl_depthrange_inputs
*) v
;
378 GET_CURRENT_CONTEXT(ctx
);
380 if (MESA_VERBOSE
& VERBOSE_API
)
381 _mesa_debug(ctx
, "glDepthRangeArrayv %d %d\n", first
, count
);
383 if ((first
+ count
) > ctx
->Const
.MaxViewports
) {
384 _mesa_error(ctx
, GL_INVALID_VALUE
,
385 "glDepthRangev: first (%d) + count (%d) >= MaxViewports (%d)",
386 first
, count
, ctx
->Const
.MaxViewports
);
390 depth_range_arrayv(ctx
, first
, count
, p
);
394 _mesa_DepthRangeArrayfvOES(GLuint first
, GLsizei count
, const GLfloat
*v
)
397 GET_CURRENT_CONTEXT(ctx
);
399 if (MESA_VERBOSE
& VERBOSE_API
)
400 _mesa_debug(ctx
, "glDepthRangeArrayfv %d %d\n", first
, count
);
402 if ((first
+ count
) > ctx
->Const
.MaxViewports
) {
403 _mesa_error(ctx
, GL_INVALID_VALUE
,
404 "glDepthRangeArrayfv: first (%d) + count (%d) >= MaxViewports (%d)",
405 first
, count
, ctx
->Const
.MaxViewports
);
409 for (i
= 0; i
< count
; i
++)
410 set_depth_range_no_notify(ctx
, i
+ first
, v
[i
* 2], v
[i
* 2 + 1]);
412 if (ctx
->Driver
.DepthRange
)
413 ctx
->Driver
.DepthRange(ctx
);
417 * Update a single DepthRange
419 * \param index array index to update
420 * \param nearval specifies the Z buffer value which should correspond to
421 * the near clip plane
422 * \param farval specifies the Z buffer value which should correspond to
426 _mesa_DepthRangeIndexed_no_error(GLuint index
, GLclampd nearval
,
429 GET_CURRENT_CONTEXT(ctx
);
430 _mesa_set_depth_range(ctx
, index
, nearval
, farval
);
435 _mesa_DepthRangeIndexed(GLuint index
, GLclampd nearval
, GLclampd farval
)
437 GET_CURRENT_CONTEXT(ctx
);
439 if (MESA_VERBOSE
& VERBOSE_API
)
440 _mesa_debug(ctx
, "glDepthRangeIndexed(%d, %f, %f)\n",
441 index
, nearval
, farval
);
443 if (index
>= ctx
->Const
.MaxViewports
) {
444 _mesa_error(ctx
, GL_INVALID_VALUE
,
445 "glDepthRangeIndexed: index (%d) >= MaxViewports (%d)",
446 index
, ctx
->Const
.MaxViewports
);
450 _mesa_set_depth_range(ctx
, index
, nearval
, farval
);
454 _mesa_DepthRangeIndexedfOES(GLuint index
, GLfloat nearval
, GLfloat farval
)
456 _mesa_DepthRangeIndexed(index
, nearval
, farval
);
460 * Initialize the context viewport attribute group.
461 * \param ctx the GL context.
463 void _mesa_init_viewport(struct gl_context
*ctx
)
467 ctx
->Transform
.ClipOrigin
= GL_LOWER_LEFT
;
468 ctx
->Transform
.ClipDepthMode
= GL_NEGATIVE_ONE_TO_ONE
;
470 /* Note: ctx->Const.MaxViewports may not have been set by the driver yet,
471 * so just initialize all of them.
473 for (i
= 0; i
< MAX_VIEWPORTS
; i
++) {
475 ctx
->ViewportArray
[i
].X
= 0;
476 ctx
->ViewportArray
[i
].Y
= 0;
477 ctx
->ViewportArray
[i
].Width
= 0;
478 ctx
->ViewportArray
[i
].Height
= 0;
479 ctx
->ViewportArray
[i
].Near
= 0.0;
480 ctx
->ViewportArray
[i
].Far
= 1.0;
486 clip_control(struct gl_context
*ctx
, GLenum origin
, GLenum depth
)
488 if (ctx
->Transform
.ClipOrigin
== origin
&&
489 ctx
->Transform
.ClipDepthMode
== depth
)
492 /* Affects transform state and the viewport transform */
493 FLUSH_VERTICES(ctx
, ctx
->DriverFlags
.NewClipControl
? 0 :
494 _NEW_TRANSFORM
| _NEW_VIEWPORT
);
495 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewClipControl
;
497 if (ctx
->Transform
.ClipOrigin
!= origin
) {
498 ctx
->Transform
.ClipOrigin
= origin
;
500 /* Affects the winding order of the front face. */
501 if (ctx
->DriverFlags
.NewPolygonState
)
502 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewPolygonState
;
504 ctx
->NewState
|= _NEW_POLYGON
;
506 if (ctx
->Driver
.FrontFace
)
507 ctx
->Driver
.FrontFace(ctx
, ctx
->Polygon
.FrontFace
);
510 if (ctx
->Transform
.ClipDepthMode
!= depth
) {
511 ctx
->Transform
.ClipDepthMode
= depth
;
513 if (ctx
->Driver
.DepthRange
)
514 ctx
->Driver
.DepthRange(ctx
);
520 _mesa_ClipControl_no_error(GLenum origin
, GLenum depth
)
522 GET_CURRENT_CONTEXT(ctx
);
523 clip_control(ctx
, origin
, depth
);
528 _mesa_ClipControl(GLenum origin
, GLenum depth
)
530 GET_CURRENT_CONTEXT(ctx
);
532 if (MESA_VERBOSE
& VERBOSE_API
)
533 _mesa_debug(ctx
, "glClipControl(%s, %s)\n",
534 _mesa_enum_to_string(origin
),
535 _mesa_enum_to_string(depth
));
537 ASSERT_OUTSIDE_BEGIN_END(ctx
);
539 if (!ctx
->Extensions
.ARB_clip_control
) {
540 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glClipControl");
544 if (origin
!= GL_LOWER_LEFT
&& origin
!= GL_UPPER_LEFT
) {
545 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClipControl");
549 if (depth
!= GL_NEGATIVE_ONE_TO_ONE
&& depth
!= GL_ZERO_TO_ONE
) {
550 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClipControl");
554 clip_control(ctx
, origin
, depth
);
558 * Computes the scaling and the translation part of the
559 * viewport transform matrix of the \param i-th viewport
560 * and writes that into \param scale and \param translate.
563 _mesa_get_viewport_xform(struct gl_context
*ctx
, unsigned i
,
564 float scale
[3], float translate
[3])
566 float x
= ctx
->ViewportArray
[i
].X
;
567 float y
= ctx
->ViewportArray
[i
].Y
;
568 float half_width
= 0.5f
* ctx
->ViewportArray
[i
].Width
;
569 float half_height
= 0.5f
* ctx
->ViewportArray
[i
].Height
;
570 double n
= ctx
->ViewportArray
[i
].Near
;
571 double f
= ctx
->ViewportArray
[i
].Far
;
573 scale
[0] = half_width
;
574 translate
[0] = half_width
+ x
;
575 if (ctx
->Transform
.ClipOrigin
== GL_UPPER_LEFT
) {
576 scale
[1] = -half_height
;
578 scale
[1] = half_height
;
580 translate
[1] = half_height
+ y
;
582 if (ctx
->Transform
.ClipDepthMode
== GL_NEGATIVE_ONE_TO_ONE
) {
583 scale
[2] = 0.5 * (f
- n
);
584 translate
[2] = 0.5 * (n
+ f
);