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