mesa: Initialize all the viewports
[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 "macros.h"
34 #include "mtypes.h"
35 #include "viewport.h"
36
37 static void
38 set_viewport_no_notify(struct gl_context *ctx, unsigned idx, GLint x, GLint y,
39 GLsizei width, GLsizei height)
40 {
41 /* clamp width and height to the implementation dependent range */
42 width = MIN2(width, (GLsizei) ctx->Const.MaxViewportWidth);
43 height = MIN2(height, (GLsizei) ctx->Const.MaxViewportHeight);
44
45 ctx->ViewportArray[idx].X = x;
46 ctx->ViewportArray[idx].Width = width;
47 ctx->ViewportArray[idx].Y = y;
48 ctx->ViewportArray[idx].Height = height;
49 ctx->NewState |= _NEW_VIEWPORT;
50
51 #if 1
52 /* XXX remove this someday. Currently the DRI drivers rely on
53 * the WindowMap matrix being up to date in the driver's Viewport
54 * and DepthRange functions.
55 */
56 _math_matrix_viewport(&ctx->ViewportArray[idx]._WindowMap,
57 ctx->ViewportArray[idx].X,
58 ctx->ViewportArray[idx].Y,
59 ctx->ViewportArray[idx].Width,
60 ctx->ViewportArray[idx].Height,
61 ctx->ViewportArray[idx].Near,
62 ctx->ViewportArray[idx].Far,
63 ctx->DrawBuffer->_DepthMaxF);
64 #endif
65 }
66
67 /**
68 * Set the viewport.
69 * \sa Called via glViewport() or display list execution.
70 *
71 * Flushes the vertices and calls _mesa_set_viewport() with the given
72 * parameters.
73 */
74 void GLAPIENTRY
75 _mesa_Viewport(GLint x, GLint y, GLsizei width, GLsizei height)
76 {
77 GET_CURRENT_CONTEXT(ctx);
78 FLUSH_VERTICES(ctx, 0);
79
80 if (MESA_VERBOSE & VERBOSE_API)
81 _mesa_debug(ctx, "glViewport %d %d %d %d\n", x, y, width, height);
82
83 if (width < 0 || height < 0) {
84 _mesa_error(ctx, GL_INVALID_VALUE,
85 "glViewport(%d, %d, %d, %d)", x, y, width, height);
86 return;
87 }
88
89 set_viewport_no_notify(ctx, 0, x, y, width, height);
90
91 if (ctx->Driver.Viewport) {
92 /* Many drivers will use this call to check for window size changes
93 * and reallocate the z/stencil/accum/etc buffers if needed.
94 */
95 ctx->Driver.Viewport(ctx);
96 }
97 }
98
99
100 /**
101 * Set new viewport parameters and update derived state (the _WindowMap
102 * matrix). Usually called from _mesa_Viewport().
103 *
104 * \param ctx GL context.
105 * \param idx Index of the viewport to be updated.
106 * \param x, y coordinates of the lower left corner of the viewport rectangle.
107 * \param width width of the viewport rectangle.
108 * \param height height of the viewport rectangle.
109 */
110 void
111 _mesa_set_viewport(struct gl_context *ctx, unsigned idx, GLint x, GLint y,
112 GLsizei width, GLsizei height)
113 {
114 set_viewport_no_notify(ctx, idx, x, y, width, height);
115
116 if (ctx->Driver.Viewport) {
117 /* Many drivers will use this call to check for window size changes
118 * and reallocate the z/stencil/accum/etc buffers if needed.
119 */
120 ctx->Driver.Viewport(ctx);
121 }
122 }
123
124 static void
125 set_depth_range_no_notify(struct gl_context *ctx, unsigned idx,
126 GLclampd nearval, GLclampd farval)
127 {
128 if (ctx->ViewportArray[idx].Near == nearval &&
129 ctx->ViewportArray[idx].Far == farval)
130 return;
131
132 ctx->ViewportArray[idx].Near = CLAMP(nearval, 0.0, 1.0);
133 ctx->ViewportArray[idx].Far = CLAMP(farval, 0.0, 1.0);
134 ctx->NewState |= _NEW_VIEWPORT;
135
136 #if 1
137 /* XXX remove this someday. Currently the DRI drivers rely on
138 * the WindowMap matrix being up to date in the driver's Viewport
139 * and DepthRange functions.
140 */
141 _math_matrix_viewport(&ctx->ViewportArray[idx]._WindowMap,
142 ctx->ViewportArray[idx].X,
143 ctx->ViewportArray[idx].Y,
144 ctx->ViewportArray[idx].Width,
145 ctx->ViewportArray[idx].Height,
146 ctx->ViewportArray[idx].Near,
147 ctx->ViewportArray[idx].Far,
148 ctx->DrawBuffer->_DepthMaxF);
149 #endif
150 }
151
152 void
153 _mesa_set_depth_range(struct gl_context *ctx, unsigned idx,
154 GLclampd nearval, GLclampd farval)
155 {
156 set_depth_range_no_notify(ctx, idx, nearval, farval);
157
158 if (ctx->Driver.DepthRange)
159 ctx->Driver.DepthRange(ctx);
160 }
161
162 /**
163 * Called by glDepthRange
164 *
165 * \param nearval specifies the Z buffer value which should correspond to
166 * the near clip plane
167 * \param farval specifies the Z buffer value which should correspond to
168 * the far clip plane
169 */
170 void GLAPIENTRY
171 _mesa_DepthRange(GLclampd nearval, GLclampd farval)
172 {
173 GET_CURRENT_CONTEXT(ctx);
174
175 FLUSH_VERTICES(ctx, 0);
176
177 if (MESA_VERBOSE&VERBOSE_API)
178 _mesa_debug(ctx, "glDepthRange %f %f\n", nearval, farval);
179
180 set_depth_range_no_notify(ctx, 0, nearval, farval);
181
182 if (ctx->Driver.DepthRange) {
183 ctx->Driver.DepthRange(ctx);
184 }
185 }
186
187 void GLAPIENTRY
188 _mesa_DepthRangef(GLclampf nearval, GLclampf farval)
189 {
190 _mesa_DepthRange(nearval, farval);
191 }
192
193 /**
194 * Initialize the context viewport attribute group.
195 * \param ctx the GL context.
196 */
197 void _mesa_init_viewport(struct gl_context *ctx)
198 {
199 GLfloat depthMax = 65535.0F; /* sorf of arbitrary */
200 unsigned i;
201
202 /* Note: ctx->Const.MaxViewports may not have been set by the driver yet,
203 * so just initialize all of them.
204 */
205 for (i = 0; i < MAX_VIEWPORTS; i++) {
206 /* Viewport group */
207 ctx->ViewportArray[i].X = 0;
208 ctx->ViewportArray[i].Y = 0;
209 ctx->ViewportArray[i].Width = 0;
210 ctx->ViewportArray[i].Height = 0;
211 ctx->ViewportArray[i].Near = 0.0;
212 ctx->ViewportArray[i].Far = 1.0;
213 _math_matrix_ctr(&ctx->ViewportArray[i]._WindowMap);
214
215 _math_matrix_viewport(&ctx->ViewportArray[i]._WindowMap, 0, 0, 0, 0,
216 0.0F, 1.0F, depthMax);
217 }
218 }
219
220
221 /**
222 * Free the context viewport attribute group data.
223 * \param ctx the GL context.
224 */
225 void _mesa_free_viewport_data(struct gl_context *ctx)
226 {
227 unsigned i;
228
229 for (i = 0; i < MAX_VIEWPORTS; i++)
230 _math_matrix_dtr(&ctx->ViewportArray[i]._WindowMap);
231 }
232