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