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 <keithw@vmware.com>
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
45
46static GLuint
47translate_fill(GLenum mode)
48{
49   switch (mode) {
50   case GL_POINT:
51      return PIPE_POLYGON_MODE_POINT;
52   case GL_LINE:
53      return PIPE_POLYGON_MODE_LINE;
54   case GL_FILL:
55      return PIPE_POLYGON_MODE_FILL;
56   case GL_FILL_RECTANGLE_NV:
57      return PIPE_POLYGON_MODE_FILL_RECTANGLE;
58   default:
59      assert(0);
60      return 0;
61   }
62}
63
64void
65st_update_rasterizer(struct st_context *st)
66{
67   struct gl_context *ctx = st->ctx;
68   struct pipe_rasterizer_state *raster = &st->state.rasterizer;
69   const struct gl_program *fragProg = ctx->FragmentProgram._Current;
70
71   memset(raster, 0, sizeof(*raster));
72
73   /* _NEW_POLYGON, _NEW_BUFFERS
74    */
75   {
76      raster->front_ccw = (ctx->Polygon.FrontFace == GL_CCW);
77
78      /* _NEW_TRANSFORM */
79      if (ctx->Transform.ClipOrigin == GL_UPPER_LEFT) {
80         raster->front_ccw ^= 1;
81      }
82
83      /*
84       * Gallium's surfaces are Y=0=TOP orientation.  OpenGL is the
85       * opposite.  Window system surfaces are Y=0=TOP.  Mesa's FBOs
86       * must match OpenGL conventions so FBOs use Y=0=BOTTOM.  In that
87       * case, we must invert Y and flip the notion of front vs. back.
88       */
89      if (st->state.fb_orientation == Y_0_BOTTOM) {
90         /* Drawing to an FBO.  The viewport will be inverted. */
91         raster->front_ccw ^= 1;
92      }
93   }
94
95   /* _NEW_LIGHT_STATE */
96   raster->flatshade = !st->lower_flatshade &&
97                       ctx->Light.ShadeModel == GL_FLAT;
98
99   raster->flatshade_first = ctx->Light.ProvokingVertex ==
100                             GL_FIRST_VERTEX_CONVENTION_EXT;
101
102   /* _NEW_LIGHT_STATE | _NEW_PROGRAM */
103   if (!st->lower_two_sided_color)
104      raster->light_twoside = _mesa_vertex_program_two_side_enabled(ctx);
105
106   /*_NEW_LIGHT_STATE | _NEW_BUFFERS */
107   raster->clamp_vertex_color = !st->clamp_vert_color_in_shader &&
108                                ctx->Light._ClampVertexColor;
109
110   /* _NEW_POLYGON
111    */
112   if (ctx->Polygon.CullFlag) {
113      switch (ctx->Polygon.CullFaceMode) {
114      case GL_FRONT:
115         raster->cull_face = PIPE_FACE_FRONT;
116         break;
117      case GL_BACK:
118         raster->cull_face = PIPE_FACE_BACK;
119         break;
120      case GL_FRONT_AND_BACK:
121         raster->cull_face = PIPE_FACE_FRONT_AND_BACK;
122         break;
123      }
124   }
125   else {
126      raster->cull_face = PIPE_FACE_NONE;
127   }
128
129   /* _NEW_POLYGON
130    */
131   {
132      if (ST_DEBUG & DEBUG_WIREFRAME) {
133         raster->fill_front = PIPE_POLYGON_MODE_LINE;
134         raster->fill_back = PIPE_POLYGON_MODE_LINE;
135      }
136      else {
137         raster->fill_front = translate_fill(ctx->Polygon.FrontMode);
138         raster->fill_back = translate_fill(ctx->Polygon.BackMode);
139      }
140
141      /* Simplify when culling is active:
142       */
143      if (raster->cull_face & PIPE_FACE_FRONT) {
144         raster->fill_front = raster->fill_back;
145      }
146
147      if (raster->cull_face & PIPE_FACE_BACK) {
148         raster->fill_back = raster->fill_front;
149      }
150   }
151
152   /* _NEW_POLYGON
153    */
154   if (ctx->Polygon.OffsetPoint ||
155       ctx->Polygon.OffsetLine ||
156       ctx->Polygon.OffsetFill) {
157      raster->offset_point = ctx->Polygon.OffsetPoint;
158      raster->offset_line = ctx->Polygon.OffsetLine;
159      raster->offset_tri = ctx->Polygon.OffsetFill;
160      raster->offset_units = ctx->Polygon.OffsetUnits;
161      raster->offset_scale = ctx->Polygon.OffsetFactor;
162      raster->offset_clamp = ctx->Polygon.OffsetClamp;
163   }
164
165   raster->poly_stipple_enable = ctx->Polygon.StippleFlag;
166
167   /* Multisampling disables point, line, and polygon smoothing.
168    *
169    * GL_ARB_multisample says:
170    *
171    *   "If MULTISAMPLE_ARB is enabled, and SAMPLE_BUFFERS_ARB is a value of
172    *    one, then points are rasterized using the following algorithm,
173    *    regardless of whether point antialiasing (POINT_SMOOTH) is enabled"
174    *
175    *   "If MULTISAMPLE_ARB is enabled, and SAMPLE_BUFFERS_ARB is a value of
176    *    one, then lines are rasterized using the following algorithm,
177    *    regardless of whether line antialiasing (LINE_SMOOTH) is enabled"
178    *
179    *   "If MULTISAMPLE_ARB is enabled, and SAMPLE_BUFFERS_ARB is a value of
180    *    one, then polygons are rasterized using the following algorithm,
181    *    regardless of whether polygon antialiasing (POLYGON_SMOOTH) is
182    *    enabled"
183    */
184
185   /* _NEW_MULTISAMPLE */
186   bool multisample = _mesa_is_multisample_enabled(ctx);
187   raster->multisample = multisample;
188
189   /* _NEW_POLYGON | _NEW_MULTISAMPLE */
190   raster->poly_smooth = !multisample && ctx->Polygon.SmoothFlag;
191
192   /* _NEW_POINT
193    */
194   raster->point_size = ctx->Point.Size;
195
196   /* _NEW_POINT | _NEW_MULTISAMPLE */
197   raster->point_smooth = !multisample && !ctx->Point.PointSprite &&
198                          ctx->Point.SmoothFlag;
199
200   /* _NEW_POINT | _NEW_PROGRAM
201    */
202   if (ctx->Point.PointSprite) {
203      /* origin */
204      if ((ctx->Point.SpriteOrigin == GL_UPPER_LEFT) ^
205          (st->state.fb_orientation == Y_0_BOTTOM))
206         raster->sprite_coord_mode = PIPE_SPRITE_COORD_UPPER_LEFT;
207      else
208         raster->sprite_coord_mode = PIPE_SPRITE_COORD_LOWER_LEFT;
209
210      /* Coord replacement flags.  If bit 'k' is set that means
211       * that we need to replace GENERIC[k] attrib with an automatically
212       * computed texture coord.
213       */
214      raster->sprite_coord_enable = ctx->Point.CoordReplace &
215         ((1u << MAX_TEXTURE_COORD_UNITS) - 1);
216      if (!st->needs_texcoord_semantic &&
217          fragProg->info.inputs_read & VARYING_BIT_PNTC) {
218         raster->sprite_coord_enable |=
219            1 << st_get_generic_varying_index(st, VARYING_SLOT_PNTC);
220      }
221
222      raster->point_quad_rasterization = 1;
223
224      raster->point_tri_clip = st->ctx->API == API_OPENGLES2;
225   }
226
227   /* ST_NEW_VERTEX_PROGRAM
228    */
229   raster->point_size_per_vertex = st_point_size_per_vertex(ctx);
230   if (!raster->point_size_per_vertex) {
231      /* clamp size now */
232      raster->point_size = CLAMP(ctx->Point.Size,
233                                 ctx->Point.MinSize,
234                                 ctx->Point.MaxSize);
235   }
236
237   /* _NEW_LINE | _NEW_MULTISAMPLE
238    */
239   if (!multisample && ctx->Line.SmoothFlag) {
240      raster->line_smooth = 1;
241      raster->line_width = CLAMP(ctx->Line.Width,
242                                 ctx->Const.MinLineWidthAA,
243                                 ctx->Const.MaxLineWidthAA);
244   }
245   else {
246      raster->line_width = CLAMP(ctx->Line.Width,
247                                 ctx->Const.MinLineWidth,
248                                 ctx->Const.MaxLineWidth);
249   }
250
251   raster->line_rectangular = multisample || ctx->Line.SmoothFlag;
252
253   /* When the pattern is all 1's, it means line stippling is disabled */
254   raster->line_stipple_enable = ctx->Line.StippleFlag && ctx->Line.StipplePattern != 0xffff;
255   raster->line_stipple_pattern = ctx->Line.StipplePattern;
256   /* GL stipple factor is in [1,256], remap to [0, 255] here */
257   raster->line_stipple_factor = ctx->Line.StippleFactor - 1;
258
259   /* _NEW_MULTISAMPLE | _NEW_BUFFERS */
260   raster->force_persample_interp =
261         !st->force_persample_in_shader &&
262         raster->multisample &&
263         ctx->Multisample.SampleShading &&
264         ctx->Multisample.MinSampleShadingValue *
265         _mesa_geometric_samples(ctx->DrawBuffer) > 1;
266
267   /* _NEW_SCISSOR */
268   raster->scissor = !!ctx->Scissor.EnableFlags;
269
270   /* gl_driver_flags::NewFragClamp */
271   raster->clamp_fragment_color = !st->clamp_frag_color_in_shader &&
272                                  ctx->Color._ClampFragmentColor;
273
274   raster->half_pixel_center = 1;
275   if (st->state.fb_orientation == Y_0_TOP)
276      raster->bottom_edge_rule = 1;
277
278   /* _NEW_TRANSFORM */
279   if (ctx->Transform.ClipOrigin == GL_UPPER_LEFT)
280      raster->bottom_edge_rule ^= 1;
281
282   /* ST_NEW_RASTERIZER */
283   raster->rasterizer_discard = ctx->RasterDiscard;
284   if (ctx->TileRasterOrderFixed) {
285      raster->tile_raster_order_fixed = true;
286      raster->tile_raster_order_increasing_x = ctx->TileRasterOrderIncreasingX;
287      raster->tile_raster_order_increasing_y = ctx->TileRasterOrderIncreasingY;
288   }
289
290   if (st->edgeflag_culls_prims) {
291      /* All edge flags are FALSE. Cull the affected faces. */
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   /* _NEW_TRANSFORM */
299   raster->depth_clip_near = !ctx->Transform.DepthClampNear;
300   raster->depth_clip_far = !ctx->Transform.DepthClampFar;
301   raster->depth_clamp = !raster->depth_clip_far;
302   /* this should be different for GL vs GLES but without NV_depth_buffer_float
303      it doesn't matter, and likely virgl would need fixes to deal with it. */
304   raster->unclamped_fragment_depth_values = false;
305   raster->clip_plane_enable = ctx->Transform.ClipPlanesEnabled;
306   raster->clip_halfz = (ctx->Transform.ClipDepthMode == GL_ZERO_TO_ONE);
307
308    /* ST_NEW_RASTERIZER */
309   if (ctx->ConservativeRasterization) {
310      if (ctx->ConservativeRasterMode == GL_CONSERVATIVE_RASTER_MODE_POST_SNAP_NV)
311         raster->conservative_raster_mode = PIPE_CONSERVATIVE_RASTER_POST_SNAP;
312      else
313         raster->conservative_raster_mode = PIPE_CONSERVATIVE_RASTER_PRE_SNAP;
314   } else if (ctx->IntelConservativeRasterization) {
315      raster->conservative_raster_mode = PIPE_CONSERVATIVE_RASTER_POST_SNAP;
316   } else {
317      raster->conservative_raster_mode = PIPE_CONSERVATIVE_RASTER_OFF;
318   }
319
320   raster->conservative_raster_dilate = ctx->ConservativeRasterDilate;
321
322   raster->subpixel_precision_x = ctx->SubpixelPrecisionBias[0];
323   raster->subpixel_precision_y = ctx->SubpixelPrecisionBias[1];
324
325   cso_set_rasterizer(st->cso_context, raster);
326}
327