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