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