• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "pipe/p_context.h"
41 #include "pipe/p_defines.h"
42 #include "cso_cache/cso_context.h"
43 
44 
45 static GLuint
translate_fill(GLenum mode)46 translate_fill(GLenum mode)
47 {
48    switch (mode) {
49    case GL_POINT:
50       return PIPE_POLYGON_MODE_POINT;
51    case GL_LINE:
52       return PIPE_POLYGON_MODE_LINE;
53    case GL_FILL:
54       return PIPE_POLYGON_MODE_FILL;
55    case GL_FILL_RECTANGLE_NV:
56       return PIPE_POLYGON_MODE_FILL_RECTANGLE;
57    default:
58       assert(0);
59       return 0;
60    }
61 }
62 
63 
64 void
st_update_rasterizer(struct st_context * st)65 st_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 *vertProg = ctx->VertexProgram._Current;
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
97     */
98    raster->flatshade = ctx->Light.ShadeModel == GL_FLAT;
99 
100    raster->flatshade_first = ctx->Light.ProvokingVertex ==
101                              GL_FIRST_VERTEX_CONVENTION_EXT;
102 
103    /* _NEW_LIGHT | _NEW_PROGRAM */
104    raster->light_twoside = _mesa_vertex_program_two_side_enabled(ctx);
105 
106    /*_NEW_LIGHT | _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_smooth = ctx->Polygon.SmoothFlag;
166    raster->poly_stipple_enable = ctx->Polygon.StippleFlag;
167 
168    /* _NEW_POINT
169     */
170    raster->point_size = ctx->Point.Size;
171    raster->point_smooth = !ctx->Point.PointSprite && ctx->Point.SmoothFlag;
172 
173    /* _NEW_POINT | _NEW_PROGRAM
174     */
175    if (ctx->Point.PointSprite) {
176       /* origin */
177       if ((ctx->Point.SpriteOrigin == GL_UPPER_LEFT) ^
178           (st->state.fb_orientation == Y_0_BOTTOM))
179          raster->sprite_coord_mode = PIPE_SPRITE_COORD_UPPER_LEFT;
180       else
181          raster->sprite_coord_mode = PIPE_SPRITE_COORD_LOWER_LEFT;
182 
183       /* Coord replacement flags.  If bit 'k' is set that means
184        * that we need to replace GENERIC[k] attrib with an automatically
185        * computed texture coord.
186        */
187       raster->sprite_coord_enable = ctx->Point.CoordReplace &
188          ((1u << MAX_TEXTURE_COORD_UNITS) - 1);
189       if (!st->needs_texcoord_semantic &&
190           fragProg->info.inputs_read & VARYING_BIT_PNTC) {
191          raster->sprite_coord_enable |=
192             1 << st_get_generic_varying_index(st, VARYING_SLOT_PNTC);
193       }
194 
195       raster->point_quad_rasterization = 1;
196    }
197 
198    /* ST_NEW_VERTEX_PROGRAM
199     */
200    if (vertProg) {
201       if (vertProg->Id == 0) {
202          if (vertProg->info.outputs_written &
203              BITFIELD64_BIT(VARYING_SLOT_PSIZ)) {
204             /* generated program which emits point size */
205             raster->point_size_per_vertex = TRUE;
206          }
207       }
208       else if (ctx->API != API_OPENGLES2) {
209          /* PointSizeEnabled is always set in ES2 contexts */
210          raster->point_size_per_vertex = ctx->VertexProgram.PointSizeEnabled;
211       }
212       else {
213          /* ST_NEW_TESSEVAL_PROGRAM | ST_NEW_GEOMETRY_PROGRAM */
214          /* We have to check the last bound stage and see if it writes psize */
215          struct gl_program *last = NULL;
216          if (ctx->GeometryProgram._Current)
217             last = ctx->GeometryProgram._Current;
218          else if (ctx->TessEvalProgram._Current)
219             last = ctx->TessEvalProgram._Current;
220          else if (ctx->VertexProgram._Current)
221             last = ctx->VertexProgram._Current;
222          if (last)
223             raster->point_size_per_vertex =
224                !!(last->info.outputs_written &
225                   BITFIELD64_BIT(VARYING_SLOT_PSIZ));
226       }
227    }
228    if (!raster->point_size_per_vertex) {
229       /* clamp size now */
230       raster->point_size = CLAMP(ctx->Point.Size,
231                                  ctx->Point.MinSize,
232                                  ctx->Point.MaxSize);
233    }
234 
235    /* _NEW_LINE
236     */
237    raster->line_smooth = ctx->Line.SmoothFlag;
238    if (ctx->Line.SmoothFlag) {
239       raster->line_width = CLAMP(ctx->Line.Width,
240                                  ctx->Const.MinLineWidthAA,
241                                  ctx->Const.MaxLineWidthAA);
242    }
243    else {
244       raster->line_width = CLAMP(ctx->Line.Width,
245                                  ctx->Const.MinLineWidth,
246                                  ctx->Const.MaxLineWidth);
247    }
248 
249    raster->line_stipple_enable = ctx->Line.StippleFlag;
250    raster->line_stipple_pattern = ctx->Line.StipplePattern;
251    /* GL stipple factor is in [1,256], remap to [0, 255] here */
252    raster->line_stipple_factor = ctx->Line.StippleFactor - 1;
253 
254    /* _NEW_MULTISAMPLE */
255    raster->multisample = _mesa_is_multisample_enabled(ctx);
256 
257    /* _NEW_MULTISAMPLE | _NEW_BUFFERS */
258    raster->force_persample_interp =
259          !st->force_persample_in_shader &&
260          raster->multisample &&
261          ctx->Multisample.SampleShading &&
262          ctx->Multisample.MinSampleShadingValue *
263          _mesa_geometric_samples(ctx->DrawBuffer) > 1;
264 
265    /* _NEW_SCISSOR */
266    raster->scissor = !!ctx->Scissor.EnableFlags;
267 
268    /* _NEW_FRAG_CLAMP */
269    raster->clamp_fragment_color = !st->clamp_frag_color_in_shader &&
270                                   ctx->Color._ClampFragmentColor;
271 
272    raster->half_pixel_center = 1;
273    if (st->state.fb_orientation == Y_0_TOP)
274       raster->bottom_edge_rule = 1;
275 
276    /* _NEW_TRANSFORM */
277    if (ctx->Transform.ClipOrigin == GL_UPPER_LEFT)
278       raster->bottom_edge_rule ^= 1;
279 
280    /* ST_NEW_RASTERIZER */
281    raster->rasterizer_discard = ctx->RasterDiscard;
282    if (ctx->TileRasterOrderFixed) {
283       raster->tile_raster_order_fixed = true;
284       raster->tile_raster_order_increasing_x = ctx->TileRasterOrderIncreasingX;
285       raster->tile_raster_order_increasing_y = ctx->TileRasterOrderIncreasingY;
286    }
287 
288    if (st->edgeflag_culls_prims) {
289       /* All edge flags are FALSE. Cull the affected faces. */
290       if (raster->fill_front != PIPE_POLYGON_MODE_FILL)
291          raster->cull_face |= PIPE_FACE_FRONT;
292       if (raster->fill_back != PIPE_POLYGON_MODE_FILL)
293          raster->cull_face |= PIPE_FACE_BACK;
294    }
295 
296    /* _NEW_TRANSFORM */
297    raster->depth_clip = !ctx->Transform.DepthClamp;
298    raster->clip_plane_enable = ctx->Transform.ClipPlanesEnabled;
299    raster->clip_halfz = (ctx->Transform.ClipDepthMode == GL_ZERO_TO_ONE);
300 
301    cso_set_rasterizer(st->cso_context, raster);
302 }
303