mesa: add copy_texture_sub_image_no_error() helper
[mesa.git] / src / mesa / main / polygon.c
1 /**
2 * \file polygon.c
3 * Polygon operations.
4 */
5
6 /*
7 * Mesa 3-D graphics library
8 *
9 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27 * OTHER DEALINGS IN THE SOFTWARE.
28 */
29
30
31 #include "glheader.h"
32 #include "imports.h"
33 #include "context.h"
34 #include "image.h"
35 #include "enums.h"
36 #include "pack.h"
37 #include "pbo.h"
38 #include "polygon.h"
39 #include "mtypes.h"
40
41
42 /**
43 * Specify whether to cull front- or back-facing facets.
44 *
45 * \param mode culling mode.
46 *
47 * \sa glCullFace().
48 *
49 * Verifies the parameter and updates gl_polygon_attrib::CullFaceMode. On
50 * change, flushes the vertices and notifies the driver via
51 * the dd_function_table::CullFace callback.
52 */
53 void GLAPIENTRY
54 _mesa_CullFace( GLenum mode )
55 {
56 GET_CURRENT_CONTEXT(ctx);
57
58 if (MESA_VERBOSE&VERBOSE_API)
59 _mesa_debug(ctx, "glCullFace %s\n", _mesa_enum_to_string(mode));
60
61 if (mode!=GL_FRONT && mode!=GL_BACK && mode!=GL_FRONT_AND_BACK) {
62 _mesa_error( ctx, GL_INVALID_ENUM, "glCullFace" );
63 return;
64 }
65
66 if (ctx->Polygon.CullFaceMode == mode)
67 return;
68
69 FLUSH_VERTICES(ctx, ctx->DriverFlags.NewPolygonState ? 0 : _NEW_POLYGON);
70 ctx->NewDriverState |= ctx->DriverFlags.NewPolygonState;
71 ctx->Polygon.CullFaceMode = mode;
72
73 if (ctx->Driver.CullFace)
74 ctx->Driver.CullFace( ctx, mode );
75 }
76
77
78 /**
79 * Define front- and back-facing
80 *
81 * \param mode orientation of front-facing polygons.
82 *
83 * \sa glFrontFace().
84 *
85 * Verifies the parameter and updates gl_polygon_attrib::FrontFace. On change
86 * flushes the vertices and notifies the driver via
87 * the dd_function_table::FrontFace callback.
88 */
89 void GLAPIENTRY
90 _mesa_FrontFace( GLenum mode )
91 {
92 GET_CURRENT_CONTEXT(ctx);
93
94 if (MESA_VERBOSE&VERBOSE_API)
95 _mesa_debug(ctx, "glFrontFace %s\n", _mesa_enum_to_string(mode));
96
97 if (ctx->Polygon.FrontFace == mode)
98 return;
99
100 if (mode!=GL_CW && mode!=GL_CCW) {
101 _mesa_error( ctx, GL_INVALID_ENUM, "glFrontFace" );
102 return;
103 }
104
105 FLUSH_VERTICES(ctx, ctx->DriverFlags.NewPolygonState ? 0 : _NEW_POLYGON);
106 ctx->NewDriverState |= ctx->DriverFlags.NewPolygonState;
107 ctx->Polygon.FrontFace = mode;
108
109 if (ctx->Driver.FrontFace)
110 ctx->Driver.FrontFace( ctx, mode );
111 }
112
113
114 /**
115 * Set the polygon rasterization mode.
116 *
117 * \param face the polygons which \p mode applies to.
118 * \param mode how polygons should be rasterized.
119 *
120 * \sa glPolygonMode().
121 *
122 * Verifies the parameters and updates gl_polygon_attrib::FrontMode and
123 * gl_polygon_attrib::BackMode. On change flushes the vertices and notifies the
124 * driver via the dd_function_table::PolygonMode callback.
125 */
126 void GLAPIENTRY
127 _mesa_PolygonMode( GLenum face, GLenum mode )
128 {
129 GET_CURRENT_CONTEXT(ctx);
130
131 if (MESA_VERBOSE&VERBOSE_API)
132 _mesa_debug(ctx, "glPolygonMode %s %s\n",
133 _mesa_enum_to_string(face),
134 _mesa_enum_to_string(mode));
135
136 switch (mode) {
137 case GL_POINT:
138 case GL_LINE:
139 case GL_FILL:
140 break;
141 case GL_FILL_RECTANGLE_NV:
142 if (ctx->Extensions.NV_fill_rectangle)
143 break;
144 /* fall-through */
145 default:
146 _mesa_error(ctx, GL_INVALID_ENUM, "glPolygonMode(mode)");
147 return;
148 }
149
150 switch (face) {
151 case GL_FRONT:
152 if (ctx->API == API_OPENGL_CORE) {
153 _mesa_error( ctx, GL_INVALID_ENUM, "glPolygonMode(face)" );
154 return;
155 }
156 if (ctx->Polygon.FrontMode == mode)
157 return;
158 FLUSH_VERTICES(ctx, ctx->DriverFlags.NewPolygonState ? 0 : _NEW_POLYGON);
159 ctx->NewDriverState |= ctx->DriverFlags.NewPolygonState;
160 ctx->Polygon.FrontMode = mode;
161 break;
162 case GL_FRONT_AND_BACK:
163 if (ctx->Polygon.FrontMode == mode && ctx->Polygon.BackMode == mode)
164 return;
165 FLUSH_VERTICES(ctx, ctx->DriverFlags.NewPolygonState ? 0 : _NEW_POLYGON);
166 ctx->NewDriverState |= ctx->DriverFlags.NewPolygonState;
167 ctx->Polygon.FrontMode = mode;
168 ctx->Polygon.BackMode = mode;
169 break;
170 case GL_BACK:
171 if (ctx->API == API_OPENGL_CORE) {
172 _mesa_error( ctx, GL_INVALID_ENUM, "glPolygonMode(face)" );
173 return;
174 }
175 if (ctx->Polygon.BackMode == mode)
176 return;
177 FLUSH_VERTICES(ctx, ctx->DriverFlags.NewPolygonState ? 0 : _NEW_POLYGON);
178 ctx->NewDriverState |= ctx->DriverFlags.NewPolygonState;
179 ctx->Polygon.BackMode = mode;
180 break;
181 default:
182 _mesa_error( ctx, GL_INVALID_ENUM, "glPolygonMode(face)" );
183 return;
184 }
185
186 if (ctx->Driver.PolygonMode)
187 ctx->Driver.PolygonMode(ctx, face, mode);
188 }
189
190
191 /**
192 * Called by glPolygonStipple.
193 */
194 void GLAPIENTRY
195 _mesa_PolygonStipple(const GLubyte *pattern)
196 {
197 GET_CURRENT_CONTEXT(ctx);
198
199 if (MESA_VERBOSE & VERBOSE_API)
200 _mesa_debug(ctx, "glPolygonStipple\n");
201
202 FLUSH_VERTICES(ctx, ctx->DriverFlags.NewPolygonStipple ? 0 :
203 _NEW_POLYGONSTIPPLE);
204 ctx->NewDriverState |= ctx->DriverFlags.NewPolygonStipple;
205
206 pattern = _mesa_map_validate_pbo_source(ctx, 2,
207 &ctx->Unpack, 32, 32, 1,
208 GL_COLOR_INDEX, GL_BITMAP,
209 INT_MAX, pattern,
210 "glPolygonStipple");
211 if (!pattern)
212 return;
213
214 _mesa_unpack_polygon_stipple(pattern, ctx->PolygonStipple, &ctx->Unpack);
215
216 _mesa_unmap_pbo_source(ctx, &ctx->Unpack);
217
218 if (ctx->Driver.PolygonStipple)
219 ctx->Driver.PolygonStipple(ctx, pattern);
220 }
221
222
223 /**
224 * Called by glPolygonStipple.
225 */
226 void GLAPIENTRY
227 _mesa_GetnPolygonStippleARB( GLsizei bufSize, GLubyte *dest )
228 {
229 GET_CURRENT_CONTEXT(ctx);
230
231 if (MESA_VERBOSE&VERBOSE_API)
232 _mesa_debug(ctx, "glGetPolygonStipple\n");
233
234 dest = _mesa_map_validate_pbo_dest(ctx, 2,
235 &ctx->Pack, 32, 32, 1,
236 GL_COLOR_INDEX, GL_BITMAP,
237 bufSize, dest, "glGetPolygonStipple");
238 if (!dest)
239 return;
240
241 _mesa_pack_polygon_stipple(ctx->PolygonStipple, dest, &ctx->Pack);
242
243 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
244 }
245
246
247 void GLAPIENTRY
248 _mesa_GetPolygonStipple( GLubyte *dest )
249 {
250 _mesa_GetnPolygonStippleARB(INT_MAX, dest);
251 }
252
253 void
254 _mesa_polygon_offset_clamp(struct gl_context *ctx,
255 GLfloat factor, GLfloat units, GLfloat clamp)
256 {
257 if (ctx->Polygon.OffsetFactor == factor &&
258 ctx->Polygon.OffsetUnits == units &&
259 ctx->Polygon.OffsetClamp == clamp)
260 return;
261
262 FLUSH_VERTICES(ctx, ctx->DriverFlags.NewPolygonState ? 0 : _NEW_POLYGON);
263 ctx->NewDriverState |= ctx->DriverFlags.NewPolygonState;
264 ctx->Polygon.OffsetFactor = factor;
265 ctx->Polygon.OffsetUnits = units;
266 ctx->Polygon.OffsetClamp = clamp;
267
268 if (ctx->Driver.PolygonOffset)
269 ctx->Driver.PolygonOffset( ctx, factor, units, clamp );
270 }
271
272 void GLAPIENTRY
273 _mesa_PolygonOffset( GLfloat factor, GLfloat units )
274 {
275 GET_CURRENT_CONTEXT(ctx);
276
277 if (MESA_VERBOSE&VERBOSE_API)
278 _mesa_debug(ctx, "glPolygonOffset %f %f\n", factor, units);
279
280 _mesa_polygon_offset_clamp(ctx, factor, units, 0.0);
281 }
282
283
284 void GLAPIENTRY
285 _mesa_PolygonOffsetEXT( GLfloat factor, GLfloat bias )
286 {
287 GET_CURRENT_CONTEXT(ctx);
288 /* XXX mult by DepthMaxF here??? */
289 _mesa_PolygonOffset(factor, bias * ctx->DrawBuffer->_DepthMaxF );
290 }
291
292 void GLAPIENTRY
293 _mesa_PolygonOffsetClampEXT( GLfloat factor, GLfloat units, GLfloat clamp )
294 {
295 GET_CURRENT_CONTEXT(ctx);
296
297 if (!ctx->Extensions.EXT_polygon_offset_clamp) {
298 _mesa_error(ctx, GL_INVALID_OPERATION,
299 "unsupported function (glPolygonOffsetClampEXT) called");
300 return;
301 }
302
303 if (MESA_VERBOSE&VERBOSE_API)
304 _mesa_debug(ctx, "glPolygonOffsetClampEXT %f %f %f\n", factor, units, clamp);
305
306 _mesa_polygon_offset_clamp(ctx, factor, units, clamp);
307 }
308
309
310
311 /**********************************************************************/
312 /** \name Initialization */
313 /*@{*/
314
315 /**
316 * Initialize the context polygon state.
317 *
318 * \param ctx GL context.
319 *
320 * Initializes __struct gl_contextRec::Polygon and __struct gl_contextRec::PolygonStipple
321 * attribute groups.
322 */
323 void _mesa_init_polygon( struct gl_context * ctx )
324 {
325 /* Polygon group */
326 ctx->Polygon.CullFlag = GL_FALSE;
327 ctx->Polygon.CullFaceMode = GL_BACK;
328 ctx->Polygon.FrontFace = GL_CCW;
329 ctx->Polygon.FrontMode = GL_FILL;
330 ctx->Polygon.BackMode = GL_FILL;
331 ctx->Polygon.SmoothFlag = GL_FALSE;
332 ctx->Polygon.StippleFlag = GL_FALSE;
333 ctx->Polygon.OffsetFactor = 0.0F;
334 ctx->Polygon.OffsetUnits = 0.0F;
335 ctx->Polygon.OffsetClamp = 0.0F;
336 ctx->Polygon.OffsetPoint = GL_FALSE;
337 ctx->Polygon.OffsetLine = GL_FALSE;
338 ctx->Polygon.OffsetFill = GL_FALSE;
339
340
341 /* Polygon Stipple group */
342 memset( ctx->PolygonStipple, 0xff, 32*sizeof(GLuint) );
343 }
344
345 /*@}*/