st_atom_rasterizer.c revision 47e3896dfd89a26abbe4ca2469c2480f3982b204
1/**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 <keith@tungstengraphics.com>
31  */
32
33#include "main/macros.h"
34#include "st_context.h"
35#include "st_atom.h"
36#include "pipe/p_context.h"
37#include "pipe/p_defines.h"
38#include "cso_cache/cso_context.h"
39
40
41static GLuint translate_fill( GLenum mode )
42{
43   switch (mode) {
44   case GL_POINT:
45      return PIPE_POLYGON_MODE_POINT;
46   case GL_LINE:
47      return PIPE_POLYGON_MODE_LINE;
48   case GL_FILL:
49      return PIPE_POLYGON_MODE_FILL;
50   default:
51      assert(0);
52      return 0;
53   }
54}
55
56
57
58static void update_raster_state( struct st_context *st )
59{
60   struct gl_context *ctx = st->ctx;
61   struct pipe_rasterizer_state *raster = &st->state.rasterizer;
62   const struct gl_vertex_program *vertProg = ctx->VertexProgram._Current;
63   const struct gl_fragment_program *fragProg = ctx->FragmentProgram._Current;
64   uint i;
65
66   memset(raster, 0, sizeof(*raster));
67
68   /* _NEW_POLYGON, _NEW_BUFFERS
69    */
70   {
71      raster->front_ccw = (ctx->Polygon.FrontFace == GL_CCW);
72
73      /*
74       * Gallium's surfaces are Y=0=TOP orientation.  OpenGL is the
75       * opposite.  Window system surfaces are Y=0=TOP.  Mesa's FBOs
76       * must match OpenGL conventions so FBOs use Y=0=BOTTOM.  In that
77       * case, we must invert Y and flip the notion of front vs. back.
78       */
79      if (st_fb_orientation(ctx->DrawBuffer) == Y_0_BOTTOM) {
80         /* Drawing to an FBO.  The viewport will be inverted. */
81         raster->front_ccw ^= 1;
82      }
83   }
84
85   /* _NEW_LIGHT
86    */
87   if (ctx->Light.ShadeModel == GL_FLAT)
88      raster->flatshade = 1;
89
90   if (ctx->Light.ProvokingVertex == GL_FIRST_VERTEX_CONVENTION_EXT)
91      raster->flatshade_first = 1;
92
93   /* _NEW_LIGHT | _NEW_PROGRAM
94    *
95    * Back-face colors can come from traditional lighting (when
96    * GL_LIGHT_MODEL_TWO_SIDE is set) or from vertex programs/shaders (when
97    * GL_VERTEX_PROGRAM_TWO_SIDE is set).  Note the logic here.
98    */
99   if (ctx->VertexProgram._Current) {
100      if (ctx->VertexProgram._Enabled ||
101          (ctx->Shader.CurrentVertexProgram &&
102           ctx->Shader.CurrentVertexProgram->LinkStatus)) {
103         /* user-defined vertex program or shader */
104         raster->light_twoside = ctx->VertexProgram.TwoSideEnabled;
105      }
106      else {
107         /* TNL-generated program */
108         raster->light_twoside = ctx->Light.Enabled && ctx->Light.Model.TwoSide;
109      }
110   }
111   else if (ctx->Light.Enabled && ctx->Light.Model.TwoSide) {
112      raster->light_twoside = 1;
113   }
114
115   raster->clamp_vertex_color = ctx->Light._ClampVertexColor;
116
117   /* _NEW_POLYGON
118    */
119   if (ctx->Polygon.CullFlag) {
120      switch (ctx->Polygon.CullFaceMode) {
121      case GL_FRONT:
122	 raster->cull_face = PIPE_FACE_FRONT;
123         break;
124      case GL_BACK:
125	 raster->cull_face = PIPE_FACE_BACK;
126         break;
127      case GL_FRONT_AND_BACK:
128	 raster->cull_face = PIPE_FACE_FRONT_AND_BACK;
129         break;
130      }
131   }
132   else {
133      raster->cull_face = PIPE_FACE_NONE;
134   }
135
136   /* _NEW_POLYGON
137    */
138   {
139      raster->fill_front = translate_fill( ctx->Polygon.FrontMode );
140      raster->fill_back = translate_fill( ctx->Polygon.BackMode );
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.OffsetUnits != 0.0 ||
156       ctx->Polygon.OffsetFactor != 0.0) {
157      raster->offset_point = ctx->Polygon.OffsetPoint;
158      raster->offset_line = ctx->Polygon.OffsetLine;
159      raster->offset_tri = ctx->Polygon.OffsetFill;
160   }
161
162   if (ctx->Polygon.OffsetPoint ||
163       ctx->Polygon.OffsetLine ||
164       ctx->Polygon.OffsetFill) {
165      raster->offset_units = ctx->Polygon.OffsetUnits;
166      raster->offset_scale = ctx->Polygon.OffsetFactor;
167   }
168
169   if (ctx->Polygon.SmoothFlag)
170      raster->poly_smooth = 1;
171
172   if (ctx->Polygon.StippleFlag)
173      raster->poly_stipple_enable = 1;
174
175   /* _NEW_POINT
176    */
177   raster->point_size = ctx->Point.Size;
178
179   if (!ctx->Point.PointSprite && ctx->Point.SmoothFlag)
180      raster->point_smooth = 1;
181
182   /* _NEW_POINT | _NEW_PROGRAM
183    */
184   if (ctx->Point.PointSprite) {
185      /* origin */
186      if ((ctx->Point.SpriteOrigin == GL_UPPER_LEFT) ^
187          (st_fb_orientation(ctx->DrawBuffer) == Y_0_BOTTOM))
188         raster->sprite_coord_mode = PIPE_SPRITE_COORD_UPPER_LEFT;
189      else
190         raster->sprite_coord_mode = PIPE_SPRITE_COORD_LOWER_LEFT;
191
192      /* Coord replacement flags.  If bit 'k' is set that means
193       * that we need to replace GENERIC[k] attrib with an automatically
194       * computed texture coord.
195       */
196      for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++) {
197         if (ctx->Point.CoordReplace[i]) {
198            raster->sprite_coord_enable |= 1 << i;
199         }
200      }
201      if (fragProg->Base.InputsRead & FRAG_BIT_PNTC) {
202         raster->sprite_coord_enable |=
203            1 << (FRAG_ATTRIB_PNTC - FRAG_ATTRIB_TEX0);
204      }
205
206      raster->point_quad_rasterization = 1;
207   }
208
209   /* ST_NEW_VERTEX_PROGRAM
210    */
211   if (vertProg) {
212      if (vertProg->Base.Id == 0) {
213         if (vertProg->Base.OutputsWritten & BITFIELD64_BIT(VERT_RESULT_PSIZ)) {
214            /* generated program which emits point size */
215            raster->point_size_per_vertex = TRUE;
216         }
217      }
218      else if (ctx->VertexProgram.PointSizeEnabled) {
219         /* user-defined program and GL_VERTEX_PROGRAM_POINT_SIZE set */
220         raster->point_size_per_vertex = ctx->VertexProgram.PointSizeEnabled;
221      }
222   }
223   if (!raster->point_size_per_vertex) {
224      /* clamp size now */
225      raster->point_size = CLAMP(ctx->Point.Size,
226                                 ctx->Point.MinSize,
227                                 ctx->Point.MaxSize);
228   }
229
230   /* _NEW_LINE
231    */
232   raster->line_smooth = ctx->Line.SmoothFlag;
233   if (ctx->Line.SmoothFlag) {
234      raster->line_width = CLAMP(ctx->Line.Width,
235                                ctx->Const.MinLineWidthAA,
236                                ctx->Const.MaxLineWidthAA);
237   }
238   else {
239      raster->line_width = CLAMP(ctx->Line.Width,
240                                ctx->Const.MinLineWidth,
241                                ctx->Const.MaxLineWidth);
242   }
243
244   raster->line_stipple_enable = ctx->Line.StippleFlag;
245   raster->line_stipple_pattern = ctx->Line.StipplePattern;
246   /* GL stipple factor is in [1,256], remap to [0, 255] here */
247   raster->line_stipple_factor = ctx->Line.StippleFactor - 1;
248
249   /* _NEW_MULTISAMPLE */
250   if (ctx->Multisample._Enabled || st->force_msaa)
251      raster->multisample = 1;
252
253   /* _NEW_SCISSOR */
254   if (ctx->Scissor.Enabled)
255      raster->scissor = 1;
256
257   /* _NEW_FRAG_CLAMP */
258   raster->clamp_fragment_color = ctx->Color._ClampFragmentColor;
259
260   raster->gl_rasterization_rules = 1;
261
262   cso_set_rasterizer(st->cso_context, raster);
263}
264
265const struct st_tracked_state st_update_rasterizer = {
266   "st_update_rasterizer",    /* name */
267   {
268      (_NEW_BUFFERS |
269       _NEW_LIGHT |
270       _NEW_LINE |
271       _NEW_MULTISAMPLE |
272       _NEW_POINT |
273       _NEW_POLYGON |
274       _NEW_PROGRAM |
275       _NEW_SCISSOR |
276       _NEW_FRAG_CLAMP),      /* mesa state dependencies*/
277      ST_NEW_VERTEX_PROGRAM,  /* state tracker dependencies */
278   },
279   update_raster_state     /* update function */
280};
281