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