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
, _NEW_VIEWPORT
);
45 /* clamp width and height to the implementation dependent range */
46 width
= MIN2(width
, (GLfloat
) ctx
->Const
.MaxViewportWidth
);
47 height
= MIN2(height
, (GLfloat
) ctx
->Const
.MaxViewportHeight
);
49 /* The GL_ARB_viewport_array spec says:
51 * "The location of the viewport's bottom-left corner, given by (x,y),
52 * are clamped to be within the implementation-dependent viewport
53 * bounds range. The viewport bounds range [min, max] tuple may be
54 * determined by calling GetFloatv with the symbolic constant
55 * VIEWPORT_BOUNDS_RANGE (see section 6.1)."
57 if (ctx
->Extensions
.ARB_viewport_array
||
58 (ctx
->Extensions
.OES_viewport_array
&&
59 _mesa_is_gles31(ctx
))) {
61 ctx
->Const
.ViewportBounds
.Min
, ctx
->Const
.ViewportBounds
.Max
);
63 ctx
->Const
.ViewportBounds
.Min
, ctx
->Const
.ViewportBounds
.Max
);
66 if (ctx
->ViewportArray
[idx
].X
== x
&&
67 ctx
->ViewportArray
[idx
].Width
== width
&&
68 ctx
->ViewportArray
[idx
].Y
== y
&&
69 ctx
->ViewportArray
[idx
].Height
== height
)
72 ctx
->ViewportArray
[idx
].X
= x
;
73 ctx
->ViewportArray
[idx
].Width
= width
;
74 ctx
->ViewportArray
[idx
].Y
= y
;
75 ctx
->ViewportArray
[idx
].Height
= height
;
78 struct gl_viewport_inputs
{
79 GLfloat X
, Y
; /**< position */
80 GLfloat Width
, Height
; /**< size */
83 struct gl_depthrange_inputs
{
84 GLdouble Near
, Far
; /**< Depth buffer range */
89 * \sa Called via glViewport() or display list execution.
91 * Flushes the vertices and calls _mesa_set_viewport() with the given
95 _mesa_Viewport(GLint x
, GLint y
, GLsizei width
, GLsizei height
)
98 GET_CURRENT_CONTEXT(ctx
);
99 FLUSH_VERTICES(ctx
, 0);
101 if (MESA_VERBOSE
& VERBOSE_API
)
102 _mesa_debug(ctx
, "glViewport %d %d %d %d\n", x
, y
, width
, height
);
104 if (width
< 0 || height
< 0) {
105 _mesa_error(ctx
, GL_INVALID_VALUE
,
106 "glViewport(%d, %d, %d, %d)", x
, y
, width
, height
);
110 /* The GL_ARB_viewport_array spec says:
112 * "Viewport sets the parameters for all viewports to the same values
113 * and is equivalent (assuming no errors are generated) to:
115 * for (uint i = 0; i < MAX_VIEWPORTS; i++)
116 * ViewportIndexedf(i, 1, (float)x, (float)y, (float)w, (float)h);"
118 * Set all of the viewports supported by the implementation, but only
119 * signal the driver once at the end.
121 for (i
= 0; i
< ctx
->Const
.MaxViewports
; i
++)
122 set_viewport_no_notify(ctx
, i
, x
, y
, width
, height
);
124 if (ctx
->Driver
.Viewport
) {
125 /* Many drivers will use this call to check for window size changes
126 * and reallocate the z/stencil/accum/etc buffers if needed.
128 ctx
->Driver
.Viewport(ctx
);
134 * Set new viewport parameters and update derived state.
135 * Usually called from _mesa_Viewport().
137 * \param ctx GL context.
138 * \param idx Index of the viewport to be updated.
139 * \param x, y coordinates of the lower left corner of the viewport rectangle.
140 * \param width width of the viewport rectangle.
141 * \param height height of the viewport rectangle.
144 _mesa_set_viewport(struct gl_context
*ctx
, unsigned idx
, GLfloat x
, GLfloat y
,
145 GLfloat width
, GLfloat height
)
147 set_viewport_no_notify(ctx
, idx
, x
, y
, width
, height
);
149 if (ctx
->Driver
.Viewport
) {
150 /* Many drivers will use this call to check for window size changes
151 * and reallocate the z/stencil/accum/etc buffers if needed.
153 ctx
->Driver
.Viewport(ctx
);
158 _mesa_ViewportArrayv(GLuint first
, GLsizei count
, const GLfloat
*v
)
161 const struct gl_viewport_inputs
*const p
= (struct gl_viewport_inputs
*) v
;
162 GET_CURRENT_CONTEXT(ctx
);
164 if (MESA_VERBOSE
& VERBOSE_API
)
165 _mesa_debug(ctx
, "glViewportArrayv %d %d\n", first
, count
);
167 if ((first
+ count
) > ctx
->Const
.MaxViewports
) {
168 _mesa_error(ctx
, GL_INVALID_VALUE
,
169 "glViewportArrayv: first (%d) + count (%d) > MaxViewports "
171 first
, count
, ctx
->Const
.MaxViewports
);
175 /* Verify width & height */
176 for (i
= 0; i
< count
; i
++) {
177 if (p
[i
].Width
< 0 || p
[i
].Height
< 0) {
178 _mesa_error(ctx
, GL_INVALID_VALUE
,
179 "glViewportArrayv: index (%d) width or height < 0 "
181 i
+ first
, p
[i
].Width
, p
[i
].Height
);
186 for (i
= 0; i
< count
; i
++)
187 set_viewport_no_notify(ctx
, i
+ first
,
189 p
[i
].Width
, p
[i
].Height
);
191 if (ctx
->Driver
.Viewport
)
192 ctx
->Driver
.Viewport(ctx
);
196 ViewportIndexedf(GLuint index
, GLfloat x
, GLfloat y
,
197 GLfloat w
, GLfloat h
, const char *function
)
199 GET_CURRENT_CONTEXT(ctx
);
201 if (MESA_VERBOSE
& VERBOSE_API
)
202 _mesa_debug(ctx
, "%s(%d, %f, %f, %f, %f)\n",
203 function
, index
, x
, y
, w
, h
);
205 if (index
>= ctx
->Const
.MaxViewports
) {
206 _mesa_error(ctx
, GL_INVALID_VALUE
,
207 "%s: index (%d) >= MaxViewports (%d)",
208 function
, index
, ctx
->Const
.MaxViewports
);
212 /* Verify width & height */
213 if (w
< 0 || h
< 0) {
214 _mesa_error(ctx
, GL_INVALID_VALUE
,
215 "%s: index (%d) width or height < 0 (%f, %f)",
216 function
, index
, w
, h
);
220 _mesa_set_viewport(ctx
, index
, x
, y
, w
, h
);
224 _mesa_ViewportIndexedf(GLuint index
, GLfloat x
, GLfloat y
,
225 GLfloat w
, GLfloat h
)
227 ViewportIndexedf(index
, x
, y
, w
, h
, "glViewportIndexedf");
231 _mesa_ViewportIndexedfv(GLuint index
, const GLfloat
*v
)
233 ViewportIndexedf(index
, v
[0], v
[1], v
[2], v
[3], "glViewportIndexedfv");
237 set_depth_range_no_notify(struct gl_context
*ctx
, unsigned idx
,
238 GLclampd nearval
, GLclampd farval
)
240 if (ctx
->ViewportArray
[idx
].Near
== nearval
&&
241 ctx
->ViewportArray
[idx
].Far
== farval
)
244 FLUSH_VERTICES(ctx
, _NEW_VIEWPORT
);
246 ctx
->ViewportArray
[idx
].Near
= CLAMP(nearval
, 0.0, 1.0);
247 ctx
->ViewportArray
[idx
].Far
= CLAMP(farval
, 0.0, 1.0);
251 _mesa_set_depth_range(struct gl_context
*ctx
, unsigned idx
,
252 GLclampd nearval
, GLclampd farval
)
254 set_depth_range_no_notify(ctx
, idx
, nearval
, farval
);
256 if (ctx
->Driver
.DepthRange
)
257 ctx
->Driver
.DepthRange(ctx
);
261 * Called by glDepthRange
263 * \param nearval specifies the Z buffer value which should correspond to
264 * the near clip plane
265 * \param farval specifies the Z buffer value which should correspond to
269 _mesa_DepthRange(GLclampd nearval
, GLclampd farval
)
272 GET_CURRENT_CONTEXT(ctx
);
274 FLUSH_VERTICES(ctx
, 0);
276 if (MESA_VERBOSE
&VERBOSE_API
)
277 _mesa_debug(ctx
, "glDepthRange %f %f\n", nearval
, farval
);
279 /* The GL_ARB_viewport_array spec says:
281 * "DepthRange sets the depth range for all viewports to the same
282 * values and is equivalent (assuming no errors are generated) to:
284 * for (uint i = 0; i < MAX_VIEWPORTS; i++)
285 * DepthRangeIndexed(i, n, f);"
287 * Set the depth range for all of the viewports supported by the
288 * implementation, but only signal the driver once at the end.
290 for (i
= 0; i
< ctx
->Const
.MaxViewports
; i
++)
291 set_depth_range_no_notify(ctx
, i
, nearval
, farval
);
293 if (ctx
->Driver
.DepthRange
) {
294 ctx
->Driver
.DepthRange(ctx
);
299 _mesa_DepthRangef(GLclampf nearval
, GLclampf farval
)
301 _mesa_DepthRange(nearval
, farval
);
305 * Update a range DepthRange values
307 * \param first starting array index
308 * \param count count of DepthRange items to update
309 * \param v pointer to memory containing
310 * GLclampd near and far clip-plane values
313 _mesa_DepthRangeArrayv(GLuint first
, GLsizei count
, const GLclampd
*v
)
316 const struct gl_depthrange_inputs
*const p
=
317 (struct gl_depthrange_inputs
*) v
;
318 GET_CURRENT_CONTEXT(ctx
);
320 if (MESA_VERBOSE
& VERBOSE_API
)
321 _mesa_debug(ctx
, "glDepthRangeArrayv %d %d\n", first
, count
);
323 if ((first
+ count
) > ctx
->Const
.MaxViewports
) {
324 _mesa_error(ctx
, GL_INVALID_VALUE
,
325 "glDepthRangev: first (%d) + count (%d) >= MaxViewports (%d)",
326 first
, count
, ctx
->Const
.MaxViewports
);
330 for (i
= 0; i
< count
; i
++)
331 set_depth_range_no_notify(ctx
, i
+ first
, p
[i
].Near
, p
[i
].Far
);
333 if (ctx
->Driver
.DepthRange
)
334 ctx
->Driver
.DepthRange(ctx
);
338 _mesa_DepthRangeArrayfvOES(GLuint first
, GLsizei count
, const GLfloat
*v
)
341 GET_CURRENT_CONTEXT(ctx
);
343 if (MESA_VERBOSE
& VERBOSE_API
)
344 _mesa_debug(ctx
, "glDepthRangeArrayfv %d %d\n", first
, count
);
346 if ((first
+ count
) > ctx
->Const
.MaxViewports
) {
347 _mesa_error(ctx
, GL_INVALID_VALUE
,
348 "glDepthRangeArrayfv: first (%d) + count (%d) >= MaxViewports (%d)",
349 first
, count
, ctx
->Const
.MaxViewports
);
353 for (i
= 0; i
< count
; i
++)
354 set_depth_range_no_notify(ctx
, i
+ first
, v
[i
* 2], v
[i
* 2 + 1]);
356 if (ctx
->Driver
.DepthRange
)
357 ctx
->Driver
.DepthRange(ctx
);
361 * Update a single DepthRange
363 * \param index array index to update
364 * \param nearval specifies the Z buffer value which should correspond to
365 * the near clip plane
366 * \param farval specifies the Z buffer value which should correspond to
370 _mesa_DepthRangeIndexed(GLuint index
, GLclampd nearval
, GLclampd farval
)
372 GET_CURRENT_CONTEXT(ctx
);
374 if (MESA_VERBOSE
& VERBOSE_API
)
375 _mesa_debug(ctx
, "glDepthRangeIndexed(%d, %f, %f)\n",
376 index
, nearval
, farval
);
378 if (index
>= ctx
->Const
.MaxViewports
) {
379 _mesa_error(ctx
, GL_INVALID_VALUE
,
380 "glDepthRangeIndexed: index (%d) >= MaxViewports (%d)",
381 index
, ctx
->Const
.MaxViewports
);
385 _mesa_set_depth_range(ctx
, index
, nearval
, farval
);
389 _mesa_DepthRangeIndexedfOES(GLuint index
, GLfloat nearval
, GLfloat farval
)
391 _mesa_DepthRangeIndexed(index
, nearval
, farval
);
395 * Initialize the context viewport attribute group.
396 * \param ctx the GL context.
398 void _mesa_init_viewport(struct gl_context
*ctx
)
402 ctx
->Transform
.ClipOrigin
= GL_LOWER_LEFT
;
403 ctx
->Transform
.ClipDepthMode
= GL_NEGATIVE_ONE_TO_ONE
;
405 /* Note: ctx->Const.MaxViewports may not have been set by the driver yet,
406 * so just initialize all of them.
408 for (i
= 0; i
< MAX_VIEWPORTS
; i
++) {
410 ctx
->ViewportArray
[i
].X
= 0;
411 ctx
->ViewportArray
[i
].Y
= 0;
412 ctx
->ViewportArray
[i
].Width
= 0;
413 ctx
->ViewportArray
[i
].Height
= 0;
414 ctx
->ViewportArray
[i
].Near
= 0.0;
415 ctx
->ViewportArray
[i
].Far
= 1.0;
420 extern void GLAPIENTRY
421 _mesa_ClipControl(GLenum origin
, GLenum depth
)
423 GET_CURRENT_CONTEXT(ctx
);
425 if (MESA_VERBOSE
&VERBOSE_API
)
426 _mesa_debug(ctx
, "glClipControl(%s, %s)\n",
427 _mesa_enum_to_string(origin
),
428 _mesa_enum_to_string(depth
));
430 ASSERT_OUTSIDE_BEGIN_END(ctx
);
432 if (!ctx
->Extensions
.ARB_clip_control
) {
433 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glClipControl");
437 if (origin
!= GL_LOWER_LEFT
&& origin
!= GL_UPPER_LEFT
) {
438 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClipControl");
442 if (depth
!= GL_NEGATIVE_ONE_TO_ONE
&& depth
!= GL_ZERO_TO_ONE
) {
443 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClipControl");
447 if (ctx
->Transform
.ClipOrigin
== origin
&&
448 ctx
->Transform
.ClipDepthMode
== depth
)
451 /* Affects transform state and the viewport transform */
452 FLUSH_VERTICES(ctx
, _NEW_TRANSFORM
| _NEW_VIEWPORT
);
454 if (ctx
->Transform
.ClipOrigin
!= origin
) {
455 ctx
->Transform
.ClipOrigin
= origin
;
457 /* Affects the winding order of the front face. */
458 ctx
->NewState
|= _NEW_POLYGON
;
460 if (ctx
->Driver
.FrontFace
)
461 ctx
->Driver
.FrontFace(ctx
, ctx
->Polygon
.FrontFace
);
464 if (ctx
->Transform
.ClipDepthMode
!= depth
) {
465 ctx
->Transform
.ClipDepthMode
= depth
;
467 if (ctx
->Driver
.DepthRange
)
468 ctx
->Driver
.DepthRange(ctx
);
473 * Computes the scaling and the translation part of the
474 * viewport transform matrix of the \param i-th viewport
475 * and writes that into \param scale and \param translate.
478 _mesa_get_viewport_xform(struct gl_context
*ctx
, unsigned i
,
479 float scale
[3], float translate
[3])
481 float x
= ctx
->ViewportArray
[i
].X
;
482 float y
= ctx
->ViewportArray
[i
].Y
;
483 float half_width
= 0.5f
* ctx
->ViewportArray
[i
].Width
;
484 float half_height
= 0.5f
* ctx
->ViewportArray
[i
].Height
;
485 double n
= ctx
->ViewportArray
[i
].Near
;
486 double f
= ctx
->ViewportArray
[i
].Far
;
488 scale
[0] = half_width
;
489 translate
[0] = half_width
+ x
;
490 if (ctx
->Transform
.ClipOrigin
== GL_UPPER_LEFT
) {
491 scale
[1] = -half_height
;
493 scale
[1] = half_height
;
495 translate
[1] = half_height
+ y
;
497 if (ctx
->Transform
.ClipDepthMode
== GL_NEGATIVE_ONE_TO_ONE
) {
498 scale
[2] = 0.5 * (f
- n
);
499 translate
[2] = 0.5 * (n
+ f
);