svga: Don't use the draw pipeline for non-AA lines with a fractional width.
[mesa.git] / src / gallium / drivers / svga / svga_pipe_rasterizer.c
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #include "draw/draw_context.h"
27 #include "util/u_inlines.h"
28 #include "pipe/p_defines.h"
29 #include "util/u_math.h"
30 #include "util/u_memory.h"
31
32 #include "svga_context.h"
33
34 #include "svga_hw_reg.h"
35
36 /* Hardware frontwinding is always set up as SVGA3D_FRONTWINDING_CW.
37 */
38 static SVGA3dFace svga_translate_cullmode( unsigned mode,
39 unsigned front_ccw )
40 {
41 const int hw_front_ccw = 0; /* hardware is always CW */
42 switch (mode) {
43 case PIPE_FACE_NONE:
44 return SVGA3D_FACE_NONE;
45 case PIPE_FACE_FRONT:
46 return front_ccw == hw_front_ccw ? SVGA3D_FACE_FRONT : SVGA3D_FACE_BACK;
47 case PIPE_FACE_BACK:
48 return front_ccw == hw_front_ccw ? SVGA3D_FACE_BACK : SVGA3D_FACE_FRONT;
49 case PIPE_FACE_FRONT_AND_BACK:
50 return SVGA3D_FACE_FRONT_BACK;
51 default:
52 assert(0);
53 return SVGA3D_FACE_NONE;
54 }
55 }
56
57 static SVGA3dShadeMode svga_translate_flatshade( unsigned mode )
58 {
59 return mode ? SVGA3D_SHADEMODE_FLAT : SVGA3D_SHADEMODE_SMOOTH;
60 }
61
62
63 static void *
64 svga_create_rasterizer_state(struct pipe_context *pipe,
65 const struct pipe_rasterizer_state *templ)
66 {
67 struct svga_context *svga = svga_context(pipe);
68 struct svga_rasterizer_state *rast = CALLOC_STRUCT( svga_rasterizer_state );
69
70 /* need this for draw module. */
71 rast->templ = *templ;
72
73 /* light_twoside - XXX: need fragment shader variant */
74 /* poly_smooth - XXX: no fallback available */
75 /* poly_stipple_enable - draw module */
76 /* sprite_coord_enable - ? */
77 /* point_quad_rasterization - ? */
78 /* point_size_per_vertex - ? */
79 /* sprite_coord_mode - ??? */
80 /* bypass_vs_viewport_and_clip - handled by viewport setup */
81 /* flatshade_first - handled by index translation */
82 /* gl_rasterization_rules - XXX - viewport code */
83 /* line_width - draw module */
84 /* fill_cw, fill_ccw - draw module or index translation */
85
86 rast->shademode = svga_translate_flatshade( templ->flatshade );
87 rast->cullmode = svga_translate_cullmode( templ->cull_face,
88 templ->front_ccw );
89 rast->scissortestenable = templ->scissor;
90 rast->multisampleantialias = templ->multisample;
91 rast->antialiasedlineenable = templ->line_smooth;
92 rast->lastpixel = templ->line_last_pixel;
93 rast->pointsize = templ->point_size;
94 rast->hw_unfilled = PIPE_POLYGON_MODE_FILL;
95
96 /* Use swtnl + decomposition implement these:
97 */
98 if (templ->poly_stipple_enable) {
99 rast->need_pipeline |= SVGA_PIPELINE_FLAG_TRIS;
100 rast->need_pipeline_tris_str = "poly stipple";
101 }
102
103 if (templ->line_width >= 1.5f &&
104 !svga->debug.no_line_width) {
105 rast->need_pipeline |= SVGA_PIPELINE_FLAG_LINES;
106 rast->need_pipeline_lines_str = "line width";
107 }
108
109 if (templ->line_stipple_enable) {
110 /* XXX: LinePattern not implemented on all backends, and there is no
111 * mechanism to query it.
112 */
113 if (!svga->debug.force_hw_line_stipple) {
114 SVGA3dLinePattern lp;
115 lp.repeat = templ->line_stipple_factor + 1;
116 lp.pattern = templ->line_stipple_pattern;
117 rast->linepattern = lp.uintValue;
118 }
119 else {
120 rast->need_pipeline |= SVGA_PIPELINE_FLAG_LINES;
121 rast->need_pipeline_lines_str = "line stipple";
122 }
123 }
124
125 if (templ->point_smooth) {
126 rast->need_pipeline |= SVGA_PIPELINE_FLAG_POINTS;
127 rast->need_pipeline_points_str = "smooth points";
128 }
129
130 {
131 int fill_front = templ->fill_front;
132 int fill_back = templ->fill_back;
133 int fill = PIPE_POLYGON_MODE_FILL;
134 boolean offset_front = util_get_offset(templ, fill_front);
135 boolean offset_back = util_get_offset(templ, fill_back);
136 boolean offset = 0;
137
138 switch (templ->cull_face) {
139 case PIPE_FACE_FRONT_AND_BACK:
140 offset = 0;
141 fill = PIPE_POLYGON_MODE_FILL;
142 break;
143
144 case PIPE_FACE_FRONT:
145 offset = offset_front;
146 fill = fill_front;
147 break;
148
149 case PIPE_FACE_BACK:
150 offset = offset_back;
151 fill = fill_back;
152 break;
153
154 case PIPE_FACE_NONE:
155 if (fill_front != fill_back || offset_front != offset_back)
156 {
157 /* Always need the draw module to work out different
158 * front/back fill modes:
159 */
160 rast->need_pipeline |= SVGA_PIPELINE_FLAG_TRIS;
161 rast->need_pipeline_tris_str = "different front/back fillmodes";
162 }
163 else {
164 offset = offset_front;
165 fill = fill_front;
166 }
167 break;
168
169 default:
170 assert(0);
171 break;
172 }
173
174 /* Unfilled primitive modes aren't implemented on all virtual
175 * hardware. We can do some unfilled processing with index
176 * translation, but otherwise need the draw module:
177 */
178 if (fill != PIPE_POLYGON_MODE_FILL &&
179 (templ->flatshade ||
180 templ->light_twoside ||
181 offset ||
182 templ->cull_face != PIPE_FACE_NONE))
183 {
184 fill = PIPE_POLYGON_MODE_FILL;
185 rast->need_pipeline |= SVGA_PIPELINE_FLAG_TRIS;
186 rast->need_pipeline_tris_str = "unfilled primitives with no index manipulation";
187 }
188
189 /* If we are decomposing to lines, and lines need the pipeline,
190 * then we also need the pipeline for tris.
191 */
192 if (fill == PIPE_POLYGON_MODE_LINE &&
193 (rast->need_pipeline & SVGA_PIPELINE_FLAG_LINES))
194 {
195 fill = PIPE_POLYGON_MODE_FILL;
196 rast->need_pipeline |= SVGA_PIPELINE_FLAG_TRIS;
197 rast->need_pipeline_tris_str = "decomposing lines";
198 }
199
200 /* Similarly for points:
201 */
202 if (fill == PIPE_POLYGON_MODE_POINT &&
203 (rast->need_pipeline & SVGA_PIPELINE_FLAG_POINTS))
204 {
205 fill = PIPE_POLYGON_MODE_FILL;
206 rast->need_pipeline |= SVGA_PIPELINE_FLAG_TRIS;
207 rast->need_pipeline_tris_str = "decomposing points";
208 }
209
210 if (offset) {
211 rast->slopescaledepthbias = templ->offset_scale;
212 rast->depthbias = templ->offset_units;
213 }
214
215 rast->hw_unfilled = fill;
216 }
217
218
219
220
221 if (rast->need_pipeline & SVGA_PIPELINE_FLAG_TRIS) {
222 /* Turn off stuff which will get done in the draw module:
223 */
224 rast->hw_unfilled = PIPE_POLYGON_MODE_FILL;
225 rast->slopescaledepthbias = 0;
226 rast->depthbias = 0;
227 }
228
229 return rast;
230 }
231
232 static void svga_bind_rasterizer_state( struct pipe_context *pipe,
233 void *state )
234 {
235 struct svga_context *svga = svga_context(pipe);
236 struct svga_rasterizer_state *raster = (struct svga_rasterizer_state *)state;
237
238 svga->curr.rast = raster;
239
240 draw_set_rasterizer_state(svga->swtnl.draw, raster ? &raster->templ : NULL,
241 state);
242
243 svga->dirty |= SVGA_NEW_RAST;
244 }
245
246 static void svga_delete_rasterizer_state(struct pipe_context *pipe,
247 void *raster)
248 {
249 FREE(raster);
250 }
251
252
253 void svga_init_rasterizer_functions( struct svga_context *svga )
254 {
255 svga->pipe.create_rasterizer_state = svga_create_rasterizer_state;
256 svga->pipe.bind_rasterizer_state = svga_bind_rasterizer_state;
257 svga->pipe.delete_rasterizer_state = svga_delete_rasterizer_state;
258 }
259
260
261 /***********************************************************************
262 * Hardware state update
263 */
264