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 struct gl_viewport_inputs input
= { x
, y
, width
, height
};
99 /* Clamp the viewport to the implementation dependent values. */
100 clamp_viewport(ctx
, &input
.X
, &input
.Y
, &input
.Width
, &input
.Height
);
102 /* The GL_ARB_viewport_array spec says:
104 * "Viewport sets the parameters for all viewports to the same values
105 * and is equivalent (assuming no errors are generated) to:
107 * for (uint i = 0; i < MAX_VIEWPORTS; i++)
108 * ViewportIndexedf(i, 1, (float)x, (float)y, (float)w, (float)h);"
110 * Set all of the viewports supported by the implementation, but only
111 * signal the driver once at the end.
113 for (unsigned i
= 0; i
< ctx
->Const
.MaxViewports
; i
++)
114 set_viewport_no_notify(ctx
, i
, input
.X
, input
.Y
, input
.Width
, input
.Height
);
116 if (ctx
->Driver
.Viewport
)
117 ctx
->Driver
.Viewport(ctx
);
122 * \sa Called via glViewport() or display list execution.
124 * Flushes the vertices and calls _mesa_set_viewport() with the given
128 _mesa_Viewport_no_error(GLint x
, GLint y
, GLsizei width
, GLsizei height
)
130 GET_CURRENT_CONTEXT(ctx
);
131 viewport(ctx
, x
, y
, width
, height
);
135 _mesa_Viewport(GLint x
, GLint y
, GLsizei width
, GLsizei height
)
137 GET_CURRENT_CONTEXT(ctx
);
139 if (MESA_VERBOSE
& VERBOSE_API
)
140 _mesa_debug(ctx
, "glViewport %d %d %d %d\n", x
, y
, width
, height
);
142 if (width
< 0 || height
< 0) {
143 _mesa_error(ctx
, GL_INVALID_VALUE
,
144 "glViewport(%d, %d, %d, %d)", x
, y
, width
, height
);
148 viewport(ctx
, x
, y
, width
, height
);
153 * Set new viewport parameters and update derived state.
154 * Usually called from _mesa_Viewport().
156 * \param ctx GL context.
157 * \param idx Index of the viewport to be updated.
158 * \param x, y coordinates of the lower left corner of the viewport rectangle.
159 * \param width width of the viewport rectangle.
160 * \param height height of the viewport rectangle.
163 _mesa_set_viewport(struct gl_context
*ctx
, unsigned idx
, GLfloat x
, GLfloat y
,
164 GLfloat width
, GLfloat height
)
166 clamp_viewport(ctx
, &x
, &y
, &width
, &height
);
167 set_viewport_no_notify(ctx
, idx
, x
, y
, width
, height
);
169 if (ctx
->Driver
.Viewport
)
170 ctx
->Driver
.Viewport(ctx
);
174 viewport_array(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
175 struct gl_viewport_inputs
*inputs
)
177 for (GLsizei i
= 0; i
< count
; i
++) {
178 clamp_viewport(ctx
, &inputs
[i
].X
, &inputs
[i
].Y
,
179 &inputs
[i
].Width
, &inputs
[i
].Height
);
181 set_viewport_no_notify(ctx
, i
+ first
, inputs
[i
].X
, inputs
[i
].Y
,
182 inputs
[i
].Width
, inputs
[i
].Height
);
185 if (ctx
->Driver
.Viewport
)
186 ctx
->Driver
.Viewport(ctx
);
190 _mesa_ViewportArrayv_no_error(GLuint first
, GLsizei count
, const GLfloat
*v
)
192 GET_CURRENT_CONTEXT(ctx
);
194 struct gl_viewport_inputs
*p
= (struct gl_viewport_inputs
*)v
;
195 viewport_array(ctx
, first
, count
, p
);
199 _mesa_ViewportArrayv(GLuint first
, GLsizei count
, const GLfloat
*v
)
202 struct gl_viewport_inputs
*p
= (struct gl_viewport_inputs
*)v
;
203 GET_CURRENT_CONTEXT(ctx
);
205 if (MESA_VERBOSE
& VERBOSE_API
)
206 _mesa_debug(ctx
, "glViewportArrayv %d %d\n", first
, count
);
208 if ((first
+ count
) > ctx
->Const
.MaxViewports
) {
209 _mesa_error(ctx
, GL_INVALID_VALUE
,
210 "glViewportArrayv: first (%d) + count (%d) > MaxViewports "
212 first
, count
, ctx
->Const
.MaxViewports
);
216 /* Verify width & height */
217 for (i
= 0; i
< count
; i
++) {
218 if (p
[i
].Width
< 0 || p
[i
].Height
< 0) {
219 _mesa_error(ctx
, GL_INVALID_VALUE
,
220 "glViewportArrayv: index (%d) width or height < 0 "
222 i
+ first
, p
[i
].Width
, p
[i
].Height
);
227 viewport_array(ctx
, first
, count
, p
);
231 viewport_indexed_err(struct gl_context
*ctx
, GLuint index
, GLfloat x
, GLfloat y
,
232 GLfloat w
, GLfloat h
, const char *function
)
234 if (MESA_VERBOSE
& VERBOSE_API
)
235 _mesa_debug(ctx
, "%s(%d, %f, %f, %f, %f)\n",
236 function
, index
, x
, y
, w
, h
);
238 if (index
>= ctx
->Const
.MaxViewports
) {
239 _mesa_error(ctx
, GL_INVALID_VALUE
,
240 "%s: index (%d) >= MaxViewports (%d)",
241 function
, index
, ctx
->Const
.MaxViewports
);
245 /* Verify width & height */
246 if (w
< 0 || h
< 0) {
247 _mesa_error(ctx
, GL_INVALID_VALUE
,
248 "%s: index (%d) width or height < 0 (%f, %f)",
249 function
, index
, w
, h
);
253 _mesa_set_viewport(ctx
, index
, x
, y
, w
, h
);
257 _mesa_ViewportIndexedf_no_error(GLuint index
, GLfloat x
, GLfloat y
,
258 GLfloat w
, GLfloat h
)
260 GET_CURRENT_CONTEXT(ctx
);
261 _mesa_set_viewport(ctx
, index
, x
, y
, w
, h
);
265 _mesa_ViewportIndexedf(GLuint index
, GLfloat x
, GLfloat y
,
266 GLfloat w
, GLfloat h
)
268 GET_CURRENT_CONTEXT(ctx
);
269 viewport_indexed_err(ctx
, index
, x
, y
, w
, h
, "glViewportIndexedf");
273 _mesa_ViewportIndexedfv_no_error(GLuint index
, const GLfloat
*v
)
275 GET_CURRENT_CONTEXT(ctx
);
276 _mesa_set_viewport(ctx
, index
, v
[0], v
[1], v
[2], v
[3]);
280 _mesa_ViewportIndexedfv(GLuint index
, const GLfloat
*v
)
282 GET_CURRENT_CONTEXT(ctx
);
283 viewport_indexed_err(ctx
, index
, v
[0], v
[1], v
[2], v
[3],
284 "glViewportIndexedfv");
288 set_depth_range_no_notify(struct gl_context
*ctx
, unsigned idx
,
289 GLclampd nearval
, GLclampd farval
)
291 if (ctx
->ViewportArray
[idx
].Near
== nearval
&&
292 ctx
->ViewportArray
[idx
].Far
== farval
)
295 /* The depth range is needed by program state constants. */
296 FLUSH_VERTICES(ctx
, _NEW_VIEWPORT
);
297 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewViewport
;
299 ctx
->ViewportArray
[idx
].Near
= CLAMP(nearval
, 0.0, 1.0);
300 ctx
->ViewportArray
[idx
].Far
= CLAMP(farval
, 0.0, 1.0);
304 _mesa_set_depth_range(struct gl_context
*ctx
, unsigned idx
,
305 GLclampd nearval
, GLclampd farval
)
307 set_depth_range_no_notify(ctx
, idx
, nearval
, farval
);
309 if (ctx
->Driver
.DepthRange
)
310 ctx
->Driver
.DepthRange(ctx
);
314 * Called by glDepthRange
316 * \param nearval specifies the Z buffer value which should correspond to
317 * the near clip plane
318 * \param farval specifies the Z buffer value which should correspond to
322 _mesa_DepthRange(GLclampd nearval
, GLclampd farval
)
325 GET_CURRENT_CONTEXT(ctx
);
327 if (MESA_VERBOSE
&VERBOSE_API
)
328 _mesa_debug(ctx
, "glDepthRange %f %f\n", nearval
, farval
);
330 /* The GL_ARB_viewport_array spec says:
332 * "DepthRange sets the depth range for all viewports to the same
333 * values and is equivalent (assuming no errors are generated) to:
335 * for (uint i = 0; i < MAX_VIEWPORTS; i++)
336 * DepthRangeIndexed(i, n, f);"
338 * Set the depth range for all of the viewports supported by the
339 * implementation, but only signal the driver once at the end.
341 for (i
= 0; i
< ctx
->Const
.MaxViewports
; i
++)
342 set_depth_range_no_notify(ctx
, i
, nearval
, farval
);
344 if (ctx
->Driver
.DepthRange
) {
345 ctx
->Driver
.DepthRange(ctx
);
350 _mesa_DepthRangef(GLclampf nearval
, GLclampf farval
)
352 _mesa_DepthRange(nearval
, farval
);
356 * Update a range DepthRange values
358 * \param first starting array index
359 * \param count count of DepthRange items to update
360 * \param v pointer to memory containing
361 * GLclampd near and far clip-plane values
363 static ALWAYS_INLINE
void
364 depth_range_arrayv(struct gl_context
*ctx
, GLuint first
, GLsizei count
,
365 const struct gl_depthrange_inputs
*const inputs
)
367 for (GLsizei i
= 0; i
< count
; i
++)
368 set_depth_range_no_notify(ctx
, i
+ first
, inputs
[i
].Near
, inputs
[i
].Far
);
370 if (ctx
->Driver
.DepthRange
)
371 ctx
->Driver
.DepthRange(ctx
);
375 _mesa_DepthRangeArrayv_no_error(GLuint first
, GLsizei count
, const GLclampd
*v
)
377 GET_CURRENT_CONTEXT(ctx
);
379 const struct gl_depthrange_inputs
*const p
=
380 (struct gl_depthrange_inputs
*)v
;
381 depth_range_arrayv(ctx
, first
, count
, p
);
385 _mesa_DepthRangeArrayv(GLuint first
, GLsizei count
, const GLclampd
*v
)
387 const struct gl_depthrange_inputs
*const p
=
388 (struct gl_depthrange_inputs
*) v
;
389 GET_CURRENT_CONTEXT(ctx
);
391 if (MESA_VERBOSE
& VERBOSE_API
)
392 _mesa_debug(ctx
, "glDepthRangeArrayv %d %d\n", first
, count
);
394 if ((first
+ count
) > ctx
->Const
.MaxViewports
) {
395 _mesa_error(ctx
, GL_INVALID_VALUE
,
396 "glDepthRangev: first (%d) + count (%d) >= MaxViewports (%d)",
397 first
, count
, ctx
->Const
.MaxViewports
);
401 depth_range_arrayv(ctx
, first
, count
, p
);
405 _mesa_DepthRangeArrayfvOES(GLuint first
, GLsizei count
, const GLfloat
*v
)
408 GET_CURRENT_CONTEXT(ctx
);
410 if (MESA_VERBOSE
& VERBOSE_API
)
411 _mesa_debug(ctx
, "glDepthRangeArrayfv %d %d\n", first
, count
);
413 if ((first
+ count
) > ctx
->Const
.MaxViewports
) {
414 _mesa_error(ctx
, GL_INVALID_VALUE
,
415 "glDepthRangeArrayfv: first (%d) + count (%d) >= MaxViewports (%d)",
416 first
, count
, ctx
->Const
.MaxViewports
);
420 for (i
= 0; i
< count
; i
++)
421 set_depth_range_no_notify(ctx
, i
+ first
, v
[i
* 2], v
[i
* 2 + 1]);
423 if (ctx
->Driver
.DepthRange
)
424 ctx
->Driver
.DepthRange(ctx
);
428 * Update a single DepthRange
430 * \param index array index to update
431 * \param nearval specifies the Z buffer value which should correspond to
432 * the near clip plane
433 * \param farval specifies the Z buffer value which should correspond to
437 _mesa_DepthRangeIndexed_no_error(GLuint index
, GLclampd nearval
,
440 GET_CURRENT_CONTEXT(ctx
);
441 _mesa_set_depth_range(ctx
, index
, nearval
, farval
);
446 _mesa_DepthRangeIndexed(GLuint index
, GLclampd nearval
, GLclampd farval
)
448 GET_CURRENT_CONTEXT(ctx
);
450 if (MESA_VERBOSE
& VERBOSE_API
)
451 _mesa_debug(ctx
, "glDepthRangeIndexed(%d, %f, %f)\n",
452 index
, nearval
, farval
);
454 if (index
>= ctx
->Const
.MaxViewports
) {
455 _mesa_error(ctx
, GL_INVALID_VALUE
,
456 "glDepthRangeIndexed: index (%d) >= MaxViewports (%d)",
457 index
, ctx
->Const
.MaxViewports
);
461 _mesa_set_depth_range(ctx
, index
, nearval
, farval
);
465 _mesa_DepthRangeIndexedfOES(GLuint index
, GLfloat nearval
, GLfloat farval
)
467 _mesa_DepthRangeIndexed(index
, nearval
, farval
);
471 * Initialize the context viewport attribute group.
472 * \param ctx the GL context.
474 void _mesa_init_viewport(struct gl_context
*ctx
)
478 ctx
->Transform
.ClipOrigin
= GL_LOWER_LEFT
;
479 ctx
->Transform
.ClipDepthMode
= GL_NEGATIVE_ONE_TO_ONE
;
481 /* Note: ctx->Const.MaxViewports may not have been set by the driver yet,
482 * so just initialize all of them.
484 for (i
= 0; i
< MAX_VIEWPORTS
; i
++) {
486 ctx
->ViewportArray
[i
].X
= 0;
487 ctx
->ViewportArray
[i
].Y
= 0;
488 ctx
->ViewportArray
[i
].Width
= 0;
489 ctx
->ViewportArray
[i
].Height
= 0;
490 ctx
->ViewportArray
[i
].Near
= 0.0;
491 ctx
->ViewportArray
[i
].Far
= 1.0;
496 static ALWAYS_INLINE
void
497 clip_control(struct gl_context
*ctx
, GLenum origin
, GLenum depth
, bool no_error
)
499 if (ctx
->Transform
.ClipOrigin
== origin
&&
500 ctx
->Transform
.ClipDepthMode
== depth
)
504 origin
!= GL_LOWER_LEFT
&& origin
!= GL_UPPER_LEFT
) {
505 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClipControl");
510 depth
!= GL_NEGATIVE_ONE_TO_ONE
&& depth
!= GL_ZERO_TO_ONE
) {
511 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClipControl");
515 /* Affects transform state and the viewport transform */
516 FLUSH_VERTICES(ctx
, ctx
->DriverFlags
.NewClipControl
? 0 :
517 _NEW_TRANSFORM
| _NEW_VIEWPORT
);
518 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewClipControl
;
520 if (ctx
->Transform
.ClipOrigin
!= origin
) {
521 ctx
->Transform
.ClipOrigin
= origin
;
523 /* Affects the winding order of the front face. */
524 if (ctx
->DriverFlags
.NewPolygonState
)
525 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewPolygonState
;
527 ctx
->NewState
|= _NEW_POLYGON
;
529 if (ctx
->Driver
.FrontFace
)
530 ctx
->Driver
.FrontFace(ctx
, ctx
->Polygon
.FrontFace
);
533 if (ctx
->Transform
.ClipDepthMode
!= depth
) {
534 ctx
->Transform
.ClipDepthMode
= depth
;
536 if (ctx
->Driver
.DepthRange
)
537 ctx
->Driver
.DepthRange(ctx
);
543 _mesa_ClipControl_no_error(GLenum origin
, GLenum depth
)
545 GET_CURRENT_CONTEXT(ctx
);
546 clip_control(ctx
, origin
, depth
, true);
551 _mesa_ClipControl(GLenum origin
, GLenum depth
)
553 GET_CURRENT_CONTEXT(ctx
);
555 if (MESA_VERBOSE
& VERBOSE_API
)
556 _mesa_debug(ctx
, "glClipControl(%s, %s)\n",
557 _mesa_enum_to_string(origin
),
558 _mesa_enum_to_string(depth
));
560 ASSERT_OUTSIDE_BEGIN_END(ctx
);
562 if (!ctx
->Extensions
.ARB_clip_control
) {
563 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glClipControl");
567 clip_control(ctx
, origin
, depth
, false);
571 * Computes the scaling and the translation part of the
572 * viewport transform matrix of the \param i-th viewport
573 * and writes that into \param scale and \param translate.
576 _mesa_get_viewport_xform(struct gl_context
*ctx
, unsigned i
,
577 float scale
[3], float translate
[3])
579 float x
= ctx
->ViewportArray
[i
].X
;
580 float y
= ctx
->ViewportArray
[i
].Y
;
581 float half_width
= 0.5f
* ctx
->ViewportArray
[i
].Width
;
582 float half_height
= 0.5f
* ctx
->ViewportArray
[i
].Height
;
583 double n
= ctx
->ViewportArray
[i
].Near
;
584 double f
= ctx
->ViewportArray
[i
].Far
;
586 scale
[0] = half_width
;
587 translate
[0] = half_width
+ x
;
588 if (ctx
->Transform
.ClipOrigin
== GL_UPPER_LEFT
) {
589 scale
[1] = -half_height
;
591 scale
[1] = half_height
;
593 translate
[1] = half_height
+ y
;
595 if (ctx
->Transform
.ClipDepthMode
== GL_NEGATIVE_ONE_TO_ONE
) {
596 scale
[2] = 0.5 * (f
- n
);
597 translate
[2] = 0.5 * (n
+ f
);