llvmpipe: re-use a1 var in linear_coef()
[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 "lp_perf.h"
35 #include "lp_setup_context.h"
36 #include "lp_rast.h"
37
38 #define NUM_CHANNELS 4
39
40
41 /**
42 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
43 */
44 static void constant_coef( struct lp_rast_triangle *tri,
45 unsigned slot,
46 const float value,
47 unsigned i )
48 {
49 tri->inputs.a0[slot][i] = value;
50 tri->inputs.dadx[slot][i] = 0.0f;
51 tri->inputs.dady[slot][i] = 0.0f;
52 }
53
54
55 /**
56 * Compute a0, dadx and dady for a linearly interpolated coefficient,
57 * for a triangle.
58 */
59 static void linear_coef( struct lp_rast_triangle *tri,
60 float oneoverarea,
61 unsigned slot,
62 const float (*v1)[4],
63 const float (*v2)[4],
64 const float (*v3)[4],
65 unsigned vert_attr,
66 unsigned i)
67 {
68 float a1 = v1[vert_attr][i];
69 float a2 = v2[vert_attr][i];
70 float a3 = v3[vert_attr][i];
71
72 float da12 = a1 - a2;
73 float da31 = a3 - a1;
74 float dadx = (da12 * tri->dy31 - tri->dy12 * da31) * oneoverarea;
75 float dady = (da31 * tri->dx12 - tri->dx31 * da12) * oneoverarea;
76
77 tri->inputs.dadx[slot][i] = dadx;
78 tri->inputs.dady[slot][i] = dady;
79
80 /* calculate a0 as the value which would be sampled for the
81 * fragment at (0,0), taking into account that we want to sample at
82 * pixel centers, in other words (0.5, 0.5).
83 *
84 * this is neat but unfortunately not a good way to do things for
85 * triangles with very large values of dadx or dady as it will
86 * result in the subtraction and re-addition from a0 of a very
87 * large number, which means we'll end up loosing a lot of the
88 * fractional bits and precision from a0. the way to fix this is
89 * to define a0 as the sample at a pixel center somewhere near vmin
90 * instead - i'll switch to this later.
91 */
92 tri->inputs.a0[slot][i] = (a1 -
93 (dadx * (v1[0][0] - 0.5f) +
94 dady * (v1[0][1] - 0.5f)));
95 }
96
97
98 /**
99 * Compute a0, dadx and dady for a perspective-corrected interpolant,
100 * for a triangle.
101 * We basically multiply the vertex value by 1/w before computing
102 * the plane coefficients (a0, dadx, dady).
103 * Later, when we compute the value at a particular fragment position we'll
104 * divide the interpolated value by the interpolated W at that fragment.
105 */
106 static void perspective_coef( struct lp_rast_triangle *tri,
107 float oneoverarea,
108 unsigned slot,
109 const float (*v1)[4],
110 const float (*v2)[4],
111 const float (*v3)[4],
112 unsigned vert_attr,
113 unsigned i)
114 {
115 /* premultiply by 1/w (v[0][3] is always 1/w):
116 */
117 float a1 = v1[vert_attr][i] * v1[0][3];
118 float a2 = v2[vert_attr][i] * v2[0][3];
119 float a3 = v3[vert_attr][i] * v3[0][3];
120 float da12 = a1 - a2;
121 float da31 = a3 - a1;
122 float dadx = (da12 * tri->dy31 - tri->dy12 * da31) * oneoverarea;
123 float dady = (da31 * tri->dx12 - tri->dx31 * da12) * oneoverarea;
124
125 tri->inputs.dadx[slot][i] = dadx;
126 tri->inputs.dady[slot][i] = dady;
127 tri->inputs.a0[slot][i] = (a1 -
128 (dadx * (v1[0][0] - 0.5f) +
129 dady * (v1[0][1] - 0.5f)));
130 }
131
132
133 /**
134 * Special coefficient setup for gl_FragCoord.
135 * X and Y are trivial
136 * Z and W are copied from position_coef which should have already been computed.
137 * We could do a bit less work if we'd examine gl_FragCoord's swizzle mask.
138 */
139 static void
140 setup_fragcoord_coef(struct lp_rast_triangle *tri,
141 float oneoverarea,
142 unsigned slot,
143 const float (*v1)[4],
144 const float (*v2)[4],
145 const float (*v3)[4])
146 {
147 /*X*/
148 tri->inputs.a0[slot][0] = 0.0;
149 tri->inputs.dadx[slot][0] = 1.0;
150 tri->inputs.dady[slot][0] = 0.0;
151 /*Y*/
152 tri->inputs.a0[slot][1] = 0.0;
153 tri->inputs.dadx[slot][1] = 0.0;
154 tri->inputs.dady[slot][1] = 1.0;
155 /*Z*/
156 linear_coef(tri, oneoverarea, slot, v1, v2, v3, 0, 2);
157 /*W*/
158 linear_coef(tri, oneoverarea, slot, v1, v2, v3, 0, 3);
159 }
160
161
162 static void setup_facing_coef( struct lp_rast_triangle *tri,
163 unsigned slot,
164 boolean frontface )
165 {
166 constant_coef( tri, slot, 1.0f - frontface, 0 );
167 constant_coef( tri, slot, 0.0f, 1 ); /* wasted */
168 constant_coef( tri, slot, 0.0f, 2 ); /* wasted */
169 constant_coef( tri, slot, 0.0f, 3 ); /* wasted */
170 }
171
172
173 /**
174 * Compute the tri->coef[] array dadx, dady, a0 values.
175 */
176 static void setup_tri_coefficients( struct setup_context *setup,
177 struct lp_rast_triangle *tri,
178 float oneoverarea,
179 const float (*v1)[4],
180 const float (*v2)[4],
181 const float (*v3)[4],
182 boolean frontface)
183 {
184 struct lp_scene *scene = lp_setup_get_current_scene(setup);
185 unsigned slot;
186
187 /* Allocate space for the a0, dadx and dady arrays
188 */
189 {
190 unsigned bytes = (setup->fs.nr_inputs + 1) * 4 * sizeof(float);
191 tri->inputs.a0 = lp_scene_alloc_aligned( scene, bytes, 16 );
192 tri->inputs.dadx = lp_scene_alloc_aligned( scene, bytes, 16 );
193 tri->inputs.dady = lp_scene_alloc_aligned( scene, bytes, 16 );
194 }
195
196 /* The internal position input is in slot zero:
197 */
198 setup_fragcoord_coef(tri, oneoverarea, 0, v1, v2, v3);
199
200 /* setup interpolation for all the remaining attributes:
201 */
202 for (slot = 0; slot < setup->fs.nr_inputs; slot++) {
203 unsigned vert_attr = setup->fs.input[slot].src_index;
204 unsigned i;
205
206 switch (setup->fs.input[slot].interp) {
207 case LP_INTERP_CONSTANT:
208 for (i = 0; i < NUM_CHANNELS; i++)
209 constant_coef(tri, slot+1, v3[vert_attr][i], i);
210 break;
211
212 case LP_INTERP_LINEAR:
213 for (i = 0; i < NUM_CHANNELS; i++)
214 linear_coef(tri, oneoverarea, slot+1, v1, v2, v3, vert_attr, i);
215 break;
216
217 case LP_INTERP_PERSPECTIVE:
218 for (i = 0; i < NUM_CHANNELS; i++)
219 perspective_coef(tri, oneoverarea, slot+1, v1, v2, v3, vert_attr, i);
220 break;
221
222 case LP_INTERP_POSITION:
223 /* XXX: fix me - duplicates the values in slot zero.
224 */
225 setup_fragcoord_coef(tri, oneoverarea, slot+1, v1, v2, v3);
226 break;
227
228 case LP_INTERP_FACING:
229 setup_facing_coef(tri, slot+1, frontface);
230 break;
231
232 default:
233 assert(0);
234 }
235 }
236 }
237
238
239
240 static inline int subpixel_snap( float a )
241 {
242 return util_iround(FIXED_ONE * a - (FIXED_ONE / 2));
243 }
244
245
246 /**
247 * Do basic setup for triangle rasterization and determine which
248 * framebuffer tiles are touched. Put the triangle in the scene's
249 * bins for the tiles which we overlap.
250 */
251 static void
252 do_triangle_ccw(struct setup_context *setup,
253 const float (*v1)[4],
254 const float (*v2)[4],
255 const float (*v3)[4],
256 boolean frontfacing )
257 {
258 /* x/y positions in fixed point */
259 const int x1 = subpixel_snap(v1[0][0]);
260 const int x2 = subpixel_snap(v2[0][0]);
261 const int x3 = subpixel_snap(v3[0][0]);
262 const int y1 = subpixel_snap(v1[0][1]);
263 const int y2 = subpixel_snap(v2[0][1]);
264 const int y3 = subpixel_snap(v3[0][1]);
265
266 struct lp_scene *scene = lp_setup_get_current_scene(setup);
267 struct lp_rast_triangle *tri = lp_scene_alloc_aligned( scene, sizeof *tri, 16 );
268 int area;
269 float oneoverarea;
270 int minx, maxx, miny, maxy;
271
272 tri->dx12 = x1 - x2;
273 tri->dx23 = x2 - x3;
274 tri->dx31 = x3 - x1;
275
276 tri->dy12 = y1 - y2;
277 tri->dy23 = y2 - y3;
278 tri->dy31 = y3 - y1;
279
280 area = (tri->dx12 * tri->dy31 - tri->dx31 * tri->dy12);
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, sizeof *tri );
290 LP_COUNT(nr_culled_tris);
291 return;
292 }
293
294 /* Bounding rectangle (in pixels) */
295 minx = (MIN3(x1, x2, x3) + (FIXED_ONE-1)) >> FIXED_ORDER;
296 maxx = (MAX3(x1, x2, x3) + (FIXED_ONE-1)) >> FIXED_ORDER;
297 miny = (MIN3(y1, y2, y3) + (FIXED_ONE-1)) >> FIXED_ORDER;
298 maxy = (MAX3(y1, y2, y3) + (FIXED_ONE-1)) >> FIXED_ORDER;
299
300 if (setup->scissor_test) {
301 minx = MAX2(minx, setup->scissor.current.minx);
302 maxx = MIN2(maxx, setup->scissor.current.maxx);
303 miny = MAX2(miny, setup->scissor.current.miny);
304 maxy = MIN2(maxy, setup->scissor.current.maxy);
305 }
306
307 if (miny == maxy ||
308 minx == maxx) {
309 lp_scene_putback_data( scene, sizeof *tri );
310 LP_COUNT(nr_culled_tris);
311 return;
312 }
313
314 /*
315 */
316 oneoverarea = ((float)FIXED_ONE) / (float)area;
317
318 /* Setup parameter interpolants:
319 */
320 setup_tri_coefficients( setup, tri, oneoverarea, v1, v2, v3, frontfacing );
321
322 /* half-edge constants, will be interated over the whole render target.
323 */
324 tri->c1 = tri->dy12 * x1 - tri->dx12 * y1;
325 tri->c2 = tri->dy23 * x2 - tri->dx23 * y2;
326 tri->c3 = tri->dy31 * x3 - tri->dx31 * y3;
327
328 /* correct for top-left fill convention:
329 */
330 if (tri->dy12 < 0 || (tri->dy12 == 0 && tri->dx12 > 0)) tri->c1++;
331 if (tri->dy23 < 0 || (tri->dy23 == 0 && tri->dx23 > 0)) tri->c2++;
332 if (tri->dy31 < 0 || (tri->dy31 == 0 && tri->dx31 > 0)) tri->c3++;
333
334 tri->dy12 *= FIXED_ONE;
335 tri->dy23 *= FIXED_ONE;
336 tri->dy31 *= FIXED_ONE;
337
338 tri->dx12 *= FIXED_ONE;
339 tri->dx23 *= FIXED_ONE;
340 tri->dx31 *= FIXED_ONE;
341
342 /* find trivial reject offsets for each edge for a single-pixel
343 * sized block. These will be scaled up at each recursive level to
344 * match the active blocksize. Scaling in this way works best if
345 * the blocks are square.
346 */
347 tri->eo1 = 0;
348 if (tri->dy12 < 0) tri->eo1 -= tri->dy12;
349 if (tri->dx12 > 0) tri->eo1 += tri->dx12;
350
351 tri->eo2 = 0;
352 if (tri->dy23 < 0) tri->eo2 -= tri->dy23;
353 if (tri->dx23 > 0) tri->eo2 += tri->dx23;
354
355 tri->eo3 = 0;
356 if (tri->dy31 < 0) tri->eo3 -= tri->dy31;
357 if (tri->dx31 > 0) tri->eo3 += tri->dx31;
358
359 /* Calculate trivial accept offsets from the above.
360 */
361 tri->ei1 = tri->dx12 - tri->dy12 - tri->eo1;
362 tri->ei2 = tri->dx23 - tri->dy23 - tri->eo2;
363 tri->ei3 = tri->dx31 - tri->dy31 - tri->eo3;
364
365 /* Fill in the inputs.step[][] arrays.
366 * We've manually unrolled some loops here.
367 */
368 {
369 const int xstep1 = -tri->dy12;
370 const int xstep2 = -tri->dy23;
371 const int xstep3 = -tri->dy31;
372 const int ystep1 = tri->dx12;
373 const int ystep2 = tri->dx23;
374 const int ystep3 = tri->dx31;
375
376 #define SETUP_STEP(i, x, y) \
377 do { \
378 tri->inputs.step[0][i] = x * xstep1 + y * ystep1; \
379 tri->inputs.step[1][i] = x * xstep2 + y * ystep2; \
380 tri->inputs.step[2][i] = x * xstep3 + y * ystep3; \
381 } while (0)
382
383 SETUP_STEP(0, 0, 0);
384 SETUP_STEP(1, 1, 0);
385 SETUP_STEP(2, 0, 1);
386 SETUP_STEP(3, 1, 1);
387
388 SETUP_STEP(4, 2, 0);
389 SETUP_STEP(5, 3, 0);
390 SETUP_STEP(6, 2, 1);
391 SETUP_STEP(7, 3, 1);
392
393 SETUP_STEP(8, 0, 2);
394 SETUP_STEP(9, 1, 2);
395 SETUP_STEP(10, 0, 3);
396 SETUP_STEP(11, 1, 3);
397
398 SETUP_STEP(12, 2, 2);
399 SETUP_STEP(13, 3, 2);
400 SETUP_STEP(14, 2, 3);
401 SETUP_STEP(15, 3, 3);
402 #undef STEP
403 }
404
405 /*
406 * All fields of 'tri' are now set. The remaining code here is
407 * concerned with binning.
408 */
409
410 /* Convert to tile coordinates:
411 */
412 minx = minx / TILE_SIZE;
413 miny = miny / TILE_SIZE;
414 maxx = maxx / TILE_SIZE;
415 maxy = maxy / TILE_SIZE;
416
417 /* Clamp maxx, maxy to framebuffer size
418 */
419 maxx = MIN2(maxx, scene->tiles_x - 1);
420 maxy = MIN2(maxy, scene->tiles_y - 1);
421
422 /* Determine which tile(s) intersect the triangle's bounding box
423 */
424 if (miny == maxy && minx == maxx)
425 {
426 /* Triangle is contained in a single tile:
427 */
428 lp_scene_bin_command( scene, minx, miny, lp_rast_triangle,
429 lp_rast_arg_triangle(tri) );
430 }
431 else
432 {
433 int c1 = (tri->c1 +
434 tri->dx12 * miny * TILE_SIZE -
435 tri->dy12 * minx * TILE_SIZE);
436 int c2 = (tri->c2 +
437 tri->dx23 * miny * TILE_SIZE -
438 tri->dy23 * minx * TILE_SIZE);
439 int c3 = (tri->c3 +
440 tri->dx31 * miny * TILE_SIZE -
441 tri->dy31 * minx * TILE_SIZE);
442
443 int ei1 = tri->ei1 << TILE_ORDER;
444 int ei2 = tri->ei2 << TILE_ORDER;
445 int ei3 = tri->ei3 << TILE_ORDER;
446
447 int eo1 = tri->eo1 << TILE_ORDER;
448 int eo2 = tri->eo2 << TILE_ORDER;
449 int eo3 = tri->eo3 << TILE_ORDER;
450
451 int xstep1 = -(tri->dy12 << TILE_ORDER);
452 int xstep2 = -(tri->dy23 << TILE_ORDER);
453 int xstep3 = -(tri->dy31 << TILE_ORDER);
454
455 int ystep1 = tri->dx12 << TILE_ORDER;
456 int ystep2 = tri->dx23 << TILE_ORDER;
457 int ystep3 = tri->dx31 << TILE_ORDER;
458 int x, y;
459
460
461 /* Test tile-sized blocks against the triangle.
462 * Discard blocks fully outside the tri. If the block is fully
463 * contained inside the tri, bin an lp_rast_shade_tile command.
464 * Else, bin a lp_rast_triangle command.
465 */
466 for (y = miny; y <= maxy; y++)
467 {
468 int cx1 = c1;
469 int cx2 = c2;
470 int cx3 = c3;
471 boolean in = FALSE; /* are we inside the triangle? */
472
473 for (x = minx; x <= maxx; x++)
474 {
475 if (cx1 + eo1 < 0 ||
476 cx2 + eo2 < 0 ||
477 cx3 + eo3 < 0)
478 {
479 /* do nothing */
480 LP_COUNT(nr_empty_64);
481 if (in)
482 break; /* exiting triangle, all done with this row */
483 }
484 else if (cx1 + ei1 > 0 &&
485 cx2 + ei2 > 0 &&
486 cx3 + ei3 > 0)
487 {
488 /* triangle covers the whole tile- shade whole tile */
489 LP_COUNT(nr_fully_covered_64);
490 in = TRUE;
491 if(setup->fs.current.opaque) {
492 lp_scene_bin_reset( scene, x, y );
493 lp_scene_bin_command( scene, x, y,
494 lp_rast_set_state,
495 lp_rast_arg_state(setup->fs.stored) );
496 }
497 lp_scene_bin_command( scene, x, y,
498 lp_rast_shade_tile,
499 lp_rast_arg_inputs(&tri->inputs) );
500 }
501 else
502 {
503 /* rasterizer/shade partial tile */
504 LP_COUNT(nr_partially_covered_64);
505 in = TRUE;
506 lp_scene_bin_command( scene, x, y,
507 lp_rast_triangle,
508 lp_rast_arg_triangle(tri) );
509 }
510
511 /* Iterate cx values across the region:
512 */
513 cx1 += xstep1;
514 cx2 += xstep2;
515 cx3 += xstep3;
516 }
517
518 /* Iterate c values down the region:
519 */
520 c1 += ystep1;
521 c2 += ystep2;
522 c3 += ystep3;
523 }
524 }
525 }
526
527
528 static void triangle_cw( struct setup_context *setup,
529 const float (*v0)[4],
530 const float (*v1)[4],
531 const float (*v2)[4] )
532 {
533 do_triangle_ccw( setup, v1, v0, v2, !setup->ccw_is_frontface );
534 }
535
536
537 static void triangle_ccw( struct setup_context *setup,
538 const float (*v0)[4],
539 const float (*v1)[4],
540 const float (*v2)[4] )
541 {
542 do_triangle_ccw( setup, v0, v1, v2, setup->ccw_is_frontface );
543 }
544
545
546 static void triangle_both( struct setup_context *setup,
547 const float (*v0)[4],
548 const float (*v1)[4],
549 const float (*v2)[4] )
550 {
551 /* edge vectors e = v0 - v2, f = v1 - v2 */
552 const float ex = v0[0][0] - v2[0][0];
553 const float ey = v0[0][1] - v2[0][1];
554 const float fx = v1[0][0] - v2[0][0];
555 const float fy = v1[0][1] - v2[0][1];
556
557 /* det = cross(e,f).z */
558 if (ex * fy - ey * fx < 0.0f)
559 triangle_ccw( setup, v0, v1, v2 );
560 else
561 triangle_cw( setup, v0, v1, v2 );
562 }
563
564
565 static void triangle_nop( struct setup_context *setup,
566 const float (*v0)[4],
567 const float (*v1)[4],
568 const float (*v2)[4] )
569 {
570 }
571
572
573 void
574 lp_setup_choose_triangle( struct setup_context *setup )
575 {
576 switch (setup->cullmode) {
577 case PIPE_WINDING_NONE:
578 setup->triangle = triangle_both;
579 break;
580 case PIPE_WINDING_CCW:
581 setup->triangle = triangle_cw;
582 break;
583 case PIPE_WINDING_CW:
584 setup->triangle = triangle_ccw;
585 break;
586 default:
587 setup->triangle = triangle_nop;
588 break;
589 }
590 }