6d3e57654ccf0a72c60fc1c962d0e478a6de1808
[mesa.git] / src / mesa / main / viewport.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
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.
23 */
24
25
26 /**
27 * \file viewport.c
28 * glViewport and glDepthRange functions.
29 */
30
31
32 #include "context.h"
33 #include "enums.h"
34 #include "macros.h"
35 #include "mtypes.h"
36 #include "viewport.h"
37
38 static void
39 set_viewport_no_notify(struct gl_context *ctx, unsigned idx,
40 GLfloat x, GLfloat y,
41 GLfloat width, GLfloat height)
42 {
43 FLUSH_VERTICES(ctx, _NEW_VIEWPORT);
44
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);
48
49 /* The GL_ARB_viewport_array spec says:
50 *
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)."
56 */
57 if (ctx->Extensions.ARB_viewport_array ||
58 (ctx->Extensions.OES_viewport_array &&
59 _mesa_is_gles31(ctx))) {
60 x = CLAMP(x,
61 ctx->Const.ViewportBounds.Min, ctx->Const.ViewportBounds.Max);
62 y = CLAMP(y,
63 ctx->Const.ViewportBounds.Min, ctx->Const.ViewportBounds.Max);
64 }
65
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)
70 return;
71
72 ctx->ViewportArray[idx].X = x;
73 ctx->ViewportArray[idx].Width = width;
74 ctx->ViewportArray[idx].Y = y;
75 ctx->ViewportArray[idx].Height = height;
76 }
77
78 struct gl_viewport_inputs {
79 GLfloat X, Y; /**< position */
80 GLfloat Width, Height; /**< size */
81 };
82
83 struct gl_depthrange_inputs {
84 GLdouble Near, Far; /**< Depth buffer range */
85 };
86
87 /**
88 * Set the viewport.
89 * \sa Called via glViewport() or display list execution.
90 *
91 * Flushes the vertices and calls _mesa_set_viewport() with the given
92 * parameters.
93 */
94 void GLAPIENTRY
95 _mesa_Viewport(GLint x, GLint y, GLsizei width, GLsizei height)
96 {
97 unsigned i;
98 GET_CURRENT_CONTEXT(ctx);
99 FLUSH_VERTICES(ctx, 0);
100
101 if (MESA_VERBOSE & VERBOSE_API)
102 _mesa_debug(ctx, "glViewport %d %d %d %d\n", x, y, width, height);
103
104 if (width < 0 || height < 0) {
105 _mesa_error(ctx, GL_INVALID_VALUE,
106 "glViewport(%d, %d, %d, %d)", x, y, width, height);
107 return;
108 }
109
110 /* The GL_ARB_viewport_array spec says:
111 *
112 * "Viewport sets the parameters for all viewports to the same values
113 * and is equivalent (assuming no errors are generated) to:
114 *
115 * for (uint i = 0; i < MAX_VIEWPORTS; i++)
116 * ViewportIndexedf(i, 1, (float)x, (float)y, (float)w, (float)h);"
117 *
118 * Set all of the viewports supported by the implementation, but only
119 * signal the driver once at the end.
120 */
121 for (i = 0; i < ctx->Const.MaxViewports; i++)
122 set_viewport_no_notify(ctx, i, x, y, width, height);
123
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.
127 */
128 ctx->Driver.Viewport(ctx);
129 }
130 }
131
132
133 /**
134 * Set new viewport parameters and update derived state.
135 * Usually called from _mesa_Viewport().
136 *
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.
142 */
143 void
144 _mesa_set_viewport(struct gl_context *ctx, unsigned idx, GLfloat x, GLfloat y,
145 GLfloat width, GLfloat height)
146 {
147 set_viewport_no_notify(ctx, idx, x, y, width, height);
148
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.
152 */
153 ctx->Driver.Viewport(ctx);
154 }
155 }
156
157 void GLAPIENTRY
158 _mesa_ViewportArrayv(GLuint first, GLsizei count, const GLfloat *v)
159 {
160 int i;
161 const struct gl_viewport_inputs *const p = (struct gl_viewport_inputs *) v;
162 GET_CURRENT_CONTEXT(ctx);
163
164 if (MESA_VERBOSE & VERBOSE_API)
165 _mesa_debug(ctx, "glViewportArrayv %d %d\n", first, count);
166
167 if ((first + count) > ctx->Const.MaxViewports) {
168 _mesa_error(ctx, GL_INVALID_VALUE,
169 "glViewportArrayv: first (%d) + count (%d) > MaxViewports "
170 "(%d)",
171 first, count, ctx->Const.MaxViewports);
172 return;
173 }
174
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 "
180 "(%f, %f)",
181 i + first, p[i].Width, p[i].Height);
182 return;
183 }
184 }
185
186 for (i = 0; i < count; i++)
187 set_viewport_no_notify(ctx, i + first,
188 p[i].X, p[i].Y,
189 p[i].Width, p[i].Height);
190
191 if (ctx->Driver.Viewport)
192 ctx->Driver.Viewport(ctx);
193 }
194
195 static void
196 ViewportIndexedf(GLuint index, GLfloat x, GLfloat y,
197 GLfloat w, GLfloat h, const char *function)
198 {
199 GET_CURRENT_CONTEXT(ctx);
200
201 if (MESA_VERBOSE & VERBOSE_API)
202 _mesa_debug(ctx, "%s(%d, %f, %f, %f, %f)\n",
203 function, index, x, y, w, h);
204
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);
209 return;
210 }
211
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);
217 return;
218 }
219
220 _mesa_set_viewport(ctx, index, x, y, w, h);
221 }
222
223 void GLAPIENTRY
224 _mesa_ViewportIndexedf(GLuint index, GLfloat x, GLfloat y,
225 GLfloat w, GLfloat h)
226 {
227 ViewportIndexedf(index, x, y, w, h, "glViewportIndexedf");
228 }
229
230 void GLAPIENTRY
231 _mesa_ViewportIndexedfv(GLuint index, const GLfloat *v)
232 {
233 ViewportIndexedf(index, v[0], v[1], v[2], v[3], "glViewportIndexedfv");
234 }
235
236 static void
237 set_depth_range_no_notify(struct gl_context *ctx, unsigned idx,
238 GLclampd nearval, GLclampd farval)
239 {
240 if (ctx->ViewportArray[idx].Near == nearval &&
241 ctx->ViewportArray[idx].Far == farval)
242 return;
243
244 FLUSH_VERTICES(ctx, _NEW_VIEWPORT);
245
246 ctx->ViewportArray[idx].Near = CLAMP(nearval, 0.0, 1.0);
247 ctx->ViewportArray[idx].Far = CLAMP(farval, 0.0, 1.0);
248 }
249
250 void
251 _mesa_set_depth_range(struct gl_context *ctx, unsigned idx,
252 GLclampd nearval, GLclampd farval)
253 {
254 set_depth_range_no_notify(ctx, idx, nearval, farval);
255
256 if (ctx->Driver.DepthRange)
257 ctx->Driver.DepthRange(ctx);
258 }
259
260 /**
261 * Called by glDepthRange
262 *
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
266 * the far clip plane
267 */
268 void GLAPIENTRY
269 _mesa_DepthRange(GLclampd nearval, GLclampd farval)
270 {
271 unsigned i;
272 GET_CURRENT_CONTEXT(ctx);
273
274 FLUSH_VERTICES(ctx, 0);
275
276 if (MESA_VERBOSE&VERBOSE_API)
277 _mesa_debug(ctx, "glDepthRange %f %f\n", nearval, farval);
278
279 /* The GL_ARB_viewport_array spec says:
280 *
281 * "DepthRange sets the depth range for all viewports to the same
282 * values and is equivalent (assuming no errors are generated) to:
283 *
284 * for (uint i = 0; i < MAX_VIEWPORTS; i++)
285 * DepthRangeIndexed(i, n, f);"
286 *
287 * Set the depth range for all of the viewports supported by the
288 * implementation, but only signal the driver once at the end.
289 */
290 for (i = 0; i < ctx->Const.MaxViewports; i++)
291 set_depth_range_no_notify(ctx, i, nearval, farval);
292
293 if (ctx->Driver.DepthRange) {
294 ctx->Driver.DepthRange(ctx);
295 }
296 }
297
298 void GLAPIENTRY
299 _mesa_DepthRangef(GLclampf nearval, GLclampf farval)
300 {
301 _mesa_DepthRange(nearval, farval);
302 }
303
304 /**
305 * Update a range DepthRange values
306 *
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
311 */
312 void GLAPIENTRY
313 _mesa_DepthRangeArrayv(GLuint first, GLsizei count, const GLclampd *v)
314 {
315 int i;
316 const struct gl_depthrange_inputs *const p =
317 (struct gl_depthrange_inputs *) v;
318 GET_CURRENT_CONTEXT(ctx);
319
320 if (MESA_VERBOSE & VERBOSE_API)
321 _mesa_debug(ctx, "glDepthRangeArrayv %d %d\n", first, count);
322
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);
327 return;
328 }
329
330 for (i = 0; i < count; i++)
331 set_depth_range_no_notify(ctx, i + first, p[i].Near, p[i].Far);
332
333 if (ctx->Driver.DepthRange)
334 ctx->Driver.DepthRange(ctx);
335 }
336
337 void GLAPIENTRY
338 _mesa_DepthRangeArrayfvOES(GLuint first, GLsizei count, const GLfloat *v)
339 {
340 int i;
341 GET_CURRENT_CONTEXT(ctx);
342
343 if (MESA_VERBOSE & VERBOSE_API)
344 _mesa_debug(ctx, "glDepthRangeArrayfv %d %d\n", first, count);
345
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);
350 return;
351 }
352
353 for (i = 0; i < count; i++)
354 set_depth_range_no_notify(ctx, i + first, v[i * 2], v[i * 2 + 1]);
355
356 if (ctx->Driver.DepthRange)
357 ctx->Driver.DepthRange(ctx);
358 }
359
360 /**
361 * Update a single DepthRange
362 *
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
367 * the far clip plane
368 */
369 void GLAPIENTRY
370 _mesa_DepthRangeIndexed(GLuint index, GLclampd nearval, GLclampd farval)
371 {
372 GET_CURRENT_CONTEXT(ctx);
373
374 if (MESA_VERBOSE & VERBOSE_API)
375 _mesa_debug(ctx, "glDepthRangeIndexed(%d, %f, %f)\n",
376 index, nearval, farval);
377
378 if (index >= ctx->Const.MaxViewports) {
379 _mesa_error(ctx, GL_INVALID_VALUE,
380 "glDepthRangeIndexed: index (%d) >= MaxViewports (%d)",
381 index, ctx->Const.MaxViewports);
382 return;
383 }
384
385 _mesa_set_depth_range(ctx, index, nearval, farval);
386 }
387
388 void GLAPIENTRY
389 _mesa_DepthRangeIndexedfOES(GLuint index, GLfloat nearval, GLfloat farval)
390 {
391 _mesa_DepthRangeIndexed(index, nearval, farval);
392 }
393
394 /**
395 * Initialize the context viewport attribute group.
396 * \param ctx the GL context.
397 */
398 void _mesa_init_viewport(struct gl_context *ctx)
399 {
400 unsigned i;
401
402 ctx->Transform.ClipOrigin = GL_LOWER_LEFT;
403 ctx->Transform.ClipDepthMode = GL_NEGATIVE_ONE_TO_ONE;
404
405 /* Note: ctx->Const.MaxViewports may not have been set by the driver yet,
406 * so just initialize all of them.
407 */
408 for (i = 0; i < MAX_VIEWPORTS; i++) {
409 /* Viewport group */
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;
416 }
417 }
418
419
420 extern void GLAPIENTRY
421 _mesa_ClipControl(GLenum origin, GLenum depth)
422 {
423 GET_CURRENT_CONTEXT(ctx);
424
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));
429
430 ASSERT_OUTSIDE_BEGIN_END(ctx);
431
432 if (!ctx->Extensions.ARB_clip_control) {
433 _mesa_error(ctx, GL_INVALID_OPERATION, "glClipControl");
434 return;
435 }
436
437 if (origin != GL_LOWER_LEFT && origin != GL_UPPER_LEFT) {
438 _mesa_error(ctx, GL_INVALID_ENUM, "glClipControl");
439 return;
440 }
441
442 if (depth != GL_NEGATIVE_ONE_TO_ONE && depth != GL_ZERO_TO_ONE) {
443 _mesa_error(ctx, GL_INVALID_ENUM, "glClipControl");
444 return;
445 }
446
447 if (ctx->Transform.ClipOrigin == origin &&
448 ctx->Transform.ClipDepthMode == depth)
449 return;
450
451 /* Affects transform state and the viewport transform */
452 FLUSH_VERTICES(ctx, _NEW_TRANSFORM | _NEW_VIEWPORT);
453
454 if (ctx->Transform.ClipOrigin != origin) {
455 ctx->Transform.ClipOrigin = origin;
456
457 /* Affects the winding order of the front face. */
458 ctx->NewState |= _NEW_POLYGON;
459
460 if (ctx->Driver.FrontFace)
461 ctx->Driver.FrontFace(ctx, ctx->Polygon.FrontFace);
462 }
463
464 if (ctx->Transform.ClipDepthMode != depth) {
465 ctx->Transform.ClipDepthMode = depth;
466
467 if (ctx->Driver.DepthRange)
468 ctx->Driver.DepthRange(ctx);
469 }
470 }
471
472 /**
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.
476 */
477 void
478 _mesa_get_viewport_xform(struct gl_context *ctx, unsigned i,
479 float scale[3], float translate[3])
480 {
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;
487
488 scale[0] = half_width;
489 translate[0] = half_width + x;
490 if (ctx->Transform.ClipOrigin == GL_UPPER_LEFT) {
491 scale[1] = -half_height;
492 } else {
493 scale[1] = half_height;
494 }
495 translate[1] = half_height + y;
496
497 if (ctx->Transform.ClipDepthMode == GL_NEGATIVE_ONE_TO_ONE) {
498 scale[2] = 0.5 * (f - n);
499 translate[2] = 0.5 * (n + f);
500 } else {
501 scale[2] = f - n;
502 translate[2] = n;
503 }
504 }