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