xref: /aosp_15_r20/external/mesa3d/src/mesa/main/polygon.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 "util/glheader.h"
32 
33 #include "context.h"
34 #include "draw_validate.h"
35 #include "image.h"
36 #include "enums.h"
37 #include "pack.h"
38 #include "pbo.h"
39 #include "polygon.h"
40 #include "mtypes.h"
41 #include "api_exec_decl.h"
42 #include "varray.h"
43 
44 #include "state_tracker/st_context.h"
45 
46 /**
47  * Specify whether to cull front- or back-facing facets.
48  *
49  * \param mode culling mode.
50  *
51  * \sa glCullFace().
52  *
53  * Verifies the parameter and updates gl_polygon_attrib::CullFaceMode. On
54  * change, flushes the vertices and notifies the driver via
55  * the dd_function_table::CullFace callback.
56  */
57 static ALWAYS_INLINE void
cull_face(struct gl_context * ctx,GLenum mode,bool no_error)58 cull_face(struct gl_context *ctx, GLenum mode, bool no_error)
59 {
60    if (ctx->Polygon.CullFaceMode == mode)
61       return;
62 
63    if (!no_error &&
64        mode != GL_FRONT && mode != GL_BACK && mode != GL_FRONT_AND_BACK) {
65       _mesa_error(ctx, GL_INVALID_ENUM, "glCullFace");
66       return;
67    }
68 
69    FLUSH_VERTICES(ctx, 0,
70                   GL_POLYGON_BIT);
71    ctx->NewDriverState |= ST_NEW_RASTERIZER;
72    ctx->Polygon.CullFaceMode = mode;
73 }
74 
75 
76 void GLAPIENTRY
_mesa_CullFace_no_error(GLenum mode)77 _mesa_CullFace_no_error(GLenum mode)
78 {
79    GET_CURRENT_CONTEXT(ctx);
80    cull_face(ctx, mode, true);
81 }
82 
83 
84 void GLAPIENTRY
_mesa_CullFace(GLenum mode)85 _mesa_CullFace(GLenum mode)
86 {
87    GET_CURRENT_CONTEXT(ctx);
88 
89    if (MESA_VERBOSE & VERBOSE_API)
90       _mesa_debug(ctx, "glCullFace %s\n", _mesa_enum_to_string(mode));
91 
92    cull_face(ctx, mode, false);
93 }
94 
95 
96 /**
97  * Define front- and back-facing
98  *
99  * \param mode orientation of front-facing polygons.
100  *
101  * \sa glFrontFace().
102  *
103  * Verifies the parameter and updates gl_polygon_attrib::FrontFace. On change
104  * flushes the vertices and notifies the driver via
105  * the dd_function_table::FrontFace callback.
106  */
107 static ALWAYS_INLINE void
front_face(struct gl_context * ctx,GLenum mode,bool no_error)108 front_face(struct gl_context *ctx, GLenum mode, bool no_error)
109 {
110    if (ctx->Polygon.FrontFace == mode)
111       return;
112 
113    if (!no_error && mode != GL_CW && mode != GL_CCW) {
114       _mesa_error(ctx, GL_INVALID_ENUM, "glFrontFace");
115       return;
116    }
117 
118    FLUSH_VERTICES(ctx, 0,
119                   GL_POLYGON_BIT);
120    ctx->NewDriverState |= ST_NEW_RASTERIZER;
121    ctx->Polygon.FrontFace = mode;
122 }
123 
124 
125 void GLAPIENTRY
_mesa_FrontFace_no_error(GLenum mode)126 _mesa_FrontFace_no_error(GLenum mode)
127 {
128    GET_CURRENT_CONTEXT(ctx);
129    front_face(ctx, mode, true);
130 }
131 
132 
133 void GLAPIENTRY
_mesa_FrontFace(GLenum mode)134 _mesa_FrontFace(GLenum mode)
135 {
136    GET_CURRENT_CONTEXT(ctx);
137 
138    if (MESA_VERBOSE & VERBOSE_API)
139       _mesa_debug(ctx, "glFrontFace %s\n", _mesa_enum_to_string(mode));
140 
141    front_face(ctx, mode, false);
142 }
143 
144 
145 /**
146  * Set the polygon rasterization mode.
147  *
148  * \param face the polygons which \p mode applies to.
149  * \param mode how polygons should be rasterized.
150  *
151  * \sa glPolygonMode().
152  *
153  * Verifies the parameters and updates gl_polygon_attrib::FrontMode and
154  * gl_polygon_attrib::BackMode. On change flushes the vertices and notifies the
155  * driver via the dd_function_table::PolygonMode callback.
156  */
157 static ALWAYS_INLINE void
polygon_mode(struct gl_context * ctx,GLenum face,GLenum mode,bool no_error)158 polygon_mode(struct gl_context *ctx, GLenum face, GLenum mode, bool no_error)
159 {
160    bool old_mode_has_fill_rectangle =
161       ctx->Polygon.FrontMode == GL_FILL_RECTANGLE_NV ||
162       ctx->Polygon.BackMode == GL_FILL_RECTANGLE_NV;
163 
164    if (MESA_VERBOSE & VERBOSE_API)
165       _mesa_debug(ctx, "glPolygonMode %s %s\n",
166                   _mesa_enum_to_string(face),
167                   _mesa_enum_to_string(mode));
168 
169    if (!no_error) {
170       switch (mode) {
171       case GL_POINT:
172       case GL_LINE:
173       case GL_FILL:
174          break;
175       case GL_FILL_RECTANGLE_NV:
176          if (ctx->Extensions.NV_fill_rectangle)
177             break;
178          FALLTHROUGH;
179       default:
180          _mesa_error(ctx, GL_INVALID_ENUM, "glPolygonMode(mode)");
181          return;
182       }
183    }
184 
185    switch (face) {
186    case GL_FRONT:
187       if (!no_error && _mesa_is_desktop_gl_core(ctx)) {
188          _mesa_error( ctx, GL_INVALID_ENUM, "glPolygonMode(face)" );
189          return;
190       }
191       if (ctx->Polygon.FrontMode == mode)
192          return;
193       FLUSH_VERTICES(ctx, 0,
194                      GL_POLYGON_BIT);
195       ctx->NewDriverState |= ST_NEW_RASTERIZER;
196       ctx->Polygon.FrontMode = mode;
197       _mesa_update_edgeflag_state_vao(ctx);
198       break;
199    case GL_FRONT_AND_BACK:
200       if (ctx->Polygon.FrontMode == mode && ctx->Polygon.BackMode == mode)
201          return;
202       FLUSH_VERTICES(ctx, 0,
203                      GL_POLYGON_BIT);
204       ctx->NewDriverState |= ST_NEW_RASTERIZER;
205       ctx->Polygon.FrontMode = mode;
206       ctx->Polygon.BackMode = mode;
207       _mesa_update_edgeflag_state_vao(ctx);
208       break;
209    case GL_BACK:
210       if (!no_error && _mesa_is_desktop_gl_core(ctx)) {
211          _mesa_error( ctx, GL_INVALID_ENUM, "glPolygonMode(face)" );
212          return;
213       }
214       if (ctx->Polygon.BackMode == mode)
215          return;
216       FLUSH_VERTICES(ctx, 0,
217                      GL_POLYGON_BIT);
218       ctx->NewDriverState |= ST_NEW_RASTERIZER;
219       ctx->Polygon.BackMode = mode;
220       _mesa_update_edgeflag_state_vao(ctx);
221       break;
222    default:
223       if (!no_error)
224          _mesa_error( ctx, GL_INVALID_ENUM, "glPolygonMode(face)" );
225       return;
226    }
227 
228    if (ctx->Extensions.INTEL_conservative_rasterization ||
229        (mode == GL_FILL_RECTANGLE_NV || old_mode_has_fill_rectangle))
230       _mesa_update_valid_to_render_state(ctx);
231 }
232 
233 
234 void GLAPIENTRY
_mesa_PolygonMode_no_error(GLenum face,GLenum mode)235 _mesa_PolygonMode_no_error(GLenum face, GLenum mode)
236 {
237    GET_CURRENT_CONTEXT(ctx);
238    polygon_mode(ctx, face, mode, true);
239 }
240 
241 
242 void GLAPIENTRY
_mesa_PolygonMode(GLenum face,GLenum mode)243 _mesa_PolygonMode(GLenum face, GLenum mode)
244 {
245    GET_CURRENT_CONTEXT(ctx);
246    polygon_mode(ctx, face, mode, false);
247 }
248 
249 
250 /**
251  * Called by glPolygonStipple.
252  */
253 void GLAPIENTRY
_mesa_PolygonStipple(const GLubyte * pattern)254 _mesa_PolygonStipple(const GLubyte *pattern)
255 {
256    GET_CURRENT_CONTEXT(ctx);
257 
258    if (MESA_VERBOSE & VERBOSE_API)
259       _mesa_debug(ctx, "glPolygonStipple\n");
260 
261    FLUSH_VERTICES(ctx, 0, GL_POLYGON_STIPPLE_BIT);
262    ctx->NewDriverState |= ST_NEW_POLY_STIPPLE;
263 
264    pattern = _mesa_map_validate_pbo_source(ctx, 2,
265                                            &ctx->Unpack, 32, 32, 1,
266                                            GL_COLOR_INDEX, GL_BITMAP,
267                                            INT_MAX, pattern,
268                                            "glPolygonStipple");
269    if (!pattern)
270       return;
271 
272    _mesa_unpack_polygon_stipple(pattern, ctx->PolygonStipple, &ctx->Unpack);
273 
274    _mesa_unmap_pbo_source(ctx, &ctx->Unpack);
275 }
276 
277 
278 /**
279  * Called by glPolygonStipple.
280  */
281 void GLAPIENTRY
_mesa_GetnPolygonStippleARB(GLsizei bufSize,GLubyte * dest)282 _mesa_GetnPolygonStippleARB( GLsizei bufSize, GLubyte *dest )
283 {
284    GET_CURRENT_CONTEXT(ctx);
285 
286    if (MESA_VERBOSE&VERBOSE_API)
287       _mesa_debug(ctx, "glGetPolygonStipple\n");
288 
289    if (ctx->Pack.BufferObj)
290       ctx->Pack.BufferObj->UsageHistory |= USAGE_PIXEL_PACK_BUFFER;
291 
292    dest = _mesa_map_validate_pbo_dest(ctx, 2,
293                                       &ctx->Pack, 32, 32, 1,
294                                       GL_COLOR_INDEX, GL_BITMAP,
295                                       bufSize, dest, "glGetPolygonStipple");
296    if (!dest)
297       return;
298 
299    _mesa_pack_polygon_stipple(ctx->PolygonStipple, dest, &ctx->Pack);
300 
301    _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
302 }
303 
304 
305 void GLAPIENTRY
_mesa_GetPolygonStipple(GLubyte * dest)306 _mesa_GetPolygonStipple( GLubyte *dest )
307 {
308    _mesa_GetnPolygonStippleARB(INT_MAX, dest);
309 }
310 
311 void
_mesa_polygon_offset_clamp(struct gl_context * ctx,GLfloat factor,GLfloat units,GLfloat clamp)312 _mesa_polygon_offset_clamp(struct gl_context *ctx,
313                            GLfloat factor, GLfloat units, GLfloat clamp)
314 {
315    if (ctx->Polygon.OffsetFactor == factor &&
316        ctx->Polygon.OffsetUnits == units &&
317        ctx->Polygon.OffsetClamp == clamp)
318       return;
319 
320    FLUSH_VERTICES(ctx, 0,
321                   GL_POLYGON_BIT);
322    ctx->NewDriverState |= ST_NEW_RASTERIZER;
323    ctx->Polygon.OffsetFactor = factor;
324    ctx->Polygon.OffsetUnits = units;
325    ctx->Polygon.OffsetClamp = clamp;
326 }
327 
328 void GLAPIENTRY
_mesa_PolygonOffset(GLfloat factor,GLfloat units)329 _mesa_PolygonOffset( GLfloat factor, GLfloat units )
330 {
331    GET_CURRENT_CONTEXT(ctx);
332 
333    if (MESA_VERBOSE&VERBOSE_API)
334       _mesa_debug(ctx, "glPolygonOffset %f %f\n", factor, units);
335 
336    _mesa_polygon_offset_clamp(ctx, factor, units, 0.0);
337 }
338 
339 void GLAPIENTRY
_mesa_PolygonOffsetClampEXT(GLfloat factor,GLfloat units,GLfloat clamp)340 _mesa_PolygonOffsetClampEXT( GLfloat factor, GLfloat units, GLfloat clamp )
341 {
342    GET_CURRENT_CONTEXT(ctx);
343 
344    if (!ctx->Extensions.ARB_polygon_offset_clamp) {
345       _mesa_error(ctx, GL_INVALID_OPERATION,
346                   "unsupported function (%s) called", "glPolygonOffsetClamp");
347       return;
348    }
349 
350    if (MESA_VERBOSE&VERBOSE_API)
351       _mesa_debug(ctx, "glPolygonOffsetClamp %f %f %f\n", factor, units, clamp);
352 
353    _mesa_polygon_offset_clamp(ctx, factor, units, clamp);
354 }
355 
356 /**********************************************************************/
357 /** \name Initialization */
358 /*@{*/
359 
360 /**
361  * Initialize the context polygon state.
362  *
363  * \param ctx GL context.
364  *
365  * Initializes __struct gl_contextRec::Polygon and __struct gl_contextRec::PolygonStipple
366  * attribute groups.
367  */
_mesa_init_polygon(struct gl_context * ctx)368 void _mesa_init_polygon( struct gl_context * ctx )
369 {
370    /* Polygon group */
371    ctx->Polygon.CullFlag = GL_FALSE;
372    ctx->Polygon.CullFaceMode = GL_BACK;
373    ctx->Polygon.FrontFace = GL_CCW;
374    ctx->Polygon.FrontMode = GL_FILL;
375    ctx->Polygon.BackMode = GL_FILL;
376    ctx->Polygon.SmoothFlag = GL_FALSE;
377    ctx->Polygon.StippleFlag = GL_FALSE;
378    ctx->Polygon.OffsetFactor = 0.0F;
379    ctx->Polygon.OffsetUnits = 0.0F;
380    ctx->Polygon.OffsetClamp = 0.0F;
381    ctx->Polygon.OffsetPoint = GL_FALSE;
382    ctx->Polygon.OffsetLine = GL_FALSE;
383    ctx->Polygon.OffsetFill = GL_FALSE;
384 
385 
386    /* Polygon Stipple group */
387    memset( ctx->PolygonStipple, 0xff, 32*sizeof(GLuint) );
388 }
389 
390 /*@}*/
391