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 FLUSH_VERTICES(ctx
, ctx
->DriverFlags
.NewViewport
? 0 : _NEW_VIEWPORT
);
44 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewViewport
;
46 /* clamp width and height to the implementation dependent range */
47 width
= MIN2(width
, (GLfloat
) ctx
->Const
.MaxViewportWidth
);
48 height
= MIN2(height
, (GLfloat
) ctx
->Const
.MaxViewportHeight
);
50 /* The GL_ARB_viewport_array spec says:
52 * "The location of the viewport's bottom-left corner, given by (x,y),
53 * are clamped to be within the implementation-dependent viewport
54 * bounds range. The viewport bounds range [min, max] tuple may be
55 * determined by calling GetFloatv with the symbolic constant
56 * VIEWPORT_BOUNDS_RANGE (see section 6.1)."
58 if (ctx
->Extensions
.ARB_viewport_array
||
59 (ctx
->Extensions
.OES_viewport_array
&&
60 _mesa_is_gles31(ctx
))) {
62 ctx
->Const
.ViewportBounds
.Min
, ctx
->Const
.ViewportBounds
.Max
);
64 ctx
->Const
.ViewportBounds
.Min
, ctx
->Const
.ViewportBounds
.Max
);
67 if (ctx
->ViewportArray
[idx
].X
== x
&&
68 ctx
->ViewportArray
[idx
].Width
== width
&&
69 ctx
->ViewportArray
[idx
].Y
== y
&&
70 ctx
->ViewportArray
[idx
].Height
== height
)
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 */
90 * \sa Called via glViewport() or display list execution.
92 * Flushes the vertices and calls _mesa_set_viewport() with the given
96 _mesa_Viewport(GLint x
, GLint y
, GLsizei width
, GLsizei height
)
99 GET_CURRENT_CONTEXT(ctx
);
100 FLUSH_VERTICES(ctx
, 0);
102 if (MESA_VERBOSE
& VERBOSE_API
)
103 _mesa_debug(ctx
, "glViewport %d %d %d %d\n", x
, y
, width
, height
);
105 if (width
< 0 || height
< 0) {
106 _mesa_error(ctx
, GL_INVALID_VALUE
,
107 "glViewport(%d, %d, %d, %d)", x
, y
, width
, height
);
111 /* The GL_ARB_viewport_array spec says:
113 * "Viewport sets the parameters for all viewports to the same values
114 * and is equivalent (assuming no errors are generated) to:
116 * for (uint i = 0; i < MAX_VIEWPORTS; i++)
117 * ViewportIndexedf(i, 1, (float)x, (float)y, (float)w, (float)h);"
119 * Set all of the viewports supported by the implementation, but only
120 * signal the driver once at the end.
122 for (i
= 0; i
< ctx
->Const
.MaxViewports
; i
++)
123 set_viewport_no_notify(ctx
, i
, x
, y
, width
, height
);
125 if (ctx
->Driver
.Viewport
) {
126 /* Many drivers will use this call to check for window size changes
127 * and reallocate the z/stencil/accum/etc buffers if needed.
129 ctx
->Driver
.Viewport(ctx
);
135 * Set new viewport parameters and update derived state.
136 * Usually called from _mesa_Viewport().
138 * \param ctx GL context.
139 * \param idx Index of the viewport to be updated.
140 * \param x, y coordinates of the lower left corner of the viewport rectangle.
141 * \param width width of the viewport rectangle.
142 * \param height height of the viewport rectangle.
145 _mesa_set_viewport(struct gl_context
*ctx
, unsigned idx
, GLfloat x
, GLfloat y
,
146 GLfloat width
, GLfloat height
)
148 set_viewport_no_notify(ctx
, idx
, x
, y
, width
, height
);
150 if (ctx
->Driver
.Viewport
) {
151 /* Many drivers will use this call to check for window size changes
152 * and reallocate the z/stencil/accum/etc buffers if needed.
154 ctx
->Driver
.Viewport(ctx
);
159 _mesa_ViewportArrayv(GLuint first
, GLsizei count
, const GLfloat
*v
)
162 const struct gl_viewport_inputs
*const p
= (struct gl_viewport_inputs
*) v
;
163 GET_CURRENT_CONTEXT(ctx
);
165 if (MESA_VERBOSE
& VERBOSE_API
)
166 _mesa_debug(ctx
, "glViewportArrayv %d %d\n", first
, count
);
168 if ((first
+ count
) > ctx
->Const
.MaxViewports
) {
169 _mesa_error(ctx
, GL_INVALID_VALUE
,
170 "glViewportArrayv: first (%d) + count (%d) > MaxViewports "
172 first
, count
, ctx
->Const
.MaxViewports
);
176 /* Verify width & height */
177 for (i
= 0; i
< count
; i
++) {
178 if (p
[i
].Width
< 0 || p
[i
].Height
< 0) {
179 _mesa_error(ctx
, GL_INVALID_VALUE
,
180 "glViewportArrayv: index (%d) width or height < 0 "
182 i
+ first
, p
[i
].Width
, p
[i
].Height
);
187 for (i
= 0; i
< count
; i
++)
188 set_viewport_no_notify(ctx
, i
+ first
,
190 p
[i
].Width
, p
[i
].Height
);
192 if (ctx
->Driver
.Viewport
)
193 ctx
->Driver
.Viewport(ctx
);
197 ViewportIndexedf(GLuint index
, GLfloat x
, GLfloat y
,
198 GLfloat w
, GLfloat h
, const char *function
)
200 GET_CURRENT_CONTEXT(ctx
);
202 if (MESA_VERBOSE
& VERBOSE_API
)
203 _mesa_debug(ctx
, "%s(%d, %f, %f, %f, %f)\n",
204 function
, index
, x
, y
, w
, h
);
206 if (index
>= ctx
->Const
.MaxViewports
) {
207 _mesa_error(ctx
, GL_INVALID_VALUE
,
208 "%s: index (%d) >= MaxViewports (%d)",
209 function
, index
, ctx
->Const
.MaxViewports
);
213 /* Verify width & height */
214 if (w
< 0 || h
< 0) {
215 _mesa_error(ctx
, GL_INVALID_VALUE
,
216 "%s: index (%d) width or height < 0 (%f, %f)",
217 function
, index
, w
, h
);
221 _mesa_set_viewport(ctx
, index
, x
, y
, w
, h
);
225 _mesa_ViewportIndexedf(GLuint index
, GLfloat x
, GLfloat y
,
226 GLfloat w
, GLfloat h
)
228 ViewportIndexedf(index
, x
, y
, w
, h
, "glViewportIndexedf");
232 _mesa_ViewportIndexedfv(GLuint index
, const GLfloat
*v
)
234 ViewportIndexedf(index
, v
[0], v
[1], v
[2], v
[3], "glViewportIndexedfv");
238 set_depth_range_no_notify(struct gl_context
*ctx
, unsigned idx
,
239 GLclampd nearval
, GLclampd farval
)
241 if (ctx
->ViewportArray
[idx
].Near
== nearval
&&
242 ctx
->ViewportArray
[idx
].Far
== farval
)
245 /* The depth range is needed by program state constants. */
246 FLUSH_VERTICES(ctx
, _NEW_VIEWPORT
);
247 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewViewport
;
249 ctx
->ViewportArray
[idx
].Near
= CLAMP(nearval
, 0.0, 1.0);
250 ctx
->ViewportArray
[idx
].Far
= CLAMP(farval
, 0.0, 1.0);
254 _mesa_set_depth_range(struct gl_context
*ctx
, unsigned idx
,
255 GLclampd nearval
, GLclampd farval
)
257 set_depth_range_no_notify(ctx
, idx
, nearval
, farval
);
259 if (ctx
->Driver
.DepthRange
)
260 ctx
->Driver
.DepthRange(ctx
);
264 * Called by glDepthRange
266 * \param nearval specifies the Z buffer value which should correspond to
267 * the near clip plane
268 * \param farval specifies the Z buffer value which should correspond to
272 _mesa_DepthRange(GLclampd nearval
, GLclampd farval
)
275 GET_CURRENT_CONTEXT(ctx
);
277 FLUSH_VERTICES(ctx
, 0);
279 if (MESA_VERBOSE
&VERBOSE_API
)
280 _mesa_debug(ctx
, "glDepthRange %f %f\n", nearval
, farval
);
282 /* The GL_ARB_viewport_array spec says:
284 * "DepthRange sets the depth range for all viewports to the same
285 * values and is equivalent (assuming no errors are generated) to:
287 * for (uint i = 0; i < MAX_VIEWPORTS; i++)
288 * DepthRangeIndexed(i, n, f);"
290 * Set the depth range for all of the viewports supported by the
291 * implementation, but only signal the driver once at the end.
293 for (i
= 0; i
< ctx
->Const
.MaxViewports
; i
++)
294 set_depth_range_no_notify(ctx
, i
, nearval
, farval
);
296 if (ctx
->Driver
.DepthRange
) {
297 ctx
->Driver
.DepthRange(ctx
);
302 _mesa_DepthRangef(GLclampf nearval
, GLclampf farval
)
304 _mesa_DepthRange(nearval
, farval
);
308 * Update a range DepthRange values
310 * \param first starting array index
311 * \param count count of DepthRange items to update
312 * \param v pointer to memory containing
313 * GLclampd near and far clip-plane values
316 _mesa_DepthRangeArrayv(GLuint first
, GLsizei count
, const GLclampd
*v
)
319 const struct gl_depthrange_inputs
*const p
=
320 (struct gl_depthrange_inputs
*) v
;
321 GET_CURRENT_CONTEXT(ctx
);
323 if (MESA_VERBOSE
& VERBOSE_API
)
324 _mesa_debug(ctx
, "glDepthRangeArrayv %d %d\n", first
, count
);
326 if ((first
+ count
) > ctx
->Const
.MaxViewports
) {
327 _mesa_error(ctx
, GL_INVALID_VALUE
,
328 "glDepthRangev: first (%d) + count (%d) >= MaxViewports (%d)",
329 first
, count
, ctx
->Const
.MaxViewports
);
333 for (i
= 0; i
< count
; i
++)
334 set_depth_range_no_notify(ctx
, i
+ first
, p
[i
].Near
, p
[i
].Far
);
336 if (ctx
->Driver
.DepthRange
)
337 ctx
->Driver
.DepthRange(ctx
);
341 _mesa_DepthRangeArrayfvOES(GLuint first
, GLsizei count
, const GLfloat
*v
)
344 GET_CURRENT_CONTEXT(ctx
);
346 if (MESA_VERBOSE
& VERBOSE_API
)
347 _mesa_debug(ctx
, "glDepthRangeArrayfv %d %d\n", first
, count
);
349 if ((first
+ count
) > ctx
->Const
.MaxViewports
) {
350 _mesa_error(ctx
, GL_INVALID_VALUE
,
351 "glDepthRangeArrayfv: first (%d) + count (%d) >= MaxViewports (%d)",
352 first
, count
, ctx
->Const
.MaxViewports
);
356 for (i
= 0; i
< count
; i
++)
357 set_depth_range_no_notify(ctx
, i
+ first
, v
[i
* 2], v
[i
* 2 + 1]);
359 if (ctx
->Driver
.DepthRange
)
360 ctx
->Driver
.DepthRange(ctx
);
364 * Update a single DepthRange
366 * \param index array index to update
367 * \param nearval specifies the Z buffer value which should correspond to
368 * the near clip plane
369 * \param farval specifies the Z buffer value which should correspond to
373 _mesa_DepthRangeIndexed(GLuint index
, GLclampd nearval
, GLclampd farval
)
375 GET_CURRENT_CONTEXT(ctx
);
377 if (MESA_VERBOSE
& VERBOSE_API
)
378 _mesa_debug(ctx
, "glDepthRangeIndexed(%d, %f, %f)\n",
379 index
, nearval
, farval
);
381 if (index
>= ctx
->Const
.MaxViewports
) {
382 _mesa_error(ctx
, GL_INVALID_VALUE
,
383 "glDepthRangeIndexed: index (%d) >= MaxViewports (%d)",
384 index
, ctx
->Const
.MaxViewports
);
388 _mesa_set_depth_range(ctx
, index
, nearval
, farval
);
392 _mesa_DepthRangeIndexedfOES(GLuint index
, GLfloat nearval
, GLfloat farval
)
394 _mesa_DepthRangeIndexed(index
, nearval
, farval
);
398 * Initialize the context viewport attribute group.
399 * \param ctx the GL context.
401 void _mesa_init_viewport(struct gl_context
*ctx
)
405 ctx
->Transform
.ClipOrigin
= GL_LOWER_LEFT
;
406 ctx
->Transform
.ClipDepthMode
= GL_NEGATIVE_ONE_TO_ONE
;
408 /* Note: ctx->Const.MaxViewports may not have been set by the driver yet,
409 * so just initialize all of them.
411 for (i
= 0; i
< MAX_VIEWPORTS
; i
++) {
413 ctx
->ViewportArray
[i
].X
= 0;
414 ctx
->ViewportArray
[i
].Y
= 0;
415 ctx
->ViewportArray
[i
].Width
= 0;
416 ctx
->ViewportArray
[i
].Height
= 0;
417 ctx
->ViewportArray
[i
].Near
= 0.0;
418 ctx
->ViewportArray
[i
].Far
= 1.0;
423 extern void GLAPIENTRY
424 _mesa_ClipControl(GLenum origin
, GLenum depth
)
426 GET_CURRENT_CONTEXT(ctx
);
428 if (MESA_VERBOSE
&VERBOSE_API
)
429 _mesa_debug(ctx
, "glClipControl(%s, %s)\n",
430 _mesa_enum_to_string(origin
),
431 _mesa_enum_to_string(depth
));
433 ASSERT_OUTSIDE_BEGIN_END(ctx
);
435 if (!ctx
->Extensions
.ARB_clip_control
) {
436 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glClipControl");
440 if (origin
!= GL_LOWER_LEFT
&& origin
!= GL_UPPER_LEFT
) {
441 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClipControl");
445 if (depth
!= GL_NEGATIVE_ONE_TO_ONE
&& depth
!= GL_ZERO_TO_ONE
) {
446 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClipControl");
450 if (ctx
->Transform
.ClipOrigin
== origin
&&
451 ctx
->Transform
.ClipDepthMode
== depth
)
454 /* Affects transform state and the viewport transform */
455 FLUSH_VERTICES(ctx
, ctx
->DriverFlags
.NewClipControl
? 0 :
456 _NEW_TRANSFORM
| _NEW_VIEWPORT
);
457 ctx
->NewDriverState
|= ctx
->DriverFlags
.NewClipControl
;
459 if (ctx
->Transform
.ClipOrigin
!= origin
) {
460 ctx
->Transform
.ClipOrigin
= origin
;
462 /* Affects the winding order of the front face. */
463 ctx
->NewState
|= _NEW_POLYGON
;
465 if (ctx
->Driver
.FrontFace
)
466 ctx
->Driver
.FrontFace(ctx
, ctx
->Polygon
.FrontFace
);
469 if (ctx
->Transform
.ClipDepthMode
!= depth
) {
470 ctx
->Transform
.ClipDepthMode
= depth
;
472 if (ctx
->Driver
.DepthRange
)
473 ctx
->Driver
.DepthRange(ctx
);
478 * Computes the scaling and the translation part of the
479 * viewport transform matrix of the \param i-th viewport
480 * and writes that into \param scale and \param translate.
483 _mesa_get_viewport_xform(struct gl_context
*ctx
, unsigned i
,
484 float scale
[3], float translate
[3])
486 float x
= ctx
->ViewportArray
[i
].X
;
487 float y
= ctx
->ViewportArray
[i
].Y
;
488 float half_width
= 0.5f
* ctx
->ViewportArray
[i
].Width
;
489 float half_height
= 0.5f
* ctx
->ViewportArray
[i
].Height
;
490 double n
= ctx
->ViewportArray
[i
].Near
;
491 double f
= ctx
->ViewportArray
[i
].Far
;
493 scale
[0] = half_width
;
494 translate
[0] = half_width
+ x
;
495 if (ctx
->Transform
.ClipOrigin
== GL_UPPER_LEFT
) {
496 scale
[1] = -half_height
;
498 scale
[1] = half_height
;
500 translate
[1] = half_height
+ y
;
502 if (ctx
->Transform
.ClipDepthMode
== GL_NEGATIVE_ONE_TO_ONE
) {
503 scale
[2] = 0.5 * (f
- n
);
504 translate
[2] = 0.5 * (n
+ f
);