Merge branch 'vbo_clean'
[mesa.git] / src / mesa / main / viewport.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.5
4 *
5 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR 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 "macros.h"
34 #include "viewport.h"
35
36
37 /**
38 * Set the viewport.
39 * \sa Called via glViewport() or display list execution.
40 *
41 * Flushes the vertices and calls _mesa_set_viewport() with the given
42 * parameters.
43 */
44 void GLAPIENTRY
45 _mesa_Viewport(GLint x, GLint y, GLsizei width, GLsizei height)
46 {
47 GET_CURRENT_CONTEXT(ctx);
48 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
49 _mesa_set_viewport(ctx, x, y, width, height);
50 }
51
52
53 /**
54 * Set new viewport parameters and update derived state (the _WindowMap
55 * matrix). Usually called from _mesa_Viewport().
56 *
57 * \param ctx GL context.
58 * \param x, y coordinates of the lower left corner of the viewport rectangle.
59 * \param width width of the viewport rectangle.
60 * \param height height of the viewport rectangle.
61 */
62 void
63 _mesa_set_viewport(GLcontext *ctx, GLint x, GLint y,
64 GLsizei width, GLsizei height)
65 {
66 if (MESA_VERBOSE & VERBOSE_API)
67 _mesa_debug(ctx, "glViewport %d %d %d %d\n", x, y, width, height);
68
69 if (width < 0 || height < 0) {
70 _mesa_error(ctx, GL_INVALID_VALUE,
71 "glViewport(%d, %d, %d, %d)", x, y, width, height);
72 return;
73 }
74
75 /* clamp width and height to the implementation dependent range */
76 width = MIN2(width, (GLsizei) ctx->Const.MaxViewportWidth);
77 height = MIN2(height, (GLsizei) ctx->Const.MaxViewportHeight);
78
79 ctx->Viewport.X = x;
80 ctx->Viewport.Width = width;
81 ctx->Viewport.Y = y;
82 ctx->Viewport.Height = height;
83 ctx->NewState |= _NEW_VIEWPORT;
84
85 #if 1
86 /* XXX remove this someday. Currently the DRI drivers rely on
87 * the WindowMap matrix being up to date in the driver's Viewport
88 * and DepthRange functions.
89 */
90 _math_matrix_viewport(&ctx->Viewport._WindowMap,
91 ctx->Viewport.X, ctx->Viewport.Y,
92 ctx->Viewport.Width, ctx->Viewport.Height,
93 ctx->Viewport.Near, ctx->Viewport.Far,
94 ctx->DrawBuffer->_DepthMaxF);
95 #endif
96
97 if (ctx->Driver.Viewport) {
98 /* Many drivers will use this call to check for window size changes
99 * and reallocate the z/stencil/accum/etc buffers if needed.
100 */
101 ctx->Driver.Viewport(ctx, x, y, width, height);
102 }
103 }
104
105
106 /**
107 * Called by glDepthRange
108 *
109 * \param nearval specifies the Z buffer value which should correspond to
110 * the near clip plane
111 * \param farval specifies the Z buffer value which should correspond to
112 * the far clip plane
113 */
114 void GLAPIENTRY
115 _mesa_DepthRange(GLclampd nearval, GLclampd farval)
116 {
117 GET_CURRENT_CONTEXT(ctx);
118 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH(ctx);
119
120 if (MESA_VERBOSE&VERBOSE_API)
121 _mesa_debug(ctx, "glDepthRange %f %f\n", nearval, farval);
122
123 if (ctx->Viewport.Near == nearval &&
124 ctx->Viewport.Far == farval)
125 return;
126
127 ctx->Viewport.Near = (GLfloat) CLAMP(nearval, 0.0, 1.0);
128 ctx->Viewport.Far = (GLfloat) CLAMP(farval, 0.0, 1.0);
129 ctx->NewState |= _NEW_VIEWPORT;
130
131 #if 1
132 /* XXX remove this someday. Currently the DRI drivers rely on
133 * the WindowMap matrix being up to date in the driver's Viewport
134 * and DepthRange functions.
135 */
136 _math_matrix_viewport(&ctx->Viewport._WindowMap,
137 ctx->Viewport.X, ctx->Viewport.Y,
138 ctx->Viewport.Width, ctx->Viewport.Height,
139 ctx->Viewport.Near, ctx->Viewport.Far,
140 ctx->DrawBuffer->_DepthMaxF);
141 #endif
142
143 if (ctx->Driver.DepthRange) {
144 ctx->Driver.DepthRange(ctx, nearval, farval);
145 }
146 }
147
148
149
150 /**
151 * Initialize the context viewport attribute group.
152 * \param ctx the GL context.
153 */
154 void _mesa_init_viewport(GLcontext *ctx)
155 {
156 GLfloat depthMax = 65535.0F; /* sorf of arbitrary */
157
158 /* Viewport group */
159 ctx->Viewport.X = 0;
160 ctx->Viewport.Y = 0;
161 ctx->Viewport.Width = 0;
162 ctx->Viewport.Height = 0;
163 ctx->Viewport.Near = 0.0;
164 ctx->Viewport.Far = 1.0;
165 _math_matrix_ctr(&ctx->Viewport._WindowMap);
166
167 _math_matrix_viewport(&ctx->Viewport._WindowMap, 0, 0, 0, 0,
168 0.0F, 1.0F, depthMax);
169 }
170
171
172 /**
173 * Free the context viewport attribute group data.
174 * \param ctx the GL context.
175 */
176 void _mesa_free_viewport_data(GLcontext *ctx)
177 {
178 _math_matrix_dtr(&ctx->Viewport._WindowMap);
179 }
180