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
64
65void
66st_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 *vertProg = ctx->VertexProgram._Current;
71   const struct gl_program *fragProg = ctx->FragmentProgram._Current;
72
73   memset(raster, 0, sizeof(*raster));
74
75   /* _NEW_POLYGON, _NEW_BUFFERS
76    */
77   {
78      raster->front_ccw = (ctx->Polygon.FrontFace == GL_CCW);
79
80      /* _NEW_TRANSFORM */
81      if (ctx->Transform.ClipOrigin == GL_UPPER_LEFT) {
82         raster->front_ccw ^= 1;
83      }
84
85      /*
86       * Gallium's surfaces are Y=0=TOP orientation.  OpenGL is the
87       * opposite.  Window system surfaces are Y=0=TOP.  Mesa's FBOs
88       * must match OpenGL conventions so FBOs use Y=0=BOTTOM.  In that
89       * case, we must invert Y and flip the notion of front vs. back.
90       */
91      if (st->state.fb_orientation == Y_0_BOTTOM) {
92         /* Drawing to an FBO.  The viewport will be inverted. */
93         raster->front_ccw ^= 1;
94      }
95   }
96
97   /* _NEW_LIGHT
98    */
99   raster->flatshade = ctx->Light.ShadeModel == GL_FLAT;
100
101   raster->flatshade_first = ctx->Light.ProvokingVertex ==
102                             GL_FIRST_VERTEX_CONVENTION_EXT;
103
104   /* _NEW_LIGHT | _NEW_PROGRAM */
105   raster->light_twoside = _mesa_vertex_program_two_side_enabled(ctx);
106
107   /*_NEW_LIGHT | _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_smooth = ctx->Polygon.SmoothFlag;
167   raster->poly_stipple_enable = ctx->Polygon.StippleFlag;
168
169   /* _NEW_POINT
170    */
171   raster->point_size = ctx->Point.Size;
172   raster->point_smooth = !ctx->Point.PointSprite && ctx->Point.SmoothFlag;
173
174   /* _NEW_POINT | _NEW_PROGRAM
175    */
176   if (ctx->Point.PointSprite) {
177      /* origin */
178      if ((ctx->Point.SpriteOrigin == GL_UPPER_LEFT) ^
179          (st->state.fb_orientation == Y_0_BOTTOM))
180         raster->sprite_coord_mode = PIPE_SPRITE_COORD_UPPER_LEFT;
181      else
182         raster->sprite_coord_mode = PIPE_SPRITE_COORD_LOWER_LEFT;
183
184      /* Coord replacement flags.  If bit 'k' is set that means
185       * that we need to replace GENERIC[k] attrib with an automatically
186       * computed texture coord.
187       */
188      raster->sprite_coord_enable = ctx->Point.CoordReplace &
189         ((1u << MAX_TEXTURE_COORD_UNITS) - 1);
190      if (!st->needs_texcoord_semantic &&
191          fragProg->info.inputs_read & VARYING_BIT_PNTC) {
192         raster->sprite_coord_enable |=
193            1 << st_get_generic_varying_index(st, VARYING_SLOT_PNTC);
194      }
195
196      raster->point_quad_rasterization = 1;
197   }
198
199   /* ST_NEW_VERTEX_PROGRAM
200    */
201   if (vertProg) {
202      if (vertProg->Id == 0) {
203         if (vertProg->info.outputs_written &
204             BITFIELD64_BIT(VARYING_SLOT_PSIZ)) {
205            /* generated program which emits point size */
206            raster->point_size_per_vertex = TRUE;
207         }
208      }
209      else if (ctx->API != API_OPENGLES2) {
210         /* PointSizeEnabled is always set in ES2 contexts */
211         raster->point_size_per_vertex = ctx->VertexProgram.PointSizeEnabled;
212      }
213      else {
214         /* ST_NEW_TESSEVAL_PROGRAM | ST_NEW_GEOMETRY_PROGRAM */
215         /* We have to check the last bound stage and see if it writes psize */
216         struct gl_program *last = NULL;
217         if (ctx->GeometryProgram._Current)
218            last = ctx->GeometryProgram._Current;
219         else if (ctx->TessEvalProgram._Current)
220            last = ctx->TessEvalProgram._Current;
221         else if (ctx->VertexProgram._Current)
222            last = ctx->VertexProgram._Current;
223         if (last)
224            raster->point_size_per_vertex =
225               !!(last->info.outputs_written &
226                  BITFIELD64_BIT(VARYING_SLOT_PSIZ));
227      }
228   }
229   if (!raster->point_size_per_vertex) {
230      /* clamp size now */
231      raster->point_size = CLAMP(ctx->Point.Size,
232                                 ctx->Point.MinSize,
233                                 ctx->Point.MaxSize);
234   }
235
236   /* _NEW_LINE
237    */
238   raster->line_smooth = ctx->Line.SmoothFlag;
239   if (ctx->Line.SmoothFlag) {
240      raster->line_width = CLAMP(ctx->Line.Width,
241                                 ctx->Const.MinLineWidthAA,
242                                 ctx->Const.MaxLineWidthAA);
243   }
244   else {
245      raster->line_width = CLAMP(ctx->Line.Width,
246                                 ctx->Const.MinLineWidth,
247                                 ctx->Const.MaxLineWidth);
248   }
249
250   raster->line_stipple_enable = ctx->Line.StippleFlag;
251   raster->line_stipple_pattern = ctx->Line.StipplePattern;
252   /* GL stipple factor is in [1,256], remap to [0, 255] here */
253   raster->line_stipple_factor = ctx->Line.StippleFactor - 1;
254
255   /* _NEW_MULTISAMPLE */
256   raster->multisample = _mesa_is_multisample_enabled(ctx);
257
258   /* _NEW_MULTISAMPLE | _NEW_BUFFERS */
259   raster->force_persample_interp =
260         !st->force_persample_in_shader &&
261         raster->multisample &&
262         ctx->Multisample.SampleShading &&
263         ctx->Multisample.MinSampleShadingValue *
264         _mesa_geometric_samples(ctx->DrawBuffer) > 1;
265
266   /* _NEW_SCISSOR */
267   raster->scissor = !!ctx->Scissor.EnableFlags;
268
269   /* _NEW_FRAG_CLAMP */
270   raster->clamp_fragment_color = !st->clamp_frag_color_in_shader &&
271                                  ctx->Color._ClampFragmentColor;
272
273   raster->half_pixel_center = 1;
274   if (st->state.fb_orientation == Y_0_TOP)
275      raster->bottom_edge_rule = 1;
276
277   /* _NEW_TRANSFORM */
278   if (ctx->Transform.ClipOrigin == GL_UPPER_LEFT)
279      raster->bottom_edge_rule ^= 1;
280
281   /* ST_NEW_RASTERIZER */
282   raster->rasterizer_discard = ctx->RasterDiscard;
283   if (ctx->TileRasterOrderFixed) {
284      raster->tile_raster_order_fixed = true;
285      raster->tile_raster_order_increasing_x = ctx->TileRasterOrderIncreasingX;
286      raster->tile_raster_order_increasing_y = ctx->TileRasterOrderIncreasingY;
287   }
288
289   if (st->edgeflag_culls_prims) {
290      /* All edge flags are FALSE. Cull the affected faces. */
291      if (raster->fill_front != PIPE_POLYGON_MODE_FILL)
292         raster->cull_face |= PIPE_FACE_FRONT;
293      if (raster->fill_back != PIPE_POLYGON_MODE_FILL)
294         raster->cull_face |= PIPE_FACE_BACK;
295   }
296
297   /* _NEW_TRANSFORM */
298   raster->depth_clip_near = !ctx->Transform.DepthClampNear;
299   raster->depth_clip_far = !ctx->Transform.DepthClampFar;
300   raster->clip_plane_enable = ctx->Transform.ClipPlanesEnabled;
301   raster->clip_halfz = (ctx->Transform.ClipDepthMode == GL_ZERO_TO_ONE);
302
303    /* ST_NEW_RASTERIZER */
304   if (ctx->ConservativeRasterization) {
305      if (ctx->ConservativeRasterMode == GL_CONSERVATIVE_RASTER_MODE_POST_SNAP_NV)
306         raster->conservative_raster_mode = PIPE_CONSERVATIVE_RASTER_POST_SNAP;
307      else
308         raster->conservative_raster_mode = PIPE_CONSERVATIVE_RASTER_PRE_SNAP;
309   } else if (ctx->IntelConservativeRasterization) {
310      raster->conservative_raster_mode = PIPE_CONSERVATIVE_RASTER_POST_SNAP;
311   } else {
312      raster->conservative_raster_mode = PIPE_CONSERVATIVE_RASTER_OFF;
313   }
314
315   raster->conservative_raster_dilate = ctx->ConservativeRasterDilate;
316
317   raster->subpixel_precision_x = ctx->SubpixelPrecisionBias[0];
318   raster->subpixel_precision_y = ctx->SubpixelPrecisionBias[1];
319
320   cso_set_rasterizer(st->cso_context, raster);
321}
322