0f01e9939de21a625babfec1ecb8e4600eee51a5
[mesa.git] / src / mesa / state_tracker / st_atom_rasterizer.c
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 "st_context.h"
35 #include "st_atom.h"
36 #include "st_debug.h"
37 #include "st_program.h"
38 #include "pipe/p_context.h"
39 #include "pipe/p_defines.h"
40 #include "cso_cache/cso_context.h"
41
42
43 static GLuint translate_fill( GLenum mode )
44 {
45 switch (mode) {
46 case GL_POINT:
47 return PIPE_POLYGON_MODE_POINT;
48 case GL_LINE:
49 return PIPE_POLYGON_MODE_LINE;
50 case GL_FILL:
51 return PIPE_POLYGON_MODE_FILL;
52 default:
53 assert(0);
54 return 0;
55 }
56 }
57
58
59
60 static void update_raster_state( struct st_context *st )
61 {
62 struct gl_context *ctx = st->ctx;
63 struct pipe_rasterizer_state *raster = &st->state.rasterizer;
64 const struct gl_vertex_program *vertProg = ctx->VertexProgram._Current;
65 const struct gl_fragment_program *fragProg = ctx->FragmentProgram._Current;
66 uint i;
67
68 memset(raster, 0, sizeof(*raster));
69
70 /* _NEW_POLYGON, _NEW_BUFFERS
71 */
72 {
73 raster->front_ccw = (ctx->Polygon.FrontFace == GL_CCW);
74
75 /* _NEW_TRANSFORM */
76 if (ctx->Transform.ClipOrigin == GL_UPPER_LEFT) {
77 raster->front_ccw ^= 1;
78 }
79
80 /*
81 * Gallium's surfaces are Y=0=TOP orientation. OpenGL is the
82 * opposite. Window system surfaces are Y=0=TOP. Mesa's FBOs
83 * must match OpenGL conventions so FBOs use Y=0=BOTTOM. In that
84 * case, we must invert Y and flip the notion of front vs. back.
85 */
86 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_BOTTOM) {
87 /* Drawing to an FBO. The viewport will be inverted. */
88 raster->front_ccw ^= 1;
89 }
90 }
91
92 /* _NEW_LIGHT
93 */
94 raster->flatshade = ctx->Light.ShadeModel == GL_FLAT;
95
96 raster->flatshade_first = ctx->Light.ProvokingVertex ==
97 GL_FIRST_VERTEX_CONVENTION_EXT;
98
99 /* _NEW_LIGHT | _NEW_PROGRAM */
100 raster->light_twoside = ctx->VertexProgram._TwoSideEnabled;
101
102 /*_NEW_LIGHT | _NEW_BUFFERS */
103 raster->clamp_vertex_color = !st->clamp_vert_color_in_shader &&
104 ctx->Light._ClampVertexColor;
105
106 /* _NEW_POLYGON
107 */
108 if (ctx->Polygon.CullFlag) {
109 switch (ctx->Polygon.CullFaceMode) {
110 case GL_FRONT:
111 raster->cull_face = PIPE_FACE_FRONT;
112 break;
113 case GL_BACK:
114 raster->cull_face = PIPE_FACE_BACK;
115 break;
116 case GL_FRONT_AND_BACK:
117 raster->cull_face = PIPE_FACE_FRONT_AND_BACK;
118 break;
119 }
120 }
121 else {
122 raster->cull_face = PIPE_FACE_NONE;
123 }
124
125 /* _NEW_POLYGON
126 */
127 {
128 if (ST_DEBUG & DEBUG_WIREFRAME) {
129 raster->fill_front = PIPE_POLYGON_MODE_LINE;
130 raster->fill_back = PIPE_POLYGON_MODE_LINE;
131 }
132 else {
133 raster->fill_front = translate_fill( ctx->Polygon.FrontMode );
134 raster->fill_back = translate_fill( ctx->Polygon.BackMode );
135 }
136
137 /* Simplify when culling is active:
138 */
139 if (raster->cull_face & PIPE_FACE_FRONT) {
140 raster->fill_front = raster->fill_back;
141 }
142
143 if (raster->cull_face & PIPE_FACE_BACK) {
144 raster->fill_back = raster->fill_front;
145 }
146 }
147
148 /* _NEW_POLYGON
149 */
150 if (ctx->Polygon.OffsetPoint ||
151 ctx->Polygon.OffsetLine ||
152 ctx->Polygon.OffsetFill) {
153 raster->offset_point = ctx->Polygon.OffsetPoint;
154 raster->offset_line = ctx->Polygon.OffsetLine;
155 raster->offset_tri = ctx->Polygon.OffsetFill;
156 raster->offset_units = ctx->Polygon.OffsetUnits;
157 raster->offset_scale = ctx->Polygon.OffsetFactor;
158 raster->offset_clamp = ctx->Polygon.OffsetClamp;
159 }
160
161 raster->poly_smooth = ctx->Polygon.SmoothFlag;
162 raster->poly_stipple_enable = ctx->Polygon.StippleFlag;
163
164 /* _NEW_POINT
165 */
166 raster->point_size = ctx->Point.Size;
167 raster->point_smooth = !ctx->Point.PointSprite && ctx->Point.SmoothFlag;
168
169 /* _NEW_POINT | _NEW_PROGRAM
170 */
171 if (ctx->Point.PointSprite) {
172 /* origin */
173 if ((ctx->Point.SpriteOrigin == GL_UPPER_LEFT) ^
174 (st_fb_orientation(ctx->DrawBuffer) == Y_0_BOTTOM))
175 raster->sprite_coord_mode = PIPE_SPRITE_COORD_UPPER_LEFT;
176 else
177 raster->sprite_coord_mode = PIPE_SPRITE_COORD_LOWER_LEFT;
178
179 /* Coord replacement flags. If bit 'k' is set that means
180 * that we need to replace GENERIC[k] attrib with an automatically
181 * computed texture coord.
182 */
183 for (i = 0; i < MAX_TEXTURE_COORD_UNITS; i++) {
184 if (ctx->Point.CoordReplace[i]) {
185 raster->sprite_coord_enable |= 1 << i;
186 }
187 }
188 if (!st->needs_texcoord_semantic &&
189 fragProg->Base.InputsRead & VARYING_BIT_PNTC) {
190 raster->sprite_coord_enable |=
191 1 << st_get_generic_varying_index(st, VARYING_SLOT_PNTC);
192 }
193
194 raster->point_quad_rasterization = 1;
195 }
196
197 /* ST_NEW_VERTEX_PROGRAM
198 */
199 if (vertProg) {
200 if (vertProg->Base.Id == 0) {
201 if (vertProg->Base.OutputsWritten & BITFIELD64_BIT(VARYING_SLOT_PSIZ)) {
202 /* generated program which emits point size */
203 raster->point_size_per_vertex = TRUE;
204 }
205 }
206 else if (ctx->VertexProgram.PointSizeEnabled) {
207 /* user-defined program and GL_VERTEX_PROGRAM_POINT_SIZE set */
208 raster->point_size_per_vertex = ctx->VertexProgram.PointSizeEnabled;
209 }
210 }
211 if (!raster->point_size_per_vertex) {
212 /* clamp size now */
213 raster->point_size = CLAMP(ctx->Point.Size,
214 ctx->Point.MinSize,
215 ctx->Point.MaxSize);
216 }
217
218 /* _NEW_LINE
219 */
220 raster->line_smooth = ctx->Line.SmoothFlag;
221 if (ctx->Line.SmoothFlag) {
222 raster->line_width = CLAMP(ctx->Line.Width,
223 ctx->Const.MinLineWidthAA,
224 ctx->Const.MaxLineWidthAA);
225 }
226 else {
227 raster->line_width = CLAMP(ctx->Line.Width,
228 ctx->Const.MinLineWidth,
229 ctx->Const.MaxLineWidth);
230 }
231
232 raster->line_stipple_enable = ctx->Line.StippleFlag;
233 raster->line_stipple_pattern = ctx->Line.StipplePattern;
234 /* GL stipple factor is in [1,256], remap to [0, 255] here */
235 raster->line_stipple_factor = ctx->Line.StippleFactor - 1;
236
237 /* _NEW_MULTISAMPLE */
238 raster->multisample = ctx->Multisample._Enabled;
239
240 /* _NEW_MULTISAMPLE | _NEW_BUFFERS */
241 raster->force_persample_interp =
242 st->can_force_persample_interp &&
243 ctx->Multisample._Enabled &&
244 ctx->Multisample.SampleShading &&
245 ctx->Multisample.MinSampleShadingValue *
246 ctx->DrawBuffer->Visual.samples > 1;
247
248 /* _NEW_SCISSOR */
249 raster->scissor = ctx->Scissor.EnableFlags;
250
251 /* _NEW_FRAG_CLAMP */
252 raster->clamp_fragment_color = !st->clamp_frag_color_in_shader &&
253 ctx->Color._ClampFragmentColor;
254
255 raster->half_pixel_center = 1;
256 if (st_fb_orientation(ctx->DrawBuffer) == Y_0_TOP)
257 raster->bottom_edge_rule = 1;
258 /* _NEW_TRANSFORM */
259 if (ctx->Transform.ClipOrigin == GL_UPPER_LEFT)
260 raster->bottom_edge_rule ^= 1;
261
262 /* ST_NEW_RASTERIZER */
263 raster->rasterizer_discard = ctx->RasterDiscard;
264
265 if (st->edgeflag_culls_prims) {
266 /* All edge flags are FALSE. Cull the affected faces. */
267 if (raster->fill_front != PIPE_POLYGON_MODE_FILL)
268 raster->cull_face |= PIPE_FACE_FRONT;
269 if (raster->fill_back != PIPE_POLYGON_MODE_FILL)
270 raster->cull_face |= PIPE_FACE_BACK;
271 }
272
273 /* _NEW_TRANSFORM */
274 raster->depth_clip = !ctx->Transform.DepthClamp;
275 raster->clip_plane_enable = ctx->Transform.ClipPlanesEnabled;
276 raster->clip_halfz = (ctx->Transform.ClipDepthMode == GL_ZERO_TO_ONE);
277
278 cso_set_rasterizer(st->cso_context, raster);
279 }
280
281 const struct st_tracked_state st_update_rasterizer = {
282 "st_update_rasterizer", /* name */
283 {
284 (_NEW_BUFFERS |
285 _NEW_LIGHT |
286 _NEW_LINE |
287 _NEW_MULTISAMPLE |
288 _NEW_POINT |
289 _NEW_POLYGON |
290 _NEW_PROGRAM |
291 _NEW_SCISSOR |
292 _NEW_FRAG_CLAMP |
293 _NEW_TRANSFORM), /* mesa state dependencies*/
294 (ST_NEW_VERTEX_PROGRAM |
295 ST_NEW_RASTERIZER), /* state tracker dependencies */
296 },
297 update_raster_state /* update function */
298 };