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