Merge branch 'mesa_7_6_branch'
[mesa.git] / src / gallium / drivers / softpipe / sp_setup.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 * \brief Primitive rasterization/rendering (points, lines, triangles)
30 *
31 * \author Keith Whitwell <keith@tungstengraphics.com>
32 * \author Brian Paul
33 */
34
35 #include "sp_context.h"
36 #include "sp_quad.h"
37 #include "sp_quad_pipe.h"
38 #include "sp_setup.h"
39 #include "sp_state.h"
40 #include "draw/draw_context.h"
41 #include "draw/draw_private.h"
42 #include "draw/draw_vertex.h"
43 #include "pipe/p_shader_tokens.h"
44 #include "pipe/p_thread.h"
45 #include "util/u_math.h"
46 #include "util/u_memory.h"
47
48
49 #define DEBUG_VERTS 0
50 #define DEBUG_FRAGS 0
51
52 /**
53 * Triangle edge info
54 */
55 struct edge {
56 float dx; /**< X(v1) - X(v0), used only during setup */
57 float dy; /**< Y(v1) - Y(v0), used only during setup */
58 float dxdy; /**< dx/dy */
59 float sx, sy; /**< first sample point coord */
60 int lines; /**< number of lines on this edge */
61 };
62
63
64 #define MAX_QUADS 16
65
66
67 /**
68 * Triangle setup info (derived from draw_stage).
69 * Also used for line drawing (taking some liberties).
70 */
71 struct setup_context {
72 struct softpipe_context *softpipe;
73
74 /* Vertices are just an array of floats making up each attribute in
75 * turn. Currently fixed at 4 floats, but should change in time.
76 * Codegen will help cope with this.
77 */
78 const float (*vmax)[4];
79 const float (*vmid)[4];
80 const float (*vmin)[4];
81 const float (*vprovoke)[4];
82
83 struct edge ebot;
84 struct edge etop;
85 struct edge emaj;
86
87 float oneoverarea;
88 int facing;
89
90 struct quad_header quad[MAX_QUADS];
91 struct quad_header *quad_ptrs[MAX_QUADS];
92 unsigned count;
93
94 struct tgsi_interp_coef coef[PIPE_MAX_SHADER_INPUTS];
95 struct tgsi_interp_coef posCoef; /* For Z, W */
96
97 struct {
98 int left[2]; /**< [0] = row0, [1] = row1 */
99 int right[2];
100 int y;
101 } span;
102
103 #if DEBUG_FRAGS
104 uint numFragsEmitted; /**< per primitive */
105 uint numFragsWritten; /**< per primitive */
106 #endif
107
108 unsigned winding; /* which winding to cull */
109 };
110
111
112
113
114 /**
115 * Do triangle cull test using tri determinant (sign indicates orientation)
116 * \return true if triangle is to be culled.
117 */
118 static INLINE boolean
119 cull_tri(const struct setup_context *setup, float det)
120 {
121 if (det != 0) {
122 /* if (det < 0 then Z points toward camera and triangle is
123 * counter-clockwise winding.
124 */
125 unsigned winding = (det < 0) ? PIPE_WINDING_CCW : PIPE_WINDING_CW;
126
127 if ((winding & setup->winding) == 0)
128 return FALSE;
129 }
130
131 /* Culled:
132 */
133 return TRUE;
134 }
135
136
137
138 /**
139 * Clip setup->quad against the scissor/surface bounds.
140 */
141 static INLINE void
142 quad_clip( struct setup_context *setup, struct quad_header *quad )
143 {
144 const struct pipe_scissor_state *cliprect = &setup->softpipe->cliprect;
145 const int minx = (int) cliprect->minx;
146 const int maxx = (int) cliprect->maxx;
147 const int miny = (int) cliprect->miny;
148 const int maxy = (int) cliprect->maxy;
149
150 if (quad->input.x0 >= maxx ||
151 quad->input.y0 >= maxy ||
152 quad->input.x0 + 1 < minx ||
153 quad->input.y0 + 1 < miny) {
154 /* totally clipped */
155 quad->inout.mask = 0x0;
156 return;
157 }
158 if (quad->input.x0 < minx)
159 quad->inout.mask &= (MASK_BOTTOM_RIGHT | MASK_TOP_RIGHT);
160 if (quad->input.y0 < miny)
161 quad->inout.mask &= (MASK_BOTTOM_LEFT | MASK_BOTTOM_RIGHT);
162 if (quad->input.x0 == maxx - 1)
163 quad->inout.mask &= (MASK_BOTTOM_LEFT | MASK_TOP_LEFT);
164 if (quad->input.y0 == maxy - 1)
165 quad->inout.mask &= (MASK_TOP_LEFT | MASK_TOP_RIGHT);
166 }
167
168
169 /**
170 * Emit a quad (pass to next stage) with clipping.
171 */
172 static INLINE void
173 clip_emit_quad( struct setup_context *setup, struct quad_header *quad )
174 {
175 quad_clip( setup, quad );
176
177 if (quad->inout.mask) {
178 struct softpipe_context *sp = setup->softpipe;
179
180 sp->quad.first->run( sp->quad.first, &quad, 1 );
181 }
182 }
183
184
185
186 /**
187 * Given an X or Y coordinate, return the block/quad coordinate that it
188 * belongs to.
189 */
190 static INLINE int block( int x )
191 {
192 return x & ~(2-1);
193 }
194
195 static INLINE int block_x( int x )
196 {
197 return x & ~(16-1);
198 }
199
200
201 /**
202 * Render a horizontal span of quads
203 */
204 static void flush_spans( struct setup_context *setup )
205 {
206 const int step = 16;
207 const int xleft0 = setup->span.left[0];
208 const int xleft1 = setup->span.left[1];
209 const int xright0 = setup->span.right[0];
210 const int xright1 = setup->span.right[1];
211 struct quad_stage *pipe = setup->softpipe->quad.first;
212
213
214 int minleft = block_x(MIN2(xleft0, xleft1));
215 int maxright = MAX2(xright0, xright1);
216 int x;
217
218 for (x = minleft; x < maxright; x += step) {
219 unsigned skip_left0 = CLAMP(xleft0 - x, 0, step);
220 unsigned skip_left1 = CLAMP(xleft1 - x, 0, step);
221 unsigned skip_right0 = CLAMP(x + step - xright0, 0, step);
222 unsigned skip_right1 = CLAMP(x + step - xright1, 0, step);
223 unsigned lx = x;
224 unsigned q = 0;
225
226 unsigned skipmask_left0 = (1U << skip_left0) - 1U;
227 unsigned skipmask_left1 = (1U << skip_left1) - 1U;
228
229 /* These calculations fail when step == 32 and skip_right == 0.
230 */
231 unsigned skipmask_right0 = ~0U << (unsigned)(step - skip_right0);
232 unsigned skipmask_right1 = ~0U << (unsigned)(step - skip_right1);
233
234 unsigned mask0 = ~skipmask_left0 & ~skipmask_right0;
235 unsigned mask1 = ~skipmask_left1 & ~skipmask_right1;
236
237 if (mask0 | mask1) {
238 do {
239 unsigned quadmask = (mask0 & 3) | ((mask1 & 3) << 2);
240 if (quadmask) {
241 setup->quad[q].input.x0 = lx;
242 setup->quad[q].input.y0 = setup->span.y;
243 setup->quad[q].input.facing = setup->facing;
244 setup->quad[q].inout.mask = quadmask;
245 setup->quad_ptrs[q] = &setup->quad[q];
246 q++;
247 }
248 mask0 >>= 2;
249 mask1 >>= 2;
250 lx += 2;
251 } while (mask0 | mask1);
252
253 pipe->run( pipe, setup->quad_ptrs, q );
254 }
255 }
256
257
258 setup->span.y = 0;
259 setup->span.right[0] = 0;
260 setup->span.right[1] = 0;
261 setup->span.left[0] = 1000000; /* greater than right[0] */
262 setup->span.left[1] = 1000000; /* greater than right[1] */
263 }
264
265
266 #if DEBUG_VERTS
267 static void print_vertex(const struct setup_context *setup,
268 const float (*v)[4])
269 {
270 int i;
271 debug_printf(" Vertex: (%p)\n", v);
272 for (i = 0; i < setup->quad[0].nr_attrs; i++) {
273 debug_printf(" %d: %f %f %f %f\n", i,
274 v[i][0], v[i][1], v[i][2], v[i][3]);
275 if (util_is_inf_or_nan(v[i][0])) {
276 debug_printf(" NaN!\n");
277 }
278 }
279 }
280 #endif
281
282 /**
283 * Sort the vertices from top to bottom order, setting up the triangle
284 * edge fields (ebot, emaj, etop).
285 * \return FALSE if coords are inf/nan (cull the tri), TRUE otherwise
286 */
287 static boolean setup_sort_vertices( struct setup_context *setup,
288 float det,
289 const float (*v0)[4],
290 const float (*v1)[4],
291 const float (*v2)[4] )
292 {
293 setup->vprovoke = v2;
294
295 /* determine bottom to top order of vertices */
296 {
297 float y0 = v0[0][1];
298 float y1 = v1[0][1];
299 float y2 = v2[0][1];
300 if (y0 <= y1) {
301 if (y1 <= y2) {
302 /* y0<=y1<=y2 */
303 setup->vmin = v0;
304 setup->vmid = v1;
305 setup->vmax = v2;
306 }
307 else if (y2 <= y0) {
308 /* y2<=y0<=y1 */
309 setup->vmin = v2;
310 setup->vmid = v0;
311 setup->vmax = v1;
312 }
313 else {
314 /* y0<=y2<=y1 */
315 setup->vmin = v0;
316 setup->vmid = v2;
317 setup->vmax = v1;
318 }
319 }
320 else {
321 if (y0 <= y2) {
322 /* y1<=y0<=y2 */
323 setup->vmin = v1;
324 setup->vmid = v0;
325 setup->vmax = v2;
326 }
327 else if (y2 <= y1) {
328 /* y2<=y1<=y0 */
329 setup->vmin = v2;
330 setup->vmid = v1;
331 setup->vmax = v0;
332 }
333 else {
334 /* y1<=y2<=y0 */
335 setup->vmin = v1;
336 setup->vmid = v2;
337 setup->vmax = v0;
338 }
339 }
340 }
341
342 setup->ebot.dx = setup->vmid[0][0] - setup->vmin[0][0];
343 setup->ebot.dy = setup->vmid[0][1] - setup->vmin[0][1];
344 setup->emaj.dx = setup->vmax[0][0] - setup->vmin[0][0];
345 setup->emaj.dy = setup->vmax[0][1] - setup->vmin[0][1];
346 setup->etop.dx = setup->vmax[0][0] - setup->vmid[0][0];
347 setup->etop.dy = setup->vmax[0][1] - setup->vmid[0][1];
348
349 /*
350 * Compute triangle's area. Use 1/area to compute partial
351 * derivatives of attributes later.
352 *
353 * The area will be the same as prim->det, but the sign may be
354 * different depending on how the vertices get sorted above.
355 *
356 * To determine whether the primitive is front or back facing we
357 * use the prim->det value because its sign is correct.
358 */
359 {
360 const float area = (setup->emaj.dx * setup->ebot.dy -
361 setup->ebot.dx * setup->emaj.dy);
362
363 setup->oneoverarea = 1.0f / area;
364
365 /*
366 debug_printf("%s one-over-area %f area %f det %f\n",
367 __FUNCTION__, setup->oneoverarea, area, det );
368 */
369 if (util_is_inf_or_nan(setup->oneoverarea))
370 return FALSE;
371 }
372
373 /* We need to know if this is a front or back-facing triangle for:
374 * - the GLSL gl_FrontFacing fragment attribute (bool)
375 * - two-sided stencil test
376 */
377 setup->facing =
378 ((det > 0.0) ^
379 (setup->softpipe->rasterizer->front_winding == PIPE_WINDING_CW));
380
381 return TRUE;
382 }
383
384
385 /**
386 * Compute a0 for a constant-valued coefficient (GL_FLAT shading).
387 * The value value comes from vertex[slot][i].
388 * The result will be put into setup->coef[slot].a0[i].
389 * \param slot which attribute slot
390 * \param i which component of the slot (0..3)
391 */
392 static void const_coeff( struct setup_context *setup,
393 struct tgsi_interp_coef *coef,
394 uint vertSlot, uint i)
395 {
396 assert(i <= 3);
397
398 coef->dadx[i] = 0;
399 coef->dady[i] = 0;
400
401 /* need provoking vertex info!
402 */
403 coef->a0[i] = setup->vprovoke[vertSlot][i];
404 }
405
406
407 /**
408 * Compute a0, dadx and dady for a linearly interpolated coefficient,
409 * for a triangle.
410 */
411 static void tri_linear_coeff( struct setup_context *setup,
412 struct tgsi_interp_coef *coef,
413 uint vertSlot, uint i)
414 {
415 float botda = setup->vmid[vertSlot][i] - setup->vmin[vertSlot][i];
416 float majda = setup->vmax[vertSlot][i] - setup->vmin[vertSlot][i];
417 float a = setup->ebot.dy * majda - botda * setup->emaj.dy;
418 float b = setup->emaj.dx * botda - majda * setup->ebot.dx;
419 float dadx = a * setup->oneoverarea;
420 float dady = b * setup->oneoverarea;
421
422 assert(i <= 3);
423
424 coef->dadx[i] = dadx;
425 coef->dady[i] = dady;
426
427 /* calculate a0 as the value which would be sampled for the
428 * fragment at (0,0), taking into account that we want to sample at
429 * pixel centers, in other words (0.5, 0.5).
430 *
431 * this is neat but unfortunately not a good way to do things for
432 * triangles with very large values of dadx or dady as it will
433 * result in the subtraction and re-addition from a0 of a very
434 * large number, which means we'll end up loosing a lot of the
435 * fractional bits and precision from a0. the way to fix this is
436 * to define a0 as the sample at a pixel center somewhere near vmin
437 * instead - i'll switch to this later.
438 */
439 coef->a0[i] = (setup->vmin[vertSlot][i] -
440 (dadx * (setup->vmin[0][0] - 0.5f) +
441 dady * (setup->vmin[0][1] - 0.5f)));
442
443 /*
444 debug_printf("attr[%d].%c: %f dx:%f dy:%f\n",
445 slot, "xyzw"[i],
446 setup->coef[slot].a0[i],
447 setup->coef[slot].dadx[i],
448 setup->coef[slot].dady[i]);
449 */
450 }
451
452
453 /**
454 * Compute a0, dadx and dady for a perspective-corrected interpolant,
455 * for a triangle.
456 * We basically multiply the vertex value by 1/w before computing
457 * the plane coefficients (a0, dadx, dady).
458 * Later, when we compute the value at a particular fragment position we'll
459 * divide the interpolated value by the interpolated W at that fragment.
460 */
461 static void tri_persp_coeff( struct setup_context *setup,
462 struct tgsi_interp_coef *coef,
463 uint vertSlot, uint i)
464 {
465 /* premultiply by 1/w (v[0][3] is always W):
466 */
467 float mina = setup->vmin[vertSlot][i] * setup->vmin[0][3];
468 float mida = setup->vmid[vertSlot][i] * setup->vmid[0][3];
469 float maxa = setup->vmax[vertSlot][i] * setup->vmax[0][3];
470 float botda = mida - mina;
471 float majda = maxa - mina;
472 float a = setup->ebot.dy * majda - botda * setup->emaj.dy;
473 float b = setup->emaj.dx * botda - majda * setup->ebot.dx;
474 float dadx = a * setup->oneoverarea;
475 float dady = b * setup->oneoverarea;
476
477 /*
478 debug_printf("tri persp %d,%d: %f %f %f\n", vertSlot, i,
479 setup->vmin[vertSlot][i],
480 setup->vmid[vertSlot][i],
481 setup->vmax[vertSlot][i]
482 );
483 */
484 assert(i <= 3);
485
486 coef->dadx[i] = dadx;
487 coef->dady[i] = dady;
488 coef->a0[i] = (mina -
489 (dadx * (setup->vmin[0][0] - 0.5f) +
490 dady * (setup->vmin[0][1] - 0.5f)));
491 }
492
493
494 /**
495 * Special coefficient setup for gl_FragCoord.
496 * X and Y are trivial, though Y has to be inverted for OpenGL.
497 * Z and W are copied from posCoef which should have already been computed.
498 * We could do a bit less work if we'd examine gl_FragCoord's swizzle mask.
499 */
500 static void
501 setup_fragcoord_coeff(struct setup_context *setup, uint slot)
502 {
503 /*X*/
504 setup->coef[slot].a0[0] = 0;
505 setup->coef[slot].dadx[0] = 1.0;
506 setup->coef[slot].dady[0] = 0.0;
507 /*Y*/
508 setup->coef[slot].a0[1] = 0.0;
509 setup->coef[slot].dadx[1] = 0.0;
510 setup->coef[slot].dady[1] = 1.0;
511 /*Z*/
512 setup->coef[slot].a0[2] = setup->posCoef.a0[2];
513 setup->coef[slot].dadx[2] = setup->posCoef.dadx[2];
514 setup->coef[slot].dady[2] = setup->posCoef.dady[2];
515 /*W*/
516 setup->coef[slot].a0[3] = setup->posCoef.a0[3];
517 setup->coef[slot].dadx[3] = setup->posCoef.dadx[3];
518 setup->coef[slot].dady[3] = setup->posCoef.dady[3];
519 }
520
521
522
523 /**
524 * Compute the setup->coef[] array dadx, dady, a0 values.
525 * Must be called after setup->vmin,vmid,vmax,vprovoke are initialized.
526 */
527 static void setup_tri_coefficients( struct setup_context *setup )
528 {
529 struct softpipe_context *softpipe = setup->softpipe;
530 const struct sp_fragment_shader *spfs = softpipe->fs;
531 const struct vertex_info *vinfo = softpipe_get_vertex_info(softpipe);
532 uint fragSlot;
533
534 /* z and w are done by linear interpolation:
535 */
536 tri_linear_coeff(setup, &setup->posCoef, 0, 2);
537 tri_linear_coeff(setup, &setup->posCoef, 0, 3);
538
539 /* setup interpolation for all the remaining attributes:
540 */
541 for (fragSlot = 0; fragSlot < spfs->info.num_inputs; fragSlot++) {
542 const uint vertSlot = vinfo->attrib[fragSlot].src_index;
543 uint j;
544
545 switch (vinfo->attrib[fragSlot].interp_mode) {
546 case INTERP_CONSTANT:
547 for (j = 0; j < NUM_CHANNELS; j++)
548 const_coeff(setup, &setup->coef[fragSlot], vertSlot, j);
549 break;
550 case INTERP_LINEAR:
551 for (j = 0; j < NUM_CHANNELS; j++)
552 tri_linear_coeff(setup, &setup->coef[fragSlot], vertSlot, j);
553 break;
554 case INTERP_PERSPECTIVE:
555 for (j = 0; j < NUM_CHANNELS; j++)
556 tri_persp_coeff(setup, &setup->coef[fragSlot], vertSlot, j);
557 break;
558 case INTERP_POS:
559 setup_fragcoord_coeff(setup, fragSlot);
560 break;
561 default:
562 assert(0);
563 }
564
565 if (spfs->info.input_semantic_name[fragSlot] == TGSI_SEMANTIC_FACE) {
566 setup->coef[fragSlot].a0[0] = 1.0f - setup->facing;
567 setup->coef[fragSlot].dadx[0] = 0.0;
568 setup->coef[fragSlot].dady[0] = 0.0;
569 }
570 }
571 }
572
573
574
575 static void setup_tri_edges( struct setup_context *setup )
576 {
577 float vmin_x = setup->vmin[0][0] + 0.5f;
578 float vmid_x = setup->vmid[0][0] + 0.5f;
579
580 float vmin_y = setup->vmin[0][1] - 0.5f;
581 float vmid_y = setup->vmid[0][1] - 0.5f;
582 float vmax_y = setup->vmax[0][1] - 0.5f;
583
584 setup->emaj.sy = ceilf(vmin_y);
585 setup->emaj.lines = (int) ceilf(vmax_y - setup->emaj.sy);
586 setup->emaj.dxdy = setup->emaj.dx / setup->emaj.dy;
587 setup->emaj.sx = vmin_x + (setup->emaj.sy - vmin_y) * setup->emaj.dxdy;
588
589 setup->etop.sy = ceilf(vmid_y);
590 setup->etop.lines = (int) ceilf(vmax_y - setup->etop.sy);
591 setup->etop.dxdy = setup->etop.dx / setup->etop.dy;
592 setup->etop.sx = vmid_x + (setup->etop.sy - vmid_y) * setup->etop.dxdy;
593
594 setup->ebot.sy = ceilf(vmin_y);
595 setup->ebot.lines = (int) ceilf(vmid_y - setup->ebot.sy);
596 setup->ebot.dxdy = setup->ebot.dx / setup->ebot.dy;
597 setup->ebot.sx = vmin_x + (setup->ebot.sy - vmin_y) * setup->ebot.dxdy;
598 }
599
600
601 /**
602 * Render the upper or lower half of a triangle.
603 * Scissoring/cliprect is applied here too.
604 */
605 static void subtriangle( struct setup_context *setup,
606 struct edge *eleft,
607 struct edge *eright,
608 unsigned lines )
609 {
610 const struct pipe_scissor_state *cliprect = &setup->softpipe->cliprect;
611 const int minx = (int) cliprect->minx;
612 const int maxx = (int) cliprect->maxx;
613 const int miny = (int) cliprect->miny;
614 const int maxy = (int) cliprect->maxy;
615 int y, start_y, finish_y;
616 int sy = (int)eleft->sy;
617
618 assert((int)eleft->sy == (int) eright->sy);
619
620 /* clip top/bottom */
621 start_y = sy;
622 if (start_y < miny)
623 start_y = miny;
624
625 finish_y = sy + lines;
626 if (finish_y > maxy)
627 finish_y = maxy;
628
629 start_y -= sy;
630 finish_y -= sy;
631
632 /*
633 debug_printf("%s %d %d\n", __FUNCTION__, start_y, finish_y);
634 */
635
636 for (y = start_y; y < finish_y; y++) {
637
638 /* avoid accumulating adds as floats don't have the precision to
639 * accurately iterate large triangle edges that way. luckily we
640 * can just multiply these days.
641 *
642 * this is all drowned out by the attribute interpolation anyway.
643 */
644 int left = (int)(eleft->sx + y * eleft->dxdy);
645 int right = (int)(eright->sx + y * eright->dxdy);
646
647 /* clip left/right */
648 if (left < minx)
649 left = minx;
650 if (right > maxx)
651 right = maxx;
652
653 if (left < right) {
654 int _y = sy + y;
655 if (block(_y) != setup->span.y) {
656 flush_spans(setup);
657 setup->span.y = block(_y);
658 }
659
660 setup->span.left[_y&1] = left;
661 setup->span.right[_y&1] = right;
662 }
663 }
664
665
666 /* save the values so that emaj can be restarted:
667 */
668 eleft->sx += lines * eleft->dxdy;
669 eright->sx += lines * eright->dxdy;
670 eleft->sy += lines;
671 eright->sy += lines;
672 }
673
674
675 /**
676 * Recalculate prim's determinant. This is needed as we don't have
677 * get this information through the vbuf_render interface & we must
678 * calculate it here.
679 */
680 static float
681 calc_det( const float (*v0)[4],
682 const float (*v1)[4],
683 const float (*v2)[4] )
684 {
685 /* edge vectors e = v0 - v2, f = v1 - v2 */
686 const float ex = v0[0][0] - v2[0][0];
687 const float ey = v0[0][1] - v2[0][1];
688 const float fx = v1[0][0] - v2[0][0];
689 const float fy = v1[0][1] - v2[0][1];
690
691 /* det = cross(e,f).z */
692 return ex * fy - ey * fx;
693 }
694
695
696 /**
697 * Do setup for triangle rasterization, then render the triangle.
698 */
699 void setup_tri( struct setup_context *setup,
700 const float (*v0)[4],
701 const float (*v1)[4],
702 const float (*v2)[4] )
703 {
704 float det;
705
706 #if DEBUG_VERTS
707 debug_printf("Setup triangle:\n");
708 print_vertex(setup, v0);
709 print_vertex(setup, v1);
710 print_vertex(setup, v2);
711 #endif
712
713 if (setup->softpipe->no_rast)
714 return;
715
716 det = calc_det(v0, v1, v2);
717 /*
718 debug_printf("%s\n", __FUNCTION__ );
719 */
720
721 #if DEBUG_FRAGS
722 setup->numFragsEmitted = 0;
723 setup->numFragsWritten = 0;
724 #endif
725
726 if (cull_tri( setup, det ))
727 return;
728
729 if (!setup_sort_vertices( setup, det, v0, v1, v2 ))
730 return;
731 setup_tri_coefficients( setup );
732 setup_tri_edges( setup );
733
734 assert(setup->softpipe->reduced_prim == PIPE_PRIM_TRIANGLES);
735
736 setup->span.y = 0;
737 setup->span.right[0] = 0;
738 setup->span.right[1] = 0;
739 /* setup->span.z_mode = tri_z_mode( setup->ctx ); */
740
741 /* init_constant_attribs( setup ); */
742
743 if (setup->oneoverarea < 0.0) {
744 /* emaj on left:
745 */
746 subtriangle( setup, &setup->emaj, &setup->ebot, setup->ebot.lines );
747 subtriangle( setup, &setup->emaj, &setup->etop, setup->etop.lines );
748 }
749 else {
750 /* emaj on right:
751 */
752 subtriangle( setup, &setup->ebot, &setup->emaj, setup->ebot.lines );
753 subtriangle( setup, &setup->etop, &setup->emaj, setup->etop.lines );
754 }
755
756 flush_spans( setup );
757
758 #if DEBUG_FRAGS
759 printf("Tri: %u frags emitted, %u written\n",
760 setup->numFragsEmitted,
761 setup->numFragsWritten);
762 #endif
763 }
764
765
766
767 /**
768 * Compute a0, dadx and dady for a linearly interpolated coefficient,
769 * for a line.
770 */
771 static void
772 line_linear_coeff(const struct setup_context *setup,
773 struct tgsi_interp_coef *coef,
774 uint vertSlot, uint i)
775 {
776 const float da = setup->vmax[vertSlot][i] - setup->vmin[vertSlot][i];
777 const float dadx = da * setup->emaj.dx * setup->oneoverarea;
778 const float dady = da * setup->emaj.dy * setup->oneoverarea;
779 coef->dadx[i] = dadx;
780 coef->dady[i] = dady;
781 coef->a0[i] = (setup->vmin[vertSlot][i] -
782 (dadx * (setup->vmin[0][0] - 0.5f) +
783 dady * (setup->vmin[0][1] - 0.5f)));
784 }
785
786
787 /**
788 * Compute a0, dadx and dady for a perspective-corrected interpolant,
789 * for a line.
790 */
791 static void
792 line_persp_coeff(const struct setup_context *setup,
793 struct tgsi_interp_coef *coef,
794 uint vertSlot, uint i)
795 {
796 /* XXX double-check/verify this arithmetic */
797 const float a0 = setup->vmin[vertSlot][i] * setup->vmin[0][3];
798 const float a1 = setup->vmax[vertSlot][i] * setup->vmax[0][3];
799 const float da = a1 - a0;
800 const float dadx = da * setup->emaj.dx * setup->oneoverarea;
801 const float dady = da * setup->emaj.dy * setup->oneoverarea;
802 coef->dadx[i] = dadx;
803 coef->dady[i] = dady;
804 coef->a0[i] = (setup->vmin[vertSlot][i] -
805 (dadx * (setup->vmin[0][0] - 0.5f) +
806 dady * (setup->vmin[0][1] - 0.5f)));
807 }
808
809
810 /**
811 * Compute the setup->coef[] array dadx, dady, a0 values.
812 * Must be called after setup->vmin,vmax are initialized.
813 */
814 static INLINE boolean
815 setup_line_coefficients(struct setup_context *setup,
816 const float (*v0)[4],
817 const float (*v1)[4])
818 {
819 struct softpipe_context *softpipe = setup->softpipe;
820 const struct sp_fragment_shader *spfs = softpipe->fs;
821 const struct vertex_info *vinfo = softpipe_get_vertex_info(softpipe);
822 uint fragSlot;
823 float area;
824
825 /* use setup->vmin, vmax to point to vertices */
826 if (softpipe->rasterizer->flatshade_first)
827 setup->vprovoke = v0;
828 else
829 setup->vprovoke = v1;
830 setup->vmin = v0;
831 setup->vmax = v1;
832
833 setup->emaj.dx = setup->vmax[0][0] - setup->vmin[0][0];
834 setup->emaj.dy = setup->vmax[0][1] - setup->vmin[0][1];
835
836 /* NOTE: this is not really area but something proportional to it */
837 area = setup->emaj.dx * setup->emaj.dx + setup->emaj.dy * setup->emaj.dy;
838 if (area == 0.0f || util_is_inf_or_nan(area))
839 return FALSE;
840 setup->oneoverarea = 1.0f / area;
841
842 /* z and w are done by linear interpolation:
843 */
844 line_linear_coeff(setup, &setup->posCoef, 0, 2);
845 line_linear_coeff(setup, &setup->posCoef, 0, 3);
846
847 /* setup interpolation for all the remaining attributes:
848 */
849 for (fragSlot = 0; fragSlot < spfs->info.num_inputs; fragSlot++) {
850 const uint vertSlot = vinfo->attrib[fragSlot].src_index;
851 uint j;
852
853 switch (vinfo->attrib[fragSlot].interp_mode) {
854 case INTERP_CONSTANT:
855 for (j = 0; j < NUM_CHANNELS; j++)
856 const_coeff(setup, &setup->coef[fragSlot], vertSlot, j);
857 break;
858 case INTERP_LINEAR:
859 for (j = 0; j < NUM_CHANNELS; j++)
860 line_linear_coeff(setup, &setup->coef[fragSlot], vertSlot, j);
861 break;
862 case INTERP_PERSPECTIVE:
863 for (j = 0; j < NUM_CHANNELS; j++)
864 line_persp_coeff(setup, &setup->coef[fragSlot], vertSlot, j);
865 break;
866 case INTERP_POS:
867 setup_fragcoord_coeff(setup, fragSlot);
868 break;
869 default:
870 assert(0);
871 }
872
873 if (spfs->info.input_semantic_name[fragSlot] == TGSI_SEMANTIC_FACE) {
874 setup->coef[fragSlot].a0[0] = 1.0f - setup->facing;
875 setup->coef[fragSlot].dadx[0] = 0.0;
876 setup->coef[fragSlot].dady[0] = 0.0;
877 }
878 }
879 return TRUE;
880 }
881
882
883 /**
884 * Plot a pixel in a line segment.
885 */
886 static INLINE void
887 plot(struct setup_context *setup, int x, int y)
888 {
889 const int iy = y & 1;
890 const int ix = x & 1;
891 const int quadX = x - ix;
892 const int quadY = y - iy;
893 const int mask = (1 << ix) << (2 * iy);
894
895 if (quadX != setup->quad[0].input.x0 ||
896 quadY != setup->quad[0].input.y0)
897 {
898 /* flush prev quad, start new quad */
899
900 if (setup->quad[0].input.x0 != -1)
901 clip_emit_quad( setup, &setup->quad[0] );
902
903 setup->quad[0].input.x0 = quadX;
904 setup->quad[0].input.y0 = quadY;
905 setup->quad[0].inout.mask = 0x0;
906 }
907
908 setup->quad[0].inout.mask |= mask;
909 }
910
911
912 /**
913 * Do setup for line rasterization, then render the line.
914 * Single-pixel width, no stipple, etc. We rely on the 'draw' module
915 * to handle stippling and wide lines.
916 */
917 void
918 setup_line(struct setup_context *setup,
919 const float (*v0)[4],
920 const float (*v1)[4])
921 {
922 int x0 = (int) v0[0][0];
923 int x1 = (int) v1[0][0];
924 int y0 = (int) v0[0][1];
925 int y1 = (int) v1[0][1];
926 int dx = x1 - x0;
927 int dy = y1 - y0;
928 int xstep, ystep;
929
930 #if DEBUG_VERTS
931 debug_printf("Setup line:\n");
932 print_vertex(setup, v0);
933 print_vertex(setup, v1);
934 #endif
935
936 if (setup->softpipe->no_rast)
937 return;
938
939 if (dx == 0 && dy == 0)
940 return;
941
942 if (!setup_line_coefficients(setup, v0, v1))
943 return;
944
945 assert(v0[0][0] < 1.0e9);
946 assert(v0[0][1] < 1.0e9);
947 assert(v1[0][0] < 1.0e9);
948 assert(v1[0][1] < 1.0e9);
949
950 if (dx < 0) {
951 dx = -dx; /* make positive */
952 xstep = -1;
953 }
954 else {
955 xstep = 1;
956 }
957
958 if (dy < 0) {
959 dy = -dy; /* make positive */
960 ystep = -1;
961 }
962 else {
963 ystep = 1;
964 }
965
966 assert(dx >= 0);
967 assert(dy >= 0);
968 assert(setup->softpipe->reduced_prim == PIPE_PRIM_LINES);
969
970 setup->quad[0].input.x0 = setup->quad[0].input.y0 = -1;
971 setup->quad[0].inout.mask = 0x0;
972
973 /* XXX temporary: set coverage to 1.0 so the line appears
974 * if AA mode happens to be enabled.
975 */
976 setup->quad[0].input.coverage[0] =
977 setup->quad[0].input.coverage[1] =
978 setup->quad[0].input.coverage[2] =
979 setup->quad[0].input.coverage[3] = 1.0;
980
981 if (dx > dy) {
982 /*** X-major line ***/
983 int i;
984 const int errorInc = dy + dy;
985 int error = errorInc - dx;
986 const int errorDec = error - dx;
987
988 for (i = 0; i < dx; i++) {
989 plot(setup, x0, y0);
990
991 x0 += xstep;
992 if (error < 0) {
993 error += errorInc;
994 }
995 else {
996 error += errorDec;
997 y0 += ystep;
998 }
999 }
1000 }
1001 else {
1002 /*** Y-major line ***/
1003 int i;
1004 const int errorInc = dx + dx;
1005 int error = errorInc - dy;
1006 const int errorDec = error - dy;
1007
1008 for (i = 0; i < dy; i++) {
1009 plot(setup, x0, y0);
1010
1011 y0 += ystep;
1012 if (error < 0) {
1013 error += errorInc;
1014 }
1015 else {
1016 error += errorDec;
1017 x0 += xstep;
1018 }
1019 }
1020 }
1021
1022 /* draw final quad */
1023 if (setup->quad[0].inout.mask) {
1024 clip_emit_quad( setup, &setup->quad[0] );
1025 }
1026 }
1027
1028
1029 static void
1030 point_persp_coeff(const struct setup_context *setup,
1031 const float (*vert)[4],
1032 struct tgsi_interp_coef *coef,
1033 uint vertSlot, uint i)
1034 {
1035 assert(i <= 3);
1036 coef->dadx[i] = 0.0F;
1037 coef->dady[i] = 0.0F;
1038 coef->a0[i] = vert[vertSlot][i] * vert[0][3];
1039 }
1040
1041
1042 /**
1043 * Do setup for point rasterization, then render the point.
1044 * Round or square points...
1045 * XXX could optimize a lot for 1-pixel points.
1046 */
1047 void
1048 setup_point( struct setup_context *setup,
1049 const float (*v0)[4] )
1050 {
1051 struct softpipe_context *softpipe = setup->softpipe;
1052 const struct sp_fragment_shader *spfs = softpipe->fs;
1053 const int sizeAttr = setup->softpipe->psize_slot;
1054 const float size
1055 = sizeAttr > 0 ? v0[sizeAttr][0]
1056 : setup->softpipe->rasterizer->point_size;
1057 const float halfSize = 0.5F * size;
1058 const boolean round = (boolean) setup->softpipe->rasterizer->point_smooth;
1059 const float x = v0[0][0]; /* Note: data[0] is always position */
1060 const float y = v0[0][1];
1061 const struct vertex_info *vinfo = softpipe_get_vertex_info(softpipe);
1062 uint fragSlot;
1063
1064 #if DEBUG_VERTS
1065 debug_printf("Setup point:\n");
1066 print_vertex(setup, v0);
1067 #endif
1068
1069 if (softpipe->no_rast)
1070 return;
1071
1072 assert(setup->softpipe->reduced_prim == PIPE_PRIM_POINTS);
1073
1074 /* For points, all interpolants are constant-valued.
1075 * However, for point sprites, we'll need to setup texcoords appropriately.
1076 * XXX: which coefficients are the texcoords???
1077 * We may do point sprites as textured quads...
1078 *
1079 * KW: We don't know which coefficients are texcoords - ultimately
1080 * the choice of what interpolation mode to use for each attribute
1081 * should be determined by the fragment program, using
1082 * per-attribute declaration statements that include interpolation
1083 * mode as a parameter. So either the fragment program will have
1084 * to be adjusted for pointsprite vs normal point behaviour, or
1085 * otherwise a special interpolation mode will have to be defined
1086 * which matches the required behaviour for point sprites. But -
1087 * the latter is not a feature of normal hardware, and as such
1088 * probably should be ruled out on that basis.
1089 */
1090 setup->vprovoke = v0;
1091
1092 /* setup Z, W */
1093 const_coeff(setup, &setup->posCoef, 0, 2);
1094 const_coeff(setup, &setup->posCoef, 0, 3);
1095
1096 for (fragSlot = 0; fragSlot < spfs->info.num_inputs; fragSlot++) {
1097 const uint vertSlot = vinfo->attrib[fragSlot].src_index;
1098 uint j;
1099
1100 switch (vinfo->attrib[fragSlot].interp_mode) {
1101 case INTERP_CONSTANT:
1102 /* fall-through */
1103 case INTERP_LINEAR:
1104 for (j = 0; j < NUM_CHANNELS; j++)
1105 const_coeff(setup, &setup->coef[fragSlot], vertSlot, j);
1106 break;
1107 case INTERP_PERSPECTIVE:
1108 for (j = 0; j < NUM_CHANNELS; j++)
1109 point_persp_coeff(setup, setup->vprovoke,
1110 &setup->coef[fragSlot], vertSlot, j);
1111 break;
1112 case INTERP_POS:
1113 setup_fragcoord_coeff(setup, fragSlot);
1114 break;
1115 default:
1116 assert(0);
1117 }
1118
1119 if (spfs->info.input_semantic_name[fragSlot] == TGSI_SEMANTIC_FACE) {
1120 setup->coef[fragSlot].a0[0] = 1.0f - setup->facing;
1121 setup->coef[fragSlot].dadx[0] = 0.0;
1122 setup->coef[fragSlot].dady[0] = 0.0;
1123 }
1124 }
1125
1126
1127 if (halfSize <= 0.5 && !round) {
1128 /* special case for 1-pixel points */
1129 const int ix = ((int) x) & 1;
1130 const int iy = ((int) y) & 1;
1131 setup->quad[0].input.x0 = (int) x - ix;
1132 setup->quad[0].input.y0 = (int) y - iy;
1133 setup->quad[0].inout.mask = (1 << ix) << (2 * iy);
1134 clip_emit_quad( setup, &setup->quad[0] );
1135 }
1136 else {
1137 if (round) {
1138 /* rounded points */
1139 const int ixmin = block((int) (x - halfSize));
1140 const int ixmax = block((int) (x + halfSize));
1141 const int iymin = block((int) (y - halfSize));
1142 const int iymax = block((int) (y + halfSize));
1143 const float rmin = halfSize - 0.7071F; /* 0.7071 = sqrt(2)/2 */
1144 const float rmax = halfSize + 0.7071F;
1145 const float rmin2 = MAX2(0.0F, rmin * rmin);
1146 const float rmax2 = rmax * rmax;
1147 const float cscale = 1.0F / (rmax2 - rmin2);
1148 int ix, iy;
1149
1150 for (iy = iymin; iy <= iymax; iy += 2) {
1151 for (ix = ixmin; ix <= ixmax; ix += 2) {
1152 float dx, dy, dist2, cover;
1153
1154 setup->quad[0].inout.mask = 0x0;
1155
1156 dx = (ix + 0.5f) - x;
1157 dy = (iy + 0.5f) - y;
1158 dist2 = dx * dx + dy * dy;
1159 if (dist2 <= rmax2) {
1160 cover = 1.0F - (dist2 - rmin2) * cscale;
1161 setup->quad[0].input.coverage[QUAD_TOP_LEFT] = MIN2(cover, 1.0f);
1162 setup->quad[0].inout.mask |= MASK_TOP_LEFT;
1163 }
1164
1165 dx = (ix + 1.5f) - x;
1166 dy = (iy + 0.5f) - y;
1167 dist2 = dx * dx + dy * dy;
1168 if (dist2 <= rmax2) {
1169 cover = 1.0F - (dist2 - rmin2) * cscale;
1170 setup->quad[0].input.coverage[QUAD_TOP_RIGHT] = MIN2(cover, 1.0f);
1171 setup->quad[0].inout.mask |= MASK_TOP_RIGHT;
1172 }
1173
1174 dx = (ix + 0.5f) - x;
1175 dy = (iy + 1.5f) - y;
1176 dist2 = dx * dx + dy * dy;
1177 if (dist2 <= rmax2) {
1178 cover = 1.0F - (dist2 - rmin2) * cscale;
1179 setup->quad[0].input.coverage[QUAD_BOTTOM_LEFT] = MIN2(cover, 1.0f);
1180 setup->quad[0].inout.mask |= MASK_BOTTOM_LEFT;
1181 }
1182
1183 dx = (ix + 1.5f) - x;
1184 dy = (iy + 1.5f) - y;
1185 dist2 = dx * dx + dy * dy;
1186 if (dist2 <= rmax2) {
1187 cover = 1.0F - (dist2 - rmin2) * cscale;
1188 setup->quad[0].input.coverage[QUAD_BOTTOM_RIGHT] = MIN2(cover, 1.0f);
1189 setup->quad[0].inout.mask |= MASK_BOTTOM_RIGHT;
1190 }
1191
1192 if (setup->quad[0].inout.mask) {
1193 setup->quad[0].input.x0 = ix;
1194 setup->quad[0].input.y0 = iy;
1195 clip_emit_quad( setup, &setup->quad[0] );
1196 }
1197 }
1198 }
1199 }
1200 else {
1201 /* square points */
1202 const int xmin = (int) (x + 0.75 - halfSize);
1203 const int ymin = (int) (y + 0.25 - halfSize);
1204 const int xmax = xmin + (int) size;
1205 const int ymax = ymin + (int) size;
1206 /* XXX could apply scissor to xmin,ymin,xmax,ymax now */
1207 const int ixmin = block(xmin);
1208 const int ixmax = block(xmax - 1);
1209 const int iymin = block(ymin);
1210 const int iymax = block(ymax - 1);
1211 int ix, iy;
1212
1213 /*
1214 debug_printf("(%f, %f) -> X:%d..%d Y:%d..%d\n", x, y, xmin, xmax,ymin,ymax);
1215 */
1216 for (iy = iymin; iy <= iymax; iy += 2) {
1217 uint rowMask = 0xf;
1218 if (iy < ymin) {
1219 /* above the top edge */
1220 rowMask &= (MASK_BOTTOM_LEFT | MASK_BOTTOM_RIGHT);
1221 }
1222 if (iy + 1 >= ymax) {
1223 /* below the bottom edge */
1224 rowMask &= (MASK_TOP_LEFT | MASK_TOP_RIGHT);
1225 }
1226
1227 for (ix = ixmin; ix <= ixmax; ix += 2) {
1228 uint mask = rowMask;
1229
1230 if (ix < xmin) {
1231 /* fragment is past left edge of point, turn off left bits */
1232 mask &= (MASK_BOTTOM_RIGHT | MASK_TOP_RIGHT);
1233 }
1234 if (ix + 1 >= xmax) {
1235 /* past the right edge */
1236 mask &= (MASK_BOTTOM_LEFT | MASK_TOP_LEFT);
1237 }
1238
1239 setup->quad[0].inout.mask = mask;
1240 setup->quad[0].input.x0 = ix;
1241 setup->quad[0].input.y0 = iy;
1242 clip_emit_quad( setup, &setup->quad[0] );
1243 }
1244 }
1245 }
1246 }
1247 }
1248
1249 void setup_prepare( struct setup_context *setup )
1250 {
1251 struct softpipe_context *sp = setup->softpipe;
1252
1253 if (sp->dirty) {
1254 softpipe_update_derived(sp);
1255 }
1256
1257 sp->quad.first->begin( sp->quad.first );
1258
1259 if (sp->reduced_api_prim == PIPE_PRIM_TRIANGLES &&
1260 sp->rasterizer->fill_cw == PIPE_POLYGON_MODE_FILL &&
1261 sp->rasterizer->fill_ccw == PIPE_POLYGON_MODE_FILL) {
1262 /* we'll do culling */
1263 setup->winding = sp->rasterizer->cull_mode;
1264 }
1265 else {
1266 /* 'draw' will do culling */
1267 setup->winding = PIPE_WINDING_NONE;
1268 }
1269 }
1270
1271
1272
1273 void setup_destroy_context( struct setup_context *setup )
1274 {
1275 FREE( setup );
1276 }
1277
1278
1279 /**
1280 * Create a new primitive setup/render stage.
1281 */
1282 struct setup_context *setup_create_context( struct softpipe_context *softpipe )
1283 {
1284 struct setup_context *setup = CALLOC_STRUCT(setup_context);
1285 unsigned i;
1286
1287 setup->softpipe = softpipe;
1288
1289 for (i = 0; i < MAX_QUADS; i++) {
1290 setup->quad[i].coef = setup->coef;
1291 setup->quad[i].posCoef = &setup->posCoef;
1292 }
1293
1294 setup->span.left[0] = 1000000; /* greater than right[0] */
1295 setup->span.left[1] = 1000000; /* greater than right[1] */
1296
1297 return setup;
1298 }
1299