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
) {
57 ctx
->Const
.ViewportBounds
.Min
, ctx
->Const
.ViewportBounds
.Max
);
59 ctx
->Const
.ViewportBounds
.Min
, ctx
->Const
.ViewportBounds
.Max
);
62 if (ctx
->ViewportArray
[idx
].X
== x
&&
63 ctx
->ViewportArray
[idx
].Width
== width
&&
64 ctx
->ViewportArray
[idx
].Y
== y
&&
65 ctx
->ViewportArray
[idx
].Height
== height
)
68 ctx
->ViewportArray
[idx
].X
= x
;
69 ctx
->ViewportArray
[idx
].Width
= width
;
70 ctx
->ViewportArray
[idx
].Y
= y
;
71 ctx
->ViewportArray
[idx
].Height
= height
;
72 ctx
->NewState
|= _NEW_VIEWPORT
;
75 struct gl_viewport_inputs
{
76 GLfloat X
, Y
; /**< position */
77 GLfloat Width
, Height
; /**< size */
80 struct gl_depthrange_inputs
{
81 GLdouble Near
, Far
; /**< Depth buffer range */
86 * \sa Called via glViewport() or display list execution.
88 * Flushes the vertices and calls _mesa_set_viewport() with the given
92 _mesa_Viewport(GLint x
, GLint y
, GLsizei width
, GLsizei height
)
95 GET_CURRENT_CONTEXT(ctx
);
96 FLUSH_VERTICES(ctx
, 0);
98 if (MESA_VERBOSE
& VERBOSE_API
)
99 _mesa_debug(ctx
, "glViewport %d %d %d %d\n", x
, y
, width
, height
);
101 if (width
< 0 || height
< 0) {
102 _mesa_error(ctx
, GL_INVALID_VALUE
,
103 "glViewport(%d, %d, %d, %d)", x
, y
, width
, height
);
107 /* The GL_ARB_viewport_array spec says:
109 * "Viewport sets the parameters for all viewports to the same values
110 * and is equivalent (assuming no errors are generated) to:
112 * for (uint i = 0; i < MAX_VIEWPORTS; i++)
113 * ViewportIndexedf(i, 1, (float)x, (float)y, (float)w, (float)h);"
115 * Set all of the viewports supported by the implementation, but only
116 * signal the driver once at the end.
118 for (i
= 0; i
< ctx
->Const
.MaxViewports
; i
++)
119 set_viewport_no_notify(ctx
, i
, x
, y
, width
, height
);
121 if (ctx
->Driver
.Viewport
) {
122 /* Many drivers will use this call to check for window size changes
123 * and reallocate the z/stencil/accum/etc buffers if needed.
125 ctx
->Driver
.Viewport(ctx
);
131 * Set new viewport parameters and update derived state.
132 * Usually called from _mesa_Viewport().
134 * \param ctx GL context.
135 * \param idx Index of the viewport to be updated.
136 * \param x, y coordinates of the lower left corner of the viewport rectangle.
137 * \param width width of the viewport rectangle.
138 * \param height height of the viewport rectangle.
141 _mesa_set_viewport(struct gl_context
*ctx
, unsigned idx
, GLfloat x
, GLfloat y
,
142 GLfloat width
, GLfloat height
)
144 set_viewport_no_notify(ctx
, idx
, x
, y
, width
, height
);
146 if (ctx
->Driver
.Viewport
) {
147 /* Many drivers will use this call to check for window size changes
148 * and reallocate the z/stencil/accum/etc buffers if needed.
150 ctx
->Driver
.Viewport(ctx
);
155 _mesa_ViewportArrayv(GLuint first
, GLsizei count
, const GLfloat
*v
)
158 const struct gl_viewport_inputs
*const p
= (struct gl_viewport_inputs
*) v
;
159 GET_CURRENT_CONTEXT(ctx
);
161 if (MESA_VERBOSE
& VERBOSE_API
)
162 _mesa_debug(ctx
, "glViewportArrayv %d %d\n", first
, count
);
164 if ((first
+ count
) > ctx
->Const
.MaxViewports
) {
165 _mesa_error(ctx
, GL_INVALID_VALUE
,
166 "glViewportArrayv: first (%d) + count (%d) > MaxViewports "
168 first
, count
, ctx
->Const
.MaxViewports
);
172 /* Verify width & height */
173 for (i
= 0; i
< count
; i
++) {
174 if (p
[i
].Width
< 0 || p
[i
].Height
< 0) {
175 _mesa_error(ctx
, GL_INVALID_VALUE
,
176 "glViewportArrayv: index (%d) width or height < 0 "
178 i
+ first
, p
[i
].Width
, p
[i
].Height
);
183 for (i
= 0; i
< count
; i
++)
184 set_viewport_no_notify(ctx
, i
+ first
,
186 p
[i
].Width
, p
[i
].Height
);
188 if (ctx
->Driver
.Viewport
)
189 ctx
->Driver
.Viewport(ctx
);
193 ViewportIndexedf(GLuint index
, GLfloat x
, GLfloat y
,
194 GLfloat w
, GLfloat h
, const char *function
)
196 GET_CURRENT_CONTEXT(ctx
);
198 if (MESA_VERBOSE
& VERBOSE_API
)
199 _mesa_debug(ctx
, "%s(%d, %f, %f, %f, %f)\n",
200 function
, index
, x
, y
, w
, h
);
202 if (index
>= ctx
->Const
.MaxViewports
) {
203 _mesa_error(ctx
, GL_INVALID_VALUE
,
204 "%s: index (%d) >= MaxViewports (%d)",
205 function
, index
, ctx
->Const
.MaxViewports
);
209 /* Verify width & height */
210 if (w
< 0 || h
< 0) {
211 _mesa_error(ctx
, GL_INVALID_VALUE
,
212 "%s: index (%d) width or height < 0 (%f, %f)",
213 function
, index
, w
, h
);
217 _mesa_set_viewport(ctx
, index
, x
, y
, w
, h
);
221 _mesa_ViewportIndexedf(GLuint index
, GLfloat x
, GLfloat y
,
222 GLfloat w
, GLfloat h
)
224 ViewportIndexedf(index
, x
, y
, w
, h
, "glViewportIndexedf");
228 _mesa_ViewportIndexedfv(GLuint index
, const GLfloat
*v
)
230 ViewportIndexedf(index
, v
[0], v
[1], v
[2], v
[3], "glViewportIndexedfv");
234 set_depth_range_no_notify(struct gl_context
*ctx
, unsigned idx
,
235 GLclampd nearval
, GLclampd farval
)
237 if (ctx
->ViewportArray
[idx
].Near
== nearval
&&
238 ctx
->ViewportArray
[idx
].Far
== farval
)
241 ctx
->ViewportArray
[idx
].Near
= CLAMP(nearval
, 0.0, 1.0);
242 ctx
->ViewportArray
[idx
].Far
= CLAMP(farval
, 0.0, 1.0);
243 ctx
->NewState
|= _NEW_VIEWPORT
;
247 _mesa_set_depth_range(struct gl_context
*ctx
, unsigned idx
,
248 GLclampd nearval
, GLclampd farval
)
250 set_depth_range_no_notify(ctx
, idx
, nearval
, farval
);
252 if (ctx
->Driver
.DepthRange
)
253 ctx
->Driver
.DepthRange(ctx
);
257 * Called by glDepthRange
259 * \param nearval specifies the Z buffer value which should correspond to
260 * the near clip plane
261 * \param farval specifies the Z buffer value which should correspond to
265 _mesa_DepthRange(GLclampd nearval
, GLclampd farval
)
268 GET_CURRENT_CONTEXT(ctx
);
270 FLUSH_VERTICES(ctx
, 0);
272 if (MESA_VERBOSE
&VERBOSE_API
)
273 _mesa_debug(ctx
, "glDepthRange %f %f\n", nearval
, farval
);
275 /* The GL_ARB_viewport_array spec says:
277 * "DepthRange sets the depth range for all viewports to the same
278 * values and is equivalent (assuming no errors are generated) to:
280 * for (uint i = 0; i < MAX_VIEWPORTS; i++)
281 * DepthRangeIndexed(i, n, f);"
283 * Set the depth range for all of the viewports supported by the
284 * implementation, but only signal the driver once at the end.
286 for (i
= 0; i
< ctx
->Const
.MaxViewports
; i
++)
287 set_depth_range_no_notify(ctx
, i
, nearval
, farval
);
289 if (ctx
->Driver
.DepthRange
) {
290 ctx
->Driver
.DepthRange(ctx
);
295 _mesa_DepthRangef(GLclampf nearval
, GLclampf farval
)
297 _mesa_DepthRange(nearval
, farval
);
301 * Update a range DepthRange values
303 * \param first starting array index
304 * \param count count of DepthRange items to update
305 * \param v pointer to memory containing
306 * GLclampd near and far clip-plane values
309 _mesa_DepthRangeArrayv(GLuint first
, GLsizei count
, const GLclampd
*v
)
312 const struct gl_depthrange_inputs
*const p
=
313 (struct gl_depthrange_inputs
*) v
;
314 GET_CURRENT_CONTEXT(ctx
);
316 if (MESA_VERBOSE
& VERBOSE_API
)
317 _mesa_debug(ctx
, "glDepthRangeArrayv %d %d\n", first
, count
);
319 if ((first
+ count
) > ctx
->Const
.MaxViewports
) {
320 _mesa_error(ctx
, GL_INVALID_VALUE
,
321 "glDepthRangev: first (%d) + count (%d) >= MaxViewports (%d)",
322 first
, count
, ctx
->Const
.MaxViewports
);
326 for (i
= 0; i
< count
; i
++)
327 set_depth_range_no_notify(ctx
, i
+ first
, p
[i
].Near
, p
[i
].Far
);
329 if (ctx
->Driver
.DepthRange
)
330 ctx
->Driver
.DepthRange(ctx
);
334 * Update a single DepthRange
336 * \param index array index to update
337 * \param nearval specifies the Z buffer value which should correspond to
338 * the near clip plane
339 * \param farval specifies the Z buffer value which should correspond to
343 _mesa_DepthRangeIndexed(GLuint index
, GLclampd nearval
, GLclampd farval
)
345 GET_CURRENT_CONTEXT(ctx
);
347 if (MESA_VERBOSE
& VERBOSE_API
)
348 _mesa_debug(ctx
, "glDepthRangeIndexed(%d, %f, %f)\n",
349 index
, nearval
, farval
);
351 if (index
>= ctx
->Const
.MaxViewports
) {
352 _mesa_error(ctx
, GL_INVALID_VALUE
,
353 "glDepthRangeIndexed: index (%d) >= MaxViewports (%d)",
354 index
, ctx
->Const
.MaxViewports
);
358 _mesa_set_depth_range(ctx
, index
, nearval
, farval
);
362 * Initialize the context viewport attribute group.
363 * \param ctx the GL context.
365 void _mesa_init_viewport(struct gl_context
*ctx
)
369 ctx
->Transform
.ClipOrigin
= GL_LOWER_LEFT
;
370 ctx
->Transform
.ClipDepthMode
= GL_NEGATIVE_ONE_TO_ONE
;
372 /* Note: ctx->Const.MaxViewports may not have been set by the driver yet,
373 * so just initialize all of them.
375 for (i
= 0; i
< MAX_VIEWPORTS
; i
++) {
377 ctx
->ViewportArray
[i
].X
= 0;
378 ctx
->ViewportArray
[i
].Y
= 0;
379 ctx
->ViewportArray
[i
].Width
= 0;
380 ctx
->ViewportArray
[i
].Height
= 0;
381 ctx
->ViewportArray
[i
].Near
= 0.0;
382 ctx
->ViewportArray
[i
].Far
= 1.0;
387 extern void GLAPIENTRY
388 _mesa_ClipControl(GLenum origin
, GLenum depth
)
390 GET_CURRENT_CONTEXT(ctx
);
392 if (MESA_VERBOSE
&VERBOSE_API
)
393 _mesa_debug(ctx
, "glClipControl(%s, %s)\n",
394 _mesa_enum_to_string(origin
),
395 _mesa_enum_to_string(depth
));
397 ASSERT_OUTSIDE_BEGIN_END(ctx
);
399 if (!ctx
->Extensions
.ARB_clip_control
) {
400 _mesa_error(ctx
, GL_INVALID_OPERATION
, "glClipControl");
404 if (origin
!= GL_LOWER_LEFT
&& origin
!= GL_UPPER_LEFT
) {
405 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClipControl");
409 if (depth
!= GL_NEGATIVE_ONE_TO_ONE
&& depth
!= GL_ZERO_TO_ONE
) {
410 _mesa_error(ctx
, GL_INVALID_ENUM
, "glClipControl");
414 if (ctx
->Transform
.ClipOrigin
== origin
&&
415 ctx
->Transform
.ClipDepthMode
== depth
)
418 /* Affects transform state and the viewport transform */
419 FLUSH_VERTICES(ctx
, _NEW_TRANSFORM
| _NEW_VIEWPORT
);
421 if (ctx
->Transform
.ClipOrigin
!= origin
) {
422 ctx
->Transform
.ClipOrigin
= origin
;
424 /* Affects the winding order of the front face. */
425 ctx
->NewState
|= _NEW_POLYGON
;
427 if (ctx
->Driver
.FrontFace
)
428 ctx
->Driver
.FrontFace(ctx
, ctx
->Polygon
.FrontFace
);
431 if (ctx
->Transform
.ClipDepthMode
!= depth
) {
432 ctx
->Transform
.ClipDepthMode
= depth
;
434 if (ctx
->Driver
.DepthRange
)
435 ctx
->Driver
.DepthRange(ctx
);
440 * Computes the scaling and the translation part of the
441 * viewport transform matrix of the \param i-th viewport
442 * and writes that into \param scale and \param translate.
445 _mesa_get_viewport_xform(struct gl_context
*ctx
, unsigned i
,
446 float scale
[3], float translate
[3])
448 float x
= ctx
->ViewportArray
[i
].X
;
449 float y
= ctx
->ViewportArray
[i
].Y
;
450 float half_width
= 0.5f
* ctx
->ViewportArray
[i
].Width
;
451 float half_height
= 0.5f
* ctx
->ViewportArray
[i
].Height
;
452 double n
= ctx
->ViewportArray
[i
].Near
;
453 double f
= ctx
->ViewportArray
[i
].Far
;
455 scale
[0] = half_width
;
456 translate
[0] = half_width
+ x
;
457 if (ctx
->Transform
.ClipOrigin
== GL_UPPER_LEFT
) {
458 scale
[1] = -half_height
;
460 scale
[1] = half_height
;
462 translate
[1] = half_height
+ y
;
464 if (ctx
->Transform
.ClipDepthMode
== GL_NEGATIVE_ONE_TO_ONE
) {
465 scale
[2] = 0.5 * (f
- n
);
466 translate
[2] = 0.5 * (n
+ f
);