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.
38 set_viewport_no_notify(struct gl_context
*ctx
, unsigned idx
, GLint x
, GLint y
,
39 GLsizei width
, GLsizei height
)
41 /* clamp width and height to the implementation dependent range */
42 width
= MIN2(width
, (GLsizei
) ctx
->Const
.MaxViewportWidth
);
43 height
= MIN2(height
, (GLsizei
) ctx
->Const
.MaxViewportHeight
);
45 ctx
->ViewportArray
[idx
].X
= x
;
46 ctx
->ViewportArray
[idx
].Width
= width
;
47 ctx
->ViewportArray
[idx
].Y
= y
;
48 ctx
->ViewportArray
[idx
].Height
= height
;
49 ctx
->NewState
|= _NEW_VIEWPORT
;
52 /* XXX remove this someday. Currently the DRI drivers rely on
53 * the WindowMap matrix being up to date in the driver's Viewport
54 * and DepthRange functions.
56 _math_matrix_viewport(&ctx
->ViewportArray
[idx
]._WindowMap
,
57 ctx
->ViewportArray
[idx
].X
,
58 ctx
->ViewportArray
[idx
].Y
,
59 ctx
->ViewportArray
[idx
].Width
,
60 ctx
->ViewportArray
[idx
].Height
,
61 ctx
->ViewportArray
[idx
].Near
,
62 ctx
->ViewportArray
[idx
].Far
,
63 ctx
->DrawBuffer
->_DepthMaxF
);
69 * \sa Called via glViewport() or display list execution.
71 * Flushes the vertices and calls _mesa_set_viewport() with the given
75 _mesa_Viewport(GLint x
, GLint y
, GLsizei width
, GLsizei height
)
78 GET_CURRENT_CONTEXT(ctx
);
79 FLUSH_VERTICES(ctx
, 0);
81 if (MESA_VERBOSE
& VERBOSE_API
)
82 _mesa_debug(ctx
, "glViewport %d %d %d %d\n", x
, y
, width
, height
);
84 if (width
< 0 || height
< 0) {
85 _mesa_error(ctx
, GL_INVALID_VALUE
,
86 "glViewport(%d, %d, %d, %d)", x
, y
, width
, height
);
90 /* The GL_ARB_viewport_array spec says:
92 * "Viewport sets the parameters for all viewports to the same values
93 * and is equivalent (assuming no errors are generated) to:
95 * for (uint i = 0; i < MAX_VIEWPORTS; i++)
96 * ViewportIndexedf(i, 1, (float)x, (float)y, (float)w, (float)h);"
98 * Set all of the viewports supported by the implementation, but only
99 * signal the driver once at the end.
101 for (i
= 0; i
< ctx
->Const
.MaxViewports
; i
++)
102 set_viewport_no_notify(ctx
, i
, x
, y
, width
, height
);
104 if (ctx
->Driver
.Viewport
) {
105 /* Many drivers will use this call to check for window size changes
106 * and reallocate the z/stencil/accum/etc buffers if needed.
108 ctx
->Driver
.Viewport(ctx
);
114 * Set new viewport parameters and update derived state (the _WindowMap
115 * matrix). Usually called from _mesa_Viewport().
117 * \param ctx GL context.
118 * \param idx Index of the viewport to be updated.
119 * \param x, y coordinates of the lower left corner of the viewport rectangle.
120 * \param width width of the viewport rectangle.
121 * \param height height of the viewport rectangle.
124 _mesa_set_viewport(struct gl_context
*ctx
, unsigned idx
, GLint x
, GLint y
,
125 GLsizei width
, GLsizei height
)
127 set_viewport_no_notify(ctx
, idx
, x
, y
, width
, height
);
129 if (ctx
->Driver
.Viewport
) {
130 /* Many drivers will use this call to check for window size changes
131 * and reallocate the z/stencil/accum/etc buffers if needed.
133 ctx
->Driver
.Viewport(ctx
);
138 set_depth_range_no_notify(struct gl_context
*ctx
, unsigned idx
,
139 GLclampd nearval
, GLclampd farval
)
141 if (ctx
->ViewportArray
[idx
].Near
== nearval
&&
142 ctx
->ViewportArray
[idx
].Far
== farval
)
145 ctx
->ViewportArray
[idx
].Near
= CLAMP(nearval
, 0.0, 1.0);
146 ctx
->ViewportArray
[idx
].Far
= CLAMP(farval
, 0.0, 1.0);
147 ctx
->NewState
|= _NEW_VIEWPORT
;
150 /* XXX remove this someday. Currently the DRI drivers rely on
151 * the WindowMap matrix being up to date in the driver's Viewport
152 * and DepthRange functions.
154 _math_matrix_viewport(&ctx
->ViewportArray
[idx
]._WindowMap
,
155 ctx
->ViewportArray
[idx
].X
,
156 ctx
->ViewportArray
[idx
].Y
,
157 ctx
->ViewportArray
[idx
].Width
,
158 ctx
->ViewportArray
[idx
].Height
,
159 ctx
->ViewportArray
[idx
].Near
,
160 ctx
->ViewportArray
[idx
].Far
,
161 ctx
->DrawBuffer
->_DepthMaxF
);
166 _mesa_set_depth_range(struct gl_context
*ctx
, unsigned idx
,
167 GLclampd nearval
, GLclampd farval
)
169 set_depth_range_no_notify(ctx
, idx
, nearval
, farval
);
171 if (ctx
->Driver
.DepthRange
)
172 ctx
->Driver
.DepthRange(ctx
);
176 * Called by glDepthRange
178 * \param nearval specifies the Z buffer value which should correspond to
179 * the near clip plane
180 * \param farval specifies the Z buffer value which should correspond to
184 _mesa_DepthRange(GLclampd nearval
, GLclampd farval
)
187 GET_CURRENT_CONTEXT(ctx
);
189 FLUSH_VERTICES(ctx
, 0);
191 if (MESA_VERBOSE
&VERBOSE_API
)
192 _mesa_debug(ctx
, "glDepthRange %f %f\n", nearval
, farval
);
194 /* The GL_ARB_viewport_array spec says:
196 * "DepthRange sets the depth range for all viewports to the same
197 * values and is equivalent (assuming no errors are generated) to:
199 * for (uint i = 0; i < MAX_VIEWPORTS; i++)
200 * DepthRangeIndexed(i, n, f);"
202 * Set the depth range for all of the viewports supported by the
203 * implementation, but only signal the driver once at the end.
205 for (i
= 0; i
< ctx
->Const
.MaxViewports
; i
++)
206 set_depth_range_no_notify(ctx
, i
, nearval
, farval
);
208 if (ctx
->Driver
.DepthRange
) {
209 ctx
->Driver
.DepthRange(ctx
);
214 _mesa_DepthRangef(GLclampf nearval
, GLclampf farval
)
216 _mesa_DepthRange(nearval
, farval
);
220 * Initialize the context viewport attribute group.
221 * \param ctx the GL context.
223 void _mesa_init_viewport(struct gl_context
*ctx
)
225 GLfloat depthMax
= 65535.0F
; /* sorf of arbitrary */
228 /* Note: ctx->Const.MaxViewports may not have been set by the driver yet,
229 * so just initialize all of them.
231 for (i
= 0; i
< MAX_VIEWPORTS
; i
++) {
233 ctx
->ViewportArray
[i
].X
= 0;
234 ctx
->ViewportArray
[i
].Y
= 0;
235 ctx
->ViewportArray
[i
].Width
= 0;
236 ctx
->ViewportArray
[i
].Height
= 0;
237 ctx
->ViewportArray
[i
].Near
= 0.0;
238 ctx
->ViewportArray
[i
].Far
= 1.0;
239 _math_matrix_ctr(&ctx
->ViewportArray
[i
]._WindowMap
);
241 _math_matrix_viewport(&ctx
->ViewportArray
[i
]._WindowMap
, 0, 0, 0, 0,
242 0.0F
, 1.0F
, depthMax
);
248 * Free the context viewport attribute group data.
249 * \param ctx the GL context.
251 void _mesa_free_viewport_data(struct gl_context
*ctx
)
255 for (i
= 0; i
< MAX_VIEWPORTS
; i
++)
256 _math_matrix_dtr(&ctx
->ViewportArray
[i
]._WindowMap
);