mesa: add viewport_array() helper
[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, ctx->DriverFlags.NewViewport ? 0 : _NEW_VIEWPORT);
44 ctx->NewDriverState |= ctx->DriverFlags.NewViewport;
45
46 /* clamp width and height to the implementation dependent range */
47 width = MIN2(width, (GLfloat) ctx->Const.MaxViewportWidth);
48 height = MIN2(height, (GLfloat) ctx->Const.MaxViewportHeight);
49
50 /* The GL_ARB_viewport_array spec says:
51 *
52 * "The location of the viewport's bottom-left corner, given by (x,y),
53 * are clamped to be within the implementation-dependent viewport
54 * bounds range. The viewport bounds range [min, max] tuple may be
55 * determined by calling GetFloatv with the symbolic constant
56 * VIEWPORT_BOUNDS_RANGE (see section 6.1)."
57 */
58 if (ctx->Extensions.ARB_viewport_array ||
59 (ctx->Extensions.OES_viewport_array &&
60 _mesa_is_gles31(ctx))) {
61 x = CLAMP(x,
62 ctx->Const.ViewportBounds.Min, ctx->Const.ViewportBounds.Max);
63 y = CLAMP(y,
64 ctx->Const.ViewportBounds.Min, ctx->Const.ViewportBounds.Max);
65 }
66
67 if (ctx->ViewportArray[idx].X == x &&
68 ctx->ViewportArray[idx].Width == width &&
69 ctx->ViewportArray[idx].Y == y &&
70 ctx->ViewportArray[idx].Height == height)
71 return;
72
73 ctx->ViewportArray[idx].X = x;
74 ctx->ViewportArray[idx].Width = width;
75 ctx->ViewportArray[idx].Y = y;
76 ctx->ViewportArray[idx].Height = height;
77 }
78
79 struct gl_viewport_inputs {
80 GLfloat X, Y; /**< position */
81 GLfloat Width, Height; /**< size */
82 };
83
84 struct gl_depthrange_inputs {
85 GLdouble Near, Far; /**< Depth buffer range */
86 };
87
88 /**
89 * Set the viewport.
90 * \sa Called via glViewport() or display list execution.
91 *
92 * Flushes the vertices and calls _mesa_set_viewport() with the given
93 * parameters.
94 */
95 void GLAPIENTRY
96 _mesa_Viewport(GLint x, GLint y, GLsizei width, GLsizei height)
97 {
98 unsigned i;
99 GET_CURRENT_CONTEXT(ctx);
100 FLUSH_VERTICES(ctx, 0);
101
102 if (MESA_VERBOSE & VERBOSE_API)
103 _mesa_debug(ctx, "glViewport %d %d %d %d\n", x, y, width, height);
104
105 if (width < 0 || height < 0) {
106 _mesa_error(ctx, GL_INVALID_VALUE,
107 "glViewport(%d, %d, %d, %d)", x, y, width, height);
108 return;
109 }
110
111 /* The GL_ARB_viewport_array spec says:
112 *
113 * "Viewport sets the parameters for all viewports to the same values
114 * and is equivalent (assuming no errors are generated) to:
115 *
116 * for (uint i = 0; i < MAX_VIEWPORTS; i++)
117 * ViewportIndexedf(i, 1, (float)x, (float)y, (float)w, (float)h);"
118 *
119 * Set all of the viewports supported by the implementation, but only
120 * signal the driver once at the end.
121 */
122 for (i = 0; i < ctx->Const.MaxViewports; i++)
123 set_viewport_no_notify(ctx, i, x, y, width, height);
124
125 if (ctx->Driver.Viewport) {
126 /* Many drivers will use this call to check for window size changes
127 * and reallocate the z/stencil/accum/etc buffers if needed.
128 */
129 ctx->Driver.Viewport(ctx);
130 }
131 }
132
133
134 /**
135 * Set new viewport parameters and update derived state.
136 * Usually called from _mesa_Viewport().
137 *
138 * \param ctx GL context.
139 * \param idx Index of the viewport to be updated.
140 * \param x, y coordinates of the lower left corner of the viewport rectangle.
141 * \param width width of the viewport rectangle.
142 * \param height height of the viewport rectangle.
143 */
144 void
145 _mesa_set_viewport(struct gl_context *ctx, unsigned idx, GLfloat x, GLfloat y,
146 GLfloat width, GLfloat height)
147 {
148 set_viewport_no_notify(ctx, idx, x, y, width, height);
149
150 if (ctx->Driver.Viewport) {
151 /* Many drivers will use this call to check for window size changes
152 * and reallocate the z/stencil/accum/etc buffers if needed.
153 */
154 ctx->Driver.Viewport(ctx);
155 }
156 }
157
158 static void
159 viewport_array(struct gl_context *ctx, GLuint first, GLsizei count,
160 const struct gl_viewport_inputs *inputs)
161 {
162 for (GLsizei i = 0; i < count; i++) {
163 set_viewport_no_notify(ctx, i + first, inputs[i].X, inputs[i].Y,
164 inputs[i].Width, inputs[i].Height);
165 }
166
167 if (ctx->Driver.Viewport)
168 ctx->Driver.Viewport(ctx);
169 }
170
171 void GLAPIENTRY
172 _mesa_ViewportArrayv(GLuint first, GLsizei count, const GLfloat *v)
173 {
174 int i;
175 const struct gl_viewport_inputs *const p = (struct gl_viewport_inputs *) v;
176 GET_CURRENT_CONTEXT(ctx);
177
178 if (MESA_VERBOSE & VERBOSE_API)
179 _mesa_debug(ctx, "glViewportArrayv %d %d\n", first, count);
180
181 if ((first + count) > ctx->Const.MaxViewports) {
182 _mesa_error(ctx, GL_INVALID_VALUE,
183 "glViewportArrayv: first (%d) + count (%d) > MaxViewports "
184 "(%d)",
185 first, count, ctx->Const.MaxViewports);
186 return;
187 }
188
189 /* Verify width & height */
190 for (i = 0; i < count; i++) {
191 if (p[i].Width < 0 || p[i].Height < 0) {
192 _mesa_error(ctx, GL_INVALID_VALUE,
193 "glViewportArrayv: index (%d) width or height < 0 "
194 "(%f, %f)",
195 i + first, p[i].Width, p[i].Height);
196 return;
197 }
198 }
199
200 viewport_array(ctx, first, count, p);
201 }
202
203 static void
204 viewport_indexed_err(struct gl_context *ctx, GLuint index, GLfloat x, GLfloat y,
205 GLfloat w, GLfloat h, const char *function)
206 {
207 if (MESA_VERBOSE & VERBOSE_API)
208 _mesa_debug(ctx, "%s(%d, %f, %f, %f, %f)\n",
209 function, index, x, y, w, h);
210
211 if (index >= ctx->Const.MaxViewports) {
212 _mesa_error(ctx, GL_INVALID_VALUE,
213 "%s: index (%d) >= MaxViewports (%d)",
214 function, index, ctx->Const.MaxViewports);
215 return;
216 }
217
218 /* Verify width & height */
219 if (w < 0 || h < 0) {
220 _mesa_error(ctx, GL_INVALID_VALUE,
221 "%s: index (%d) width or height < 0 (%f, %f)",
222 function, index, w, h);
223 return;
224 }
225
226 _mesa_set_viewport(ctx, index, x, y, w, h);
227 }
228
229 void GLAPIENTRY
230 _mesa_ViewportIndexedf_no_error(GLuint index, GLfloat x, GLfloat y,
231 GLfloat w, GLfloat h)
232 {
233 GET_CURRENT_CONTEXT(ctx);
234 _mesa_set_viewport(ctx, index, x, y, w, h);
235 }
236
237 void GLAPIENTRY
238 _mesa_ViewportIndexedf(GLuint index, GLfloat x, GLfloat y,
239 GLfloat w, GLfloat h)
240 {
241 GET_CURRENT_CONTEXT(ctx);
242 viewport_indexed_err(ctx, index, x, y, w, h, "glViewportIndexedf");
243 }
244
245 void GLAPIENTRY
246 _mesa_ViewportIndexedfv_no_error(GLuint index, const GLfloat *v)
247 {
248 GET_CURRENT_CONTEXT(ctx);
249 _mesa_set_viewport(ctx, index, v[0], v[1], v[2], v[3]);
250 }
251
252 void GLAPIENTRY
253 _mesa_ViewportIndexedfv(GLuint index, const GLfloat *v)
254 {
255 GET_CURRENT_CONTEXT(ctx);
256 viewport_indexed_err(ctx, index, v[0], v[1], v[2], v[3],
257 "glViewportIndexedfv");
258 }
259
260 static void
261 set_depth_range_no_notify(struct gl_context *ctx, unsigned idx,
262 GLclampd nearval, GLclampd farval)
263 {
264 if (ctx->ViewportArray[idx].Near == nearval &&
265 ctx->ViewportArray[idx].Far == farval)
266 return;
267
268 /* The depth range is needed by program state constants. */
269 FLUSH_VERTICES(ctx, _NEW_VIEWPORT);
270 ctx->NewDriverState |= ctx->DriverFlags.NewViewport;
271
272 ctx->ViewportArray[idx].Near = CLAMP(nearval, 0.0, 1.0);
273 ctx->ViewportArray[idx].Far = CLAMP(farval, 0.0, 1.0);
274 }
275
276 void
277 _mesa_set_depth_range(struct gl_context *ctx, unsigned idx,
278 GLclampd nearval, GLclampd farval)
279 {
280 set_depth_range_no_notify(ctx, idx, nearval, farval);
281
282 if (ctx->Driver.DepthRange)
283 ctx->Driver.DepthRange(ctx);
284 }
285
286 /**
287 * Called by glDepthRange
288 *
289 * \param nearval specifies the Z buffer value which should correspond to
290 * the near clip plane
291 * \param farval specifies the Z buffer value which should correspond to
292 * the far clip plane
293 */
294 void GLAPIENTRY
295 _mesa_DepthRange(GLclampd nearval, GLclampd farval)
296 {
297 unsigned i;
298 GET_CURRENT_CONTEXT(ctx);
299
300 FLUSH_VERTICES(ctx, 0);
301
302 if (MESA_VERBOSE&VERBOSE_API)
303 _mesa_debug(ctx, "glDepthRange %f %f\n", nearval, farval);
304
305 /* The GL_ARB_viewport_array spec says:
306 *
307 * "DepthRange sets the depth range for all viewports to the same
308 * values and is equivalent (assuming no errors are generated) to:
309 *
310 * for (uint i = 0; i < MAX_VIEWPORTS; i++)
311 * DepthRangeIndexed(i, n, f);"
312 *
313 * Set the depth range for all of the viewports supported by the
314 * implementation, but only signal the driver once at the end.
315 */
316 for (i = 0; i < ctx->Const.MaxViewports; i++)
317 set_depth_range_no_notify(ctx, i, nearval, farval);
318
319 if (ctx->Driver.DepthRange) {
320 ctx->Driver.DepthRange(ctx);
321 }
322 }
323
324 void GLAPIENTRY
325 _mesa_DepthRangef(GLclampf nearval, GLclampf farval)
326 {
327 _mesa_DepthRange(nearval, farval);
328 }
329
330 /**
331 * Update a range DepthRange values
332 *
333 * \param first starting array index
334 * \param count count of DepthRange items to update
335 * \param v pointer to memory containing
336 * GLclampd near and far clip-plane values
337 */
338 void GLAPIENTRY
339 _mesa_DepthRangeArrayv(GLuint first, GLsizei count, const GLclampd *v)
340 {
341 int i;
342 const struct gl_depthrange_inputs *const p =
343 (struct gl_depthrange_inputs *) v;
344 GET_CURRENT_CONTEXT(ctx);
345
346 if (MESA_VERBOSE & VERBOSE_API)
347 _mesa_debug(ctx, "glDepthRangeArrayv %d %d\n", first, count);
348
349 if ((first + count) > ctx->Const.MaxViewports) {
350 _mesa_error(ctx, GL_INVALID_VALUE,
351 "glDepthRangev: first (%d) + count (%d) >= MaxViewports (%d)",
352 first, count, ctx->Const.MaxViewports);
353 return;
354 }
355
356 for (i = 0; i < count; i++)
357 set_depth_range_no_notify(ctx, i + first, p[i].Near, p[i].Far);
358
359 if (ctx->Driver.DepthRange)
360 ctx->Driver.DepthRange(ctx);
361 }
362
363 void GLAPIENTRY
364 _mesa_DepthRangeArrayfvOES(GLuint first, GLsizei count, const GLfloat *v)
365 {
366 int i;
367 GET_CURRENT_CONTEXT(ctx);
368
369 if (MESA_VERBOSE & VERBOSE_API)
370 _mesa_debug(ctx, "glDepthRangeArrayfv %d %d\n", first, count);
371
372 if ((first + count) > ctx->Const.MaxViewports) {
373 _mesa_error(ctx, GL_INVALID_VALUE,
374 "glDepthRangeArrayfv: first (%d) + count (%d) >= MaxViewports (%d)",
375 first, count, ctx->Const.MaxViewports);
376 return;
377 }
378
379 for (i = 0; i < count; i++)
380 set_depth_range_no_notify(ctx, i + first, v[i * 2], v[i * 2 + 1]);
381
382 if (ctx->Driver.DepthRange)
383 ctx->Driver.DepthRange(ctx);
384 }
385
386 /**
387 * Update a single DepthRange
388 *
389 * \param index array index to update
390 * \param nearval specifies the Z buffer value which should correspond to
391 * the near clip plane
392 * \param farval specifies the Z buffer value which should correspond to
393 * the far clip plane
394 */
395 void GLAPIENTRY
396 _mesa_DepthRangeIndexed(GLuint index, GLclampd nearval, GLclampd farval)
397 {
398 GET_CURRENT_CONTEXT(ctx);
399
400 if (MESA_VERBOSE & VERBOSE_API)
401 _mesa_debug(ctx, "glDepthRangeIndexed(%d, %f, %f)\n",
402 index, nearval, farval);
403
404 if (index >= ctx->Const.MaxViewports) {
405 _mesa_error(ctx, GL_INVALID_VALUE,
406 "glDepthRangeIndexed: index (%d) >= MaxViewports (%d)",
407 index, ctx->Const.MaxViewports);
408 return;
409 }
410
411 _mesa_set_depth_range(ctx, index, nearval, farval);
412 }
413
414 void GLAPIENTRY
415 _mesa_DepthRangeIndexedfOES(GLuint index, GLfloat nearval, GLfloat farval)
416 {
417 _mesa_DepthRangeIndexed(index, nearval, farval);
418 }
419
420 /**
421 * Initialize the context viewport attribute group.
422 * \param ctx the GL context.
423 */
424 void _mesa_init_viewport(struct gl_context *ctx)
425 {
426 unsigned i;
427
428 ctx->Transform.ClipOrigin = GL_LOWER_LEFT;
429 ctx->Transform.ClipDepthMode = GL_NEGATIVE_ONE_TO_ONE;
430
431 /* Note: ctx->Const.MaxViewports may not have been set by the driver yet,
432 * so just initialize all of them.
433 */
434 for (i = 0; i < MAX_VIEWPORTS; i++) {
435 /* Viewport group */
436 ctx->ViewportArray[i].X = 0;
437 ctx->ViewportArray[i].Y = 0;
438 ctx->ViewportArray[i].Width = 0;
439 ctx->ViewportArray[i].Height = 0;
440 ctx->ViewportArray[i].Near = 0.0;
441 ctx->ViewportArray[i].Far = 1.0;
442 }
443 }
444
445
446 static void
447 clip_control(struct gl_context *ctx, GLenum origin, GLenum depth)
448 {
449 if (ctx->Transform.ClipOrigin == origin &&
450 ctx->Transform.ClipDepthMode == depth)
451 return;
452
453 /* Affects transform state and the viewport transform */
454 FLUSH_VERTICES(ctx, ctx->DriverFlags.NewClipControl ? 0 :
455 _NEW_TRANSFORM | _NEW_VIEWPORT);
456 ctx->NewDriverState |= ctx->DriverFlags.NewClipControl;
457
458 if (ctx->Transform.ClipOrigin != origin) {
459 ctx->Transform.ClipOrigin = origin;
460
461 /* Affects the winding order of the front face. */
462 if (ctx->DriverFlags.NewPolygonState)
463 ctx->NewDriverState |= ctx->DriverFlags.NewPolygonState;
464 else
465 ctx->NewState |= _NEW_POLYGON;
466
467 if (ctx->Driver.FrontFace)
468 ctx->Driver.FrontFace(ctx, ctx->Polygon.FrontFace);
469 }
470
471 if (ctx->Transform.ClipDepthMode != depth) {
472 ctx->Transform.ClipDepthMode = depth;
473
474 if (ctx->Driver.DepthRange)
475 ctx->Driver.DepthRange(ctx);
476 }
477 }
478
479
480 void GLAPIENTRY
481 _mesa_ClipControl_no_error(GLenum origin, GLenum depth)
482 {
483 GET_CURRENT_CONTEXT(ctx);
484 clip_control(ctx, origin, depth);
485 }
486
487
488 void GLAPIENTRY
489 _mesa_ClipControl(GLenum origin, GLenum depth)
490 {
491 GET_CURRENT_CONTEXT(ctx);
492
493 if (MESA_VERBOSE & VERBOSE_API)
494 _mesa_debug(ctx, "glClipControl(%s, %s)\n",
495 _mesa_enum_to_string(origin),
496 _mesa_enum_to_string(depth));
497
498 ASSERT_OUTSIDE_BEGIN_END(ctx);
499
500 if (!ctx->Extensions.ARB_clip_control) {
501 _mesa_error(ctx, GL_INVALID_OPERATION, "glClipControl");
502 return;
503 }
504
505 if (origin != GL_LOWER_LEFT && origin != GL_UPPER_LEFT) {
506 _mesa_error(ctx, GL_INVALID_ENUM, "glClipControl");
507 return;
508 }
509
510 if (depth != GL_NEGATIVE_ONE_TO_ONE && depth != GL_ZERO_TO_ONE) {
511 _mesa_error(ctx, GL_INVALID_ENUM, "glClipControl");
512 return;
513 }
514
515 clip_control(ctx, origin, depth);
516 }
517
518 /**
519 * Computes the scaling and the translation part of the
520 * viewport transform matrix of the \param i-th viewport
521 * and writes that into \param scale and \param translate.
522 */
523 void
524 _mesa_get_viewport_xform(struct gl_context *ctx, unsigned i,
525 float scale[3], float translate[3])
526 {
527 float x = ctx->ViewportArray[i].X;
528 float y = ctx->ViewportArray[i].Y;
529 float half_width = 0.5f * ctx->ViewportArray[i].Width;
530 float half_height = 0.5f * ctx->ViewportArray[i].Height;
531 double n = ctx->ViewportArray[i].Near;
532 double f = ctx->ViewportArray[i].Far;
533
534 scale[0] = half_width;
535 translate[0] = half_width + x;
536 if (ctx->Transform.ClipOrigin == GL_UPPER_LEFT) {
537 scale[1] = -half_height;
538 } else {
539 scale[1] = half_height;
540 }
541 translate[1] = half_height + y;
542
543 if (ctx->Transform.ClipDepthMode == GL_NEGATIVE_ONE_TO_ONE) {
544 scale[2] = 0.5 * (f - n);
545 translate[2] = 0.5 * (n + f);
546 } else {
547 scale[2] = f - n;
548 translate[2] = n;
549 }
550 }