mesa: add new entrypoints for GL_OES_viewport_array
[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
337 }
338
339 /**
340 * Update a single DepthRange
341 *
342 * \param index array index to update
343 * \param nearval specifies the Z buffer value which should correspond to
344 * the near clip plane
345 * \param farval specifies the Z buffer value which should correspond to
346 * the far clip plane
347 */
348 void GLAPIENTRY
349 _mesa_DepthRangeIndexed(GLuint index, GLclampd nearval, GLclampd farval)
350 {
351 GET_CURRENT_CONTEXT(ctx);
352
353 if (MESA_VERBOSE & VERBOSE_API)
354 _mesa_debug(ctx, "glDepthRangeIndexed(%d, %f, %f)\n",
355 index, nearval, farval);
356
357 if (index >= ctx->Const.MaxViewports) {
358 _mesa_error(ctx, GL_INVALID_VALUE,
359 "glDepthRangeIndexed: index (%d) >= MaxViewports (%d)",
360 index, ctx->Const.MaxViewports);
361 return;
362 }
363
364 _mesa_set_depth_range(ctx, index, nearval, farval);
365 }
366
367 void GLAPIENTRY
368 _mesa_DepthRangeIndexedfOES(GLuint index, GLfloat nearval, GLfloat farval)
369 {
370
371 }
372
373 /**
374 * Initialize the context viewport attribute group.
375 * \param ctx the GL context.
376 */
377 void _mesa_init_viewport(struct gl_context *ctx)
378 {
379 unsigned i;
380
381 ctx->Transform.ClipOrigin = GL_LOWER_LEFT;
382 ctx->Transform.ClipDepthMode = GL_NEGATIVE_ONE_TO_ONE;
383
384 /* Note: ctx->Const.MaxViewports may not have been set by the driver yet,
385 * so just initialize all of them.
386 */
387 for (i = 0; i < MAX_VIEWPORTS; i++) {
388 /* Viewport group */
389 ctx->ViewportArray[i].X = 0;
390 ctx->ViewportArray[i].Y = 0;
391 ctx->ViewportArray[i].Width = 0;
392 ctx->ViewportArray[i].Height = 0;
393 ctx->ViewportArray[i].Near = 0.0;
394 ctx->ViewportArray[i].Far = 1.0;
395 }
396 }
397
398
399 extern void GLAPIENTRY
400 _mesa_ClipControl(GLenum origin, GLenum depth)
401 {
402 GET_CURRENT_CONTEXT(ctx);
403
404 if (MESA_VERBOSE&VERBOSE_API)
405 _mesa_debug(ctx, "glClipControl(%s, %s)\n",
406 _mesa_enum_to_string(origin),
407 _mesa_enum_to_string(depth));
408
409 ASSERT_OUTSIDE_BEGIN_END(ctx);
410
411 if (!ctx->Extensions.ARB_clip_control) {
412 _mesa_error(ctx, GL_INVALID_OPERATION, "glClipControl");
413 return;
414 }
415
416 if (origin != GL_LOWER_LEFT && origin != GL_UPPER_LEFT) {
417 _mesa_error(ctx, GL_INVALID_ENUM, "glClipControl");
418 return;
419 }
420
421 if (depth != GL_NEGATIVE_ONE_TO_ONE && depth != GL_ZERO_TO_ONE) {
422 _mesa_error(ctx, GL_INVALID_ENUM, "glClipControl");
423 return;
424 }
425
426 if (ctx->Transform.ClipOrigin == origin &&
427 ctx->Transform.ClipDepthMode == depth)
428 return;
429
430 /* Affects transform state and the viewport transform */
431 FLUSH_VERTICES(ctx, _NEW_TRANSFORM | _NEW_VIEWPORT);
432
433 if (ctx->Transform.ClipOrigin != origin) {
434 ctx->Transform.ClipOrigin = origin;
435
436 /* Affects the winding order of the front face. */
437 ctx->NewState |= _NEW_POLYGON;
438
439 if (ctx->Driver.FrontFace)
440 ctx->Driver.FrontFace(ctx, ctx->Polygon.FrontFace);
441 }
442
443 if (ctx->Transform.ClipDepthMode != depth) {
444 ctx->Transform.ClipDepthMode = depth;
445
446 if (ctx->Driver.DepthRange)
447 ctx->Driver.DepthRange(ctx);
448 }
449 }
450
451 /**
452 * Computes the scaling and the translation part of the
453 * viewport transform matrix of the \param i-th viewport
454 * and writes that into \param scale and \param translate.
455 */
456 void
457 _mesa_get_viewport_xform(struct gl_context *ctx, unsigned i,
458 float scale[3], float translate[3])
459 {
460 float x = ctx->ViewportArray[i].X;
461 float y = ctx->ViewportArray[i].Y;
462 float half_width = 0.5f * ctx->ViewportArray[i].Width;
463 float half_height = 0.5f * ctx->ViewportArray[i].Height;
464 double n = ctx->ViewportArray[i].Near;
465 double f = ctx->ViewportArray[i].Far;
466
467 scale[0] = half_width;
468 translate[0] = half_width + x;
469 if (ctx->Transform.ClipOrigin == GL_UPPER_LEFT) {
470 scale[1] = -half_height;
471 } else {
472 scale[1] = half_height;
473 }
474 translate[1] = half_height + y;
475
476 if (ctx->Transform.ClipDepthMode == GL_NEGATIVE_ONE_TO_ONE) {
477 scale[2] = 0.5 * (f - n);
478 translate[2] = 0.5 * (n + f);
479 } else {
480 scale[2] = f - n;
481 translate[2] = n;
482 }
483 }