i965: use pack/unpackDouble lowering
[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 "pipe/p_defines.h"
27 #include "draw/draw_context.h"
28 #include "util/u_bitmask.h"
29 #include "util/u_inlines.h"
30 #include "util/u_math.h"
31 #include "util/u_memory.h"
32
33 #include "svga_cmd.h"
34 #include "svga_context.h"
35 #include "svga_hw_reg.h"
36 #include "svga_screen.h"
37
38
39 /* Hardware frontwinding is always set up as SVGA3D_FRONTWINDING_CW.
40 */
41 static SVGA3dFace
42 svga_translate_cullmode(unsigned mode, unsigned front_ccw)
43 {
44 const int hw_front_ccw = 0; /* hardware is always CW */
45 switch (mode) {
46 case PIPE_FACE_NONE:
47 return SVGA3D_FACE_NONE;
48 case PIPE_FACE_FRONT:
49 return front_ccw == hw_front_ccw ? SVGA3D_FACE_FRONT : SVGA3D_FACE_BACK;
50 case PIPE_FACE_BACK:
51 return front_ccw == hw_front_ccw ? SVGA3D_FACE_BACK : SVGA3D_FACE_FRONT;
52 case PIPE_FACE_FRONT_AND_BACK:
53 return SVGA3D_FACE_FRONT_BACK;
54 default:
55 assert(0);
56 return SVGA3D_FACE_NONE;
57 }
58 }
59
60 static SVGA3dShadeMode
61 svga_translate_flatshade(unsigned mode)
62 {
63 return mode ? SVGA3D_SHADEMODE_FLAT : SVGA3D_SHADEMODE_SMOOTH;
64 }
65
66
67 static unsigned
68 translate_fill_mode(unsigned fill)
69 {
70 switch (fill) {
71 case PIPE_POLYGON_MODE_POINT:
72 return SVGA3D_FILLMODE_POINT;
73 case PIPE_POLYGON_MODE_LINE:
74 return SVGA3D_FILLMODE_LINE;
75 case PIPE_POLYGON_MODE_FILL:
76 return SVGA3D_FILLMODE_FILL;
77 default:
78 assert(!"Bad fill mode");
79 return SVGA3D_FILLMODE_FILL;
80 }
81 }
82
83
84 static unsigned
85 translate_cull_mode(unsigned cull)
86 {
87 switch (cull) {
88 case PIPE_FACE_NONE:
89 return SVGA3D_CULL_NONE;
90 case PIPE_FACE_FRONT:
91 return SVGA3D_CULL_FRONT;
92 case PIPE_FACE_BACK:
93 return SVGA3D_CULL_BACK;
94 case PIPE_FACE_FRONT_AND_BACK:
95 /* NOTE: we simply no-op polygon drawing in svga_draw_vbo() */
96 return SVGA3D_CULL_NONE;
97 default:
98 assert(!"Bad cull mode");
99 return SVGA3D_CULL_NONE;
100 }
101 }
102
103
104 static void
105 define_rasterizer_object(struct svga_context *svga,
106 struct svga_rasterizer_state *rast)
107 {
108 unsigned fill_mode = translate_fill_mode(rast->templ.fill_front);
109 unsigned cull_mode = translate_cull_mode(rast->templ.cull_face);
110 int depth_bias = rast->templ.offset_units;
111 float slope_scaled_depth_bias = rast->templ.offset_scale;
112 float depth_bias_clamp = 0.0; /* XXX fix me */
113 unsigned try;
114 const float line_width = rast->templ.line_width > 0.0f ?
115 rast->templ.line_width : 1.0f;
116 const uint8 line_factor = rast->templ.line_stipple_enable ?
117 rast->templ.line_stipple_factor : 0;
118 const uint16 line_pattern = rast->templ.line_stipple_enable ?
119 rast->templ.line_stipple_pattern : 0;
120
121 rast->id = util_bitmask_add(svga->rast_object_id_bm);
122
123 if (rast->templ.fill_front != rast->templ.fill_back) {
124 /* The VGPU10 device can't handle different front/back fill modes.
125 * We'll handle that with a swtnl/draw fallback. But we need to
126 * make sure we always fill triangles in that case.
127 */
128 fill_mode = SVGA3D_FILLMODE_FILL;
129 }
130
131 for (try = 0; try < 2; try++) {
132 enum pipe_error ret =
133 SVGA3D_vgpu10_DefineRasterizerState(svga->swc,
134 rast->id,
135 fill_mode,
136 cull_mode,
137 rast->templ.front_ccw,
138 depth_bias,
139 depth_bias_clamp,
140 slope_scaled_depth_bias,
141 rast->templ.depth_clip,
142 rast->templ.scissor,
143 rast->templ.multisample,
144 rast->templ.line_smooth,
145 line_width,
146 rast->templ.line_stipple_enable,
147 line_factor,
148 line_pattern,
149 !rast->templ.flatshade_first);
150 if (ret == PIPE_OK)
151 return;
152 svga_context_flush(svga, NULL);
153 }
154 }
155
156
157 static void *
158 svga_create_rasterizer_state(struct pipe_context *pipe,
159 const struct pipe_rasterizer_state *templ)
160 {
161 struct svga_context *svga = svga_context(pipe);
162 struct svga_rasterizer_state *rast = CALLOC_STRUCT(svga_rasterizer_state);
163 struct svga_screen *screen = svga_screen(pipe->screen);
164
165 if (!rast)
166 return NULL;
167
168 /* need this for draw module. */
169 rast->templ = *templ;
170
171 /* light_twoside - XXX: need fragment shader variant */
172 /* poly_smooth - XXX: no fallback available */
173 /* poly_stipple_enable - draw module */
174 /* sprite_coord_enable - ? */
175 /* point_quad_rasterization - ? */
176 /* point_size_per_vertex - ? */
177 /* sprite_coord_mode - ??? */
178 /* flatshade_first - handled by index translation */
179 /* half_pixel_center - XXX - viewport code */
180 /* line_width - draw module */
181 /* fill_cw, fill_ccw - draw module or index translation */
182
183 rast->shademode = svga_translate_flatshade(templ->flatshade);
184 rast->cullmode = svga_translate_cullmode(templ->cull_face, templ->front_ccw);
185 rast->scissortestenable = templ->scissor;
186 rast->multisampleantialias = templ->multisample;
187 rast->antialiasedlineenable = templ->line_smooth;
188 rast->lastpixel = templ->line_last_pixel;
189 rast->pointsprite = templ->sprite_coord_enable != 0x0;
190
191 if (templ->point_smooth) {
192 /* For smooth points we need to generate fragments for at least
193 * a 2x2 region. Otherwise the quad we draw may be too small and
194 * we may generate no fragments at all.
195 */
196 rast->pointsize = MAX2(2.0f, templ->point_size);
197 }
198 else {
199 rast->pointsize = templ->point_size;
200 }
201
202 rast->hw_fillmode = PIPE_POLYGON_MODE_FILL;
203
204 /* Use swtnl + decomposition implement these:
205 */
206
207 if (templ->line_width <= screen->maxLineWidth) {
208 /* pass line width to device */
209 rast->linewidth = MAX2(1.0F, templ->line_width);
210 }
211 else if (svga->debug.no_line_width) {
212 /* nothing */
213 }
214 else {
215 /* use 'draw' pipeline for wide line */
216 rast->need_pipeline |= SVGA_PIPELINE_FLAG_LINES;
217 rast->need_pipeline_lines_str = "line width";
218 }
219
220 if (templ->line_stipple_enable) {
221 if (screen->haveLineStipple || svga->debug.force_hw_line_stipple) {
222 SVGA3dLinePattern lp;
223 lp.repeat = templ->line_stipple_factor + 1;
224 lp.pattern = templ->line_stipple_pattern;
225 rast->linepattern = lp.uintValue;
226 }
227 else {
228 /* use 'draw' module to decompose into short line segments */
229 rast->need_pipeline |= SVGA_PIPELINE_FLAG_LINES;
230 rast->need_pipeline_lines_str = "line stipple";
231 }
232 }
233
234 if (!svga_have_vgpu10(svga) && templ->point_smooth) {
235 rast->need_pipeline |= SVGA_PIPELINE_FLAG_POINTS;
236 rast->need_pipeline_points_str = "smooth points";
237 }
238
239 if (templ->line_smooth && !screen->haveLineSmooth) {
240 /*
241 * XXX: Enabling the pipeline slows down performance immensely, so ignore
242 * line smooth state, where there is very little visual improvement.
243 * Smooth lines will still be drawn for wide lines.
244 */
245 #if 0
246 rast->need_pipeline |= SVGA_PIPELINE_FLAG_LINES;
247 rast->need_pipeline_lines_str = "smooth lines";
248 #endif
249 }
250
251 {
252 int fill_front = templ->fill_front;
253 int fill_back = templ->fill_back;
254 int fill = PIPE_POLYGON_MODE_FILL;
255 boolean offset_front = util_get_offset(templ, fill_front);
256 boolean offset_back = util_get_offset(templ, fill_back);
257 boolean offset = FALSE;
258
259 switch (templ->cull_face) {
260 case PIPE_FACE_FRONT_AND_BACK:
261 offset = FALSE;
262 fill = PIPE_POLYGON_MODE_FILL;
263 break;
264
265 case PIPE_FACE_FRONT:
266 offset = offset_front;
267 fill = fill_front;
268 break;
269
270 case PIPE_FACE_BACK:
271 offset = offset_back;
272 fill = fill_back;
273 break;
274
275 case PIPE_FACE_NONE:
276 if (fill_front != fill_back || offset_front != offset_back)
277 {
278 /* Always need the draw module to work out different
279 * front/back fill modes:
280 */
281 rast->need_pipeline |= SVGA_PIPELINE_FLAG_TRIS;
282 rast->need_pipeline_tris_str = "different front/back fillmodes";
283 }
284 else {
285 offset = offset_front;
286 fill = fill_front;
287 }
288 break;
289
290 default:
291 assert(0);
292 break;
293 }
294
295 /* Unfilled primitive modes aren't implemented on all virtual
296 * hardware. We can do some unfilled processing with index
297 * translation, but otherwise need the draw module:
298 */
299 if (fill != PIPE_POLYGON_MODE_FILL &&
300 (templ->flatshade ||
301 templ->light_twoside ||
302 offset ||
303 templ->cull_face != PIPE_FACE_NONE))
304 {
305 fill = PIPE_POLYGON_MODE_FILL;
306 rast->need_pipeline |= SVGA_PIPELINE_FLAG_TRIS;
307 rast->need_pipeline_tris_str = "unfilled primitives with no index manipulation";
308 }
309
310 /* If we are decomposing to lines, and lines need the pipeline,
311 * then we also need the pipeline for tris.
312 */
313 if (fill == PIPE_POLYGON_MODE_LINE &&
314 (rast->need_pipeline & SVGA_PIPELINE_FLAG_LINES))
315 {
316 fill = PIPE_POLYGON_MODE_FILL;
317 rast->need_pipeline |= SVGA_PIPELINE_FLAG_TRIS;
318 rast->need_pipeline_tris_str = "decomposing lines";
319 }
320
321 /* Similarly for points:
322 */
323 if (fill == PIPE_POLYGON_MODE_POINT &&
324 (rast->need_pipeline & SVGA_PIPELINE_FLAG_POINTS))
325 {
326 fill = PIPE_POLYGON_MODE_FILL;
327 rast->need_pipeline |= SVGA_PIPELINE_FLAG_TRIS;
328 rast->need_pipeline_tris_str = "decomposing points";
329 }
330
331 if (offset) {
332 rast->slopescaledepthbias = templ->offset_scale;
333 rast->depthbias = templ->offset_units;
334 }
335
336 rast->hw_fillmode = fill;
337 }
338
339 if (rast->need_pipeline & SVGA_PIPELINE_FLAG_TRIS) {
340 /* Turn off stuff which will get done in the draw module:
341 */
342 rast->hw_fillmode = PIPE_POLYGON_MODE_FILL;
343 rast->slopescaledepthbias = 0;
344 rast->depthbias = 0;
345 }
346
347 if (0 && rast->need_pipeline) {
348 debug_printf("svga: rast need_pipeline = 0x%x\n", rast->need_pipeline);
349 debug_printf(" pnts: %s \n", rast->need_pipeline_points_str);
350 debug_printf(" lins: %s \n", rast->need_pipeline_lines_str);
351 debug_printf(" tris: %s \n", rast->need_pipeline_tris_str);
352 }
353
354 if (svga_have_vgpu10(svga)) {
355 define_rasterizer_object(svga, rast);
356 }
357
358 if (templ->poly_smooth) {
359 pipe_debug_message(&svga->debug.callback, CONFORMANCE,
360 "GL_POLYGON_SMOOTH not supported");
361 }
362
363 svga->hud.num_rasterizer_objects++;
364
365 return rast;
366 }
367
368
369 static void
370 svga_bind_rasterizer_state(struct pipe_context *pipe, void *state)
371 {
372 struct svga_context *svga = svga_context(pipe);
373 struct svga_rasterizer_state *raster = (struct svga_rasterizer_state *)state;
374
375 if (!raster ||
376 !svga->curr.rast ||
377 raster->templ.poly_stipple_enable !=
378 svga->curr.rast->templ.poly_stipple_enable) {
379 svga->dirty |= SVGA_NEW_STIPPLE;
380 }
381
382 svga->curr.rast = raster;
383
384 svga->dirty |= SVGA_NEW_RAST;
385 }
386
387
388 static void
389 svga_delete_rasterizer_state(struct pipe_context *pipe, void *state)
390 {
391 struct svga_context *svga = svga_context(pipe);
392 struct svga_rasterizer_state *raster =
393 (struct svga_rasterizer_state *) state;
394
395 if (svga_have_vgpu10(svga)) {
396 enum pipe_error ret =
397 SVGA3D_vgpu10_DestroyRasterizerState(svga->swc, raster->id);
398 if (ret != PIPE_OK) {
399 svga_context_flush(svga, NULL);
400 ret = SVGA3D_vgpu10_DestroyRasterizerState(svga->swc, raster->id);
401 }
402
403 if (raster->id == svga->state.hw_draw.rasterizer_id)
404 svga->state.hw_draw.rasterizer_id = SVGA3D_INVALID_ID;
405
406 util_bitmask_clear(svga->rast_object_id_bm, raster->id);
407 }
408
409 FREE(state);
410 svga->hud.num_rasterizer_objects--;
411 }
412
413
414 void
415 svga_init_rasterizer_functions(struct svga_context *svga)
416 {
417 svga->pipe.create_rasterizer_state = svga_create_rasterizer_state;
418 svga->pipe.bind_rasterizer_state = svga_bind_rasterizer_state;
419 svga->pipe.delete_rasterizer_state = svga_delete_rasterizer_state;
420 }