7 * Mesa 3-D graphics library
10 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
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:
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
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.
33 #include "bufferobj.h"
43 * Specify whether to cull front- or back-facing facets.
45 * \param mode culling mode.
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.
54 _mesa_CullFace( GLenum mode
)
56 GET_CURRENT_CONTEXT(ctx
);
57 ASSERT_OUTSIDE_BEGIN_END(ctx
);
59 if (MESA_VERBOSE
&VERBOSE_API
)
60 _mesa_debug(ctx
, "glCullFace %s\n", _mesa_lookup_enum_by_nr(mode
));
62 if (mode
!=GL_FRONT
&& mode
!=GL_BACK
&& mode
!=GL_FRONT_AND_BACK
) {
63 _mesa_error( ctx
, GL_INVALID_ENUM
, "glCullFace" );
67 if (ctx
->Polygon
.CullFaceMode
== mode
)
70 FLUSH_VERTICES(ctx
, _NEW_POLYGON
);
71 ctx
->Polygon
.CullFaceMode
= mode
;
73 if (ctx
->Driver
.CullFace
)
74 ctx
->Driver
.CullFace( ctx
, mode
);
79 * Define front- and back-facing
81 * \param mode orientation of front-facing polygons.
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.
90 _mesa_FrontFace( GLenum mode
)
92 GET_CURRENT_CONTEXT(ctx
);
93 ASSERT_OUTSIDE_BEGIN_END(ctx
);
95 if (MESA_VERBOSE
&VERBOSE_API
)
96 _mesa_debug(ctx
, "glFrontFace %s\n", _mesa_lookup_enum_by_nr(mode
));
98 if (mode
!=GL_CW
&& mode
!=GL_CCW
) {
99 _mesa_error( ctx
, GL_INVALID_ENUM
, "glFrontFace" );
103 if (ctx
->Polygon
.FrontFace
== mode
)
106 FLUSH_VERTICES(ctx
, _NEW_POLYGON
);
107 ctx
->Polygon
.FrontFace
= mode
;
109 ctx
->Polygon
._FrontBit
= (GLboolean
) (mode
== GL_CW
);
111 if (ctx
->Driver
.FrontFace
)
112 ctx
->Driver
.FrontFace( ctx
, mode
);
117 * Set the polygon rasterization mode.
119 * \param face the polygons which \p mode applies to.
120 * \param mode how polygons should be rasterized.
122 * \sa glPolygonMode().
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.
129 _mesa_PolygonMode( GLenum face
, GLenum mode
)
131 GET_CURRENT_CONTEXT(ctx
);
132 ASSERT_OUTSIDE_BEGIN_END(ctx
);
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
));
139 if (mode
!=GL_POINT
&& mode
!=GL_LINE
&& mode
!=GL_FILL
) {
140 _mesa_error( ctx
, GL_INVALID_ENUM
, "glPolygonMode(mode)" );
146 if (ctx
->Polygon
.FrontMode
== mode
)
148 FLUSH_VERTICES(ctx
, _NEW_POLYGON
);
149 ctx
->Polygon
.FrontMode
= mode
;
151 case GL_FRONT_AND_BACK
:
152 if (ctx
->Polygon
.FrontMode
== mode
&&
153 ctx
->Polygon
.BackMode
== mode
)
155 FLUSH_VERTICES(ctx
, _NEW_POLYGON
);
156 ctx
->Polygon
.FrontMode
= mode
;
157 ctx
->Polygon
.BackMode
= mode
;
160 if (ctx
->Polygon
.BackMode
== mode
)
162 FLUSH_VERTICES(ctx
, _NEW_POLYGON
);
163 ctx
->Polygon
.BackMode
= mode
;
166 _mesa_error( ctx
, GL_INVALID_ENUM
, "glPolygonMode(face)" );
175 * This routine updates the ctx->Polygon.Stipple state.
176 * If we're getting the stipple data from a PBO, we map the buffer
177 * in order to access the data.
178 * In any case, we obey the current pixel unpacking parameters when fetching
181 * In the future, this routine should be used as a fallback, called via
182 * ctx->Driver.PolygonStipple(). We'll have to update all the DRI drivers
186 _mesa_polygon_stipple(GLcontext
*ctx
, const GLubyte
*pattern
)
188 if (ctx
->Unpack
.BufferObj
->Name
) {
189 /* Get/unpack the stipple pattern from a PBO */
191 if (!_mesa_validate_pbo_access(2, &ctx
->Unpack
, 32, 32, 1,
192 GL_COLOR_INDEX
, GL_BITMAP
, pattern
)) {
193 _mesa_error(ctx
, GL_INVALID_OPERATION
,
194 "glPolygonStipple(bad PBO access)");
197 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
199 ctx
->Unpack
.BufferObj
);
201 _mesa_error(ctx
, GL_INVALID_OPERATION
,
202 "glPolygonStipple(PBO mapped)");
205 buf
= ADD_POINTERS(buf
, pattern
);
206 _mesa_unpack_polygon_stipple(buf
, ctx
->PolygonStipple
, &ctx
->Unpack
);
207 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_UNPACK_BUFFER_EXT
,
208 ctx
->Unpack
.BufferObj
);
211 /* Get/unpack the stipple pattern from user memory */
212 _mesa_unpack_polygon_stipple(pattern
, ctx
->PolygonStipple
, &ctx
->Unpack
);
218 * Called by glPolygonStipple.
221 _mesa_PolygonStipple( const GLubyte
*pattern
)
223 GET_CURRENT_CONTEXT(ctx
);
224 ASSERT_OUTSIDE_BEGIN_END(ctx
);
226 if (MESA_VERBOSE
&VERBOSE_API
)
227 _mesa_debug(ctx
, "glPolygonStipple\n");
229 FLUSH_VERTICES(ctx
, _NEW_POLYGONSTIPPLE
);
231 _mesa_polygon_stipple(ctx
, pattern
);
233 if (ctx
->Driver
.PolygonStipple
)
234 ctx
->Driver
.PolygonStipple(ctx
, pattern
);
239 * Called by glPolygonStipple.
242 _mesa_GetPolygonStipple( GLubyte
*dest
)
244 GET_CURRENT_CONTEXT(ctx
);
245 ASSERT_OUTSIDE_BEGIN_END(ctx
);
247 if (MESA_VERBOSE
&VERBOSE_API
)
248 _mesa_debug(ctx
, "glGetPolygonStipple\n");
250 /* XXX someday we may put this code into a separate function and call
251 * it with ctx->Driver.GetPolygonStipple().
253 if (ctx
->Pack
.BufferObj
->Name
) {
254 /* Put/pack the stipple pattern into a PBO */
256 if (!_mesa_validate_pbo_access(2, &ctx
->Pack
, 32, 32, 1,
257 GL_COLOR_INDEX
, GL_BITMAP
, dest
)) {
258 _mesa_error(ctx
, GL_INVALID_OPERATION
,
259 "glGetPolygonStipple(bad PBO access)");
262 buf
= (GLubyte
*) ctx
->Driver
.MapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
264 ctx
->Pack
.BufferObj
);
266 _mesa_error(ctx
, GL_INVALID_OPERATION
,
267 "glGetPolygonStipple(PBO mapped)");
270 buf
= ADD_POINTERS(buf
, dest
);
271 _mesa_pack_polygon_stipple(ctx
->PolygonStipple
, buf
, &ctx
->Pack
);
272 ctx
->Driver
.UnmapBuffer(ctx
, GL_PIXEL_PACK_BUFFER_EXT
,
273 ctx
->Pack
.BufferObj
);
276 /* Put/pack the stipple pattern into user memory */
277 _mesa_pack_polygon_stipple(ctx
->PolygonStipple
, dest
, &ctx
->Pack
);
283 _mesa_PolygonOffset( GLfloat factor
, GLfloat units
)
285 GET_CURRENT_CONTEXT(ctx
);
286 ASSERT_OUTSIDE_BEGIN_END(ctx
);
288 if (MESA_VERBOSE
&VERBOSE_API
)
289 _mesa_debug(ctx
, "glPolygonOffset %f %f\n", factor
, units
);
291 if (ctx
->Polygon
.OffsetFactor
== factor
&&
292 ctx
->Polygon
.OffsetUnits
== units
)
295 FLUSH_VERTICES(ctx
, _NEW_POLYGON
);
296 ctx
->Polygon
.OffsetFactor
= factor
;
297 ctx
->Polygon
.OffsetUnits
= units
;
299 if (ctx
->Driver
.PolygonOffset
)
300 ctx
->Driver
.PolygonOffset( ctx
, factor
, units
);
305 _mesa_PolygonOffsetEXT( GLfloat factor
, GLfloat bias
)
307 GET_CURRENT_CONTEXT(ctx
);
308 /* XXX mult by DepthMaxF here??? */
309 _mesa_PolygonOffset(factor
, bias
* ctx
->DrawBuffer
->_DepthMaxF
);
315 /**********************************************************************/
316 /** \name Initialization */
320 * Initialize the context polygon state.
322 * \param ctx GL context.
324 * Initializes __GLcontextRec::Polygon and __GLcontextRec::PolygonStipple
327 void _mesa_init_polygon( GLcontext
* ctx
)
330 ctx
->Polygon
.CullFlag
= GL_FALSE
;
331 ctx
->Polygon
.CullFaceMode
= GL_BACK
;
332 ctx
->Polygon
.FrontFace
= GL_CCW
;
333 ctx
->Polygon
._FrontBit
= 0;
334 ctx
->Polygon
.FrontMode
= GL_FILL
;
335 ctx
->Polygon
.BackMode
= GL_FILL
;
336 ctx
->Polygon
.SmoothFlag
= GL_FALSE
;
337 ctx
->Polygon
.StippleFlag
= GL_FALSE
;
338 ctx
->Polygon
.OffsetFactor
= 0.0F
;
339 ctx
->Polygon
.OffsetUnits
= 0.0F
;
340 ctx
->Polygon
.OffsetPoint
= GL_FALSE
;
341 ctx
->Polygon
.OffsetLine
= GL_FALSE
;
342 ctx
->Polygon
.OffsetFill
= GL_FALSE
;
345 /* Polygon Stipple group */
346 MEMSET( ctx
->PolygonStipple
, 0xff, 32*sizeof(GLuint
) );