Merge branch 'mesa_7_5_branch'
[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 * Version: 6.5.1
9 *
10 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 */
29
30
31 #include "glheader.h"
32 #include "imports.h"
33 #include "bufferobj.h"
34 #include "context.h"
35 #include "image.h"
36 #include "enums.h"
37 #include "macros.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 ASSERT_OUTSIDE_BEGIN_END(ctx);
58
59 if (MESA_VERBOSE&VERBOSE_API)
60 _mesa_debug(ctx, "glCullFace %s\n", _mesa_lookup_enum_by_nr(mode));
61
62 if (mode!=GL_FRONT && mode!=GL_BACK && mode!=GL_FRONT_AND_BACK) {
63 _mesa_error( ctx, GL_INVALID_ENUM, "glCullFace" );
64 return;
65 }
66
67 if (ctx->Polygon.CullFaceMode == mode)
68 return;
69
70 FLUSH_VERTICES(ctx, _NEW_POLYGON);
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 ASSERT_OUTSIDE_BEGIN_END(ctx);
94
95 if (MESA_VERBOSE&VERBOSE_API)
96 _mesa_debug(ctx, "glFrontFace %s\n", _mesa_lookup_enum_by_nr(mode));
97
98 if (mode!=GL_CW && mode!=GL_CCW) {
99 _mesa_error( ctx, GL_INVALID_ENUM, "glFrontFace" );
100 return;
101 }
102
103 if (ctx->Polygon.FrontFace == mode)
104 return;
105
106 FLUSH_VERTICES(ctx, _NEW_POLYGON);
107 ctx->Polygon.FrontFace = mode;
108
109 ctx->Polygon._FrontBit = (GLboolean) (mode == GL_CW);
110
111 if (ctx->Driver.FrontFace)
112 ctx->Driver.FrontFace( ctx, mode );
113 }
114
115
116 /**
117 * Set the polygon rasterization mode.
118 *
119 * \param face the polygons which \p mode applies to.
120 * \param mode how polygons should be rasterized.
121 *
122 * \sa glPolygonMode().
123 *
124 * Verifies the parameters and updates gl_polygon_attrib::FrontMode and
125 * gl_polygon_attrib::BackMode. On change flushes the vertices and notifies the
126 * driver via the dd_function_table::PolygonMode callback.
127 */
128 void GLAPIENTRY
129 _mesa_PolygonMode( GLenum face, GLenum mode )
130 {
131 GET_CURRENT_CONTEXT(ctx);
132 ASSERT_OUTSIDE_BEGIN_END(ctx);
133
134 if (MESA_VERBOSE&VERBOSE_API)
135 _mesa_debug(ctx, "glPolygonMode %s %s\n",
136 _mesa_lookup_enum_by_nr(face),
137 _mesa_lookup_enum_by_nr(mode));
138
139 if (mode!=GL_POINT && mode!=GL_LINE && mode!=GL_FILL) {
140 _mesa_error( ctx, GL_INVALID_ENUM, "glPolygonMode(mode)" );
141 return;
142 }
143
144 switch (face) {
145 case GL_FRONT:
146 if (ctx->Polygon.FrontMode == mode)
147 return;
148 FLUSH_VERTICES(ctx, _NEW_POLYGON);
149 ctx->Polygon.FrontMode = mode;
150 break;
151 case GL_FRONT_AND_BACK:
152 if (ctx->Polygon.FrontMode == mode &&
153 ctx->Polygon.BackMode == mode)
154 return;
155 FLUSH_VERTICES(ctx, _NEW_POLYGON);
156 ctx->Polygon.FrontMode = mode;
157 ctx->Polygon.BackMode = mode;
158 break;
159 case GL_BACK:
160 if (ctx->Polygon.BackMode == mode)
161 return;
162 FLUSH_VERTICES(ctx, _NEW_POLYGON);
163 ctx->Polygon.BackMode = mode;
164 break;
165 default:
166 _mesa_error( ctx, GL_INVALID_ENUM, "glPolygonMode(face)" );
167 return;
168 }
169
170 if (ctx->Polygon.FrontMode == GL_FILL && ctx->Polygon.BackMode == GL_FILL)
171 ctx->_TriangleCaps &= ~DD_TRI_UNFILLED;
172 else
173 ctx->_TriangleCaps |= DD_TRI_UNFILLED;
174
175 if (ctx->Driver.PolygonMode)
176 ctx->Driver.PolygonMode(ctx, face, mode);
177 }
178
179 #if _HAVE_FULL_GL
180
181
182 /**
183 * This routine updates the ctx->Polygon.Stipple state.
184 * If we're getting the stipple data from a PBO, we map the buffer
185 * in order to access the data.
186 * In any case, we obey the current pixel unpacking parameters when fetching
187 * the stipple data.
188 *
189 * In the future, this routine should be used as a fallback, called via
190 * ctx->Driver.PolygonStipple(). We'll have to update all the DRI drivers
191 * too.
192 */
193 void
194 _mesa_polygon_stipple(GLcontext *ctx, const GLubyte *pattern)
195 {
196 if (_mesa_is_bufferobj(ctx->Unpack.BufferObj)) {
197 /* Get/unpack the stipple pattern from a PBO */
198 GLubyte *buf;
199 if (!_mesa_validate_pbo_access(2, &ctx->Unpack, 32, 32, 1,
200 GL_COLOR_INDEX, GL_BITMAP, pattern)) {
201 _mesa_error(ctx, GL_INVALID_OPERATION,
202 "glPolygonStipple(bad PBO access)");
203 return;
204 }
205 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
206 GL_READ_ONLY_ARB,
207 ctx->Unpack.BufferObj);
208 if (!buf) {
209 _mesa_error(ctx, GL_INVALID_OPERATION,
210 "glPolygonStipple(PBO mapped)");
211 return;
212 }
213 buf = ADD_POINTERS(buf, pattern);
214 _mesa_unpack_polygon_stipple(buf, ctx->PolygonStipple, &ctx->Unpack);
215 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
216 ctx->Unpack.BufferObj);
217 }
218 else {
219 /* Get/unpack the stipple pattern from user memory */
220 _mesa_unpack_polygon_stipple(pattern, ctx->PolygonStipple, &ctx->Unpack);
221 }
222 }
223
224
225 /**
226 * Called by glPolygonStipple.
227 */
228 void GLAPIENTRY
229 _mesa_PolygonStipple( const GLubyte *pattern )
230 {
231 GET_CURRENT_CONTEXT(ctx);
232 ASSERT_OUTSIDE_BEGIN_END(ctx);
233
234 if (MESA_VERBOSE&VERBOSE_API)
235 _mesa_debug(ctx, "glPolygonStipple\n");
236
237 FLUSH_VERTICES(ctx, _NEW_POLYGONSTIPPLE);
238
239 _mesa_polygon_stipple(ctx, pattern);
240
241 if (ctx->Driver.PolygonStipple)
242 ctx->Driver.PolygonStipple(ctx, pattern);
243 }
244
245
246 /**
247 * Called by glPolygonStipple.
248 */
249 void GLAPIENTRY
250 _mesa_GetPolygonStipple( GLubyte *dest )
251 {
252 GET_CURRENT_CONTEXT(ctx);
253 ASSERT_OUTSIDE_BEGIN_END(ctx);
254
255 if (MESA_VERBOSE&VERBOSE_API)
256 _mesa_debug(ctx, "glGetPolygonStipple\n");
257
258 /* XXX someday we may put this code into a separate function and call
259 * it with ctx->Driver.GetPolygonStipple().
260 */
261 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
262 /* Put/pack the stipple pattern into a PBO */
263 GLubyte *buf;
264 if (!_mesa_validate_pbo_access(2, &ctx->Pack, 32, 32, 1,
265 GL_COLOR_INDEX, GL_BITMAP, dest)) {
266 _mesa_error(ctx, GL_INVALID_OPERATION,
267 "glGetPolygonStipple(bad PBO access)");
268 return;
269 }
270 buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
271 GL_WRITE_ONLY_ARB,
272 ctx->Pack.BufferObj);
273 if (!buf) {
274 _mesa_error(ctx, GL_INVALID_OPERATION,
275 "glGetPolygonStipple(PBO mapped)");
276 return;
277 }
278 buf = ADD_POINTERS(buf, dest);
279 _mesa_pack_polygon_stipple(ctx->PolygonStipple, buf, &ctx->Pack);
280 ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
281 ctx->Pack.BufferObj);
282 }
283 else {
284 /* Put/pack the stipple pattern into user memory */
285 _mesa_pack_polygon_stipple(ctx->PolygonStipple, dest, &ctx->Pack);
286 }
287 }
288
289
290 void GLAPIENTRY
291 _mesa_PolygonOffset( GLfloat factor, GLfloat units )
292 {
293 GET_CURRENT_CONTEXT(ctx);
294 ASSERT_OUTSIDE_BEGIN_END(ctx);
295
296 if (MESA_VERBOSE&VERBOSE_API)
297 _mesa_debug(ctx, "glPolygonOffset %f %f\n", factor, units);
298
299 if (ctx->Polygon.OffsetFactor == factor &&
300 ctx->Polygon.OffsetUnits == units)
301 return;
302
303 FLUSH_VERTICES(ctx, _NEW_POLYGON);
304 ctx->Polygon.OffsetFactor = factor;
305 ctx->Polygon.OffsetUnits = units;
306
307 if (ctx->Driver.PolygonOffset)
308 ctx->Driver.PolygonOffset( ctx, factor, units );
309 }
310
311
312 void GLAPIENTRY
313 _mesa_PolygonOffsetEXT( GLfloat factor, GLfloat bias )
314 {
315 GET_CURRENT_CONTEXT(ctx);
316 /* XXX mult by DepthMaxF here??? */
317 _mesa_PolygonOffset(factor, bias * ctx->DrawBuffer->_DepthMaxF );
318 }
319
320 #endif
321
322
323 /**********************************************************************/
324 /** \name Initialization */
325 /*@{*/
326
327 /**
328 * Initialize the context polygon state.
329 *
330 * \param ctx GL context.
331 *
332 * Initializes __GLcontextRec::Polygon and __GLcontextRec::PolygonStipple
333 * attribute groups.
334 */
335 void _mesa_init_polygon( GLcontext * ctx )
336 {
337 /* Polygon group */
338 ctx->Polygon.CullFlag = GL_FALSE;
339 ctx->Polygon.CullFaceMode = GL_BACK;
340 ctx->Polygon.FrontFace = GL_CCW;
341 ctx->Polygon._FrontBit = 0;
342 ctx->Polygon.FrontMode = GL_FILL;
343 ctx->Polygon.BackMode = GL_FILL;
344 ctx->Polygon.SmoothFlag = GL_FALSE;
345 ctx->Polygon.StippleFlag = GL_FALSE;
346 ctx->Polygon.OffsetFactor = 0.0F;
347 ctx->Polygon.OffsetUnits = 0.0F;
348 ctx->Polygon.OffsetPoint = GL_FALSE;
349 ctx->Polygon.OffsetLine = GL_FALSE;
350 ctx->Polygon.OffsetFill = GL_FALSE;
351
352
353 /* Polygon Stipple group */
354 MEMSET( ctx->PolygonStipple, 0xff, 32*sizeof(GLuint) );
355 }
356
357 /*@}*/