041716adc939f50fa6f6324aa25a0e5bcac21ac9
[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
121 tri->inputs.dadx[slot][i] = dadx;
122 tri->inputs.dady[slot][i] = dady;
123 tri->inputs.a0[slot][i] = (a1 -
124 (dadx * (v1[0][0] - 0.5f) +
125 dady * (v1[0][1] - 0.5f)));
126 }
127
128
129 /**
130 * Special coefficient setup for gl_FragCoord.
131 * X and Y are trivial, though Y has to be inverted for OpenGL.
132 * Z and W are copied from position_coef which should have already been computed.
133 * We could do a bit less work if we'd examine gl_FragCoord's swizzle mask.
134 */
135 static void
136 setup_fragcoord_coef(struct lp_rast_triangle *tri,
137 unsigned slot,
138 const float (*v1)[4],
139 const float (*v2)[4],
140 const float (*v3)[4])
141 {
142 /*X*/
143 tri->inputs.a0[slot][0] = 0.0;
144 tri->inputs.dadx[slot][0] = 1.0;
145 tri->inputs.dady[slot][0] = 0.0;
146 /*Y*/
147 tri->inputs.a0[slot][1] = 0.0;
148 tri->inputs.dadx[slot][1] = 0.0;
149 tri->inputs.dady[slot][1] = 1.0;
150 /*Z*/
151 linear_coef(tri, slot, v1, v2, v3, 0, 2);
152 /*W*/
153 linear_coef(tri, slot, v1, v2, v3, 0, 3);
154 }
155
156
157 static void setup_facing_coef( struct lp_rast_triangle *tri,
158 unsigned slot,
159 boolean frontface )
160 {
161 constant_coef( tri, slot, 1.0f - frontface, 0 );
162 constant_coef( tri, slot, 0.0f, 1 ); /* wasted */
163 constant_coef( tri, slot, 0.0f, 2 ); /* wasted */
164 constant_coef( tri, slot, 0.0f, 3 ); /* wasted */
165 }
166
167
168 /**
169 * Compute the tri->coef[] array dadx, dady, a0 values.
170 */
171 static void setup_tri_coefficients( struct setup_context *setup,
172 struct lp_rast_triangle *tri,
173 const float (*v1)[4],
174 const float (*v2)[4],
175 const float (*v3)[4],
176 boolean frontface )
177 {
178 unsigned slot;
179
180 /* The internal position input is in slot zero:
181 */
182 setup_fragcoord_coef(tri, 0, v1, v2, v3);
183
184 /* setup interpolation for all the remaining attrbutes:
185 */
186 for (slot = 0; slot < setup->fs.nr_inputs; slot++) {
187 unsigned vert_attr = setup->fs.input[slot].src_index;
188 unsigned i;
189
190 switch (setup->fs.input[slot].interp) {
191 case LP_INTERP_CONSTANT:
192 for (i = 0; i < NUM_CHANNELS; i++)
193 constant_coef(tri, slot+1, v3[vert_attr][i], i);
194 break;
195
196 case LP_INTERP_LINEAR:
197 for (i = 0; i < NUM_CHANNELS; i++)
198 linear_coef(tri, slot+1, v1, v2, v3, vert_attr, i);
199 break;
200
201 case LP_INTERP_PERSPECTIVE:
202 for (i = 0; i < NUM_CHANNELS; i++)
203 perspective_coef(tri, slot+1, v1, v2, v3, vert_attr, i);
204 break;
205
206 case LP_INTERP_POSITION:
207 /* XXX: fix me - duplicates the values in slot zero.
208 */
209 setup_fragcoord_coef(tri, slot+1, v1, v2, v3);
210 break;
211
212 case LP_INTERP_FACING:
213 setup_facing_coef(tri, slot+1, frontface);
214 break;
215
216 default:
217 assert(0);
218 }
219 }
220 }
221
222
223
224 static inline int subpixel_snap( float a )
225 {
226 return util_iround(FIXED_ONE * a);
227 }
228
229
230 #define MIN3(a,b,c) MIN2(MIN2(a,b),c)
231 #define MAX3(a,b,c) MAX2(MAX2(a,b),c)
232
233 static void
234 do_triangle_ccw(struct setup_context *setup,
235 const float (*v1)[4],
236 const float (*v2)[4],
237 const float (*v3)[4],
238 boolean frontfacing )
239 {
240
241 const int y1 = subpixel_snap(v1[0][1]);
242 const int y2 = subpixel_snap(v2[0][1]);
243 const int y3 = subpixel_snap(v3[0][1]);
244
245 const int x1 = subpixel_snap(v1[0][0]);
246 const int x2 = subpixel_snap(v2[0][0]);
247 const int x3 = subpixel_snap(v3[0][0]);
248
249 struct lp_rast_triangle *tri = get_data( &setup->data, sizeof *tri );
250 float area;
251 int minx, maxx, miny, maxy;
252
253 tri->dx12 = x1 - x2;
254 tri->dx23 = x2 - x3;
255 tri->dx31 = x3 - x1;
256
257 tri->dy12 = y1 - y2;
258 tri->dy23 = y2 - y3;
259 tri->dy31 = y3 - y1;
260
261 area = (tri->dx12 * tri->dy31 -
262 tri->dx31 * tri->dy12);
263
264 /* Cull non-ccw and zero-sized triangles.
265 *
266 * XXX: subject to overflow??
267 */
268 if (area <= 0) {
269 putback_data( &setup->data, sizeof *tri );
270 return;
271 }
272
273 // Bounding rectangle
274 tri->minx = (MIN3(x1, x2, x3) + 0xf) >> FIXED_ORDER;
275 tri->maxx = (MAX3(x1, x2, x3) + 0xf) >> FIXED_ORDER;
276 tri->miny = (MIN3(y1, y2, y3) + 0xf) >> FIXED_ORDER;
277 tri->maxy = (MAX3(y1, y2, y3) + 0xf) >> FIXED_ORDER;
278
279 if (tri->miny == tri->maxy ||
280 tri->minx == tri->maxx) {
281 putback_data( &setup->data, sizeof *tri );
282 return;
283 }
284
285 tri->inputs.state = setup->fs.stored;
286
287 /*
288 */
289 tri->oneoverarea = ((float)FIXED_ONE) / (float)area;
290
291 /* Setup parameter interpolants:
292 */
293 setup_tri_coefficients( setup, tri, v1, v2, v3, frontfacing );
294
295 /* half-edge constants, will be interated over the whole
296 * rendertarget.
297 */
298 tri->c1 = tri->dy12 * x1 - tri->dx12 * y1;
299 tri->c2 = tri->dy23 * x2 - tri->dx23 * y2;
300 tri->c3 = tri->dy31 * x3 - tri->dx31 * y3;
301
302 /* correct for top-left fill convention:
303 */
304 if (tri->dy12 < 0 || (tri->dy12 == 0 && tri->dx12 > 0)) tri->c1++;
305 if (tri->dy23 < 0 || (tri->dy23 == 0 && tri->dx23 > 0)) tri->c2++;
306 if (tri->dy31 < 0 || (tri->dy31 == 0 && tri->dx31 > 0)) tri->c3++;
307
308 tri->dy12 *= FIXED_ONE;
309 tri->dy23 *= FIXED_ONE;
310 tri->dy31 *= FIXED_ONE;
311
312 tri->dx12 *= FIXED_ONE;
313 tri->dx23 *= FIXED_ONE;
314 tri->dx31 *= FIXED_ONE;
315
316 /* find trivial reject offsets for each edge for a single-pixel
317 * sized block. These will be scaled up at each recursive level to
318 * match the active blocksize. Scaling in this way works best if
319 * the blocks are square.
320 */
321 tri->eo1 = 0;
322 if (tri->dy12 < 0) tri->eo1 -= tri->dy12;
323 if (tri->dx12 > 0) tri->eo1 += tri->dx12;
324
325 tri->eo2 = 0;
326 if (tri->dy23 < 0) tri->eo2 -= tri->dy23;
327 if (tri->dx23 > 0) tri->eo2 += tri->dx23;
328
329 tri->eo3 = 0;
330 if (tri->dy31 < 0) tri->eo3 -= tri->dy31;
331 if (tri->dx31 > 0) tri->eo3 += tri->dx31;
332
333 /* Calculate trivial accept offsets from the above.
334 */
335 tri->ei1 = tri->dx12 - tri->dy12 - tri->eo1;
336 tri->ei2 = tri->dx23 - tri->dy23 - tri->eo2;
337 tri->ei3 = tri->dx31 - tri->dy31 - tri->eo3;
338
339 {
340 int xstep1 = -tri->dy12;
341 int xstep2 = -tri->dy23;
342 int xstep3 = -tri->dy31;
343
344 int ystep1 = tri->dx12;
345 int ystep2 = tri->dx23;
346 int ystep3 = tri->dx31;
347
348 int ix, iy;
349 int i = 0;
350
351 int c1 = 0;
352 int c2 = 0;
353 int c3 = 0;
354
355 for (iy = 0; iy < 4; iy++) {
356 int cx1 = c1;
357 int cx2 = c2;
358 int cx3 = c3;
359
360 for (ix = 0; ix < 4; ix++, i++) {
361 tri->step[0][i] = cx1;
362 tri->step[1][i] = cx2;
363 tri->step[2][i] = cx3;
364 cx1 += xstep1;
365 cx2 += xstep2;
366 cx3 += xstep3;
367 }
368
369 c1 += ystep1;
370 c2 += ystep2;
371 c3 += ystep3;
372 }
373 }
374
375 minx = tri->minx / TILESIZE;
376 miny = tri->miny / TILESIZE;
377 maxx = tri->maxx / TILESIZE;
378 maxy = tri->maxy / TILESIZE;
379
380
381 /* Convert to tile coordinates:
382 */
383 if (miny == maxy && minx == maxx)
384 {
385 /* Triangle is contained in a single tile:
386 */
387 bin_command( &setup->tile[minx][miny], lp_rast_triangle,
388 lp_rast_arg_triangle(tri) );
389 }
390 else
391 {
392 int c1 = (tri->c1 +
393 tri->dx12 * miny * TILESIZE -
394 tri->dy12 * minx * TILESIZE);
395 int c2 = (tri->c2 +
396 tri->dx23 * miny * TILESIZE -
397 tri->dy23 * minx * TILESIZE);
398 int c3 = (tri->c3 +
399 tri->dx31 * miny * TILESIZE -
400 tri->dy31 * minx * TILESIZE);
401
402 int ei1 = tri->ei1 << TILE_ORDER;
403 int ei2 = tri->ei2 << TILE_ORDER;
404 int ei3 = tri->ei3 << TILE_ORDER;
405
406 int eo1 = tri->eo1 << TILE_ORDER;
407 int eo2 = tri->eo2 << TILE_ORDER;
408 int eo3 = tri->eo3 << TILE_ORDER;
409
410 int xstep1 = -(tri->dy12 << TILE_ORDER);
411 int xstep2 = -(tri->dy23 << TILE_ORDER);
412 int xstep3 = -(tri->dy31 << TILE_ORDER);
413
414 int ystep1 = tri->dx12 << TILE_ORDER;
415 int ystep2 = tri->dx23 << TILE_ORDER;
416 int ystep3 = tri->dx31 << TILE_ORDER;
417 int x, y;
418
419
420 /* Trivially accept or reject blocks, else jump to per-pixel
421 * examination above.
422 */
423 for (y = miny; y <= maxy; y++)
424 {
425 int cx1 = c1;
426 int cx2 = c2;
427 int cx3 = c3;
428 int in = 0;
429
430 for (x = minx; x <= maxx; x++)
431 {
432 if (cx1 + eo1 < 0 ||
433 cx2 + eo2 < 0 ||
434 cx3 + eo3 < 0)
435 {
436 /* do nothing */
437 if (in)
438 break;
439 }
440 else if (cx1 + ei1 > 0 &&
441 cx2 + ei2 > 0 &&
442 cx3 + ei3 > 0)
443 {
444 in = 1;
445 /* shade whole tile */
446 bin_command( &setup->tile[x][y], lp_rast_shade_tile,
447 lp_rast_arg_inputs(&tri->inputs) );
448 }
449 else
450 {
451 in = 1;
452 /* shade partial tile */
453 bin_command( &setup->tile[x][y],
454 lp_rast_triangle,
455 lp_rast_arg_triangle(tri) );
456 }
457
458 /* Iterate cx values across the region:
459 */
460 cx1 += xstep1;
461 cx2 += xstep2;
462 cx3 += xstep3;
463 }
464
465 /* Iterate c values down the region:
466 */
467 c1 += ystep1;
468 c2 += ystep2;
469 c3 += ystep3;
470 }
471 }
472 }
473
474 static void triangle_cw( struct setup_context *setup,
475 const float (*v0)[4],
476 const float (*v1)[4],
477 const float (*v2)[4] )
478 {
479 do_triangle_ccw( setup, v1, v0, v2, !setup->ccw_is_frontface );
480 }
481
482 static void triangle_ccw( struct setup_context *setup,
483 const float (*v0)[4],
484 const float (*v1)[4],
485 const float (*v2)[4] )
486 {
487 do_triangle_ccw( setup, v0, v1, v2, setup->ccw_is_frontface );
488 }
489
490 static void triangle_both( struct setup_context *setup,
491 const float (*v0)[4],
492 const float (*v1)[4],
493 const float (*v2)[4] )
494 {
495 /* edge vectors e = v0 - v2, f = v1 - v2 */
496 const float ex = v0[0][0] - v2[0][0];
497 const float ey = v0[0][1] - v2[0][1];
498 const float fx = v1[0][0] - v2[0][0];
499 const float fy = v1[0][1] - v2[0][1];
500
501 /* det = cross(e,f).z */
502 if (ex * fy - ey * fx < 0)
503 triangle_ccw( setup, v0, v1, v2 );
504 else
505 triangle_cw( setup, v0, v1, v2 );
506 }
507
508 static void triangle_nop( struct setup_context *setup,
509 const float (*v0)[4],
510 const float (*v1)[4],
511 const float (*v2)[4] )
512 {
513 }
514
515
516 void
517 lp_setup_choose_triangle( struct setup_context *setup )
518 {
519 switch (setup->cullmode) {
520 case PIPE_WINDING_NONE:
521 setup->triangle = triangle_both;
522 break;
523 case PIPE_WINDING_CCW:
524 setup->triangle = triangle_cw;
525 break;
526 case PIPE_WINDING_CW:
527 setup->triangle = triangle_ccw;
528 break;
529 default:
530 setup->triangle = triangle_nop;
531 break;
532 }
533 }
534
535