xref: /aosp_15_r20/external/mesa3d/src/mesa/state_tracker/st_atom_rasterizer.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright 2007 VMware, Inc.
4  * 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
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28  /*
29   * Authors:
30   *   Keith Whitwell <[email protected]>
31   */
32 
33 #include "main/macros.h"
34 #include "main/framebuffer.h"
35 #include "main/state.h"
36 #include "st_context.h"
37 #include "st_atom.h"
38 #include "st_debug.h"
39 #include "st_program.h"
40 #include "st_util.h"
41 #include "pipe/p_context.h"
42 #include "pipe/p_defines.h"
43 #include "cso_cache/cso_context.h"
44 #include "main/context.h"
45 
46 
47 static GLuint
translate_fill(GLenum mode)48 translate_fill(GLenum mode)
49 {
50    switch (mode) {
51    case GL_POINT:
52       return PIPE_POLYGON_MODE_POINT;
53    case GL_LINE:
54       return PIPE_POLYGON_MODE_LINE;
55    case GL_FILL:
56       return PIPE_POLYGON_MODE_FILL;
57    case GL_FILL_RECTANGLE_NV:
58       return PIPE_POLYGON_MODE_FILL_RECTANGLE;
59    default:
60       assert(0);
61       return 0;
62    }
63 }
64 
65 void
st_update_rasterizer(struct st_context * st)66 st_update_rasterizer(struct st_context *st)
67 {
68    struct gl_context *ctx = st->ctx;
69    struct pipe_rasterizer_state *raster = &st->state.rasterizer;
70    const struct gl_program *fragProg = ctx->FragmentProgram._Current;
71 
72    memset(raster, 0, sizeof(*raster));
73 
74    /* _NEW_POLYGON, _NEW_BUFFERS
75     */
76    {
77       raster->front_ccw = (ctx->Polygon.FrontFace == GL_CCW);
78 
79       /* _NEW_TRANSFORM */
80       if (ctx->Transform.ClipOrigin == GL_UPPER_LEFT) {
81          raster->front_ccw ^= 1;
82       }
83 
84       /*
85        * Gallium's surfaces are Y=0=TOP orientation.  OpenGL is the
86        * opposite.  Window system surfaces are Y=0=TOP.  Mesa's FBOs
87        * must match OpenGL conventions so FBOs use Y=0=BOTTOM.  In that
88        * case, we must invert Y and flip the notion of front vs. back.
89        */
90       if (st->state.fb_orientation == Y_0_BOTTOM) {
91          /* Drawing to an FBO.  The viewport will be inverted. */
92          raster->front_ccw ^= 1;
93       }
94    }
95 
96    /* _NEW_LIGHT_STATE */
97    raster->flatshade = !st->lower_flatshade &&
98                        ctx->Light.ShadeModel == GL_FLAT;
99 
100    raster->flatshade_first = ctx->Light.ProvokingVertex ==
101                              GL_FIRST_VERTEX_CONVENTION_EXT;
102 
103    /* _NEW_LIGHT_STATE | _NEW_PROGRAM */
104    if (!st->lower_two_sided_color)
105       raster->light_twoside = _mesa_vertex_program_two_side_enabled(ctx);
106 
107    /*_NEW_LIGHT_STATE | _NEW_BUFFERS */
108    raster->clamp_vertex_color = !st->clamp_vert_color_in_shader &&
109                                 ctx->Light._ClampVertexColor;
110 
111    /* _NEW_POLYGON
112     */
113    if (ctx->Polygon.CullFlag) {
114       switch (ctx->Polygon.CullFaceMode) {
115       case GL_FRONT:
116          raster->cull_face = PIPE_FACE_FRONT;
117          break;
118       case GL_BACK:
119          raster->cull_face = PIPE_FACE_BACK;
120          break;
121       case GL_FRONT_AND_BACK:
122          raster->cull_face = PIPE_FACE_FRONT_AND_BACK;
123          break;
124       }
125    }
126    else {
127       raster->cull_face = PIPE_FACE_NONE;
128    }
129 
130    /* _NEW_POLYGON
131     */
132    {
133       if (ST_DEBUG & DEBUG_WIREFRAME) {
134          raster->fill_front = PIPE_POLYGON_MODE_LINE;
135          raster->fill_back = PIPE_POLYGON_MODE_LINE;
136       }
137       else {
138          raster->fill_front = translate_fill(ctx->Polygon.FrontMode);
139          raster->fill_back = translate_fill(ctx->Polygon.BackMode);
140       }
141 
142       /* Simplify when culling is active:
143        */
144       if (raster->cull_face & PIPE_FACE_FRONT) {
145          raster->fill_front = raster->fill_back;
146       }
147 
148       if (raster->cull_face & PIPE_FACE_BACK) {
149          raster->fill_back = raster->fill_front;
150       }
151    }
152 
153    /* _NEW_POLYGON
154     */
155    if (ctx->Polygon.OffsetPoint ||
156        ctx->Polygon.OffsetLine ||
157        ctx->Polygon.OffsetFill) {
158       raster->offset_point = ctx->Polygon.OffsetPoint;
159       raster->offset_line = ctx->Polygon.OffsetLine;
160       raster->offset_tri = ctx->Polygon.OffsetFill;
161       raster->offset_units = ctx->Polygon.OffsetUnits;
162       raster->offset_scale = ctx->Polygon.OffsetFactor;
163       raster->offset_clamp = ctx->Polygon.OffsetClamp;
164    }
165 
166    raster->poly_stipple_enable = ctx->Polygon.StippleFlag;
167 
168    /* Multisampling disables point, line, and polygon smoothing.
169     *
170     * GL_ARB_multisample says:
171     *
172     *   "If MULTISAMPLE_ARB is enabled, and SAMPLE_BUFFERS_ARB is a value of
173     *    one, then points are rasterized using the following algorithm,
174     *    regardless of whether point antialiasing (POINT_SMOOTH) is enabled"
175     *
176     *   "If MULTISAMPLE_ARB is enabled, and SAMPLE_BUFFERS_ARB is a value of
177     *    one, then lines are rasterized using the following algorithm,
178     *    regardless of whether line antialiasing (LINE_SMOOTH) is enabled"
179     *
180     *   "If MULTISAMPLE_ARB is enabled, and SAMPLE_BUFFERS_ARB is a value of
181     *    one, then polygons are rasterized using the following algorithm,
182     *    regardless of whether polygon antialiasing (POLYGON_SMOOTH) is
183     *    enabled"
184     */
185 
186    /* _NEW_MULTISAMPLE */
187    bool multisample = _mesa_is_multisample_enabled(ctx);
188    raster->multisample = multisample;
189 
190    /* _NEW_POLYGON | _NEW_MULTISAMPLE */
191    raster->poly_smooth = !multisample && ctx->Polygon.SmoothFlag;
192 
193    /* _NEW_POINT
194     */
195    raster->point_size = ctx->Point.Size;
196 
197    /* _NEW_POINT | _NEW_MULTISAMPLE */
198    raster->point_smooth = !multisample && !ctx->Point.PointSprite &&
199                           ctx->Point.SmoothFlag;
200 
201    /* _NEW_POINT | _NEW_PROGRAM
202     */
203    if (ctx->Point.PointSprite) {
204       /* origin */
205       if ((ctx->Point.SpriteOrigin == GL_UPPER_LEFT) ^
206           (st->state.fb_orientation == Y_0_BOTTOM))
207          raster->sprite_coord_mode = PIPE_SPRITE_COORD_UPPER_LEFT;
208       else
209          raster->sprite_coord_mode = PIPE_SPRITE_COORD_LOWER_LEFT;
210 
211       /* Coord replacement flags.  If bit 'k' is set that means
212        * that we need to replace GENERIC[k] attrib with an automatically
213        * computed texture coord.
214        */
215       raster->sprite_coord_enable = ctx->Point.CoordReplace &
216          ((1u << MAX_TEXTURE_COORD_UNITS) - 1);
217       if (!st->needs_texcoord_semantic &&
218           fragProg->info.inputs_read & VARYING_BIT_PNTC) {
219          raster->sprite_coord_enable |=
220             1 << st_get_generic_varying_index(st, VARYING_SLOT_PNTC);
221       }
222 
223       raster->point_quad_rasterization = 1;
224 
225       raster->point_line_tri_clip = _mesa_is_gles2(st->ctx);
226    }
227 
228    /* ST_NEW_VERTEX_PROGRAM
229     */
230    raster->point_size_per_vertex = st_point_size_per_vertex(ctx);
231    if (!raster->point_size_per_vertex) {
232       /* clamp size now */
233       raster->point_size = CLAMP(ctx->Point.Size,
234                                  ctx->Point.MinSize,
235                                  ctx->Point.MaxSize);
236    }
237 
238    /* _NEW_LINE | _NEW_MULTISAMPLE
239     */
240    if (!multisample && ctx->Line.SmoothFlag) {
241       raster->line_smooth = 1;
242       raster->line_width = CLAMP(ctx->Line.Width,
243                                  ctx->Const.MinLineWidthAA,
244                                  ctx->Const.MaxLineWidthAA);
245    }
246    else {
247       raster->line_width = CLAMP(ctx->Line.Width,
248                                  ctx->Const.MinLineWidth,
249                                  ctx->Const.MaxLineWidth);
250    }
251 
252    raster->line_rectangular = multisample || ctx->Line.SmoothFlag;
253 
254    /* When the pattern is all 1's, it means line stippling is disabled */
255    raster->line_stipple_enable = ctx->Line.StippleFlag && ctx->Line.StipplePattern != 0xffff;
256    raster->line_stipple_pattern = ctx->Line.StipplePattern;
257    /* GL stipple factor is in [1,256], remap to [0, 255] here */
258    raster->line_stipple_factor = ctx->Line.StippleFactor - 1;
259 
260    /* _NEW_MULTISAMPLE | _NEW_BUFFERS */
261    raster->force_persample_interp =
262          !st->force_persample_in_shader &&
263          raster->multisample &&
264          ctx->Multisample.SampleShading &&
265          ctx->Multisample.MinSampleShadingValue *
266          _mesa_geometric_samples(ctx->DrawBuffer) > 1;
267 
268    /* _NEW_SCISSOR */
269    raster->scissor = !!ctx->Scissor.EnableFlags;
270 
271    /* gl_driver_flags::NewFragClamp */
272    raster->clamp_fragment_color = !st->clamp_frag_color_in_shader &&
273                                   ctx->Color._ClampFragmentColor;
274 
275    raster->half_pixel_center = 1;
276    if (st->state.fb_orientation == Y_0_TOP)
277       raster->bottom_edge_rule = 1;
278 
279    /* _NEW_TRANSFORM */
280    if (ctx->Transform.ClipOrigin == GL_UPPER_LEFT)
281       raster->bottom_edge_rule ^= 1;
282 
283    /* ST_NEW_RASTERIZER */
284    raster->rasterizer_discard = ctx->RasterDiscard;
285    if (ctx->TileRasterOrderFixed) {
286       raster->tile_raster_order_fixed = true;
287       raster->tile_raster_order_increasing_x = ctx->TileRasterOrderIncreasingX;
288       raster->tile_raster_order_increasing_y = ctx->TileRasterOrderIncreasingY;
289    }
290 
291    if (ctx->Array._PolygonModeAlwaysCulls) {
292       if (raster->fill_front != PIPE_POLYGON_MODE_FILL)
293          raster->cull_face |= PIPE_FACE_FRONT;
294       if (raster->fill_back != PIPE_POLYGON_MODE_FILL)
295          raster->cull_face |= PIPE_FACE_BACK;
296    }
297 
298    /* Disable two-sided colors if back faces are culled. */
299    if (raster->cull_face & PIPE_FACE_BACK)
300       raster->light_twoside = 0;
301 
302    /* _NEW_TRANSFORM */
303    raster->depth_clip_near = !ctx->Transform.DepthClampNear;
304    raster->depth_clip_far = !ctx->Transform.DepthClampFar;
305    raster->depth_clamp = !raster->depth_clip_far;
306    /* this should be different for GL vs GLES but without NV_depth_buffer_float
307       it doesn't matter, and likely virgl would need fixes to deal with it. */
308    raster->unclamped_fragment_depth_values = false;
309    raster->clip_plane_enable = ctx->Transform.ClipPlanesEnabled;
310    raster->clip_halfz = (ctx->Transform.ClipDepthMode == GL_ZERO_TO_ONE);
311 
312     /* ST_NEW_RASTERIZER */
313    if (ctx->ConservativeRasterization) {
314       if (ctx->ConservativeRasterMode == GL_CONSERVATIVE_RASTER_MODE_POST_SNAP_NV)
315          raster->conservative_raster_mode = PIPE_CONSERVATIVE_RASTER_POST_SNAP;
316       else
317          raster->conservative_raster_mode = PIPE_CONSERVATIVE_RASTER_PRE_SNAP;
318    } else if (ctx->IntelConservativeRasterization) {
319       raster->conservative_raster_mode = PIPE_CONSERVATIVE_RASTER_POST_SNAP;
320    } else {
321       raster->conservative_raster_mode = PIPE_CONSERVATIVE_RASTER_OFF;
322    }
323 
324    raster->conservative_raster_dilate = ctx->ConservativeRasterDilate;
325 
326    raster->subpixel_precision_x = ctx->SubpixelPrecisionBias[0];
327    raster->subpixel_precision_y = ctx->SubpixelPrecisionBias[1];
328 
329    cso_set_rasterizer(st->cso_context, raster);
330 }
331