Merge remote branch 'origin/master' into nv50-compiler
[mesa.git] / src / gallium / drivers / llvmpipe / lp_setup_tri.c
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 * Binning code for triangles
30 */
31
32 #include "util/u_math.h"
33 #include "util/u_memory.h"
34 #include "util/u_rect.h"
35 #include "lp_perf.h"
36 #include "lp_setup_context.h"
37 #include "lp_setup_coef.h"
38 #include "lp_rast.h"
39 #include "lp_state_fs.h"
40
41 #define NUM_CHANNELS 4
42
43
44
45 static INLINE int
46 subpixel_snap(float a)
47 {
48 return util_iround(FIXED_ONE * a);
49 }
50
51 static INLINE float
52 fixed_to_float(int a)
53 {
54 return a * (1.0 / FIXED_ONE);
55 }
56
57
58
59
60
61
62
63 /**
64 * Alloc space for a new triangle plus the input.a0/dadx/dady arrays
65 * immediately after it.
66 * The memory is allocated from the per-scene pool, not per-tile.
67 * \param tri_size returns number of bytes allocated
68 * \param nr_inputs number of fragment shader inputs
69 * \return pointer to triangle space
70 */
71 struct lp_rast_triangle *
72 lp_setup_alloc_triangle(struct lp_scene *scene,
73 unsigned nr_inputs,
74 unsigned nr_planes,
75 unsigned *tri_size)
76 {
77 unsigned input_array_sz = NUM_CHANNELS * (nr_inputs + 1) * sizeof(float);
78 struct lp_rast_triangle *tri;
79 unsigned tri_bytes, bytes;
80 char *inputs;
81
82 tri_bytes = align(Offset(struct lp_rast_triangle, plane[nr_planes]), 16);
83 bytes = tri_bytes + (3 * input_array_sz);
84
85 tri = lp_scene_alloc_aligned( scene, bytes, 16 );
86
87 if (tri) {
88 inputs = ((char *)tri) + tri_bytes;
89 tri->inputs.a0 = (float (*)[4]) inputs;
90 tri->inputs.dadx = (float (*)[4]) (inputs + input_array_sz);
91 tri->inputs.dady = (float (*)[4]) (inputs + 2 * input_array_sz);
92
93 *tri_size = bytes;
94 }
95
96 return tri;
97 }
98
99 void
100 lp_setup_print_vertex(struct lp_setup_context *setup,
101 const char *name,
102 const float (*v)[4])
103 {
104 int i, j;
105
106 debug_printf(" wpos (%s[0]) xyzw %f %f %f %f\n",
107 name,
108 v[0][0], v[0][1], v[0][2], v[0][3]);
109
110 for (i = 0; i < setup->fs.nr_inputs; i++) {
111 const float *in = v[setup->fs.input[i].src_index];
112
113 debug_printf(" in[%d] (%s[%d]) %s%s%s%s ",
114 i,
115 name, setup->fs.input[i].src_index,
116 (setup->fs.input[i].usage_mask & 0x1) ? "x" : " ",
117 (setup->fs.input[i].usage_mask & 0x2) ? "y" : " ",
118 (setup->fs.input[i].usage_mask & 0x4) ? "z" : " ",
119 (setup->fs.input[i].usage_mask & 0x8) ? "w" : " ");
120
121 for (j = 0; j < 4; j++)
122 if (setup->fs.input[i].usage_mask & (1<<j))
123 debug_printf("%.5f ", in[j]);
124
125 debug_printf("\n");
126 }
127 }
128
129
130 /**
131 * Print triangle vertex attribs (for debug).
132 */
133 void
134 lp_setup_print_triangle(struct lp_setup_context *setup,
135 const float (*v0)[4],
136 const float (*v1)[4],
137 const float (*v2)[4])
138 {
139 debug_printf("triangle\n");
140
141 {
142 const float ex = v0[0][0] - v2[0][0];
143 const float ey = v0[0][1] - v2[0][1];
144 const float fx = v1[0][0] - v2[0][0];
145 const float fy = v1[0][1] - v2[0][1];
146
147 /* det = cross(e,f).z */
148 const float det = ex * fy - ey * fx;
149 if (det < 0.0f)
150 debug_printf(" - ccw\n");
151 else if (det > 0.0f)
152 debug_printf(" - cw\n");
153 else
154 debug_printf(" - zero area\n");
155 }
156
157 lp_setup_print_vertex(setup, "v0", v0);
158 lp_setup_print_vertex(setup, "v1", v1);
159 lp_setup_print_vertex(setup, "v2", v2);
160 }
161
162
163 lp_rast_cmd lp_rast_tri_tab[9] = {
164 NULL, /* should be impossible */
165 lp_rast_triangle_1,
166 lp_rast_triangle_2,
167 lp_rast_triangle_3,
168 lp_rast_triangle_4,
169 lp_rast_triangle_5,
170 lp_rast_triangle_6,
171 lp_rast_triangle_7,
172 lp_rast_triangle_8
173 };
174
175 /**
176 * Do basic setup for triangle rasterization and determine which
177 * framebuffer tiles are touched. Put the triangle in the scene's
178 * bins for the tiles which we overlap.
179 */
180 static void
181 do_triangle_ccw(struct lp_setup_context *setup,
182 const float (*v0)[4],
183 const float (*v1)[4],
184 const float (*v2)[4],
185 boolean frontfacing )
186 {
187 struct lp_scene *scene = lp_setup_get_current_scene(setup);
188 struct lp_rast_triangle *tri;
189 int x[3];
190 int y[3];
191 float dy01, dy20;
192 float dx01, dx20;
193 float oneoverarea;
194 struct lp_tri_info info;
195 int area;
196 struct u_rect bbox;
197 unsigned tri_bytes;
198 int i;
199 int nr_planes = 3;
200
201 if (0)
202 lp_setup_print_triangle(setup, v0, v1, v2);
203
204 if (setup->scissor_test) {
205 nr_planes = 7;
206 }
207 else {
208 nr_planes = 3;
209 }
210
211 /* x/y positions in fixed point */
212 x[0] = subpixel_snap(v0[0][0] - setup->pixel_offset);
213 x[1] = subpixel_snap(v1[0][0] - setup->pixel_offset);
214 x[2] = subpixel_snap(v2[0][0] - setup->pixel_offset);
215 y[0] = subpixel_snap(v0[0][1] - setup->pixel_offset);
216 y[1] = subpixel_snap(v1[0][1] - setup->pixel_offset);
217 y[2] = subpixel_snap(v2[0][1] - setup->pixel_offset);
218
219
220 /* Bounding rectangle (in pixels) */
221 {
222 /* Yes this is necessary to accurately calculate bounding boxes
223 * with the two fill-conventions we support. GL (normally) ends
224 * up needing a bottom-left fill convention, which requires
225 * slightly different rounding.
226 */
227 int adj = (setup->pixel_offset != 0) ? 1 : 0;
228
229 bbox.x0 = (MIN3(x[0], x[1], x[2]) + (FIXED_ONE-1)) >> FIXED_ORDER;
230 bbox.x1 = (MAX3(x[0], x[1], x[2]) + (FIXED_ONE-1)) >> FIXED_ORDER;
231 bbox.y0 = (MIN3(y[0], y[1], y[2]) + (FIXED_ONE-1) + adj) >> FIXED_ORDER;
232 bbox.y1 = (MAX3(y[0], y[1], y[2]) + (FIXED_ONE-1) + adj) >> FIXED_ORDER;
233
234 /* Inclusive coordinates:
235 */
236 bbox.x1--;
237 bbox.y1--;
238 }
239
240 if (bbox.x1 < bbox.x0 ||
241 bbox.y1 < bbox.y0) {
242 if (0) debug_printf("empty bounding box\n");
243 LP_COUNT(nr_culled_tris);
244 return;
245 }
246
247 if (!u_rect_test_intersection(&setup->draw_region, &bbox)) {
248 if (0) debug_printf("offscreen\n");
249 LP_COUNT(nr_culled_tris);
250 return;
251 }
252
253 u_rect_find_intersection(&setup->draw_region, &bbox);
254
255 tri = lp_setup_alloc_triangle(scene,
256 setup->fs.nr_inputs,
257 nr_planes,
258 &tri_bytes);
259 if (!tri)
260 return;
261
262 #ifdef DEBUG
263 tri->v[0][0] = v0[0][0];
264 tri->v[1][0] = v1[0][0];
265 tri->v[2][0] = v2[0][0];
266 tri->v[0][1] = v0[0][1];
267 tri->v[1][1] = v1[0][1];
268 tri->v[2][1] = v2[0][1];
269 #endif
270
271 tri->plane[0].dcdy = x[0] - x[1];
272 tri->plane[1].dcdy = x[1] - x[2];
273 tri->plane[2].dcdy = x[2] - x[0];
274
275 tri->plane[0].dcdx = y[0] - y[1];
276 tri->plane[1].dcdx = y[1] - y[2];
277 tri->plane[2].dcdx = y[2] - y[0];
278
279 area = (tri->plane[0].dcdy * tri->plane[2].dcdx -
280 tri->plane[2].dcdy * tri->plane[0].dcdx);
281
282 LP_COUNT(nr_tris);
283
284 /* Cull non-ccw and zero-sized triangles.
285 *
286 * XXX: subject to overflow??
287 */
288 if (area <= 0) {
289 lp_scene_putback_data( scene, tri_bytes );
290 LP_COUNT(nr_culled_tris);
291 return;
292 }
293
294
295 /*
296 */
297 dx01 = v0[0][0] - v1[0][0];
298 dy01 = v0[0][1] - v1[0][1];
299 dx20 = v2[0][0] - v0[0][0];
300 dy20 = v2[0][1] - v0[0][1];
301 oneoverarea = 1.0f / (dx01 * dy20 - dx20 * dy01);
302
303 info.v0 = v0;
304 info.v1 = v1;
305 info.v2 = v2;
306 info.frontfacing = frontfacing;
307 info.x0_center = v0[0][0] - setup->pixel_offset;
308 info.y0_center = v0[0][1] - setup->pixel_offset;
309 info.dx01_ooa = dx01 * oneoverarea;
310 info.dx20_ooa = dx20 * oneoverarea;
311 info.dy01_ooa = dy01 * oneoverarea;
312 info.dy20_ooa = dy20 * oneoverarea;
313
314 /* Setup parameter interpolants:
315 */
316 lp_setup_tri_coef( setup, &tri->inputs, &info );
317
318 tri->inputs.facing = frontfacing ? 1.0F : -1.0F;
319 tri->inputs.state = setup->fs.stored;
320
321
322
323 for (i = 0; i < 3; i++) {
324 struct lp_rast_plane *plane = &tri->plane[i];
325
326 /* half-edge constants, will be interated over the whole render
327 * target.
328 */
329 plane->c = plane->dcdx * x[i] - plane->dcdy * y[i];
330
331 /* correct for top-left vs. bottom-left fill convention.
332 *
333 * note that we're overloading gl_rasterization_rules to mean
334 * both (0.5,0.5) pixel centers *and* bottom-left filling
335 * convention.
336 *
337 * GL actually has a top-left filling convention, but GL's
338 * notion of "top" differs from gallium's...
339 *
340 * Also, sometimes (in FBO cases) GL will render upside down
341 * to its usual method, in which case it will probably want
342 * to use the opposite, top-left convention.
343 */
344 if (plane->dcdx < 0) {
345 /* both fill conventions want this - adjust for left edges */
346 plane->c++;
347 }
348 else if (plane->dcdx == 0) {
349 if (setup->pixel_offset == 0) {
350 /* correct for top-left fill convention:
351 */
352 if (plane->dcdy > 0) plane->c++;
353 }
354 else {
355 /* correct for bottom-left fill convention:
356 */
357 if (plane->dcdy < 0) plane->c++;
358 }
359 }
360
361 plane->dcdx *= FIXED_ONE;
362 plane->dcdy *= FIXED_ONE;
363
364 /* find trivial reject offsets for each edge for a single-pixel
365 * sized block. These will be scaled up at each recursive level to
366 * match the active blocksize. Scaling in this way works best if
367 * the blocks are square.
368 */
369 plane->eo = 0;
370 if (plane->dcdx < 0) plane->eo -= plane->dcdx;
371 if (plane->dcdy > 0) plane->eo += plane->dcdy;
372
373 /* Calculate trivial accept offsets from the above.
374 */
375 plane->ei = plane->dcdy - plane->dcdx - plane->eo;
376 }
377
378
379 /*
380 * When rasterizing scissored tris, use the intersection of the
381 * triangle bounding box and the scissor rect to generate the
382 * scissor planes.
383 *
384 * This permits us to cut off the triangle "tails" that are present
385 * in the intermediate recursive levels caused when two of the
386 * triangles edges don't diverge quickly enough to trivially reject
387 * exterior blocks from the triangle.
388 *
389 * It's not really clear if it's worth worrying about these tails,
390 * but since we generate the planes for each scissored tri, it's
391 * free to trim them in this case.
392 *
393 * Note that otherwise, the scissor planes only vary in 'C' value,
394 * and even then only on state-changes. Could alternatively store
395 * these planes elsewhere.
396 */
397 if (nr_planes == 7) {
398 tri->plane[3].dcdx = -1;
399 tri->plane[3].dcdy = 0;
400 tri->plane[3].c = 1-bbox.x0;
401 tri->plane[3].ei = 0;
402 tri->plane[3].eo = 1;
403
404 tri->plane[4].dcdx = 1;
405 tri->plane[4].dcdy = 0;
406 tri->plane[4].c = bbox.x1+1;
407 tri->plane[4].ei = -1;
408 tri->plane[4].eo = 0;
409
410 tri->plane[5].dcdx = 0;
411 tri->plane[5].dcdy = 1;
412 tri->plane[5].c = 1-bbox.y0;
413 tri->plane[5].ei = 0;
414 tri->plane[5].eo = 1;
415
416 tri->plane[6].dcdx = 0;
417 tri->plane[6].dcdy = -1;
418 tri->plane[6].c = bbox.y1+1;
419 tri->plane[6].ei = -1;
420 tri->plane[6].eo = 0;
421 }
422
423 lp_setup_bin_triangle( setup, tri, &bbox, nr_planes );
424 }
425
426
427 void
428 lp_setup_bin_triangle( struct lp_setup_context *setup,
429 struct lp_rast_triangle *tri,
430 const struct u_rect *bbox,
431 int nr_planes )
432 {
433 struct lp_scene *scene = setup->scene;
434 struct lp_fragment_shader_variant *variant = setup->fs.current.variant;
435 int ix0, ix1, iy0, iy1;
436 int i;
437
438 /*
439 * All fields of 'tri' are now set. The remaining code here is
440 * concerned with binning.
441 */
442
443 /* Convert to tile coordinates, and inclusive ranges:
444 */
445 if (nr_planes == 3) {
446 int ix0 = bbox->x0 / 16;
447 int iy0 = bbox->y0 / 16;
448 int ix1 = bbox->x1 / 16;
449 int iy1 = bbox->y1 / 16;
450
451 if (iy0 == iy1 && ix0 == ix1)
452 {
453
454 /* Triangle is contained in a single 16x16 block:
455 */
456 int mask = (ix0 & 3) | ((iy0 & 3) << 4);
457
458 lp_scene_bin_command( scene, ix0/4, iy0/4,
459 lp_rast_triangle_3_16,
460 lp_rast_arg_triangle(tri, mask) );
461 return;
462 }
463 }
464
465 ix0 = bbox->x0 / TILE_SIZE;
466 iy0 = bbox->y0 / TILE_SIZE;
467 ix1 = bbox->x1 / TILE_SIZE;
468 iy1 = bbox->y1 / TILE_SIZE;
469
470 /*
471 * Clamp to framebuffer size
472 */
473 assert(ix0 == MAX2(ix0, 0));
474 assert(iy0 == MAX2(iy0, 0));
475 assert(ix1 == MIN2(ix1, scene->tiles_x - 1));
476 assert(iy1 == MIN2(iy1, scene->tiles_y - 1));
477
478 /* Determine which tile(s) intersect the triangle's bounding box
479 */
480 if (iy0 == iy1 && ix0 == ix1)
481 {
482 /* Triangle is contained in a single tile:
483 */
484 lp_scene_bin_command( scene, ix0, iy0,
485 lp_rast_tri_tab[nr_planes],
486 lp_rast_arg_triangle(tri, (1<<nr_planes)-1) );
487 }
488 else
489 {
490 int c[7];
491 int ei[7];
492 int eo[7];
493 int xstep[7];
494 int ystep[7];
495 int x, y;
496
497 for (i = 0; i < nr_planes; i++) {
498 c[i] = (tri->plane[i].c +
499 tri->plane[i].dcdy * iy0 * TILE_SIZE -
500 tri->plane[i].dcdx * ix0 * TILE_SIZE);
501
502 ei[i] = tri->plane[i].ei << TILE_ORDER;
503 eo[i] = tri->plane[i].eo << TILE_ORDER;
504 xstep[i] = -(tri->plane[i].dcdx << TILE_ORDER);
505 ystep[i] = tri->plane[i].dcdy << TILE_ORDER;
506 }
507
508
509
510 /* Test tile-sized blocks against the triangle.
511 * Discard blocks fully outside the tri. If the block is fully
512 * contained inside the tri, bin an lp_rast_shade_tile command.
513 * Else, bin a lp_rast_triangle command.
514 */
515 for (y = iy0; y <= iy1; y++)
516 {
517 boolean in = FALSE; /* are we inside the triangle? */
518 int cx[7];
519
520 for (i = 0; i < nr_planes; i++)
521 cx[i] = c[i];
522
523 for (x = ix0; x <= ix1; x++)
524 {
525 int out = 0;
526 int partial = 0;
527
528 for (i = 0; i < nr_planes; i++) {
529 int planeout = cx[i] + eo[i];
530 int planepartial = cx[i] + ei[i] - 1;
531 out |= (planeout >> 31);
532 partial |= (planepartial >> 31) & (1<<i);
533 }
534
535 if (out) {
536 /* do nothing */
537 if (in)
538 break; /* exiting triangle, all done with this row */
539 LP_COUNT(nr_empty_64);
540 }
541 else if (partial) {
542 /* Not trivially accepted by at least one plane -
543 * rasterize/shade partial tile
544 */
545 int count = util_bitcount(partial);
546 in = TRUE;
547 lp_scene_bin_command( scene, x, y,
548 lp_rast_tri_tab[count],
549 lp_rast_arg_triangle(tri, partial) );
550
551 LP_COUNT(nr_partially_covered_64);
552 }
553 else {
554 /* triangle covers the whole tile- shade whole tile */
555 LP_COUNT(nr_fully_covered_64);
556 in = TRUE;
557 if (variant->opaque &&
558 !setup->fb.zsbuf) {
559 lp_scene_bin_reset( scene, x, y );
560 }
561 lp_scene_bin_command( scene, x, y,
562 lp_rast_shade_tile,
563 lp_rast_arg_inputs(&tri->inputs) );
564 }
565
566 /* Iterate cx values across the region:
567 */
568 for (i = 0; i < nr_planes; i++)
569 cx[i] += xstep[i];
570 }
571
572 /* Iterate c values down the region:
573 */
574 for (i = 0; i < nr_planes; i++)
575 c[i] += ystep[i];
576 }
577 }
578 }
579
580
581 /**
582 * Draw triangle if it's CW, cull otherwise.
583 */
584 static void triangle_cw( struct lp_setup_context *setup,
585 const float (*v0)[4],
586 const float (*v1)[4],
587 const float (*v2)[4] )
588 {
589 do_triangle_ccw( setup, v1, v0, v2, !setup->ccw_is_frontface );
590 }
591
592
593 /**
594 * Draw triangle if it's CCW, cull otherwise.
595 */
596 static void triangle_ccw( struct lp_setup_context *setup,
597 const float (*v0)[4],
598 const float (*v1)[4],
599 const float (*v2)[4] )
600 {
601 do_triangle_ccw( setup, v0, v1, v2, setup->ccw_is_frontface );
602 }
603
604
605
606 /**
607 * Draw triangle whether it's CW or CCW.
608 */
609 static void triangle_both( struct lp_setup_context *setup,
610 const float (*v0)[4],
611 const float (*v1)[4],
612 const float (*v2)[4] )
613 {
614 /* edge vectors e = v0 - v2, f = v1 - v2 */
615 const float ex = v0[0][0] - v2[0][0];
616 const float ey = v0[0][1] - v2[0][1];
617 const float fx = v1[0][0] - v2[0][0];
618 const float fy = v1[0][1] - v2[0][1];
619
620 /* det = cross(e,f).z */
621 const float det = ex * fy - ey * fx;
622 if (det < 0.0f)
623 triangle_ccw( setup, v0, v1, v2 );
624 else if (det > 0.0f)
625 triangle_cw( setup, v0, v1, v2 );
626 }
627
628
629 static void triangle_nop( struct lp_setup_context *setup,
630 const float (*v0)[4],
631 const float (*v1)[4],
632 const float (*v2)[4] )
633 {
634 }
635
636
637 void
638 lp_setup_choose_triangle( struct lp_setup_context *setup )
639 {
640 switch (setup->cullmode) {
641 case PIPE_FACE_NONE:
642 setup->triangle = triangle_both;
643 break;
644 case PIPE_FACE_BACK:
645 setup->triangle = setup->ccw_is_frontface ? triangle_ccw : triangle_cw;
646 break;
647 case PIPE_FACE_FRONT:
648 setup->triangle = setup->ccw_is_frontface ? triangle_cw : triangle_ccw;
649 break;
650 default:
651 setup->triangle = triangle_nop;
652 break;
653 }
654 }