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 ctx
->ViewportArray
[idx
].X
= x
;
71 ctx
->ViewportArray
[idx
].Width
= width
;
72 ctx
->ViewportArray
[idx
].Y
= y
;
73 ctx
->ViewportArray
[idx
].Height
= height
;
74 ctx
->NewState
|= _NEW_VIEWPORT
;
77 struct gl_viewport_inputs
{
78 GLfloat X
, Y
; /**< position */
79 GLfloat Width
, Height
; /**< size */
82 struct gl_depthrange_inputs
{
83 GLdouble Near
, Far
; /**< Depth buffer range */
88 * \sa Called via glViewport() or display list execution.
90 * Flushes the vertices and calls _mesa_set_viewport() with the given
94 _mesa_Viewport(GLint x
, GLint y
, GLsizei width
, GLsizei height
)
97 GET_CURRENT_CONTEXT(ctx
);
98 FLUSH_VERTICES(ctx
, 0);
100 if (MESA_VERBOSE
& VERBOSE_API
)
101 _mesa_debug(ctx
, "glViewport %d %d %d %d\n", x
, y
, width
, height
);
103 if (width
< 0 || height
< 0) {
104 _mesa_error(ctx
, GL_INVALID_VALUE
,
105 "glViewport(%d, %d, %d, %d)", x
, y
, width
, height
);
109 /* The GL_ARB_viewport_array spec says:
111 * "Viewport sets the parameters for all viewports to the same values
112 * and is equivalent (assuming no errors are generated) to:
114 * for (uint i = 0; i < MAX_VIEWPORTS; i++)
115 * ViewportIndexedf(i, 1, (float)x, (float)y, (float)w, (float)h);"
117 * Set all of the viewports supported by the implementation, but only
118 * signal the driver once at the end.
120 for (i
= 0; i
< ctx
->Const
.MaxViewports
; i
++)
121 set_viewport_no_notify(ctx
, i
, x
, y
, width
, height
);
123 if (ctx
->Driver
.Viewport
) {
124 /* Many drivers will use this call to check for window size changes
125 * and reallocate the z/stencil/accum/etc buffers if needed.
127 ctx
->Driver
.Viewport(ctx
);
133 * Set new viewport parameters and update derived state.
134 * Usually called from _mesa_Viewport().
136 * \param ctx GL context.
137 * \param idx Index of the viewport to be updated.
138 * \param x, y coordinates of the lower left corner of the viewport rectangle.
139 * \param width width of the viewport rectangle.
140 * \param height height of the viewport rectangle.
143 _mesa_set_viewport(struct gl_context
*ctx
, unsigned idx
, GLfloat x
, GLfloat y
,
144 GLfloat width
, GLfloat height
)
146 set_viewport_no_notify(ctx
, idx
, x
, y
, width
, height
);
148 if (ctx
->Driver
.Viewport
) {
149 /* Many drivers will use this call to check for window size changes
150 * and reallocate the z/stencil/accum/etc buffers if needed.
152 ctx
->Driver
.Viewport(ctx
);
157 _mesa_ViewportArrayv(GLuint first
, GLsizei count
, const GLfloat
*v
)
160 const struct gl_viewport_inputs
*const p
= (struct gl_viewport_inputs
*) v
;
161 GET_CURRENT_CONTEXT(ctx
);
163 if (MESA_VERBOSE
& VERBOSE_API
)
164 _mesa_debug(ctx
, "glViewportArrayv %d %d\n", first
, count
);
166 if ((first
+ count
) > ctx
->Const
.MaxViewports
) {
167 _mesa_error(ctx
, GL_INVALID_VALUE
,
168 "glViewportArrayv: first (%d) + count (%d) > MaxViewports "
170 first
, count
, ctx
->Const
.MaxViewports
);
174 /* Verify width & height */
175 for (i
= 0; i
< count
; i
++) {
176 if (p
[i
].Width
< 0 || p
[i
].Height
< 0) {
177 _mesa_error(ctx
, GL_INVALID_VALUE
,
178 "glViewportArrayv: index (%d) width or height < 0 "
180 i
+ first
, p
[i
].Width
, p
[i
].Height
);
185 for (i
= 0; i
< count
; i
++)
186 set_viewport_no_notify(ctx
, i
+ first
,
188 p
[i
].Width
, p
[i
].Height
);
190 if (ctx
->Driver
.Viewport
)
191 ctx
->Driver
.Viewport(ctx
);
195 ViewportIndexedf(GLuint index
, GLfloat x
, GLfloat y
,
196 GLfloat w
, GLfloat h
, const char *function
)
198 GET_CURRENT_CONTEXT(ctx
);
200 if (MESA_VERBOSE
& VERBOSE_API
)
201 _mesa_debug(ctx
, "%s(%d, %f, %f, %f, %f)\n",
202 function
, index
, x
, y
, w
, h
);
204 if (index
>= ctx
->Const
.MaxViewports
) {
205 _mesa_error(ctx
, GL_INVALID_VALUE
,
206 "%s: index (%d) >= MaxViewports (%d)",
207 function
, index
, ctx
->Const
.MaxViewports
);
211 /* Verify width & height */
212 if (w
< 0 || h
< 0) {
213 _mesa_error(ctx
, GL_INVALID_VALUE
,
214 "%s: index (%d) width or height < 0 (%f, %f)",
215 function
, index
, w
, h
);
219 _mesa_set_viewport(ctx
, index
, x
, y
, w
, h
);
223 _mesa_ViewportIndexedf(GLuint index
, GLfloat x
, GLfloat y
,
224 GLfloat w
, GLfloat h
)
226 ViewportIndexedf(index
, x
, y
, w
, h
, "glViewportIndexedf");
230 _mesa_ViewportIndexedfv(GLuint index
, const GLfloat
*v
)
232 ViewportIndexedf(index
, v
[0], v
[1], v
[2], v
[3], "glViewportIndexedfv");
236 set_depth_range_no_notify(struct gl_context
*ctx
, unsigned idx
,
237 GLclampd nearval
, GLclampd farval
)
239 if (ctx
->ViewportArray
[idx
].Near
== nearval
&&
240 ctx
->ViewportArray
[idx
].Far
== farval
)
243 ctx
->ViewportArray
[idx
].Near
= CLAMP(nearval
, 0.0, 1.0);
244 ctx
->ViewportArray
[idx
].Far
= CLAMP(farval
, 0.0, 1.0);
245 ctx
->NewState
|= _NEW_VIEWPORT
;
249 _mesa_set_depth_range(struct gl_context
*ctx
, unsigned idx
,
250 GLclampd nearval
, GLclampd farval
)
252 set_depth_range_no_notify(ctx
, idx
, nearval
, farval
);
254 if (ctx
->Driver
.DepthRange
)
255 ctx
->Driver
.DepthRange(ctx
);
259 * Called by glDepthRange
261 * \param nearval specifies the Z buffer value which should correspond to
262 * the near clip plane
263 * \param farval specifies the Z buffer value which should correspond to
267 _mesa_DepthRange(GLclampd nearval
, GLclampd farval
)
270 GET_CURRENT_CONTEXT(ctx
);
272 FLUSH_VERTICES(ctx
, 0);
274 if (MESA_VERBOSE
&VERBOSE_API
)
275 _mesa_debug(ctx
, "glDepthRange %f %f\n", nearval
, farval
);
277 /* The GL_ARB_viewport_array spec says:
279 * "DepthRange sets the depth range for all viewports to the same
280 * values and is equivalent (assuming no errors are generated) to:
282 * for (uint i = 0; i < MAX_VIEWPORTS; i++)
283 * DepthRangeIndexed(i, n, f);"
285 * Set the depth range for all of the viewports supported by the
286 * implementation, but only signal the driver once at the end.
288 for (i
= 0; i
< ctx
->Const
.MaxViewports
; i
++)
289 set_depth_range_no_notify(ctx
, i
, nearval
, farval
);
291 if (ctx
->Driver
.DepthRange
) {
292 ctx
->Driver
.DepthRange(ctx
);
297 _mesa_DepthRangef(GLclampf nearval
, GLclampf farval
)
299 _mesa_DepthRange(nearval
, farval
);
303 * Update a range DepthRange values
305 * \param first starting array index
306 * \param count count of DepthRange items to update
307 * \param v pointer to memory containing
308 * GLclampd near and far clip-plane values
311 _mesa_DepthRangeArrayv(GLuint first
, GLsizei count
, const GLclampd
*v
)
314 const struct gl_depthrange_inputs
*const p
=
315 (struct gl_depthrange_inputs
*) v
;
316 GET_CURRENT_CONTEXT(ctx
);
318 if (MESA_VERBOSE
& VERBOSE_API
)
319 _mesa_debug(ctx
, "glDepthRangeArrayv %d %d\n", first
, count
);
321 if ((first
+ count
) > ctx
->Const
.MaxViewports
) {
322 _mesa_error(ctx
, GL_INVALID_VALUE
,
323 "glDepthRangev: first (%d) + count (%d) >= MaxViewports (%d)",
324 first
, count
, ctx
->Const
.MaxViewports
);
328 for (i
= 0; i
< count
; i
++)
329 set_depth_range_no_notify(ctx
, i
+ first
, p
[i
].Near
, p
[i
].Far
);
331 if (ctx
->Driver
.DepthRange
)
332 ctx
->Driver
.DepthRange(ctx
);
336 _mesa_DepthRangeArrayfvOES(GLuint first
, GLsizei count
, const GLfloat
*v
)
339 GET_CURRENT_CONTEXT(ctx
);
341 if (MESA_VERBOSE
& VERBOSE_API
)
342 _mesa_debug(ctx
, "glDepthRangeArrayfv %d %d\n", first
, count
);
344 if ((first
+ count
) > ctx
->Const
.MaxViewports
) {
345 _mesa_error(ctx
, GL_INVALID_VALUE
,
346 "glDepthRangeArrayfv: first (%d) + count (%d) >= MaxViewports (%d)",
347 first
, count
, ctx
->Const
.MaxViewports
);
351 for (i
= 0; i
< count
; i
++)
352 set_depth_range_no_notify(ctx
, i
+ first
, v
[i
* 2], v
[i
* 2 + 1]);
354 if (ctx
->Driver
.DepthRange
)
355 ctx
->Driver
.DepthRange(ctx
);
359 * Update a single DepthRange
361 * \param index array index to update
362 * \param nearval specifies the Z buffer value which should correspond to
363 * the near clip plane
364 * \param farval specifies the Z buffer value which should correspond to
368 _mesa_DepthRangeIndexed(GLuint index
, GLclampd nearval
, GLclampd farval
)
370 GET_CURRENT_CONTEXT(ctx
);
372 if (MESA_VERBOSE
& VERBOSE_API
)
373 _mesa_debug(ctx
, "glDepthRangeIndexed(%d, %f, %f)\n",
374 index
, nearval
, farval
);
376 if (index
>= ctx
->Const
.MaxViewports
) {
377 _mesa_error(ctx
, GL_INVALID_VALUE
,
378 "glDepthRangeIndexed: index (%d) >= MaxViewports (%d)",
379 index
, ctx
->Const
.MaxViewports
);
383 _mesa_set_depth_range(ctx
, index
, nearval
, farval
);
387 _mesa_DepthRangeIndexedfOES(GLuint index
, GLfloat nearval
, GLfloat farval
)
389 _mesa_DepthRangeIndexed(index
, nearval
, farval
);
393 * Initialize the context viewport attribute group.
394 * \param ctx the GL context.
396 void _mesa_init_viewport(struct gl_context
*ctx
)
400 ctx
->Transform
.ClipOrigin
= GL_LOWER_LEFT
;
401 ctx
->Transform
.ClipDepthMode
= GL_NEGATIVE_ONE_TO_ONE
;
403 /* Note: ctx->Const.MaxViewports may not have been set by the driver yet,
404 * so just initialize all of them.
406 for (i
= 0; i
< MAX_VIEWPORTS
; i
++) {
408 ctx
->ViewportArray
[i
].X
= 0;
409 ctx
->ViewportArray
[i
].Y
= 0;
410 ctx
->ViewportArray
[i
].Width
= 0;
411 ctx
->ViewportArray
[i
].Height
= 0;
412 ctx
->ViewportArray
[i
].Near
= 0.0;
413 ctx
->ViewportArray
[i
].Far
= 1.0;
418 extern void GLAPIENTRY
419 _mesa_ClipControl(GLenum origin
, GLenum depth
)
421 GET_CURRENT_CONTEXT(ctx
);
423 if (MESA_VERBOSE
&VERBOSE_API
)
424 _mesa_debug(ctx
, "glClipControl(%s, %s)\n",
425 _mesa_enum_to_string(origin
),
426 _mesa_enum_to_string(depth
));
428 ASSERT_OUTSIDE_BEGIN_END(ctx
);
430 if (!ctx
->Extensions
.ARB_clip_control
) {
431 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glClipControl");
435 if (origin
!= GL_LOWER_LEFT
&& origin
!= GL_UPPER_LEFT
) {
436 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClipControl");
440 if (depth
!= GL_NEGATIVE_ONE_TO_ONE
&& depth
!= GL_ZERO_TO_ONE
) {
441 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClipControl");
445 if (ctx
->Transform
.ClipOrigin
== origin
&&
446 ctx
->Transform
.ClipDepthMode
== depth
)
449 /* Affects transform state and the viewport transform */
450 FLUSH_VERTICES(ctx
, _NEW_TRANSFORM
| _NEW_VIEWPORT
);
452 if (ctx
->Transform
.ClipOrigin
!= origin
) {
453 ctx
->Transform
.ClipOrigin
= origin
;
455 /* Affects the winding order of the front face. */
456 ctx
->NewState
|= _NEW_POLYGON
;
458 if (ctx
->Driver
.FrontFace
)
459 ctx
->Driver
.FrontFace(ctx
, ctx
->Polygon
.FrontFace
);
462 if (ctx
->Transform
.ClipDepthMode
!= depth
) {
463 ctx
->Transform
.ClipDepthMode
= depth
;
465 if (ctx
->Driver
.DepthRange
)
466 ctx
->Driver
.DepthRange(ctx
);
471 * Computes the scaling and the translation part of the
472 * viewport transform matrix of the \param i-th viewport
473 * and writes that into \param scale and \param translate.
476 _mesa_get_viewport_xform(struct gl_context
*ctx
, unsigned i
,
477 float scale
[3], float translate
[3])
479 float x
= ctx
->ViewportArray
[i
].X
;
480 float y
= ctx
->ViewportArray
[i
].Y
;
481 float half_width
= 0.5f
* ctx
->ViewportArray
[i
].Width
;
482 float half_height
= 0.5f
* ctx
->ViewportArray
[i
].Height
;
483 double n
= ctx
->ViewportArray
[i
].Near
;
484 double f
= ctx
->ViewportArray
[i
].Far
;
486 scale
[0] = half_width
;
487 translate
[0] = half_width
+ x
;
488 if (ctx
->Transform
.ClipOrigin
== GL_UPPER_LEFT
) {
489 scale
[1] = -half_height
;
491 scale
[1] = half_height
;
493 translate
[1] = half_height
+ y
;
495 if (ctx
->Transform
.ClipDepthMode
== GL_NEGATIVE_ONE_TO_ONE
) {
496 scale
[2] = 0.5 * (f
- n
);
497 translate
[2] = 0.5 * (n
+ f
);