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